4 * IO engine using the Linux native aio interface.
18 struct io_event *aio_events;
24 struct libaio_options {
25 struct thread_data *td;
26 unsigned int userspace_reap;
29 static struct fio_option options[] = {
31 .name = "userspace_reap",
32 .type = FIO_OPT_STR_SET,
33 .off1 = offsetof(struct libaio_options, userspace_reap),
34 .help = "Use alternative user-space reap implementation",
41 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
43 struct fio_file *f = io_u->file;
45 if (io_u->ddir == DDIR_READ)
46 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
47 else if (io_u->ddir == DDIR_WRITE)
48 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
49 else if (ddir_sync(io_u->ddir))
50 io_prep_fsync(&io_u->iocb, f->fd);
55 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
57 struct libaio_data *ld = td->io_ops->data;
61 ev = ld->aio_events + event;
62 io_u = container_of(ev->obj, struct io_u, iocb);
64 if (ev->res != io_u->xfer_buflen) {
65 if (ev->res > io_u->xfer_buflen)
66 io_u->error = -ev->res;
68 io_u->resid = io_u->xfer_buflen - ev->res;
76 unsigned id; /** kernel internal index number */
77 unsigned nr; /** number of io_events */
82 unsigned compat_features;
83 unsigned incompat_features;
84 unsigned header_length; /** size of aio_ring */
86 struct io_event events[0];
89 #define AIO_RING_MAGIC 0xa10a10a1
91 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
92 struct io_event *events)
96 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
101 if (head == ring->tail) {
102 /* There are no more completions */
105 /* There is another completion to reap */
106 events[i] = ring->events[head];
108 ring->head = (head + 1) % ring->nr;
116 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
117 unsigned int max, struct timespec *t)
119 struct libaio_data *ld = td->io_ops->data;
120 struct libaio_options *o = td->eo;
121 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
125 if (o->userspace_reap == 1
127 && ((struct aio_ring *)(ld->aio_ctx))->magic
129 r = user_io_getevents(ld->aio_ctx, max,
130 ld->aio_events + events);
132 r = io_getevents(ld->aio_ctx, actual_min,
133 max, ld->aio_events + events, t);
137 else if (r == -EAGAIN)
139 } while (events < min);
141 return r < 0 ? r : events;
144 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
146 struct libaio_data *ld = td->io_ops->data;
148 fio_ro_check(td, io_u);
150 if (ld->iocbs_nr == (int) td->o.iodepth)
154 * fsync is tricky, since it can fail and we need to do it
155 * serialized with other io. the reason is that linux doesn't
156 * support aio fsync yet. So return busy for the case where we
157 * have pending io, to let fio complete those first.
159 if (ddir_sync(io_u->ddir)) {
163 do_io_u_sync(td, io_u);
164 return FIO_Q_COMPLETED;
167 if (io_u->ddir == DDIR_TRIM) {
171 do_io_u_trim(td, io_u);
172 return FIO_Q_COMPLETED;
175 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
176 ld->io_us[ld->iocbs_nr] = io_u;
181 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
187 if (!fio_fill_issue_time(td))
190 fio_gettime(&now, NULL);
192 for (i = 0; i < nr; i++) {
193 struct io_u *io_u = io_us[i];
195 memcpy(&io_u->issue_time, &now, sizeof(now));
196 io_u_queued(td, io_u);
200 static int fio_libaio_commit(struct thread_data *td)
202 struct libaio_data *ld = td->io_ops->data;
213 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
215 fio_libaio_queued(td, io_us, ret);
216 io_u_mark_submit(td, ret);
221 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
223 io_u_mark_submit(td, ret);
227 } while (ld->iocbs_nr);
232 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
234 struct libaio_data *ld = td->io_ops->data;
236 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
239 static void fio_libaio_cleanup(struct thread_data *td)
241 struct libaio_data *ld = td->io_ops->data;
244 io_destroy(ld->aio_ctx);
245 free(ld->aio_events);
252 static int fio_libaio_init(struct thread_data *td)
254 struct libaio_data *ld = malloc(sizeof(*ld));
255 struct libaio_options *o = td->eo;
258 memset(ld, 0, sizeof(*ld));
261 * First try passing in 0 for queue depth, since we don't
262 * care about the user ring. If that fails, the kernel is too old
263 * and we need the right depth.
265 if (!o->userspace_reap)
266 err = io_queue_init(INT_MAX, &ld->aio_ctx);
267 if (o->userspace_reap || err == -EINVAL)
268 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
270 td_verror(td, -err, "io_queue_init");
271 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
276 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
277 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
278 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
279 memset(ld->iocbs, 0, sizeof(struct iocb *));
280 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
281 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
284 td->io_ops->data = ld;
288 static struct ioengine_ops ioengine = {
290 .version = FIO_IOOPS_VERSION,
291 .init = fio_libaio_init,
292 .prep = fio_libaio_prep,
293 .queue = fio_libaio_queue,
294 .commit = fio_libaio_commit,
295 .cancel = fio_libaio_cancel,
296 .getevents = fio_libaio_getevents,
297 .event = fio_libaio_event,
298 .cleanup = fio_libaio_cleanup,
299 .open_file = generic_open_file,
300 .close_file = generic_close_file,
301 .get_file_size = generic_get_file_size,
303 .option_struct_size = sizeof(struct libaio_options),
306 static void fio_init fio_libaio_register(void)
308 register_ioengine(&ioengine);
311 static void fio_exit fio_libaio_unregister(void)
313 unregister_ioengine(&ioengine);