4 * IO engine using the Linux native aio interface.
12 #include <sys/resource.h>
15 #include "../lib/pow2.h"
16 #include "../optgroup.h"
17 #include "../lib/memalign.h"
20 /* Should be defined in newest aio_abi.h */
21 #ifndef IOCB_FLAG_IOPRIO
22 #define IOCB_FLAG_IOPRIO (1 << 1)
25 /* Hack for libaio < 0.3.111 */
26 #ifndef CONFIG_LIBAIO_RW_FLAGS
27 #define aio_rw_flags __pad2
30 static int fio_libaio_commit(struct thread_data *td);
31 static int fio_libaio_init(struct thread_data *td);
35 struct io_event *aio_events;
39 struct io_u **io_u_index;
42 * Basic ring buffer. 'head' is incremented in _queue(), and
43 * 'tail' is incremented in _commit(). We keep 'queued' so
44 * that we know if the ring is full or empty, when
45 * 'head' == 'tail'. 'entries' is the ring size, and
46 * 'is_pow2' is just an optimization to use AND instead of
47 * modulus to get the remainder on ring increment.
58 struct libaio_options {
59 struct thread_data *td;
60 unsigned int userspace_reap;
61 struct cmdprio cmdprio;
65 static int str_cmdprio_bssplit_cb(void *data, const char *input)
67 struct libaio_options *o = data;
68 struct thread_data *td = o->td;
69 struct cmdprio *cmdprio = &o->cmdprio;
71 return fio_cmdprio_bssplit_parse(td, input, cmdprio);
74 static struct fio_option options[] = {
76 .name = "userspace_reap",
77 .lname = "Libaio userspace reaping",
78 .type = FIO_OPT_STR_SET,
79 .off1 = offsetof(struct libaio_options, userspace_reap),
80 .help = "Use alternative user-space reap implementation",
81 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_LIBAIO,
84 #ifdef FIO_HAVE_IOPRIO_CLASS
86 .name = "cmdprio_percentage",
87 .lname = "high priority percentage",
89 .off1 = offsetof(struct libaio_options,
90 cmdprio.percentage[DDIR_READ]),
91 .off2 = offsetof(struct libaio_options,
92 cmdprio.percentage[DDIR_WRITE]),
95 .help = "Send high priority I/O this percentage of the time",
96 .category = FIO_OPT_C_ENGINE,
97 .group = FIO_OPT_G_LIBAIO,
100 .name = "cmdprio_class",
101 .lname = "Asynchronous I/O priority class",
103 .off1 = offsetof(struct libaio_options,
104 cmdprio.class[DDIR_READ]),
105 .off2 = offsetof(struct libaio_options,
106 cmdprio.class[DDIR_WRITE]),
107 .help = "Set asynchronous IO priority class",
108 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
109 .maxval = IOPRIO_MAX_PRIO_CLASS,
111 .category = FIO_OPT_C_ENGINE,
112 .group = FIO_OPT_G_LIBAIO,
116 .lname = "Asynchronous I/O priority level",
118 .off1 = offsetof(struct libaio_options,
119 cmdprio.level[DDIR_READ]),
120 .off2 = offsetof(struct libaio_options,
121 cmdprio.level[DDIR_WRITE]),
122 .help = "Set asynchronous IO priority level",
123 .minval = IOPRIO_MIN_PRIO,
124 .maxval = IOPRIO_MAX_PRIO,
126 .category = FIO_OPT_C_ENGINE,
127 .group = FIO_OPT_G_LIBAIO,
130 .name = "cmdprio_bssplit",
131 .lname = "Priority percentage block size split",
132 .type = FIO_OPT_STR_ULL,
133 .cb = str_cmdprio_bssplit_cb,
134 .off1 = offsetof(struct libaio_options, cmdprio.bssplit),
135 .help = "Set priority percentages for different block sizes",
136 .category = FIO_OPT_C_ENGINE,
137 .group = FIO_OPT_G_LIBAIO,
141 .name = "cmdprio_percentage",
142 .lname = "high priority percentage",
143 .type = FIO_OPT_UNSUPPORTED,
144 .help = "Your platform does not support I/O priority classes",
147 .name = "cmdprio_class",
148 .lname = "Asynchronous I/O priority class",
149 .type = FIO_OPT_UNSUPPORTED,
150 .help = "Your platform does not support I/O priority classes",
154 .lname = "Asynchronous I/O priority level",
155 .type = FIO_OPT_UNSUPPORTED,
156 .help = "Your platform does not support I/O priority classes",
159 .name = "cmdprio_bssplit",
160 .lname = "Priority percentage block size split",
161 .type = FIO_OPT_UNSUPPORTED,
162 .help = "Your platform does not support I/O priority classes",
167 .lname = "RWF_NOWAIT",
168 .type = FIO_OPT_BOOL,
169 .off1 = offsetof(struct libaio_options, nowait),
170 .help = "Set RWF_NOWAIT for reads/writes",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_LIBAIO,
179 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
183 *val = (*val + add) & (ld->entries - 1);
185 *val = (*val + add) % ld->entries;
188 static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
190 struct libaio_options *o = td->eo;
191 struct fio_file *f = io_u->file;
192 struct iocb *iocb = &io_u->iocb;
194 if (io_u->ddir == DDIR_READ) {
195 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
197 iocb->aio_rw_flags |= RWF_NOWAIT;
198 } else if (io_u->ddir == DDIR_WRITE) {
199 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
201 iocb->aio_rw_flags |= RWF_NOWAIT;
202 } else if (ddir_sync(io_u->ddir))
203 io_prep_fsync(iocb, f->fd);
208 static void fio_libaio_prio_prep(struct thread_data *td, struct io_u *io_u)
210 struct libaio_options *o = td->eo;
211 struct cmdprio *cmdprio = &o->cmdprio;
212 enum fio_ddir ddir = io_u->ddir;
213 unsigned int p = fio_cmdprio_percentage(cmdprio, io_u);
215 if (p && rand_between(&td->prio_state, 0, 99) < p) {
216 io_u->iocb.aio_reqprio =
217 ioprio_value(cmdprio->class[ddir], cmdprio->level[ddir]);
218 io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO;
219 io_u->flags |= IO_U_F_PRIORITY;
223 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
225 struct libaio_data *ld = td->io_ops_data;
229 ev = ld->aio_events + event;
230 io_u = container_of(ev->obj, struct io_u, iocb);
232 if (ev->res != io_u->xfer_buflen) {
233 if (ev->res > io_u->xfer_buflen)
234 io_u->error = -ev->res;
236 io_u->resid = io_u->xfer_buflen - ev->res;
244 unsigned id; /** kernel internal index number */
245 unsigned nr; /** number of io_events */
250 unsigned compat_features;
251 unsigned incompat_features;
252 unsigned header_length; /** size of aio_ring */
254 struct io_event events[0];
257 #define AIO_RING_MAGIC 0xa10a10a1
259 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
260 struct io_event *events)
264 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
269 if (head == ring->tail) {
270 /* There are no more completions */
273 /* There is another completion to reap */
274 events[i] = ring->events[head];
275 atomic_store_release(&ring->head,
276 (head + 1) % ring->nr);
284 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
285 unsigned int max, const struct timespec *t)
287 struct libaio_data *ld = td->io_ops_data;
288 struct libaio_options *o = td->eo;
289 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
290 struct timespec __lt, *lt = NULL;
299 if (o->userspace_reap == 1
301 && ((struct aio_ring *)(ld->aio_ctx))->magic
303 r = user_io_getevents(ld->aio_ctx, max,
304 ld->aio_events + events);
306 r = io_getevents(ld->aio_ctx, actual_min,
307 max, ld->aio_events + events, lt);
311 else if ((min && r == 0) || r == -EAGAIN) {
312 fio_libaio_commit(td);
315 } else if (r != -EINTR)
317 } while (events < min);
319 return r < 0 ? r : events;
322 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
325 struct libaio_data *ld = td->io_ops_data;
327 fio_ro_check(td, io_u);
329 if (ld->queued == td->o.iodepth)
333 * fsync is tricky, since it can fail and we need to do it
334 * serialized with other io. the reason is that linux doesn't
335 * support aio fsync yet. So return busy for the case where we
336 * have pending io, to let fio complete those first.
338 if (ddir_sync(io_u->ddir)) {
342 do_io_u_sync(td, io_u);
343 return FIO_Q_COMPLETED;
346 if (io_u->ddir == DDIR_TRIM) {
350 do_io_u_trim(td, io_u);
351 io_u_mark_submit(td, 1);
352 io_u_mark_complete(td, 1);
353 return FIO_Q_COMPLETED;
357 fio_libaio_prio_prep(td, io_u);
359 ld->iocbs[ld->head] = &io_u->iocb;
360 ld->io_us[ld->head] = io_u;
361 ring_inc(ld, &ld->head, 1);
366 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
372 if (!fio_fill_issue_time(td))
375 fio_gettime(&now, NULL);
377 for (i = 0; i < nr; i++) {
378 struct io_u *io_u = io_us[i];
380 memcpy(&io_u->issue_time, &now, sizeof(now));
381 io_u_queued(td, io_u);
385 static int fio_libaio_commit(struct thread_data *td)
387 struct libaio_data *ld = td->io_ops_data;
391 int ret, wait_start = 0;
397 long nr = ld->queued;
399 nr = min((unsigned int) nr, ld->entries - ld->tail);
400 io_us = ld->io_us + ld->tail;
401 iocbs = ld->iocbs + ld->tail;
403 ret = io_submit(ld->aio_ctx, nr, iocbs);
405 fio_libaio_queued(td, io_us, ret);
406 io_u_mark_submit(td, ret);
409 ring_inc(ld, &ld->tail, ret);
412 } else if (ret == -EINTR || !ret) {
414 io_u_mark_submit(td, ret);
417 } else if (ret == -EAGAIN) {
419 * If we get EAGAIN, we should break out without
420 * error and let the upper layer reap some
421 * events for us. If we have no queued IO, we
422 * must loop here. If we loop for more than 30s,
423 * just error out, something must be buggy in the
431 fio_gettime(&ts, NULL);
433 } else if (mtime_since_now(&ts) > 30000) {
434 log_err("fio: aio appears to be stalled, giving up\n");
439 } else if (ret == -ENOMEM) {
441 * If we get -ENOMEM, reap events if we can. If
442 * we cannot, treat it as a fatal event since there's
443 * nothing we can do about it.
450 } while (ld->queued);
455 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
457 struct libaio_data *ld = td->io_ops_data;
459 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
462 static void fio_libaio_cleanup(struct thread_data *td)
464 struct libaio_data *ld = td->io_ops_data;
468 * Work-around to avoid huge RCU stalls at exit time. If we
469 * don't do this here, then it'll be torn down by exit_aio().
470 * But for that case we can parallellize the freeing, thus
471 * speeding it up a lot.
473 if (!(td->flags & TD_F_CHILD))
474 io_destroy(ld->aio_ctx);
475 free(ld->aio_events);
482 static int fio_libaio_post_init(struct thread_data *td)
484 struct libaio_data *ld = td->io_ops_data;
487 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
489 td_verror(td, -err, "io_queue_init");
496 static int fio_libaio_init(struct thread_data *td)
498 struct libaio_data *ld;
499 struct libaio_options *o = td->eo;
500 struct cmdprio *cmdprio = &o->cmdprio;
503 ld = calloc(1, sizeof(*ld));
505 ld->entries = td->o.iodepth;
506 ld->is_pow2 = is_power_of_2(ld->entries);
507 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
508 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
509 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
511 td->io_ops_data = ld;
513 ret = fio_cmdprio_init(td, cmdprio, &ld->use_cmdprio);
515 td_verror(td, EINVAL, "fio_libaio_init");
522 FIO_STATIC struct ioengine_ops ioengine = {
524 .version = FIO_IOOPS_VERSION,
525 .flags = FIO_ASYNCIO_SYNC_TRIM,
526 .init = fio_libaio_init,
527 .post_init = fio_libaio_post_init,
528 .prep = fio_libaio_prep,
529 .queue = fio_libaio_queue,
530 .commit = fio_libaio_commit,
531 .cancel = fio_libaio_cancel,
532 .getevents = fio_libaio_getevents,
533 .event = fio_libaio_event,
534 .cleanup = fio_libaio_cleanup,
535 .open_file = generic_open_file,
536 .close_file = generic_close_file,
537 .get_file_size = generic_get_file_size,
539 .option_struct_size = sizeof(struct libaio_options),
542 static void fio_init fio_libaio_register(void)
544 register_ioengine(&ioengine);
547 static void fio_exit fio_libaio_unregister(void)
549 unregister_ioengine(&ioengine);