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