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
29 #include "../os/io_uring.h"
45 struct io_uring_event *events;
57 struct io_u **io_u_index;
59 struct io_sq_ring sq_ring;
60 struct io_uring_iocb *iocbs;
62 unsigned sq_ring_mask;
64 struct io_cq_ring cq_ring;
65 unsigned cq_ring_mask;
74 struct ioring_mmap mmap[3];
77 struct ioring_options {
80 unsigned int fixedbufs;
81 unsigned int sqthread;
82 unsigned int sqthread_set;
83 unsigned int sqthread_poll;
87 static int fio_ioring_sqthread_cb(void *data, unsigned long long *val)
89 struct ioring_options *o = data;
96 static struct fio_option options[] = {
99 .lname = "High Priority",
100 .type = FIO_OPT_STR_SET,
101 .off1 = offsetof(struct ioring_options, hipri),
102 .help = "Use polled IO completions",
103 .category = FIO_OPT_C_ENGINE,
104 .group = FIO_OPT_G_LIBAIO,
108 .lname = "Fixed (pre-mapped) IO buffers",
109 .type = FIO_OPT_STR_SET,
110 .off1 = offsetof(struct ioring_options, fixedbufs),
111 .help = "Pre map IO buffers",
112 .category = FIO_OPT_C_ENGINE,
113 .group = FIO_OPT_G_LIBAIO,
117 .lname = "Use kernel SQ thread on this CPU",
119 .cb = fio_ioring_sqthread_cb,
120 .help = "Offload submission to kernel thread",
121 .category = FIO_OPT_C_ENGINE,
122 .group = FIO_OPT_G_LIBAIO,
125 .name = "sqthread_poll",
126 .lname = "Kernel SQ thread should poll",
127 .type = FIO_OPT_STR_SET,
128 .off1 = offsetof(struct ioring_options, sqthread_poll),
129 .help = "Used with sqthread, enables kernel side polling",
130 .category = FIO_OPT_C_ENGINE,
131 .group = FIO_OPT_G_LIBAIO,
135 .lname = "Offload submission to kernel workqueue",
136 .type = FIO_OPT_STR_SET,
137 .off1 = offsetof(struct ioring_options, sqwq),
138 .help = "Offload submission to kernel workqueue",
139 .category = FIO_OPT_C_ENGINE,
140 .group = FIO_OPT_G_LIBAIO,
147 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
148 unsigned int min_complete, unsigned int flags)
150 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
151 min_complete, flags);
154 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
156 struct ioring_data *ld = td->io_ops_data;
157 struct fio_file *f = io_u->file;
158 struct io_uring_iocb *iocb;
160 iocb = &ld->iocbs[io_u->index];
165 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
166 if (io_u->ddir == DDIR_READ)
167 iocb->opcode = IORING_OP_READ;
169 iocb->opcode = IORING_OP_WRITE;
170 iocb->off = io_u->offset;
171 iocb->addr = io_u->xfer_buf;
172 iocb->len = io_u->xfer_buflen;
173 } else if (ddir_sync(io_u->ddir))
174 iocb->opcode = IORING_OP_FSYNC;
179 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
181 struct ioring_data *ld = td->io_ops_data;
182 struct io_uring_event *ev;
186 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
188 ev = &ld->cq_ring.events[index];
189 io_u = ld->io_u_index[ev->index];
191 if (ev->res != io_u->xfer_buflen) {
192 if (ev->res > io_u->xfer_buflen)
193 io_u->error = -ev->res;
195 io_u->resid = io_u->xfer_buflen - ev->res;
199 if (io_u->ddir == DDIR_READ) {
200 if (ev->flags & IOEV_FLAG_CACHEHIT)
209 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
212 struct ioring_data *ld = td->io_ops_data;
213 struct io_cq_ring *ring = &ld->cq_ring;
214 u32 head, reaped = 0;
219 if (head == *ring->tail)
223 } while (reaped + events < max);
230 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
231 unsigned int max, const struct timespec *t)
233 struct ioring_data *ld = td->io_ops_data;
234 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
235 struct ioring_options *o = td->eo;
236 struct io_cq_ring *ring = &ld->cq_ring;
240 ld->cq_ring_off = *ring->head;
242 r = fio_ioring_cqring_reap(td, events, max);
248 if (!o->sqthread_poll) {
249 r = io_uring_enter(ld, 0, actual_min,
250 IORING_ENTER_GETEVENTS);
254 td_verror(td, errno, "io_uring_enter");
258 } while (events < min);
260 return r < 0 ? r : events;
263 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
266 struct ioring_data *ld = td->io_ops_data;
267 struct io_sq_ring *ring = &ld->sq_ring;
268 unsigned tail, next_tail;
270 fio_ro_check(td, io_u);
272 if (ld->queued == ld->iodepth)
275 if (io_u->ddir == DDIR_TRIM) {
279 do_io_u_trim(td, io_u);
280 io_u_mark_submit(td, 1);
281 io_u_mark_complete(td, 1);
282 return FIO_Q_COMPLETED;
286 next_tail = tail + 1;
288 if (next_tail == *ring->head)
291 ring->array[tail & ld->sq_ring_mask] = io_u->index;
292 *ring->tail = next_tail;
299 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
301 struct ioring_data *ld = td->io_ops_data;
304 if (!fio_fill_issue_time(td))
307 fio_gettime(&now, NULL);
310 struct io_sq_ring *ring = &ld->sq_ring;
311 int index = ring->array[start & ld->sq_ring_mask];
312 struct io_u *io_u = ld->io_u_index[index];
314 memcpy(&io_u->issue_time, &now, sizeof(now));
315 io_u_queued(td, io_u);
321 static int fio_ioring_commit(struct thread_data *td)
323 struct ioring_data *ld = td->io_ops_data;
324 struct ioring_options *o = td->eo;
331 if (o->sqthread_poll) {
332 struct io_sq_ring *ring = &ld->sq_ring;
334 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
335 io_uring_enter(ld, ld->queued, 0, 0);
341 unsigned start = *ld->sq_ring.head;
342 long nr = ld->queued;
344 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
346 fio_ioring_queued(td, start, ret);
347 io_u_mark_submit(td, ret);
352 io_u_mark_submit(td, ret);
355 if (errno == EAGAIN) {
356 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
359 /* Shouldn't happen */
363 td_verror(td, errno, "io_uring_enter submit");
366 } while (ld->queued);
371 static void fio_ioring_unmap(struct ioring_data *ld)
375 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
376 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
380 static void fio_ioring_cleanup(struct thread_data *td)
382 struct ioring_data *ld = td->io_ops_data;
385 td->ts.cachehit += ld->cachehit;
386 td->ts.cachemiss += ld->cachemiss;
388 if (!(td->flags & TD_F_CHILD))
389 fio_ioring_unmap(ld);
391 free(ld->io_u_index);
398 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
400 struct io_sq_ring *sring = &ld->sq_ring;
401 struct io_cq_ring *cring = &ld->cq_ring;
404 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(u32);
405 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
406 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
408 ld->mmap[0].ptr = ptr;
409 sring->head = ptr + p->sq_off.head;
410 sring->tail = ptr + p->sq_off.tail;
411 sring->ring_mask = ptr + p->sq_off.ring_mask;
412 sring->ring_entries = ptr + p->sq_off.ring_entries;
413 sring->flags = ptr + p->sq_off.flags;
414 sring->array = ptr + p->sq_off.array;
415 ld->sq_ring_mask = *sring->ring_mask;
417 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_iocb);
418 ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
419 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
421 ld->mmap[1].ptr = ld->iocbs;
423 ld->mmap[2].len = p->cq_off.events +
424 p->cq_entries * sizeof(struct io_uring_event);
425 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
426 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
428 ld->mmap[2].ptr = ptr;
429 cring->head = ptr + p->cq_off.head;
430 cring->tail = ptr + p->cq_off.tail;
431 cring->ring_mask = ptr + p->cq_off.ring_mask;
432 cring->ring_entries = ptr + p->cq_off.ring_entries;
433 cring->events = ptr + p->cq_off.events;
434 ld->cq_ring_mask = *cring->ring_mask;
438 static int fio_ioring_queue_init(struct thread_data *td)
440 struct ioring_data *ld = td->io_ops_data;
441 struct ioring_options *o = td->eo;
442 int depth = td->o.iodepth;
443 struct io_uring_params p;
446 memset(&p, 0, sizeof(p));
449 p.flags |= IORING_SETUP_IOPOLL;
450 if (o->sqthread_set) {
451 p.sq_thread_cpu = o->sqthread;
452 p.flags |= IORING_SETUP_SQTHREAD;
453 if (o->sqthread_poll)
454 p.flags |= IORING_SETUP_SQPOLL;
457 p.flags |= IORING_SETUP_SQWQ;
460 struct rlimit rlim = {
461 .rlim_cur = RLIM_INFINITY,
462 .rlim_max = RLIM_INFINITY,
465 setrlimit(RLIMIT_MEMLOCK, &rlim);
466 p.flags |= IORING_SETUP_FIXEDBUFS;
469 ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
474 return fio_ioring_mmap(ld, &p);
477 static int fio_ioring_post_init(struct thread_data *td)
479 struct ioring_data *ld = td->io_ops_data;
480 struct ioring_options *o = td->eo;
487 for (i = 0; i < td->o.iodepth; i++) {
488 struct iovec *iov = &ld->iovecs[i];
490 io_u = ld->io_u_index[i];
491 iov->iov_base = io_u->buf;
492 iov->iov_len = td_max_bs(td);
496 err = fio_ioring_queue_init(td);
498 td_verror(td, errno, "io_queue_init");
505 static unsigned roundup_pow2(unsigned depth)
507 return 1UL << __fls(depth - 1);
510 static int fio_ioring_init(struct thread_data *td)
512 struct ioring_data *ld;
514 ld = calloc(1, sizeof(*ld));
516 /* ring depth must be a power-of-2 */
517 ld->iodepth = td->o.iodepth;
518 td->o.iodepth = roundup_pow2(td->o.iodepth);
521 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
522 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
524 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
526 td->io_ops_data = ld;
530 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
532 struct ioring_data *ld = td->io_ops_data;
534 ld->io_u_index[io_u->index] = io_u;
538 static struct ioengine_ops ioengine = {
540 .version = FIO_IOOPS_VERSION,
541 .init = fio_ioring_init,
542 .post_init = fio_ioring_post_init,
543 .io_u_init = fio_ioring_io_u_init,
544 .prep = fio_ioring_prep,
545 .queue = fio_ioring_queue,
546 .commit = fio_ioring_commit,
547 .getevents = fio_ioring_getevents,
548 .event = fio_ioring_event,
549 .cleanup = fio_ioring_cleanup,
550 .open_file = generic_open_file,
551 .close_file = generic_close_file,
552 .get_file_size = generic_get_file_size,
554 .option_struct_size = sizeof(struct ioring_options),
557 static void fio_init fio_ioring_register(void)
559 register_ioengine(&ioengine);
562 static void fio_exit fio_ioring_unregister(void)
564 unregister_ioengine(&ioengine);