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