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