Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | /* |
2 | * The io parts of the fio tool, includes workers for sync and mmap'ed | |
3 | * io, as well as both posix and linux libaio support. | |
4 | * | |
5 | * sync io is implemented on top of aio. | |
6 | * | |
7 | * This is not really specific to fio, if the get_io_u/put_io_u and | |
8 | * structures was pulled into this as well it would be a perfectly | |
9 | * generic io engine that could be used for other projects. | |
10 | * | |
11 | */ | |
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
5c4e1dbc | 15 | #include <string.h> |
2866c82d | 16 | #include <dlfcn.h> |
0c6e7517 | 17 | #include <assert.h> |
8c16d840 | 18 | |
ebac4655 | 19 | #include "fio.h" |
ebac4655 | 20 | |
5f350952 JA |
21 | static LIST_HEAD(engine_list); |
22 | ||
8c16d840 JA |
23 | static int check_engine_ops(struct ioengine_ops *ops) |
24 | { | |
5f350952 | 25 | if (ops->version != FIO_IOOPS_VERSION) { |
5ec10eaa JA |
26 | log_err("bad ioops version %d (want %d)\n", ops->version, |
27 | FIO_IOOPS_VERSION); | |
5f350952 JA |
28 | return 1; |
29 | } | |
30 | ||
36167d82 JA |
31 | if (!ops->queue) { |
32 | log_err("%s: no queue handler\n", ops->name); | |
33 | return 1; | |
34 | } | |
35 | ||
36 | /* | |
37 | * sync engines only need a ->queue() | |
38 | */ | |
39 | if (ops->flags & FIO_SYNCIO) | |
40 | return 0; | |
5ec10eaa | 41 | |
8c16d840 | 42 | if (!ops->event) { |
36167d82 | 43 | log_err("%s: no event handler\n", ops->name); |
8c16d840 JA |
44 | return 1; |
45 | } | |
46 | if (!ops->getevents) { | |
36167d82 | 47 | log_err("%s: no getevents handler\n", ops->name); |
8c16d840 JA |
48 | return 1; |
49 | } | |
50 | if (!ops->queue) { | |
36167d82 | 51 | log_err("%s: no queue handler\n", ops->name); |
8c16d840 JA |
52 | return 1; |
53 | } | |
5ec10eaa | 54 | |
8c16d840 JA |
55 | return 0; |
56 | } | |
57 | ||
5f350952 | 58 | void unregister_ioengine(struct ioengine_ops *ops) |
ebac4655 | 59 | { |
ee56ad50 | 60 | dprint(FD_IO, "ioengine %s unregistered\n", ops->name); |
5f350952 JA |
61 | list_del(&ops->list); |
62 | INIT_LIST_HEAD(&ops->list); | |
63 | } | |
64 | ||
b2fdda43 | 65 | void register_ioengine(struct ioengine_ops *ops) |
5f350952 | 66 | { |
ee56ad50 | 67 | dprint(FD_IO, "ioengine %s registered\n", ops->name); |
5f350952 JA |
68 | INIT_LIST_HEAD(&ops->list); |
69 | list_add_tail(&ops->list, &engine_list); | |
5f350952 JA |
70 | } |
71 | ||
72 | static struct ioengine_ops *find_ioengine(const char *name) | |
73 | { | |
74 | struct ioengine_ops *ops; | |
75 | struct list_head *entry; | |
ebac4655 | 76 | |
5f350952 JA |
77 | list_for_each(entry, &engine_list) { |
78 | ops = list_entry(entry, struct ioengine_ops, list); | |
bc5b77a8 | 79 | if (!strcmp(name, ops->name)) |
5f350952 JA |
80 | return ops; |
81 | } | |
82 | ||
83 | return NULL; | |
84 | } | |
85 | ||
86 | static struct ioengine_ops *dlopen_ioengine(struct thread_data *td, | |
87 | const char *engine_lib) | |
88 | { | |
89 | struct ioengine_ops *ops; | |
90 | void *dlhandle; | |
91 | ||
ee56ad50 JA |
92 | dprint(FD_IO, "dload engine %s\n", engine_lib); |
93 | ||
2866c82d JA |
94 | dlerror(); |
95 | dlhandle = dlopen(engine_lib, RTLD_LAZY); | |
d4dbaaa8 | 96 | if (!dlhandle) { |
e1161c32 | 97 | td_vmsg(td, -1, dlerror(), "dlopen"); |
d4dbaaa8 JA |
98 | return NULL; |
99 | } | |
8756e4d4 | 100 | |
da51c050 JA |
101 | /* |
102 | * Unlike the included modules, external engines should have a | |
103 | * non-static ioengine structure that we can reference. | |
104 | */ | |
2866c82d | 105 | ops = dlsym(dlhandle, "ioengine"); |
d4dbaaa8 | 106 | if (!ops) { |
e1161c32 | 107 | td_vmsg(td, -1, dlerror(), "dlsym"); |
d4dbaaa8 JA |
108 | dlclose(dlhandle); |
109 | return NULL; | |
110 | } | |
8756e4d4 | 111 | |
5f350952 JA |
112 | ops->dlhandle = dlhandle; |
113 | return ops; | |
114 | } | |
115 | ||
116 | struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name) | |
117 | { | |
118 | struct ioengine_ops *ops, *ret; | |
119 | char engine[16]; | |
120 | ||
ee56ad50 JA |
121 | dprint(FD_IO, "load ioengine %s\n", name); |
122 | ||
5f350952 JA |
123 | strncpy(engine, name, sizeof(engine) - 1); |
124 | ||
125 | /* | |
126 | * linux libaio has alias names, so convert to what we want | |
127 | */ | |
128 | if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) | |
129 | strcpy(engine, "libaio"); | |
130 | ||
131 | ops = find_ioengine(engine); | |
132 | if (!ops) | |
133 | ops = dlopen_ioengine(td, name); | |
134 | ||
135 | if (!ops) { | |
136 | log_err("fio: engine %s not loadable\n", name); | |
b902ceb5 JA |
137 | return NULL; |
138 | } | |
139 | ||
8c16d840 JA |
140 | /* |
141 | * Check that the required methods are there. | |
142 | */ | |
5f350952 | 143 | if (check_engine_ops(ops)) |
8c16d840 | 144 | return NULL; |
8c16d840 | 145 | |
84585003 JA |
146 | ret = malloc(sizeof(*ret)); |
147 | memcpy(ret, ops, sizeof(*ret)); | |
148 | ret->data = NULL; | |
84585003 JA |
149 | |
150 | return ret; | |
8756e4d4 JA |
151 | } |
152 | ||
2866c82d | 153 | void close_ioengine(struct thread_data *td) |
8756e4d4 | 154 | { |
ee56ad50 JA |
155 | dprint(FD_IO, "close ioengine %s\n", td->io_ops->name); |
156 | ||
2866c82d JA |
157 | if (td->io_ops->cleanup) |
158 | td->io_ops->cleanup(td); | |
b990b5c0 | 159 | |
5f350952 JA |
160 | if (td->io_ops->dlhandle) |
161 | dlclose(td->io_ops->dlhandle); | |
162 | ||
84585003 JA |
163 | free(td->io_ops); |
164 | td->io_ops = NULL; | |
b990b5c0 | 165 | } |
10ba535a JA |
166 | |
167 | int td_io_prep(struct thread_data *td, struct io_u *io_u) | |
168 | { | |
ee56ad50 | 169 | dprint_io_u(io_u, "prep"); |
7101d9c2 JA |
170 | fio_ro_check(td, io_u); |
171 | ||
4d4e80f2 | 172 | lock_file(td, io_u->file, io_u->ddir); |
b2bd2bd9 | 173 | |
2ba1c290 JA |
174 | if (td->io_ops->prep) { |
175 | int ret = td->io_ops->prep(td, io_u); | |
176 | ||
177 | dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret); | |
b2bd2bd9 | 178 | if (ret) |
4d4e80f2 | 179 | unlock_file(td, io_u->file); |
2ba1c290 JA |
180 | return ret; |
181 | } | |
10ba535a JA |
182 | |
183 | return 0; | |
184 | } | |
185 | ||
e7d2e616 | 186 | int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max, |
10ba535a JA |
187 | struct timespec *t) |
188 | { | |
ee56ad50 | 189 | int r = 0; |
face81b2 | 190 | |
ee56ad50 JA |
191 | if (min > 0 && td->io_ops->commit) { |
192 | r = td->io_ops->commit(td); | |
face81b2 | 193 | if (r < 0) |
ee56ad50 | 194 | goto out; |
face81b2 | 195 | } |
36167d82 | 196 | |
ee56ad50 JA |
197 | r = 0; |
198 | if (td->io_ops->getevents) | |
199 | r = td->io_ops->getevents(td, min, max, t); | |
200 | out: | |
201 | dprint(FD_IO, "getevents: %d\n", r); | |
202 | return r; | |
10ba535a JA |
203 | } |
204 | ||
205 | int td_io_queue(struct thread_data *td, struct io_u *io_u) | |
206 | { | |
7e77dd02 JA |
207 | int ret; |
208 | ||
ee56ad50 | 209 | dprint_io_u(io_u, "queue"); |
7101d9c2 JA |
210 | fio_ro_check(td, io_u); |
211 | ||
0c6e7517 JA |
212 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
213 | io_u->flags |= IO_U_F_FLIGHT; | |
214 | ||
3d7b485f JA |
215 | assert(io_u->file->flags & FIO_FILE_OPEN); |
216 | ||
11786802 JA |
217 | io_u->error = 0; |
218 | io_u->resid = 0; | |
219 | ||
433afcb4 | 220 | if (td->io_ops->flags & FIO_SYNCIO) { |
5aeb77df | 221 | fio_gettime(&io_u->issue_time, NULL); |
5ec10eaa JA |
222 | memcpy(&td->last_issue, &io_u->issue_time, |
223 | sizeof(struct timeval)); | |
10ba535a | 224 | |
433afcb4 JA |
225 | /* |
226 | * for a sync engine, set the timeout upfront | |
227 | */ | |
5ec10eaa JA |
228 | if (mtime_since(&td->timeout_end, &io_u->issue_time) |
229 | < IO_U_TIMEOUT) | |
433afcb4 JA |
230 | io_u_set_timeout(td); |
231 | } | |
232 | ||
755200a3 JA |
233 | if (io_u->ddir != DDIR_SYNC) |
234 | td->io_issues[io_u->ddir]++; | |
235 | ||
7e77dd02 | 236 | ret = td->io_ops->queue(td, io_u); |
5aeb77df | 237 | |
4d4e80f2 | 238 | unlock_file(td, io_u->file); |
b2bd2bd9 | 239 | |
163f849e JA |
240 | if (ret != FIO_Q_BUSY) |
241 | io_u_mark_depth(td, io_u); | |
242 | ||
eb7c8ae2 JA |
243 | if (ret == FIO_Q_QUEUED) { |
244 | int r; | |
245 | ||
cb5ab512 | 246 | td->io_u_queued++; |
2dc1bbeb | 247 | if (td->io_u_queued > td->o.iodepth_batch) { |
eb7c8ae2 JA |
248 | r = td_io_commit(td); |
249 | if (r < 0) | |
250 | return r; | |
251 | } | |
252 | } | |
cb5ab512 | 253 | |
433afcb4 | 254 | if ((td->io_ops->flags & FIO_SYNCIO) == 0) { |
5aeb77df | 255 | fio_gettime(&io_u->issue_time, NULL); |
5ec10eaa JA |
256 | memcpy(&td->last_issue, &io_u->issue_time, |
257 | sizeof(struct timeval)); | |
5aeb77df | 258 | |
433afcb4 JA |
259 | /* |
260 | * async engine, set the timeout here | |
261 | */ | |
262 | if (ret == FIO_Q_QUEUED && | |
5ec10eaa JA |
263 | (mtime_since(&td->timeout_end, &io_u->issue_time) |
264 | < IO_U_TIMEOUT)) { | |
433afcb4 | 265 | io_u_set_timeout(td); |
5ec10eaa | 266 | } |
433afcb4 JA |
267 | } |
268 | ||
7e77dd02 | 269 | return ret; |
10ba535a | 270 | } |
8c16d840 JA |
271 | |
272 | int td_io_init(struct thread_data *td) | |
273 | { | |
eeb12160 | 274 | int ret = 0; |
8c16d840 | 275 | |
eeb12160 JA |
276 | if (td->io_ops->init) { |
277 | ret = td->io_ops->init(td); | |
5ec10eaa JA |
278 | if (ret && td->o.iodepth > 1) { |
279 | log_err("fio: io engine init failed. Perhaps try" | |
280 | " reducing io depth?\n"); | |
281 | } | |
eeb12160 JA |
282 | } |
283 | ||
284 | return ret; | |
8c16d840 | 285 | } |
755200a3 JA |
286 | |
287 | int td_io_commit(struct thread_data *td) | |
288 | { | |
ee56ad50 JA |
289 | dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth); |
290 | ||
e1161c32 JA |
291 | if (!td->cur_depth) |
292 | return 0; | |
cb5ab512 JA |
293 | |
294 | td->io_u_queued = 0; | |
755200a3 JA |
295 | if (td->io_ops->commit) |
296 | return td->io_ops->commit(td); | |
297 | ||
298 | return 0; | |
299 | } | |
b5af8293 JA |
300 | |
301 | int td_io_open_file(struct thread_data *td, struct fio_file *f) | |
302 | { | |
413d6693 JA |
303 | if (td->io_ops->open_file(td, f)) { |
304 | if (td->error == EINVAL && td->o.odirect) | |
305 | log_err("fio: destination does not support O_DIRECT\n"); | |
5ec10eaa JA |
306 | if (td->error == EMFILE) { |
307 | log_err("fio: try reducing/setting openfiles (failed" | |
308 | " at %u of %u)\n", td->nr_open_files, | |
309 | td->o.nr_files); | |
310 | } | |
413d6693 JA |
311 | |
312 | return 1; | |
313 | } | |
314 | ||
66159828 JA |
315 | if (f->filetype == FIO_TYPE_PIPE) { |
316 | if (td_random(td)) { | |
317 | log_err("fio: can't seek on pipes (no random io)\n"); | |
318 | goto err; | |
319 | } | |
320 | } | |
321 | ||
413d6693 | 322 | f->last_free_lookup = 0; |
bcdedd0a | 323 | f->last_pos = f->file_offset; |
413d6693 JA |
324 | f->flags |= FIO_FILE_OPEN; |
325 | f->flags &= ~FIO_FILE_CLOSING; | |
326 | ||
327 | if (td->io_ops->flags & FIO_DISKLESSIO) | |
328 | goto done; | |
329 | ||
330 | if (td->o.invalidate_cache && file_invalidate_cache(td, f)) | |
331 | goto err; | |
332 | ||
66159828 JA |
333 | if (td->o.fadvise_hint && |
334 | (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) { | |
413d6693 JA |
335 | int flags; |
336 | ||
337 | if (td_random(td)) | |
338 | flags = POSIX_FADV_RANDOM; | |
339 | else | |
340 | flags = POSIX_FADV_SEQUENTIAL; | |
341 | ||
342 | if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) { | |
343 | td_verror(td, errno, "fadvise"); | |
344 | goto err; | |
345 | } | |
7bb48f84 | 346 | } |
a978ba68 | 347 | |
413d6693 JA |
348 | if (f->file_map) |
349 | memset(f->file_map, 0, f->num_maps * sizeof(long)); | |
a978ba68 | 350 | |
413d6693 | 351 | done: |
f29b25a3 | 352 | log_file(td, f, FIO_LOG_OPEN_FILE); |
413d6693 JA |
353 | td->nr_open_files++; |
354 | get_file(f); | |
355 | return 0; | |
356 | err: | |
b284075a JA |
357 | if (td->io_ops->close_file) |
358 | td->io_ops->close_file(td, f); | |
7bb48f84 | 359 | return 1; |
b5af8293 JA |
360 | } |
361 | ||
6977bcd0 | 362 | int td_io_close_file(struct thread_data *td, struct fio_file *f) |
b5af8293 | 363 | { |
f29b25a3 JA |
364 | if (!(f->flags & FIO_FILE_CLOSING)) |
365 | log_file(td, f, FIO_LOG_CLOSE_FILE); | |
366 | ||
0ad920e7 JA |
367 | /* |
368 | * mark as closing, do real close when last io on it has completed | |
369 | */ | |
370 | f->flags |= FIO_FILE_CLOSING; | |
371 | ||
4d4e80f2 | 372 | unlock_file_all(td, f); |
29c1349f | 373 | |
6977bcd0 | 374 | return put_file(td, f); |
b5af8293 | 375 | } |