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)
20 static int fio_libaio_commit(struct thread_data *td);
24 struct io_event *aio_events;
29 * Basic ring buffer. 'head' is incremented in _queue(), and
30 * 'tail' is incremented in _commit(). We keep 'queued' so
31 * that we know if the ring is full or empty, when
32 * 'head' == 'tail'. 'entries' is the ring size, and
33 * 'is_pow2' is just an optimization to use AND instead of
34 * modulus to get the remainder on ring increment.
43 struct libaio_options {
45 unsigned int userspace_reap;
49 static struct fio_option options[] = {
51 .name = "userspace_reap",
52 .lname = "Libaio userspace reaping",
53 .type = FIO_OPT_STR_SET,
54 .off1 = offsetof(struct libaio_options, userspace_reap),
55 .help = "Use alternative user-space reap implementation",
56 .category = FIO_OPT_C_ENGINE,
57 .group = FIO_OPT_G_LIBAIO,
61 .lname = "High Priority",
62 .type = FIO_OPT_STR_SET,
63 .off1 = offsetof(struct libaio_options, hipri),
64 .help = "Use polled IO completions",
65 .category = FIO_OPT_C_ENGINE,
66 .group = FIO_OPT_G_LIBAIO,
73 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
77 *val = (*val + add) & (ld->entries - 1);
79 *val = (*val + add) % ld->entries;
82 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
84 struct fio_file *f = io_u->file;
85 struct libaio_options *o = td->eo;
87 if (io_u->ddir == DDIR_READ) {
88 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
90 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
91 } else if (io_u->ddir == DDIR_WRITE) {
92 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
94 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
95 } else if (ddir_sync(io_u->ddir))
96 io_prep_fsync(&io_u->iocb, f->fd);
101 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
103 struct libaio_data *ld = td->io_ops_data;
107 ev = ld->aio_events + event;
108 io_u = container_of(ev->obj, struct io_u, iocb);
110 if (ev->res != io_u->xfer_buflen) {
111 if (ev->res > io_u->xfer_buflen)
112 io_u->error = -ev->res;
114 io_u->resid = io_u->xfer_buflen - ev->res;
122 unsigned id; /** kernel internal index number */
123 unsigned nr; /** number of io_events */
128 unsigned compat_features;
129 unsigned incompat_features;
130 unsigned header_length; /** size of aio_ring */
132 struct io_event events[0];
135 #define AIO_RING_MAGIC 0xa10a10a1
137 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
138 struct io_event *events)
142 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
147 if (head == ring->tail) {
148 /* There are no more completions */
151 /* There is another completion to reap */
152 events[i] = ring->events[head];
154 ring->head = (head + 1) % ring->nr;
162 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
163 unsigned int max, const struct timespec *t)
165 struct libaio_data *ld = td->io_ops_data;
166 struct libaio_options *o = td->eo;
167 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
168 struct timespec __lt, *lt = NULL;
177 if (o->userspace_reap == 1
179 && ((struct aio_ring *)(ld->aio_ctx))->magic
181 r = user_io_getevents(ld->aio_ctx, max,
182 ld->aio_events + events);
184 r = io_getevents(ld->aio_ctx, actual_min,
185 max, ld->aio_events + events, lt);
189 else if ((min && r == 0) || r == -EAGAIN) {
190 fio_libaio_commit(td);
193 } else if (r != -EINTR)
195 } while (events < min);
197 return r < 0 ? r : events;
200 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
203 struct libaio_data *ld = td->io_ops_data;
205 fio_ro_check(td, io_u);
207 if (ld->queued == td->o.iodepth)
211 * fsync is tricky, since it can fail and we need to do it
212 * serialized with other io. the reason is that linux doesn't
213 * support aio fsync yet. So return busy for the case where we
214 * have pending io, to let fio complete those first.
216 if (ddir_sync(io_u->ddir)) {
220 do_io_u_sync(td, io_u);
221 return FIO_Q_COMPLETED;
224 if (io_u->ddir == DDIR_TRIM) {
228 do_io_u_trim(td, io_u);
229 io_u_mark_submit(td, 1);
230 io_u_mark_complete(td, 1);
231 return FIO_Q_COMPLETED;
234 ld->iocbs[ld->head] = &io_u->iocb;
235 ld->io_us[ld->head] = io_u;
236 ring_inc(ld, &ld->head, 1);
241 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
247 if (!fio_fill_issue_time(td))
250 fio_gettime(&now, NULL);
252 for (i = 0; i < nr; i++) {
253 struct io_u *io_u = io_us[i];
255 memcpy(&io_u->issue_time, &now, sizeof(now));
256 io_u_queued(td, io_u);
260 static int fio_libaio_commit(struct thread_data *td)
262 struct libaio_data *ld = td->io_ops_data;
266 int ret, wait_start = 0;
272 long nr = ld->queued;
274 nr = min((unsigned int) nr, ld->entries - ld->tail);
275 io_us = ld->io_us + ld->tail;
276 iocbs = ld->iocbs + ld->tail;
278 ret = io_submit(ld->aio_ctx, nr, iocbs);
280 fio_libaio_queued(td, io_us, ret);
281 io_u_mark_submit(td, ret);
284 ring_inc(ld, &ld->tail, ret);
287 } else if (ret == -EINTR || !ret) {
289 io_u_mark_submit(td, ret);
292 } else if (ret == -EAGAIN) {
294 * If we get EAGAIN, we should break out without
295 * error and let the upper layer reap some
296 * events for us. If we have no queued IO, we
297 * must loop here. If we loop for more than 30s,
298 * just error out, something must be buggy in the
306 fio_gettime(&ts, NULL);
308 } else if (mtime_since_now(&ts) > 30000) {
309 log_err("fio: aio appears to be stalled, giving up\n");
314 } else if (ret == -ENOMEM) {
316 * If we get -ENOMEM, reap events if we can. If
317 * we cannot, treat it as a fatal event since there's
318 * nothing we can do about it.
325 } while (ld->queued);
330 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
332 struct libaio_data *ld = td->io_ops_data;
334 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
337 static void fio_libaio_cleanup(struct thread_data *td)
339 struct libaio_data *ld = td->io_ops_data;
343 * Work-around to avoid huge RCU stalls at exit time. If we
344 * don't do this here, then it'll be torn down by exit_aio().
345 * But for that case we can parallellize the freeing, thus
346 * speeding it up a lot.
348 if (!(td->flags & TD_F_CHILD))
349 io_destroy(ld->aio_ctx);
350 free(ld->aio_events);
357 static int fio_libaio_init(struct thread_data *td)
359 struct libaio_options *o = td->eo;
360 struct libaio_data *ld;
363 ld = calloc(1, sizeof(*ld));
366 * First try passing in 0 for queue depth, since we don't
367 * care about the user ring. If that fails, the kernel is too old
368 * and we need the right depth.
370 if (!o->userspace_reap)
371 err = io_queue_init(INT_MAX, &ld->aio_ctx);
372 if (o->userspace_reap || err == -EINVAL)
373 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
375 td_verror(td, -err, "io_queue_init");
376 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
381 ld->entries = td->o.iodepth;
382 ld->is_pow2 = is_power_of_2(ld->entries);
383 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
384 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
385 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
387 td->io_ops_data = ld;
391 static struct ioengine_ops ioengine = {
393 .version = FIO_IOOPS_VERSION,
394 .init = fio_libaio_init,
395 .prep = fio_libaio_prep,
396 .queue = fio_libaio_queue,
397 .commit = fio_libaio_commit,
398 .cancel = fio_libaio_cancel,
399 .getevents = fio_libaio_getevents,
400 .event = fio_libaio_event,
401 .cleanup = fio_libaio_cleanup,
402 .open_file = generic_open_file,
403 .close_file = generic_close_file,
404 .get_file_size = generic_get_file_size,
406 .option_struct_size = sizeof(struct libaio_options),
409 static void fio_init fio_libaio_register(void)
411 register_ioengine(&ioengine);
414 static void fio_exit fio_libaio_unregister(void)
416 unregister_ioengine(&ioengine);