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