14 #ifdef FIO_HAVE_POSIXAIO
16 struct posixaio_data {
17 struct io_u **aio_events;
20 static int fill_timespec(struct timespec *ts)
23 if (!clock_gettime(CLOCK_MONOTONIC, ts))
26 perror("clock_gettime");
31 static unsigned long long ts_utime_since_now(struct timespec *t)
36 if (fill_timespec(&now))
39 sec = now.tv_sec - t->tv_sec;
40 nsec = now.tv_nsec - t->tv_nsec;
41 if (sec > 0 && nsec < 0) {
51 static int fio_posixaio_cancel(struct thread_data fio_unused *td,
54 struct fio_file *f = io_u->file;
55 int r = aio_cancel(f->fd, &io_u->aiocb);
57 if (r == 1 || r == AIO_CANCELED)
63 static int fio_posixaio_prep(struct thread_data fio_unused *td,
66 struct aiocb *aiocb = &io_u->aiocb;
67 struct fio_file *f = io_u->file;
69 aiocb->aio_fildes = f->fd;
70 aiocb->aio_buf = io_u->xfer_buf;
71 aiocb->aio_nbytes = io_u->xfer_buflen;
72 aiocb->aio_offset = io_u->offset;
78 static int fio_posixaio_getevents(struct thread_data *td, int min, int max,
81 struct posixaio_data *pd = td->io_ops->data;
82 struct list_head *entry;
83 struct timespec start;
84 int r, have_timeout = 0;
86 if (t && !fill_timespec(&start))
91 list_for_each(entry, &td->io_u_busylist) {
92 struct io_u *io_u = list_entry(entry, struct io_u, list);
98 err = aio_error(&io_u->aiocb);
104 pd->aio_events[r++] = io_u;
119 unsigned long long usec;
121 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
122 if (ts_utime_since_now(&start) > usec)
127 * hrmpf, we need to wait for more. we should use aio_suspend, for
128 * now just sleep a little and recheck status of busy-and-not-seen
134 static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
136 struct posixaio_data *pd = td->io_ops->data;
138 return pd->aio_events[event];
141 static int fio_posixaio_queue(struct thread_data fio_unused *td,
144 struct aiocb *aiocb = &io_u->aiocb;
147 if (io_u->ddir == DDIR_READ)
148 ret = aio_read(aiocb);
149 else if (io_u->ddir == DDIR_WRITE)
150 ret = aio_write(aiocb);
152 ret = aio_fsync(O_SYNC, aiocb);
156 td_verror(td, io_u->error, "xfer");
157 return FIO_Q_COMPLETED;
163 static void fio_posixaio_cleanup(struct thread_data *td)
165 struct posixaio_data *pd = td->io_ops->data;
168 free(pd->aio_events);
170 td->io_ops->data = NULL;
174 static int fio_posixaio_init(struct thread_data *td)
176 struct posixaio_data *pd = malloc(sizeof(*pd));
178 memset(pd, 0, sizeof(*pd));
179 pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *));
180 memset(pd->aio_events, 0, td->iodepth * sizeof(struct io_u *));
182 td->io_ops->data = pd;
186 static struct ioengine_ops ioengine = {
188 .version = FIO_IOOPS_VERSION,
189 .init = fio_posixaio_init,
190 .prep = fio_posixaio_prep,
191 .queue = fio_posixaio_queue,
192 .cancel = fio_posixaio_cancel,
193 .getevents = fio_posixaio_getevents,
194 .event = fio_posixaio_event,
195 .cleanup = fio_posixaio_cleanup,
198 #else /* FIO_HAVE_POSIXAIO */
201 * When we have a proper configure system in place, we simply wont build
202 * and install this io engine. For now install a crippled version that
203 * just complains and fails to load.
205 static int fio_posixaio_init(struct thread_data fio_unused *td)
207 fprintf(stderr, "fio: posixaio not available\n");
211 static struct ioengine_ops ioengine = {
213 .version = FIO_IOOPS_VERSION,
214 .init = fio_posixaio_init,
219 static void fio_init fio_posixaio_register(void)
221 register_ioengine(&ioengine);
224 static void fio_exit fio_posixaio_unregister(void)
226 unregister_ioengine(&ioengine);