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