libaio,io_uring: relax cmdprio_percentage constraints
[fio.git] / engines / libaio.c
1 /*
2  * libaio engine
3  *
4  * IO engine using the Linux native aio interface.
5  *
6  */
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <libaio.h>
11 #include <sys/time.h>
12 #include <sys/resource.h>
13
14 #include "../fio.h"
15 #include "../lib/pow2.h"
16 #include "../optgroup.h"
17 #include "../lib/memalign.h"
18 #include "cmdprio.h"
19
20 /* Should be defined in newest aio_abi.h */
21 #ifndef IOCB_FLAG_IOPRIO
22 #define IOCB_FLAG_IOPRIO    (1 << 1)
23 #endif
24
25 /* Hack for libaio < 0.3.111 */
26 #ifndef CONFIG_LIBAIO_RW_FLAGS
27 #define aio_rw_flags __pad2
28 #endif
29
30 static int fio_libaio_commit(struct thread_data *td);
31 static int fio_libaio_init(struct thread_data *td);
32
33 struct libaio_data {
34         io_context_t aio_ctx;
35         struct io_event *aio_events;
36         struct iocb **iocbs;
37         struct io_u **io_us;
38
39         struct io_u **io_u_index;
40
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;
54
55         bool use_cmdprio;
56 };
57
58 struct libaio_options {
59         struct thread_data *td;
60         unsigned int userspace_reap;
61         struct cmdprio cmdprio;
62         unsigned int nowait;
63 };
64
65 static 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
74 static struct fio_option options[] = {
75         {
76                 .name   = "userspace_reap",
77                 .lname  = "Libaio userspace reaping",
78                 .type   = FIO_OPT_STR_SET,
79                 .off1   = offsetof(struct libaio_options, userspace_reap),
80                 .help   = "Use alternative user-space reap implementation",
81                 .category = FIO_OPT_C_ENGINE,
82                 .group  = FIO_OPT_G_LIBAIO,
83         },
84 #ifdef FIO_HAVE_IOPRIO_CLASS
85         {
86                 .name   = "cmdprio_percentage",
87                 .lname  = "high priority percentage",
88                 .type   = FIO_OPT_INT,
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,
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         },
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         },
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         },
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         },
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         },
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         },
164 #endif
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         },
174         {
175                 .name   = NULL,
176         },
177 };
178
179 static 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
188 static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
189 {
190         struct libaio_options *o = td->eo;
191         struct fio_file *f = io_u->file;
192         struct iocb *iocb = &io_u->iocb;
193
194         if (io_u->ddir == DDIR_READ) {
195                 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
196                 if (o->nowait)
197                         iocb->aio_rw_flags |= RWF_NOWAIT;
198         } else if (io_u->ddir == DDIR_WRITE) {
199                 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
200                 if (o->nowait)
201                         iocb->aio_rw_flags |= RWF_NOWAIT;
202         } else if (ddir_sync(io_u->ddir))
203                 io_prep_fsync(iocb, f->fd);
204
205         return 0;
206 }
207
208 static void fio_libaio_prio_prep(struct thread_data *td, struct io_u *io_u)
209 {
210         struct libaio_options *o = td->eo;
211         struct cmdprio *cmdprio = &o->cmdprio;
212         enum fio_ddir ddir = io_u->ddir;
213         unsigned int p = fio_cmdprio_percentage(cmdprio, io_u);
214         unsigned int cmdprio_value =
215                 ioprio_value(cmdprio->class[ddir], cmdprio->level[ddir]);
216
217         if (p && rand_between(&td->prio_state, 0, 99) < p) {
218                 io_u->iocb.aio_reqprio = cmdprio_value;
219                 io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO;
220                 if (!td->ioprio || cmdprio_value < td->ioprio) {
221                         /*
222                          * The async IO priority is higher (has a lower value)
223                          * than the default context priority.
224                          */
225                         io_u->flags |= IO_U_F_PRIORITY;
226                 }
227         } else if (td->ioprio && td->ioprio < cmdprio_value) {
228                 /*
229                  * The IO will be executed with the default context priority,
230                  * and this priority is higher (has a lower value) than the
231                  * async IO priority.
232                  */
233                 io_u->flags |= IO_U_F_PRIORITY;
234         }
235 }
236
237 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
238 {
239         struct libaio_data *ld = td->io_ops_data;
240         struct io_event *ev;
241         struct io_u *io_u;
242
243         ev = ld->aio_events + event;
244         io_u = container_of(ev->obj, struct io_u, iocb);
245
246         if (ev->res != io_u->xfer_buflen) {
247                 if (ev->res > io_u->xfer_buflen)
248                         io_u->error = -ev->res;
249                 else
250                         io_u->resid = io_u->xfer_buflen - ev->res;
251         } else
252                 io_u->error = 0;
253
254         return io_u;
255 }
256
257 struct aio_ring {
258         unsigned id;             /** kernel internal index number */
259         unsigned nr;             /** number of io_events */
260         unsigned head;
261         unsigned tail;
262
263         unsigned magic;
264         unsigned compat_features;
265         unsigned incompat_features;
266         unsigned header_length; /** size of aio_ring */
267
268         struct io_event events[0];
269 };
270
271 #define AIO_RING_MAGIC  0xa10a10a1
272
273 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
274                              struct io_event *events)
275 {
276         long i = 0;
277         unsigned head;
278         struct aio_ring *ring = (struct aio_ring*) aio_ctx;
279
280         while (i < max) {
281                 head = ring->head;
282
283                 if (head == ring->tail) {
284                         /* There are no more completions */
285                         break;
286                 } else {
287                         /* There is another completion to reap */
288                         events[i] = ring->events[head];
289                         atomic_store_release(&ring->head,
290                                              (head + 1) % ring->nr);
291                         i++;
292                 }
293         }
294
295         return i;
296 }
297
298 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
299                                 unsigned int max, const struct timespec *t)
300 {
301         struct libaio_data *ld = td->io_ops_data;
302         struct libaio_options *o = td->eo;
303         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
304         struct timespec __lt, *lt = NULL;
305         int r, events = 0;
306
307         if (t) {
308                 __lt = *t;
309                 lt = &__lt;
310         }
311
312         do {
313                 if (o->userspace_reap == 1
314                     && actual_min == 0
315                     && ((struct aio_ring *)(ld->aio_ctx))->magic
316                                 == AIO_RING_MAGIC) {
317                         r = user_io_getevents(ld->aio_ctx, max,
318                                 ld->aio_events + events);
319                 } else {
320                         r = io_getevents(ld->aio_ctx, actual_min,
321                                 max, ld->aio_events + events, lt);
322                 }
323                 if (r > 0)
324                         events += r;
325                 else if ((min && r == 0) || r == -EAGAIN) {
326                         fio_libaio_commit(td);
327                         if (actual_min)
328                                 usleep(10);
329                 } else if (r != -EINTR)
330                         break;
331         } while (events < min);
332
333         return r < 0 ? r : events;
334 }
335
336 static enum fio_q_status fio_libaio_queue(struct thread_data *td,
337                                           struct io_u *io_u)
338 {
339         struct libaio_data *ld = td->io_ops_data;
340
341         fio_ro_check(td, io_u);
342
343         if (ld->queued == td->o.iodepth)
344                 return FIO_Q_BUSY;
345
346         /*
347          * fsync is tricky, since it can fail and we need to do it
348          * serialized with other io. the reason is that linux doesn't
349          * support aio fsync yet. So return busy for the case where we
350          * have pending io, to let fio complete those first.
351          */
352         if (ddir_sync(io_u->ddir)) {
353                 if (ld->queued)
354                         return FIO_Q_BUSY;
355
356                 do_io_u_sync(td, io_u);
357                 return FIO_Q_COMPLETED;
358         }
359
360         if (io_u->ddir == DDIR_TRIM) {
361                 if (ld->queued)
362                         return FIO_Q_BUSY;
363
364                 do_io_u_trim(td, io_u);
365                 io_u_mark_submit(td, 1);
366                 io_u_mark_complete(td, 1);
367                 return FIO_Q_COMPLETED;
368         }
369
370         if (ld->use_cmdprio)
371                 fio_libaio_prio_prep(td, io_u);
372
373         ld->iocbs[ld->head] = &io_u->iocb;
374         ld->io_us[ld->head] = io_u;
375         ring_inc(ld, &ld->head, 1);
376         ld->queued++;
377         return FIO_Q_QUEUED;
378 }
379
380 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
381                               unsigned int nr)
382 {
383         struct timespec now;
384         unsigned int i;
385
386         if (!fio_fill_issue_time(td))
387                 return;
388
389         fio_gettime(&now, NULL);
390
391         for (i = 0; i < nr; i++) {
392                 struct io_u *io_u = io_us[i];
393
394                 memcpy(&io_u->issue_time, &now, sizeof(now));
395                 io_u_queued(td, io_u);
396         }
397 }
398
399 static int fio_libaio_commit(struct thread_data *td)
400 {
401         struct libaio_data *ld = td->io_ops_data;
402         struct iocb **iocbs;
403         struct io_u **io_us;
404         struct timespec ts;
405         int ret, wait_start = 0;
406
407         if (!ld->queued)
408                 return 0;
409
410         do {
411                 long nr = ld->queued;
412
413                 nr = min((unsigned int) nr, ld->entries - ld->tail);
414                 io_us = ld->io_us + ld->tail;
415                 iocbs = ld->iocbs + ld->tail;
416
417                 ret = io_submit(ld->aio_ctx, nr, iocbs);
418                 if (ret > 0) {
419                         fio_libaio_queued(td, io_us, ret);
420                         io_u_mark_submit(td, ret);
421
422                         ld->queued -= ret;
423                         ring_inc(ld, &ld->tail, ret);
424                         ret = 0;
425                         wait_start = 0;
426                 } else if (ret == -EINTR || !ret) {
427                         if (!ret)
428                                 io_u_mark_submit(td, ret);
429                         wait_start = 0;
430                         continue;
431                 } else if (ret == -EAGAIN) {
432                         /*
433                          * If we get EAGAIN, we should break out without
434                          * error and let the upper layer reap some
435                          * events for us. If we have no queued IO, we
436                          * must loop here. If we loop for more than 30s,
437                          * just error out, something must be buggy in the
438                          * IO path.
439                          */
440                         if (ld->queued) {
441                                 ret = 0;
442                                 break;
443                         }
444                         if (!wait_start) {
445                                 fio_gettime(&ts, NULL);
446                                 wait_start = 1;
447                         } else if (mtime_since_now(&ts) > 30000) {
448                                 log_err("fio: aio appears to be stalled, giving up\n");
449                                 break;
450                         }
451                         usleep(1);
452                         continue;
453                 } else if (ret == -ENOMEM) {
454                         /*
455                          * If we get -ENOMEM, reap events if we can. If
456                          * we cannot, treat it as a fatal event since there's
457                          * nothing we can do about it.
458                          */
459                         if (ld->queued)
460                                 ret = 0;
461                         break;
462                 } else
463                         break;
464         } while (ld->queued);
465
466         return ret;
467 }
468
469 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
470 {
471         struct libaio_data *ld = td->io_ops_data;
472
473         return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
474 }
475
476 static void fio_libaio_cleanup(struct thread_data *td)
477 {
478         struct libaio_data *ld = td->io_ops_data;
479
480         if (ld) {
481                 /*
482                  * Work-around to avoid huge RCU stalls at exit time. If we
483                  * don't do this here, then it'll be torn down by exit_aio().
484                  * But for that case we can parallellize the freeing, thus
485                  * speeding it up a lot.
486                  */
487                 if (!(td->flags & TD_F_CHILD))
488                         io_destroy(ld->aio_ctx);
489                 free(ld->aio_events);
490                 free(ld->iocbs);
491                 free(ld->io_us);
492                 free(ld);
493         }
494 }
495
496 static int fio_libaio_post_init(struct thread_data *td)
497 {
498         struct libaio_data *ld = td->io_ops_data;
499         int err;
500
501         err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
502         if (err) {
503                 td_verror(td, -err, "io_queue_init");
504                 return 1;
505         }
506
507         return 0;
508 }
509
510 static int fio_libaio_init(struct thread_data *td)
511 {
512         struct libaio_data *ld;
513         struct libaio_options *o = td->eo;
514         struct cmdprio *cmdprio = &o->cmdprio;
515         int ret;
516
517         ld = calloc(1, sizeof(*ld));
518
519         ld->entries = td->o.iodepth;
520         ld->is_pow2 = is_power_of_2(ld->entries);
521         ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
522         ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
523         ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
524
525         td->io_ops_data = ld;
526
527         ret = fio_cmdprio_init(td, cmdprio, &ld->use_cmdprio);
528         if (ret) {
529                 td_verror(td, EINVAL, "fio_libaio_init");
530                 return 1;
531         }
532
533         return 0;
534 }
535
536 FIO_STATIC struct ioengine_ops ioengine = {
537         .name                   = "libaio",
538         .version                = FIO_IOOPS_VERSION,
539         .flags                  = FIO_ASYNCIO_SYNC_TRIM,
540         .init                   = fio_libaio_init,
541         .post_init              = fio_libaio_post_init,
542         .prep                   = fio_libaio_prep,
543         .queue                  = fio_libaio_queue,
544         .commit                 = fio_libaio_commit,
545         .cancel                 = fio_libaio_cancel,
546         .getevents              = fio_libaio_getevents,
547         .event                  = fio_libaio_event,
548         .cleanup                = fio_libaio_cleanup,
549         .open_file              = generic_open_file,
550         .close_file             = generic_close_file,
551         .get_file_size          = generic_get_file_size,
552         .options                = options,
553         .option_struct_size     = sizeof(struct libaio_options),
554 };
555
556 static void fio_init fio_libaio_register(void)
557 {
558         register_ioengine(&ioengine);
559 }
560
561 static void fio_exit fio_libaio_unregister(void)
562 {
563         unregister_ioengine(&ioengine);
564 }