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