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