118c6dd5a0cc599ca2d36572eb86bcf92fec0e24
[fio.git] / engines / fusion-aw.c
1 /*
2  * Custom fio(1) engine that submits synchronous atomic writes to file.
3  *
4  * Copyright (C) 2012 Fusion-io, Inc.
5  * Author: Santhosh Kumar Koundinya.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; under version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License version
14  * 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License Version 2
17  * along with this program; if not see <http://www.gnu.org/licenses/>
18  */
19
20 #include <stdlib.h>
21 #include <stdint.h>
22
23 #include "../fio.h"
24
25 #ifdef FIO_HAVE_FUSION_AW
26
27 #include <vsl_dp_experimental/vectored_write.h>
28
29 /* Fix sector size to 512 bytes independent of actual sector size, just like
30  * the linux kernel. */
31 #define SECTOR_SHIFT    9
32 #define SECTOR_SIZE    (1U<<SECTOR_SHIFT)
33
34 struct acs_file_data {
35         struct vsl_iovec iov[IO_VECTOR_LIMIT];
36 };
37
38 static int queue(struct thread_data *td, struct io_u *io_u)
39 {
40         int rc;
41         int iov_index;
42         off_t offset;
43         char *xfer_buf;
44         size_t xfer_buflen;
45         struct acs_file_data *d = io_u->file->file_data;
46
47         if (io_u->ddir != DDIR_WRITE) {
48                 td_vmsg(td, -EIO, "only writes supported", "io_u->ddir");
49                 rc = -EIO;
50                 goto out;
51         }
52         if (io_u->xfer_buflen > IO_SIZE_MAX) {
53                 td_vmsg(td, -EIO, "data too big", "io_u->xfer_buflen");
54                 rc = -EIO;
55                 goto out;
56         }
57         if (io_u->xfer_buflen & (SECTOR_SIZE - 1)) {
58                 td_vmsg(td, -EIO, "unaligned data size", "io_u->xfer_buflen");
59                 rc = -EIO;
60                 goto out;
61         }
62
63         /* Chop up the write into minimal number of iovec's necessary */
64         iov_index = 0;
65         offset = io_u->offset;
66         xfer_buf = io_u->xfer_buf;
67         xfer_buflen = io_u->xfer_buflen;
68         while (xfer_buflen) {
69                 struct vsl_iovec *iov = &d->iov[iov_index++];
70
71                 iov->iov_len = xfer_buflen > IO_VECTOR_MAX_SIZE ?
72                     IO_VECTOR_MAX_SIZE : xfer_buflen;
73                 iov->iov_base = (uint64_t) xfer_buf;
74                 iov->sector = offset >> SECTOR_SHIFT;
75                 iov->iov_flag = VSL_IOV_WRITE;
76
77                 offset += iov->iov_len;
78                 xfer_buf += iov->iov_len;
79                 xfer_buflen -= iov->iov_len;
80         }
81         assert(xfer_buflen == 0);
82         assert(iov_index <= IO_VECTOR_LIMIT);
83
84         rc = vsl_vectored_write(io_u->file->fd, d->iov, iov_index, O_ATOMIC);
85         if (rc == -1) {
86                 td_verror(td, -errno, "vsl_vectored_write");
87                 rc = -EIO;
88                 goto out;
89         } else {
90                 io_u->error = 0;
91                 rc = FIO_Q_COMPLETED;
92         }
93
94 out:
95         if (rc < 0)
96                 io_u->error = rc;
97
98         return rc;
99 }
100
101 static int open_file(struct thread_data *td, struct fio_file *f)
102 {
103         int rc;
104         struct acs_file_data *d = NULL;
105
106         d = malloc(sizeof(*d));
107         if (!d) {
108                 td_verror(td, -ENOMEM, "malloc");
109                 rc = -ENOMEM;
110                 goto error;
111         }
112         f->file_data = d;
113
114         rc = generic_open_file(td, f);
115
116 out:
117         return rc;
118
119 error:
120         f->fd = -1;
121         f->file_data = NULL;
122         if (d)
123                 free(d);
124
125         goto out;
126 }
127
128 static int close_file(struct thread_data *td, struct fio_file *f)
129 {
130         if (f->file_data) {
131                 free(f->file_data);
132                 f->file_data = NULL;
133         }
134
135         return generic_close_file(td, f);
136 }
137
138 static struct ioengine_ops ioengine = {
139         .name = "fusion-aw-sync",
140         .version = FIO_IOOPS_VERSION,
141         .queue = queue,
142         .open_file = open_file,
143         .close_file = close_file,
144         .get_file_size = generic_get_file_size,
145         .flags = FIO_SYNCIO | FIO_RAWIO | FIO_MEMALIGN
146 };
147
148 #else /* !FUSION_HAVE_FUSION_AW */
149
150 static int fio_fusion_aw_eng_init(struct thread_data fio_unused *td)
151 {
152         log_err("fio: fusion atomic write engine not available\n");
153         return 1;
154 }
155
156 static struct ioengine_ops ioengine = {
157         .name           = "fusion-aw-sync",
158         .version        = FIO_IOOPS_VERSION,
159         .init           = fio_fusion_aw_eng_init,
160 };
161
162 #endif /* FUSION_HAVE_FUSION_AW */
163
164 static void fio_init fio_fusion_aw_init(void)
165 {
166         register_ioengine(&ioengine);
167 }
168
169 static void fio_exit fio_fusion_aw_exit(void)
170 {
171         unregister_ioengine(&ioengine);
172 }