4 * IO engine using the Linux native aio interface.
13 #include "../lib/pow2.h"
14 #include "../optgroup.h"
16 #ifndef IOCB_FLAG_HIPRI
17 #define IOCB_FLAG_HIPRI (1 << 2)
19 #ifndef IOCTX_FLAG_IOPOLL
20 #define IOCTX_FLAG_IOPOLL (1 << 0)
22 #ifndef IOCTX_FLAG_USERIOCB
23 #define IOCTX_FLAG_USERIOCB (1 << 1)
27 static int fio_libaio_commit(struct thread_data *td);
31 struct io_event *aio_events;
35 struct iocb *user_iocbs;
36 struct io_u **io_u_index;
39 * Basic ring buffer. 'head' is incremented in _queue(), and
40 * 'tail' is incremented in _commit(). We keep 'queued' so
41 * that we know if the ring is full or empty, when
42 * 'head' == 'tail'. 'entries' is the ring size, and
43 * 'is_pow2' is just an optimization to use AND instead of
44 * modulus to get the remainder on ring increment.
53 struct libaio_options {
55 unsigned int userspace_reap;
57 unsigned int useriocb;
60 static struct fio_option options[] = {
62 .name = "userspace_reap",
63 .lname = "Libaio userspace reaping",
64 .type = FIO_OPT_STR_SET,
65 .off1 = offsetof(struct libaio_options, userspace_reap),
66 .help = "Use alternative user-space reap implementation",
67 .category = FIO_OPT_C_ENGINE,
68 .group = FIO_OPT_G_LIBAIO,
72 .lname = "High Priority",
73 .type = FIO_OPT_STR_SET,
74 .off1 = offsetof(struct libaio_options, hipri),
75 .help = "Use polled IO completions",
76 .category = FIO_OPT_C_ENGINE,
77 .group = FIO_OPT_G_LIBAIO,
81 .lname = "User IOCBs",
82 .type = FIO_OPT_STR_SET,
83 .off1 = offsetof(struct libaio_options, useriocb),
84 .help = "Use user mapped IOCBs",
85 .category = FIO_OPT_C_ENGINE,
86 .group = FIO_OPT_G_LIBAIO,
93 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
97 *val = (*val + add) & (ld->entries - 1);
99 *val = (*val + add) % ld->entries;
102 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
104 struct libaio_data *ld = td->io_ops_data;
105 struct fio_file *f = io_u->file;
106 struct libaio_options *o = td->eo;
110 iocb = &ld->user_iocbs[io_u->index];
114 if (io_u->ddir == DDIR_READ) {
115 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
117 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
118 } else if (io_u->ddir == DDIR_WRITE) {
119 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
121 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
122 } else if (ddir_sync(io_u->ddir))
123 io_prep_fsync(iocb, f->fd);
128 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
130 struct libaio_data *ld = td->io_ops_data;
131 struct libaio_options *o = td->eo;
135 ev = ld->aio_events + event;
137 int index = (int) (uintptr_t) ev->obj;
138 io_u = ld->io_u_index[index];
140 io_u = container_of(ev->obj, struct io_u, iocb);
142 if (ev->res != io_u->xfer_buflen) {
143 if (ev->res > io_u->xfer_buflen)
144 io_u->error = -ev->res;
146 io_u->resid = io_u->xfer_buflen - ev->res;
154 unsigned id; /** kernel internal index number */
155 unsigned nr; /** number of io_events */
160 unsigned compat_features;
161 unsigned incompat_features;
162 unsigned header_length; /** size of aio_ring */
164 struct io_event events[0];
167 #define AIO_RING_MAGIC 0xa10a10a1
169 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
170 struct io_event *events)
174 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
179 if (head == ring->tail) {
180 /* There are no more completions */
183 /* There is another completion to reap */
184 events[i] = ring->events[head];
186 ring->head = (head + 1) % ring->nr;
194 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
195 unsigned int max, const struct timespec *t)
197 struct libaio_data *ld = td->io_ops_data;
198 struct libaio_options *o = td->eo;
199 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
200 struct timespec __lt, *lt = NULL;
209 if (o->userspace_reap == 1
211 && ((struct aio_ring *)(ld->aio_ctx))->magic
213 r = user_io_getevents(ld->aio_ctx, max,
214 ld->aio_events + events);
216 r = io_getevents(ld->aio_ctx, actual_min,
217 max, ld->aio_events + events, lt);
221 else if ((min && r == 0) || r == -EAGAIN) {
222 fio_libaio_commit(td);
225 } else if (r != -EINTR)
227 } while (events < min);
229 return r < 0 ? r : events;
232 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
235 struct libaio_data *ld = td->io_ops_data;
236 struct libaio_options *o = td->eo;
238 fio_ro_check(td, io_u);
240 if (ld->queued == td->o.iodepth)
244 * fsync is tricky, since it can fail and we need to do it
245 * serialized with other io. the reason is that linux doesn't
246 * support aio fsync yet. So return busy for the case where we
247 * have pending io, to let fio complete those first.
249 if (ddir_sync(io_u->ddir)) {
253 do_io_u_sync(td, io_u);
254 return FIO_Q_COMPLETED;
257 if (io_u->ddir == DDIR_TRIM) {
261 do_io_u_trim(td, io_u);
262 io_u_mark_submit(td, 1);
263 io_u_mark_complete(td, 1);
264 return FIO_Q_COMPLETED;
268 ld->iocbs[ld->head] = (struct iocb *) (uintptr_t) io_u->index;
270 ld->iocbs[ld->head] = &io_u->iocb;
272 ld->io_us[ld->head] = io_u;
273 ring_inc(ld, &ld->head, 1);
278 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
284 if (!fio_fill_issue_time(td))
287 fio_gettime(&now, NULL);
289 for (i = 0; i < nr; i++) {
290 struct io_u *io_u = io_us[i];
292 memcpy(&io_u->issue_time, &now, sizeof(now));
293 io_u_queued(td, io_u);
297 static int fio_libaio_commit(struct thread_data *td)
299 struct libaio_data *ld = td->io_ops_data;
303 int ret, wait_start = 0;
309 long nr = ld->queued;
311 nr = min((unsigned int) nr, ld->entries - ld->tail);
312 io_us = ld->io_us + ld->tail;
313 iocbs = ld->iocbs + ld->tail;
315 ret = io_submit(ld->aio_ctx, nr, iocbs);
317 fio_libaio_queued(td, io_us, ret);
318 io_u_mark_submit(td, ret);
321 ring_inc(ld, &ld->tail, ret);
324 } else if (ret == -EINTR || !ret) {
326 io_u_mark_submit(td, ret);
329 } else if (ret == -EAGAIN) {
331 * If we get EAGAIN, we should break out without
332 * error and let the upper layer reap some
333 * events for us. If we have no queued IO, we
334 * must loop here. If we loop for more than 30s,
335 * just error out, something must be buggy in the
343 fio_gettime(&ts, NULL);
345 } else if (mtime_since_now(&ts) > 30000) {
346 log_err("fio: aio appears to be stalled, giving up\n");
351 } else if (ret == -ENOMEM) {
353 * If we get -ENOMEM, reap events if we can. If
354 * we cannot, treat it as a fatal event since there's
355 * nothing we can do about it.
362 } while (ld->queued);
367 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
369 struct libaio_data *ld = td->io_ops_data;
371 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
374 static void fio_libaio_cleanup(struct thread_data *td)
376 struct libaio_data *ld = td->io_ops_data;
380 * Work-around to avoid huge RCU stalls at exit time. If we
381 * don't do this here, then it'll be torn down by exit_aio().
382 * But for that case we can parallellize the freeing, thus
383 * speeding it up a lot.
385 if (!(td->flags & TD_F_CHILD))
386 io_destroy(ld->aio_ctx);
387 free(ld->aio_events);
391 free(ld->user_iocbs);
396 static int fio_libaio_old_queue_init(struct libaio_data *ld, unsigned int depth,
397 bool hipri, bool useriocb)
400 log_err("fio: polled aio not available on your platform\n");
404 log_err("fio: user mapped iocbs not available on your platform\n");
408 return io_queue_init(depth, &ld->aio_ctx);
411 static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
412 bool hipri, bool useriocb)
414 #ifdef __NR_sys_io_setup2
418 flags |= IOCTX_FLAG_IOPOLL;
420 flags |= IOCTX_FLAG_USERIOCB;
422 ret = syscall(__NR_sys_io_setup2, depth, flags, ld->user_iocbs,
427 return fio_libaio_old_queue_init(ld, depth, hipri, useriocb);
429 return fio_libaio_old_queue_init(ld, depth, hipri, useriocb);
433 static int fio_libaio_init(struct thread_data *td)
435 struct libaio_options *o = td->eo;
436 struct libaio_data *ld;
439 ld = calloc(1, sizeof(*ld));
445 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
446 size = td->o.iodepth * sizeof(struct iocb);
447 if (posix_memalign(&p, page_size, size)) {
448 log_err("fio: libaio iocb allocation failure\n");
456 * First try passing in 0 for queue depth, since we don't
457 * care about the user ring. If that fails, the kernel is too old
458 * and we need the right depth.
460 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri, o->useriocb);
462 td_verror(td, -err, "io_queue_init");
463 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
465 free(ld->user_iocbs);
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);