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
27 #ifndef IOCTX_FLAG_IOPOLL
28 #define IOCTX_FLAG_IOPOLL (1 << 0)
30 #ifndef IOCTX_FLAG_SCQRING
31 #define IOCTX_FLAG_SCQRING (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)
48 * io_ring_enter(2) flags
50 #ifndef IORING_FLAG_GETEVENTS
51 #define IORING_FLAG_GETEVENTS (1 << 0)
58 #define IORING_SQ_NEED_WAKEUP (1 << 0)
60 #define IOEV_RES2_CACHEHIT (1 << 0)
86 struct io_event events[0];
92 struct io_u **io_u_index;
94 struct aio_sq_ring *sq_ring;
96 unsigned sq_ring_mask;
98 struct aio_cq_ring *cq_ring;
99 struct io_event *events;
100 unsigned cq_ring_mask;
110 struct aioring_options {
113 unsigned int fixedbufs;
114 unsigned int sqthread;
115 unsigned int sqthread_set;
116 unsigned int sqthread_poll;
120 static int fio_aioring_sqthread_cb(void *data,
121 unsigned long long *val)
123 struct aioring_options *o = data;
130 static struct fio_option options[] = {
133 .lname = "High Priority",
134 .type = FIO_OPT_STR_SET,
135 .off1 = offsetof(struct aioring_options, hipri),
136 .help = "Use polled IO completions",
137 .category = FIO_OPT_C_ENGINE,
138 .group = FIO_OPT_G_LIBAIO,
142 .lname = "Fixed (pre-mapped) IO buffers",
143 .type = FIO_OPT_STR_SET,
144 .off1 = offsetof(struct aioring_options, fixedbufs),
145 .help = "Pre map IO buffers",
146 .category = FIO_OPT_C_ENGINE,
147 .group = FIO_OPT_G_LIBAIO,
151 .lname = "Use kernel SQ thread on this CPU",
153 .cb = fio_aioring_sqthread_cb,
154 .help = "Offload submission to kernel thread",
155 .category = FIO_OPT_C_ENGINE,
156 .group = FIO_OPT_G_LIBAIO,
159 .name = "sqthread_poll",
160 .lname = "Kernel SQ thread should poll",
161 .type = FIO_OPT_STR_SET,
162 .off1 = offsetof(struct aioring_options, sqthread_poll),
163 .help = "Used with sqthread, enables kernel side polling",
164 .category = FIO_OPT_C_ENGINE,
165 .group = FIO_OPT_G_LIBAIO,
169 .lname = "Offload submission to kernel workqueue",
170 .type = FIO_OPT_STR_SET,
171 .off1 = offsetof(struct aioring_options, sqwq),
172 .help = "Offload submission to kernel workqueue",
173 .category = FIO_OPT_C_ENGINE,
174 .group = FIO_OPT_G_LIBAIO,
181 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
182 unsigned int min_complete, unsigned int flags)
184 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
188 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
190 struct aioring_data *ld = td->io_ops_data;
191 struct fio_file *f = io_u->file;
194 iocb = &ld->iocbs[io_u->index];
196 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
197 if (io_u->ddir == DDIR_READ)
198 iocb->aio_lio_opcode = IO_CMD_PREAD;
200 iocb->aio_lio_opcode = IO_CMD_PWRITE;
201 iocb->aio_reqprio = 0;
202 iocb->aio_fildes = f->fd;
203 iocb->u.c.buf = io_u->xfer_buf;
204 iocb->u.c.nbytes = io_u->xfer_buflen;
205 iocb->u.c.offset = io_u->offset;
207 } else if (ddir_sync(io_u->ddir))
208 io_prep_fsync(iocb, f->fd);
214 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
216 struct aioring_data *ld = td->io_ops_data;
221 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
223 ev = &ld->cq_ring->events[index];
226 if (ev->res != io_u->xfer_buflen) {
227 if (ev->res > io_u->xfer_buflen)
228 io_u->error = -ev->res;
230 io_u->resid = io_u->xfer_buflen - ev->res;
234 if (io_u->ddir == DDIR_READ) {
235 if (ev->res2 & IOEV_RES2_CACHEHIT)
244 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
247 struct aioring_data *ld = td->io_ops_data;
248 struct aio_cq_ring *ring = ld->cq_ring;
249 u32 head, reaped = 0;
254 if (head == ring->tail)
258 } while (reaped + events < max);
265 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
266 unsigned int max, const struct timespec *t)
268 struct aioring_data *ld = td->io_ops_data;
269 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
270 struct aioring_options *o = td->eo;
271 struct aio_cq_ring *ring = ld->cq_ring;
275 ld->cq_ring_off = ring->head;
277 r = fio_aioring_cqring_reap(td, events, max);
283 if (!o->sqthread_poll) {
284 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
285 IORING_FLAG_GETEVENTS);
289 td_verror(td, errno, "io_ring_enter get");
293 } while (events < min);
295 return r < 0 ? r : events;
298 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
301 struct aioring_data *ld = td->io_ops_data;
302 struct aio_sq_ring *ring = ld->sq_ring;
303 unsigned tail, next_tail;
305 fio_ro_check(td, io_u);
307 if (ld->queued == ld->iodepth)
310 if (io_u->ddir == DDIR_TRIM) {
314 do_io_u_trim(td, io_u);
315 io_u_mark_submit(td, 1);
316 io_u_mark_complete(td, 1);
317 return FIO_Q_COMPLETED;
321 next_tail = tail + 1;
323 if (next_tail == ring->head)
326 ring->array[tail & ld->sq_ring_mask] = io_u->index;
327 ring->tail = next_tail;
334 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
336 struct aioring_data *ld = td->io_ops_data;
339 if (!fio_fill_issue_time(td))
342 fio_gettime(&now, NULL);
345 int index = ld->sq_ring->array[start & ld->sq_ring_mask];
346 struct io_u *io_u = ld->io_u_index[index];
348 memcpy(&io_u->issue_time, &now, sizeof(now));
349 io_u_queued(td, io_u);
355 static int fio_aioring_commit(struct thread_data *td)
357 struct aioring_data *ld = td->io_ops_data;
358 struct aioring_options *o = td->eo;
365 if (o->sqthread_poll) {
366 struct aio_sq_ring *ring = ld->sq_ring;
368 if (ring->kflags & IORING_SQ_NEED_WAKEUP)
369 io_ring_enter(ld->aio_ctx, ld->queued, 0, 0);
375 unsigned start = ld->sq_ring->head;
376 long nr = ld->queued;
378 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_GETEVENTS);
380 fio_aioring_queued(td, start, ret);
381 io_u_mark_submit(td, ret);
386 io_u_mark_submit(td, ret);
389 if (errno == EAGAIN) {
390 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
393 /* Shouldn't happen */
397 td_verror(td, errno, "io_ring_enter sumit");
400 } while (ld->queued);
405 static size_t aioring_cq_size(struct thread_data *td)
407 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
410 static size_t aioring_sq_iocb(struct thread_data *td)
412 return sizeof(struct iocb) * td->o.iodepth;
415 static size_t aioring_sq_size(struct thread_data *td)
417 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
420 static unsigned roundup_pow2(unsigned depth)
422 return 1UL << __fls(depth - 1);
425 static void fio_aioring_cleanup(struct thread_data *td)
427 struct aioring_data *ld = td->io_ops_data;
430 td->ts.cachehit += ld->cachehit;
431 td->ts.cachemiss += ld->cachemiss;
434 * Work-around to avoid huge RCU stalls at exit time. If we
435 * don't do this here, then it'll be torn down by exit_aio().
436 * But for that case we can parallellize the freeing, thus
437 * speeding it up a lot.
439 if (!(td->flags & TD_F_CHILD))
440 io_destroy(ld->aio_ctx);
441 free(ld->io_u_index);
443 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
444 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
445 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
450 static int fio_aioring_queue_init(struct thread_data *td)
452 struct aioring_data *ld = td->io_ops_data;
453 struct aioring_options *o = td->eo;
454 int flags = IOCTX_FLAG_SCQRING;
455 int depth = td->o.iodepth;
458 flags |= IOCTX_FLAG_IOPOLL;
459 if (o->sqthread_set) {
460 ld->sq_ring->sq_thread_cpu = o->sqthread;
461 flags |= IOCTX_FLAG_SQTHREAD;
462 if (o->sqthread_poll)
463 flags |= IOCTX_FLAG_SQPOLL;
466 flags |= IOCTX_FLAG_SQWQ;
469 struct rlimit rlim = {
470 .rlim_cur = RLIM_INFINITY,
471 .rlim_max = RLIM_INFINITY,
474 setrlimit(RLIMIT_MEMLOCK, &rlim);
475 flags |= IOCTX_FLAG_FIXEDBUFS;
478 return syscall(__NR_sys_io_setup2, depth, flags,
479 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
482 static int fio_aioring_post_init(struct thread_data *td)
484 struct aioring_data *ld = td->io_ops_data;
485 struct aioring_options *o = td->eo;
493 for (i = 0; i < td->o.iodepth; i++) {
494 io_u = ld->io_u_index[i];
495 iocb = &ld->iocbs[i];
496 iocb->u.c.buf = io_u->buf;
497 iocb->u.c.nbytes = td_max_bs(td);
501 err = fio_aioring_queue_init(td);
504 td_verror(td, errno, "io_queue_init");
511 static int fio_aioring_init(struct thread_data *td)
513 struct aioring_data *ld;
515 ld = calloc(1, sizeof(*ld));
517 /* ring depth must be a power-of-2 */
518 ld->iodepth = td->o.iodepth;
519 td->o.iodepth = roundup_pow2(td->o.iodepth);
522 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
523 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
525 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
526 memset(ld->iocbs, 0, aioring_sq_iocb(td));
528 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
529 memset(ld->sq_ring, 0, aioring_sq_size(td));
530 ld->sq_ring->nr_events = td->o.iodepth;
531 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
532 ld->sq_ring_mask = td->o.iodepth - 1;
534 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
535 memset(ld->cq_ring, 0, aioring_cq_size(td));
536 ld->cq_ring->nr_events = td->o.iodepth * 2;
537 ld->cq_ring_mask = (2 * td->o.iodepth) - 1;
539 td->io_ops_data = ld;
543 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
545 struct aioring_data *ld = td->io_ops_data;
547 ld->io_u_index[io_u->index] = io_u;
551 static struct ioengine_ops ioengine = {
553 .version = FIO_IOOPS_VERSION,
554 .init = fio_aioring_init,
555 .post_init = fio_aioring_post_init,
556 .io_u_init = fio_aioring_io_u_init,
557 .prep = fio_aioring_prep,
558 .queue = fio_aioring_queue,
559 .commit = fio_aioring_commit,
560 .getevents = fio_aioring_getevents,
561 .event = fio_aioring_event,
562 .cleanup = fio_aioring_cleanup,
563 .open_file = generic_open_file,
564 .close_file = generic_close_file,
565 .get_file_size = generic_get_file_size,
567 .option_struct_size = sizeof(struct aioring_options),
570 static void fio_init fio_aioring_register(void)
572 register_ioengine(&ioengine);
575 static void fio_exit fio_aioring_unregister(void)
577 unregister_ioengine(&ioengine);