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;
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 == AIO_ALLDONE || 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 #define SUSPEND_ENTRIES 8
82 static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
83 unsigned int max, struct timespec *t)
85 struct posixaio_data *pd = td->io_ops->data;
86 struct aiocb *suspend_list[SUSPEND_ENTRIES];
87 struct flist_head *entry;
88 struct timespec start;
90 int suspend_entries = 0;
93 if (t && !fill_timespec(&start))
97 memset(suspend_list, 0, sizeof(*suspend_list));
99 flist_for_each(entry, &td->io_u_busylist) {
100 struct io_u *io_u = flist_entry(entry, struct io_u, list);
106 err = aio_error(&io_u->aiocb);
107 if (err == EINPROGRESS) {
108 if (suspend_entries < SUSPEND_ENTRIES) {
109 suspend_list[suspend_entries] = &io_u->aiocb;
117 pd->aio_events[r++] = io_u;
119 if (err == ECANCELED)
120 io_u->resid = io_u->xfer_buflen;
122 ssize_t retval = aio_return(&io_u->aiocb);
124 io_u->resid = io_u->xfer_buflen - retval;
133 unsigned long long usec;
135 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
136 if (ts_utime_since_now(&start) > usec)
141 * must have some in-flight, wait for at least one
143 aio_suspend((const struct aiocb * const *)suspend_list,
148 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
150 struct posixaio_data *pd = td->io_ops->data;
152 return pd->aio_events[event];
155 static int fio_posixaio_queue(struct thread_data *td,
158 struct posixaio_data *pd = td->io_ops->data;
159 struct aiocb *aiocb = &io_u->aiocb;
162 fio_ro_check(td, io_u);
164 if (io_u->ddir == DDIR_READ)
165 ret = aio_read(aiocb);
166 else if (io_u->ddir == DDIR_WRITE)
167 ret = aio_write(aiocb);
168 else if (io_u->ddir == DDIR_TRIM) {
172 do_io_u_trim(td, io_u);
173 return FIO_Q_COMPLETED;
175 #ifdef FIO_HAVE_POSIXAIO_FSYNC
176 ret = aio_fsync(O_SYNC, aiocb);
181 do_io_u_sync(td, io_u);
182 return FIO_Q_COMPLETED;
188 * At least OSX has a very low limit on the number of pending
189 * IOs, so if it will return EAGAIN.
195 td_verror(td, io_u->error, "xfer");
196 return FIO_Q_COMPLETED;
203 static void fio_posixaio_cleanup(struct thread_data *td)
205 struct posixaio_data *pd = td->io_ops->data;
208 free(pd->aio_events);
213 static int fio_posixaio_init(struct thread_data *td)
215 struct posixaio_data *pd = malloc(sizeof(*pd));
217 memset(pd, 0, sizeof(*pd));
218 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
219 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
221 td->io_ops->data = pd;
225 static struct ioengine_ops ioengine = {
227 .version = FIO_IOOPS_VERSION,
228 .init = fio_posixaio_init,
229 .prep = fio_posixaio_prep,
230 .queue = fio_posixaio_queue,
231 .cancel = fio_posixaio_cancel,
232 .getevents = fio_posixaio_getevents,
233 .event = fio_posixaio_event,
234 .cleanup = fio_posixaio_cleanup,
235 .open_file = generic_open_file,
236 .close_file = generic_close_file,
237 .get_file_size = generic_get_file_size,
240 #else /* FIO_HAVE_POSIXAIO */
243 * When we have a proper configure system in place, we simply wont build
244 * and install this io engine. For now install a crippled version that
245 * just complains and fails to load.
247 static int fio_posixaio_init(struct thread_data fio_unused *td)
249 log_err("fio: posixaio not available\n");
253 static struct ioengine_ops ioengine = {
255 .version = FIO_IOOPS_VERSION,
256 .init = fio_posixaio_init,
261 static void fio_init fio_posixaio_register(void)
263 register_ioengine(&ioengine);
266 static void fio_exit fio_posixaio_unregister(void)
268 unregister_ioengine(&ioengine);