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) {
202 iocb->aio_fildes = f->fd;
203 iocb->aio_lio_opcode = IO_CMD_PREAD;
204 iocb->u.c.offset = io_u->offset;
206 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
208 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
210 } else if (io_u->ddir == DDIR_WRITE) {
212 iocb->aio_fildes = f->fd;
213 iocb->aio_lio_opcode = IO_CMD_PWRITE;
214 iocb->u.c.offset = io_u->offset;
216 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
218 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
220 } else if (ddir_sync(io_u->ddir))
221 io_prep_fsync(iocb, f->fd);
227 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
229 struct aioring_data *ld = td->io_ops_data;
234 index = event + ld->cq_ring_off;
235 if (index >= ld->cq_ring->nr_events)
236 index -= ld->cq_ring->nr_events;
238 ev = &ld->cq_ring->events[index];
241 if (ev->res != io_u->xfer_buflen) {
242 if (ev->res > io_u->xfer_buflen)
243 io_u->error = -ev->res;
245 io_u->resid = io_u->xfer_buflen - ev->res;
249 if (io_u->ddir == DDIR_READ) {
250 if (ev->res2 & IOEV_RES2_CACHEHIT)
259 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
262 struct aioring_data *ld = td->io_ops_data;
263 struct aio_cq_ring *ring = ld->cq_ring;
264 u32 head, reaped = 0;
269 if (head == ring->tail)
273 if (head == ring->nr_events)
275 } while (reaped + events < max);
282 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
283 unsigned int max, const struct timespec *t)
285 struct aioring_data *ld = td->io_ops_data;
286 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
287 struct aioring_options *o = td->eo;
288 struct aio_cq_ring *ring = ld->cq_ring;
291 ld->cq_ring_off = ring->head;
293 r = fio_aioring_cqring_reap(td, events, max);
299 if (!o->sqthread_poll) {
300 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
301 IORING_FLAG_GETEVENTS);
305 td_verror(td, errno, "io_ring_enter get");
309 } while (events < min);
311 return r < 0 ? r : events;
314 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
317 struct aioring_data *ld = td->io_ops_data;
318 struct aio_sq_ring *ring = ld->sq_ring;
319 unsigned tail, next_tail;
321 fio_ro_check(td, io_u);
323 if (ld->queued == td->o.iodepth)
326 if (io_u->ddir == DDIR_TRIM) {
330 do_io_u_trim(td, io_u);
331 io_u_mark_submit(td, 1);
332 io_u_mark_complete(td, 1);
333 return FIO_Q_COMPLETED;
337 next_tail = tail + 1;
338 if (next_tail == ring->nr_events)
341 if (next_tail == ring->head)
344 ring->array[tail] = io_u->index;
345 ring->tail = next_tail;
352 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
354 struct aioring_data *ld = td->io_ops_data;
357 if (!fio_fill_issue_time(td))
360 fio_gettime(&now, NULL);
363 int index = ld->sq_ring->array[start];
364 struct io_u *io_u = io_u = ld->io_u_index[index];
366 memcpy(&io_u->issue_time, &now, sizeof(now));
367 io_u_queued(td, io_u);
370 if (start == ld->sq_ring->nr_events)
375 static int fio_aioring_commit(struct thread_data *td)
377 struct aioring_data *ld = td->io_ops_data;
378 struct aioring_options *o = td->eo;
385 if (o->sqthread_poll) {
386 struct aio_sq_ring *ring = ld->sq_ring;
388 if (ring->kflags & IORING_SQ_NEED_WAKEUP)
389 io_ring_enter(ld->aio_ctx, ld->queued, 0, IORING_FLAG_SUBMIT);
395 int start = ld->sq_ring->head;
396 long nr = ld->queued;
398 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
399 IORING_FLAG_GETEVENTS);
401 fio_aioring_queued(td, start, ret);
402 io_u_mark_submit(td, ret);
407 io_u_mark_submit(td, ret);
410 if (errno == EAGAIN) {
411 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
414 /* Shouldn't happen */
418 td_verror(td, errno, "io_ring_enter sumit");
421 } while (ld->queued);
426 static size_t aioring_cq_size(struct thread_data *td)
428 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
431 static size_t aioring_sq_iocb(struct thread_data *td)
433 return sizeof(struct iocb) * td->o.iodepth;
436 static size_t aioring_sq_size(struct thread_data *td)
438 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
441 static void fio_aioring_cleanup(struct thread_data *td)
443 struct aioring_data *ld = td->io_ops_data;
446 td->ts.cachehit += ld->cachehit;
447 td->ts.cachemiss += ld->cachemiss;
449 /* Bump depth to match init depth */
453 * Work-around to avoid huge RCU stalls at exit time. If we
454 * don't do this here, then it'll be torn down by exit_aio().
455 * But for that case we can parallellize the freeing, thus
456 * speeding it up a lot.
458 if (!(td->flags & TD_F_CHILD))
459 io_destroy(ld->aio_ctx);
460 free(ld->io_u_index);
462 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
463 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
464 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
469 static int fio_aioring_queue_init(struct thread_data *td)
471 struct aioring_data *ld = td->io_ops_data;
472 struct aioring_options *o = td->eo;
473 int flags = IOCTX_FLAG_SCQRING;
474 int depth = td->o.iodepth;
477 flags |= IOCTX_FLAG_IOPOLL;
478 if (o->sqthread_set) {
479 ld->sq_ring->sq_thread_cpu = o->sqthread;
480 flags |= IOCTX_FLAG_SQTHREAD;
481 if (o->sqthread_poll)
482 flags |= IOCTX_FLAG_SQPOLL;
484 flags |= IOCTX_FLAG_SQWQ;
487 struct rlimit rlim = {
488 .rlim_cur = RLIM_INFINITY,
489 .rlim_max = RLIM_INFINITY,
492 setrlimit(RLIMIT_MEMLOCK, &rlim);
493 flags |= IOCTX_FLAG_FIXEDBUFS;
496 return syscall(__NR_sys_io_setup2, depth, flags,
497 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
500 static int fio_aioring_post_init(struct thread_data *td)
502 struct aioring_data *ld = td->io_ops_data;
503 struct aioring_options *o = td->eo;
511 for (i = 0; i < td->o.iodepth; i++) {
512 io_u = ld->io_u_index[i];
513 iocb = &ld->iocbs[i];
514 iocb->u.c.buf = io_u->buf;
515 iocb->u.c.nbytes = td_max_bs(td);
518 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
522 err = fio_aioring_queue_init(td);
524 td_verror(td, -err, "io_queue_init");
528 /* Adjust depth back again */
533 static int fio_aioring_init(struct thread_data *td)
535 struct aioring_options *o = td->eo;
536 struct aioring_data *ld;
538 if (o->sqthread_set && o->sqwq) {
539 log_err("fio: aioring sqthread and sqwq are mutually exclusive\n");
543 /* ring needs an extra entry, add one to achieve QD set */
546 ld = calloc(1, sizeof(*ld));
549 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
550 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
552 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
553 memset(ld->iocbs, 0, aioring_sq_iocb(td));
555 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
556 memset(ld->sq_ring, 0, aioring_sq_size(td));
557 ld->sq_ring->nr_events = td->o.iodepth;
558 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
560 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
561 memset(ld->cq_ring, 0, aioring_cq_size(td));
562 ld->cq_ring->nr_events = td->o.iodepth * 2;
564 td->io_ops_data = ld;
568 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
570 struct aioring_data *ld = td->io_ops_data;
572 ld->io_u_index[io_u->index] = io_u;
576 static struct ioengine_ops ioengine = {
578 .version = FIO_IOOPS_VERSION,
579 .init = fio_aioring_init,
580 .post_init = fio_aioring_post_init,
581 .io_u_init = fio_aioring_io_u_init,
582 .prep = fio_aioring_prep,
583 .queue = fio_aioring_queue,
584 .commit = fio_aioring_commit,
585 .getevents = fio_aioring_getevents,
586 .event = fio_aioring_event,
587 .cleanup = fio_aioring_cleanup,
588 .open_file = generic_open_file,
589 .close_file = generic_close_file,
590 .get_file_size = generic_get_file_size,
592 .option_struct_size = sizeof(struct aioring_options),
595 static void fio_init fio_aioring_register(void)
597 register_ioengine(&ioengine);
600 static void fio_exit fio_aioring_unregister(void)
602 unregister_ioengine(&ioengine);