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