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 static int fio_libaio_commit(struct thread_data *td);
23 struct io_event *aio_events;
27 struct io_u **io_u_index;
30 * Basic ring buffer. 'head' is incremented in _queue(), and
31 * 'tail' is incremented in _commit(). We keep 'queued' so
32 * that we know if the ring is full or empty, when
33 * 'head' == 'tail'. 'entries' is the ring size, and
34 * 'is_pow2' is just an optimization to use AND instead of
35 * modulus to get the remainder on ring increment.
44 struct libaio_options {
46 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,
64 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
68 *val = (*val + add) & (ld->entries - 1);
70 *val = (*val + add) % ld->entries;
73 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
75 struct fio_file *f = io_u->file;
76 struct iocb *iocb = &io_u->iocb;
78 if (io_u->ddir == DDIR_READ) {
79 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
80 } else if (io_u->ddir == DDIR_WRITE) {
81 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
82 } else if (ddir_sync(io_u->ddir))
83 io_prep_fsync(iocb, f->fd);
88 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
90 struct libaio_data *ld = td->io_ops_data;
94 ev = ld->aio_events + event;
95 io_u = container_of(ev->obj, struct io_u, iocb);
97 if (ev->res != io_u->xfer_buflen) {
98 if (ev->res > io_u->xfer_buflen)
99 io_u->error = -ev->res;
101 io_u->resid = io_u->xfer_buflen - ev->res;
109 unsigned id; /** kernel internal index number */
110 unsigned nr; /** number of io_events */
115 unsigned compat_features;
116 unsigned incompat_features;
117 unsigned header_length; /** size of aio_ring */
119 struct io_event events[0];
122 #define AIO_RING_MAGIC 0xa10a10a1
124 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
125 struct io_event *events)
129 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
134 if (head == ring->tail) {
135 /* There are no more completions */
138 /* There is another completion to reap */
139 events[i] = ring->events[head];
141 ring->head = (head + 1) % ring->nr;
149 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
150 unsigned int max, const struct timespec *t)
152 struct libaio_data *ld = td->io_ops_data;
153 struct libaio_options *o = td->eo;
154 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
155 struct timespec __lt, *lt = NULL;
164 if (o->userspace_reap == 1
166 && ((struct aio_ring *)(ld->aio_ctx))->magic
168 r = user_io_getevents(ld->aio_ctx, max,
169 ld->aio_events + events);
171 r = io_getevents(ld->aio_ctx, actual_min,
172 max, ld->aio_events + events, lt);
176 else if ((min && r == 0) || r == -EAGAIN) {
177 fio_libaio_commit(td);
180 } else if (r != -EINTR)
182 } while (events < min);
184 return r < 0 ? r : events;
187 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
190 struct libaio_data *ld = td->io_ops_data;
192 fio_ro_check(td, io_u);
194 if (ld->queued == td->o.iodepth)
198 * fsync is tricky, since it can fail and we need to do it
199 * serialized with other io. the reason is that linux doesn't
200 * support aio fsync yet. So return busy for the case where we
201 * have pending io, to let fio complete those first.
203 if (ddir_sync(io_u->ddir)) {
207 do_io_u_sync(td, io_u);
208 return FIO_Q_COMPLETED;
211 if (io_u->ddir == DDIR_TRIM) {
215 do_io_u_trim(td, io_u);
216 io_u_mark_submit(td, 1);
217 io_u_mark_complete(td, 1);
218 return FIO_Q_COMPLETED;
221 ld->iocbs[ld->head] = &io_u->iocb;
222 ld->io_us[ld->head] = io_u;
223 ring_inc(ld, &ld->head, 1);
228 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
234 if (!fio_fill_issue_time(td))
237 fio_gettime(&now, NULL);
239 for (i = 0; i < nr; i++) {
240 struct io_u *io_u = io_us[i];
242 memcpy(&io_u->issue_time, &now, sizeof(now));
243 io_u_queued(td, io_u);
247 static int fio_libaio_commit(struct thread_data *td)
249 struct libaio_data *ld = td->io_ops_data;
253 int ret, wait_start = 0;
259 long nr = ld->queued;
261 nr = min((unsigned int) nr, ld->entries - ld->tail);
262 io_us = ld->io_us + ld->tail;
263 iocbs = ld->iocbs + ld->tail;
265 ret = io_submit(ld->aio_ctx, nr, iocbs);
267 fio_libaio_queued(td, io_us, ret);
268 io_u_mark_submit(td, ret);
271 ring_inc(ld, &ld->tail, ret);
274 } else if (ret == -EINTR || !ret) {
276 io_u_mark_submit(td, ret);
279 } else if (ret == -EAGAIN) {
281 * If we get EAGAIN, we should break out without
282 * error and let the upper layer reap some
283 * events for us. If we have no queued IO, we
284 * must loop here. If we loop for more than 30s,
285 * just error out, something must be buggy in the
293 fio_gettime(&ts, NULL);
295 } else if (mtime_since_now(&ts) > 30000) {
296 log_err("fio: aio appears to be stalled, giving up\n");
301 } else if (ret == -ENOMEM) {
303 * If we get -ENOMEM, reap events if we can. If
304 * we cannot, treat it as a fatal event since there's
305 * nothing we can do about it.
312 } while (ld->queued);
317 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
319 struct libaio_data *ld = td->io_ops_data;
321 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
324 static void fio_libaio_cleanup(struct thread_data *td)
326 struct libaio_data *ld = td->io_ops_data;
330 * Work-around to avoid huge RCU stalls at exit time. If we
331 * don't do this here, then it'll be torn down by exit_aio().
332 * But for that case we can parallellize the freeing, thus
333 * speeding it up a lot.
335 if (!(td->flags & TD_F_CHILD))
336 io_destroy(ld->aio_ctx);
337 free(ld->aio_events);
344 static int fio_libaio_post_init(struct thread_data *td)
346 struct libaio_data *ld = td->io_ops_data;
349 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
351 td_verror(td, -err, "io_queue_init");
358 static int fio_libaio_init(struct thread_data *td)
360 struct libaio_data *ld;
362 ld = calloc(1, sizeof(*ld));
364 ld->entries = td->o.iodepth;
365 ld->is_pow2 = is_power_of_2(ld->entries);
366 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
367 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
368 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
370 td->io_ops_data = ld;
374 static struct ioengine_ops ioengine = {
376 .version = FIO_IOOPS_VERSION,
377 .flags = FIO_ASYNCIO_SYNC_TRIM,
378 .init = fio_libaio_init,
379 .post_init = fio_libaio_post_init,
380 .prep = fio_libaio_prep,
381 .queue = fio_libaio_queue,
382 .commit = fio_libaio_commit,
383 .cancel = fio_libaio_cancel,
384 .getevents = fio_libaio_getevents,
385 .event = fio_libaio_event,
386 .cleanup = fio_libaio_cleanup,
387 .open_file = generic_open_file,
388 .close_file = generic_close_file,
389 .get_file_size = generic_get_file_size,
391 .option_struct_size = sizeof(struct libaio_options),
394 static void fio_init fio_libaio_register(void)
396 register_ioengine(&ioengine);
399 static void fio_exit fio_libaio_unregister(void)
401 unregister_ioengine(&ioengine);