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