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"
19 /* Should be defined in newest aio_abi.h */
20 #ifndef IOCB_FLAG_IOPRIO
21 #define IOCB_FLAG_IOPRIO (1 << 1)
24 /* Hack for libaio < 0.3.111 */
25 #ifndef CONFIG_LIBAIO_RW_FLAGS
26 #define aio_rw_flags __pad2
29 static int fio_libaio_commit(struct thread_data *td);
30 static int fio_libaio_init(struct thread_data *td);
34 struct io_event *aio_events;
38 struct io_u **io_u_index;
41 * Basic ring buffer. 'head' is incremented in _queue(), and
42 * 'tail' is incremented in _commit(). We keep 'queued' so
43 * that we know if the ring is full or empty, when
44 * 'head' == 'tail'. 'entries' is the ring size, and
45 * 'is_pow2' is just an optimization to use AND instead of
46 * modulus to get the remainder on ring increment.
55 struct libaio_options {
57 unsigned int userspace_reap;
58 unsigned int cmdprio_percentage;
62 static struct fio_option options[] = {
64 .name = "userspace_reap",
65 .lname = "Libaio userspace reaping",
66 .type = FIO_OPT_STR_SET,
67 .off1 = offsetof(struct libaio_options, userspace_reap),
68 .help = "Use alternative user-space reap implementation",
69 .category = FIO_OPT_C_ENGINE,
70 .group = FIO_OPT_G_LIBAIO,
72 #ifdef FIO_HAVE_IOPRIO_CLASS
74 .name = "cmdprio_percentage",
75 .lname = "high priority percentage",
77 .off1 = offsetof(struct libaio_options, cmdprio_percentage),
80 .help = "Send high priority I/O this percentage of the time",
81 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_LIBAIO,
86 .name = "cmdprio_percentage",
87 .lname = "high priority percentage",
88 .type = FIO_OPT_UNSUPPORTED,
89 .help = "Your platform does not support I/O priority classes",
94 .lname = "RWF_NOWAIT",
96 .off1 = offsetof(struct libaio_options, nowait),
97 .help = "Set RWF_NOWAIT for reads/writes",
98 .category = FIO_OPT_C_ENGINE,
99 .group = FIO_OPT_G_LIBAIO,
106 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
110 *val = (*val + add) & (ld->entries - 1);
112 *val = (*val + add) % ld->entries;
115 static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
117 struct libaio_options *o = td->eo;
118 struct fio_file *f = io_u->file;
119 struct iocb *iocb = &io_u->iocb;
121 if (io_u->ddir == DDIR_READ) {
122 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
124 iocb->aio_rw_flags |= RWF_NOWAIT;
125 } else if (io_u->ddir == DDIR_WRITE) {
126 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
128 iocb->aio_rw_flags |= RWF_NOWAIT;
129 } else if (ddir_sync(io_u->ddir))
130 io_prep_fsync(iocb, f->fd);
135 static void fio_libaio_prio_prep(struct thread_data *td, struct io_u *io_u)
137 struct libaio_options *o = td->eo;
138 if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
139 io_u->iocb.aio_reqprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
140 io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO;
141 io_u->flags |= IO_U_F_PRIORITY;
146 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
148 struct libaio_data *ld = td->io_ops_data;
152 ev = ld->aio_events + event;
153 io_u = container_of(ev->obj, struct io_u, iocb);
155 if (ev->res != io_u->xfer_buflen) {
156 if (ev->res > io_u->xfer_buflen)
157 io_u->error = -ev->res;
159 io_u->resid = io_u->xfer_buflen - ev->res;
167 unsigned id; /** kernel internal index number */
168 unsigned nr; /** number of io_events */
173 unsigned compat_features;
174 unsigned incompat_features;
175 unsigned header_length; /** size of aio_ring */
177 struct io_event events[0];
180 #define AIO_RING_MAGIC 0xa10a10a1
182 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
183 struct io_event *events)
187 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
192 if (head == ring->tail) {
193 /* There are no more completions */
196 /* There is another completion to reap */
197 events[i] = ring->events[head];
198 atomic_store_release(&ring->head,
199 (head + 1) % ring->nr);
207 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
208 unsigned int max, const struct timespec *t)
210 struct libaio_data *ld = td->io_ops_data;
211 struct libaio_options *o = td->eo;
212 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
213 struct timespec __lt, *lt = NULL;
222 if (o->userspace_reap == 1
224 && ((struct aio_ring *)(ld->aio_ctx))->magic
226 r = user_io_getevents(ld->aio_ctx, max,
227 ld->aio_events + events);
229 r = io_getevents(ld->aio_ctx, actual_min,
230 max, ld->aio_events + events, lt);
234 else if ((min && r == 0) || r == -EAGAIN) {
235 fio_libaio_commit(td);
238 } else if (r != -EINTR)
240 } while (events < min);
242 return r < 0 ? r : events;
245 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
248 struct libaio_data *ld = td->io_ops_data;
249 struct libaio_options *o = td->eo;
251 fio_ro_check(td, io_u);
253 if (ld->queued == td->o.iodepth)
257 * fsync is tricky, since it can fail and we need to do it
258 * serialized with other io. the reason is that linux doesn't
259 * support aio fsync yet. So return busy for the case where we
260 * have pending io, to let fio complete those first.
262 if (ddir_sync(io_u->ddir)) {
266 do_io_u_sync(td, io_u);
267 return FIO_Q_COMPLETED;
270 if (io_u->ddir == DDIR_TRIM) {
274 do_io_u_trim(td, io_u);
275 io_u_mark_submit(td, 1);
276 io_u_mark_complete(td, 1);
277 return FIO_Q_COMPLETED;
280 if (o->cmdprio_percentage)
281 fio_libaio_prio_prep(td, io_u);
283 ld->iocbs[ld->head] = &io_u->iocb;
284 ld->io_us[ld->head] = io_u;
285 ring_inc(ld, &ld->head, 1);
290 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
296 if (!fio_fill_issue_time(td))
299 fio_gettime(&now, NULL);
301 for (i = 0; i < nr; i++) {
302 struct io_u *io_u = io_us[i];
304 memcpy(&io_u->issue_time, &now, sizeof(now));
305 io_u_queued(td, io_u);
309 static int fio_libaio_commit(struct thread_data *td)
311 struct libaio_data *ld = td->io_ops_data;
315 int ret, wait_start = 0;
321 long nr = ld->queued;
323 nr = min((unsigned int) nr, ld->entries - ld->tail);
324 io_us = ld->io_us + ld->tail;
325 iocbs = ld->iocbs + ld->tail;
327 ret = io_submit(ld->aio_ctx, nr, iocbs);
329 fio_libaio_queued(td, io_us, ret);
330 io_u_mark_submit(td, ret);
333 ring_inc(ld, &ld->tail, ret);
336 } else if (ret == -EINTR || !ret) {
338 io_u_mark_submit(td, ret);
341 } else if (ret == -EAGAIN) {
343 * If we get EAGAIN, we should break out without
344 * error and let the upper layer reap some
345 * events for us. If we have no queued IO, we
346 * must loop here. If we loop for more than 30s,
347 * just error out, something must be buggy in the
355 fio_gettime(&ts, NULL);
357 } else if (mtime_since_now(&ts) > 30000) {
358 log_err("fio: aio appears to be stalled, giving up\n");
363 } else if (ret == -ENOMEM) {
365 * If we get -ENOMEM, reap events if we can. If
366 * we cannot, treat it as a fatal event since there's
367 * nothing we can do about it.
374 } while (ld->queued);
379 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
381 struct libaio_data *ld = td->io_ops_data;
383 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
386 static void fio_libaio_cleanup(struct thread_data *td)
388 struct libaio_data *ld = td->io_ops_data;
392 * Work-around to avoid huge RCU stalls at exit time. If we
393 * don't do this here, then it'll be torn down by exit_aio().
394 * But for that case we can parallellize the freeing, thus
395 * speeding it up a lot.
397 if (!(td->flags & TD_F_CHILD))
398 io_destroy(ld->aio_ctx);
399 free(ld->aio_events);
406 static int fio_libaio_post_init(struct thread_data *td)
408 struct libaio_data *ld = td->io_ops_data;
411 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
413 td_verror(td, -err, "io_queue_init");
420 static int fio_libaio_init(struct thread_data *td)
422 struct libaio_data *ld;
423 struct thread_options *to = &td->o;
424 struct libaio_options *o = td->eo;
426 ld = calloc(1, sizeof(*ld));
428 ld->entries = td->o.iodepth;
429 ld->is_pow2 = is_power_of_2(ld->entries);
430 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
431 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
432 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
434 td->io_ops_data = ld;
436 * Check for option conflicts
438 if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
439 o->cmdprio_percentage != 0) {
440 log_err("%s: cmdprio_percentage option and mutually exclusive "
441 "prio or prioclass option is set, exiting\n", to->name);
442 td_verror(td, EINVAL, "fio_libaio_init");
448 FIO_STATIC struct ioengine_ops ioengine = {
450 .version = FIO_IOOPS_VERSION,
451 .flags = FIO_ASYNCIO_SYNC_TRIM,
452 .init = fio_libaio_init,
453 .post_init = fio_libaio_post_init,
454 .prep = fio_libaio_prep,
455 .queue = fio_libaio_queue,
456 .commit = fio_libaio_commit,
457 .cancel = fio_libaio_cancel,
458 .getevents = fio_libaio_getevents,
459 .event = fio_libaio_event,
460 .cleanup = fio_libaio_cleanup,
461 .open_file = generic_open_file,
462 .close_file = generic_close_file,
463 .get_file_size = generic_get_file_size,
465 .option_struct_size = sizeof(struct libaio_options),
468 static void fio_init fio_libaio_register(void)
470 register_ioengine(&ioengine);
473 static void fio_exit fio_libaio_unregister(void)
475 unregister_ioengine(&ioengine);