Commit | Line | Data |
---|---|---|
2866c82d | 1 | /* |
da751ca9 JA |
2 | * libaio engine |
3 | * | |
4 | * IO engine using the Linux native aio interface. | |
2866c82d JA |
5 | * |
6 | */ | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <unistd.h> | |
10 | #include <errno.h> | |
11 | #include <assert.h> | |
5f350952 JA |
12 | |
13 | #include "../fio.h" | |
2866c82d | 14 | |
34cfcdaf JA |
15 | #ifdef FIO_HAVE_LIBAIO |
16 | ||
2866c82d JA |
17 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
18 | ||
19 | struct libaio_data { | |
20 | io_context_t aio_ctx; | |
21 | struct io_event *aio_events; | |
755200a3 | 22 | struct iocb **iocbs; |
7e77dd02 | 23 | struct io_u **io_us; |
755200a3 | 24 | int iocbs_nr; |
2866c82d JA |
25 | }; |
26 | ||
de890a1e SL |
27 | struct libaio_options { |
28 | struct thread_data *td; | |
29 | unsigned int userspace_reap; | |
30 | }; | |
31 | ||
32 | static struct fio_option options[] = { | |
33 | { | |
34 | .name = "userspace_reap", | |
e8b0e958 | 35 | .lname = "Libaio userspace reaping", |
de890a1e SL |
36 | .type = FIO_OPT_STR_SET, |
37 | .off1 = offsetof(struct libaio_options, userspace_reap), | |
38 | .help = "Use alternative user-space reap implementation", | |
e8b0e958 | 39 | .category = FIO_OPT_C_IO, |
de890a1e SL |
40 | }, |
41 | { | |
42 | .name = NULL, | |
43 | }, | |
44 | }; | |
45 | ||
7a16dd02 | 46 | static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
2866c82d | 47 | { |
53cdc686 JA |
48 | struct fio_file *f = io_u->file; |
49 | ||
2866c82d | 50 | if (io_u->ddir == DDIR_READ) |
cec6b55d | 51 | io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
87dc1ab1 | 52 | else if (io_u->ddir == DDIR_WRITE) |
cec6b55d | 53 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
5f9099ea | 54 | else if (ddir_sync(io_u->ddir)) |
87dc1ab1 | 55 | io_prep_fsync(&io_u->iocb, f->fd); |
2866c82d JA |
56 | |
57 | return 0; | |
58 | } | |
59 | ||
60 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) | |
61 | { | |
62 | struct libaio_data *ld = td->io_ops->data; | |
f423479d JA |
63 | struct io_event *ev; |
64 | struct io_u *io_u; | |
2866c82d | 65 | |
f423479d JA |
66 | ev = ld->aio_events + event; |
67 | io_u = ev_to_iou(ev); | |
68 | ||
69 | if (ev->res != io_u->xfer_buflen) { | |
70 | if (ev->res > io_u->xfer_buflen) | |
71 | io_u->error = -ev->res; | |
72 | else | |
73 | io_u->resid = io_u->xfer_buflen - ev->res; | |
74 | } else | |
75 | io_u->error = 0; | |
76 | ||
77 | return io_u; | |
2866c82d JA |
78 | } |
79 | ||
675012f0 DE |
80 | struct aio_ring { |
81 | unsigned id; /** kernel internal index number */ | |
82 | unsigned nr; /** number of io_events */ | |
83 | unsigned head; | |
84 | unsigned tail; | |
c44b1ff5 | 85 | |
675012f0 DE |
86 | unsigned magic; |
87 | unsigned compat_features; | |
88 | unsigned incompat_features; | |
89 | unsigned header_length; /** size of aio_ring */ | |
90 | ||
91 | struct io_event events[0]; | |
92 | }; | |
93 | ||
94 | #define AIO_RING_MAGIC 0xa10a10a1 | |
95 | ||
96 | static int user_io_getevents(io_context_t aio_ctx, unsigned int max, | |
c44b1ff5 | 97 | struct io_event *events) |
675012f0 DE |
98 | { |
99 | long i = 0; | |
100 | unsigned head; | |
c44b1ff5 | 101 | struct aio_ring *ring = (struct aio_ring*) aio_ctx; |
675012f0 DE |
102 | |
103 | while (i < max) { | |
104 | head = ring->head; | |
105 | ||
106 | if (head == ring->tail) { | |
107 | /* There are no more completions */ | |
108 | break; | |
109 | } else { | |
110 | /* There is another completion to reap */ | |
111 | events[i] = ring->events[head]; | |
112 | read_barrier(); | |
c44b1ff5 | 113 | ring->head = (head + 1) % ring->nr; |
675012f0 DE |
114 | i++; |
115 | } | |
116 | } | |
117 | ||
118 | return i; | |
119 | } | |
120 | ||
e7d2e616 JA |
121 | static int fio_libaio_getevents(struct thread_data *td, unsigned int min, |
122 | unsigned int max, struct timespec *t) | |
2866c82d JA |
123 | { |
124 | struct libaio_data *ld = td->io_ops->data; | |
de890a1e | 125 | struct libaio_options *o = td->eo; |
0b7fdba7 DE |
126 | unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min; |
127 | int r, events = 0; | |
2866c82d JA |
128 | |
129 | do { | |
de890a1e | 130 | if (o->userspace_reap == 1 |
675012f0 DE |
131 | && actual_min == 0 |
132 | && ((struct aio_ring *)(ld->aio_ctx))->magic | |
133 | == AIO_RING_MAGIC) { | |
134 | r = user_io_getevents(ld->aio_ctx, max, | |
135 | ld->aio_events + events); | |
136 | } else { | |
137 | r = io_getevents(ld->aio_ctx, actual_min, | |
138 | max, ld->aio_events + events, t); | |
139 | } | |
0b7fdba7 DE |
140 | if (r >= 0) |
141 | events += r; | |
142 | else if (r == -EAGAIN) | |
2866c82d | 143 | usleep(100); |
0b7fdba7 DE |
144 | } while (events < min); |
145 | ||
146 | return r < 0 ? r : events; | |
2866c82d JA |
147 | } |
148 | ||
149 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) | |
150 | { | |
151 | struct libaio_data *ld = td->io_ops->data; | |
2866c82d | 152 | |
7101d9c2 JA |
153 | fio_ro_check(td, io_u); |
154 | ||
2dc1bbeb | 155 | if (ld->iocbs_nr == (int) td->o.iodepth) |
755200a3 JA |
156 | return FIO_Q_BUSY; |
157 | ||
158 | /* | |
159 | * fsync is tricky, since it can fail and we need to do it | |
160 | * serialized with other io. the reason is that linux doesn't | |
161 | * support aio fsync yet. So return busy for the case where we | |
162 | * have pending io, to let fio complete those first. | |
163 | */ | |
f011531e | 164 | if (ddir_sync(io_u->ddir)) { |
755200a3 JA |
165 | if (ld->iocbs_nr) |
166 | return FIO_Q_BUSY; | |
5f9099ea | 167 | |
f011531e | 168 | do_io_u_sync(td, io_u); |
755200a3 JA |
169 | return FIO_Q_COMPLETED; |
170 | } | |
171 | ||
a5f3027c JA |
172 | if (io_u->ddir == DDIR_TRIM) { |
173 | if (ld->iocbs_nr) | |
174 | return FIO_Q_BUSY; | |
175 | ||
176 | do_io_u_trim(td, io_u); | |
177 | return FIO_Q_COMPLETED; | |
178 | } | |
179 | ||
755200a3 | 180 | ld->iocbs[ld->iocbs_nr] = &io_u->iocb; |
7e77dd02 | 181 | ld->io_us[ld->iocbs_nr] = io_u; |
755200a3 JA |
182 | ld->iocbs_nr++; |
183 | return FIO_Q_QUEUED; | |
184 | } | |
185 | ||
7e77dd02 JA |
186 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
187 | unsigned int nr) | |
188 | { | |
189 | struct timeval now; | |
190 | unsigned int i; | |
191 | ||
12d9d841 JA |
192 | if (!fio_fill_issue_time(td)) |
193 | return; | |
194 | ||
7e77dd02 JA |
195 | fio_gettime(&now, NULL); |
196 | ||
197 | for (i = 0; i < nr; i++) { | |
198 | struct io_u *io_u = io_us[i]; | |
199 | ||
200 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
201 | io_u_queued(td, io_u); | |
202 | } | |
203 | } | |
204 | ||
755200a3 JA |
205 | static int fio_libaio_commit(struct thread_data *td) |
206 | { | |
207 | struct libaio_data *ld = td->io_ops->data; | |
208 | struct iocb **iocbs; | |
7e77dd02 | 209 | struct io_u **io_us; |
5e00c2c4 | 210 | int ret; |
755200a3 JA |
211 | |
212 | if (!ld->iocbs_nr) | |
213 | return 0; | |
214 | ||
7e77dd02 | 215 | io_us = ld->io_us; |
755200a3 | 216 | iocbs = ld->iocbs; |
2866c82d | 217 | do { |
5e00c2c4 JA |
218 | ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs); |
219 | if (ret > 0) { | |
7e77dd02 | 220 | fio_libaio_queued(td, io_us, ret); |
838bc709 | 221 | io_u_mark_submit(td, ret); |
5e00c2c4 | 222 | ld->iocbs_nr -= ret; |
7e77dd02 | 223 | io_us += ret; |
755200a3 | 224 | iocbs += ret; |
5e00c2c4 | 225 | ret = 0; |
838bc709 JA |
226 | } else if (!ret || ret == -EAGAIN || ret == -EINTR) { |
227 | if (!ret) | |
228 | io_u_mark_submit(td, ret); | |
2866c82d | 229 | continue; |
838bc709 | 230 | } else |
2866c82d | 231 | break; |
5e00c2c4 | 232 | } while (ld->iocbs_nr); |
2866c82d | 233 | |
36167d82 | 234 | return ret; |
2866c82d JA |
235 | } |
236 | ||
237 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) | |
238 | { | |
239 | struct libaio_data *ld = td->io_ops->data; | |
240 | ||
241 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); | |
242 | } | |
243 | ||
244 | static void fio_libaio_cleanup(struct thread_data *td) | |
245 | { | |
246 | struct libaio_data *ld = td->io_ops->data; | |
247 | ||
248 | if (ld) { | |
249 | io_destroy(ld->aio_ctx); | |
7e77dd02 JA |
250 | free(ld->aio_events); |
251 | free(ld->iocbs); | |
252 | free(ld->io_us); | |
2866c82d | 253 | free(ld); |
2866c82d JA |
254 | } |
255 | } | |
256 | ||
257 | static int fio_libaio_init(struct thread_data *td) | |
258 | { | |
259 | struct libaio_data *ld = malloc(sizeof(*ld)); | |
c90e1017 JA |
260 | struct libaio_options *o = td->eo; |
261 | int err = 0; | |
2866c82d JA |
262 | |
263 | memset(ld, 0, sizeof(*ld)); | |
c1db2dce | 264 | |
c90e1017 JA |
265 | /* |
266 | * First try passing in 0 for queue depth, since we don't | |
267 | * care about the user ring. If that fails, the kernel is too old | |
268 | * and we need the right depth. | |
269 | */ | |
270 | if (!o->userspace_reap) | |
c224ec05 | 271 | err = io_queue_init(INT_MAX, &ld->aio_ctx); |
c90e1017 JA |
272 | if (o->userspace_reap || err == -EINVAL) |
273 | err = io_queue_init(td->o.iodepth, &ld->aio_ctx); | |
c1db2dce JA |
274 | if (err) { |
275 | td_verror(td, -err, "io_queue_init"); | |
75de55ac | 276 | log_err("fio: check /proc/sys/fs/aio-max-nr\n"); |
cb781c75 | 277 | free(ld); |
2866c82d JA |
278 | return 1; |
279 | } | |
280 | ||
2dc1bbeb JA |
281 | ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event)); |
282 | memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event)); | |
283 | ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *)); | |
755200a3 | 284 | memset(ld->iocbs, 0, sizeof(struct iocb *)); |
2dc1bbeb JA |
285 | ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
286 | memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); | |
755200a3 JA |
287 | ld->iocbs_nr = 0; |
288 | ||
2866c82d JA |
289 | td->io_ops->data = ld; |
290 | return 0; | |
291 | } | |
292 | ||
5f350952 | 293 | static struct ioengine_ops ioengine = { |
de890a1e SL |
294 | .name = "libaio", |
295 | .version = FIO_IOOPS_VERSION, | |
296 | .init = fio_libaio_init, | |
297 | .prep = fio_libaio_prep, | |
298 | .queue = fio_libaio_queue, | |
299 | .commit = fio_libaio_commit, | |
300 | .cancel = fio_libaio_cancel, | |
301 | .getevents = fio_libaio_getevents, | |
302 | .event = fio_libaio_event, | |
303 | .cleanup = fio_libaio_cleanup, | |
304 | .open_file = generic_open_file, | |
305 | .close_file = generic_close_file, | |
306 | .get_file_size = generic_get_file_size, | |
307 | .options = options, | |
308 | .option_struct_size = sizeof(struct libaio_options), | |
2866c82d | 309 | }; |
34cfcdaf JA |
310 | |
311 | #else /* FIO_HAVE_LIBAIO */ | |
312 | ||
313 | /* | |
314 | * When we have a proper configure system in place, we simply wont build | |
315 | * and install this io engine. For now install a crippled version that | |
316 | * just complains and fails to load. | |
317 | */ | |
318 | static int fio_libaio_init(struct thread_data fio_unused *td) | |
319 | { | |
a3edaf76 | 320 | log_err("fio: libaio not available\n"); |
34cfcdaf JA |
321 | return 1; |
322 | } | |
323 | ||
5f350952 | 324 | static struct ioengine_ops ioengine = { |
34cfcdaf JA |
325 | .name = "libaio", |
326 | .version = FIO_IOOPS_VERSION, | |
327 | .init = fio_libaio_init, | |
328 | }; | |
329 | ||
330 | #endif | |
5f350952 JA |
331 | |
332 | static void fio_init fio_libaio_register(void) | |
333 | { | |
334 | register_ioengine(&ioengine); | |
335 | } | |
336 | ||
337 | static void fio_exit fio_libaio_unregister(void) | |
338 | { | |
339 | unregister_ioengine(&ioengine); | |
340 | } |