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)
90 struct io_event events[0];
96 struct io_u **io_u_index;
98 struct aio_sq_ring *sq_ring;
101 struct aio_cq_ring *cq_ring;
102 struct io_event *events;
108 struct aioring_options {
111 unsigned int fixedbufs;
112 unsigned int sqthread;
113 unsigned int sqthread_set;
114 unsigned int sqthread_poll;
118 static int fio_aioring_sqthread_cb(void *data,
119 unsigned long long *val)
121 struct aioring_options *o = data;
128 static struct fio_option options[] = {
131 .lname = "High Priority",
132 .type = FIO_OPT_STR_SET,
133 .off1 = offsetof(struct aioring_options, hipri),
134 .help = "Use polled IO completions",
135 .category = FIO_OPT_C_ENGINE,
136 .group = FIO_OPT_G_LIBAIO,
140 .lname = "Fixed (pre-mapped) IO buffers",
141 .type = FIO_OPT_STR_SET,
142 .off1 = offsetof(struct aioring_options, fixedbufs),
143 .help = "Pre map IO buffers",
144 .category = FIO_OPT_C_ENGINE,
145 .group = FIO_OPT_G_LIBAIO,
149 .lname = "Use kernel SQ thread on this CPU",
151 .cb = fio_aioring_sqthread_cb,
152 .help = "Offload submission to kernel thread",
153 .category = FIO_OPT_C_ENGINE,
154 .group = FIO_OPT_G_LIBAIO,
157 .name = "sqthread_poll",
158 .lname = "Kernel SQ thread should poll",
159 .type = FIO_OPT_STR_SET,
160 .off1 = offsetof(struct aioring_options, sqthread_poll),
161 .help = "Used with sqthread, enables kernel side polling",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_LIBAIO,
167 .lname = "Offload submission to kernel workqueue",
168 .type = FIO_OPT_STR_SET,
169 .off1 = offsetof(struct aioring_options, sqwq),
170 .help = "Offload submission to kernel workqueue",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_LIBAIO,
179 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
180 unsigned int min_complete, unsigned int flags)
182 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
186 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
188 struct aioring_data *ld = td->io_ops_data;
189 struct fio_file *f = io_u->file;
190 struct aioring_options *o = td->eo;
193 iocb = &ld->iocbs[io_u->index];
195 if (io_u->ddir == DDIR_READ) {
197 iocb->aio_fildes = f->fd;
198 iocb->aio_lio_opcode = IO_CMD_PREAD;
199 iocb->u.c.offset = io_u->offset;
201 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
203 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
205 } else if (io_u->ddir == DDIR_WRITE) {
207 iocb->aio_fildes = f->fd;
208 iocb->aio_lio_opcode = IO_CMD_PWRITE;
209 iocb->u.c.offset = io_u->offset;
211 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
213 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;
230 if (index >= ld->cq_ring->nr_events)
231 index -= ld->cq_ring->nr_events;
233 ev = &ld->cq_ring->events[index];
236 if (ev->res != io_u->xfer_buflen) {
237 if (ev->res > io_u->xfer_buflen)
238 io_u->error = -ev->res;
240 io_u->resid = io_u->xfer_buflen - ev->res;
247 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
250 struct aioring_data *ld = td->io_ops_data;
251 struct aio_cq_ring *ring = ld->cq_ring;
252 u32 head, reaped = 0;
257 if (head == ring->tail)
261 if (head == ring->nr_events)
263 } while (reaped + events < max);
270 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
271 unsigned int max, const struct timespec *t)
273 struct aioring_data *ld = td->io_ops_data;
274 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
275 struct aioring_options *o = td->eo;
276 struct aio_cq_ring *ring = ld->cq_ring;
279 ld->cq_ring_off = ring->head;
281 r = fio_aioring_cqring_reap(td, events, max);
287 if (!o->sqthread_poll) {
288 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
289 IORING_FLAG_GETEVENTS);
293 td_verror(td, errno, "io_ring_enter get");
297 } while (events < min);
299 return r < 0 ? r : events;
302 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
305 struct aioring_data *ld = td->io_ops_data;
306 struct aio_sq_ring *ring = ld->sq_ring;
307 unsigned tail, next_tail;
309 fio_ro_check(td, io_u);
311 if (ld->queued == td->o.iodepth)
314 if (io_u->ddir == DDIR_TRIM) {
318 do_io_u_trim(td, io_u);
319 io_u_mark_submit(td, 1);
320 io_u_mark_complete(td, 1);
321 return FIO_Q_COMPLETED;
325 next_tail = tail + 1;
326 if (next_tail == ring->nr_events)
329 if (next_tail == ring->head)
332 ring->array[tail] = io_u->index;
333 ring->tail = next_tail;
340 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
342 struct aioring_data *ld = td->io_ops_data;
345 if (!fio_fill_issue_time(td))
348 fio_gettime(&now, NULL);
351 int index = ld->sq_ring->array[start];
352 struct io_u *io_u = io_u = ld->io_u_index[index];
354 memcpy(&io_u->issue_time, &now, sizeof(now));
355 io_u_queued(td, io_u);
358 if (start == ld->sq_ring->nr_events)
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, IORING_FLAG_SUBMIT);
383 int start = ld->sq_ring->head;
384 long nr = ld->queued;
386 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
387 IORING_FLAG_GETEVENTS);
389 fio_aioring_queued(td, start, ret);
390 io_u_mark_submit(td, ret);
395 io_u_mark_submit(td, ret);
398 if (errno == EAGAIN) {
399 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
402 /* Shouldn't happen */
406 td_verror(td, errno, "io_ring_enter sumit");
409 } while (ld->queued);
414 static size_t aioring_cq_size(struct thread_data *td)
416 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
419 static size_t aioring_sq_iocb(struct thread_data *td)
421 return sizeof(struct iocb) * td->o.iodepth;
424 static size_t aioring_sq_size(struct thread_data *td)
426 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
429 static void fio_aioring_cleanup(struct thread_data *td)
431 struct aioring_data *ld = td->io_ops_data;
434 /* Bump depth to match init depth */
438 * Work-around to avoid huge RCU stalls at exit time. If we
439 * don't do this here, then it'll be torn down by exit_aio().
440 * But for that case we can parallellize the freeing, thus
441 * speeding it up a lot.
443 if (!(td->flags & TD_F_CHILD))
444 io_destroy(ld->aio_ctx);
445 free(ld->io_u_index);
447 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
448 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
449 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
454 static int fio_aioring_queue_init(struct thread_data *td)
456 struct aioring_data *ld = td->io_ops_data;
457 struct aioring_options *o = td->eo;
458 int flags = IOCTX_FLAG_SCQRING;
459 int depth = td->o.iodepth;
462 flags |= IOCTX_FLAG_IOPOLL;
463 if (o->sqthread_set) {
464 ld->sq_ring->sq_thread_cpu = o->sqthread;
465 flags |= IOCTX_FLAG_SQTHREAD;
466 if (o->sqthread_poll)
467 flags |= IOCTX_FLAG_SQPOLL;
469 flags |= IOCTX_FLAG_SQWQ;
472 struct rlimit rlim = {
473 .rlim_cur = RLIM_INFINITY,
474 .rlim_max = RLIM_INFINITY,
477 setrlimit(RLIMIT_MEMLOCK, &rlim);
478 flags |= IOCTX_FLAG_FIXEDBUFS;
481 return syscall(__NR_sys_io_setup2, depth, flags,
482 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
485 static int fio_aioring_post_init(struct thread_data *td)
487 struct aioring_data *ld = td->io_ops_data;
488 struct aioring_options *o = td->eo;
496 for (i = 0; i < td->o.iodepth; i++) {
497 io_u = ld->io_u_index[i];
498 iocb = &ld->iocbs[i];
499 iocb->u.c.buf = io_u->buf;
500 iocb->u.c.nbytes = td_max_bs(td);
503 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
507 err = fio_aioring_queue_init(td);
509 td_verror(td, -err, "io_queue_init");
513 /* Adjust depth back again */
518 static int fio_aioring_init(struct thread_data *td)
520 struct aioring_options *o = td->eo;
521 struct aioring_data *ld;
523 if (o->sqthread_set && o->sqwq) {
524 log_err("fio: aioring sqthread and sqwq are mutually exclusive\n");
528 /* ring needs an extra entry, add one to achieve QD set */
531 ld = calloc(1, sizeof(*ld));
534 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
535 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
537 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
538 memset(ld->iocbs, 0, aioring_sq_iocb(td));
540 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
541 memset(ld->sq_ring, 0, aioring_sq_size(td));
542 ld->sq_ring->nr_events = td->o.iodepth;
543 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
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;
549 td->io_ops_data = ld;
553 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
555 struct aioring_data *ld = td->io_ops_data;
557 ld->io_u_index[io_u->index] = io_u;
561 static struct ioengine_ops ioengine = {
563 .version = FIO_IOOPS_VERSION,
564 .init = fio_aioring_init,
565 .post_init = fio_aioring_post_init,
566 .io_u_init = fio_aioring_io_u_init,
567 .prep = fio_aioring_prep,
568 .queue = fio_aioring_queue,
569 .commit = fio_aioring_commit,
570 .getevents = fio_aioring_getevents,
571 .event = fio_aioring_event,
572 .cleanup = fio_aioring_cleanup,
573 .open_file = generic_open_file,
574 .close_file = generic_close_file,
575 .get_file_size = generic_get_file_size,
577 .option_struct_size = sizeof(struct aioring_options),
580 static void fio_init fio_aioring_register(void)
582 register_ioengine(&ioengine);
585 static void fio_exit fio_aioring_unregister(void)
587 unregister_ioengine(&ioengine);