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 int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
89 struct ioring_options *o = data;
96 static struct fio_option options[] = {
99 .lname = "High Priority",
100 .type = FIO_OPT_STR_SET,
101 .off1 = offsetof(struct ioring_options, hipri),
102 .help = "Use polled IO completions",
103 .category = FIO_OPT_C_ENGINE,
104 .group = FIO_OPT_G_IOURING,
108 .lname = "Fixed (pre-mapped) IO buffers",
109 .type = FIO_OPT_STR_SET,
110 .off1 = offsetof(struct ioring_options, fixedbufs),
111 .help = "Pre map IO buffers",
112 .category = FIO_OPT_C_ENGINE,
113 .group = FIO_OPT_G_IOURING,
116 .name = "registerfiles",
117 .lname = "Register file set",
118 .type = FIO_OPT_STR_SET,
119 .off1 = offsetof(struct ioring_options, registerfiles),
120 .help = "Pre-open/register files",
121 .category = FIO_OPT_C_ENGINE,
122 .group = FIO_OPT_G_IOURING,
125 .name = "sqthread_poll",
126 .lname = "Kernel SQ thread polling",
128 .off1 = offsetof(struct ioring_options, sqpoll_thread),
129 .help = "Offload submission/completion to kernel thread",
130 .category = FIO_OPT_C_ENGINE,
131 .group = FIO_OPT_G_IOURING,
134 .name = "sqthread_poll_cpu",
135 .lname = "SQ Thread Poll CPU",
137 .cb = fio_ioring_sqpoll_cb,
138 .help = "What CPU to run SQ thread polling on",
139 .category = FIO_OPT_C_ENGINE,
140 .group = FIO_OPT_G_IOURING,
143 .name = "nonvectored",
144 .lname = "Non-vectored",
146 .off1 = offsetof(struct ioring_options, nonvectored),
147 .help = "Use non-vectored read/write commands",
148 .category = FIO_OPT_C_ENGINE,
149 .group = FIO_OPT_G_IOURING,
155 .off1 = offsetof(struct ioring_options, uncached),
156 .help = "Use RWF_UNCACHED for buffered read/writes",
157 .category = FIO_OPT_C_ENGINE,
158 .group = FIO_OPT_G_IOURING,
165 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
166 unsigned int min_complete, unsigned int flags)
168 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
169 min_complete, flags, NULL, 0);
172 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
174 struct ioring_data *ld = td->io_ops_data;
175 struct ioring_options *o = td->eo;
176 struct fio_file *f = io_u->file;
177 struct io_uring_sqe *sqe;
179 sqe = &ld->sqes[io_u->index];
181 /* zero out fields not used in this submission */
182 memset(sqe, 0, sizeof(*sqe));
184 if (o->registerfiles) {
185 sqe->fd = f->engine_pos;
186 sqe->flags = IOSQE_FIXED_FILE;
191 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
192 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
194 sqe->addr = (unsigned long) io_u->xfer_buf;
195 sqe->len = io_u->xfer_buflen;
196 sqe->buf_index = io_u->index;
198 if (o->nonvectored) {
199 sqe->addr = (unsigned long)
200 ld->iovecs[io_u->index].iov_base;
201 sqe->len = ld->iovecs[io_u->index].iov_len;
203 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
207 if (!td->o.odirect && o->uncached)
208 sqe->rw_flags = RWF_UNCACHED;
209 if (fio_option_is_set(&td->o, ioprio_class))
210 sqe->ioprio = td->o.ioprio_class << 13;
211 if (fio_option_is_set(&td->o, ioprio))
212 sqe->ioprio |= td->o.ioprio;
213 sqe->off = io_u->offset;
214 } else if (ddir_sync(io_u->ddir)) {
215 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
216 sqe->off = f->first_write;
217 sqe->len = f->last_write - f->first_write;
218 sqe->sync_range_flags = td->o.sync_file_range;
219 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
221 if (io_u->ddir == DDIR_DATASYNC)
222 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
223 sqe->opcode = IORING_OP_FSYNC;
227 sqe->user_data = (unsigned long) io_u;
231 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
233 struct ioring_data *ld = td->io_ops_data;
234 struct io_uring_cqe *cqe;
238 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
240 cqe = &ld->cq_ring.cqes[index];
241 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
243 if (cqe->res != io_u->xfer_buflen) {
244 if (cqe->res > io_u->xfer_buflen)
245 io_u->error = -cqe->res;
247 io_u->resid = io_u->xfer_buflen - cqe->res;
254 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
257 struct ioring_data *ld = td->io_ops_data;
258 struct io_cq_ring *ring = &ld->cq_ring;
259 unsigned head, reaped = 0;
264 if (head == *ring->tail)
268 } while (reaped + events < max);
275 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
276 unsigned int max, const struct timespec *t)
278 struct ioring_data *ld = td->io_ops_data;
279 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
280 struct ioring_options *o = td->eo;
281 struct io_cq_ring *ring = &ld->cq_ring;
285 ld->cq_ring_off = *ring->head;
287 r = fio_ioring_cqring_reap(td, events, max);
295 if (!o->sqpoll_thread) {
296 r = io_uring_enter(ld, 0, actual_min,
297 IORING_ENTER_GETEVENTS);
299 if (errno == EAGAIN || errno == EINTR)
301 td_verror(td, errno, "io_uring_enter");
305 } while (events < min);
307 return r < 0 ? r : events;
310 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
313 struct ioring_data *ld = td->io_ops_data;
314 struct io_sq_ring *ring = &ld->sq_ring;
315 unsigned tail, next_tail;
317 fio_ro_check(td, io_u);
319 if (ld->queued == ld->iodepth)
322 if (io_u->ddir == DDIR_TRIM) {
326 do_io_u_trim(td, io_u);
327 io_u_mark_submit(td, 1);
328 io_u_mark_complete(td, 1);
329 return FIO_Q_COMPLETED;
333 next_tail = tail + 1;
335 if (next_tail == *ring->head)
338 /* ensure sqe stores are ordered with tail update */
340 ring->array[tail & ld->sq_ring_mask] = io_u->index;
341 *ring->tail = next_tail;
348 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
350 struct ioring_data *ld = td->io_ops_data;
353 if (!fio_fill_issue_time(td))
356 fio_gettime(&now, NULL);
359 struct io_sq_ring *ring = &ld->sq_ring;
360 int index = ring->array[start & ld->sq_ring_mask];
361 struct io_u *io_u = ld->io_u_index[index];
363 memcpy(&io_u->issue_time, &now, sizeof(now));
364 io_u_queued(td, io_u);
370 static int fio_ioring_commit(struct thread_data *td)
372 struct ioring_data *ld = td->io_ops_data;
373 struct ioring_options *o = td->eo;
380 * Kernel side does submission. just need to check if the ring is
381 * flagged as needing a kick, if so, call io_uring_enter(). This
382 * only happens if we've been idle too long.
384 if (o->sqpoll_thread) {
385 struct io_sq_ring *ring = &ld->sq_ring;
388 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
389 io_uring_enter(ld, ld->queued, 0,
390 IORING_ENTER_SQ_WAKEUP);
396 unsigned start = *ld->sq_ring.head;
397 long nr = ld->queued;
399 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
401 fio_ioring_queued(td, start, ret);
402 io_u_mark_submit(td, ret);
407 io_u_mark_submit(td, ret);
410 if (errno == EAGAIN || errno == EINTR) {
411 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
414 /* Shouldn't happen */
418 td_verror(td, errno, "io_uring_enter submit");
421 } while (ld->queued);
426 static void fio_ioring_unmap(struct ioring_data *ld)
430 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
431 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
435 static void fio_ioring_cleanup(struct thread_data *td)
437 struct ioring_data *ld = td->io_ops_data;
440 if (!(td->flags & TD_F_CHILD))
441 fio_ioring_unmap(ld);
443 free(ld->io_u_index);
450 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
452 struct io_sq_ring *sring = &ld->sq_ring;
453 struct io_cq_ring *cring = &ld->cq_ring;
456 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
457 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
458 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
460 ld->mmap[0].ptr = ptr;
461 sring->head = ptr + p->sq_off.head;
462 sring->tail = ptr + p->sq_off.tail;
463 sring->ring_mask = ptr + p->sq_off.ring_mask;
464 sring->ring_entries = ptr + p->sq_off.ring_entries;
465 sring->flags = ptr + p->sq_off.flags;
466 sring->array = ptr + p->sq_off.array;
467 ld->sq_ring_mask = *sring->ring_mask;
469 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
470 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
471 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
473 ld->mmap[1].ptr = ld->sqes;
475 ld->mmap[2].len = p->cq_off.cqes +
476 p->cq_entries * sizeof(struct io_uring_cqe);
477 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
478 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
480 ld->mmap[2].ptr = ptr;
481 cring->head = ptr + p->cq_off.head;
482 cring->tail = ptr + p->cq_off.tail;
483 cring->ring_mask = ptr + p->cq_off.ring_mask;
484 cring->ring_entries = ptr + p->cq_off.ring_entries;
485 cring->cqes = ptr + p->cq_off.cqes;
486 ld->cq_ring_mask = *cring->ring_mask;
490 static int fio_ioring_queue_init(struct thread_data *td)
492 struct ioring_data *ld = td->io_ops_data;
493 struct ioring_options *o = td->eo;
494 int depth = td->o.iodepth;
495 struct io_uring_params p;
498 memset(&p, 0, sizeof(p));
501 p.flags |= IORING_SETUP_IOPOLL;
502 if (o->sqpoll_thread) {
503 p.flags |= IORING_SETUP_SQPOLL;
505 p.flags |= IORING_SETUP_SQ_AFF;
506 p.sq_thread_cpu = o->sqpoll_cpu;
510 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
517 struct rlimit rlim = {
518 .rlim_cur = RLIM_INFINITY,
519 .rlim_max = RLIM_INFINITY,
522 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
525 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
526 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
531 return fio_ioring_mmap(ld, &p);
534 static int fio_ioring_register_files(struct thread_data *td)
536 struct ioring_data *ld = td->io_ops_data;
541 ld->fds = calloc(td->o.nr_files, sizeof(int));
543 for_each_file(td, f, i) {
544 ret = generic_open_file(td, f);
551 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
552 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
560 * Pretend the file is closed again, and really close it if we hit
563 for_each_file(td, f, i) {
566 ret2 = generic_close_file(td, f);
574 static int fio_ioring_post_init(struct thread_data *td)
576 struct ioring_data *ld = td->io_ops_data;
577 struct ioring_options *o = td->eo;
581 for (i = 0; i < td->o.iodepth; i++) {
582 struct iovec *iov = &ld->iovecs[i];
584 io_u = ld->io_u_index[i];
585 iov->iov_base = io_u->buf;
586 iov->iov_len = td_max_bs(td);
589 err = fio_ioring_queue_init(td);
591 td_verror(td, errno, "io_queue_init");
595 if (o->registerfiles) {
596 err = fio_ioring_register_files(td);
598 td_verror(td, errno, "ioring_register_files");
606 static unsigned roundup_pow2(unsigned depth)
608 return 1UL << __fls(depth - 1);
611 static int fio_ioring_init(struct thread_data *td)
613 struct ioring_options *o = td->eo;
614 struct ioring_data *ld;
616 /* sqthread submission requires registered files */
617 if (o->sqpoll_thread)
618 o->registerfiles = 1;
620 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
621 log_err("fio: io_uring registered files require nr_files to "
622 "be identical to open_files\n");
626 ld = calloc(1, sizeof(*ld));
628 /* ring depth must be a power-of-2 */
629 ld->iodepth = td->o.iodepth;
630 td->o.iodepth = roundup_pow2(td->o.iodepth);
633 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
634 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
636 td->io_ops_data = ld;
640 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
642 struct ioring_data *ld = td->io_ops_data;
644 ld->io_u_index[io_u->index] = io_u;
648 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
650 struct ioring_data *ld = td->io_ops_data;
651 struct ioring_options *o = td->eo;
653 if (!ld || !o->registerfiles)
654 return generic_open_file(td, f);
656 f->fd = ld->fds[f->engine_pos];
660 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
662 struct ioring_data *ld = td->io_ops_data;
663 struct ioring_options *o = td->eo;
665 if (!ld || !o->registerfiles)
666 return generic_close_file(td, f);
672 static struct ioengine_ops ioengine = {
674 .version = FIO_IOOPS_VERSION,
675 .flags = FIO_ASYNCIO_SYNC_TRIM,
676 .init = fio_ioring_init,
677 .post_init = fio_ioring_post_init,
678 .io_u_init = fio_ioring_io_u_init,
679 .prep = fio_ioring_prep,
680 .queue = fio_ioring_queue,
681 .commit = fio_ioring_commit,
682 .getevents = fio_ioring_getevents,
683 .event = fio_ioring_event,
684 .cleanup = fio_ioring_cleanup,
685 .open_file = fio_ioring_open_file,
686 .close_file = fio_ioring_close_file,
687 .get_file_size = generic_get_file_size,
689 .option_struct_size = sizeof(struct ioring_options),
692 static void fio_init fio_ioring_register(void)
694 register_ioengine(&ioengine);
697 static void fio_exit fio_ioring_unregister(void)
699 unregister_ioengine(&ioengine);