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)
23 static int fio_libaio_commit(struct thread_data *td);
27 struct io_event *aio_events;
32 * Basic ring buffer. 'head' is incremented in _queue(), and
33 * 'tail' is incremented in _commit(). We keep 'queued' so
34 * that we know if the ring is full or empty, when
35 * 'head' == 'tail'. 'entries' is the ring size, and
36 * 'is_pow2' is just an optimization to use AND instead of
37 * modulus to get the remainder on ring increment.
46 struct libaio_options {
48 unsigned int userspace_reap;
52 static struct fio_option options[] = {
54 .name = "userspace_reap",
55 .lname = "Libaio userspace reaping",
56 .type = FIO_OPT_STR_SET,
57 .off1 = offsetof(struct libaio_options, userspace_reap),
58 .help = "Use alternative user-space reap implementation",
59 .category = FIO_OPT_C_ENGINE,
60 .group = FIO_OPT_G_LIBAIO,
64 .lname = "High Priority",
65 .type = FIO_OPT_STR_SET,
66 .off1 = offsetof(struct libaio_options, hipri),
67 .help = "Use polled IO completions",
68 .category = FIO_OPT_C_ENGINE,
69 .group = FIO_OPT_G_LIBAIO,
76 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
80 *val = (*val + add) & (ld->entries - 1);
82 *val = (*val + add) % ld->entries;
85 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
87 struct fio_file *f = io_u->file;
88 struct libaio_options *o = td->eo;
90 if (io_u->ddir == DDIR_READ) {
91 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
93 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
94 } else if (io_u->ddir == DDIR_WRITE) {
95 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
97 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
98 } else if (ddir_sync(io_u->ddir))
99 io_prep_fsync(&io_u->iocb, f->fd);
104 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
106 struct libaio_data *ld = td->io_ops_data;
110 ev = ld->aio_events + event;
111 io_u = container_of(ev->obj, struct io_u, iocb);
113 if (ev->res != io_u->xfer_buflen) {
114 if (ev->res > io_u->xfer_buflen)
115 io_u->error = -ev->res;
117 io_u->resid = io_u->xfer_buflen - ev->res;
125 unsigned id; /** kernel internal index number */
126 unsigned nr; /** number of io_events */
131 unsigned compat_features;
132 unsigned incompat_features;
133 unsigned header_length; /** size of aio_ring */
135 struct io_event events[0];
138 #define AIO_RING_MAGIC 0xa10a10a1
140 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
141 struct io_event *events)
145 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
150 if (head == ring->tail) {
151 /* There are no more completions */
154 /* There is another completion to reap */
155 events[i] = ring->events[head];
157 ring->head = (head + 1) % ring->nr;
165 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
166 unsigned int max, const struct timespec *t)
168 struct libaio_data *ld = td->io_ops_data;
169 struct libaio_options *o = td->eo;
170 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
171 struct timespec __lt, *lt = NULL;
180 if (o->userspace_reap == 1
182 && ((struct aio_ring *)(ld->aio_ctx))->magic
184 r = user_io_getevents(ld->aio_ctx, max,
185 ld->aio_events + events);
187 r = io_getevents(ld->aio_ctx, actual_min,
188 max, ld->aio_events + events, lt);
192 else if ((min && r == 0) || r == -EAGAIN) {
193 fio_libaio_commit(td);
196 } else if (r != -EINTR)
198 } while (events < min);
200 return r < 0 ? r : events;
203 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
206 struct libaio_data *ld = td->io_ops_data;
208 fio_ro_check(td, io_u);
210 if (ld->queued == td->o.iodepth)
214 * fsync is tricky, since it can fail and we need to do it
215 * serialized with other io. the reason is that linux doesn't
216 * support aio fsync yet. So return busy for the case where we
217 * have pending io, to let fio complete those first.
219 if (ddir_sync(io_u->ddir)) {
223 do_io_u_sync(td, io_u);
224 return FIO_Q_COMPLETED;
227 if (io_u->ddir == DDIR_TRIM) {
231 do_io_u_trim(td, io_u);
232 io_u_mark_submit(td, 1);
233 io_u_mark_complete(td, 1);
234 return FIO_Q_COMPLETED;
237 ld->iocbs[ld->head] = &io_u->iocb;
238 ld->io_us[ld->head] = io_u;
239 ring_inc(ld, &ld->head, 1);
244 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
250 if (!fio_fill_issue_time(td))
253 fio_gettime(&now, NULL);
255 for (i = 0; i < nr; i++) {
256 struct io_u *io_u = io_us[i];
258 memcpy(&io_u->issue_time, &now, sizeof(now));
259 io_u_queued(td, io_u);
263 static int fio_libaio_commit(struct thread_data *td)
265 struct libaio_data *ld = td->io_ops_data;
269 int ret, wait_start = 0;
275 long nr = ld->queued;
277 nr = min((unsigned int) nr, ld->entries - ld->tail);
278 io_us = ld->io_us + ld->tail;
279 iocbs = ld->iocbs + ld->tail;
281 ret = io_submit(ld->aio_ctx, nr, iocbs);
283 fio_libaio_queued(td, io_us, ret);
284 io_u_mark_submit(td, ret);
287 ring_inc(ld, &ld->tail, ret);
290 } else if (ret == -EINTR || !ret) {
292 io_u_mark_submit(td, ret);
295 } else if (ret == -EAGAIN) {
297 * If we get EAGAIN, we should break out without
298 * error and let the upper layer reap some
299 * events for us. If we have no queued IO, we
300 * must loop here. If we loop for more than 30s,
301 * just error out, something must be buggy in the
309 fio_gettime(&ts, NULL);
311 } else if (mtime_since_now(&ts) > 30000) {
312 log_err("fio: aio appears to be stalled, giving up\n");
317 } else if (ret == -ENOMEM) {
319 * If we get -ENOMEM, reap events if we can. If
320 * we cannot, treat it as a fatal event since there's
321 * nothing we can do about it.
328 } while (ld->queued);
333 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
335 struct libaio_data *ld = td->io_ops_data;
337 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
340 static void fio_libaio_cleanup(struct thread_data *td)
342 struct libaio_data *ld = td->io_ops_data;
346 * Work-around to avoid huge RCU stalls at exit time. If we
347 * don't do this here, then it'll be torn down by exit_aio().
348 * But for that case we can parallellize the freeing, thus
349 * speeding it up a lot.
351 if (!(td->flags & TD_F_CHILD))
352 io_destroy(ld->aio_ctx);
353 free(ld->aio_events);
360 static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
363 #ifdef __NR_sys_io_setup2
367 flags = IOCTX_FLAG_IOPOLL;
369 return syscall(__NR_sys_io_setup2, depth, flags, &ld->aio_ctx);
372 log_err("fio: polled aio not available on your platform\n");
375 return io_queue_init(depth, &ld->aio_ctx);
379 static int fio_libaio_init(struct thread_data *td)
381 struct libaio_options *o = td->eo;
382 struct libaio_data *ld;
385 ld = calloc(1, sizeof(*ld));
388 * First try passing in 0 for queue depth, since we don't
389 * care about the user ring. If that fails, the kernel is too old
390 * and we need the right depth.
392 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri);
394 td_verror(td, -err, "io_queue_init");
395 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
400 ld->entries = td->o.iodepth;
401 ld->is_pow2 = is_power_of_2(ld->entries);
402 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
403 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
404 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
406 td->io_ops_data = ld;
410 static struct ioengine_ops ioengine = {
412 .version = FIO_IOOPS_VERSION,
413 .init = fio_libaio_init,
414 .prep = fio_libaio_prep,
415 .queue = fio_libaio_queue,
416 .commit = fio_libaio_commit,
417 .cancel = fio_libaio_cancel,
418 .getevents = fio_libaio_getevents,
419 .event = fio_libaio_event,
420 .cleanup = fio_libaio_cleanup,
421 .open_file = generic_open_file,
422 .close_file = generic_close_file,
423 .get_file_size = generic_get_file_size,
425 .option_struct_size = sizeof(struct libaio_options),
428 static void fio_init fio_libaio_register(void)
430 register_ioengine(&ioengine);
433 static void fio_exit fio_libaio_unregister(void)
435 unregister_ioengine(&ioengine);