ioengines: add helper for trims with async ioengines
[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 },
b2a432bf
PC
75#ifdef FIO_HAVE_IOPRIO_CLASS
76 {
77 .name = "cmdprio_percentage",
78 .lname = "high priority percentage",
79 .type = FIO_OPT_INT,
e9f6567a 80 .off1 = offsetof(struct libaio_options,
d6cbeab4 81 cmdprio_options.percentage[DDIR_READ]),
e9f6567a 82 .off2 = offsetof(struct libaio_options,
d6cbeab4 83 cmdprio_options.percentage[DDIR_WRITE]),
e9f6567a 84 .minval = 0,
b2a432bf
PC
85 .maxval = 100,
86 .help = "Send high priority I/O this percentage of the time",
87 .category = FIO_OPT_C_ENGINE,
88 .group = FIO_OPT_G_LIBAIO,
89 },
12f9d54a
DLM
90 {
91 .name = "cmdprio_class",
92 .lname = "Asynchronous I/O priority class",
93 .type = FIO_OPT_INT,
94 .off1 = offsetof(struct libaio_options,
d6cbeab4 95 cmdprio_options.class[DDIR_READ]),
12f9d54a 96 .off2 = offsetof(struct libaio_options,
d6cbeab4 97 cmdprio_options.class[DDIR_WRITE]),
12f9d54a
DLM
98 .help = "Set asynchronous IO priority class",
99 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
100 .maxval = IOPRIO_MAX_PRIO_CLASS,
101 .interval = 1,
102 .category = FIO_OPT_C_ENGINE,
103 .group = FIO_OPT_G_LIBAIO,
104 },
105 {
106 .name = "cmdprio",
107 .lname = "Asynchronous I/O priority level",
108 .type = FIO_OPT_INT,
109 .off1 = offsetof(struct libaio_options,
d6cbeab4 110 cmdprio_options.level[DDIR_READ]),
12f9d54a 111 .off2 = offsetof(struct libaio_options,
d6cbeab4 112 cmdprio_options.level[DDIR_WRITE]),
12f9d54a
DLM
113 .help = "Set asynchronous IO priority level",
114 .minval = IOPRIO_MIN_PRIO,
115 .maxval = IOPRIO_MAX_PRIO,
116 .interval = 1,
117 .category = FIO_OPT_C_ENGINE,
118 .group = FIO_OPT_G_LIBAIO,
119 },
a48f0cc7
DLM
120 {
121 .name = "cmdprio_bssplit",
122 .lname = "Priority percentage block size split",
d6cbeab4
NC
123 .type = FIO_OPT_STR_STORE,
124 .off1 = offsetof(struct libaio_options,
125 cmdprio_options.bssplit_str),
a48f0cc7
DLM
126 .help = "Set priority percentages for different block sizes",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_LIBAIO,
129 },
b2a432bf
PC
130#else
131 {
132 .name = "cmdprio_percentage",
133 .lname = "high priority percentage",
134 .type = FIO_OPT_UNSUPPORTED,
135 .help = "Your platform does not support I/O priority classes",
136 },
12f9d54a
DLM
137 {
138 .name = "cmdprio_class",
139 .lname = "Asynchronous I/O priority class",
140 .type = FIO_OPT_UNSUPPORTED,
141 .help = "Your platform does not support I/O priority classes",
142 },
143 {
144 .name = "cmdprio",
145 .lname = "Asynchronous I/O priority level",
146 .type = FIO_OPT_UNSUPPORTED,
147 .help = "Your platform does not support I/O priority classes",
148 },
a48f0cc7
DLM
149 {
150 .name = "cmdprio_bssplit",
151 .lname = "Priority percentage block size split",
152 .type = FIO_OPT_UNSUPPORTED,
153 .help = "Your platform does not support I/O priority classes",
154 },
b2a432bf 155#endif
7d42e66e
KK
156 {
157 .name = "nowait",
158 .lname = "RWF_NOWAIT",
159 .type = FIO_OPT_BOOL,
160 .off1 = offsetof(struct libaio_options, nowait),
161 .help = "Set RWF_NOWAIT for reads/writes",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_LIBAIO,
164 },
de890a1e
SL
165 {
166 .name = NULL,
167 },
168};
169
acd45e02
JA
170static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
171 unsigned int add)
172{
173 if (ld->is_pow2)
174 *val = (*val + add) & (ld->entries - 1);
175 else
176 *val = (*val + add) % ld->entries;
177}
178
7d42e66e 179static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
2866c82d 180{
7d42e66e 181 struct libaio_options *o = td->eo;
53cdc686 182 struct fio_file *f = io_u->file;
ad4e9298 183 struct iocb *iocb = &io_u->iocb;
53cdc686 184
a391d73d 185 if (io_u->ddir == DDIR_READ) {
702906e9 186 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
7d42e66e
KK
187 if (o->nowait)
188 iocb->aio_rw_flags |= RWF_NOWAIT;
a391d73d 189 } else if (io_u->ddir == DDIR_WRITE) {
702906e9 190 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
7d42e66e
KK
191 if (o->nowait)
192 iocb->aio_rw_flags |= RWF_NOWAIT;
a391d73d 193 } else if (ddir_sync(io_u->ddir))
3c3168e9 194 io_prep_fsync(iocb, f->fd);
2866c82d
JA
195
196 return 0;
197}
198
127715b6
NC
199static inline void fio_libaio_cmdprio_prep(struct thread_data *td,
200 struct io_u *io_u)
b2a432bf 201{
d6cbeab4
NC
202 struct libaio_data *ld = td->io_ops_data;
203 struct cmdprio *cmdprio = &ld->cmdprio;
127715b6
NC
204
205 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u)) {
206 io_u->iocb.aio_reqprio = io_u->ioprio;
b2a432bf 207 io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO;
b2a432bf 208 }
b2a432bf
PC
209}
210
2866c82d
JA
211static struct io_u *fio_libaio_event(struct thread_data *td, int event)
212{
565e784d 213 struct libaio_data *ld = td->io_ops_data;
f423479d
JA
214 struct io_event *ev;
215 struct io_u *io_u;
2866c82d 216
f423479d 217 ev = ld->aio_events + event;
702906e9 218 io_u = container_of(ev->obj, struct io_u, iocb);
f423479d
JA
219
220 if (ev->res != io_u->xfer_buflen) {
221 if (ev->res > io_u->xfer_buflen)
222 io_u->error = -ev->res;
223 else
224 io_u->resid = io_u->xfer_buflen - ev->res;
225 } else
226 io_u->error = 0;
227
228 return io_u;
2866c82d
JA
229}
230
675012f0
DE
231struct aio_ring {
232 unsigned id; /** kernel internal index number */
233 unsigned nr; /** number of io_events */
234 unsigned head;
235 unsigned tail;
c44b1ff5 236
675012f0
DE
237 unsigned magic;
238 unsigned compat_features;
239 unsigned incompat_features;
240 unsigned header_length; /** size of aio_ring */
241
242 struct io_event events[0];
243};
244
245#define AIO_RING_MAGIC 0xa10a10a1
246
247static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
c44b1ff5 248 struct io_event *events)
675012f0
DE
249{
250 long i = 0;
251 unsigned head;
c44b1ff5 252 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
675012f0
DE
253
254 while (i < max) {
255 head = ring->head;
256
257 if (head == ring->tail) {
258 /* There are no more completions */
259 break;
260 } else {
261 /* There is another completion to reap */
262 events[i] = ring->events[head];
d473a06d
BVA
263 atomic_store_release(&ring->head,
264 (head + 1) % ring->nr);
675012f0
DE
265 i++;
266 }
267 }
268
269 return i;
270}
271
e7d2e616 272static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
1f440ece 273 unsigned int max, const struct timespec *t)
2866c82d 274{
565e784d 275 struct libaio_data *ld = td->io_ops_data;
de890a1e 276 struct libaio_options *o = td->eo;
82407585 277 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
1f440ece 278 struct timespec __lt, *lt = NULL;
0b7fdba7 279 int r, events = 0;
2866c82d 280
1f440ece
JA
281 if (t) {
282 __lt = *t;
283 lt = &__lt;
284 }
285
2866c82d 286 do {
de890a1e 287 if (o->userspace_reap == 1
675012f0
DE
288 && actual_min == 0
289 && ((struct aio_ring *)(ld->aio_ctx))->magic
290 == AIO_RING_MAGIC) {
291 r = user_io_getevents(ld->aio_ctx, max,
292 ld->aio_events + events);
293 } else {
294 r = io_getevents(ld->aio_ctx, actual_min,
1f440ece 295 max, ld->aio_events + events, lt);
675012f0 296 }
3441a52d 297 if (r > 0)
0b7fdba7 298 events += r;
3441a52d 299 else if ((min && r == 0) || r == -EAGAIN) {
0fc2e103 300 fio_libaio_commit(td);
6347e43d
JA
301 if (actual_min)
302 usleep(10);
0fc2e103 303 } else if (r != -EINTR)
a31dc2dd 304 break;
0b7fdba7
DE
305 } while (events < min);
306
307 return r < 0 ? r : events;
2866c82d
JA
308}
309
2e4ef4fb
JA
310static enum fio_q_status fio_libaio_queue(struct thread_data *td,
311 struct io_u *io_u)
2866c82d 312{
565e784d 313 struct libaio_data *ld = td->io_ops_data;
2866c82d 314
7101d9c2
JA
315 fio_ro_check(td, io_u);
316
acd45e02 317 if (ld->queued == td->o.iodepth)
755200a3
JA
318 return FIO_Q_BUSY;
319
320 /*
321 * fsync is tricky, since it can fail and we need to do it
322 * serialized with other io. the reason is that linux doesn't
323 * support aio fsync yet. So return busy for the case where we
324 * have pending io, to let fio complete those first.
325 */
f011531e 326 if (ddir_sync(io_u->ddir)) {
acd45e02 327 if (ld->queued)
755200a3 328 return FIO_Q_BUSY;
5f9099ea 329
f011531e 330 do_io_u_sync(td, io_u);
755200a3
JA
331 return FIO_Q_COMPLETED;
332 }
333
a5f3027c 334 if (io_u->ddir == DDIR_TRIM) {
acd45e02 335 if (ld->queued)
a5f3027c
JA
336 return FIO_Q_BUSY;
337
338 do_io_u_trim(td, io_u);
c0681c9d
VF
339 io_u_mark_submit(td, 1);
340 io_u_mark_complete(td, 1);
a5f3027c
JA
341 return FIO_Q_COMPLETED;
342 }
343
d6cbeab4 344 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
ff00f247 345 fio_libaio_cmdprio_prep(td, io_u);
b2a432bf 346
702906e9 347 ld->iocbs[ld->head] = &io_u->iocb;
acd45e02
JA
348 ld->io_us[ld->head] = io_u;
349 ring_inc(ld, &ld->head, 1);
350 ld->queued++;
755200a3
JA
351 return FIO_Q_QUEUED;
352}
353
7e77dd02
JA
354static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
355 unsigned int nr)
356{
8b6a404c 357 struct timespec now;
7e77dd02
JA
358 unsigned int i;
359
12d9d841
JA
360 if (!fio_fill_issue_time(td))
361 return;
362
7e77dd02
JA
363 fio_gettime(&now, NULL);
364
365 for (i = 0; i < nr; i++) {
366 struct io_u *io_u = io_us[i];
367
368 memcpy(&io_u->issue_time, &now, sizeof(now));
369 io_u_queued(td, io_u);
370 }
371}
372
755200a3
JA
373static int fio_libaio_commit(struct thread_data *td)
374{
565e784d 375 struct libaio_data *ld = td->io_ops_data;
755200a3 376 struct iocb **iocbs;
7e77dd02 377 struct io_u **io_us;
8b6a404c 378 struct timespec ts;
a120ca7f 379 int ret, wait_start = 0;
755200a3 380
acd45e02 381 if (!ld->queued)
755200a3
JA
382 return 0;
383
2866c82d 384 do {
acd45e02
JA
385 long nr = ld->queued;
386
387 nr = min((unsigned int) nr, ld->entries - ld->tail);
388 io_us = ld->io_us + ld->tail;
389 iocbs = ld->iocbs + ld->tail;
390
391 ret = io_submit(ld->aio_ctx, nr, iocbs);
5e00c2c4 392 if (ret > 0) {
7e77dd02 393 fio_libaio_queued(td, io_us, ret);
838bc709 394 io_u_mark_submit(td, ret);
acd45e02
JA
395
396 ld->queued -= ret;
397 ring_inc(ld, &ld->tail, ret);
5e00c2c4 398 ret = 0;
e3b4e568 399 wait_start = 0;
acd45e02 400 } else if (ret == -EINTR || !ret) {
838bc709
JA
401 if (!ret)
402 io_u_mark_submit(td, ret);
e3b4e568 403 wait_start = 0;
2866c82d 404 continue;
acd45e02
JA
405 } else if (ret == -EAGAIN) {
406 /*
407 * If we get EAGAIN, we should break out without
408 * error and let the upper layer reap some
a120ca7f
JA
409 * events for us. If we have no queued IO, we
410 * must loop here. If we loop for more than 30s,
411 * just error out, something must be buggy in the
412 * IO path.
acd45e02 413 */
a120ca7f
JA
414 if (ld->queued) {
415 ret = 0;
416 break;
417 }
418 if (!wait_start) {
8b6a404c 419 fio_gettime(&ts, NULL);
d36b072d 420 wait_start = 1;
8b6a404c 421 } else if (mtime_since_now(&ts) > 30000) {
a120ca7f
JA
422 log_err("fio: aio appears to be stalled, giving up\n");
423 break;
424 }
425 usleep(1);
426 continue;
a31dc2dd
JA
427 } else if (ret == -ENOMEM) {
428 /*
429 * If we get -ENOMEM, reap events if we can. If
430 * we cannot, treat it as a fatal event since there's
431 * nothing we can do about it.
432 */
433 if (ld->queued)
434 ret = 0;
435 break;
838bc709 436 } else
2866c82d 437 break;
2c3a4ae9 438 } while (ld->queued);
2866c82d 439
36167d82 440 return ret;
2866c82d
JA
441}
442
443static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
444{
565e784d 445 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
446
447 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
448}
449
450static void fio_libaio_cleanup(struct thread_data *td)
451{
565e784d 452 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
453
454 if (ld) {
f24c2649
JA
455 /*
456 * Work-around to avoid huge RCU stalls at exit time. If we
457 * don't do this here, then it'll be torn down by exit_aio().
458 * But for that case we can parallellize the freeing, thus
459 * speeding it up a lot.
460 */
461 if (!(td->flags & TD_F_CHILD))
462 io_destroy(ld->aio_ctx);
d6cbeab4
NC
463
464 fio_cmdprio_cleanup(&ld->cmdprio);
7e77dd02
JA
465 free(ld->aio_events);
466 free(ld->iocbs);
467 free(ld->io_us);
2866c82d 468 free(ld);
2866c82d
JA
469 }
470}
471
2041bd34
JA
472static int fio_libaio_post_init(struct thread_data *td)
473{
474 struct libaio_data *ld = td->io_ops_data;
ad4e9298 475 int err;
2041bd34 476
ad4e9298 477 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
2041bd34
JA
478 if (err) {
479 td_verror(td, -err, "io_queue_init");
480 return 1;
481 }
482
483 return 0;
ebec344d
JA
484}
485
2866c82d
JA
486static int fio_libaio_init(struct thread_data *td)
487{
acd45e02 488 struct libaio_data *ld;
b2a432bf 489 struct libaio_options *o = td->eo;
e9f6567a 490 int ret;
2866c82d 491
acd45e02 492 ld = calloc(1, sizeof(*ld));
c1db2dce 493
acd45e02
JA
494 ld->entries = td->o.iodepth;
495 ld->is_pow2 = is_power_of_2(ld->entries);
496 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
497 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
498 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
755200a3 499
565e784d 500 td->io_ops_data = ld;
e9f6567a 501
d6cbeab4 502 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
e9f6567a 503 if (ret) {
b2a432bf
PC
504 td_verror(td, EINVAL, "fio_libaio_init");
505 return 1;
506 }
e9f6567a 507
2866c82d
JA
508 return 0;
509}
510
5a8a6a03 511FIO_STATIC struct ioengine_ops ioengine = {
de890a1e
SL
512 .name = "libaio",
513 .version = FIO_IOOPS_VERSION,
04ba61df 514 .flags = FIO_ASYNCIO_SYNC_TRIM,
de890a1e 515 .init = fio_libaio_init,
2041bd34 516 .post_init = fio_libaio_post_init,
de890a1e
SL
517 .prep = fio_libaio_prep,
518 .queue = fio_libaio_queue,
519 .commit = fio_libaio_commit,
520 .cancel = fio_libaio_cancel,
521 .getevents = fio_libaio_getevents,
522 .event = fio_libaio_event,
523 .cleanup = fio_libaio_cleanup,
524 .open_file = generic_open_file,
525 .close_file = generic_close_file,
526 .get_file_size = generic_get_file_size,
527 .options = options,
528 .option_struct_size = sizeof(struct libaio_options),
2866c82d 529};
34cfcdaf 530
5f350952
JA
531static void fio_init fio_libaio_register(void)
532{
533 register_ioengine(&ioengine);
534}
535
536static void fio_exit fio_libaio_unregister(void)
537{
538 unregister_ioengine(&ioengine);
539}