backend: initialize io engine before io_u buffers
[fio.git] / engines / libaio.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * libaio engine
3 *
4 * IO engine using the Linux native aio interface.
2866c82d
JA
5 *
6 */
2866c82d
JA
7#include <stdlib.h>
8#include <unistd.h>
9#include <errno.h>
67bf9823 10#include <libaio.h>
5f350952
JA
11
12#include "../fio.h"
0f38bbef 13#include "../lib/pow2.h"
d220c761 14#include "../optgroup.h"
2866c82d 15
ee636f3f
JA
16#ifndef IOCB_FLAG_HIPRI
17#define IOCB_FLAG_HIPRI (1 << 2)
18#endif
ebec344d
JA
19#ifndef IOCTX_FLAG_IOPOLL
20#define IOCTX_FLAG_IOPOLL (1 << 0)
21#endif
a391d73d 22
0fc2e103
JA
23static int fio_libaio_commit(struct thread_data *td);
24
2866c82d
JA
25struct libaio_data {
26 io_context_t aio_ctx;
27 struct io_event *aio_events;
755200a3 28 struct iocb **iocbs;
7e77dd02 29 struct io_u **io_us;
acd45e02
JA
30
31 /*
32 * Basic ring buffer. 'head' is incremented in _queue(), and
33 * 'tail' is incremented in _commit(). We keep 'queued' so
34 * that we know if the ring is full or empty, when
35 * 'head' == 'tail'. 'entries' is the ring size, and
36 * 'is_pow2' is just an optimization to use AND instead of
37 * modulus to get the remainder on ring increment.
38 */
39 int is_pow2;
40 unsigned int entries;
41 unsigned int queued;
42 unsigned int head;
43 unsigned int tail;
2866c82d
JA
44};
45
de890a1e 46struct libaio_options {
a1f871c7 47 void *pad;
de890a1e 48 unsigned int userspace_reap;
a391d73d 49 unsigned int hipri;
de890a1e
SL
50};
51
52static struct fio_option options[] = {
53 {
54 .name = "userspace_reap",
e8b0e958 55 .lname = "Libaio userspace reaping",
de890a1e
SL
56 .type = FIO_OPT_STR_SET,
57 .off1 = offsetof(struct libaio_options, userspace_reap),
58 .help = "Use alternative user-space reap implementation",
e90a0adf 59 .category = FIO_OPT_C_ENGINE,
5a3cd5f3 60 .group = FIO_OPT_G_LIBAIO,
de890a1e 61 },
a391d73d
JA
62 {
63 .name = "hipri",
ee636f3f 64 .lname = "High Priority",
a391d73d
JA
65 .type = FIO_OPT_STR_SET,
66 .off1 = offsetof(struct libaio_options, hipri),
6acc46d8 67 .help = "Use polled IO completions",
a391d73d
JA
68 .category = FIO_OPT_C_ENGINE,
69 .group = FIO_OPT_G_LIBAIO,
70 },
de890a1e
SL
71 {
72 .name = NULL,
73 },
74};
75
acd45e02
JA
76static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
77 unsigned int add)
78{
79 if (ld->is_pow2)
80 *val = (*val + add) & (ld->entries - 1);
81 else
82 *val = (*val + add) % ld->entries;
83}
84
7a16dd02 85static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
2866c82d 86{
53cdc686 87 struct fio_file *f = io_u->file;
a391d73d 88 struct libaio_options *o = td->eo;
53cdc686 89
a391d73d 90 if (io_u->ddir == DDIR_READ) {
cec6b55d 91 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
a391d73d 92 if (o->hipri)
ee636f3f 93 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
a391d73d 94 } else if (io_u->ddir == DDIR_WRITE) {
cec6b55d 95 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
a391d73d 96 if (o->hipri)
ee636f3f 97 io_u->iocb.u.c.flags |= IOCB_FLAG_HIPRI;
a391d73d 98 } else if (ddir_sync(io_u->ddir))
87dc1ab1 99 io_prep_fsync(&io_u->iocb, f->fd);
2866c82d
JA
100
101 return 0;
102}
103
104static struct io_u *fio_libaio_event(struct thread_data *td, int event)
105{
565e784d 106 struct libaio_data *ld = td->io_ops_data;
f423479d
JA
107 struct io_event *ev;
108 struct io_u *io_u;
2866c82d 109
f423479d 110 ev = ld->aio_events + event;
0cc6ee59 111 io_u = container_of(ev->obj, struct io_u, iocb);
f423479d
JA
112
113 if (ev->res != io_u->xfer_buflen) {
114 if (ev->res > io_u->xfer_buflen)
115 io_u->error = -ev->res;
116 else
117 io_u->resid = io_u->xfer_buflen - ev->res;
118 } else
119 io_u->error = 0;
120
121 return io_u;
2866c82d
JA
122}
123
675012f0
DE
124struct aio_ring {
125 unsigned id; /** kernel internal index number */
126 unsigned nr; /** number of io_events */
127 unsigned head;
128 unsigned tail;
c44b1ff5 129
675012f0
DE
130 unsigned magic;
131 unsigned compat_features;
132 unsigned incompat_features;
133 unsigned header_length; /** size of aio_ring */
134
135 struct io_event events[0];
136};
137
138#define AIO_RING_MAGIC 0xa10a10a1
139
140static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
c44b1ff5 141 struct io_event *events)
675012f0
DE
142{
143 long i = 0;
144 unsigned head;
c44b1ff5 145 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
675012f0
DE
146
147 while (i < max) {
148 head = ring->head;
149
150 if (head == ring->tail) {
151 /* There are no more completions */
152 break;
153 } else {
154 /* There is another completion to reap */
155 events[i] = ring->events[head];
156 read_barrier();
c44b1ff5 157 ring->head = (head + 1) % ring->nr;
675012f0
DE
158 i++;
159 }
160 }
161
162 return i;
163}
164
e7d2e616 165static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
1f440ece 166 unsigned int max, const struct timespec *t)
2866c82d 167{
565e784d 168 struct libaio_data *ld = td->io_ops_data;
de890a1e 169 struct libaio_options *o = td->eo;
82407585 170 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
1f440ece 171 struct timespec __lt, *lt = NULL;
0b7fdba7 172 int r, events = 0;
2866c82d 173
1f440ece
JA
174 if (t) {
175 __lt = *t;
176 lt = &__lt;
177 }
178
2866c82d 179 do {
de890a1e 180 if (o->userspace_reap == 1
675012f0
DE
181 && actual_min == 0
182 && ((struct aio_ring *)(ld->aio_ctx))->magic
183 == AIO_RING_MAGIC) {
184 r = user_io_getevents(ld->aio_ctx, max,
185 ld->aio_events + events);
186 } else {
187 r = io_getevents(ld->aio_ctx, actual_min,
1f440ece 188 max, ld->aio_events + events, lt);
675012f0 189 }
3441a52d 190 if (r > 0)
0b7fdba7 191 events += r;
3441a52d 192 else if ((min && r == 0) || r == -EAGAIN) {
0fc2e103 193 fio_libaio_commit(td);
6347e43d
JA
194 if (actual_min)
195 usleep(10);
0fc2e103 196 } else if (r != -EINTR)
a31dc2dd 197 break;
0b7fdba7
DE
198 } while (events < min);
199
200 return r < 0 ? r : events;
2866c82d
JA
201}
202
2e4ef4fb
JA
203static enum fio_q_status fio_libaio_queue(struct thread_data *td,
204 struct io_u *io_u)
2866c82d 205{
565e784d 206 struct libaio_data *ld = td->io_ops_data;
2866c82d 207
7101d9c2
JA
208 fio_ro_check(td, io_u);
209
acd45e02 210 if (ld->queued == td->o.iodepth)
755200a3
JA
211 return FIO_Q_BUSY;
212
213 /*
214 * fsync is tricky, since it can fail and we need to do it
215 * serialized with other io. the reason is that linux doesn't
216 * support aio fsync yet. So return busy for the case where we
217 * have pending io, to let fio complete those first.
218 */
f011531e 219 if (ddir_sync(io_u->ddir)) {
acd45e02 220 if (ld->queued)
755200a3 221 return FIO_Q_BUSY;
5f9099ea 222
f011531e 223 do_io_u_sync(td, io_u);
755200a3
JA
224 return FIO_Q_COMPLETED;
225 }
226
a5f3027c 227 if (io_u->ddir == DDIR_TRIM) {
acd45e02 228 if (ld->queued)
a5f3027c
JA
229 return FIO_Q_BUSY;
230
231 do_io_u_trim(td, io_u);
c0681c9d
VF
232 io_u_mark_submit(td, 1);
233 io_u_mark_complete(td, 1);
a5f3027c
JA
234 return FIO_Q_COMPLETED;
235 }
236
acd45e02
JA
237 ld->iocbs[ld->head] = &io_u->iocb;
238 ld->io_us[ld->head] = io_u;
239 ring_inc(ld, &ld->head, 1);
240 ld->queued++;
755200a3
JA
241 return FIO_Q_QUEUED;
242}
243
7e77dd02
JA
244static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
245 unsigned int nr)
246{
8b6a404c 247 struct timespec now;
7e77dd02
JA
248 unsigned int i;
249
12d9d841
JA
250 if (!fio_fill_issue_time(td))
251 return;
252
7e77dd02
JA
253 fio_gettime(&now, NULL);
254
255 for (i = 0; i < nr; i++) {
256 struct io_u *io_u = io_us[i];
257
258 memcpy(&io_u->issue_time, &now, sizeof(now));
259 io_u_queued(td, io_u);
260 }
261}
262
755200a3
JA
263static int fio_libaio_commit(struct thread_data *td)
264{
565e784d 265 struct libaio_data *ld = td->io_ops_data;
755200a3 266 struct iocb **iocbs;
7e77dd02 267 struct io_u **io_us;
8b6a404c 268 struct timespec ts;
a120ca7f 269 int ret, wait_start = 0;
755200a3 270
acd45e02 271 if (!ld->queued)
755200a3
JA
272 return 0;
273
2866c82d 274 do {
acd45e02
JA
275 long nr = ld->queued;
276
277 nr = min((unsigned int) nr, ld->entries - ld->tail);
278 io_us = ld->io_us + ld->tail;
279 iocbs = ld->iocbs + ld->tail;
280
281 ret = io_submit(ld->aio_ctx, nr, iocbs);
5e00c2c4 282 if (ret > 0) {
7e77dd02 283 fio_libaio_queued(td, io_us, ret);
838bc709 284 io_u_mark_submit(td, ret);
acd45e02
JA
285
286 ld->queued -= ret;
287 ring_inc(ld, &ld->tail, ret);
5e00c2c4 288 ret = 0;
e3b4e568 289 wait_start = 0;
acd45e02 290 } else if (ret == -EINTR || !ret) {
838bc709
JA
291 if (!ret)
292 io_u_mark_submit(td, ret);
e3b4e568 293 wait_start = 0;
2866c82d 294 continue;
acd45e02
JA
295 } else if (ret == -EAGAIN) {
296 /*
297 * If we get EAGAIN, we should break out without
298 * error and let the upper layer reap some
a120ca7f
JA
299 * events for us. If we have no queued IO, we
300 * must loop here. If we loop for more than 30s,
301 * just error out, something must be buggy in the
302 * IO path.
acd45e02 303 */
a120ca7f
JA
304 if (ld->queued) {
305 ret = 0;
306 break;
307 }
308 if (!wait_start) {
8b6a404c 309 fio_gettime(&ts, NULL);
d36b072d 310 wait_start = 1;
8b6a404c 311 } else if (mtime_since_now(&ts) > 30000) {
a120ca7f
JA
312 log_err("fio: aio appears to be stalled, giving up\n");
313 break;
314 }
315 usleep(1);
316 continue;
a31dc2dd
JA
317 } else if (ret == -ENOMEM) {
318 /*
319 * If we get -ENOMEM, reap events if we can. If
320 * we cannot, treat it as a fatal event since there's
321 * nothing we can do about it.
322 */
323 if (ld->queued)
324 ret = 0;
325 break;
838bc709 326 } else
2866c82d 327 break;
2c3a4ae9 328 } while (ld->queued);
2866c82d 329
36167d82 330 return ret;
2866c82d
JA
331}
332
333static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
334{
565e784d 335 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
336
337 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
338}
339
340static void fio_libaio_cleanup(struct thread_data *td)
341{
565e784d 342 struct libaio_data *ld = td->io_ops_data;
2866c82d
JA
343
344 if (ld) {
f24c2649
JA
345 /*
346 * Work-around to avoid huge RCU stalls at exit time. If we
347 * don't do this here, then it'll be torn down by exit_aio().
348 * But for that case we can parallellize the freeing, thus
349 * speeding it up a lot.
350 */
351 if (!(td->flags & TD_F_CHILD))
352 io_destroy(ld->aio_ctx);
7e77dd02
JA
353 free(ld->aio_events);
354 free(ld->iocbs);
355 free(ld->io_us);
2866c82d 356 free(ld);
2866c82d
JA
357 }
358}
359
ebec344d
JA
360static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
361 bool hipri)
362{
363#ifdef __NR_sys_io_setup2
a1b006fe 364 int flags = 0;
ebec344d
JA
365
366 if (hipri)
367 flags = IOCTX_FLAG_IOPOLL;
368
369 return syscall(__NR_sys_io_setup2, depth, flags, &ld->aio_ctx);
370#else
371 if (hipri) {
372 log_err("fio: polled aio not available on your platform\n");
373 return 1;
374 }
375 return io_queue_init(depth, &ld->aio_ctx);
376#endif
377}
378
2866c82d
JA
379static int fio_libaio_init(struct thread_data *td)
380{
c90e1017 381 struct libaio_options *o = td->eo;
acd45e02 382 struct libaio_data *ld;
c90e1017 383 int err = 0;
2866c82d 384
acd45e02 385 ld = calloc(1, sizeof(*ld));
c1db2dce 386
c90e1017
JA
387 /*
388 * First try passing in 0 for queue depth, since we don't
389 * care about the user ring. If that fails, the kernel is too old
390 * and we need the right depth.
391 */
a1b006fe 392 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri);
c1db2dce
JA
393 if (err) {
394 td_verror(td, -err, "io_queue_init");
75de55ac 395 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
cb781c75 396 free(ld);
2866c82d
JA
397 return 1;
398 }
399
acd45e02
JA
400 ld->entries = td->o.iodepth;
401 ld->is_pow2 = is_power_of_2(ld->entries);
402 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
403 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
404 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
755200a3 405
565e784d 406 td->io_ops_data = ld;
2866c82d
JA
407 return 0;
408}
409
5f350952 410static struct ioengine_ops ioengine = {
de890a1e
SL
411 .name = "libaio",
412 .version = FIO_IOOPS_VERSION,
413 .init = fio_libaio_init,
414 .prep = fio_libaio_prep,
415 .queue = fio_libaio_queue,
416 .commit = fio_libaio_commit,
417 .cancel = fio_libaio_cancel,
418 .getevents = fio_libaio_getevents,
419 .event = fio_libaio_event,
420 .cleanup = fio_libaio_cleanup,
421 .open_file = generic_open_file,
422 .close_file = generic_close_file,
423 .get_file_size = generic_get_file_size,
424 .options = options,
425 .option_struct_size = sizeof(struct libaio_options),
2866c82d 426};
34cfcdaf 427
5f350952
JA
428static void fio_init fio_libaio_register(void)
429{
430 register_ioengine(&ioengine);
431}
432
433static void fio_exit fio_libaio_unregister(void)
434{
435 unregister_ioengine(&ioengine);
436}