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