4 * IO engine using the Linux native aio interface.
16 static int fio_libaio_commit(struct thread_data *td);
20 struct io_event *aio_events;
25 * Basic ring buffer. 'head' is incremented in _queue(), and
26 * 'tail' is incremented in _commit(). We keep 'queued' so
27 * that we know if the ring is full or empty, when
28 * 'head' == 'tail'. 'entries' is the ring size, and
29 * 'is_pow2' is just an optimization to use AND instead of
30 * modulus to get the remainder on ring increment.
39 struct libaio_options {
41 unsigned int userspace_reap;
44 static struct fio_option options[] = {
46 .name = "userspace_reap",
47 .lname = "Libaio userspace reaping",
48 .type = FIO_OPT_STR_SET,
49 .off1 = offsetof(struct libaio_options, userspace_reap),
50 .help = "Use alternative user-space reap implementation",
51 .category = FIO_OPT_C_ENGINE,
52 .group = FIO_OPT_G_LIBAIO,
59 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
63 *val = (*val + add) & (ld->entries - 1);
65 *val = (*val + add) % ld->entries;
68 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
70 struct fio_file *f = io_u->file;
72 if (io_u->ddir == DDIR_READ)
73 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
74 else if (io_u->ddir == DDIR_WRITE)
75 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
76 else if (ddir_sync(io_u->ddir))
77 io_prep_fsync(&io_u->iocb, f->fd);
82 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
84 struct libaio_data *ld = td->io_ops->data;
88 ev = ld->aio_events + event;
89 io_u = container_of(ev->obj, struct io_u, iocb);
91 if (ev->res != io_u->xfer_buflen) {
92 if (ev->res > io_u->xfer_buflen)
93 io_u->error = -ev->res;
95 io_u->resid = io_u->xfer_buflen - ev->res;
103 unsigned id; /** kernel internal index number */
104 unsigned nr; /** number of io_events */
109 unsigned compat_features;
110 unsigned incompat_features;
111 unsigned header_length; /** size of aio_ring */
113 struct io_event events[0];
116 #define AIO_RING_MAGIC 0xa10a10a1
118 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
119 struct io_event *events)
123 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
128 if (head == ring->tail) {
129 /* There are no more completions */
132 /* There is another completion to reap */
133 events[i] = ring->events[head];
135 ring->head = (head + 1) % ring->nr;
143 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
144 unsigned int max, const struct timespec *t)
146 struct libaio_data *ld = td->io_ops->data;
147 struct libaio_options *o = td->eo;
148 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
149 struct timespec __lt, *lt = NULL;
158 if (o->userspace_reap == 1
160 && ((struct aio_ring *)(ld->aio_ctx))->magic
162 r = user_io_getevents(ld->aio_ctx, max,
163 ld->aio_events + events);
165 r = io_getevents(ld->aio_ctx, actual_min,
166 max, ld->aio_events + events, lt);
170 else if ((min && r == 0) || r == -EAGAIN) {
171 fio_libaio_commit(td);
173 } else if (r != -EINTR)
175 } while (events < min);
177 return r < 0 ? r : events;
180 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
182 struct libaio_data *ld = td->io_ops->data;
184 fio_ro_check(td, io_u);
186 if (ld->queued == td->o.iodepth)
190 * fsync is tricky, since it can fail and we need to do it
191 * serialized with other io. the reason is that linux doesn't
192 * support aio fsync yet. So return busy for the case where we
193 * have pending io, to let fio complete those first.
195 if (ddir_sync(io_u->ddir)) {
199 do_io_u_sync(td, io_u);
200 return FIO_Q_COMPLETED;
203 if (io_u->ddir == DDIR_TRIM) {
207 do_io_u_trim(td, io_u);
208 return FIO_Q_COMPLETED;
211 ld->iocbs[ld->head] = &io_u->iocb;
212 ld->io_us[ld->head] = io_u;
213 ring_inc(ld, &ld->head, 1);
218 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
224 if (!fio_fill_issue_time(td))
227 fio_gettime(&now, NULL);
229 for (i = 0; i < nr; i++) {
230 struct io_u *io_u = io_us[i];
232 memcpy(&io_u->issue_time, &now, sizeof(now));
233 io_u_queued(td, io_u);
237 static int fio_libaio_commit(struct thread_data *td)
239 struct libaio_data *ld = td->io_ops->data;
243 int ret, wait_start = 0;
249 long nr = ld->queued;
251 nr = min((unsigned int) nr, ld->entries - ld->tail);
252 io_us = ld->io_us + ld->tail;
253 iocbs = ld->iocbs + ld->tail;
255 ret = io_submit(ld->aio_ctx, nr, iocbs);
257 fio_libaio_queued(td, io_us, ret);
258 io_u_mark_submit(td, ret);
261 ring_inc(ld, &ld->tail, ret);
264 } else if (ret == -EINTR || !ret) {
266 io_u_mark_submit(td, ret);
269 } else if (ret == -EAGAIN) {
271 * If we get EAGAIN, we should break out without
272 * error and let the upper layer reap some
273 * events for us. If we have no queued IO, we
274 * must loop here. If we loop for more than 30s,
275 * just error out, something must be buggy in the
283 fio_gettime(&tv, NULL);
285 } else if (mtime_since_now(&tv) > 30000) {
286 log_err("fio: aio appears to be stalled, giving up\n");
291 } else if (ret == -ENOMEM) {
293 * If we get -ENOMEM, reap events if we can. If
294 * we cannot, treat it as a fatal event since there's
295 * nothing we can do about it.
302 } while (ld->queued);
307 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
309 struct libaio_data *ld = td->io_ops->data;
311 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
314 static void fio_libaio_cleanup(struct thread_data *td)
316 struct libaio_data *ld = td->io_ops->data;
319 io_destroy(ld->aio_ctx);
320 free(ld->aio_events);
327 static int fio_libaio_init(struct thread_data *td)
329 struct libaio_options *o = td->eo;
330 struct libaio_data *ld;
333 ld = calloc(1, sizeof(*ld));
336 * First try passing in 0 for queue depth, since we don't
337 * care about the user ring. If that fails, the kernel is too old
338 * and we need the right depth.
340 if (!o->userspace_reap)
341 err = io_queue_init(INT_MAX, &ld->aio_ctx);
342 if (o->userspace_reap || err == -EINVAL)
343 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
345 td_verror(td, -err, "io_queue_init");
346 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
351 ld->entries = td->o.iodepth;
352 ld->is_pow2 = is_power_of_2(ld->entries);
353 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
354 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
355 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
357 td->io_ops->data = ld;
361 static struct ioengine_ops ioengine = {
363 .version = FIO_IOOPS_VERSION,
364 .init = fio_libaio_init,
365 .prep = fio_libaio_prep,
366 .queue = fio_libaio_queue,
367 .commit = fio_libaio_commit,
368 .cancel = fio_libaio_cancel,
369 .getevents = fio_libaio_getevents,
370 .event = fio_libaio_event,
371 .cleanup = fio_libaio_cleanup,
372 .open_file = generic_open_file,
373 .close_file = generic_close_file,
374 .get_file_size = generic_get_file_size,
376 .option_struct_size = sizeof(struct libaio_options),
379 static void fio_init fio_libaio_register(void)
381 register_ioengine(&ioengine);
384 static void fio_exit fio_libaio_unregister(void)
386 unregister_ioengine(&ioengine);