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