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
24 #ifndef IOCB_FLAG_HIPRI
25 #define IOCB_FLAG_HIPRI (1 << 2)
31 #ifndef IOCTX_FLAG_IOPOLL
32 #define IOCTX_FLAG_IOPOLL (1 << 0)
34 #ifndef IOCTX_FLAG_SCQRING
35 #define IOCTX_FLAG_SCQRING (1 << 1)
37 #ifndef IOCTX_FLAG_FIXEDBUFS
38 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
40 #ifndef IOCTX_FLAG_SQTHREAD
41 #define IOCTX_FLAG_SQTHREAD (1 << 3)
43 #ifndef IOCTX_FLAG_SQWQ
44 #define IOCTX_FLAG_SQWQ (1 << 4)
46 #ifndef IOCTX_FLAG_SQPOLL
47 #define IOCTX_FLAG_SQPOLL (1 << 5)
52 * io_ring_enter(2) flags
54 #ifndef IORING_FLAG_GETEVENTS
55 #define IORING_FLAG_GETEVENTS (1 << 0)
62 #define IORING_SQ_NEED_WAKEUP (1 << 0)
64 #define IOEV_RES2_CACHEHIT (1 << 0)
90 struct io_event events[0];
96 struct io_u **io_u_index;
98 struct aio_sq_ring *sq_ring;
100 unsigned sq_ring_mask;
102 struct aio_cq_ring *cq_ring;
103 struct io_event *events;
104 unsigned cq_ring_mask;
114 struct aioring_options {
117 unsigned int fixedbufs;
118 unsigned int sqthread;
119 unsigned int sqthread_set;
120 unsigned int sqthread_poll;
124 static int fio_aioring_sqthread_cb(void *data,
125 unsigned long long *val)
127 struct aioring_options *o = data;
134 static struct fio_option options[] = {
137 .lname = "High Priority",
138 .type = FIO_OPT_STR_SET,
139 .off1 = offsetof(struct aioring_options, hipri),
140 .help = "Use polled IO completions",
141 .category = FIO_OPT_C_ENGINE,
142 .group = FIO_OPT_G_LIBAIO,
146 .lname = "Fixed (pre-mapped) IO buffers",
147 .type = FIO_OPT_STR_SET,
148 .off1 = offsetof(struct aioring_options, fixedbufs),
149 .help = "Pre map IO buffers",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_LIBAIO,
155 .lname = "Use kernel SQ thread on this CPU",
157 .cb = fio_aioring_sqthread_cb,
158 .help = "Offload submission to kernel thread",
159 .category = FIO_OPT_C_ENGINE,
160 .group = FIO_OPT_G_LIBAIO,
163 .name = "sqthread_poll",
164 .lname = "Kernel SQ thread should poll",
165 .type = FIO_OPT_STR_SET,
166 .off1 = offsetof(struct aioring_options, sqthread_poll),
167 .help = "Used with sqthread, enables kernel side polling",
168 .category = FIO_OPT_C_ENGINE,
169 .group = FIO_OPT_G_LIBAIO,
173 .lname = "Offload submission to kernel workqueue",
174 .type = FIO_OPT_STR_SET,
175 .off1 = offsetof(struct aioring_options, sqwq),
176 .help = "Offload submission to kernel workqueue",
177 .category = FIO_OPT_C_ENGINE,
178 .group = FIO_OPT_G_LIBAIO,
185 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
186 unsigned int min_complete, unsigned int flags)
188 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
192 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
194 struct aioring_data *ld = td->io_ops_data;
195 struct fio_file *f = io_u->file;
196 struct aioring_options *o = td->eo;
199 iocb = &ld->iocbs[io_u->index];
201 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
202 if (io_u->ddir == DDIR_READ)
203 iocb->aio_lio_opcode = IO_CMD_PREAD;
205 iocb->aio_lio_opcode = IO_CMD_PWRITE;
206 iocb->aio_reqprio = 0;
207 iocb->aio_fildes = f->fd;
208 iocb->u.c.buf = io_u->xfer_buf;
209 iocb->u.c.nbytes = io_u->xfer_buflen;
210 iocb->u.c.offset = io_u->offset;
212 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
215 } else if (ddir_sync(io_u->ddir))
216 io_prep_fsync(iocb, f->fd);
222 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
224 struct aioring_data *ld = td->io_ops_data;
229 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
231 ev = &ld->cq_ring->events[index];
234 if (ev->res != io_u->xfer_buflen) {
235 if (ev->res > io_u->xfer_buflen)
236 io_u->error = -ev->res;
238 io_u->resid = io_u->xfer_buflen - ev->res;
242 if (io_u->ddir == DDIR_READ) {
243 if (ev->res2 & IOEV_RES2_CACHEHIT)
252 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
255 struct aioring_data *ld = td->io_ops_data;
256 struct aio_cq_ring *ring = ld->cq_ring;
257 u32 head, reaped = 0;
262 if (head == ring->tail)
266 } while (reaped + events < max);
273 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
274 unsigned int max, const struct timespec *t)
276 struct aioring_data *ld = td->io_ops_data;
277 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
278 struct aioring_options *o = td->eo;
279 struct aio_cq_ring *ring = ld->cq_ring;
283 ld->cq_ring_off = ring->head;
285 r = fio_aioring_cqring_reap(td, events, max);
291 if (!o->sqthread_poll) {
292 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
293 IORING_FLAG_GETEVENTS);
297 td_verror(td, errno, "io_ring_enter get");
301 } while (events < min);
303 return r < 0 ? r : events;
306 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
309 struct aioring_data *ld = td->io_ops_data;
310 struct aio_sq_ring *ring = ld->sq_ring;
311 unsigned tail, next_tail;
313 fio_ro_check(td, io_u);
315 if (ld->queued == ld->iodepth)
318 if (io_u->ddir == DDIR_TRIM) {
322 do_io_u_trim(td, io_u);
323 io_u_mark_submit(td, 1);
324 io_u_mark_complete(td, 1);
325 return FIO_Q_COMPLETED;
329 next_tail = tail + 1;
331 if (next_tail == ring->head)
334 ring->array[tail & ld->sq_ring_mask] = io_u->index;
335 ring->tail = next_tail;
342 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
344 struct aioring_data *ld = td->io_ops_data;
347 if (!fio_fill_issue_time(td))
350 fio_gettime(&now, NULL);
353 int index = ld->sq_ring->array[start & ld->sq_ring_mask];
354 struct io_u *io_u = ld->io_u_index[index];
356 memcpy(&io_u->issue_time, &now, sizeof(now));
357 io_u_queued(td, io_u);
363 static int fio_aioring_commit(struct thread_data *td)
365 struct aioring_data *ld = td->io_ops_data;
366 struct aioring_options *o = td->eo;
373 if (o->sqthread_poll) {
374 struct aio_sq_ring *ring = ld->sq_ring;
376 if (ring->kflags & IORING_SQ_NEED_WAKEUP)
377 io_ring_enter(ld->aio_ctx, ld->queued, 0, 0);
383 unsigned start = ld->sq_ring->head;
384 long nr = ld->queued;
386 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_GETEVENTS);
388 fio_aioring_queued(td, start, ret);
389 io_u_mark_submit(td, ret);
394 io_u_mark_submit(td, ret);
397 if (errno == EAGAIN) {
398 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
401 /* Shouldn't happen */
405 td_verror(td, errno, "io_ring_enter sumit");
408 } while (ld->queued);
413 static size_t aioring_cq_size(struct thread_data *td)
415 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
418 static size_t aioring_sq_iocb(struct thread_data *td)
420 return sizeof(struct iocb) * td->o.iodepth;
423 static size_t aioring_sq_size(struct thread_data *td)
425 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
428 static unsigned roundup_pow2(unsigned depth)
430 return 1UL << __fls(depth - 1);
433 static void fio_aioring_cleanup(struct thread_data *td)
435 struct aioring_data *ld = td->io_ops_data;
438 td->ts.cachehit += ld->cachehit;
439 td->ts.cachemiss += ld->cachemiss;
442 * Work-around to avoid huge RCU stalls at exit time. If we
443 * don't do this here, then it'll be torn down by exit_aio().
444 * But for that case we can parallellize the freeing, thus
445 * speeding it up a lot.
447 if (!(td->flags & TD_F_CHILD))
448 io_destroy(ld->aio_ctx);
449 free(ld->io_u_index);
451 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
452 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
453 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
458 static int fio_aioring_queue_init(struct thread_data *td)
460 struct aioring_data *ld = td->io_ops_data;
461 struct aioring_options *o = td->eo;
462 int flags = IOCTX_FLAG_SCQRING;
463 int depth = td->o.iodepth;
466 flags |= IOCTX_FLAG_IOPOLL;
467 if (o->sqthread_set) {
468 ld->sq_ring->sq_thread_cpu = o->sqthread;
469 flags |= IOCTX_FLAG_SQTHREAD;
470 if (o->sqthread_poll)
471 flags |= IOCTX_FLAG_SQPOLL;
474 flags |= IOCTX_FLAG_SQWQ;
477 struct rlimit rlim = {
478 .rlim_cur = RLIM_INFINITY,
479 .rlim_max = RLIM_INFINITY,
482 setrlimit(RLIMIT_MEMLOCK, &rlim);
483 flags |= IOCTX_FLAG_FIXEDBUFS;
486 return syscall(__NR_sys_io_setup2, depth, flags,
487 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
490 static int fio_aioring_post_init(struct thread_data *td)
492 struct aioring_data *ld = td->io_ops_data;
493 struct aioring_options *o = td->eo;
501 for (i = 0; i < td->o.iodepth; i++) {
502 io_u = ld->io_u_index[i];
503 iocb = &ld->iocbs[i];
504 iocb->u.c.buf = io_u->buf;
505 iocb->u.c.nbytes = td_max_bs(td);
508 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
512 err = fio_aioring_queue_init(td);
515 td_verror(td, errno, "io_queue_init");
522 static int fio_aioring_init(struct thread_data *td)
524 struct aioring_data *ld;
526 ld = calloc(1, sizeof(*ld));
528 /* ring depth must be a power-of-2 */
529 ld->iodepth = td->o.iodepth;
530 td->o.iodepth = roundup_pow2(td->o.iodepth);
533 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
534 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
536 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
537 memset(ld->iocbs, 0, aioring_sq_iocb(td));
539 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
540 memset(ld->sq_ring, 0, aioring_sq_size(td));
541 ld->sq_ring->nr_events = td->o.iodepth;
542 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
543 ld->sq_ring_mask = td->o.iodepth - 1;
545 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
546 memset(ld->cq_ring, 0, aioring_cq_size(td));
547 ld->cq_ring->nr_events = td->o.iodepth * 2;
548 ld->cq_ring_mask = (2 * td->o.iodepth) - 1;
550 td->io_ops_data = ld;
554 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
556 struct aioring_data *ld = td->io_ops_data;
558 ld->io_u_index[io_u->index] = io_u;
562 static struct ioengine_ops ioengine = {
564 .version = FIO_IOOPS_VERSION,
565 .init = fio_aioring_init,
566 .post_init = fio_aioring_post_init,
567 .io_u_init = fio_aioring_io_u_init,
568 .prep = fio_aioring_prep,
569 .queue = fio_aioring_queue,
570 .commit = fio_aioring_commit,
571 .getevents = fio_aioring_getevents,
572 .event = fio_aioring_event,
573 .cleanup = fio_aioring_cleanup,
574 .open_file = generic_open_file,
575 .close_file = generic_close_file,
576 .get_file_size = generic_get_file_size,
578 .option_struct_size = sizeof(struct aioring_options),
581 static void fio_init fio_aioring_register(void)
583 register_ioengine(&ioengine);
586 static void fio_exit fio_aioring_unregister(void)
588 unregister_ioengine(&ioengine);