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