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