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.
55 struct cmdprio cmdprio;
58 struct libaio_options {
59 struct thread_data *td;
60 unsigned int userspace_reap;
61 struct cmdprio_options cmdprio_options;
65 static struct fio_option options[] = {
67 .name = "userspace_reap",
68 .lname = "Libaio userspace reaping",
69 .type = FIO_OPT_STR_SET,
70 .off1 = offsetof(struct libaio_options, userspace_reap),
71 .help = "Use alternative user-space reap implementation",
72 .category = FIO_OPT_C_ENGINE,
73 .group = FIO_OPT_G_LIBAIO,
75 #ifdef FIO_HAVE_IOPRIO_CLASS
77 .name = "cmdprio_percentage",
78 .lname = "high priority percentage",
80 .off1 = offsetof(struct libaio_options,
81 cmdprio_options.percentage[DDIR_READ]),
82 .off2 = offsetof(struct libaio_options,
83 cmdprio_options.percentage[DDIR_WRITE]),
86 .help = "Send high priority I/O this percentage of the time",
87 .category = FIO_OPT_C_ENGINE,
88 .group = FIO_OPT_G_LIBAIO,
91 .name = "cmdprio_class",
92 .lname = "Asynchronous I/O priority class",
94 .off1 = offsetof(struct libaio_options,
95 cmdprio_options.class[DDIR_READ]),
96 .off2 = offsetof(struct libaio_options,
97 cmdprio_options.class[DDIR_WRITE]),
98 .help = "Set asynchronous IO priority class",
99 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
100 .maxval = IOPRIO_MAX_PRIO_CLASS,
102 .category = FIO_OPT_C_ENGINE,
103 .group = FIO_OPT_G_LIBAIO,
107 .lname = "Asynchronous I/O priority level",
109 .off1 = offsetof(struct libaio_options,
110 cmdprio_options.level[DDIR_READ]),
111 .off2 = offsetof(struct libaio_options,
112 cmdprio_options.level[DDIR_WRITE]),
113 .help = "Set asynchronous IO priority level",
114 .minval = IOPRIO_MIN_PRIO,
115 .maxval = IOPRIO_MAX_PRIO,
117 .category = FIO_OPT_C_ENGINE,
118 .group = FIO_OPT_G_LIBAIO,
121 .name = "cmdprio_bssplit",
122 .lname = "Priority percentage block size split",
123 .type = FIO_OPT_STR_STORE,
124 .off1 = offsetof(struct libaio_options,
125 cmdprio_options.bssplit_str),
126 .help = "Set priority percentages for different block sizes",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_LIBAIO,
132 .name = "cmdprio_percentage",
133 .lname = "high priority percentage",
134 .type = FIO_OPT_UNSUPPORTED,
135 .help = "Your platform does not support I/O priority classes",
138 .name = "cmdprio_class",
139 .lname = "Asynchronous I/O priority class",
140 .type = FIO_OPT_UNSUPPORTED,
141 .help = "Your platform does not support I/O priority classes",
145 .lname = "Asynchronous I/O priority level",
146 .type = FIO_OPT_UNSUPPORTED,
147 .help = "Your platform does not support I/O priority classes",
150 .name = "cmdprio_bssplit",
151 .lname = "Priority percentage block size split",
152 .type = FIO_OPT_UNSUPPORTED,
153 .help = "Your platform does not support I/O priority classes",
158 .lname = "RWF_NOWAIT",
159 .type = FIO_OPT_BOOL,
160 .off1 = offsetof(struct libaio_options, nowait),
161 .help = "Set RWF_NOWAIT for reads/writes",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_LIBAIO,
170 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
174 *val = (*val + add) & (ld->entries - 1);
176 *val = (*val + add) % ld->entries;
179 static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
181 struct libaio_options *o = td->eo;
182 struct fio_file *f = io_u->file;
183 struct iocb *iocb = &io_u->iocb;
185 if (io_u->ddir == DDIR_READ) {
186 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
188 iocb->aio_rw_flags |= RWF_NOWAIT;
189 } else if (io_u->ddir == DDIR_WRITE) {
190 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
192 iocb->aio_rw_flags |= RWF_NOWAIT;
193 } else if (ddir_sync(io_u->ddir))
194 io_prep_fsync(iocb, f->fd);
199 static inline void fio_libaio_cmdprio_prep(struct thread_data *td,
202 struct libaio_data *ld = td->io_ops_data;
203 struct cmdprio *cmdprio = &ld->cmdprio;
205 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u)) {
206 io_u->iocb.aio_reqprio = io_u->ioprio;
207 io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO;
211 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
213 struct libaio_data *ld = td->io_ops_data;
217 ev = ld->aio_events + event;
218 io_u = container_of(ev->obj, struct io_u, iocb);
220 if (ev->res != io_u->xfer_buflen) {
221 if (ev->res > io_u->xfer_buflen)
222 io_u->error = -ev->res;
224 io_u->resid = io_u->xfer_buflen - ev->res;
232 unsigned id; /** kernel internal index number */
233 unsigned nr; /** number of io_events */
238 unsigned compat_features;
239 unsigned incompat_features;
240 unsigned header_length; /** size of aio_ring */
242 struct io_event events[0];
245 #define AIO_RING_MAGIC 0xa10a10a1
247 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
248 struct io_event *events)
252 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
257 if (head == ring->tail) {
258 /* There are no more completions */
261 /* There is another completion to reap */
262 events[i] = ring->events[head];
263 atomic_store_release(&ring->head,
264 (head + 1) % ring->nr);
272 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
273 unsigned int max, const struct timespec *t)
275 struct libaio_data *ld = td->io_ops_data;
276 struct libaio_options *o = td->eo;
277 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
278 struct timespec __lt, *lt = NULL;
287 if (o->userspace_reap == 1
289 && ((struct aio_ring *)(ld->aio_ctx))->magic
291 r = user_io_getevents(ld->aio_ctx, max - events,
292 ld->aio_events + events);
294 r = io_getevents(ld->aio_ctx, actual_min,
295 max - events, ld->aio_events + events, lt);
299 actual_min = actual_min > events ? actual_min - events : 0;
301 else if ((min && r == 0) || r == -EAGAIN) {
302 fio_libaio_commit(td);
305 } else if (r != -EINTR)
307 } while (events < min);
309 return r < 0 ? r : events;
312 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
315 struct libaio_data *ld = td->io_ops_data;
317 fio_ro_check(td, io_u);
319 if (ld->queued == td->o.iodepth)
323 * fsync is tricky, since it can fail and we need to do it
324 * serialized with other io. the reason is that linux doesn't
325 * support aio fsync yet. So return busy for the case where we
326 * have pending io, to let fio complete those first.
328 if (ddir_sync(io_u->ddir)) {
332 do_io_u_sync(td, io_u);
333 return FIO_Q_COMPLETED;
336 if (io_u->ddir == DDIR_TRIM) {
340 do_io_u_trim(td, io_u);
341 io_u_mark_submit(td, 1);
342 io_u_mark_complete(td, 1);
343 return FIO_Q_COMPLETED;
346 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
347 fio_libaio_cmdprio_prep(td, io_u);
349 ld->iocbs[ld->head] = &io_u->iocb;
350 ld->io_us[ld->head] = io_u;
351 ring_inc(ld, &ld->head, 1);
356 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
362 if (!fio_fill_issue_time(td))
365 fio_gettime(&now, NULL);
367 for (i = 0; i < nr; i++) {
368 struct io_u *io_u = io_us[i];
370 memcpy(&io_u->issue_time, &now, sizeof(now));
371 io_u_queued(td, io_u);
375 * only used for iolog
377 if (td->o.read_iolog_file)
378 memcpy(&td->last_issue, &now, sizeof(now));
381 static int fio_libaio_commit(struct thread_data *td)
383 struct libaio_data *ld = td->io_ops_data;
387 int ret, wait_start = 0;
393 long nr = ld->queued;
395 nr = min((unsigned int) nr, ld->entries - ld->tail);
396 io_us = ld->io_us + ld->tail;
397 iocbs = ld->iocbs + ld->tail;
399 ret = io_submit(ld->aio_ctx, nr, iocbs);
401 fio_libaio_queued(td, io_us, ret);
402 io_u_mark_submit(td, ret);
405 ring_inc(ld, &ld->tail, ret);
408 } else if (ret == -EINTR || !ret) {
410 io_u_mark_submit(td, ret);
413 } else if (ret == -EAGAIN) {
415 * If we get EAGAIN, we should break out without
416 * error and let the upper layer reap some
417 * events for us. If we have no queued IO, we
418 * must loop here. If we loop for more than 30s,
419 * just error out, something must be buggy in the
427 fio_gettime(&ts, NULL);
429 } else if (mtime_since_now(&ts) > 30000) {
430 log_err("fio: aio appears to be stalled, giving up\n");
435 } else if (ret == -ENOMEM) {
437 * If we get -ENOMEM, reap events if we can. If
438 * we cannot, treat it as a fatal event since there's
439 * nothing we can do about it.
446 } while (ld->queued);
451 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
453 struct libaio_data *ld = td->io_ops_data;
455 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
458 static void fio_libaio_cleanup(struct thread_data *td)
460 struct libaio_data *ld = td->io_ops_data;
464 * Work-around to avoid huge RCU stalls at exit time. If we
465 * don't do this here, then it'll be torn down by exit_aio().
466 * But for that case we can parallellize the freeing, thus
467 * speeding it up a lot.
469 if (!(td->flags & TD_F_CHILD))
470 io_destroy(ld->aio_ctx);
472 fio_cmdprio_cleanup(&ld->cmdprio);
473 free(ld->aio_events);
480 static int fio_libaio_post_init(struct thread_data *td)
482 struct libaio_data *ld = td->io_ops_data;
485 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
487 td_verror(td, -err, "io_queue_init");
494 static int fio_libaio_init(struct thread_data *td)
496 struct libaio_data *ld;
497 struct libaio_options *o = td->eo;
500 ld = calloc(1, sizeof(*ld));
502 ld->entries = td->o.iodepth;
503 ld->is_pow2 = is_power_of_2(ld->entries);
504 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
505 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
506 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
508 td->io_ops_data = ld;
510 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
512 td_verror(td, EINVAL, "fio_libaio_init");
519 FIO_STATIC struct ioengine_ops ioengine = {
521 .version = FIO_IOOPS_VERSION,
522 .flags = FIO_ASYNCIO_SYNC_TRIM |
523 FIO_ASYNCIO_SETS_ISSUE_TIME,
524 .init = fio_libaio_init,
525 .post_init = fio_libaio_post_init,
526 .prep = fio_libaio_prep,
527 .queue = fio_libaio_queue,
528 .commit = fio_libaio_commit,
529 .cancel = fio_libaio_cancel,
530 .getevents = fio_libaio_getevents,
531 .event = fio_libaio_event,
532 .cleanup = fio_libaio_cleanup,
533 .open_file = generic_open_file,
534 .close_file = generic_close_file,
535 .get_file_size = generic_get_file_size,
537 .option_struct_size = sizeof(struct libaio_options),
540 static void fio_init fio_libaio_register(void)
542 register_ioengine(&ioengine);
545 static void fio_exit fio_libaio_unregister(void)
547 unregister_ioengine(&ioengine);