Fix libaio prep
[fio.git] / engines / libaio.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * libaio engine
3 *
4 * IO engine using the Linux native aio interface.
2866c82d
JA
5 *
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
11#include <assert.h>
5f350952
JA
12
13#include "../fio.h"
2866c82d 14
34cfcdaf
JA
15#ifdef FIO_HAVE_LIBAIO
16
2866c82d
JA
17#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
18
19struct libaio_data {
20 io_context_t aio_ctx;
21 struct io_event *aio_events;
755200a3 22 struct iocb **iocbs;
7e77dd02 23 struct io_u **io_us;
755200a3 24 int iocbs_nr;
2866c82d
JA
25};
26
7a16dd02 27static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
2866c82d 28{
53cdc686
JA
29 struct fio_file *f = io_u->file;
30
2866c82d 31 if (io_u->ddir == DDIR_READ)
cec6b55d 32 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
87dc1ab1 33 else if (io_u->ddir == DDIR_WRITE)
cec6b55d 34 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
5f9099ea 35 else if (ddir_sync(io_u->ddir))
87dc1ab1 36 io_prep_fsync(&io_u->iocb, f->fd);
2866c82d
JA
37
38 return 0;
39}
40
41static struct io_u *fio_libaio_event(struct thread_data *td, int event)
42{
43 struct libaio_data *ld = td->io_ops->data;
f423479d
JA
44 struct io_event *ev;
45 struct io_u *io_u;
2866c82d 46
f423479d
JA
47 ev = ld->aio_events + event;
48 io_u = ev_to_iou(ev);
49
50 if (ev->res != io_u->xfer_buflen) {
51 if (ev->res > io_u->xfer_buflen)
52 io_u->error = -ev->res;
53 else
54 io_u->resid = io_u->xfer_buflen - ev->res;
55 } else
56 io_u->error = 0;
57
58 return io_u;
2866c82d
JA
59}
60
e7d2e616
JA
61static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
62 unsigned int max, struct timespec *t)
2866c82d
JA
63{
64 struct libaio_data *ld = td->io_ops->data;
de9937d1 65 int r;
2866c82d
JA
66
67 do {
68 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
de9937d1 69 if (r >= (int) min)
ccbb91cb
JA
70 break;
71 else if (r == -EAGAIN) {
2866c82d
JA
72 usleep(100);
73 continue;
74 } else if (r == -EINTR)
75 continue;
84585003 76 else if (r != 0)
2866c82d
JA
77 break;
78 } while (1);
79
22819ec2 80 return r;
2866c82d
JA
81}
82
83static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
84{
85 struct libaio_data *ld = td->io_ops->data;
2866c82d 86
7101d9c2
JA
87 fio_ro_check(td, io_u);
88
2dc1bbeb 89 if (ld->iocbs_nr == (int) td->o.iodepth)
755200a3
JA
90 return FIO_Q_BUSY;
91
92 /*
93 * fsync is tricky, since it can fail and we need to do it
94 * serialized with other io. the reason is that linux doesn't
95 * support aio fsync yet. So return busy for the case where we
96 * have pending io, to let fio complete those first.
97 */
f011531e 98 if (ddir_sync(io_u->ddir)) {
755200a3
JA
99 if (ld->iocbs_nr)
100 return FIO_Q_BUSY;
5f9099ea 101
f011531e 102 do_io_u_sync(td, io_u);
755200a3
JA
103 return FIO_Q_COMPLETED;
104 }
105
a5f3027c
JA
106 if (io_u->ddir == DDIR_TRIM) {
107 if (ld->iocbs_nr)
108 return FIO_Q_BUSY;
109
110 do_io_u_trim(td, io_u);
111 return FIO_Q_COMPLETED;
112 }
113
755200a3 114 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
7e77dd02 115 ld->io_us[ld->iocbs_nr] = io_u;
755200a3
JA
116 ld->iocbs_nr++;
117 return FIO_Q_QUEUED;
118}
119
7e77dd02
JA
120static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
121 unsigned int nr)
122{
123 struct timeval now;
124 unsigned int i;
125
12d9d841
JA
126 if (!fio_fill_issue_time(td))
127 return;
128
7e77dd02
JA
129 fio_gettime(&now, NULL);
130
131 for (i = 0; i < nr; i++) {
132 struct io_u *io_u = io_us[i];
133
134 memcpy(&io_u->issue_time, &now, sizeof(now));
135 io_u_queued(td, io_u);
136 }
137}
138
755200a3
JA
139static int fio_libaio_commit(struct thread_data *td)
140{
141 struct libaio_data *ld = td->io_ops->data;
142 struct iocb **iocbs;
7e77dd02 143 struct io_u **io_us;
5e00c2c4 144 int ret;
755200a3
JA
145
146 if (!ld->iocbs_nr)
147 return 0;
148
7e77dd02 149 io_us = ld->io_us;
755200a3 150 iocbs = ld->iocbs;
2866c82d 151 do {
5e00c2c4
JA
152 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
153 if (ret > 0) {
7e77dd02 154 fio_libaio_queued(td, io_us, ret);
838bc709 155 io_u_mark_submit(td, ret);
5e00c2c4 156 ld->iocbs_nr -= ret;
7e77dd02 157 io_us += ret;
755200a3 158 iocbs += ret;
5e00c2c4 159 ret = 0;
838bc709
JA
160 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
161 if (!ret)
162 io_u_mark_submit(td, ret);
2866c82d 163 continue;
838bc709 164 } else
2866c82d 165 break;
5e00c2c4 166 } while (ld->iocbs_nr);
2866c82d 167
36167d82 168 return ret;
2866c82d
JA
169}
170
171static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
172{
173 struct libaio_data *ld = td->io_ops->data;
174
175 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
176}
177
178static void fio_libaio_cleanup(struct thread_data *td)
179{
180 struct libaio_data *ld = td->io_ops->data;
181
182 if (ld) {
183 io_destroy(ld->aio_ctx);
7e77dd02
JA
184 free(ld->aio_events);
185 free(ld->iocbs);
186 free(ld->io_us);
2866c82d 187 free(ld);
2866c82d
JA
188 }
189}
190
191static int fio_libaio_init(struct thread_data *td)
192{
193 struct libaio_data *ld = malloc(sizeof(*ld));
c1db2dce 194 int err;
2866c82d
JA
195
196 memset(ld, 0, sizeof(*ld));
c1db2dce
JA
197
198 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
199 if (err) {
200 td_verror(td, -err, "io_queue_init");
75de55ac 201 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
cb781c75 202 free(ld);
2866c82d
JA
203 return 1;
204 }
205
2dc1bbeb
JA
206 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
207 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
208 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
755200a3 209 memset(ld->iocbs, 0, sizeof(struct iocb *));
2dc1bbeb
JA
210 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
211 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
755200a3
JA
212 ld->iocbs_nr = 0;
213
2866c82d
JA
214 td->io_ops->data = ld;
215 return 0;
216}
217
5f350952 218static struct ioengine_ops ioengine = {
2866c82d
JA
219 .name = "libaio",
220 .version = FIO_IOOPS_VERSION,
221 .init = fio_libaio_init,
222 .prep = fio_libaio_prep,
223 .queue = fio_libaio_queue,
755200a3 224 .commit = fio_libaio_commit,
2866c82d
JA
225 .cancel = fio_libaio_cancel,
226 .getevents = fio_libaio_getevents,
227 .event = fio_libaio_event,
228 .cleanup = fio_libaio_cleanup,
b5af8293
JA
229 .open_file = generic_open_file,
230 .close_file = generic_close_file,
df9c26b1 231 .get_file_size = generic_get_file_size,
2866c82d 232};
34cfcdaf
JA
233
234#else /* FIO_HAVE_LIBAIO */
235
236/*
237 * When we have a proper configure system in place, we simply wont build
238 * and install this io engine. For now install a crippled version that
239 * just complains and fails to load.
240 */
241static int fio_libaio_init(struct thread_data fio_unused *td)
242{
243 fprintf(stderr, "fio: libaio not available\n");
244 return 1;
245}
246
5f350952 247static struct ioengine_ops ioengine = {
34cfcdaf
JA
248 .name = "libaio",
249 .version = FIO_IOOPS_VERSION,
250 .init = fio_libaio_init,
251};
252
253#endif
5f350952
JA
254
255static void fio_init fio_libaio_register(void)
256{
257 register_ioengine(&ioengine);
258}
259
260static void fio_exit fio_libaio_unregister(void)
261{
262 unregister_ioengine(&ioengine);
263}