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