88641a93813807799b36ef2436cd48919ea9e8d0
[fio.git] / engines / aioring.c
1 /*
2  * aioring engine
3  *
4  * IO engine using the new native Linux libaio ring interface. See:
5  *
6  * http://git.kernel.dk/cgit/linux-block/log/?h=aio-poll
7  *
8  */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <libaio.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15
16 #include "../fio.h"
17 #include "../lib/pow2.h"
18 #include "../optgroup.h"
19 #include "../lib/memalign.h"
20 #include "../lib/fls.h"
21
22 #ifdef ARCH_HAVE_AIORING
23
24 #ifndef IOCB_FLAG_HIPRI
25 #define IOCB_FLAG_HIPRI (1 << 2)
26 #endif
27
28 /*
29  * io_setup2(2) flags
30  */
31 #ifndef IOCTX_FLAG_IOPOLL
32 #define IOCTX_FLAG_IOPOLL       (1 << 0)
33 #endif
34 #ifndef IOCTX_FLAG_SCQRING
35 #define IOCTX_FLAG_SCQRING      (1 << 1)
36 #endif
37 #ifndef IOCTX_FLAG_FIXEDBUFS
38 #define IOCTX_FLAG_FIXEDBUFS    (1 << 2)
39 #endif
40 #ifndef IOCTX_FLAG_SQTHREAD
41 #define IOCTX_FLAG_SQTHREAD     (1 << 3)
42 #endif
43 #ifndef IOCTX_FLAG_SQWQ
44 #define IOCTX_FLAG_SQWQ         (1 << 4)
45 #endif
46 #ifndef IOCTX_FLAG_SQPOLL
47 #define IOCTX_FLAG_SQPOLL       (1 << 5)
48 #endif
49
50
51 /*
52  * io_ring_enter(2) flags
53  */
54 #ifndef IORING_FLAG_GETEVENTS
55 #define IORING_FLAG_GETEVENTS   (1 << 0)
56 #endif
57
58 typedef uint64_t u64;
59 typedef uint32_t u32;
60 typedef uint16_t u16;
61
62 #define IORING_SQ_NEED_WAKEUP   (1 << 0)
63
64 #define IOEV_RES2_CACHEHIT      (1 << 0)
65
66 struct aio_sq_ring {
67         union {
68                 struct {
69                         u32 head;
70                         u32 tail;
71                         u32 nr_events;
72                         u16 sq_thread_cpu;
73                         u16 kflags;
74                         u64 iocbs;
75                 };
76                 u32 pad[16];
77         };
78         u32 array[0];
79 };
80
81 struct aio_cq_ring {
82         union {
83                 struct {
84                         u32 head;
85                         u32 tail;
86                         u32 nr_events;
87                 };
88                 struct io_event pad;
89         };
90         struct io_event events[0];
91 };
92
93 struct aioring_data {
94         io_context_t aio_ctx;
95         struct io_u **io_us;
96         struct io_u **io_u_index;
97
98         struct aio_sq_ring *sq_ring;
99         struct iocb *iocbs;
100         unsigned sq_ring_mask;
101
102         struct aio_cq_ring *cq_ring;
103         struct io_event *events;
104         unsigned cq_ring_mask;
105
106         int queued;
107         int cq_ring_off;
108         unsigned iodepth;
109
110         uint64_t cachehit;
111         uint64_t cachemiss;
112 };
113
114 struct aioring_options {
115         void *pad;
116         unsigned int hipri;
117         unsigned int fixedbufs;
118         unsigned int sqthread;
119         unsigned int sqthread_set;
120         unsigned int sqthread_poll;
121         unsigned int sqwq;
122 };
123
124 static int fio_aioring_sqthread_cb(void *data,
125                                    unsigned long long *val)
126 {
127         struct aioring_options *o = data;
128
129         o->sqthread = *val;
130         o->sqthread_set = 1;
131         return 0;
132 }
133
134 static struct fio_option options[] = {
135         {
136                 .name   = "hipri",
137                 .lname  = "High Priority",
138                 .type   = FIO_OPT_STR_SET,
139                 .off1   = offsetof(struct aioring_options, hipri),
140                 .help   = "Use polled IO completions",
141                 .category = FIO_OPT_C_ENGINE,
142                 .group  = FIO_OPT_G_LIBAIO,
143         },
144         {
145                 .name   = "fixedbufs",
146                 .lname  = "Fixed (pre-mapped) IO buffers",
147                 .type   = FIO_OPT_STR_SET,
148                 .off1   = offsetof(struct aioring_options, fixedbufs),
149                 .help   = "Pre map IO buffers",
150                 .category = FIO_OPT_C_ENGINE,
151                 .group  = FIO_OPT_G_LIBAIO,
152         },
153         {
154                 .name   = "sqthread",
155                 .lname  = "Use kernel SQ thread on this CPU",
156                 .type   = FIO_OPT_INT,
157                 .cb     = fio_aioring_sqthread_cb,
158                 .help   = "Offload submission to kernel thread",
159                 .category = FIO_OPT_C_ENGINE,
160                 .group  = FIO_OPT_G_LIBAIO,
161         },
162         {
163                 .name   = "sqthread_poll",
164                 .lname  = "Kernel SQ thread should poll",
165                 .type   = FIO_OPT_STR_SET,
166                 .off1   = offsetof(struct aioring_options, sqthread_poll),
167                 .help   = "Used with sqthread, enables kernel side polling",
168                 .category = FIO_OPT_C_ENGINE,
169                 .group  = FIO_OPT_G_LIBAIO,
170         },
171         {
172                 .name   = "sqwq",
173                 .lname  = "Offload submission to kernel workqueue",
174                 .type   = FIO_OPT_STR_SET,
175                 .off1   = offsetof(struct aioring_options, sqwq),
176                 .help   = "Offload submission to kernel workqueue",
177                 .category = FIO_OPT_C_ENGINE,
178                 .group  = FIO_OPT_G_LIBAIO,
179         },
180         {
181                 .name   = NULL,
182         },
183 };
184
185 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
186                          unsigned int min_complete, unsigned int flags)
187 {
188         return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
189                         flags);
190 }
191
192 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
193 {
194         struct aioring_data *ld = td->io_ops_data;
195         struct fio_file *f = io_u->file;
196         struct aioring_options *o = td->eo;
197         struct iocb *iocb;
198
199         iocb = &ld->iocbs[io_u->index];
200
201         if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
202                 if (io_u->ddir == DDIR_READ)
203                         iocb->aio_lio_opcode = IO_CMD_PREAD;
204                 else
205                         iocb->aio_lio_opcode = IO_CMD_PWRITE;
206                 iocb->aio_reqprio = 0;
207                 iocb->aio_fildes = f->fd;
208                 iocb->u.c.buf = io_u->xfer_buf;
209                 iocb->u.c.nbytes = io_u->xfer_buflen;
210                 iocb->u.c.offset = io_u->offset;
211                 if (o->hipri)
212                         iocb->u.c.flags |= IOCB_FLAG_HIPRI;
213                 else
214                         iocb->u.c.flags = 0;
215         } else if (ddir_sync(io_u->ddir))
216                 io_prep_fsync(iocb, f->fd);
217
218         iocb->data = io_u;
219         return 0;
220 }
221
222 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
223 {
224         struct aioring_data *ld = td->io_ops_data;
225         struct io_event *ev;
226         struct io_u *io_u;
227         unsigned index;
228
229         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
230
231         ev = &ld->cq_ring->events[index];
232         io_u = ev->data;
233
234         if (ev->res != io_u->xfer_buflen) {
235                 if (ev->res > io_u->xfer_buflen)
236                         io_u->error = -ev->res;
237                 else
238                         io_u->resid = io_u->xfer_buflen - ev->res;
239         } else
240                 io_u->error = 0;
241
242         if (io_u->ddir == DDIR_READ) {
243                 if (ev->res2 & IOEV_RES2_CACHEHIT)
244                         ld->cachehit++;
245                 else
246                         ld->cachemiss++;
247         }
248
249         return io_u;
250 }
251
252 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
253                                    unsigned int max)
254 {
255         struct aioring_data *ld = td->io_ops_data;
256         struct aio_cq_ring *ring = ld->cq_ring;
257         u32 head, reaped = 0;
258
259         head = ring->head;
260         do {
261                 read_barrier();
262                 if (head == ring->tail)
263                         break;
264                 reaped++;
265                 head++;
266         } while (reaped + events < max);
267
268         ring->head = head;
269         write_barrier();
270         return reaped;
271 }
272
273 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
274                                  unsigned int max, const struct timespec *t)
275 {
276         struct aioring_data *ld = td->io_ops_data;
277         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
278         struct aioring_options *o = td->eo;
279         struct aio_cq_ring *ring = ld->cq_ring;
280         unsigned events = 0;
281         int r;
282
283         ld->cq_ring_off = ring->head;
284         do {
285                 r = fio_aioring_cqring_reap(td, events, max);
286                 if (r) {
287                         events += r;
288                         continue;
289                 }
290
291                 if (!o->sqthread_poll) {
292                         r = io_ring_enter(ld->aio_ctx, 0, actual_min,
293                                                 IORING_FLAG_GETEVENTS);
294                         if (r < 0) {
295                                 if (errno == EAGAIN)
296                                         continue;
297                                 td_verror(td, errno, "io_ring_enter get");
298                                 break;
299                         }
300                 }
301         } while (events < min);
302
303         return r < 0 ? r : events;
304 }
305
306 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
307                                            struct io_u *io_u)
308 {
309         struct aioring_data *ld = td->io_ops_data;
310         struct aio_sq_ring *ring = ld->sq_ring;
311         unsigned tail, next_tail;
312
313         fio_ro_check(td, io_u);
314
315         if (ld->queued == ld->iodepth)
316                 return FIO_Q_BUSY;
317
318         if (io_u->ddir == DDIR_TRIM) {
319                 if (ld->queued)
320                         return FIO_Q_BUSY;
321
322                 do_io_u_trim(td, io_u);
323                 io_u_mark_submit(td, 1);
324                 io_u_mark_complete(td, 1);
325                 return FIO_Q_COMPLETED;
326         }
327
328         tail = ring->tail;
329         next_tail = tail + 1;
330         read_barrier();
331         if (next_tail == ring->head)
332                 return FIO_Q_BUSY;
333
334         ring->array[tail & ld->sq_ring_mask] = io_u->index;
335         ring->tail = next_tail;
336         write_barrier();
337
338         ld->queued++;
339         return FIO_Q_QUEUED;
340 }
341
342 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
343 {
344         struct aioring_data *ld = td->io_ops_data;
345         struct timespec now;
346
347         if (!fio_fill_issue_time(td))
348                 return;
349
350         fio_gettime(&now, NULL);
351
352         while (nr--) {
353                 int index = ld->sq_ring->array[start & ld->sq_ring_mask];
354                 struct io_u *io_u = ld->io_u_index[index];
355
356                 memcpy(&io_u->issue_time, &now, sizeof(now));
357                 io_u_queued(td, io_u);
358
359                 start++;
360         }
361 }
362
363 static int fio_aioring_commit(struct thread_data *td)
364 {
365         struct aioring_data *ld = td->io_ops_data;
366         struct aioring_options *o = td->eo;
367         int ret;
368
369         if (!ld->queued)
370                 return 0;
371
372         /* Nothing to do */
373         if (o->sqthread_poll) {
374                 struct aio_sq_ring *ring = ld->sq_ring;
375
376                 if (ring->kflags & IORING_SQ_NEED_WAKEUP)
377                         io_ring_enter(ld->aio_ctx, ld->queued, 0, 0);
378                 ld->queued = 0;
379                 return 0;
380         }
381
382         do {
383                 unsigned start = ld->sq_ring->head;
384                 long nr = ld->queued;
385
386                 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_GETEVENTS);
387                 if (ret > 0) {
388                         fio_aioring_queued(td, start, ret);
389                         io_u_mark_submit(td, ret);
390
391                         ld->queued -= ret;
392                         ret = 0;
393                 } else if (!ret) {
394                         io_u_mark_submit(td, ret);
395                         continue;
396                 } else {
397                         if (errno == EAGAIN) {
398                                 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
399                                 if (ret)
400                                         continue;
401                                 /* Shouldn't happen */
402                                 usleep(1);
403                                 continue;
404                         }
405                         td_verror(td, errno, "io_ring_enter sumit");
406                         break;
407                 }
408         } while (ld->queued);
409
410         return ret;
411 }
412
413 static size_t aioring_cq_size(struct thread_data *td)
414 {
415         return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
416 }
417
418 static size_t aioring_sq_iocb(struct thread_data *td)
419 {
420         return sizeof(struct iocb) * td->o.iodepth;
421 }
422
423 static size_t aioring_sq_size(struct thread_data *td)
424 {
425         return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
426 }
427
428 static unsigned roundup_pow2(unsigned depth)
429 {
430         return 1UL << __fls(depth - 1);
431 }
432
433 static void fio_aioring_cleanup(struct thread_data *td)
434 {
435         struct aioring_data *ld = td->io_ops_data;
436
437         if (ld) {
438                 td->ts.cachehit += ld->cachehit;
439                 td->ts.cachemiss += ld->cachemiss;
440
441                 /*
442                  * Work-around to avoid huge RCU stalls at exit time. If we
443                  * don't do this here, then it'll be torn down by exit_aio().
444                  * But for that case we can parallellize the freeing, thus
445                  * speeding it up a lot.
446                  */
447                 if (!(td->flags & TD_F_CHILD))
448                         io_destroy(ld->aio_ctx);
449                 free(ld->io_u_index);
450                 free(ld->io_us);
451                 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
452                 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
453                 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
454                 free(ld);
455         }
456 }
457
458 static int fio_aioring_queue_init(struct thread_data *td)
459 {
460         struct aioring_data *ld = td->io_ops_data;
461         struct aioring_options *o = td->eo;
462         int flags = IOCTX_FLAG_SCQRING;
463         int depth = td->o.iodepth;
464
465         if (o->hipri)
466                 flags |= IOCTX_FLAG_IOPOLL;
467         if (o->sqthread_set) {
468                 ld->sq_ring->sq_thread_cpu = o->sqthread;
469                 flags |= IOCTX_FLAG_SQTHREAD;
470                 if (o->sqthread_poll)
471                         flags |= IOCTX_FLAG_SQPOLL;
472         }
473         if (o->sqwq)
474                 flags |= IOCTX_FLAG_SQWQ;
475
476         if (o->fixedbufs) {
477                 struct rlimit rlim = {
478                         .rlim_cur = RLIM_INFINITY,
479                         .rlim_max = RLIM_INFINITY,
480                 };
481
482                 setrlimit(RLIMIT_MEMLOCK, &rlim);
483                 flags |= IOCTX_FLAG_FIXEDBUFS;
484         }
485
486         return syscall(__NR_sys_io_setup2, depth, flags,
487                         ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
488 }
489
490 static int fio_aioring_post_init(struct thread_data *td)
491 {
492         struct aioring_data *ld = td->io_ops_data;
493         struct aioring_options *o = td->eo;
494         struct io_u *io_u;
495         struct iocb *iocb;
496         int err = 0;
497
498         if (o->fixedbufs) {
499                 int i;
500
501                 for (i = 0; i < td->o.iodepth; i++) {
502                         io_u = ld->io_u_index[i];
503                         iocb = &ld->iocbs[i];
504                         iocb->u.c.buf = io_u->buf;
505                         iocb->u.c.nbytes = td_max_bs(td);
506
507                         if (o->hipri)
508                                 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
509                 }
510         }
511
512         err = fio_aioring_queue_init(td);
513
514         if (err) {
515                 td_verror(td, errno, "io_queue_init");
516                 return 1;
517         }
518
519         return 0;
520 }
521
522 static int fio_aioring_init(struct thread_data *td)
523 {
524         struct aioring_data *ld;
525
526         ld = calloc(1, sizeof(*ld));
527
528         /* ring depth must be a power-of-2 */
529         ld->iodepth = td->o.iodepth;
530         td->o.iodepth = roundup_pow2(td->o.iodepth);
531
532         /* io_u index */
533         ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
534         ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
535
536         ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
537         memset(ld->iocbs, 0, aioring_sq_iocb(td));
538
539         ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
540         memset(ld->sq_ring, 0, aioring_sq_size(td));
541         ld->sq_ring->nr_events = td->o.iodepth;
542         ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
543         ld->sq_ring_mask = td->o.iodepth - 1;
544
545         ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
546         memset(ld->cq_ring, 0, aioring_cq_size(td));
547         ld->cq_ring->nr_events = td->o.iodepth * 2;
548         ld->cq_ring_mask = (2 * td->o.iodepth) - 1;
549
550         td->io_ops_data = ld;
551         return 0;
552 }
553
554 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
555 {
556         struct aioring_data *ld = td->io_ops_data;
557
558         ld->io_u_index[io_u->index] = io_u;
559         return 0;
560 }
561
562 static struct ioengine_ops ioengine = {
563         .name                   = "aio-ring",
564         .version                = FIO_IOOPS_VERSION,
565         .init                   = fio_aioring_init,
566         .post_init              = fio_aioring_post_init,
567         .io_u_init              = fio_aioring_io_u_init,
568         .prep                   = fio_aioring_prep,
569         .queue                  = fio_aioring_queue,
570         .commit                 = fio_aioring_commit,
571         .getevents              = fio_aioring_getevents,
572         .event                  = fio_aioring_event,
573         .cleanup                = fio_aioring_cleanup,
574         .open_file              = generic_open_file,
575         .close_file             = generic_close_file,
576         .get_file_size          = generic_get_file_size,
577         .options                = options,
578         .option_struct_size     = sizeof(struct aioring_options),
579 };
580
581 static void fio_init fio_aioring_register(void)
582 {
583         register_ioengine(&ioengine);
584 }
585
586 static void fio_exit fio_aioring_unregister(void)
587 {
588         unregister_ioengine(&ioengine);
589 }
590 #endif