4 * IO engine using the Linux native aio interface.
15 #ifdef FIO_HAVE_LIBAIO
19 struct io_event *aio_events;
25 struct libaio_options {
26 struct thread_data *td;
27 unsigned int userspace_reap;
30 static struct fio_option options[] = {
32 .name = "userspace_reap",
33 .type = FIO_OPT_STR_SET,
34 .off1 = offsetof(struct libaio_options, userspace_reap),
35 .help = "Use alternative user-space reap implementation",
42 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
44 struct fio_file *f = io_u->file;
46 if (io_u->ddir == DDIR_READ)
47 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
48 else if (io_u->ddir == DDIR_WRITE)
49 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
50 else if (ddir_sync(io_u->ddir))
51 io_prep_fsync(&io_u->iocb, f->fd);
56 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
58 struct libaio_data *ld = td->io_ops->data;
62 ev = ld->aio_events + event;
63 io_u = container_of(ev->obj, struct io_u, iocb);
65 if (ev->res != io_u->xfer_buflen) {
66 if (ev->res > io_u->xfer_buflen)
67 io_u->error = -ev->res;
69 io_u->resid = io_u->xfer_buflen - ev->res;
77 unsigned id; /** kernel internal index number */
78 unsigned nr; /** number of io_events */
83 unsigned compat_features;
84 unsigned incompat_features;
85 unsigned header_length; /** size of aio_ring */
87 struct io_event events[0];
90 #define AIO_RING_MAGIC 0xa10a10a1
92 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
93 struct io_event *events)
97 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
102 if (head == ring->tail) {
103 /* There are no more completions */
106 /* There is another completion to reap */
107 events[i] = ring->events[head];
109 ring->head = (head + 1) % ring->nr;
117 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
118 unsigned int max, struct timespec *t)
120 struct libaio_data *ld = td->io_ops->data;
121 struct libaio_options *o = td->eo;
122 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
126 if (o->userspace_reap == 1
128 && ((struct aio_ring *)(ld->aio_ctx))->magic
130 r = user_io_getevents(ld->aio_ctx, max,
131 ld->aio_events + events);
133 r = io_getevents(ld->aio_ctx, actual_min,
134 max, ld->aio_events + events, t);
138 else if (r == -EAGAIN)
140 } while (events < min);
142 return r < 0 ? r : events;
145 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
147 struct libaio_data *ld = td->io_ops->data;
149 fio_ro_check(td, io_u);
151 if (ld->iocbs_nr == (int) td->o.iodepth)
155 * fsync is tricky, since it can fail and we need to do it
156 * serialized with other io. the reason is that linux doesn't
157 * support aio fsync yet. So return busy for the case where we
158 * have pending io, to let fio complete those first.
160 if (ddir_sync(io_u->ddir)) {
164 do_io_u_sync(td, io_u);
165 return FIO_Q_COMPLETED;
168 if (io_u->ddir == DDIR_TRIM) {
172 do_io_u_trim(td, io_u);
173 return FIO_Q_COMPLETED;
176 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
177 ld->io_us[ld->iocbs_nr] = io_u;
182 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
188 if (!fio_fill_issue_time(td))
191 fio_gettime(&now, NULL);
193 for (i = 0; i < nr; i++) {
194 struct io_u *io_u = io_us[i];
196 memcpy(&io_u->issue_time, &now, sizeof(now));
197 io_u_queued(td, io_u);
201 static int fio_libaio_commit(struct thread_data *td)
203 struct libaio_data *ld = td->io_ops->data;
214 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
216 fio_libaio_queued(td, io_us, ret);
217 io_u_mark_submit(td, ret);
222 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
224 io_u_mark_submit(td, ret);
228 } while (ld->iocbs_nr);
233 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
235 struct libaio_data *ld = td->io_ops->data;
237 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
240 static void fio_libaio_cleanup(struct thread_data *td)
242 struct libaio_data *ld = td->io_ops->data;
245 io_destroy(ld->aio_ctx);
246 free(ld->aio_events);
253 static int fio_libaio_init(struct thread_data *td)
255 struct libaio_data *ld = malloc(sizeof(*ld));
256 struct libaio_options *o = td->eo;
259 memset(ld, 0, sizeof(*ld));
262 * First try passing in 0 for queue depth, since we don't
263 * care about the user ring. If that fails, the kernel is too old
264 * and we need the right depth.
266 if (!o->userspace_reap)
267 err = io_queue_init(INT_MAX, &ld->aio_ctx);
268 if (o->userspace_reap || err == -EINVAL)
269 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
271 td_verror(td, -err, "io_queue_init");
272 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
277 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
278 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
279 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
280 memset(ld->iocbs, 0, sizeof(struct iocb *));
281 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
282 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
285 td->io_ops->data = ld;
289 static struct ioengine_ops ioengine = {
291 .version = FIO_IOOPS_VERSION,
292 .init = fio_libaio_init,
293 .prep = fio_libaio_prep,
294 .queue = fio_libaio_queue,
295 .commit = fio_libaio_commit,
296 .cancel = fio_libaio_cancel,
297 .getevents = fio_libaio_getevents,
298 .event = fio_libaio_event,
299 .cleanup = fio_libaio_cleanup,
300 .open_file = generic_open_file,
301 .close_file = generic_close_file,
302 .get_file_size = generic_get_file_size,
304 .option_struct_size = sizeof(struct libaio_options),
307 #else /* FIO_HAVE_LIBAIO */
310 * When we have a proper configure system in place, we simply wont build
311 * and install this io engine. For now install a crippled version that
312 * just complains and fails to load.
314 static int fio_libaio_init(struct thread_data fio_unused *td)
316 log_err("fio: libaio not available\n");
320 static struct ioengine_ops ioengine = {
322 .version = FIO_IOOPS_VERSION,
323 .init = fio_libaio_init,
328 static void fio_init fio_libaio_register(void)
330 register_ioengine(&ioengine);
333 static void fio_exit fio_libaio_unregister(void)
335 unregister_ioengine(&ioengine);