t/aio-ring: use syscall defines
[fio.git] / engines / aioring.c
CommitLineData
52885fa2
JA
1/*
2 * aioring engine
3 *
a90cd050
JA
4 * IO engine using the new native Linux libaio ring interface. See:
5 *
6 * http://git.kernel.dk/cgit/linux-block/log/?h=aio-poll
52885fa2
JA
7 *
8 */
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <libaio.h>
13#include <sys/time.h>
14#include <sys/resource.h>
15
16#include "../fio.h"
17#include "../lib/pow2.h"
18#include "../optgroup.h"
19#include "../lib/memalign.h"
b87aa01a 20#include "../lib/fls.h"
52885fa2 21
a30e63cf
JA
22#ifdef ARCH_HAVE_AIORING
23
52885fa2 24/*
9a2d78b3 25 * io_uring_setup(2) flags
52885fa2 26 */
52885fa2 27#ifndef IOCTX_FLAG_SCQRING
9a2d78b3
JA
28#define IOCTX_FLAG_SCQRING (1 << 0)
29#endif
30#ifndef IOCTX_FLAG_IOPOLL
31#define IOCTX_FLAG_IOPOLL (1 << 1)
52885fa2
JA
32#endif
33#ifndef IOCTX_FLAG_FIXEDBUFS
34#define IOCTX_FLAG_FIXEDBUFS (1 << 2)
35#endif
36#ifndef IOCTX_FLAG_SQTHREAD
37#define IOCTX_FLAG_SQTHREAD (1 << 3)
38#endif
39#ifndef IOCTX_FLAG_SQWQ
40#define IOCTX_FLAG_SQWQ (1 << 4)
41#endif
771c9901
JA
42#ifndef IOCTX_FLAG_SQPOLL
43#define IOCTX_FLAG_SQPOLL (1 << 5)
44#endif
45
9a2d78b3
JA
46#define IORING_OFF_SQ_RING 0ULL
47#define IORING_OFF_CQ_RING 0x8000000ULL
48#define IORING_OFF_IOCB 0x10000000ULL
52885fa2
JA
49
50/*
9a2d78b3 51 * io_uring_enter(2) flags
52885fa2 52 */
9a2d78b3
JA
53#ifndef IORING_ENTER_GETEVENTS
54#define IORING_ENTER_GETEVENTS (1 << 0)
52885fa2
JA
55#endif
56
57typedef uint64_t u64;
58typedef uint32_t u32;
59typedef uint16_t u16;
60
4cdbc048
JA
61#define IORING_SQ_NEED_WAKEUP (1 << 0)
62
96563db9
JA
63#define IOEV_RES2_CACHEHIT (1 << 0)
64
9a2d78b3
JA
65struct aio_uring_offsets {
66 u32 head;
67 u32 tail;
68 u32 ring_mask;
69 u32 ring_entries;
70 u32 flags;
71 u32 elems;
72};
73
74struct aio_uring_params {
75 u32 sq_entries;
76 u32 cq_entries;
77 u32 flags;
78 u16 sq_thread_cpu;
79 u16 resv[9];
80 struct aio_uring_offsets sq_off;
81 struct aio_uring_offsets cq_off;
82};
83
52885fa2 84struct aio_sq_ring {
9a2d78b3
JA
85 u32 *head;
86 u32 *tail;
87 u32 *ring_mask;
88 u32 *ring_entries;
89 u32 *flags;
90 u32 *array;
52885fa2
JA
91};
92
93struct aio_cq_ring {
9a2d78b3
JA
94 u32 *head;
95 u32 *tail;
96 u32 *ring_mask;
97 u32 *ring_entries;
98 struct io_event *events;
99};
100
101struct aioring_mmap {
102 void *ptr;
103 size_t len;
52885fa2
JA
104};
105
106struct aioring_data {
9a2d78b3
JA
107 int ring_fd;
108
52885fa2
JA
109 struct io_u **io_us;
110 struct io_u **io_u_index;
111
9a2d78b3 112 struct aio_sq_ring sq_ring;
52885fa2 113 struct iocb *iocbs;
9a2d78b3 114 struct iovec *iovecs;
b87aa01a 115 unsigned sq_ring_mask;
52885fa2 116
9a2d78b3 117 struct aio_cq_ring cq_ring;
52885fa2 118 struct io_event *events;
b87aa01a 119 unsigned cq_ring_mask;
52885fa2
JA
120
121 int queued;
122 int cq_ring_off;
b87aa01a 123 unsigned iodepth;
96563db9
JA
124
125 uint64_t cachehit;
126 uint64_t cachemiss;
9a2d78b3
JA
127
128 struct aioring_mmap mmap[3];
52885fa2
JA
129};
130
131struct aioring_options {
132 void *pad;
133 unsigned int hipri;
134 unsigned int fixedbufs;
a90cd050
JA
135 unsigned int sqthread;
136 unsigned int sqthread_set;
771c9901 137 unsigned int sqthread_poll;
a90cd050 138 unsigned int sqwq;
52885fa2
JA
139};
140
a90cd050
JA
141static int fio_aioring_sqthread_cb(void *data,
142 unsigned long long *val)
143{
144 struct aioring_options *o = data;
145
146 o->sqthread = *val;
147 o->sqthread_set = 1;
148 return 0;
149}
150
52885fa2
JA
151static struct fio_option options[] = {
152 {
153 .name = "hipri",
154 .lname = "High Priority",
155 .type = FIO_OPT_STR_SET,
156 .off1 = offsetof(struct aioring_options, hipri),
157 .help = "Use polled IO completions",
158 .category = FIO_OPT_C_ENGINE,
159 .group = FIO_OPT_G_LIBAIO,
160 },
161 {
162 .name = "fixedbufs",
163 .lname = "Fixed (pre-mapped) IO buffers",
164 .type = FIO_OPT_STR_SET,
165 .off1 = offsetof(struct aioring_options, fixedbufs),
166 .help = "Pre map IO buffers",
167 .category = FIO_OPT_C_ENGINE,
168 .group = FIO_OPT_G_LIBAIO,
169 },
a90cd050
JA
170 {
171 .name = "sqthread",
172 .lname = "Use kernel SQ thread on this CPU",
173 .type = FIO_OPT_INT,
174 .cb = fio_aioring_sqthread_cb,
175 .help = "Offload submission to kernel thread",
176 .category = FIO_OPT_C_ENGINE,
177 .group = FIO_OPT_G_LIBAIO,
178 },
771c9901
JA
179 {
180 .name = "sqthread_poll",
181 .lname = "Kernel SQ thread should poll",
182 .type = FIO_OPT_STR_SET,
183 .off1 = offsetof(struct aioring_options, sqthread_poll),
184 .help = "Used with sqthread, enables kernel side polling",
185 .category = FIO_OPT_C_ENGINE,
186 .group = FIO_OPT_G_LIBAIO,
187 },
a90cd050
JA
188 {
189 .name = "sqwq",
190 .lname = "Offload submission to kernel workqueue",
191 .type = FIO_OPT_STR_SET,
192 .off1 = offsetof(struct aioring_options, sqwq),
193 .help = "Offload submission to kernel workqueue",
194 .category = FIO_OPT_C_ENGINE,
195 .group = FIO_OPT_G_LIBAIO,
196 },
52885fa2
JA
197 {
198 .name = NULL,
199 },
200};
201
9a2d78b3 202static int io_uring_enter(struct aioring_data *ld, unsigned int to_submit,
52885fa2
JA
203 unsigned int min_complete, unsigned int flags)
204{
9a2d78b3
JA
205 return syscall(__NR_sys_io_uring_enter, ld->ring_fd, to_submit,
206 min_complete, flags);
52885fa2
JA
207}
208
209static int fio_aioring_prep(struct thread_data *td, struct io_u *io_u)
210{
211 struct aioring_data *ld = td->io_ops_data;
212 struct fio_file *f = io_u->file;
52885fa2
JA
213 struct iocb *iocb;
214
215 iocb = &ld->iocbs[io_u->index];
216
e3970057
JA
217 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
218 if (io_u->ddir == DDIR_READ)
52885fa2 219 iocb->aio_lio_opcode = IO_CMD_PREAD;
e3970057 220 else
52885fa2 221 iocb->aio_lio_opcode = IO_CMD_PWRITE;
e3970057
JA
222 iocb->aio_reqprio = 0;
223 iocb->aio_fildes = f->fd;
224 iocb->u.c.buf = io_u->xfer_buf;
225 iocb->u.c.nbytes = io_u->xfer_buflen;
226 iocb->u.c.offset = io_u->offset;
ac4f3d4e 227 iocb->u.c.flags = 0;
52885fa2
JA
228 } else if (ddir_sync(io_u->ddir))
229 io_prep_fsync(iocb, f->fd);
230
231 iocb->data = io_u;
232 return 0;
233}
234
235static struct io_u *fio_aioring_event(struct thread_data *td, int event)
236{
237 struct aioring_data *ld = td->io_ops_data;
238 struct io_event *ev;
239 struct io_u *io_u;
b87aa01a 240 unsigned index;
52885fa2 241
b87aa01a 242 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
52885fa2 243
9a2d78b3 244 ev = &ld->cq_ring.events[index];
52885fa2
JA
245 io_u = ev->data;
246
247 if (ev->res != io_u->xfer_buflen) {
248 if (ev->res > io_u->xfer_buflen)
249 io_u->error = -ev->res;
250 else
251 io_u->resid = io_u->xfer_buflen - ev->res;
252 } else
253 io_u->error = 0;
254
96563db9
JA
255 if (io_u->ddir == DDIR_READ) {
256 if (ev->res2 & IOEV_RES2_CACHEHIT)
257 ld->cachehit++;
258 else
259 ld->cachemiss++;
260 }
261
52885fa2
JA
262 return io_u;
263}
264
265static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
266 unsigned int max)
267{
268 struct aioring_data *ld = td->io_ops_data;
9a2d78b3 269 struct aio_cq_ring *ring = &ld->cq_ring;
52885fa2
JA
270 u32 head, reaped = 0;
271
9a2d78b3 272 head = *ring->head;
52885fa2
JA
273 do {
274 read_barrier();
9a2d78b3 275 if (head == *ring->tail)
52885fa2
JA
276 break;
277 reaped++;
278 head++;
52885fa2
JA
279 } while (reaped + events < max);
280
9a2d78b3 281 *ring->head = head;
52885fa2
JA
282 write_barrier();
283 return reaped;
284}
285
286static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
287 unsigned int max, const struct timespec *t)
288{
289 struct aioring_data *ld = td->io_ops_data;
290 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
771c9901 291 struct aioring_options *o = td->eo;
9a2d78b3 292 struct aio_cq_ring *ring = &ld->cq_ring;
b87aa01a
JA
293 unsigned events = 0;
294 int r;
52885fa2 295
9a2d78b3 296 ld->cq_ring_off = *ring->head;
52885fa2
JA
297 do {
298 r = fio_aioring_cqring_reap(td, events, max);
299 if (r) {
300 events += r;
301 continue;
302 }
303
771c9901 304 if (!o->sqthread_poll) {
9a2d78b3
JA
305 r = io_uring_enter(ld, 0, actual_min,
306 IORING_ENTER_GETEVENTS);
771c9901
JA
307 if (r < 0) {
308 if (errno == EAGAIN)
309 continue;
9a2d78b3 310 td_verror(td, errno, "io_uring_enter");
771c9901
JA
311 break;
312 }
52885fa2
JA
313 }
314 } while (events < min);
315
316 return r < 0 ? r : events;
317}
318
319static enum fio_q_status fio_aioring_queue(struct thread_data *td,
320 struct io_u *io_u)
321{
322 struct aioring_data *ld = td->io_ops_data;
9a2d78b3 323 struct aio_sq_ring *ring = &ld->sq_ring;
52885fa2
JA
324 unsigned tail, next_tail;
325
326 fio_ro_check(td, io_u);
327
b87aa01a 328 if (ld->queued == ld->iodepth)
52885fa2
JA
329 return FIO_Q_BUSY;
330
52885fa2
JA
331 if (io_u->ddir == DDIR_TRIM) {
332 if (ld->queued)
333 return FIO_Q_BUSY;
334
335 do_io_u_trim(td, io_u);
336 io_u_mark_submit(td, 1);
337 io_u_mark_complete(td, 1);
338 return FIO_Q_COMPLETED;
339 }
340
9a2d78b3 341 tail = *ring->tail;
52885fa2 342 next_tail = tail + 1;
52885fa2 343 read_barrier();
9a2d78b3 344 if (next_tail == *ring->head)
52885fa2
JA
345 return FIO_Q_BUSY;
346
b87aa01a 347 ring->array[tail & ld->sq_ring_mask] = io_u->index;
9a2d78b3 348 *ring->tail = next_tail;
52885fa2
JA
349 write_barrier();
350
351 ld->queued++;
352 return FIO_Q_QUEUED;
353}
354
355static void fio_aioring_queued(struct thread_data *td, int start, int nr)
356{
357 struct aioring_data *ld = td->io_ops_data;
358 struct timespec now;
359
360 if (!fio_fill_issue_time(td))
361 return;
362
363 fio_gettime(&now, NULL);
364
365 while (nr--) {
9a2d78b3
JA
366 struct aio_sq_ring *ring = &ld->sq_ring;
367 int index = ring->array[start & ld->sq_ring_mask];
f8289afc 368 struct io_u *io_u = ld->io_u_index[index];
52885fa2
JA
369
370 memcpy(&io_u->issue_time, &now, sizeof(now));
371 io_u_queued(td, io_u);
372
373 start++;
52885fa2
JA
374 }
375}
376
377static int fio_aioring_commit(struct thread_data *td)
378{
379 struct aioring_data *ld = td->io_ops_data;
771c9901 380 struct aioring_options *o = td->eo;
52885fa2
JA
381 int ret;
382
383 if (!ld->queued)
384 return 0;
385
771c9901
JA
386 /* Nothing to do */
387 if (o->sqthread_poll) {
9a2d78b3 388 struct aio_sq_ring *ring = &ld->sq_ring;
4cdbc048 389
9a2d78b3
JA
390 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
391 io_uring_enter(ld, ld->queued, 0, 0);
771c9901
JA
392 ld->queued = 0;
393 return 0;
394 }
395
52885fa2 396 do {
9a2d78b3 397 unsigned start = *ld->sq_ring.head;
52885fa2
JA
398 long nr = ld->queued;
399
9a2d78b3 400 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
52885fa2
JA
401 if (ret > 0) {
402 fio_aioring_queued(td, start, ret);
403 io_u_mark_submit(td, ret);
404
405 ld->queued -= ret;
406 ret = 0;
a90cd050
JA
407 } else if (!ret) {
408 io_u_mark_submit(td, ret);
52885fa2 409 continue;
a90cd050
JA
410 } else {
411 if (errno == EAGAIN) {
412 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
413 if (ret)
414 continue;
415 /* Shouldn't happen */
416 usleep(1);
417 continue;
52885fa2 418 }
9a2d78b3 419 td_verror(td, errno, "io_uring_enter submit");
52885fa2 420 break;
a90cd050 421 }
52885fa2
JA
422 } while (ld->queued);
423
424 return ret;
425}
426
9a2d78b3 427static void fio_aioring_unmap(struct aioring_data *ld)
52885fa2 428{
9a2d78b3 429 int i;
52885fa2 430
9a2d78b3
JA
431 for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
432 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
433 close(ld->ring_fd);
b87aa01a
JA
434}
435
52885fa2
JA
436static void fio_aioring_cleanup(struct thread_data *td)
437{
438 struct aioring_data *ld = td->io_ops_data;
439
440 if (ld) {
96563db9
JA
441 td->ts.cachehit += ld->cachehit;
442 td->ts.cachemiss += ld->cachemiss;
443
52885fa2
JA
444 /*
445 * Work-around to avoid huge RCU stalls at exit time. If we
446 * don't do this here, then it'll be torn down by exit_aio().
447 * But for that case we can parallellize the freeing, thus
448 * speeding it up a lot.
449 */
450 if (!(td->flags & TD_F_CHILD))
9a2d78b3
JA
451 fio_aioring_unmap(ld);
452
52885fa2
JA
453 free(ld->io_u_index);
454 free(ld->io_us);
9a2d78b3 455 free(ld->iovecs);
52885fa2
JA
456 free(ld);
457 }
458}
459
9a2d78b3
JA
460static int fio_aioring_mmap(struct aioring_data *ld, struct aio_uring_params *p)
461{
462 struct aio_sq_ring *sring = &ld->sq_ring;
463 struct aio_cq_ring *cring = &ld->cq_ring;
464 void *ptr;
465
466 ld->mmap[0].len = p->sq_off.elems + p->sq_entries * sizeof(u32);
467 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
468 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
469 IORING_OFF_SQ_RING);
470 ld->mmap[0].ptr = ptr;
471 sring->head = ptr + p->sq_off.head;
472 sring->tail = ptr + p->sq_off.tail;
473 sring->ring_mask = ptr + p->sq_off.ring_mask;
474 sring->ring_entries = ptr + p->sq_off.ring_entries;
475 sring->flags = ptr + p->sq_off.flags;
476 sring->array = ptr + p->sq_off.elems;
477 ld->sq_ring_mask = *sring->ring_mask;
478
479 ld->mmap[1].len = p->sq_entries * sizeof(struct iocb);
480 ld->iocbs = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
481 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
482 IORING_OFF_IOCB);
483 ld->mmap[1].ptr = ld->iocbs;
484
485 ld->mmap[2].len = p->cq_off.elems +
486 p->cq_entries * sizeof(struct io_event);
487 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
488 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
489 IORING_OFF_CQ_RING);
490 ld->mmap[2].ptr = ptr;
491 cring->head = ptr + p->cq_off.head;
492 cring->tail = ptr + p->cq_off.tail;
493 cring->ring_mask = ptr + p->cq_off.ring_mask;
494 cring->ring_entries = ptr + p->cq_off.ring_entries;
495 cring->events = ptr + p->cq_off.elems;
496 ld->cq_ring_mask = *cring->ring_mask;
497 return 0;
498}
499
52885fa2
JA
500static int fio_aioring_queue_init(struct thread_data *td)
501{
52885fa2
JA
502 struct aioring_data *ld = td->io_ops_data;
503 struct aioring_options *o = td->eo;
52885fa2 504 int depth = td->o.iodepth;
9a2d78b3
JA
505 struct aio_uring_params p;
506 int ret;
507
508 memset(&p, 0, sizeof(p));
509 p.flags = IOCTX_FLAG_SCQRING;
52885fa2
JA
510
511 if (o->hipri)
9a2d78b3 512 p.flags |= IOCTX_FLAG_IOPOLL;
a90cd050 513 if (o->sqthread_set) {
9a2d78b3
JA
514 p.sq_thread_cpu = o->sqthread;
515 p.flags |= IOCTX_FLAG_SQTHREAD;
771c9901 516 if (o->sqthread_poll)
9a2d78b3 517 p.flags |= IOCTX_FLAG_SQPOLL;
f635f1fb
JA
518 }
519 if (o->sqwq)
9a2d78b3 520 p.flags |= IOCTX_FLAG_SQWQ;
a90cd050 521
52885fa2
JA
522 if (o->fixedbufs) {
523 struct rlimit rlim = {
524 .rlim_cur = RLIM_INFINITY,
525 .rlim_max = RLIM_INFINITY,
526 };
527
528 setrlimit(RLIMIT_MEMLOCK, &rlim);
9a2d78b3 529 p.flags |= IOCTX_FLAG_FIXEDBUFS;
52885fa2
JA
530 }
531
9a2d78b3
JA
532 ret = syscall(__NR_sys_io_uring_setup, depth, ld->iovecs, &p);
533 if (ret < 0)
534 return ret;
535
536 ld->ring_fd = ret;
537 return fio_aioring_mmap(ld, &p);
52885fa2
JA
538}
539
540static int fio_aioring_post_init(struct thread_data *td)
541{
542 struct aioring_data *ld = td->io_ops_data;
543 struct aioring_options *o = td->eo;
544 struct io_u *io_u;
9a2d78b3 545 int err;
52885fa2
JA
546
547 if (o->fixedbufs) {
548 int i;
549
550 for (i = 0; i < td->o.iodepth; i++) {
9a2d78b3
JA
551 struct iovec *iov = &ld->iovecs[i];
552
52885fa2 553 io_u = ld->io_u_index[i];
9a2d78b3
JA
554 iov->iov_base = io_u->buf;
555 iov->iov_len = td_max_bs(td);
52885fa2
JA
556 }
557 }
558
559 err = fio_aioring_queue_init(td);
560 if (err) {
d63a472d 561 td_verror(td, errno, "io_queue_init");
52885fa2
JA
562 return 1;
563 }
564
565 return 0;
566}
567
9a2d78b3
JA
568static unsigned roundup_pow2(unsigned depth)
569{
570 return 1UL << __fls(depth - 1);
571}
572
52885fa2
JA
573static int fio_aioring_init(struct thread_data *td)
574{
575 struct aioring_data *ld;
576
52885fa2
JA
577 ld = calloc(1, sizeof(*ld));
578
b87aa01a
JA
579 /* ring depth must be a power-of-2 */
580 ld->iodepth = td->o.iodepth;
581 td->o.iodepth = roundup_pow2(td->o.iodepth);
582
52885fa2
JA
583 /* io_u index */
584 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
585 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
586
9a2d78b3 587 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
52885fa2
JA
588
589 td->io_ops_data = ld;
590 return 0;
591}
592
593static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
594{
595 struct aioring_data *ld = td->io_ops_data;
596
597 ld->io_u_index[io_u->index] = io_u;
598 return 0;
599}
600
601static struct ioengine_ops ioengine = {
602 .name = "aio-ring",
603 .version = FIO_IOOPS_VERSION,
604 .init = fio_aioring_init,
605 .post_init = fio_aioring_post_init,
606 .io_u_init = fio_aioring_io_u_init,
607 .prep = fio_aioring_prep,
608 .queue = fio_aioring_queue,
609 .commit = fio_aioring_commit,
610 .getevents = fio_aioring_getevents,
611 .event = fio_aioring_event,
612 .cleanup = fio_aioring_cleanup,
613 .open_file = generic_open_file,
614 .close_file = generic_close_file,
615 .get_file_size = generic_get_file_size,
616 .options = options,
617 .option_struct_size = sizeof(struct aioring_options),
618};
619
620static void fio_init fio_aioring_register(void)
621{
52885fa2 622 register_ioengine(&ioengine);
52885fa2
JA
623}
624
625static void fio_exit fio_aioring_unregister(void)
626{
52885fa2 627 unregister_ioengine(&ioengine);
52885fa2 628}
1f90e9bb 629#endif