t/io_uring: remember to set p->sq_thread_cpu
[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) {
f0403f94 153 if (o->fixedbufs) {
48e698fa
JA
154 if (io_u->ddir == DDIR_READ)
155 sqe->opcode = IORING_OP_READ_FIXED;
156 else
157 sqe->opcode = IORING_OP_WRITE_FIXED;
f0403f94
JA
158 sqe->addr = io_u->xfer_buf;
159 sqe->len = io_u->xfer_buflen;
2ea53ca3 160 sqe->buf_index = io_u->index;
cfcc8564 161 } else {
48e698fa
JA
162 if (io_u->ddir == DDIR_READ)
163 sqe->opcode = IORING_OP_READV;
164 else
165 sqe->opcode = IORING_OP_WRITEV;
f0403f94
JA
166 sqe->addr = &ld->iovecs[io_u->index];
167 sqe->len = 1;
cfcc8564 168 }
f0403f94 169 sqe->off = io_u->offset;
48e698fa
JA
170 } else if (ddir_sync(io_u->ddir)) {
171 sqe->fsync_flags = 0;
172 if (io_u->ddir == DDIR_DATASYNC)
173 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
f0403f94 174 sqe->opcode = IORING_OP_FSYNC;
48e698fa 175 }
52885fa2 176
48e698fa 177 sqe->user_data = (unsigned long) io_u;
52885fa2
JA
178 return 0;
179}
180
bffad86f 181static struct io_u *fio_ioring_event(struct thread_data *td, int event)
52885fa2 182{
bffad86f 183 struct ioring_data *ld = td->io_ops_data;
f0403f94 184 struct io_uring_cqe *cqe;
52885fa2 185 struct io_u *io_u;
b87aa01a 186 unsigned index;
52885fa2 187
b87aa01a 188 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
52885fa2 189
f0403f94 190 cqe = &ld->cq_ring.cqes[index];
48e698fa 191 io_u = (struct io_u *) cqe->user_data;
52885fa2 192
f0403f94
JA
193 if (cqe->res != io_u->xfer_buflen) {
194 if (cqe->res > io_u->xfer_buflen)
195 io_u->error = -cqe->res;
52885fa2 196 else
f0403f94 197 io_u->resid = io_u->xfer_buflen - cqe->res;
52885fa2
JA
198 } else
199 io_u->error = 0;
200
96563db9 201 if (io_u->ddir == DDIR_READ) {
f0403f94 202 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
96563db9
JA
203 ld->cachehit++;
204 else
205 ld->cachemiss++;
206 }
207
52885fa2
JA
208 return io_u;
209}
210
bffad86f 211static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
52885fa2
JA
212 unsigned int max)
213{
bffad86f
JA
214 struct ioring_data *ld = td->io_ops_data;
215 struct io_cq_ring *ring = &ld->cq_ring;
e2239016 216 unsigned head, reaped = 0;
52885fa2 217
9a2d78b3 218 head = *ring->head;
52885fa2
JA
219 do {
220 read_barrier();
9a2d78b3 221 if (head == *ring->tail)
52885fa2
JA
222 break;
223 reaped++;
224 head++;
52885fa2
JA
225 } while (reaped + events < max);
226
9a2d78b3 227 *ring->head = head;
52885fa2
JA
228 write_barrier();
229 return reaped;
230}
231
bffad86f
JA
232static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
233 unsigned int max, const struct timespec *t)
52885fa2 234{
bffad86f 235 struct ioring_data *ld = td->io_ops_data;
52885fa2 236 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
bffad86f
JA
237 struct ioring_options *o = td->eo;
238 struct io_cq_ring *ring = &ld->cq_ring;
b87aa01a
JA
239 unsigned events = 0;
240 int r;
52885fa2 241
9a2d78b3 242 ld->cq_ring_off = *ring->head;
52885fa2 243 do {
bffad86f 244 r = fio_ioring_cqring_reap(td, events, max);
52885fa2
JA
245 if (r) {
246 events += r;
247 continue;
248 }
249
3d7d00a3 250 if (!o->sqpoll_thread) {
9a2d78b3
JA
251 r = io_uring_enter(ld, 0, actual_min,
252 IORING_ENTER_GETEVENTS);
771c9901
JA
253 if (r < 0) {
254 if (errno == EAGAIN)
255 continue;
9a2d78b3 256 td_verror(td, errno, "io_uring_enter");
771c9901
JA
257 break;
258 }
52885fa2
JA
259 }
260 } while (events < min);
261
262 return r < 0 ? r : events;
263}
264
bffad86f
JA
265static enum fio_q_status fio_ioring_queue(struct thread_data *td,
266 struct io_u *io_u)
52885fa2 267{
bffad86f
JA
268 struct ioring_data *ld = td->io_ops_data;
269 struct io_sq_ring *ring = &ld->sq_ring;
52885fa2
JA
270 unsigned tail, next_tail;
271
272 fio_ro_check(td, io_u);
273
b87aa01a 274 if (ld->queued == ld->iodepth)
52885fa2
JA
275 return FIO_Q_BUSY;
276
52885fa2
JA
277 if (io_u->ddir == DDIR_TRIM) {
278 if (ld->queued)
279 return FIO_Q_BUSY;
280
281 do_io_u_trim(td, io_u);
282 io_u_mark_submit(td, 1);
283 io_u_mark_complete(td, 1);
284 return FIO_Q_COMPLETED;
285 }
286
9a2d78b3 287 tail = *ring->tail;
52885fa2 288 next_tail = tail + 1;
52885fa2 289 read_barrier();
9a2d78b3 290 if (next_tail == *ring->head)
52885fa2
JA
291 return FIO_Q_BUSY;
292
b87aa01a 293 ring->array[tail & ld->sq_ring_mask] = io_u->index;
9a2d78b3 294 *ring->tail = next_tail;
52885fa2
JA
295 write_barrier();
296
297 ld->queued++;
298 return FIO_Q_QUEUED;
299}
300
bffad86f 301static void fio_ioring_queued(struct thread_data *td, int start, int nr)
52885fa2 302{
bffad86f 303 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
304 struct timespec now;
305
306 if (!fio_fill_issue_time(td))
307 return;
308
309 fio_gettime(&now, NULL);
310
311 while (nr--) {
bffad86f 312 struct io_sq_ring *ring = &ld->sq_ring;
9a2d78b3 313 int index = ring->array[start & ld->sq_ring_mask];
f8289afc 314 struct io_u *io_u = ld->io_u_index[index];
52885fa2
JA
315
316 memcpy(&io_u->issue_time, &now, sizeof(now));
317 io_u_queued(td, io_u);
318
319 start++;
52885fa2
JA
320 }
321}
322
bffad86f 323static int fio_ioring_commit(struct thread_data *td)
52885fa2 324{
bffad86f
JA
325 struct ioring_data *ld = td->io_ops_data;
326 struct ioring_options *o = td->eo;
52885fa2
JA
327 int ret;
328
329 if (!ld->queued)
330 return 0;
331
3d7d00a3
JA
332 /*
333 * Kernel side does submission. just need to check if the ring is
334 * flagged as needing a kick, if so, call io_uring_enter(). This
335 * only happens if we've been idle too long.
336 */
337 if (o->sqpoll_thread) {
bffad86f 338 struct io_sq_ring *ring = &ld->sq_ring;
4cdbc048 339
2ea53ca3 340 read_barrier();
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;
bffad86f 450 struct io_uring_params p;
9a2d78b3
JA
451 int ret;
452
453 memset(&p, 0, sizeof(p));
52885fa2
JA
454
455 if (o->hipri)
bffad86f 456 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3
JA
457 if (o->sqpoll_thread) {
458 p.flags |= IORING_SETUP_SQPOLL;
459 if (o->sqpoll_set) {
460 p.flags |= IORING_SETUP_SQ_AFF;
461 p.sq_thread_cpu = o->sqpoll_cpu;
462 }
f635f1fb 463 }
a90cd050 464
52885fa2
JA
465 if (o->fixedbufs) {
466 struct rlimit rlim = {
467 .rlim_cur = RLIM_INFINITY,
468 .rlim_max = RLIM_INFINITY,
469 };
470
471 setrlimit(RLIMIT_MEMLOCK, &rlim);
52885fa2
JA
472 }
473
2ea53ca3 474 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
9a2d78b3
JA
475 if (ret < 0)
476 return ret;
477
478 ld->ring_fd = ret;
2ea53ca3
JA
479
480 if (o->fixedbufs) {
481 struct io_uring_register_buffers reg = {
482 .iovecs = ld->iovecs,
483 .nr_iovecs = depth
484 };
485
486 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
487 IORING_REGISTER_BUFFERS, &reg);
488 if (ret < 0)
489 return ret;
490 }
491
bffad86f 492 return fio_ioring_mmap(ld, &p);
52885fa2
JA
493}
494
bffad86f 495static int fio_ioring_post_init(struct thread_data *td)
52885fa2 496{
bffad86f 497 struct ioring_data *ld = td->io_ops_data;
52885fa2 498 struct io_u *io_u;
650346e1 499 int err, i;
52885fa2 500
650346e1
JA
501 for (i = 0; i < td->o.iodepth; i++) {
502 struct iovec *iov = &ld->iovecs[i];
9a2d78b3 503
650346e1
JA
504 io_u = ld->io_u_index[i];
505 iov->iov_base = io_u->buf;
506 iov->iov_len = td_max_bs(td);
52885fa2
JA
507 }
508
bffad86f 509 err = fio_ioring_queue_init(td);
52885fa2 510 if (err) {
d63a472d 511 td_verror(td, errno, "io_queue_init");
52885fa2
JA
512 return 1;
513 }
514
515 return 0;
516}
517
9a2d78b3
JA
518static unsigned roundup_pow2(unsigned depth)
519{
520 return 1UL << __fls(depth - 1);
521}
522
bffad86f 523static int fio_ioring_init(struct thread_data *td)
52885fa2 524{
bffad86f 525 struct ioring_data *ld;
52885fa2 526
52885fa2
JA
527 ld = calloc(1, sizeof(*ld));
528
b87aa01a
JA
529 /* ring depth must be a power-of-2 */
530 ld->iodepth = td->o.iodepth;
531 td->o.iodepth = roundup_pow2(td->o.iodepth);
532
52885fa2
JA
533 /* io_u index */
534 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
535 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
650346e1 536 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
52885fa2
JA
537
538 td->io_ops_data = ld;
539 return 0;
540}
541
bffad86f 542static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
52885fa2 543{
bffad86f 544 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
545
546 ld->io_u_index[io_u->index] = io_u;
547 return 0;
548}
549
550static struct ioengine_ops ioengine = {
bffad86f 551 .name = "io_uring",
52885fa2 552 .version = FIO_IOOPS_VERSION,
bffad86f
JA
553 .init = fio_ioring_init,
554 .post_init = fio_ioring_post_init,
555 .io_u_init = fio_ioring_io_u_init,
556 .prep = fio_ioring_prep,
557 .queue = fio_ioring_queue,
558 .commit = fio_ioring_commit,
559 .getevents = fio_ioring_getevents,
560 .event = fio_ioring_event,
561 .cleanup = fio_ioring_cleanup,
52885fa2
JA
562 .open_file = generic_open_file,
563 .close_file = generic_close_file,
564 .get_file_size = generic_get_file_size,
565 .options = options,
bffad86f 566 .option_struct_size = sizeof(struct ioring_options),
52885fa2
JA
567};
568
bffad86f 569static void fio_init fio_ioring_register(void)
52885fa2 570{
52885fa2 571 register_ioengine(&ioengine);
52885fa2
JA
572}
573
bffad86f 574static void fio_exit fio_ioring_unregister(void)
52885fa2 575{
52885fa2 576 unregister_ioengine(&ioengine);
52885fa2 577}
1f90e9bb 578#endif