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