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