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 | */ | |
ebac4655 JA |
12 | #include <stdlib.h> |
13 | #include <unistd.h> | |
5c4e1dbc | 14 | #include <string.h> |
2866c82d | 15 | #include <dlfcn.h> |
ecc314ba | 16 | #include <fcntl.h> |
0c6e7517 | 17 | #include <assert.h> |
35300d5f ES |
18 | #include <sys/types.h> |
19 | #include <dirent.h> | |
83276370 | 20 | #include <errno.h> |
8c16d840 | 21 | |
ebac4655 | 22 | #include "fio.h" |
7c9b1bce | 23 | #include "diskutil.h" |
bfbdd35b | 24 | #include "zbd.h" |
ebac4655 | 25 | |
01743ee1 | 26 | static FLIST_HEAD(engine_list); |
5f350952 | 27 | |
abfd235a | 28 | static bool check_engine_ops(struct thread_data *td, struct ioengine_ops *ops) |
8c16d840 | 29 | { |
5f350952 | 30 | if (ops->version != FIO_IOOPS_VERSION) { |
5ec10eaa JA |
31 | log_err("bad ioops version %d (want %d)\n", ops->version, |
32 | FIO_IOOPS_VERSION); | |
1cc954ba | 33 | return true; |
5f350952 JA |
34 | } |
35 | ||
36167d82 JA |
36 | if (!ops->queue) { |
37 | log_err("%s: no queue handler\n", ops->name); | |
1cc954ba | 38 | return true; |
36167d82 JA |
39 | } |
40 | ||
41 | /* | |
42 | * sync engines only need a ->queue() | |
43 | */ | |
44 | if (ops->flags & FIO_SYNCIO) | |
1cc954ba | 45 | return false; |
5ec10eaa | 46 | |
abfd235a JA |
47 | /* |
48 | * async engines aren't reliable with offload | |
49 | */ | |
695611a9 | 50 | if ((td->o.io_submit_mode == IO_MODE_OFFLOAD) && |
8bfe330e | 51 | (ops->flags & FIO_NO_OFFLOAD)) { |
abfd235a JA |
52 | log_err("%s: can't be used with offloaded submit. Use a sync " |
53 | "engine\n", ops->name); | |
54 | return true; | |
55 | } | |
56 | ||
1cc954ba JA |
57 | if (!ops->event || !ops->getevents) { |
58 | log_err("%s: no event/getevents handler\n", ops->name); | |
59 | return true; | |
8c16d840 | 60 | } |
5ec10eaa | 61 | |
1cc954ba | 62 | return false; |
8c16d840 JA |
63 | } |
64 | ||
5f350952 | 65 | void unregister_ioengine(struct ioengine_ops *ops) |
ebac4655 | 66 | { |
ee56ad50 | 67 | dprint(FD_IO, "ioengine %s unregistered\n", ops->name); |
adf075fa | 68 | flist_del_init(&ops->list); |
5f350952 JA |
69 | } |
70 | ||
b2fdda43 | 71 | void register_ioengine(struct ioengine_ops *ops) |
5f350952 | 72 | { |
ee56ad50 | 73 | dprint(FD_IO, "ioengine %s registered\n", ops->name); |
01743ee1 | 74 | flist_add_tail(&ops->list, &engine_list); |
5f350952 JA |
75 | } |
76 | ||
77 | static struct ioengine_ops *find_ioengine(const char *name) | |
78 | { | |
79 | struct ioengine_ops *ops; | |
01743ee1 | 80 | struct flist_head *entry; |
ebac4655 | 81 | |
01743ee1 JA |
82 | flist_for_each(entry, &engine_list) { |
83 | ops = flist_entry(entry, struct ioengine_ops, list); | |
bc5b77a8 | 84 | if (!strcmp(name, ops->name)) |
5f350952 JA |
85 | return ops; |
86 | } | |
87 | ||
88 | return NULL; | |
89 | } | |
90 | ||
5a8a6a03 YK |
91 | #ifdef CONFIG_DYNAMIC_ENGINES |
92 | static void *dlopen_external(struct thread_data *td, const char *engine) | |
93 | { | |
94 | char engine_path[PATH_MAX]; | |
7dc1183d | 95 | void *dlhandle; |
5a8a6a03 | 96 | |
25b9b58e | 97 | sprintf(engine_path, "%s/fio-%s.so", FIO_EXT_ENG_DIR, engine); |
5a8a6a03 | 98 | |
48ff7df9 | 99 | dprint(FD_IO, "dlopen external %s\n", engine_path); |
7dc1183d YK |
100 | dlhandle = dlopen(engine_path, RTLD_LAZY); |
101 | if (!dlhandle) | |
102 | log_info("Engine %s not found; Either name is invalid, was not built, or fio-engine-%s package is missing.\n", | |
103 | engine, engine); | |
104 | ||
105 | return dlhandle; | |
5a8a6a03 YK |
106 | } |
107 | #else | |
108 | #define dlopen_external(td, engine) (NULL) | |
109 | #endif | |
110 | ||
5f350952 JA |
111 | static struct ioengine_ops *dlopen_ioengine(struct thread_data *td, |
112 | const char *engine_lib) | |
113 | { | |
114 | struct ioengine_ops *ops; | |
115 | void *dlhandle; | |
116 | ||
8bdf7a84 JA |
117 | if (!strncmp(engine_lib, "linuxaio", 8) || |
118 | !strncmp(engine_lib, "aio", 3)) | |
119 | engine_lib = "libaio"; | |
120 | ||
48ff7df9 | 121 | dprint(FD_IO, "dlopen engine %s\n", engine_lib); |
ee56ad50 | 122 | |
2866c82d JA |
123 | dlerror(); |
124 | dlhandle = dlopen(engine_lib, RTLD_LAZY); | |
d4dbaaa8 | 125 | if (!dlhandle) { |
5a8a6a03 YK |
126 | dlhandle = dlopen_external(td, engine_lib); |
127 | if (!dlhandle) { | |
128 | td_vmsg(td, -1, dlerror(), "dlopen"); | |
129 | return NULL; | |
130 | } | |
d4dbaaa8 | 131 | } |
8756e4d4 | 132 | |
da51c050 JA |
133 | /* |
134 | * Unlike the included modules, external engines should have a | |
135 | * non-static ioengine structure that we can reference. | |
136 | */ | |
0abea0b7 DM |
137 | ops = dlsym(dlhandle, engine_lib); |
138 | if (!ops) | |
139 | ops = dlsym(dlhandle, "ioengine"); | |
a8075704 DG |
140 | |
141 | /* | |
142 | * For some external engines (like C++ ones) it is not that trivial | |
143 | * to provide a non-static ionengine structure that we can reference. | |
144 | * Instead we call a method which allocates the required ioengine | |
145 | * structure. | |
146 | */ | |
147 | if (!ops) { | |
148 | get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine"); | |
149 | ||
150 | if (get_ioengine) | |
151 | get_ioengine(&ops); | |
152 | } | |
153 | ||
d4dbaaa8 | 154 | if (!ops) { |
e1161c32 | 155 | td_vmsg(td, -1, dlerror(), "dlsym"); |
d4dbaaa8 JA |
156 | dlclose(dlhandle); |
157 | return NULL; | |
158 | } | |
8756e4d4 | 159 | |
f6931a1d | 160 | ops->dlhandle = dlhandle; |
5f350952 JA |
161 | return ops; |
162 | } | |
163 | ||
044be36e | 164 | static struct ioengine_ops *__load_ioengine(const char *engine) |
97bb54c9 | 165 | { |
97bb54c9 TK |
166 | /* |
167 | * linux libaio has alias names, so convert to what we want | |
168 | */ | |
8bdf7a84 | 169 | if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) { |
044be36e BVA |
170 | dprint(FD_IO, "converting ioengine name: %s -> libaio\n", |
171 | engine); | |
172 | engine = "libaio"; | |
966fcbd4 | 173 | } |
97bb54c9 TK |
174 | |
175 | dprint(FD_IO, "load ioengine %s\n", engine); | |
176 | return find_ioengine(engine); | |
177 | } | |
178 | ||
4fedf59a | 179 | struct ioengine_ops *load_ioengine(struct thread_data *td) |
5f350952 | 180 | { |
81647a9a | 181 | struct ioengine_ops *ops = NULL; |
ba872a0b | 182 | const char *name; |
81647a9a | 183 | |
ba872a0b TK |
184 | /* |
185 | * Use ->ioengine_so_path if an external ioengine path is specified. | |
186 | * In this case, ->ioengine is "external" which also means the prefix | |
187 | * for external ioengines "external:" is properly used. | |
188 | */ | |
189 | name = td->o.ioengine_so_path ?: td->o.ioengine; | |
190 | ||
191 | /* | |
192 | * Try to load ->ioengine first, and if failed try to dlopen(3) either | |
193 | * ->ioengine or ->ioengine_so_path. This is redundant for an external | |
194 | * ioengine with prefix, and also leaves the possibility of unexpected | |
195 | * behavior (e.g. if the "external" ioengine exists), but we do this | |
196 | * so as not to break job files not using the prefix. | |
197 | */ | |
198 | ops = __load_ioengine(td->o.ioengine); | |
48ff7df9 ES |
199 | |
200 | /* We do re-dlopen existing handles, for reference counting */ | |
201 | if (!ops || ops->dlhandle) | |
5f350952 JA |
202 | ops = dlopen_ioengine(td, name); |
203 | ||
ba872a0b TK |
204 | /* |
205 | * If ops is NULL, we failed to load ->ioengine, and also failed to | |
206 | * dlopen(3) either ->ioengine or ->ioengine_so_path as a path. | |
207 | */ | |
5f350952 JA |
208 | if (!ops) { |
209 | log_err("fio: engine %s not loadable\n", name); | |
b902ceb5 JA |
210 | return NULL; |
211 | } | |
212 | ||
8c16d840 JA |
213 | /* |
214 | * Check that the required methods are there. | |
215 | */ | |
abfd235a | 216 | if (check_engine_ops(td, ops)) |
8c16d840 | 217 | return NULL; |
8c16d840 | 218 | |
565e784d | 219 | return ops; |
8756e4d4 JA |
220 | } |
221 | ||
de890a1e SL |
222 | /* |
223 | * For cleaning up an ioengine which never made it to init(). | |
224 | */ | |
225 | void free_ioengine(struct thread_data *td) | |
8756e4d4 | 226 | { |
6203763e DP |
227 | assert(td != NULL && td->io_ops != NULL); |
228 | ||
de890a1e | 229 | dprint(FD_IO, "free ioengine %s\n", td->io_ops->name); |
ee56ad50 | 230 | |
de890a1e SL |
231 | if (td->eo && td->io_ops->options) { |
232 | options_free(td->io_ops->options, td->eo); | |
233 | free(td->eo); | |
234 | td->eo = NULL; | |
2992b059 | 235 | } |
b990b5c0 | 236 | |
f6931a1d | 237 | if (td->io_ops->dlhandle) { |
48ff7df9 | 238 | dprint(FD_IO, "dlclose ioengine %s\n", td->io_ops->name); |
f6931a1d | 239 | dlclose(td->io_ops->dlhandle); |
9b50942e | 240 | } |
5f350952 | 241 | |
84585003 | 242 | td->io_ops = NULL; |
b990b5c0 | 243 | } |
10ba535a | 244 | |
de890a1e SL |
245 | void close_ioengine(struct thread_data *td) |
246 | { | |
247 | dprint(FD_IO, "close ioengine %s\n", td->io_ops->name); | |
248 | ||
249 | if (td->io_ops->cleanup) { | |
250 | td->io_ops->cleanup(td); | |
565e784d | 251 | td->io_ops_data = NULL; |
de890a1e SL |
252 | } |
253 | ||
254 | free_ioengine(td); | |
255 | } | |
256 | ||
10ba535a JA |
257 | int td_io_prep(struct thread_data *td, struct io_u *io_u) |
258 | { | |
ee56ad50 | 259 | dprint_io_u(io_u, "prep"); |
7101d9c2 JA |
260 | fio_ro_check(td, io_u); |
261 | ||
4d4e80f2 | 262 | lock_file(td, io_u->file, io_u->ddir); |
b2bd2bd9 | 263 | |
2ba1c290 JA |
264 | if (td->io_ops->prep) { |
265 | int ret = td->io_ops->prep(td, io_u); | |
266 | ||
e5f9a813 RE |
267 | dprint(FD_IO, "prep: io_u %p: ret=%d\n", io_u, ret); |
268 | ||
b2bd2bd9 | 269 | if (ret) |
4d4e80f2 | 270 | unlock_file(td, io_u->file); |
2ba1c290 JA |
271 | return ret; |
272 | } | |
10ba535a JA |
273 | |
274 | return 0; | |
275 | } | |
276 | ||
e7d2e616 | 277 | int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max, |
1f440ece | 278 | const struct timespec *t) |
10ba535a | 279 | { |
ee56ad50 | 280 | int r = 0; |
face81b2 | 281 | |
a05d62b2 YR |
282 | /* |
283 | * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ, | |
284 | * server side gets a message from the client | |
285 | * side that the task is finished, and | |
286 | * td->done is set to 1 after td_io_commit(). In this case, | |
287 | * there is no need to reap complete event in server side. | |
288 | */ | |
289 | if (td->done) | |
290 | return 0; | |
291 | ||
ee56ad50 JA |
292 | if (min > 0 && td->io_ops->commit) { |
293 | r = td->io_ops->commit(td); | |
face81b2 | 294 | if (r < 0) |
ee56ad50 | 295 | goto out; |
face81b2 | 296 | } |
4950421a JA |
297 | if (max > td->cur_depth) |
298 | max = td->cur_depth; | |
299 | if (min > max) | |
300 | max = min; | |
36167d82 | 301 | |
ee56ad50 | 302 | r = 0; |
4950421a | 303 | if (max && td->io_ops->getevents) |
ee56ad50 JA |
304 | r = td->io_ops->getevents(td, min, max, t); |
305 | out: | |
422f9e4b RM |
306 | if (r >= 0) { |
307 | /* | |
3fd9efbc | 308 | * Reflect that our submitted requests were retrieved with |
422f9e4b RM |
309 | * whatever OS async calls are in the underlying engine. |
310 | */ | |
311 | td->io_u_in_flight -= r; | |
838bc709 | 312 | io_u_mark_complete(td, r); |
422f9e4b | 313 | } else |
7c639b14 | 314 | td_verror(td, r, "get_events"); |
f3e11d05 | 315 | |
ee56ad50 JA |
316 | dprint(FD_IO, "getevents: %d\n", r); |
317 | return r; | |
10ba535a JA |
318 | } |
319 | ||
d3b07186 | 320 | enum fio_q_status td_io_queue(struct thread_data *td, struct io_u *io_u) |
10ba535a | 321 | { |
a9da8ab2 | 322 | const enum fio_ddir ddir = acct_ddir(io_u); |
5fff9543 | 323 | unsigned long long buflen = io_u->xfer_buflen; |
d3b07186 | 324 | enum fio_q_status ret; |
7e77dd02 | 325 | |
ee56ad50 | 326 | dprint_io_u(io_u, "queue"); |
7101d9c2 JA |
327 | fio_ro_check(td, io_u); |
328 | ||
0c6e7517 | 329 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
1651e431 | 330 | io_u_set(td, io_u, IO_U_F_FLIGHT); |
817ae977 VF |
331 | |
332 | /* | |
333 | * If overlap checking was enabled in offload mode we | |
334 | * can release this lock that was acquired when we | |
335 | * started the overlap check because the IO_U_F_FLIGHT | |
336 | * flag is now set | |
337 | */ | |
a38c70a8 | 338 | if (td_offload_overlap(td)) { |
83276370 DP |
339 | int res; |
340 | ||
341 | res = pthread_mutex_unlock(&overlap_check); | |
342 | if (fio_unlikely(res != 0)) { | |
343 | log_err("failed to unlock overlap check mutex, err: %i:%s", errno, strerror(errno)); | |
344 | abort(); | |
345 | } | |
a38c70a8 | 346 | } |
0c6e7517 | 347 | |
d6aed795 | 348 | assert(fio_file_open(io_u->file)); |
3d7b485f | 349 | |
bcd5abfa JA |
350 | /* |
351 | * If using a write iolog, store this entry. | |
352 | */ | |
353 | log_io_u(td, io_u); | |
354 | ||
11786802 JA |
355 | io_u->error = 0; |
356 | io_u->resid = 0; | |
357 | ||
04ba61df VF |
358 | if (td_ioengine_flagged(td, FIO_SYNCIO) || |
359 | (td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) && | |
360 | io_u->ddir == DDIR_TRIM)) { | |
12d9d841 | 361 | if (fio_fill_issue_time(td)) |
9520ebb9 | 362 | fio_gettime(&io_u->issue_time, NULL); |
d0c15328 JA |
363 | |
364 | /* | |
365 | * only used for iolog | |
366 | */ | |
367 | if (td->o.read_iolog_file) | |
368 | memcpy(&td->last_issue, &io_u->issue_time, | |
8b6a404c | 369 | sizeof(io_u->issue_time)); |
433afcb4 JA |
370 | } |
371 | ||
b2a432bf | 372 | |
a9da8ab2 | 373 | if (ddir_rw(ddir)) { |
a7d01f02 JA |
374 | if (!(io_u->flags & IO_U_F_VER_LIST)) { |
375 | td->io_issues[ddir]++; | |
376 | td->io_issue_bytes[ddir] += buflen; | |
377 | } | |
50a8ce86 | 378 | td->rate_io_issue_bytes[ddir] += buflen; |
74d6277f | 379 | } |
755200a3 | 380 | |
7e77dd02 | 381 | ret = td->io_ops->queue(td, io_u); |
b2da58c4 | 382 | zbd_queue_io_u(td, io_u, ret); |
5aeb77df | 383 | |
4d4e80f2 | 384 | unlock_file(td, io_u->file); |
b2bd2bd9 | 385 | |
a9da8ab2 JA |
386 | if (ret == FIO_Q_BUSY && ddir_rw(ddir)) { |
387 | td->io_issues[ddir]--; | |
388 | td->io_issue_bytes[ddir] -= buflen; | |
50a8ce86 | 389 | td->rate_io_issue_bytes[ddir] -= buflen; |
871467d9 | 390 | io_u_clear(td, io_u, IO_U_F_FLIGHT); |
cd8a19e6 JA |
391 | } |
392 | ||
39a43d34 JA |
393 | /* |
394 | * If an error was seen and the io engine didn't propagate it | |
395 | * back to 'td', do so. | |
396 | */ | |
397 | if (io_u->error && !td->error) | |
398 | td_verror(td, io_u->error, "td_io_queue"); | |
399 | ||
cb211682 JA |
400 | /* |
401 | * Add warning for O_DIRECT so that users have an easier time | |
402 | * spotting potentially bad alignment. If this triggers for the first | |
403 | * IO, then it's likely an alignment problem or because the host fs | |
404 | * does not support O_DIRECT | |
405 | */ | |
ff58fced | 406 | if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 && |
cb211682 | 407 | td->o.odirect) { |
214ac7e0 | 408 | |
cb211682 | 409 | log_info("fio: first direct IO errored. File system may not " |
78d4a262 TK |
410 | "support direct IO, or iomem_align= is bad, or " |
411 | "invalid block size. Try setting direct=0.\n"); | |
cb211682 JA |
412 | } |
413 | ||
bfbdd35b BVA |
414 | if (zbd_unaligned_write(io_u->error) && |
415 | td->io_issues[io_u->ddir & 1] == 1 && | |
416 | td->o.zone_mode != ZONE_MODE_ZBD) { | |
417 | log_info("fio: first I/O failed. If %s is a zoned block device, consider --zonemode=zbd\n", | |
418 | io_u->file->file_name); | |
419 | } | |
420 | ||
c0681c9d | 421 | if (!td->io_ops->commit) { |
838bc709 JA |
422 | io_u_mark_submit(td, 1); |
423 | io_u_mark_complete(td, 1); | |
424 | } | |
425 | ||
d8005759 | 426 | if (ret == FIO_Q_COMPLETED) { |
00615bfb SW |
427 | if (ddir_rw(io_u->ddir) || |
428 | (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING)) { | |
d8005759 JA |
429 | io_u_mark_depth(td, 1); |
430 | td->ts.total_io_u[io_u->ddir]++; | |
6eaf09d6 | 431 | } |
d8005759 | 432 | } else if (ret == FIO_Q_QUEUED) { |
e6727cbd JA |
433 | td->io_u_queued++; |
434 | ||
00615bfb SW |
435 | if (ddir_rw(io_u->ddir) || |
436 | (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING)) | |
d8005759 | 437 | td->ts.total_io_u[io_u->ddir]++; |
d8005759 | 438 | |
a80cb54b BVA |
439 | if (td->io_u_queued >= td->o.iodepth_batch) |
440 | td_io_commit(td); | |
eb7c8ae2 | 441 | } |
cb5ab512 | 442 | |
04ba61df VF |
443 | if (!td_ioengine_flagged(td, FIO_SYNCIO) && |
444 | (!td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) || | |
445 | io_u->ddir != DDIR_TRIM)) { | |
12d9d841 | 446 | if (fio_fill_issue_time(td)) |
9520ebb9 | 447 | fio_gettime(&io_u->issue_time, NULL); |
d0c15328 JA |
448 | |
449 | /* | |
450 | * only used for iolog | |
451 | */ | |
452 | if (td->o.read_iolog_file) | |
453 | memcpy(&td->last_issue, &io_u->issue_time, | |
8b6a404c | 454 | sizeof(io_u->issue_time)); |
433afcb4 JA |
455 | } |
456 | ||
7e77dd02 | 457 | return ret; |
10ba535a | 458 | } |
8c16d840 JA |
459 | |
460 | int td_io_init(struct thread_data *td) | |
461 | { | |
eeb12160 | 462 | int ret = 0; |
8c16d840 | 463 | |
eeb12160 JA |
464 | if (td->io_ops->init) { |
465 | ret = td->io_ops->init(td); | |
356ef1a1 TK |
466 | if (ret) |
467 | log_err("fio: io engine %s init failed.%s\n", | |
468 | td->io_ops->name, | |
469 | td->o.iodepth > 1 ? | |
470 | " Perhaps try reducing io depth?" : ""); | |
471 | else | |
472 | td->io_ops_init = 1; | |
7c973896 JA |
473 | if (!td->error) |
474 | td->error = ret; | |
eeb12160 JA |
475 | } |
476 | ||
477 | return ret; | |
8c16d840 | 478 | } |
755200a3 | 479 | |
a80cb54b | 480 | void td_io_commit(struct thread_data *td) |
755200a3 | 481 | { |
f3e11d05 JA |
482 | int ret; |
483 | ||
ee56ad50 JA |
484 | dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth); |
485 | ||
d8005759 | 486 | if (!td->cur_depth || !td->io_u_queued) |
a80cb54b | 487 | return; |
cb5ab512 | 488 | |
3fd9efbc | 489 | io_u_mark_depth(td, td->io_u_queued); |
d8005759 | 490 | |
f3e11d05 JA |
491 | if (td->io_ops->commit) { |
492 | ret = td->io_ops->commit(td); | |
493 | if (ret) | |
494 | td_verror(td, -ret, "io commit"); | |
495 | } | |
3fd9efbc | 496 | |
422f9e4b RM |
497 | /* |
498 | * Reflect that events were submitted as async IO requests. | |
499 | */ | |
500 | td->io_u_in_flight += td->io_u_queued; | |
501 | td->io_u_queued = 0; | |
755200a3 | 502 | } |
b5af8293 JA |
503 | |
504 | int td_io_open_file(struct thread_data *td, struct fio_file *f) | |
505 | { | |
230f33fb AK |
506 | if (fio_file_closing(f)) { |
507 | /* | |
508 | * Open translates to undo closing. | |
509 | */ | |
510 | fio_file_clear_closing(f); | |
511 | get_file(f); | |
512 | return 0; | |
513 | } | |
22a57ba8 JA |
514 | assert(!fio_file_open(f)); |
515 | assert(f->fd == -1); | |
966fcbd4 | 516 | assert(td->io_ops->open_file); |
22a57ba8 | 517 | |
413d6693 JA |
518 | if (td->io_ops->open_file(td, f)) { |
519 | if (td->error == EINVAL && td->o.odirect) | |
520 | log_err("fio: destination does not support O_DIRECT\n"); | |
5ec10eaa JA |
521 | if (td->error == EMFILE) { |
522 | log_err("fio: try reducing/setting openfiles (failed" | |
523 | " at %u of %u)\n", td->nr_open_files, | |
524 | td->o.nr_files); | |
525 | } | |
413d6693 | 526 | |
22a57ba8 JA |
527 | assert(f->fd == -1); |
528 | assert(!fio_file_open(f)); | |
413d6693 JA |
529 | return 1; |
530 | } | |
531 | ||
33c48814 | 532 | fio_file_reset(td, f); |
d6aed795 JA |
533 | fio_file_set_open(f); |
534 | fio_file_clear_closing(f); | |
c97bd0fa | 535 | disk_util_inc(f->du); |
d5707a35 JA |
536 | |
537 | td->nr_open_files++; | |
538 | get_file(f); | |
539 | ||
66159828 JA |
540 | if (f->filetype == FIO_TYPE_PIPE) { |
541 | if (td_random(td)) { | |
542 | log_err("fio: can't seek on pipes (no random io)\n"); | |
543 | goto err; | |
544 | } | |
545 | } | |
546 | ||
9b87f09b | 547 | if (td_ioengine_flagged(td, FIO_DISKLESSIO)) |
413d6693 JA |
548 | goto done; |
549 | ||
550 | if (td->o.invalidate_cache && file_invalidate_cache(td, f)) | |
551 | goto err; | |
552 | ||
ecb2083d | 553 | if (td->o.fadvise_hint != F_ADV_NONE && |
686fbd31 | 554 | (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) { |
413d6693 JA |
555 | int flags; |
556 | ||
ecb2083d JA |
557 | if (td->o.fadvise_hint == F_ADV_TYPE) { |
558 | if (td_random(td)) | |
559 | flags = POSIX_FADV_RANDOM; | |
560 | else | |
561 | flags = POSIX_FADV_SEQUENTIAL; | |
562 | } else if (td->o.fadvise_hint == F_ADV_RANDOM) | |
413d6693 | 563 | flags = POSIX_FADV_RANDOM; |
ecb2083d | 564 | else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL) |
413d6693 | 565 | flags = POSIX_FADV_SEQUENTIAL; |
ecb2083d JA |
566 | else { |
567 | log_err("fio: unknown fadvise type %d\n", | |
568 | td->o.fadvise_hint); | |
569 | flags = POSIX_FADV_NORMAL; | |
570 | } | |
413d6693 | 571 | |
ecc314ba | 572 | if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) { |
825b0a75 JA |
573 | if (!fio_did_warn(FIO_WARN_FADVISE)) |
574 | log_err("fio: fadvise hint failed\n"); | |
413d6693 | 575 | } |
7bb48f84 | 576 | } |
ae8e559e JA |
577 | #ifdef FIO_HAVE_WRITE_HINT |
578 | if (fio_option_is_set(&td->o, write_hint) && | |
686fbd31 | 579 | (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) { |
ae8e559e | 580 | uint64_t hint = td->o.write_hint; |
bd553af6 | 581 | int cmd; |
37659335 | 582 | |
bd553af6 JA |
583 | /* |
584 | * For direct IO, we just need/want to set the hint on | |
585 | * the file descriptor. For buffered IO, we need to set | |
586 | * it on the inode. | |
587 | */ | |
588 | if (td->o.odirect) | |
589 | cmd = F_SET_FILE_RW_HINT; | |
590 | else | |
591 | cmd = F_SET_RW_HINT; | |
592 | ||
593 | if (fcntl(f->fd, cmd, &hint) < 0) { | |
ae8e559e | 594 | td_verror(td, errno, "fcntl write hint"); |
37659335 JA |
595 | goto err; |
596 | } | |
597 | } | |
598 | #endif | |
a978ba68 | 599 | |
47534cda TK |
600 | if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f)) |
601 | goto err; | |
e116f2b9 | 602 | |
413d6693 | 603 | done: |
f29b25a3 | 604 | log_file(td, f, FIO_LOG_OPEN_FILE); |
413d6693 JA |
605 | return 0; |
606 | err: | |
c97bd0fa | 607 | disk_util_dec(f->du); |
b284075a JA |
608 | if (td->io_ops->close_file) |
609 | td->io_ops->close_file(td, f); | |
7bb48f84 | 610 | return 1; |
b5af8293 JA |
611 | } |
612 | ||
6977bcd0 | 613 | int td_io_close_file(struct thread_data *td, struct fio_file *f) |
b5af8293 | 614 | { |
d6aed795 | 615 | if (!fio_file_closing(f)) |
f29b25a3 JA |
616 | log_file(td, f, FIO_LOG_CLOSE_FILE); |
617 | ||
0ad920e7 JA |
618 | /* |
619 | * mark as closing, do real close when last io on it has completed | |
620 | */ | |
d6aed795 | 621 | fio_file_set_closing(f); |
0ad920e7 | 622 | |
6977bcd0 | 623 | return put_file(td, f); |
b5af8293 | 624 | } |
df9c26b1 | 625 | |
38ef9c90 CF |
626 | int td_io_unlink_file(struct thread_data *td, struct fio_file *f) |
627 | { | |
628 | if (td->io_ops->unlink_file) | |
629 | return td->io_ops->unlink_file(td, f); | |
2442c935 JA |
630 | else { |
631 | int ret; | |
632 | ||
633 | ret = unlink(f->file_name); | |
634 | if (ret < 0) | |
635 | return errno; | |
636 | ||
637 | return 0; | |
638 | } | |
38ef9c90 CF |
639 | } |
640 | ||
df9c26b1 JA |
641 | int td_io_get_file_size(struct thread_data *td, struct fio_file *f) |
642 | { | |
643 | if (!td->io_ops->get_file_size) | |
644 | return 0; | |
645 | ||
646 | return td->io_ops->get_file_size(td, f); | |
647 | } | |
44f29692 | 648 | |
35300d5f ES |
649 | #ifdef CONFIG_DYNAMIC_ENGINES |
650 | /* Load all dynamic engines in FIO_EXT_ENG_DIR for enghelp command */ | |
651 | static void | |
652 | fio_load_dynamic_engines(struct thread_data *td) | |
653 | { | |
654 | DIR *dirhandle = NULL; | |
655 | struct dirent *dirent = NULL; | |
656 | char engine_path[PATH_MAX]; | |
657 | ||
658 | dirhandle = opendir(FIO_EXT_ENG_DIR); | |
659 | if (!dirhandle) | |
660 | return; | |
661 | ||
662 | while ((dirent = readdir(dirhandle)) != NULL) { | |
663 | if (!strcmp(dirent->d_name, ".") || | |
664 | !strcmp(dirent->d_name, "..")) | |
665 | continue; | |
666 | ||
667 | sprintf(engine_path, "%s/%s", FIO_EXT_ENG_DIR, dirent->d_name); | |
668 | dlopen_ioengine(td, engine_path); | |
669 | } | |
670 | ||
671 | closedir(dirhandle); | |
672 | } | |
673 | #else | |
674 | #define fio_load_dynamic_engines(td) do { } while (0) | |
675 | #endif | |
676 | ||
de890a1e SL |
677 | int fio_show_ioengine_help(const char *engine) |
678 | { | |
679 | struct flist_head *entry; | |
d04f1d5b | 680 | struct thread_data td; |
755dcbbd | 681 | struct ioengine_ops *io_ops; |
de890a1e SL |
682 | char *sep; |
683 | int ret = 1; | |
684 | ||
35300d5f ES |
685 | memset(&td, 0, sizeof(struct thread_data)); |
686 | ||
de890a1e SL |
687 | if (!engine || !*engine) { |
688 | log_info("Available IO engines:\n"); | |
35300d5f | 689 | fio_load_dynamic_engines(&td); |
de890a1e | 690 | flist_for_each(entry, &engine_list) { |
755dcbbd TK |
691 | io_ops = flist_entry(entry, struct ioengine_ops, list); |
692 | log_info("\t%s\n", io_ops->name); | |
de890a1e SL |
693 | } |
694 | return 0; | |
695 | } | |
696 | sep = strchr(engine, ','); | |
697 | if (sep) { | |
698 | *sep = 0; | |
699 | sep++; | |
700 | } | |
701 | ||
d04f1d5b | 702 | td.o.ioengine = (char *)engine; |
2459bd33 | 703 | td.io_ops = load_ioengine(&td); |
d04f1d5b | 704 | |
2459bd33 | 705 | if (!td.io_ops) { |
de890a1e SL |
706 | log_info("IO engine %s not found\n", engine); |
707 | return 1; | |
708 | } | |
709 | ||
2459bd33 VF |
710 | if (td.io_ops->options) |
711 | ret = show_cmd_help(td.io_ops->options, sep); | |
de890a1e | 712 | else |
2459bd33 | 713 | log_info("IO engine %s has no options\n", td.io_ops->name); |
de890a1e | 714 | |
d04f1d5b | 715 | free_ioengine(&td); |
de890a1e SL |
716 | return ret; |
717 | } |