parse: dump option type when using --debug=parse
[fio.git] / init.c
CommitLineData
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>
ebac4655
JA
12#include <sys/types.h>
13#include <sys/stat.h>
14
15#include "fio.h"
a5e0ee11
O
16#ifndef FIO_NO_HAVE_SHM_H
17#include <sys/shm.h>
18#endif
19
cb2c86fd 20#include "parse.h"
2e5cdb11 21#include "smalloc.h"
380065aa 22#include "filehash.h"
4f5af7b2 23#include "verify.h"
79d16311 24#include "profile.h"
50d16976 25#include "server.h"
f2a2ce0e 26#include "idletime.h"
243bfe19 27#include "filelock.h"
16e56d25 28#include "steadystate.h"
ebac4655 29
f87cc561 30#include "oslib/getopt.h"
984f30c9 31#include "oslib/strcasestr.h"
bf2e821a 32
10aa136b 33#include "crc/test.h"
d694a6a7 34#include "lib/pow2.h"
b76d85a4 35#include "lib/memcpy.h"
10aa136b 36
3d43382c 37const char fio_version_string[] = FIO_VERSION;
214e1eca 38
ee738499 39#define FIO_RANDSEED (0xb1899bedUL)
ebac4655 40
214e1eca 41static char **ini_file;
fca70358 42static int max_jobs = FIO_MAX_JOBS;
cca73aa7 43static int dump_cmdline;
111e032d 44static int parse_only;
e1f36503 45
be4ecfdf 46static struct thread_data def_thread;
214e1eca 47struct thread_data *threads = NULL;
10aa136b
JA
48static char **job_sections;
49static int nr_job_sections;
e1f36503 50
214e1eca 51int exitall_on_terminate = 0;
f3afa57e 52int output_format = FIO_OUTPUT_NORMAL;
5be4c944 53int eta_print = FIO_ETA_AUTO;
db37d890 54unsigned int eta_interval_msec = 1000;
e382e661 55int eta_new_line = 0;
214e1eca
JA
56FILE *f_out = NULL;
57FILE *f_err = NULL;
07b3232d 58char *exec_profile = NULL;
a9523c6f 59int warnings_fatal = 0;
4d658652 60int terse_version = 3;
50d16976 61int is_backend = 0;
a37f69b7 62int nr_clients = 0;
e46d8091 63int log_syslog = 0;
ee738499 64
214e1eca 65int write_bw_log = 0;
4241ea8f 66int read_only = 0;
06464907 67int status_interval = 0;
214e1eca 68
ca09be4b 69char *trigger_file = NULL;
ca09be4b 70long long trigger_timeout = 0;
b63efd30
JA
71char *trigger_cmd = NULL;
72char *trigger_remote_cmd = NULL;
ca09be4b 73
d264264a
JA
74char *aux_path = NULL;
75
214e1eca 76static int prev_group_jobs;
b4692828 77
ee56ad50 78unsigned long fio_debug = 0;
5e1d306e
JA
79unsigned int fio_debug_jobno = -1;
80unsigned int *fio_debug_jobp = NULL;
ee56ad50 81
4c6107ff 82static char cmd_optstr[256];
1201b24a 83static bool did_arg;
4c6107ff 84
7a4b8240
JA
85#define FIO_CLIENT_FLAG (1 << 16)
86
b4692828
JA
87/*
88 * Command line options. These will contain the above, plus a few
89 * extra that only pertain to fio itself and not jobs.
90 */
5ec10eaa 91static struct option l_opts[FIO_NR_OPTIONS] = {
b4692828 92 {
08d2a19c 93 .name = (char *) "output",
b4692828 94 .has_arg = required_argument,
7a4b8240 95 .val = 'o' | FIO_CLIENT_FLAG,
b4692828 96 },
b4692828 97 {
08d2a19c 98 .name = (char *) "latency-log",
b4692828 99 .has_arg = required_argument,
7a4b8240 100 .val = 'l' | FIO_CLIENT_FLAG,
b4692828
JA
101 },
102 {
08d2a19c 103 .name = (char *) "bandwidth-log",
2fd6fad5 104 .has_arg = no_argument,
7a4b8240 105 .val = 'b' | FIO_CLIENT_FLAG,
b4692828
JA
106 },
107 {
08d2a19c 108 .name = (char *) "minimal",
c4ec0a1d 109 .has_arg = no_argument,
7a4b8240 110 .val = 'm' | FIO_CLIENT_FLAG,
b4692828 111 },
cc372b17 112 {
f3afa57e 113 .name = (char *) "output-format",
d2c87a78 114 .has_arg = required_argument,
f3afa57e 115 .val = 'F' | FIO_CLIENT_FLAG,
cc372b17 116 },
f6a7df53
JA
117 {
118 .name = (char *) "append-terse",
119 .has_arg = optional_argument,
120 .val = 'f',
121 },
b4692828 122 {
08d2a19c 123 .name = (char *) "version",
b4692828 124 .has_arg = no_argument,
7a4b8240 125 .val = 'v' | FIO_CLIENT_FLAG,
b4692828 126 },
fd28ca49 127 {
08d2a19c 128 .name = (char *) "help",
fd28ca49 129 .has_arg = no_argument,
7a4b8240 130 .val = 'h' | FIO_CLIENT_FLAG,
fd28ca49
JA
131 },
132 {
08d2a19c 133 .name = (char *) "cmdhelp",
320beefe 134 .has_arg = optional_argument,
7a4b8240 135 .val = 'c' | FIO_CLIENT_FLAG,
fd28ca49 136 },
de890a1e 137 {
06464907 138 .name = (char *) "enghelp",
de890a1e 139 .has_arg = optional_argument,
06464907 140 .val = 'i' | FIO_CLIENT_FLAG,
de890a1e 141 },
cca73aa7 142 {
08d2a19c 143 .name = (char *) "showcmd",
cca73aa7 144 .has_arg = no_argument,
7a4b8240 145 .val = 's' | FIO_CLIENT_FLAG,
724e4435
JA
146 },
147 {
08d2a19c 148 .name = (char *) "readonly",
724e4435 149 .has_arg = no_argument,
7a4b8240 150 .val = 'r' | FIO_CLIENT_FLAG,
cca73aa7 151 },
e592a06b 152 {
08d2a19c 153 .name = (char *) "eta",
e592a06b 154 .has_arg = required_argument,
7a4b8240 155 .val = 'e' | FIO_CLIENT_FLAG,
e592a06b 156 },
db37d890
JA
157 {
158 .name = (char *) "eta-interval",
159 .has_arg = required_argument,
160 .val = 'O' | FIO_CLIENT_FLAG,
161 },
e382e661
JA
162 {
163 .name = (char *) "eta-newline",
164 .has_arg = required_argument,
165 .val = 'E' | FIO_CLIENT_FLAG,
166 },
ee56ad50 167 {
08d2a19c 168 .name = (char *) "debug",
ee56ad50 169 .has_arg = required_argument,
7a4b8240 170 .val = 'd' | FIO_CLIENT_FLAG,
ee56ad50 171 },
111e032d
JA
172 {
173 .name = (char *) "parse-only",
174 .has_arg = no_argument,
175 .val = 'P' | FIO_CLIENT_FLAG,
176 },
01f06b63 177 {
08d2a19c 178 .name = (char *) "section",
01f06b63 179 .has_arg = required_argument,
7a4b8240 180 .val = 'x' | FIO_CLIENT_FLAG,
01f06b63 181 },
b26317c9
JA
182#ifdef CONFIG_ZLIB
183 {
184 .name = (char *) "inflate-log",
185 .has_arg = required_argument,
186 .val = 'X' | FIO_CLIENT_FLAG,
187 },
188#endif
2b386d25 189 {
08d2a19c 190 .name = (char *) "alloc-size",
2b386d25 191 .has_arg = required_argument,
7a4b8240 192 .val = 'a' | FIO_CLIENT_FLAG,
2b386d25 193 },
9ac8a797 194 {
08d2a19c 195 .name = (char *) "profile",
9ac8a797 196 .has_arg = required_argument,
7a4b8240 197 .val = 'p' | FIO_CLIENT_FLAG,
9ac8a797 198 },
a9523c6f 199 {
08d2a19c 200 .name = (char *) "warnings-fatal",
a9523c6f 201 .has_arg = no_argument,
7a4b8240 202 .val = 'w' | FIO_CLIENT_FLAG,
a9523c6f 203 },
fca70358
JA
204 {
205 .name = (char *) "max-jobs",
206 .has_arg = required_argument,
7a4b8240 207 .val = 'j' | FIO_CLIENT_FLAG,
fca70358 208 },
f57a9c59
JA
209 {
210 .name = (char *) "terse-version",
211 .has_arg = required_argument,
7a4b8240 212 .val = 'V' | FIO_CLIENT_FLAG,
f57a9c59 213 },
50d16976
JA
214 {
215 .name = (char *) "server",
87aa8f19 216 .has_arg = optional_argument,
50d16976
JA
217 .val = 'S',
218 },
e46d8091 219 { .name = (char *) "daemonize",
402668f3 220 .has_arg = required_argument,
e46d8091
JA
221 .val = 'D',
222 },
132159a5
JA
223 {
224 .name = (char *) "client",
225 .has_arg = required_argument,
226 .val = 'C',
227 },
323255cc
JA
228 {
229 .name = (char *) "remote-config",
230 .has_arg = required_argument,
231 .val = 'R',
232 },
7d11f871
JA
233 {
234 .name = (char *) "cpuclock-test",
235 .has_arg = no_argument,
236 .val = 'T',
237 },
fec0f21c
JA
238 {
239 .name = (char *) "crctest",
240 .has_arg = optional_argument,
241 .val = 'G',
242 },
b76d85a4
JA
243 {
244 .name = (char *) "memcpytest",
245 .has_arg = optional_argument,
246 .val = 'M',
247 },
f2a2ce0e
HL
248 {
249 .name = (char *) "idle-prof",
250 .has_arg = required_argument,
251 .val = 'I',
252 },
06464907
JA
253 {
254 .name = (char *) "status-interval",
255 .has_arg = required_argument,
256 .val = 'L',
257 },
ca09be4b 258 {
b63efd30 259 .name = (char *) "trigger-file",
ca09be4b
JA
260 .has_arg = required_argument,
261 .val = 'W',
262 },
263 {
264 .name = (char *) "trigger-timeout",
265 .has_arg = required_argument,
266 .val = 'B',
267 },
b63efd30
JA
268 {
269 .name = (char *) "trigger",
270 .has_arg = required_argument,
271 .val = 'H',
272 },
273 {
274 .name = (char *) "trigger-remote",
275 .has_arg = required_argument,
276 .val = 'J',
277 },
d264264a
JA
278 {
279 .name = (char *) "aux-path",
280 .has_arg = required_argument,
281 .val = 'K',
282 },
b4692828
JA
283 {
284 .name = NULL,
285 },
286};
287
2bb3f0a7 288void free_threads_shm(void)
9d9eb2e7 289{
9d9eb2e7
JA
290 if (threads) {
291 void *tp = threads;
c8931876
JA
292#ifndef CONFIG_NO_SHM
293 struct shmid_ds sbuf;
9d9eb2e7
JA
294
295 threads = NULL;
2bb3f0a7
JA
296 shmdt(tp);
297 shmctl(shm_id, IPC_RMID, &sbuf);
298 shm_id = -1;
c8931876
JA
299#else
300 threads = NULL;
301 free(tp);
302#endif
2bb3f0a7
JA
303 }
304}
305
10aa136b 306static void free_shm(void)
2bb3f0a7
JA
307{
308 if (threads) {
9e684a49 309 flow_exit();
9d9eb2e7 310 fio_debug_jobp = NULL;
2bb3f0a7 311 free_threads_shm();
9d9eb2e7
JA
312 }
313
b63efd30
JA
314 free(trigger_file);
315 free(trigger_cmd);
316 free(trigger_remote_cmd);
317 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
318
5e333ada 319 options_free(fio_options, &def_thread.o);
243bfe19 320 fio_filelock_exit();
d619275c 321 file_hash_exit();
9d9eb2e7
JA
322 scleanup();
323}
324
325/*
326 * The thread area is shared between the main process and the job
327 * threads/processes. So setup a shared memory segment that will hold
328 * all the job info. We use the end of the region for keeping track of
329 * open files across jobs, for file sharing.
330 */
331static int setup_thread_area(void)
332{
9d9eb2e7
JA
333 if (threads)
334 return 0;
335
336 /*
337 * 1024 is too much on some machines, scale max_jobs if
338 * we get a failure that looks like too large a shm segment
339 */
340 do {
341 size_t size = max_jobs * sizeof(struct thread_data);
342
9d9eb2e7
JA
343 size += sizeof(unsigned int);
344
c8931876 345#ifndef CONFIG_NO_SHM
9d9eb2e7
JA
346 shm_id = shmget(0, size, IPC_CREAT | 0600);
347 if (shm_id != -1)
348 break;
f0c77f03 349 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
9d9eb2e7
JA
350 perror("shmget");
351 break;
352 }
c8931876
JA
353#else
354 threads = malloc(size);
355 if (threads)
356 break;
357#endif
9d9eb2e7
JA
358
359 max_jobs >>= 1;
360 } while (max_jobs);
361
c8931876 362#ifndef CONFIG_NO_SHM
9d9eb2e7
JA
363 if (shm_id == -1)
364 return 1;
365
366 threads = shmat(shm_id, NULL, 0);
367 if (threads == (void *) -1) {
368 perror("shmat");
369 return 1;
370 }
0cfe2089
TK
371 if (shm_attach_to_open_removed())
372 shmctl(shm_id, IPC_RMID, NULL);
c8931876 373#endif
9d9eb2e7
JA
374
375 memset(threads, 0, max_jobs * sizeof(struct thread_data));
5cf985e5 376 fio_debug_jobp = (unsigned int *)(threads + max_jobs);
9d9eb2e7 377 *fio_debug_jobp = -1;
9e684a49
DE
378
379 flow_init();
380
9d9eb2e7
JA
381 return 0;
382}
383
c2292325
JA
384static void dump_print_option(struct print_option *p)
385{
386 const char *delim;
387
388 if (!strcmp("description", p->name))
389 delim = "\"";
390 else
391 delim = "";
392
393 log_info("--%s%s", p->name, p->value ? "" : " ");
394 if (p->value)
395 log_info("=%s%s%s ", delim, p->value, delim);
396}
397
398static void dump_opt_list(struct thread_data *td)
399{
400 struct flist_head *entry;
401 struct print_option *p;
402
403 if (flist_empty(&td->opt_list))
404 return;
405
406 flist_for_each(entry, &td->opt_list) {
407 p = flist_entry(entry, struct print_option, list);
408 dump_print_option(p);
409 }
410}
411
412static void fio_dump_options_free(struct thread_data *td)
413{
414 while (!flist_empty(&td->opt_list)) {
415 struct print_option *p;
416
417 p = flist_first_entry(&td->opt_list, struct print_option, list);
418 flist_del_init(&p->list);
419 free(p->name);
420 free(p->value);
421 free(p);
422 }
423}
424
876e1001
JA
425static void copy_opt_list(struct thread_data *dst, struct thread_data *src)
426{
427 struct flist_head *entry;
428
429 if (flist_empty(&src->opt_list))
430 return;
431
432 flist_for_each(entry, &src->opt_list) {
433 struct print_option *srcp, *dstp;
434
435 srcp = flist_entry(entry, struct print_option, list);
436 dstp = malloc(sizeof(*dstp));
437 dstp->name = strdup(srcp->name);
438 if (srcp->value)
439 dstp->value = strdup(srcp->value);
440 else
441 dstp->value = NULL;
442 flist_add_tail(&dstp->list, &dst->opt_list);
443 }
444}
445
906c8d75
JA
446/*
447 * Return a free job structure.
448 */
8983ce71
JA
449static struct thread_data *get_new_job(bool global, struct thread_data *parent,
450 bool preserve_eo, const char *jobname)
ebac4655
JA
451{
452 struct thread_data *td;
453
5bba8b3a 454 if (global)
ebac4655 455 return &def_thread;
9d9eb2e7
JA
456 if (setup_thread_area()) {
457 log_err("error: failed to setup shm segment\n");
458 return NULL;
459 }
e61f1ec8
BC
460 if (thread_number >= max_jobs) {
461 log_err("error: maximum number of jobs (%d) reached.\n",
462 max_jobs);
ebac4655 463 return NULL;
e61f1ec8 464 }
ebac4655
JA
465
466 td = &threads[thread_number++];
ddaeaa5a 467 *td = *parent;
ebac4655 468
c2292325 469 INIT_FLIST_HEAD(&td->opt_list);
876e1001
JA
470 if (parent != &def_thread)
471 copy_opt_list(td, parent);
c2292325 472
de890a1e 473 td->io_ops = NULL;
356ef1a1 474 td->io_ops_init = 0;
de890a1e
SL
475 if (!preserve_eo)
476 td->eo = NULL;
477
e0b0d892
JA
478 td->o.uid = td->o.gid = -1U;
479
cade3ef4 480 dup_files(td, parent);
de890a1e 481 fio_options_mem_dupe(td);
cade3ef4 482
15dc1934
JA
483 profile_add_hooks(td);
484
ebac4655 485 td->thread_number = thread_number;
69bdd6ba 486 td->subjob_number = 0;
108fea77 487
ef3d8e53
JA
488 if (jobname)
489 td->o.name = strdup(jobname);
490
13978e86 491 if (!parent->o.group_reporting || parent == &def_thread)
108fea77
JA
492 stat_number++;
493
ebac4655
JA
494 return td;
495}
496
497static void put_job(struct thread_data *td)
498{
549577a7
JA
499 if (td == &def_thread)
500 return;
84dd1886 501
58c55ba0 502 profile_td_exit(td);
9e684a49 503 flow_exit_job(td);
549577a7 504
16edf25d 505 if (td->error)
6d86144d 506 log_info("fio: %s\n", td->verror);
16edf25d 507
7e356b2d 508 fio_options_free(td);
c2292325 509 fio_dump_options_free(td);
de890a1e
SL
510 if (td->io_ops)
511 free_ioengine(td);
7e356b2d 512
ef3d8e53
JA
513 if (td->o.name)
514 free(td->o.name);
515
ebac4655
JA
516 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
517 thread_number--;
518}
519
581e7141 520static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
127f6865 521{
581e7141 522 unsigned int bs = td->o.min_bs[ddir];
127f6865 523
ff58fced
JA
524 assert(ddir_rw(ddir));
525
ba3e4e0c 526 if (td->o.rate[ddir])
1b8dbf25 527 td->rate_bps[ddir] = td->o.rate[ddir];
ba3e4e0c 528 else
194fffd0 529 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
127f6865 530
1b8dbf25 531 if (!td->rate_bps[ddir]) {
127f6865
JA
532 log_err("rate lower than supported\n");
533 return -1;
534 }
535
50a8ce86 536 td->rate_next_io_time[ddir] = 0;
89cdea5e 537 td->rate_io_issue_bytes[ddir] = 0;
6c8611c7 538 td->last_usec[ddir] = 0;
127f6865
JA
539 return 0;
540}
541
581e7141
JA
542static int setup_rate(struct thread_data *td)
543{
544 int ret = 0;
545
546 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
547 ret = __setup_rate(td, DDIR_READ);
548 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
549 ret |= __setup_rate(td, DDIR_WRITE);
6eaf09d6
SL
550 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
551 ret |= __setup_rate(td, DDIR_TRIM);
581e7141
JA
552
553 return ret;
554}
555
8347239a
JA
556static int fixed_block_size(struct thread_options *o)
557{
558 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
559 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
6eaf09d6
SL
560 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
561 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
562 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
8347239a
JA
563}
564
23ed19b0
CE
565
566static unsigned long long get_rand_start_delay(struct thread_data *td)
567{
568 unsigned long long delayrange;
c3546b53 569 uint64_t frand_max;
23ed19b0
CE
570 unsigned long r;
571
572 delayrange = td->o.start_delay_high - td->o.start_delay;
573
c3546b53 574 frand_max = rand_max(&td->delay_state);
d6b72507 575 r = __rand(&td->delay_state);
c3546b53 576 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
23ed19b0
CE
577
578 delayrange += td->o.start_delay;
579 return delayrange;
580}
581
8c4e634a
JA
582/*
583 * <3 Johannes
584 */
9258468e
JA
585static unsigned int gcd(unsigned int m, unsigned int n)
586{
8c4e634a
JA
587 if (!n)
588 return m;
9258468e 589
8c4e634a 590 return gcd(n, m % n);
9258468e
JA
591}
592
dad915e3
JA
593/*
594 * Lazy way of fixing up options that depend on each other. We could also
595 * define option callback handlers, but this is easier.
596 */
4e991c23 597static int fixup_options(struct thread_data *td)
e1f36503 598{
2dc1bbeb 599 struct thread_options *o = &td->o;
eefd98b1 600 int ret = 0;
dad915e3 601
06eac6b2 602#ifndef CONFIG_PSHARED
9bbf57cc 603 if (!o->use_thread) {
f356d01d
JA
604 log_info("fio: this platform does not support process shared"
605 " mutexes, forcing use of threads. Use the 'thread'"
606 " option to get rid of this warning.\n");
9bbf57cc 607 o->use_thread = 1;
a9523c6f 608 ret = warnings_fatal;
f356d01d
JA
609 }
610#endif
611
2dc1bbeb 612 if (o->write_iolog_file && o->read_iolog_file) {
076efc7c 613 log_err("fio: read iolog overrides write_iolog\n");
2dc1bbeb
JA
614 free(o->write_iolog_file);
615 o->write_iolog_file = NULL;
a9523c6f 616 ret = warnings_fatal;
076efc7c 617 }
16b462ae 618
16b462ae 619 /*
ed335855 620 * only really works with 1 file
16b462ae 621 */
627aa1a8 622 if (o->zone_size && o->open_files > 1)
2dc1bbeb 623 o->zone_size = 0;
16b462ae 624
ed335855
SN
625 /*
626 * If zone_range isn't specified, backward compatibility dictates it
627 * should be made equal to zone_size.
628 */
629 if (o->zone_size && !o->zone_range)
630 o->zone_range = o->zone_size;
631
16b462ae
JA
632 /*
633 * Reads can do overwrites, we always need to pre-create the file
634 */
dcf9844e 635 if (td_read(td))
2dc1bbeb 636 o->overwrite = 1;
16b462ae 637
2dc1bbeb 638 if (!o->min_bs[DDIR_READ])
5ec10eaa 639 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
2dc1bbeb
JA
640 if (!o->max_bs[DDIR_READ])
641 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
642 if (!o->min_bs[DDIR_WRITE])
5ec10eaa 643 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
2dc1bbeb
JA
644 if (!o->max_bs[DDIR_WRITE])
645 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
6eaf09d6
SL
646 if (!o->min_bs[DDIR_TRIM])
647 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
648 if (!o->max_bs[DDIR_TRIM])
649 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
650
2dc1bbeb 651 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
6eaf09d6 652 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
a00735e6 653
2b7a01d0
JA
654 /*
655 * For random IO, allow blockalign offset other than min_bs.
656 */
657 if (!o->ba[DDIR_READ] || !td_random(td))
658 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
659 if (!o->ba[DDIR_WRITE] || !td_random(td))
660 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
6eaf09d6
SL
661 if (!o->ba[DDIR_TRIM] || !td_random(td))
662 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
2b7a01d0
JA
663
664 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
6eaf09d6
SL
665 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
666 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
9bbf57cc 667 !o->norandommap) {
2b7a01d0 668 log_err("fio: Any use of blockalign= turns off randommap\n");
9bbf57cc 669 o->norandommap = 1;
a9523c6f 670 ret = warnings_fatal;
2b7a01d0
JA
671 }
672
2dc1bbeb
JA
673 if (!o->file_size_high)
674 o->file_size_high = o->file_size_low;
9c60ce64 675
23ed19b0
CE
676 if (o->start_delay_high)
677 o->start_delay = get_rand_start_delay(td);
678
8347239a
JA
679 if (o->norandommap && o->verify != VERIFY_NONE
680 && !fixed_block_size(o)) {
681 log_err("fio: norandommap given for variable block sizes, "
83da8fbf 682 "verify limited\n");
a9523c6f 683 ret = warnings_fatal;
bb8895e0 684 }
9b87f09b 685 if (o->bs_unaligned && (o->odirect || td_ioengine_flagged(td, FIO_RAWIO)))
690adba3 686 log_err("fio: bs_unaligned may not work with raw io\n");
e0a22335 687
48097d5c
JA
688 /*
689 * thinktime_spin must be less than thinktime
690 */
2dc1bbeb
JA
691 if (o->thinktime_spin > o->thinktime)
692 o->thinktime_spin = o->thinktime;
e916b390
JA
693
694 /*
695 * The low water mark cannot be bigger than the iodepth
696 */
67bf9823
JA
697 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
698 o->iodepth_low = o->iodepth;
cb5ab512
JA
699
700 /*
701 * If batch number isn't set, default to the same as iodepth
702 */
2dc1bbeb
JA
703 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
704 o->iodepth_batch = o->iodepth;
b5af8293 705
82407585
RP
706 /*
707 * If max batch complete number isn't set or set incorrectly,
708 * default to the same as iodepth_batch_complete_min
709 */
710 if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
711 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
712
997b5680
SW
713 /*
714 * There's no need to check for in-flight overlapping IOs if the job
715 * isn't changing data or the maximum iodepth is guaranteed to be 1
716 */
717 if (o->serialize_overlap && !(td->flags & TD_F_READ_IOLOG) &&
718 (!(td_write(td) || td_trim(td)) || o->iodepth == 1))
719 o->serialize_overlap = 0;
720 /*
721 * Currently can't check for overlaps in offload mode
722 */
723 if (o->serialize_overlap && o->io_submit_mode == IO_MODE_OFFLOAD) {
724 log_err("fio: checking for in-flight overlaps when the "
725 "io_submit_mode is offload is not supported\n");
726 o->serialize_overlap = 0;
727 ret = warnings_fatal;
728 }
729
2dc1bbeb
JA
730 if (o->nr_files > td->files_index)
731 o->nr_files = td->files_index;
9f9214f2 732
2dc1bbeb
JA
733 if (o->open_files > o->nr_files || !o->open_files)
734 o->open_files = o->nr_files;
4e991c23 735
6eaf09d6
SL
736 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
737 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
738 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
739 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
4e991c23 740 log_err("fio: rate and rate_iops are mutually exclusive\n");
a9523c6f 741 ret = 1;
4e991c23 742 }
2af0ad5e
D
743 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
744 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
745 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
746 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
747 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
748 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
4e991c23 749 log_err("fio: minimum rate exceeds rate\n");
a9523c6f 750 ret = 1;
4e991c23
JA
751 }
752
cf4464ca
JA
753 if (!o->timeout && o->time_based) {
754 log_err("fio: time_based requires a runtime/timeout setting\n");
755 o->time_based = 0;
a9523c6f 756 ret = warnings_fatal;
cf4464ca
JA
757 }
758
aa31f1f1 759 if (o->fill_device && !o->size)
5921e80c 760 o->size = -1ULL;
5ec10eaa 761
9bbf57cc 762 if (o->verify != VERIFY_NONE) {
9f6d7080
SW
763 if (td_write(td) && o->do_verify && o->numjobs > 1 &&
764 (o->filename ||
765 !(o->unique_filename &&
766 strstr(o->filename_format, "$jobname") &&
767 strstr(o->filename_format, "$jobnum") &&
768 strstr(o->filename_format, "$filenum")))) {
769 log_info("fio: multiple writers may overwrite blocks "
770 "that belong to other jobs. This can cause "
a9523c6f
JA
771 "verification failures.\n");
772 ret = warnings_fatal;
773 }
774
b9f3ee6c
SW
775 /*
776 * Warn if verification is requested but no verification of any
777 * kind can be started due to time constraints
778 */
779 if (td_write(td) && o->do_verify && o->timeout &&
780 o->time_based && !td_read(td) && !o->verify_backlog) {
781 log_info("fio: verification read phase will never "
782 "start because write phase uses all of "
783 "runtime\n");
784 ret = warnings_fatal;
785 }
786
7627557b
JA
787 if (!fio_option_is_set(o, refill_buffers))
788 o->refill_buffers = 1;
789
2f1b8e8b
JA
790 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
791 !o->verify_interval)
792 o->verify_interval = o->min_bs[DDIR_WRITE];
7ffa8930
JA
793
794 /*
795 * Verify interval must be smaller or equal to the
796 * write size.
797 */
798 if (o->verify_interval > o->min_bs[DDIR_WRITE])
799 o->verify_interval = o->min_bs[DDIR_WRITE];
800 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
801 o->verify_interval = o->min_bs[DDIR_READ];
9258468e
JA
802
803 /*
804 * Verify interval must be a factor or both min and max
805 * write size
806 */
807 if (o->verify_interval % o->min_bs[DDIR_WRITE] ||
808 o->verify_interval % o->max_bs[DDIR_WRITE])
809 o->verify_interval = gcd(o->min_bs[DDIR_WRITE],
810 o->max_bs[DDIR_WRITE]);
d990588e 811 }
41ccd845 812
9bbf57cc 813 if (o->pre_read) {
49d719fc 814 if (o->invalidate_cache)
21c1b29e 815 o->invalidate_cache = 0;
9b87f09b 816 if (td_ioengine_flagged(td, FIO_PIPEIO)) {
9c0d2241
JA
817 log_info("fio: cannot pre-read files with an IO engine"
818 " that isn't seekable. Pre-read disabled.\n");
a9523c6f
JA
819 ret = warnings_fatal;
820 }
9c0d2241 821 }
34f1c044 822
ad705bcb 823 if (!o->unit_base) {
9b87f09b 824 if (td_ioengine_flagged(td, FIO_BIT_BASED))
ad705bcb
SN
825 o->unit_base = 1;
826 else
827 o->unit_base = 8;
828 }
829
cbbdf1c8
SW
830#ifndef FIO_HAVE_ANY_FALLOCATE
831 /* Platform doesn't support any fallocate so force it to none */
d27f75b8
SW
832 o->fallocate_mode = FIO_FALLOCATE_NONE;
833#endif
834
67bf9823 835#ifndef CONFIG_FDATASYNC
9bbf57cc 836 if (o->fdatasync_blocks) {
e72fa4d4
JA
837 log_info("fio: this platform does not support fdatasync()"
838 " falling back to using fsync(). Use the 'fsync'"
839 " option instead of 'fdatasync' to get rid of"
840 " this warning\n");
9bbf57cc
JA
841 o->fsync_blocks = o->fdatasync_blocks;
842 o->fdatasync_blocks = 0;
a9523c6f 843 ret = warnings_fatal;
e72fa4d4
JA
844 }
845#endif
846
93bcfd20
BC
847#ifdef WIN32
848 /*
849 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
850 * so fail if we're passed those flags
851 */
8945237c 852 if (td_ioengine_flagged(td, FIO_SYNCIO) && (o->odirect || o->sync_io)) {
93bcfd20
BC
853 log_err("fio: Windows does not support direct or non-buffered io with"
854 " the synchronous ioengines. Use the 'windowsaio' ioengine"
855 " with 'direct=1' and 'iodepth=1' instead.\n");
856 ret = 1;
857 }
858#endif
859
811ac503
JA
860 /*
861 * For fully compressible data, just zero them at init time.
6269123c
JA
862 * It's faster than repeatedly filling it. For non-zero
863 * compression, we should have refill_buffers set. Set it, unless
864 * the job file already changed it.
811ac503 865 */
6269123c
JA
866 if (o->compress_percentage) {
867 if (o->compress_percentage == 100) {
868 o->zero_buffers = 1;
869 o->compress_percentage = 0;
fbf6ded2 870 } else if (!fio_option_is_set(o, refill_buffers)) {
6269123c 871 o->refill_buffers = 1;
fbf6ded2
JA
872 td->flags |= TD_F_REFILL_BUFFERS;
873 }
811ac503
JA
874 }
875
5881fda4
JA
876 /*
877 * Using a non-uniform random distribution excludes usage of
878 * a random map
879 */
8945237c
JA
880 if (o->random_distribution != FIO_RAND_DIST_RANDOM)
881 o->norandommap = 1;
5881fda4 882
8d916c94
JA
883 /*
884 * If size is set but less than the min block size, complain
885 */
886 if (o->size && o->size < td_min_bs(td)) {
13e0f06b
JS
887 log_err("fio: size too small, must not be less than minimum block size: %llu < %u\n",
888 (unsigned long long) o->size, td_min_bs(td));
8d916c94
JA
889 ret = 1;
890 }
891
d01612f3
CM
892 /*
893 * O_ATOMIC implies O_DIRECT
894 */
8945237c
JA
895 if (o->oatomic)
896 o->odirect = 1;
d01612f3 897
04778baf
JA
898 /*
899 * If randseed is set, that overrides randrepeat
900 */
8945237c
JA
901 if (fio_option_is_set(o, rand_seed))
902 o->rand_repeatable = 0;
04778baf 903
8945237c 904 if (td_ioengine_flagged(td, FIO_NOEXTEND) && o->file_append) {
b1bebc32
JA
905 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
906 ret = 1;
907 }
908
79c896a1
JA
909 if (fio_option_is_set(o, gtod_cpu)) {
910 fio_gtod_init();
911 fio_gtod_set_cpu(o->gtod_cpu);
912 fio_gtod_offload = 1;
913 }
914
cf8a46a7
JA
915 td->loops = o->loops;
916 if (!td->loops)
917 td->loops = 1;
918
8945237c 919 if (o->block_error_hist && o->nr_files != 1) {
b3ec877c 920 log_err("fio: block error histogram only available "
66347cfa 921 "with a single file per job, but %d files "
8945237c 922 "provided\n", o->nr_files);
66347cfa
DE
923 ret = 1;
924 }
925
b599759b
JA
926 if (fio_option_is_set(o, clat_percentiles) &&
927 !fio_option_is_set(o, lat_percentiles)) {
928 o->lat_percentiles = !o->clat_percentiles;
929 } else if (fio_option_is_set(o, lat_percentiles) &&
930 !fio_option_is_set(o, clat_percentiles)) {
931 o->clat_percentiles = !o->lat_percentiles;
932 } else if (fio_option_is_set(o, lat_percentiles) &&
933 fio_option_is_set(o, clat_percentiles) &&
934 o->lat_percentiles && o->clat_percentiles) {
935 log_err("fio: lat_percentiles and clat_percentiles are "
936 "mutually exclusive\n");
937 ret = 1;
938 }
939
c3a32714
JA
940 /*
941 * Fix these up to be nsec internally
942 */
943 o->max_latency *= 1000ULL;
944 o->latency_target *= 1000ULL;
945 o->latency_window *= 1000ULL;
946
a9523c6f 947 return ret;
e1f36503
JA
948}
949
99c94a6b
JA
950static void init_rand_file_service(struct thread_data *td)
951{
952 unsigned long nranges = td->o.nr_files << FIO_FSERVICE_SHIFT;
953 const unsigned int seed = td->rand_seeds[FIO_RAND_FILE_OFF];
954
955 if (td->o.file_service_type == FIO_FSERVICE_ZIPF) {
956 zipf_init(&td->next_file_zipf, nranges, td->zipf_theta, seed);
957 zipf_disable_hash(&td->next_file_zipf);
958 } else if (td->o.file_service_type == FIO_FSERVICE_PARETO) {
959 pareto_init(&td->next_file_zipf, nranges, td->pareto_h, seed);
960 zipf_disable_hash(&td->next_file_zipf);
961 } else if (td->o.file_service_type == FIO_FSERVICE_GAUSS) {
962 gauss_init(&td->next_file_gauss, nranges, td->gauss_dev, seed);
963 gauss_disable_hash(&td->next_file_gauss);
964 }
965}
966
8f380de2
CJ
967void td_fill_verify_state_seed(struct thread_data *td)
968{
969 bool use64;
970
971 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
40c49471 972 use64 = true;
8f380de2 973 else
40c49471 974 use64 = false;
8f380de2
CJ
975
976 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF],
977 use64);
978}
979
36dd3379 980static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64)
4c07ad86 981{
36dd3379
JA
982 int i;
983
71cf4d08
JA
984 /*
985 * trimwrite is special in that we need to generate the same
986 * offsets to get the "write after trim" effect. If we are
987 * using bssplit to set buffer length distributions, ensure that
988 * we seed the trim and write generators identically.
989 */
2f282cec
JA
990 if (td_trimwrite(td)) {
991 init_rand_seed(&td->bsrange_state[DDIR_READ], td->rand_seeds[FIO_RAND_BS_OFF], use64);
992 init_rand_seed(&td->bsrange_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_BS1_OFF], use64);
993 init_rand_seed(&td->bsrange_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_BS1_OFF], use64);
994 } else {
995 init_rand_seed(&td->bsrange_state[DDIR_READ], td->rand_seeds[FIO_RAND_BS_OFF], use64);
996 init_rand_seed(&td->bsrange_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_BS1_OFF], use64);
997 init_rand_seed(&td->bsrange_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_BS2_OFF], use64);
998 }
999
8f380de2 1000 td_fill_verify_state_seed(td);
36dd3379 1001 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false);
4c07ad86
JA
1002
1003 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
c3546b53 1004 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
99c94a6b
JA
1005 else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
1006 init_rand_file_service(td);
4c07ad86 1007
c3546b53
JA
1008 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
1009 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
1010 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
6c8611c7
JA
1011 init_rand_seed(&td->poisson_state[0], td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
1012 init_rand_seed(&td->poisson_state[1], td->rand_seeds[FIO_RAND_POISSON2_OFF], 0);
1013 init_rand_seed(&td->poisson_state[2], td->rand_seeds[FIO_RAND_POISSON3_OFF], 0);
36dd3379
JA
1014 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false);
1015 init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false);
4c07ad86
JA
1016
1017 if (!td_random(td))
1018 return;
1019
1020 if (td->o.rand_repeatable)
d24945f0 1021 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
4c07ad86 1022
c3546b53 1023 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
36dd3379
JA
1024
1025 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1026 struct frand_state *s = &td->seq_rand_state[i];
1027
1028 init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false);
1029 }
5bfc35d7
JA
1030}
1031
4c07ad86
JA
1032void td_fill_rand_seeds(struct thread_data *td)
1033{
36dd3379 1034 bool use64;
c3546b53 1035
56e2a5fc 1036 if (td->o.allrand_repeatable) {
5c94b008
JA
1037 unsigned int i;
1038
1039 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
56e2a5fc
CE
1040 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
1041 + i;
1042 }
1043
c3546b53 1044 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
40c49471 1045 use64 = true;
c3546b53 1046 else
40c49471 1047 use64 = false;
c3546b53
JA
1048
1049 td_fill_rand_seeds_internal(td, use64);
3545a109 1050
c3546b53 1051 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
5c94b008 1052 frand_copy(&td->buf_state_prev, &td->buf_state);
4c07ad86
JA
1053}
1054
de890a1e
SL
1055/*
1056 * Initializes the ioengine configured for a job, if it has not been done so
1057 * already.
1058 */
1059int ioengine_load(struct thread_data *td)
1060{
6e5c4b8e
JA
1061 if (!td->o.ioengine) {
1062 log_err("fio: internal fault, no IO engine specified\n");
1063 return 1;
1064 }
de890a1e 1065
800334db
BW
1066 if (td->io_ops) {
1067 /* An engine is loaded, but the requested ioengine
1068 * may have changed.
1069 */
1070 if (!strcmp(td->io_ops->name, td->o.ioengine)) {
1071 /* The right engine is already loaded */
1072 return 0;
1073 }
1074
1075 /* Unload the old engine. */
1076 free_ioengine(td);
1077 }
1078
4fedf59a 1079 td->io_ops = load_ioengine(td);
de890a1e 1080 if (!td->io_ops) {
4fedf59a 1081 log_err("fio: failed to load engine\n");
de890a1e
SL
1082 return 1;
1083 }
1084
1085 if (td->io_ops->option_struct_size && td->io_ops->options) {
1086 /*
1087 * In cases where td->eo is set, clone it for a child thread.
1088 * This requires that the parent thread has the same ioengine,
1089 * but that requirement must be enforced by the code which
1090 * cloned the thread.
1091 */
1092 void *origeo = td->eo;
1093 /*
1094 * Otherwise use the default thread options.
1095 */
1096 if (!origeo && td != &def_thread && def_thread.eo &&
1097 def_thread.io_ops->options == td->io_ops->options)
1098 origeo = def_thread.eo;
1099
1100 options_init(td->io_ops->options);
1101 td->eo = malloc(td->io_ops->option_struct_size);
1102 /*
1103 * Use the default thread as an option template if this uses the
1104 * same options structure and there are non-default options
1105 * used.
1106 */
1107 if (origeo) {
1108 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
53b5693d 1109 options_mem_dupe(td->io_ops->options, td->eo);
de890a1e
SL
1110 } else {
1111 memset(td->eo, 0, td->io_ops->option_struct_size);
1112 fill_default_options(td->eo, td->io_ops->options);
1113 }
1114 *(struct thread_data **)td->eo = td;
1115 }
1116
9b87f09b
JA
1117 if (td->o.odirect)
1118 td->io_ops->flags |= FIO_RAWIO;
1119
1120 td_set_ioengine_flags(td);
de890a1e
SL
1121 return 0;
1122}
1123
d72be545
JA
1124static void init_flags(struct thread_data *td)
1125{
1126 struct thread_options *o = &td->o;
5c81ff5e 1127 int i;
d72be545
JA
1128
1129 if (o->verify_backlog)
1130 td->flags |= TD_F_VER_BACKLOG;
1131 if (o->trim_backlog)
1132 td->flags |= TD_F_TRIM_BACKLOG;
1133 if (o->read_iolog_file)
1134 td->flags |= TD_F_READ_IOLOG;
1135 if (o->refill_buffers)
1136 td->flags |= TD_F_REFILL_BUFFERS;
1bf2498d 1137 /*
b04f5905 1138 * Always scramble buffers if asked to
1bf2498d 1139 */
b04f5905
JA
1140 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
1141 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1142 /*
1143 * But also scramble buffers, unless we were explicitly asked
1144 * to zero them.
1145 */
1146 if (o->scramble_buffers && !(o->zero_buffers &&
1147 fio_option_is_set(o, zero_buffers)))
d72be545
JA
1148 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1149 if (o->verify != VERIFY_NONE)
1150 td->flags |= TD_F_VER_NONE;
a9da8ab2
JA
1151
1152 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
1153 td->flags |= TD_F_NEED_LOCK;
15600335
JA
1154
1155 if (o->mem_type == MEM_CUDA_MALLOC)
1156 td->flags &= ~TD_F_SCRAMBLE_BUFFERS;
5c81ff5e
JA
1157
1158 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1159 if (option_check_rate(td, i)) {
1160 td->flags |= TD_F_CHECK_RATE;
1161 break;
1162 }
1163 }
d72be545
JA
1164}
1165
9dbc7bfe
JA
1166static int setup_random_seeds(struct thread_data *td)
1167{
1168 unsigned long seed;
1169 unsigned int i;
1170
1ed84d86
TK
1171 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed)) {
1172 int ret = init_random_seeds(td->rand_seeds, sizeof(td->rand_seeds));
1173 if (!ret)
1174 td_fill_rand_seeds(td);
1175 return ret;
1176 }
9dbc7bfe 1177
40fe5e7b 1178 seed = td->o.rand_seed;
04778baf 1179 for (i = 0; i < 4; i++)
9dbc7bfe
JA
1180 seed *= 0x9e370001UL;
1181
1182 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
87a0ea3b 1183 td->rand_seeds[i] = seed * td->thread_number + i;
9dbc7bfe
JA
1184 seed *= 0x9e370001UL;
1185 }
1186
1187 td_fill_rand_seeds(td);
1188 return 0;
1189}
1190
de98bd30
JA
1191enum {
1192 FPRE_NONE = 0,
1193 FPRE_JOBNAME,
1194 FPRE_JOBNUM,
1195 FPRE_FILENUM
1196};
1197
1198static struct fpre_keyword {
1199 const char *keyword;
1200 size_t strlen;
1201 int key;
1202} fpre_keywords[] = {
1203 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1204 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1205 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1206 { .keyword = NULL, },
1207 };
1208
3660ceae 1209static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
de98bd30
JA
1210 const char *jobname, int jobnum, int filenum)
1211{
1212 struct fpre_keyword *f;
1213 char copy[PATH_MAX];
b400a20e 1214 size_t dst_left = PATH_MAX - 1;
de98bd30
JA
1215
1216 if (!o->filename_format || !strlen(o->filename_format)) {
1217 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
e5a43605 1218 return buf;
de98bd30
JA
1219 }
1220
1221 for (f = &fpre_keywords[0]; f->keyword; f++)
1222 f->strlen = strlen(f->keyword);
1223
3660ceae
JA
1224 buf[buf_size - 1] = '\0';
1225 strncpy(buf, o->filename_format, buf_size - 1);
1226
de98bd30
JA
1227 memset(copy, 0, sizeof(copy));
1228 for (f = &fpre_keywords[0]; f->keyword; f++) {
1229 do {
1230 size_t pre_len, post_start = 0;
1231 char *str, *dst = copy;
1232
e25b6d5a 1233 str = strcasestr(buf, f->keyword);
de98bd30
JA
1234 if (!str)
1235 break;
1236
1237 pre_len = str - buf;
1238 if (strlen(str) != f->strlen)
1239 post_start = pre_len + f->strlen;
1240
1241 if (pre_len) {
1242 strncpy(dst, buf, pre_len);
1243 dst += pre_len;
73a467e6 1244 dst_left -= pre_len;
de98bd30
JA
1245 }
1246
1247 switch (f->key) {
73a467e6
JA
1248 case FPRE_JOBNAME: {
1249 int ret;
1250
1251 ret = snprintf(dst, dst_left, "%s", jobname);
1252 if (ret < 0)
1253 break;
17a2be59
JA
1254 else if (ret > dst_left) {
1255 log_err("fio: truncated filename\n");
1256 dst += dst_left;
1257 dst_left = 0;
1258 } else {
1259 dst += ret;
1260 dst_left -= ret;
1261 }
de98bd30 1262 break;
73a467e6
JA
1263 }
1264 case FPRE_JOBNUM: {
1265 int ret;
1266
1267 ret = snprintf(dst, dst_left, "%d", jobnum);
1268 if (ret < 0)
1269 break;
17a2be59
JA
1270 else if (ret > dst_left) {
1271 log_err("fio: truncated filename\n");
1272 dst += dst_left;
1273 dst_left = 0;
1274 } else {
1275 dst += ret;
1276 dst_left -= ret;
1277 }
de98bd30 1278 break;
73a467e6
JA
1279 }
1280 case FPRE_FILENUM: {
1281 int ret;
1282
1283 ret = snprintf(dst, dst_left, "%d", filenum);
1284 if (ret < 0)
1285 break;
17a2be59
JA
1286 else if (ret > dst_left) {
1287 log_err("fio: truncated filename\n");
1288 dst += dst_left;
1289 dst_left = 0;
1290 } else {
1291 dst += ret;
1292 dst_left -= ret;
1293 }
de98bd30 1294 break;
73a467e6 1295 }
de98bd30
JA
1296 default:
1297 assert(0);
1298 break;
1299 }
1300
1301 if (post_start)
73a467e6 1302 strncpy(dst, buf + post_start, dst_left);
de98bd30 1303
3660ceae 1304 strncpy(buf, copy, buf_size - 1);
de98bd30
JA
1305 } while (1);
1306 }
1307
1308 return buf;
1309}
f9633d72 1310
8aa89d70 1311bool parse_dryrun(void)
f9633d72
JA
1312{
1313 return dump_cmdline || parse_only;
1314}
1315
3a5db920
JA
1316static void gen_log_name(char *name, size_t size, const char *logtype,
1317 const char *logname, unsigned int num,
1318 const char *suf, int per_job)
1319{
1320 if (per_job)
1321 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1322 else
1323 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1324}
1325
9cc8cb91
AK
1326static int check_waitees(char *waitee)
1327{
1328 struct thread_data *td;
1329 int i, ret = 0;
1330
1331 for_each_td(td, i) {
1332 if (td->subjob_number)
1333 continue;
1334
1335 ret += !strcmp(td->o.name, waitee);
1336 }
1337
1338 return ret;
1339}
1340
1341static bool wait_for_ok(const char *jobname, struct thread_options *o)
1342{
1343 int nw;
1344
1345 if (!o->wait_for)
1346 return true;
1347
1348 if (!strcmp(jobname, o->wait_for)) {
1349 log_err("%s: a job cannot wait for itself (wait_for=%s).\n",
1350 jobname, o->wait_for);
1351 return false;
1352 }
1353
1354 if (!(nw = check_waitees(o->wait_for))) {
1355 log_err("%s: waitee job %s unknown.\n", jobname, o->wait_for);
1356 return false;
1357 }
1358
1359 if (nw > 1) {
1360 log_err("%s: multiple waitees %s found,\n"
1361 "please avoid duplicates when using wait_for option.\n",
1362 jobname, o->wait_for);
1363 return false;
1364 }
1365
1366 return true;
1367}
1368
906c8d75
JA
1369/*
1370 * Adds a job to the list of things todo. Sanitizes the various options
1371 * to make sure we don't have conflicts, and initializes various
1372 * members of td.
1373 */
b7cfb2e1 1374static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
46bcd498 1375 int recursed, int client_type)
ebac4655 1376{
af52b345 1377 unsigned int i;
af52b345 1378 char fname[PATH_MAX];
e132cbae 1379 int numjobs, file_alloced;
de98bd30 1380 struct thread_options *o = &td->o;
cb7e0ace 1381 char logname[PATH_MAX + 32];
ebac4655 1382
ebac4655
JA
1383 /*
1384 * the def_thread is just for options, it's not a real job
1385 */
1386 if (td == &def_thread)
1387 return 0;
1388
d72be545
JA
1389 init_flags(td);
1390
cca73aa7
JA
1391 /*
1392 * if we are just dumping the output command line, don't add the job
1393 */
f9633d72 1394 if (parse_dryrun()) {
cca73aa7
JA
1395 put_job(td);
1396 return 0;
1397 }
1398
46bcd498
JA
1399 td->client_type = client_type;
1400
58c55ba0 1401 if (profile_td_init(td))
66251554 1402 goto err;
58c55ba0 1403
de890a1e 1404 if (ioengine_load(td))
205927a3 1405 goto err;
df64119d 1406
e132cbae 1407 file_alloced = 0;
de98bd30 1408 if (!o->filename && !td->files_index && !o->read_iolog_file) {
e132cbae 1409 file_alloced = 1;
80be24f4 1410
5e62565b 1411 if (o->nr_files == 1 && exists_and_not_regfile(jobname))
5903e7b7 1412 add_file(td, jobname, job_add_num, 0);
7b05a215 1413 else {
de98bd30 1414 for (i = 0; i < o->nr_files; i++)
3660ceae 1415 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
af52b345 1416 }
0af7b542 1417 }
ebac4655 1418
4e991c23
JA
1419 if (fixup_options(td))
1420 goto err;
e0a22335 1421
9cc8cb91
AK
1422 /*
1423 * Belongs to fixup_options, but o->name is not necessarily set as yet
1424 */
1425 if (!wait_for_ok(jobname, o))
1426 goto err;
1427
9e684a49
DE
1428 flow_init_job(td);
1429
de890a1e
SL
1430 /*
1431 * IO engines only need this for option callbacks, and the address may
1432 * change in subprocesses.
1433 */
1434 if (td->eo)
1435 *(struct thread_data **)td->eo = NULL;
1436
9b87f09b 1437 if (td_ioengine_flagged(td, FIO_DISKLESSIO)) {
07eb79df
JA
1438 struct fio_file *f;
1439
1440 for_each_file(td, f, i)
1441 f->real_file_size = -1ULL;
1442 }
1443
521da527 1444 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
ebac4655 1445
de98bd30 1446 td->ts.clat_percentiles = o->clat_percentiles;
b599759b 1447 td->ts.lat_percentiles = o->lat_percentiles;
de98bd30
JA
1448 td->ts.percentile_precision = o->percentile_precision;
1449 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
83349190 1450
6eaf09d6
SL
1451 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1452 td->ts.clat_stat[i].min_val = ULONG_MAX;
1453 td->ts.slat_stat[i].min_val = ULONG_MAX;
1454 td->ts.lat_stat[i].min_val = ULONG_MAX;
1455 td->ts.bw_stat[i].min_val = ULONG_MAX;
188b6016 1456 td->ts.iops_stat[i].min_val = ULONG_MAX;
6eaf09d6 1457 }
de98bd30 1458 td->ddir_seq_nr = o->ddir_seq_nr;
ebac4655 1459
de98bd30 1460 if ((o->stonewall || o->new_group) && prev_group_jobs) {
3c5df6fa 1461 prev_group_jobs = 0;
ebac4655 1462 groupid++;
d883efbd
JA
1463 if (groupid == INT_MAX) {
1464 log_err("fio: too many groups defined\n");
1465 goto err;
1466 }
3c5df6fa 1467 }
ebac4655
JA
1468
1469 td->groupid = groupid;
3c5df6fa 1470 prev_group_jobs++;
ebac4655 1471
9dbc7bfe 1472 if (setup_random_seeds(td)) {
1ed84d86 1473 td_verror(td, errno, "setup_random_seeds");
9c60ce64 1474 goto err;
93bcfd20 1475 }
9c60ce64 1476
ebac4655
JA
1477 if (setup_rate(td))
1478 goto err;
1479
dded427c 1480 if (o->write_lat_log) {
aee2ab67
JA
1481 struct log_params p = {
1482 .td = td,
1483 .avg_msec = o->log_avg_msec,
1e613c9c
KC
1484 .hist_msec = o->log_hist_msec,
1485 .hist_coarseness = o->log_hist_coarseness,
aee2ab67
JA
1486 .log_type = IO_LOG_TYPE_LAT,
1487 .log_offset = o->log_offset,
1488 .log_gz = o->log_gz,
b26317c9 1489 .log_gz_store = o->log_gz_store,
aee2ab67 1490 };
dded427c 1491 const char *pre = o->lat_log_file ? o->lat_log_file : o->name;
b26317c9 1492 const char *suf;
aee2ab67 1493
b26317c9
JA
1494 if (p.log_gz_store)
1495 suf = "log.fz";
1496 else
1497 suf = "log";
1498
dded427c 1499 gen_log_name(logname, sizeof(logname), "lat", pre,
3a5db920 1500 td->thread_number, suf, o->per_job_logs);
aee2ab67 1501 setup_log(&td->lat_log, &p, logname);
3a5db920 1502
dded427c 1503 gen_log_name(logname, sizeof(logname), "slat", pre,
3a5db920 1504 td->thread_number, suf, o->per_job_logs);
aee2ab67 1505 setup_log(&td->slat_log, &p, logname);
3a5db920 1506
dded427c 1507 gen_log_name(logname, sizeof(logname), "clat", pre,
3a5db920 1508 td->thread_number, suf, o->per_job_logs);
aee2ab67 1509 setup_log(&td->clat_log, &p, logname);
ebac4655 1510 }
1e613c9c 1511
dded427c 1512 if (o->write_hist_log) {
1e613c9c
KC
1513 struct log_params p = {
1514 .td = td,
1515 .avg_msec = o->log_avg_msec,
1516 .hist_msec = o->log_hist_msec,
1517 .hist_coarseness = o->log_hist_coarseness,
1518 .log_type = IO_LOG_TYPE_HIST,
1519 .log_offset = o->log_offset,
1520 .log_gz = o->log_gz,
1521 .log_gz_store = o->log_gz_store,
1522 };
dded427c 1523 const char *pre = o->hist_log_file ? o->hist_log_file : o->name;
1e613c9c
KC
1524 const char *suf;
1525
66da8a60
JA
1526#ifndef CONFIG_ZLIB
1527 if (td->client_type) {
1528 log_err("fio: --write_hist_log requires zlib in client/server mode\n");
1529 goto err;
1530 }
1531#endif
1532
1e613c9c
KC
1533 if (p.log_gz_store)
1534 suf = "log.fz";
1535 else
1536 suf = "log";
1537
dded427c 1538 gen_log_name(logname, sizeof(logname), "clat_hist", pre,
1e613c9c
KC
1539 td->thread_number, suf, o->per_job_logs);
1540 setup_log(&td->clat_hist_log, &p, logname);
1541 }
1542
dded427c 1543 if (o->write_bw_log) {
aee2ab67
JA
1544 struct log_params p = {
1545 .td = td,
1546 .avg_msec = o->log_avg_msec,
1e613c9c
KC
1547 .hist_msec = o->log_hist_msec,
1548 .hist_coarseness = o->log_hist_coarseness,
aee2ab67
JA
1549 .log_type = IO_LOG_TYPE_BW,
1550 .log_offset = o->log_offset,
1551 .log_gz = o->log_gz,
b26317c9 1552 .log_gz_store = o->log_gz_store,
aee2ab67 1553 };
dded427c 1554 const char *pre = o->bw_log_file ? o->bw_log_file : o->name;
b26317c9
JA
1555 const char *suf;
1556
a47591e4
JA
1557 if (fio_option_is_set(o, bw_avg_time))
1558 p.avg_msec = min(o->log_avg_msec, o->bw_avg_time);
1559 else
1560 o->bw_avg_time = p.avg_msec;
1e613c9c
KC
1561
1562 p.hist_msec = o->log_hist_msec;
1563 p.hist_coarseness = o->log_hist_coarseness;
a47591e4 1564
b26317c9
JA
1565 if (p.log_gz_store)
1566 suf = "log.fz";
1567 else
1568 suf = "log";
aee2ab67 1569
dded427c 1570 gen_log_name(logname, sizeof(logname), "bw", pre,
3a5db920 1571 td->thread_number, suf, o->per_job_logs);
aee2ab67 1572 setup_log(&td->bw_log, &p, logname);
cb7e0ace 1573 }
dded427c 1574 if (o->write_iops_log) {
aee2ab67
JA
1575 struct log_params p = {
1576 .td = td,
1577 .avg_msec = o->log_avg_msec,
1e613c9c
KC
1578 .hist_msec = o->log_hist_msec,
1579 .hist_coarseness = o->log_hist_coarseness,
aee2ab67
JA
1580 .log_type = IO_LOG_TYPE_IOPS,
1581 .log_offset = o->log_offset,
1582 .log_gz = o->log_gz,
b26317c9 1583 .log_gz_store = o->log_gz_store,
aee2ab67 1584 };
dded427c 1585 const char *pre = o->iops_log_file ? o->iops_log_file : o->name;
b26317c9
JA
1586 const char *suf;
1587
a47591e4
JA
1588 if (fio_option_is_set(o, iops_avg_time))
1589 p.avg_msec = min(o->log_avg_msec, o->iops_avg_time);
1590 else
1591 o->iops_avg_time = p.avg_msec;
1e613c9c
KC
1592
1593 p.hist_msec = o->log_hist_msec;
1594 p.hist_coarseness = o->log_hist_coarseness;
a47591e4 1595
b26317c9
JA
1596 if (p.log_gz_store)
1597 suf = "log.fz";
1598 else
1599 suf = "log";
aee2ab67 1600
dded427c 1601 gen_log_name(logname, sizeof(logname), "iops", pre,
3a5db920 1602 td->thread_number, suf, o->per_job_logs);
aee2ab67 1603 setup_log(&td->iops_log, &p, logname);
cb7e0ace 1604 }
ebac4655 1605
de98bd30
JA
1606 if (!o->name)
1607 o->name = strdup(jobname);
01452055 1608
129fb2d4 1609 if (output_format & FIO_OUTPUT_NORMAL) {
b990b5c0 1610 if (!job_add_num) {
b7cfb2e1 1611 if (is_backend && !recursed)
2f122b13 1612 fio_server_send_add_job(td);
807f9971 1613
9b87f09b 1614 if (!td_ioengine_flagged(td, FIO_NOIO)) {
d21e2535
JA
1615 char *c1, *c2, *c3, *c4;
1616 char *c5 = NULL, *c6 = NULL;
d694a6a7 1617 int i2p = is_power_of_2(o->kb_base);
f8977ee6 1618
e883cb35
JF
1619 c1 = num2str(o->min_bs[DDIR_READ], o->sig_figs, 1, i2p, N2S_BYTE);
1620 c2 = num2str(o->max_bs[DDIR_READ], o->sig_figs, 1, i2p, N2S_BYTE);
1621 c3 = num2str(o->min_bs[DDIR_WRITE], o->sig_figs, 1, i2p, N2S_BYTE);
1622 c4 = num2str(o->max_bs[DDIR_WRITE], o->sig_figs, 1, i2p, N2S_BYTE);
d21e2535
JA
1623
1624 if (!o->bs_is_seq_rand) {
e883cb35
JF
1625 c5 = num2str(o->min_bs[DDIR_TRIM], o->sig_figs, 1, i2p, N2S_BYTE);
1626 c6 = num2str(o->max_bs[DDIR_TRIM], o->sig_figs, 1, i2p, N2S_BYTE);
d21e2535
JA
1627 }
1628
1629 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1630 td->groupid,
1631 ddir_str(o->td_ddir));
1632
1633 if (o->bs_is_seq_rand)
7108dfd4 1634 log_info("bs=(R) %s-%s, (W) %s-%s, bs_is_seq_rand, ",
d21e2535
JA
1635 c1, c2, c3, c4);
1636 else
7108dfd4 1637 log_info("bs=(R) %s-%s, (W) %s-%s, (T) %s-%s, ",
d21e2535
JA
1638 c1, c2, c3, c4, c5, c6);
1639
1640 log_info("ioengine=%s, iodepth=%u\n",
de98bd30 1641 td->io_ops->name, o->iodepth);
f8977ee6
JA
1642
1643 free(c1);
1644 free(c2);
1645 free(c3);
1646 free(c4);
6eaf09d6
SL
1647 free(c5);
1648 free(c6);
f8977ee6 1649 }
b990b5c0 1650 } else if (job_add_num == 1)
6d86144d 1651 log_info("...\n");
c6ae0a5b 1652 }
ebac4655 1653
56a90eba
VF
1654 if (td_steadystate_init(td))
1655 goto err;
16e56d25 1656
ebac4655
JA
1657 /*
1658 * recurse add identical jobs, clear numjobs and stonewall options
1659 * as they don't apply to sub-jobs
1660 */
de98bd30 1661 numjobs = o->numjobs;
ebac4655 1662 while (--numjobs) {
8983ce71 1663 struct thread_data *td_new = get_new_job(false, td, true, jobname);
ebac4655
JA
1664
1665 if (!td_new)
1666 goto err;
1667
2dc1bbeb
JA
1668 td_new->o.numjobs = 1;
1669 td_new->o.stonewall = 0;
92c1d41f 1670 td_new->o.new_group = 0;
69bdd6ba 1671 td_new->subjob_number = numjobs;
16e56d25
VF
1672 td_new->o.ss_dur = o->ss_dur * 1000000l;
1673 td_new->o.ss_limit = o->ss_limit;
e132cbae
JA
1674
1675 if (file_alloced) {
455a50fa
CE
1676 if (td_new->files) {
1677 struct fio_file *f;
1678 for_each_file(td_new, f, i) {
1679 if (f->file_name)
ece6d647
AK
1680 sfree(f->file_name);
1681 sfree(f);
455a50fa 1682 }
ece6d647 1683 free(td_new->files);
455a50fa
CE
1684 td_new->files = NULL;
1685 }
ece6d647
AK
1686 td_new->files_index = 0;
1687 td_new->files_size = 0;
455a50fa
CE
1688 if (td_new->o.filename) {
1689 free(td_new->o.filename);
1690 td_new->o.filename = NULL;
1691 }
e132cbae
JA
1692 }
1693
bcbfeefa 1694 if (add_job(td_new, jobname, numjobs, 1, client_type))
ebac4655
JA
1695 goto err;
1696 }
3c5df6fa 1697
ebac4655
JA
1698 return 0;
1699err:
1700 put_job(td);
1701 return -1;
1702}
1703
79d16311
JA
1704/*
1705 * Parse as if 'o' was a command line
1706 */
46bcd498 1707void add_job_opts(const char **o, int client_type)
79d16311
JA
1708{
1709 struct thread_data *td, *td_parent;
1710 int i, in_global = 1;
1711 char jobname[32];
1712
1713 i = 0;
1714 td_parent = td = NULL;
1715 while (o[i]) {
1716 if (!strncmp(o[i], "name", 4)) {
1717 in_global = 0;
1718 if (td)
46bcd498 1719 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1720 td = NULL;
1721 sprintf(jobname, "%s", o[i] + 5);
1722 }
1723 if (in_global && !td_parent)
8983ce71 1724 td_parent = get_new_job(true, &def_thread, false, jobname);
79d16311
JA
1725 else if (!in_global && !td) {
1726 if (!td_parent)
1727 td_parent = &def_thread;
8983ce71 1728 td = get_new_job(false, td_parent, false, jobname);
79d16311
JA
1729 }
1730 if (in_global)
c2292325 1731 fio_options_parse(td_parent, (char **) &o[i], 1);
79d16311 1732 else
c2292325 1733 fio_options_parse(td, (char **) &o[i], 1);
79d16311
JA
1734 i++;
1735 }
1736
1737 if (td)
46bcd498 1738 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1739}
1740
01f06b63
JA
1741static int skip_this_section(const char *name)
1742{
ad0a2735
JA
1743 int i;
1744
1745 if (!nr_job_sections)
01f06b63
JA
1746 return 0;
1747 if (!strncmp(name, "global", 6))
1748 return 0;
1749
ad0a2735
JA
1750 for (i = 0; i < nr_job_sections; i++)
1751 if (!strcmp(job_sections[i], name))
1752 return 0;
1753
1754 return 1;
01f06b63
JA
1755}
1756
ebac4655
JA
1757static int is_empty_or_comment(char *line)
1758{
1759 unsigned int i;
1760
1761 for (i = 0; i < strlen(line); i++) {
1762 if (line[i] == ';')
1763 return 1;
5cc2da30
IM
1764 if (line[i] == '#')
1765 return 1;
76cd9378 1766 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
ebac4655
JA
1767 return 0;
1768 }
1769
1770 return 1;
1771}
1772
07261983
JA
1773/*
1774 * This is our [ini] type file parser.
1775 */
fc8d6d05 1776static int __parse_jobs_ini(struct thread_data *td,
b3270921
AK
1777 char *file, int is_buf, int stonewall_flag, int type,
1778 int nested, char *name, char ***popts, int *aopts, int *nopts)
ebac4655 1779{
8983ce71 1780 bool global = false;
b3270921 1781 char *string;
ebac4655
JA
1782 FILE *f;
1783 char *p;
0c7e37a0 1784 int ret = 0, stonewall;
097b2991 1785 int first_sect = 1;
ccf8f127 1786 int skip_fgets = 0;
01f06b63 1787 int inside_skip = 0;
3b8b7135
JA
1788 char **opts;
1789 int i, alloc_opts, num_opts;
ebac4655 1790
b3270921
AK
1791 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1792 assert(td || !nested);
1793
50d16976
JA
1794 if (is_buf)
1795 f = NULL;
1796 else {
1797 if (!strcmp(file, "-"))
1798 f = stdin;
1799 else
1800 f = fopen(file, "r");
5a729cbe 1801
50d16976 1802 if (!f) {
323255cc
JA
1803 int __err = errno;
1804
1805 log_err("fio: unable to open '%s' job file\n", file);
02957cd4
JA
1806 if (td)
1807 td_verror(td, __err, "job file open");
50d16976
JA
1808 return 1;
1809 }
ebac4655
JA
1810 }
1811
1812 string = malloc(4096);
7f7e6e59
JA
1813
1814 /*
1815 * it's really 256 + small bit, 280 should suffice
1816 */
b3270921
AK
1817 if (!nested) {
1818 name = malloc(280);
1819 memset(name, 0, 280);
1820 }
ebac4655 1821
b3270921
AK
1822 opts = NULL;
1823 if (nested && popts) {
1824 opts = *popts;
1825 alloc_opts = *aopts;
1826 num_opts = *nopts;
1827 }
1828
1829 if (!opts) {
1830 alloc_opts = 8;
1831 opts = malloc(sizeof(char *) * alloc_opts);
1832 num_opts = 0;
1833 }
3b8b7135 1834
0c7e37a0 1835 stonewall = stonewall_flag;
7c124ac1 1836 do {
ccf8f127
JA
1837 /*
1838 * if skip_fgets is set, we already have loaded a line we
1839 * haven't handled.
1840 */
1841 if (!skip_fgets) {
50d16976
JA
1842 if (is_buf)
1843 p = strsep(&file, "\n");
1844 else
43f74792 1845 p = fgets(string, 4096, f);
ccf8f127
JA
1846 if (!p)
1847 break;
1848 }
cdc7f193 1849
ccf8f127 1850 skip_fgets = 0;
cdc7f193 1851 strip_blank_front(&p);
6c7c7da1 1852 strip_blank_end(p);
cdc7f193 1853
b3270921 1854 dprint(FD_PARSE, "%s\n", p);
ebac4655
JA
1855 if (is_empty_or_comment(p))
1856 continue;
b3270921
AK
1857
1858 if (!nested) {
1859 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1860 if (inside_skip)
1861 continue;
1862
1863 log_err("fio: option <%s> outside of "
1864 "[] job section\n", p);
0e9c21a2 1865 ret = 1;
b3270921
AK
1866 break;
1867 }
1868
1869 name[strlen(name) - 1] = '\0';
1870
1871 if (skip_this_section(name)) {
1872 inside_skip = 1;
01f06b63 1873 continue;
b3270921
AK
1874 } else
1875 inside_skip = 0;
ebac4655 1876
b3270921 1877 dprint(FD_PARSE, "Parsing section [%s]\n", name);
7a4b80a1 1878
b3270921 1879 global = !strncmp(name, "global", 6);
01f06b63 1880
b3270921
AK
1881 if (dump_cmdline) {
1882 if (first_sect)
1883 log_info("fio ");
1884 if (!global)
1885 log_info("--name=%s ", name);
1886 first_sect = 0;
1887 }
ebac4655 1888
8983ce71 1889 td = get_new_job(global, &def_thread, false, name);
b3270921
AK
1890 if (!td) {
1891 ret = 1;
1892 break;
1893 }
cca73aa7 1894
b3270921
AK
1895 /*
1896 * Separate multiple job files by a stonewall
1897 */
1898 if (!global && stonewall) {
1899 td->o.stonewall = stonewall;
1900 stonewall = 0;
1901 }
ebac4655 1902
b3270921
AK
1903 num_opts = 0;
1904 memset(opts, 0, alloc_opts * sizeof(char *));
972cfd25 1905 }
b3270921
AK
1906 else
1907 skip_fgets = 1;
3b8b7135 1908
50d16976 1909 while (1) {
b3270921
AK
1910 if (!skip_fgets) {
1911 if (is_buf)
1912 p = strsep(&file, "\n");
1913 else
1914 p = fgets(string, 4096, f);
1915 if (!p)
1916 break;
1917 dprint(FD_PARSE, "%s", p);
1918 }
50d16976 1919 else
b3270921 1920 skip_fgets = 0;
50d16976 1921
ebac4655
JA
1922 if (is_empty_or_comment(p))
1923 continue;
e1f36503 1924
b6754f9d 1925 strip_blank_front(&p);
7c124ac1 1926
ccf8f127
JA
1927 /*
1928 * new section, break out and make sure we don't
1929 * fgets() a new line at the top.
1930 */
1931 if (p[0] == '[') {
b3270921
AK
1932 if (nested) {
1933 log_err("No new sections in included files\n");
1934 return 1;
1935 }
1936
ccf8f127 1937 skip_fgets = 1;
7c124ac1 1938 break;
ccf8f127 1939 }
7c124ac1 1940
4ae3f763 1941 strip_blank_end(p);
aea47d44 1942
b3270921 1943 if (!strncmp(p, "include", strlen("include"))) {
c0821a08
AK
1944 char *filename = p + strlen("include") + 1,
1945 *ts, *full_fn = NULL;
1946
1947 /*
1948 * Allow for the include filename
1949 * specification to be relative.
1950 */
1951 if (access(filename, F_OK) &&
1952 (ts = strrchr(file, '/'))) {
1953 int len = ts - file +
1954 strlen(filename) + 2;
1955
1956 if (!(full_fn = calloc(1, len))) {
1957 ret = ENOMEM;
1958 break;
1959 }
1960
1961 strncpy(full_fn,
1962 file, (ts - file) + 1);
1963 strncpy(full_fn + (ts - file) + 1,
1964 filename, strlen(filename));
1965 full_fn[len - 1] = 0;
1966 filename = full_fn;
1967 }
1968
1969 ret = __parse_jobs_ini(td, filename, is_buf,
1970 stonewall_flag, type, 1,
1971 name, &opts,
1972 &alloc_opts, &num_opts);
b3270921 1973
c0821a08
AK
1974 if (ret) {
1975 log_err("Error %d while parsing "
1976 "include file %s\n",
b3270921 1977 ret, filename);
b3270921 1978 }
c0821a08
AK
1979
1980 if (full_fn)
1981 free(full_fn);
1982
1983 if (ret)
1984 break;
1985
b3270921
AK
1986 continue;
1987 }
1988
3b8b7135
JA
1989 if (num_opts == alloc_opts) {
1990 alloc_opts <<= 1;
1991 opts = realloc(opts,
1992 alloc_opts * sizeof(char *));
1993 }
1994
1995 opts[num_opts] = strdup(p);
1996 num_opts++;
ebac4655 1997 }
ebac4655 1998
b3270921
AK
1999 if (nested) {
2000 *popts = opts;
2001 *aopts = alloc_opts;
2002 *nopts = num_opts;
2003 goto out;
2004 }
2005
c2292325
JA
2006 ret = fio_options_parse(td, opts, num_opts);
2007 if (!ret) {
2008 if (dump_cmdline)
2009 dump_opt_list(td);
2010
46bcd498 2011 ret = add_job(td, name, 0, 0, type);
c2292325 2012 } else {
b1508cf9
JA
2013 log_err("fio: job %s dropped\n", name);
2014 put_job(td);
45410acb 2015 }
3b8b7135
JA
2016
2017 for (i = 0; i < num_opts; i++)
2018 free(opts[i]);
2019 num_opts = 0;
7c124ac1 2020 } while (!ret);
ebac4655 2021
cca73aa7
JA
2022 if (dump_cmdline)
2023 log_info("\n");
2024
7e356b2d
JA
2025 i = 0;
2026 while (i < nr_job_sections) {
2027 free(job_sections[i]);
2028 i++;
2029 }
2030
2577594d 2031 free(opts);
b3270921
AK
2032out:
2033 free(string);
2034 if (!nested)
2035 free(name);
50d16976 2036 if (!is_buf && f != stdin)
5a729cbe 2037 fclose(f);
45410acb 2038 return ret;
ebac4655
JA
2039}
2040
b3270921
AK
2041int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
2042{
2043 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
2044 0, NULL, NULL, NULL, NULL);
2045}
2046
ebac4655
JA
2047static int fill_def_thread(void)
2048{
2049 memset(&def_thread, 0, sizeof(def_thread));
66e19a38 2050 INIT_FLIST_HEAD(&def_thread.opt_list);
ebac4655 2051
375b2695 2052 fio_getaffinity(getpid(), &def_thread.o.cpumask);
8b28bd41 2053 def_thread.o.error_dump = 1;
25bd16ce 2054
ebac4655 2055 /*
ee738499 2056 * fill default options
ebac4655 2057 */
214e1eca 2058 fio_fill_default_options(&def_thread);
ebac4655
JA
2059 return 0;
2060}
2061
2236df3d
JA
2062static void show_debug_categories(void)
2063{
44acdcf4 2064#ifdef FIO_INC_DEBUG
2236df3d
JA
2065 struct debug_level *dl = &debug_levels[0];
2066 int curlen, first = 1;
2067
2068 curlen = 0;
2069 while (dl->name) {
2070 int has_next = (dl + 1)->name != NULL;
2071
2072 if (first || curlen + strlen(dl->name) >= 80) {
2073 if (!first) {
2074 printf("\n");
2075 curlen = 0;
2076 }
2077 curlen += printf("\t\t\t%s", dl->name);
2078 curlen += 3 * (8 - 1);
2079 if (has_next)
2080 curlen += printf(",");
2081 } else {
2082 curlen += printf("%s", dl->name);
2083 if (has_next)
2084 curlen += printf(",");
2085 }
2086 dl++;
2087 first = 0;
2088 }
2089 printf("\n");
44acdcf4 2090#endif
2236df3d
JA
2091}
2092
baa9fe6b
TK
2093/*
2094 * Following options aren't printed by usage().
2095 * --append-terse - Equivalent to --output-format=terse, see f6a7df53.
2096 * --latency-log - Deprecated option.
2097 */
45378b35 2098static void usage(const char *name)
4785f995 2099{
3d43382c 2100 printf("%s\n", fio_version_string);
45378b35 2101 printf("%s [options] [job options] <job file(s)>\n", name);
2236df3d
JA
2102 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
2103 show_debug_categories();
111e032d 2104 printf(" --parse-only\t\tParse options only, don't start any IO\n");
83881283 2105 printf(" --output\t\tWrite output to file\n");
d23ae827 2106 printf(" --bandwidth-log\tGenerate aggregate bandwidth logs\n");
83881283 2107 printf(" --minimal\t\tMinimal (terse) output\n");
95a12fbe
TK
2108 printf(" --output-format=type\tOutput format (terse,json,json+,normal)\n");
2109 printf(" --terse-version=type\tSet terse version output format"
2110 " (default 3, or 2 or 4)\n");
f3afa57e 2111 printf(" --version\t\tPrint version info and exit\n");
83881283 2112 printf(" --help\t\tPrint this page\n");
23893646 2113 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
1a1822bf 2114 printf(" --crctest=[type]\tTest speed of checksum functions\n");
83881283 2115 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
5ec10eaa 2116 " them\n");
de890a1e
SL
2117 printf(" --enghelp=engine\tPrint ioengine help, or list"
2118 " available ioengines\n");
2119 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
2120 " cmd\n");
83881283
JA
2121 printf(" --showcmd\t\tTurn a job file into command line options\n");
2122 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
2123 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
e382e661
JA
2124 printf(" --eta-newline=time\tForce a new line for every 'time'");
2125 printf(" period passed\n");
06464907
JA
2126 printf(" --status-interval=t\tForce full status dump every");
2127 printf(" 't' period passed\n");
83881283 2128 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
5ec10eaa 2129 " writes\n");
95a12fbe
TK
2130 printf(" --section=name\tOnly run specified section in job file,"
2131 " multiple sections can be specified\n");
83881283 2132 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
95a12fbe 2133 " (def 16384)\n");
83881283
JA
2134 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
2135 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2136 printf(" --server=args\t\tStart a backend fio server\n");
2137 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
95a12fbe 2138 printf(" --client=hostname\tTalk to remote backend(s) fio server at hostname\n");
323255cc 2139 printf(" --remote-config=file\tTell fio server to load this local job file\n");
f2a2ce0e
HL
2140 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2141 "\t\t\t(option=system,percpu) or run unit work\n"
2142 "\t\t\tcalibration only (option=calibrate)\n");
b26317c9
JA
2143#ifdef CONFIG_ZLIB
2144 printf(" --inflate-log=log\tInflate and output compressed log\n");
2145#endif
b63efd30 2146 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
2ae7a90d 2147 printf(" --trigger-timeout=t\tExecute trigger at this time\n");
b63efd30
JA
2148 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
2149 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
d264264a 2150 printf(" --aux-path=path\tUse this path for fio state generated files\n");
d292596c 2151 printf("\nFio was written by Jens Axboe <axboe@kernel.dk>\n");
4785f995
JA
2152}
2153
79e48f72 2154#ifdef FIO_INC_DEBUG
ee56ad50 2155struct debug_level debug_levels[] = {
0b8d11ed
JA
2156 { .name = "process",
2157 .help = "Process creation/exit logging",
2158 .shift = FD_PROCESS,
2159 },
2160 { .name = "file",
2161 .help = "File related action logging",
2162 .shift = FD_FILE,
2163 },
2164 { .name = "io",
2165 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2166 .shift = FD_IO,
2167 },
2168 { .name = "mem",
2169 .help = "Memory allocation/freeing logging",
2170 .shift = FD_MEM,
2171 },
2172 { .name = "blktrace",
2173 .help = "blktrace action logging",
2174 .shift = FD_BLKTRACE,
2175 },
2176 { .name = "verify",
2177 .help = "IO verification action logging",
2178 .shift = FD_VERIFY,
2179 },
2180 { .name = "random",
2181 .help = "Random generation logging",
2182 .shift = FD_RANDOM,
2183 },
2184 { .name = "parse",
2185 .help = "Parser logging",
2186 .shift = FD_PARSE,
2187 },
2188 { .name = "diskutil",
2189 .help = "Disk utility logging actions",
2190 .shift = FD_DISKUTIL,
2191 },
2192 { .name = "job",
2193 .help = "Logging related to creating/destroying jobs",
2194 .shift = FD_JOB,
2195 },
2196 { .name = "mutex",
2197 .help = "Mutex logging",
2198 .shift = FD_MUTEX
2199 },
2200 { .name = "profile",
2201 .help = "Logging related to profiles",
2202 .shift = FD_PROFILE,
2203 },
2204 { .name = "time",
2205 .help = "Logging related to time keeping functions",
2206 .shift = FD_TIME,
2207 },
2208 { .name = "net",
2209 .help = "Network logging",
2210 .shift = FD_NET,
2211 },
3e260a46
JA
2212 { .name = "rate",
2213 .help = "Rate logging",
2214 .shift = FD_RATE,
2215 },
0c56718d
JA
2216 { .name = "compress",
2217 .help = "Log compression logging",
2218 .shift = FD_COMPRESS,
2219 },
16e56d25
VF
2220 { .name = "steadystate",
2221 .help = "Steady state detection logging",
2222 .shift = FD_STEADYSTATE,
2223 },
e569ca6b
VF
2224 { .name = "helperthread",
2225 .help = "Helper thread logging",
2226 .shift = FD_HELPERTHREAD,
2227 },
02444ad1 2228 { .name = NULL, },
ee56ad50
JA
2229};
2230
c09823ab 2231static int set_debug(const char *string)
ee56ad50
JA
2232{
2233 struct debug_level *dl;
2234 char *p = (char *) string;
2235 char *opt;
2236 int i;
2237
2706fa00
JA
2238 if (!string)
2239 return 0;
2240
ee56ad50 2241 if (!strcmp(string, "?") || !strcmp(string, "help")) {
ee56ad50
JA
2242 log_info("fio: dumping debug options:");
2243 for (i = 0; debug_levels[i].name; i++) {
2244 dl = &debug_levels[i];
2245 log_info("%s,", dl->name);
2246 }
bd6f78b2 2247 log_info("all\n");
c09823ab 2248 return 1;
ee56ad50
JA
2249 }
2250
2251 while ((opt = strsep(&p, ",")) != NULL) {
2252 int found = 0;
2253
5e1d306e
JA
2254 if (!strncmp(opt, "all", 3)) {
2255 log_info("fio: set all debug options\n");
2256 fio_debug = ~0UL;
2257 continue;
2258 }
2259
ee56ad50
JA
2260 for (i = 0; debug_levels[i].name; i++) {
2261 dl = &debug_levels[i];
5e1d306e
JA
2262 found = !strncmp(opt, dl->name, strlen(dl->name));
2263 if (!found)
2264 continue;
2265
2266 if (dl->shift == FD_JOB) {
2267 opt = strchr(opt, ':');
2268 if (!opt) {
2269 log_err("fio: missing job number\n");
2270 break;
2271 }
2272 opt++;
2273 fio_debug_jobno = atoi(opt);
2274 log_info("fio: set debug jobno %d\n",
2275 fio_debug_jobno);
2276 } else {
ee56ad50 2277 log_info("fio: set debug option %s\n", opt);
bd6f78b2 2278 fio_debug |= (1UL << dl->shift);
ee56ad50 2279 }
5e1d306e 2280 break;
ee56ad50
JA
2281 }
2282
2283 if (!found)
2284 log_err("fio: debug mask %s not found\n", opt);
2285 }
c09823ab 2286 return 0;
ee56ad50 2287}
79e48f72 2288#else
69b98d4c 2289static int set_debug(const char *string)
79e48f72
JA
2290{
2291 log_err("fio: debug tracing not included in build\n");
c09823ab 2292 return 1;
79e48f72
JA
2293}
2294#endif
9ac8a797 2295
4c6107ff
JA
2296static void fio_options_fill_optstring(void)
2297{
2298 char *ostr = cmd_optstr;
2299 int i, c;
2300
2301 c = i = 0;
2302 while (l_opts[i].name) {
2303 ostr[c++] = l_opts[i].val;
2304 if (l_opts[i].has_arg == required_argument)
2305 ostr[c++] = ':';
2306 else if (l_opts[i].has_arg == optional_argument) {
2307 ostr[c++] = ':';
2308 ostr[c++] = ':';
2309 }
2310 i++;
2311 }
2312 ostr[c] = '\0';
2313}
2314
c2c94585
JA
2315static int client_flag_set(char c)
2316{
2317 int i;
2318
2319 i = 0;
2320 while (l_opts[i].name) {
2321 int val = l_opts[i].val;
2322
2323 if (c == (val & 0xff))
2324 return (val & FIO_CLIENT_FLAG);
2325
2326 i++;
2327 }
2328
2329 return 0;
2330}
2331
10aa136b 2332static void parse_cmd_client(void *client, char *opt)
7a4b8240 2333{
fa2ea806 2334 fio_client_add_cmd_option(client, opt);
7a4b8240
JA
2335}
2336
a893c261
JA
2337static void show_closest_option(const char *name)
2338{
2339 int best_option, best_distance;
2340 int i, distance;
2341
2342 while (*name == '-')
2343 name++;
2344
2345 best_option = -1;
2346 best_distance = INT_MAX;
2347 i = 0;
2348 while (l_opts[i].name) {
2349 distance = string_distance(name, l_opts[i].name);
2350 if (distance < best_distance) {
2351 best_distance = distance;
2352 best_option = i;
2353 }
2354 i++;
2355 }
2356
3701636d 2357 if (best_option != -1 && string_distance_ok(name, best_distance))
a893c261
JA
2358 log_err("Did you mean %s?\n", l_opts[best_option].name);
2359}
2360
a666cab8
JA
2361static int parse_output_format(const char *optarg)
2362{
2363 char *p, *orig, *opt;
2364 int ret = 0;
2365
2366 p = orig = strdup(optarg);
2367
2368 output_format = 0;
2369
2370 while ((opt = strsep(&p, ",")) != NULL) {
2371 if (!strcmp(opt, "minimal") ||
2372 !strcmp(opt, "terse") ||
2373 !strcmp(opt, "csv"))
2374 output_format |= FIO_OUTPUT_TERSE;
2375 else if (!strcmp(opt, "json"))
2376 output_format |= FIO_OUTPUT_JSON;
513e37ee
VF
2377 else if (!strcmp(opt, "json+"))
2378 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
a666cab8
JA
2379 else if (!strcmp(opt, "normal"))
2380 output_format |= FIO_OUTPUT_NORMAL;
2381 else {
2382 log_err("fio: invalid output format %s\n", opt);
2383 ret = 1;
2384 break;
2385 }
2386 }
2387
2388 free(orig);
2389 return ret;
2390}
2391
46bcd498 2392int parse_cmd_line(int argc, char *argv[], int client_type)
ebac4655 2393{
b4692828 2394 struct thread_data *td = NULL;
c09823ab 2395 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
4c6107ff 2396 char *ostr = cmd_optstr;
977b9596 2397 char *pid_file = NULL;
9bae26e8 2398 void *cur_client = NULL;
81179eec 2399 int backend = 0;
ebac4655 2400
c2c94585
JA
2401 /*
2402 * Reset optind handling, since we may call this multiple times
2403 * for the backend.
2404 */
2405 optind = 1;
7a4b8240 2406
c2c94585
JA
2407 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2408 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
fa2ea806 2409 parse_cmd_client(cur_client, argv[optind - 1]);
7a4b8240
JA
2410 c &= ~FIO_CLIENT_FLAG;
2411 }
2412
ebac4655 2413 switch (c) {
2b386d25
JA
2414 case 'a':
2415 smalloc_pool_size = atoi(optarg);
52d892b2 2416 smalloc_pool_size <<= 10;
5f9454a2 2417 sinit();
2b386d25 2418 break;
b4692828 2419 case 'l':
cb7e0ace
JA
2420 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2421 do_exit++;
2422 exit_val = 1;
b4692828 2423 break;
3d73e5a9 2424 case 'b':
b4692828
JA
2425 write_bw_log = 1;
2426 break;
34b1b5c6
JA
2427 case 'o': {
2428 FILE *tmp;
2429
988da120 2430 if (f_out && f_out != stdout)
5e1d8745
JA
2431 fclose(f_out);
2432
34b1b5c6
JA
2433 tmp = fopen(optarg, "w+");
2434 if (!tmp) {
2435 log_err("fio: output file open error: %s\n", strerror(errno));
2436 exit_val = 1;
2437 do_exit++;
2438 break;
b4692828 2439 }
34b1b5c6 2440 f_err = f_out = tmp;
b4692828 2441 break;
34b1b5c6 2442 }
b4692828 2443 case 'm':
f3afa57e 2444 output_format = FIO_OUTPUT_TERSE;
b4692828 2445 break;
f3afa57e 2446 case 'F':
a666cab8
JA
2447 if (parse_output_format(optarg)) {
2448 log_err("fio: failed parsing output-format\n");
2449 exit_val = 1;
2450 do_exit++;
2451 break;
2452 }
2b8c71b0 2453 break;
f6a7df53
JA
2454 case 'f':
2455 output_format |= FIO_OUTPUT_TERSE;
2456 break;
b4692828 2457 case 'h':
1201b24a 2458 did_arg = true;
7874f8b7 2459 if (!cur_client) {
c2c94585 2460 usage(argv[0]);
7874f8b7
JA
2461 do_exit++;
2462 }
c2c94585 2463 break;
fd28ca49 2464 case 'c':
1201b24a 2465 did_arg = true;
7874f8b7 2466 if (!cur_client) {
c2c94585 2467 fio_show_option_help(optarg);
7874f8b7
JA
2468 do_exit++;
2469 }
c2c94585 2470 break;
de890a1e 2471 case 'i':
1201b24a 2472 did_arg = true;
de890a1e
SL
2473 if (!cur_client) {
2474 fio_show_ioengine_help(optarg);
2475 do_exit++;
2476 }
2477 break;
cca73aa7 2478 case 's':
1201b24a 2479 did_arg = true;
cca73aa7
JA
2480 dump_cmdline = 1;
2481 break;
724e4435
JA
2482 case 'r':
2483 read_only = 1;
2484 break;
b4692828 2485 case 'v':
1201b24a 2486 did_arg = true;
7874f8b7 2487 if (!cur_client) {
3d43382c 2488 log_info("%s\n", fio_version_string);
7874f8b7
JA
2489 do_exit++;
2490 }
c2c94585 2491 break;
f57a9c59
JA
2492 case 'V':
2493 terse_version = atoi(optarg);
a2c95580 2494 if (!(terse_version >= 2 && terse_version <= 5)) {
f57a9c59
JA
2495 log_err("fio: bad terse version format\n");
2496 exit_val = 1;
2497 do_exit++;
2498 }
2499 break;
e592a06b
AC
2500 case 'e':
2501 if (!strcmp("always", optarg))
2502 eta_print = FIO_ETA_ALWAYS;
2503 else if (!strcmp("never", optarg))
2504 eta_print = FIO_ETA_NEVER;
2505 break;
e382e661
JA
2506 case 'E': {
2507 long long t = 0;
2508
88038bc7 2509 if (check_str_time(optarg, &t, 1)) {
e382e661
JA
2510 log_err("fio: failed parsing eta time %s\n", optarg);
2511 exit_val = 1;
2512 do_exit++;
db37d890 2513 break;
e382e661 2514 }
88038bc7 2515 eta_new_line = t / 1000;
db37d890
JA
2516 if (!eta_new_line) {
2517 log_err("fio: eta new line time too short\n");
2518 exit_val = 1;
2519 do_exit++;
2520 }
2521 break;
2522 }
2523 case 'O': {
2524 long long t = 0;
2525
2526 if (check_str_time(optarg, &t, 1)) {
2527 log_err("fio: failed parsing eta interval %s\n", optarg);
2528 exit_val = 1;
2529 do_exit++;
2530 break;
2531 }
2532 eta_interval_msec = t / 1000;
2533 if (eta_interval_msec < DISK_UTIL_MSEC) {
2534 log_err("fio: eta interval time too short (%umsec min)\n", DISK_UTIL_MSEC);
2535 exit_val = 1;
2536 do_exit++;
2537 }
e382e661
JA
2538 break;
2539 }
ee56ad50 2540 case 'd':
c09823ab
JA
2541 if (set_debug(optarg))
2542 do_exit++;
ee56ad50 2543 break;
111e032d 2544 case 'P':
1201b24a 2545 did_arg = true;
111e032d
JA
2546 parse_only = 1;
2547 break;
ad0a2735
JA
2548 case 'x': {
2549 size_t new_size;
2550
01f06b63 2551 if (!strcmp(optarg, "global")) {
5ec10eaa
JA
2552 log_err("fio: can't use global as only "
2553 "section\n");
c09823ab
JA
2554 do_exit++;
2555 exit_val = 1;
01f06b63
JA
2556 break;
2557 }
ad0a2735
JA
2558 new_size = (nr_job_sections + 1) * sizeof(char *);
2559 job_sections = realloc(job_sections, new_size);
2560 job_sections[nr_job_sections] = strdup(optarg);
2561 nr_job_sections++;
01f06b63 2562 break;
ad0a2735 2563 }
b26317c9
JA
2564#ifdef CONFIG_ZLIB
2565 case 'X':
2566 exit_val = iolog_file_inflate(optarg);
1201b24a 2567 did_arg = true;
b26317c9
JA
2568 do_exit++;
2569 break;
2570#endif
9ac8a797 2571 case 'p':
1201b24a 2572 did_arg = true;
4af7c007
JA
2573 if (exec_profile)
2574 free(exec_profile);
07b3232d 2575 exec_profile = strdup(optarg);
9ac8a797 2576 break;
b4692828 2577 case FIO_GETOPT_JOB: {
5ec10eaa 2578 const char *opt = l_opts[lidx].name;
b4692828
JA
2579 char *val = optarg;
2580
c2b1e753 2581 if (!strncmp(opt, "name", 4) && td) {
46bcd498 2582 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
66251554 2583 if (ret)
4a4ac4e3 2584 goto out_free;
c2b1e753 2585 td = NULL;
1201b24a 2586 did_arg = true;
c2b1e753 2587 }
b4692828 2588 if (!td) {
01f06b63 2589 int is_section = !strncmp(opt, "name", 4);
3106f220
JA
2590 int global = 0;
2591
01f06b63 2592 if (!is_section || !strncmp(val, "global", 6))
3106f220 2593 global = 1;
c2b1e753 2594
01f06b63
JA
2595 if (is_section && skip_this_section(val))
2596 continue;
2597
8983ce71 2598 td = get_new_job(global, &def_thread, true, NULL);
f6ff24cb
JA
2599 if (!td || ioengine_load(td)) {
2600 if (td) {
2601 put_job(td);
2602 td = NULL;
2603 }
2604 do_exit++;
205a719e 2605 exit_val = 1;
f6ff24cb
JA
2606 break;
2607 }
de890a1e 2608 fio_options_set_ioengine_opts(l_opts, td);
b4692828 2609 }
38d0adb0 2610
9d918187
JA
2611 if ((!val || !strlen(val)) &&
2612 l_opts[lidx].has_arg == required_argument) {
2613 log_err("fio: option %s requires an argument\n", opt);
2614 ret = 1;
2615 } else
2616 ret = fio_cmd_option_parse(td, opt, val);
2617
bfb3ea22
JA
2618 if (ret) {
2619 if (td) {
2620 put_job(td);
2621 td = NULL;
2622 }
a88c8c14 2623 do_exit++;
205a719e 2624 exit_val = 1;
bfb3ea22 2625 }
de890a1e
SL
2626
2627 if (!ret && !strcmp(opt, "ioengine")) {
f6ff24cb 2628 if (ioengine_load(td)) {
342f570e
JA
2629 put_job(td);
2630 td = NULL;
f6ff24cb 2631 do_exit++;
205a719e 2632 exit_val = 1;
f6ff24cb
JA
2633 break;
2634 }
de890a1e
SL
2635 fio_options_set_ioengine_opts(l_opts, td);
2636 }
2637 break;
2638 }
2639 case FIO_GETOPT_IOENGINE: {
2640 const char *opt = l_opts[lidx].name;
2641 char *val = optarg;
b1ed74e0
JA
2642
2643 if (!td)
2644 break;
2645
de890a1e 2646 ret = fio_cmd_ioengine_option_parse(td, opt, val);
b4692828
JA
2647 break;
2648 }
a9523c6f
JA
2649 case 'w':
2650 warnings_fatal = 1;
2651 break;
fca70358
JA
2652 case 'j':
2653 max_jobs = atoi(optarg);
2654 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2655 log_err("fio: invalid max jobs: %d\n", max_jobs);
2656 do_exit++;
2657 exit_val = 1;
2658 }
2659 break;
50d16976 2660 case 'S':
1201b24a 2661 did_arg = true;
c8931876 2662#ifndef CONFIG_NO_SHM
a37f69b7 2663 if (nr_clients) {
132159a5
JA
2664 log_err("fio: can't be both client and server\n");
2665 do_exit++;
2666 exit_val = 1;
2667 break;
2668 }
87aa8f19 2669 if (optarg)
bebe6398 2670 fio_server_set_arg(optarg);
50d16976 2671 is_backend = 1;
81179eec 2672 backend = 1;
c8931876
JA
2673#else
2674 log_err("fio: client/server requires SHM support\n");
2675 do_exit++;
2676 exit_val = 1;
2677#endif
50d16976 2678 break;
e46d8091 2679 case 'D':
6cfe9a8c
JA
2680 if (pid_file)
2681 free(pid_file);
402668f3 2682 pid_file = strdup(optarg);
e46d8091 2683 break;
f2a2ce0e
HL
2684 case 'I':
2685 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2686 /* exit on error and calibration only */
1201b24a 2687 did_arg = true;
f2a2ce0e 2688 do_exit++;
7d8ea970 2689 if (ret == -1)
f2a2ce0e
HL
2690 exit_val = 1;
2691 }
2692 break;
132159a5 2693 case 'C':
1201b24a 2694 did_arg = true;
132159a5
JA
2695 if (is_backend) {
2696 log_err("fio: can't be both client and server\n");
2697 do_exit++;
2698 exit_val = 1;
2699 break;
2700 }
08633c32
BE
2701 /* if --client parameter contains a pathname */
2702 if (0 == access(optarg, R_OK)) {
2703 /* file contains a list of host addrs or names */
2972708f 2704 char hostaddr[PATH_MAX] = {0};
08633c32
BE
2705 char formatstr[8];
2706 FILE * hostf = fopen(optarg, "r");
2707 if (!hostf) {
2708 log_err("fio: could not open client list file %s for read\n", optarg);
2709 do_exit++;
2710 exit_val = 1;
2711 break;
2712 }
2972708f
JA
2713 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2714 /*
2715 * read at most PATH_MAX-1 chars from each
2716 * record in this file
2717 */
08633c32
BE
2718 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2719 /* expect EVERY host in file to be valid */
2720 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2721 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2722 do_exit++;
2723 exit_val = 1;
2724 break;
2725 }
2726 }
2727 fclose(hostf);
2728 break; /* no possibility of job file for "this client only" */
2729 }
a5276616 2730 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
bebe6398
JA
2731 log_err("fio: failed adding client %s\n", optarg);
2732 do_exit++;
2733 exit_val = 1;
2734 break;
2735 }
14ea90ed
JA
2736 /*
2737 * If the next argument exists and isn't an option,
2738 * assume it's a job file for this client only.
2739 */
2740 while (optind < argc) {
2741 if (!strncmp(argv[optind], "--", 2) ||
2742 !strncmp(argv[optind], "-", 1))
2743 break;
2744
bb781643 2745 if (fio_client_add_ini_file(cur_client, argv[optind], false))
323255cc 2746 break;
14ea90ed
JA
2747 optind++;
2748 }
132159a5 2749 break;
323255cc 2750 case 'R':
1201b24a 2751 did_arg = true;
bb781643 2752 if (fio_client_add_ini_file(cur_client, optarg, true)) {
323255cc
JA
2753 do_exit++;
2754 exit_val = 1;
2755 }
2756 break;
7d11f871 2757 case 'T':
1201b24a 2758 did_arg = true;
7d11f871 2759 do_exit++;
aad918e4 2760 exit_val = fio_monotonic_clocktest(1);
7d11f871 2761 break;
fec0f21c 2762 case 'G':
1201b24a 2763 did_arg = true;
fec0f21c
JA
2764 do_exit++;
2765 exit_val = fio_crctest(optarg);
2766 break;
b76d85a4
JA
2767 case 'M':
2768 did_arg = true;
2769 do_exit++;
2770 exit_val = fio_memcpy_test(optarg);
2771 break;
06464907
JA
2772 case 'L': {
2773 long long val;
2774
88038bc7 2775 if (check_str_time(optarg, &val, 1)) {
06464907
JA
2776 log_err("fio: failed parsing time %s\n", optarg);
2777 do_exit++;
2778 exit_val = 1;
2779 break;
2780 }
969357b0
JA
2781 if (val < 1000) {
2782 log_err("fio: status interval too small\n");
2783 do_exit++;
2784 exit_val = 1;
2785 }
88038bc7 2786 status_interval = val / 1000;
06464907
JA
2787 break;
2788 }
b63efd30 2789 case 'W':
f9cbae9e
JA
2790 if (trigger_file)
2791 free(trigger_file);
b63efd30
JA
2792 trigger_file = strdup(optarg);
2793 break;
2794 case 'H':
f9cbae9e
JA
2795 if (trigger_cmd)
2796 free(trigger_cmd);
b63efd30
JA
2797 trigger_cmd = strdup(optarg);
2798 break;
2799 case 'J':
f9cbae9e
JA
2800 if (trigger_remote_cmd)
2801 free(trigger_remote_cmd);
b63efd30 2802 trigger_remote_cmd = strdup(optarg);
ca09be4b 2803 break;
d264264a
JA
2804 case 'K':
2805 if (aux_path)
2806 free(aux_path);
2807 aux_path = strdup(optarg);
2808 break;
ca09be4b
JA
2809 case 'B':
2810 if (check_str_time(optarg, &trigger_timeout, 1)) {
2811 log_err("fio: failed parsing time %s\n", optarg);
2812 do_exit++;
2813 exit_val = 1;
2814 }
2815 trigger_timeout /= 1000000;
2816 break;
798827c8
JA
2817 case '?':
2818 log_err("%s: unrecognized option '%s'\n", argv[0],
2819 argv[optind - 1]);
a893c261 2820 show_closest_option(argv[optind - 1]);
b4692828 2821 default:
c09823ab
JA
2822 do_exit++;
2823 exit_val = 1;
b4692828 2824 break;
ebac4655 2825 }
c7d5c941
JA
2826 if (do_exit)
2827 break;
ebac4655 2828 }
c9fad893 2829
0b14f0a8
JA
2830 if (do_exit && !(is_backend || nr_clients))
2831 exit(exit_val);
536582bf 2832
fb296043
JA
2833 if (nr_clients && fio_clients_connect())
2834 exit(1);
132159a5 2835
81179eec 2836 if (is_backend && backend)
402668f3 2837 return fio_start_server(pid_file);
b8ba87ac
JA
2838 else if (pid_file)
2839 free(pid_file);
50d16976 2840
b4692828 2841 if (td) {
7d8ea970 2842 if (!ret) {
46bcd498 2843 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
7d8ea970 2844 if (ret)
3bc20ebd 2845 exit(1);
7d8ea970 2846 }
972cfd25 2847 }
774a6177 2848
7874f8b7 2849 while (!ret && optind < argc) {
b4692828
JA
2850 ini_idx++;
2851 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2852 ini_file[ini_idx - 1] = strdup(argv[optind]);
2853 optind++;
eb8bbf48 2854 }
972cfd25 2855
4a4ac4e3 2856out_free:
972cfd25 2857 return ini_idx;
ebac4655
JA
2858}
2859
0420ba6a 2860int fio_init_options(void)
ebac4655 2861{
b4692828
JA
2862 f_out = stdout;
2863 f_err = stderr;
2864
4c6107ff 2865 fio_options_fill_optstring();
5ec10eaa 2866 fio_options_dup_and_init(l_opts);
b4692828 2867
9d9eb2e7
JA
2868 atexit(free_shm);
2869
ebac4655
JA
2870 if (fill_def_thread())
2871 return 1;
2872
0420ba6a
JA
2873 return 0;
2874}
2875
51167799
JA
2876extern int fio_check_options(struct thread_options *);
2877
0420ba6a
JA
2878int parse_options(int argc, char *argv[])
2879{
46bcd498 2880 const int type = FIO_CLIENT_TYPE_CLI;
0420ba6a
JA
2881 int job_files, i;
2882
2883 if (fio_init_options())
2884 return 1;
51167799
JA
2885 if (fio_test_cconv(&def_thread.o))
2886 log_err("fio: failed internal cconv test\n");
0420ba6a 2887
46bcd498 2888 job_files = parse_cmd_line(argc, argv, type);
ebac4655 2889
cdf54d85
JA
2890 if (job_files > 0) {
2891 for (i = 0; i < job_files; i++) {
bc4f5ef6 2892 if (i && fill_def_thread())
132159a5 2893 return 1;
cdf54d85
JA
2894 if (nr_clients) {
2895 if (fio_clients_send_ini(ini_file[i]))
2896 return 1;
2897 free(ini_file[i]);
2898 } else if (!is_backend) {
46bcd498 2899 if (parse_jobs_ini(ini_file[i], 0, i, type))
cdf54d85
JA
2900 return 1;
2901 free(ini_file[i]);
2902 }
132159a5 2903 }
14ea90ed
JA
2904 } else if (nr_clients) {
2905 if (fill_def_thread())
2906 return 1;
2907 if (fio_clients_send_ini(NULL))
2908 return 1;
972cfd25 2909 }
ebac4655 2910
88c6ed80 2911 free(ini_file);
7e356b2d 2912 fio_options_free(&def_thread);
bcbfeefa 2913 filesetup_mem_free();
b4692828
JA
2914
2915 if (!thread_number) {
f9633d72 2916 if (parse_dryrun())
cca73aa7 2917 return 0;
07b3232d
JA
2918 if (exec_profile)
2919 return 0;
a37f69b7 2920 if (is_backend || nr_clients)
5c341e9a 2921 return 0;
085399db
JA
2922 if (did_arg)
2923 return 0;
cca73aa7 2924
4bb7a775 2925 log_err("No job(s) defined\n\n");
a2e4a093
JA
2926 usage(argv[0]);
2927 return 1;
b4692828
JA
2928 }
2929
129fb2d4 2930 if (output_format & FIO_OUTPUT_NORMAL)
3d43382c 2931 log_info("%s\n", fio_version_string);
f6dea4d3 2932
ebac4655
JA
2933 return 0;
2934}
588b7f09
JA
2935
2936void options_default_fill(struct thread_options *o)
2937{
2938 memcpy(o, &def_thread.o, sizeof(*o));
2939}
c2292325 2940
66e19a38 2941struct thread_data *get_global_options(void)
c2292325 2942{
66e19a38 2943 return &def_thread;
c2292325 2944}