4 * IO engine using the new native Linux aio io_uring interface. See:
6 * http://git.kernel.dk/cgit/linux-block/log/?h=io_uring
13 #include <sys/resource.h>
16 #include "../lib/pow2.h"
17 #include "../optgroup.h"
18 #include "../lib/memalign.h"
19 #include "../lib/fls.h"
21 #ifdef ARCH_HAVE_IOURING
23 #include "../lib/types.h"
24 #include "../os/io_uring.h"
30 unsigned *ring_entries;
39 unsigned *ring_entries;
40 struct io_uring_cqe *cqes;
52 struct io_u **io_u_index;
54 struct io_sq_ring sq_ring;
55 struct io_uring_sqe *sqes;
57 unsigned sq_ring_mask;
59 struct io_cq_ring cq_ring;
60 unsigned cq_ring_mask;
69 struct ioring_mmap mmap[3];
72 struct ioring_options {
75 unsigned int fixedbufs;
76 unsigned int sqthread;
77 unsigned int sqthread_set;
78 unsigned int sqthread_poll;
82 static int fio_ioring_sqthread_cb(void *data, unsigned long long *val)
84 struct ioring_options *o = data;
91 static struct fio_option options[] = {
94 .lname = "High Priority",
95 .type = FIO_OPT_STR_SET,
96 .off1 = offsetof(struct ioring_options, hipri),
97 .help = "Use polled IO completions",
98 .category = FIO_OPT_C_ENGINE,
99 .group = FIO_OPT_G_LIBAIO,
103 .lname = "Fixed (pre-mapped) IO buffers",
104 .type = FIO_OPT_STR_SET,
105 .off1 = offsetof(struct ioring_options, fixedbufs),
106 .help = "Pre map IO buffers",
107 .category = FIO_OPT_C_ENGINE,
108 .group = FIO_OPT_G_LIBAIO,
112 .lname = "Use kernel SQ thread on this CPU",
114 .cb = fio_ioring_sqthread_cb,
115 .help = "Offload submission to kernel thread",
116 .category = FIO_OPT_C_ENGINE,
117 .group = FIO_OPT_G_LIBAIO,
120 .name = "sqthread_poll",
121 .lname = "Kernel SQ thread should poll",
122 .type = FIO_OPT_STR_SET,
123 .off1 = offsetof(struct ioring_options, sqthread_poll),
124 .help = "Used with sqthread, enables kernel side polling",
125 .category = FIO_OPT_C_ENGINE,
126 .group = FIO_OPT_G_LIBAIO,
130 .lname = "Offload submission to kernel workqueue",
131 .type = FIO_OPT_STR_SET,
132 .off1 = offsetof(struct ioring_options, sqwq),
133 .help = "Offload submission to kernel workqueue",
134 .category = FIO_OPT_C_ENGINE,
135 .group = FIO_OPT_G_LIBAIO,
142 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
143 unsigned int min_complete, unsigned int flags)
145 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
146 min_complete, flags);
149 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
151 struct ioring_data *ld = td->io_ops_data;
152 struct ioring_options *o = td->eo;
153 struct fio_file *f = io_u->file;
154 struct io_uring_sqe *sqe;
156 sqe = &ld->sqes[io_u->index];
161 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
163 if (io_u->ddir == DDIR_READ)
164 sqe->opcode = IORING_OP_READ_FIXED;
166 sqe->opcode = IORING_OP_WRITE_FIXED;
167 sqe->addr = io_u->xfer_buf;
168 sqe->len = io_u->xfer_buflen;
169 sqe->index = io_u->index;
171 if (io_u->ddir == DDIR_READ)
172 sqe->opcode = IORING_OP_READV;
174 sqe->opcode = IORING_OP_WRITEV;
175 sqe->addr = &ld->iovecs[io_u->index];
178 sqe->off = io_u->offset;
179 } else if (ddir_sync(io_u->ddir))
180 sqe->opcode = IORING_OP_FSYNC;
182 sqe->data = (unsigned long) io_u;
186 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
188 struct ioring_data *ld = td->io_ops_data;
189 struct io_uring_cqe *cqe;
193 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
195 cqe = &ld->cq_ring.cqes[index];
196 io_u = (struct io_u *) cqe->data;
198 if (cqe->res != io_u->xfer_buflen) {
199 if (cqe->res > io_u->xfer_buflen)
200 io_u->error = -cqe->res;
202 io_u->resid = io_u->xfer_buflen - cqe->res;
206 if (io_u->ddir == DDIR_READ) {
207 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
216 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
219 struct ioring_data *ld = td->io_ops_data;
220 struct io_cq_ring *ring = &ld->cq_ring;
221 unsigned head, reaped = 0;
226 if (head == *ring->tail)
230 } while (reaped + events < max);
237 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
238 unsigned int max, const struct timespec *t)
240 struct ioring_data *ld = td->io_ops_data;
241 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
242 struct ioring_options *o = td->eo;
243 struct io_cq_ring *ring = &ld->cq_ring;
247 ld->cq_ring_off = *ring->head;
249 r = fio_ioring_cqring_reap(td, events, max);
255 if (!o->sqthread_poll) {
256 r = io_uring_enter(ld, 0, actual_min,
257 IORING_ENTER_GETEVENTS);
261 td_verror(td, errno, "io_uring_enter");
265 } while (events < min);
267 return r < 0 ? r : events;
270 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
273 struct ioring_data *ld = td->io_ops_data;
274 struct io_sq_ring *ring = &ld->sq_ring;
275 unsigned tail, next_tail;
277 fio_ro_check(td, io_u);
279 if (ld->queued == ld->iodepth)
282 if (io_u->ddir == DDIR_TRIM) {
286 do_io_u_trim(td, io_u);
287 io_u_mark_submit(td, 1);
288 io_u_mark_complete(td, 1);
289 return FIO_Q_COMPLETED;
293 next_tail = tail + 1;
295 if (next_tail == *ring->head)
298 ring->array[tail & ld->sq_ring_mask] = io_u->index;
299 *ring->tail = next_tail;
306 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
308 struct ioring_data *ld = td->io_ops_data;
311 if (!fio_fill_issue_time(td))
314 fio_gettime(&now, NULL);
317 struct io_sq_ring *ring = &ld->sq_ring;
318 int index = ring->array[start & ld->sq_ring_mask];
319 struct io_u *io_u = ld->io_u_index[index];
321 memcpy(&io_u->issue_time, &now, sizeof(now));
322 io_u_queued(td, io_u);
328 static int fio_ioring_commit(struct thread_data *td)
330 struct ioring_data *ld = td->io_ops_data;
331 struct ioring_options *o = td->eo;
338 if (o->sqthread_poll) {
339 struct io_sq_ring *ring = &ld->sq_ring;
341 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
342 io_uring_enter(ld, ld->queued, 0, 0);
348 unsigned start = *ld->sq_ring.head;
349 long nr = ld->queued;
351 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
353 fio_ioring_queued(td, start, ret);
354 io_u_mark_submit(td, ret);
359 io_u_mark_submit(td, ret);
362 if (errno == EAGAIN) {
363 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
366 /* Shouldn't happen */
370 td_verror(td, errno, "io_uring_enter submit");
373 } while (ld->queued);
378 static void fio_ioring_unmap(struct ioring_data *ld)
382 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
383 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
387 static void fio_ioring_cleanup(struct thread_data *td)
389 struct ioring_data *ld = td->io_ops_data;
392 td->ts.cachehit += ld->cachehit;
393 td->ts.cachemiss += ld->cachemiss;
395 if (!(td->flags & TD_F_CHILD))
396 fio_ioring_unmap(ld);
398 free(ld->io_u_index);
405 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
407 struct io_sq_ring *sring = &ld->sq_ring;
408 struct io_cq_ring *cring = &ld->cq_ring;
411 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
412 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
413 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
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;
421 sring->array = ptr + p->sq_off.array;
422 ld->sq_ring_mask = *sring->ring_mask;
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,
426 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
428 ld->mmap[1].ptr = ld->sqes;
430 ld->mmap[2].len = p->cq_off.cqes +
431 p->cq_entries * sizeof(struct io_uring_cqe);
432 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
433 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
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;
440 cring->cqes = ptr + p->cq_off.cqes;
441 ld->cq_ring_mask = *cring->ring_mask;
445 static int fio_ioring_queue_init(struct thread_data *td)
447 struct ioring_data *ld = td->io_ops_data;
448 struct ioring_options *o = td->eo;
449 int depth = td->o.iodepth;
450 struct iovec *vecs = NULL;
451 struct io_uring_params p;
454 memset(&p, 0, sizeof(p));
457 p.flags |= IORING_SETUP_IOPOLL;
458 if (o->sqthread_set) {
459 p.sq_thread_cpu = o->sqthread;
460 p.flags |= IORING_SETUP_SQTHREAD;
461 if (o->sqthread_poll)
462 p.flags |= IORING_SETUP_SQPOLL;
465 p.flags |= IORING_SETUP_SQWQ;
468 struct rlimit rlim = {
469 .rlim_cur = RLIM_INFINITY,
470 .rlim_max = RLIM_INFINITY,
473 setrlimit(RLIMIT_MEMLOCK, &rlim);
477 ret = syscall(__NR_sys_io_uring_setup, depth, vecs, &p);
482 return fio_ioring_mmap(ld, &p);
485 static int fio_ioring_post_init(struct thread_data *td)
487 struct ioring_data *ld = td->io_ops_data;
491 for (i = 0; i < td->o.iodepth; i++) {
492 struct iovec *iov = &ld->iovecs[i];
494 io_u = ld->io_u_index[i];
495 iov->iov_base = io_u->buf;
496 iov->iov_len = td_max_bs(td);
499 err = fio_ioring_queue_init(td);
501 td_verror(td, errno, "io_queue_init");
508 static unsigned roundup_pow2(unsigned depth)
510 return 1UL << __fls(depth - 1);
513 static int fio_ioring_init(struct thread_data *td)
515 struct ioring_data *ld;
517 ld = calloc(1, sizeof(*ld));
519 /* ring depth must be a power-of-2 */
520 ld->iodepth = td->o.iodepth;
521 td->o.iodepth = roundup_pow2(td->o.iodepth);
524 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
525 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
526 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
528 td->io_ops_data = ld;
532 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
534 struct ioring_data *ld = td->io_ops_data;
536 ld->io_u_index[io_u->index] = io_u;
540 static struct ioengine_ops ioengine = {
542 .version = FIO_IOOPS_VERSION,
543 .init = fio_ioring_init,
544 .post_init = fio_ioring_post_init,
545 .io_u_init = fio_ioring_io_u_init,
546 .prep = fio_ioring_prep,
547 .queue = fio_ioring_queue,
548 .commit = fio_ioring_commit,
549 .getevents = fio_ioring_getevents,
550 .event = fio_ioring_event,
551 .cleanup = fio_ioring_cleanup,
552 .open_file = generic_open_file,
553 .close_file = generic_close_file,
554 .get_file_size = generic_get_file_size,
556 .option_struct_size = sizeof(struct ioring_options),
559 static void fio_init fio_ioring_register(void)
561 register_ioengine(&ioengine);
564 static void fio_exit fio_ioring_unregister(void)
566 unregister_ioengine(&ioengine);