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