4 * IO engine that uses the posix defined aio interface.
16 #ifdef FIO_HAVE_POSIXAIO
18 struct posixaio_data {
19 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 == 1 || 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 static int fio_posixaio_getevents(struct thread_data *td, int min, int max,
83 struct posixaio_data *pd = td->io_ops->data;
84 struct list_head *entry;
85 struct timespec start;
86 int r, have_timeout = 0;
88 if (t && !fill_timespec(&start))
93 list_for_each(entry, &td->io_u_busylist) {
94 struct io_u *io_u = list_entry(entry, struct io_u, list);
100 err = aio_error(&io_u->aiocb);
105 io_u->resid = io_u->xfer_buflen;
107 pd->aio_events[r++] = io_u;
122 unsigned long long usec;
124 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
125 if (ts_utime_since_now(&start) > usec)
130 * hrmpf, we need to wait for more. we should use aio_suspend, for
131 * now just sleep a little and recheck status of busy-and-not-seen
137 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
139 struct posixaio_data *pd = td->io_ops->data;
141 return pd->aio_events[event];
144 static int fio_posixaio_queue(struct thread_data fio_unused *td,
147 struct aiocb *aiocb = &io_u->aiocb;
150 if (io_u->ddir == DDIR_READ)
151 ret = aio_read(aiocb);
152 else if (io_u->ddir == DDIR_WRITE)
153 ret = aio_write(aiocb);
155 ret = aio_fsync(O_SYNC, aiocb);
159 td_verror(td, io_u->error, "xfer");
160 return FIO_Q_COMPLETED;
166 static void fio_posixaio_cleanup(struct thread_data *td)
168 struct posixaio_data *pd = td->io_ops->data;
171 free(pd->aio_events);
173 td->io_ops->data = NULL;
177 static int fio_posixaio_init(struct thread_data *td)
179 struct posixaio_data *pd = malloc(sizeof(*pd));
181 memset(pd, 0, sizeof(*pd));
182 pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *));
183 memset(pd->aio_events, 0, td->iodepth * sizeof(struct io_u *));
185 td->io_ops->data = pd;
189 static struct ioengine_ops ioengine = {
191 .version = FIO_IOOPS_VERSION,
192 .init = fio_posixaio_init,
193 .prep = fio_posixaio_prep,
194 .queue = fio_posixaio_queue,
195 .cancel = fio_posixaio_cancel,
196 .getevents = fio_posixaio_getevents,
197 .event = fio_posixaio_event,
198 .cleanup = fio_posixaio_cleanup,
199 .open_file = generic_open_file,
200 .close_file = generic_close_file,
203 #else /* FIO_HAVE_POSIXAIO */
206 * When we have a proper configure system in place, we simply wont build
207 * and install this io engine. For now install a crippled version that
208 * just complains and fails to load.
210 static int fio_posixaio_init(struct thread_data fio_unused *td)
212 fprintf(stderr, "fio: posixaio not available\n");
216 static struct ioengine_ops ioengine = {
218 .version = FIO_IOOPS_VERSION,
219 .init = fio_posixaio_init,
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);