engines/libaio: add support for user mapped iocbs
[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 */
2866c82d
JA
7#include <stdlib.h>
8#include <unistd.h>
9#include <errno.h>
67bf9823 10#include <libaio.h>
5f350952
JA
11
12#include "../fio.h"
0f38bbef 13#include "../lib/pow2.h"
d220c761 14#include "../optgroup.h"
2866c82d 15
ee636f3f
JA
16#ifndef IOCB_FLAG_HIPRI
17#define IOCB_FLAG_HIPRI (1 << 2)
18#endif
ebec344d
JA
19#ifndef IOCTX_FLAG_IOPOLL
20#define IOCTX_FLAG_IOPOLL (1 << 0)
21#endif
3c3168e9
JA
22#ifndef IOCTX_FLAG_USERIOCB
23#define IOCTX_FLAG_USERIOCB (1 << 1)
24#endif
25
a391d73d 26
0fc2e103
JA
27static int fio_libaio_commit(struct thread_data *td);
28
2866c82d
JA
29struct libaio_data {
30 io_context_t aio_ctx;
31 struct io_event *aio_events;
755200a3 32 struct iocb **iocbs;
7e77dd02 33 struct io_u **io_us;
acd45e02 34
3c3168e9
JA
35 struct iocb *user_iocbs;
36 struct io_u **io_u_index;
37
acd45e02
JA
38 /*
39 * Basic ring buffer. 'head' is incremented in _queue(), and
40 * 'tail' is incremented in _commit(). We keep 'queued' so
41 * that we know if the ring is full or empty, when
42 * 'head' == 'tail'. 'entries' is the ring size, and
43 * 'is_pow2' is just an optimization to use AND instead of
44 * modulus to get the remainder on ring increment.
45 */
46 int is_pow2;
47 unsigned int entries;
48 unsigned int queued;
49 unsigned int head;
50 unsigned int tail;
2866c82d
JA
51};
52
de890a1e 53struct libaio_options {
a1f871c7 54 void *pad;
de890a1e 55 unsigned int userspace_reap;
a391d73d 56 unsigned int hipri;
3c3168e9 57 unsigned int useriocb;
de890a1e
SL
58};
59
60static struct fio_option options[] = {
61 {
62 .name = "userspace_reap",
e8b0e958 63 .lname = "Libaio userspace reaping",
de890a1e
SL
64 .type = FIO_OPT_STR_SET,
65 .off1 = offsetof(struct libaio_options, userspace_reap),
66 .help = "Use alternative user-space reap implementation",
e90a0adf 67 .category = FIO_OPT_C_ENGINE,
5a3cd5f3 68 .group = FIO_OPT_G_LIBAIO,
de890a1e 69 },
a391d73d
JA
70 {
71 .name = "hipri",
ee636f3f 72 .lname = "High Priority",
a391d73d
JA
73 .type = FIO_OPT_STR_SET,
74 .off1 = offsetof(struct libaio_options, hipri),
6acc46d8 75 .help = "Use polled IO completions",
a391d73d
JA
76 .category = FIO_OPT_C_ENGINE,
77 .group = FIO_OPT_G_LIBAIO,
78 },
3c3168e9
JA
79 {
80 .name = "useriocb",
81 .lname = "User IOCBs",
82 .type = FIO_OPT_STR_SET,
83 .off1 = offsetof(struct libaio_options, useriocb),
84 .help = "Use user mapped IOCBs",
85 .category = FIO_OPT_C_ENGINE,
86 .group = FIO_OPT_G_LIBAIO,
87 },
de890a1e
SL
88 {
89 .name = NULL,
90 },
91};
92
acd45e02
JA
93static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
94 unsigned int add)
95{
96 if (ld->is_pow2)
97 *val = (*val + add) & (ld->entries - 1);
98 else
99 *val = (*val + add) % ld->entries;
100}
101
7a16dd02 102static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
2866c82d 103{
3c3168e9 104 struct libaio_data *ld = td->io_ops_data;
53cdc686 105 struct fio_file *f = io_u->file;
a391d73d 106 struct libaio_options *o = td->eo;
3c3168e9
JA
107 struct iocb *iocb;
108
109 if (o->useriocb)
110 iocb = &ld->user_iocbs[io_u->index];
111 else
112 iocb = &io_u->iocb;
53cdc686 113
a391d73d 114 if (io_u->ddir == DDIR_READ) {
3c3168e9 115 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
a391d73d 116 if (o->hipri)
3c3168e9 117 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
a391d73d 118 } else if (io_u->ddir == DDIR_WRITE) {
3c3168e9 119 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
a391d73d 120 if (o->hipri)
3c3168e9 121 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
a391d73d 122 } else if (ddir_sync(io_u->ddir))
3c3168e9 123 io_prep_fsync(iocb, f->fd);
2866c82d
JA
124
125 return 0;
126}
127
128static struct io_u *fio_libaio_event(struct thread_data *td, int event)
129{
565e784d 130 struct libaio_data *ld = td->io_ops_data;
3c3168e9 131 struct libaio_options *o = td->eo;
f423479d
JA
132 struct io_event *ev;
133 struct io_u *io_u;
2866c82d 134
f423479d 135 ev = ld->aio_events + event;
3c3168e9
JA
136 if (o->useriocb) {
137 int index = (int) (uintptr_t) ev->obj;
138 io_u = ld->io_u_index[index];
139 } else
140 io_u = container_of(ev->obj, struct io_u, iocb);
f423479d
JA
141
142 if (ev->res != io_u->xfer_buflen) {
143 if (ev->res > io_u->xfer_buflen)
144 io_u->error = -ev->res;
145 else
146 io_u->resid = io_u->xfer_buflen - ev->res;
147 } else
148 io_u->error = 0;
149
150 return io_u;
2866c82d
JA
151}
152
675012f0
DE
153struct aio_ring {
154 unsigned id; /** kernel internal index number */
155 unsigned nr; /** number of io_events */
156 unsigned head;
157 unsigned tail;
c44b1ff5 158
675012f0
DE
159 unsigned magic;
160 unsigned compat_features;
161 unsigned incompat_features;
162 unsigned header_length; /** size of aio_ring */
163
164 struct io_event events[0];
165};
166
167#define AIO_RING_MAGIC 0xa10a10a1
168
169static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
c44b1ff5 170 struct io_event *events)
675012f0
DE
171{
172 long i = 0;
173 unsigned head;
c44b1ff5 174 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
675012f0
DE
175
176 while (i < max) {
177 head = ring->head;
178
179 if (head == ring->tail) {
180 /* There are no more completions */
181 break;
182 } else {
183 /* There is another completion to reap */
184 events[i] = ring->events[head];
185 read_barrier();
c44b1ff5 186 ring->head = (head + 1) % ring->nr;
675012f0
DE
187 i++;
188 }
189 }
190
191 return i;
192}
193
e7d2e616 194static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
1f440ece 195 unsigned int max, const struct timespec *t)
2866c82d 196{
565e784d 197 struct libaio_data *ld = td->io_ops_data;
de890a1e 198 struct libaio_options *o = td->eo;
82407585 199 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
1f440ece 200 struct timespec __lt, *lt = NULL;
0b7fdba7 201 int r, events = 0;
2866c82d 202
1f440ece
JA
203 if (t) {
204 __lt = *t;
205 lt = &__lt;
206 }
207
2866c82d 208 do {
de890a1e 209 if (o->userspace_reap == 1
675012f0
DE
210 && actual_min == 0
211 && ((struct aio_ring *)(ld->aio_ctx))->magic
212 == AIO_RING_MAGIC) {
213 r = user_io_getevents(ld->aio_ctx, max,
214 ld->aio_events + events);
215 } else {
216 r = io_getevents(ld->aio_ctx, actual_min,
1f440ece 217 max, ld->aio_events + events, lt);
675012f0 218 }
3441a52d 219 if (r > 0)
0b7fdba7 220 events += r;
3441a52d 221 else if ((min && r == 0) || r == -EAGAIN) {
0fc2e103 222 fio_libaio_commit(td);
6347e43d
JA
223 if (actual_min)
224 usleep(10);
0fc2e103 225 } else if (r != -EINTR)
a31dc2dd 226 break;
0b7fdba7
DE
227 } while (events < min);
228
229 return r < 0 ? r : events;
2866c82d
JA
230}
231
2e4ef4fb
JA
232static enum fio_q_status fio_libaio_queue(struct thread_data *td,
233 struct io_u *io_u)
2866c82d 234{
565e784d 235 struct libaio_data *ld = td->io_ops_data;
3c3168e9 236 struct libaio_options *o = td->eo;
2866c82d 237
7101d9c2
JA
238 fio_ro_check(td, io_u);
239
acd45e02 240 if (ld->queued == td->o.iodepth)
755200a3
JA
241 return FIO_Q_BUSY;
242
243 /*
244 * fsync is tricky, since it can fail and we need to do it
245 * serialized with other io. the reason is that linux doesn't
246 * support aio fsync yet. So return busy for the case where we
247 * have pending io, to let fio complete those first.
248 */
f011531e 249 if (ddir_sync(io_u->ddir)) {
acd45e02 250 if (ld->queued)
755200a3 251 return FIO_Q_BUSY;
5f9099ea 252
f011531e 253 do_io_u_sync(td, io_u);
755200a3
JA
254 return FIO_Q_COMPLETED;
255 }
256
a5f3027c 257 if (io_u->ddir == DDIR_TRIM) {
acd45e02 258 if (ld->queued)
a5f3027c
JA
259 return FIO_Q_BUSY;
260
261 do_io_u_trim(td, io_u);
c0681c9d
VF
262 io_u_mark_submit(td, 1);
263 io_u_mark_complete(td, 1);
a5f3027c
JA
264 return FIO_Q_COMPLETED;
265 }
266
3c3168e9
JA
267 if (o->useriocb)
268 ld->iocbs[ld->head] = (struct iocb *) (uintptr_t) io_u->index;
269 else
270 ld->iocbs[ld->head] = &io_u->iocb;
271
acd45e02
JA
272 ld->io_us[ld->head] = io_u;
273 ring_inc(ld, &ld->head, 1);
274 ld->queued++;
755200a3
JA
275 return FIO_Q_QUEUED;
276}
277
7e77dd02
JA
278static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
279 unsigned int nr)
280{
8b6a404c 281 struct timespec now;
7e77dd02
JA
282 unsigned int i;
283
12d9d841
JA
284 if (!fio_fill_issue_time(td))
285 return;
286
7e77dd02
JA
287 fio_gettime(&now, NULL);
288
289 for (i = 0; i < nr; i++) {
290 struct io_u *io_u = io_us[i];
291
292 memcpy(&io_u->issue_time, &now, sizeof(now));
293 io_u_queued(td, io_u);
294 }
295}
296
755200a3
JA
297static int fio_libaio_commit(struct thread_data *td)
298{
565e784d 299 struct libaio_data *ld = td->io_ops_data;
755200a3 300 struct iocb **iocbs;
7e77dd02 301 struct io_u **io_us;
8b6a404c 302 struct timespec ts;
a120ca7f 303 int ret, wait_start = 0;
755200a3 304
acd45e02 305 if (!ld->queued)
755200a3
JA
306 return 0;
307
2866c82d 308 do {
acd45e02
JA
309 long nr = ld->queued;
310
311 nr = min((unsigned int) nr, ld->entries - ld->tail);
312 io_us = ld->io_us + ld->tail;
313 iocbs = ld->iocbs + ld->tail;
314
315 ret = io_submit(ld->aio_ctx, nr, iocbs);
5e00c2c4 316 if (ret > 0) {
7e77dd02 317 fio_libaio_queued(td, io_us, ret);
838bc709 318 io_u_mark_submit(td, ret);
acd45e02
JA
319
320 ld->queued -= ret;
321 ring_inc(ld, &ld->tail, ret);
5e00c2c4 322 ret = 0;
e3b4e568 323 wait_start = 0;
acd45e02 324 } else if (ret == -EINTR || !ret) {
838bc709
JA
325 if (!ret)
326 io_u_mark_submit(td, ret);
e3b4e568 327 wait_start = 0;
2866c82d 328 continue;
acd45e02
JA
329 } else if (ret == -EAGAIN) {
330 /*
331 * If we get EAGAIN, we should break out without
332 * error and let the upper layer reap some
a120ca7f
JA
333 * events for us. If we have no queued IO, we
334 * must loop here. If we loop for more than 30s,
335 * just error out, something must be buggy in the
336 * IO path.
acd45e02 337 */
a120ca7f
JA
338 if (ld->queued) {
339 ret = 0;
340 break;
341 }
342 if (!wait_start) {
8b6a404c 343 fio_gettime(&ts, NULL);
d36b072d 344 wait_start = 1;
8b6a404c 345 } else if (mtime_since_now(&ts) > 30000) {
a120ca7f
JA
346 log_err("fio: aio appears to be stalled, giving up\n");
347 break;
348 }
349 usleep(1);
350 continue;
a31dc2dd
JA
351 } else if (ret == -ENOMEM) {
352 /*
353 * If we get -ENOMEM, reap events if we can. If
354 * we cannot, treat it as a fatal event since there's
355 * nothing we can do about it.
356 */
357 if (ld->queued)
358 ret = 0;
359 break;
838bc709 360 } else
2866c82d 361 break;
2c3a4ae9 362 } while (ld->queued);
2866c82d 363
36167d82 364 return ret;
2866c82d
JA
365}
366
367static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
368{
565e784d 369 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
370
371 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
372}
373
374static void fio_libaio_cleanup(struct thread_data *td)
375{
565e784d 376 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
377
378 if (ld) {
f24c2649
JA
379 /*
380 * Work-around to avoid huge RCU stalls at exit time. If we
381 * don't do this here, then it'll be torn down by exit_aio().
382 * But for that case we can parallellize the freeing, thus
383 * speeding it up a lot.
384 */
385 if (!(td->flags & TD_F_CHILD))
386 io_destroy(ld->aio_ctx);
7e77dd02
JA
387 free(ld->aio_events);
388 free(ld->iocbs);
389 free(ld->io_us);
3c3168e9
JA
390 if (ld->user_iocbs)
391 free(ld->user_iocbs);
2866c82d 392 free(ld);
2866c82d
JA
393 }
394}
395
ebec344d 396static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
3c3168e9 397 bool hipri, bool useriocb)
ebec344d
JA
398{
399#ifdef __NR_sys_io_setup2
a1b006fe 400 int flags = 0;
ebec344d
JA
401
402 if (hipri)
3c3168e9
JA
403 flags |= IOCTX_FLAG_IOPOLL;
404 if (useriocb)
405 flags |= IOCTX_FLAG_USERIOCB;
ebec344d 406
3c3168e9 407 return syscall(__NR_sys_io_setup2, depth, flags, ld->user_iocbs, &ld->aio_ctx);
ebec344d
JA
408#else
409 if (hipri) {
410 log_err("fio: polled aio not available on your platform\n");
411 return 1;
412 }
3c3168e9
JA
413 if (useriocb) {
414 log_err("fio: user mapped iocbs not available on your platform\n");
415 return 1;
416 }
417
ebec344d
JA
418 return io_queue_init(depth, &ld->aio_ctx);
419#endif
420}
421
2866c82d
JA
422static int fio_libaio_init(struct thread_data *td)
423{
c90e1017 424 struct libaio_options *o = td->eo;
acd45e02 425 struct libaio_data *ld;
c90e1017 426 int err = 0;
2866c82d 427
acd45e02 428 ld = calloc(1, sizeof(*ld));
c1db2dce 429
3c3168e9
JA
430 if (o->useriocb) {
431 size_t size;
432 void *p;
433
434 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
435 size = td->o.iodepth * sizeof(struct iocb);
436 if (posix_memalign(&p, page_size, size)) {
437 log_err("fio: libaio iocb allocation failure\n");
438 free(ld);
439 return 1;
440 }
441 ld->user_iocbs = p;
442 }
443
c90e1017
JA
444 /*
445 * First try passing in 0 for queue depth, since we don't
446 * care about the user ring. If that fails, the kernel is too old
447 * and we need the right depth.
448 */
3c3168e9 449 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri, o->useriocb);
c1db2dce
JA
450 if (err) {
451 td_verror(td, -err, "io_queue_init");
75de55ac 452 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
3c3168e9
JA
453 if (ld->user_iocbs)
454 free(ld->user_iocbs);
cb781c75 455 free(ld);
2866c82d
JA
456 return 1;
457 }
458
acd45e02
JA
459 ld->entries = td->o.iodepth;
460 ld->is_pow2 = is_power_of_2(ld->entries);
461 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
462 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
463 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
755200a3 464
565e784d 465 td->io_ops_data = ld;
2866c82d
JA
466 return 0;
467}
468
3c3168e9
JA
469static int fio_libaio_io_u_init(struct thread_data *td, struct io_u *io_u)
470{
471 struct libaio_options *o = td->eo;
472
473 if (o->useriocb) {
474 struct libaio_data *ld = td->io_ops_data;
475
476 ld->io_u_index[io_u->index] = io_u;
477 }
478
479 return 0;
480}
481
5f350952 482static struct ioengine_ops ioengine = {
de890a1e
SL
483 .name = "libaio",
484 .version = FIO_IOOPS_VERSION,
485 .init = fio_libaio_init,
3c3168e9 486 .io_u_init = fio_libaio_io_u_init,
de890a1e
SL
487 .prep = fio_libaio_prep,
488 .queue = fio_libaio_queue,
489 .commit = fio_libaio_commit,
490 .cancel = fio_libaio_cancel,
491 .getevents = fio_libaio_getevents,
492 .event = fio_libaio_event,
493 .cleanup = fio_libaio_cleanup,
494 .open_file = generic_open_file,
495 .close_file = generic_close_file,
496 .get_file_size = generic_get_file_size,
497 .options = options,
498 .option_struct_size = sizeof(struct libaio_options),
2866c82d 499};
34cfcdaf 500
5f350952
JA
501static void fio_init fio_libaio_register(void)
502{
503 register_ioengine(&ioengine);
504}
505
506static void fio_exit fio_libaio_unregister(void)
507{
508 unregister_ioengine(&ioengine);
509}