2 * native linux aio io engine
14 #ifdef FIO_HAVE_LIBAIO
16 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
20 struct io_event *aio_events;
21 struct io_u *sync_io_u;
24 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
26 struct fio_file *f = io_u->file;
28 if (io_u->ddir == DDIR_READ)
29 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
30 else if (io_u->ddir == DDIR_WRITE)
31 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
32 else if (io_u->ddir == DDIR_SYNC)
33 io_prep_fsync(&io_u->iocb, f->fd);
40 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
42 struct libaio_data *ld = td->io_ops->data;
45 struct io_u *ret = ld->sync_io_u;
51 return ev_to_iou(ld->aio_events + event);
54 static int fio_libaio_getevents(struct thread_data *td, int min, int max,
57 struct libaio_data *ld = td->io_ops->data;
64 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
67 else if (r == -EAGAIN) {
70 } else if (r == -EINTR)
82 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
84 struct libaio_data *ld = td->io_ops->data;
85 struct iocb *iocb = &io_u->iocb;
89 ret = io_submit(ld->aio_ctx, 1, &iocb);
92 else if (ret == -EAGAIN || !ret)
94 else if (ret == -EINTR)
96 else if (ret == -EINVAL && io_u->ddir == DDIR_SYNC) {
98 * the async fsync doesn't currently seem to be
99 * supported, so just fsync if we fail with EINVAL
100 * for a sync. since buffered io is also sync
101 * with libaio (still), we don't have pending
102 * requests to flush first.
104 if (fsync(io_u->file->fd) < 0)
108 ld->sync_io_u = io_u;
116 io_u->resid = io_u->xfer_buflen;
118 td_verror(td, io_u->error);
125 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
127 struct libaio_data *ld = td->io_ops->data;
129 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
132 static void fio_libaio_cleanup(struct thread_data *td)
134 struct libaio_data *ld = td->io_ops->data;
137 io_destroy(ld->aio_ctx);
139 free(ld->aio_events);
142 td->io_ops->data = NULL;
146 static int fio_libaio_init(struct thread_data *td)
148 struct libaio_data *ld = malloc(sizeof(*ld));
150 memset(ld, 0, sizeof(*ld));
151 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
152 td_verror(td, errno);
157 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
158 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
159 td->io_ops->data = ld;
163 static struct ioengine_ops ioengine = {
165 .version = FIO_IOOPS_VERSION,
166 .init = fio_libaio_init,
167 .prep = fio_libaio_prep,
168 .queue = fio_libaio_queue,
169 .cancel = fio_libaio_cancel,
170 .getevents = fio_libaio_getevents,
171 .event = fio_libaio_event,
172 .cleanup = fio_libaio_cleanup,
175 #else /* FIO_HAVE_LIBAIO */
178 * When we have a proper configure system in place, we simply wont build
179 * and install this io engine. For now install a crippled version that
180 * just complains and fails to load.
182 static int fio_libaio_init(struct thread_data fio_unused *td)
184 fprintf(stderr, "fio: libaio not available\n");
188 static struct ioengine_ops ioengine = {
190 .version = FIO_IOOPS_VERSION,
191 .init = fio_libaio_init,
196 static void fio_init fio_libaio_register(void)
198 register_ioengine(&ioengine);
201 static void fio_exit fio_libaio_unregister(void)
203 unregister_ioengine(&ioengine);