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