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/linux/io_uring.h"
30 unsigned *ring_entries;
39 unsigned *ring_entries;
40 struct io_uring_cqe *cqes;
51 struct io_u **io_u_index;
55 struct io_sq_ring sq_ring;
56 struct io_uring_sqe *sqes;
58 unsigned sq_ring_mask;
60 struct io_cq_ring cq_ring;
61 unsigned cq_ring_mask;
66 bool ioprio_class_set;
69 struct ioring_mmap mmap[3];
72 struct ioring_options {
75 unsigned int cmdprio_percentage;
76 unsigned int fixedbufs;
77 unsigned int registerfiles;
78 unsigned int sqpoll_thread;
79 unsigned int sqpoll_set;
80 unsigned int sqpoll_cpu;
81 unsigned int nonvectored;
82 unsigned int uncached;
85 static const int ddir_to_op[2][2] = {
86 { IORING_OP_READV, IORING_OP_READ },
87 { IORING_OP_WRITEV, IORING_OP_WRITE }
90 static const int fixed_ddir_to_op[2] = {
95 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
97 struct ioring_options *o = data;
104 static struct fio_option options[] = {
107 .lname = "High Priority",
108 .type = FIO_OPT_STR_SET,
109 .off1 = offsetof(struct ioring_options, hipri),
110 .help = "Use polled IO completions",
111 .category = FIO_OPT_C_ENGINE,
112 .group = FIO_OPT_G_IOURING,
114 #ifdef FIO_HAVE_IOPRIO_CLASS
116 .name = "cmdprio_percentage",
117 .lname = "high priority percentage",
119 .off1 = offsetof(struct ioring_options, cmdprio_percentage),
122 .help = "Send high priority I/O this percentage of the time",
123 .category = FIO_OPT_C_ENGINE,
124 .group = FIO_OPT_G_IOURING,
128 .name = "cmdprio_percentage",
129 .lname = "high priority percentage",
130 .type = FIO_OPT_UNSUPPORTED,
131 .help = "Your platform does not support I/O priority classes",
136 .lname = "Fixed (pre-mapped) IO buffers",
137 .type = FIO_OPT_STR_SET,
138 .off1 = offsetof(struct ioring_options, fixedbufs),
139 .help = "Pre map IO buffers",
140 .category = FIO_OPT_C_ENGINE,
141 .group = FIO_OPT_G_IOURING,
144 .name = "registerfiles",
145 .lname = "Register file set",
146 .type = FIO_OPT_STR_SET,
147 .off1 = offsetof(struct ioring_options, registerfiles),
148 .help = "Pre-open/register files",
149 .category = FIO_OPT_C_ENGINE,
150 .group = FIO_OPT_G_IOURING,
153 .name = "sqthread_poll",
154 .lname = "Kernel SQ thread polling",
156 .off1 = offsetof(struct ioring_options, sqpoll_thread),
157 .help = "Offload submission/completion to kernel thread",
158 .category = FIO_OPT_C_ENGINE,
159 .group = FIO_OPT_G_IOURING,
162 .name = "sqthread_poll_cpu",
163 .lname = "SQ Thread Poll CPU",
165 .cb = fio_ioring_sqpoll_cb,
166 .help = "What CPU to run SQ thread polling on",
167 .category = FIO_OPT_C_ENGINE,
168 .group = FIO_OPT_G_IOURING,
171 .name = "nonvectored",
172 .lname = "Non-vectored",
174 .off1 = offsetof(struct ioring_options, nonvectored),
175 .help = "Use non-vectored read/write commands",
176 .category = FIO_OPT_C_ENGINE,
177 .group = FIO_OPT_G_IOURING,
183 .off1 = offsetof(struct ioring_options, uncached),
184 .help = "Use RWF_UNCACHED for buffered read/writes",
185 .category = FIO_OPT_C_ENGINE,
186 .group = FIO_OPT_G_IOURING,
193 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
194 unsigned int min_complete, unsigned int flags)
196 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
197 min_complete, flags, NULL, 0);
200 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
202 struct ioring_data *ld = td->io_ops_data;
203 struct ioring_options *o = td->eo;
204 struct fio_file *f = io_u->file;
205 struct io_uring_sqe *sqe;
207 sqe = &ld->sqes[io_u->index];
209 /* zero out fields not used in this submission */
210 memset(sqe, 0, sizeof(*sqe));
212 if (o->registerfiles) {
213 sqe->fd = f->engine_pos;
214 sqe->flags = IOSQE_FIXED_FILE;
219 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
221 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
222 sqe->addr = (unsigned long) io_u->xfer_buf;
223 sqe->len = io_u->xfer_buflen;
224 sqe->buf_index = io_u->index;
226 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
227 if (o->nonvectored) {
228 sqe->addr = (unsigned long)
229 ld->iovecs[io_u->index].iov_base;
230 sqe->len = ld->iovecs[io_u->index].iov_len;
232 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
236 if (!td->o.odirect && o->uncached)
237 sqe->rw_flags = RWF_UNCACHED;
238 if (ld->ioprio_class_set)
239 sqe->ioprio = td->o.ioprio_class << 13;
241 sqe->ioprio |= td->o.ioprio;
242 sqe->off = io_u->offset;
243 } else if (ddir_sync(io_u->ddir)) {
244 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
245 sqe->off = f->first_write;
246 sqe->len = f->last_write - f->first_write;
247 sqe->sync_range_flags = td->o.sync_file_range;
248 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
250 if (io_u->ddir == DDIR_DATASYNC)
251 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
252 sqe->opcode = IORING_OP_FSYNC;
256 sqe->user_data = (unsigned long) io_u;
260 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
262 struct ioring_data *ld = td->io_ops_data;
263 struct io_uring_cqe *cqe;
267 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
269 cqe = &ld->cq_ring.cqes[index];
270 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
272 if (cqe->res != io_u->xfer_buflen) {
273 if (cqe->res > io_u->xfer_buflen)
274 io_u->error = -cqe->res;
276 io_u->resid = io_u->xfer_buflen - cqe->res;
283 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
286 struct ioring_data *ld = td->io_ops_data;
287 struct io_cq_ring *ring = &ld->cq_ring;
288 unsigned head, reaped = 0;
293 if (head == *ring->tail)
297 } while (reaped + events < max);
304 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
305 unsigned int max, const struct timespec *t)
307 struct ioring_data *ld = td->io_ops_data;
308 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
309 struct ioring_options *o = td->eo;
310 struct io_cq_ring *ring = &ld->cq_ring;
314 ld->cq_ring_off = *ring->head;
316 r = fio_ioring_cqring_reap(td, events, max);
324 if (!o->sqpoll_thread) {
325 r = io_uring_enter(ld, 0, actual_min,
326 IORING_ENTER_GETEVENTS);
328 if (errno == EAGAIN || errno == EINTR)
330 td_verror(td, errno, "io_uring_enter");
334 } while (events < min);
336 return r < 0 ? r : events;
339 static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
341 struct ioring_options *o = td->eo;
342 struct ioring_data *ld = td->io_ops_data;
343 if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
344 ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
345 io_u->flags |= IO_U_F_PRIORITY;
350 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
353 struct ioring_data *ld = td->io_ops_data;
354 struct io_sq_ring *ring = &ld->sq_ring;
355 struct ioring_options *o = td->eo;
356 unsigned tail, next_tail;
358 fio_ro_check(td, io_u);
360 if (ld->queued == ld->iodepth)
363 if (io_u->ddir == DDIR_TRIM) {
367 do_io_u_trim(td, io_u);
368 io_u_mark_submit(td, 1);
369 io_u_mark_complete(td, 1);
370 return FIO_Q_COMPLETED;
374 next_tail = tail + 1;
376 if (next_tail == *ring->head)
379 if (o->cmdprio_percentage)
380 fio_ioring_prio_prep(td, io_u);
381 ring->array[tail & ld->sq_ring_mask] = io_u->index;
382 *ring->tail = next_tail;
389 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
391 struct ioring_data *ld = td->io_ops_data;
394 if (!fio_fill_issue_time(td))
397 fio_gettime(&now, NULL);
400 struct io_sq_ring *ring = &ld->sq_ring;
401 int index = ring->array[start & ld->sq_ring_mask];
402 struct io_u *io_u = ld->io_u_index[index];
404 memcpy(&io_u->issue_time, &now, sizeof(now));
405 io_u_queued(td, io_u);
411 static int fio_ioring_commit(struct thread_data *td)
413 struct ioring_data *ld = td->io_ops_data;
414 struct ioring_options *o = td->eo;
421 * Kernel side does submission. just need to check if the ring is
422 * flagged as needing a kick, if so, call io_uring_enter(). This
423 * only happens if we've been idle too long.
425 if (o->sqpoll_thread) {
426 struct io_sq_ring *ring = &ld->sq_ring;
429 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
430 io_uring_enter(ld, ld->queued, 0,
431 IORING_ENTER_SQ_WAKEUP);
437 unsigned start = *ld->sq_ring.head;
438 long nr = ld->queued;
440 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
442 fio_ioring_queued(td, start, ret);
443 io_u_mark_submit(td, ret);
448 io_u_mark_submit(td, ret);
451 if (errno == EAGAIN || errno == EINTR) {
452 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
455 /* Shouldn't happen */
459 td_verror(td, errno, "io_uring_enter submit");
462 } while (ld->queued);
467 static void fio_ioring_unmap(struct ioring_data *ld)
471 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
472 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
476 static void fio_ioring_cleanup(struct thread_data *td)
478 struct ioring_data *ld = td->io_ops_data;
481 if (!(td->flags & TD_F_CHILD))
482 fio_ioring_unmap(ld);
484 free(ld->io_u_index);
491 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
493 struct io_sq_ring *sring = &ld->sq_ring;
494 struct io_cq_ring *cring = &ld->cq_ring;
497 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
498 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
499 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
501 ld->mmap[0].ptr = ptr;
502 sring->head = ptr + p->sq_off.head;
503 sring->tail = ptr + p->sq_off.tail;
504 sring->ring_mask = ptr + p->sq_off.ring_mask;
505 sring->ring_entries = ptr + p->sq_off.ring_entries;
506 sring->flags = ptr + p->sq_off.flags;
507 sring->array = ptr + p->sq_off.array;
508 ld->sq_ring_mask = *sring->ring_mask;
510 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
511 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
512 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
514 ld->mmap[1].ptr = ld->sqes;
516 ld->mmap[2].len = p->cq_off.cqes +
517 p->cq_entries * sizeof(struct io_uring_cqe);
518 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
519 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
521 ld->mmap[2].ptr = ptr;
522 cring->head = ptr + p->cq_off.head;
523 cring->tail = ptr + p->cq_off.tail;
524 cring->ring_mask = ptr + p->cq_off.ring_mask;
525 cring->ring_entries = ptr + p->cq_off.ring_entries;
526 cring->cqes = ptr + p->cq_off.cqes;
527 ld->cq_ring_mask = *cring->ring_mask;
531 static int fio_ioring_queue_init(struct thread_data *td)
533 struct ioring_data *ld = td->io_ops_data;
534 struct ioring_options *o = td->eo;
535 int depth = td->o.iodepth;
536 struct io_uring_params p;
539 memset(&p, 0, sizeof(p));
542 p.flags |= IORING_SETUP_IOPOLL;
543 if (o->sqpoll_thread) {
544 p.flags |= IORING_SETUP_SQPOLL;
546 p.flags |= IORING_SETUP_SQ_AFF;
547 p.sq_thread_cpu = o->sqpoll_cpu;
551 ret = syscall(__NR_io_uring_setup, depth, &p);
558 struct rlimit rlim = {
559 .rlim_cur = RLIM_INFINITY,
560 .rlim_max = RLIM_INFINITY,
563 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
566 ret = syscall(__NR_io_uring_register, ld->ring_fd,
567 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
572 return fio_ioring_mmap(ld, &p);
575 static int fio_ioring_register_files(struct thread_data *td)
577 struct ioring_data *ld = td->io_ops_data;
582 ld->fds = calloc(td->o.nr_files, sizeof(int));
584 for_each_file(td, f, i) {
585 ret = generic_open_file(td, f);
592 ret = syscall(__NR_io_uring_register, ld->ring_fd,
593 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
601 * Pretend the file is closed again, and really close it if we hit
604 for_each_file(td, f, i) {
607 ret2 = generic_close_file(td, f);
615 static int fio_ioring_post_init(struct thread_data *td)
617 struct ioring_data *ld = td->io_ops_data;
618 struct ioring_options *o = td->eo;
622 for (i = 0; i < td->o.iodepth; i++) {
623 struct iovec *iov = &ld->iovecs[i];
625 io_u = ld->io_u_index[i];
626 iov->iov_base = io_u->buf;
627 iov->iov_len = td_max_bs(td);
630 err = fio_ioring_queue_init(td);
632 td_verror(td, errno, "io_queue_init");
636 if (o->registerfiles) {
637 err = fio_ioring_register_files(td);
639 td_verror(td, errno, "ioring_register_files");
647 static unsigned roundup_pow2(unsigned depth)
649 return 1UL << __fls(depth - 1);
652 static int fio_ioring_init(struct thread_data *td)
654 struct ioring_options *o = td->eo;
655 struct ioring_data *ld;
656 struct thread_options *to = &td->o;
658 /* sqthread submission requires registered files */
659 if (o->sqpoll_thread)
660 o->registerfiles = 1;
662 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
663 log_err("fio: io_uring registered files require nr_files to "
664 "be identical to open_files\n");
668 ld = calloc(1, sizeof(*ld));
670 /* ring depth must be a power-of-2 */
671 ld->iodepth = td->o.iodepth;
672 td->o.iodepth = roundup_pow2(td->o.iodepth);
675 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
676 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
678 td->io_ops_data = ld;
681 * Check for option conflicts
683 if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
684 o->cmdprio_percentage != 0) {
685 log_err("%s: cmdprio_percentage option and mutually exclusive "
686 "prio or prioclass option is set, exiting\n", to->name);
687 td_verror(td, EINVAL, "fio_io_uring_init");
691 if (fio_option_is_set(&td->o, ioprio_class))
692 ld->ioprio_class_set = true;
693 if (fio_option_is_set(&td->o, ioprio))
694 ld->ioprio_set = true;
699 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
701 struct ioring_data *ld = td->io_ops_data;
703 ld->io_u_index[io_u->index] = io_u;
707 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
709 struct ioring_data *ld = td->io_ops_data;
710 struct ioring_options *o = td->eo;
712 if (!ld || !o->registerfiles)
713 return generic_open_file(td, f);
715 f->fd = ld->fds[f->engine_pos];
719 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
721 struct ioring_data *ld = td->io_ops_data;
722 struct ioring_options *o = td->eo;
724 if (!ld || !o->registerfiles)
725 return generic_close_file(td, f);
731 static struct ioengine_ops ioengine = {
733 .version = FIO_IOOPS_VERSION,
734 .flags = FIO_ASYNCIO_SYNC_TRIM,
735 .init = fio_ioring_init,
736 .post_init = fio_ioring_post_init,
737 .io_u_init = fio_ioring_io_u_init,
738 .prep = fio_ioring_prep,
739 .queue = fio_ioring_queue,
740 .commit = fio_ioring_commit,
741 .getevents = fio_ioring_getevents,
742 .event = fio_ioring_event,
743 .cleanup = fio_ioring_cleanup,
744 .open_file = fio_ioring_open_file,
745 .close_file = fio_ioring_close_file,
746 .get_file_size = generic_get_file_size,
748 .option_struct_size = sizeof(struct ioring_options),
751 static void fio_init fio_ioring_register(void)
753 register_ioengine(&ioengine);
756 static void fio_exit fio_ioring_unregister(void)
758 unregister_ioengine(&ioengine);