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"
20 #include "../lib/roundup.h"
22 #ifdef ARCH_HAVE_IOURING
24 #include "../lib/types.h"
25 #include "../os/linux/io_uring.h"
31 unsigned *ring_entries;
40 unsigned *ring_entries;
41 struct io_uring_cqe *cqes;
52 struct io_u **io_u_index;
56 struct io_sq_ring sq_ring;
57 struct io_uring_sqe *sqes;
59 unsigned sq_ring_mask;
61 struct io_cq_ring cq_ring;
62 unsigned cq_ring_mask;
67 bool ioprio_class_set;
70 struct ioring_mmap mmap[3];
73 struct ioring_options {
76 unsigned int cmdprio_percentage;
77 unsigned int fixedbufs;
78 unsigned int registerfiles;
79 unsigned int sqpoll_thread;
80 unsigned int sqpoll_set;
81 unsigned int sqpoll_cpu;
82 unsigned int nonvectored;
83 unsigned int uncached;
87 static const int ddir_to_op[2][2] = {
88 { IORING_OP_READV, IORING_OP_READ },
89 { IORING_OP_WRITEV, IORING_OP_WRITE }
92 static const int fixed_ddir_to_op[2] = {
97 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
99 struct ioring_options *o = data;
101 o->sqpoll_cpu = *val;
106 static struct fio_option options[] = {
109 .lname = "High Priority",
110 .type = FIO_OPT_STR_SET,
111 .off1 = offsetof(struct ioring_options, hipri),
112 .help = "Use polled IO completions",
113 .category = FIO_OPT_C_ENGINE,
114 .group = FIO_OPT_G_IOURING,
116 #ifdef FIO_HAVE_IOPRIO_CLASS
118 .name = "cmdprio_percentage",
119 .lname = "high priority percentage",
121 .off1 = offsetof(struct ioring_options, cmdprio_percentage),
124 .help = "Send high priority I/O this percentage of the time",
125 .category = FIO_OPT_C_ENGINE,
126 .group = FIO_OPT_G_IOURING,
130 .name = "cmdprio_percentage",
131 .lname = "high priority percentage",
132 .type = FIO_OPT_UNSUPPORTED,
133 .help = "Your platform does not support I/O priority classes",
138 .lname = "Fixed (pre-mapped) IO buffers",
139 .type = FIO_OPT_STR_SET,
140 .off1 = offsetof(struct ioring_options, fixedbufs),
141 .help = "Pre map IO buffers",
142 .category = FIO_OPT_C_ENGINE,
143 .group = FIO_OPT_G_IOURING,
146 .name = "registerfiles",
147 .lname = "Register file set",
148 .type = FIO_OPT_STR_SET,
149 .off1 = offsetof(struct ioring_options, registerfiles),
150 .help = "Pre-open/register files",
151 .category = FIO_OPT_C_ENGINE,
152 .group = FIO_OPT_G_IOURING,
155 .name = "sqthread_poll",
156 .lname = "Kernel SQ thread polling",
158 .off1 = offsetof(struct ioring_options, sqpoll_thread),
159 .help = "Offload submission/completion to kernel thread",
160 .category = FIO_OPT_C_ENGINE,
161 .group = FIO_OPT_G_IOURING,
164 .name = "sqthread_poll_cpu",
165 .lname = "SQ Thread Poll CPU",
167 .cb = fio_ioring_sqpoll_cb,
168 .help = "What CPU to run SQ thread polling on",
169 .category = FIO_OPT_C_ENGINE,
170 .group = FIO_OPT_G_IOURING,
173 .name = "nonvectored",
174 .lname = "Non-vectored",
176 .off1 = offsetof(struct ioring_options, nonvectored),
178 .help = "Use non-vectored read/write commands",
179 .category = FIO_OPT_C_ENGINE,
180 .group = FIO_OPT_G_IOURING,
186 .off1 = offsetof(struct ioring_options, uncached),
187 .help = "Use RWF_UNCACHED for buffered read/writes",
188 .category = FIO_OPT_C_ENGINE,
189 .group = FIO_OPT_G_IOURING,
193 .lname = "RWF_NOWAIT",
194 .type = FIO_OPT_BOOL,
195 .off1 = offsetof(struct ioring_options, nowait),
196 .help = "Use RWF_NOWAIT for reads/writes",
197 .category = FIO_OPT_C_ENGINE,
198 .group = FIO_OPT_G_IOURING,
205 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
206 unsigned int min_complete, unsigned int flags)
208 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
209 min_complete, flags, NULL, 0);
212 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
214 struct ioring_data *ld = td->io_ops_data;
215 struct ioring_options *o = td->eo;
216 struct fio_file *f = io_u->file;
217 struct io_uring_sqe *sqe;
219 sqe = &ld->sqes[io_u->index];
221 if (o->registerfiles) {
222 sqe->fd = f->engine_pos;
223 sqe->flags = IOSQE_FIXED_FILE;
228 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
230 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
231 sqe->addr = (unsigned long) io_u->xfer_buf;
232 sqe->len = io_u->xfer_buflen;
233 sqe->buf_index = io_u->index;
235 struct iovec *iov = &ld->iovecs[io_u->index];
238 * Update based on actual io_u, requeue could have
241 iov->iov_base = io_u->xfer_buf;
242 iov->iov_len = io_u->xfer_buflen;
244 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
245 if (o->nonvectored) {
246 sqe->addr = (unsigned long) iov->iov_base;
247 sqe->len = iov->iov_len;
249 sqe->addr = (unsigned long) iov;
253 if (!td->o.odirect && o->uncached)
254 sqe->rw_flags = RWF_UNCACHED;
256 sqe->rw_flags |= RWF_NOWAIT;
257 if (ld->ioprio_class_set)
258 sqe->ioprio = td->o.ioprio_class << 13;
260 sqe->ioprio |= td->o.ioprio;
261 sqe->off = io_u->offset;
263 } else if (ddir_sync(io_u->ddir)) {
265 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
266 sqe->off = f->first_write;
267 sqe->len = f->last_write - f->first_write;
268 sqe->sync_range_flags = td->o.sync_file_range;
269 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
274 if (io_u->ddir == DDIR_DATASYNC)
275 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
276 sqe->opcode = IORING_OP_FSYNC;
280 sqe->user_data = (unsigned long) io_u;
284 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
286 struct ioring_data *ld = td->io_ops_data;
287 struct io_uring_cqe *cqe;
291 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
293 cqe = &ld->cq_ring.cqes[index];
294 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
296 if (cqe->res != io_u->xfer_buflen) {
297 if (cqe->res > io_u->xfer_buflen)
298 io_u->error = -cqe->res;
300 io_u->resid = io_u->xfer_buflen - cqe->res;
307 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
310 struct ioring_data *ld = td->io_ops_data;
311 struct io_cq_ring *ring = &ld->cq_ring;
312 unsigned head, reaped = 0;
316 if (head == atomic_load_acquire(ring->tail))
320 } while (reaped + events < max);
323 atomic_store_release(ring->head, head);
328 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
329 unsigned int max, const struct timespec *t)
331 struct ioring_data *ld = td->io_ops_data;
332 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
333 struct ioring_options *o = td->eo;
334 struct io_cq_ring *ring = &ld->cq_ring;
338 ld->cq_ring_off = *ring->head;
340 r = fio_ioring_cqring_reap(td, events, max);
348 if (!o->sqpoll_thread) {
349 r = io_uring_enter(ld, 0, actual_min,
350 IORING_ENTER_GETEVENTS);
352 if (errno == EAGAIN || errno == EINTR)
354 td_verror(td, errno, "io_uring_enter");
358 } while (events < min);
360 return r < 0 ? r : events;
363 static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
365 struct ioring_options *o = td->eo;
366 struct ioring_data *ld = td->io_ops_data;
367 if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
368 ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
369 io_u->flags |= IO_U_F_PRIORITY;
374 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
377 struct ioring_data *ld = td->io_ops_data;
378 struct io_sq_ring *ring = &ld->sq_ring;
379 struct ioring_options *o = td->eo;
380 unsigned tail, next_tail;
382 fio_ro_check(td, io_u);
384 if (ld->queued == ld->iodepth)
387 if (io_u->ddir == DDIR_TRIM) {
391 do_io_u_trim(td, io_u);
392 io_u_mark_submit(td, 1);
393 io_u_mark_complete(td, 1);
394 return FIO_Q_COMPLETED;
398 next_tail = tail + 1;
399 if (next_tail == atomic_load_acquire(ring->head))
402 if (o->cmdprio_percentage)
403 fio_ioring_prio_prep(td, io_u);
404 ring->array[tail & ld->sq_ring_mask] = io_u->index;
405 atomic_store_release(ring->tail, next_tail);
411 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
413 struct ioring_data *ld = td->io_ops_data;
416 if (!fio_fill_issue_time(td))
419 fio_gettime(&now, NULL);
422 struct io_sq_ring *ring = &ld->sq_ring;
423 int index = ring->array[start & ld->sq_ring_mask];
424 struct io_u *io_u = ld->io_u_index[index];
426 memcpy(&io_u->issue_time, &now, sizeof(now));
427 io_u_queued(td, io_u);
433 static int fio_ioring_commit(struct thread_data *td)
435 struct ioring_data *ld = td->io_ops_data;
436 struct ioring_options *o = td->eo;
443 * Kernel side does submission. just need to check if the ring is
444 * flagged as needing a kick, if so, call io_uring_enter(). This
445 * only happens if we've been idle too long.
447 if (o->sqpoll_thread) {
448 struct io_sq_ring *ring = &ld->sq_ring;
451 flags = atomic_load_acquire(ring->flags);
452 if (flags & IORING_SQ_NEED_WAKEUP)
453 io_uring_enter(ld, ld->queued, 0,
454 IORING_ENTER_SQ_WAKEUP);
460 unsigned start = *ld->sq_ring.head;
461 long nr = ld->queued;
463 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
465 fio_ioring_queued(td, start, ret);
466 io_u_mark_submit(td, ret);
471 io_u_mark_submit(td, ret);
474 if (errno == EAGAIN || errno == EINTR) {
475 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
478 /* Shouldn't happen */
482 td_verror(td, errno, "io_uring_enter submit");
485 } while (ld->queued);
490 static void fio_ioring_unmap(struct ioring_data *ld)
494 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
495 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
499 static void fio_ioring_cleanup(struct thread_data *td)
501 struct ioring_data *ld = td->io_ops_data;
504 if (!(td->flags & TD_F_CHILD))
505 fio_ioring_unmap(ld);
507 free(ld->io_u_index);
514 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
516 struct io_sq_ring *sring = &ld->sq_ring;
517 struct io_cq_ring *cring = &ld->cq_ring;
520 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
521 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
522 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
524 ld->mmap[0].ptr = ptr;
525 sring->head = ptr + p->sq_off.head;
526 sring->tail = ptr + p->sq_off.tail;
527 sring->ring_mask = ptr + p->sq_off.ring_mask;
528 sring->ring_entries = ptr + p->sq_off.ring_entries;
529 sring->flags = ptr + p->sq_off.flags;
530 sring->array = ptr + p->sq_off.array;
531 ld->sq_ring_mask = *sring->ring_mask;
533 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
534 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
535 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
537 ld->mmap[1].ptr = ld->sqes;
539 ld->mmap[2].len = p->cq_off.cqes +
540 p->cq_entries * sizeof(struct io_uring_cqe);
541 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
542 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
544 ld->mmap[2].ptr = ptr;
545 cring->head = ptr + p->cq_off.head;
546 cring->tail = ptr + p->cq_off.tail;
547 cring->ring_mask = ptr + p->cq_off.ring_mask;
548 cring->ring_entries = ptr + p->cq_off.ring_entries;
549 cring->cqes = ptr + p->cq_off.cqes;
550 ld->cq_ring_mask = *cring->ring_mask;
554 static void fio_ioring_probe(struct thread_data *td)
556 struct ioring_data *ld = td->io_ops_data;
557 struct ioring_options *o = td->eo;
558 struct io_uring_probe *p;
561 /* already set by user, don't touch */
562 if (o->nonvectored != -1)
565 /* default to off, as that's always safe */
568 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
572 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
573 ret = syscall(__NR_io_uring_register, ld->ring_fd,
574 IORING_REGISTER_PROBE, p, 256);
578 if (IORING_OP_WRITE > p->ops_len)
581 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
582 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
588 static int fio_ioring_queue_init(struct thread_data *td)
590 struct ioring_data *ld = td->io_ops_data;
591 struct ioring_options *o = td->eo;
592 int depth = td->o.iodepth;
593 struct io_uring_params p;
596 memset(&p, 0, sizeof(p));
599 p.flags |= IORING_SETUP_IOPOLL;
600 if (o->sqpoll_thread) {
601 p.flags |= IORING_SETUP_SQPOLL;
603 p.flags |= IORING_SETUP_SQ_AFF;
604 p.sq_thread_cpu = o->sqpoll_cpu;
608 ret = syscall(__NR_io_uring_setup, depth, &p);
614 fio_ioring_probe(td);
617 ret = syscall(__NR_io_uring_register, ld->ring_fd,
618 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
623 return fio_ioring_mmap(ld, &p);
626 static int fio_ioring_register_files(struct thread_data *td)
628 struct ioring_data *ld = td->io_ops_data;
633 ld->fds = calloc(td->o.nr_files, sizeof(int));
635 for_each_file(td, f, i) {
636 ret = generic_open_file(td, f);
643 ret = syscall(__NR_io_uring_register, ld->ring_fd,
644 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
652 * Pretend the file is closed again, and really close it if we hit
655 for_each_file(td, f, i) {
658 ret2 = generic_close_file(td, f);
666 static int fio_ioring_post_init(struct thread_data *td)
668 struct ioring_data *ld = td->io_ops_data;
669 struct ioring_options *o = td->eo;
673 for (i = 0; i < td->o.iodepth; i++) {
674 struct iovec *iov = &ld->iovecs[i];
676 io_u = ld->io_u_index[i];
677 iov->iov_base = io_u->buf;
678 iov->iov_len = td_max_bs(td);
681 err = fio_ioring_queue_init(td);
683 td_verror(td, errno, "io_queue_init");
687 for (i = 0; i < td->o.iodepth; i++) {
688 struct io_uring_sqe *sqe;
691 memset(sqe, 0, sizeof(*sqe));
694 if (o->registerfiles) {
695 err = fio_ioring_register_files(td);
697 td_verror(td, errno, "ioring_register_files");
705 static int fio_ioring_init(struct thread_data *td)
707 struct ioring_options *o = td->eo;
708 struct ioring_data *ld;
709 struct thread_options *to = &td->o;
711 /* sqthread submission requires registered files */
712 if (o->sqpoll_thread)
713 o->registerfiles = 1;
715 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
716 log_err("fio: io_uring registered files require nr_files to "
717 "be identical to open_files\n");
721 ld = calloc(1, sizeof(*ld));
723 /* ring depth must be a power-of-2 */
724 ld->iodepth = td->o.iodepth;
725 td->o.iodepth = roundup_pow2(td->o.iodepth);
728 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
729 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
731 td->io_ops_data = ld;
734 * Check for option conflicts
736 if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
737 o->cmdprio_percentage != 0) {
738 log_err("%s: cmdprio_percentage option and mutually exclusive "
739 "prio or prioclass option is set, exiting\n", to->name);
740 td_verror(td, EINVAL, "fio_io_uring_init");
744 if (fio_option_is_set(&td->o, ioprio_class))
745 ld->ioprio_class_set = true;
746 if (fio_option_is_set(&td->o, ioprio))
747 ld->ioprio_set = true;
752 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
754 struct ioring_data *ld = td->io_ops_data;
756 ld->io_u_index[io_u->index] = io_u;
760 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
762 struct ioring_data *ld = td->io_ops_data;
763 struct ioring_options *o = td->eo;
765 if (!ld || !o->registerfiles)
766 return generic_open_file(td, f);
768 f->fd = ld->fds[f->engine_pos];
772 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
774 struct ioring_data *ld = td->io_ops_data;
775 struct ioring_options *o = td->eo;
777 if (!ld || !o->registerfiles)
778 return generic_close_file(td, f);
784 static struct ioengine_ops ioengine = {
786 .version = FIO_IOOPS_VERSION,
787 .flags = FIO_ASYNCIO_SYNC_TRIM,
788 .init = fio_ioring_init,
789 .post_init = fio_ioring_post_init,
790 .io_u_init = fio_ioring_io_u_init,
791 .prep = fio_ioring_prep,
792 .queue = fio_ioring_queue,
793 .commit = fio_ioring_commit,
794 .getevents = fio_ioring_getevents,
795 .event = fio_ioring_event,
796 .cleanup = fio_ioring_cleanup,
797 .open_file = fio_ioring_open_file,
798 .close_file = fio_ioring_close_file,
799 .get_file_size = generic_get_file_size,
801 .option_struct_size = sizeof(struct ioring_options),
804 static void fio_init fio_ioring_register(void)
806 register_ioengine(&ioengine);
809 static void fio_exit fio_ioring_unregister(void)
811 unregister_ioengine(&ioengine);