4 * IO engine that uses the posix defined aio interface.
16 #ifdef FIO_HAVE_POSIXAIO
18 struct posixaio_data {
19 struct io_u **aio_events;
22 static int fill_timespec(struct timespec *ts)
25 if (!clock_gettime(CLOCK_MONOTONIC, ts))
28 perror("clock_gettime");
33 static unsigned long long ts_utime_since_now(struct timespec *t)
38 if (fill_timespec(&now))
41 sec = now.tv_sec - t->tv_sec;
42 nsec = now.tv_nsec - t->tv_nsec;
43 if (sec > 0 && nsec < 0) {
53 static int fio_posixaio_cancel(struct thread_data fio_unused *td,
56 struct fio_file *f = io_u->file;
57 int r = aio_cancel(f->fd, &io_u->aiocb);
59 if (r == 1 || r == AIO_CANCELED)
65 static int fio_posixaio_prep(struct thread_data fio_unused *td,
68 struct aiocb *aiocb = &io_u->aiocb;
69 struct fio_file *f = io_u->file;
71 aiocb->aio_fildes = f->fd;
72 aiocb->aio_buf = io_u->xfer_buf;
73 aiocb->aio_nbytes = io_u->xfer_buflen;
74 aiocb->aio_offset = io_u->offset;
80 static int fio_posixaio_getevents(struct thread_data *td, int min, int max,
83 struct posixaio_data *pd = td->io_ops->data;
84 struct list_head *entry;
85 struct timespec start;
86 int r, have_timeout = 0;
88 if (t && !fill_timespec(&start))
93 list_for_each(entry, &td->io_u_busylist) {
94 struct io_u *io_u = list_entry(entry, struct io_u, list);
100 err = aio_error(&io_u->aiocb);
101 if (err == EINPROGRESS)
105 pd->aio_events[r++] = io_u;
107 if (err == ECANCELED)
108 io_u->resid = io_u->xfer_buflen;
110 ssize_t retval = aio_return(&io_u->aiocb);
112 io_u->resid = io_u->xfer_buflen - retval;
124 unsigned long long usec;
126 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
127 if (ts_utime_since_now(&start) > usec)
132 * hrmpf, we need to wait for more. we should use aio_suspend, for
133 * now just sleep a little and recheck status of busy-and-not-seen
139 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
141 struct posixaio_data *pd = td->io_ops->data;
143 return pd->aio_events[event];
146 static int fio_posixaio_queue(struct thread_data fio_unused *td,
149 struct aiocb *aiocb = &io_u->aiocb;
152 if (io_u->ddir == DDIR_READ)
153 ret = aio_read(aiocb);
154 else if (io_u->ddir == DDIR_WRITE)
155 ret = aio_write(aiocb);
157 ret = aio_fsync(O_SYNC, aiocb);
161 td_verror(td, io_u->error, "xfer");
162 return FIO_Q_COMPLETED;
168 static void fio_posixaio_cleanup(struct thread_data *td)
170 struct posixaio_data *pd = td->io_ops->data;
173 free(pd->aio_events);
175 td->io_ops->data = NULL;
179 static int fio_posixaio_init(struct thread_data *td)
181 struct posixaio_data *pd = malloc(sizeof(*pd));
183 memset(pd, 0, sizeof(*pd));
184 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
185 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
187 td->io_ops->data = pd;
191 static struct ioengine_ops ioengine = {
193 .version = FIO_IOOPS_VERSION,
194 .init = fio_posixaio_init,
195 .prep = fio_posixaio_prep,
196 .queue = fio_posixaio_queue,
197 .cancel = fio_posixaio_cancel,
198 .getevents = fio_posixaio_getevents,
199 .event = fio_posixaio_event,
200 .cleanup = fio_posixaio_cleanup,
201 .open_file = generic_open_file,
202 .close_file = generic_close_file,
205 #else /* FIO_HAVE_POSIXAIO */
208 * When we have a proper configure system in place, we simply wont build
209 * and install this io engine. For now install a crippled version that
210 * just complains and fails to load.
212 static int fio_posixaio_init(struct thread_data fio_unused *td)
214 fprintf(stderr, "fio: posixaio not available\n");
218 static struct ioengine_ops ioengine = {
220 .version = FIO_IOOPS_VERSION,
221 .init = fio_posixaio_init,
226 static void fio_init fio_posixaio_register(void)
228 register_ioengine(&ioengine);
231 static void fio_exit fio_posixaio_unregister(void)
233 unregister_ioengine(&ioengine);