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;
25 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
27 struct fio_file *f = io_u->file;
29 if (io_u->ddir == DDIR_READ)
30 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
31 else if (io_u->ddir == DDIR_WRITE)
32 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
33 else if (io_u->ddir == DDIR_SYNC)
34 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;
45 return ev_to_iou(ld->aio_events + event);
48 static int fio_libaio_getevents(struct thread_data *td, int min, int max,
51 struct libaio_data *ld = td->io_ops->data;
55 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
58 else if (r == -EAGAIN) {
61 } else if (r == -EINTR)
70 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
72 struct libaio_data *ld = td->io_ops->data;
74 if (ld->iocbs_nr == (int) td->iodepth)
78 * fsync is tricky, since it can fail and we need to do it
79 * serialized with other io. the reason is that linux doesn't
80 * support aio fsync yet. So return busy for the case where we
81 * have pending io, to let fio complete those first.
83 if (io_u->ddir == DDIR_SYNC) {
86 if (fsync(io_u->file->fd) < 0)
89 return FIO_Q_COMPLETED;
92 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
97 static int fio_libaio_commit(struct thread_data *td)
99 struct libaio_data *ld = td->io_ops->data;
106 iocbs_nr = ld->iocbs_nr;
109 ret = io_submit(ld->aio_ctx, iocbs_nr, iocbs);
110 if (ret == iocbs_nr) {
113 } else if (ret > 0) {
117 } else if (ret == -EAGAIN || !ret)
119 else if (ret == -EINTR)
131 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
133 struct libaio_data *ld = td->io_ops->data;
135 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
138 static void fio_libaio_cleanup(struct thread_data *td)
140 struct libaio_data *ld = td->io_ops->data;
143 io_destroy(ld->aio_ctx);
145 free(ld->aio_events);
150 td->io_ops->data = NULL;
154 static int fio_libaio_init(struct thread_data *td)
156 struct libaio_data *ld = malloc(sizeof(*ld));
158 memset(ld, 0, sizeof(*ld));
159 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
160 td_verror(td, errno);
165 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
166 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
167 ld->iocbs = malloc(td->iodepth * sizeof(struct iocb *));
168 memset(ld->iocbs, 0, sizeof(struct iocb *));
171 td->io_ops->data = ld;
175 static struct ioengine_ops ioengine = {
177 .version = FIO_IOOPS_VERSION,
178 .init = fio_libaio_init,
179 .prep = fio_libaio_prep,
180 .queue = fio_libaio_queue,
181 .commit = fio_libaio_commit,
182 .cancel = fio_libaio_cancel,
183 .getevents = fio_libaio_getevents,
184 .event = fio_libaio_event,
185 .cleanup = fio_libaio_cleanup,
188 #else /* FIO_HAVE_LIBAIO */
191 * When we have a proper configure system in place, we simply wont build
192 * and install this io engine. For now install a crippled version that
193 * just complains and fails to load.
195 static int fio_libaio_init(struct thread_data fio_unused *td)
197 fprintf(stderr, "fio: libaio not available\n");
201 static struct ioengine_ops ioengine = {
203 .version = FIO_IOOPS_VERSION,
204 .init = fio_libaio_init,
209 static void fio_init fio_libaio_register(void)
211 register_ioengine(&ioengine);
214 static void fio_exit fio_libaio_unregister(void)
216 unregister_ioengine(&ioengine);