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 os_aiocb_t *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;
75 aiocb->aio_sigevent.sigev_notify = SIGEV_NONE;
81 #define SUSPEND_ENTRIES 8
83 static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
84 unsigned int max, struct timespec *t)
86 struct posixaio_data *pd = td->io_ops->data;
87 os_aiocb_t *suspend_list[SUSPEND_ENTRIES];
88 struct flist_head *entry;
89 struct timespec start;
91 int suspend_entries = 0;
94 if (t && !fill_timespec(&start))
98 memset(suspend_list, 0, sizeof(*suspend_list));
100 flist_for_each(entry, &td->io_u_busylist) {
101 struct io_u *io_u = flist_entry(entry, struct io_u, list);
107 err = aio_error(&io_u->aiocb);
108 if (err == EINPROGRESS) {
109 if (suspend_entries < SUSPEND_ENTRIES) {
110 suspend_list[suspend_entries] = &io_u->aiocb;
118 pd->aio_events[r++] = io_u;
120 if (err == ECANCELED)
121 io_u->resid = io_u->xfer_buflen;
123 ssize_t retval = aio_return(&io_u->aiocb);
125 io_u->resid = io_u->xfer_buflen - retval;
134 unsigned long long usec;
136 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
137 if (ts_utime_since_now(&start) > usec)
142 * must have some in-flight, wait for at least one
144 aio_suspend((const os_aiocb_t * const *)suspend_list,
149 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
151 struct posixaio_data *pd = td->io_ops->data;
153 return pd->aio_events[event];
156 static int fio_posixaio_queue(struct thread_data *td,
159 struct posixaio_data *pd = td->io_ops->data;
160 os_aiocb_t *aiocb = &io_u->aiocb;
163 fio_ro_check(td, io_u);
165 if (io_u->ddir == DDIR_READ)
166 ret = aio_read(aiocb);
167 else if (io_u->ddir == DDIR_WRITE)
168 ret = aio_write(aiocb);
169 else if (io_u->ddir == DDIR_TRIM) {
173 do_io_u_trim(td, io_u);
174 return FIO_Q_COMPLETED;
176 #ifdef FIO_HAVE_POSIXAIO_FSYNC
177 ret = aio_fsync(O_SYNC, aiocb);
182 do_io_u_sync(td, io_u);
183 return FIO_Q_COMPLETED;
189 * At least OSX has a very low limit on the number of pending
190 * IOs, so if it returns EAGAIN, we are out of resources
191 * to queue more. Just return FIO_Q_BUSY to naturally
192 * drop off at this depth.
198 td_verror(td, io_u->error, "xfer");
199 return FIO_Q_COMPLETED;
206 static void fio_posixaio_cleanup(struct thread_data *td)
208 struct posixaio_data *pd = td->io_ops->data;
211 free(pd->aio_events);
216 static int fio_posixaio_init(struct thread_data *td)
218 struct posixaio_data *pd = malloc(sizeof(*pd));
220 memset(pd, 0, sizeof(*pd));
221 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
222 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
224 td->io_ops->data = pd;
228 static struct ioengine_ops ioengine = {
230 .version = FIO_IOOPS_VERSION,
231 .init = fio_posixaio_init,
232 .prep = fio_posixaio_prep,
233 .queue = fio_posixaio_queue,
234 .cancel = fio_posixaio_cancel,
235 .getevents = fio_posixaio_getevents,
236 .event = fio_posixaio_event,
237 .cleanup = fio_posixaio_cleanup,
238 .open_file = generic_open_file,
239 .close_file = generic_close_file,
240 .get_file_size = generic_get_file_size,
243 #else /* FIO_HAVE_POSIXAIO */
246 * When we have a proper configure system in place, we simply wont build
247 * and install this io engine. For now install a crippled version that
248 * just complains and fails to load.
250 static int fio_posixaio_init(struct thread_data fio_unused *td)
252 log_err("fio: posixaio not available\n");
256 static struct ioengine_ops ioengine = {
258 .version = FIO_IOOPS_VERSION,
259 .init = fio_posixaio_init,
264 static void fio_init fio_posixaio_register(void)
266 register_ioengine(&ioengine);
269 static void fio_exit fio_posixaio_unregister(void)
271 unregister_ioengine(&ioengine);