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;
53 struct io_sq_ring sq_ring;
54 struct io_uring_sqe *sqes;
56 unsigned sq_ring_mask;
58 struct io_cq_ring cq_ring;
59 unsigned cq_ring_mask;
68 struct ioring_mmap mmap[3];
71 struct ioring_options {
74 unsigned int fixedbufs;
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_LIBAIO,
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_LIBAIO,
109 .name = "sqthread_poll",
110 .lname = "Kernel SQ thread polling",
112 .off1 = offsetof(struct ioring_options, sqpoll_thread),
113 .help = "Offload submission/completion to kernel thread",
114 .category = FIO_OPT_C_ENGINE,
115 .group = FIO_OPT_G_LIBAIO,
118 .name = "sqthread_poll_cpu",
119 .lname = "SQ Thread Poll CPU",
121 .cb = fio_ioring_sqpoll_cb,
122 .help = "What CPU to run SQ thread polling on",
123 .category = FIO_OPT_C_ENGINE,
124 .group = FIO_OPT_G_LIBAIO,
131 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
132 unsigned int min_complete, unsigned int flags)
134 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
135 min_complete, flags, NULL, 0);
138 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
140 struct ioring_data *ld = td->io_ops_data;
141 struct ioring_options *o = td->eo;
142 struct fio_file *f = io_u->file;
143 struct io_uring_sqe *sqe;
145 sqe = &ld->sqes[io_u->index];
151 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
153 if (io_u->ddir == DDIR_READ)
154 sqe->opcode = IORING_OP_READ_FIXED;
156 sqe->opcode = IORING_OP_WRITE_FIXED;
157 sqe->addr = (unsigned long) io_u->xfer_buf;
158 sqe->len = io_u->xfer_buflen;
159 sqe->buf_index = io_u->index;
161 if (io_u->ddir == DDIR_READ)
162 sqe->opcode = IORING_OP_READV;
164 sqe->opcode = IORING_OP_WRITEV;
165 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
168 sqe->off = io_u->offset;
169 } else if (ddir_sync(io_u->ddir)) {
170 sqe->fsync_flags = 0;
171 if (io_u->ddir == DDIR_DATASYNC)
172 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
173 sqe->opcode = IORING_OP_FSYNC;
176 sqe->user_data = (unsigned long) io_u;
180 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
182 struct ioring_data *ld = td->io_ops_data;
183 struct io_uring_cqe *cqe;
187 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
189 cqe = &ld->cq_ring.cqes[index];
190 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
192 if (cqe->res != io_u->xfer_buflen) {
193 if (cqe->res > io_u->xfer_buflen)
194 io_u->error = -cqe->res;
196 io_u->resid = io_u->xfer_buflen - cqe->res;
200 if (io_u->ddir == DDIR_READ) {
201 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
210 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
213 struct ioring_data *ld = td->io_ops_data;
214 struct io_cq_ring *ring = &ld->cq_ring;
215 unsigned head, reaped = 0;
220 if (head == *ring->tail)
224 } while (reaped + events < max);
231 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
232 unsigned int max, const struct timespec *t)
234 struct ioring_data *ld = td->io_ops_data;
235 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
236 struct ioring_options *o = td->eo;
237 struct io_cq_ring *ring = &ld->cq_ring;
241 ld->cq_ring_off = *ring->head;
243 r = fio_ioring_cqring_reap(td, events, max);
249 if (!o->sqpoll_thread) {
250 r = io_uring_enter(ld, 0, actual_min,
251 IORING_ENTER_GETEVENTS);
255 td_verror(td, errno, "io_uring_enter");
259 } while (events < min);
261 return r < 0 ? r : events;
264 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
267 struct ioring_data *ld = td->io_ops_data;
268 struct io_sq_ring *ring = &ld->sq_ring;
269 unsigned tail, next_tail;
271 fio_ro_check(td, io_u);
273 if (ld->queued == ld->iodepth)
276 if (io_u->ddir == DDIR_TRIM) {
280 do_io_u_trim(td, io_u);
281 io_u_mark_submit(td, 1);
282 io_u_mark_complete(td, 1);
283 return FIO_Q_COMPLETED;
287 next_tail = tail + 1;
289 if (next_tail == *ring->head)
292 /* ensure sqe stores are ordered with tail update */
294 ring->array[tail & ld->sq_ring_mask] = io_u->index;
295 *ring->tail = next_tail;
302 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
304 struct ioring_data *ld = td->io_ops_data;
307 if (!fio_fill_issue_time(td))
310 fio_gettime(&now, NULL);
313 struct io_sq_ring *ring = &ld->sq_ring;
314 int index = ring->array[start & ld->sq_ring_mask];
315 struct io_u *io_u = ld->io_u_index[index];
317 memcpy(&io_u->issue_time, &now, sizeof(now));
318 io_u_queued(td, io_u);
324 static int fio_ioring_commit(struct thread_data *td)
326 struct ioring_data *ld = td->io_ops_data;
327 struct ioring_options *o = td->eo;
334 * Kernel side does submission. just need to check if the ring is
335 * flagged as needing a kick, if so, call io_uring_enter(). This
336 * only happens if we've been idle too long.
338 if (o->sqpoll_thread) {
339 struct io_sq_ring *ring = &ld->sq_ring;
342 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
343 io_uring_enter(ld, ld->queued, 0,
344 IORING_ENTER_SQ_WAKEUP);
350 unsigned start = *ld->sq_ring.head;
351 long nr = ld->queued;
353 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
355 fio_ioring_queued(td, start, ret);
356 io_u_mark_submit(td, ret);
361 io_u_mark_submit(td, ret);
364 if (errno == EAGAIN) {
365 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
368 /* Shouldn't happen */
372 td_verror(td, errno, "io_uring_enter submit");
375 } while (ld->queued);
380 static void fio_ioring_unmap(struct ioring_data *ld)
384 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
385 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
389 static void fio_ioring_cleanup(struct thread_data *td)
391 struct ioring_data *ld = td->io_ops_data;
394 td->ts.cachehit += ld->cachehit;
395 td->ts.cachemiss += ld->cachemiss;
397 if (!(td->flags & TD_F_CHILD))
398 fio_ioring_unmap(ld);
400 free(ld->io_u_index);
406 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
408 struct io_sq_ring *sring = &ld->sq_ring;
409 struct io_cq_ring *cring = &ld->cq_ring;
412 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
413 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
414 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
416 ld->mmap[0].ptr = ptr;
417 sring->head = ptr + p->sq_off.head;
418 sring->tail = ptr + p->sq_off.tail;
419 sring->ring_mask = ptr + p->sq_off.ring_mask;
420 sring->ring_entries = ptr + p->sq_off.ring_entries;
421 sring->flags = ptr + p->sq_off.flags;
422 sring->array = ptr + p->sq_off.array;
423 ld->sq_ring_mask = *sring->ring_mask;
425 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
426 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
427 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
429 ld->mmap[1].ptr = ld->sqes;
431 ld->mmap[2].len = p->cq_off.cqes +
432 p->cq_entries * sizeof(struct io_uring_cqe);
433 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
434 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
436 ld->mmap[2].ptr = ptr;
437 cring->head = ptr + p->cq_off.head;
438 cring->tail = ptr + p->cq_off.tail;
439 cring->ring_mask = ptr + p->cq_off.ring_mask;
440 cring->ring_entries = ptr + p->cq_off.ring_entries;
441 cring->cqes = ptr + p->cq_off.cqes;
442 ld->cq_ring_mask = *cring->ring_mask;
446 static int fio_ioring_queue_init(struct thread_data *td)
448 struct ioring_data *ld = td->io_ops_data;
449 struct ioring_options *o = td->eo;
450 int depth = td->o.iodepth;
451 struct io_uring_params p;
454 memset(&p, 0, sizeof(p));
457 p.flags |= IORING_SETUP_IOPOLL;
458 if (o->sqpoll_thread) {
459 p.flags |= IORING_SETUP_SQPOLL;
461 p.flags |= IORING_SETUP_SQ_AFF;
462 p.sq_thread_cpu = o->sqpoll_cpu;
466 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
473 struct rlimit rlim = {
474 .rlim_cur = RLIM_INFINITY,
475 .rlim_max = RLIM_INFINITY,
478 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
481 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
482 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
487 return fio_ioring_mmap(ld, &p);
490 static int fio_ioring_post_init(struct thread_data *td)
492 struct ioring_data *ld = td->io_ops_data;
496 for (i = 0; i < td->o.iodepth; i++) {
497 struct iovec *iov = &ld->iovecs[i];
499 io_u = ld->io_u_index[i];
500 iov->iov_base = io_u->buf;
501 iov->iov_len = td_max_bs(td);
504 err = fio_ioring_queue_init(td);
506 td_verror(td, errno, "io_queue_init");
513 static unsigned roundup_pow2(unsigned depth)
515 return 1UL << __fls(depth - 1);
518 static int fio_ioring_init(struct thread_data *td)
520 struct ioring_data *ld;
522 ld = calloc(1, sizeof(*ld));
524 /* ring depth must be a power-of-2 */
525 ld->iodepth = td->o.iodepth;
526 td->o.iodepth = roundup_pow2(td->o.iodepth);
529 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
530 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
532 td->io_ops_data = ld;
536 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
538 struct ioring_data *ld = td->io_ops_data;
540 ld->io_u_index[io_u->index] = io_u;
544 static struct ioengine_ops ioengine = {
546 .version = FIO_IOOPS_VERSION,
547 .init = fio_ioring_init,
548 .post_init = fio_ioring_post_init,
549 .io_u_init = fio_ioring_io_u_init,
550 .prep = fio_ioring_prep,
551 .queue = fio_ioring_queue,
552 .commit = fio_ioring_commit,
553 .getevents = fio_ioring_getevents,
554 .event = fio_ioring_event,
555 .cleanup = fio_ioring_cleanup,
556 .open_file = generic_open_file,
557 .close_file = generic_close_file,
558 .get_file_size = generic_get_file_size,
560 .option_struct_size = sizeof(struct ioring_options),
563 static void fio_init fio_ioring_register(void)
565 register_ioengine(&ioengine);
568 static void fio_exit fio_ioring_unregister(void)
570 unregister_ioengine(&ioengine);