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