4 * IO engine that uses the posix defined aio interface.
15 struct posixaio_data {
16 struct io_u **aio_events;
20 static int fill_timespec(struct timespec *ts)
22 #ifdef CONFIG_CLOCK_GETTIME
23 #ifdef CONFIG_CLOCK_MONOTONIC
24 clockid_t clk = CLOCK_MONOTONIC;
26 clockid_t clk = CLOCK_REALTIME;
28 if (!clock_gettime(clk, ts))
31 perror("clock_gettime");
36 gettimeofday(&tv, NULL);
37 ts->tv_sec = tv.tv_sec;
38 ts->tv_nsec = tv.tv_usec * 1000;
43 static unsigned long long ts_utime_since_now(struct timespec *t)
48 if (fill_timespec(&now))
51 sec = now.tv_sec - t->tv_sec;
52 nsec = now.tv_nsec - t->tv_nsec;
53 if (sec > 0 && nsec < 0) {
63 static int fio_posixaio_cancel(struct thread_data fio_unused *td,
66 struct fio_file *f = io_u->file;
67 int r = aio_cancel(f->fd, &io_u->aiocb);
69 if (r == AIO_ALLDONE || r == AIO_CANCELED)
75 static int fio_posixaio_prep(struct thread_data fio_unused *td,
78 os_aiocb_t *aiocb = &io_u->aiocb;
79 struct fio_file *f = io_u->file;
81 aiocb->aio_fildes = f->fd;
82 aiocb->aio_buf = io_u->xfer_buf;
83 aiocb->aio_nbytes = io_u->xfer_buflen;
84 aiocb->aio_offset = io_u->offset;
85 aiocb->aio_sigevent.sigev_notify = SIGEV_NONE;
91 #define SUSPEND_ENTRIES 8
93 static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
94 unsigned int max, const struct timespec *t)
96 struct posixaio_data *pd = td->io_ops->data;
97 os_aiocb_t *suspend_list[SUSPEND_ENTRIES];
98 struct timespec start;
105 if (t && !fill_timespec(&start))
108 memset(&start, 0, sizeof(start));
112 memset(suspend_list, 0, sizeof(*suspend_list));
114 io_u_qiter(&td->io_u_all, io_u, i) {
117 if (io_u->seen || !(io_u->flags & IO_U_F_FLIGHT))
120 err = aio_error(&io_u->aiocb);
121 if (err == EINPROGRESS) {
122 if (suspend_entries < SUSPEND_ENTRIES) {
123 suspend_list[suspend_entries] = &io_u->aiocb;
131 pd->aio_events[r++] = io_u;
133 if (err == ECANCELED)
134 io_u->resid = io_u->xfer_buflen;
136 ssize_t retval = aio_return(&io_u->aiocb);
138 io_u->resid = io_u->xfer_buflen - retval;
147 unsigned long long usec;
149 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
150 if (ts_utime_since_now(&start) > usec)
155 * must have some in-flight, wait for at least one
157 aio_suspend((const os_aiocb_t * const *)suspend_list,
162 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
164 struct posixaio_data *pd = td->io_ops->data;
166 return pd->aio_events[event];
169 static int fio_posixaio_queue(struct thread_data *td,
172 struct posixaio_data *pd = td->io_ops->data;
173 os_aiocb_t *aiocb = &io_u->aiocb;
176 fio_ro_check(td, io_u);
178 if (io_u->ddir == DDIR_READ)
179 ret = aio_read(aiocb);
180 else if (io_u->ddir == DDIR_WRITE)
181 ret = aio_write(aiocb);
182 else if (io_u->ddir == DDIR_TRIM) {
186 do_io_u_trim(td, io_u);
187 return FIO_Q_COMPLETED;
189 #ifdef CONFIG_POSIXAIO_FSYNC
190 ret = aio_fsync(O_SYNC, aiocb);
195 do_io_u_sync(td, io_u);
196 return FIO_Q_COMPLETED;
201 int aio_err = aio_error(aiocb);
204 * At least OSX has a very low limit on the number of pending
205 * IOs, so if it returns EAGAIN, we are out of resources
206 * to queue more. Just return FIO_Q_BUSY to naturally
207 * drop off at this depth.
209 if (aio_err == EAGAIN)
212 io_u->error = aio_err;
213 td_verror(td, io_u->error, "xfer");
214 return FIO_Q_COMPLETED;
221 static void fio_posixaio_cleanup(struct thread_data *td)
223 struct posixaio_data *pd = td->io_ops->data;
226 free(pd->aio_events);
231 static int fio_posixaio_init(struct thread_data *td)
233 struct posixaio_data *pd = malloc(sizeof(*pd));
235 memset(pd, 0, sizeof(*pd));
236 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
237 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
239 td->io_ops->data = pd;
243 static struct ioengine_ops ioengine = {
245 .version = FIO_IOOPS_VERSION,
246 .init = fio_posixaio_init,
247 .prep = fio_posixaio_prep,
248 .queue = fio_posixaio_queue,
249 .cancel = fio_posixaio_cancel,
250 .getevents = fio_posixaio_getevents,
251 .event = fio_posixaio_event,
252 .cleanup = fio_posixaio_cleanup,
253 .open_file = generic_open_file,
254 .close_file = generic_close_file,
255 .get_file_size = generic_get_file_size,
258 static void fio_init fio_posixaio_register(void)
260 register_ioengine(&ioengine);
263 static void fio_exit fio_posixaio_unregister(void)
265 unregister_ioengine(&ioengine);