t/io_uring: add support for registered files
[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;
3d7d00a3 76 unsigned int sqpoll_thread;
2ea53ca3
JA
77 unsigned int sqpoll_set;
78 unsigned int sqpoll_cpu;
52885fa2
JA
79};
80
2ea53ca3 81static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
a90cd050 82{
bffad86f 83 struct ioring_options *o = data;
a90cd050 84
2ea53ca3
JA
85 o->sqpoll_cpu = *val;
86 o->sqpoll_set = 1;
a90cd050
JA
87 return 0;
88}
89
52885fa2
JA
90static struct fio_option options[] = {
91 {
92 .name = "hipri",
93 .lname = "High Priority",
94 .type = FIO_OPT_STR_SET,
bffad86f 95 .off1 = offsetof(struct ioring_options, hipri),
52885fa2
JA
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,
bffad86f 104 .off1 = offsetof(struct ioring_options, fixedbufs),
52885fa2
JA
105 .help = "Pre map IO buffers",
106 .category = FIO_OPT_C_ENGINE,
107 .group = FIO_OPT_G_LIBAIO,
108 },
771c9901
JA
109 {
110 .name = "sqthread_poll",
3d7d00a3
JA
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",
2ea53ca3
JA
121 .type = FIO_OPT_INT,
122 .cb = fio_ioring_sqpoll_cb,
3d7d00a3 123 .help = "What CPU to run SQ thread polling on",
a90cd050
JA
124 .category = FIO_OPT_C_ENGINE,
125 .group = FIO_OPT_G_LIBAIO,
126 },
52885fa2
JA
127 {
128 .name = NULL,
129 },
130};
131
bffad86f 132static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
52885fa2
JA
133 unsigned int min_complete, unsigned int flags)
134{
9a2d78b3
JA
135 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
136 min_complete, flags);
52885fa2
JA
137}
138
bffad86f 139static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
52885fa2 140{
bffad86f 141 struct ioring_data *ld = td->io_ops_data;
cfcc8564 142 struct ioring_options *o = td->eo;
52885fa2 143 struct fio_file *f = io_u->file;
f0403f94 144 struct io_uring_sqe *sqe;
52885fa2 145
f0403f94
JA
146 sqe = &ld->sqes[io_u->index];
147 sqe->fd = f->fd;
148 sqe->flags = 0;
149 sqe->ioprio = 0;
2ea53ca3 150 sqe->buf_index = 0;
52885fa2 151
e3970057 152 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
2ea53ca3
JA
153 if (io_u->ddir == DDIR_READ)
154 sqe->opcode = IORING_OP_READV;
155 else
156 sqe->opcode = IORING_OP_WRITEV;
157
f0403f94 158 if (o->fixedbufs) {
2ea53ca3 159 sqe->flags |= IOSQE_FIXED_BUFFER;
f0403f94
JA
160 sqe->addr = io_u->xfer_buf;
161 sqe->len = io_u->xfer_buflen;
2ea53ca3 162 sqe->buf_index = io_u->index;
cfcc8564 163 } else {
f0403f94
JA
164 sqe->addr = &ld->iovecs[io_u->index];
165 sqe->len = 1;
cfcc8564 166 }
f0403f94 167 sqe->off = io_u->offset;
52885fa2 168 } else if (ddir_sync(io_u->ddir))
f0403f94 169 sqe->opcode = IORING_OP_FSYNC;
52885fa2 170
a7086591 171 sqe->data = (unsigned long) io_u;
52885fa2
JA
172 return 0;
173}
174
bffad86f 175static struct io_u *fio_ioring_event(struct thread_data *td, int event)
52885fa2 176{
bffad86f 177 struct ioring_data *ld = td->io_ops_data;
f0403f94 178 struct io_uring_cqe *cqe;
52885fa2 179 struct io_u *io_u;
b87aa01a 180 unsigned index;
52885fa2 181
b87aa01a 182 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
52885fa2 183
f0403f94 184 cqe = &ld->cq_ring.cqes[index];
a7086591 185 io_u = (struct io_u *) cqe->data;
52885fa2 186
f0403f94
JA
187 if (cqe->res != io_u->xfer_buflen) {
188 if (cqe->res > io_u->xfer_buflen)
189 io_u->error = -cqe->res;
52885fa2 190 else
f0403f94 191 io_u->resid = io_u->xfer_buflen - cqe->res;
52885fa2
JA
192 } else
193 io_u->error = 0;
194
96563db9 195 if (io_u->ddir == DDIR_READ) {
f0403f94 196 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
96563db9
JA
197 ld->cachehit++;
198 else
199 ld->cachemiss++;
200 }
201
52885fa2
JA
202 return io_u;
203}
204
bffad86f 205static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
52885fa2
JA
206 unsigned int max)
207{
bffad86f
JA
208 struct ioring_data *ld = td->io_ops_data;
209 struct io_cq_ring *ring = &ld->cq_ring;
e2239016 210 unsigned head, reaped = 0;
52885fa2 211
9a2d78b3 212 head = *ring->head;
52885fa2
JA
213 do {
214 read_barrier();
9a2d78b3 215 if (head == *ring->tail)
52885fa2
JA
216 break;
217 reaped++;
218 head++;
52885fa2
JA
219 } while (reaped + events < max);
220
9a2d78b3 221 *ring->head = head;
52885fa2
JA
222 write_barrier();
223 return reaped;
224}
225
bffad86f
JA
226static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
227 unsigned int max, const struct timespec *t)
52885fa2 228{
bffad86f 229 struct ioring_data *ld = td->io_ops_data;
52885fa2 230 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
bffad86f
JA
231 struct ioring_options *o = td->eo;
232 struct io_cq_ring *ring = &ld->cq_ring;
b87aa01a
JA
233 unsigned events = 0;
234 int r;
52885fa2 235
9a2d78b3 236 ld->cq_ring_off = *ring->head;
52885fa2 237 do {
bffad86f 238 r = fio_ioring_cqring_reap(td, events, max);
52885fa2
JA
239 if (r) {
240 events += r;
241 continue;
242 }
243
3d7d00a3 244 if (!o->sqpoll_thread) {
9a2d78b3
JA
245 r = io_uring_enter(ld, 0, actual_min,
246 IORING_ENTER_GETEVENTS);
771c9901
JA
247 if (r < 0) {
248 if (errno == EAGAIN)
249 continue;
9a2d78b3 250 td_verror(td, errno, "io_uring_enter");
771c9901
JA
251 break;
252 }
52885fa2
JA
253 }
254 } while (events < min);
255
256 return r < 0 ? r : events;
257}
258
bffad86f
JA
259static enum fio_q_status fio_ioring_queue(struct thread_data *td,
260 struct io_u *io_u)
52885fa2 261{
bffad86f
JA
262 struct ioring_data *ld = td->io_ops_data;
263 struct io_sq_ring *ring = &ld->sq_ring;
52885fa2
JA
264 unsigned tail, next_tail;
265
266 fio_ro_check(td, io_u);
267
b87aa01a 268 if (ld->queued == ld->iodepth)
52885fa2
JA
269 return FIO_Q_BUSY;
270
52885fa2
JA
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
9a2d78b3 281 tail = *ring->tail;
52885fa2 282 next_tail = tail + 1;
52885fa2 283 read_barrier();
9a2d78b3 284 if (next_tail == *ring->head)
52885fa2
JA
285 return FIO_Q_BUSY;
286
b87aa01a 287 ring->array[tail & ld->sq_ring_mask] = io_u->index;
9a2d78b3 288 *ring->tail = next_tail;
52885fa2
JA
289 write_barrier();
290
291 ld->queued++;
292 return FIO_Q_QUEUED;
293}
294
bffad86f 295static void fio_ioring_queued(struct thread_data *td, int start, int nr)
52885fa2 296{
bffad86f 297 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
298 struct timespec now;
299
300 if (!fio_fill_issue_time(td))
301 return;
302
303 fio_gettime(&now, NULL);
304
305 while (nr--) {
bffad86f 306 struct io_sq_ring *ring = &ld->sq_ring;
9a2d78b3 307 int index = ring->array[start & ld->sq_ring_mask];
f8289afc 308 struct io_u *io_u = ld->io_u_index[index];
52885fa2
JA
309
310 memcpy(&io_u->issue_time, &now, sizeof(now));
311 io_u_queued(td, io_u);
312
313 start++;
52885fa2
JA
314 }
315}
316
bffad86f 317static int fio_ioring_commit(struct thread_data *td)
52885fa2 318{
bffad86f
JA
319 struct ioring_data *ld = td->io_ops_data;
320 struct ioring_options *o = td->eo;
52885fa2
JA
321 int ret;
322
323 if (!ld->queued)
324 return 0;
325
3d7d00a3
JA
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) {
bffad86f 332 struct io_sq_ring *ring = &ld->sq_ring;
4cdbc048 333
2ea53ca3 334 read_barrier();
9a2d78b3
JA
335 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
336 io_uring_enter(ld, ld->queued, 0, 0);
771c9901
JA
337 ld->queued = 0;
338 return 0;
339 }
340
52885fa2 341 do {
9a2d78b3 342 unsigned start = *ld->sq_ring.head;
52885fa2
JA
343 long nr = ld->queued;
344
9a2d78b3 345 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
52885fa2 346 if (ret > 0) {
bffad86f 347 fio_ioring_queued(td, start, ret);
52885fa2
JA
348 io_u_mark_submit(td, ret);
349
350 ld->queued -= ret;
351 ret = 0;
a90cd050
JA
352 } else if (!ret) {
353 io_u_mark_submit(td, ret);
52885fa2 354 continue;
a90cd050
JA
355 } else {
356 if (errno == EAGAIN) {
bffad86f 357 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
a90cd050
JA
358 if (ret)
359 continue;
360 /* Shouldn't happen */
361 usleep(1);
362 continue;
52885fa2 363 }
9a2d78b3 364 td_verror(td, errno, "io_uring_enter submit");
52885fa2 365 break;
a90cd050 366 }
52885fa2
JA
367 } while (ld->queued);
368
369 return ret;
370}
371
bffad86f 372static void fio_ioring_unmap(struct ioring_data *ld)
52885fa2 373{
9a2d78b3 374 int i;
52885fa2 375
9a2d78b3
JA
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);
b87aa01a
JA
379}
380
bffad86f 381static void fio_ioring_cleanup(struct thread_data *td)
52885fa2 382{
bffad86f 383 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
384
385 if (ld) {
96563db9
JA
386 td->ts.cachehit += ld->cachehit;
387 td->ts.cachemiss += ld->cachemiss;
388
52885fa2 389 if (!(td->flags & TD_F_CHILD))
bffad86f 390 fio_ioring_unmap(ld);
9a2d78b3 391
52885fa2
JA
392 free(ld->io_u_index);
393 free(ld->io_us);
9a2d78b3 394 free(ld->iovecs);
52885fa2
JA
395 free(ld);
396 }
397}
398
bffad86f 399static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
9a2d78b3 400{
bffad86f
JA
401 struct io_sq_ring *sring = &ld->sq_ring;
402 struct io_cq_ring *cring = &ld->cq_ring;
9a2d78b3
JA
403 void *ptr;
404
e2239016 405 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
9a2d78b3
JA
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;
ac122fea 415 sring->array = ptr + p->sq_off.array;
9a2d78b3
JA
416 ld->sq_ring_mask = *sring->ring_mask;
417
f0403f94
JA
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,
9a2d78b3 420 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
f0403f94
JA
421 IORING_OFF_SQES);
422 ld->mmap[1].ptr = ld->sqes;
9a2d78b3 423
f0403f94
JA
424 ld->mmap[2].len = p->cq_off.cqes +
425 p->cq_entries * sizeof(struct io_uring_cqe);
9a2d78b3
JA
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;
f0403f94 434 cring->cqes = ptr + p->cq_off.cqes;
9a2d78b3
JA
435 ld->cq_ring_mask = *cring->ring_mask;
436 return 0;
437}
438
bffad86f 439static int fio_ioring_queue_init(struct thread_data *td)
52885fa2 440{
bffad86f
JA
441 struct ioring_data *ld = td->io_ops_data;
442 struct ioring_options *o = td->eo;
52885fa2 443 int depth = td->o.iodepth;
bffad86f 444 struct io_uring_params p;
9a2d78b3
JA
445 int ret;
446
447 memset(&p, 0, sizeof(p));
52885fa2
JA
448
449 if (o->hipri)
bffad86f 450 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3
JA
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 }
f635f1fb 457 }
a90cd050 458
52885fa2
JA
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);
52885fa2
JA
466 }
467
2ea53ca3 468 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
9a2d78b3
JA
469 if (ret < 0)
470 return ret;
471
472 ld->ring_fd = ret;
2ea53ca3
JA
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
bffad86f 486 return fio_ioring_mmap(ld, &p);
52885fa2
JA
487}
488
bffad86f 489static int fio_ioring_post_init(struct thread_data *td)
52885fa2 490{
bffad86f 491 struct ioring_data *ld = td->io_ops_data;
52885fa2 492 struct io_u *io_u;
650346e1 493 int err, i;
52885fa2 494
650346e1
JA
495 for (i = 0; i < td->o.iodepth; i++) {
496 struct iovec *iov = &ld->iovecs[i];
9a2d78b3 497
650346e1
JA
498 io_u = ld->io_u_index[i];
499 iov->iov_base = io_u->buf;
500 iov->iov_len = td_max_bs(td);
52885fa2
JA
501 }
502
bffad86f 503 err = fio_ioring_queue_init(td);
52885fa2 504 if (err) {
d63a472d 505 td_verror(td, errno, "io_queue_init");
52885fa2
JA
506 return 1;
507 }
508
509 return 0;
510}
511
9a2d78b3
JA
512static unsigned roundup_pow2(unsigned depth)
513{
514 return 1UL << __fls(depth - 1);
515}
516
bffad86f 517static int fio_ioring_init(struct thread_data *td)
52885fa2 518{
bffad86f 519 struct ioring_data *ld;
52885fa2 520
52885fa2
JA
521 ld = calloc(1, sizeof(*ld));
522
b87aa01a
JA
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
52885fa2
JA
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 *));
650346e1 530 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
52885fa2
JA
531
532 td->io_ops_data = ld;
533 return 0;
534}
535
bffad86f 536static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
52885fa2 537{
bffad86f 538 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
539
540 ld->io_u_index[io_u->index] = io_u;
541 return 0;
542}
543
544static struct ioengine_ops ioengine = {
bffad86f 545 .name = "io_uring",
52885fa2 546 .version = FIO_IOOPS_VERSION,
bffad86f
JA
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,
52885fa2
JA
556 .open_file = generic_open_file,
557 .close_file = generic_close_file,
558 .get_file_size = generic_get_file_size,
559 .options = options,
bffad86f 560 .option_struct_size = sizeof(struct ioring_options),
52885fa2
JA
561};
562
bffad86f 563static void fio_init fio_ioring_register(void)
52885fa2 564{
52885fa2 565 register_ioengine(&ioengine);
52885fa2
JA
566}
567
bffad86f 568static void fio_exit fio_ioring_unregister(void)
52885fa2 569{
52885fa2 570 unregister_ioengine(&ioengine);
52885fa2 571}
1f90e9bb 572#endif