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