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;
65 struct ioring_mmap mmap[3];
68 struct ioring_options {
71 unsigned int fixedbufs;
72 unsigned int sqpoll_thread;
73 unsigned int sqpoll_set;
74 unsigned int sqpoll_cpu;
77 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
79 struct ioring_options *o = data;
86 static struct fio_option options[] = {
89 .lname = "High Priority",
90 .type = FIO_OPT_STR_SET,
91 .off1 = offsetof(struct ioring_options, hipri),
92 .help = "Use polled IO completions",
93 .category = FIO_OPT_C_ENGINE,
94 .group = FIO_OPT_G_LIBAIO,
98 .lname = "Fixed (pre-mapped) IO buffers",
99 .type = FIO_OPT_STR_SET,
100 .off1 = offsetof(struct ioring_options, fixedbufs),
101 .help = "Pre map IO buffers",
102 .category = FIO_OPT_C_ENGINE,
103 .group = FIO_OPT_G_LIBAIO,
106 .name = "sqthread_poll",
107 .lname = "Kernel SQ thread polling",
109 .off1 = offsetof(struct ioring_options, sqpoll_thread),
110 .help = "Offload submission/completion to kernel thread",
111 .category = FIO_OPT_C_ENGINE,
112 .group = FIO_OPT_G_LIBAIO,
115 .name = "sqthread_poll_cpu",
116 .lname = "SQ Thread Poll CPU",
118 .cb = fio_ioring_sqpoll_cb,
119 .help = "What CPU to run SQ thread polling on",
120 .category = FIO_OPT_C_ENGINE,
121 .group = FIO_OPT_G_LIBAIO,
128 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
129 unsigned int min_complete, unsigned int flags)
131 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
132 min_complete, flags, NULL, 0);
135 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
137 struct ioring_data *ld = td->io_ops_data;
138 struct ioring_options *o = td->eo;
139 struct fio_file *f = io_u->file;
140 struct io_uring_sqe *sqe;
142 sqe = &ld->sqes[io_u->index];
148 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
150 if (io_u->ddir == DDIR_READ)
151 sqe->opcode = IORING_OP_READ_FIXED;
153 sqe->opcode = IORING_OP_WRITE_FIXED;
154 sqe->addr = (unsigned long) io_u->xfer_buf;
155 sqe->len = io_u->xfer_buflen;
156 sqe->buf_index = io_u->index;
158 if (io_u->ddir == DDIR_READ)
159 sqe->opcode = IORING_OP_READV;
161 sqe->opcode = IORING_OP_WRITEV;
162 sqe->addr = (unsigned long) &ld->iovecs[io_u->index];
165 sqe->off = io_u->offset;
166 } else if (ddir_sync(io_u->ddir)) {
167 sqe->fsync_flags = 0;
168 if (io_u->ddir == DDIR_DATASYNC)
169 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
170 sqe->opcode = IORING_OP_FSYNC;
173 sqe->user_data = (unsigned long) io_u;
177 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
179 struct ioring_data *ld = td->io_ops_data;
180 struct io_uring_cqe *cqe;
184 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
186 cqe = &ld->cq_ring.cqes[index];
187 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
189 if (cqe->res != io_u->xfer_buflen) {
190 if (cqe->res > io_u->xfer_buflen)
191 io_u->error = -cqe->res;
193 io_u->resid = io_u->xfer_buflen - cqe->res;
200 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
203 struct ioring_data *ld = td->io_ops_data;
204 struct io_cq_ring *ring = &ld->cq_ring;
205 unsigned head, reaped = 0;
210 if (head == *ring->tail)
214 } while (reaped + events < max);
221 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
222 unsigned int max, const struct timespec *t)
224 struct ioring_data *ld = td->io_ops_data;
225 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
226 struct ioring_options *o = td->eo;
227 struct io_cq_ring *ring = &ld->cq_ring;
231 ld->cq_ring_off = *ring->head;
233 r = fio_ioring_cqring_reap(td, events, max);
241 if (!o->sqpoll_thread) {
242 r = io_uring_enter(ld, 0, actual_min,
243 IORING_ENTER_GETEVENTS);
247 td_verror(td, errno, "io_uring_enter");
251 } while (events < min);
253 return r < 0 ? r : events;
256 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
259 struct ioring_data *ld = td->io_ops_data;
260 struct io_sq_ring *ring = &ld->sq_ring;
261 unsigned tail, next_tail;
263 fio_ro_check(td, io_u);
265 if (ld->queued == ld->iodepth)
268 if (io_u->ddir == DDIR_TRIM) {
272 do_io_u_trim(td, io_u);
273 io_u_mark_submit(td, 1);
274 io_u_mark_complete(td, 1);
275 return FIO_Q_COMPLETED;
279 next_tail = tail + 1;
281 if (next_tail == *ring->head)
284 /* ensure sqe stores are ordered with tail update */
286 ring->array[tail & ld->sq_ring_mask] = io_u->index;
287 *ring->tail = next_tail;
294 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
296 struct ioring_data *ld = td->io_ops_data;
299 if (!fio_fill_issue_time(td))
302 fio_gettime(&now, NULL);
305 struct io_sq_ring *ring = &ld->sq_ring;
306 int index = ring->array[start & ld->sq_ring_mask];
307 struct io_u *io_u = ld->io_u_index[index];
309 memcpy(&io_u->issue_time, &now, sizeof(now));
310 io_u_queued(td, io_u);
316 static int fio_ioring_commit(struct thread_data *td)
318 struct ioring_data *ld = td->io_ops_data;
319 struct ioring_options *o = td->eo;
326 * Kernel side does submission. just need to check if the ring is
327 * flagged as needing a kick, if so, call io_uring_enter(). This
328 * only happens if we've been idle too long.
330 if (o->sqpoll_thread) {
331 struct io_sq_ring *ring = &ld->sq_ring;
334 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
335 io_uring_enter(ld, ld->queued, 0,
336 IORING_ENTER_SQ_WAKEUP);
342 unsigned start = *ld->sq_ring.head;
343 long nr = ld->queued;
345 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
347 fio_ioring_queued(td, start, ret);
348 io_u_mark_submit(td, ret);
353 io_u_mark_submit(td, ret);
356 if (errno == EAGAIN) {
357 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
360 /* Shouldn't happen */
364 td_verror(td, errno, "io_uring_enter submit");
367 } while (ld->queued);
372 static void fio_ioring_unmap(struct ioring_data *ld)
376 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
377 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
381 static void fio_ioring_cleanup(struct thread_data *td)
383 struct ioring_data *ld = td->io_ops_data;
386 if (!(td->flags & TD_F_CHILD))
387 fio_ioring_unmap(ld);
389 free(ld->io_u_index);
395 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
397 struct io_sq_ring *sring = &ld->sq_ring;
398 struct io_cq_ring *cring = &ld->cq_ring;
401 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
402 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
403 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
405 ld->mmap[0].ptr = ptr;
406 sring->head = ptr + p->sq_off.head;
407 sring->tail = ptr + p->sq_off.tail;
408 sring->ring_mask = ptr + p->sq_off.ring_mask;
409 sring->ring_entries = ptr + p->sq_off.ring_entries;
410 sring->flags = ptr + p->sq_off.flags;
411 sring->array = ptr + p->sq_off.array;
412 ld->sq_ring_mask = *sring->ring_mask;
414 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
415 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
416 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
418 ld->mmap[1].ptr = ld->sqes;
420 ld->mmap[2].len = p->cq_off.cqes +
421 p->cq_entries * sizeof(struct io_uring_cqe);
422 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
423 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
425 ld->mmap[2].ptr = ptr;
426 cring->head = ptr + p->cq_off.head;
427 cring->tail = ptr + p->cq_off.tail;
428 cring->ring_mask = ptr + p->cq_off.ring_mask;
429 cring->ring_entries = ptr + p->cq_off.ring_entries;
430 cring->cqes = ptr + p->cq_off.cqes;
431 ld->cq_ring_mask = *cring->ring_mask;
435 static int fio_ioring_queue_init(struct thread_data *td)
437 struct ioring_data *ld = td->io_ops_data;
438 struct ioring_options *o = td->eo;
439 int depth = td->o.iodepth;
440 struct io_uring_params p;
443 memset(&p, 0, sizeof(p));
446 p.flags |= IORING_SETUP_IOPOLL;
447 if (o->sqpoll_thread) {
448 p.flags |= IORING_SETUP_SQPOLL;
450 p.flags |= IORING_SETUP_SQ_AFF;
451 p.sq_thread_cpu = o->sqpoll_cpu;
455 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
462 struct rlimit rlim = {
463 .rlim_cur = RLIM_INFINITY,
464 .rlim_max = RLIM_INFINITY,
467 if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
470 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
471 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
476 return fio_ioring_mmap(ld, &p);
479 static int fio_ioring_post_init(struct thread_data *td)
481 struct ioring_data *ld = td->io_ops_data;
485 for (i = 0; i < td->o.iodepth; i++) {
486 struct iovec *iov = &ld->iovecs[i];
488 io_u = ld->io_u_index[i];
489 iov->iov_base = io_u->buf;
490 iov->iov_len = td_max_bs(td);
493 err = fio_ioring_queue_init(td);
495 td_verror(td, errno, "io_queue_init");
502 static unsigned roundup_pow2(unsigned depth)
504 return 1UL << __fls(depth - 1);
507 static int fio_ioring_init(struct thread_data *td)
509 struct ioring_data *ld;
511 ld = calloc(1, sizeof(*ld));
513 /* ring depth must be a power-of-2 */
514 ld->iodepth = td->o.iodepth;
515 td->o.iodepth = roundup_pow2(td->o.iodepth);
518 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
519 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
521 td->io_ops_data = ld;
525 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
527 struct ioring_data *ld = td->io_ops_data;
529 ld->io_u_index[io_u->index] = io_u;
533 static struct ioengine_ops ioengine = {
535 .version = FIO_IOOPS_VERSION,
536 .init = fio_ioring_init,
537 .post_init = fio_ioring_post_init,
538 .io_u_init = fio_ioring_io_u_init,
539 .prep = fio_ioring_prep,
540 .queue = fio_ioring_queue,
541 .commit = fio_ioring_commit,
542 .getevents = fio_ioring_getevents,
543 .event = fio_ioring_event,
544 .cleanup = fio_ioring_cleanup,
545 .open_file = generic_open_file,
546 .close_file = generic_close_file,
547 .get_file_size = generic_get_file_size,
549 .option_struct_size = sizeof(struct ioring_options),
552 static void fio_init fio_ioring_register(void)
554 register_ioengine(&ioengine);
557 static void fio_exit fio_ioring_unregister(void)
559 unregister_ioengine(&ioengine);