4 * IO engine using the Linux native aio interface.
18 struct io_event *aio_events;
23 * Basic ring buffer. 'head' is incremented in _queue(), and
24 * 'tail' is incremented in _commit(). We keep 'queued' so
25 * that we know if the ring is full or empty, when
26 * 'head' == 'tail'. 'entries' is the ring size, and
27 * 'is_pow2' is just an optimization to use AND instead of
28 * modulus to get the remainder on ring increment.
37 struct libaio_options {
38 struct thread_data *td;
39 unsigned int userspace_reap;
42 static struct fio_option options[] = {
44 .name = "userspace_reap",
45 .lname = "Libaio userspace reaping",
46 .type = FIO_OPT_STR_SET,
47 .off1 = offsetof(struct libaio_options, userspace_reap),
48 .help = "Use alternative user-space reap implementation",
49 .category = FIO_OPT_C_ENGINE,
50 .group = FIO_OPT_G_LIBAIO,
57 static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
61 *val = (*val + add) & (ld->entries - 1);
63 *val = (*val + add) % ld->entries;
66 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
68 struct fio_file *f = io_u->file;
70 if (io_u->ddir == DDIR_READ)
71 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
72 else if (io_u->ddir == DDIR_WRITE)
73 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
74 else if (ddir_sync(io_u->ddir))
75 io_prep_fsync(&io_u->iocb, f->fd);
80 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
82 struct libaio_data *ld = td->io_ops->data;
86 ev = ld->aio_events + event;
87 io_u = container_of(ev->obj, struct io_u, iocb);
89 if (ev->res != io_u->xfer_buflen) {
90 if (ev->res > io_u->xfer_buflen)
91 io_u->error = -ev->res;
93 io_u->resid = io_u->xfer_buflen - ev->res;
101 unsigned id; /** kernel internal index number */
102 unsigned nr; /** number of io_events */
107 unsigned compat_features;
108 unsigned incompat_features;
109 unsigned header_length; /** size of aio_ring */
111 struct io_event events[0];
114 #define AIO_RING_MAGIC 0xa10a10a1
116 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
117 struct io_event *events)
121 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
126 if (head == ring->tail) {
127 /* There are no more completions */
130 /* There is another completion to reap */
131 events[i] = ring->events[head];
133 ring->head = (head + 1) % ring->nr;
141 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
142 unsigned int max, struct timespec *t)
144 struct libaio_data *ld = td->io_ops->data;
145 struct libaio_options *o = td->eo;
146 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
150 if (o->userspace_reap == 1
152 && ((struct aio_ring *)(ld->aio_ctx))->magic
154 r = user_io_getevents(ld->aio_ctx, max,
155 ld->aio_events + events);
157 r = io_getevents(ld->aio_ctx, actual_min,
158 max, ld->aio_events + events, t);
162 else if (r == -EAGAIN)
164 } while (events < min);
166 return r < 0 ? r : events;
169 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
171 struct libaio_data *ld = td->io_ops->data;
173 fio_ro_check(td, io_u);
175 if (ld->queued == td->o.iodepth)
179 * fsync is tricky, since it can fail and we need to do it
180 * serialized with other io. the reason is that linux doesn't
181 * support aio fsync yet. So return busy for the case where we
182 * have pending io, to let fio complete those first.
184 if (ddir_sync(io_u->ddir)) {
188 do_io_u_sync(td, io_u);
189 return FIO_Q_COMPLETED;
192 if (io_u->ddir == DDIR_TRIM) {
196 do_io_u_trim(td, io_u);
197 return FIO_Q_COMPLETED;
200 ld->iocbs[ld->head] = &io_u->iocb;
201 ld->io_us[ld->head] = io_u;
202 ring_inc(ld, &ld->head, 1);
207 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
213 if (!fio_fill_issue_time(td))
216 fio_gettime(&now, NULL);
218 for (i = 0; i < nr; i++) {
219 struct io_u *io_u = io_us[i];
221 memcpy(&io_u->issue_time, &now, sizeof(now));
222 io_u_queued(td, io_u);
226 static int fio_libaio_commit(struct thread_data *td)
228 struct libaio_data *ld = td->io_ops->data;
237 long nr = ld->queued;
239 nr = min((unsigned int) nr, ld->entries - ld->tail);
240 io_us = ld->io_us + ld->tail;
241 iocbs = ld->iocbs + ld->tail;
243 ret = io_submit(ld->aio_ctx, nr, iocbs);
245 fio_libaio_queued(td, io_us, ret);
246 io_u_mark_submit(td, ret);
249 ring_inc(ld, &ld->tail, ret);
251 } else if (ret == -EINTR || !ret) {
253 io_u_mark_submit(td, ret);
255 } else if (ret == -EAGAIN) {
257 * If we get EAGAIN, we should break out without
258 * error and let the upper layer reap some
265 } while (ld->head != ld->tail);
270 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
272 struct libaio_data *ld = td->io_ops->data;
274 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
277 static void fio_libaio_cleanup(struct thread_data *td)
279 struct libaio_data *ld = td->io_ops->data;
282 io_destroy(ld->aio_ctx);
283 free(ld->aio_events);
290 static int fio_libaio_init(struct thread_data *td)
292 struct libaio_options *o = td->eo;
293 struct libaio_data *ld;
296 ld = calloc(1, sizeof(*ld));
299 * First try passing in 0 for queue depth, since we don't
300 * care about the user ring. If that fails, the kernel is too old
301 * and we need the right depth.
303 if (!o->userspace_reap)
304 err = io_queue_init(INT_MAX, &ld->aio_ctx);
305 if (o->userspace_reap || err == -EINVAL)
306 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
308 td_verror(td, -err, "io_queue_init");
309 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
314 ld->entries = td->o.iodepth;
315 ld->is_pow2 = is_power_of_2(ld->entries);
316 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
317 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
318 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
320 td->io_ops->data = ld;
324 static struct ioengine_ops ioengine = {
326 .version = FIO_IOOPS_VERSION,
327 .init = fio_libaio_init,
328 .prep = fio_libaio_prep,
329 .queue = fio_libaio_queue,
330 .commit = fio_libaio_commit,
331 .cancel = fio_libaio_cancel,
332 .getevents = fio_libaio_getevents,
333 .event = fio_libaio_event,
334 .cleanup = fio_libaio_cleanup,
335 .open_file = generic_open_file,
336 .close_file = generic_close_file,
337 .get_file_size = generic_get_file_size,
339 .option_struct_size = sizeof(struct libaio_options),
342 static void fio_init fio_libaio_register(void)
344 register_ioengine(&ioengine);
347 static void fio_exit fio_libaio_unregister(void)
349 unregister_ioengine(&ioengine);