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