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