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