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"
32 unsigned *ring_entries;
41 unsigned *ring_entries;
42 struct io_uring_cqe *cqes;
53 struct io_u **io_u_index;
57 struct io_sq_ring sq_ring;
58 struct io_uring_sqe *sqes;
60 unsigned sq_ring_mask;
62 struct io_cq_ring cq_ring;
63 unsigned cq_ring_mask;
68 bool ioprio_class_set;
72 struct ioring_mmap mmap[3];
77 struct ioring_options {
78 struct thread_data *td;
80 struct cmdprio cmdprio;
81 unsigned int fixedbufs;
82 unsigned int registerfiles;
83 unsigned int sqpoll_thread;
84 unsigned int sqpoll_set;
85 unsigned int sqpoll_cpu;
86 unsigned int nonvectored;
87 unsigned int uncached;
89 unsigned int force_async;
92 static const int ddir_to_op[2][2] = {
93 { IORING_OP_READV, IORING_OP_READ },
94 { IORING_OP_WRITEV, IORING_OP_WRITE }
97 static const int fixed_ddir_to_op[2] = {
102 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
104 struct ioring_options *o = data;
106 o->sqpoll_cpu = *val;
111 static int str_cmdprio_bssplit_cb(void *data, const char *input)
113 struct ioring_options *o = data;
114 struct thread_data *td = o->td;
115 struct cmdprio *cmdprio = &o->cmdprio;
117 return fio_cmdprio_bssplit_parse(td, input, cmdprio);
120 static struct fio_option options[] = {
123 .lname = "High Priority",
124 .type = FIO_OPT_STR_SET,
125 .off1 = offsetof(struct ioring_options, hipri),
126 .help = "Use polled IO completions",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_IOURING,
130 #ifdef FIO_HAVE_IOPRIO_CLASS
132 .name = "cmdprio_percentage",
133 .lname = "high priority percentage",
135 .off1 = offsetof(struct ioring_options,
136 cmdprio.percentage[DDIR_READ]),
137 .off2 = offsetof(struct ioring_options,
138 cmdprio.percentage[DDIR_WRITE]),
141 .help = "Send high priority I/O this percentage of the time",
142 .category = FIO_OPT_C_ENGINE,
143 .group = FIO_OPT_G_IOURING,
146 .name = "cmdprio_class",
147 .lname = "Asynchronous I/O priority class",
149 .off1 = offsetof(struct ioring_options,
150 cmdprio.class[DDIR_READ]),
151 .off2 = offsetof(struct ioring_options,
152 cmdprio.class[DDIR_WRITE]),
153 .help = "Set asynchronous IO priority class",
154 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
155 .maxval = IOPRIO_MAX_PRIO_CLASS,
157 .category = FIO_OPT_C_ENGINE,
158 .group = FIO_OPT_G_IOURING,
162 .lname = "Asynchronous I/O priority level",
164 .off1 = offsetof(struct ioring_options,
165 cmdprio.level[DDIR_READ]),
166 .off2 = offsetof(struct ioring_options,
167 cmdprio.level[DDIR_WRITE]),
168 .help = "Set asynchronous IO priority level",
169 .minval = IOPRIO_MIN_PRIO,
170 .maxval = IOPRIO_MAX_PRIO,
172 .category = FIO_OPT_C_ENGINE,
173 .group = FIO_OPT_G_IOURING,
176 .name = "cmdprio_bssplit",
177 .lname = "Priority percentage block size split",
178 .type = FIO_OPT_STR_ULL,
179 .cb = str_cmdprio_bssplit_cb,
180 .off1 = offsetof(struct ioring_options, cmdprio.bssplit),
181 .help = "Set priority percentages for different block sizes",
182 .category = FIO_OPT_C_ENGINE,
183 .group = FIO_OPT_G_IOURING,
187 .name = "cmdprio_percentage",
188 .lname = "high priority percentage",
189 .type = FIO_OPT_UNSUPPORTED,
190 .help = "Your platform does not support I/O priority classes",
193 .name = "cmdprio_class",
194 .lname = "Asynchronous I/O priority class",
195 .type = FIO_OPT_UNSUPPORTED,
196 .help = "Your platform does not support I/O priority classes",
200 .lname = "Asynchronous I/O priority level",
201 .type = FIO_OPT_UNSUPPORTED,
202 .help = "Your platform does not support I/O priority classes",
205 .name = "cmdprio_bssplit",
206 .lname = "Priority percentage block size split",
207 .type = FIO_OPT_UNSUPPORTED,
208 .help = "Your platform does not support I/O priority classes",
213 .lname = "Fixed (pre-mapped) IO buffers",
214 .type = FIO_OPT_STR_SET,
215 .off1 = offsetof(struct ioring_options, fixedbufs),
216 .help = "Pre map IO buffers",
217 .category = FIO_OPT_C_ENGINE,
218 .group = FIO_OPT_G_IOURING,
221 .name = "registerfiles",
222 .lname = "Register file set",
223 .type = FIO_OPT_STR_SET,
224 .off1 = offsetof(struct ioring_options, registerfiles),
225 .help = "Pre-open/register files",
226 .category = FIO_OPT_C_ENGINE,
227 .group = FIO_OPT_G_IOURING,
230 .name = "sqthread_poll",
231 .lname = "Kernel SQ thread polling",
233 .off1 = offsetof(struct ioring_options, sqpoll_thread),
234 .help = "Offload submission/completion to kernel thread",
235 .category = FIO_OPT_C_ENGINE,
236 .group = FIO_OPT_G_IOURING,
239 .name = "sqthread_poll_cpu",
240 .lname = "SQ Thread Poll CPU",
242 .cb = fio_ioring_sqpoll_cb,
243 .help = "What CPU to run SQ thread polling on",
244 .category = FIO_OPT_C_ENGINE,
245 .group = FIO_OPT_G_IOURING,
248 .name = "nonvectored",
249 .lname = "Non-vectored",
251 .off1 = offsetof(struct ioring_options, nonvectored),
253 .help = "Use non-vectored read/write commands",
254 .category = FIO_OPT_C_ENGINE,
255 .group = FIO_OPT_G_IOURING,
261 .off1 = offsetof(struct ioring_options, uncached),
262 .help = "Use RWF_UNCACHED for buffered read/writes",
263 .category = FIO_OPT_C_ENGINE,
264 .group = FIO_OPT_G_IOURING,
268 .lname = "RWF_NOWAIT",
269 .type = FIO_OPT_BOOL,
270 .off1 = offsetof(struct ioring_options, nowait),
271 .help = "Use RWF_NOWAIT for reads/writes",
272 .category = FIO_OPT_C_ENGINE,
273 .group = FIO_OPT_G_IOURING,
276 .name = "force_async",
277 .lname = "Force async",
279 .off1 = offsetof(struct ioring_options, force_async),
280 .help = "Set IOSQE_ASYNC every N requests",
281 .category = FIO_OPT_C_ENGINE,
282 .group = FIO_OPT_G_IOURING,
289 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
290 unsigned int min_complete, unsigned int flags)
292 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
293 min_complete, flags, NULL, 0);
296 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
298 struct ioring_data *ld = td->io_ops_data;
299 struct ioring_options *o = td->eo;
300 struct fio_file *f = io_u->file;
301 struct io_uring_sqe *sqe;
303 sqe = &ld->sqes[io_u->index];
305 if (o->registerfiles) {
306 sqe->fd = f->engine_pos;
307 sqe->flags = IOSQE_FIXED_FILE;
313 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
315 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
316 sqe->addr = (unsigned long) io_u->xfer_buf;
317 sqe->len = io_u->xfer_buflen;
318 sqe->buf_index = io_u->index;
320 struct iovec *iov = &ld->iovecs[io_u->index];
323 * Update based on actual io_u, requeue could have
326 iov->iov_base = io_u->xfer_buf;
327 iov->iov_len = io_u->xfer_buflen;
329 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
330 if (o->nonvectored) {
331 sqe->addr = (unsigned long) iov->iov_base;
332 sqe->len = iov->iov_len;
334 sqe->addr = (unsigned long) iov;
339 if (!td->o.odirect && o->uncached)
340 sqe->rw_flags |= RWF_UNCACHED;
342 sqe->rw_flags |= RWF_NOWAIT;
343 if (ld->ioprio_class_set)
344 sqe->ioprio = td->o.ioprio_class << 13;
346 sqe->ioprio |= td->o.ioprio;
347 sqe->off = io_u->offset;
348 } else if (ddir_sync(io_u->ddir)) {
350 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
351 sqe->off = f->first_write;
352 sqe->len = f->last_write - f->first_write;
353 sqe->sync_range_flags = td->o.sync_file_range;
354 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
359 if (io_u->ddir == DDIR_DATASYNC)
360 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
361 sqe->opcode = IORING_OP_FSYNC;
365 if (o->force_async && ++ld->prepped == o->force_async) {
367 sqe->flags |= IOSQE_ASYNC;
370 sqe->user_data = (unsigned long) io_u;
374 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
376 struct ioring_data *ld = td->io_ops_data;
377 struct io_uring_cqe *cqe;
381 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
383 cqe = &ld->cq_ring.cqes[index];
384 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
386 if (cqe->res != io_u->xfer_buflen) {
387 if (cqe->res > io_u->xfer_buflen)
388 io_u->error = -cqe->res;
390 io_u->resid = io_u->xfer_buflen - cqe->res;
397 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
400 struct ioring_data *ld = td->io_ops_data;
401 struct io_cq_ring *ring = &ld->cq_ring;
402 unsigned head, reaped = 0;
406 if (head == atomic_load_acquire(ring->tail))
410 } while (reaped + events < max);
413 atomic_store_release(ring->head, head);
418 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
419 unsigned int max, const struct timespec *t)
421 struct ioring_data *ld = td->io_ops_data;
422 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
423 struct ioring_options *o = td->eo;
424 struct io_cq_ring *ring = &ld->cq_ring;
428 ld->cq_ring_off = *ring->head;
430 r = fio_ioring_cqring_reap(td, events, max);
438 if (!o->sqpoll_thread) {
439 r = io_uring_enter(ld, 0, actual_min,
440 IORING_ENTER_GETEVENTS);
442 if (errno == EAGAIN || errno == EINTR)
444 td_verror(td, errno, "io_uring_enter");
448 } while (events < min);
450 return r < 0 ? r : events;
453 static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
455 struct ioring_options *o = td->eo;
456 struct ioring_data *ld = td->io_ops_data;
457 struct io_uring_sqe *sqe = &ld->sqes[io_u->index];
458 struct cmdprio *cmdprio = &o->cmdprio;
459 enum fio_ddir ddir = io_u->ddir;
460 unsigned int p = fio_cmdprio_percentage(cmdprio, io_u);
462 if (p && rand_between(&td->prio_state, 0, 99) < p) {
464 ioprio_value(cmdprio->class[ddir], cmdprio->level[ddir]);
465 io_u->flags |= IO_U_F_PRIORITY;
471 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
474 struct ioring_data *ld = td->io_ops_data;
475 struct io_sq_ring *ring = &ld->sq_ring;
476 unsigned tail, next_tail;
478 fio_ro_check(td, io_u);
480 if (ld->queued == ld->iodepth)
483 if (io_u->ddir == DDIR_TRIM) {
487 do_io_u_trim(td, io_u);
488 io_u_mark_submit(td, 1);
489 io_u_mark_complete(td, 1);
490 return FIO_Q_COMPLETED;
494 next_tail = tail + 1;
495 if (next_tail == atomic_load_acquire(ring->head))
499 fio_ioring_prio_prep(td, io_u);
500 ring->array[tail & ld->sq_ring_mask] = io_u->index;
501 atomic_store_release(ring->tail, next_tail);
507 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
509 struct ioring_data *ld = td->io_ops_data;
512 if (!fio_fill_issue_time(td))
515 fio_gettime(&now, NULL);
518 struct io_sq_ring *ring = &ld->sq_ring;
519 int index = ring->array[start & ld->sq_ring_mask];
520 struct io_u *io_u = ld->io_u_index[index];
522 memcpy(&io_u->issue_time, &now, sizeof(now));
523 io_u_queued(td, io_u);
529 static int fio_ioring_commit(struct thread_data *td)
531 struct ioring_data *ld = td->io_ops_data;
532 struct ioring_options *o = td->eo;
539 * Kernel side does submission. just need to check if the ring is
540 * flagged as needing a kick, if so, call io_uring_enter(). This
541 * only happens if we've been idle too long.
543 if (o->sqpoll_thread) {
544 struct io_sq_ring *ring = &ld->sq_ring;
547 flags = atomic_load_acquire(ring->flags);
548 if (flags & IORING_SQ_NEED_WAKEUP)
549 io_uring_enter(ld, ld->queued, 0,
550 IORING_ENTER_SQ_WAKEUP);
556 unsigned start = *ld->sq_ring.head;
557 long nr = ld->queued;
559 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
561 fio_ioring_queued(td, start, ret);
562 io_u_mark_submit(td, ret);
567 io_u_mark_submit(td, ret);
570 if (errno == EAGAIN || errno == EINTR) {
571 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
574 /* Shouldn't happen */
578 td_verror(td, errno, "io_uring_enter submit");
581 } while (ld->queued);
586 static void fio_ioring_unmap(struct ioring_data *ld)
590 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
591 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
595 static void fio_ioring_cleanup(struct thread_data *td)
597 struct ioring_data *ld = td->io_ops_data;
600 if (!(td->flags & TD_F_CHILD))
601 fio_ioring_unmap(ld);
603 free(ld->io_u_index);
610 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
612 struct io_sq_ring *sring = &ld->sq_ring;
613 struct io_cq_ring *cring = &ld->cq_ring;
616 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
617 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
618 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
620 ld->mmap[0].ptr = ptr;
621 sring->head = ptr + p->sq_off.head;
622 sring->tail = ptr + p->sq_off.tail;
623 sring->ring_mask = ptr + p->sq_off.ring_mask;
624 sring->ring_entries = ptr + p->sq_off.ring_entries;
625 sring->flags = ptr + p->sq_off.flags;
626 sring->array = ptr + p->sq_off.array;
627 ld->sq_ring_mask = *sring->ring_mask;
629 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
630 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
631 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
633 ld->mmap[1].ptr = ld->sqes;
635 ld->mmap[2].len = p->cq_off.cqes +
636 p->cq_entries * sizeof(struct io_uring_cqe);
637 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
638 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
640 ld->mmap[2].ptr = ptr;
641 cring->head = ptr + p->cq_off.head;
642 cring->tail = ptr + p->cq_off.tail;
643 cring->ring_mask = ptr + p->cq_off.ring_mask;
644 cring->ring_entries = ptr + p->cq_off.ring_entries;
645 cring->cqes = ptr + p->cq_off.cqes;
646 ld->cq_ring_mask = *cring->ring_mask;
650 static void fio_ioring_probe(struct thread_data *td)
652 struct ioring_data *ld = td->io_ops_data;
653 struct ioring_options *o = td->eo;
654 struct io_uring_probe *p;
657 /* already set by user, don't touch */
658 if (o->nonvectored != -1)
661 /* default to off, as that's always safe */
664 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
668 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
669 ret = syscall(__NR_io_uring_register, ld->ring_fd,
670 IORING_REGISTER_PROBE, p, 256);
674 if (IORING_OP_WRITE > p->ops_len)
677 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
678 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
684 static int fio_ioring_queue_init(struct thread_data *td)
686 struct ioring_data *ld = td->io_ops_data;
687 struct ioring_options *o = td->eo;
688 int depth = td->o.iodepth;
689 struct io_uring_params p;
692 memset(&p, 0, sizeof(p));
695 p.flags |= IORING_SETUP_IOPOLL;
696 if (o->sqpoll_thread) {
697 p.flags |= IORING_SETUP_SQPOLL;
699 p.flags |= IORING_SETUP_SQ_AFF;
700 p.sq_thread_cpu = o->sqpoll_cpu;
704 ret = syscall(__NR_io_uring_setup, depth, &p);
710 fio_ioring_probe(td);
713 ret = syscall(__NR_io_uring_register, ld->ring_fd,
714 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
719 return fio_ioring_mmap(ld, &p);
722 static int fio_ioring_register_files(struct thread_data *td)
724 struct ioring_data *ld = td->io_ops_data;
729 ld->fds = calloc(td->o.nr_files, sizeof(int));
731 for_each_file(td, f, i) {
732 ret = generic_open_file(td, f);
739 ret = syscall(__NR_io_uring_register, ld->ring_fd,
740 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
748 * Pretend the file is closed again, and really close it if we hit
751 for_each_file(td, f, i) {
754 ret2 = generic_close_file(td, f);
762 static int fio_ioring_post_init(struct thread_data *td)
764 struct ioring_data *ld = td->io_ops_data;
765 struct ioring_options *o = td->eo;
769 for (i = 0; i < td->o.iodepth; i++) {
770 struct iovec *iov = &ld->iovecs[i];
772 io_u = ld->io_u_index[i];
773 iov->iov_base = io_u->buf;
774 iov->iov_len = td_max_bs(td);
777 err = fio_ioring_queue_init(td);
779 int init_err = errno;
781 if (init_err == ENOSYS)
782 log_err("fio: your kernel doesn't support io_uring\n");
783 td_verror(td, init_err, "io_queue_init");
787 for (i = 0; i < td->o.iodepth; i++) {
788 struct io_uring_sqe *sqe;
791 memset(sqe, 0, sizeof(*sqe));
794 if (o->registerfiles) {
795 err = fio_ioring_register_files(td);
797 td_verror(td, errno, "ioring_register_files");
805 static int fio_ioring_init(struct thread_data *td)
807 struct ioring_options *o = td->eo;
808 struct ioring_data *ld;
809 struct cmdprio *cmdprio = &o->cmdprio;
812 /* sqthread submission requires registered files */
813 if (o->sqpoll_thread)
814 o->registerfiles = 1;
816 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
817 log_err("fio: io_uring registered files require nr_files to "
818 "be identical to open_files\n");
822 ld = calloc(1, sizeof(*ld));
824 /* ring depth must be a power-of-2 */
825 ld->iodepth = td->o.iodepth;
826 td->o.iodepth = roundup_pow2(td->o.iodepth);
829 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
830 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
832 td->io_ops_data = ld;
834 ret = fio_cmdprio_init(td, cmdprio, &ld->use_cmdprio);
836 td_verror(td, EINVAL, "fio_ioring_init");
840 if (fio_option_is_set(&td->o, ioprio_class))
841 ld->ioprio_class_set = true;
842 if (fio_option_is_set(&td->o, ioprio))
843 ld->ioprio_set = true;
848 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
850 struct ioring_data *ld = td->io_ops_data;
852 ld->io_u_index[io_u->index] = io_u;
856 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
858 struct ioring_data *ld = td->io_ops_data;
859 struct ioring_options *o = td->eo;
861 if (!ld || !o->registerfiles)
862 return generic_open_file(td, f);
864 f->fd = ld->fds[f->engine_pos];
868 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
870 struct ioring_data *ld = td->io_ops_data;
871 struct ioring_options *o = td->eo;
873 if (!ld || !o->registerfiles)
874 return generic_close_file(td, f);
880 static struct ioengine_ops ioengine = {
882 .version = FIO_IOOPS_VERSION,
883 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD,
884 .init = fio_ioring_init,
885 .post_init = fio_ioring_post_init,
886 .io_u_init = fio_ioring_io_u_init,
887 .prep = fio_ioring_prep,
888 .queue = fio_ioring_queue,
889 .commit = fio_ioring_commit,
890 .getevents = fio_ioring_getevents,
891 .event = fio_ioring_event,
892 .cleanup = fio_ioring_cleanup,
893 .open_file = fio_ioring_open_file,
894 .close_file = fio_ioring_close_file,
895 .get_file_size = generic_get_file_size,
897 .option_struct_size = sizeof(struct ioring_options),
900 static void fio_init fio_ioring_register(void)
902 register_ioengine(&ioengine);
905 static void fio_exit fio_ioring_unregister(void)
907 unregister_ioengine(&ioengine);