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