4 * IO engine that uses the posix defined aio interface.
15 struct posixaio_data {
16 struct io_u **aio_events;
20 static unsigned long long ts_utime_since_now(const struct timespec *start)
24 if (fio_get_mono_time(&now) < 0)
27 return utime_since(start, &now);
30 static int fio_posixaio_cancel(struct thread_data fio_unused *td,
33 struct fio_file *f = io_u->file;
34 int r = aio_cancel(f->fd, &io_u->aiocb);
36 if (r == AIO_ALLDONE || r == AIO_CANCELED)
42 static int fio_posixaio_prep(struct thread_data fio_unused *td,
45 os_aiocb_t *aiocb = &io_u->aiocb;
46 struct fio_file *f = io_u->file;
48 aiocb->aio_fildes = f->fd;
49 aiocb->aio_buf = io_u->xfer_buf;
50 aiocb->aio_nbytes = io_u->xfer_buflen;
51 aiocb->aio_offset = io_u->offset;
52 aiocb->aio_sigevent.sigev_notify = SIGEV_NONE;
58 #define SUSPEND_ENTRIES 8
60 static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
61 unsigned int max, const struct timespec *t)
63 struct posixaio_data *pd = td->io_ops_data;
64 os_aiocb_t *suspend_list[SUSPEND_ENTRIES];
65 struct timespec start;
72 if (t && fio_get_mono_time(&start) == 0)
75 memset(&start, 0, sizeof(start));
79 memset(suspend_list, 0, sizeof(suspend_list));
81 io_u_qiter(&td->io_u_all, io_u, i) {
84 if (io_u->seen || !(io_u->flags & IO_U_F_FLIGHT))
87 err = aio_error(&io_u->aiocb);
88 if (err == EINPROGRESS) {
89 if (suspend_entries < SUSPEND_ENTRIES) {
90 suspend_list[suspend_entries] = &io_u->aiocb;
98 pd->aio_events[r++] = io_u;
100 if (err == ECANCELED)
101 io_u->resid = io_u->xfer_buflen;
103 ssize_t retval = aio_return(&io_u->aiocb);
105 io_u->resid = io_u->xfer_buflen - retval;
114 unsigned long long usec;
116 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
117 if (ts_utime_since_now(&start) > usec)
122 * must have some in-flight, wait for at least one
124 aio_suspend((const os_aiocb_t * const *)suspend_list,
129 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
131 struct posixaio_data *pd = td->io_ops_data;
133 return pd->aio_events[event];
136 static enum fio_q_status fio_posixaio_queue(struct thread_data *td,
139 struct posixaio_data *pd = td->io_ops_data;
140 os_aiocb_t *aiocb = &io_u->aiocb;
143 fio_ro_check(td, io_u);
145 if (io_u->ddir == DDIR_READ)
146 ret = aio_read(aiocb);
147 else if (io_u->ddir == DDIR_WRITE)
148 ret = aio_write(aiocb);
149 else if (io_u->ddir == DDIR_TRIM) {
153 do_io_u_trim(td, io_u);
154 return FIO_Q_COMPLETED;
156 #ifdef CONFIG_POSIXAIO_FSYNC
157 ret = aio_fsync(O_SYNC, aiocb);
162 do_io_u_sync(td, io_u);
163 return FIO_Q_COMPLETED;
171 * At least OSX has a very low limit on the number of pending
172 * IOs, so if it returns EAGAIN, we are out of resources
173 * to queue more. Just return FIO_Q_BUSY to naturally
174 * drop off at this depth.
176 if (aio_err == EAGAIN)
179 io_u->error = aio_err;
180 td_verror(td, io_u->error, "xfer");
181 return FIO_Q_COMPLETED;
188 static void fio_posixaio_cleanup(struct thread_data *td)
190 struct posixaio_data *pd = td->io_ops_data;
193 free(pd->aio_events);
198 static int fio_posixaio_init(struct thread_data *td)
200 struct posixaio_data *pd;
201 pd = calloc(1, sizeof(*pd));
202 pd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
204 td->io_ops_data = pd;
208 static struct ioengine_ops ioengine = {
210 .version = FIO_IOOPS_VERSION,
211 .flags = FIO_ASYNCIO_SYNC_TRIM,
212 .init = fio_posixaio_init,
213 .prep = fio_posixaio_prep,
214 .queue = fio_posixaio_queue,
215 .cancel = fio_posixaio_cancel,
216 .getevents = fio_posixaio_getevents,
217 .event = fio_posixaio_event,
218 .cleanup = fio_posixaio_cleanup,
219 .open_file = generic_open_file,
220 .close_file = generic_close_file,
221 .get_file_size = generic_get_file_size,
224 static void fio_init fio_posixaio_register(void)
226 register_ioengine(&ioengine);
229 static void fio_exit fio_posixaio_unregister(void)
231 unregister_ioengine(&ioengine);