libaio: switch to newer libaio polled IO API
[fio.git] / engines / libaio.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * libaio engine
3 *
4 * IO engine using the Linux native aio interface.
2866c82d
JA
5 *
6 */
2866c82d
JA
7#include <stdlib.h>
8#include <unistd.h>
9#include <errno.h>
67bf9823 10#include <libaio.h>
5f350952
JA
11
12#include "../fio.h"
0f38bbef 13#include "../lib/pow2.h"
d220c761 14#include "../optgroup.h"
2866c82d 15
ee636f3f
JA
16#ifndef IOCB_FLAG_HIPRI
17#define IOCB_FLAG_HIPRI (1 << 2)
18#endif
a391d73d 19
0fc2e103
JA
20static int fio_libaio_commit(struct thread_data *td);
21
2866c82d
JA
22struct libaio_data {
23 io_context_t aio_ctx;
24 struct io_event *aio_events;
755200a3 25 struct iocb **iocbs;
7e77dd02 26 struct io_u **io_us;
acd45e02
JA
27
28 /*
29 * Basic ring buffer. 'head' is incremented in _queue(), and
30 * 'tail' is incremented in _commit(). We keep 'queued' so
31 * that we know if the ring is full or empty, when
32 * 'head' == 'tail'. 'entries' is the ring size, and
33 * 'is_pow2' is just an optimization to use AND instead of
34 * modulus to get the remainder on ring increment.
35 */
36 int is_pow2;
37 unsigned int entries;
38 unsigned int queued;
39 unsigned int head;
40 unsigned int tail;
2866c82d
JA
41};
42
de890a1e 43struct libaio_options {
a1f871c7 44 void *pad;
de890a1e 45 unsigned int userspace_reap;
a391d73d 46 unsigned int hipri;
de890a1e
SL
47};
48
49static struct fio_option options[] = {
50 {
51 .name = "userspace_reap",
e8b0e958 52 .lname = "Libaio userspace reaping",
de890a1e
SL
53 .type = FIO_OPT_STR_SET,
54 .off1 = offsetof(struct libaio_options, userspace_reap),
55 .help = "Use alternative user-space reap implementation",
e90a0adf 56 .category = FIO_OPT_C_ENGINE,
5a3cd5f3 57 .group = FIO_OPT_G_LIBAIO,
de890a1e 58 },
a391d73d
JA
59 {
60 .name = "hipri",
ee636f3f 61 .lname = "High Priority",
a391d73d
JA
62 .type = FIO_OPT_STR_SET,
63 .off1 = offsetof(struct libaio_options, hipri),
6acc46d8 64 .help = "Use polled IO completions",
a391d73d
JA
65 .category = FIO_OPT_C_ENGINE,
66 .group = FIO_OPT_G_LIBAIO,
67 },
de890a1e
SL
68 {
69 .name = NULL,
70 },
71};
72
acd45e02
JA
73static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
74 unsigned int add)
75{
76 if (ld->is_pow2)
77 *val = (*val + add) & (ld->entries - 1);
78 else
79 *val = (*val + add) % ld->entries;
80}
81
7a16dd02 82static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
2866c82d 83{
53cdc686 84 struct fio_file *f = io_u->file;
a391d73d 85 struct libaio_options *o = td->eo;
53cdc686 86
a391d73d 87 if (io_u->ddir == DDIR_READ) {
cec6b55d 88 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
a391d73d 89 if (o->hipri)
ee636f3f 90 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
a391d73d 91 } else if (io_u->ddir == DDIR_WRITE) {
cec6b55d 92 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
a391d73d 93 if (o->hipri)
ee636f3f 94 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
a391d73d 95 } else if (ddir_sync(io_u->ddir))
87dc1ab1 96 io_prep_fsync(&io_u->iocb, f->fd);
2866c82d
JA
97
98 return 0;
99}
100
101static struct io_u *fio_libaio_event(struct thread_data *td, int event)
102{
565e784d 103 struct libaio_data *ld = td->io_ops_data;
f423479d
JA
104 struct io_event *ev;
105 struct io_u *io_u;
2866c82d 106
f423479d 107 ev = ld->aio_events + event;
0cc6ee59 108 io_u = container_of(ev->obj, struct io_u, iocb);
f423479d
JA
109
110 if (ev->res != io_u->xfer_buflen) {
111 if (ev->res > io_u->xfer_buflen)
112 io_u->error = -ev->res;
113 else
114 io_u->resid = io_u->xfer_buflen - ev->res;
115 } else
116 io_u->error = 0;
117
118 return io_u;
2866c82d
JA
119}
120
675012f0
DE
121struct aio_ring {
122 unsigned id; /** kernel internal index number */
123 unsigned nr; /** number of io_events */
124 unsigned head;
125 unsigned tail;
c44b1ff5 126
675012f0
DE
127 unsigned magic;
128 unsigned compat_features;
129 unsigned incompat_features;
130 unsigned header_length; /** size of aio_ring */
131
132 struct io_event events[0];
133};
134
135#define AIO_RING_MAGIC 0xa10a10a1
136
137static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
c44b1ff5 138 struct io_event *events)
675012f0
DE
139{
140 long i = 0;
141 unsigned head;
c44b1ff5 142 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
675012f0
DE
143
144 while (i < max) {
145 head = ring->head;
146
147 if (head == ring->tail) {
148 /* There are no more completions */
149 break;
150 } else {
151 /* There is another completion to reap */
152 events[i] = ring->events[head];
153 read_barrier();
c44b1ff5 154 ring->head = (head + 1) % ring->nr;
675012f0
DE
155 i++;
156 }
157 }
158
159 return i;
160}
161
e7d2e616 162static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
1f440ece 163 unsigned int max, const struct timespec *t)
2866c82d 164{
565e784d 165 struct libaio_data *ld = td->io_ops_data;
de890a1e 166 struct libaio_options *o = td->eo;
82407585 167 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
1f440ece 168 struct timespec __lt, *lt = NULL;
0b7fdba7 169 int r, events = 0;
2866c82d 170
1f440ece
JA
171 if (t) {
172 __lt = *t;
173 lt = &__lt;
174 }
175
2866c82d 176 do {
de890a1e 177 if (o->userspace_reap == 1
675012f0
DE
178 && actual_min == 0
179 && ((struct aio_ring *)(ld->aio_ctx))->magic
180 == AIO_RING_MAGIC) {
181 r = user_io_getevents(ld->aio_ctx, max,
182 ld->aio_events + events);
183 } else {
184 r = io_getevents(ld->aio_ctx, actual_min,
1f440ece 185 max, ld->aio_events + events, lt);
675012f0 186 }
3441a52d 187 if (r > 0)
0b7fdba7 188 events += r;
3441a52d 189 else if ((min && r == 0) || r == -EAGAIN) {
0fc2e103 190 fio_libaio_commit(td);
6347e43d
JA
191 if (actual_min)
192 usleep(10);
0fc2e103 193 } else if (r != -EINTR)
a31dc2dd 194 break;
0b7fdba7
DE
195 } while (events < min);
196
197 return r < 0 ? r : events;
2866c82d
JA
198}
199
2e4ef4fb
JA
200static enum fio_q_status fio_libaio_queue(struct thread_data *td,
201 struct io_u *io_u)
2866c82d 202{
565e784d 203 struct libaio_data *ld = td->io_ops_data;
2866c82d 204
7101d9c2
JA
205 fio_ro_check(td, io_u);
206
acd45e02 207 if (ld->queued == td->o.iodepth)
755200a3
JA
208 return FIO_Q_BUSY;
209
210 /*
211 * fsync is tricky, since it can fail and we need to do it
212 * serialized with other io. the reason is that linux doesn't
213 * support aio fsync yet. So return busy for the case where we
214 * have pending io, to let fio complete those first.
215 */
f011531e 216 if (ddir_sync(io_u->ddir)) {
acd45e02 217 if (ld->queued)
755200a3 218 return FIO_Q_BUSY;
5f9099ea 219
f011531e 220 do_io_u_sync(td, io_u);
755200a3
JA
221 return FIO_Q_COMPLETED;
222 }
223
a5f3027c 224 if (io_u->ddir == DDIR_TRIM) {
acd45e02 225 if (ld->queued)
a5f3027c
JA
226 return FIO_Q_BUSY;
227
228 do_io_u_trim(td, io_u);
c0681c9d
VF
229 io_u_mark_submit(td, 1);
230 io_u_mark_complete(td, 1);
a5f3027c
JA
231 return FIO_Q_COMPLETED;
232 }
233
acd45e02
JA
234 ld->iocbs[ld->head] = &io_u->iocb;
235 ld->io_us[ld->head] = io_u;
236 ring_inc(ld, &ld->head, 1);
237 ld->queued++;
755200a3
JA
238 return FIO_Q_QUEUED;
239}
240
7e77dd02
JA
241static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
242 unsigned int nr)
243{
8b6a404c 244 struct timespec now;
7e77dd02
JA
245 unsigned int i;
246
12d9d841
JA
247 if (!fio_fill_issue_time(td))
248 return;
249
7e77dd02
JA
250 fio_gettime(&now, NULL);
251
252 for (i = 0; i < nr; i++) {
253 struct io_u *io_u = io_us[i];
254
255 memcpy(&io_u->issue_time, &now, sizeof(now));
256 io_u_queued(td, io_u);
257 }
258}
259
755200a3
JA
260static int fio_libaio_commit(struct thread_data *td)
261{
565e784d 262 struct libaio_data *ld = td->io_ops_data;
755200a3 263 struct iocb **iocbs;
7e77dd02 264 struct io_u **io_us;
8b6a404c 265 struct timespec ts;
a120ca7f 266 int ret, wait_start = 0;
755200a3 267
acd45e02 268 if (!ld->queued)
755200a3
JA
269 return 0;
270
2866c82d 271 do {
acd45e02
JA
272 long nr = ld->queued;
273
274 nr = min((unsigned int) nr, ld->entries - ld->tail);
275 io_us = ld->io_us + ld->tail;
276 iocbs = ld->iocbs + ld->tail;
277
278 ret = io_submit(ld->aio_ctx, nr, iocbs);
5e00c2c4 279 if (ret > 0) {
7e77dd02 280 fio_libaio_queued(td, io_us, ret);
838bc709 281 io_u_mark_submit(td, ret);
acd45e02
JA
282
283 ld->queued -= ret;
284 ring_inc(ld, &ld->tail, ret);
5e00c2c4 285 ret = 0;
e3b4e568 286 wait_start = 0;
acd45e02 287 } else if (ret == -EINTR || !ret) {
838bc709
JA
288 if (!ret)
289 io_u_mark_submit(td, ret);
e3b4e568 290 wait_start = 0;
2866c82d 291 continue;
acd45e02
JA
292 } else if (ret == -EAGAIN) {
293 /*
294 * If we get EAGAIN, we should break out without
295 * error and let the upper layer reap some
a120ca7f
JA
296 * events for us. If we have no queued IO, we
297 * must loop here. If we loop for more than 30s,
298 * just error out, something must be buggy in the
299 * IO path.
acd45e02 300 */
a120ca7f
JA
301 if (ld->queued) {
302 ret = 0;
303 break;
304 }
305 if (!wait_start) {
8b6a404c 306 fio_gettime(&ts, NULL);
d36b072d 307 wait_start = 1;
8b6a404c 308 } else if (mtime_since_now(&ts) > 30000) {
a120ca7f
JA
309 log_err("fio: aio appears to be stalled, giving up\n");
310 break;
311 }
312 usleep(1);
313 continue;
a31dc2dd
JA
314 } else if (ret == -ENOMEM) {
315 /*
316 * If we get -ENOMEM, reap events if we can. If
317 * we cannot, treat it as a fatal event since there's
318 * nothing we can do about it.
319 */
320 if (ld->queued)
321 ret = 0;
322 break;
838bc709 323 } else
2866c82d 324 break;
2c3a4ae9 325 } while (ld->queued);
2866c82d 326
36167d82 327 return ret;
2866c82d
JA
328}
329
330static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
331{
565e784d 332 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
333
334 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
335}
336
337static void fio_libaio_cleanup(struct thread_data *td)
338{
565e784d 339 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
340
341 if (ld) {
f24c2649
JA
342 /*
343 * Work-around to avoid huge RCU stalls at exit time. If we
344 * don't do this here, then it'll be torn down by exit_aio().
345 * But for that case we can parallellize the freeing, thus
346 * speeding it up a lot.
347 */
348 if (!(td->flags & TD_F_CHILD))
349 io_destroy(ld->aio_ctx);
7e77dd02
JA
350 free(ld->aio_events);
351 free(ld->iocbs);
352 free(ld->io_us);
2866c82d 353 free(ld);
2866c82d
JA
354 }
355}
356
357static int fio_libaio_init(struct thread_data *td)
358{
c90e1017 359 struct libaio_options *o = td->eo;
acd45e02 360 struct libaio_data *ld;
c90e1017 361 int err = 0;
2866c82d 362
acd45e02 363 ld = calloc(1, sizeof(*ld));
c1db2dce 364
c90e1017
JA
365 /*
366 * First try passing in 0 for queue depth, since we don't
367 * care about the user ring. If that fails, the kernel is too old
368 * and we need the right depth.
369 */
370 if (!o->userspace_reap)
c224ec05 371 err = io_queue_init(INT_MAX, &ld->aio_ctx);
c90e1017
JA
372 if (o->userspace_reap || err == -EINVAL)
373 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
c1db2dce
JA
374 if (err) {
375 td_verror(td, -err, "io_queue_init");
75de55ac 376 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
cb781c75 377 free(ld);
2866c82d
JA
378 return 1;
379 }
380
acd45e02
JA
381 ld->entries = td->o.iodepth;
382 ld->is_pow2 = is_power_of_2(ld->entries);
383 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
384 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
385 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
755200a3 386
565e784d 387 td->io_ops_data = ld;
2866c82d
JA
388 return 0;
389}
390
5f350952 391static struct ioengine_ops ioengine = {
de890a1e
SL
392 .name = "libaio",
393 .version = FIO_IOOPS_VERSION,
394 .init = fio_libaio_init,
395 .prep = fio_libaio_prep,
396 .queue = fio_libaio_queue,
397 .commit = fio_libaio_commit,
398 .cancel = fio_libaio_cancel,
399 .getevents = fio_libaio_getevents,
400 .event = fio_libaio_event,
401 .cleanup = fio_libaio_cleanup,
402 .open_file = generic_open_file,
403 .close_file = generic_close_file,
404 .get_file_size = generic_get_file_size,
405 .options = options,
406 .option_struct_size = sizeof(struct libaio_options),
2866c82d 407};
34cfcdaf 408
5f350952
JA
409static void fio_init fio_libaio_register(void)
410{
411 register_ioengine(&ioengine);
412}
413
414static void fio_exit fio_libaio_unregister(void)
415{
416 unregister_ioengine(&ioengine);
417}