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 {
60 unsigned int userspace_reap;
61 struct cmdprio cmdprio;
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.percentage[DDIR_READ]),
82 .off2 = offsetof(struct libaio_options,
83 cmdprio.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,
92 .name = "cmdprio_percentage",
93 .lname = "high priority percentage",
94 .type = FIO_OPT_UNSUPPORTED,
95 .help = "Your platform does not support I/O priority classes",
100 .lname = "RWF_NOWAIT",
101 .type = FIO_OPT_BOOL,
102 .off1 = offsetof(struct libaio_options, nowait),
103 .help = "Set RWF_NOWAIT for reads/writes",
104 .category = FIO_OPT_C_ENGINE,
105 .group = FIO_OPT_G_LIBAIO,
112 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
116 *val = (*val + add) & (ld->entries - 1);
118 *val = (*val + add) % ld->entries;
121 static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
123 struct libaio_options *o = td->eo;
124 struct fio_file *f = io_u->file;
125 struct iocb *iocb = &io_u->iocb;
127 if (io_u->ddir == DDIR_READ) {
128 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
130 iocb->aio_rw_flags |= RWF_NOWAIT;
131 } else if (io_u->ddir == DDIR_WRITE) {
132 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
134 iocb->aio_rw_flags |= RWF_NOWAIT;
135 } else if (ddir_sync(io_u->ddir))
136 io_prep_fsync(iocb, f->fd);
141 static void fio_libaio_prio_prep(struct thread_data *td, struct io_u *io_u)
143 struct libaio_options *o = td->eo;
144 struct cmdprio *cmdprio = &o->cmdprio;
145 unsigned int p = cmdprio->percentage[io_u->ddir];
147 if (p && rand_between(&td->prio_state, 0, 99) < p) {
148 io_u->iocb.aio_reqprio = ioprio_value(IOPRIO_CLASS_RT, 0);
149 io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO;
150 io_u->flags |= IO_U_F_PRIORITY;
154 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
156 struct libaio_data *ld = td->io_ops_data;
160 ev = ld->aio_events + event;
161 io_u = container_of(ev->obj, struct io_u, iocb);
163 if (ev->res != io_u->xfer_buflen) {
164 if (ev->res > io_u->xfer_buflen)
165 io_u->error = -ev->res;
167 io_u->resid = io_u->xfer_buflen - ev->res;
175 unsigned id; /** kernel internal index number */
176 unsigned nr; /** number of io_events */
181 unsigned compat_features;
182 unsigned incompat_features;
183 unsigned header_length; /** size of aio_ring */
185 struct io_event events[0];
188 #define AIO_RING_MAGIC 0xa10a10a1
190 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
191 struct io_event *events)
195 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
200 if (head == ring->tail) {
201 /* There are no more completions */
204 /* There is another completion to reap */
205 events[i] = ring->events[head];
206 atomic_store_release(&ring->head,
207 (head + 1) % ring->nr);
215 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
216 unsigned int max, const struct timespec *t)
218 struct libaio_data *ld = td->io_ops_data;
219 struct libaio_options *o = td->eo;
220 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
221 struct timespec __lt, *lt = NULL;
230 if (o->userspace_reap == 1
232 && ((struct aio_ring *)(ld->aio_ctx))->magic
234 r = user_io_getevents(ld->aio_ctx, max,
235 ld->aio_events + events);
237 r = io_getevents(ld->aio_ctx, actual_min,
238 max, ld->aio_events + events, lt);
242 else if ((min && r == 0) || r == -EAGAIN) {
243 fio_libaio_commit(td);
246 } else if (r != -EINTR)
248 } while (events < min);
250 return r < 0 ? r : events;
253 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
256 struct libaio_data *ld = td->io_ops_data;
258 fio_ro_check(td, io_u);
260 if (ld->queued == td->o.iodepth)
264 * fsync is tricky, since it can fail and we need to do it
265 * serialized with other io. the reason is that linux doesn't
266 * support aio fsync yet. So return busy for the case where we
267 * have pending io, to let fio complete those first.
269 if (ddir_sync(io_u->ddir)) {
273 do_io_u_sync(td, io_u);
274 return FIO_Q_COMPLETED;
277 if (io_u->ddir == DDIR_TRIM) {
281 do_io_u_trim(td, io_u);
282 io_u_mark_submit(td, 1);
283 io_u_mark_complete(td, 1);
284 return FIO_Q_COMPLETED;
288 fio_libaio_prio_prep(td, io_u);
290 ld->iocbs[ld->head] = &io_u->iocb;
291 ld->io_us[ld->head] = io_u;
292 ring_inc(ld, &ld->head, 1);
297 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
303 if (!fio_fill_issue_time(td))
306 fio_gettime(&now, NULL);
308 for (i = 0; i < nr; i++) {
309 struct io_u *io_u = io_us[i];
311 memcpy(&io_u->issue_time, &now, sizeof(now));
312 io_u_queued(td, io_u);
316 static int fio_libaio_commit(struct thread_data *td)
318 struct libaio_data *ld = td->io_ops_data;
322 int ret, wait_start = 0;
328 long nr = ld->queued;
330 nr = min((unsigned int) nr, ld->entries - ld->tail);
331 io_us = ld->io_us + ld->tail;
332 iocbs = ld->iocbs + ld->tail;
334 ret = io_submit(ld->aio_ctx, nr, iocbs);
336 fio_libaio_queued(td, io_us, ret);
337 io_u_mark_submit(td, ret);
340 ring_inc(ld, &ld->tail, ret);
343 } else if (ret == -EINTR || !ret) {
345 io_u_mark_submit(td, ret);
348 } else if (ret == -EAGAIN) {
350 * If we get EAGAIN, we should break out without
351 * error and let the upper layer reap some
352 * events for us. If we have no queued IO, we
353 * must loop here. If we loop for more than 30s,
354 * just error out, something must be buggy in the
362 fio_gettime(&ts, NULL);
364 } else if (mtime_since_now(&ts) > 30000) {
365 log_err("fio: aio appears to be stalled, giving up\n");
370 } else if (ret == -ENOMEM) {
372 * If we get -ENOMEM, reap events if we can. If
373 * we cannot, treat it as a fatal event since there's
374 * nothing we can do about it.
381 } while (ld->queued);
386 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
388 struct libaio_data *ld = td->io_ops_data;
390 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
393 static void fio_libaio_cleanup(struct thread_data *td)
395 struct libaio_data *ld = td->io_ops_data;
399 * Work-around to avoid huge RCU stalls at exit time. If we
400 * don't do this here, then it'll be torn down by exit_aio().
401 * But for that case we can parallellize the freeing, thus
402 * speeding it up a lot.
404 if (!(td->flags & TD_F_CHILD))
405 io_destroy(ld->aio_ctx);
406 free(ld->aio_events);
413 static int fio_libaio_post_init(struct thread_data *td)
415 struct libaio_data *ld = td->io_ops_data;
418 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
420 td_verror(td, -err, "io_queue_init");
427 static int fio_libaio_init(struct thread_data *td)
429 struct libaio_data *ld;
430 struct libaio_options *o = td->eo;
431 struct cmdprio *cmdprio = &o->cmdprio;
434 ld = calloc(1, sizeof(*ld));
436 ld->entries = td->o.iodepth;
437 ld->is_pow2 = is_power_of_2(ld->entries);
438 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
439 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
440 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
442 td->io_ops_data = ld;
444 ret = fio_cmdprio_init(td, cmdprio, &ld->use_cmdprio);
446 td_verror(td, EINVAL, "fio_libaio_init");
453 FIO_STATIC struct ioengine_ops ioengine = {
455 .version = FIO_IOOPS_VERSION,
456 .flags = FIO_ASYNCIO_SYNC_TRIM,
457 .init = fio_libaio_init,
458 .post_init = fio_libaio_post_init,
459 .prep = fio_libaio_prep,
460 .queue = fio_libaio_queue,
461 .commit = fio_libaio_commit,
462 .cancel = fio_libaio_cancel,
463 .getevents = fio_libaio_getevents,
464 .event = fio_libaio_event,
465 .cleanup = fio_libaio_cleanup,
466 .open_file = generic_open_file,
467 .close_file = generic_close_file,
468 .get_file_size = generic_get_file_size,
470 .option_struct_size = sizeof(struct libaio_options),
473 static void fio_init fio_libaio_register(void)
475 register_ioengine(&ioengine);
478 static void fio_exit fio_libaio_unregister(void)
480 unregister_ioengine(&ioengine);