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