blob: ddc75d1cfe54802c8c0ac09349930ed852e5a52a [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>
11#include <assert.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;
19};
20
21static int fill_timespec(struct timespec *ts)
22{
23#ifdef _POSIX_TIMERS
24 if (!clock_gettime(CLOCK_MONOTONIC, ts))
25 return 0;
26
27 perror("clock_gettime");
28#endif
29 return 1;
30}
31
32static unsigned long long ts_utime_since_now(struct timespec *t)
33{
34 long long sec, nsec;
35 struct timespec now;
36
37 if (fill_timespec(&now))
38 return 0;
39
40 sec = now.tv_sec - t->tv_sec;
41 nsec = now.tv_nsec - t->tv_nsec;
42 if (sec > 0 && nsec < 0) {
43 sec--;
44 nsec += 1000000000;
45 }
46
47 sec *= 1000000;
48 nsec /= 1000;
49 return sec + nsec;
50}
51
Jens Axboe7a16dd02006-10-18 17:21:58 +020052static int fio_posixaio_cancel(struct thread_data fio_unused *td,
53 struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020054{
Jens Axboe53cdc682006-10-18 11:50:58 +020055 struct fio_file *f = io_u->file;
56 int r = aio_cancel(f->fd, &io_u->aiocb);
Jens Axboe2866c822006-10-09 15:57:48 +020057
58 if (r == 1 || r == AIO_CANCELED)
59 return 0;
60
61 return 1;
62}
63
Jens Axboe7a16dd02006-10-18 17:21:58 +020064static int fio_posixaio_prep(struct thread_data fio_unused *td,
65 struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020066{
67 struct aiocb *aiocb = &io_u->aiocb;
Jens Axboe53cdc682006-10-18 11:50:58 +020068 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +020069
Jens Axboe53cdc682006-10-18 11:50:58 +020070 aiocb->aio_fildes = f->fd;
Jens Axboecec6b552007-02-06 20:15:38 +010071 aiocb->aio_buf = io_u->xfer_buf;
72 aiocb->aio_nbytes = io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020073 aiocb->aio_offset = io_u->offset;
74
75 io_u->seen = 0;
76 return 0;
77}
78
Jens Axboe3c770372008-03-10 18:45:57 +010079#define SUSPEND_ENTRIES 8
80
Jens Axboee7d2e612007-12-11 10:49:39 +010081static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
82 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +020083{
84 struct posixaio_data *pd = td->io_ops->data;
Jens Axboe3c770372008-03-10 18:45:57 +010085 struct aiocb *suspend_list[SUSPEND_ENTRIES];
Jens Axboe2866c822006-10-09 15:57:48 +020086 struct list_head *entry;
87 struct timespec start;
Jens Axboea3cc7702007-12-11 13:23:27 +010088 int have_timeout = 0;
Jens Axboe3c770372008-03-10 18:45:57 +010089 int suspend_entries = 0;
Jens Axboea3cc7702007-12-11 13:23:27 +010090 unsigned int r;
Jens Axboe2866c822006-10-09 15:57:48 +020091
92 if (t && !fill_timespec(&start))
93 have_timeout = 1;
94
95 r = 0;
Jens Axboe565cc352008-03-10 18:47:53 +010096 memset(suspend_list, 0, sizeof(*suspend_list));
Jens Axboe2866c822006-10-09 15:57:48 +020097restart:
98 list_for_each(entry, &td->io_u_busylist) {
99 struct io_u *io_u = list_entry(entry, struct io_u, list);
100 int err;
101
102 if (io_u->seen)
103 continue;
104
105 err = aio_error(&io_u->aiocb);
Jens Axboe3c770372008-03-10 18:45:57 +0100106 if (err == EINPROGRESS) {
107 if (suspend_entries < SUSPEND_ENTRIES) {
108 suspend_list[suspend_entries] = &io_u->aiocb;
109 suspend_entries++;
110 }
Jens Axboe3f344312007-03-14 14:14:48 +0100111 continue;
Jens Axboe3c770372008-03-10 18:45:57 +0100112 }
Jens Axboe3f344312007-03-14 14:14:48 +0100113
114 io_u->seen = 1;
115 pd->aio_events[r++] = io_u;
116
117 if (err == ECANCELED)
118 io_u->resid = io_u->xfer_buflen;
119 else if (!err) {
120 ssize_t retval = aio_return(&io_u->aiocb);
121
122 io_u->resid = io_u->xfer_buflen - retval;
123 } else
124 io_u->error = err;
Jens Axboe2866c822006-10-09 15:57:48 +0200125
126 if (r >= max)
127 break;
128 }
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 Axboe3c770372008-03-10 18:45:57 +0100144 aio_suspend((const struct aiocb * const *)suspend_list,
145 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
156static int fio_posixaio_queue(struct thread_data fio_unused *td,
157 struct io_u *io_u)
158{
159 struct aiocb *aiocb = &io_u->aiocb;
160 int ret;
161
Jens Axboe7101d9c2007-09-12 13:12:39 +0200162 fio_ro_check(td, io_u);
163
Jens Axboe2866c822006-10-09 15:57:48 +0200164 if (io_u->ddir == DDIR_READ)
165 ret = aio_read(aiocb);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200166 else if (io_u->ddir == DDIR_WRITE)
Jens Axboe2866c822006-10-09 15:57:48 +0200167 ret = aio_write(aiocb);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200168 else
169 ret = aio_fsync(O_SYNC, aiocb);
Jens Axboe2866c822006-10-09 15:57:48 +0200170
Jens Axboe95bcd812007-02-11 01:01:57 +0100171 if (ret) {
Jens Axboe2866c822006-10-09 15:57:48 +0200172 io_u->error = errno;
Jens Axboee1161c32007-02-22 19:36:48 +0100173 td_verror(td, io_u->error, "xfer");
Jens Axboe36167d82007-02-18 05:41:31 +0100174 return FIO_Q_COMPLETED;
Jens Axboe95bcd812007-02-11 01:01:57 +0100175 }
Jens Axboe36167d82007-02-18 05:41:31 +0100176
177 return FIO_Q_QUEUED;
Jens Axboe2866c822006-10-09 15:57:48 +0200178}
179
180static void fio_posixaio_cleanup(struct thread_data *td)
181{
182 struct posixaio_data *pd = td->io_ops->data;
183
184 if (pd) {
185 free(pd->aio_events);
186 free(pd);
187 td->io_ops->data = NULL;
188 }
189}
190
191static int fio_posixaio_init(struct thread_data *td)
192{
193 struct posixaio_data *pd = malloc(sizeof(*pd));
194
Jens Axboecb781c72006-11-07 14:02:48 +0100195 memset(pd, 0, sizeof(*pd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100196 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
197 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe2866c822006-10-09 15:57:48 +0200198
199 td->io_ops->data = pd;
200 return 0;
201}
202
Jens Axboe5f350952006-11-07 15:20:59 +0100203static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200204 .name = "posixaio",
205 .version = FIO_IOOPS_VERSION,
206 .init = fio_posixaio_init,
207 .prep = fio_posixaio_prep,
208 .queue = fio_posixaio_queue,
209 .cancel = fio_posixaio_cancel,
210 .getevents = fio_posixaio_getevents,
211 .event = fio_posixaio_event,
212 .cleanup = fio_posixaio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100213 .open_file = generic_open_file,
214 .close_file = generic_close_file,
Jens Axboe2866c822006-10-09 15:57:48 +0200215};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100216
217#else /* FIO_HAVE_POSIXAIO */
218
219/*
220 * When we have a proper configure system in place, we simply wont build
221 * and install this io engine. For now install a crippled version that
222 * just complains and fails to load.
223 */
224static int fio_posixaio_init(struct thread_data fio_unused *td)
225{
226 fprintf(stderr, "fio: posixaio not available\n");
227 return 1;
228}
229
Jens Axboe5f350952006-11-07 15:20:59 +0100230static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100231 .name = "posixaio",
232 .version = FIO_IOOPS_VERSION,
233 .init = fio_posixaio_init,
234};
235
236#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100237
238static void fio_init fio_posixaio_register(void)
239{
240 register_ioengine(&ioengine);
241}
242
243static void fio_exit fio_posixaio_unregister(void)
244{
245 unregister_ioengine(&ioengine);
246}