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