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),
177 .help = "Use non-vectored read/write commands",
178 .category = FIO_OPT_C_ENGINE,
179 .group = FIO_OPT_G_IOURING,
185 .off1 = offsetof(struct ioring_options, uncached),
186 .help = "Use RWF_UNCACHED for buffered read/writes",
187 .category = FIO_OPT_C_ENGINE,
188 .group = FIO_OPT_G_IOURING,
192 .lname = "RWF_NOWAIT",
193 .type = FIO_OPT_BOOL,
194 .off1 = offsetof(struct ioring_options, nowait),
195 .help = "Use RWF_NOWAIT for reads/writes",
196 .category = FIO_OPT_C_ENGINE,
197 .group = FIO_OPT_G_IOURING,
204 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
205 unsigned int min_complete, unsigned int flags)
207 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
208 min_complete, flags, NULL, 0);
211 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
213 struct ioring_data *ld = td->io_ops_data;
214 struct ioring_options *o = td->eo;
215 struct fio_file *f = io_u->file;
216 struct io_uring_sqe *sqe;
218 sqe = &ld->sqes[io_u->index];
220 /* zero out fields not used in this submission */
221 memset(sqe, 0, sizeof(*sqe));
223 if (o->registerfiles) {
224 sqe->fd = f->engine_pos;
225 sqe->flags = IOSQE_FIXED_FILE;
230 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
232 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
233 sqe->addr = (unsigned long) io_u->xfer_buf;
234 sqe->len = io_u->xfer_buflen;
235 sqe->buf_index = io_u->index;
237 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
238 if (o->nonvectored) {
239 sqe->addr = (unsigned long)
240 ld->iovecs[io_u->index].iov_base;
241 sqe->len = ld->iovecs[io_u->index].iov_len;
243 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
247 if (!td->o.odirect && o->uncached)
248 sqe->rw_flags = RWF_UNCACHED;
250 sqe->rw_flags |= RWF_NOWAIT;
251 if (ld->ioprio_class_set)
252 sqe->ioprio = td->o.ioprio_class << 13;
254 sqe->ioprio |= td->o.ioprio;
255 sqe->off = io_u->offset;
256 } else if (ddir_sync(io_u->ddir)) {
257 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
258 sqe->off = f->first_write;
259 sqe->len = f->last_write - f->first_write;
260 sqe->sync_range_flags = td->o.sync_file_range;
261 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
263 if (io_u->ddir == DDIR_DATASYNC)
264 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
265 sqe->opcode = IORING_OP_FSYNC;
269 sqe->user_data = (unsigned long) io_u;
273 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
275 struct ioring_data *ld = td->io_ops_data;
276 struct io_uring_cqe *cqe;
280 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
282 cqe = &ld->cq_ring.cqes[index];
283 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
285 if (cqe->res != io_u->xfer_buflen) {
286 if (cqe->res > io_u->xfer_buflen)
287 io_u->error = -cqe->res;
289 io_u->resid = io_u->xfer_buflen - cqe->res;
296 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
299 struct ioring_data *ld = td->io_ops_data;
300 struct io_cq_ring *ring = &ld->cq_ring;
301 unsigned head, reaped = 0;
305 if (head == atomic_load_acquire(ring->tail))
309 } while (reaped + events < max);
312 atomic_store_release(ring->head, head);
317 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
318 unsigned int max, const struct timespec *t)
320 struct ioring_data *ld = td->io_ops_data;
321 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
322 struct ioring_options *o = td->eo;
323 struct io_cq_ring *ring = &ld->cq_ring;
327 ld->cq_ring_off = *ring->head;
329 r = fio_ioring_cqring_reap(td, events, max);
337 if (!o->sqpoll_thread) {
338 r = io_uring_enter(ld, 0, actual_min,
339 IORING_ENTER_GETEVENTS);
341 if (errno == EAGAIN || errno == EINTR)
343 td_verror(td, errno, "io_uring_enter");
347 } while (events < min);
349 return r < 0 ? r : events;
352 static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
354 struct ioring_options *o = td->eo;
355 struct ioring_data *ld = td->io_ops_data;
356 if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
357 ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
358 io_u->flags |= IO_U_F_PRIORITY;
363 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
366 struct ioring_data *ld = td->io_ops_data;
367 struct io_sq_ring *ring = &ld->sq_ring;
368 struct ioring_options *o = td->eo;
369 unsigned tail, next_tail;
371 fio_ro_check(td, io_u);
373 if (ld->queued == ld->iodepth)
376 if (io_u->ddir == DDIR_TRIM) {
380 do_io_u_trim(td, io_u);
381 io_u_mark_submit(td, 1);
382 io_u_mark_complete(td, 1);
383 return FIO_Q_COMPLETED;
387 next_tail = tail + 1;
388 if (next_tail == atomic_load_acquire(ring->head))
391 if (o->cmdprio_percentage)
392 fio_ioring_prio_prep(td, io_u);
393 ring->array[tail & ld->sq_ring_mask] = io_u->index;
394 atomic_store_release(ring->tail, next_tail);
400 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
402 struct ioring_data *ld = td->io_ops_data;
405 if (!fio_fill_issue_time(td))
408 fio_gettime(&now, NULL);
411 struct io_sq_ring *ring = &ld->sq_ring;
412 int index = ring->array[start & ld->sq_ring_mask];
413 struct io_u *io_u = ld->io_u_index[index];
415 memcpy(&io_u->issue_time, &now, sizeof(now));
416 io_u_queued(td, io_u);
422 static int fio_ioring_commit(struct thread_data *td)
424 struct ioring_data *ld = td->io_ops_data;
425 struct ioring_options *o = td->eo;
432 * Kernel side does submission. just need to check if the ring is
433 * flagged as needing a kick, if so, call io_uring_enter(). This
434 * only happens if we've been idle too long.
436 if (o->sqpoll_thread) {
437 struct io_sq_ring *ring = &ld->sq_ring;
440 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
441 io_uring_enter(ld, ld->queued, 0,
442 IORING_ENTER_SQ_WAKEUP);
448 unsigned start = *ld->sq_ring.head;
449 long nr = ld->queued;
451 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
453 fio_ioring_queued(td, start, ret);
454 io_u_mark_submit(td, ret);
459 io_u_mark_submit(td, ret);
462 if (errno == EAGAIN || errno == EINTR) {
463 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
466 /* Shouldn't happen */
470 td_verror(td, errno, "io_uring_enter submit");
473 } while (ld->queued);
478 static void fio_ioring_unmap(struct ioring_data *ld)
482 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
483 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
487 static void fio_ioring_cleanup(struct thread_data *td)
489 struct ioring_data *ld = td->io_ops_data;
492 if (!(td->flags & TD_F_CHILD))
493 fio_ioring_unmap(ld);
495 free(ld->io_u_index);
502 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
504 struct io_sq_ring *sring = &ld->sq_ring;
505 struct io_cq_ring *cring = &ld->cq_ring;
508 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
509 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
510 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
512 ld->mmap[0].ptr = ptr;
513 sring->head = ptr + p->sq_off.head;
514 sring->tail = ptr + p->sq_off.tail;
515 sring->ring_mask = ptr + p->sq_off.ring_mask;
516 sring->ring_entries = ptr + p->sq_off.ring_entries;
517 sring->flags = ptr + p->sq_off.flags;
518 sring->array = ptr + p->sq_off.array;
519 ld->sq_ring_mask = *sring->ring_mask;
521 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
522 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
523 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
525 ld->mmap[1].ptr = ld->sqes;
527 ld->mmap[2].len = p->cq_off.cqes +
528 p->cq_entries * sizeof(struct io_uring_cqe);
529 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
530 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
532 ld->mmap[2].ptr = ptr;
533 cring->head = ptr + p->cq_off.head;
534 cring->tail = ptr + p->cq_off.tail;
535 cring->ring_mask = ptr + p->cq_off.ring_mask;
536 cring->ring_entries = ptr + p->cq_off.ring_entries;
537 cring->cqes = ptr + p->cq_off.cqes;
538 ld->cq_ring_mask = *cring->ring_mask;
542 static int fio_ioring_queue_init(struct thread_data *td)
544 struct ioring_data *ld = td->io_ops_data;
545 struct ioring_options *o = td->eo;
546 int depth = td->o.iodepth;
547 struct io_uring_params p;
550 memset(&p, 0, sizeof(p));
553 p.flags |= IORING_SETUP_IOPOLL;
554 if (o->sqpoll_thread) {
555 p.flags |= IORING_SETUP_SQPOLL;
557 p.flags |= IORING_SETUP_SQ_AFF;
558 p.sq_thread_cpu = o->sqpoll_cpu;
562 ret = syscall(__NR_io_uring_setup, depth, &p);
569 struct rlimit rlim = {
570 .rlim_cur = RLIM_INFINITY,
571 .rlim_max = RLIM_INFINITY,
574 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
577 ret = syscall(__NR_io_uring_register, ld->ring_fd,
578 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
583 return fio_ioring_mmap(ld, &p);
586 static int fio_ioring_register_files(struct thread_data *td)
588 struct ioring_data *ld = td->io_ops_data;
593 ld->fds = calloc(td->o.nr_files, sizeof(int));
595 for_each_file(td, f, i) {
596 ret = generic_open_file(td, f);
603 ret = syscall(__NR_io_uring_register, ld->ring_fd,
604 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
612 * Pretend the file is closed again, and really close it if we hit
615 for_each_file(td, f, i) {
618 ret2 = generic_close_file(td, f);
626 static int fio_ioring_post_init(struct thread_data *td)
628 struct ioring_data *ld = td->io_ops_data;
629 struct ioring_options *o = td->eo;
633 for (i = 0; i < td->o.iodepth; i++) {
634 struct iovec *iov = &ld->iovecs[i];
636 io_u = ld->io_u_index[i];
637 iov->iov_base = io_u->buf;
638 iov->iov_len = td_max_bs(td);
641 err = fio_ioring_queue_init(td);
643 td_verror(td, errno, "io_queue_init");
647 if (o->registerfiles) {
648 err = fio_ioring_register_files(td);
650 td_verror(td, errno, "ioring_register_files");
658 static int fio_ioring_init(struct thread_data *td)
660 struct ioring_options *o = td->eo;
661 struct ioring_data *ld;
662 struct thread_options *to = &td->o;
664 /* sqthread submission requires registered files */
665 if (o->sqpoll_thread)
666 o->registerfiles = 1;
668 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
669 log_err("fio: io_uring registered files require nr_files to "
670 "be identical to open_files\n");
674 ld = calloc(1, sizeof(*ld));
676 /* ring depth must be a power-of-2 */
677 ld->iodepth = td->o.iodepth;
678 td->o.iodepth = roundup_pow2(td->o.iodepth);
681 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
682 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
684 td->io_ops_data = ld;
687 * Check for option conflicts
689 if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
690 o->cmdprio_percentage != 0) {
691 log_err("%s: cmdprio_percentage option and mutually exclusive "
692 "prio or prioclass option is set, exiting\n", to->name);
693 td_verror(td, EINVAL, "fio_io_uring_init");
697 if (fio_option_is_set(&td->o, ioprio_class))
698 ld->ioprio_class_set = true;
699 if (fio_option_is_set(&td->o, ioprio))
700 ld->ioprio_set = true;
705 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
707 struct ioring_data *ld = td->io_ops_data;
709 ld->io_u_index[io_u->index] = io_u;
713 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
715 struct ioring_data *ld = td->io_ops_data;
716 struct ioring_options *o = td->eo;
718 if (!ld || !o->registerfiles)
719 return generic_open_file(td, f);
721 f->fd = ld->fds[f->engine_pos];
725 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
727 struct ioring_data *ld = td->io_ops_data;
728 struct ioring_options *o = td->eo;
730 if (!ld || !o->registerfiles)
731 return generic_close_file(td, f);
737 static struct ioengine_ops ioengine = {
739 .version = FIO_IOOPS_VERSION,
740 .flags = FIO_ASYNCIO_SYNC_TRIM,
741 .init = fio_ioring_init,
742 .post_init = fio_ioring_post_init,
743 .io_u_init = fio_ioring_io_u_init,
744 .prep = fio_ioring_prep,
745 .queue = fio_ioring_queue,
746 .commit = fio_ioring_commit,
747 .getevents = fio_ioring_getevents,
748 .event = fio_ioring_event,
749 .cleanup = fio_ioring_cleanup,
750 .open_file = fio_ioring_open_file,
751 .close_file = fio_ioring_close_file,
752 .get_file_size = generic_get_file_size,
754 .option_struct_size = sizeof(struct ioring_options),
757 static void fio_init fio_ioring_register(void)
759 register_ioengine(&ioengine);
762 static void fio_exit fio_ioring_unregister(void)
764 unregister_ioengine(&ioengine);