io_uring: cleanup sq thread poll/cpu setup
[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 sqpoll_thread;
77         unsigned int sqpoll_set;
78         unsigned int sqpoll_cpu;
79 };
80
81 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
82 {
83         struct ioring_options *o = data;
84
85         o->sqpoll_cpu = *val;
86         o->sqpoll_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_poll",
111                 .lname  = "Kernel SQ thread polling",
112                 .type   = FIO_OPT_INT,
113                 .off1   = offsetof(struct ioring_options, sqpoll_thread),
114                 .help   = "Offload submission/completion to kernel thread",
115                 .category = FIO_OPT_C_ENGINE,
116                 .group  = FIO_OPT_G_LIBAIO,
117         },
118         {
119                 .name   = "sqthread_poll_cpu",
120                 .lname  = "SQ Thread Poll CPU",
121                 .type   = FIO_OPT_INT,
122                 .cb     = fio_ioring_sqpoll_cb,
123                 .help   = "What CPU to run SQ thread polling on",
124                 .category = FIO_OPT_C_ENGINE,
125                 .group  = FIO_OPT_G_LIBAIO,
126         },
127         {
128                 .name   = NULL,
129         },
130 };
131
132 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
133                          unsigned int min_complete, unsigned int flags)
134 {
135         return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
136                         min_complete, flags);
137 }
138
139 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
140 {
141         struct ioring_data *ld = td->io_ops_data;
142         struct ioring_options *o = td->eo;
143         struct fio_file *f = io_u->file;
144         struct io_uring_sqe *sqe;
145
146         sqe = &ld->sqes[io_u->index];
147         sqe->fd = f->fd;
148         sqe->flags = 0;
149         sqe->ioprio = 0;
150         sqe->buf_index = 0;
151
152         if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
153                 if (io_u->ddir == DDIR_READ)
154                         sqe->opcode = IORING_OP_READV;
155                 else
156                         sqe->opcode = IORING_OP_WRITEV;
157
158                 if (o->fixedbufs) {
159                         sqe->flags |= IOSQE_FIXED_BUFFER;
160                         sqe->addr = io_u->xfer_buf;
161                         sqe->len = io_u->xfer_buflen;
162                         sqe->buf_index = io_u->index;
163                 } else {
164                         sqe->addr = &ld->iovecs[io_u->index];
165                         sqe->len = 1;
166                 }
167                 sqe->off = io_u->offset;
168         } else if (ddir_sync(io_u->ddir))
169                 sqe->opcode = IORING_OP_FSYNC;
170
171         sqe->data = (unsigned long) io_u;
172         return 0;
173 }
174
175 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
176 {
177         struct ioring_data *ld = td->io_ops_data;
178         struct io_uring_cqe *cqe;
179         struct io_u *io_u;
180         unsigned index;
181
182         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
183
184         cqe = &ld->cq_ring.cqes[index];
185         io_u = (struct io_u *) cqe->data;
186
187         if (cqe->res != io_u->xfer_buflen) {
188                 if (cqe->res > io_u->xfer_buflen)
189                         io_u->error = -cqe->res;
190                 else
191                         io_u->resid = io_u->xfer_buflen - cqe->res;
192         } else
193                 io_u->error = 0;
194
195         if (io_u->ddir == DDIR_READ) {
196                 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
197                         ld->cachehit++;
198                 else
199                         ld->cachemiss++;
200         }
201
202         return io_u;
203 }
204
205 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
206                                    unsigned int max)
207 {
208         struct ioring_data *ld = td->io_ops_data;
209         struct io_cq_ring *ring = &ld->cq_ring;
210         unsigned head, reaped = 0;
211
212         head = *ring->head;
213         do {
214                 read_barrier();
215                 if (head == *ring->tail)
216                         break;
217                 reaped++;
218                 head++;
219         } while (reaped + events < max);
220
221         *ring->head = head;
222         write_barrier();
223         return reaped;
224 }
225
226 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
227                                 unsigned int max, const struct timespec *t)
228 {
229         struct ioring_data *ld = td->io_ops_data;
230         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
231         struct ioring_options *o = td->eo;
232         struct io_cq_ring *ring = &ld->cq_ring;
233         unsigned events = 0;
234         int r;
235
236         ld->cq_ring_off = *ring->head;
237         do {
238                 r = fio_ioring_cqring_reap(td, events, max);
239                 if (r) {
240                         events += r;
241                         continue;
242                 }
243
244                 if (!o->sqpoll_thread) {
245                         r = io_uring_enter(ld, 0, actual_min,
246                                                 IORING_ENTER_GETEVENTS);
247                         if (r < 0) {
248                                 if (errno == EAGAIN)
249                                         continue;
250                                 td_verror(td, errno, "io_uring_enter");
251                                 break;
252                         }
253                 }
254         } while (events < min);
255
256         return r < 0 ? r : events;
257 }
258
259 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
260                                           struct io_u *io_u)
261 {
262         struct ioring_data *ld = td->io_ops_data;
263         struct io_sq_ring *ring = &ld->sq_ring;
264         unsigned tail, next_tail;
265
266         fio_ro_check(td, io_u);
267
268         if (ld->queued == ld->iodepth)
269                 return FIO_Q_BUSY;
270
271         if (io_u->ddir == DDIR_TRIM) {
272                 if (ld->queued)
273                         return FIO_Q_BUSY;
274
275                 do_io_u_trim(td, io_u);
276                 io_u_mark_submit(td, 1);
277                 io_u_mark_complete(td, 1);
278                 return FIO_Q_COMPLETED;
279         }
280
281         tail = *ring->tail;
282         next_tail = tail + 1;
283         read_barrier();
284         if (next_tail == *ring->head)
285                 return FIO_Q_BUSY;
286
287         ring->array[tail & ld->sq_ring_mask] = io_u->index;
288         *ring->tail = next_tail;
289         write_barrier();
290
291         ld->queued++;
292         return FIO_Q_QUEUED;
293 }
294
295 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
296 {
297         struct ioring_data *ld = td->io_ops_data;
298         struct timespec now;
299
300         if (!fio_fill_issue_time(td))
301                 return;
302
303         fio_gettime(&now, NULL);
304
305         while (nr--) {
306                 struct io_sq_ring *ring = &ld->sq_ring;
307                 int index = ring->array[start & ld->sq_ring_mask];
308                 struct io_u *io_u = ld->io_u_index[index];
309
310                 memcpy(&io_u->issue_time, &now, sizeof(now));
311                 io_u_queued(td, io_u);
312
313                 start++;
314         }
315 }
316
317 static int fio_ioring_commit(struct thread_data *td)
318 {
319         struct ioring_data *ld = td->io_ops_data;
320         struct ioring_options *o = td->eo;
321         int ret;
322
323         if (!ld->queued)
324                 return 0;
325
326         /*
327          * Kernel side does submission. just need to check if the ring is
328          * flagged as needing a kick, if so, call io_uring_enter(). This
329          * only happens if we've been idle too long.
330          */
331         if (o->sqpoll_thread) {
332                 struct io_sq_ring *ring = &ld->sq_ring;
333
334                 read_barrier();
335                 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
336                         io_uring_enter(ld, ld->queued, 0, 0);
337                 ld->queued = 0;
338                 return 0;
339         }
340
341         do {
342                 unsigned start = *ld->sq_ring.head;
343                 long nr = ld->queued;
344
345                 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
346                 if (ret > 0) {
347                         fio_ioring_queued(td, start, ret);
348                         io_u_mark_submit(td, ret);
349
350                         ld->queued -= ret;
351                         ret = 0;
352                 } else if (!ret) {
353                         io_u_mark_submit(td, ret);
354                         continue;
355                 } else {
356                         if (errno == EAGAIN) {
357                                 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
358                                 if (ret)
359                                         continue;
360                                 /* Shouldn't happen */
361                                 usleep(1);
362                                 continue;
363                         }
364                         td_verror(td, errno, "io_uring_enter submit");
365                         break;
366                 }
367         } while (ld->queued);
368
369         return ret;
370 }
371
372 static void fio_ioring_unmap(struct ioring_data *ld)
373 {
374         int i;
375
376         for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
377                 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
378         close(ld->ring_fd);
379 }
380
381 static void fio_ioring_cleanup(struct thread_data *td)
382 {
383         struct ioring_data *ld = td->io_ops_data;
384
385         if (ld) {
386                 td->ts.cachehit += ld->cachehit;
387                 td->ts.cachemiss += ld->cachemiss;
388
389                 if (!(td->flags & TD_F_CHILD))
390                         fio_ioring_unmap(ld);
391
392                 free(ld->io_u_index);
393                 free(ld->io_us);
394                 free(ld->iovecs);
395                 free(ld);
396         }
397 }
398
399 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
400 {
401         struct io_sq_ring *sring = &ld->sq_ring;
402         struct io_cq_ring *cring = &ld->cq_ring;
403         void *ptr;
404
405         ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
406         ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
407                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
408                         IORING_OFF_SQ_RING);
409         ld->mmap[0].ptr = ptr;
410         sring->head = ptr + p->sq_off.head;
411         sring->tail = ptr + p->sq_off.tail;
412         sring->ring_mask = ptr + p->sq_off.ring_mask;
413         sring->ring_entries = ptr + p->sq_off.ring_entries;
414         sring->flags = ptr + p->sq_off.flags;
415         sring->array = ptr + p->sq_off.array;
416         ld->sq_ring_mask = *sring->ring_mask;
417
418         ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
419         ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
420                                 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
421                                 IORING_OFF_SQES);
422         ld->mmap[1].ptr = ld->sqes;
423
424         ld->mmap[2].len = p->cq_off.cqes +
425                                 p->cq_entries * sizeof(struct io_uring_cqe);
426         ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
427                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
428                         IORING_OFF_CQ_RING);
429         ld->mmap[2].ptr = ptr;
430         cring->head = ptr + p->cq_off.head;
431         cring->tail = ptr + p->cq_off.tail;
432         cring->ring_mask = ptr + p->cq_off.ring_mask;
433         cring->ring_entries = ptr + p->cq_off.ring_entries;
434         cring->cqes = ptr + p->cq_off.cqes;
435         ld->cq_ring_mask = *cring->ring_mask;
436         return 0;
437 }
438
439 static int fio_ioring_queue_init(struct thread_data *td)
440 {
441         struct ioring_data *ld = td->io_ops_data;
442         struct ioring_options *o = td->eo;
443         int depth = td->o.iodepth;
444         struct io_uring_params p;
445         int ret;
446
447         memset(&p, 0, sizeof(p));
448
449         if (o->hipri)
450                 p.flags |= IORING_SETUP_IOPOLL;
451         if (o->sqpoll_thread) {
452                 p.flags |= IORING_SETUP_SQPOLL;
453                 if (o->sqpoll_set) {
454                         p.flags |= IORING_SETUP_SQ_AFF;
455                         p.sq_thread_cpu = o->sqpoll_cpu;
456                 }
457         }
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         }
467
468         ret = syscall(__NR_sys_io_uring_setup, depth, &p);
469         if (ret < 0)
470                 return ret;
471
472         ld->ring_fd = ret;
473
474         if (o->fixedbufs) {
475                 struct io_uring_register_buffers reg = {
476                         .iovecs = ld->iovecs,
477                         .nr_iovecs = depth
478                 };
479
480                 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
481                                 IORING_REGISTER_BUFFERS, &reg);
482                 if (ret < 0)
483                         return ret;
484         }
485
486         return fio_ioring_mmap(ld, &p);
487 }
488
489 static int fio_ioring_post_init(struct thread_data *td)
490 {
491         struct ioring_data *ld = td->io_ops_data;
492         struct io_u *io_u;
493         int err, i;
494
495         for (i = 0; i < td->o.iodepth; i++) {
496                 struct iovec *iov = &ld->iovecs[i];
497
498                 io_u = ld->io_u_index[i];
499                 iov->iov_base = io_u->buf;
500                 iov->iov_len = td_max_bs(td);
501         }
502
503         err = fio_ioring_queue_init(td);
504         if (err) {
505                 td_verror(td, errno, "io_queue_init");
506                 return 1;
507         }
508
509         return 0;
510 }
511
512 static unsigned roundup_pow2(unsigned depth)
513 {
514         return 1UL << __fls(depth - 1);
515 }
516
517 static int fio_ioring_init(struct thread_data *td)
518 {
519         struct ioring_data *ld;
520
521         ld = calloc(1, sizeof(*ld));
522
523         /* ring depth must be a power-of-2 */
524         ld->iodepth = td->o.iodepth;
525         td->o.iodepth = roundup_pow2(td->o.iodepth);
526
527         /* io_u index */
528         ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
529         ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
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