1 #ifdef ARCH_HAVE_AIORING
5 * IO engine using the new native Linux libaio ring interface
13 #include <sys/resource.h>
16 #include "../lib/pow2.h"
17 #include "../optgroup.h"
18 #include "../lib/memalign.h"
20 #ifndef IOCB_FLAG_HIPRI
21 #define IOCB_FLAG_HIPRI (1 << 2)
27 #ifndef IOCTX_FLAG_IOPOLL
28 #define IOCTX_FLAG_IOPOLL (1 << 0)
30 #ifndef IOCTX_FLAG_SCQRING
31 #define IOCTX_FLAG_SCQRING (1 << 1)
33 #ifndef IOCTX_FLAG_FIXEDBUFS
34 #define IOCTX_FLAG_FIXEDBUFS (1 << 2)
36 #ifndef IOCTX_FLAG_SQTHREAD
37 #define IOCTX_FLAG_SQTHREAD (1 << 3)
39 #ifndef IOCTX_FLAG_SQWQ
40 #define IOCTX_FLAG_SQWQ (1 << 4)
44 * io_ring_enter(2) flags
46 #ifndef IORING_FLAG_SUBMIT
47 #define IORING_FLAG_SUBMIT (1 << 0)
49 #ifndef IORING_FLAG_GETEVENTS
50 #define IORING_FLAG_GETEVENTS (1 << 1)
80 struct io_event events[0];
86 struct io_u **io_u_index;
88 struct aio_sq_ring *sq_ring;
91 struct aio_cq_ring *cq_ring;
92 struct io_event *events;
98 struct aioring_options {
101 unsigned int fixedbufs;
104 static struct fio_option options[] = {
107 .lname = "High Priority",
108 .type = FIO_OPT_STR_SET,
109 .off1 = offsetof(struct aioring_options, hipri),
110 .help = "Use polled IO completions",
111 .category = FIO_OPT_C_ENGINE,
112 .group = FIO_OPT_G_LIBAIO,
116 .lname = "Fixed (pre-mapped) IO buffers",
117 .type = FIO_OPT_STR_SET,
118 .off1 = offsetof(struct aioring_options, fixedbufs),
119 .help = "Pre map IO buffers",
120 .category = FIO_OPT_C_ENGINE,
121 .group = FIO_OPT_G_LIBAIO,
128 static int fio_aioring_commit(struct thread_data *td);
130 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
131 unsigned int min_complete, unsigned int flags)
133 #ifdef __NR_sys_io_ring_enter
134 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
141 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
143 struct aioring_data *ld = td->io_ops_data;
144 struct fio_file *f = io_u->file;
145 struct aioring_options *o = td->eo;
148 iocb = &ld->iocbs[io_u->index];
150 if (io_u->ddir == DDIR_READ) {
152 iocb->aio_fildes = f->fd;
153 iocb->aio_lio_opcode = IO_CMD_PREAD;
154 iocb->u.c.offset = io_u->offset;
156 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
158 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
160 } else if (io_u->ddir == DDIR_WRITE) {
162 iocb->aio_fildes = f->fd;
163 iocb->aio_lio_opcode = IO_CMD_PWRITE;
164 iocb->u.c.offset = io_u->offset;
166 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
168 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
170 } else if (ddir_sync(io_u->ddir))
171 io_prep_fsync(iocb, f->fd);
177 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
179 struct aioring_data *ld = td->io_ops_data;
184 index = event + ld->cq_ring_off;
185 if (index >= ld->cq_ring->nr_events)
186 index -= ld->cq_ring->nr_events;
188 ev = &ld->cq_ring->events[index];
191 if (ev->res != io_u->xfer_buflen) {
192 if (ev->res > io_u->xfer_buflen)
193 io_u->error = -ev->res;
195 io_u->resid = io_u->xfer_buflen - ev->res;
202 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
205 struct aioring_data *ld = td->io_ops_data;
206 struct aio_cq_ring *ring = ld->cq_ring;
207 u32 head, reaped = 0;
212 if (head == ring->tail)
216 if (head == ring->nr_events)
218 } while (reaped + events < max);
225 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
226 unsigned int max, const struct timespec *t)
228 struct aioring_data *ld = td->io_ops_data;
229 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
230 struct aio_cq_ring *ring = ld->cq_ring;
233 ld->cq_ring_off = ring->head;
235 r = fio_aioring_cqring_reap(td, events, max);
241 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
242 IORING_FLAG_GETEVENTS);
246 perror("ring enter");
249 } while (events < min);
251 return r < 0 ? r : events;
254 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
257 struct aioring_data *ld = td->io_ops_data;
258 struct aio_sq_ring *ring = ld->sq_ring;
259 unsigned tail, next_tail;
261 fio_ro_check(td, io_u);
263 if (ld->queued == td->o.iodepth)
267 * fsync is tricky, since it can fail and we need to do it
268 * serialized with other io. the reason is that linux doesn't
269 * support aio fsync yet. So return busy for the case where we
270 * have pending io, to let fio complete those first.
272 if (ddir_sync(io_u->ddir)) {
276 do_io_u_sync(td, io_u);
277 return FIO_Q_COMPLETED;
280 if (io_u->ddir == DDIR_TRIM) {
284 do_io_u_trim(td, io_u);
285 io_u_mark_submit(td, 1);
286 io_u_mark_complete(td, 1);
287 return FIO_Q_COMPLETED;
291 next_tail = tail + 1;
292 if (next_tail == ring->nr_events)
295 if (next_tail == ring->head)
298 ring->array[tail] = io_u->index;
299 ring->tail = next_tail;
306 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
308 struct aioring_data *ld = td->io_ops_data;
311 if (!fio_fill_issue_time(td))
314 fio_gettime(&now, NULL);
317 int index = ld->sq_ring->array[start];
318 struct io_u *io_u = io_u = ld->io_u_index[index];
320 memcpy(&io_u->issue_time, &now, sizeof(now));
321 io_u_queued(td, io_u);
324 if (start == ld->sq_ring->nr_events)
329 static int fio_aioring_commit(struct thread_data *td)
331 struct aioring_data *ld = td->io_ops_data;
338 int start = ld->sq_ring->head;
339 long nr = ld->queued;
341 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
342 IORING_FLAG_GETEVENTS);
344 perror("io_ring_enter");
346 fio_aioring_queued(td, start, ret);
347 io_u_mark_submit(td, ret);
351 } else if (ret == -EINTR || !ret) {
353 io_u_mark_submit(td, ret);
355 } else if (ret == -EAGAIN) {
357 * If we get EAGAIN, we should break out without
358 * error and let the upper layer reap some
359 * events for us. If we have no queued IO, we
360 * must loop here. If we loop for more than 30s,
361 * just error out, something must be buggy in the
370 } else if (ret == -ENOMEM) {
372 * If we get -ENOMEM, reap events if we can. If
373 * we cannot, treat it as a fatal event since there's
374 * nothing we can do about it.
381 } while (ld->queued);
386 static size_t aioring_cq_size(struct thread_data *td)
388 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
391 static size_t aioring_sq_iocb(struct thread_data *td)
393 return sizeof(struct iocb) * td->o.iodepth;
396 static size_t aioring_sq_size(struct thread_data *td)
398 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
401 static void fio_aioring_cleanup(struct thread_data *td)
403 struct aioring_data *ld = td->io_ops_data;
407 * Work-around to avoid huge RCU stalls at exit time. If we
408 * don't do this here, then it'll be torn down by exit_aio().
409 * But for that case we can parallellize the freeing, thus
410 * speeding it up a lot.
412 if (!(td->flags & TD_F_CHILD))
413 io_destroy(ld->aio_ctx);
414 free(ld->io_u_index);
416 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
417 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
418 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
423 static int fio_aioring_queue_init(struct thread_data *td)
425 #ifdef __NR_sys_io_setup2
426 struct aioring_data *ld = td->io_ops_data;
427 struct aioring_options *o = td->eo;
428 int flags = IOCTX_FLAG_SCQRING;
429 int depth = td->o.iodepth;
432 flags |= IOCTX_FLAG_IOPOLL;
434 struct rlimit rlim = {
435 .rlim_cur = RLIM_INFINITY,
436 .rlim_max = RLIM_INFINITY,
439 setrlimit(RLIMIT_MEMLOCK, &rlim);
440 flags |= IOCTX_FLAG_FIXEDBUFS;
443 return syscall(__NR_sys_io_setup2, depth, flags,
444 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
450 static int fio_aioring_post_init(struct thread_data *td)
452 struct aioring_data *ld = td->io_ops_data;
453 struct aioring_options *o = td->eo;
461 for (i = 0; i < td->o.iodepth; i++) {
462 io_u = ld->io_u_index[i];
463 iocb = &ld->iocbs[i];
464 iocb->u.c.buf = io_u->buf;
465 iocb->u.c.nbytes = td_max_bs(td);
468 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
472 err = fio_aioring_queue_init(td);
474 td_verror(td, -err, "io_queue_init");
481 static int fio_aioring_init(struct thread_data *td)
483 struct aioring_data *ld;
485 if (td->o.iodepth <= 1) {
486 printf("aio-ring: needs a minimum QD of 2\n");
490 ld = calloc(1, sizeof(*ld));
493 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
494 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
496 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
497 memset(ld->iocbs, 0, aioring_sq_iocb(td));
499 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
500 memset(ld->sq_ring, 0, aioring_sq_size(td));
501 ld->sq_ring->nr_events = td->o.iodepth;
502 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
504 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
505 memset(ld->cq_ring, 0, aioring_cq_size(td));
506 ld->cq_ring->nr_events = td->o.iodepth * 2;
508 td->io_ops_data = ld;
512 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
514 struct aioring_data *ld = td->io_ops_data;
516 ld->io_u_index[io_u->index] = io_u;
520 static struct ioengine_ops ioengine = {
522 .version = FIO_IOOPS_VERSION,
523 .init = fio_aioring_init,
524 .post_init = fio_aioring_post_init,
525 .io_u_init = fio_aioring_io_u_init,
526 .prep = fio_aioring_prep,
527 .queue = fio_aioring_queue,
528 .commit = fio_aioring_commit,
529 .getevents = fio_aioring_getevents,
530 .event = fio_aioring_event,
531 .cleanup = fio_aioring_cleanup,
532 .open_file = generic_open_file,
533 .close_file = generic_close_file,
534 .get_file_size = generic_get_file_size,
536 .option_struct_size = sizeof(struct aioring_options),
539 static void fio_init fio_aioring_register(void)
541 register_ioengine(&ioengine);
544 static void fio_exit fio_aioring_unregister(void)
546 unregister_ioengine(&ioengine);