Commit | Line | Data |
---|---|---|
906c8d75 | 1 | /* |
cb2c86fd | 2 | * This file contains job initialization and setup functions. |
906c8d75 | 3 | */ |
ebac4655 JA |
4 | #include <stdio.h> |
5 | #include <stdlib.h> | |
6 | #include <unistd.h> | |
7 | #include <fcntl.h> | |
8 | #include <ctype.h> | |
9 | #include <string.h> | |
10 | #include <errno.h> | |
11 | #include <sys/ipc.h> | |
12 | #include <sys/shm.h> | |
13 | #include <sys/types.h> | |
14 | #include <sys/stat.h> | |
15 | ||
16 | #include "fio.h" | |
cb2c86fd | 17 | #include "parse.h" |
2e5cdb11 | 18 | #include "smalloc.h" |
380065aa | 19 | #include "filehash.h" |
4f5af7b2 | 20 | #include "verify.h" |
79d16311 | 21 | #include "profile.h" |
ebac4655 | 22 | |
bf2e821a CC |
23 | #include "lib/getopt.h" |
24 | ||
173dbaa4 | 25 | static char fio_version_string[] = "fio 1.57"; |
214e1eca | 26 | |
ee738499 | 27 | #define FIO_RANDSEED (0xb1899bedUL) |
ebac4655 | 28 | |
214e1eca | 29 | static char **ini_file; |
fca70358 | 30 | static int max_jobs = FIO_MAX_JOBS; |
cca73aa7 | 31 | static int dump_cmdline; |
e1f36503 | 32 | |
be4ecfdf | 33 | static struct thread_data def_thread; |
214e1eca | 34 | struct thread_data *threads = NULL; |
e1f36503 | 35 | |
214e1eca JA |
36 | int exitall_on_terminate = 0; |
37 | int terse_output = 0; | |
e592a06b | 38 | int eta_print; |
214e1eca JA |
39 | unsigned long long mlock_size = 0; |
40 | FILE *f_out = NULL; | |
41 | FILE *f_err = NULL; | |
ad0a2735 JA |
42 | char **job_sections = NULL; |
43 | int nr_job_sections = 0; | |
07b3232d | 44 | char *exec_profile = NULL; |
a9523c6f | 45 | int warnings_fatal = 0; |
ee738499 | 46 | |
214e1eca | 47 | int write_bw_log = 0; |
4241ea8f | 48 | int read_only = 0; |
214e1eca | 49 | |
5ec10eaa | 50 | static int write_lat_log; |
e1f36503 | 51 | |
214e1eca | 52 | static int prev_group_jobs; |
b4692828 | 53 | |
ee56ad50 | 54 | unsigned long fio_debug = 0; |
5e1d306e JA |
55 | unsigned int fio_debug_jobno = -1; |
56 | unsigned int *fio_debug_jobp = NULL; | |
ee56ad50 | 57 | |
4c6107ff JA |
58 | static char cmd_optstr[256]; |
59 | ||
b4692828 JA |
60 | /* |
61 | * Command line options. These will contain the above, plus a few | |
62 | * extra that only pertain to fio itself and not jobs. | |
63 | */ | |
5ec10eaa | 64 | static struct option l_opts[FIO_NR_OPTIONS] = { |
b4692828 | 65 | { |
08d2a19c | 66 | .name = (char *) "output", |
b4692828 JA |
67 | .has_arg = required_argument, |
68 | .val = 'o', | |
69 | }, | |
70 | { | |
08d2a19c | 71 | .name = (char *) "timeout", |
b4692828 JA |
72 | .has_arg = required_argument, |
73 | .val = 't', | |
74 | }, | |
75 | { | |
08d2a19c | 76 | .name = (char *) "latency-log", |
b4692828 JA |
77 | .has_arg = required_argument, |
78 | .val = 'l', | |
79 | }, | |
80 | { | |
08d2a19c | 81 | .name = (char *) "bandwidth-log", |
b4692828 JA |
82 | .has_arg = required_argument, |
83 | .val = 'b', | |
84 | }, | |
85 | { | |
08d2a19c | 86 | .name = (char *) "minimal", |
b4692828 JA |
87 | .has_arg = optional_argument, |
88 | .val = 'm', | |
89 | }, | |
90 | { | |
08d2a19c | 91 | .name = (char *) "version", |
b4692828 JA |
92 | .has_arg = no_argument, |
93 | .val = 'v', | |
94 | }, | |
fd28ca49 | 95 | { |
08d2a19c | 96 | .name = (char *) "help", |
fd28ca49 JA |
97 | .has_arg = no_argument, |
98 | .val = 'h', | |
99 | }, | |
100 | { | |
08d2a19c | 101 | .name = (char *) "cmdhelp", |
320beefe | 102 | .has_arg = optional_argument, |
fd28ca49 JA |
103 | .val = 'c', |
104 | }, | |
cca73aa7 | 105 | { |
08d2a19c | 106 | .name = (char *) "showcmd", |
cca73aa7 | 107 | .has_arg = no_argument, |
724e4435 JA |
108 | .val = 's', |
109 | }, | |
110 | { | |
08d2a19c | 111 | .name = (char *) "readonly", |
724e4435 JA |
112 | .has_arg = no_argument, |
113 | .val = 'r', | |
cca73aa7 | 114 | }, |
e592a06b | 115 | { |
08d2a19c | 116 | .name = (char *) "eta", |
e592a06b AC |
117 | .has_arg = required_argument, |
118 | .val = 'e', | |
119 | }, | |
ee56ad50 | 120 | { |
08d2a19c | 121 | .name = (char *) "debug", |
ee56ad50 JA |
122 | .has_arg = required_argument, |
123 | .val = 'd', | |
124 | }, | |
01f06b63 | 125 | { |
08d2a19c | 126 | .name = (char *) "section", |
01f06b63 JA |
127 | .has_arg = required_argument, |
128 | .val = 'x', | |
129 | }, | |
2b386d25 | 130 | { |
08d2a19c | 131 | .name = (char *) "alloc-size", |
2b386d25 JA |
132 | .has_arg = required_argument, |
133 | .val = 'a', | |
134 | }, | |
9ac8a797 | 135 | { |
08d2a19c | 136 | .name = (char *) "profile", |
9ac8a797 JA |
137 | .has_arg = required_argument, |
138 | .val = 'p', | |
139 | }, | |
a9523c6f | 140 | { |
08d2a19c | 141 | .name = (char *) "warnings-fatal", |
a9523c6f JA |
142 | .has_arg = no_argument, |
143 | .val = 'w', | |
144 | }, | |
fca70358 JA |
145 | { |
146 | .name = (char *) "max-jobs", | |
147 | .has_arg = required_argument, | |
148 | .val = 'j', | |
149 | }, | |
b4692828 JA |
150 | { |
151 | .name = NULL, | |
152 | }, | |
153 | }; | |
154 | ||
9728ce37 JB |
155 | FILE *get_f_out() |
156 | { | |
157 | return f_out; | |
158 | } | |
159 | ||
160 | FILE *get_f_err() | |
161 | { | |
162 | return f_err; | |
163 | } | |
164 | ||
906c8d75 JA |
165 | /* |
166 | * Return a free job structure. | |
167 | */ | |
ebac4655 JA |
168 | static struct thread_data *get_new_job(int global, struct thread_data *parent) |
169 | { | |
170 | struct thread_data *td; | |
171 | ||
172 | if (global) | |
173 | return &def_thread; | |
e61f1ec8 BC |
174 | if (thread_number >= max_jobs) { |
175 | log_err("error: maximum number of jobs (%d) reached.\n", | |
176 | max_jobs); | |
ebac4655 | 177 | return NULL; |
e61f1ec8 | 178 | } |
ebac4655 JA |
179 | |
180 | td = &threads[thread_number++]; | |
ddaeaa5a | 181 | *td = *parent; |
ebac4655 | 182 | |
e0b0d892 JA |
183 | td->o.uid = td->o.gid = -1U; |
184 | ||
cade3ef4 | 185 | dup_files(td, parent); |
d23bb327 | 186 | options_mem_dupe(td); |
cade3ef4 | 187 | |
15dc1934 JA |
188 | profile_add_hooks(td); |
189 | ||
ebac4655 | 190 | td->thread_number = thread_number; |
ebac4655 JA |
191 | return td; |
192 | } | |
193 | ||
194 | static void put_job(struct thread_data *td) | |
195 | { | |
549577a7 JA |
196 | if (td == &def_thread) |
197 | return; | |
84dd1886 | 198 | |
58c55ba0 | 199 | profile_td_exit(td); |
549577a7 | 200 | |
16edf25d | 201 | if (td->error) |
6d86144d | 202 | log_info("fio: %s\n", td->verror); |
16edf25d | 203 | |
ebac4655 JA |
204 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); |
205 | thread_number--; | |
206 | } | |
207 | ||
581e7141 | 208 | static int __setup_rate(struct thread_data *td, enum fio_ddir ddir) |
127f6865 | 209 | { |
581e7141 | 210 | unsigned int bs = td->o.min_bs[ddir]; |
ba3e4e0c | 211 | unsigned long long bytes_per_sec; |
127f6865 | 212 | |
ff58fced JA |
213 | assert(ddir_rw(ddir)); |
214 | ||
ba3e4e0c RR |
215 | if (td->o.rate[ddir]) |
216 | bytes_per_sec = td->o.rate[ddir]; | |
217 | else | |
218 | bytes_per_sec = td->o.rate_iops[ddir] * bs; | |
127f6865 | 219 | |
ba3e4e0c | 220 | if (!bytes_per_sec) { |
127f6865 JA |
221 | log_err("rate lower than supported\n"); |
222 | return -1; | |
223 | } | |
224 | ||
ba3e4e0c | 225 | td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec; |
581e7141 | 226 | td->rate_pending_usleep[ddir] = 0; |
127f6865 JA |
227 | return 0; |
228 | } | |
229 | ||
581e7141 JA |
230 | static int setup_rate(struct thread_data *td) |
231 | { | |
232 | int ret = 0; | |
233 | ||
234 | if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ]) | |
235 | ret = __setup_rate(td, DDIR_READ); | |
236 | if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE]) | |
237 | ret |= __setup_rate(td, DDIR_WRITE); | |
238 | ||
239 | return ret; | |
240 | } | |
241 | ||
8347239a JA |
242 | static int fixed_block_size(struct thread_options *o) |
243 | { | |
244 | return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] && | |
245 | o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] && | |
246 | o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE]; | |
247 | } | |
248 | ||
dad915e3 JA |
249 | /* |
250 | * Lazy way of fixing up options that depend on each other. We could also | |
251 | * define option callback handlers, but this is easier. | |
252 | */ | |
4e991c23 | 253 | static int fixup_options(struct thread_data *td) |
e1f36503 | 254 | { |
2dc1bbeb | 255 | struct thread_options *o = &td->o; |
ff217451 | 256 | int ret = 0; |
dad915e3 | 257 | |
f356d01d | 258 | #ifndef FIO_HAVE_PSHARED_MUTEX |
9bbf57cc | 259 | if (!o->use_thread) { |
f356d01d JA |
260 | log_info("fio: this platform does not support process shared" |
261 | " mutexes, forcing use of threads. Use the 'thread'" | |
262 | " option to get rid of this warning.\n"); | |
9bbf57cc | 263 | o->use_thread = 1; |
a9523c6f | 264 | ret = warnings_fatal; |
f356d01d JA |
265 | } |
266 | #endif | |
267 | ||
2dc1bbeb | 268 | if (o->write_iolog_file && o->read_iolog_file) { |
076efc7c | 269 | log_err("fio: read iolog overrides write_iolog\n"); |
2dc1bbeb JA |
270 | free(o->write_iolog_file); |
271 | o->write_iolog_file = NULL; | |
a9523c6f | 272 | ret = warnings_fatal; |
076efc7c | 273 | } |
16b462ae | 274 | |
16b462ae JA |
275 | /* |
276 | * only really works for sequential io for now, and with 1 file | |
277 | */ | |
2dc1bbeb JA |
278 | if (o->zone_size && td_random(td) && o->open_files == 1) |
279 | o->zone_size = 0; | |
16b462ae JA |
280 | |
281 | /* | |
282 | * Reads can do overwrites, we always need to pre-create the file | |
283 | */ | |
284 | if (td_read(td) || td_rw(td)) | |
2dc1bbeb | 285 | o->overwrite = 1; |
16b462ae | 286 | |
2dc1bbeb | 287 | if (!o->min_bs[DDIR_READ]) |
5ec10eaa | 288 | o->min_bs[DDIR_READ] = o->bs[DDIR_READ]; |
2dc1bbeb JA |
289 | if (!o->max_bs[DDIR_READ]) |
290 | o->max_bs[DDIR_READ] = o->bs[DDIR_READ]; | |
291 | if (!o->min_bs[DDIR_WRITE]) | |
5ec10eaa | 292 | o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE]; |
2dc1bbeb JA |
293 | if (!o->max_bs[DDIR_WRITE]) |
294 | o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE]; | |
a00735e6 | 295 | |
2dc1bbeb | 296 | o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]); |
a00735e6 | 297 | |
2b7a01d0 JA |
298 | /* |
299 | * For random IO, allow blockalign offset other than min_bs. | |
300 | */ | |
301 | if (!o->ba[DDIR_READ] || !td_random(td)) | |
302 | o->ba[DDIR_READ] = o->min_bs[DDIR_READ]; | |
303 | if (!o->ba[DDIR_WRITE] || !td_random(td)) | |
304 | o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE]; | |
305 | ||
306 | if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] || | |
307 | o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) && | |
9bbf57cc | 308 | !o->norandommap) { |
2b7a01d0 | 309 | log_err("fio: Any use of blockalign= turns off randommap\n"); |
9bbf57cc | 310 | o->norandommap = 1; |
a9523c6f | 311 | ret = warnings_fatal; |
2b7a01d0 JA |
312 | } |
313 | ||
2dc1bbeb JA |
314 | if (!o->file_size_high) |
315 | o->file_size_high = o->file_size_low; | |
9c60ce64 | 316 | |
8347239a JA |
317 | if (o->norandommap && o->verify != VERIFY_NONE |
318 | && !fixed_block_size(o)) { | |
319 | log_err("fio: norandommap given for variable block sizes, " | |
320 | "verify disabled\n"); | |
2dc1bbeb | 321 | o->verify = VERIFY_NONE; |
a9523c6f | 322 | ret = warnings_fatal; |
bb8895e0 | 323 | } |
2dc1bbeb | 324 | if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO)) |
690adba3 | 325 | log_err("fio: bs_unaligned may not work with raw io\n"); |
e0a22335 | 326 | |
48097d5c JA |
327 | /* |
328 | * thinktime_spin must be less than thinktime | |
329 | */ | |
2dc1bbeb JA |
330 | if (o->thinktime_spin > o->thinktime) |
331 | o->thinktime_spin = o->thinktime; | |
e916b390 JA |
332 | |
333 | /* | |
334 | * The low water mark cannot be bigger than the iodepth | |
335 | */ | |
2dc1bbeb | 336 | if (o->iodepth_low > o->iodepth || !o->iodepth_low) { |
9467b77c JA |
337 | /* |
338 | * syslet work around - if the workload is sequential, | |
339 | * we want to let the queue drain all the way down to | |
340 | * avoid seeking between async threads | |
341 | */ | |
342 | if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td)) | |
2dc1bbeb | 343 | o->iodepth_low = 1; |
9467b77c | 344 | else |
2dc1bbeb | 345 | o->iodepth_low = o->iodepth; |
9467b77c | 346 | } |
cb5ab512 JA |
347 | |
348 | /* | |
349 | * If batch number isn't set, default to the same as iodepth | |
350 | */ | |
2dc1bbeb JA |
351 | if (o->iodepth_batch > o->iodepth || !o->iodepth_batch) |
352 | o->iodepth_batch = o->iodepth; | |
b5af8293 | 353 | |
2dc1bbeb JA |
354 | if (o->nr_files > td->files_index) |
355 | o->nr_files = td->files_index; | |
9f9214f2 | 356 | |
2dc1bbeb JA |
357 | if (o->open_files > o->nr_files || !o->open_files) |
358 | o->open_files = o->nr_files; | |
4e991c23 | 359 | |
581e7141 JA |
360 | if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))|| |
361 | ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] + | |
362 | o->rate_iops_min[1]))) { | |
4e991c23 | 363 | log_err("fio: rate and rate_iops are mutually exclusive\n"); |
a9523c6f | 364 | ret = 1; |
4e991c23 | 365 | } |
581e7141 JA |
366 | if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) || |
367 | (o->rate_iops[0] < o->rate_iops_min[0]) || | |
368 | (o->rate_iops[1] < o->rate_iops_min[1])) { | |
4e991c23 | 369 | log_err("fio: minimum rate exceeds rate\n"); |
a9523c6f | 370 | ret = 1; |
4e991c23 JA |
371 | } |
372 | ||
cf4464ca JA |
373 | if (!o->timeout && o->time_based) { |
374 | log_err("fio: time_based requires a runtime/timeout setting\n"); | |
375 | o->time_based = 0; | |
a9523c6f | 376 | ret = warnings_fatal; |
cf4464ca JA |
377 | } |
378 | ||
aa31f1f1 | 379 | if (o->fill_device && !o->size) |
5921e80c | 380 | o->size = -1ULL; |
5ec10eaa | 381 | |
9bbf57cc | 382 | if (o->verify != VERIFY_NONE) { |
996936f9 | 383 | if (td_write(td) && o->do_verify && o->numjobs > 1) { |
a9523c6f JA |
384 | log_info("Multiple writers may overwrite blocks that " |
385 | "belong to other jobs. This can cause " | |
386 | "verification failures.\n"); | |
387 | ret = warnings_fatal; | |
388 | } | |
389 | ||
9bbf57cc | 390 | o->refill_buffers = 1; |
2f1b8e8b JA |
391 | if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] && |
392 | !o->verify_interval) | |
393 | o->verify_interval = o->min_bs[DDIR_WRITE]; | |
d990588e | 394 | } |
41ccd845 | 395 | |
9bbf57cc JA |
396 | if (o->pre_read) { |
397 | o->invalidate_cache = 0; | |
a9523c6f | 398 | if (td->io_ops->flags & FIO_PIPEIO) { |
9c0d2241 JA |
399 | log_info("fio: cannot pre-read files with an IO engine" |
400 | " that isn't seekable. Pre-read disabled.\n"); | |
a9523c6f JA |
401 | ret = warnings_fatal; |
402 | } | |
9c0d2241 | 403 | } |
34f1c044 | 404 | |
e72fa4d4 | 405 | #ifndef FIO_HAVE_FDATASYNC |
9bbf57cc | 406 | if (o->fdatasync_blocks) { |
e72fa4d4 JA |
407 | log_info("fio: this platform does not support fdatasync()" |
408 | " falling back to using fsync(). Use the 'fsync'" | |
409 | " option instead of 'fdatasync' to get rid of" | |
410 | " this warning\n"); | |
9bbf57cc JA |
411 | o->fsync_blocks = o->fdatasync_blocks; |
412 | o->fdatasync_blocks = 0; | |
a9523c6f | 413 | ret = warnings_fatal; |
e72fa4d4 JA |
414 | } |
415 | #endif | |
416 | ||
a9523c6f | 417 | return ret; |
e1f36503 JA |
418 | } |
419 | ||
f8977ee6 JA |
420 | /* |
421 | * This function leaks the buffer | |
422 | */ | |
423 | static char *to_kmg(unsigned int val) | |
424 | { | |
425 | char *buf = malloc(32); | |
f3502ba2 | 426 | char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 }; |
f8977ee6 JA |
427 | char *p = post; |
428 | ||
245142ff | 429 | do { |
f8977ee6 JA |
430 | if (val & 1023) |
431 | break; | |
432 | ||
433 | val >>= 10; | |
434 | p++; | |
245142ff | 435 | } while (*p); |
f8977ee6 JA |
436 | |
437 | snprintf(buf, 31, "%u%c", val, *p); | |
438 | return buf; | |
439 | } | |
440 | ||
09629a90 JA |
441 | /* External engines are specified by "external:name.o") */ |
442 | static const char *get_engine_name(const char *str) | |
443 | { | |
444 | char *p = strstr(str, ":"); | |
445 | ||
446 | if (!p) | |
447 | return str; | |
448 | ||
449 | p++; | |
450 | strip_blank_front(&p); | |
451 | strip_blank_end(p); | |
452 | return p; | |
453 | } | |
454 | ||
e132cbae JA |
455 | static int exists_and_not_file(const char *filename) |
456 | { | |
457 | struct stat sb; | |
458 | ||
459 | if (lstat(filename, &sb) == -1) | |
460 | return 0; | |
461 | ||
ecc314ba BC |
462 | /* \\.\ is the device namespace in Windows, where every file |
463 | * is a device node */ | |
464 | if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0) | |
e132cbae JA |
465 | return 0; |
466 | ||
467 | return 1; | |
468 | } | |
469 | ||
4c07ad86 | 470 | static void td_fill_rand_seeds_os(struct thread_data *td) |
5bfc35d7 JA |
471 | { |
472 | os_random_seed(td->rand_seeds[0], &td->bsrange_state); | |
473 | os_random_seed(td->rand_seeds[1], &td->verify_state); | |
474 | os_random_seed(td->rand_seeds[2], &td->rwmix_state); | |
475 | ||
476 | if (td->o.file_service_type == FIO_FSERVICE_RANDOM) | |
477 | os_random_seed(td->rand_seeds[3], &td->next_file_state); | |
478 | ||
479 | os_random_seed(td->rand_seeds[5], &td->file_size_state); | |
0d29de83 | 480 | os_random_seed(td->rand_seeds[6], &td->trim_state); |
5bfc35d7 JA |
481 | |
482 | if (!td_random(td)) | |
483 | return; | |
484 | ||
485 | if (td->o.rand_repeatable) | |
486 | td->rand_seeds[4] = FIO_RANDSEED * td->thread_number; | |
487 | ||
488 | os_random_seed(td->rand_seeds[4], &td->random_state); | |
4c07ad86 JA |
489 | } |
490 | ||
491 | static void td_fill_rand_seeds_internal(struct thread_data *td) | |
492 | { | |
493 | init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]); | |
494 | init_rand_seed(&td->__verify_state, td->rand_seeds[1]); | |
495 | init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]); | |
496 | ||
497 | if (td->o.file_service_type == FIO_FSERVICE_RANDOM) | |
498 | init_rand_seed(&td->__next_file_state, td->rand_seeds[3]); | |
499 | ||
500 | init_rand_seed(&td->__file_size_state, td->rand_seeds[5]); | |
501 | init_rand_seed(&td->__trim_state, td->rand_seeds[6]); | |
502 | ||
503 | if (!td_random(td)) | |
504 | return; | |
505 | ||
506 | if (td->o.rand_repeatable) | |
507 | td->rand_seeds[4] = FIO_RANDSEED * td->thread_number; | |
508 | ||
2615cc4b | 509 | init_rand_seed(&td->__random_state, td->rand_seeds[4]); |
5bfc35d7 JA |
510 | } |
511 | ||
4c07ad86 JA |
512 | void td_fill_rand_seeds(struct thread_data *td) |
513 | { | |
514 | if (td->o.use_os_rand) | |
515 | td_fill_rand_seeds_os(td); | |
516 | else | |
517 | td_fill_rand_seeds_internal(td); | |
3545a109 JA |
518 | |
519 | init_rand_seed(&td->buf_state, td->rand_seeds[7]); | |
4c07ad86 JA |
520 | } |
521 | ||
9c60ce64 JA |
522 | /* |
523 | * Initialize the various random states we need (random io, block size ranges, | |
524 | * read/write mix, etc). | |
525 | */ | |
526 | static int init_random_state(struct thread_data *td) | |
527 | { | |
68727076 | 528 | int fd; |
9c60ce64 JA |
529 | |
530 | fd = open("/dev/urandom", O_RDONLY); | |
531 | if (fd == -1) { | |
532 | td_verror(td, errno, "open"); | |
533 | return 1; | |
534 | } | |
535 | ||
5bfc35d7 JA |
536 | if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) < |
537 | (int) sizeof(td->rand_seeds)) { | |
9c60ce64 JA |
538 | td_verror(td, EIO, "read"); |
539 | close(fd); | |
540 | return 1; | |
541 | } | |
542 | ||
543 | close(fd); | |
5bfc35d7 | 544 | td_fill_rand_seeds(td); |
9c60ce64 JA |
545 | return 0; |
546 | } | |
547 | ||
906c8d75 JA |
548 | /* |
549 | * Adds a job to the list of things todo. Sanitizes the various options | |
550 | * to make sure we don't have conflicts, and initializes various | |
551 | * members of td. | |
552 | */ | |
75154845 | 553 | static int add_job(struct thread_data *td, const char *jobname, int job_add_num) |
ebac4655 | 554 | { |
413dd459 JA |
555 | const char *ddir_str[] = { NULL, "read", "write", "rw", NULL, |
556 | "randread", "randwrite", "randrw" }; | |
af52b345 | 557 | unsigned int i; |
09629a90 | 558 | const char *engine; |
af52b345 | 559 | char fname[PATH_MAX]; |
e132cbae | 560 | int numjobs, file_alloced; |
ebac4655 | 561 | |
ebac4655 JA |
562 | /* |
563 | * the def_thread is just for options, it's not a real job | |
564 | */ | |
565 | if (td == &def_thread) | |
566 | return 0; | |
567 | ||
cca73aa7 JA |
568 | /* |
569 | * if we are just dumping the output command line, don't add the job | |
570 | */ | |
571 | if (dump_cmdline) { | |
572 | put_job(td); | |
573 | return 0; | |
574 | } | |
575 | ||
58c55ba0 | 576 | if (profile_td_init(td)) |
66251554 | 577 | goto err; |
58c55ba0 | 578 | |
2dc1bbeb | 579 | engine = get_engine_name(td->o.ioengine); |
09629a90 JA |
580 | td->io_ops = load_ioengine(td, engine); |
581 | if (!td->io_ops) { | |
582 | log_err("fio: failed to load engine %s\n", engine); | |
205927a3 | 583 | goto err; |
09629a90 | 584 | } |
df64119d | 585 | |
2dc1bbeb | 586 | if (td->o.use_thread) |
9cedf167 JA |
587 | nr_thread++; |
588 | else | |
589 | nr_process++; | |
590 | ||
2dc1bbeb | 591 | if (td->o.odirect) |
690adba3 JA |
592 | td->io_ops->flags |= FIO_RAWIO; |
593 | ||
e132cbae | 594 | file_alloced = 0; |
691c8fb0 | 595 | if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) { |
e132cbae | 596 | file_alloced = 1; |
80be24f4 | 597 | |
2dc1bbeb | 598 | if (td->o.nr_files == 1 && exists_and_not_file(jobname)) |
e132cbae | 599 | add_file(td, jobname); |
7b05a215 | 600 | else { |
2dc1bbeb | 601 | for (i = 0; i < td->o.nr_files; i++) { |
5ec10eaa JA |
602 | sprintf(fname, "%s.%d.%d", jobname, |
603 | td->thread_number, i); | |
7b05a215 JA |
604 | add_file(td, fname); |
605 | } | |
af52b345 | 606 | } |
0af7b542 | 607 | } |
ebac4655 | 608 | |
4e991c23 JA |
609 | if (fixup_options(td)) |
610 | goto err; | |
e0a22335 | 611 | |
07eb79df JA |
612 | if (td->io_ops->flags & FIO_DISKLESSIO) { |
613 | struct fio_file *f; | |
614 | ||
615 | for_each_file(td, f, i) | |
616 | f->real_file_size = -1ULL; | |
617 | } | |
618 | ||
cdd18ad8 | 619 | td->mutex = fio_mutex_init(0); |
ebac4655 | 620 | |
83349190 YH |
621 | td->ts.clat_percentiles = td->o.clat_percentiles; |
622 | if (td->o.overwrite_plist) | |
623 | td->ts.percentile_list = td->o.percentile_list; | |
624 | else | |
625 | td->ts.percentile_list = NULL; | |
626 | ||
756867bd JA |
627 | td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX; |
628 | td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX; | |
02af0988 | 629 | td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX; |
756867bd | 630 | td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX; |
cdd5411e | 631 | td->ddir_seq_nr = td->o.ddir_seq_nr; |
ebac4655 | 632 | |
755c3189 | 633 | if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) { |
3c5df6fa | 634 | prev_group_jobs = 0; |
ebac4655 | 635 | groupid++; |
3c5df6fa | 636 | } |
ebac4655 JA |
637 | |
638 | td->groupid = groupid; | |
3c5df6fa | 639 | prev_group_jobs++; |
ebac4655 | 640 | |
9c60ce64 JA |
641 | if (init_random_state(td)) |
642 | goto err; | |
643 | ||
ebac4655 JA |
644 | if (setup_rate(td)) |
645 | goto err; | |
646 | ||
2dc1bbeb | 647 | if (td->o.write_lat_log) { |
02af0988 | 648 | setup_log(&td->ts.lat_log); |
756867bd JA |
649 | setup_log(&td->ts.slat_log); |
650 | setup_log(&td->ts.clat_log); | |
ebac4655 | 651 | } |
2dc1bbeb | 652 | if (td->o.write_bw_log) |
756867bd | 653 | setup_log(&td->ts.bw_log); |
ebac4655 | 654 | |
2dc1bbeb JA |
655 | if (!td->o.name) |
656 | td->o.name = strdup(jobname); | |
01452055 | 657 | |
c6ae0a5b | 658 | if (!terse_output) { |
b990b5c0 | 659 | if (!job_add_num) { |
5ec10eaa JA |
660 | if (!strcmp(td->io_ops->name, "cpuio")) { |
661 | log_info("%s: ioengine=cpu, cpuload=%u," | |
662 | " cpucycle=%u\n", td->o.name, | |
663 | td->o.cpuload, | |
664 | td->o.cpucycle); | |
665 | } else { | |
f8977ee6 JA |
666 | char *c1, *c2, *c3, *c4; |
667 | ||
2dc1bbeb JA |
668 | c1 = to_kmg(td->o.min_bs[DDIR_READ]); |
669 | c2 = to_kmg(td->o.max_bs[DDIR_READ]); | |
670 | c3 = to_kmg(td->o.min_bs[DDIR_WRITE]); | |
671 | c4 = to_kmg(td->o.max_bs[DDIR_WRITE]); | |
f8977ee6 | 672 | |
5ec10eaa JA |
673 | log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s," |
674 | " ioengine=%s, iodepth=%u\n", | |
675 | td->o.name, td->groupid, | |
676 | ddir_str[td->o.td_ddir], | |
677 | c1, c2, c3, c4, | |
678 | td->io_ops->name, | |
679 | td->o.iodepth); | |
f8977ee6 JA |
680 | |
681 | free(c1); | |
682 | free(c2); | |
683 | free(c3); | |
684 | free(c4); | |
685 | } | |
b990b5c0 | 686 | } else if (job_add_num == 1) |
6d86144d | 687 | log_info("...\n"); |
c6ae0a5b | 688 | } |
ebac4655 JA |
689 | |
690 | /* | |
691 | * recurse add identical jobs, clear numjobs and stonewall options | |
692 | * as they don't apply to sub-jobs | |
693 | */ | |
2dc1bbeb | 694 | numjobs = td->o.numjobs; |
ebac4655 JA |
695 | while (--numjobs) { |
696 | struct thread_data *td_new = get_new_job(0, td); | |
697 | ||
698 | if (!td_new) | |
699 | goto err; | |
700 | ||
2dc1bbeb JA |
701 | td_new->o.numjobs = 1; |
702 | td_new->o.stonewall = 0; | |
92c1d41f | 703 | td_new->o.new_group = 0; |
e132cbae JA |
704 | |
705 | if (file_alloced) { | |
2dc1bbeb | 706 | td_new->o.filename = NULL; |
e132cbae | 707 | td_new->files_index = 0; |
4e6ea2f1 | 708 | td_new->files_size = 0; |
e132cbae JA |
709 | td_new->files = NULL; |
710 | } | |
711 | ||
75154845 | 712 | job_add_num = numjobs - 1; |
ebac4655 | 713 | |
75154845 | 714 | if (add_job(td_new, jobname, job_add_num)) |
ebac4655 JA |
715 | goto err; |
716 | } | |
3c5df6fa | 717 | |
ebac4655 JA |
718 | return 0; |
719 | err: | |
720 | put_job(td); | |
721 | return -1; | |
722 | } | |
723 | ||
79d16311 JA |
724 | /* |
725 | * Parse as if 'o' was a command line | |
726 | */ | |
727 | void add_job_opts(const char **o) | |
728 | { | |
729 | struct thread_data *td, *td_parent; | |
730 | int i, in_global = 1; | |
731 | char jobname[32]; | |
732 | ||
733 | i = 0; | |
734 | td_parent = td = NULL; | |
735 | while (o[i]) { | |
736 | if (!strncmp(o[i], "name", 4)) { | |
737 | in_global = 0; | |
738 | if (td) | |
739 | add_job(td, jobname, 0); | |
740 | td = NULL; | |
741 | sprintf(jobname, "%s", o[i] + 5); | |
742 | } | |
743 | if (in_global && !td_parent) | |
744 | td_parent = get_new_job(1, &def_thread); | |
745 | else if (!in_global && !td) { | |
746 | if (!td_parent) | |
747 | td_parent = &def_thread; | |
748 | td = get_new_job(0, td_parent); | |
749 | } | |
750 | if (in_global) | |
751 | fio_options_parse(td_parent, (char **) &o[i], 1); | |
752 | else | |
753 | fio_options_parse(td, (char **) &o[i], 1); | |
754 | i++; | |
755 | } | |
756 | ||
757 | if (td) | |
758 | add_job(td, jobname, 0); | |
759 | } | |
760 | ||
01f06b63 JA |
761 | static int skip_this_section(const char *name) |
762 | { | |
ad0a2735 JA |
763 | int i; |
764 | ||
765 | if (!nr_job_sections) | |
01f06b63 JA |
766 | return 0; |
767 | if (!strncmp(name, "global", 6)) | |
768 | return 0; | |
769 | ||
ad0a2735 JA |
770 | for (i = 0; i < nr_job_sections; i++) |
771 | if (!strcmp(job_sections[i], name)) | |
772 | return 0; | |
773 | ||
774 | return 1; | |
01f06b63 JA |
775 | } |
776 | ||
ebac4655 JA |
777 | static int is_empty_or_comment(char *line) |
778 | { | |
779 | unsigned int i; | |
780 | ||
781 | for (i = 0; i < strlen(line); i++) { | |
782 | if (line[i] == ';') | |
783 | return 1; | |
5cc2da30 IM |
784 | if (line[i] == '#') |
785 | return 1; | |
76cd9378 | 786 | if (!isspace((int) line[i]) && !iscntrl((int) line[i])) |
ebac4655 JA |
787 | return 0; |
788 | } | |
789 | ||
790 | return 1; | |
791 | } | |
792 | ||
07261983 JA |
793 | /* |
794 | * This is our [ini] type file parser. | |
795 | */ | |
1e97cce9 | 796 | static int parse_jobs_ini(char *file, int stonewall_flag) |
ebac4655 | 797 | { |
e1f36503 | 798 | unsigned int global; |
ebac4655 | 799 | struct thread_data *td; |
fee3bb48 | 800 | char *string, *name; |
ebac4655 JA |
801 | FILE *f; |
802 | char *p; | |
0c7e37a0 | 803 | int ret = 0, stonewall; |
097b2991 | 804 | int first_sect = 1; |
ccf8f127 | 805 | int skip_fgets = 0; |
01f06b63 | 806 | int inside_skip = 0; |
3b8b7135 JA |
807 | char **opts; |
808 | int i, alloc_opts, num_opts; | |
ebac4655 | 809 | |
5a729cbe AC |
810 | if (!strcmp(file, "-")) |
811 | f = stdin; | |
812 | else | |
813 | f = fopen(file, "r"); | |
814 | ||
ebac4655 | 815 | if (!f) { |
aea47d44 | 816 | perror("fopen job file"); |
ebac4655 JA |
817 | return 1; |
818 | } | |
819 | ||
820 | string = malloc(4096); | |
7f7e6e59 JA |
821 | |
822 | /* | |
823 | * it's really 256 + small bit, 280 should suffice | |
824 | */ | |
825 | name = malloc(280); | |
826 | memset(name, 0, 280); | |
ebac4655 | 827 | |
3b8b7135 JA |
828 | alloc_opts = 8; |
829 | opts = malloc(sizeof(char *) * alloc_opts); | |
b7c6302d | 830 | num_opts = 0; |
3b8b7135 | 831 | |
0c7e37a0 | 832 | stonewall = stonewall_flag; |
7c124ac1 | 833 | do { |
ccf8f127 JA |
834 | /* |
835 | * if skip_fgets is set, we already have loaded a line we | |
836 | * haven't handled. | |
837 | */ | |
838 | if (!skip_fgets) { | |
839 | p = fgets(string, 4095, f); | |
840 | if (!p) | |
841 | break; | |
842 | } | |
cdc7f193 | 843 | |
ccf8f127 | 844 | skip_fgets = 0; |
cdc7f193 | 845 | strip_blank_front(&p); |
6c7c7da1 | 846 | strip_blank_end(p); |
cdc7f193 | 847 | |
ebac4655 JA |
848 | if (is_empty_or_comment(p)) |
849 | continue; | |
84dd1886 | 850 | if (sscanf(p, "[%255[^\n]]", name) != 1) { |
01f06b63 JA |
851 | if (inside_skip) |
852 | continue; | |
5ec10eaa JA |
853 | log_err("fio: option <%s> outside of [] job section\n", |
854 | p); | |
088b4207 | 855 | break; |
6c7c7da1 | 856 | } |
ebac4655 | 857 | |
7a4b80a1 JA |
858 | name[strlen(name) - 1] = '\0'; |
859 | ||
01f06b63 JA |
860 | if (skip_this_section(name)) { |
861 | inside_skip = 1; | |
862 | continue; | |
863 | } else | |
864 | inside_skip = 0; | |
865 | ||
ebac4655 JA |
866 | global = !strncmp(name, "global", 6); |
867 | ||
cca73aa7 | 868 | if (dump_cmdline) { |
097b2991 JA |
869 | if (first_sect) |
870 | log_info("fio "); | |
cca73aa7 JA |
871 | if (!global) |
872 | log_info("--name=%s ", name); | |
097b2991 | 873 | first_sect = 0; |
cca73aa7 JA |
874 | } |
875 | ||
ebac4655 | 876 | td = get_new_job(global, &def_thread); |
45410acb JA |
877 | if (!td) { |
878 | ret = 1; | |
879 | break; | |
880 | } | |
ebac4655 | 881 | |
972cfd25 JA |
882 | /* |
883 | * Seperate multiple job files by a stonewall | |
884 | */ | |
f9481919 | 885 | if (!global && stonewall) { |
2dc1bbeb | 886 | td->o.stonewall = stonewall; |
972cfd25 JA |
887 | stonewall = 0; |
888 | } | |
889 | ||
3b8b7135 JA |
890 | num_opts = 0; |
891 | memset(opts, 0, alloc_opts * sizeof(char *)); | |
892 | ||
ebac4655 JA |
893 | while ((p = fgets(string, 4096, f)) != NULL) { |
894 | if (is_empty_or_comment(p)) | |
895 | continue; | |
e1f36503 | 896 | |
b6754f9d | 897 | strip_blank_front(&p); |
7c124ac1 | 898 | |
ccf8f127 JA |
899 | /* |
900 | * new section, break out and make sure we don't | |
901 | * fgets() a new line at the top. | |
902 | */ | |
903 | if (p[0] == '[') { | |
904 | skip_fgets = 1; | |
7c124ac1 | 905 | break; |
ccf8f127 | 906 | } |
7c124ac1 | 907 | |
4ae3f763 | 908 | strip_blank_end(p); |
aea47d44 | 909 | |
3b8b7135 JA |
910 | if (num_opts == alloc_opts) { |
911 | alloc_opts <<= 1; | |
912 | opts = realloc(opts, | |
913 | alloc_opts * sizeof(char *)); | |
914 | } | |
915 | ||
916 | opts[num_opts] = strdup(p); | |
917 | num_opts++; | |
ebac4655 | 918 | } |
ebac4655 | 919 | |
3b8b7135 JA |
920 | ret = fio_options_parse(td, opts, num_opts); |
921 | if (!ret) { | |
922 | if (dump_cmdline) | |
923 | for (i = 0; i < num_opts; i++) | |
924 | log_info("--%s ", opts[i]); | |
925 | ||
45410acb | 926 | ret = add_job(td, name, 0); |
3b8b7135 | 927 | } else { |
b1508cf9 JA |
928 | log_err("fio: job %s dropped\n", name); |
929 | put_job(td); | |
45410acb | 930 | } |
3b8b7135 JA |
931 | |
932 | for (i = 0; i < num_opts; i++) | |
933 | free(opts[i]); | |
934 | num_opts = 0; | |
7c124ac1 | 935 | } while (!ret); |
ebac4655 | 936 | |
cca73aa7 JA |
937 | if (dump_cmdline) |
938 | log_info("\n"); | |
939 | ||
3b8b7135 JA |
940 | for (i = 0; i < num_opts; i++) |
941 | free(opts[i]); | |
942 | ||
ebac4655 JA |
943 | free(string); |
944 | free(name); | |
2577594d | 945 | free(opts); |
5a729cbe AC |
946 | if (f != stdin) |
947 | fclose(f); | |
45410acb | 948 | return ret; |
ebac4655 JA |
949 | } |
950 | ||
951 | static int fill_def_thread(void) | |
952 | { | |
953 | memset(&def_thread, 0, sizeof(def_thread)); | |
954 | ||
375b2695 | 955 | fio_getaffinity(getpid(), &def_thread.o.cpumask); |
ebac4655 JA |
956 | |
957 | /* | |
ee738499 | 958 | * fill default options |
ebac4655 | 959 | */ |
214e1eca | 960 | fio_fill_default_options(&def_thread); |
ebac4655 JA |
961 | return 0; |
962 | } | |
963 | ||
214e1eca JA |
964 | static void free_shm(void) |
965 | { | |
966 | struct shmid_ds sbuf; | |
967 | ||
968 | if (threads) { | |
5e1d306e JA |
969 | void *tp = threads; |
970 | ||
214e1eca | 971 | threads = NULL; |
5e1d306e JA |
972 | file_hash_exit(); |
973 | fio_debug_jobp = NULL; | |
974 | shmdt(tp); | |
214e1eca JA |
975 | shmctl(shm_id, IPC_RMID, &sbuf); |
976 | } | |
2e5cdb11 JA |
977 | |
978 | scleanup(); | |
214e1eca JA |
979 | } |
980 | ||
981 | /* | |
982 | * The thread area is shared between the main process and the job | |
983 | * threads/processes. So setup a shared memory segment that will hold | |
380065aa JA |
984 | * all the job info. We use the end of the region for keeping track of |
985 | * open files across jobs, for file sharing. | |
214e1eca JA |
986 | */ |
987 | static int setup_thread_area(void) | |
988 | { | |
380065aa JA |
989 | void *hash; |
990 | ||
214e1eca JA |
991 | /* |
992 | * 1024 is too much on some machines, scale max_jobs if | |
993 | * we get a failure that looks like too large a shm segment | |
994 | */ | |
995 | do { | |
996 | size_t size = max_jobs * sizeof(struct thread_data); | |
997 | ||
380065aa | 998 | size += file_hash_size; |
5e1d306e | 999 | size += sizeof(unsigned int); |
380065aa | 1000 | |
214e1eca JA |
1001 | shm_id = shmget(0, size, IPC_CREAT | 0600); |
1002 | if (shm_id != -1) | |
1003 | break; | |
1004 | if (errno != EINVAL) { | |
1005 | perror("shmget"); | |
1006 | break; | |
1007 | } | |
1008 | ||
1009 | max_jobs >>= 1; | |
1010 | } while (max_jobs); | |
1011 | ||
1012 | if (shm_id == -1) | |
1013 | return 1; | |
1014 | ||
1015 | threads = shmat(shm_id, NULL, 0); | |
1016 | if (threads == (void *) -1) { | |
1017 | perror("shmat"); | |
1018 | return 1; | |
1019 | } | |
1020 | ||
1f809d15 | 1021 | memset(threads, 0, max_jobs * sizeof(struct thread_data)); |
380065aa | 1022 | hash = (void *) threads + max_jobs * sizeof(struct thread_data); |
5e1d306e JA |
1023 | fio_debug_jobp = (void *) hash + file_hash_size; |
1024 | *fio_debug_jobp = -1; | |
380065aa | 1025 | file_hash_init(hash); |
214e1eca JA |
1026 | atexit(free_shm); |
1027 | return 0; | |
1028 | } | |
1029 | ||
45378b35 | 1030 | static void usage(const char *name) |
4785f995 | 1031 | { |
43617628 | 1032 | printf("%s\n", fio_version_string); |
45378b35 | 1033 | printf("%s [options] [job options] <job file(s)>\n", name); |
ee56ad50 | 1034 | printf("\t--debug=options\tEnable debug logging\n"); |
b4692828 JA |
1035 | printf("\t--output\tWrite output to file\n"); |
1036 | printf("\t--timeout\tRuntime in seconds\n"); | |
1037 | printf("\t--latency-log\tGenerate per-job latency logs\n"); | |
1038 | printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n"); | |
1039 | printf("\t--minimal\tMinimal (terse) output\n"); | |
1040 | printf("\t--version\tPrint version info and exit\n"); | |
fd28ca49 | 1041 | printf("\t--help\t\tPrint this page\n"); |
5ec10eaa JA |
1042 | printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of" |
1043 | " them\n"); | |
cca73aa7 | 1044 | printf("\t--showcmd\tTurn a job file into command line options\n"); |
e592a06b AC |
1045 | printf("\t--eta=when\tWhen ETA estimate should be printed\n"); |
1046 | printf("\t \tMay be \"always\", \"never\" or \"auto\"\n"); | |
5ec10eaa JA |
1047 | printf("\t--readonly\tTurn on safety read-only checks, preventing" |
1048 | " writes\n"); | |
01f06b63 | 1049 | printf("\t--section=name\tOnly run specified section in job file\n"); |
2b386d25 JA |
1050 | printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb" |
1051 | " (def 1024)\n"); | |
a9523c6f | 1052 | printf("\t--warnings-fatal Fio parser warnings are fatal\n"); |
fca70358 | 1053 | printf("\t--max-jobs\tMaximum number of threads/processes to support\n"); |
aa58d252 JA |
1054 | printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>"); |
1055 | printf("\n Jens Axboe <jaxboe@fusionio.com>\n"); | |
4785f995 JA |
1056 | } |
1057 | ||
79e48f72 | 1058 | #ifdef FIO_INC_DEBUG |
ee56ad50 | 1059 | struct debug_level debug_levels[] = { |
bd6f78b2 JA |
1060 | { .name = "process", .shift = FD_PROCESS, }, |
1061 | { .name = "file", .shift = FD_FILE, }, | |
1062 | { .name = "io", .shift = FD_IO, }, | |
1063 | { .name = "mem", .shift = FD_MEM, }, | |
1064 | { .name = "blktrace", .shift = FD_BLKTRACE }, | |
1065 | { .name = "verify", .shift = FD_VERIFY }, | |
84422acd | 1066 | { .name = "random", .shift = FD_RANDOM }, |
a3d741fa | 1067 | { .name = "parse", .shift = FD_PARSE }, |
cd991b9e | 1068 | { .name = "diskutil", .shift = FD_DISKUTIL }, |
5e1d306e | 1069 | { .name = "job", .shift = FD_JOB }, |
29adda3c | 1070 | { .name = "mutex", .shift = FD_MUTEX }, |
79d16311 | 1071 | { .name = "profile", .shift = FD_PROFILE }, |
c223da83 | 1072 | { .name = "time", .shift = FD_TIME }, |
02444ad1 | 1073 | { .name = NULL, }, |
ee56ad50 JA |
1074 | }; |
1075 | ||
c09823ab | 1076 | static int set_debug(const char *string) |
ee56ad50 JA |
1077 | { |
1078 | struct debug_level *dl; | |
1079 | char *p = (char *) string; | |
1080 | char *opt; | |
1081 | int i; | |
1082 | ||
1083 | if (!strcmp(string, "?") || !strcmp(string, "help")) { | |
ee56ad50 JA |
1084 | log_info("fio: dumping debug options:"); |
1085 | for (i = 0; debug_levels[i].name; i++) { | |
1086 | dl = &debug_levels[i]; | |
1087 | log_info("%s,", dl->name); | |
1088 | } | |
bd6f78b2 | 1089 | log_info("all\n"); |
c09823ab | 1090 | return 1; |
ee56ad50 JA |
1091 | } |
1092 | ||
1093 | while ((opt = strsep(&p, ",")) != NULL) { | |
1094 | int found = 0; | |
1095 | ||
5e1d306e JA |
1096 | if (!strncmp(opt, "all", 3)) { |
1097 | log_info("fio: set all debug options\n"); | |
1098 | fio_debug = ~0UL; | |
1099 | continue; | |
1100 | } | |
1101 | ||
ee56ad50 JA |
1102 | for (i = 0; debug_levels[i].name; i++) { |
1103 | dl = &debug_levels[i]; | |
5e1d306e JA |
1104 | found = !strncmp(opt, dl->name, strlen(dl->name)); |
1105 | if (!found) | |
1106 | continue; | |
1107 | ||
1108 | if (dl->shift == FD_JOB) { | |
1109 | opt = strchr(opt, ':'); | |
1110 | if (!opt) { | |
1111 | log_err("fio: missing job number\n"); | |
1112 | break; | |
1113 | } | |
1114 | opt++; | |
1115 | fio_debug_jobno = atoi(opt); | |
1116 | log_info("fio: set debug jobno %d\n", | |
1117 | fio_debug_jobno); | |
1118 | } else { | |
ee56ad50 | 1119 | log_info("fio: set debug option %s\n", opt); |
bd6f78b2 | 1120 | fio_debug |= (1UL << dl->shift); |
ee56ad50 | 1121 | } |
5e1d306e | 1122 | break; |
ee56ad50 JA |
1123 | } |
1124 | ||
1125 | if (!found) | |
1126 | log_err("fio: debug mask %s not found\n", opt); | |
1127 | } | |
c09823ab | 1128 | return 0; |
ee56ad50 | 1129 | } |
79e48f72 | 1130 | #else |
69b98d4c | 1131 | static int set_debug(const char *string) |
79e48f72 JA |
1132 | { |
1133 | log_err("fio: debug tracing not included in build\n"); | |
c09823ab | 1134 | return 1; |
79e48f72 JA |
1135 | } |
1136 | #endif | |
9ac8a797 | 1137 | |
4c6107ff JA |
1138 | static void fio_options_fill_optstring(void) |
1139 | { | |
1140 | char *ostr = cmd_optstr; | |
1141 | int i, c; | |
1142 | ||
1143 | c = i = 0; | |
1144 | while (l_opts[i].name) { | |
1145 | ostr[c++] = l_opts[i].val; | |
1146 | if (l_opts[i].has_arg == required_argument) | |
1147 | ostr[c++] = ':'; | |
1148 | else if (l_opts[i].has_arg == optional_argument) { | |
1149 | ostr[c++] = ':'; | |
1150 | ostr[c++] = ':'; | |
1151 | } | |
1152 | i++; | |
1153 | } | |
1154 | ostr[c] = '\0'; | |
1155 | } | |
1156 | ||
972cfd25 | 1157 | static int parse_cmd_line(int argc, char *argv[]) |
ebac4655 | 1158 | { |
b4692828 | 1159 | struct thread_data *td = NULL; |
c09823ab | 1160 | int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0; |
4c6107ff | 1161 | char *ostr = cmd_optstr; |
ebac4655 | 1162 | |
4c6107ff | 1163 | while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) { |
ebac4655 | 1164 | switch (c) { |
2b386d25 JA |
1165 | case 'a': |
1166 | smalloc_pool_size = atoi(optarg); | |
1167 | break; | |
b4692828 | 1168 | case 't': |
ab2cabdb | 1169 | def_thread.o.timeout = atoi(optarg); |
b4692828 JA |
1170 | break; |
1171 | case 'l': | |
1172 | write_lat_log = 1; | |
1173 | break; | |
3d73e5a9 | 1174 | case 'b': |
b4692828 JA |
1175 | write_bw_log = 1; |
1176 | break; | |
1177 | case 'o': | |
1178 | f_out = fopen(optarg, "w+"); | |
1179 | if (!f_out) { | |
1180 | perror("fopen output"); | |
1181 | exit(1); | |
1182 | } | |
1183 | f_err = f_out; | |
1184 | break; | |
1185 | case 'm': | |
1186 | terse_output = 1; | |
1187 | break; | |
1188 | case 'h': | |
45378b35 | 1189 | usage(argv[0]); |
b4692828 | 1190 | exit(0); |
fd28ca49 | 1191 | case 'c': |
214e1eca | 1192 | exit(fio_show_option_help(optarg)); |
cca73aa7 JA |
1193 | case 's': |
1194 | dump_cmdline = 1; | |
1195 | break; | |
724e4435 JA |
1196 | case 'r': |
1197 | read_only = 1; | |
1198 | break; | |
b4692828 | 1199 | case 'v': |
ca740f4e | 1200 | log_info("%s\n", fio_version_string); |
b4692828 | 1201 | exit(0); |
e592a06b AC |
1202 | case 'e': |
1203 | if (!strcmp("always", optarg)) | |
1204 | eta_print = FIO_ETA_ALWAYS; | |
1205 | else if (!strcmp("never", optarg)) | |
1206 | eta_print = FIO_ETA_NEVER; | |
1207 | break; | |
ee56ad50 | 1208 | case 'd': |
c09823ab JA |
1209 | if (set_debug(optarg)) |
1210 | do_exit++; | |
ee56ad50 | 1211 | break; |
ad0a2735 JA |
1212 | case 'x': { |
1213 | size_t new_size; | |
1214 | ||
01f06b63 | 1215 | if (!strcmp(optarg, "global")) { |
5ec10eaa JA |
1216 | log_err("fio: can't use global as only " |
1217 | "section\n"); | |
c09823ab JA |
1218 | do_exit++; |
1219 | exit_val = 1; | |
01f06b63 JA |
1220 | break; |
1221 | } | |
ad0a2735 JA |
1222 | new_size = (nr_job_sections + 1) * sizeof(char *); |
1223 | job_sections = realloc(job_sections, new_size); | |
1224 | job_sections[nr_job_sections] = strdup(optarg); | |
1225 | nr_job_sections++; | |
01f06b63 | 1226 | break; |
ad0a2735 | 1227 | } |
9ac8a797 | 1228 | case 'p': |
07b3232d | 1229 | exec_profile = strdup(optarg); |
9ac8a797 | 1230 | break; |
b4692828 | 1231 | case FIO_GETOPT_JOB: { |
5ec10eaa | 1232 | const char *opt = l_opts[lidx].name; |
b4692828 JA |
1233 | char *val = optarg; |
1234 | ||
c2b1e753 | 1235 | if (!strncmp(opt, "name", 4) && td) { |
2dc1bbeb | 1236 | ret = add_job(td, td->o.name ?: "fio", 0); |
66251554 | 1237 | if (ret) |
c2b1e753 | 1238 | return 0; |
c2b1e753 JA |
1239 | td = NULL; |
1240 | } | |
b4692828 | 1241 | if (!td) { |
01f06b63 | 1242 | int is_section = !strncmp(opt, "name", 4); |
3106f220 JA |
1243 | int global = 0; |
1244 | ||
01f06b63 | 1245 | if (!is_section || !strncmp(val, "global", 6)) |
3106f220 | 1246 | global = 1; |
c2b1e753 | 1247 | |
01f06b63 JA |
1248 | if (is_section && skip_this_section(val)) |
1249 | continue; | |
1250 | ||
c2b1e753 | 1251 | td = get_new_job(global, &def_thread); |
b4692828 JA |
1252 | if (!td) |
1253 | return 0; | |
1254 | } | |
38d0adb0 | 1255 | |
214e1eca | 1256 | ret = fio_cmd_option_parse(td, opt, val); |
b4692828 JA |
1257 | break; |
1258 | } | |
a9523c6f JA |
1259 | case 'w': |
1260 | warnings_fatal = 1; | |
1261 | break; | |
fca70358 JA |
1262 | case 'j': |
1263 | max_jobs = atoi(optarg); | |
1264 | if (!max_jobs || max_jobs > REAL_MAX_JOBS) { | |
1265 | log_err("fio: invalid max jobs: %d\n", max_jobs); | |
1266 | do_exit++; | |
1267 | exit_val = 1; | |
1268 | } | |
1269 | break; | |
b4692828 | 1270 | default: |
c09823ab JA |
1271 | do_exit++; |
1272 | exit_val = 1; | |
b4692828 | 1273 | break; |
ebac4655 JA |
1274 | } |
1275 | } | |
c9fad893 | 1276 | |
c09823ab JA |
1277 | if (do_exit) |
1278 | exit(exit_val); | |
536582bf | 1279 | |
b4692828 | 1280 | if (td) { |
7d6a8904 | 1281 | if (!ret) |
2dc1bbeb | 1282 | ret = add_job(td, td->o.name ?: "fio", 0); |
972cfd25 | 1283 | } |
774a6177 | 1284 | |
b4692828 JA |
1285 | while (optind < argc) { |
1286 | ini_idx++; | |
1287 | ini_file = realloc(ini_file, ini_idx * sizeof(char *)); | |
1288 | ini_file[ini_idx - 1] = strdup(argv[optind]); | |
1289 | optind++; | |
eb8bbf48 | 1290 | } |
972cfd25 JA |
1291 | |
1292 | return ini_idx; | |
ebac4655 JA |
1293 | } |
1294 | ||
ebac4655 JA |
1295 | int parse_options(int argc, char *argv[]) |
1296 | { | |
972cfd25 JA |
1297 | int job_files, i; |
1298 | ||
b4692828 JA |
1299 | f_out = stdout; |
1300 | f_err = stderr; | |
1301 | ||
4c6107ff | 1302 | fio_options_fill_optstring(); |
5ec10eaa | 1303 | fio_options_dup_and_init(l_opts); |
b4692828 | 1304 | |
ebac4655 JA |
1305 | if (setup_thread_area()) |
1306 | return 1; | |
1307 | if (fill_def_thread()) | |
1308 | return 1; | |
1309 | ||
972cfd25 | 1310 | job_files = parse_cmd_line(argc, argv); |
ebac4655 | 1311 | |
972cfd25 JA |
1312 | for (i = 0; i < job_files; i++) { |
1313 | if (fill_def_thread()) | |
1314 | return 1; | |
0c7e37a0 | 1315 | if (parse_jobs_ini(ini_file[i], i)) |
972cfd25 | 1316 | return 1; |
88c6ed80 | 1317 | free(ini_file[i]); |
972cfd25 | 1318 | } |
ebac4655 | 1319 | |
88c6ed80 | 1320 | free(ini_file); |
d23bb327 | 1321 | options_mem_free(&def_thread); |
b4692828 JA |
1322 | |
1323 | if (!thread_number) { | |
cca73aa7 JA |
1324 | if (dump_cmdline) |
1325 | return 0; | |
07b3232d JA |
1326 | if (exec_profile) |
1327 | return 0; | |
cca73aa7 | 1328 | |
03e20d68 | 1329 | log_err("No jobs(s) defined\n\n"); |
45378b35 | 1330 | usage(argv[0]); |
b4692828 JA |
1331 | return 1; |
1332 | } | |
1333 | ||
be4ecfdf JA |
1334 | if (def_thread.o.gtod_offload) { |
1335 | fio_gtod_init(); | |
1336 | fio_gtod_offload = 1; | |
1337 | fio_gtod_cpu = def_thread.o.gtod_cpu; | |
1338 | } | |
1339 | ||
ca740f4e | 1340 | log_info("%s\n", fio_version_string); |
ebac4655 JA |
1341 | return 0; |
1342 | } |