4 * IO engine using the Linux native aio interface.
13 #include "../lib/pow2.h"
14 #include "../optgroup.h"
15 #include "../lib/memalign.h"
17 #ifndef IOCB_FLAG_HIPRI
18 #define IOCB_FLAG_HIPRI (1 << 2)
21 #ifndef IOCTX_FLAG_USERIOCB
22 #define IOCTX_FLAG_USERIOCB (1 << 0)
24 #ifndef IOCTX_FLAG_IOPOLL
25 #define IOCTX_FLAG_IOPOLL (1 << 1)
27 #ifndef IOCTX_FLAG_FIXEDBUFS
28 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
31 static int fio_libaio_commit(struct thread_data *td);
35 struct io_event *aio_events;
39 struct iocb *user_iocbs;
40 struct io_u **io_u_index;
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.
57 struct libaio_options {
59 unsigned int userspace_reap;
61 unsigned int useriocb;
62 unsigned int fixedbufs;
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,
77 .lname = "High Priority",
78 .type = FIO_OPT_STR_SET,
79 .off1 = offsetof(struct libaio_options, hipri),
80 .help = "Use polled IO completions",
81 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_LIBAIO,
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,
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,
107 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
111 *val = (*val + add) & (ld->entries - 1);
113 *val = (*val + add) % ld->entries;
116 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
118 struct libaio_data *ld = td->io_ops_data;
119 struct fio_file *f = io_u->file;
120 struct libaio_options *o = td->eo;
124 iocb = &ld->user_iocbs[io_u->index];
128 if (io_u->ddir == DDIR_READ) {
130 iocb->aio_fildes = f->fd;
131 iocb->aio_lio_opcode = IO_CMD_PREAD;
132 iocb->u.c.offset = io_u->offset;
134 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
136 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
138 } else if (io_u->ddir == DDIR_WRITE) {
140 iocb->aio_fildes = f->fd;
141 iocb->aio_lio_opcode = IO_CMD_PWRITE;
142 iocb->u.c.offset = io_u->offset;
144 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
146 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
148 } else if (ddir_sync(io_u->ddir))
149 io_prep_fsync(iocb, f->fd);
154 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
156 struct libaio_data *ld = td->io_ops_data;
157 struct libaio_options *o = td->eo;
161 ev = ld->aio_events + event;
163 int index = (int) (uintptr_t) ev->obj;
164 io_u = ld->io_u_index[index];
166 io_u = container_of(ev->obj, struct io_u, iocb);
168 if (ev->res != io_u->xfer_buflen) {
169 if (ev->res > io_u->xfer_buflen)
170 io_u->error = -ev->res;
172 io_u->resid = io_u->xfer_buflen - ev->res;
180 unsigned id; /** kernel internal index number */
181 unsigned nr; /** number of io_events */
186 unsigned compat_features;
187 unsigned incompat_features;
188 unsigned header_length; /** size of aio_ring */
190 struct io_event events[0];
193 #define AIO_RING_MAGIC 0xa10a10a1
195 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
196 struct io_event *events)
200 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
205 if (head == ring->tail) {
206 /* There are no more completions */
209 /* There is another completion to reap */
210 events[i] = ring->events[head];
212 ring->head = (head + 1) % ring->nr;
220 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
221 unsigned int max, const struct timespec *t)
223 struct libaio_data *ld = td->io_ops_data;
224 struct libaio_options *o = td->eo;
225 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
226 struct timespec __lt, *lt = NULL;
235 if (o->userspace_reap == 1
237 && ((struct aio_ring *)(ld->aio_ctx))->magic
239 r = user_io_getevents(ld->aio_ctx, max,
240 ld->aio_events + events);
242 r = io_getevents(ld->aio_ctx, actual_min,
243 max, ld->aio_events + events, lt);
247 else if ((min && r == 0) || r == -EAGAIN) {
248 fio_libaio_commit(td);
251 } else if (r != -EINTR)
253 } while (events < min);
255 return r < 0 ? r : events;
258 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
261 struct libaio_data *ld = td->io_ops_data;
262 struct libaio_options *o = td->eo;
264 fio_ro_check(td, io_u);
266 if (ld->queued == td->o.iodepth)
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.
275 if (ddir_sync(io_u->ddir)) {
279 do_io_u_sync(td, io_u);
280 return FIO_Q_COMPLETED;
283 if (io_u->ddir == DDIR_TRIM) {
287 do_io_u_trim(td, io_u);
288 io_u_mark_submit(td, 1);
289 io_u_mark_complete(td, 1);
290 return FIO_Q_COMPLETED;
294 ld->iocbs[ld->head] = (struct iocb *) (uintptr_t) io_u->index;
296 ld->iocbs[ld->head] = &io_u->iocb;
298 ld->io_us[ld->head] = io_u;
299 ring_inc(ld, &ld->head, 1);
304 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
310 if (!fio_fill_issue_time(td))
313 fio_gettime(&now, NULL);
315 for (i = 0; i < nr; i++) {
316 struct io_u *io_u = io_us[i];
318 memcpy(&io_u->issue_time, &now, sizeof(now));
319 io_u_queued(td, io_u);
323 static int fio_libaio_commit(struct thread_data *td)
325 struct libaio_data *ld = td->io_ops_data;
329 int ret, wait_start = 0;
335 long nr = ld->queued;
337 nr = min((unsigned int) nr, ld->entries - ld->tail);
338 io_us = ld->io_us + ld->tail;
339 iocbs = ld->iocbs + ld->tail;
341 ret = io_submit(ld->aio_ctx, nr, iocbs);
343 fio_libaio_queued(td, io_us, ret);
344 io_u_mark_submit(td, ret);
347 ring_inc(ld, &ld->tail, ret);
350 } else if (ret == -EINTR || !ret) {
352 io_u_mark_submit(td, ret);
355 } else if (ret == -EAGAIN) {
357 * If we get EAGAIN, we should break out without
358 * error and let the upper layer reap some
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
369 fio_gettime(&ts, NULL);
371 } else if (mtime_since_now(&ts) > 30000) {
372 log_err("fio: aio appears to be stalled, giving up\n");
377 } else if (ret == -ENOMEM) {
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.
388 } while (ld->queued);
393 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
395 struct libaio_data *ld = td->io_ops_data;
397 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
400 static void fio_libaio_cleanup(struct thread_data *td)
402 struct libaio_data *ld = td->io_ops_data;
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.
411 if (!(td->flags & TD_F_CHILD))
412 io_destroy(ld->aio_ctx);
413 free(ld->aio_events);
416 if (ld->user_iocbs) {
417 size_t size = td->o.iodepth * sizeof(struct iocb);
418 fio_memfree(ld->user_iocbs, size, false);
424 static int fio_libaio_old_queue_init(struct libaio_data *ld, unsigned int depth,
425 bool hipri, bool useriocb, bool fixedbufs)
428 log_err("fio: polled aio not available on your platform\n");
432 log_err("fio: user mapped iocbs not available on your platform\n");
436 log_err("fio: fixed buffers not available on your platform\n");
440 return io_queue_init(depth, &ld->aio_ctx);
443 static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
444 bool hipri, bool useriocb, bool fixedbufs)
446 #ifdef __NR_sys_io_setup2
450 flags |= IOCTX_FLAG_IOPOLL;
452 flags |= IOCTX_FLAG_USERIOCB;
454 flags |= IOCTX_FLAG_FIXEDBUFS;
456 ret = syscall(__NR_sys_io_setup2, depth, flags, ld->user_iocbs,
460 /* fall through to old syscall */
462 return fio_libaio_old_queue_init(ld, depth, hipri, useriocb, fixedbufs);
465 static int fio_libaio_post_init(struct thread_data *td)
467 struct libaio_data *ld = td->io_ops_data;
468 struct libaio_options *o = td->eo;
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;
480 iocb->u.c.nbytes = td_max_bs(td);
484 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
488 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri, o->useriocb,
491 td_verror(td, -err, "io_queue_init");
498 static int fio_libaio_init(struct thread_data *td)
500 struct libaio_options *o = td->eo;
501 struct libaio_data *ld;
503 ld = calloc(1, sizeof(*ld));
508 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
509 size = td->o.iodepth * sizeof(struct iocb);
510 ld->user_iocbs = fio_memalign(page_size, size, false);
511 memset(ld->user_iocbs, 0, size);
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 *));
520 td->io_ops_data = ld;
524 static int fio_libaio_io_u_init(struct thread_data *td, struct io_u *io_u)
526 struct libaio_options *o = td->eo;
529 struct libaio_data *ld = td->io_ops_data;
531 ld->io_u_index[io_u->index] = io_u;
537 static struct ioengine_ops ioengine = {
539 .version = FIO_IOOPS_VERSION,
540 .init = fio_libaio_init,
541 .post_init = fio_libaio_post_init,
542 .io_u_init = fio_libaio_io_u_init,
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,
554 .option_struct_size = sizeof(struct libaio_options),
557 static void fio_init fio_libaio_register(void)
559 register_ioengine(&ioengine);
562 static void fio_exit fio_libaio_unregister(void)
564 unregister_ioengine(&ioengine);