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