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)
87 struct io_event events[0];
93 struct io_u **io_u_index;
95 struct aio_sq_ring *sq_ring;
98 struct aio_cq_ring *cq_ring;
99 struct io_event *events;
105 struct aioring_options {
108 unsigned int fixedbufs;
109 unsigned int sqthread;
110 unsigned int sqthread_set;
111 unsigned int sqthread_poll;
115 static int fio_aioring_sqthread_cb(void *data,
116 unsigned long long *val)
118 struct aioring_options *o = data;
125 static struct fio_option options[] = {
128 .lname = "High Priority",
129 .type = FIO_OPT_STR_SET,
130 .off1 = offsetof(struct aioring_options, hipri),
131 .help = "Use polled IO completions",
132 .category = FIO_OPT_C_ENGINE,
133 .group = FIO_OPT_G_LIBAIO,
137 .lname = "Fixed (pre-mapped) IO buffers",
138 .type = FIO_OPT_STR_SET,
139 .off1 = offsetof(struct aioring_options, fixedbufs),
140 .help = "Pre map IO buffers",
141 .category = FIO_OPT_C_ENGINE,
142 .group = FIO_OPT_G_LIBAIO,
146 .lname = "Use kernel SQ thread on this CPU",
148 .cb = fio_aioring_sqthread_cb,
149 .help = "Offload submission to kernel thread",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_LIBAIO,
154 .name = "sqthread_poll",
155 .lname = "Kernel SQ thread should poll",
156 .type = FIO_OPT_STR_SET,
157 .off1 = offsetof(struct aioring_options, sqthread_poll),
158 .help = "Used with sqthread, enables kernel side polling",
159 .category = FIO_OPT_C_ENGINE,
160 .group = FIO_OPT_G_LIBAIO,
164 .lname = "Offload submission to kernel workqueue",
165 .type = FIO_OPT_STR_SET,
166 .off1 = offsetof(struct aioring_options, sqwq),
167 .help = "Offload submission to kernel workqueue",
168 .category = FIO_OPT_C_ENGINE,
169 .group = FIO_OPT_G_LIBAIO,
176 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
177 unsigned int min_complete, unsigned int flags)
179 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
183 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
185 struct aioring_data *ld = td->io_ops_data;
186 struct fio_file *f = io_u->file;
187 struct aioring_options *o = td->eo;
190 iocb = &ld->iocbs[io_u->index];
192 if (io_u->ddir == DDIR_READ) {
194 iocb->aio_fildes = f->fd;
195 iocb->aio_lio_opcode = IO_CMD_PREAD;
196 iocb->u.c.offset = io_u->offset;
198 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
200 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
202 } else if (io_u->ddir == DDIR_WRITE) {
204 iocb->aio_fildes = f->fd;
205 iocb->aio_lio_opcode = IO_CMD_PWRITE;
206 iocb->u.c.offset = io_u->offset;
208 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
210 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
212 } else if (ddir_sync(io_u->ddir))
213 io_prep_fsync(iocb, f->fd);
219 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
221 struct aioring_data *ld = td->io_ops_data;
226 index = event + ld->cq_ring_off;
227 if (index >= ld->cq_ring->nr_events)
228 index -= ld->cq_ring->nr_events;
230 ev = &ld->cq_ring->events[index];
233 if (ev->res != io_u->xfer_buflen) {
234 if (ev->res > io_u->xfer_buflen)
235 io_u->error = -ev->res;
237 io_u->resid = io_u->xfer_buflen - ev->res;
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 if (head == ring->nr_events)
260 } while (reaped + events < max);
267 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
268 unsigned int max, const struct timespec *t)
270 struct aioring_data *ld = td->io_ops_data;
271 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
272 struct aioring_options *o = td->eo;
273 struct aio_cq_ring *ring = ld->cq_ring;
276 ld->cq_ring_off = ring->head;
278 r = fio_aioring_cqring_reap(td, events, max);
284 if (!o->sqthread_poll) {
285 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
286 IORING_FLAG_GETEVENTS);
290 td_verror(td, errno, "io_ring_enter get");
294 } while (events < min);
296 return r < 0 ? r : events;
299 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
302 struct aioring_data *ld = td->io_ops_data;
303 struct aio_sq_ring *ring = ld->sq_ring;
304 unsigned tail, next_tail;
306 fio_ro_check(td, io_u);
308 if (ld->queued == td->o.iodepth)
311 if (io_u->ddir == DDIR_TRIM) {
315 do_io_u_trim(td, io_u);
316 io_u_mark_submit(td, 1);
317 io_u_mark_complete(td, 1);
318 return FIO_Q_COMPLETED;
322 next_tail = tail + 1;
323 if (next_tail == ring->nr_events)
326 if (next_tail == ring->head)
329 ring->array[tail] = io_u->index;
330 ring->tail = next_tail;
337 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
339 struct aioring_data *ld = td->io_ops_data;
342 if (!fio_fill_issue_time(td))
345 fio_gettime(&now, NULL);
348 int index = ld->sq_ring->array[start];
349 struct io_u *io_u = io_u = ld->io_u_index[index];
351 memcpy(&io_u->issue_time, &now, sizeof(now));
352 io_u_queued(td, io_u);
355 if (start == ld->sq_ring->nr_events)
360 static int fio_aioring_commit(struct thread_data *td)
362 struct aioring_data *ld = td->io_ops_data;
363 struct aioring_options *o = td->eo;
370 if (o->sqthread_poll) {
376 int start = ld->sq_ring->head;
377 long nr = ld->queued;
379 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
380 IORING_FLAG_GETEVENTS);
382 fio_aioring_queued(td, start, ret);
383 io_u_mark_submit(td, ret);
388 io_u_mark_submit(td, ret);
391 if (errno == EAGAIN) {
392 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
395 /* Shouldn't happen */
399 td_verror(td, errno, "io_ring_enter sumit");
402 } while (ld->queued);
407 static size_t aioring_cq_size(struct thread_data *td)
409 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
412 static size_t aioring_sq_iocb(struct thread_data *td)
414 return sizeof(struct iocb) * td->o.iodepth;
417 static size_t aioring_sq_size(struct thread_data *td)
419 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
422 static void fio_aioring_cleanup(struct thread_data *td)
424 struct aioring_data *ld = td->io_ops_data;
427 /* Bump depth to match init depth */
431 * Work-around to avoid huge RCU stalls at exit time. If we
432 * don't do this here, then it'll be torn down by exit_aio().
433 * But for that case we can parallellize the freeing, thus
434 * speeding it up a lot.
436 if (!(td->flags & TD_F_CHILD))
437 io_destroy(ld->aio_ctx);
438 free(ld->io_u_index);
440 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
441 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
442 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
447 static int fio_aioring_queue_init(struct thread_data *td)
449 struct aioring_data *ld = td->io_ops_data;
450 struct aioring_options *o = td->eo;
451 int flags = IOCTX_FLAG_SCQRING;
452 int depth = td->o.iodepth;
455 flags |= IOCTX_FLAG_IOPOLL;
456 if (o->sqthread_set) {
457 ld->sq_ring->sq_thread_cpu = o->sqthread;
458 flags |= IOCTX_FLAG_SQTHREAD;
459 if (o->sqthread_poll)
460 flags |= IOCTX_FLAG_SQPOLL;
462 flags |= IOCTX_FLAG_SQWQ;
465 struct rlimit rlim = {
466 .rlim_cur = RLIM_INFINITY,
467 .rlim_max = RLIM_INFINITY,
470 setrlimit(RLIMIT_MEMLOCK, &rlim);
471 flags |= IOCTX_FLAG_FIXEDBUFS;
474 return syscall(__NR_sys_io_setup2, depth, flags,
475 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
478 static int fio_aioring_post_init(struct thread_data *td)
480 struct aioring_data *ld = td->io_ops_data;
481 struct aioring_options *o = td->eo;
489 for (i = 0; i < td->o.iodepth; i++) {
490 io_u = ld->io_u_index[i];
491 iocb = &ld->iocbs[i];
492 iocb->u.c.buf = io_u->buf;
493 iocb->u.c.nbytes = td_max_bs(td);
496 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
500 err = fio_aioring_queue_init(td);
502 td_verror(td, -err, "io_queue_init");
506 /* Adjust depth back again */
511 static int fio_aioring_init(struct thread_data *td)
513 struct aioring_options *o = td->eo;
514 struct aioring_data *ld;
516 if (o->sqthread_set && o->sqwq) {
517 log_err("fio: aioring sqthread and sqwq are mutually exclusive\n");
521 /* ring needs an extra entry, add one to achieve QD set */
524 ld = calloc(1, sizeof(*ld));
527 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
528 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
530 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
531 memset(ld->iocbs, 0, aioring_sq_iocb(td));
533 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
534 memset(ld->sq_ring, 0, aioring_sq_size(td));
535 ld->sq_ring->nr_events = td->o.iodepth;
536 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
538 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
539 memset(ld->cq_ring, 0, aioring_cq_size(td));
540 ld->cq_ring->nr_events = td->o.iodepth * 2;
542 td->io_ops_data = ld;
546 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
548 struct aioring_data *ld = td->io_ops_data;
550 ld->io_u_index[io_u->index] = io_u;
554 static struct ioengine_ops ioengine = {
556 .version = FIO_IOOPS_VERSION,
557 .init = fio_aioring_init,
558 .post_init = fio_aioring_post_init,
559 .io_u_init = fio_aioring_io_u_init,
560 .prep = fio_aioring_prep,
561 .queue = fio_aioring_queue,
562 .commit = fio_aioring_commit,
563 .getevents = fio_aioring_getevents,
564 .event = fio_aioring_event,
565 .cleanup = fio_aioring_cleanup,
566 .open_file = generic_open_file,
567 .close_file = generic_close_file,
568 .get_file_size = generic_get_file_size,
570 .option_struct_size = sizeof(struct aioring_options),
573 static void fio_init fio_aioring_register(void)
575 register_ioengine(&ioengine);
578 static void fio_exit fio_aioring_unregister(void)
580 unregister_ioengine(&ioengine);