Fix aiocb compile warnings on HPUX
[fio.git] / engines / posixaio.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * posixaio engine
3 *
4 * IO engine that uses the posix defined aio interface.
2866c82d
JA
5 *
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
bc733f3b 11#include <fcntl.h>
5f350952
JA
12
13#include "../fio.h"
2866c82d 14
34cfcdaf
JA
15#ifdef FIO_HAVE_POSIXAIO
16
2866c82d
JA
17struct posixaio_data {
18 struct io_u **aio_events;
207cb0f0 19 unsigned int queued;
2866c82d
JA
20};
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
7a16dd02
JA
53static int fio_posixaio_cancel(struct thread_data fio_unused *td,
54 struct io_u *io_u)
2866c82d 55{
53cdc686
JA
56 struct fio_file *f = io_u->file;
57 int r = aio_cancel(f->fd, &io_u->aiocb);
2866c82d 58
2faf9ec8 59 if (r == AIO_ALLDONE || r == AIO_CANCELED)
2866c82d
JA
60 return 0;
61
62 return 1;
63}
64
7a16dd02
JA
65static int fio_posixaio_prep(struct thread_data fio_unused *td,
66 struct io_u *io_u)
2866c82d 67{
e97c1442 68 os_aiocb_t *aiocb = &io_u->aiocb;
53cdc686 69 struct fio_file *f = io_u->file;
2866c82d 70
53cdc686 71 aiocb->aio_fildes = f->fd;
cec6b55d
JA
72 aiocb->aio_buf = io_u->xfer_buf;
73 aiocb->aio_nbytes = io_u->xfer_buflen;
2866c82d 74 aiocb->aio_offset = io_u->offset;
9918be5a 75 aiocb->aio_sigevent.sigev_notify = SIGEV_NONE;
2866c82d
JA
76
77 io_u->seen = 0;
78 return 0;
79}
80
3c77037e
JA
81#define SUSPEND_ENTRIES 8
82
e7d2e616
JA
83static int fio_posixaio_getevents(struct thread_data *td, unsigned int min,
84 unsigned int max, struct timespec *t)
2866c82d
JA
85{
86 struct posixaio_data *pd = td->io_ops->data;
e97c1442 87 os_aiocb_t *suspend_list[SUSPEND_ENTRIES];
01743ee1 88 struct flist_head *entry;
2866c82d 89 struct timespec start;
a3cc770d 90 int have_timeout = 0;
3c77037e 91 int suspend_entries = 0;
a3cc770d 92 unsigned int r;
2866c82d
JA
93
94 if (t && !fill_timespec(&start))
95 have_timeout = 1;
96
97 r = 0;
565cc35f 98 memset(suspend_list, 0, sizeof(*suspend_list));
2866c82d 99restart:
01743ee1
JA
100 flist_for_each(entry, &td->io_u_busylist) {
101 struct io_u *io_u = flist_entry(entry, struct io_u, list);
2866c82d
JA
102 int err;
103
104 if (io_u->seen)
105 continue;
106
107 err = aio_error(&io_u->aiocb);
3c77037e
JA
108 if (err == EINPROGRESS) {
109 if (suspend_entries < SUSPEND_ENTRIES) {
110 suspend_list[suspend_entries] = &io_u->aiocb;
111 suspend_entries++;
112 }
3f344316 113 continue;
3c77037e 114 }
3f344316
JA
115
116 io_u->seen = 1;
207cb0f0 117 pd->queued--;
3f344316
JA
118 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;
2866c82d
JA
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 /*
3c77037e 142 * must have some in-flight, wait for at least one
2866c82d 143 */
e97c1442 144 aio_suspend((const os_aiocb_t * const *)suspend_list,
3c77037e 145 suspend_entries, t);
2866c82d
JA
146 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
03e20d68 156static int fio_posixaio_queue(struct thread_data *td,
2866c82d
JA
157 struct io_u *io_u)
158{
207cb0f0 159 struct posixaio_data *pd = td->io_ops->data;
e97c1442 160 os_aiocb_t *aiocb = &io_u->aiocb;
2866c82d
JA
161 int ret;
162
7101d9c2
JA
163 fio_ro_check(td, io_u);
164
2866c82d
JA
165 if (io_u->ddir == DDIR_READ)
166 ret = aio_read(aiocb);
87dc1ab1 167 else if (io_u->ddir == DDIR_WRITE)
2866c82d 168 ret = aio_write(aiocb);
a5f3027c
JA
169 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 {
207cb0f0 176#ifdef FIO_HAVE_POSIXAIO_FSYNC
87dc1ab1 177 ret = aio_fsync(O_SYNC, aiocb);
207cb0f0
JA
178#else
179 if (pd->queued)
180 return FIO_Q_BUSY;
181
f011531e 182 do_io_u_sync(td, io_u);
207cb0f0
JA
183 return FIO_Q_COMPLETED;
184#endif
185 }
186
95bcd815 187 if (ret) {
afa16407
JA
188 /*
189 * At least OSX has a very low limit on the number of pending
def1d8e3
JA
190 * 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.
afa16407
JA
193 */
194 if (errno == EAGAIN)
195 return FIO_Q_BUSY;
196
2866c82d 197 io_u->error = errno;
e1161c32 198 td_verror(td, io_u->error, "xfer");
36167d82 199 return FIO_Q_COMPLETED;
95bcd815 200 }
36167d82 201
207cb0f0 202 pd->queued++;
36167d82 203 return FIO_Q_QUEUED;
2866c82d
JA
204}
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);
2866c82d
JA
213 }
214}
215
216static int fio_posixaio_init(struct thread_data *td)
217{
218 struct posixaio_data *pd = malloc(sizeof(*pd));
219
cb781c75 220 memset(pd, 0, sizeof(*pd));
2dc1bbeb
JA
221 pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
222 memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
2866c82d
JA
223
224 td->io_ops->data = pd;
225 return 0;
226}
227
5f350952 228static struct ioengine_ops ioengine = {
2866c82d
JA
229 .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,
b5af8293
JA
238 .open_file = generic_open_file,
239 .close_file = generic_close_file,
df9c26b1 240 .get_file_size = generic_get_file_size,
2866c82d 241};
34cfcdaf
JA
242
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{
a3edaf76 252 log_err("fio: posixaio not available\n");
34cfcdaf
JA
253 return 1;
254}
255
5f350952 256static struct ioengine_ops ioengine = {
34cfcdaf
JA
257 .name = "posixaio",
258 .version = FIO_IOOPS_VERSION,
259 .init = fio_posixaio_init,
260};
261
262#endif
5f350952
JA
263
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}