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