blob: 3ffdcb6bccc8d062b1be302b27c3cb004bd57d11 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * posixaio engine
3 *
4 * IO engine that uses the posix defined aio interface.
Jens Axboe2866c822006-10-09 15:57:48 +02005 *
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
Jens Axboebc733f32008-06-02 12:25:12 +020011#include <fcntl.h>
Jens Axboe5f350952006-11-07 15:20:59 +010012
13#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020014
Jens Axboe34cfcda2006-11-03 14:00:45 +010015#ifdef FIO_HAVE_POSIXAIO
16
Jens Axboe2866c822006-10-09 15:57:48 +020017struct posixaio_data {
18 struct io_u **aio_events;
Jens Axboe207cb0f2008-06-02 12:28:02 +020019 unsigned int queued;
Jens Axboe2866c822006-10-09 15:57:48 +020020};
21
22static int fill_timespec(struct timespec *ts)
23{
24#ifdef _POSIX_TIMERS
25 if (!clock_gettime(CLOCK_MONOTONIC, ts))
26 return 0;
27
28 perror("clock_gettime");
29#endif
30 return 1;
31}
32
33static unsigned long long ts_utime_since_now(struct timespec *t)
34{
35 long long sec, nsec;
36 struct timespec now;
37
38 if (fill_timespec(&now))
39 return 0;
40
41 sec = now.tv_sec - t->tv_sec;
42 nsec = now.tv_nsec - t->tv_nsec;
43 if (sec > 0 && nsec < 0) {
44 sec--;
45 nsec += 1000000000;
46 }
47
48 sec *= 1000000;
49 nsec /= 1000;
50 return sec + nsec;
51}
52
Jens Axboe7a16dd02006-10-18 17:21:58 +020053static int fio_posixaio_cancel(struct thread_data fio_unused *td,
54 struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020055{
Jens Axboe53cdc682006-10-18 11:50:58 +020056 struct fio_file *f = io_u->file;
57 int r = aio_cancel(f->fd, &io_u->aiocb);
Jens Axboe2866c822006-10-09 15:57:48 +020058
59 if (r == 1 || r == AIO_CANCELED)
60 return 0;
61
62 return 1;
63}
64
Jens Axboe7a16dd02006-10-18 17:21:58 +020065static int fio_posixaio_prep(struct thread_data fio_unused *td,
66 struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020067{
68 struct aiocb *aiocb = &io_u->aiocb;
Jens Axboe53cdc682006-10-18 11:50:58 +020069 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +020070
Jens Axboe53cdc682006-10-18 11:50:58 +020071 aiocb->aio_fildes = f->fd;
Jens Axboecec6b552007-02-06 20:15:38 +010072 aiocb->aio_buf = io_u->xfer_buf;
73 aiocb->aio_nbytes = io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020074 aiocb->aio_offset = io_u->offset;
75
76 io_u->seen = 0;
77 return 0;
78}
79
Jens Axboe3c770372008-03-10 18:45:57 +010080#define SUSPEND_ENTRIES 8
81
Jens Axboee7d2e612007-12-11 10:49:39 +010082static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
83 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +020084{
85 struct posixaio_data *pd = td->io_ops->data;
Jens Axboe3c770372008-03-10 18:45:57 +010086 struct aiocb *suspend_list[SUSPEND_ENTRIES];
Jens Axboe01743ee2008-06-02 12:19:19 +020087 struct flist_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020088 struct timespec start;
Jens Axboea3cc7702007-12-11 13:23:27 +010089 int have_timeout = 0;
Jens Axboe3c770372008-03-10 18:45:57 +010090 int suspend_entries = 0;
Jens Axboea3cc7702007-12-11 13:23:27 +010091 unsigned int r;
Jens Axboe2866c822006-10-09 15:57:48 +020092
93 if (t && !fill_timespec(&start))
94 have_timeout = 1;
95
96 r = 0;
Jens Axboe565cc352008-03-10 18:47:53 +010097 memset(suspend_list, 0, sizeof(*suspend_list));
Jens Axboe2866c822006-10-09 15:57:48 +020098restart:
Jens Axboe01743ee2008-06-02 12:19:19 +020099 flist_for_each(entry, &td->io_u_busylist) {
100 struct io_u *io_u = flist_entry(entry, struct io_u, list);
Jens Axboe2866c822006-10-09 15:57:48 +0200101 int err;
102
103 if (io_u->seen)
104 continue;
105
106 err = aio_error(&io_u->aiocb);
Jens Axboe3c770372008-03-10 18:45:57 +0100107 if (err == EINPROGRESS) {
108 if (suspend_entries < SUSPEND_ENTRIES) {
109 suspend_list[suspend_entries] = &io_u->aiocb;
110 suspend_entries++;
111 }
Jens Axboe3f344312007-03-14 14:14:48 +0100112 continue;
Jens Axboe3c770372008-03-10 18:45:57 +0100113 }
Jens Axboe3f344312007-03-14 14:14:48 +0100114
115 io_u->seen = 1;
Jens Axboe207cb0f2008-06-02 12:28:02 +0200116 pd->queued--;
Jens Axboe3f344312007-03-14 14:14:48 +0100117 pd->aio_events[r++] = io_u;
118
119 if (err == ECANCELED)
120 io_u->resid = io_u->xfer_buflen;
121 else if (!err) {
122 ssize_t retval = aio_return(&io_u->aiocb);
123
124 io_u->resid = io_u->xfer_buflen - retval;
125 } else
126 io_u->error = err;
Jens Axboe2866c822006-10-09 15:57:48 +0200127
128 if (r >= max)
129 break;
130 }
131
132 if (r >= min)
133 return r;
134
135 if (have_timeout) {
136 unsigned long long usec;
137
138 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
139 if (ts_utime_since_now(&start) > usec)
140 return r;
141 }
142
143 /*
Jens Axboe3c770372008-03-10 18:45:57 +0100144 * must have some in-flight, wait for at least one
Jens Axboe2866c822006-10-09 15:57:48 +0200145 */
Jens Axboe3c770372008-03-10 18:45:57 +0100146 aio_suspend((const struct aiocb * const *)suspend_list,
147 suspend_entries, t);
Jens Axboe2866c822006-10-09 15:57:48 +0200148 goto restart;
149}
150
151static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
152{
153 struct posixaio_data *pd = td->io_ops->data;
154
155 return pd->aio_events[event];
156}
157
158static int fio_posixaio_queue(struct thread_data fio_unused *td,
159 struct io_u *io_u)
160{
Jens Axboe207cb0f2008-06-02 12:28:02 +0200161 struct posixaio_data *pd = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200162 struct aiocb *aiocb = &io_u->aiocb;
163 int ret;
164
Jens Axboe7101d9c2007-09-12 13:12:39 +0200165 fio_ro_check(td, io_u);
166
Jens Axboe2866c822006-10-09 15:57:48 +0200167 if (io_u->ddir == DDIR_READ)
168 ret = aio_read(aiocb);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200169 else if (io_u->ddir == DDIR_WRITE)
Jens Axboe2866c822006-10-09 15:57:48 +0200170 ret = aio_write(aiocb);
Jens Axboe207cb0f2008-06-02 12:28:02 +0200171 else {
172#ifdef FIO_HAVE_POSIXAIO_FSYNC
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200173 ret = aio_fsync(O_SYNC, aiocb);
Jens Axboe207cb0f2008-06-02 12:28:02 +0200174#else
175 if (pd->queued)
176 return FIO_Q_BUSY;
Jens Axboe2866c822006-10-09 15:57:48 +0200177
Jens Axboe207cb0f2008-06-02 12:28:02 +0200178 if (fsync(io_u->file->fd) < 0)
179 io_u->error = errno;
180
181 return FIO_Q_COMPLETED;
182#endif
183 }
184
Jens Axboe95bcd812007-02-11 01:01:57 +0100185 if (ret) {
Jens Axboe2866c822006-10-09 15:57:48 +0200186 io_u->error = errno;
Jens Axboee1161c32007-02-22 19:36:48 +0100187 td_verror(td, io_u->error, "xfer");
Jens Axboe36167d82007-02-18 05:41:31 +0100188 return FIO_Q_COMPLETED;
Jens Axboe95bcd812007-02-11 01:01:57 +0100189 }
Jens Axboe36167d82007-02-18 05:41:31 +0100190
Jens Axboe207cb0f2008-06-02 12:28:02 +0200191 pd->queued++;
Jens Axboe36167d82007-02-18 05:41:31 +0100192 return FIO_Q_QUEUED;
Jens Axboe2866c822006-10-09 15:57:48 +0200193}
194
195static void fio_posixaio_cleanup(struct thread_data *td)
196{
197 struct posixaio_data *pd = td->io_ops->data;
198
199 if (pd) {
200 free(pd->aio_events);
201 free(pd);
Jens Axboe2866c822006-10-09 15:57:48 +0200202 }
203}
204
205static int fio_posixaio_init(struct thread_data *td)
206{
207 struct posixaio_data *pd = malloc(sizeof(*pd));
208
Jens Axboecb781c72006-11-07 14:02:48 +0100209 memset(pd, 0, sizeof(*pd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100210 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
211 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe2866c822006-10-09 15:57:48 +0200212
213 td->io_ops->data = pd;
214 return 0;
215}
216
Jens Axboe5f350952006-11-07 15:20:59 +0100217static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200218 .name = "posixaio",
219 .version = FIO_IOOPS_VERSION,
220 .init = fio_posixaio_init,
221 .prep = fio_posixaio_prep,
222 .queue = fio_posixaio_queue,
223 .cancel = fio_posixaio_cancel,
224 .getevents = fio_posixaio_getevents,
225 .event = fio_posixaio_event,
226 .cleanup = fio_posixaio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100227 .open_file = generic_open_file,
228 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100229 .get_file_size = generic_get_file_size,
Jens Axboe2866c822006-10-09 15:57:48 +0200230};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100231
232#else /* FIO_HAVE_POSIXAIO */
233
234/*
235 * When we have a proper configure system in place, we simply wont build
236 * and install this io engine. For now install a crippled version that
237 * just complains and fails to load.
238 */
239static int fio_posixaio_init(struct thread_data fio_unused *td)
240{
241 fprintf(stderr, "fio: posixaio not available\n");
242 return 1;
243}
244
Jens Axboe5f350952006-11-07 15:20:59 +0100245static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100246 .name = "posixaio",
247 .version = FIO_IOOPS_VERSION,
248 .init = fio_posixaio_init,
249};
250
251#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100252
253static void fio_init fio_posixaio_register(void)
254{
255 register_ioengine(&ioengine);
256}
257
258static void fio_exit fio_posixaio_unregister(void)
259{
260 unregister_ioengine(&ioengine);
261}