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