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 #ifdef ARCH_HAVE_AIORING
21 #ifndef IOCB_FLAG_HIPRI
22 #define IOCB_FLAG_HIPRI (1 << 2)
28 #ifndef IOCTX_FLAG_IOPOLL
29 #define IOCTX_FLAG_IOPOLL (1 << 0)
31 #ifndef IOCTX_FLAG_SCQRING
32 #define IOCTX_FLAG_SCQRING (1 << 1)
34 #ifndef IOCTX_FLAG_FIXEDBUFS
35 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
37 #ifndef IOCTX_FLAG_SQTHREAD
38 #define IOCTX_FLAG_SQTHREAD (1 << 3)
40 #ifndef IOCTX_FLAG_SQWQ
41 #define IOCTX_FLAG_SQWQ (1 << 4)
45 * io_ring_enter(2) flags
47 #ifndef IORING_FLAG_SUBMIT
48 #define IORING_FLAG_SUBMIT (1 << 0)
50 #ifndef IORING_FLAG_GETEVENTS
51 #define IORING_FLAG_GETEVENTS (1 << 1)
81 struct io_event events[0];
87 struct io_u **io_u_index;
89 struct aio_sq_ring *sq_ring;
92 struct aio_cq_ring *cq_ring;
93 struct io_event *events;
99 struct aioring_options {
102 unsigned int fixedbufs;
105 static struct fio_option options[] = {
108 .lname = "High Priority",
109 .type = FIO_OPT_STR_SET,
110 .off1 = offsetof(struct aioring_options, hipri),
111 .help = "Use polled IO completions",
112 .category = FIO_OPT_C_ENGINE,
113 .group = FIO_OPT_G_LIBAIO,
117 .lname = "Fixed (pre-mapped) IO buffers",
118 .type = FIO_OPT_STR_SET,
119 .off1 = offsetof(struct aioring_options, fixedbufs),
120 .help = "Pre map IO buffers",
121 .category = FIO_OPT_C_ENGINE,
122 .group = FIO_OPT_G_LIBAIO,
129 static int fio_aioring_commit(struct thread_data *td);
131 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
132 unsigned int min_complete, unsigned int flags)
134 #ifdef __NR_sys_io_ring_enter
135 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
142 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
144 struct aioring_data *ld = td->io_ops_data;
145 struct fio_file *f = io_u->file;
146 struct aioring_options *o = td->eo;
149 iocb = &ld->iocbs[io_u->index];
151 if (io_u->ddir == DDIR_READ) {
153 iocb->aio_fildes = f->fd;
154 iocb->aio_lio_opcode = IO_CMD_PREAD;
155 iocb->u.c.offset = io_u->offset;
157 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
159 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
161 } else if (io_u->ddir == DDIR_WRITE) {
163 iocb->aio_fildes = f->fd;
164 iocb->aio_lio_opcode = IO_CMD_PWRITE;
165 iocb->u.c.offset = io_u->offset;
167 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
169 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
171 } else if (ddir_sync(io_u->ddir))
172 io_prep_fsync(iocb, f->fd);
178 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
180 struct aioring_data *ld = td->io_ops_data;
185 index = event + ld->cq_ring_off;
186 if (index >= ld->cq_ring->nr_events)
187 index -= ld->cq_ring->nr_events;
189 ev = &ld->cq_ring->events[index];
192 if (ev->res != io_u->xfer_buflen) {
193 if (ev->res > io_u->xfer_buflen)
194 io_u->error = -ev->res;
196 io_u->resid = io_u->xfer_buflen - ev->res;
203 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
206 struct aioring_data *ld = td->io_ops_data;
207 struct aio_cq_ring *ring = ld->cq_ring;
208 u32 head, reaped = 0;
213 if (head == ring->tail)
217 if (head == ring->nr_events)
219 } while (reaped + events < max);
226 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
227 unsigned int max, const struct timespec *t)
229 struct aioring_data *ld = td->io_ops_data;
230 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
231 struct aio_cq_ring *ring = ld->cq_ring;
234 ld->cq_ring_off = ring->head;
236 r = fio_aioring_cqring_reap(td, events, max);
242 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
243 IORING_FLAG_GETEVENTS);
247 perror("ring enter");
250 } while (events < min);
252 return r < 0 ? r : events;
255 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
258 struct aioring_data *ld = td->io_ops_data;
259 struct aio_sq_ring *ring = ld->sq_ring;
260 unsigned tail, next_tail;
262 fio_ro_check(td, io_u);
264 if (ld->queued == td->o.iodepth)
268 * fsync is tricky, since it can fail and we need to do it
269 * serialized with other io. the reason is that linux doesn't
270 * support aio fsync yet. So return busy for the case where we
271 * have pending io, to let fio complete those first.
273 if (ddir_sync(io_u->ddir)) {
277 do_io_u_sync(td, io_u);
278 return FIO_Q_COMPLETED;
281 if (io_u->ddir == DDIR_TRIM) {
285 do_io_u_trim(td, io_u);
286 io_u_mark_submit(td, 1);
287 io_u_mark_complete(td, 1);
288 return FIO_Q_COMPLETED;
292 next_tail = tail + 1;
293 if (next_tail == ring->nr_events)
296 if (next_tail == ring->head)
299 ring->array[tail] = io_u->index;
300 ring->tail = next_tail;
307 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
309 struct aioring_data *ld = td->io_ops_data;
312 if (!fio_fill_issue_time(td))
315 fio_gettime(&now, NULL);
318 int index = ld->sq_ring->array[start];
319 struct io_u *io_u = io_u = ld->io_u_index[index];
321 memcpy(&io_u->issue_time, &now, sizeof(now));
322 io_u_queued(td, io_u);
325 if (start == ld->sq_ring->nr_events)
330 static int fio_aioring_commit(struct thread_data *td)
332 struct aioring_data *ld = td->io_ops_data;
339 int start = ld->sq_ring->head;
340 long nr = ld->queued;
342 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
343 IORING_FLAG_GETEVENTS);
345 perror("io_ring_enter");
347 fio_aioring_queued(td, start, ret);
348 io_u_mark_submit(td, ret);
352 } else if (ret == -EINTR || !ret) {
354 io_u_mark_submit(td, ret);
356 } else if (ret == -EAGAIN) {
358 * If we get EAGAIN, we should break out without
359 * error and let the upper layer reap some
360 * events for us. If we have no queued IO, we
361 * must loop here. If we loop for more than 30s,
362 * just error out, something must be buggy in the
371 } else if (ret == -ENOMEM) {
373 * If we get -ENOMEM, reap events if we can. If
374 * we cannot, treat it as a fatal event since there's
375 * nothing we can do about it.
382 } while (ld->queued);
387 static size_t aioring_cq_size(struct thread_data *td)
389 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
392 static size_t aioring_sq_iocb(struct thread_data *td)
394 return sizeof(struct iocb) * td->o.iodepth;
397 static size_t aioring_sq_size(struct thread_data *td)
399 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
402 static void fio_aioring_cleanup(struct thread_data *td)
404 struct aioring_data *ld = td->io_ops_data;
408 * Work-around to avoid huge RCU stalls at exit time. If we
409 * don't do this here, then it'll be torn down by exit_aio().
410 * But for that case we can parallellize the freeing, thus
411 * speeding it up a lot.
413 if (!(td->flags & TD_F_CHILD))
414 io_destroy(ld->aio_ctx);
415 free(ld->io_u_index);
417 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
418 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
419 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
424 static int fio_aioring_queue_init(struct thread_data *td)
426 #ifdef __NR_sys_io_setup2
427 struct aioring_data *ld = td->io_ops_data;
428 struct aioring_options *o = td->eo;
429 int flags = IOCTX_FLAG_SCQRING;
430 int depth = td->o.iodepth;
433 flags |= IOCTX_FLAG_IOPOLL;
435 struct rlimit rlim = {
436 .rlim_cur = RLIM_INFINITY,
437 .rlim_max = RLIM_INFINITY,
440 setrlimit(RLIMIT_MEMLOCK, &rlim);
441 flags |= IOCTX_FLAG_FIXEDBUFS;
444 return syscall(__NR_sys_io_setup2, depth, flags,
445 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
451 static int fio_aioring_post_init(struct thread_data *td)
453 struct aioring_data *ld = td->io_ops_data;
454 struct aioring_options *o = td->eo;
462 for (i = 0; i < td->o.iodepth; i++) {
463 io_u = ld->io_u_index[i];
464 iocb = &ld->iocbs[i];
465 iocb->u.c.buf = io_u->buf;
466 iocb->u.c.nbytes = td_max_bs(td);
469 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
473 err = fio_aioring_queue_init(td);
475 td_verror(td, -err, "io_queue_init");
482 static int fio_aioring_init(struct thread_data *td)
484 struct aioring_data *ld;
486 /* ring needs an extra entry, add one to achieve QD set */
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) (uintptr_t) 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 register_ioengine(&ioengine);
543 static void fio_exit fio_aioring_unregister(void)
545 unregister_ioengine(&ioengine);