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