4 * IO engine using the new native Linux libaio ring interface. See:
6 * http://git.kernel.dk/cgit/linux-block/log/?h=aio-poll
14 #include <sys/resource.h>
17 #include "../lib/pow2.h"
18 #include "../optgroup.h"
19 #include "../lib/memalign.h"
20 #include "../lib/fls.h"
22 #ifdef ARCH_HAVE_AIORING
25 * io_uring_setup(2) flags
27 #ifndef IOCTX_FLAG_SCQRING
28 #define IOCTX_FLAG_SCQRING (1 << 0)
30 #ifndef IOCTX_FLAG_IOPOLL
31 #define IOCTX_FLAG_IOPOLL (1 << 1)
33 #ifndef IOCTX_FLAG_FIXEDBUFS
34 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
36 #ifndef IOCTX_FLAG_SQTHREAD
37 #define IOCTX_FLAG_SQTHREAD (1 << 3)
39 #ifndef IOCTX_FLAG_SQWQ
40 #define IOCTX_FLAG_SQWQ (1 << 4)
42 #ifndef IOCTX_FLAG_SQPOLL
43 #define IOCTX_FLAG_SQPOLL (1 << 5)
46 #define IORING_OFF_SQ_RING 0ULL
47 #define IORING_OFF_CQ_RING 0x8000000ULL
48 #define IORING_OFF_IOCB 0x10000000ULL
51 * io_uring_enter(2) flags
53 #ifndef IORING_ENTER_GETEVENTS
54 #define IORING_ENTER_GETEVENTS (1 << 0)
61 #define IORING_SQ_NEED_WAKEUP (1 << 0)
63 #define IOEV_RES2_CACHEHIT (1 << 0)
65 struct aio_sqring_offsets {
76 struct aio_cqring_offsets {
86 struct aio_uring_params {
92 struct aio_sqring_offsets sq_off;
93 struct aio_cqring_offsets cq_off;
110 struct io_event *events;
113 struct aioring_mmap {
118 struct aioring_data {
122 struct io_u **io_u_index;
124 struct aio_sq_ring sq_ring;
126 struct iovec *iovecs;
127 unsigned sq_ring_mask;
129 struct aio_cq_ring cq_ring;
130 struct io_event *events;
131 unsigned cq_ring_mask;
140 struct aioring_mmap mmap[3];
143 struct aioring_options {
146 unsigned int fixedbufs;
147 unsigned int sqthread;
148 unsigned int sqthread_set;
149 unsigned int sqthread_poll;
153 static int fio_aioring_sqthread_cb(void *data,
154 unsigned long long *val)
156 struct aioring_options *o = data;
163 static struct fio_option options[] = {
166 .lname = "High Priority",
167 .type = FIO_OPT_STR_SET,
168 .off1 = offsetof(struct aioring_options, hipri),
169 .help = "Use polled IO completions",
170 .category = FIO_OPT_C_ENGINE,
171 .group = FIO_OPT_G_LIBAIO,
175 .lname = "Fixed (pre-mapped) IO buffers",
176 .type = FIO_OPT_STR_SET,
177 .off1 = offsetof(struct aioring_options, fixedbufs),
178 .help = "Pre map IO buffers",
179 .category = FIO_OPT_C_ENGINE,
180 .group = FIO_OPT_G_LIBAIO,
184 .lname = "Use kernel SQ thread on this CPU",
186 .cb = fio_aioring_sqthread_cb,
187 .help = "Offload submission to kernel thread",
188 .category = FIO_OPT_C_ENGINE,
189 .group = FIO_OPT_G_LIBAIO,
192 .name = "sqthread_poll",
193 .lname = "Kernel SQ thread should poll",
194 .type = FIO_OPT_STR_SET,
195 .off1 = offsetof(struct aioring_options, sqthread_poll),
196 .help = "Used with sqthread, enables kernel side polling",
197 .category = FIO_OPT_C_ENGINE,
198 .group = FIO_OPT_G_LIBAIO,
202 .lname = "Offload submission to kernel workqueue",
203 .type = FIO_OPT_STR_SET,
204 .off1 = offsetof(struct aioring_options, sqwq),
205 .help = "Offload submission to kernel workqueue",
206 .category = FIO_OPT_C_ENGINE,
207 .group = FIO_OPT_G_LIBAIO,
214 static int io_uring_enter(struct aioring_data *ld, unsigned int to_submit,
215 unsigned int min_complete, unsigned int flags)
217 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
218 min_complete, flags);
221 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
223 struct aioring_data *ld = td->io_ops_data;
224 struct fio_file *f = io_u->file;
227 iocb = &ld->iocbs[io_u->index];
229 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
230 if (io_u->ddir == DDIR_READ)
231 iocb->aio_lio_opcode = IO_CMD_PREAD;
233 iocb->aio_lio_opcode = IO_CMD_PWRITE;
234 iocb->aio_reqprio = 0;
235 iocb->aio_fildes = f->fd;
236 iocb->u.c.buf = io_u->xfer_buf;
237 iocb->u.c.nbytes = io_u->xfer_buflen;
238 iocb->u.c.offset = io_u->offset;
240 } else if (ddir_sync(io_u->ddir))
241 io_prep_fsync(iocb, f->fd);
247 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
249 struct aioring_data *ld = td->io_ops_data;
254 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
256 ev = &ld->cq_ring.events[index];
259 if (ev->res != io_u->xfer_buflen) {
260 if (ev->res > io_u->xfer_buflen)
261 io_u->error = -ev->res;
263 io_u->resid = io_u->xfer_buflen - ev->res;
267 if (io_u->ddir == DDIR_READ) {
268 if (ev->res2 & IOEV_RES2_CACHEHIT)
277 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
280 struct aioring_data *ld = td->io_ops_data;
281 struct aio_cq_ring *ring = &ld->cq_ring;
282 u32 head, reaped = 0;
287 if (head == *ring->tail)
291 } while (reaped + events < max);
298 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
299 unsigned int max, const struct timespec *t)
301 struct aioring_data *ld = td->io_ops_data;
302 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
303 struct aioring_options *o = td->eo;
304 struct aio_cq_ring *ring = &ld->cq_ring;
308 ld->cq_ring_off = *ring->head;
310 r = fio_aioring_cqring_reap(td, events, max);
316 if (!o->sqthread_poll) {
317 r = io_uring_enter(ld, 0, actual_min,
318 IORING_ENTER_GETEVENTS);
322 td_verror(td, errno, "io_uring_enter");
326 } while (events < min);
328 return r < 0 ? r : events;
331 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
334 struct aioring_data *ld = td->io_ops_data;
335 struct aio_sq_ring *ring = &ld->sq_ring;
336 unsigned tail, next_tail;
338 fio_ro_check(td, io_u);
340 if (ld->queued == ld->iodepth)
343 if (io_u->ddir == DDIR_TRIM) {
347 do_io_u_trim(td, io_u);
348 io_u_mark_submit(td, 1);
349 io_u_mark_complete(td, 1);
350 return FIO_Q_COMPLETED;
354 next_tail = tail + 1;
356 if (next_tail == *ring->head)
359 ring->array[tail & ld->sq_ring_mask] = io_u->index;
360 *ring->tail = next_tail;
367 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
369 struct aioring_data *ld = td->io_ops_data;
372 if (!fio_fill_issue_time(td))
375 fio_gettime(&now, NULL);
378 struct aio_sq_ring *ring = &ld->sq_ring;
379 int index = ring->array[start & ld->sq_ring_mask];
380 struct io_u *io_u = ld->io_u_index[index];
382 memcpy(&io_u->issue_time, &now, sizeof(now));
383 io_u_queued(td, io_u);
389 static int fio_aioring_commit(struct thread_data *td)
391 struct aioring_data *ld = td->io_ops_data;
392 struct aioring_options *o = td->eo;
399 if (o->sqthread_poll) {
400 struct aio_sq_ring *ring = &ld->sq_ring;
402 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
403 io_uring_enter(ld, ld->queued, 0, 0);
409 unsigned start = *ld->sq_ring.head;
410 long nr = ld->queued;
412 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
414 fio_aioring_queued(td, start, ret);
415 io_u_mark_submit(td, ret);
420 io_u_mark_submit(td, ret);
423 if (errno == EAGAIN) {
424 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
427 /* Shouldn't happen */
431 td_verror(td, errno, "io_uring_enter submit");
434 } while (ld->queued);
439 static void fio_aioring_unmap(struct aioring_data *ld)
443 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
444 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
448 static void fio_aioring_cleanup(struct thread_data *td)
450 struct aioring_data *ld = td->io_ops_data;
453 td->ts.cachehit += ld->cachehit;
454 td->ts.cachemiss += ld->cachemiss;
457 * Work-around to avoid huge RCU stalls at exit time. If we
458 * don't do this here, then it'll be torn down by exit_aio().
459 * But for that case we can parallellize the freeing, thus
460 * speeding it up a lot.
462 if (!(td->flags & TD_F_CHILD))
463 fio_aioring_unmap(ld);
465 free(ld->io_u_index);
472 static int fio_aioring_mmap(struct aioring_data *ld, struct aio_uring_params *p)
474 struct aio_sq_ring *sring = &ld->sq_ring;
475 struct aio_cq_ring *cring = &ld->cq_ring;
478 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(u32);
479 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
480 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
482 ld->mmap[0].ptr = ptr;
483 sring->head = ptr + p->sq_off.head;
484 sring->tail = ptr + p->sq_off.tail;
485 sring->ring_mask = ptr + p->sq_off.ring_mask;
486 sring->ring_entries = ptr + p->sq_off.ring_entries;
487 sring->flags = ptr + p->sq_off.flags;
488 sring->array = ptr + p->sq_off.array;
489 ld->sq_ring_mask = *sring->ring_mask;
491 ld->mmap[1].len = p->sq_entries * sizeof(struct iocb);
492 ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
493 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
495 ld->mmap[1].ptr = ld->iocbs;
497 ld->mmap[2].len = p->cq_off.events +
498 p->cq_entries * sizeof(struct io_event);
499 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
500 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
502 ld->mmap[2].ptr = ptr;
503 cring->head = ptr + p->cq_off.head;
504 cring->tail = ptr + p->cq_off.tail;
505 cring->ring_mask = ptr + p->cq_off.ring_mask;
506 cring->ring_entries = ptr + p->cq_off.ring_entries;
507 cring->events = ptr + p->cq_off.events;
508 ld->cq_ring_mask = *cring->ring_mask;
512 static int fio_aioring_queue_init(struct thread_data *td)
514 struct aioring_data *ld = td->io_ops_data;
515 struct aioring_options *o = td->eo;
516 int depth = td->o.iodepth;
517 struct aio_uring_params p;
520 memset(&p, 0, sizeof(p));
521 p.flags = IOCTX_FLAG_SCQRING;
524 p.flags |= IOCTX_FLAG_IOPOLL;
525 if (o->sqthread_set) {
526 p.sq_thread_cpu = o->sqthread;
527 p.flags |= IOCTX_FLAG_SQTHREAD;
528 if (o->sqthread_poll)
529 p.flags |= IOCTX_FLAG_SQPOLL;
532 p.flags |= IOCTX_FLAG_SQWQ;
535 struct rlimit rlim = {
536 .rlim_cur = RLIM_INFINITY,
537 .rlim_max = RLIM_INFINITY,
540 setrlimit(RLIMIT_MEMLOCK, &rlim);
541 p.flags |= IOCTX_FLAG_FIXEDBUFS;
544 ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
549 return fio_aioring_mmap(ld, &p);
552 static int fio_aioring_post_init(struct thread_data *td)
554 struct aioring_data *ld = td->io_ops_data;
555 struct aioring_options *o = td->eo;
562 for (i = 0; i < td->o.iodepth; i++) {
563 struct iovec *iov = &ld->iovecs[i];
565 io_u = ld->io_u_index[i];
566 iov->iov_base = io_u->buf;
567 iov->iov_len = td_max_bs(td);
571 err = fio_aioring_queue_init(td);
573 td_verror(td, errno, "io_queue_init");
580 static unsigned roundup_pow2(unsigned depth)
582 return 1UL << __fls(depth - 1);
585 static int fio_aioring_init(struct thread_data *td)
587 struct aioring_data *ld;
589 ld = calloc(1, sizeof(*ld));
591 /* ring depth must be a power-of-2 */
592 ld->iodepth = td->o.iodepth;
593 td->o.iodepth = roundup_pow2(td->o.iodepth);
596 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
597 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
599 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
601 td->io_ops_data = ld;
605 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
607 struct aioring_data *ld = td->io_ops_data;
609 ld->io_u_index[io_u->index] = io_u;
613 static struct ioengine_ops ioengine = {
615 .version = FIO_IOOPS_VERSION,
616 .init = fio_aioring_init,
617 .post_init = fio_aioring_post_init,
618 .io_u_init = fio_aioring_io_u_init,
619 .prep = fio_aioring_prep,
620 .queue = fio_aioring_queue,
621 .commit = fio_aioring_commit,
622 .getevents = fio_aioring_getevents,
623 .event = fio_aioring_event,
624 .cleanup = fio_aioring_cleanup,
625 .open_file = generic_open_file,
626 .close_file = generic_close_file,
627 .get_file_size = generic_get_file_size,
629 .option_struct_size = sizeof(struct aioring_options),
632 static void fio_init fio_aioring_register(void)
634 register_ioengine(&ioengine);
637 static void fio_exit fio_aioring_unregister(void)
639 unregister_ioengine(&ioengine);