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