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