blob: 0966e0d5e7fa66b261c20bb1948177e108ab4455 [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
YAMAMOTO Takashi2faf9ec2010-06-11 09:21:33 +020059 if (r == AIO_ALLDONE || r == AIO_CANCELED)
Jens Axboe2866c822006-10-09 15:57:48 +020060 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{
Jens Axboee97c1442011-09-21 09:38:01 +020068 os_aiocb_t *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;
Jens Axboe9918be52011-07-09 13:33:38 +020075 aiocb->aio_sigevent.sigev_notify = SIGEV_NONE;
Jens Axboe2866c822006-10-09 15:57:48 +020076
77 io_u->seen = 0;
78 return 0;
79}
80
Jens Axboe3c770372008-03-10 18:45:57 +010081#define SUSPEND_ENTRIES 8
82
Jens Axboee7d2e612007-12-11 10:49:39 +010083static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
84 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +020085{
86 struct posixaio_data *pd = td->io_ops->data;
Jens Axboee97c1442011-09-21 09:38:01 +020087 os_aiocb_t *suspend_list[SUSPEND_ENTRIES];
Jens Axboe01743ee2008-06-02 12:19:19 +020088 struct flist_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020089 struct timespec start;
Jens Axboea3cc7702007-12-11 13:23:27 +010090 int have_timeout = 0;
Jens Axboe3c770372008-03-10 18:45:57 +010091 int suspend_entries = 0;
Jens Axboea3cc7702007-12-11 13:23:27 +010092 unsigned int r;
Jens Axboe2866c822006-10-09 15:57:48 +020093
94 if (t && !fill_timespec(&start))
95 have_timeout = 1;
96
97 r = 0;
Jens Axboe565cc352008-03-10 18:47:53 +010098 memset(suspend_list, 0, sizeof(*suspend_list));
Jens Axboe2866c822006-10-09 15:57:48 +020099restart:
Jens Axboe01743ee2008-06-02 12:19:19 +0200100 flist_for_each(entry, &td->io_u_busylist) {
101 struct io_u *io_u = flist_entry(entry, struct io_u, list);
Jens Axboe2866c822006-10-09 15:57:48 +0200102 int err;
103
104 if (io_u->seen)
105 continue;
106
107 err = aio_error(&io_u->aiocb);
Jens Axboe3c770372008-03-10 18:45:57 +0100108 if (err == EINPROGRESS) {
109 if (suspend_entries < SUSPEND_ENTRIES) {
110 suspend_list[suspend_entries] = &io_u->aiocb;
111 suspend_entries++;
112 }
Jens Axboe3f344312007-03-14 14:14:48 +0100113 continue;
Jens Axboe3c770372008-03-10 18:45:57 +0100114 }
Jens Axboe3f344312007-03-14 14:14:48 +0100115
116 io_u->seen = 1;
Jens Axboe207cb0f2008-06-02 12:28:02 +0200117 pd->queued--;
Jens Axboe3f344312007-03-14 14:14:48 +0100118 pd->aio_events[r++] = io_u;
119
120 if (err == ECANCELED)
121 io_u->resid = io_u->xfer_buflen;
122 else if (!err) {
123 ssize_t retval = aio_return(&io_u->aiocb);
124
125 io_u->resid = io_u->xfer_buflen - retval;
126 } else
127 io_u->error = err;
Jens Axboe2866c822006-10-09 15:57:48 +0200128 }
129
130 if (r >= min)
131 return r;
132
133 if (have_timeout) {
134 unsigned long long usec;
135
136 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
137 if (ts_utime_since_now(&start) > usec)
138 return r;
139 }
140
141 /*
Jens Axboe3c770372008-03-10 18:45:57 +0100142 * must have some in-flight, wait for at least one
Jens Axboe2866c822006-10-09 15:57:48 +0200143 */
Jens Axboee97c1442011-09-21 09:38:01 +0200144 aio_suspend((const os_aiocb_t * const *)suspend_list,
Jens Axboe3c770372008-03-10 18:45:57 +0100145 suspend_entries, t);
Jens Axboe2866c822006-10-09 15:57:48 +0200146 goto restart;
147}
148
149static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
150{
151 struct posixaio_data *pd = td->io_ops->data;
152
153 return pd->aio_events[event];
154}
155
Bruce Cran03e20d62011-01-02 20:14:54 +0100156static int fio_posixaio_queue(struct thread_data *td,
Jens Axboe2866c822006-10-09 15:57:48 +0200157 struct io_u *io_u)
158{
Jens Axboe207cb0f2008-06-02 12:28:02 +0200159 struct posixaio_data *pd = td->io_ops->data;
Jens Axboee97c1442011-09-21 09:38:01 +0200160 os_aiocb_t *aiocb = &io_u->aiocb;
Jens Axboe2866c822006-10-09 15:57:48 +0200161 int ret;
162
Jens Axboe7101d9c2007-09-12 13:12:39 +0200163 fio_ro_check(td, io_u);
164
Jens Axboe2866c822006-10-09 15:57:48 +0200165 if (io_u->ddir == DDIR_READ)
166 ret = aio_read(aiocb);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200167 else if (io_u->ddir == DDIR_WRITE)
Jens Axboe2866c822006-10-09 15:57:48 +0200168 ret = aio_write(aiocb);
Jens Axboea5f30272010-07-19 16:19:55 -0600169 else if (io_u->ddir == DDIR_TRIM) {
170 if (pd->queued)
171 return FIO_Q_BUSY;
172
173 do_io_u_trim(td, io_u);
174 return FIO_Q_COMPLETED;
175 } else {
Jens Axboe207cb0f2008-06-02 12:28:02 +0200176#ifdef FIO_HAVE_POSIXAIO_FSYNC
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200177 ret = aio_fsync(O_SYNC, aiocb);
Jens Axboe207cb0f2008-06-02 12:28:02 +0200178#else
179 if (pd->queued)
180 return FIO_Q_BUSY;
Jens Axboe2866c822006-10-09 15:57:48 +0200181
Jens Axboef0115312010-03-09 21:47:15 +0100182 do_io_u_sync(td, io_u);
Jens Axboe207cb0f2008-06-02 12:28:02 +0200183 return FIO_Q_COMPLETED;
184#endif
185 }
186
Jens Axboe95bcd812007-02-11 01:01:57 +0100187 if (ret) {
Jens Axboeafa16402011-07-07 21:06:40 +0200188 /*
189 * At least OSX has a very low limit on the number of pending
Jens Axboedef1d8e2011-07-08 08:33:37 +0200190 * IOs, so if it returns EAGAIN, we are out of resources
191 * to queue more. Just return FIO_Q_BUSY to naturally
192 * drop off at this depth.
Jens Axboeafa16402011-07-07 21:06:40 +0200193 */
194 if (errno == EAGAIN)
195 return FIO_Q_BUSY;
196
Jens Axboe2866c822006-10-09 15:57:48 +0200197 io_u->error = errno;
Jens Axboee1161c32007-02-22 19:36:48 +0100198 td_verror(td, io_u->error, "xfer");
Jens Axboe36167d82007-02-18 05:41:31 +0100199 return FIO_Q_COMPLETED;
Jens Axboe95bcd812007-02-11 01:01:57 +0100200 }
Jens Axboe36167d82007-02-18 05:41:31 +0100201
Jens Axboe207cb0f2008-06-02 12:28:02 +0200202 pd->queued++;
Jens Axboe36167d82007-02-18 05:41:31 +0100203 return FIO_Q_QUEUED;
Jens Axboe2866c822006-10-09 15:57:48 +0200204}
205
206static void fio_posixaio_cleanup(struct thread_data *td)
207{
208 struct posixaio_data *pd = td->io_ops->data;
209
210 if (pd) {
211 free(pd->aio_events);
212 free(pd);
Jens Axboe2866c822006-10-09 15:57:48 +0200213 }
214}
215
216static int fio_posixaio_init(struct thread_data *td)
217{
218 struct posixaio_data *pd = malloc(sizeof(*pd));
219
Jens Axboecb781c72006-11-07 14:02:48 +0100220 memset(pd, 0, sizeof(*pd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100221 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
222 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe2866c822006-10-09 15:57:48 +0200223
224 td->io_ops->data = pd;
225 return 0;
226}
227
Jens Axboe5f350952006-11-07 15:20:59 +0100228static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200229 .name = "posixaio",
230 .version = FIO_IOOPS_VERSION,
231 .init = fio_posixaio_init,
232 .prep = fio_posixaio_prep,
233 .queue = fio_posixaio_queue,
234 .cancel = fio_posixaio_cancel,
235 .getevents = fio_posixaio_getevents,
236 .event = fio_posixaio_event,
237 .cleanup = fio_posixaio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100238 .open_file = generic_open_file,
239 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100240 .get_file_size = generic_get_file_size,
Jens Axboe2866c822006-10-09 15:57:48 +0200241};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100242
243#else /* FIO_HAVE_POSIXAIO */
244
245/*
246 * When we have a proper configure system in place, we simply wont build
247 * and install this io engine. For now install a crippled version that
248 * just complains and fails to load.
249 */
250static int fio_posixaio_init(struct thread_data fio_unused *td)
251{
Jens Axboea3edaf72010-09-26 10:53:40 +0900252 log_err("fio: posixaio not available\n");
Jens Axboe34cfcda2006-11-03 14:00:45 +0100253 return 1;
254}
255
Jens Axboe5f350952006-11-07 15:20:59 +0100256static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100257 .name = "posixaio",
258 .version = FIO_IOOPS_VERSION,
259 .init = fio_posixaio_init,
260};
261
262#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100263
264static void fio_init fio_posixaio_register(void)
265{
266 register_ioengine(&ioengine);
267}
268
269static void fio_exit fio_posixaio_unregister(void)
270{
271 unregister_ioengine(&ioengine);
272}