4 * IO engine using the new native Linux libaio ring interface
12 #include <sys/resource.h>
15 #include "../lib/pow2.h"
16 #include "../optgroup.h"
17 #include "../lib/memalign.h"
19 #ifndef IOCB_FLAG_HIPRI
20 #define IOCB_FLAG_HIPRI (1 << 2)
26 #ifndef IOCTX_FLAG_IOPOLL
27 #define IOCTX_FLAG_IOPOLL (1 << 0)
29 #ifndef IOCTX_FLAG_SCQRING
30 #define IOCTX_FLAG_SCQRING (1 << 1)
32 #ifndef IOCTX_FLAG_FIXEDBUFS
33 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
35 #ifndef IOCTX_FLAG_SQTHREAD
36 #define IOCTX_FLAG_SQTHREAD (1 << 3)
38 #ifndef IOCTX_FLAG_SQWQ
39 #define IOCTX_FLAG_SQWQ (1 << 4)
43 * io_ring_enter(2) flags
45 #ifndef IORING_FLAG_SUBMIT
46 #define IORING_FLAG_SUBMIT (1 << 0)
48 #ifndef IORING_FLAG_GETEVENTS
49 #define IORING_FLAG_GETEVENTS (1 << 1)
79 struct io_event events[0];
85 struct io_u **io_u_index;
87 struct aio_sq_ring *sq_ring;
90 struct aio_cq_ring *cq_ring;
91 struct io_event *events;
97 struct aioring_options {
100 unsigned int fixedbufs;
103 static struct fio_option options[] = {
106 .lname = "High Priority",
107 .type = FIO_OPT_STR_SET,
108 .off1 = offsetof(struct aioring_options, hipri),
109 .help = "Use polled IO completions",
110 .category = FIO_OPT_C_ENGINE,
111 .group = FIO_OPT_G_LIBAIO,
115 .lname = "Fixed (pre-mapped) IO buffers",
116 .type = FIO_OPT_STR_SET,
117 .off1 = offsetof(struct aioring_options, fixedbufs),
118 .help = "Pre map IO buffers",
119 .category = FIO_OPT_C_ENGINE,
120 .group = FIO_OPT_G_LIBAIO,
127 static int fio_aioring_commit(struct thread_data *td);
129 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
130 unsigned int min_complete, unsigned int flags)
132 #ifdef __NR_sys_io_ring_enter
133 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
140 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
142 struct aioring_data *ld = td->io_ops_data;
143 struct fio_file *f = io_u->file;
144 struct aioring_options *o = td->eo;
147 iocb = &ld->iocbs[io_u->index];
149 if (io_u->ddir == DDIR_READ) {
151 iocb->aio_fildes = f->fd;
152 iocb->aio_lio_opcode = IO_CMD_PREAD;
153 iocb->u.c.offset = io_u->offset;
155 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
157 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
159 } else if (io_u->ddir == DDIR_WRITE) {
161 iocb->aio_fildes = f->fd;
162 iocb->aio_lio_opcode = IO_CMD_PWRITE;
163 iocb->u.c.offset = io_u->offset;
165 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
167 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
169 } else if (ddir_sync(io_u->ddir))
170 io_prep_fsync(iocb, f->fd);
176 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
178 struct aioring_data *ld = td->io_ops_data;
183 index = event + ld->cq_ring_off;
184 if (index >= ld->cq_ring->nr_events)
185 index -= ld->cq_ring->nr_events;
187 ev = &ld->cq_ring->events[index];
190 if (ev->res != io_u->xfer_buflen) {
191 if (ev->res > io_u->xfer_buflen)
192 io_u->error = -ev->res;
194 io_u->resid = io_u->xfer_buflen - ev->res;
201 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
204 struct aioring_data *ld = td->io_ops_data;
205 struct aio_cq_ring *ring = ld->cq_ring;
206 u32 head, reaped = 0;
211 if (head == ring->tail)
215 if (head == ring->nr_events)
217 } while (reaped + events < max);
224 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
225 unsigned int max, const struct timespec *t)
227 struct aioring_data *ld = td->io_ops_data;
228 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
229 struct aio_cq_ring *ring = ld->cq_ring;
232 ld->cq_ring_off = ring->head;
234 r = fio_aioring_cqring_reap(td, events, max);
240 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
241 IORING_FLAG_GETEVENTS);
245 perror("ring enter");
248 } while (events < min);
250 return r < 0 ? r : events;
253 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
256 struct aioring_data *ld = td->io_ops_data;
257 struct aio_sq_ring *ring = ld->sq_ring;
258 unsigned tail, next_tail;
260 fio_ro_check(td, io_u);
262 if (ld->queued == td->o.iodepth)
266 * fsync is tricky, since it can fail and we need to do it
267 * serialized with other io. the reason is that linux doesn't
268 * support aio fsync yet. So return busy for the case where we
269 * have pending io, to let fio complete those first.
271 if (ddir_sync(io_u->ddir)) {
275 do_io_u_sync(td, io_u);
276 return FIO_Q_COMPLETED;
279 if (io_u->ddir == DDIR_TRIM) {
283 do_io_u_trim(td, io_u);
284 io_u_mark_submit(td, 1);
285 io_u_mark_complete(td, 1);
286 return FIO_Q_COMPLETED;
290 next_tail = tail + 1;
291 if (next_tail == ring->nr_events)
294 if (next_tail == ring->head)
297 ring->array[tail] = io_u->index;
298 ring->tail = next_tail;
305 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
307 struct aioring_data *ld = td->io_ops_data;
310 if (!fio_fill_issue_time(td))
313 fio_gettime(&now, NULL);
316 int index = ld->sq_ring->array[start];
317 struct io_u *io_u = io_u = ld->io_u_index[index];
319 memcpy(&io_u->issue_time, &now, sizeof(now));
320 io_u_queued(td, io_u);
323 if (start == ld->sq_ring->nr_events)
328 static int fio_aioring_commit(struct thread_data *td)
330 struct aioring_data *ld = td->io_ops_data;
337 int start = ld->sq_ring->head;
338 long nr = ld->queued;
340 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
341 IORING_FLAG_GETEVENTS);
343 perror("io_ring_enter");
345 fio_aioring_queued(td, start, ret);
346 io_u_mark_submit(td, ret);
350 } else if (ret == -EINTR || !ret) {
352 io_u_mark_submit(td, ret);
354 } else if (ret == -EAGAIN) {
356 * If we get EAGAIN, we should break out without
357 * error and let the upper layer reap some
358 * events for us. If we have no queued IO, we
359 * must loop here. If we loop for more than 30s,
360 * just error out, something must be buggy in the
369 } else if (ret == -ENOMEM) {
371 * If we get -ENOMEM, reap events if we can. If
372 * we cannot, treat it as a fatal event since there's
373 * nothing we can do about it.
380 } while (ld->queued);
385 static size_t aioring_cq_size(struct thread_data *td)
387 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
390 static size_t aioring_sq_iocb(struct thread_data *td)
392 return sizeof(struct iocb) * td->o.iodepth;
395 static size_t aioring_sq_size(struct thread_data *td)
397 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
400 static void fio_aioring_cleanup(struct thread_data *td)
402 struct aioring_data *ld = td->io_ops_data;
406 * Work-around to avoid huge RCU stalls at exit time. If we
407 * don't do this here, then it'll be torn down by exit_aio().
408 * But for that case we can parallellize the freeing, thus
409 * speeding it up a lot.
411 if (!(td->flags & TD_F_CHILD))
412 io_destroy(ld->aio_ctx);
413 free(ld->io_u_index);
415 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
416 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
417 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
422 static int fio_aioring_queue_init(struct thread_data *td)
424 #ifdef __NR_sys_io_setup2
425 struct aioring_data *ld = td->io_ops_data;
426 struct aioring_options *o = td->eo;
427 int flags = IOCTX_FLAG_SCQRING;
428 int depth = td->o.iodepth;
431 flags |= IOCTX_FLAG_IOPOLL;
433 struct rlimit rlim = {
434 .rlim_cur = RLIM_INFINITY,
435 .rlim_max = RLIM_INFINITY,
438 setrlimit(RLIMIT_MEMLOCK, &rlim);
439 flags |= IOCTX_FLAG_FIXEDBUFS;
442 return syscall(__NR_sys_io_setup2, depth, flags,
443 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
449 static int fio_aioring_post_init(struct thread_data *td)
451 struct aioring_data *ld = td->io_ops_data;
452 struct aioring_options *o = td->eo;
460 for (i = 0; i < td->o.iodepth; i++) {
461 io_u = ld->io_u_index[i];
462 iocb = &ld->iocbs[i];
463 iocb->u.c.buf = io_u->buf;
464 iocb->u.c.nbytes = td_max_bs(td);
467 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
471 err = fio_aioring_queue_init(td);
473 td_verror(td, -err, "io_queue_init");
480 static int fio_aioring_init(struct thread_data *td)
482 struct aioring_data *ld;
484 if (td->o.iodepth <= 1) {
485 printf("aio-ring: needs a minimum QD of 2\n");
489 ld = calloc(1, sizeof(*ld));
492 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
493 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
495 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
496 memset(ld->iocbs, 0, aioring_sq_iocb(td));
498 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
499 memset(ld->sq_ring, 0, aioring_sq_size(td));
500 ld->sq_ring->nr_events = td->o.iodepth;
501 ld->sq_ring->iocbs = (u64) ld->iocbs;
503 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
504 memset(ld->cq_ring, 0, aioring_cq_size(td));
505 ld->cq_ring->nr_events = td->o.iodepth * 2;
507 td->io_ops_data = ld;
511 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
513 struct aioring_data *ld = td->io_ops_data;
515 ld->io_u_index[io_u->index] = io_u;
519 static struct ioengine_ops ioengine = {
521 .version = FIO_IOOPS_VERSION,
522 .init = fio_aioring_init,
523 .post_init = fio_aioring_post_init,
524 .io_u_init = fio_aioring_io_u_init,
525 .prep = fio_aioring_prep,
526 .queue = fio_aioring_queue,
527 .commit = fio_aioring_commit,
528 .getevents = fio_aioring_getevents,
529 .event = fio_aioring_event,
530 .cleanup = fio_aioring_cleanup,
531 .open_file = generic_open_file,
532 .close_file = generic_close_file,
533 .get_file_size = generic_get_file_size,
535 .option_struct_size = sizeof(struct aioring_options),
538 static void fio_init fio_aioring_register(void)
540 #ifdef __NR_sys_io_setup2
541 #ifdef __NR_sys_io_ring_enter
542 register_ioengine(&ioengine);
547 static void fio_exit fio_aioring_unregister(void)
549 #ifdef __NR_sys_io_setup2
550 #ifdef __NR_sys_io_ring_enter
551 unregister_ioengine(&ioengine);