engines/aio-ring: initialization error handling
[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
e3970057
JA
200 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
201 if (io_u->ddir == DDIR_READ)
52885fa2 202 iocb->aio_lio_opcode = IO_CMD_PREAD;
e3970057 203 else
52885fa2 204 iocb->aio_lio_opcode = IO_CMD_PWRITE;
e3970057
JA
205 iocb->aio_reqprio = 0;
206 iocb->aio_fildes = f->fd;
207 iocb->u.c.buf = io_u->xfer_buf;
208 iocb->u.c.nbytes = io_u->xfer_buflen;
209 iocb->u.c.offset = io_u->offset;
210 if (o->hipri)
211 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
212 else
213 iocb->u.c.flags = 0;
52885fa2
JA
214 } else if (ddir_sync(io_u->ddir))
215 io_prep_fsync(iocb, f->fd);
216
217 iocb->data = io_u;
218 return 0;
219}
220
221static struct io_u *fio_aioring_event(struct thread_data *td, int event)
222{
223 struct aioring_data *ld = td->io_ops_data;
224 struct io_event *ev;
225 struct io_u *io_u;
226 int index;
227
228 index = event + ld->cq_ring_off;
229 if (index >= ld->cq_ring->nr_events)
230 index -= ld->cq_ring->nr_events;
231
232 ev = &ld->cq_ring->events[index];
233 io_u = ev->data;
234
235 if (ev->res != io_u->xfer_buflen) {
236 if (ev->res > io_u->xfer_buflen)
237 io_u->error = -ev->res;
238 else
239 io_u->resid = io_u->xfer_buflen - ev->res;
240 } else
241 io_u->error = 0;
242
96563db9
JA
243 if (io_u->ddir == DDIR_READ) {
244 if (ev->res2 & IOEV_RES2_CACHEHIT)
245 ld->cachehit++;
246 else
247 ld->cachemiss++;
248 }
249
52885fa2
JA
250 return io_u;
251}
252
253static int fio_aioring_cqring_reap(struct thread_data *td, unsigned int events,
254 unsigned int max)
255{
256 struct aioring_data *ld = td->io_ops_data;
257 struct aio_cq_ring *ring = ld->cq_ring;
258 u32 head, reaped = 0;
259
260 head = ring->head;
261 do {
262 read_barrier();
263 if (head == ring->tail)
264 break;
265 reaped++;
266 head++;
267 if (head == ring->nr_events)
268 head = 0;
269 } while (reaped + events < max);
270
271 ring->head = head;
272 write_barrier();
273 return reaped;
274}
275
276static int fio_aioring_getevents(struct thread_data *td, unsigned int min,
277 unsigned int max, const struct timespec *t)
278{
279 struct aioring_data *ld = td->io_ops_data;
280 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
771c9901 281 struct aioring_options *o = td->eo;
52885fa2
JA
282 struct aio_cq_ring *ring = ld->cq_ring;
283 int r, events = 0;
284
285 ld->cq_ring_off = ring->head;
286 do {
287 r = fio_aioring_cqring_reap(td, events, max);
288 if (r) {
289 events += r;
290 continue;
291 }
292
771c9901
JA
293 if (!o->sqthread_poll) {
294 r = io_ring_enter(ld->aio_ctx, 0, actual_min,
295 IORING_FLAG_GETEVENTS);
296 if (r < 0) {
297 if (errno == EAGAIN)
298 continue;
299 td_verror(td, errno, "io_ring_enter get");
300 break;
301 }
52885fa2
JA
302 }
303 } while (events < min);
304
305 return r < 0 ? r : events;
306}
307
308static enum fio_q_status fio_aioring_queue(struct thread_data *td,
309 struct io_u *io_u)
310{
311 struct aioring_data *ld = td->io_ops_data;
312 struct aio_sq_ring *ring = ld->sq_ring;
313 unsigned tail, next_tail;
314
315 fio_ro_check(td, io_u);
316
317 if (ld->queued == td->o.iodepth)
318 return FIO_Q_BUSY;
319
52885fa2
JA
320 if (io_u->ddir == DDIR_TRIM) {
321 if (ld->queued)
322 return FIO_Q_BUSY;
323
324 do_io_u_trim(td, io_u);
325 io_u_mark_submit(td, 1);
326 io_u_mark_complete(td, 1);
327 return FIO_Q_COMPLETED;
328 }
329
330 tail = ring->tail;
331 next_tail = tail + 1;
332 if (next_tail == ring->nr_events)
333 next_tail = 0;
334 read_barrier();
335 if (next_tail == ring->head)
336 return FIO_Q_BUSY;
337
338 ring->array[tail] = io_u->index;
339 ring->tail = next_tail;
340 write_barrier();
341
342 ld->queued++;
343 return FIO_Q_QUEUED;
344}
345
346static void fio_aioring_queued(struct thread_data *td, int start, int nr)
347{
348 struct aioring_data *ld = td->io_ops_data;
349 struct timespec now;
350
351 if (!fio_fill_issue_time(td))
352 return;
353
354 fio_gettime(&now, NULL);
355
356 while (nr--) {
357 int index = ld->sq_ring->array[start];
358 struct io_u *io_u = io_u = ld->io_u_index[index];
359
360 memcpy(&io_u->issue_time, &now, sizeof(now));
361 io_u_queued(td, io_u);
362
363 start++;
364 if (start == ld->sq_ring->nr_events)
365 start = 0;
366 }
367}
368
369static int fio_aioring_commit(struct thread_data *td)
370{
371 struct aioring_data *ld = td->io_ops_data;
771c9901 372 struct aioring_options *o = td->eo;
52885fa2
JA
373 int ret;
374
375 if (!ld->queued)
376 return 0;
377
771c9901
JA
378 /* Nothing to do */
379 if (o->sqthread_poll) {
4cdbc048
JA
380 struct aio_sq_ring *ring = ld->sq_ring;
381
382 if (ring->kflags & IORING_SQ_NEED_WAKEUP)
383 io_ring_enter(ld->aio_ctx, ld->queued, 0, IORING_FLAG_SUBMIT);
771c9901
JA
384 ld->queued = 0;
385 return 0;
386 }
387
52885fa2
JA
388 do {
389 int start = ld->sq_ring->head;
390 long nr = ld->queued;
391
392 ret = io_ring_enter(ld->aio_ctx, nr, 0, IORING_FLAG_SUBMIT |
393 IORING_FLAG_GETEVENTS);
52885fa2
JA
394 if (ret > 0) {
395 fio_aioring_queued(td, start, ret);
396 io_u_mark_submit(td, ret);
397
398 ld->queued -= ret;
399 ret = 0;
a90cd050
JA
400 } else if (!ret) {
401 io_u_mark_submit(td, ret);
52885fa2 402 continue;
a90cd050
JA
403 } else {
404 if (errno == EAGAIN) {
405 ret = fio_aioring_cqring_reap(td, 0, ld->queued);
406 if (ret)
407 continue;
408 /* Shouldn't happen */
409 usleep(1);
410 continue;
52885fa2 411 }
a90cd050 412 td_verror(td, errno, "io_ring_enter sumit");
52885fa2 413 break;
a90cd050 414 }
52885fa2
JA
415 } while (ld->queued);
416
417 return ret;
418}
419
420static size_t aioring_cq_size(struct thread_data *td)
421{
422 return sizeof(struct aio_cq_ring) + 2 * td->o.iodepth * sizeof(struct io_event);
423}
424
425static size_t aioring_sq_iocb(struct thread_data *td)
426{
427 return sizeof(struct iocb) * td->o.iodepth;
428}
429
430static size_t aioring_sq_size(struct thread_data *td)
431{
432 return sizeof(struct aio_sq_ring) + td->o.iodepth * sizeof(u32);
433}
434
435static void fio_aioring_cleanup(struct thread_data *td)
436{
437 struct aioring_data *ld = td->io_ops_data;
438
439 if (ld) {
96563db9
JA
440 td->ts.cachehit += ld->cachehit;
441 td->ts.cachemiss += ld->cachemiss;
442
a90cd050
JA
443 /* Bump depth to match init depth */
444 td->o.iodepth++;
445
52885fa2
JA
446 /*
447 * Work-around to avoid huge RCU stalls at exit time. If we
448 * don't do this here, then it'll be torn down by exit_aio().
449 * But for that case we can parallellize the freeing, thus
450 * speeding it up a lot.
451 */
452 if (!(td->flags & TD_F_CHILD))
453 io_destroy(ld->aio_ctx);
454 free(ld->io_u_index);
455 free(ld->io_us);
456 fio_memfree(ld->sq_ring, aioring_sq_size(td), false);
457 fio_memfree(ld->iocbs, aioring_sq_iocb(td), false);
458 fio_memfree(ld->cq_ring, aioring_cq_size(td), false);
459 free(ld);
460 }
461}
462
463static int fio_aioring_queue_init(struct thread_data *td)
464{
52885fa2
JA
465 struct aioring_data *ld = td->io_ops_data;
466 struct aioring_options *o = td->eo;
467 int flags = IOCTX_FLAG_SCQRING;
468 int depth = td->o.iodepth;
469
470 if (o->hipri)
471 flags |= IOCTX_FLAG_IOPOLL;
a90cd050
JA
472 if (o->sqthread_set) {
473 ld->sq_ring->sq_thread_cpu = o->sqthread;
474 flags |= IOCTX_FLAG_SQTHREAD;
771c9901
JA
475 if (o->sqthread_poll)
476 flags |= IOCTX_FLAG_SQPOLL;
f635f1fb
JA
477 }
478 if (o->sqwq)
a90cd050
JA
479 flags |= IOCTX_FLAG_SQWQ;
480
52885fa2
JA
481 if (o->fixedbufs) {
482 struct rlimit rlim = {
483 .rlim_cur = RLIM_INFINITY,
484 .rlim_max = RLIM_INFINITY,
485 };
486
487 setrlimit(RLIMIT_MEMLOCK, &rlim);
488 flags |= IOCTX_FLAG_FIXEDBUFS;
489 }
490
491 return syscall(__NR_sys_io_setup2, depth, flags,
492 ld->sq_ring, ld->cq_ring, &ld->aio_ctx);
52885fa2
JA
493}
494
495static int fio_aioring_post_init(struct thread_data *td)
496{
497 struct aioring_data *ld = td->io_ops_data;
498 struct aioring_options *o = td->eo;
499 struct io_u *io_u;
500 struct iocb *iocb;
501 int err = 0;
502
503 if (o->fixedbufs) {
504 int i;
505
506 for (i = 0; i < td->o.iodepth; i++) {
507 io_u = ld->io_u_index[i];
508 iocb = &ld->iocbs[i];
509 iocb->u.c.buf = io_u->buf;
510 iocb->u.c.nbytes = td_max_bs(td);
511
512 if (o->hipri)
513 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
514 }
515 }
516
517 err = fio_aioring_queue_init(td);
d63a472d
JA
518
519 /* Adjust depth back again */
520 td->o.iodepth--;
521
52885fa2 522 if (err) {
d63a472d 523 td_verror(td, errno, "io_queue_init");
52885fa2
JA
524 return 1;
525 }
526
527 return 0;
528}
529
530static int fio_aioring_init(struct thread_data *td)
531{
532 struct aioring_data *ld;
533
3b617146
JA
534 /* ring needs an extra entry, add one to achieve QD set */
535 td->o.iodepth++;
52885fa2
JA
536
537 ld = calloc(1, sizeof(*ld));
538
539 /* io_u index */
540 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
541 ld->io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
542
543 ld->iocbs = fio_memalign(page_size, aioring_sq_iocb(td), false);
544 memset(ld->iocbs, 0, aioring_sq_iocb(td));
545
546 ld->sq_ring = fio_memalign(page_size, aioring_sq_size(td), false);
547 memset(ld->sq_ring, 0, aioring_sq_size(td));
548 ld->sq_ring->nr_events = td->o.iodepth;
1f90e9bb 549 ld->sq_ring->iocbs = (u64) (uintptr_t) ld->iocbs;
52885fa2
JA
550
551 ld->cq_ring = fio_memalign(page_size, aioring_cq_size(td), false);
552 memset(ld->cq_ring, 0, aioring_cq_size(td));
553 ld->cq_ring->nr_events = td->o.iodepth * 2;
554
555 td->io_ops_data = ld;
556 return 0;
557}
558
559static int fio_aioring_io_u_init(struct thread_data *td, struct io_u *io_u)
560{
561 struct aioring_data *ld = td->io_ops_data;
562
563 ld->io_u_index[io_u->index] = io_u;
564 return 0;
565}
566
567static struct ioengine_ops ioengine = {
568 .name = "aio-ring",
569 .version = FIO_IOOPS_VERSION,
570 .init = fio_aioring_init,
571 .post_init = fio_aioring_post_init,
572 .io_u_init = fio_aioring_io_u_init,
573 .prep = fio_aioring_prep,
574 .queue = fio_aioring_queue,
575 .commit = fio_aioring_commit,
576 .getevents = fio_aioring_getevents,
577 .event = fio_aioring_event,
578 .cleanup = fio_aioring_cleanup,
579 .open_file = generic_open_file,
580 .close_file = generic_close_file,
581 .get_file_size = generic_get_file_size,
582 .options = options,
583 .option_struct_size = sizeof(struct aioring_options),
584};
585
586static void fio_init fio_aioring_register(void)
587{
52885fa2 588 register_ioengine(&ioengine);
52885fa2
JA
589}
590
591static void fio_exit fio_aioring_unregister(void)
592{
52885fa2 593 unregister_ioengine(&ioengine);
52885fa2 594}
1f90e9bb 595#endif