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