Commit | Line | Data |
---|---|---|
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" |
e9f6567a | 18 | #include "cmdprio.h" |
2866c82d | 19 | |
b2a432bf PC |
20 | /* Should be defined in newest aio_abi.h */ |
21 | #ifndef IOCB_FLAG_IOPRIO | |
22 | #define IOCB_FLAG_IOPRIO (1 << 1) | |
23 | #endif | |
24 | ||
7d42e66e KK |
25 | /* Hack for libaio < 0.3.111 */ |
26 | #ifndef CONFIG_LIBAIO_RW_FLAGS | |
27 | #define aio_rw_flags __pad2 | |
28 | #endif | |
29 | ||
0fc2e103 | 30 | static int fio_libaio_commit(struct thread_data *td); |
b2a432bf | 31 | static int fio_libaio_init(struct thread_data *td); |
0fc2e103 | 32 | |
2866c82d JA |
33 | struct libaio_data { |
34 | io_context_t aio_ctx; | |
35 | struct io_event *aio_events; | |
755200a3 | 36 | struct iocb **iocbs; |
7e77dd02 | 37 | struct io_u **io_us; |
acd45e02 | 38 | |
3c3168e9 | 39 | struct io_u **io_u_index; |
1ceaf63b | 40 | struct iovec *iovecs; /* for vectored requests */ |
3c3168e9 | 41 | |
acd45e02 JA |
42 | /* |
43 | * Basic ring buffer. 'head' is incremented in _queue(), and | |
44 | * 'tail' is incremented in _commit(). We keep 'queued' so | |
45 | * that we know if the ring is full or empty, when | |
46 | * 'head' == 'tail'. 'entries' is the ring size, and | |
47 | * 'is_pow2' is just an optimization to use AND instead of | |
48 | * modulus to get the remainder on ring increment. | |
49 | */ | |
50 | int is_pow2; | |
51 | unsigned int entries; | |
52 | unsigned int queued; | |
53 | unsigned int head; | |
54 | unsigned int tail; | |
d6cbeab4 NC |
55 | |
56 | struct cmdprio cmdprio; | |
2866c82d JA |
57 | }; |
58 | ||
de890a1e | 59 | struct libaio_options { |
a48f0cc7 | 60 | struct thread_data *td; |
de890a1e | 61 | unsigned int userspace_reap; |
d6cbeab4 | 62 | struct cmdprio_options cmdprio_options; |
7d42e66e | 63 | unsigned int nowait; |
1ceaf63b | 64 | unsigned int vectored; |
de890a1e SL |
65 | }; |
66 | ||
67 | static struct fio_option options[] = { | |
68 | { | |
69 | .name = "userspace_reap", | |
e8b0e958 | 70 | .lname = "Libaio userspace reaping", |
de890a1e SL |
71 | .type = FIO_OPT_STR_SET, |
72 | .off1 = offsetof(struct libaio_options, userspace_reap), | |
73 | .help = "Use alternative user-space reap implementation", | |
e90a0adf | 74 | .category = FIO_OPT_C_ENGINE, |
5a3cd5f3 | 75 | .group = FIO_OPT_G_LIBAIO, |
de890a1e | 76 | }, |
7d42e66e KK |
77 | { |
78 | .name = "nowait", | |
79 | .lname = "RWF_NOWAIT", | |
80 | .type = FIO_OPT_BOOL, | |
81 | .off1 = offsetof(struct libaio_options, nowait), | |
82 | .help = "Set RWF_NOWAIT for reads/writes", | |
83 | .category = FIO_OPT_C_ENGINE, | |
84 | .group = FIO_OPT_G_LIBAIO, | |
85 | }, | |
1ceaf63b RHI |
86 | { |
87 | .name = "libaio_vectored", | |
88 | .lname = "Use libaio preadv,pwritev", | |
89 | .type = FIO_OPT_BOOL, | |
90 | .off1 = offsetof(struct libaio_options, vectored), | |
91 | .help = "Use libaio {preadv,pwritev} instead of libaio {pread,pwrite}", | |
92 | .category = FIO_OPT_C_ENGINE, | |
93 | .group = FIO_OPT_G_LIBAIO, | |
94 | }, | |
95 | ||
2838f77a | 96 | CMDPRIO_OPTIONS(struct libaio_options, FIO_OPT_G_LIBAIO), |
de890a1e SL |
97 | { |
98 | .name = NULL, | |
99 | }, | |
100 | }; | |
101 | ||
acd45e02 JA |
102 | static inline void ring_inc(struct libaio_data *ld, unsigned int *val, |
103 | unsigned int add) | |
104 | { | |
105 | if (ld->is_pow2) | |
106 | *val = (*val + add) & (ld->entries - 1); | |
107 | else | |
108 | *val = (*val + add) % ld->entries; | |
109 | } | |
110 | ||
7d42e66e | 111 | static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u) |
2866c82d | 112 | { |
7d42e66e | 113 | struct libaio_options *o = td->eo; |
53cdc686 | 114 | struct fio_file *f = io_u->file; |
ad4e9298 | 115 | struct iocb *iocb = &io_u->iocb; |
1ceaf63b | 116 | struct libaio_data *ld = td->io_ops_data; |
53cdc686 | 117 | |
a391d73d | 118 | if (io_u->ddir == DDIR_READ) { |
1ceaf63b RHI |
119 | if (o->vectored) { |
120 | struct iovec *iov = &ld->iovecs[io_u->index]; | |
121 | ||
122 | iov->iov_base = io_u->xfer_buf; | |
123 | iov->iov_len = (size_t)io_u->xfer_buflen; | |
124 | io_prep_preadv(iocb, f->fd, iov, 1, io_u->offset); | |
125 | } else { | |
126 | io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, | |
127 | io_u->offset); | |
128 | } | |
7d42e66e KK |
129 | if (o->nowait) |
130 | iocb->aio_rw_flags |= RWF_NOWAIT; | |
a391d73d | 131 | } else if (io_u->ddir == DDIR_WRITE) { |
1ceaf63b RHI |
132 | if (o->vectored) { |
133 | struct iovec *iov = &ld->iovecs[io_u->index]; | |
134 | ||
135 | iov->iov_base = io_u->xfer_buf; | |
136 | iov->iov_len = (size_t)io_u->xfer_buflen; | |
137 | io_prep_pwritev(iocb, f->fd, iov, 1, io_u->offset); | |
138 | } else { | |
139 | io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, | |
140 | io_u->offset); | |
141 | } | |
7d42e66e KK |
142 | if (o->nowait) |
143 | iocb->aio_rw_flags |= RWF_NOWAIT; | |
a79319fe JG |
144 | #ifdef FIO_HAVE_RWF_ATOMIC |
145 | if (td->o.oatomic) | |
146 | iocb->aio_rw_flags |= RWF_ATOMIC; | |
147 | #endif | |
a391d73d | 148 | } else if (ddir_sync(io_u->ddir)) |
3c3168e9 | 149 | io_prep_fsync(iocb, f->fd); |
2866c82d JA |
150 | |
151 | return 0; | |
152 | } | |
153 | ||
127715b6 NC |
154 | static inline void fio_libaio_cmdprio_prep(struct thread_data *td, |
155 | struct io_u *io_u) | |
b2a432bf | 156 | { |
d6cbeab4 NC |
157 | struct libaio_data *ld = td->io_ops_data; |
158 | struct cmdprio *cmdprio = &ld->cmdprio; | |
127715b6 NC |
159 | |
160 | if (fio_cmdprio_set_ioprio(td, cmdprio, io_u)) { | |
161 | io_u->iocb.aio_reqprio = io_u->ioprio; | |
b2a432bf | 162 | io_u->iocb.u.c.flags |= IOCB_FLAG_IOPRIO; |
b2a432bf | 163 | } |
b2a432bf PC |
164 | } |
165 | ||
2866c82d JA |
166 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) |
167 | { | |
565e784d | 168 | struct libaio_data *ld = td->io_ops_data; |
f423479d JA |
169 | struct io_event *ev; |
170 | struct io_u *io_u; | |
2866c82d | 171 | |
f423479d | 172 | ev = ld->aio_events + event; |
702906e9 | 173 | io_u = container_of(ev->obj, struct io_u, iocb); |
f423479d JA |
174 | |
175 | if (ev->res != io_u->xfer_buflen) { | |
176 | if (ev->res > io_u->xfer_buflen) | |
177 | io_u->error = -ev->res; | |
178 | else | |
179 | io_u->resid = io_u->xfer_buflen - ev->res; | |
180 | } else | |
181 | io_u->error = 0; | |
182 | ||
183 | return io_u; | |
2866c82d JA |
184 | } |
185 | ||
675012f0 DE |
186 | struct aio_ring { |
187 | unsigned id; /** kernel internal index number */ | |
188 | unsigned nr; /** number of io_events */ | |
189 | unsigned head; | |
190 | unsigned tail; | |
c44b1ff5 | 191 | |
675012f0 DE |
192 | unsigned magic; |
193 | unsigned compat_features; | |
194 | unsigned incompat_features; | |
195 | unsigned header_length; /** size of aio_ring */ | |
196 | ||
197 | struct io_event events[0]; | |
198 | }; | |
199 | ||
200 | #define AIO_RING_MAGIC 0xa10a10a1 | |
201 | ||
202 | static int user_io_getevents(io_context_t aio_ctx, unsigned int max, | |
c44b1ff5 | 203 | struct io_event *events) |
675012f0 DE |
204 | { |
205 | long i = 0; | |
206 | unsigned head; | |
c44b1ff5 | 207 | struct aio_ring *ring = (struct aio_ring*) aio_ctx; |
675012f0 DE |
208 | |
209 | while (i < max) { | |
210 | head = ring->head; | |
211 | ||
212 | if (head == ring->tail) { | |
213 | /* There are no more completions */ | |
214 | break; | |
215 | } else { | |
216 | /* There is another completion to reap */ | |
217 | events[i] = ring->events[head]; | |
d473a06d BVA |
218 | atomic_store_release(&ring->head, |
219 | (head + 1) % ring->nr); | |
675012f0 DE |
220 | i++; |
221 | } | |
222 | } | |
223 | ||
224 | return i; | |
225 | } | |
226 | ||
e7d2e616 | 227 | static int fio_libaio_getevents(struct thread_data *td, unsigned int min, |
1f440ece | 228 | unsigned int max, const struct timespec *t) |
2866c82d | 229 | { |
565e784d | 230 | struct libaio_data *ld = td->io_ops_data; |
de890a1e | 231 | struct libaio_options *o = td->eo; |
82407585 | 232 | unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min; |
1f440ece | 233 | struct timespec __lt, *lt = NULL; |
0b7fdba7 | 234 | int r, events = 0; |
2866c82d | 235 | |
1f440ece JA |
236 | if (t) { |
237 | __lt = *t; | |
238 | lt = &__lt; | |
239 | } | |
240 | ||
2866c82d | 241 | do { |
de890a1e | 242 | if (o->userspace_reap == 1 |
675012f0 DE |
243 | && actual_min == 0 |
244 | && ((struct aio_ring *)(ld->aio_ctx))->magic | |
245 | == AIO_RING_MAGIC) { | |
a8f6714e | 246 | r = user_io_getevents(ld->aio_ctx, max - events, |
675012f0 DE |
247 | ld->aio_events + events); |
248 | } else { | |
249 | r = io_getevents(ld->aio_ctx, actual_min, | |
a8f6714e | 250 | max - events, ld->aio_events + events, lt); |
675012f0 | 251 | } |
a8f6714e | 252 | if (r > 0) { |
0b7fdba7 | 253 | events += r; |
a51fe82b | 254 | actual_min -= min((unsigned int)events, actual_min); |
a8f6714e | 255 | } |
3441a52d | 256 | else if ((min && r == 0) || r == -EAGAIN) { |
0fc2e103 | 257 | fio_libaio_commit(td); |
6347e43d JA |
258 | if (actual_min) |
259 | usleep(10); | |
0fc2e103 | 260 | } else if (r != -EINTR) |
a31dc2dd | 261 | break; |
0b7fdba7 DE |
262 | } while (events < min); |
263 | ||
264 | return r < 0 ? r : events; | |
2866c82d JA |
265 | } |
266 | ||
2e4ef4fb JA |
267 | static enum fio_q_status fio_libaio_queue(struct thread_data *td, |
268 | struct io_u *io_u) | |
2866c82d | 269 | { |
565e784d | 270 | struct libaio_data *ld = td->io_ops_data; |
2866c82d | 271 | |
7101d9c2 JA |
272 | fio_ro_check(td, io_u); |
273 | ||
acd45e02 | 274 | if (ld->queued == td->o.iodepth) |
755200a3 JA |
275 | return FIO_Q_BUSY; |
276 | ||
277 | /* | |
278 | * fsync is tricky, since it can fail and we need to do it | |
279 | * serialized with other io. the reason is that linux doesn't | |
280 | * support aio fsync yet. So return busy for the case where we | |
281 | * have pending io, to let fio complete those first. | |
282 | */ | |
f011531e | 283 | if (ddir_sync(io_u->ddir)) { |
acd45e02 | 284 | if (ld->queued) |
755200a3 | 285 | return FIO_Q_BUSY; |
5f9099ea | 286 | |
f011531e | 287 | do_io_u_sync(td, io_u); |
755200a3 JA |
288 | return FIO_Q_COMPLETED; |
289 | } | |
290 | ||
a5f3027c | 291 | if (io_u->ddir == DDIR_TRIM) { |
acd45e02 | 292 | if (ld->queued) |
a5f3027c JA |
293 | return FIO_Q_BUSY; |
294 | ||
295 | do_io_u_trim(td, io_u); | |
c0681c9d VF |
296 | io_u_mark_submit(td, 1); |
297 | io_u_mark_complete(td, 1); | |
a5f3027c JA |
298 | return FIO_Q_COMPLETED; |
299 | } | |
300 | ||
d6cbeab4 | 301 | if (ld->cmdprio.mode != CMDPRIO_MODE_NONE) |
ff00f247 | 302 | fio_libaio_cmdprio_prep(td, io_u); |
b2a432bf | 303 | |
702906e9 | 304 | ld->iocbs[ld->head] = &io_u->iocb; |
acd45e02 JA |
305 | ld->io_us[ld->head] = io_u; |
306 | ring_inc(ld, &ld->head, 1); | |
307 | ld->queued++; | |
755200a3 JA |
308 | return FIO_Q_QUEUED; |
309 | } | |
310 | ||
7e77dd02 JA |
311 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
312 | unsigned int nr) | |
313 | { | |
8b6a404c | 314 | struct timespec now; |
7e77dd02 JA |
315 | unsigned int i; |
316 | ||
12d9d841 JA |
317 | if (!fio_fill_issue_time(td)) |
318 | return; | |
319 | ||
7e77dd02 JA |
320 | fio_gettime(&now, NULL); |
321 | ||
322 | for (i = 0; i < nr; i++) { | |
323 | struct io_u *io_u = io_us[i]; | |
324 | ||
325 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
326 | io_u_queued(td, io_u); | |
327 | } | |
39f56400 VF |
328 | |
329 | /* | |
330 | * only used for iolog | |
331 | */ | |
332 | if (td->o.read_iolog_file) | |
333 | memcpy(&td->last_issue, &now, sizeof(now)); | |
7e77dd02 JA |
334 | } |
335 | ||
755200a3 JA |
336 | static int fio_libaio_commit(struct thread_data *td) |
337 | { | |
565e784d | 338 | struct libaio_data *ld = td->io_ops_data; |
755200a3 | 339 | struct iocb **iocbs; |
7e77dd02 | 340 | struct io_u **io_us; |
8b6a404c | 341 | struct timespec ts; |
a120ca7f | 342 | int ret, wait_start = 0; |
755200a3 | 343 | |
acd45e02 | 344 | if (!ld->queued) |
755200a3 JA |
345 | return 0; |
346 | ||
2866c82d | 347 | do { |
acd45e02 JA |
348 | long nr = ld->queued; |
349 | ||
350 | nr = min((unsigned int) nr, ld->entries - ld->tail); | |
351 | io_us = ld->io_us + ld->tail; | |
352 | iocbs = ld->iocbs + ld->tail; | |
353 | ||
354 | ret = io_submit(ld->aio_ctx, nr, iocbs); | |
5e00c2c4 | 355 | if (ret > 0) { |
7e77dd02 | 356 | fio_libaio_queued(td, io_us, ret); |
838bc709 | 357 | io_u_mark_submit(td, ret); |
acd45e02 JA |
358 | |
359 | ld->queued -= ret; | |
360 | ring_inc(ld, &ld->tail, ret); | |
5e00c2c4 | 361 | ret = 0; |
e3b4e568 | 362 | wait_start = 0; |
acd45e02 | 363 | } else if (ret == -EINTR || !ret) { |
838bc709 JA |
364 | if (!ret) |
365 | io_u_mark_submit(td, ret); | |
e3b4e568 | 366 | wait_start = 0; |
2866c82d | 367 | continue; |
acd45e02 JA |
368 | } else if (ret == -EAGAIN) { |
369 | /* | |
370 | * If we get EAGAIN, we should break out without | |
371 | * error and let the upper layer reap some | |
a120ca7f JA |
372 | * events for us. If we have no queued IO, we |
373 | * must loop here. If we loop for more than 30s, | |
374 | * just error out, something must be buggy in the | |
375 | * IO path. | |
acd45e02 | 376 | */ |
a120ca7f JA |
377 | if (ld->queued) { |
378 | ret = 0; | |
379 | break; | |
380 | } | |
381 | if (!wait_start) { | |
8b6a404c | 382 | fio_gettime(&ts, NULL); |
d36b072d | 383 | wait_start = 1; |
8b6a404c | 384 | } else if (mtime_since_now(&ts) > 30000) { |
a120ca7f JA |
385 | log_err("fio: aio appears to be stalled, giving up\n"); |
386 | break; | |
387 | } | |
388 | usleep(1); | |
389 | continue; | |
a31dc2dd JA |
390 | } else if (ret == -ENOMEM) { |
391 | /* | |
392 | * If we get -ENOMEM, reap events if we can. If | |
393 | * we cannot, treat it as a fatal event since there's | |
394 | * nothing we can do about it. | |
395 | */ | |
396 | if (ld->queued) | |
397 | ret = 0; | |
398 | break; | |
838bc709 | 399 | } else |
2866c82d | 400 | break; |
2c3a4ae9 | 401 | } while (ld->queued); |
2866c82d | 402 | |
36167d82 | 403 | return ret; |
2866c82d JA |
404 | } |
405 | ||
406 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) | |
407 | { | |
565e784d | 408 | struct libaio_data *ld = td->io_ops_data; |
2866c82d JA |
409 | |
410 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); | |
411 | } | |
412 | ||
413 | static void fio_libaio_cleanup(struct thread_data *td) | |
414 | { | |
565e784d | 415 | struct libaio_data *ld = td->io_ops_data; |
2866c82d JA |
416 | |
417 | if (ld) { | |
f24c2649 JA |
418 | /* |
419 | * Work-around to avoid huge RCU stalls at exit time. If we | |
420 | * don't do this here, then it'll be torn down by exit_aio(). | |
421 | * But for that case we can parallellize the freeing, thus | |
422 | * speeding it up a lot. | |
423 | */ | |
424 | if (!(td->flags & TD_F_CHILD)) | |
425 | io_destroy(ld->aio_ctx); | |
d6cbeab4 NC |
426 | |
427 | fio_cmdprio_cleanup(&ld->cmdprio); | |
1ceaf63b | 428 | free(ld->iovecs); |
7e77dd02 JA |
429 | free(ld->aio_events); |
430 | free(ld->iocbs); | |
431 | free(ld->io_us); | |
2866c82d | 432 | free(ld); |
2866c82d JA |
433 | } |
434 | } | |
435 | ||
2041bd34 JA |
436 | static int fio_libaio_post_init(struct thread_data *td) |
437 | { | |
438 | struct libaio_data *ld = td->io_ops_data; | |
ad4e9298 | 439 | int err; |
2041bd34 | 440 | |
ad4e9298 | 441 | err = io_queue_init(td->o.iodepth, &ld->aio_ctx); |
2041bd34 JA |
442 | if (err) { |
443 | td_verror(td, -err, "io_queue_init"); | |
444 | return 1; | |
445 | } | |
446 | ||
447 | return 0; | |
ebec344d JA |
448 | } |
449 | ||
2866c82d JA |
450 | static int fio_libaio_init(struct thread_data *td) |
451 | { | |
acd45e02 | 452 | struct libaio_data *ld; |
b2a432bf | 453 | struct libaio_options *o = td->eo; |
e9f6567a | 454 | int ret; |
2866c82d | 455 | |
acd45e02 | 456 | ld = calloc(1, sizeof(*ld)); |
c1db2dce | 457 | |
acd45e02 JA |
458 | ld->entries = td->o.iodepth; |
459 | ld->is_pow2 = is_power_of_2(ld->entries); | |
460 | ld->aio_events = calloc(ld->entries, sizeof(struct io_event)); | |
461 | ld->iocbs = calloc(ld->entries, sizeof(struct iocb *)); | |
462 | ld->io_us = calloc(ld->entries, sizeof(struct io_u *)); | |
1ceaf63b | 463 | ld->iovecs = calloc(ld->entries, sizeof(ld->iovecs[0])); |
755200a3 | 464 | |
565e784d | 465 | td->io_ops_data = ld; |
e9f6567a | 466 | |
d6cbeab4 | 467 | ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options); |
e9f6567a | 468 | if (ret) { |
b2a432bf PC |
469 | td_verror(td, EINVAL, "fio_libaio_init"); |
470 | return 1; | |
471 | } | |
e9f6567a | 472 | |
2866c82d JA |
473 | return 0; |
474 | } | |
475 | ||
5a8a6a03 | 476 | FIO_STATIC struct ioengine_ops ioengine = { |
de890a1e SL |
477 | .name = "libaio", |
478 | .version = FIO_IOOPS_VERSION, | |
4e7e7898 | 479 | .flags = FIO_ASYNCIO_SYNC_TRIM | |
a79319fe JG |
480 | FIO_ASYNCIO_SETS_ISSUE_TIME | |
481 | FIO_ATOMICWRITES, | |
de890a1e | 482 | .init = fio_libaio_init, |
2041bd34 | 483 | .post_init = fio_libaio_post_init, |
de890a1e SL |
484 | .prep = fio_libaio_prep, |
485 | .queue = fio_libaio_queue, | |
486 | .commit = fio_libaio_commit, | |
487 | .cancel = fio_libaio_cancel, | |
488 | .getevents = fio_libaio_getevents, | |
489 | .event = fio_libaio_event, | |
490 | .cleanup = fio_libaio_cleanup, | |
491 | .open_file = generic_open_file, | |
492 | .close_file = generic_close_file, | |
493 | .get_file_size = generic_get_file_size, | |
494 | .options = options, | |
495 | .option_struct_size = sizeof(struct libaio_options), | |
2866c82d | 496 | }; |
34cfcdaf | 497 | |
5f350952 JA |
498 | static void fio_init fio_libaio_register(void) |
499 | { | |
500 | register_ioengine(&ioengine); | |
501 | } | |
502 | ||
503 | static void fio_exit fio_libaio_unregister(void) | |
504 | { | |
505 | unregister_ioengine(&ioengine); | |
506 | } |