2 * native linux aio io engine
13 #ifdef FIO_HAVE_LIBAIO
15 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
19 struct io_event *aio_events;
22 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
24 struct fio_file *f = io_u->file;
26 if (io_u->ddir == DDIR_READ)
27 io_prep_pread(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
28 else if (io_u->ddir == DDIR_WRITE)
29 io_prep_pwrite(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
30 else if (io_u->ddir == DDIR_SYNC)
31 io_prep_fsync(&io_u->iocb, f->fd);
38 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
40 struct libaio_data *ld = td->io_ops->data;
42 return ev_to_iou(ld->aio_events + event);
45 static int fio_libaio_getevents(struct thread_data *td, int min, int max,
48 struct libaio_data *ld = td->io_ops->data;
52 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
56 } 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)
87 io_u->resid = io_u->buflen;
95 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
97 struct libaio_data *ld = td->io_ops->data;
99 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
102 static void fio_libaio_cleanup(struct thread_data *td)
104 struct libaio_data *ld = td->io_ops->data;
107 io_destroy(ld->aio_ctx);
109 free(ld->aio_events);
112 td->io_ops->data = NULL;
116 static int fio_libaio_init(struct thread_data *td)
118 struct libaio_data *ld = malloc(sizeof(*ld));
120 memset(ld, 0, sizeof(*ld));
121 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
122 td_verror(td, errno);
127 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
128 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
129 td->io_ops->data = ld;
133 struct ioengine_ops ioengine = {
135 .version = FIO_IOOPS_VERSION,
136 .init = fio_libaio_init,
137 .prep = fio_libaio_prep,
138 .queue = fio_libaio_queue,
139 .cancel = fio_libaio_cancel,
140 .getevents = fio_libaio_getevents,
141 .event = fio_libaio_event,
142 .cleanup = fio_libaio_cleanup,
145 #else /* FIO_HAVE_LIBAIO */
148 * When we have a proper configure system in place, we simply wont build
149 * and install this io engine. For now install a crippled version that
150 * just complains and fails to load.
152 static int fio_libaio_init(struct thread_data fio_unused *td)
154 fprintf(stderr, "fio: libaio not available\n");
158 struct ioengine_ops ioengine = {
160 .version = FIO_IOOPS_VERSION,
161 .init = fio_libaio_init,