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;
86 static const int ddir_to_op[2][2] = {
87 { IORING_OP_READV, IORING_OP_READ },
88 { IORING_OP_WRITEV, IORING_OP_WRITE }
91 static const int fixed_ddir_to_op[2] = {
96 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
98 struct ioring_options *o = data;
100 o->sqpoll_cpu = *val;
105 static struct fio_option options[] = {
108 .lname = "High Priority",
109 .type = FIO_OPT_STR_SET,
110 .off1 = offsetof(struct ioring_options, hipri),
111 .help = "Use polled IO completions",
112 .category = FIO_OPT_C_ENGINE,
113 .group = FIO_OPT_G_IOURING,
115 #ifdef FIO_HAVE_IOPRIO_CLASS
117 .name = "cmdprio_percentage",
118 .lname = "high priority percentage",
120 .off1 = offsetof(struct ioring_options, cmdprio_percentage),
123 .help = "Send high priority I/O this percentage of the time",
124 .category = FIO_OPT_C_ENGINE,
125 .group = FIO_OPT_G_IOURING,
129 .name = "cmdprio_percentage",
130 .lname = "high priority percentage",
131 .type = FIO_OPT_UNSUPPORTED,
132 .help = "Your platform does not support I/O priority classes",
137 .lname = "Fixed (pre-mapped) IO buffers",
138 .type = FIO_OPT_STR_SET,
139 .off1 = offsetof(struct ioring_options, fixedbufs),
140 .help = "Pre map IO buffers",
141 .category = FIO_OPT_C_ENGINE,
142 .group = FIO_OPT_G_IOURING,
145 .name = "registerfiles",
146 .lname = "Register file set",
147 .type = FIO_OPT_STR_SET,
148 .off1 = offsetof(struct ioring_options, registerfiles),
149 .help = "Pre-open/register files",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_IOURING,
154 .name = "sqthread_poll",
155 .lname = "Kernel SQ thread polling",
157 .off1 = offsetof(struct ioring_options, sqpoll_thread),
158 .help = "Offload submission/completion to kernel thread",
159 .category = FIO_OPT_C_ENGINE,
160 .group = FIO_OPT_G_IOURING,
163 .name = "sqthread_poll_cpu",
164 .lname = "SQ Thread Poll CPU",
166 .cb = fio_ioring_sqpoll_cb,
167 .help = "What CPU to run SQ thread polling on",
168 .category = FIO_OPT_C_ENGINE,
169 .group = FIO_OPT_G_IOURING,
172 .name = "nonvectored",
173 .lname = "Non-vectored",
175 .off1 = offsetof(struct ioring_options, nonvectored),
176 .help = "Use non-vectored read/write commands",
177 .category = FIO_OPT_C_ENGINE,
178 .group = FIO_OPT_G_IOURING,
184 .off1 = offsetof(struct ioring_options, uncached),
185 .help = "Use RWF_UNCACHED for buffered read/writes",
186 .category = FIO_OPT_C_ENGINE,
187 .group = FIO_OPT_G_IOURING,
191 .lname = "RWF_NOWAIT",
192 .type = FIO_OPT_BOOL,
193 .off1 = offsetof(struct ioring_options, nowait),
194 .help = "Use RWF_NOWAIT for reads/writes",
195 .category = FIO_OPT_C_ENGINE,
196 .group = FIO_OPT_G_IOURING,
203 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
204 unsigned int min_complete, unsigned int flags)
206 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
207 min_complete, flags, NULL, 0);
210 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
212 struct ioring_data *ld = td->io_ops_data;
213 struct ioring_options *o = td->eo;
214 struct fio_file *f = io_u->file;
215 struct io_uring_sqe *sqe;
217 sqe = &ld->sqes[io_u->index];
219 /* zero out fields not used in this submission */
220 memset(sqe, 0, sizeof(*sqe));
222 if (o->registerfiles) {
223 sqe->fd = f->engine_pos;
224 sqe->flags = IOSQE_FIXED_FILE;
229 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
231 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
232 sqe->addr = (unsigned long) io_u->xfer_buf;
233 sqe->len = io_u->xfer_buflen;
234 sqe->buf_index = io_u->index;
236 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
237 if (o->nonvectored) {
238 sqe->addr = (unsigned long)
239 ld->iovecs[io_u->index].iov_base;
240 sqe->len = ld->iovecs[io_u->index].iov_len;
242 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
246 if (!td->o.odirect && o->uncached)
247 sqe->rw_flags = RWF_UNCACHED;
249 sqe->rw_flags |= RWF_NOWAIT;
250 if (ld->ioprio_class_set)
251 sqe->ioprio = td->o.ioprio_class << 13;
253 sqe->ioprio |= td->o.ioprio;
254 sqe->off = io_u->offset;
255 } else if (ddir_sync(io_u->ddir)) {
256 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
257 sqe->off = f->first_write;
258 sqe->len = f->last_write - f->first_write;
259 sqe->sync_range_flags = td->o.sync_file_range;
260 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
262 if (io_u->ddir == DDIR_DATASYNC)
263 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
264 sqe->opcode = IORING_OP_FSYNC;
268 sqe->user_data = (unsigned long) io_u;
272 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
274 struct ioring_data *ld = td->io_ops_data;
275 struct io_uring_cqe *cqe;
279 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
281 cqe = &ld->cq_ring.cqes[index];
282 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
284 if (cqe->res != io_u->xfer_buflen) {
285 if (cqe->res > io_u->xfer_buflen)
286 io_u->error = -cqe->res;
288 io_u->resid = io_u->xfer_buflen - cqe->res;
295 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
298 struct ioring_data *ld = td->io_ops_data;
299 struct io_cq_ring *ring = &ld->cq_ring;
300 unsigned head, reaped = 0;
304 if (head == atomic_load_acquire(ring->tail))
308 } while (reaped + events < max);
310 atomic_store_release(ring->head, head);
314 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
315 unsigned int max, const struct timespec *t)
317 struct ioring_data *ld = td->io_ops_data;
318 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
319 struct ioring_options *o = td->eo;
320 struct io_cq_ring *ring = &ld->cq_ring;
324 ld->cq_ring_off = *ring->head;
326 r = fio_ioring_cqring_reap(td, events, max);
334 if (!o->sqpoll_thread) {
335 r = io_uring_enter(ld, 0, actual_min,
336 IORING_ENTER_GETEVENTS);
338 if (errno == EAGAIN || errno == EINTR)
340 td_verror(td, errno, "io_uring_enter");
344 } while (events < min);
346 return r < 0 ? r : events;
349 static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
351 struct ioring_options *o = td->eo;
352 struct ioring_data *ld = td->io_ops_data;
353 if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
354 ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
355 io_u->flags |= IO_U_F_PRIORITY;
360 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
363 struct ioring_data *ld = td->io_ops_data;
364 struct io_sq_ring *ring = &ld->sq_ring;
365 struct ioring_options *o = td->eo;
366 unsigned tail, next_tail;
368 fio_ro_check(td, io_u);
370 if (ld->queued == ld->iodepth)
373 if (io_u->ddir == DDIR_TRIM) {
377 do_io_u_trim(td, io_u);
378 io_u_mark_submit(td, 1);
379 io_u_mark_complete(td, 1);
380 return FIO_Q_COMPLETED;
384 next_tail = tail + 1;
385 if (next_tail == atomic_load_acquire(ring->head))
388 if (o->cmdprio_percentage)
389 fio_ioring_prio_prep(td, io_u);
390 ring->array[tail & ld->sq_ring_mask] = io_u->index;
391 atomic_store_release(ring->tail, next_tail);
397 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
399 struct ioring_data *ld = td->io_ops_data;
402 if (!fio_fill_issue_time(td))
405 fio_gettime(&now, NULL);
408 struct io_sq_ring *ring = &ld->sq_ring;
409 int index = ring->array[start & ld->sq_ring_mask];
410 struct io_u *io_u = ld->io_u_index[index];
412 memcpy(&io_u->issue_time, &now, sizeof(now));
413 io_u_queued(td, io_u);
419 static int fio_ioring_commit(struct thread_data *td)
421 struct ioring_data *ld = td->io_ops_data;
422 struct ioring_options *o = td->eo;
429 * Kernel side does submission. just need to check if the ring is
430 * flagged as needing a kick, if so, call io_uring_enter(). This
431 * only happens if we've been idle too long.
433 if (o->sqpoll_thread) {
434 struct io_sq_ring *ring = &ld->sq_ring;
437 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
438 io_uring_enter(ld, ld->queued, 0,
439 IORING_ENTER_SQ_WAKEUP);
445 unsigned start = *ld->sq_ring.head;
446 long nr = ld->queued;
448 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
450 fio_ioring_queued(td, start, ret);
451 io_u_mark_submit(td, ret);
456 io_u_mark_submit(td, ret);
459 if (errno == EAGAIN || errno == EINTR) {
460 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
463 /* Shouldn't happen */
467 td_verror(td, errno, "io_uring_enter submit");
470 } while (ld->queued);
475 static void fio_ioring_unmap(struct ioring_data *ld)
479 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
480 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
484 static void fio_ioring_cleanup(struct thread_data *td)
486 struct ioring_data *ld = td->io_ops_data;
489 if (!(td->flags & TD_F_CHILD))
490 fio_ioring_unmap(ld);
492 free(ld->io_u_index);
499 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
501 struct io_sq_ring *sring = &ld->sq_ring;
502 struct io_cq_ring *cring = &ld->cq_ring;
505 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
506 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
507 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
509 ld->mmap[0].ptr = ptr;
510 sring->head = ptr + p->sq_off.head;
511 sring->tail = ptr + p->sq_off.tail;
512 sring->ring_mask = ptr + p->sq_off.ring_mask;
513 sring->ring_entries = ptr + p->sq_off.ring_entries;
514 sring->flags = ptr + p->sq_off.flags;
515 sring->array = ptr + p->sq_off.array;
516 ld->sq_ring_mask = *sring->ring_mask;
518 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
519 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
520 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
522 ld->mmap[1].ptr = ld->sqes;
524 ld->mmap[2].len = p->cq_off.cqes +
525 p->cq_entries * sizeof(struct io_uring_cqe);
526 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
527 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
529 ld->mmap[2].ptr = ptr;
530 cring->head = ptr + p->cq_off.head;
531 cring->tail = ptr + p->cq_off.tail;
532 cring->ring_mask = ptr + p->cq_off.ring_mask;
533 cring->ring_entries = ptr + p->cq_off.ring_entries;
534 cring->cqes = ptr + p->cq_off.cqes;
535 ld->cq_ring_mask = *cring->ring_mask;
539 static int fio_ioring_queue_init(struct thread_data *td)
541 struct ioring_data *ld = td->io_ops_data;
542 struct ioring_options *o = td->eo;
543 int depth = td->o.iodepth;
544 struct io_uring_params p;
547 memset(&p, 0, sizeof(p));
550 p.flags |= IORING_SETUP_IOPOLL;
551 if (o->sqpoll_thread) {
552 p.flags |= IORING_SETUP_SQPOLL;
554 p.flags |= IORING_SETUP_SQ_AFF;
555 p.sq_thread_cpu = o->sqpoll_cpu;
559 ret = syscall(__NR_io_uring_setup, depth, &p);
566 struct rlimit rlim = {
567 .rlim_cur = RLIM_INFINITY,
568 .rlim_max = RLIM_INFINITY,
571 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
574 ret = syscall(__NR_io_uring_register, ld->ring_fd,
575 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
580 return fio_ioring_mmap(ld, &p);
583 static int fio_ioring_register_files(struct thread_data *td)
585 struct ioring_data *ld = td->io_ops_data;
590 ld->fds = calloc(td->o.nr_files, sizeof(int));
592 for_each_file(td, f, i) {
593 ret = generic_open_file(td, f);
600 ret = syscall(__NR_io_uring_register, ld->ring_fd,
601 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
609 * Pretend the file is closed again, and really close it if we hit
612 for_each_file(td, f, i) {
615 ret2 = generic_close_file(td, f);
623 static int fio_ioring_post_init(struct thread_data *td)
625 struct ioring_data *ld = td->io_ops_data;
626 struct ioring_options *o = td->eo;
630 for (i = 0; i < td->o.iodepth; i++) {
631 struct iovec *iov = &ld->iovecs[i];
633 io_u = ld->io_u_index[i];
634 iov->iov_base = io_u->buf;
635 iov->iov_len = td_max_bs(td);
638 err = fio_ioring_queue_init(td);
640 td_verror(td, errno, "io_queue_init");
644 if (o->registerfiles) {
645 err = fio_ioring_register_files(td);
647 td_verror(td, errno, "ioring_register_files");
655 static unsigned roundup_pow2(unsigned depth)
657 return 1UL << __fls(depth - 1);
660 static int fio_ioring_init(struct thread_data *td)
662 struct ioring_options *o = td->eo;
663 struct ioring_data *ld;
664 struct thread_options *to = &td->o;
666 /* sqthread submission requires registered files */
667 if (o->sqpoll_thread)
668 o->registerfiles = 1;
670 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
671 log_err("fio: io_uring registered files require nr_files to "
672 "be identical to open_files\n");
676 ld = calloc(1, sizeof(*ld));
678 /* ring depth must be a power-of-2 */
679 ld->iodepth = td->o.iodepth;
680 td->o.iodepth = roundup_pow2(td->o.iodepth);
683 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
684 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
686 td->io_ops_data = ld;
689 * Check for option conflicts
691 if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
692 o->cmdprio_percentage != 0) {
693 log_err("%s: cmdprio_percentage option and mutually exclusive "
694 "prio or prioclass option is set, exiting\n", to->name);
695 td_verror(td, EINVAL, "fio_io_uring_init");
699 if (fio_option_is_set(&td->o, ioprio_class))
700 ld->ioprio_class_set = true;
701 if (fio_option_is_set(&td->o, ioprio))
702 ld->ioprio_set = true;
707 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
709 struct ioring_data *ld = td->io_ops_data;
711 ld->io_u_index[io_u->index] = io_u;
715 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
717 struct ioring_data *ld = td->io_ops_data;
718 struct ioring_options *o = td->eo;
720 if (!ld || !o->registerfiles)
721 return generic_open_file(td, f);
723 f->fd = ld->fds[f->engine_pos];
727 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
729 struct ioring_data *ld = td->io_ops_data;
730 struct ioring_options *o = td->eo;
732 if (!ld || !o->registerfiles)
733 return generic_close_file(td, f);
739 static struct ioengine_ops ioengine = {
741 .version = FIO_IOOPS_VERSION,
742 .flags = FIO_ASYNCIO_SYNC_TRIM,
743 .init = fio_ioring_init,
744 .post_init = fio_ioring_post_init,
745 .io_u_init = fio_ioring_io_u_init,
746 .prep = fio_ioring_prep,
747 .queue = fio_ioring_queue,
748 .commit = fio_ioring_commit,
749 .getevents = fio_ioring_getevents,
750 .event = fio_ioring_event,
751 .cleanup = fio_ioring_cleanup,
752 .open_file = fio_ioring_open_file,
753 .close_file = fio_ioring_close_file,
754 .get_file_size = generic_get_file_size,
756 .option_struct_size = sizeof(struct ioring_options),
759 static void fio_init fio_ioring_register(void)
761 register_ioengine(&ioengine);
764 static void fio_exit fio_ioring_unregister(void)
766 unregister_ioengine(&ioengine);