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;
80 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
82 struct ioring_options *o = data;
89 static struct fio_option options[] = {
92 .lname = "High Priority",
93 .type = FIO_OPT_STR_SET,
94 .off1 = offsetof(struct ioring_options, hipri),
95 .help = "Use polled IO completions",
96 .category = FIO_OPT_C_ENGINE,
97 .group = FIO_OPT_G_IOURING,
101 .lname = "Fixed (pre-mapped) IO buffers",
102 .type = FIO_OPT_STR_SET,
103 .off1 = offsetof(struct ioring_options, fixedbufs),
104 .help = "Pre map IO buffers",
105 .category = FIO_OPT_C_ENGINE,
106 .group = FIO_OPT_G_IOURING,
109 .name = "registerfiles",
110 .lname = "Register file set",
111 .type = FIO_OPT_STR_SET,
112 .off1 = offsetof(struct ioring_options, registerfiles),
113 .help = "Pre-open/register files",
114 .category = FIO_OPT_C_ENGINE,
115 .group = FIO_OPT_G_IOURING,
118 .name = "sqthread_poll",
119 .lname = "Kernel SQ thread polling",
121 .off1 = offsetof(struct ioring_options, sqpoll_thread),
122 .help = "Offload submission/completion to kernel thread",
123 .category = FIO_OPT_C_ENGINE,
124 .group = FIO_OPT_G_IOURING,
127 .name = "sqthread_poll_cpu",
128 .lname = "SQ Thread Poll CPU",
130 .cb = fio_ioring_sqpoll_cb,
131 .help = "What CPU to run SQ thread polling on",
132 .category = FIO_OPT_C_ENGINE,
133 .group = FIO_OPT_G_IOURING,
140 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
141 unsigned int min_complete, unsigned int flags)
143 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
144 min_complete, flags, NULL, 0);
147 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
149 struct ioring_data *ld = td->io_ops_data;
150 struct ioring_options *o = td->eo;
151 struct fio_file *f = io_u->file;
152 struct io_uring_sqe *sqe;
154 sqe = &ld->sqes[io_u->index];
156 /* zero out fields not used in this submission */
157 memset(sqe, 0, sizeof(*sqe));
159 if (o->registerfiles) {
160 sqe->fd = f->engine_pos;
161 sqe->flags = IOSQE_FIXED_FILE;
166 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
168 if (io_u->ddir == DDIR_READ)
169 sqe->opcode = IORING_OP_READ_FIXED;
171 sqe->opcode = IORING_OP_WRITE_FIXED;
172 sqe->addr = (unsigned long) io_u->xfer_buf;
173 sqe->len = io_u->xfer_buflen;
174 sqe->buf_index = io_u->index;
176 if (io_u->ddir == DDIR_READ)
177 sqe->opcode = IORING_OP_READV;
179 sqe->opcode = IORING_OP_WRITEV;
180 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
183 sqe->off = io_u->offset;
184 } else if (ddir_sync(io_u->ddir)) {
185 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
186 sqe->off = f->first_write;
187 sqe->len = f->last_write - f->first_write;
188 sqe->sync_range_flags = td->o.sync_file_range;
189 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
191 if (io_u->ddir == DDIR_DATASYNC)
192 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
193 sqe->opcode = IORING_OP_FSYNC;
197 sqe->user_data = (unsigned long) io_u;
201 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
203 struct ioring_data *ld = td->io_ops_data;
204 struct io_uring_cqe *cqe;
208 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
210 cqe = &ld->cq_ring.cqes[index];
211 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
213 if (cqe->res != io_u->xfer_buflen) {
214 if (cqe->res > io_u->xfer_buflen)
215 io_u->error = -cqe->res;
217 io_u->resid = io_u->xfer_buflen - cqe->res;
224 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
227 struct ioring_data *ld = td->io_ops_data;
228 struct io_cq_ring *ring = &ld->cq_ring;
229 unsigned head, reaped = 0;
234 if (head == *ring->tail)
238 } while (reaped + events < max);
245 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
246 unsigned int max, const struct timespec *t)
248 struct ioring_data *ld = td->io_ops_data;
249 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
250 struct ioring_options *o = td->eo;
251 struct io_cq_ring *ring = &ld->cq_ring;
255 ld->cq_ring_off = *ring->head;
257 r = fio_ioring_cqring_reap(td, events, max);
265 if (!o->sqpoll_thread) {
266 r = io_uring_enter(ld, 0, actual_min,
267 IORING_ENTER_GETEVENTS);
269 if (errno == EAGAIN || errno == EINTR)
271 td_verror(td, errno, "io_uring_enter");
275 } while (events < min);
277 return r < 0 ? r : events;
280 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
283 struct ioring_data *ld = td->io_ops_data;
284 struct io_sq_ring *ring = &ld->sq_ring;
285 unsigned tail, next_tail;
287 fio_ro_check(td, io_u);
289 if (ld->queued == ld->iodepth)
292 if (io_u->ddir == DDIR_TRIM) {
296 do_io_u_trim(td, io_u);
297 io_u_mark_submit(td, 1);
298 io_u_mark_complete(td, 1);
299 return FIO_Q_COMPLETED;
303 next_tail = tail + 1;
305 if (next_tail == *ring->head)
308 /* ensure sqe stores are ordered with tail update */
310 ring->array[tail & ld->sq_ring_mask] = io_u->index;
311 *ring->tail = next_tail;
318 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
320 struct ioring_data *ld = td->io_ops_data;
323 if (!fio_fill_issue_time(td))
326 fio_gettime(&now, NULL);
329 struct io_sq_ring *ring = &ld->sq_ring;
330 int index = ring->array[start & ld->sq_ring_mask];
331 struct io_u *io_u = ld->io_u_index[index];
333 memcpy(&io_u->issue_time, &now, sizeof(now));
334 io_u_queued(td, io_u);
340 static int fio_ioring_commit(struct thread_data *td)
342 struct ioring_data *ld = td->io_ops_data;
343 struct ioring_options *o = td->eo;
350 * Kernel side does submission. just need to check if the ring is
351 * flagged as needing a kick, if so, call io_uring_enter(). This
352 * only happens if we've been idle too long.
354 if (o->sqpoll_thread) {
355 struct io_sq_ring *ring = &ld->sq_ring;
358 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
359 io_uring_enter(ld, ld->queued, 0,
360 IORING_ENTER_SQ_WAKEUP);
366 unsigned start = *ld->sq_ring.head;
367 long nr = ld->queued;
369 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
371 fio_ioring_queued(td, start, ret);
372 io_u_mark_submit(td, ret);
377 io_u_mark_submit(td, ret);
380 if (errno == EAGAIN || errno == EINTR) {
381 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
384 /* Shouldn't happen */
388 td_verror(td, errno, "io_uring_enter submit");
391 } while (ld->queued);
396 static void fio_ioring_unmap(struct ioring_data *ld)
400 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
401 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
405 static void fio_ioring_cleanup(struct thread_data *td)
407 struct ioring_data *ld = td->io_ops_data;
410 if (!(td->flags & TD_F_CHILD))
411 fio_ioring_unmap(ld);
413 free(ld->io_u_index);
420 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
422 struct io_sq_ring *sring = &ld->sq_ring;
423 struct io_cq_ring *cring = &ld->cq_ring;
426 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
427 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
428 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
430 ld->mmap[0].ptr = ptr;
431 sring->head = ptr + p->sq_off.head;
432 sring->tail = ptr + p->sq_off.tail;
433 sring->ring_mask = ptr + p->sq_off.ring_mask;
434 sring->ring_entries = ptr + p->sq_off.ring_entries;
435 sring->flags = ptr + p->sq_off.flags;
436 sring->array = ptr + p->sq_off.array;
437 ld->sq_ring_mask = *sring->ring_mask;
439 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
440 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
441 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
443 ld->mmap[1].ptr = ld->sqes;
445 ld->mmap[2].len = p->cq_off.cqes +
446 p->cq_entries * sizeof(struct io_uring_cqe);
447 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
448 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
450 ld->mmap[2].ptr = ptr;
451 cring->head = ptr + p->cq_off.head;
452 cring->tail = ptr + p->cq_off.tail;
453 cring->ring_mask = ptr + p->cq_off.ring_mask;
454 cring->ring_entries = ptr + p->cq_off.ring_entries;
455 cring->cqes = ptr + p->cq_off.cqes;
456 ld->cq_ring_mask = *cring->ring_mask;
460 static int fio_ioring_queue_init(struct thread_data *td)
462 struct ioring_data *ld = td->io_ops_data;
463 struct ioring_options *o = td->eo;
464 int depth = td->o.iodepth;
465 struct io_uring_params p;
468 memset(&p, 0, sizeof(p));
471 p.flags |= IORING_SETUP_IOPOLL;
472 if (o->sqpoll_thread) {
473 p.flags |= IORING_SETUP_SQPOLL;
475 p.flags |= IORING_SETUP_SQ_AFF;
476 p.sq_thread_cpu = o->sqpoll_cpu;
480 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
487 struct rlimit rlim = {
488 .rlim_cur = RLIM_INFINITY,
489 .rlim_max = RLIM_INFINITY,
492 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
495 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
496 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
501 return fio_ioring_mmap(ld, &p);
504 static int fio_ioring_register_files(struct thread_data *td)
506 struct ioring_data *ld = td->io_ops_data;
511 ld->fds = calloc(td->o.nr_files, sizeof(int));
513 for_each_file(td, f, i) {
514 ret = generic_open_file(td, f);
521 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
522 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
530 * Pretend the file is closed again, and really close it if we hit
533 for_each_file(td, f, i) {
536 ret2 = generic_close_file(td, f);
544 static int fio_ioring_post_init(struct thread_data *td)
546 struct ioring_data *ld = td->io_ops_data;
547 struct ioring_options *o = td->eo;
551 for (i = 0; i < td->o.iodepth; i++) {
552 struct iovec *iov = &ld->iovecs[i];
554 io_u = ld->io_u_index[i];
555 iov->iov_base = io_u->buf;
556 iov->iov_len = td_max_bs(td);
559 err = fio_ioring_queue_init(td);
561 td_verror(td, errno, "io_queue_init");
565 if (o->registerfiles) {
566 err = fio_ioring_register_files(td);
568 td_verror(td, errno, "ioring_register_files");
576 static unsigned roundup_pow2(unsigned depth)
578 return 1UL << __fls(depth - 1);
581 static int fio_ioring_init(struct thread_data *td)
583 struct ioring_options *o = td->eo;
584 struct ioring_data *ld;
586 /* sqthread submission requires registered files */
587 if (o->sqpoll_thread)
588 o->registerfiles = 1;
590 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
591 log_err("fio: io_uring registered files require nr_files to "
592 "be identical to open_files\n");
596 ld = calloc(1, sizeof(*ld));
598 /* ring depth must be a power-of-2 */
599 ld->iodepth = td->o.iodepth;
600 td->o.iodepth = roundup_pow2(td->o.iodepth);
603 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
604 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
606 td->io_ops_data = ld;
610 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
612 struct ioring_data *ld = td->io_ops_data;
614 ld->io_u_index[io_u->index] = io_u;
618 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
620 struct ioring_data *ld = td->io_ops_data;
621 struct ioring_options *o = td->eo;
623 if (!ld || !o->registerfiles)
624 return generic_open_file(td, f);
626 f->fd = ld->fds[f->engine_pos];
630 static int fio_ioring_close_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_close_file(td, f);
642 static struct ioengine_ops ioengine = {
644 .version = FIO_IOOPS_VERSION,
645 .flags = FIO_ASYNCIO_SYNC_TRIM,
646 .init = fio_ioring_init,
647 .post_init = fio_ioring_post_init,
648 .io_u_init = fio_ioring_io_u_init,
649 .prep = fio_ioring_prep,
650 .queue = fio_ioring_queue,
651 .commit = fio_ioring_commit,
652 .getevents = fio_ioring_getevents,
653 .event = fio_ioring_event,
654 .cleanup = fio_ioring_cleanup,
655 .open_file = fio_ioring_open_file,
656 .close_file = fio_ioring_close_file,
657 .get_file_size = generic_get_file_size,
659 .option_struct_size = sizeof(struct ioring_options),
662 static void fio_init fio_ioring_register(void)
664 register_ioengine(&ioengine);
667 static void fio_exit fio_ioring_unregister(void)
669 unregister_ioengine(&ioengine);