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