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/io_uring.h"
30 unsigned *ring_entries;
39 unsigned *ring_entries;
40 struct io_uring_cqe *cqes;
52 struct io_u **io_u_index;
54 struct io_sq_ring sq_ring;
55 struct io_uring_sqe *sqes;
57 unsigned sq_ring_mask;
59 struct io_cq_ring cq_ring;
60 unsigned cq_ring_mask;
69 struct ioring_mmap mmap[3];
72 struct ioring_options {
75 unsigned int fixedbufs;
76 unsigned int sqpoll_thread;
77 unsigned int sqpoll_set;
78 unsigned int sqpoll_cpu;
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_LIBAIO,
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_LIBAIO,
110 .name = "sqthread_poll",
111 .lname = "Kernel SQ thread polling",
113 .off1 = offsetof(struct ioring_options, sqpoll_thread),
114 .help = "Offload submission/completion to kernel thread",
115 .category = FIO_OPT_C_ENGINE,
116 .group = FIO_OPT_G_LIBAIO,
119 .name = "sqthread_poll_cpu",
120 .lname = "SQ Thread Poll CPU",
122 .cb = fio_ioring_sqpoll_cb,
123 .help = "What CPU to run SQ thread polling on",
124 .category = FIO_OPT_C_ENGINE,
125 .group = FIO_OPT_G_LIBAIO,
132 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
133 unsigned int min_complete, unsigned int flags)
135 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
136 min_complete, flags);
139 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
141 struct ioring_data *ld = td->io_ops_data;
142 struct ioring_options *o = td->eo;
143 struct fio_file *f = io_u->file;
144 struct io_uring_sqe *sqe;
146 sqe = &ld->sqes[io_u->index];
152 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
153 if (io_u->ddir == DDIR_READ)
154 sqe->opcode = IORING_OP_READV;
156 sqe->opcode = IORING_OP_WRITEV;
159 sqe->flags |= IOSQE_FIXED_BUFFER;
160 sqe->addr = io_u->xfer_buf;
161 sqe->len = io_u->xfer_buflen;
162 sqe->buf_index = io_u->index;
164 sqe->addr = &ld->iovecs[io_u->index];
167 sqe->off = io_u->offset;
168 } else if (ddir_sync(io_u->ddir))
169 sqe->opcode = IORING_OP_FSYNC;
171 sqe->data = (unsigned long) io_u;
175 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
177 struct ioring_data *ld = td->io_ops_data;
178 struct io_uring_cqe *cqe;
182 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
184 cqe = &ld->cq_ring.cqes[index];
185 io_u = (struct io_u *) cqe->data;
187 if (cqe->res != io_u->xfer_buflen) {
188 if (cqe->res > io_u->xfer_buflen)
189 io_u->error = -cqe->res;
191 io_u->resid = io_u->xfer_buflen - cqe->res;
195 if (io_u->ddir == DDIR_READ) {
196 if (cqe->flags & IOCQE_FLAG_CACHEHIT)
205 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
208 struct ioring_data *ld = td->io_ops_data;
209 struct io_cq_ring *ring = &ld->cq_ring;
210 unsigned head, reaped = 0;
215 if (head == *ring->tail)
219 } while (reaped + events < max);
226 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
227 unsigned int max, const struct timespec *t)
229 struct ioring_data *ld = td->io_ops_data;
230 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
231 struct ioring_options *o = td->eo;
232 struct io_cq_ring *ring = &ld->cq_ring;
236 ld->cq_ring_off = *ring->head;
238 r = fio_ioring_cqring_reap(td, events, max);
244 if (!o->sqpoll_thread) {
245 r = io_uring_enter(ld, 0, actual_min,
246 IORING_ENTER_GETEVENTS);
250 td_verror(td, errno, "io_uring_enter");
254 } while (events < min);
256 return r < 0 ? r : events;
259 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
262 struct ioring_data *ld = td->io_ops_data;
263 struct io_sq_ring *ring = &ld->sq_ring;
264 unsigned tail, next_tail;
266 fio_ro_check(td, io_u);
268 if (ld->queued == ld->iodepth)
271 if (io_u->ddir == DDIR_TRIM) {
275 do_io_u_trim(td, io_u);
276 io_u_mark_submit(td, 1);
277 io_u_mark_complete(td, 1);
278 return FIO_Q_COMPLETED;
282 next_tail = tail + 1;
284 if (next_tail == *ring->head)
287 ring->array[tail & ld->sq_ring_mask] = io_u->index;
288 *ring->tail = next_tail;
295 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
297 struct ioring_data *ld = td->io_ops_data;
300 if (!fio_fill_issue_time(td))
303 fio_gettime(&now, NULL);
306 struct io_sq_ring *ring = &ld->sq_ring;
307 int index = ring->array[start & ld->sq_ring_mask];
308 struct io_u *io_u = ld->io_u_index[index];
310 memcpy(&io_u->issue_time, &now, sizeof(now));
311 io_u_queued(td, io_u);
317 static int fio_ioring_commit(struct thread_data *td)
319 struct ioring_data *ld = td->io_ops_data;
320 struct ioring_options *o = td->eo;
327 * Kernel side does submission. just need to check if the ring is
328 * flagged as needing a kick, if so, call io_uring_enter(). This
329 * only happens if we've been idle too long.
331 if (o->sqpoll_thread) {
332 struct io_sq_ring *ring = &ld->sq_ring;
335 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
336 io_uring_enter(ld, ld->queued, 0, 0);
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 td->ts.cachehit += ld->cachehit;
387 td->ts.cachemiss += ld->cachemiss;
389 if (!(td->flags & TD_F_CHILD))
390 fio_ioring_unmap(ld);
392 free(ld->io_u_index);
399 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
401 struct io_sq_ring *sring = &ld->sq_ring;
402 struct io_cq_ring *cring = &ld->cq_ring;
405 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
406 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
407 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
409 ld->mmap[0].ptr = ptr;
410 sring->head = ptr + p->sq_off.head;
411 sring->tail = ptr + p->sq_off.tail;
412 sring->ring_mask = ptr + p->sq_off.ring_mask;
413 sring->ring_entries = ptr + p->sq_off.ring_entries;
414 sring->flags = ptr + p->sq_off.flags;
415 sring->array = ptr + p->sq_off.array;
416 ld->sq_ring_mask = *sring->ring_mask;
418 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
419 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
420 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
422 ld->mmap[1].ptr = ld->sqes;
424 ld->mmap[2].len = p->cq_off.cqes +
425 p->cq_entries * sizeof(struct io_uring_cqe);
426 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
427 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
429 ld->mmap[2].ptr = ptr;
430 cring->head = ptr + p->cq_off.head;
431 cring->tail = ptr + p->cq_off.tail;
432 cring->ring_mask = ptr + p->cq_off.ring_mask;
433 cring->ring_entries = ptr + p->cq_off.ring_entries;
434 cring->cqes = ptr + p->cq_off.cqes;
435 ld->cq_ring_mask = *cring->ring_mask;
439 static int fio_ioring_queue_init(struct thread_data *td)
441 struct ioring_data *ld = td->io_ops_data;
442 struct ioring_options *o = td->eo;
443 int depth = td->o.iodepth;
444 struct io_uring_params p;
447 memset(&p, 0, sizeof(p));
450 p.flags |= IORING_SETUP_IOPOLL;
451 if (o->sqpoll_thread) {
452 p.flags |= IORING_SETUP_SQPOLL;
454 p.flags |= IORING_SETUP_SQ_AFF;
455 p.sq_thread_cpu = o->sqpoll_cpu;
460 struct rlimit rlim = {
461 .rlim_cur = RLIM_INFINITY,
462 .rlim_max = RLIM_INFINITY,
465 setrlimit(RLIMIT_MEMLOCK, &rlim);
468 ret = syscall(__NR_sys_io_uring_setup, depth, &p);
475 struct io_uring_register_buffers reg = {
476 .iovecs = ld->iovecs,
480 ret = syscall(__NR_sys_io_uring_register, ld->ring_fd,
481 IORING_REGISTER_BUFFERS, ®);
486 return fio_ioring_mmap(ld, &p);
489 static int fio_ioring_post_init(struct thread_data *td)
491 struct ioring_data *ld = td->io_ops_data;
495 for (i = 0; i < td->o.iodepth; i++) {
496 struct iovec *iov = &ld->iovecs[i];
498 io_u = ld->io_u_index[i];
499 iov->iov_base = io_u->buf;
500 iov->iov_len = td_max_bs(td);
503 err = fio_ioring_queue_init(td);
505 td_verror(td, errno, "io_queue_init");
512 static unsigned roundup_pow2(unsigned depth)
514 return 1UL << __fls(depth - 1);
517 static int fio_ioring_init(struct thread_data *td)
519 struct ioring_data *ld;
521 ld = calloc(1, sizeof(*ld));
523 /* ring depth must be a power-of-2 */
524 ld->iodepth = td->o.iodepth;
525 td->o.iodepth = roundup_pow2(td->o.iodepth);
528 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
529 ld->io_us = 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);