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