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