blob: a943e5bbf6eaad2c953d21f5c9abd431690fdabf [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
15struct posixaio_data {
16 struct io_u **aio_events;
Jens Axboe207cb0f2008-06-02 12:28:02 +020017 unsigned int queued;
Jens Axboe2866c822006-10-09 15:57:48 +020018};
19
20static int fill_timespec(struct timespec *ts)
21{
Jens Axboe5351f562013-01-23 14:02:23 -070022#ifdef CONFIG_CLOCK_GETTIME
23#ifdef CONFIG_CLOCK_MONOTONIC
24 clockid_t clk = CLOCK_MONOTONIC;
25#else
26 clockid_t clk = CLOCK_REALTIME;
27#endif
28 if (!clock_gettime(clk, ts))
Jens Axboe2866c822006-10-09 15:57:48 +020029 return 0;
30
31 perror("clock_gettime");
Jens Axboe2866c822006-10-09 15:57:48 +020032 return 1;
Jens Axboe5351f562013-01-23 14:02:23 -070033#else
34 struct timeval tv;
35
36 gettimeofday(&tv, NULL);
37 ts->tv_sec = tv.tv_sec;
38 ts->tv_nsec = tv.tv_usec * 1000;
39 return 0;
40#endif
Jens Axboe2866c822006-10-09 15:57:48 +020041}
42
43static unsigned long long ts_utime_since_now(struct timespec *t)
44{
45 long long sec, nsec;
46 struct timespec now;
47
48 if (fill_timespec(&now))
49 return 0;
50
51 sec = now.tv_sec - t->tv_sec;
52 nsec = now.tv_nsec - t->tv_nsec;
53 if (sec > 0 && nsec < 0) {
54 sec--;
55 nsec += 1000000000;
56 }
57
58 sec *= 1000000;
59 nsec /= 1000;
60 return sec + nsec;
61}
62
Jens Axboe7a16dd02006-10-18 17:21:58 +020063static int fio_posixaio_cancel(struct thread_data fio_unused *td,
64 struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020065{
Jens Axboe53cdc682006-10-18 11:50:58 +020066 struct fio_file *f = io_u->file;
67 int r = aio_cancel(f->fd, &io_u->aiocb);
Jens Axboe2866c822006-10-09 15:57:48 +020068
YAMAMOTO Takashi2faf9ec2010-06-11 09:21:33 +020069 if (r == AIO_ALLDONE || r == AIO_CANCELED)
Jens Axboe2866c822006-10-09 15:57:48 +020070 return 0;
71
72 return 1;
73}
74
Jens Axboe7a16dd02006-10-18 17:21:58 +020075static int fio_posixaio_prep(struct thread_data fio_unused *td,
76 struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020077{
Jens Axboee97c1442011-09-21 09:38:01 +020078 os_aiocb_t *aiocb = &io_u->aiocb;
Jens Axboe53cdc682006-10-18 11:50:58 +020079 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +020080
Jens Axboe53cdc682006-10-18 11:50:58 +020081 aiocb->aio_fildes = f->fd;
Jens Axboecec6b552007-02-06 20:15:38 +010082 aiocb->aio_buf = io_u->xfer_buf;
83 aiocb->aio_nbytes = io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020084 aiocb->aio_offset = io_u->offset;
Jens Axboe9918be52011-07-09 13:33:38 +020085 aiocb->aio_sigevent.sigev_notify = SIGEV_NONE;
Jens Axboe2866c822006-10-09 15:57:48 +020086
87 io_u->seen = 0;
88 return 0;
89}
90
Jens Axboe3c770372008-03-10 18:45:57 +010091#define SUSPEND_ENTRIES 8
92
Jens Axboee7d2e612007-12-11 10:49:39 +010093static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
94 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +020095{
96 struct posixaio_data *pd = td->io_ops->data;
Jens Axboee97c1442011-09-21 09:38:01 +020097 os_aiocb_t *suspend_list[SUSPEND_ENTRIES];
Jens Axboe01743ee2008-06-02 12:19:19 +020098 struct flist_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020099 struct timespec start;
Jens Axboea3cc7702007-12-11 13:23:27 +0100100 int have_timeout = 0;
Jens Axboe3c770372008-03-10 18:45:57 +0100101 int suspend_entries = 0;
Jens Axboea3cc7702007-12-11 13:23:27 +0100102 unsigned int r;
Jens Axboe2866c822006-10-09 15:57:48 +0200103
104 if (t && !fill_timespec(&start))
105 have_timeout = 1;
Jens Axboe5351f562013-01-23 14:02:23 -0700106 else
107 memset(&start, 0, sizeof(start));
Jens Axboe2866c822006-10-09 15:57:48 +0200108
109 r = 0;
Jens Axboe565cc352008-03-10 18:47:53 +0100110 memset(suspend_list, 0, sizeof(*suspend_list));
Jens Axboe2866c822006-10-09 15:57:48 +0200111restart:
Jens Axboe01743ee2008-06-02 12:19:19 +0200112 flist_for_each(entry, &td->io_u_busylist) {
113 struct io_u *io_u = flist_entry(entry, struct io_u, list);
Jens Axboe2866c822006-10-09 15:57:48 +0200114 int err;
115
116 if (io_u->seen)
117 continue;
118
119 err = aio_error(&io_u->aiocb);
Jens Axboe3c770372008-03-10 18:45:57 +0100120 if (err == EINPROGRESS) {
121 if (suspend_entries < SUSPEND_ENTRIES) {
122 suspend_list[suspend_entries] = &io_u->aiocb;
123 suspend_entries++;
124 }
Jens Axboe3f344312007-03-14 14:14:48 +0100125 continue;
Jens Axboe3c770372008-03-10 18:45:57 +0100126 }
Jens Axboe3f344312007-03-14 14:14:48 +0100127
128 io_u->seen = 1;
Jens Axboe207cb0f2008-06-02 12:28:02 +0200129 pd->queued--;
Jens Axboe3f344312007-03-14 14:14:48 +0100130 pd->aio_events[r++] = io_u;
131
132 if (err == ECANCELED)
133 io_u->resid = io_u->xfer_buflen;
134 else if (!err) {
135 ssize_t retval = aio_return(&io_u->aiocb);
136
137 io_u->resid = io_u->xfer_buflen - retval;
138 } else
139 io_u->error = err;
Jens Axboe2866c822006-10-09 15:57:48 +0200140 }
141
142 if (r >= min)
143 return r;
144
145 if (have_timeout) {
146 unsigned long long usec;
147
148 usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000);
149 if (ts_utime_since_now(&start) > usec)
150 return r;
151 }
152
153 /*
Jens Axboe3c770372008-03-10 18:45:57 +0100154 * must have some in-flight, wait for at least one
Jens Axboe2866c822006-10-09 15:57:48 +0200155 */
Jens Axboee97c1442011-09-21 09:38:01 +0200156 aio_suspend((const os_aiocb_t * const *)suspend_list,
Jens Axboe3c770372008-03-10 18:45:57 +0100157 suspend_entries, t);
Jens Axboe2866c822006-10-09 15:57:48 +0200158 goto restart;
159}
160
161static struct io_u *fio_posixaio_event(struct thread_data *td, int event)
162{
163 struct posixaio_data *pd = td->io_ops->data;
164
165 return pd->aio_events[event];
166}
167
Bruce Cran03e20d62011-01-02 20:14:54 +0100168static int fio_posixaio_queue(struct thread_data *td,
Jens Axboe2866c822006-10-09 15:57:48 +0200169 struct io_u *io_u)
170{
Jens Axboe207cb0f2008-06-02 12:28:02 +0200171 struct posixaio_data *pd = td->io_ops->data;
Jens Axboee97c1442011-09-21 09:38:01 +0200172 os_aiocb_t *aiocb = &io_u->aiocb;
Jens Axboe2866c822006-10-09 15:57:48 +0200173 int ret;
174
Jens Axboe7101d9c2007-09-12 13:12:39 +0200175 fio_ro_check(td, io_u);
176
Jens Axboe2866c822006-10-09 15:57:48 +0200177 if (io_u->ddir == DDIR_READ)
178 ret = aio_read(aiocb);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200179 else if (io_u->ddir == DDIR_WRITE)
Jens Axboe2866c822006-10-09 15:57:48 +0200180 ret = aio_write(aiocb);
Jens Axboea5f30272010-07-19 16:19:55 -0600181 else if (io_u->ddir == DDIR_TRIM) {
182 if (pd->queued)
183 return FIO_Q_BUSY;
184
185 do_io_u_trim(td, io_u);
186 return FIO_Q_COMPLETED;
187 } else {
Jens Axboe67bf9822013-01-10 11:23:19 +0100188#ifdef CONFIG_POSIXAIO_FSYNC
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200189 ret = aio_fsync(O_SYNC, aiocb);
Jens Axboe207cb0f2008-06-02 12:28:02 +0200190#else
191 if (pd->queued)
192 return FIO_Q_BUSY;
Jens Axboe2866c822006-10-09 15:57:48 +0200193
Jens Axboef0115312010-03-09 21:47:15 +0100194 do_io_u_sync(td, io_u);
Jens Axboe207cb0f2008-06-02 12:28:02 +0200195 return FIO_Q_COMPLETED;
196#endif
197 }
198
Jens Axboe95bcd812007-02-11 01:01:57 +0100199 if (ret) {
Jens Axboeafa16402011-07-07 21:06:40 +0200200 /*
201 * At least OSX has a very low limit on the number of pending
Jens Axboedef1d8e2011-07-08 08:33:37 +0200202 * 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.
Jens Axboeafa16402011-07-07 21:06:40 +0200205 */
206 if (errno == EAGAIN)
207 return FIO_Q_BUSY;
208
Jens Axboe2866c822006-10-09 15:57:48 +0200209 io_u->error = errno;
Jens Axboee1161c32007-02-22 19:36:48 +0100210 td_verror(td, io_u->error, "xfer");
Jens Axboe36167d82007-02-18 05:41:31 +0100211 return FIO_Q_COMPLETED;
Jens Axboe95bcd812007-02-11 01:01:57 +0100212 }
Jens Axboe36167d82007-02-18 05:41:31 +0100213
Jens Axboe207cb0f2008-06-02 12:28:02 +0200214 pd->queued++;
Jens Axboe36167d82007-02-18 05:41:31 +0100215 return FIO_Q_QUEUED;
Jens Axboe2866c822006-10-09 15:57:48 +0200216}
217
218static void fio_posixaio_cleanup(struct thread_data *td)
219{
220 struct posixaio_data *pd = td->io_ops->data;
221
222 if (pd) {
223 free(pd->aio_events);
224 free(pd);
Jens Axboe2866c822006-10-09 15:57:48 +0200225 }
226}
227
228static int fio_posixaio_init(struct thread_data *td)
229{
230 struct posixaio_data *pd = malloc(sizeof(*pd));
231
Jens Axboecb781c72006-11-07 14:02:48 +0100232 memset(pd, 0, sizeof(*pd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100233 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
234 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe2866c822006-10-09 15:57:48 +0200235
236 td->io_ops->data = pd;
237 return 0;
238}
239
Jens Axboe5f350952006-11-07 15:20:59 +0100240static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200241 .name = "posixaio",
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,
Jens Axboeb5af8292007-03-08 12:43:13 +0100250 .open_file = generic_open_file,
251 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100252 .get_file_size = generic_get_file_size,
Jens Axboe2866c822006-10-09 15:57:48 +0200253};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100254
Jens Axboe5f350952006-11-07 15:20:59 +0100255static void fio_init fio_posixaio_register(void)
256{
257 register_ioengine(&ioengine);
258}
259
260static void fio_exit fio_posixaio_unregister(void)
261{
262 unregister_ioengine(&ioengine);
263}