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