Add configure script
[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 #include <vsl_dp_experimental/vectored_write.h>
26
27 /* Fix sector size to 512 bytes independent of actual sector size, just like
28  * the linux kernel. */
29 #define SECTOR_SHIFT    9
30 #define SECTOR_SIZE    (1U<<SECTOR_SHIFT)
31
32 struct acs_file_data {
33         struct vsl_iovec iov[IO_VECTOR_LIMIT];
34 };
35
36 static int queue(struct thread_data *td, struct io_u *io_u)
37 {
38         int rc;
39         int iov_index;
40         off_t offset;
41         char *xfer_buf;
42         size_t xfer_buflen;
43         struct acs_file_data *d = io_u->file->file_data;
44
45         if (io_u->ddir != DDIR_WRITE) {
46                 td_vmsg(td, -EIO, "only writes supported", "io_u->ddir");
47                 rc = -EIO;
48                 goto out;
49         }
50         if (io_u->xfer_buflen > IO_SIZE_MAX) {
51                 td_vmsg(td, -EIO, "data too big", "io_u->xfer_buflen");
52                 rc = -EIO;
53                 goto out;
54         }
55         if (io_u->xfer_buflen & (SECTOR_SIZE - 1)) {
56                 td_vmsg(td, -EIO, "unaligned data size", "io_u->xfer_buflen");
57                 rc = -EIO;
58                 goto out;
59         }
60
61         /* Chop up the write into minimal number of iovec's necessary */
62         iov_index = 0;
63         offset = io_u->offset;
64         xfer_buf = io_u->xfer_buf;
65         xfer_buflen = io_u->xfer_buflen;
66         while (xfer_buflen) {
67                 struct vsl_iovec *iov = &d->iov[iov_index++];
68
69                 iov->iov_len = xfer_buflen > IO_VECTOR_MAX_SIZE ?
70                     IO_VECTOR_MAX_SIZE : xfer_buflen;
71                 iov->iov_base = (uint64_t) xfer_buf;
72                 iov->sector = offset >> SECTOR_SHIFT;
73                 iov->iov_flag = VSL_IOV_WRITE;
74
75                 offset += iov->iov_len;
76                 xfer_buf += iov->iov_len;
77                 xfer_buflen -= iov->iov_len;
78         }
79         assert(xfer_buflen == 0);
80         assert(iov_index <= IO_VECTOR_LIMIT);
81
82         rc = vsl_vectored_write(io_u->file->fd, d->iov, iov_index, O_ATOMIC);
83         if (rc == -1) {
84                 td_verror(td, -errno, "vsl_vectored_write");
85                 rc = -EIO;
86                 goto out;
87         } else {
88                 io_u->error = 0;
89                 rc = FIO_Q_COMPLETED;
90         }
91
92 out:
93         if (rc < 0)
94                 io_u->error = rc;
95
96         return rc;
97 }
98
99 static int open_file(struct thread_data *td, struct fio_file *f)
100 {
101         int rc;
102         struct acs_file_data *d = NULL;
103
104         d = malloc(sizeof(*d));
105         if (!d) {
106                 td_verror(td, -ENOMEM, "malloc");
107                 rc = -ENOMEM;
108                 goto error;
109         }
110         f->file_data = d;
111
112         rc = generic_open_file(td, f);
113
114 out:
115         return rc;
116
117 error:
118         f->fd = -1;
119         f->file_data = NULL;
120         if (d)
121                 free(d);
122
123         goto out;
124 }
125
126 static int close_file(struct thread_data *td, struct fio_file *f)
127 {
128         if (f->file_data) {
129                 free(f->file_data);
130                 f->file_data = NULL;
131         }
132
133         return generic_close_file(td, f);
134 }
135
136 static struct ioengine_ops ioengine = {
137         .name = "fusion-aw-sync",
138         .version = FIO_IOOPS_VERSION,
139         .queue = queue,
140         .open_file = open_file,
141         .close_file = close_file,
142         .get_file_size = generic_get_file_size,
143         .flags = FIO_SYNCIO | FIO_RAWIO | FIO_MEMALIGN
144 };
145
146 static void fio_init fio_fusion_aw_init(void)
147 {
148         register_ioengine(&ioengine);
149 }
150
151 static void fio_exit fio_fusion_aw_exit(void)
152 {
153         unregister_ioengine(&ioengine);
154 }