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 {
74 struct aio_cqring_offsets {
83 struct aio_uring_params {
89 struct aio_sqring_offsets sq_off;
90 struct aio_cqring_offsets cq_off;
107 struct io_event *events;
110 struct aioring_mmap {
115 struct aioring_data {
119 struct io_u **io_u_index;
121 struct aio_sq_ring sq_ring;
123 struct iovec *iovecs;
124 unsigned sq_ring_mask;
126 struct aio_cq_ring cq_ring;
127 struct io_event *events;
128 unsigned cq_ring_mask;
137 struct aioring_mmap mmap[3];
140 struct aioring_options {
143 unsigned int fixedbufs;
144 unsigned int sqthread;
145 unsigned int sqthread_set;
146 unsigned int sqthread_poll;
150 static int fio_aioring_sqthread_cb(void *data,
151 unsigned long long *val)
153 struct aioring_options *o = data;
160 static struct fio_option options[] = {
163 .lname = "High Priority",
164 .type = FIO_OPT_STR_SET,
165 .off1 = offsetof(struct aioring_options, hipri),
166 .help = "Use polled IO completions",
167 .category = FIO_OPT_C_ENGINE,
168 .group = FIO_OPT_G_LIBAIO,
172 .lname = "Fixed (pre-mapped) IO buffers",
173 .type = FIO_OPT_STR_SET,
174 .off1 = offsetof(struct aioring_options, fixedbufs),
175 .help = "Pre map IO buffers",
176 .category = FIO_OPT_C_ENGINE,
177 .group = FIO_OPT_G_LIBAIO,
181 .lname = "Use kernel SQ thread on this CPU",
183 .cb = fio_aioring_sqthread_cb,
184 .help = "Offload submission to kernel thread",
185 .category = FIO_OPT_C_ENGINE,
186 .group = FIO_OPT_G_LIBAIO,
189 .name = "sqthread_poll",
190 .lname = "Kernel SQ thread should poll",
191 .type = FIO_OPT_STR_SET,
192 .off1 = offsetof(struct aioring_options, sqthread_poll),
193 .help = "Used with sqthread, enables kernel side polling",
194 .category = FIO_OPT_C_ENGINE,
195 .group = FIO_OPT_G_LIBAIO,
199 .lname = "Offload submission to kernel workqueue",
200 .type = FIO_OPT_STR_SET,
201 .off1 = offsetof(struct aioring_options, sqwq),
202 .help = "Offload submission to kernel workqueue",
203 .category = FIO_OPT_C_ENGINE,
204 .group = FIO_OPT_G_LIBAIO,
211 static int io_uring_enter(struct aioring_data *ld, unsigned int to_submit,
212 unsigned int min_complete, unsigned int flags)
214 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
215 min_complete, flags);
218 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
220 struct aioring_data *ld = td->io_ops_data;
221 struct fio_file *f = io_u->file;
224 iocb = &ld->iocbs[io_u->index];
226 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
227 if (io_u->ddir == DDIR_READ)
228 iocb->aio_lio_opcode = IO_CMD_PREAD;
230 iocb->aio_lio_opcode = IO_CMD_PWRITE;
231 iocb->aio_reqprio = 0;
232 iocb->aio_fildes = f->fd;
233 iocb->u.c.buf = io_u->xfer_buf;
234 iocb->u.c.nbytes = io_u->xfer_buflen;
235 iocb->u.c.offset = io_u->offset;
237 } else if (ddir_sync(io_u->ddir))
238 io_prep_fsync(iocb, f->fd);
244 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
246 struct aioring_data *ld = td->io_ops_data;
251 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
253 ev = &ld->cq_ring.events[index];
256 if (ev->res != io_u->xfer_buflen) {
257 if (ev->res > io_u->xfer_buflen)
258 io_u->error = -ev->res;
260 io_u->resid = io_u->xfer_buflen - ev->res;
264 if (io_u->ddir == DDIR_READ) {
265 if (ev->res2 & IOEV_RES2_CACHEHIT)
274 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
277 struct aioring_data *ld = td->io_ops_data;
278 struct aio_cq_ring *ring = &ld->cq_ring;
279 u32 head, reaped = 0;
284 if (head == *ring->tail)
288 } while (reaped + events < max);
295 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
296 unsigned int max, const struct timespec *t)
298 struct aioring_data *ld = td->io_ops_data;
299 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
300 struct aioring_options *o = td->eo;
301 struct aio_cq_ring *ring = &ld->cq_ring;
305 ld->cq_ring_off = *ring->head;
307 r = fio_aioring_cqring_reap(td, events, max);
313 if (!o->sqthread_poll) {
314 r = io_uring_enter(ld, 0, actual_min,
315 IORING_ENTER_GETEVENTS);
319 td_verror(td, errno, "io_uring_enter");
323 } while (events < min);
325 return r < 0 ? r : events;
328 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
331 struct aioring_data *ld = td->io_ops_data;
332 struct aio_sq_ring *ring = &ld->sq_ring;
333 unsigned tail, next_tail;
335 fio_ro_check(td, io_u);
337 if (ld->queued == ld->iodepth)
340 if (io_u->ddir == DDIR_TRIM) {
344 do_io_u_trim(td, io_u);
345 io_u_mark_submit(td, 1);
346 io_u_mark_complete(td, 1);
347 return FIO_Q_COMPLETED;
351 next_tail = tail + 1;
353 if (next_tail == *ring->head)
356 ring->array[tail & ld->sq_ring_mask] = io_u->index;
357 *ring->tail = next_tail;
364 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
366 struct aioring_data *ld = td->io_ops_data;
369 if (!fio_fill_issue_time(td))
372 fio_gettime(&now, NULL);
375 struct aio_sq_ring *ring = &ld->sq_ring;
376 int index = ring->array[start & ld->sq_ring_mask];
377 struct io_u *io_u = ld->io_u_index[index];
379 memcpy(&io_u->issue_time, &now, sizeof(now));
380 io_u_queued(td, io_u);
386 static int fio_aioring_commit(struct thread_data *td)
388 struct aioring_data *ld = td->io_ops_data;
389 struct aioring_options *o = td->eo;
396 if (o->sqthread_poll) {
397 struct aio_sq_ring *ring = &ld->sq_ring;
399 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
400 io_uring_enter(ld, ld->queued, 0, 0);
406 unsigned start = *ld->sq_ring.head;
407 long nr = ld->queued;
409 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
411 fio_aioring_queued(td, start, ret);
412 io_u_mark_submit(td, ret);
417 io_u_mark_submit(td, ret);
420 if (errno == EAGAIN) {
421 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
424 /* Shouldn't happen */
428 td_verror(td, errno, "io_uring_enter submit");
431 } while (ld->queued);
436 static void fio_aioring_unmap(struct aioring_data *ld)
440 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
441 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
445 static void fio_aioring_cleanup(struct thread_data *td)
447 struct aioring_data *ld = td->io_ops_data;
450 td->ts.cachehit += ld->cachehit;
451 td->ts.cachemiss += ld->cachemiss;
454 * Work-around to avoid huge RCU stalls at exit time. If we
455 * don't do this here, then it'll be torn down by exit_aio().
456 * But for that case we can parallellize the freeing, thus
457 * speeding it up a lot.
459 if (!(td->flags & TD_F_CHILD))
460 fio_aioring_unmap(ld);
462 free(ld->io_u_index);
469 static int fio_aioring_mmap(struct aioring_data *ld, struct aio_uring_params *p)
471 struct aio_sq_ring *sring = &ld->sq_ring;
472 struct aio_cq_ring *cring = &ld->cq_ring;
475 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(u32);
476 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
477 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
479 ld->mmap[0].ptr = ptr;
480 sring->head = ptr + p->sq_off.head;
481 sring->tail = ptr + p->sq_off.tail;
482 sring->ring_mask = ptr + p->sq_off.ring_mask;
483 sring->ring_entries = ptr + p->sq_off.ring_entries;
484 sring->flags = ptr + p->sq_off.flags;
485 sring->array = ptr + p->sq_off.array;
486 ld->sq_ring_mask = *sring->ring_mask;
488 ld->mmap[1].len = p->sq_entries * sizeof(struct iocb);
489 ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
490 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
492 ld->mmap[1].ptr = ld->iocbs;
494 ld->mmap[2].len = p->cq_off.events +
495 p->cq_entries * sizeof(struct io_event);
496 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
497 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
499 ld->mmap[2].ptr = ptr;
500 cring->head = ptr + p->cq_off.head;
501 cring->tail = ptr + p->cq_off.tail;
502 cring->ring_mask = ptr + p->cq_off.ring_mask;
503 cring->ring_entries = ptr + p->cq_off.ring_entries;
504 cring->events = ptr + p->cq_off.events;
505 ld->cq_ring_mask = *cring->ring_mask;
509 static int fio_aioring_queue_init(struct thread_data *td)
511 struct aioring_data *ld = td->io_ops_data;
512 struct aioring_options *o = td->eo;
513 int depth = td->o.iodepth;
514 struct aio_uring_params p;
517 memset(&p, 0, sizeof(p));
518 p.flags = IOCTX_FLAG_SCQRING;
521 p.flags |= IOCTX_FLAG_IOPOLL;
522 if (o->sqthread_set) {
523 p.sq_thread_cpu = o->sqthread;
524 p.flags |= IOCTX_FLAG_SQTHREAD;
525 if (o->sqthread_poll)
526 p.flags |= IOCTX_FLAG_SQPOLL;
529 p.flags |= IOCTX_FLAG_SQWQ;
532 struct rlimit rlim = {
533 .rlim_cur = RLIM_INFINITY,
534 .rlim_max = RLIM_INFINITY,
537 setrlimit(RLIMIT_MEMLOCK, &rlim);
538 p.flags |= IOCTX_FLAG_FIXEDBUFS;
541 ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
546 return fio_aioring_mmap(ld, &p);
549 static int fio_aioring_post_init(struct thread_data *td)
551 struct aioring_data *ld = td->io_ops_data;
552 struct aioring_options *o = td->eo;
559 for (i = 0; i < td->o.iodepth; i++) {
560 struct iovec *iov = &ld->iovecs[i];
562 io_u = ld->io_u_index[i];
563 iov->iov_base = io_u->buf;
564 iov->iov_len = td_max_bs(td);
568 err = fio_aioring_queue_init(td);
570 td_verror(td, errno, "io_queue_init");
577 static unsigned roundup_pow2(unsigned depth)
579 return 1UL << __fls(depth - 1);
582 static int fio_aioring_init(struct thread_data *td)
584 struct aioring_data *ld;
586 ld = calloc(1, sizeof(*ld));
588 /* ring depth must be a power-of-2 */
589 ld->iodepth = td->o.iodepth;
590 td->o.iodepth = roundup_pow2(td->o.iodepth);
593 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
594 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
596 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
598 td->io_ops_data = ld;
602 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
604 struct aioring_data *ld = td->io_ops_data;
606 ld->io_u_index[io_u->index] = io_u;
610 static struct ioengine_ops ioengine = {
612 .version = FIO_IOOPS_VERSION,
613 .init = fio_aioring_init,
614 .post_init = fio_aioring_post_init,
615 .io_u_init = fio_aioring_io_u_init,
616 .prep = fio_aioring_prep,
617 .queue = fio_aioring_queue,
618 .commit = fio_aioring_commit,
619 .getevents = fio_aioring_getevents,
620 .event = fio_aioring_event,
621 .cleanup = fio_aioring_cleanup,
622 .open_file = generic_open_file,
623 .close_file = generic_close_file,
624 .get_file_size = generic_get_file_size,
626 .option_struct_size = sizeof(struct aioring_options),
629 static void fio_init fio_aioring_register(void)
631 register_ioengine(&ioengine);
634 static void fio_exit fio_aioring_unregister(void)
636 unregister_ioengine(&ioengine);