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)
47 * io_ring_enter(2) flags
49 #ifndef IORING_FLAG_SUBMIT
50 #define IORING_FLAG_SUBMIT (1 << 0)
52 #ifndef IORING_FLAG_GETEVENTS
53 #define IORING_FLAG_GETEVENTS (1 << 1)
83 struct io_event events[0];
89 struct io_u **io_u_index;
91 struct aio_sq_ring *sq_ring;
94 struct aio_cq_ring *cq_ring;
95 struct io_event *events;
101 struct aioring_options {
104 unsigned int fixedbufs;
105 unsigned int sqthread;
106 unsigned int sqthread_set;
110 static int fio_aioring_sqthread_cb(void *data,
111 unsigned long long *val)
113 struct aioring_options *o = data;
120 static struct fio_option options[] = {
123 .lname = "High Priority",
124 .type = FIO_OPT_STR_SET,
125 .off1 = offsetof(struct aioring_options, hipri),
126 .help = "Use polled IO completions",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_LIBAIO,
132 .lname = "Fixed (pre-mapped) IO buffers",
133 .type = FIO_OPT_STR_SET,
134 .off1 = offsetof(struct aioring_options, fixedbufs),
135 .help = "Pre map IO buffers",
136 .category = FIO_OPT_C_ENGINE,
137 .group = FIO_OPT_G_LIBAIO,
141 .lname = "Use kernel SQ thread on this CPU",
143 .cb = fio_aioring_sqthread_cb,
144 .help = "Offload submission to kernel thread",
145 .category = FIO_OPT_C_ENGINE,
146 .group = FIO_OPT_G_LIBAIO,
150 .lname = "Offload submission to kernel workqueue",
151 .type = FIO_OPT_STR_SET,
152 .off1 = offsetof(struct aioring_options, sqwq),
153 .help = "Offload submission to kernel workqueue",
154 .category = FIO_OPT_C_ENGINE,
155 .group = FIO_OPT_G_LIBAIO,
162 static int fio_aioring_commit(struct thread_data *td);
164 static int io_ring_enter(io_context_t ctx, unsigned int to_submit,
165 unsigned int min_complete, unsigned int flags)
167 return syscall(__NR_sys_io_ring_enter, ctx, to_submit, min_complete,
171 static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
173 struct aioring_data *ld = td->io_ops_data;
174 struct fio_file *f = io_u->file;
175 struct aioring_options *o = td->eo;
178 iocb = &ld->iocbs[io_u->index];
180 if (io_u->ddir == DDIR_READ) {
182 iocb->aio_fildes = f->fd;
183 iocb->aio_lio_opcode = IO_CMD_PREAD;
184 iocb->u.c.offset = io_u->offset;
186 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
188 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
190 } else if (io_u->ddir == DDIR_WRITE) {
192 iocb->aio_fildes = f->fd;
193 iocb->aio_lio_opcode = IO_CMD_PWRITE;
194 iocb->u.c.offset = io_u->offset;
196 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
198 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
200 } else if (ddir_sync(io_u->ddir))
201 io_prep_fsync(iocb, f->fd);
207 static struct io_u *fio_aioring_event(struct thread_data *td, int event)
209 struct aioring_data *ld = td->io_ops_data;
214 index = event + ld->cq_ring_off;
215 if (index >= ld->cq_ring->nr_events)
216 index -= ld->cq_ring->nr_events;
218 ev = &ld->cq_ring->events[index];
221 if (ev->res != io_u->xfer_buflen) {
222 if (ev->res > io_u->xfer_buflen)
223 io_u->error = -ev->res;
225 io_u->resid = io_u->xfer_buflen - ev->res;
232 static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
235 struct aioring_data *ld = td->io_ops_data;
236 struct aio_cq_ring *ring = ld->cq_ring;
237 u32 head, reaped = 0;
242 if (head == ring->tail)
246 if (head == ring->nr_events)
248 } while (reaped + events < max);
255 static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
256 unsigned int max, const struct timespec *t)
258 struct aioring_data *ld = td->io_ops_data;
259 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
260 struct aio_cq_ring *ring = ld->cq_ring;
263 ld->cq_ring_off = ring->head;
265 r = fio_aioring_cqring_reap(td, events, max);
271 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
272 IORING_FLAG_GETEVENTS);
276 td_verror(td, errno, "io_ring_enter get");
279 } while (events < min);
281 return r < 0 ? r : events;
284 static enum fio_q_status fio_aioring_queue(struct thread_data *td,
287 struct aioring_data *ld = td->io_ops_data;
288 struct aio_sq_ring *ring = ld->sq_ring;
289 unsigned tail, next_tail;
291 fio_ro_check(td, io_u);
293 if (ld->queued == td->o.iodepth)
296 if (io_u->ddir == DDIR_TRIM) {
300 do_io_u_trim(td, io_u);
301 io_u_mark_submit(td, 1);
302 io_u_mark_complete(td, 1);
303 return FIO_Q_COMPLETED;
307 next_tail = tail + 1;
308 if (next_tail == ring->nr_events)
311 if (next_tail == ring->head)
314 ring->array[tail] = io_u->index;
315 ring->tail = next_tail;
322 static void fio_aioring_queued(struct thread_data *td, int start, int nr)
324 struct aioring_data *ld = td->io_ops_data;
327 if (!fio_fill_issue_time(td))
330 fio_gettime(&now, NULL);
333 int index = ld->sq_ring->array[start];
334 struct io_u *io_u = io_u = ld->io_u_index[index];
336 memcpy(&io_u->issue_time, &now, sizeof(now));
337 io_u_queued(td, io_u);
340 if (start == ld->sq_ring->nr_events)
345 static int fio_aioring_commit(struct thread_data *td)
347 struct aioring_data *ld = td->io_ops_data;
354 int start = ld->sq_ring->head;
355 long nr = ld->queued;
357 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
358 IORING_FLAG_GETEVENTS);
360 fio_aioring_queued(td, start, ret);
361 io_u_mark_submit(td, ret);
366 io_u_mark_submit(td, ret);
369 if (errno == EAGAIN) {
370 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
373 /* Shouldn't happen */
377 td_verror(td, errno, "io_ring_enter sumit");
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;
405 /* Bump depth to match init depth */
409 * Work-around to avoid huge RCU stalls at exit time. If we
410 * don't do this here, then it'll be torn down by exit_aio().
411 * But for that case we can parallellize the freeing, thus
412 * speeding it up a lot.
414 if (!(td->flags & TD_F_CHILD))
415 io_destroy(ld->aio_ctx);
416 free(ld->io_u_index);
418 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
419 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
420 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
425 static int fio_aioring_queue_init(struct thread_data *td)
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;
434 if (o->sqthread_set) {
435 ld->sq_ring->sq_thread_cpu = o->sqthread;
436 flags |= IOCTX_FLAG_SQTHREAD;
438 flags |= IOCTX_FLAG_SQWQ;
441 struct rlimit rlim = {
442 .rlim_cur = RLIM_INFINITY,
443 .rlim_max = RLIM_INFINITY,
446 setrlimit(RLIMIT_MEMLOCK, &rlim);
447 flags |= IOCTX_FLAG_FIXEDBUFS;
450 return syscall(__NR_sys_io_setup2, depth, flags,
451 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
454 static int fio_aioring_post_init(struct thread_data *td)
456 struct aioring_data *ld = td->io_ops_data;
457 struct aioring_options *o = td->eo;
465 for (i = 0; i < td->o.iodepth; i++) {
466 io_u = ld->io_u_index[i];
467 iocb = &ld->iocbs[i];
468 iocb->u.c.buf = io_u->buf;
469 iocb->u.c.nbytes = td_max_bs(td);
472 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
476 err = fio_aioring_queue_init(td);
478 td_verror(td, -err, "io_queue_init");
482 /* Adjust depth back again */
487 static int fio_aioring_init(struct thread_data *td)
489 struct aioring_options *o = td->eo;
490 struct aioring_data *ld;
492 if (o->sqthread_set && o->sqwq) {
493 log_err("fio: aioring sqthread and sqwq are mutually exclusive\n");
497 /* ring needs an extra entry, add one to achieve QD set */
500 ld = calloc(1, sizeof(*ld));
503 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
504 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
506 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
507 memset(ld->iocbs, 0, aioring_sq_iocb(td));
509 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
510 memset(ld->sq_ring, 0, aioring_sq_size(td));
511 ld->sq_ring->nr_events = td->o.iodepth;
512 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
514 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
515 memset(ld->cq_ring, 0, aioring_cq_size(td));
516 ld->cq_ring->nr_events = td->o.iodepth * 2;
518 td->io_ops_data = ld;
522 static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
524 struct aioring_data *ld = td->io_ops_data;
526 ld->io_u_index[io_u->index] = io_u;
530 static struct ioengine_ops ioengine = {
532 .version = FIO_IOOPS_VERSION,
533 .init = fio_aioring_init,
534 .post_init = fio_aioring_post_init,
535 .io_u_init = fio_aioring_io_u_init,
536 .prep = fio_aioring_prep,
537 .queue = fio_aioring_queue,
538 .commit = fio_aioring_commit,
539 .getevents = fio_aioring_getevents,
540 .event = fio_aioring_event,
541 .cleanup = fio_aioring_cleanup,
542 .open_file = generic_open_file,
543 .close_file = generic_close_file,
544 .get_file_size = generic_get_file_size,
546 .option_struct_size = sizeof(struct aioring_options),
549 static void fio_init fio_aioring_register(void)
551 register_ioengine(&ioengine);
554 static void fio_exit fio_aioring_unregister(void)
556 unregister_ioengine(&ioengine);