4 * IO engine using the Linux native aio interface.
15 #ifdef FIO_HAVE_LIBAIO
17 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
21 struct io_event *aio_events;
27 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
29 struct fio_file *f = io_u->file;
31 if (io_u->ddir == DDIR_READ)
32 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
33 else if (io_u->ddir == DDIR_WRITE)
34 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
35 else if (ddir_sync(io_u->ddir))
36 io_prep_fsync(&io_u->iocb, f->fd);
41 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
43 struct libaio_data *ld = td->io_ops->data;
47 ev = ld->aio_events + event;
50 if (ev->res != io_u->xfer_buflen) {
51 if (ev->res > io_u->xfer_buflen)
52 io_u->error = -ev->res;
54 io_u->resid = io_u->xfer_buflen - ev->res;
62 unsigned id; /** kernel internal index number */
63 unsigned nr; /** number of io_events */
68 unsigned compat_features;
69 unsigned incompat_features;
70 unsigned header_length; /** size of aio_ring */
72 struct io_event events[0];
75 #define AIO_RING_MAGIC 0xa10a10a1
77 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
78 struct io_event *events)
82 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
87 if (head == ring->tail) {
88 /* There are no more completions */
91 /* There is another completion to reap */
92 events[i] = ring->events[head];
94 ring->head = (head + 1) % ring->nr;
102 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
103 unsigned int max, struct timespec *t)
105 struct libaio_data *ld = td->io_ops->data;
106 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
110 if (td->o.userspace_libaio_reap == 1
112 && ((struct aio_ring *)(ld->aio_ctx))->magic
114 r = user_io_getevents(ld->aio_ctx, max,
115 ld->aio_events + events);
117 r = io_getevents(ld->aio_ctx, actual_min,
118 max, ld->aio_events + events, t);
122 else if (r == -EAGAIN)
124 } while (events < min);
126 return r < 0 ? r : events;
129 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
131 struct libaio_data *ld = td->io_ops->data;
133 fio_ro_check(td, io_u);
135 if (ld->iocbs_nr == (int) td->o.iodepth)
139 * fsync is tricky, since it can fail and we need to do it
140 * serialized with other io. the reason is that linux doesn't
141 * support aio fsync yet. So return busy for the case where we
142 * have pending io, to let fio complete those first.
144 if (ddir_sync(io_u->ddir)) {
148 do_io_u_sync(td, io_u);
149 return FIO_Q_COMPLETED;
152 if (io_u->ddir == DDIR_TRIM) {
156 do_io_u_trim(td, io_u);
157 return FIO_Q_COMPLETED;
160 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
161 ld->io_us[ld->iocbs_nr] = io_u;
166 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
172 if (!fio_fill_issue_time(td))
175 fio_gettime(&now, NULL);
177 for (i = 0; i < nr; i++) {
178 struct io_u *io_u = io_us[i];
180 memcpy(&io_u->issue_time, &now, sizeof(now));
181 io_u_queued(td, io_u);
185 static int fio_libaio_commit(struct thread_data *td)
187 struct libaio_data *ld = td->io_ops->data;
198 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
200 fio_libaio_queued(td, io_us, ret);
201 io_u_mark_submit(td, ret);
206 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
208 io_u_mark_submit(td, ret);
212 } while (ld->iocbs_nr);
217 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
219 struct libaio_data *ld = td->io_ops->data;
221 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
224 static void fio_libaio_cleanup(struct thread_data *td)
226 struct libaio_data *ld = td->io_ops->data;
229 io_destroy(ld->aio_ctx);
230 free(ld->aio_events);
237 static int fio_libaio_init(struct thread_data *td)
239 struct libaio_data *ld = malloc(sizeof(*ld));
242 memset(ld, 0, sizeof(*ld));
244 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
246 td_verror(td, -err, "io_queue_init");
247 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
252 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
253 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
254 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
255 memset(ld->iocbs, 0, sizeof(struct iocb *));
256 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
257 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
260 td->io_ops->data = ld;
264 static struct ioengine_ops ioengine = {
266 .version = FIO_IOOPS_VERSION,
267 .init = fio_libaio_init,
268 .prep = fio_libaio_prep,
269 .queue = fio_libaio_queue,
270 .commit = fio_libaio_commit,
271 .cancel = fio_libaio_cancel,
272 .getevents = fio_libaio_getevents,
273 .event = fio_libaio_event,
274 .cleanup = fio_libaio_cleanup,
275 .open_file = generic_open_file,
276 .close_file = generic_close_file,
277 .get_file_size = generic_get_file_size,
280 #else /* FIO_HAVE_LIBAIO */
283 * When we have a proper configure system in place, we simply wont build
284 * and install this io engine. For now install a crippled version that
285 * just complains and fails to load.
287 static int fio_libaio_init(struct thread_data fio_unused *td)
289 log_err("fio: libaio not available\n");
293 static struct ioengine_ops ioengine = {
295 .version = FIO_IOOPS_VERSION,
296 .init = fio_libaio_init,
301 static void fio_init fio_libaio_register(void)
303 register_ioengine(&ioengine);
306 static void fio_exit fio_libaio_unregister(void)
308 unregister_ioengine(&ioengine);