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 uncached;
81 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
83 struct ioring_options *o = data;
90 static struct fio_option options[] = {
93 .lname = "High Priority",
94 .type = FIO_OPT_STR_SET,
95 .off1 = offsetof(struct ioring_options, hipri),
96 .help = "Use polled IO completions",
97 .category = FIO_OPT_C_ENGINE,
98 .group = FIO_OPT_G_IOURING,
102 .lname = "Fixed (pre-mapped) IO buffers",
103 .type = FIO_OPT_STR_SET,
104 .off1 = offsetof(struct ioring_options, fixedbufs),
105 .help = "Pre map IO buffers",
106 .category = FIO_OPT_C_ENGINE,
107 .group = FIO_OPT_G_IOURING,
110 .name = "registerfiles",
111 .lname = "Register file set",
112 .type = FIO_OPT_STR_SET,
113 .off1 = offsetof(struct ioring_options, registerfiles),
114 .help = "Pre-open/register files",
115 .category = FIO_OPT_C_ENGINE,
116 .group = FIO_OPT_G_IOURING,
119 .name = "sqthread_poll",
120 .lname = "Kernel SQ thread polling",
122 .off1 = offsetof(struct ioring_options, sqpoll_thread),
123 .help = "Offload submission/completion to kernel thread",
124 .category = FIO_OPT_C_ENGINE,
125 .group = FIO_OPT_G_IOURING,
128 .name = "sqthread_poll_cpu",
129 .lname = "SQ Thread Poll CPU",
131 .cb = fio_ioring_sqpoll_cb,
132 .help = "What CPU to run SQ thread polling on",
133 .category = FIO_OPT_C_ENGINE,
134 .group = FIO_OPT_G_IOURING,
140 .off1 = offsetof(struct ioring_options, uncached),
141 .help = "Use RWF_UNCACHED for buffered read/writes",
142 .category = FIO_OPT_C_ENGINE,
143 .group = FIO_OPT_G_IOURING,
150 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
151 unsigned int min_complete, unsigned int flags)
153 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
154 min_complete, flags, NULL, 0);
157 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
159 struct ioring_data *ld = td->io_ops_data;
160 struct ioring_options *o = td->eo;
161 struct fio_file *f = io_u->file;
162 struct io_uring_sqe *sqe;
164 sqe = &ld->sqes[io_u->index];
166 /* zero out fields not used in this submission */
167 memset(sqe, 0, sizeof(*sqe));
169 if (o->registerfiles) {
170 sqe->fd = f->engine_pos;
171 sqe->flags = IOSQE_FIXED_FILE;
176 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
178 if (io_u->ddir == DDIR_READ)
179 sqe->opcode = IORING_OP_READ_FIXED;
181 sqe->opcode = IORING_OP_WRITE_FIXED;
182 sqe->addr = (unsigned long) io_u->xfer_buf;
183 sqe->len = io_u->xfer_buflen;
184 sqe->buf_index = io_u->index;
186 if (io_u->ddir == DDIR_READ)
187 sqe->opcode = IORING_OP_READV;
189 sqe->opcode = IORING_OP_WRITEV;
190 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
193 if (!td->o.odirect && o->uncached)
194 sqe->rw_flags = RWF_UNCACHED;
195 sqe->off = io_u->offset;
196 } else if (ddir_sync(io_u->ddir)) {
197 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
198 sqe->off = f->first_write;
199 sqe->len = f->last_write - f->first_write;
200 sqe->sync_range_flags = td->o.sync_file_range;
201 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
203 if (io_u->ddir == DDIR_DATASYNC)
204 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
205 sqe->opcode = IORING_OP_FSYNC;
209 sqe->user_data = (unsigned long) io_u;
213 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
215 struct ioring_data *ld = td->io_ops_data;
216 struct io_uring_cqe *cqe;
220 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
222 cqe = &ld->cq_ring.cqes[index];
223 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
225 if (cqe->res != io_u->xfer_buflen) {
226 if (cqe->res > io_u->xfer_buflen)
227 io_u->error = -cqe->res;
229 io_u->resid = io_u->xfer_buflen - cqe->res;
236 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
239 struct ioring_data *ld = td->io_ops_data;
240 struct io_cq_ring *ring = &ld->cq_ring;
241 unsigned head, reaped = 0;
246 if (head == *ring->tail)
250 } while (reaped + events < max);
257 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
258 unsigned int max, const struct timespec *t)
260 struct ioring_data *ld = td->io_ops_data;
261 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
262 struct ioring_options *o = td->eo;
263 struct io_cq_ring *ring = &ld->cq_ring;
267 ld->cq_ring_off = *ring->head;
269 r = fio_ioring_cqring_reap(td, events, max);
277 if (!o->sqpoll_thread) {
278 r = io_uring_enter(ld, 0, actual_min,
279 IORING_ENTER_GETEVENTS);
281 if (errno == EAGAIN || errno == EINTR)
283 td_verror(td, errno, "io_uring_enter");
287 } while (events < min);
289 return r < 0 ? r : events;
292 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
295 struct ioring_data *ld = td->io_ops_data;
296 struct io_sq_ring *ring = &ld->sq_ring;
297 unsigned tail, next_tail;
299 fio_ro_check(td, io_u);
301 if (ld->queued == ld->iodepth)
304 if (io_u->ddir == DDIR_TRIM) {
308 do_io_u_trim(td, io_u);
309 io_u_mark_submit(td, 1);
310 io_u_mark_complete(td, 1);
311 return FIO_Q_COMPLETED;
315 next_tail = tail + 1;
317 if (next_tail == *ring->head)
320 /* ensure sqe stores are ordered with tail update */
322 ring->array[tail & ld->sq_ring_mask] = io_u->index;
323 *ring->tail = next_tail;
330 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
332 struct ioring_data *ld = td->io_ops_data;
335 if (!fio_fill_issue_time(td))
338 fio_gettime(&now, NULL);
341 struct io_sq_ring *ring = &ld->sq_ring;
342 int index = ring->array[start & ld->sq_ring_mask];
343 struct io_u *io_u = ld->io_u_index[index];
345 memcpy(&io_u->issue_time, &now, sizeof(now));
346 io_u_queued(td, io_u);
352 static int fio_ioring_commit(struct thread_data *td)
354 struct ioring_data *ld = td->io_ops_data;
355 struct ioring_options *o = td->eo;
362 * Kernel side does submission. just need to check if the ring is
363 * flagged as needing a kick, if so, call io_uring_enter(). This
364 * only happens if we've been idle too long.
366 if (o->sqpoll_thread) {
367 struct io_sq_ring *ring = &ld->sq_ring;
370 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
371 io_uring_enter(ld, ld->queued, 0,
372 IORING_ENTER_SQ_WAKEUP);
378 unsigned start = *ld->sq_ring.head;
379 long nr = ld->queued;
381 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
383 fio_ioring_queued(td, start, ret);
384 io_u_mark_submit(td, ret);
389 io_u_mark_submit(td, ret);
392 if (errno == EAGAIN || errno == EINTR) {
393 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
396 /* Shouldn't happen */
400 td_verror(td, errno, "io_uring_enter submit");
403 } while (ld->queued);
408 static void fio_ioring_unmap(struct ioring_data *ld)
412 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
413 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
417 static void fio_ioring_cleanup(struct thread_data *td)
419 struct ioring_data *ld = td->io_ops_data;
422 if (!(td->flags & TD_F_CHILD))
423 fio_ioring_unmap(ld);
425 free(ld->io_u_index);
432 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
434 struct io_sq_ring *sring = &ld->sq_ring;
435 struct io_cq_ring *cring = &ld->cq_ring;
438 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
439 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
440 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
442 ld->mmap[0].ptr = ptr;
443 sring->head = ptr + p->sq_off.head;
444 sring->tail = ptr + p->sq_off.tail;
445 sring->ring_mask = ptr + p->sq_off.ring_mask;
446 sring->ring_entries = ptr + p->sq_off.ring_entries;
447 sring->flags = ptr + p->sq_off.flags;
448 sring->array = ptr + p->sq_off.array;
449 ld->sq_ring_mask = *sring->ring_mask;
451 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
452 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
453 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
455 ld->mmap[1].ptr = ld->sqes;
457 ld->mmap[2].len = p->cq_off.cqes +
458 p->cq_entries * sizeof(struct io_uring_cqe);
459 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
460 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
462 ld->mmap[2].ptr = ptr;
463 cring->head = ptr + p->cq_off.head;
464 cring->tail = ptr + p->cq_off.tail;
465 cring->ring_mask = ptr + p->cq_off.ring_mask;
466 cring->ring_entries = ptr + p->cq_off.ring_entries;
467 cring->cqes = ptr + p->cq_off.cqes;
468 ld->cq_ring_mask = *cring->ring_mask;
472 static int fio_ioring_queue_init(struct thread_data *td)
474 struct ioring_data *ld = td->io_ops_data;
475 struct ioring_options *o = td->eo;
476 int depth = td->o.iodepth;
477 struct io_uring_params p;
480 memset(&p, 0, sizeof(p));
483 p.flags |= IORING_SETUP_IOPOLL;
484 if (o->sqpoll_thread) {
485 p.flags |= IORING_SETUP_SQPOLL;
487 p.flags |= IORING_SETUP_SQ_AFF;
488 p.sq_thread_cpu = o->sqpoll_cpu;
492 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
499 struct rlimit rlim = {
500 .rlim_cur = RLIM_INFINITY,
501 .rlim_max = RLIM_INFINITY,
504 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
507 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
508 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
513 return fio_ioring_mmap(ld, &p);
516 static int fio_ioring_register_files(struct thread_data *td)
518 struct ioring_data *ld = td->io_ops_data;
523 ld->fds = calloc(td->o.nr_files, sizeof(int));
525 for_each_file(td, f, i) {
526 ret = generic_open_file(td, f);
533 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
534 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
542 * Pretend the file is closed again, and really close it if we hit
545 for_each_file(td, f, i) {
548 ret2 = generic_close_file(td, f);
556 static int fio_ioring_post_init(struct thread_data *td)
558 struct ioring_data *ld = td->io_ops_data;
559 struct ioring_options *o = td->eo;
563 for (i = 0; i < td->o.iodepth; i++) {
564 struct iovec *iov = &ld->iovecs[i];
566 io_u = ld->io_u_index[i];
567 iov->iov_base = io_u->buf;
568 iov->iov_len = td_max_bs(td);
571 err = fio_ioring_queue_init(td);
573 td_verror(td, errno, "io_queue_init");
577 if (o->registerfiles) {
578 err = fio_ioring_register_files(td);
580 td_verror(td, errno, "ioring_register_files");
588 static unsigned roundup_pow2(unsigned depth)
590 return 1UL << __fls(depth - 1);
593 static int fio_ioring_init(struct thread_data *td)
595 struct ioring_options *o = td->eo;
596 struct ioring_data *ld;
598 /* sqthread submission requires registered files */
599 if (o->sqpoll_thread)
600 o->registerfiles = 1;
602 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
603 log_err("fio: io_uring registered files require nr_files to "
604 "be identical to open_files\n");
608 ld = calloc(1, sizeof(*ld));
610 /* ring depth must be a power-of-2 */
611 ld->iodepth = td->o.iodepth;
612 td->o.iodepth = roundup_pow2(td->o.iodepth);
615 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
616 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
618 td->io_ops_data = ld;
622 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
624 struct ioring_data *ld = td->io_ops_data;
626 ld->io_u_index[io_u->index] = io_u;
630 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
632 struct ioring_data *ld = td->io_ops_data;
633 struct ioring_options *o = td->eo;
635 if (!ld || !o->registerfiles)
636 return generic_open_file(td, f);
638 f->fd = ld->fds[f->engine_pos];
642 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
644 struct ioring_data *ld = td->io_ops_data;
645 struct ioring_options *o = td->eo;
647 if (!ld || !o->registerfiles)
648 return generic_close_file(td, f);
654 static struct ioengine_ops ioengine = {
656 .version = FIO_IOOPS_VERSION,
657 .flags = FIO_ASYNCIO_SYNC_TRIM,
658 .init = fio_ioring_init,
659 .post_init = fio_ioring_post_init,
660 .io_u_init = fio_ioring_io_u_init,
661 .prep = fio_ioring_prep,
662 .queue = fio_ioring_queue,
663 .commit = fio_ioring_commit,
664 .getevents = fio_ioring_getevents,
665 .event = fio_ioring_event,
666 .cleanup = fio_ioring_cleanup,
667 .open_file = fio_ioring_open_file,
668 .close_file = fio_ioring_close_file,
669 .get_file_size = generic_get_file_size,
671 .option_struct_size = sizeof(struct ioring_options),
674 static void fio_init fio_ioring_register(void)
676 register_ioengine(&ioengine);
679 static void fio_exit fio_ioring_unregister(void)
681 unregister_ioengine(&ioengine);