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)
28 static int fio_libaio_commit(struct thread_data *td);
32 struct io_event *aio_events;
36 struct iocb *user_iocbs;
37 struct io_u **io_u_index;
40 * Basic ring buffer. 'head' is incremented in _queue(), and
41 * 'tail' is incremented in _commit(). We keep 'queued' so
42 * that we know if the ring is full or empty, when
43 * 'head' == 'tail'. 'entries' is the ring size, and
44 * 'is_pow2' is just an optimization to use AND instead of
45 * modulus to get the remainder on ring increment.
54 struct libaio_options {
56 unsigned int userspace_reap;
58 unsigned int useriocb;
61 static struct fio_option options[] = {
63 .name = "userspace_reap",
64 .lname = "Libaio userspace reaping",
65 .type = FIO_OPT_STR_SET,
66 .off1 = offsetof(struct libaio_options, userspace_reap),
67 .help = "Use alternative user-space reap implementation",
68 .category = FIO_OPT_C_ENGINE,
69 .group = FIO_OPT_G_LIBAIO,
73 .lname = "High Priority",
74 .type = FIO_OPT_STR_SET,
75 .off1 = offsetof(struct libaio_options, hipri),
76 .help = "Use polled IO completions",
77 .category = FIO_OPT_C_ENGINE,
78 .group = FIO_OPT_G_LIBAIO,
82 .lname = "User IOCBs",
83 .type = FIO_OPT_STR_SET,
84 .off1 = offsetof(struct libaio_options, useriocb),
85 .help = "Use user mapped IOCBs",
86 .category = FIO_OPT_C_ENGINE,
87 .group = FIO_OPT_G_LIBAIO,
94 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
98 *val = (*val + add) & (ld->entries - 1);
100 *val = (*val + add) % ld->entries;
103 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
105 struct libaio_data *ld = td->io_ops_data;
106 struct fio_file *f = io_u->file;
107 struct libaio_options *o = td->eo;
111 iocb = &ld->user_iocbs[io_u->index];
117 if (io_u->ddir == DDIR_READ) {
118 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
120 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
121 } else if (io_u->ddir == DDIR_WRITE) {
122 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
124 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
125 } else if (ddir_sync(io_u->ddir))
126 io_prep_fsync(iocb, f->fd);
131 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
133 struct libaio_data *ld = td->io_ops_data;
134 struct libaio_options *o = td->eo;
138 ev = ld->aio_events + event;
140 int index = (int) (uintptr_t) ev->obj;
141 io_u = ld->io_u_index[index];
143 io_u = container_of(ev->obj, struct io_u, iocb);
145 if (ev->res != io_u->xfer_buflen) {
146 if (ev->res > io_u->xfer_buflen)
147 io_u->error = -ev->res;
149 io_u->resid = io_u->xfer_buflen - ev->res;
157 unsigned id; /** kernel internal index number */
158 unsigned nr; /** number of io_events */
163 unsigned compat_features;
164 unsigned incompat_features;
165 unsigned header_length; /** size of aio_ring */
167 struct io_event events[0];
170 #define AIO_RING_MAGIC 0xa10a10a1
172 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
173 struct io_event *events)
177 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
182 if (head == ring->tail) {
183 /* There are no more completions */
186 /* There is another completion to reap */
187 events[i] = ring->events[head];
189 ring->head = (head + 1) % ring->nr;
197 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
198 unsigned int max, const struct timespec *t)
200 struct libaio_data *ld = td->io_ops_data;
201 struct libaio_options *o = td->eo;
202 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
203 struct timespec __lt, *lt = NULL;
212 if (o->userspace_reap == 1
214 && ((struct aio_ring *)(ld->aio_ctx))->magic
216 r = user_io_getevents(ld->aio_ctx, max,
217 ld->aio_events + events);
219 r = io_getevents(ld->aio_ctx, actual_min,
220 max, ld->aio_events + events, lt);
224 else if ((min && r == 0) || r == -EAGAIN) {
225 fio_libaio_commit(td);
228 } else if (r != -EINTR)
230 } while (events < min);
232 return r < 0 ? r : events;
235 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
238 struct libaio_data *ld = td->io_ops_data;
239 struct libaio_options *o = td->eo;
241 fio_ro_check(td, io_u);
243 if (ld->queued == td->o.iodepth)
247 * fsync is tricky, since it can fail and we need to do it
248 * serialized with other io. the reason is that linux doesn't
249 * support aio fsync yet. So return busy for the case where we
250 * have pending io, to let fio complete those first.
252 if (ddir_sync(io_u->ddir)) {
256 do_io_u_sync(td, io_u);
257 return FIO_Q_COMPLETED;
260 if (io_u->ddir == DDIR_TRIM) {
264 do_io_u_trim(td, io_u);
265 io_u_mark_submit(td, 1);
266 io_u_mark_complete(td, 1);
267 return FIO_Q_COMPLETED;
271 ld->iocbs[ld->head] = (struct iocb *) (uintptr_t) io_u->index;
273 ld->iocbs[ld->head] = &io_u->iocb;
275 ld->io_us[ld->head] = io_u;
276 ring_inc(ld, &ld->head, 1);
281 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
287 if (!fio_fill_issue_time(td))
290 fio_gettime(&now, NULL);
292 for (i = 0; i < nr; i++) {
293 struct io_u *io_u = io_us[i];
295 memcpy(&io_u->issue_time, &now, sizeof(now));
296 io_u_queued(td, io_u);
300 static int fio_libaio_commit(struct thread_data *td)
302 struct libaio_data *ld = td->io_ops_data;
306 int ret, wait_start = 0;
312 long nr = ld->queued;
314 nr = min((unsigned int) nr, ld->entries - ld->tail);
315 io_us = ld->io_us + ld->tail;
316 iocbs = ld->iocbs + ld->tail;
318 ret = io_submit(ld->aio_ctx, nr, iocbs);
320 fio_libaio_queued(td, io_us, ret);
321 io_u_mark_submit(td, ret);
324 ring_inc(ld, &ld->tail, ret);
327 } else if (ret == -EINTR || !ret) {
329 io_u_mark_submit(td, ret);
332 } else if (ret == -EAGAIN) {
334 * If we get EAGAIN, we should break out without
335 * error and let the upper layer reap some
336 * events for us. If we have no queued IO, we
337 * must loop here. If we loop for more than 30s,
338 * just error out, something must be buggy in the
346 fio_gettime(&ts, NULL);
348 } else if (mtime_since_now(&ts) > 30000) {
349 log_err("fio: aio appears to be stalled, giving up\n");
354 } else if (ret == -ENOMEM) {
356 * If we get -ENOMEM, reap events if we can. If
357 * we cannot, treat it as a fatal event since there's
358 * nothing we can do about it.
365 } while (ld->queued);
370 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
372 struct libaio_data *ld = td->io_ops_data;
374 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
377 static void fio_libaio_cleanup(struct thread_data *td)
379 struct libaio_data *ld = td->io_ops_data;
383 * Work-around to avoid huge RCU stalls at exit time. If we
384 * don't do this here, then it'll be torn down by exit_aio().
385 * But for that case we can parallellize the freeing, thus
386 * speeding it up a lot.
388 if (!(td->flags & TD_F_CHILD))
389 io_destroy(ld->aio_ctx);
390 free(ld->aio_events);
393 if (ld->user_iocbs) {
394 size_t size = td->o.iodepth * sizeof(struct iocb);
395 fio_memfree(ld->user_iocbs, size, false);
401 static int fio_libaio_old_queue_init(struct libaio_data *ld, unsigned int depth,
402 bool hipri, bool useriocb)
405 log_err("fio: polled aio not available on your platform\n");
409 log_err("fio: user mapped iocbs not available on your platform\n");
413 return io_queue_init(depth, &ld->aio_ctx);
416 static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
417 bool hipri, bool useriocb)
419 #ifdef __NR_sys_io_setup2
423 flags |= IOCTX_FLAG_IOPOLL;
425 flags |= IOCTX_FLAG_USERIOCB;
427 ret = syscall(__NR_sys_io_setup2, depth, flags, ld->user_iocbs,
431 /* fall through to old syscall */
433 return fio_libaio_old_queue_init(ld, depth, hipri, useriocb);
436 static int fio_libaio_init(struct thread_data *td)
438 struct libaio_options *o = td->eo;
439 struct libaio_data *ld;
442 ld = calloc(1, sizeof(*ld));
447 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
448 size = td->o.iodepth * sizeof(struct iocb);
449 ld->user_iocbs = fio_memalign(page_size, size, false);
450 memset(ld->user_iocbs, 0, size);
454 * First try passing in 0 for queue depth, since we don't
455 * care about the user ring. If that fails, the kernel is too old
456 * and we need the right depth.
458 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri, o->useriocb);
460 td_verror(td, -err, "io_queue_init");
461 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
462 if (ld->user_iocbs) {
463 size_t size = td->o.iodepth * sizeof(struct iocb);
464 fio_memfree(ld->user_iocbs, size, false);
470 ld->entries = td->o.iodepth;
471 ld->is_pow2 = is_power_of_2(ld->entries);
472 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
473 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
474 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
476 td->io_ops_data = ld;
480 static int fio_libaio_io_u_init(struct thread_data *td, struct io_u *io_u)
482 struct libaio_options *o = td->eo;
485 struct libaio_data *ld = td->io_ops_data;
487 ld->io_u_index[io_u->index] = io_u;
493 static struct ioengine_ops ioengine = {
495 .version = FIO_IOOPS_VERSION,
496 .init = fio_libaio_init,
497 .io_u_init = fio_libaio_io_u_init,
498 .prep = fio_libaio_prep,
499 .queue = fio_libaio_queue,
500 .commit = fio_libaio_commit,
501 .cancel = fio_libaio_cancel,
502 .getevents = fio_libaio_getevents,
503 .event = fio_libaio_event,
504 .cleanup = fio_libaio_cleanup,
505 .open_file = generic_open_file,
506 .close_file = generic_close_file,
507 .get_file_size = generic_get_file_size,
509 .option_struct_size = sizeof(struct libaio_options),
512 static void fio_init fio_libaio_register(void)
514 register_ioengine(&ioengine);
517 static void fio_exit fio_libaio_unregister(void)
519 unregister_ioengine(&ioengine);