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;
70 struct ioring_mmap mmap[3];
72 struct cmdprio cmdprio;
75 struct ioring_options {
76 struct thread_data *td;
78 struct cmdprio_options cmdprio_options;
79 unsigned int fixedbufs;
80 unsigned int registerfiles;
81 unsigned int sqpoll_thread;
82 unsigned int sqpoll_set;
83 unsigned int sqpoll_cpu;
84 unsigned int nonvectored;
85 unsigned int uncached;
87 unsigned int force_async;
90 static const int ddir_to_op[2][2] = {
91 { IORING_OP_READV, IORING_OP_READ },
92 { IORING_OP_WRITEV, IORING_OP_WRITE }
95 static const int fixed_ddir_to_op[2] = {
100 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
102 struct ioring_options *o = data;
104 o->sqpoll_cpu = *val;
109 static struct fio_option options[] = {
112 .lname = "High Priority",
113 .type = FIO_OPT_STR_SET,
114 .off1 = offsetof(struct ioring_options, hipri),
115 .help = "Use polled IO completions",
116 .category = FIO_OPT_C_ENGINE,
117 .group = FIO_OPT_G_IOURING,
119 #ifdef FIO_HAVE_IOPRIO_CLASS
121 .name = "cmdprio_percentage",
122 .lname = "high priority percentage",
124 .off1 = offsetof(struct ioring_options,
125 cmdprio_options.percentage[DDIR_READ]),
126 .off2 = offsetof(struct ioring_options,
127 cmdprio_options.percentage[DDIR_WRITE]),
130 .help = "Send high priority I/O this percentage of the time",
131 .category = FIO_OPT_C_ENGINE,
132 .group = FIO_OPT_G_IOURING,
135 .name = "cmdprio_class",
136 .lname = "Asynchronous I/O priority class",
138 .off1 = offsetof(struct ioring_options,
139 cmdprio_options.class[DDIR_READ]),
140 .off2 = offsetof(struct ioring_options,
141 cmdprio_options.class[DDIR_WRITE]),
142 .help = "Set asynchronous IO priority class",
143 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
144 .maxval = IOPRIO_MAX_PRIO_CLASS,
146 .category = FIO_OPT_C_ENGINE,
147 .group = FIO_OPT_G_IOURING,
151 .lname = "Asynchronous I/O priority level",
153 .off1 = offsetof(struct ioring_options,
154 cmdprio_options.level[DDIR_READ]),
155 .off2 = offsetof(struct ioring_options,
156 cmdprio_options.level[DDIR_WRITE]),
157 .help = "Set asynchronous IO priority level",
158 .minval = IOPRIO_MIN_PRIO,
159 .maxval = IOPRIO_MAX_PRIO,
161 .category = FIO_OPT_C_ENGINE,
162 .group = FIO_OPT_G_IOURING,
165 .name = "cmdprio_bssplit",
166 .lname = "Priority percentage block size split",
167 .type = FIO_OPT_STR_STORE,
168 .off1 = offsetof(struct ioring_options,
169 cmdprio_options.bssplit_str),
170 .help = "Set priority percentages for different block sizes",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_IOURING,
176 .name = "cmdprio_percentage",
177 .lname = "high priority percentage",
178 .type = FIO_OPT_UNSUPPORTED,
179 .help = "Your platform does not support I/O priority classes",
182 .name = "cmdprio_class",
183 .lname = "Asynchronous I/O priority class",
184 .type = FIO_OPT_UNSUPPORTED,
185 .help = "Your platform does not support I/O priority classes",
189 .lname = "Asynchronous I/O priority level",
190 .type = FIO_OPT_UNSUPPORTED,
191 .help = "Your platform does not support I/O priority classes",
194 .name = "cmdprio_bssplit",
195 .lname = "Priority percentage block size split",
196 .type = FIO_OPT_UNSUPPORTED,
197 .help = "Your platform does not support I/O priority classes",
202 .lname = "Fixed (pre-mapped) IO buffers",
203 .type = FIO_OPT_STR_SET,
204 .off1 = offsetof(struct ioring_options, fixedbufs),
205 .help = "Pre map IO buffers",
206 .category = FIO_OPT_C_ENGINE,
207 .group = FIO_OPT_G_IOURING,
210 .name = "registerfiles",
211 .lname = "Register file set",
212 .type = FIO_OPT_STR_SET,
213 .off1 = offsetof(struct ioring_options, registerfiles),
214 .help = "Pre-open/register files",
215 .category = FIO_OPT_C_ENGINE,
216 .group = FIO_OPT_G_IOURING,
219 .name = "sqthread_poll",
220 .lname = "Kernel SQ thread polling",
222 .off1 = offsetof(struct ioring_options, sqpoll_thread),
223 .help = "Offload submission/completion to kernel thread",
224 .category = FIO_OPT_C_ENGINE,
225 .group = FIO_OPT_G_IOURING,
228 .name = "sqthread_poll_cpu",
229 .lname = "SQ Thread Poll CPU",
231 .cb = fio_ioring_sqpoll_cb,
232 .help = "What CPU to run SQ thread polling on",
233 .category = FIO_OPT_C_ENGINE,
234 .group = FIO_OPT_G_IOURING,
237 .name = "nonvectored",
238 .lname = "Non-vectored",
240 .off1 = offsetof(struct ioring_options, nonvectored),
242 .help = "Use non-vectored read/write commands",
243 .category = FIO_OPT_C_ENGINE,
244 .group = FIO_OPT_G_IOURING,
250 .off1 = offsetof(struct ioring_options, uncached),
251 .help = "Use RWF_UNCACHED for buffered read/writes",
252 .category = FIO_OPT_C_ENGINE,
253 .group = FIO_OPT_G_IOURING,
257 .lname = "RWF_NOWAIT",
258 .type = FIO_OPT_BOOL,
259 .off1 = offsetof(struct ioring_options, nowait),
260 .help = "Use RWF_NOWAIT for reads/writes",
261 .category = FIO_OPT_C_ENGINE,
262 .group = FIO_OPT_G_IOURING,
265 .name = "force_async",
266 .lname = "Force async",
268 .off1 = offsetof(struct ioring_options, force_async),
269 .help = "Set IOSQE_ASYNC every N requests",
270 .category = FIO_OPT_C_ENGINE,
271 .group = FIO_OPT_G_IOURING,
278 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
279 unsigned int min_complete, unsigned int flags)
281 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
282 min_complete, flags, NULL, 0);
285 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
287 struct ioring_data *ld = td->io_ops_data;
288 struct ioring_options *o = td->eo;
289 struct fio_file *f = io_u->file;
290 struct io_uring_sqe *sqe;
292 sqe = &ld->sqes[io_u->index];
294 if (o->registerfiles) {
295 sqe->fd = f->engine_pos;
296 sqe->flags = IOSQE_FIXED_FILE;
302 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
304 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
305 sqe->addr = (unsigned long) io_u->xfer_buf;
306 sqe->len = io_u->xfer_buflen;
307 sqe->buf_index = io_u->index;
309 struct iovec *iov = &ld->iovecs[io_u->index];
312 * Update based on actual io_u, requeue could have
315 iov->iov_base = io_u->xfer_buf;
316 iov->iov_len = io_u->xfer_buflen;
318 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
319 if (o->nonvectored) {
320 sqe->addr = (unsigned long) iov->iov_base;
321 sqe->len = iov->iov_len;
323 sqe->addr = (unsigned long) iov;
328 if (!td->o.odirect && o->uncached)
329 sqe->rw_flags |= RWF_UNCACHED;
331 sqe->rw_flags |= RWF_NOWAIT;
334 * Since io_uring can have a submission context (sqthread_poll)
335 * that is different from the process context, we cannot rely on
336 * the IO priority set by ioprio_set() (option prio/prioclass)
338 * td->ioprio will have the value of the "default prio", so set
339 * this unconditionally. This value might get overridden by
340 * fio_ioring_cmdprio_prep() if the option cmdprio_percentage or
341 * cmdprio_bssplit is used.
343 sqe->ioprio = td->ioprio;
344 sqe->off = io_u->offset;
345 } else if (ddir_sync(io_u->ddir)) {
347 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
348 sqe->off = f->first_write;
349 sqe->len = f->last_write - f->first_write;
350 sqe->sync_range_flags = td->o.sync_file_range;
351 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
356 if (io_u->ddir == DDIR_DATASYNC)
357 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
358 sqe->opcode = IORING_OP_FSYNC;
362 if (o->force_async && ++ld->prepped == o->force_async) {
364 sqe->flags |= IOSQE_ASYNC;
367 sqe->user_data = (unsigned long) io_u;
371 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
373 struct ioring_data *ld = td->io_ops_data;
374 struct io_uring_cqe *cqe;
378 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
380 cqe = &ld->cq_ring.cqes[index];
381 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
383 if (cqe->res != io_u->xfer_buflen) {
384 if (cqe->res > io_u->xfer_buflen)
385 io_u->error = -cqe->res;
387 io_u->resid = io_u->xfer_buflen - cqe->res;
394 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
397 struct ioring_data *ld = td->io_ops_data;
398 struct io_cq_ring *ring = &ld->cq_ring;
399 unsigned head, reaped = 0;
403 if (head == atomic_load_acquire(ring->tail))
407 } while (reaped + events < max);
410 atomic_store_release(ring->head, head);
415 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
416 unsigned int max, const struct timespec *t)
418 struct ioring_data *ld = td->io_ops_data;
419 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
420 struct ioring_options *o = td->eo;
421 struct io_cq_ring *ring = &ld->cq_ring;
425 ld->cq_ring_off = *ring->head;
427 r = fio_ioring_cqring_reap(td, events, max);
435 if (!o->sqpoll_thread) {
436 r = io_uring_enter(ld, 0, actual_min,
437 IORING_ENTER_GETEVENTS);
439 if (errno == EAGAIN || errno == EINTR)
441 td_verror(td, errno, "io_uring_enter");
445 } while (events < min);
447 return r < 0 ? r : events;
450 static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
453 struct ioring_data *ld = td->io_ops_data;
454 struct cmdprio *cmdprio = &ld->cmdprio;
456 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
457 ld->sqes[io_u->index].ioprio = io_u->ioprio;
460 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
463 struct ioring_data *ld = td->io_ops_data;
464 struct io_sq_ring *ring = &ld->sq_ring;
465 unsigned tail, next_tail;
467 fio_ro_check(td, io_u);
469 if (ld->queued == ld->iodepth)
472 if (io_u->ddir == DDIR_TRIM) {
476 do_io_u_trim(td, io_u);
477 io_u_mark_submit(td, 1);
478 io_u_mark_complete(td, 1);
479 return FIO_Q_COMPLETED;
483 next_tail = tail + 1;
484 if (next_tail == atomic_load_acquire(ring->head))
487 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
488 fio_ioring_cmdprio_prep(td, io_u);
490 ring->array[tail & ld->sq_ring_mask] = io_u->index;
491 atomic_store_release(ring->tail, next_tail);
497 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
499 struct ioring_data *ld = td->io_ops_data;
502 if (!fio_fill_issue_time(td))
505 fio_gettime(&now, NULL);
508 struct io_sq_ring *ring = &ld->sq_ring;
509 int index = ring->array[start & ld->sq_ring_mask];
510 struct io_u *io_u = ld->io_u_index[index];
512 memcpy(&io_u->issue_time, &now, sizeof(now));
513 io_u_queued(td, io_u);
519 static int fio_ioring_commit(struct thread_data *td)
521 struct ioring_data *ld = td->io_ops_data;
522 struct ioring_options *o = td->eo;
529 * Kernel side does submission. just need to check if the ring is
530 * flagged as needing a kick, if so, call io_uring_enter(). This
531 * only happens if we've been idle too long.
533 if (o->sqpoll_thread) {
534 struct io_sq_ring *ring = &ld->sq_ring;
537 flags = atomic_load_acquire(ring->flags);
538 if (flags & IORING_SQ_NEED_WAKEUP)
539 io_uring_enter(ld, ld->queued, 0,
540 IORING_ENTER_SQ_WAKEUP);
546 unsigned start = *ld->sq_ring.head;
547 long nr = ld->queued;
549 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
551 fio_ioring_queued(td, start, ret);
552 io_u_mark_submit(td, ret);
557 io_u_mark_submit(td, ret);
560 if (errno == EAGAIN || errno == EINTR) {
561 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
564 /* Shouldn't happen */
568 td_verror(td, errno, "io_uring_enter submit");
571 } while (ld->queued);
576 static void fio_ioring_unmap(struct ioring_data *ld)
580 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
581 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
585 static void fio_ioring_cleanup(struct thread_data *td)
587 struct ioring_data *ld = td->io_ops_data;
590 if (!(td->flags & TD_F_CHILD))
591 fio_ioring_unmap(ld);
593 fio_cmdprio_cleanup(&ld->cmdprio);
594 free(ld->io_u_index);
601 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
603 struct io_sq_ring *sring = &ld->sq_ring;
604 struct io_cq_ring *cring = &ld->cq_ring;
607 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
608 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
609 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
611 ld->mmap[0].ptr = ptr;
612 sring->head = ptr + p->sq_off.head;
613 sring->tail = ptr + p->sq_off.tail;
614 sring->ring_mask = ptr + p->sq_off.ring_mask;
615 sring->ring_entries = ptr + p->sq_off.ring_entries;
616 sring->flags = ptr + p->sq_off.flags;
617 sring->array = ptr + p->sq_off.array;
618 ld->sq_ring_mask = *sring->ring_mask;
620 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
621 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
622 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
624 ld->mmap[1].ptr = ld->sqes;
626 ld->mmap[2].len = p->cq_off.cqes +
627 p->cq_entries * sizeof(struct io_uring_cqe);
628 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
629 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
631 ld->mmap[2].ptr = ptr;
632 cring->head = ptr + p->cq_off.head;
633 cring->tail = ptr + p->cq_off.tail;
634 cring->ring_mask = ptr + p->cq_off.ring_mask;
635 cring->ring_entries = ptr + p->cq_off.ring_entries;
636 cring->cqes = ptr + p->cq_off.cqes;
637 ld->cq_ring_mask = *cring->ring_mask;
641 static void fio_ioring_probe(struct thread_data *td)
643 struct ioring_data *ld = td->io_ops_data;
644 struct ioring_options *o = td->eo;
645 struct io_uring_probe *p;
648 /* already set by user, don't touch */
649 if (o->nonvectored != -1)
652 /* default to off, as that's always safe */
655 p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
659 memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
660 ret = syscall(__NR_io_uring_register, ld->ring_fd,
661 IORING_REGISTER_PROBE, p, 256);
665 if (IORING_OP_WRITE > p->ops_len)
668 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
669 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
675 static int fio_ioring_queue_init(struct thread_data *td)
677 struct ioring_data *ld = td->io_ops_data;
678 struct ioring_options *o = td->eo;
679 int depth = td->o.iodepth;
680 struct io_uring_params p;
683 memset(&p, 0, sizeof(p));
686 p.flags |= IORING_SETUP_IOPOLL;
687 if (o->sqpoll_thread) {
688 p.flags |= IORING_SETUP_SQPOLL;
690 p.flags |= IORING_SETUP_SQ_AFF;
691 p.sq_thread_cpu = o->sqpoll_cpu;
696 * Clamp CQ ring size at our SQ ring size, we don't need more entries
699 p.flags |= IORING_SETUP_CQSIZE;
700 p.cq_entries = depth;
702 ret = syscall(__NR_io_uring_setup, depth, &p);
708 fio_ioring_probe(td);
711 ret = syscall(__NR_io_uring_register, ld->ring_fd,
712 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
717 return fio_ioring_mmap(ld, &p);
720 static int fio_ioring_register_files(struct thread_data *td)
722 struct ioring_data *ld = td->io_ops_data;
727 ld->fds = calloc(td->o.nr_files, sizeof(int));
729 for_each_file(td, f, i) {
730 ret = generic_open_file(td, f);
737 ret = syscall(__NR_io_uring_register, ld->ring_fd,
738 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
746 * Pretend the file is closed again, and really close it if we hit
749 for_each_file(td, f, i) {
752 ret2 = generic_close_file(td, f);
760 static int fio_ioring_post_init(struct thread_data *td)
762 struct ioring_data *ld = td->io_ops_data;
763 struct ioring_options *o = td->eo;
767 for (i = 0; i < td->o.iodepth; i++) {
768 struct iovec *iov = &ld->iovecs[i];
770 io_u = ld->io_u_index[i];
771 iov->iov_base = io_u->buf;
772 iov->iov_len = td_max_bs(td);
775 err = fio_ioring_queue_init(td);
777 int init_err = errno;
779 if (init_err == ENOSYS)
780 log_err("fio: your kernel doesn't support io_uring\n");
781 td_verror(td, init_err, "io_queue_init");
785 for (i = 0; i < td->o.iodepth; i++) {
786 struct io_uring_sqe *sqe;
789 memset(sqe, 0, sizeof(*sqe));
792 if (o->registerfiles) {
793 err = fio_ioring_register_files(td);
795 td_verror(td, errno, "ioring_register_files");
803 static int fio_ioring_init(struct thread_data *td)
805 struct ioring_options *o = td->eo;
806 struct ioring_data *ld;
809 /* sqthread submission requires registered files */
810 if (o->sqpoll_thread)
811 o->registerfiles = 1;
813 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
814 log_err("fio: io_uring registered files require nr_files to "
815 "be identical to open_files\n");
819 ld = calloc(1, sizeof(*ld));
821 /* ring depth must be a power-of-2 */
822 ld->iodepth = td->o.iodepth;
823 td->o.iodepth = roundup_pow2(td->o.iodepth);
826 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
827 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
829 td->io_ops_data = ld;
831 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
833 td_verror(td, EINVAL, "fio_ioring_init");
840 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
842 struct ioring_data *ld = td->io_ops_data;
844 ld->io_u_index[io_u->index] = io_u;
848 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
850 struct ioring_data *ld = td->io_ops_data;
851 struct ioring_options *o = td->eo;
853 if (!ld || !o->registerfiles)
854 return generic_open_file(td, f);
856 f->fd = ld->fds[f->engine_pos];
860 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
862 struct ioring_data *ld = td->io_ops_data;
863 struct ioring_options *o = td->eo;
865 if (!ld || !o->registerfiles)
866 return generic_close_file(td, f);
872 static struct ioengine_ops ioengine = {
874 .version = FIO_IOOPS_VERSION,
875 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD,
876 .init = fio_ioring_init,
877 .post_init = fio_ioring_post_init,
878 .io_u_init = fio_ioring_io_u_init,
879 .prep = fio_ioring_prep,
880 .queue = fio_ioring_queue,
881 .commit = fio_ioring_commit,
882 .getevents = fio_ioring_getevents,
883 .event = fio_ioring_event,
884 .cleanup = fio_ioring_cleanup,
885 .open_file = fio_ioring_open_file,
886 .close_file = fio_ioring_close_file,
887 .get_file_size = generic_get_file_size,
889 .option_struct_size = sizeof(struct ioring_options),
892 static void fio_init fio_ioring_register(void)
894 register_ioengine(&ioengine);
897 static void fio_exit fio_ioring_unregister(void)
899 unregister_ioengine(&ioengine);