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_event *events;
52 struct io_u **io_u_index;
54 struct io_sq_ring sq_ring;
55 struct io_uring_iocb *iocbs;
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 sqthread;
77 unsigned int sqthread_set;
78 unsigned int sqthread_poll;
82 static int fio_ioring_sqthread_cb(void *data, unsigned long long *val)
84 struct ioring_options *o = data;
91 static struct fio_option options[] = {
94 .lname = "High Priority",
95 .type = FIO_OPT_STR_SET,
96 .off1 = offsetof(struct ioring_options, hipri),
97 .help = "Use polled IO completions",
98 .category = FIO_OPT_C_ENGINE,
99 .group = FIO_OPT_G_LIBAIO,
103 .lname = "Fixed (pre-mapped) IO buffers",
104 .type = FIO_OPT_STR_SET,
105 .off1 = offsetof(struct ioring_options, fixedbufs),
106 .help = "Pre map IO buffers",
107 .category = FIO_OPT_C_ENGINE,
108 .group = FIO_OPT_G_LIBAIO,
112 .lname = "Use kernel SQ thread on this CPU",
114 .cb = fio_ioring_sqthread_cb,
115 .help = "Offload submission to kernel thread",
116 .category = FIO_OPT_C_ENGINE,
117 .group = FIO_OPT_G_LIBAIO,
120 .name = "sqthread_poll",
121 .lname = "Kernel SQ thread should poll",
122 .type = FIO_OPT_STR_SET,
123 .off1 = offsetof(struct ioring_options, sqthread_poll),
124 .help = "Used with sqthread, enables kernel side polling",
125 .category = FIO_OPT_C_ENGINE,
126 .group = FIO_OPT_G_LIBAIO,
130 .lname = "Offload submission to kernel workqueue",
131 .type = FIO_OPT_STR_SET,
132 .off1 = offsetof(struct ioring_options, sqwq),
133 .help = "Offload submission to kernel workqueue",
134 .category = FIO_OPT_C_ENGINE,
135 .group = FIO_OPT_G_LIBAIO,
142 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
143 unsigned int min_complete, unsigned int flags)
145 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
146 min_complete, flags);
149 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
151 struct ioring_data *ld = td->io_ops_data;
152 struct ioring_options *o = td->eo;
153 struct fio_file *f = io_u->file;
154 struct io_uring_iocb *iocb;
156 iocb = &ld->iocbs[io_u->index];
161 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
162 if (io_u->ddir == DDIR_READ) {
164 iocb->opcode = IORING_OP_READ_FIXED;
166 iocb->opcode = IORING_OP_READ;
169 iocb->opcode = IORING_OP_WRITE_FIXED;
171 iocb->opcode = IORING_OP_WRITE;
173 iocb->off = io_u->offset;
174 iocb->addr = io_u->xfer_buf;
175 iocb->len = io_u->xfer_buflen;
176 } else if (ddir_sync(io_u->ddir))
177 iocb->opcode = IORING_OP_FSYNC;
182 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
184 struct ioring_data *ld = td->io_ops_data;
185 struct io_uring_event *ev;
189 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
191 ev = &ld->cq_ring.events[index];
192 io_u = ld->io_u_index[ev->index];
194 if (ev->res != io_u->xfer_buflen) {
195 if (ev->res > io_u->xfer_buflen)
196 io_u->error = -ev->res;
198 io_u->resid = io_u->xfer_buflen - ev->res;
202 if (io_u->ddir == DDIR_READ) {
203 if (ev->flags & IOEV_FLAG_CACHEHIT)
212 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
215 struct ioring_data *ld = td->io_ops_data;
216 struct io_cq_ring *ring = &ld->cq_ring;
217 unsigned head, reaped = 0;
222 if (head == *ring->tail)
226 } while (reaped + events < max);
233 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
234 unsigned int max, const struct timespec *t)
236 struct ioring_data *ld = td->io_ops_data;
237 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
238 struct ioring_options *o = td->eo;
239 struct io_cq_ring *ring = &ld->cq_ring;
243 ld->cq_ring_off = *ring->head;
245 r = fio_ioring_cqring_reap(td, events, max);
251 if (!o->sqthread_poll) {
252 r = io_uring_enter(ld, 0, actual_min,
253 IORING_ENTER_GETEVENTS);
257 td_verror(td, errno, "io_uring_enter");
261 } while (events < min);
263 return r < 0 ? r : events;
266 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
269 struct ioring_data *ld = td->io_ops_data;
270 struct io_sq_ring *ring = &ld->sq_ring;
271 unsigned tail, next_tail;
273 fio_ro_check(td, io_u);
275 if (ld->queued == ld->iodepth)
278 if (io_u->ddir == DDIR_TRIM) {
282 do_io_u_trim(td, io_u);
283 io_u_mark_submit(td, 1);
284 io_u_mark_complete(td, 1);
285 return FIO_Q_COMPLETED;
289 next_tail = tail + 1;
291 if (next_tail == *ring->head)
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 if (o->sqthread_poll) {
335 struct io_sq_ring *ring = &ld->sq_ring;
337 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
338 io_uring_enter(ld, ld->queued, 0, 0);
344 unsigned start = *ld->sq_ring.head;
345 long nr = ld->queued;
347 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
349 fio_ioring_queued(td, start, ret);
350 io_u_mark_submit(td, ret);
355 io_u_mark_submit(td, ret);
358 if (errno == EAGAIN) {
359 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
362 /* Shouldn't happen */
366 td_verror(td, errno, "io_uring_enter submit");
369 } while (ld->queued);
374 static void fio_ioring_unmap(struct ioring_data *ld)
378 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
379 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
383 static void fio_ioring_cleanup(struct thread_data *td)
385 struct ioring_data *ld = td->io_ops_data;
388 td->ts.cachehit += ld->cachehit;
389 td->ts.cachemiss += ld->cachemiss;
391 if (!(td->flags & TD_F_CHILD))
392 fio_ioring_unmap(ld);
394 free(ld->io_u_index);
401 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
403 struct io_sq_ring *sring = &ld->sq_ring;
404 struct io_cq_ring *cring = &ld->cq_ring;
407 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
408 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
409 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
411 ld->mmap[0].ptr = ptr;
412 sring->head = ptr + p->sq_off.head;
413 sring->tail = ptr + p->sq_off.tail;
414 sring->ring_mask = ptr + p->sq_off.ring_mask;
415 sring->ring_entries = ptr + p->sq_off.ring_entries;
416 sring->flags = ptr + p->sq_off.flags;
417 sring->array = ptr + p->sq_off.array;
418 ld->sq_ring_mask = *sring->ring_mask;
420 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_iocb);
421 ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
422 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
424 ld->mmap[1].ptr = ld->iocbs;
426 ld->mmap[2].len = p->cq_off.events +
427 p->cq_entries * sizeof(struct io_uring_event);
428 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
429 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
431 ld->mmap[2].ptr = ptr;
432 cring->head = ptr + p->cq_off.head;
433 cring->tail = ptr + p->cq_off.tail;
434 cring->ring_mask = ptr + p->cq_off.ring_mask;
435 cring->ring_entries = ptr + p->cq_off.ring_entries;
436 cring->events = ptr + p->cq_off.events;
437 ld->cq_ring_mask = *cring->ring_mask;
441 static int fio_ioring_queue_init(struct thread_data *td)
443 struct ioring_data *ld = td->io_ops_data;
444 struct ioring_options *o = td->eo;
445 int depth = td->o.iodepth;
446 struct io_uring_params p;
449 memset(&p, 0, sizeof(p));
452 p.flags |= IORING_SETUP_IOPOLL;
453 if (o->sqthread_set) {
454 p.sq_thread_cpu = o->sqthread;
455 p.flags |= IORING_SETUP_SQTHREAD;
456 if (o->sqthread_poll)
457 p.flags |= IORING_SETUP_SQPOLL;
460 p.flags |= IORING_SETUP_SQWQ;
463 struct rlimit rlim = {
464 .rlim_cur = RLIM_INFINITY,
465 .rlim_max = RLIM_INFINITY,
468 setrlimit(RLIMIT_MEMLOCK, &rlim);
469 p.flags |= IORING_SETUP_FIXEDBUFS;
472 ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
477 return fio_ioring_mmap(ld, &p);
480 static int fio_ioring_post_init(struct thread_data *td)
482 struct ioring_data *ld = td->io_ops_data;
483 struct ioring_options *o = td->eo;
490 for (i = 0; i < td->o.iodepth; i++) {
491 struct iovec *iov = &ld->iovecs[i];
493 io_u = ld->io_u_index[i];
494 iov->iov_base = io_u->buf;
495 iov->iov_len = td_max_bs(td);
499 err = fio_ioring_queue_init(td);
501 td_verror(td, errno, "io_queue_init");
508 static unsigned roundup_pow2(unsigned depth)
510 return 1UL << __fls(depth - 1);
513 static int fio_ioring_init(struct thread_data *td)
515 struct ioring_data *ld;
517 ld = calloc(1, sizeof(*ld));
519 /* ring depth must be a power-of-2 */
520 ld->iodepth = td->o.iodepth;
521 td->o.iodepth = roundup_pow2(td->o.iodepth);
524 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
525 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
527 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
529 td->io_ops_data = ld;
533 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
535 struct ioring_data *ld = td->io_ops_data;
537 ld->io_u_index[io_u->index] = io_u;
541 static struct ioengine_ops ioengine = {
543 .version = FIO_IOOPS_VERSION,
544 .init = fio_ioring_init,
545 .post_init = fio_ioring_post_init,
546 .io_u_init = fio_ioring_io_u_init,
547 .prep = fio_ioring_prep,
548 .queue = fio_ioring_queue,
549 .commit = fio_ioring_commit,
550 .getevents = fio_ioring_getevents,
551 .event = fio_ioring_event,
552 .cleanup = fio_ioring_cleanup,
553 .open_file = generic_open_file,
554 .close_file = generic_close_file,
555 .get_file_size = generic_get_file_size,
557 .option_struct_size = sizeof(struct ioring_options),
560 static void fio_init fio_ioring_register(void)
562 register_ioengine(&ioengine);
565 static void fio_exit fio_ioring_unregister(void)
567 unregister_ioengine(&ioengine);