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