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