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, struct timespec *t)
96 struct posixaio_data *pd = td->io_ops->data;
97 os_aiocb_t *suspend_list[SUSPEND_ENTRIES];
98 struct flist_head *entry;
99 struct timespec start;
100 int have_timeout = 0;
101 int suspend_entries = 0;
104 if (t && !fill_timespec(&start))
107 memset(&start, 0, sizeof(start));
110 memset(suspend_list, 0, sizeof(*suspend_list));
112 flist_for_each(entry, &td->io_u_busylist) {
113 struct io_u *io_u = flist_entry(entry, struct io_u, list);
119 err = aio_error(&io_u->aiocb);
120 if (err == EINPROGRESS) {
121 if (suspend_entries < SUSPEND_ENTRIES) {
122 suspend_list[suspend_entries] = &io_u->aiocb;
130 pd->aio_events[r++] = io_u;
132 if (err == ECANCELED)
133 io_u->resid = io_u->xfer_buflen;
135 ssize_t retval = aio_return(&io_u->aiocb);
137 io_u->resid = io_u->xfer_buflen - retval;
146 unsigned long long usec;
148 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
149 if (ts_utime_since_now(&start) > usec)
154 * must have some in-flight, wait for at least one
156 aio_suspend((const os_aiocb_t * const *)suspend_list,
161 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
163 struct posixaio_data *pd = td->io_ops->data;
165 return pd->aio_events[event];
168 static int fio_posixaio_queue(struct thread_data *td,
171 struct posixaio_data *pd = td->io_ops->data;
172 os_aiocb_t *aiocb = &io_u->aiocb;
175 fio_ro_check(td, io_u);
177 if (io_u->ddir == DDIR_READ)
178 ret = aio_read(aiocb);
179 else if (io_u->ddir == DDIR_WRITE)
180 ret = aio_write(aiocb);
181 else if (io_u->ddir == DDIR_TRIM) {
185 do_io_u_trim(td, io_u);
186 return FIO_Q_COMPLETED;
188 #ifdef CONFIG_POSIXAIO_FSYNC
189 ret = aio_fsync(O_SYNC, aiocb);
194 do_io_u_sync(td, io_u);
195 return FIO_Q_COMPLETED;
201 * At least OSX has a very low limit on the number of pending
202 * IOs, so if it returns EAGAIN, we are out of resources
203 * to queue more. Just return FIO_Q_BUSY to naturally
204 * drop off at this depth.
210 td_verror(td, io_u->error, "xfer");
211 return FIO_Q_COMPLETED;
218 static void fio_posixaio_cleanup(struct thread_data *td)
220 struct posixaio_data *pd = td->io_ops->data;
223 free(pd->aio_events);
228 static int fio_posixaio_init(struct thread_data *td)
230 struct posixaio_data *pd = malloc(sizeof(*pd));
232 memset(pd, 0, sizeof(*pd));
233 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
234 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
236 td->io_ops->data = pd;
240 static struct ioengine_ops ioengine = {
242 .version = FIO_IOOPS_VERSION,
243 .init = fio_posixaio_init,
244 .prep = fio_posixaio_prep,
245 .queue = fio_posixaio_queue,
246 .cancel = fio_posixaio_cancel,
247 .getevents = fio_posixaio_getevents,
248 .event = fio_posixaio_event,
249 .cleanup = fio_posixaio_cleanup,
250 .open_file = generic_open_file,
251 .close_file = generic_close_file,
252 .get_file_size = generic_get_file_size,
255 static void fio_init fio_posixaio_register(void)
257 register_ioengine(&ioengine);
260 static void fio_exit fio_posixaio_unregister(void)
262 unregister_ioengine(&ioengine);