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