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