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 #ifndef IOCB_FLAG_HIPRI
20 #define IOCB_FLAG_HIPRI (1 << 2)
23 #ifndef IOCTX_FLAG_IOPOLL
24 #define IOCTX_FLAG_IOPOLL (1 << 0)
27 static int fio_libaio_commit(struct thread_data *td);
31 struct io_event *aio_events;
35 struct io_u **io_u_index;
38 * Basic ring buffer. 'head' is incremented in _queue(), and
39 * 'tail' is incremented in _commit(). We keep 'queued' so
40 * that we know if the ring is full or empty, when
41 * 'head' == 'tail'. 'entries' is the ring size, and
42 * 'is_pow2' is just an optimization to use AND instead of
43 * modulus to get the remainder on ring increment.
52 struct libaio_options {
54 unsigned int userspace_reap;
58 static struct fio_option options[] = {
60 .name = "userspace_reap",
61 .lname = "Libaio userspace reaping",
62 .type = FIO_OPT_STR_SET,
63 .off1 = offsetof(struct libaio_options, userspace_reap),
64 .help = "Use alternative user-space reap implementation",
65 .category = FIO_OPT_C_ENGINE,
66 .group = FIO_OPT_G_LIBAIO,
70 .lname = "High Priority",
71 .type = FIO_OPT_STR_SET,
72 .off1 = offsetof(struct libaio_options, hipri),
73 .help = "Use polled IO completions",
74 .category = FIO_OPT_C_ENGINE,
75 .group = FIO_OPT_G_LIBAIO,
82 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
86 *val = (*val + add) & (ld->entries - 1);
88 *val = (*val + add) % ld->entries;
91 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
93 struct fio_file *f = io_u->file;
94 struct libaio_options *o = td->eo;
99 if (io_u->ddir == DDIR_READ) {
100 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
102 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
103 } else if (io_u->ddir == DDIR_WRITE) {
104 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
106 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
107 } else if (ddir_sync(io_u->ddir))
108 io_prep_fsync(iocb, f->fd);
113 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
115 struct libaio_data *ld = td->io_ops_data;
119 ev = ld->aio_events + event;
120 io_u = container_of(ev->obj, struct io_u, iocb);
122 if (ev->res != io_u->xfer_buflen) {
123 if (ev->res > io_u->xfer_buflen)
124 io_u->error = -ev->res;
126 io_u->resid = io_u->xfer_buflen - ev->res;
134 unsigned id; /** kernel internal index number */
135 unsigned nr; /** number of io_events */
140 unsigned compat_features;
141 unsigned incompat_features;
142 unsigned header_length; /** size of aio_ring */
144 struct io_event events[0];
147 #define AIO_RING_MAGIC 0xa10a10a1
149 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
150 struct io_event *events)
154 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
159 if (head == ring->tail) {
160 /* There are no more completions */
163 /* There is another completion to reap */
164 events[i] = ring->events[head];
166 ring->head = (head + 1) % ring->nr;
174 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
175 unsigned int max, const struct timespec *t)
177 struct libaio_data *ld = td->io_ops_data;
178 struct libaio_options *o = td->eo;
179 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
180 struct timespec __lt, *lt = NULL;
189 if (o->userspace_reap == 1
191 && ((struct aio_ring *)(ld->aio_ctx))->magic
193 r = user_io_getevents(ld->aio_ctx, max,
194 ld->aio_events + events);
196 r = io_getevents(ld->aio_ctx, actual_min,
197 max, ld->aio_events + events, lt);
201 else if ((min && r == 0) || r == -EAGAIN) {
202 fio_libaio_commit(td);
205 } else if (r != -EINTR)
207 } while (events < min);
209 return r < 0 ? r : events;
212 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
215 struct libaio_data *ld = td->io_ops_data;
217 fio_ro_check(td, io_u);
219 if (ld->queued == td->o.iodepth)
223 * fsync is tricky, since it can fail and we need to do it
224 * serialized with other io. the reason is that linux doesn't
225 * support aio fsync yet. So return busy for the case where we
226 * have pending io, to let fio complete those first.
228 if (ddir_sync(io_u->ddir)) {
232 do_io_u_sync(td, io_u);
233 return FIO_Q_COMPLETED;
236 if (io_u->ddir == DDIR_TRIM) {
240 do_io_u_trim(td, io_u);
241 io_u_mark_submit(td, 1);
242 io_u_mark_complete(td, 1);
243 return FIO_Q_COMPLETED;
246 ld->iocbs[ld->head] = &io_u->iocb;
247 ld->io_us[ld->head] = io_u;
248 ring_inc(ld, &ld->head, 1);
253 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
259 if (!fio_fill_issue_time(td))
262 fio_gettime(&now, NULL);
264 for (i = 0; i < nr; i++) {
265 struct io_u *io_u = io_us[i];
267 memcpy(&io_u->issue_time, &now, sizeof(now));
268 io_u_queued(td, io_u);
272 static int fio_libaio_commit(struct thread_data *td)
274 struct libaio_data *ld = td->io_ops_data;
278 int ret, wait_start = 0;
284 long nr = ld->queued;
286 nr = min((unsigned int) nr, ld->entries - ld->tail);
287 io_us = ld->io_us + ld->tail;
288 iocbs = ld->iocbs + ld->tail;
290 ret = io_submit(ld->aio_ctx, nr, iocbs);
292 fio_libaio_queued(td, io_us, ret);
293 io_u_mark_submit(td, ret);
296 ring_inc(ld, &ld->tail, ret);
299 } else if (ret == -EINTR || !ret) {
301 io_u_mark_submit(td, ret);
304 } else if (ret == -EAGAIN) {
306 * If we get EAGAIN, we should break out without
307 * error and let the upper layer reap some
308 * events for us. If we have no queued IO, we
309 * must loop here. If we loop for more than 30s,
310 * just error out, something must be buggy in the
318 fio_gettime(&ts, NULL);
320 } else if (mtime_since_now(&ts) > 30000) {
321 log_err("fio: aio appears to be stalled, giving up\n");
326 } else if (ret == -ENOMEM) {
328 * If we get -ENOMEM, reap events if we can. If
329 * we cannot, treat it as a fatal event since there's
330 * nothing we can do about it.
337 } while (ld->queued);
342 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
344 struct libaio_data *ld = td->io_ops_data;
346 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
349 static void fio_libaio_cleanup(struct thread_data *td)
351 struct libaio_data *ld = td->io_ops_data;
355 * Work-around to avoid huge RCU stalls at exit time. If we
356 * don't do this here, then it'll be torn down by exit_aio().
357 * But for that case we can parallellize the freeing, thus
358 * speeding it up a lot.
360 if (!(td->flags & TD_F_CHILD))
361 io_destroy(ld->aio_ctx);
362 free(ld->aio_events);
369 static int fio_libaio_old_queue_init(struct libaio_data *ld, unsigned int depth,
373 log_err("fio: polled aio not available on your platform\n");
377 return io_queue_init(depth, &ld->aio_ctx);
380 static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
383 #ifdef __NR_sys_io_setup2
387 flags |= IOCTX_FLAG_IOPOLL;
389 ret = syscall(__NR_sys_io_setup2, depth, flags, NULL, NULL,
393 /* fall through to old syscall */
395 return fio_libaio_old_queue_init(ld, depth, hipri);
398 static int fio_libaio_post_init(struct thread_data *td)
400 struct libaio_data *ld = td->io_ops_data;
401 struct libaio_options *o = td->eo;
404 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri);
406 td_verror(td, -err, "io_queue_init");
413 static int fio_libaio_init(struct thread_data *td)
415 struct libaio_data *ld;
417 ld = calloc(1, sizeof(*ld));
419 ld->entries = td->o.iodepth;
420 ld->is_pow2 = is_power_of_2(ld->entries);
421 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
422 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
423 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
425 td->io_ops_data = ld;
429 static struct ioengine_ops ioengine = {
431 .version = FIO_IOOPS_VERSION,
432 .init = fio_libaio_init,
433 .post_init = fio_libaio_post_init,
434 .prep = fio_libaio_prep,
435 .queue = fio_libaio_queue,
436 .commit = fio_libaio_commit,
437 .cancel = fio_libaio_cancel,
438 .getevents = fio_libaio_getevents,
439 .event = fio_libaio_event,
440 .cleanup = fio_libaio_cleanup,
441 .open_file = generic_open_file,
442 .close_file = generic_close_file,
443 .get_file_size = generic_get_file_size,
445 .option_struct_size = sizeof(struct libaio_options),
448 static void fio_init fio_libaio_register(void)
450 register_ioengine(&ioengine);
453 static void fio_exit fio_libaio_unregister(void)
455 unregister_ioengine(&ioengine);