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;
23 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
25 struct fio_file *f = io_u->file;
27 if (io_u->ddir == DDIR_READ)
28 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
29 else if (io_u->ddir == DDIR_WRITE)
30 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
31 else if (io_u->ddir == DDIR_SYNC)
32 io_prep_fsync(&io_u->iocb, f->fd);
39 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
41 struct libaio_data *ld = td->io_ops->data;
43 return ev_to_iou(ld->aio_events + event);
46 static int fio_libaio_getevents(struct thread_data *td, int min, int max,
49 struct libaio_data *ld = td->io_ops->data;
53 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
56 else if (r == -EAGAIN) {
59 } else if (r == -EINTR)
68 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
70 struct libaio_data *ld = td->io_ops->data;
71 struct iocb *iocb = &io_u->iocb;
75 ret = io_submit(ld->aio_ctx, 1, &iocb);
78 else if (ret == -EAGAIN || !ret)
80 else if (ret == -EINTR)
82 else if (ret == -EINVAL && io_u->ddir == DDIR_SYNC) {
84 * the async fsync doesn't currently seem to be
85 * supported, so just fsync if we fail with EINVAL
86 * for a sync. since buffered io is also sync
87 * with libaio (still), we don't have pending
88 * requests to flush first.
90 if (fsync(io_u->file->fd) < 0)
93 ret = FIO_Q_COMPLETED;
100 io_u->resid = io_u->xfer_buflen;
102 td_verror(td, io_u->error);
103 return FIO_Q_COMPLETED;
109 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
111 struct libaio_data *ld = td->io_ops->data;
113 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
116 static void fio_libaio_cleanup(struct thread_data *td)
118 struct libaio_data *ld = td->io_ops->data;
121 io_destroy(ld->aio_ctx);
123 free(ld->aio_events);
126 td->io_ops->data = NULL;
130 static int fio_libaio_init(struct thread_data *td)
132 struct libaio_data *ld = malloc(sizeof(*ld));
134 memset(ld, 0, sizeof(*ld));
135 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
136 td_verror(td, errno);
141 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
142 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
143 td->io_ops->data = ld;
147 static struct ioengine_ops ioengine = {
149 .version = FIO_IOOPS_VERSION,
150 .init = fio_libaio_init,
151 .prep = fio_libaio_prep,
152 .queue = fio_libaio_queue,
153 .cancel = fio_libaio_cancel,
154 .getevents = fio_libaio_getevents,
155 .event = fio_libaio_event,
156 .cleanup = fio_libaio_cleanup,
159 #else /* FIO_HAVE_LIBAIO */
162 * When we have a proper configure system in place, we simply wont build
163 * and install this io engine. For now install a crippled version that
164 * just complains and fails to load.
166 static int fio_libaio_init(struct thread_data fio_unused *td)
168 fprintf(stderr, "fio: libaio not available\n");
172 static struct ioengine_ops ioengine = {
174 .version = FIO_IOOPS_VERSION,
175 .init = fio_libaio_init,
180 static void fio_init fio_libaio_register(void)
182 register_ioengine(&ioengine);
185 static void fio_exit fio_libaio_unregister(void)
187 unregister_ioengine(&ioengine);