4 * IO engine using the Linux native aio interface.
13 #include "../lib/pow2.h"
14 #include "../optgroup.h"
16 #define IOCB_CMD_PREAD_POLL 9
17 #define IOCB_CMD_PWRITE_POLL 10
19 static int fio_libaio_commit(struct thread_data *td);
23 struct io_event *aio_events;
28 * Basic ring buffer. 'head' is incremented in _queue(), and
29 * 'tail' is incremented in _commit(). We keep 'queued' so
30 * that we know if the ring is full or empty, when
31 * 'head' == 'tail'. 'entries' is the ring size, and
32 * 'is_pow2' is just an optimization to use AND instead of
33 * modulus to get the remainder on ring increment.
42 struct libaio_options {
44 unsigned int userspace_reap;
48 static struct fio_option options[] = {
50 .name = "userspace_reap",
51 .lname = "Libaio userspace reaping",
52 .type = FIO_OPT_STR_SET,
53 .off1 = offsetof(struct libaio_options, userspace_reap),
54 .help = "Use alternative user-space reap implementation",
55 .category = FIO_OPT_C_ENGINE,
56 .group = FIO_OPT_G_LIBAIO,
61 .type = FIO_OPT_STR_SET,
62 .off1 = offsetof(struct libaio_options, hipri),
63 .help = "Use polled IO completions",
64 .category = FIO_OPT_C_ENGINE,
65 .group = FIO_OPT_G_LIBAIO,
72 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
76 *val = (*val + add) & (ld->entries - 1);
78 *val = (*val + add) % ld->entries;
81 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
83 struct fio_file *f = io_u->file;
84 struct libaio_options *o = td->eo;
86 if (io_u->ddir == DDIR_READ) {
87 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
89 io_u->iocb.aio_lio_opcode = IOCB_CMD_PREAD_POLL;
90 } else if (io_u->ddir == DDIR_WRITE) {
91 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
93 io_u->iocb.aio_lio_opcode = IOCB_CMD_PWRITE_POLL;
94 } else if (ddir_sync(io_u->ddir))
95 io_prep_fsync(&io_u->iocb, f->fd);
100 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
102 struct libaio_data *ld = td->io_ops_data;
106 ev = ld->aio_events + event;
107 io_u = container_of(ev->obj, struct io_u, iocb);
109 if (ev->res != io_u->xfer_buflen) {
110 if (ev->res > io_u->xfer_buflen)
111 io_u->error = -ev->res;
113 io_u->resid = io_u->xfer_buflen - ev->res;
121 unsigned id; /** kernel internal index number */
122 unsigned nr; /** number of io_events */
127 unsigned compat_features;
128 unsigned incompat_features;
129 unsigned header_length; /** size of aio_ring */
131 struct io_event events[0];
134 #define AIO_RING_MAGIC 0xa10a10a1
136 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
137 struct io_event *events)
141 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
146 if (head == ring->tail) {
147 /* There are no more completions */
150 /* There is another completion to reap */
151 events[i] = ring->events[head];
153 ring->head = (head + 1) % ring->nr;
161 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
162 unsigned int max, const struct timespec *t)
164 struct libaio_data *ld = td->io_ops_data;
165 struct libaio_options *o = td->eo;
166 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
167 struct timespec __lt, *lt = NULL;
176 if (o->userspace_reap == 1
178 && ((struct aio_ring *)(ld->aio_ctx))->magic
180 r = user_io_getevents(ld->aio_ctx, max,
181 ld->aio_events + events);
183 r = io_getevents(ld->aio_ctx, actual_min,
184 max, ld->aio_events + events, lt);
188 else if ((min && r == 0) || r == -EAGAIN) {
189 fio_libaio_commit(td);
192 } else if (r != -EINTR)
194 } while (events < min);
196 return r < 0 ? r : events;
199 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
202 struct libaio_data *ld = td->io_ops_data;
204 fio_ro_check(td, io_u);
206 if (ld->queued == td->o.iodepth)
210 * fsync is tricky, since it can fail and we need to do it
211 * serialized with other io. the reason is that linux doesn't
212 * support aio fsync yet. So return busy for the case where we
213 * have pending io, to let fio complete those first.
215 if (ddir_sync(io_u->ddir)) {
219 do_io_u_sync(td, io_u);
220 return FIO_Q_COMPLETED;
223 if (io_u->ddir == DDIR_TRIM) {
227 do_io_u_trim(td, io_u);
228 io_u_mark_submit(td, 1);
229 io_u_mark_complete(td, 1);
230 return FIO_Q_COMPLETED;
233 ld->iocbs[ld->head] = &io_u->iocb;
234 ld->io_us[ld->head] = io_u;
235 ring_inc(ld, &ld->head, 1);
240 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
246 if (!fio_fill_issue_time(td))
249 fio_gettime(&now, NULL);
251 for (i = 0; i < nr; i++) {
252 struct io_u *io_u = io_us[i];
254 memcpy(&io_u->issue_time, &now, sizeof(now));
255 io_u_queued(td, io_u);
259 static int fio_libaio_commit(struct thread_data *td)
261 struct libaio_data *ld = td->io_ops_data;
265 int ret, wait_start = 0;
271 long nr = ld->queued;
273 nr = min((unsigned int) nr, ld->entries - ld->tail);
274 io_us = ld->io_us + ld->tail;
275 iocbs = ld->iocbs + ld->tail;
277 ret = io_submit(ld->aio_ctx, nr, iocbs);
279 fio_libaio_queued(td, io_us, ret);
280 io_u_mark_submit(td, ret);
283 ring_inc(ld, &ld->tail, ret);
286 } else if (ret == -EINTR || !ret) {
288 io_u_mark_submit(td, ret);
291 } else if (ret == -EAGAIN) {
293 * If we get EAGAIN, we should break out without
294 * error and let the upper layer reap some
295 * events for us. If we have no queued IO, we
296 * must loop here. If we loop for more than 30s,
297 * just error out, something must be buggy in the
305 fio_gettime(&ts, NULL);
307 } else if (mtime_since_now(&ts) > 30000) {
308 log_err("fio: aio appears to be stalled, giving up\n");
313 } else if (ret == -ENOMEM) {
315 * If we get -ENOMEM, reap events if we can. If
316 * we cannot, treat it as a fatal event since there's
317 * nothing we can do about it.
324 } while (ld->queued);
329 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
331 struct libaio_data *ld = td->io_ops_data;
333 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
336 static void fio_libaio_cleanup(struct thread_data *td)
338 struct libaio_data *ld = td->io_ops_data;
342 * Work-around to avoid huge RCU stalls at exit time. If we
343 * don't do this here, then it'll be torn down by exit_aio().
344 * But for that case we can parallellize the freeing, thus
345 * speeding it up a lot.
347 if (!(td->flags & TD_F_CHILD))
348 io_destroy(ld->aio_ctx);
349 free(ld->aio_events);
356 static int fio_libaio_init(struct thread_data *td)
358 struct libaio_options *o = td->eo;
359 struct libaio_data *ld;
362 ld = calloc(1, sizeof(*ld));
365 * First try passing in 0 for queue depth, since we don't
366 * care about the user ring. If that fails, the kernel is too old
367 * and we need the right depth.
369 if (!o->userspace_reap)
370 err = io_queue_init(INT_MAX, &ld->aio_ctx);
371 if (o->userspace_reap || err == -EINVAL)
372 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
374 td_verror(td, -err, "io_queue_init");
375 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
380 ld->entries = td->o.iodepth;
381 ld->is_pow2 = is_power_of_2(ld->entries);
382 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
383 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
384 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
386 td->io_ops_data = ld;
390 static struct ioengine_ops ioengine = {
392 .version = FIO_IOOPS_VERSION,
393 .init = fio_libaio_init,
394 .prep = fio_libaio_prep,
395 .queue = fio_libaio_queue,
396 .commit = fio_libaio_commit,
397 .cancel = fio_libaio_cancel,
398 .getevents = fio_libaio_getevents,
399 .event = fio_libaio_event,
400 .cleanup = fio_libaio_cleanup,
401 .open_file = generic_open_file,
402 .close_file = generic_close_file,
403 .get_file_size = generic_get_file_size,
405 .option_struct_size = sizeof(struct libaio_options),
408 static void fio_init fio_libaio_register(void)
410 register_ioengine(&ioengine);
413 static void fio_exit fio_libaio_unregister(void)
415 unregister_ioengine(&ioengine);