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;
67 struct ioring_mmap mmap[3];
70 struct ioring_options {
73 unsigned int fixedbufs;
74 unsigned int registerfiles;
75 unsigned int sqpoll_thread;
76 unsigned int sqpoll_set;
77 unsigned int sqpoll_cpu;
78 unsigned int nonvectored;
79 unsigned int uncached;
82 static const int ddir_to_op[2][2] = {
83 { IORING_OP_READV, IORING_OP_READ },
84 { IORING_OP_WRITEV, IORING_OP_WRITE }
87 static const int fixed_ddir_to_op[2] = {
92 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
94 struct ioring_options *o = data;
101 static struct fio_option options[] = {
104 .lname = "High Priority",
105 .type = FIO_OPT_STR_SET,
106 .off1 = offsetof(struct ioring_options, hipri),
107 .help = "Use polled IO completions",
108 .category = FIO_OPT_C_ENGINE,
109 .group = FIO_OPT_G_IOURING,
113 .lname = "Fixed (pre-mapped) IO buffers",
114 .type = FIO_OPT_STR_SET,
115 .off1 = offsetof(struct ioring_options, fixedbufs),
116 .help = "Pre map IO buffers",
117 .category = FIO_OPT_C_ENGINE,
118 .group = FIO_OPT_G_IOURING,
121 .name = "registerfiles",
122 .lname = "Register file set",
123 .type = FIO_OPT_STR_SET,
124 .off1 = offsetof(struct ioring_options, registerfiles),
125 .help = "Pre-open/register files",
126 .category = FIO_OPT_C_ENGINE,
127 .group = FIO_OPT_G_IOURING,
130 .name = "sqthread_poll",
131 .lname = "Kernel SQ thread polling",
133 .off1 = offsetof(struct ioring_options, sqpoll_thread),
134 .help = "Offload submission/completion to kernel thread",
135 .category = FIO_OPT_C_ENGINE,
136 .group = FIO_OPT_G_IOURING,
139 .name = "sqthread_poll_cpu",
140 .lname = "SQ Thread Poll CPU",
142 .cb = fio_ioring_sqpoll_cb,
143 .help = "What CPU to run SQ thread polling on",
144 .category = FIO_OPT_C_ENGINE,
145 .group = FIO_OPT_G_IOURING,
148 .name = "nonvectored",
149 .lname = "Non-vectored",
151 .off1 = offsetof(struct ioring_options, nonvectored),
152 .help = "Use non-vectored read/write commands",
153 .category = FIO_OPT_C_ENGINE,
154 .group = FIO_OPT_G_IOURING,
160 .off1 = offsetof(struct ioring_options, uncached),
161 .help = "Use RWF_UNCACHED for buffered read/writes",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_IOURING,
170 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
171 unsigned int min_complete, unsigned int flags)
173 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
174 min_complete, flags, NULL, 0);
177 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
179 struct ioring_data *ld = td->io_ops_data;
180 struct ioring_options *o = td->eo;
181 struct fio_file *f = io_u->file;
182 struct io_uring_sqe *sqe;
184 sqe = &ld->sqes[io_u->index];
186 /* zero out fields not used in this submission */
187 memset(sqe, 0, sizeof(*sqe));
189 if (o->registerfiles) {
190 sqe->fd = f->engine_pos;
191 sqe->flags = IOSQE_FIXED_FILE;
196 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
198 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
199 sqe->addr = (unsigned long) io_u->xfer_buf;
200 sqe->len = io_u->xfer_buflen;
201 sqe->buf_index = io_u->index;
203 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
204 if (o->nonvectored) {
205 sqe->addr = (unsigned long)
206 ld->iovecs[io_u->index].iov_base;
207 sqe->len = ld->iovecs[io_u->index].iov_len;
209 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
213 if (!td->o.odirect && o->uncached)
214 sqe->rw_flags = RWF_UNCACHED;
215 if (fio_option_is_set(&td->o, ioprio_class))
216 sqe->ioprio = td->o.ioprio_class << 13;
217 if (fio_option_is_set(&td->o, ioprio))
218 sqe->ioprio |= td->o.ioprio;
219 sqe->off = io_u->offset;
220 } else if (ddir_sync(io_u->ddir)) {
221 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
222 sqe->off = f->first_write;
223 sqe->len = f->last_write - f->first_write;
224 sqe->sync_range_flags = td->o.sync_file_range;
225 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
227 if (io_u->ddir == DDIR_DATASYNC)
228 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
229 sqe->opcode = IORING_OP_FSYNC;
233 sqe->user_data = (unsigned long) io_u;
237 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
239 struct ioring_data *ld = td->io_ops_data;
240 struct io_uring_cqe *cqe;
244 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
246 cqe = &ld->cq_ring.cqes[index];
247 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
249 if (cqe->res != io_u->xfer_buflen) {
250 if (cqe->res > io_u->xfer_buflen)
251 io_u->error = -cqe->res;
253 io_u->resid = io_u->xfer_buflen - cqe->res;
260 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
263 struct ioring_data *ld = td->io_ops_data;
264 struct io_cq_ring *ring = &ld->cq_ring;
265 unsigned head, reaped = 0;
270 if (head == *ring->tail)
274 } while (reaped + events < max);
281 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
282 unsigned int max, const struct timespec *t)
284 struct ioring_data *ld = td->io_ops_data;
285 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
286 struct ioring_options *o = td->eo;
287 struct io_cq_ring *ring = &ld->cq_ring;
291 ld->cq_ring_off = *ring->head;
293 r = fio_ioring_cqring_reap(td, events, max);
301 if (!o->sqpoll_thread) {
302 r = io_uring_enter(ld, 0, actual_min,
303 IORING_ENTER_GETEVENTS);
305 if (errno == EAGAIN || errno == EINTR)
307 td_verror(td, errno, "io_uring_enter");
311 } while (events < min);
313 return r < 0 ? r : events;
316 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
319 struct ioring_data *ld = td->io_ops_data;
320 struct io_sq_ring *ring = &ld->sq_ring;
321 unsigned tail, next_tail;
323 fio_ro_check(td, io_u);
325 if (ld->queued == ld->iodepth)
328 if (io_u->ddir == DDIR_TRIM) {
332 do_io_u_trim(td, io_u);
333 io_u_mark_submit(td, 1);
334 io_u_mark_complete(td, 1);
335 return FIO_Q_COMPLETED;
339 next_tail = tail + 1;
341 if (next_tail == *ring->head)
344 /* ensure sqe stores are ordered with tail update */
346 ring->array[tail & ld->sq_ring_mask] = io_u->index;
347 *ring->tail = next_tail;
354 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
356 struct ioring_data *ld = td->io_ops_data;
359 if (!fio_fill_issue_time(td))
362 fio_gettime(&now, NULL);
365 struct io_sq_ring *ring = &ld->sq_ring;
366 int index = ring->array[start & ld->sq_ring_mask];
367 struct io_u *io_u = ld->io_u_index[index];
369 memcpy(&io_u->issue_time, &now, sizeof(now));
370 io_u_queued(td, io_u);
376 static int fio_ioring_commit(struct thread_data *td)
378 struct ioring_data *ld = td->io_ops_data;
379 struct ioring_options *o = td->eo;
386 * Kernel side does submission. just need to check if the ring is
387 * flagged as needing a kick, if so, call io_uring_enter(). This
388 * only happens if we've been idle too long.
390 if (o->sqpoll_thread) {
391 struct io_sq_ring *ring = &ld->sq_ring;
394 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
395 io_uring_enter(ld, ld->queued, 0,
396 IORING_ENTER_SQ_WAKEUP);
402 unsigned start = *ld->sq_ring.head;
403 long nr = ld->queued;
405 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
407 fio_ioring_queued(td, start, ret);
408 io_u_mark_submit(td, ret);
413 io_u_mark_submit(td, ret);
416 if (errno == EAGAIN || errno == EINTR) {
417 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
420 /* Shouldn't happen */
424 td_verror(td, errno, "io_uring_enter submit");
427 } while (ld->queued);
432 static void fio_ioring_unmap(struct ioring_data *ld)
436 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
437 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
441 static void fio_ioring_cleanup(struct thread_data *td)
443 struct ioring_data *ld = td->io_ops_data;
446 if (!(td->flags & TD_F_CHILD))
447 fio_ioring_unmap(ld);
449 free(ld->io_u_index);
456 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
458 struct io_sq_ring *sring = &ld->sq_ring;
459 struct io_cq_ring *cring = &ld->cq_ring;
462 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
463 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
464 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
466 ld->mmap[0].ptr = ptr;
467 sring->head = ptr + p->sq_off.head;
468 sring->tail = ptr + p->sq_off.tail;
469 sring->ring_mask = ptr + p->sq_off.ring_mask;
470 sring->ring_entries = ptr + p->sq_off.ring_entries;
471 sring->flags = ptr + p->sq_off.flags;
472 sring->array = ptr + p->sq_off.array;
473 ld->sq_ring_mask = *sring->ring_mask;
475 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
476 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
477 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
479 ld->mmap[1].ptr = ld->sqes;
481 ld->mmap[2].len = p->cq_off.cqes +
482 p->cq_entries * sizeof(struct io_uring_cqe);
483 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
484 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
486 ld->mmap[2].ptr = ptr;
487 cring->head = ptr + p->cq_off.head;
488 cring->tail = ptr + p->cq_off.tail;
489 cring->ring_mask = ptr + p->cq_off.ring_mask;
490 cring->ring_entries = ptr + p->cq_off.ring_entries;
491 cring->cqes = ptr + p->cq_off.cqes;
492 ld->cq_ring_mask = *cring->ring_mask;
496 static int fio_ioring_queue_init(struct thread_data *td)
498 struct ioring_data *ld = td->io_ops_data;
499 struct ioring_options *o = td->eo;
500 int depth = td->o.iodepth;
501 struct io_uring_params p;
504 memset(&p, 0, sizeof(p));
507 p.flags |= IORING_SETUP_IOPOLL;
508 if (o->sqpoll_thread) {
509 p.flags |= IORING_SETUP_SQPOLL;
511 p.flags |= IORING_SETUP_SQ_AFF;
512 p.sq_thread_cpu = o->sqpoll_cpu;
516 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
523 struct rlimit rlim = {
524 .rlim_cur = RLIM_INFINITY,
525 .rlim_max = RLIM_INFINITY,
528 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
531 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
532 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
537 return fio_ioring_mmap(ld, &p);
540 static int fio_ioring_register_files(struct thread_data *td)
542 struct ioring_data *ld = td->io_ops_data;
547 ld->fds = calloc(td->o.nr_files, sizeof(int));
549 for_each_file(td, f, i) {
550 ret = generic_open_file(td, f);
557 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
558 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
566 * Pretend the file is closed again, and really close it if we hit
569 for_each_file(td, f, i) {
572 ret2 = generic_close_file(td, f);
580 static int fio_ioring_post_init(struct thread_data *td)
582 struct ioring_data *ld = td->io_ops_data;
583 struct ioring_options *o = td->eo;
587 for (i = 0; i < td->o.iodepth; i++) {
588 struct iovec *iov = &ld->iovecs[i];
590 io_u = ld->io_u_index[i];
591 iov->iov_base = io_u->buf;
592 iov->iov_len = td_max_bs(td);
595 err = fio_ioring_queue_init(td);
597 td_verror(td, errno, "io_queue_init");
601 if (o->registerfiles) {
602 err = fio_ioring_register_files(td);
604 td_verror(td, errno, "ioring_register_files");
612 static unsigned roundup_pow2(unsigned depth)
614 return 1UL << __fls(depth - 1);
617 static int fio_ioring_init(struct thread_data *td)
619 struct ioring_options *o = td->eo;
620 struct ioring_data *ld;
622 /* sqthread submission requires registered files */
623 if (o->sqpoll_thread)
624 o->registerfiles = 1;
626 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
627 log_err("fio: io_uring registered files require nr_files to "
628 "be identical to open_files\n");
632 ld = calloc(1, sizeof(*ld));
634 /* ring depth must be a power-of-2 */
635 ld->iodepth = td->o.iodepth;
636 td->o.iodepth = roundup_pow2(td->o.iodepth);
639 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
640 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
642 td->io_ops_data = ld;
646 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
648 struct ioring_data *ld = td->io_ops_data;
650 ld->io_u_index[io_u->index] = io_u;
654 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
656 struct ioring_data *ld = td->io_ops_data;
657 struct ioring_options *o = td->eo;
659 if (!ld || !o->registerfiles)
660 return generic_open_file(td, f);
662 f->fd = ld->fds[f->engine_pos];
666 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
668 struct ioring_data *ld = td->io_ops_data;
669 struct ioring_options *o = td->eo;
671 if (!ld || !o->registerfiles)
672 return generic_close_file(td, f);
678 static struct ioengine_ops ioengine = {
680 .version = FIO_IOOPS_VERSION,
681 .flags = FIO_ASYNCIO_SYNC_TRIM,
682 .init = fio_ioring_init,
683 .post_init = fio_ioring_post_init,
684 .io_u_init = fio_ioring_io_u_init,
685 .prep = fio_ioring_prep,
686 .queue = fio_ioring_queue,
687 .commit = fio_ioring_commit,
688 .getevents = fio_ioring_getevents,
689 .event = fio_ioring_event,
690 .cleanup = fio_ioring_cleanup,
691 .open_file = fio_ioring_open_file,
692 .close_file = fio_ioring_close_file,
693 .get_file_size = generic_get_file_size,
695 .option_struct_size = sizeof(struct ioring_options),
698 static void fio_init fio_ioring_register(void)
700 register_ioengine(&ioengine);
703 static void fio_exit fio_ioring_unregister(void)
705 unregister_ioengine(&ioengine);