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;
40 struct iovec *iovecs; /* for vectored requests */
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.
56 struct cmdprio cmdprio;
59 struct libaio_options {
60 struct thread_data *td;
61 unsigned int userspace_reap;
62 struct cmdprio_options cmdprio_options;
64 unsigned int vectored;
67 static struct fio_option options[] = {
69 .name = "userspace_reap",
70 .lname = "Libaio userspace reaping",
71 .type = FIO_OPT_STR_SET,
72 .off1 = offsetof(struct libaio_options, userspace_reap),
73 .help = "Use alternative user-space reap implementation",
74 .category = FIO_OPT_C_ENGINE,
75 .group = FIO_OPT_G_LIBAIO,
79 .lname = "RWF_NOWAIT",
81 .off1 = offsetof(struct libaio_options, nowait),
82 .help = "Set RWF_NOWAIT for reads/writes",
83 .category = FIO_OPT_C_ENGINE,
84 .group = FIO_OPT_G_LIBAIO,
87 .name = "libaio_vectored",
88 .lname = "Use libaio preadv,pwritev",
90 .off1 = offsetof(struct libaio_options, vectored),
91 .help = "Use libaio {preadv,pwritev} instead of libaio {pread,pwrite}",
92 .category = FIO_OPT_C_ENGINE,
93 .group = FIO_OPT_G_LIBAIO,
96 CMDPRIO_OPTIONS(struct libaio_options, FIO_OPT_G_LIBAIO),
102 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
106 *val = (*val + add) & (ld->entries - 1);
108 *val = (*val + add) % ld->entries;
111 static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
113 struct libaio_options *o = td->eo;
114 struct fio_file *f = io_u->file;
115 struct iocb *iocb = &io_u->iocb;
116 struct libaio_data *ld = td->io_ops_data;
118 if (io_u->ddir == DDIR_READ) {
120 struct iovec *iov = &ld->iovecs[io_u->index];
122 iov->iov_base = io_u->xfer_buf;
123 iov->iov_len = (size_t)io_u->xfer_buflen;
124 io_prep_preadv(iocb, f->fd, iov, 1, io_u->offset);
126 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen,
130 iocb->aio_rw_flags |= RWF_NOWAIT;
131 } else if (io_u->ddir == DDIR_WRITE) {
133 struct iovec *iov = &ld->iovecs[io_u->index];
135 iov->iov_base = io_u->xfer_buf;
136 iov->iov_len = (size_t)io_u->xfer_buflen;
137 io_prep_pwritev(iocb, f->fd, iov, 1, io_u->offset);
139 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen,
143 iocb->aio_rw_flags |= RWF_NOWAIT;
144 #ifdef FIO_HAVE_RWF_ATOMIC
146 iocb->aio_rw_flags |= RWF_ATOMIC;
148 } else if (ddir_sync(io_u->ddir))
149 io_prep_fsync(iocb, f->fd);
154 static inline void fio_libaio_cmdprio_prep(struct thread_data *td,
157 struct libaio_data *ld = td->io_ops_data;
158 struct cmdprio *cmdprio = &ld->cmdprio;
160 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u)) {
161 io_u->iocb.aio_reqprio = io_u->ioprio;
162 io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO;
166 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
168 struct libaio_data *ld = td->io_ops_data;
172 ev = ld->aio_events + event;
173 io_u = container_of(ev->obj, struct io_u, iocb);
175 if (ev->res != io_u->xfer_buflen) {
176 if (ev->res > io_u->xfer_buflen)
177 io_u->error = -ev->res;
179 io_u->resid = io_u->xfer_buflen - ev->res;
187 unsigned id; /** kernel internal index number */
188 unsigned nr; /** number of io_events */
193 unsigned compat_features;
194 unsigned incompat_features;
195 unsigned header_length; /** size of aio_ring */
197 struct io_event events[0];
200 #define AIO_RING_MAGIC 0xa10a10a1
202 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
203 struct io_event *events)
207 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
212 if (head == ring->tail) {
213 /* There are no more completions */
216 /* There is another completion to reap */
217 events[i] = ring->events[head];
218 atomic_store_release(&ring->head,
219 (head + 1) % ring->nr);
227 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
228 unsigned int max, const struct timespec *t)
230 struct libaio_data *ld = td->io_ops_data;
231 struct libaio_options *o = td->eo;
232 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
233 struct timespec __lt, *lt = NULL;
242 if (o->userspace_reap == 1
244 && ((struct aio_ring *)(ld->aio_ctx))->magic
246 r = user_io_getevents(ld->aio_ctx, max - events,
247 ld->aio_events + events);
249 r = io_getevents(ld->aio_ctx, actual_min,
250 max - events, ld->aio_events + events, lt);
254 actual_min -= min((unsigned int)events, actual_min);
256 else if ((min && r == 0) || r == -EAGAIN) {
257 fio_libaio_commit(td);
260 } else if (r != -EINTR)
262 } while (events < min);
264 return r < 0 ? r : events;
267 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
270 struct libaio_data *ld = td->io_ops_data;
272 fio_ro_check(td, io_u);
274 if (ld->queued == td->o.iodepth)
278 * fsync is tricky, since it can fail and we need to do it
279 * serialized with other io. the reason is that linux doesn't
280 * support aio fsync yet. So return busy for the case where we
281 * have pending io, to let fio complete those first.
283 if (ddir_sync(io_u->ddir)) {
287 do_io_u_sync(td, io_u);
288 return FIO_Q_COMPLETED;
291 if (io_u->ddir == DDIR_TRIM) {
295 do_io_u_trim(td, io_u);
296 io_u_mark_submit(td, 1);
297 io_u_mark_complete(td, 1);
298 return FIO_Q_COMPLETED;
301 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
302 fio_libaio_cmdprio_prep(td, io_u);
304 ld->iocbs[ld->head] = &io_u->iocb;
305 ld->io_us[ld->head] = io_u;
306 ring_inc(ld, &ld->head, 1);
311 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
317 if (!fio_fill_issue_time(td))
320 fio_gettime(&now, NULL);
322 for (i = 0; i < nr; i++) {
323 struct io_u *io_u = io_us[i];
325 memcpy(&io_u->issue_time, &now, sizeof(now));
326 io_u_queued(td, io_u);
330 * only used for iolog
332 if (td->o.read_iolog_file)
333 memcpy(&td->last_issue, &now, sizeof(now));
336 static int fio_libaio_commit(struct thread_data *td)
338 struct libaio_data *ld = td->io_ops_data;
342 int ret, wait_start = 0;
348 long nr = ld->queued;
350 nr = min((unsigned int) nr, ld->entries - ld->tail);
351 io_us = ld->io_us + ld->tail;
352 iocbs = ld->iocbs + ld->tail;
354 ret = io_submit(ld->aio_ctx, nr, iocbs);
356 fio_libaio_queued(td, io_us, ret);
357 io_u_mark_submit(td, ret);
360 ring_inc(ld, &ld->tail, ret);
363 } else if (ret == -EINTR || !ret) {
365 io_u_mark_submit(td, ret);
368 } else if (ret == -EAGAIN) {
370 * If we get EAGAIN, we should break out without
371 * error and let the upper layer reap some
372 * events for us. If we have no queued IO, we
373 * must loop here. If we loop for more than 30s,
374 * just error out, something must be buggy in the
382 fio_gettime(&ts, NULL);
384 } else if (mtime_since_now(&ts) > 30000) {
385 log_err("fio: aio appears to be stalled, giving up\n");
390 } else if (ret == -ENOMEM) {
392 * If we get -ENOMEM, reap events if we can. If
393 * we cannot, treat it as a fatal event since there's
394 * nothing we can do about it.
401 } while (ld->queued);
406 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
408 struct libaio_data *ld = td->io_ops_data;
410 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
413 static void fio_libaio_cleanup(struct thread_data *td)
415 struct libaio_data *ld = td->io_ops_data;
419 * Work-around to avoid huge RCU stalls at exit time. If we
420 * don't do this here, then it'll be torn down by exit_aio().
421 * But for that case we can parallellize the freeing, thus
422 * speeding it up a lot.
424 if (!(td->flags & TD_F_CHILD))
425 io_destroy(ld->aio_ctx);
427 fio_cmdprio_cleanup(&ld->cmdprio);
429 free(ld->aio_events);
436 static int fio_libaio_post_init(struct thread_data *td)
438 struct libaio_data *ld = td->io_ops_data;
441 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
443 td_verror(td, -err, "io_queue_init");
450 static int fio_libaio_init(struct thread_data *td)
452 struct libaio_data *ld;
453 struct libaio_options *o = td->eo;
456 ld = calloc(1, sizeof(*ld));
458 ld->entries = td->o.iodepth;
459 ld->is_pow2 = is_power_of_2(ld->entries);
460 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
461 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
462 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
463 ld->iovecs = calloc(ld->entries, sizeof(ld->iovecs[0]));
465 td->io_ops_data = ld;
467 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
469 td_verror(td, EINVAL, "fio_libaio_init");
476 FIO_STATIC struct ioengine_ops ioengine = {
478 .version = FIO_IOOPS_VERSION,
479 .flags = FIO_ASYNCIO_SYNC_TRIM |
480 FIO_ASYNCIO_SETS_ISSUE_TIME |
482 .init = fio_libaio_init,
483 .post_init = fio_libaio_post_init,
484 .prep = fio_libaio_prep,
485 .queue = fio_libaio_queue,
486 .commit = fio_libaio_commit,
487 .cancel = fio_libaio_cancel,
488 .getevents = fio_libaio_getevents,
489 .event = fio_libaio_event,
490 .cleanup = fio_libaio_cleanup,
491 .open_file = generic_open_file,
492 .close_file = generic_close_file,
493 .get_file_size = generic_get_file_size,
495 .option_struct_size = sizeof(struct libaio_options),
498 static void fio_init fio_libaio_register(void)
500 register_ioengine(&ioengine);
503 static void fio_exit fio_libaio_unregister(void)
505 unregister_ioengine(&ioengine);