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