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