2 * native linux aio io engine
13 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
17 struct io_event *aio_events;
20 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
22 struct fio_file *f = io_u->file;
24 if (io_u->ddir == DDIR_READ)
25 io_prep_pread(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
26 else if (io_u->ddir == DDIR_WRITE)
27 io_prep_pwrite(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
28 else if (io_u->ddir == DDIR_SYNC)
29 io_prep_fsync(&io_u->iocb, f->fd);
36 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
38 struct libaio_data *ld = td->io_ops->data;
40 return ev_to_iou(ld->aio_events + event);
43 static int fio_libaio_getevents(struct thread_data *td, int min, int max,
46 struct libaio_data *ld = td->io_ops->data;
50 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
54 } else if (r == -EINTR)
66 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
68 struct libaio_data *ld = td->io_ops->data;
69 struct iocb *iocb = &io_u->iocb;
73 ret = io_submit(ld->aio_ctx, 1, &iocb);
76 else if (ret == -EAGAIN || !ret)
78 else if (ret == -EINTR)
85 io_u->resid = io_u->buflen;
93 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
95 struct libaio_data *ld = td->io_ops->data;
97 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
100 static void fio_libaio_cleanup(struct thread_data *td)
102 struct libaio_data *ld = td->io_ops->data;
105 io_destroy(ld->aio_ctx);
107 free(ld->aio_events);
110 td->io_ops->data = NULL;
114 static int fio_libaio_init(struct thread_data *td)
116 struct libaio_data *ld = malloc(sizeof(*ld));
118 memset(ld, 0, sizeof(*ld));
119 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
120 td_verror(td, errno);
124 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
125 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
126 td->io_ops->data = ld;
130 struct ioengine_ops ioengine = {
132 .version = FIO_IOOPS_VERSION,
133 .init = fio_libaio_init,
134 .prep = fio_libaio_prep,
135 .queue = fio_libaio_queue,
136 .cancel = fio_libaio_cancel,
137 .getevents = fio_libaio_getevents,
138 .event = fio_libaio_event,
139 .cleanup = fio_libaio_cleanup,