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