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;
71 struct ioring_mmap mmap[3];
74 struct ioring_options {
77 unsigned int cmdprio_percentage;
78 unsigned int fixedbufs;
79 unsigned int registerfiles;
80 unsigned int sqpoll_thread;
81 unsigned int sqpoll_set;
82 unsigned int sqpoll_cpu;
83 unsigned int nonvectored;
84 unsigned int uncached;
86 unsigned int force_async;
89 static const int ddir_to_op[2][2] = {
90 { IORING_OP_READV, IORING_OP_READ },
91 { IORING_OP_WRITEV, IORING_OP_WRITE }
94 static const int fixed_ddir_to_op[2] = {
99 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
101 struct ioring_options *o = data;
103 o->sqpoll_cpu = *val;
108 static struct fio_option options[] = {
111 .lname = "High Priority",
112 .type = FIO_OPT_STR_SET,
113 .off1 = offsetof(struct ioring_options, hipri),
114 .help = "Use polled IO completions",
115 .category = FIO_OPT_C_ENGINE,
116 .group = FIO_OPT_G_IOURING,
118 #ifdef FIO_HAVE_IOPRIO_CLASS
120 .name = "cmdprio_percentage",
121 .lname = "high priority percentage",
123 .off1 = offsetof(struct ioring_options, cmdprio_percentage),
126 .help = "Send high priority I/O this percentage of the time",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_IOURING,
132 .name = "cmdprio_percentage",
133 .lname = "high priority percentage",
134 .type = FIO_OPT_UNSUPPORTED,
135 .help = "Your platform does not support I/O priority classes",
140 .lname = "Fixed (pre-mapped) IO buffers",
141 .type = FIO_OPT_STR_SET,
142 .off1 = offsetof(struct ioring_options, fixedbufs),
143 .help = "Pre map IO buffers",
144 .category = FIO_OPT_C_ENGINE,
145 .group = FIO_OPT_G_IOURING,
148 .name = "registerfiles",
149 .lname = "Register file set",
150 .type = FIO_OPT_STR_SET,
151 .off1 = offsetof(struct ioring_options, registerfiles),
152 .help = "Pre-open/register files",
153 .category = FIO_OPT_C_ENGINE,
154 .group = FIO_OPT_G_IOURING,
157 .name = "sqthread_poll",
158 .lname = "Kernel SQ thread polling",
160 .off1 = offsetof(struct ioring_options, sqpoll_thread),
161 .help = "Offload submission/completion to kernel thread",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_IOURING,
166 .name = "sqthread_poll_cpu",
167 .lname = "SQ Thread Poll CPU",
169 .cb = fio_ioring_sqpoll_cb,
170 .help = "What CPU to run SQ thread polling on",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_IOURING,
175 .name = "nonvectored",
176 .lname = "Non-vectored",
178 .off1 = offsetof(struct ioring_options, nonvectored),
180 .help = "Use non-vectored read/write commands",
181 .category = FIO_OPT_C_ENGINE,
182 .group = FIO_OPT_G_IOURING,
188 .off1 = offsetof(struct ioring_options, uncached),
189 .help = "Use RWF_UNCACHED for buffered read/writes",
190 .category = FIO_OPT_C_ENGINE,
191 .group = FIO_OPT_G_IOURING,
195 .lname = "RWF_NOWAIT",
196 .type = FIO_OPT_BOOL,
197 .off1 = offsetof(struct ioring_options, nowait),
198 .help = "Use RWF_NOWAIT for reads/writes",
199 .category = FIO_OPT_C_ENGINE,
200 .group = FIO_OPT_G_IOURING,
203 .name = "force_async",
204 .lname = "Force async",
206 .off1 = offsetof(struct ioring_options, force_async),
207 .help = "Set IOSQE_ASYNC every N requests",
208 .category = FIO_OPT_C_ENGINE,
209 .group = FIO_OPT_G_IOURING,
216 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
217 unsigned int min_complete, unsigned int flags)
219 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
220 min_complete, flags, NULL, 0);
223 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
225 struct ioring_data *ld = td->io_ops_data;
226 struct ioring_options *o = td->eo;
227 struct fio_file *f = io_u->file;
228 struct io_uring_sqe *sqe;
230 sqe = &ld->sqes[io_u->index];
232 if (o->registerfiles) {
233 sqe->fd = f->engine_pos;
234 sqe->flags = IOSQE_FIXED_FILE;
239 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
241 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
242 sqe->addr = (unsigned long) io_u->xfer_buf;
243 sqe->len = io_u->xfer_buflen;
244 sqe->buf_index = io_u->index;
246 struct iovec *iov = &ld->iovecs[io_u->index];
249 * Update based on actual io_u, requeue could have
252 iov->iov_base = io_u->xfer_buf;
253 iov->iov_len = io_u->xfer_buflen;
255 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
256 if (o->nonvectored) {
257 sqe->addr = (unsigned long) iov->iov_base;
258 sqe->len = iov->iov_len;
260 sqe->addr = (unsigned long) iov;
264 if (!td->o.odirect && o->uncached)
265 sqe->rw_flags = RWF_UNCACHED;
267 sqe->rw_flags |= RWF_NOWAIT;
268 if (ld->ioprio_class_set)
269 sqe->ioprio = td->o.ioprio_class << 13;
271 sqe->ioprio |= td->o.ioprio;
272 sqe->off = io_u->offset;
274 } else if (ddir_sync(io_u->ddir)) {
276 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
277 sqe->off = f->first_write;
278 sqe->len = f->last_write - f->first_write;
279 sqe->sync_range_flags = td->o.sync_file_range;
280 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
285 if (io_u->ddir == DDIR_DATASYNC)
286 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
287 sqe->opcode = IORING_OP_FSYNC;
291 if (o->force_async && ++ld->prepped == o->force_async) {
293 sqe->flags |= IOSQE_ASYNC;
296 sqe->user_data = (unsigned long) io_u;
300 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
302 struct ioring_data *ld = td->io_ops_data;
303 struct io_uring_cqe *cqe;
307 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
309 cqe = &ld->cq_ring.cqes[index];
310 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
312 if (cqe->res != io_u->xfer_buflen) {
313 if (cqe->res > io_u->xfer_buflen)
314 io_u->error = -cqe->res;
316 io_u->resid = io_u->xfer_buflen - cqe->res;
323 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
326 struct ioring_data *ld = td->io_ops_data;
327 struct io_cq_ring *ring = &ld->cq_ring;
328 unsigned head, reaped = 0;
332 if (head == atomic_load_acquire(ring->tail))
336 } while (reaped + events < max);
339 atomic_store_release(ring->head, head);
344 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
345 unsigned int max, const struct timespec *t)
347 struct ioring_data *ld = td->io_ops_data;
348 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
349 struct ioring_options *o = td->eo;
350 struct io_cq_ring *ring = &ld->cq_ring;
354 ld->cq_ring_off = *ring->head;
356 r = fio_ioring_cqring_reap(td, events, max);
364 if (!o->sqpoll_thread) {
365 r = io_uring_enter(ld, 0, actual_min,
366 IORING_ENTER_GETEVENTS);
368 if (errno == EAGAIN || errno == EINTR)
370 td_verror(td, errno, "io_uring_enter");
374 } while (events < min);
376 return r < 0 ? r : events;
379 static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
381 struct ioring_options *o = td->eo;
382 struct ioring_data *ld = td->io_ops_data;
383 if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
384 ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
385 io_u->flags |= IO_U_F_PRIORITY;
390 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
393 struct ioring_data *ld = td->io_ops_data;
394 struct io_sq_ring *ring = &ld->sq_ring;
395 struct ioring_options *o = td->eo;
396 unsigned tail, next_tail;
398 fio_ro_check(td, io_u);
400 if (ld->queued == ld->iodepth)
403 if (io_u->ddir == DDIR_TRIM) {
407 do_io_u_trim(td, io_u);
408 io_u_mark_submit(td, 1);
409 io_u_mark_complete(td, 1);
410 return FIO_Q_COMPLETED;
414 next_tail = tail + 1;
415 if (next_tail == atomic_load_acquire(ring->head))
418 if (o->cmdprio_percentage)
419 fio_ioring_prio_prep(td, io_u);
420 ring->array[tail & ld->sq_ring_mask] = io_u->index;
421 atomic_store_release(ring->tail, next_tail);
427 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
429 struct ioring_data *ld = td->io_ops_data;
432 if (!fio_fill_issue_time(td))
435 fio_gettime(&now, NULL);
438 struct io_sq_ring *ring = &ld->sq_ring;
439 int index = ring->array[start & ld->sq_ring_mask];
440 struct io_u *io_u = ld->io_u_index[index];
442 memcpy(&io_u->issue_time, &now, sizeof(now));
443 io_u_queued(td, io_u);
449 static int fio_ioring_commit(struct thread_data *td)
451 struct ioring_data *ld = td->io_ops_data;
452 struct ioring_options *o = td->eo;
459 * Kernel side does submission. just need to check if the ring is
460 * flagged as needing a kick, if so, call io_uring_enter(). This
461 * only happens if we've been idle too long.
463 if (o->sqpoll_thread) {
464 struct io_sq_ring *ring = &ld->sq_ring;
467 flags = atomic_load_acquire(ring->flags);
468 if (flags & IORING_SQ_NEED_WAKEUP)
469 io_uring_enter(ld, ld->queued, 0,
470 IORING_ENTER_SQ_WAKEUP);
476 unsigned start = *ld->sq_ring.head;
477 long nr = ld->queued;
479 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
481 fio_ioring_queued(td, start, ret);
482 io_u_mark_submit(td, ret);
487 io_u_mark_submit(td, ret);
490 if (errno == EAGAIN || errno == EINTR) {
491 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
494 /* Shouldn't happen */
498 td_verror(td, errno, "io_uring_enter submit");
501 } while (ld->queued);
506 static void fio_ioring_unmap(struct ioring_data *ld)
510 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
511 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
515 static void fio_ioring_cleanup(struct thread_data *td)
517 struct ioring_data *ld = td->io_ops_data;
520 if (!(td->flags & TD_F_CHILD))
521 fio_ioring_unmap(ld);
523 free(ld->io_u_index);
530 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
532 struct io_sq_ring *sring = &ld->sq_ring;
533 struct io_cq_ring *cring = &ld->cq_ring;
536 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
537 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
538 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
540 ld->mmap[0].ptr = ptr;
541 sring->head = ptr + p->sq_off.head;
542 sring->tail = ptr + p->sq_off.tail;
543 sring->ring_mask = ptr + p->sq_off.ring_mask;
544 sring->ring_entries = ptr + p->sq_off.ring_entries;
545 sring->flags = ptr + p->sq_off.flags;
546 sring->array = ptr + p->sq_off.array;
547 ld->sq_ring_mask = *sring->ring_mask;
549 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
550 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
551 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
553 ld->mmap[1].ptr = ld->sqes;
555 ld->mmap[2].len = p->cq_off.cqes +
556 p->cq_entries * sizeof(struct io_uring_cqe);
557 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
558 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
560 ld->mmap[2].ptr = ptr;
561 cring->head = ptr + p->cq_off.head;
562 cring->tail = ptr + p->cq_off.tail;
563 cring->ring_mask = ptr + p->cq_off.ring_mask;
564 cring->ring_entries = ptr + p->cq_off.ring_entries;
565 cring->cqes = ptr + p->cq_off.cqes;
566 ld->cq_ring_mask = *cring->ring_mask;
570 static void fio_ioring_probe(struct thread_data *td)
572 struct ioring_data *ld = td->io_ops_data;
573 struct ioring_options *o = td->eo;
574 struct io_uring_probe *p;
577 /* already set by user, don't touch */
578 if (o->nonvectored != -1)
581 /* default to off, as that's always safe */
584 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
588 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
589 ret = syscall(__NR_io_uring_register, ld->ring_fd,
590 IORING_REGISTER_PROBE, p, 256);
594 if (IORING_OP_WRITE > p->ops_len)
597 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
598 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
604 static int fio_ioring_queue_init(struct thread_data *td)
606 struct ioring_data *ld = td->io_ops_data;
607 struct ioring_options *o = td->eo;
608 int depth = td->o.iodepth;
609 struct io_uring_params p;
612 memset(&p, 0, sizeof(p));
615 p.flags |= IORING_SETUP_IOPOLL;
616 if (o->sqpoll_thread) {
617 p.flags |= IORING_SETUP_SQPOLL;
619 p.flags |= IORING_SETUP_SQ_AFF;
620 p.sq_thread_cpu = o->sqpoll_cpu;
624 ret = syscall(__NR_io_uring_setup, depth, &p);
630 fio_ioring_probe(td);
633 ret = syscall(__NR_io_uring_register, ld->ring_fd,
634 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
639 return fio_ioring_mmap(ld, &p);
642 static int fio_ioring_register_files(struct thread_data *td)
644 struct ioring_data *ld = td->io_ops_data;
649 ld->fds = calloc(td->o.nr_files, sizeof(int));
651 for_each_file(td, f, i) {
652 ret = generic_open_file(td, f);
659 ret = syscall(__NR_io_uring_register, ld->ring_fd,
660 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
668 * Pretend the file is closed again, and really close it if we hit
671 for_each_file(td, f, i) {
674 ret2 = generic_close_file(td, f);
682 static int fio_ioring_post_init(struct thread_data *td)
684 struct ioring_data *ld = td->io_ops_data;
685 struct ioring_options *o = td->eo;
689 for (i = 0; i < td->o.iodepth; i++) {
690 struct iovec *iov = &ld->iovecs[i];
692 io_u = ld->io_u_index[i];
693 iov->iov_base = io_u->buf;
694 iov->iov_len = td_max_bs(td);
697 err = fio_ioring_queue_init(td);
699 int init_err = errno;
701 if (init_err == ENOSYS)
702 log_err("fio: your kernel doesn't support io_uring\n");
703 td_verror(td, init_err, "io_queue_init");
707 for (i = 0; i < td->o.iodepth; i++) {
708 struct io_uring_sqe *sqe;
711 memset(sqe, 0, sizeof(*sqe));
714 if (o->registerfiles) {
715 err = fio_ioring_register_files(td);
717 td_verror(td, errno, "ioring_register_files");
725 static int fio_ioring_init(struct thread_data *td)
727 struct ioring_options *o = td->eo;
728 struct ioring_data *ld;
729 struct thread_options *to = &td->o;
731 if (to->io_submit_mode == IO_MODE_OFFLOAD) {
732 log_err("fio: io_submit_mode=offload is not compatible (or "
733 "useful) with io_uring\n");
737 /* sqthread submission requires registered files */
738 if (o->sqpoll_thread)
739 o->registerfiles = 1;
741 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
742 log_err("fio: io_uring registered files require nr_files to "
743 "be identical to open_files\n");
747 ld = calloc(1, sizeof(*ld));
749 /* ring depth must be a power-of-2 */
750 ld->iodepth = td->o.iodepth;
751 td->o.iodepth = roundup_pow2(td->o.iodepth);
754 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
755 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
757 td->io_ops_data = ld;
760 * Check for option conflicts
762 if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
763 o->cmdprio_percentage != 0) {
764 log_err("%s: cmdprio_percentage option and mutually exclusive "
765 "prio or prioclass option is set, exiting\n", to->name);
766 td_verror(td, EINVAL, "fio_io_uring_init");
770 if (fio_option_is_set(&td->o, ioprio_class))
771 ld->ioprio_class_set = true;
772 if (fio_option_is_set(&td->o, ioprio))
773 ld->ioprio_set = true;
778 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
780 struct ioring_data *ld = td->io_ops_data;
782 ld->io_u_index[io_u->index] = io_u;
786 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
788 struct ioring_data *ld = td->io_ops_data;
789 struct ioring_options *o = td->eo;
791 if (!ld || !o->registerfiles)
792 return generic_open_file(td, f);
794 f->fd = ld->fds[f->engine_pos];
798 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
800 struct ioring_data *ld = td->io_ops_data;
801 struct ioring_options *o = td->eo;
803 if (!ld || !o->registerfiles)
804 return generic_close_file(td, f);
810 static struct ioengine_ops ioengine = {
812 .version = FIO_IOOPS_VERSION,
813 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD,
814 .init = fio_ioring_init,
815 .post_init = fio_ioring_post_init,
816 .io_u_init = fio_ioring_io_u_init,
817 .prep = fio_ioring_prep,
818 .queue = fio_ioring_queue,
819 .commit = fio_ioring_commit,
820 .getevents = fio_ioring_getevents,
821 .event = fio_ioring_event,
822 .cleanup = fio_ioring_cleanup,
823 .open_file = fio_ioring_open_file,
824 .close_file = fio_ioring_close_file,
825 .get_file_size = generic_get_file_size,
827 .option_struct_size = sizeof(struct ioring_options),
830 static void fio_init fio_ioring_register(void)
832 register_ioengine(&ioengine);
835 static void fio_exit fio_ioring_unregister(void)
837 unregister_ioengine(&ioengine);