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 "../os/io_uring.h"
29 unsigned *ring_entries;
38 unsigned *ring_entries;
39 struct io_uring_event *events;
51 struct io_u **io_u_index;
53 struct io_sq_ring sq_ring;
54 struct io_uring_iocb *iocbs;
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 sqthread;
76 unsigned int sqthread_set;
77 unsigned int sqthread_poll;
81 static int fio_ioring_sqthread_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,
111 .lname = "Use kernel SQ thread on this CPU",
113 .cb = fio_ioring_sqthread_cb,
114 .help = "Offload submission to kernel thread",
115 .category = FIO_OPT_C_ENGINE,
116 .group = FIO_OPT_G_LIBAIO,
119 .name = "sqthread_poll",
120 .lname = "Kernel SQ thread should poll",
121 .type = FIO_OPT_STR_SET,
122 .off1 = offsetof(struct ioring_options, sqthread_poll),
123 .help = "Used with sqthread, enables kernel side polling",
124 .category = FIO_OPT_C_ENGINE,
125 .group = FIO_OPT_G_LIBAIO,
129 .lname = "Offload submission to kernel workqueue",
130 .type = FIO_OPT_STR_SET,
131 .off1 = offsetof(struct ioring_options, sqwq),
132 .help = "Offload submission to kernel workqueue",
133 .category = FIO_OPT_C_ENGINE,
134 .group = FIO_OPT_G_LIBAIO,
141 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
142 unsigned int min_complete, unsigned int flags)
144 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
145 min_complete, flags);
148 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
150 struct ioring_data *ld = td->io_ops_data;
151 struct fio_file *f = io_u->file;
152 struct io_uring_iocb *iocb;
154 iocb = &ld->iocbs[io_u->index];
159 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
160 if (io_u->ddir == DDIR_READ)
161 iocb->opcode = IORING_OP_READ;
163 iocb->opcode = IORING_OP_WRITE;
164 iocb->off = io_u->offset;
165 iocb->addr = io_u->xfer_buf;
166 iocb->len = io_u->xfer_buflen;
167 } else if (ddir_sync(io_u->ddir))
168 iocb->opcode = IORING_OP_FSYNC;
173 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
175 struct ioring_data *ld = td->io_ops_data;
176 struct io_uring_event *ev;
180 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
182 ev = &ld->cq_ring.events[index];
183 io_u = ld->io_u_index[ev->index];
185 if (ev->res != io_u->xfer_buflen) {
186 if (ev->res > io_u->xfer_buflen)
187 io_u->error = -ev->res;
189 io_u->resid = io_u->xfer_buflen - ev->res;
193 if (io_u->ddir == DDIR_READ) {
194 if (ev->flags & IOEV_FLAG_CACHEHIT)
203 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
206 struct ioring_data *ld = td->io_ops_data;
207 struct io_cq_ring *ring = &ld->cq_ring;
208 unsigned head, reaped = 0;
213 if (head == *ring->tail)
217 } while (reaped + events < max);
224 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
225 unsigned int max, const struct timespec *t)
227 struct ioring_data *ld = td->io_ops_data;
228 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
229 struct ioring_options *o = td->eo;
230 struct io_cq_ring *ring = &ld->cq_ring;
234 ld->cq_ring_off = *ring->head;
236 r = fio_ioring_cqring_reap(td, events, max);
242 if (!o->sqthread_poll) {
243 r = io_uring_enter(ld, 0, actual_min,
244 IORING_ENTER_GETEVENTS);
248 td_verror(td, errno, "io_uring_enter");
252 } while (events < min);
254 return r < 0 ? r : events;
257 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
260 struct ioring_data *ld = td->io_ops_data;
261 struct io_sq_ring *ring = &ld->sq_ring;
262 unsigned tail, next_tail;
264 fio_ro_check(td, io_u);
266 if (ld->queued == ld->iodepth)
269 if (io_u->ddir == DDIR_TRIM) {
273 do_io_u_trim(td, io_u);
274 io_u_mark_submit(td, 1);
275 io_u_mark_complete(td, 1);
276 return FIO_Q_COMPLETED;
280 next_tail = tail + 1;
282 if (next_tail == *ring->head)
285 ring->array[tail & ld->sq_ring_mask] = io_u->index;
286 *ring->tail = next_tail;
293 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
295 struct ioring_data *ld = td->io_ops_data;
298 if (!fio_fill_issue_time(td))
301 fio_gettime(&now, NULL);
304 struct io_sq_ring *ring = &ld->sq_ring;
305 int index = ring->array[start & ld->sq_ring_mask];
306 struct io_u *io_u = ld->io_u_index[index];
308 memcpy(&io_u->issue_time, &now, sizeof(now));
309 io_u_queued(td, io_u);
315 static int fio_ioring_commit(struct thread_data *td)
317 struct ioring_data *ld = td->io_ops_data;
318 struct ioring_options *o = td->eo;
325 if (o->sqthread_poll) {
326 struct io_sq_ring *ring = &ld->sq_ring;
328 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
329 io_uring_enter(ld, ld->queued, 0, 0);
335 unsigned start = *ld->sq_ring.head;
336 long nr = ld->queued;
338 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
340 fio_ioring_queued(td, start, ret);
341 io_u_mark_submit(td, ret);
346 io_u_mark_submit(td, ret);
349 if (errno == EAGAIN) {
350 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
353 /* Shouldn't happen */
357 td_verror(td, errno, "io_uring_enter submit");
360 } while (ld->queued);
365 static void fio_ioring_unmap(struct ioring_data *ld)
369 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
370 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
374 static void fio_ioring_cleanup(struct thread_data *td)
376 struct ioring_data *ld = td->io_ops_data;
379 td->ts.cachehit += ld->cachehit;
380 td->ts.cachemiss += ld->cachemiss;
382 if (!(td->flags & TD_F_CHILD))
383 fio_ioring_unmap(ld);
385 free(ld->io_u_index);
392 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
394 struct io_sq_ring *sring = &ld->sq_ring;
395 struct io_cq_ring *cring = &ld->cq_ring;
398 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
399 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
400 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
402 ld->mmap[0].ptr = ptr;
403 sring->head = ptr + p->sq_off.head;
404 sring->tail = ptr + p->sq_off.tail;
405 sring->ring_mask = ptr + p->sq_off.ring_mask;
406 sring->ring_entries = ptr + p->sq_off.ring_entries;
407 sring->flags = ptr + p->sq_off.flags;
408 sring->array = ptr + p->sq_off.array;
409 ld->sq_ring_mask = *sring->ring_mask;
411 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_iocb);
412 ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
413 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
415 ld->mmap[1].ptr = ld->iocbs;
417 ld->mmap[2].len = p->cq_off.events +
418 p->cq_entries * sizeof(struct io_uring_event);
419 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
420 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
422 ld->mmap[2].ptr = ptr;
423 cring->head = ptr + p->cq_off.head;
424 cring->tail = ptr + p->cq_off.tail;
425 cring->ring_mask = ptr + p->cq_off.ring_mask;
426 cring->ring_entries = ptr + p->cq_off.ring_entries;
427 cring->events = ptr + p->cq_off.events;
428 ld->cq_ring_mask = *cring->ring_mask;
432 static int fio_ioring_queue_init(struct thread_data *td)
434 struct ioring_data *ld = td->io_ops_data;
435 struct ioring_options *o = td->eo;
436 int depth = td->o.iodepth;
437 struct io_uring_params p;
440 memset(&p, 0, sizeof(p));
443 p.flags |= IORING_SETUP_IOPOLL;
444 if (o->sqthread_set) {
445 p.sq_thread_cpu = o->sqthread;
446 p.flags |= IORING_SETUP_SQTHREAD;
447 if (o->sqthread_poll)
448 p.flags |= IORING_SETUP_SQPOLL;
451 p.flags |= IORING_SETUP_SQWQ;
454 struct rlimit rlim = {
455 .rlim_cur = RLIM_INFINITY,
456 .rlim_max = RLIM_INFINITY,
459 setrlimit(RLIMIT_MEMLOCK, &rlim);
460 p.flags |= IORING_SETUP_FIXEDBUFS;
463 ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
468 return fio_ioring_mmap(ld, &p);
471 static int fio_ioring_post_init(struct thread_data *td)
473 struct ioring_data *ld = td->io_ops_data;
474 struct ioring_options *o = td->eo;
481 for (i = 0; i < td->o.iodepth; i++) {
482 struct iovec *iov = &ld->iovecs[i];
484 io_u = ld->io_u_index[i];
485 iov->iov_base = io_u->buf;
486 iov->iov_len = td_max_bs(td);
490 err = fio_ioring_queue_init(td);
492 td_verror(td, errno, "io_queue_init");
499 static unsigned roundup_pow2(unsigned depth)
501 return 1UL << __fls(depth - 1);
504 static int fio_ioring_init(struct thread_data *td)
506 struct ioring_data *ld;
508 ld = calloc(1, sizeof(*ld));
510 /* ring depth must be a power-of-2 */
511 ld->iodepth = td->o.iodepth;
512 td->o.iodepth = roundup_pow2(td->o.iodepth);
515 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
516 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
518 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
520 td->io_ops_data = ld;
524 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
526 struct ioring_data *ld = td->io_ops_data;
528 ld->io_u_index[io_u->index] = io_u;
532 static struct ioengine_ops ioengine = {
534 .version = FIO_IOOPS_VERSION,
535 .init = fio_ioring_init,
536 .post_init = fio_ioring_post_init,
537 .io_u_init = fio_ioring_io_u_init,
538 .prep = fio_ioring_prep,
539 .queue = fio_ioring_queue,
540 .commit = fio_ioring_commit,
541 .getevents = fio_ioring_getevents,
542 .event = fio_ioring_event,
543 .cleanup = fio_ioring_cleanup,
544 .open_file = generic_open_file,
545 .close_file = generic_close_file,
546 .get_file_size = generic_get_file_size,
548 .option_struct_size = sizeof(struct ioring_options),
551 static void fio_init fio_ioring_register(void)
553 register_ioengine(&ioengine);
556 static void fio_exit fio_ioring_unregister(void)
558 unregister_ioengine(&ioengine);