4 * IO engine that uses the posix defined aio interface.
15 #ifdef FIO_HAVE_POSIXAIO
17 struct posixaio_data {
18 struct io_u **aio_events;
21 static int fill_timespec(struct timespec *ts)
24 if (!clock_gettime(CLOCK_MONOTONIC, ts))
27 perror("clock_gettime");
32 static unsigned long long ts_utime_since_now(struct timespec *t)
37 if (fill_timespec(&now))
40 sec = now.tv_sec - t->tv_sec;
41 nsec = now.tv_nsec - t->tv_nsec;
42 if (sec > 0 && nsec < 0) {
52 static int fio_posixaio_cancel(struct thread_data fio_unused *td,
55 struct fio_file *f = io_u->file;
56 int r = aio_cancel(f->fd, &io_u->aiocb);
58 if (r == 1 || r == AIO_CANCELED)
64 static int fio_posixaio_prep(struct thread_data fio_unused *td,
67 struct aiocb *aiocb = &io_u->aiocb;
68 struct fio_file *f = io_u->file;
70 aiocb->aio_fildes = f->fd;
71 aiocb->aio_buf = io_u->xfer_buf;
72 aiocb->aio_nbytes = io_u->xfer_buflen;
73 aiocb->aio_offset = io_u->offset;
79 static int fio_posixaio_getevents(struct thread_data *td, int min, int max,
82 struct posixaio_data *pd = td->io_ops->data;
83 struct list_head *entry;
84 struct timespec start;
85 int r, have_timeout = 0;
87 if (t && !fill_timespec(&start))
92 list_for_each(entry, &td->io_u_busylist) {
93 struct io_u *io_u = list_entry(entry, struct io_u, list);
99 err = aio_error(&io_u->aiocb);
100 if (err == EINPROGRESS)
104 pd->aio_events[r++] = io_u;
106 if (err == ECANCELED)
107 io_u->resid = io_u->xfer_buflen;
109 ssize_t retval = aio_return(&io_u->aiocb);
111 io_u->resid = io_u->xfer_buflen - retval;
123 unsigned long long usec;
125 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
126 if (ts_utime_since_now(&start) > usec)
131 * hrmpf, we need to wait for more. we should use aio_suspend, for
132 * now just sleep a little and recheck status of busy-and-not-seen
138 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
140 struct posixaio_data *pd = td->io_ops->data;
142 return pd->aio_events[event];
145 static int fio_posixaio_queue(struct thread_data fio_unused *td,
148 struct aiocb *aiocb = &io_u->aiocb;
151 fio_ro_check(td, io_u);
153 if (io_u->ddir == DDIR_READ)
154 ret = aio_read(aiocb);
155 else if (io_u->ddir == DDIR_WRITE)
156 ret = aio_write(aiocb);
158 ret = aio_fsync(O_SYNC, aiocb);
162 td_verror(td, io_u->error, "xfer");
163 return FIO_Q_COMPLETED;
169 static void fio_posixaio_cleanup(struct thread_data *td)
171 struct posixaio_data *pd = td->io_ops->data;
174 free(pd->aio_events);
176 td->io_ops->data = NULL;
180 static int fio_posixaio_init(struct thread_data *td)
182 struct posixaio_data *pd = malloc(sizeof(*pd));
184 memset(pd, 0, sizeof(*pd));
185 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
186 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
188 td->io_ops->data = pd;
192 static struct ioengine_ops ioengine = {
194 .version = FIO_IOOPS_VERSION,
195 .init = fio_posixaio_init,
196 .prep = fio_posixaio_prep,
197 .queue = fio_posixaio_queue,
198 .cancel = fio_posixaio_cancel,
199 .getevents = fio_posixaio_getevents,
200 .event = fio_posixaio_event,
201 .cleanup = fio_posixaio_cleanup,
202 .open_file = generic_open_file,
203 .close_file = generic_close_file,
206 #else /* FIO_HAVE_POSIXAIO */
209 * When we have a proper configure system in place, we simply wont build
210 * and install this io engine. For now install a crippled version that
211 * just complains and fails to load.
213 static int fio_posixaio_init(struct thread_data fio_unused *td)
215 fprintf(stderr, "fio: posixaio not available\n");
219 static struct ioengine_ops ioengine = {
221 .version = FIO_IOOPS_VERSION,
222 .init = fio_posixaio_init,
227 static void fio_init fio_posixaio_register(void)
229 register_ioengine(&ioengine);
232 static void fio_exit fio_posixaio_unregister(void)
234 unregister_ioengine(&ioengine);