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"
21 #ifdef ARCH_HAVE_AIORING
23 #ifndef IOCB_FLAG_HIPRI
24 #define IOCB_FLAG_HIPRI (1 << 2)
30 #ifndef IOCTX_FLAG_IOPOLL
31 #define IOCTX_FLAG_IOPOLL (1 << 0)
33 #ifndef IOCTX_FLAG_SCQRING
34 #define IOCTX_FLAG_SCQRING (1 << 1)
36 #ifndef IOCTX_FLAG_FIXEDBUFS
37 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
39 #ifndef IOCTX_FLAG_SQTHREAD
40 #define IOCTX_FLAG_SQTHREAD (1 << 3)
42 #ifndef IOCTX_FLAG_SQWQ
43 #define IOCTX_FLAG_SQWQ (1 << 4)
45 #ifndef IOCTX_FLAG_SQPOLL
46 #define IOCTX_FLAG_SQPOLL (1 << 5)
51 * io_ring_enter(2) flags
53 #ifndef IORING_FLAG_SUBMIT
54 #define IORING_FLAG_SUBMIT (1 << 0)
56 #ifndef IORING_FLAG_GETEVENTS
57 #define IORING_FLAG_GETEVENTS (1 << 1)
64 #define IORING_SQ_NEED_WAKEUP (1 << 0)
66 #define IOEV_RES2_CACHEHIT (1 << 0)
92 struct io_event events[0];
98 struct io_u **io_u_index;
100 struct aio_sq_ring *sq_ring;
103 struct aio_cq_ring *cq_ring;
104 struct io_event *events;
113 struct aioring_options {
116 unsigned int fixedbufs;
117 unsigned int sqthread;
118 unsigned int sqthread_set;
119 unsigned int sqthread_poll;
123 static int fio_aioring_sqthread_cb(void *data,
124 unsigned long long *val)
126 struct aioring_options *o = data;
133 static struct fio_option options[] = {
136 .lname = "High Priority",
137 .type = FIO_OPT_STR_SET,
138 .off1 = offsetof(struct aioring_options, hipri),
139 .help = "Use polled IO completions",
140 .category = FIO_OPT_C_ENGINE,
141 .group = FIO_OPT_G_LIBAIO,
145 .lname = "Fixed (pre-mapped) IO buffers",
146 .type = FIO_OPT_STR_SET,
147 .off1 = offsetof(struct aioring_options, fixedbufs),
148 .help = "Pre map IO buffers",
149 .category = FIO_OPT_C_ENGINE,
150 .group = FIO_OPT_G_LIBAIO,
154 .lname = "Use kernel SQ thread on this CPU",
156 .cb = fio_aioring_sqthread_cb,
157 .help = "Offload submission to kernel thread",
158 .category = FIO_OPT_C_ENGINE,
159 .group = FIO_OPT_G_LIBAIO,
162 .name = "sqthread_poll",
163 .lname = "Kernel SQ thread should poll",
164 .type = FIO_OPT_STR_SET,
165 .off1 = offsetof(struct aioring_options, sqthread_poll),
166 .help = "Used with sqthread, enables kernel side polling",
167 .category = FIO_OPT_C_ENGINE,
168 .group = FIO_OPT_G_LIBAIO,
172 .lname = "Offload submission to kernel workqueue",
173 .type = FIO_OPT_STR_SET,
174 .off1 = offsetof(struct aioring_options, sqwq),
175 .help = "Offload submission to kernel workqueue",
176 .category = FIO_OPT_C_ENGINE,
177 .group = FIO_OPT_G_LIBAIO,
184 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
185 unsigned int min_complete, unsigned int flags)
187 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
191 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
193 struct aioring_data *ld = td->io_ops_data;
194 struct fio_file *f = io_u->file;
195 struct aioring_options *o = td->eo;
198 iocb = &ld->iocbs[io_u->index];
200 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
201 if (io_u->ddir == DDIR_READ)
202 iocb->aio_lio_opcode = IO_CMD_PREAD;
204 iocb->aio_lio_opcode = IO_CMD_PWRITE;
205 iocb->aio_reqprio = 0;
206 iocb->aio_fildes = f->fd;
207 iocb->u.c.buf = io_u->xfer_buf;
208 iocb->u.c.nbytes = io_u->xfer_buflen;
209 iocb->u.c.offset = io_u->offset;
211 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
214 } else if (ddir_sync(io_u->ddir))
215 io_prep_fsync(iocb, f->fd);
221 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
223 struct aioring_data *ld = td->io_ops_data;
228 index = event + ld->cq_ring_off;
229 if (index >= ld->cq_ring->nr_events)
230 index -= ld->cq_ring->nr_events;
232 ev = &ld->cq_ring->events[index];
235 if (ev->res != io_u->xfer_buflen) {
236 if (ev->res > io_u->xfer_buflen)
237 io_u->error = -ev->res;
239 io_u->resid = io_u->xfer_buflen - ev->res;
243 if (io_u->ddir == DDIR_READ) {
244 if (ev->res2 & IOEV_RES2_CACHEHIT)
253 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
256 struct aioring_data *ld = td->io_ops_data;
257 struct aio_cq_ring *ring = ld->cq_ring;
258 u32 head, reaped = 0;
263 if (head == ring->tail)
267 if (head == ring->nr_events)
269 } while (reaped + events < max);
276 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
277 unsigned int max, const struct timespec *t)
279 struct aioring_data *ld = td->io_ops_data;
280 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
281 struct aioring_options *o = td->eo;
282 struct aio_cq_ring *ring = ld->cq_ring;
285 ld->cq_ring_off = ring->head;
287 r = fio_aioring_cqring_reap(td, events, max);
293 if (!o->sqthread_poll) {
294 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
295 IORING_FLAG_GETEVENTS);
299 td_verror(td, errno, "io_ring_enter get");
303 } while (events < min);
305 return r < 0 ? r : events;
308 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
311 struct aioring_data *ld = td->io_ops_data;
312 struct aio_sq_ring *ring = ld->sq_ring;
313 unsigned tail, next_tail;
315 fio_ro_check(td, io_u);
317 if (ld->queued == td->o.iodepth)
320 if (io_u->ddir == DDIR_TRIM) {
324 do_io_u_trim(td, io_u);
325 io_u_mark_submit(td, 1);
326 io_u_mark_complete(td, 1);
327 return FIO_Q_COMPLETED;
331 next_tail = tail + 1;
332 if (next_tail == ring->nr_events)
335 if (next_tail == ring->head)
338 ring->array[tail] = io_u->index;
339 ring->tail = next_tail;
346 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
348 struct aioring_data *ld = td->io_ops_data;
351 if (!fio_fill_issue_time(td))
354 fio_gettime(&now, NULL);
357 int index = ld->sq_ring->array[start];
358 struct io_u *io_u = io_u = ld->io_u_index[index];
360 memcpy(&io_u->issue_time, &now, sizeof(now));
361 io_u_queued(td, io_u);
364 if (start == ld->sq_ring->nr_events)
369 static int fio_aioring_commit(struct thread_data *td)
371 struct aioring_data *ld = td->io_ops_data;
372 struct aioring_options *o = td->eo;
379 if (o->sqthread_poll) {
380 struct aio_sq_ring *ring = ld->sq_ring;
382 if (ring->kflags & IORING_SQ_NEED_WAKEUP)
383 io_ring_enter(ld->aio_ctx, ld->queued, 0, IORING_FLAG_SUBMIT);
389 int start = ld->sq_ring->head;
390 long nr = ld->queued;
392 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
393 IORING_FLAG_GETEVENTS);
395 fio_aioring_queued(td, start, ret);
396 io_u_mark_submit(td, ret);
401 io_u_mark_submit(td, ret);
404 if (errno == EAGAIN) {
405 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
408 /* Shouldn't happen */
412 td_verror(td, errno, "io_ring_enter sumit");
415 } while (ld->queued);
420 static size_t aioring_cq_size(struct thread_data *td)
422 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
425 static size_t aioring_sq_iocb(struct thread_data *td)
427 return sizeof(struct iocb) * td->o.iodepth;
430 static size_t aioring_sq_size(struct thread_data *td)
432 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
435 static void fio_aioring_cleanup(struct thread_data *td)
437 struct aioring_data *ld = td->io_ops_data;
440 td->ts.cachehit += ld->cachehit;
441 td->ts.cachemiss += ld->cachemiss;
443 /* Bump depth to match init depth */
447 * Work-around to avoid huge RCU stalls at exit time. If we
448 * don't do this here, then it'll be torn down by exit_aio().
449 * But for that case we can parallellize the freeing, thus
450 * speeding it up a lot.
452 if (!(td->flags & TD_F_CHILD))
453 io_destroy(ld->aio_ctx);
454 free(ld->io_u_index);
456 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
457 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
458 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
463 static int fio_aioring_queue_init(struct thread_data *td)
465 struct aioring_data *ld = td->io_ops_data;
466 struct aioring_options *o = td->eo;
467 int flags = IOCTX_FLAG_SCQRING;
468 int depth = td->o.iodepth;
471 flags |= IOCTX_FLAG_IOPOLL;
472 if (o->sqthread_set) {
473 ld->sq_ring->sq_thread_cpu = o->sqthread;
474 flags |= IOCTX_FLAG_SQTHREAD;
475 if (o->sqthread_poll)
476 flags |= IOCTX_FLAG_SQPOLL;
479 flags |= IOCTX_FLAG_SQWQ;
482 struct rlimit rlim = {
483 .rlim_cur = RLIM_INFINITY,
484 .rlim_max = RLIM_INFINITY,
487 setrlimit(RLIMIT_MEMLOCK, &rlim);
488 flags |= IOCTX_FLAG_FIXEDBUFS;
491 return syscall(__NR_sys_io_setup2, depth, flags,
492 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
495 static int fio_aioring_post_init(struct thread_data *td)
497 struct aioring_data *ld = td->io_ops_data;
498 struct aioring_options *o = td->eo;
506 for (i = 0; i < td->o.iodepth; i++) {
507 io_u = ld->io_u_index[i];
508 iocb = &ld->iocbs[i];
509 iocb->u.c.buf = io_u->buf;
510 iocb->u.c.nbytes = td_max_bs(td);
513 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
517 err = fio_aioring_queue_init(td);
519 /* Adjust depth back again */
523 td_verror(td, errno, "io_queue_init");
530 static int fio_aioring_init(struct thread_data *td)
532 struct aioring_data *ld;
534 /* ring needs an extra entry, add one to achieve QD set */
537 ld = calloc(1, sizeof(*ld));
540 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
541 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
543 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
544 memset(ld->iocbs, 0, aioring_sq_iocb(td));
546 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
547 memset(ld->sq_ring, 0, aioring_sq_size(td));
548 ld->sq_ring->nr_events = td->o.iodepth;
549 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
551 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
552 memset(ld->cq_ring, 0, aioring_cq_size(td));
553 ld->cq_ring->nr_events = td->o.iodepth * 2;
555 td->io_ops_data = ld;
559 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
561 struct aioring_data *ld = td->io_ops_data;
563 ld->io_u_index[io_u->index] = io_u;
567 static struct ioengine_ops ioengine = {
569 .version = FIO_IOOPS_VERSION,
570 .init = fio_aioring_init,
571 .post_init = fio_aioring_post_init,
572 .io_u_init = fio_aioring_io_u_init,
573 .prep = fio_aioring_prep,
574 .queue = fio_aioring_queue,
575 .commit = fio_aioring_commit,
576 .getevents = fio_aioring_getevents,
577 .event = fio_aioring_event,
578 .cleanup = fio_aioring_cleanup,
579 .open_file = generic_open_file,
580 .close_file = generic_close_file,
581 .get_file_size = generic_get_file_size,
583 .option_struct_size = sizeof(struct aioring_options),
586 static void fio_init fio_aioring_register(void)
588 register_ioengine(&ioengine);
591 static void fio_exit fio_aioring_unregister(void)
593 unregister_ioengine(&ioengine);