Allow fio to terminate jobs when steady state is attained
[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
1606
1607 /* when group reporting is enabled only the cache allocated for the final td is actually used */
1608 td->ss.cache = malloc(o->ss_dur * sizeof(*(td->ss.cache)));
1609 if (td->ss.cache == NULL)
1610 {
1611 log_err("fio: unable to allocate memory for steadystate cache\n");
1612 goto err;
1613 }
1614 for (i = 0; i < td->ss.dur; i++)
1615 td->ss.cache[i] = 0;
1616 /* initialize so that it is obvious if the cache is not full in the output */
1617
1618 td->ss.ramp_time_over = (td->ss.ramp_time == 0);
1619 td->ss.attained = 0;
1620 td->ss.last_in_group = 0;
1621 td->ss.head = 0;
1622 td->ss.tail = 0;
1623 td->ss.sum_x = o->ss_dur * (o->ss_dur - 1) / 2;
1624 td->ss.sum_x_sq = (o->ss_dur - 1) * (o->ss_dur) * (2*o->ss_dur - 1) / 6;
1625 td->ss.prev_bytes = 0;
1626 td->ss.prev_iops = 0;
1627 td->ss.sum_y = 0;
1628 td->ss.oldest_y = 0;
1629 td->ss.criterion = 0.0;
1630 td->ts.ss = &td->ss;
1631 }
1632 else
1633 td->ts.ss = NULL;
1634
ebac4655
JA
1635 /*
1636 * recurse add identical jobs, clear numjobs and stonewall options
1637 * as they don't apply to sub-jobs
1638 */
de98bd30 1639 numjobs = o->numjobs;
ebac4655 1640 while (--numjobs) {
ef3d8e53 1641 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
ebac4655
JA
1642
1643 if (!td_new)
1644 goto err;
1645
2dc1bbeb
JA
1646 td_new->o.numjobs = 1;
1647 td_new->o.stonewall = 0;
92c1d41f 1648 td_new->o.new_group = 0;
69bdd6ba 1649 td_new->subjob_number = numjobs;
16e56d25
VF
1650 td_new->o.ss_dur = o->ss_dur * 1000000l;
1651 td_new->o.ss_limit = o->ss_limit;
e132cbae
JA
1652
1653 if (file_alloced) {
455a50fa
CE
1654 if (td_new->files) {
1655 struct fio_file *f;
1656 for_each_file(td_new, f, i) {
1657 if (f->file_name)
ece6d647
AK
1658 sfree(f->file_name);
1659 sfree(f);
455a50fa 1660 }
ece6d647 1661 free(td_new->files);
455a50fa
CE
1662 td_new->files = NULL;
1663 }
ece6d647
AK
1664 td_new->files_index = 0;
1665 td_new->files_size = 0;
455a50fa
CE
1666 if (td_new->o.filename) {
1667 free(td_new->o.filename);
1668 td_new->o.filename = NULL;
1669 }
e132cbae
JA
1670 }
1671
bcbfeefa 1672 if (add_job(td_new, jobname, numjobs, 1, client_type))
ebac4655
JA
1673 goto err;
1674 }
3c5df6fa 1675
ebac4655
JA
1676 return 0;
1677err:
1678 put_job(td);
1679 return -1;
1680}
1681
79d16311
JA
1682/*
1683 * Parse as if 'o' was a command line
1684 */
46bcd498 1685void add_job_opts(const char **o, int client_type)
79d16311
JA
1686{
1687 struct thread_data *td, *td_parent;
1688 int i, in_global = 1;
1689 char jobname[32];
1690
1691 i = 0;
1692 td_parent = td = NULL;
1693 while (o[i]) {
1694 if (!strncmp(o[i], "name", 4)) {
1695 in_global = 0;
1696 if (td)
46bcd498 1697 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1698 td = NULL;
1699 sprintf(jobname, "%s", o[i] + 5);
1700 }
1701 if (in_global && !td_parent)
ef3d8e53 1702 td_parent = get_new_job(1, &def_thread, 0, jobname);
79d16311
JA
1703 else if (!in_global && !td) {
1704 if (!td_parent)
1705 td_parent = &def_thread;
ef3d8e53 1706 td = get_new_job(0, td_parent, 0, jobname);
79d16311
JA
1707 }
1708 if (in_global)
c2292325 1709 fio_options_parse(td_parent, (char **) &o[i], 1);
79d16311 1710 else
c2292325 1711 fio_options_parse(td, (char **) &o[i], 1);
79d16311
JA
1712 i++;
1713 }
1714
1715 if (td)
46bcd498 1716 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1717}
1718
01f06b63
JA
1719static int skip_this_section(const char *name)
1720{
ad0a2735
JA
1721 int i;
1722
1723 if (!nr_job_sections)
01f06b63
JA
1724 return 0;
1725 if (!strncmp(name, "global", 6))
1726 return 0;
1727
ad0a2735
JA
1728 for (i = 0; i < nr_job_sections; i++)
1729 if (!strcmp(job_sections[i], name))
1730 return 0;
1731
1732 return 1;
01f06b63
JA
1733}
1734
ebac4655
JA
1735static int is_empty_or_comment(char *line)
1736{
1737 unsigned int i;
1738
1739 for (i = 0; i < strlen(line); i++) {
1740 if (line[i] == ';')
1741 return 1;
5cc2da30
IM
1742 if (line[i] == '#')
1743 return 1;
76cd9378 1744 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
ebac4655
JA
1745 return 0;
1746 }
1747
1748 return 1;
1749}
1750
07261983
JA
1751/*
1752 * This is our [ini] type file parser.
1753 */
b3270921
AK
1754int __parse_jobs_ini(struct thread_data *td,
1755 char *file, int is_buf, int stonewall_flag, int type,
1756 int nested, char *name, char ***popts, int *aopts, int *nopts)
ebac4655 1757{
b3270921
AK
1758 unsigned int global = 0;
1759 char *string;
ebac4655
JA
1760 FILE *f;
1761 char *p;
0c7e37a0 1762 int ret = 0, stonewall;
097b2991 1763 int first_sect = 1;
ccf8f127 1764 int skip_fgets = 0;
01f06b63 1765 int inside_skip = 0;
3b8b7135
JA
1766 char **opts;
1767 int i, alloc_opts, num_opts;
ebac4655 1768
b3270921
AK
1769 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1770 assert(td || !nested);
1771
50d16976
JA
1772 if (is_buf)
1773 f = NULL;
1774 else {
1775 if (!strcmp(file, "-"))
1776 f = stdin;
1777 else
1778 f = fopen(file, "r");
5a729cbe 1779
50d16976 1780 if (!f) {
323255cc
JA
1781 int __err = errno;
1782
1783 log_err("fio: unable to open '%s' job file\n", file);
02957cd4
JA
1784 if (td)
1785 td_verror(td, __err, "job file open");
50d16976
JA
1786 return 1;
1787 }
ebac4655
JA
1788 }
1789
1790 string = malloc(4096);
7f7e6e59
JA
1791
1792 /*
1793 * it's really 256 + small bit, 280 should suffice
1794 */
b3270921
AK
1795 if (!nested) {
1796 name = malloc(280);
1797 memset(name, 0, 280);
1798 }
ebac4655 1799
b3270921
AK
1800 opts = NULL;
1801 if (nested && popts) {
1802 opts = *popts;
1803 alloc_opts = *aopts;
1804 num_opts = *nopts;
1805 }
1806
1807 if (!opts) {
1808 alloc_opts = 8;
1809 opts = malloc(sizeof(char *) * alloc_opts);
1810 num_opts = 0;
1811 }
3b8b7135 1812
0c7e37a0 1813 stonewall = stonewall_flag;
7c124ac1 1814 do {
ccf8f127
JA
1815 /*
1816 * if skip_fgets is set, we already have loaded a line we
1817 * haven't handled.
1818 */
1819 if (!skip_fgets) {
50d16976
JA
1820 if (is_buf)
1821 p = strsep(&file, "\n");
1822 else
43f74792 1823 p = fgets(string, 4096, f);
ccf8f127
JA
1824 if (!p)
1825 break;
1826 }
cdc7f193 1827
ccf8f127 1828 skip_fgets = 0;
cdc7f193 1829 strip_blank_front(&p);
6c7c7da1 1830 strip_blank_end(p);
cdc7f193 1831
b3270921 1832 dprint(FD_PARSE, "%s\n", p);
ebac4655
JA
1833 if (is_empty_or_comment(p))
1834 continue;
b3270921
AK
1835
1836 if (!nested) {
1837 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1838 if (inside_skip)
1839 continue;
1840
1841 log_err("fio: option <%s> outside of "
1842 "[] job section\n", p);
0e9c21a2 1843 ret = 1;
b3270921
AK
1844 break;
1845 }
1846
1847 name[strlen(name) - 1] = '\0';
1848
1849 if (skip_this_section(name)) {
1850 inside_skip = 1;
01f06b63 1851 continue;
b3270921
AK
1852 } else
1853 inside_skip = 0;
ebac4655 1854
b3270921 1855 dprint(FD_PARSE, "Parsing section [%s]\n", name);
7a4b80a1 1856
b3270921 1857 global = !strncmp(name, "global", 6);
01f06b63 1858
b3270921
AK
1859 if (dump_cmdline) {
1860 if (first_sect)
1861 log_info("fio ");
1862 if (!global)
1863 log_info("--name=%s ", name);
1864 first_sect = 0;
1865 }
ebac4655 1866
b3270921
AK
1867 td = get_new_job(global, &def_thread, 0, name);
1868 if (!td) {
1869 ret = 1;
1870 break;
1871 }
cca73aa7 1872
b3270921
AK
1873 /*
1874 * Separate multiple job files by a stonewall
1875 */
1876 if (!global && stonewall) {
1877 td->o.stonewall = stonewall;
1878 stonewall = 0;
1879 }
ebac4655 1880
b3270921
AK
1881 num_opts = 0;
1882 memset(opts, 0, alloc_opts * sizeof(char *));
972cfd25 1883 }
b3270921
AK
1884 else
1885 skip_fgets = 1;
3b8b7135 1886
50d16976 1887 while (1) {
b3270921
AK
1888 if (!skip_fgets) {
1889 if (is_buf)
1890 p = strsep(&file, "\n");
1891 else
1892 p = fgets(string, 4096, f);
1893 if (!p)
1894 break;
1895 dprint(FD_PARSE, "%s", p);
1896 }
50d16976 1897 else
b3270921 1898 skip_fgets = 0;
50d16976 1899
ebac4655
JA
1900 if (is_empty_or_comment(p))
1901 continue;
e1f36503 1902
b6754f9d 1903 strip_blank_front(&p);
7c124ac1 1904
ccf8f127
JA
1905 /*
1906 * new section, break out and make sure we don't
1907 * fgets() a new line at the top.
1908 */
1909 if (p[0] == '[') {
b3270921
AK
1910 if (nested) {
1911 log_err("No new sections in included files\n");
1912 return 1;
1913 }
1914
ccf8f127 1915 skip_fgets = 1;
7c124ac1 1916 break;
ccf8f127 1917 }
7c124ac1 1918
4ae3f763 1919 strip_blank_end(p);
aea47d44 1920
b3270921 1921 if (!strncmp(p, "include", strlen("include"))) {
c0821a08
AK
1922 char *filename = p + strlen("include") + 1,
1923 *ts, *full_fn = NULL;
1924
1925 /*
1926 * Allow for the include filename
1927 * specification to be relative.
1928 */
1929 if (access(filename, F_OK) &&
1930 (ts = strrchr(file, '/'))) {
1931 int len = ts - file +
1932 strlen(filename) + 2;
1933
1934 if (!(full_fn = calloc(1, len))) {
1935 ret = ENOMEM;
1936 break;
1937 }
1938
1939 strncpy(full_fn,
1940 file, (ts - file) + 1);
1941 strncpy(full_fn + (ts - file) + 1,
1942 filename, strlen(filename));
1943 full_fn[len - 1] = 0;
1944 filename = full_fn;
1945 }
1946
1947 ret = __parse_jobs_ini(td, filename, is_buf,
1948 stonewall_flag, type, 1,
1949 name, &opts,
1950 &alloc_opts, &num_opts);
b3270921 1951
c0821a08
AK
1952 if (ret) {
1953 log_err("Error %d while parsing "
1954 "include file %s\n",
b3270921 1955 ret, filename);
b3270921 1956 }
c0821a08
AK
1957
1958 if (full_fn)
1959 free(full_fn);
1960
1961 if (ret)
1962 break;
1963
b3270921
AK
1964 continue;
1965 }
1966
3b8b7135
JA
1967 if (num_opts == alloc_opts) {
1968 alloc_opts <<= 1;
1969 opts = realloc(opts,
1970 alloc_opts * sizeof(char *));
1971 }
1972
1973 opts[num_opts] = strdup(p);
1974 num_opts++;
ebac4655 1975 }
ebac4655 1976
b3270921
AK
1977 if (nested) {
1978 *popts = opts;
1979 *aopts = alloc_opts;
1980 *nopts = num_opts;
1981 goto out;
1982 }
1983
c2292325
JA
1984 ret = fio_options_parse(td, opts, num_opts);
1985 if (!ret) {
1986 if (dump_cmdline)
1987 dump_opt_list(td);
1988
46bcd498 1989 ret = add_job(td, name, 0, 0, type);
c2292325 1990 } else {
b1508cf9
JA
1991 log_err("fio: job %s dropped\n", name);
1992 put_job(td);
45410acb 1993 }
3b8b7135
JA
1994
1995 for (i = 0; i < num_opts; i++)
1996 free(opts[i]);
1997 num_opts = 0;
7c124ac1 1998 } while (!ret);
ebac4655 1999
cca73aa7
JA
2000 if (dump_cmdline)
2001 log_info("\n");
2002
7e356b2d
JA
2003 i = 0;
2004 while (i < nr_job_sections) {
2005 free(job_sections[i]);
2006 i++;
2007 }
2008
2577594d 2009 free(opts);
b3270921
AK
2010out:
2011 free(string);
2012 if (!nested)
2013 free(name);
50d16976 2014 if (!is_buf && f != stdin)
5a729cbe 2015 fclose(f);
45410acb 2016 return ret;
ebac4655
JA
2017}
2018
b3270921
AK
2019int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
2020{
2021 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
2022 0, NULL, NULL, NULL, NULL);
2023}
2024
ebac4655
JA
2025static int fill_def_thread(void)
2026{
2027 memset(&def_thread, 0, sizeof(def_thread));
66e19a38 2028 INIT_FLIST_HEAD(&def_thread.opt_list);
ebac4655 2029
375b2695 2030 fio_getaffinity(getpid(), &def_thread.o.cpumask);
8b28bd41 2031 def_thread.o.error_dump = 1;
25bd16ce 2032
ebac4655 2033 /*
ee738499 2034 * fill default options
ebac4655 2035 */
214e1eca 2036 fio_fill_default_options(&def_thread);
ebac4655
JA
2037 return 0;
2038}
2039
2236df3d
JA
2040static void show_debug_categories(void)
2041{
44acdcf4 2042#ifdef FIO_INC_DEBUG
2236df3d
JA
2043 struct debug_level *dl = &debug_levels[0];
2044 int curlen, first = 1;
2045
2046 curlen = 0;
2047 while (dl->name) {
2048 int has_next = (dl + 1)->name != NULL;
2049
2050 if (first || curlen + strlen(dl->name) >= 80) {
2051 if (!first) {
2052 printf("\n");
2053 curlen = 0;
2054 }
2055 curlen += printf("\t\t\t%s", dl->name);
2056 curlen += 3 * (8 - 1);
2057 if (has_next)
2058 curlen += printf(",");
2059 } else {
2060 curlen += printf("%s", dl->name);
2061 if (has_next)
2062 curlen += printf(",");
2063 }
2064 dl++;
2065 first = 0;
2066 }
2067 printf("\n");
44acdcf4 2068#endif
2236df3d
JA
2069}
2070
45378b35 2071static void usage(const char *name)
4785f995 2072{
3d43382c 2073 printf("%s\n", fio_version_string);
45378b35 2074 printf("%s [options] [job options] <job file(s)>\n", name);
2236df3d
JA
2075 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
2076 show_debug_categories();
111e032d 2077 printf(" --parse-only\t\tParse options only, don't start any IO\n");
83881283 2078 printf(" --output\t\tWrite output to file\n");
b2cecdc2 2079 printf(" --runtime\t\tRuntime in seconds\n");
83881283
JA
2080 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
2081 printf(" --minimal\t\tMinimal (terse) output\n");
513e37ee 2082 printf(" --output-format=x\tOutput format (terse,json,json+,normal)\n");
75db6e2c 2083 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
f3afa57e 2084 printf(" --version\t\tPrint version info and exit\n");
83881283 2085 printf(" --help\t\tPrint this page\n");
23893646 2086 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
fec0f21c 2087 printf(" --crctest\t\tTest speed of checksum functions\n");
83881283 2088 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
5ec10eaa 2089 " them\n");
de890a1e
SL
2090 printf(" --enghelp=engine\tPrint ioengine help, or list"
2091 " available ioengines\n");
2092 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
2093 " cmd\n");
83881283
JA
2094 printf(" --showcmd\t\tTurn a job file into command line options\n");
2095 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
2096 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
e382e661
JA
2097 printf(" --eta-newline=time\tForce a new line for every 'time'");
2098 printf(" period passed\n");
06464907
JA
2099 printf(" --status-interval=t\tForce full status dump every");
2100 printf(" 't' period passed\n");
83881283 2101 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
5ec10eaa 2102 " writes\n");
83881283
JA
2103 printf(" --section=name\tOnly run specified section in job file\n");
2104 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2b386d25 2105 " (def 1024)\n");
83881283
JA
2106 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
2107 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2108 printf(" --server=args\t\tStart a backend fio server\n");
2109 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
2110 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
323255cc 2111 printf(" --remote-config=file\tTell fio server to load this local job file\n");
f2a2ce0e
HL
2112 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2113 "\t\t\t(option=system,percpu) or run unit work\n"
2114 "\t\t\tcalibration only (option=calibrate)\n");
b26317c9
JA
2115#ifdef CONFIG_ZLIB
2116 printf(" --inflate-log=log\tInflate and output compressed log\n");
2117#endif
b63efd30 2118 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
ca09be4b 2119 printf(" --trigger-timeout=t\tExecute trigger af this time\n");
b63efd30
JA
2120 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
2121 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
d264264a 2122 printf(" --aux-path=path\tUse this path for fio state generated files\n");
aa58d252 2123 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
bfca2c74
JA
2124 printf("\n Jens Axboe <jaxboe@fusionio.com>");
2125 printf("\n Jens Axboe <axboe@fb.com>\n");
4785f995
JA
2126}
2127
79e48f72 2128#ifdef FIO_INC_DEBUG
ee56ad50 2129struct debug_level debug_levels[] = {
0b8d11ed
JA
2130 { .name = "process",
2131 .help = "Process creation/exit logging",
2132 .shift = FD_PROCESS,
2133 },
2134 { .name = "file",
2135 .help = "File related action logging",
2136 .shift = FD_FILE,
2137 },
2138 { .name = "io",
2139 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2140 .shift = FD_IO,
2141 },
2142 { .name = "mem",
2143 .help = "Memory allocation/freeing logging",
2144 .shift = FD_MEM,
2145 },
2146 { .name = "blktrace",
2147 .help = "blktrace action logging",
2148 .shift = FD_BLKTRACE,
2149 },
2150 { .name = "verify",
2151 .help = "IO verification action logging",
2152 .shift = FD_VERIFY,
2153 },
2154 { .name = "random",
2155 .help = "Random generation logging",
2156 .shift = FD_RANDOM,
2157 },
2158 { .name = "parse",
2159 .help = "Parser logging",
2160 .shift = FD_PARSE,
2161 },
2162 { .name = "diskutil",
2163 .help = "Disk utility logging actions",
2164 .shift = FD_DISKUTIL,
2165 },
2166 { .name = "job",
2167 .help = "Logging related to creating/destroying jobs",
2168 .shift = FD_JOB,
2169 },
2170 { .name = "mutex",
2171 .help = "Mutex logging",
2172 .shift = FD_MUTEX
2173 },
2174 { .name = "profile",
2175 .help = "Logging related to profiles",
2176 .shift = FD_PROFILE,
2177 },
2178 { .name = "time",
2179 .help = "Logging related to time keeping functions",
2180 .shift = FD_TIME,
2181 },
2182 { .name = "net",
2183 .help = "Network logging",
2184 .shift = FD_NET,
2185 },
3e260a46
JA
2186 { .name = "rate",
2187 .help = "Rate logging",
2188 .shift = FD_RATE,
2189 },
0c56718d
JA
2190 { .name = "compress",
2191 .help = "Log compression logging",
2192 .shift = FD_COMPRESS,
2193 },
16e56d25
VF
2194 { .name = "steadystate",
2195 .help = "Steady state detection logging",
2196 .shift = FD_STEADYSTATE,
2197 },
02444ad1 2198 { .name = NULL, },
ee56ad50
JA
2199};
2200
c09823ab 2201static int set_debug(const char *string)
ee56ad50
JA
2202{
2203 struct debug_level *dl;
2204 char *p = (char *) string;
2205 char *opt;
2206 int i;
2207
2706fa00
JA
2208 if (!string)
2209 return 0;
2210
ee56ad50 2211 if (!strcmp(string, "?") || !strcmp(string, "help")) {
ee56ad50
JA
2212 log_info("fio: dumping debug options:");
2213 for (i = 0; debug_levels[i].name; i++) {
2214 dl = &debug_levels[i];
2215 log_info("%s,", dl->name);
2216 }
bd6f78b2 2217 log_info("all\n");
c09823ab 2218 return 1;
ee56ad50
JA
2219 }
2220
2221 while ((opt = strsep(&p, ",")) != NULL) {
2222 int found = 0;
2223
5e1d306e
JA
2224 if (!strncmp(opt, "all", 3)) {
2225 log_info("fio: set all debug options\n");
2226 fio_debug = ~0UL;
2227 continue;
2228 }
2229
ee56ad50
JA
2230 for (i = 0; debug_levels[i].name; i++) {
2231 dl = &debug_levels[i];
5e1d306e
JA
2232 found = !strncmp(opt, dl->name, strlen(dl->name));
2233 if (!found)
2234 continue;
2235
2236 if (dl->shift == FD_JOB) {
2237 opt = strchr(opt, ':');
2238 if (!opt) {
2239 log_err("fio: missing job number\n");
2240 break;
2241 }
2242 opt++;
2243 fio_debug_jobno = atoi(opt);
2244 log_info("fio: set debug jobno %d\n",
2245 fio_debug_jobno);
2246 } else {
ee56ad50 2247 log_info("fio: set debug option %s\n", opt);
bd6f78b2 2248 fio_debug |= (1UL << dl->shift);
ee56ad50 2249 }
5e1d306e 2250 break;
ee56ad50
JA
2251 }
2252
2253 if (!found)
2254 log_err("fio: debug mask %s not found\n", opt);
2255 }
c09823ab 2256 return 0;
ee56ad50 2257}
79e48f72 2258#else
69b98d4c 2259static int set_debug(const char *string)
79e48f72
JA
2260{
2261 log_err("fio: debug tracing not included in build\n");
c09823ab 2262 return 1;
79e48f72
JA
2263}
2264#endif
9ac8a797 2265
4c6107ff
JA
2266static void fio_options_fill_optstring(void)
2267{
2268 char *ostr = cmd_optstr;
2269 int i, c;
2270
2271 c = i = 0;
2272 while (l_opts[i].name) {
2273 ostr[c++] = l_opts[i].val;
2274 if (l_opts[i].has_arg == required_argument)
2275 ostr[c++] = ':';
2276 else if (l_opts[i].has_arg == optional_argument) {
2277 ostr[c++] = ':';
2278 ostr[c++] = ':';
2279 }
2280 i++;
2281 }
2282 ostr[c] = '\0';
2283}
2284
c2c94585
JA
2285static int client_flag_set(char c)
2286{
2287 int i;
2288
2289 i = 0;
2290 while (l_opts[i].name) {
2291 int val = l_opts[i].val;
2292
2293 if (c == (val & 0xff))
2294 return (val & FIO_CLIENT_FLAG);
2295
2296 i++;
2297 }
2298
2299 return 0;
2300}
2301
10aa136b 2302static void parse_cmd_client(void *client, char *opt)
7a4b8240 2303{
fa2ea806 2304 fio_client_add_cmd_option(client, opt);
7a4b8240
JA
2305}
2306
a893c261
JA
2307static void show_closest_option(const char *name)
2308{
2309 int best_option, best_distance;
2310 int i, distance;
2311
2312 while (*name == '-')
2313 name++;
2314
2315 best_option = -1;
2316 best_distance = INT_MAX;
2317 i = 0;
2318 while (l_opts[i].name) {
2319 distance = string_distance(name, l_opts[i].name);
2320 if (distance < best_distance) {
2321 best_distance = distance;
2322 best_option = i;
2323 }
2324 i++;
2325 }
2326
3701636d 2327 if (best_option != -1 && string_distance_ok(name, best_distance))
a893c261
JA
2328 log_err("Did you mean %s?\n", l_opts[best_option].name);
2329}
2330
a666cab8
JA
2331static int parse_output_format(const char *optarg)
2332{
2333 char *p, *orig, *opt;
2334 int ret = 0;
2335
2336 p = orig = strdup(optarg);
2337
2338 output_format = 0;
2339
2340 while ((opt = strsep(&p, ",")) != NULL) {
2341 if (!strcmp(opt, "minimal") ||
2342 !strcmp(opt, "terse") ||
2343 !strcmp(opt, "csv"))
2344 output_format |= FIO_OUTPUT_TERSE;
2345 else if (!strcmp(opt, "json"))
2346 output_format |= FIO_OUTPUT_JSON;
513e37ee
VF
2347 else if (!strcmp(opt, "json+"))
2348 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
a666cab8
JA
2349 else if (!strcmp(opt, "normal"))
2350 output_format |= FIO_OUTPUT_NORMAL;
2351 else {
2352 log_err("fio: invalid output format %s\n", opt);
2353 ret = 1;
2354 break;
2355 }
2356 }
2357
2358 free(orig);
2359 return ret;
2360}
2361
46bcd498 2362int parse_cmd_line(int argc, char *argv[], int client_type)
ebac4655 2363{
b4692828 2364 struct thread_data *td = NULL;
c09823ab 2365 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
4c6107ff 2366 char *ostr = cmd_optstr;
977b9596 2367 char *pid_file = NULL;
9bae26e8 2368 void *cur_client = NULL;
81179eec 2369 int backend = 0;
ebac4655 2370
c2c94585
JA
2371 /*
2372 * Reset optind handling, since we may call this multiple times
2373 * for the backend.
2374 */
2375 optind = 1;
7a4b8240 2376
c2c94585
JA
2377 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2378 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
fa2ea806 2379 parse_cmd_client(cur_client, argv[optind - 1]);
7a4b8240
JA
2380 c &= ~FIO_CLIENT_FLAG;
2381 }
2382
ebac4655 2383 switch (c) {
2b386d25
JA
2384 case 'a':
2385 smalloc_pool_size = atoi(optarg);
2386 break;
b4692828 2387 case 't':
25bd16ce
JA
2388 if (check_str_time(optarg, &def_timeout, 1)) {
2389 log_err("fio: failed parsing time %s\n", optarg);
2390 do_exit++;
2391 exit_val = 1;
2392 }
b4692828
JA
2393 break;
2394 case 'l':
cb7e0ace
JA
2395 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2396 do_exit++;
2397 exit_val = 1;
b4692828 2398 break;
3d73e5a9 2399 case 'b':
b4692828
JA
2400 write_bw_log = 1;
2401 break;
2402 case 'o':
988da120 2403 if (f_out && f_out != stdout)
5e1d8745
JA
2404 fclose(f_out);
2405
b4692828
JA
2406 f_out = fopen(optarg, "w+");
2407 if (!f_out) {
2408 perror("fopen output");
2409 exit(1);
2410 }
2411 f_err = f_out;
2412 break;
2413 case 'm':
f3afa57e 2414 output_format = FIO_OUTPUT_TERSE;
b4692828 2415 break;
f3afa57e 2416 case 'F':
a666cab8
JA
2417 if (parse_output_format(optarg)) {
2418 log_err("fio: failed parsing output-format\n");
2419 exit_val = 1;
2420 do_exit++;
2421 break;
2422 }
2b8c71b0 2423 break;
f6a7df53
JA
2424 case 'f':
2425 output_format |= FIO_OUTPUT_TERSE;
2426 break;
b4692828 2427 case 'h':
7d8ea970 2428 did_arg = 1;
7874f8b7 2429 if (!cur_client) {
c2c94585 2430 usage(argv[0]);
7874f8b7
JA
2431 do_exit++;
2432 }
c2c94585 2433 break;
fd28ca49 2434 case 'c':
7d8ea970 2435 did_arg = 1;
7874f8b7 2436 if (!cur_client) {
c2c94585 2437 fio_show_option_help(optarg);
7874f8b7
JA
2438 do_exit++;
2439 }
c2c94585 2440 break;
de890a1e 2441 case 'i':
7d8ea970 2442 did_arg = 1;
de890a1e
SL
2443 if (!cur_client) {
2444 fio_show_ioengine_help(optarg);
2445 do_exit++;
2446 }
2447 break;
cca73aa7 2448 case 's':
7d8ea970 2449 did_arg = 1;
cca73aa7
JA
2450 dump_cmdline = 1;
2451 break;
724e4435
JA
2452 case 'r':
2453 read_only = 1;
2454 break;
b4692828 2455 case 'v':
7d8ea970 2456 did_arg = 1;
7874f8b7 2457 if (!cur_client) {
3d43382c 2458 log_info("%s\n", fio_version_string);
7874f8b7
JA
2459 do_exit++;
2460 }
c2c94585 2461 break;
f57a9c59
JA
2462 case 'V':
2463 terse_version = atoi(optarg);
09786f5f
JA
2464 if (!(terse_version == 2 || terse_version == 3 ||
2465 terse_version == 4)) {
f57a9c59
JA
2466 log_err("fio: bad terse version format\n");
2467 exit_val = 1;
2468 do_exit++;
2469 }
2470 break;
e592a06b
AC
2471 case 'e':
2472 if (!strcmp("always", optarg))
2473 eta_print = FIO_ETA_ALWAYS;
2474 else if (!strcmp("never", optarg))
2475 eta_print = FIO_ETA_NEVER;
2476 break;
e382e661
JA
2477 case 'E': {
2478 long long t = 0;
2479
88038bc7 2480 if (check_str_time(optarg, &t, 1)) {
e382e661
JA
2481 log_err("fio: failed parsing eta time %s\n", optarg);
2482 exit_val = 1;
2483 do_exit++;
2484 }
88038bc7 2485 eta_new_line = t / 1000;
e382e661
JA
2486 break;
2487 }
ee56ad50 2488 case 'd':
c09823ab
JA
2489 if (set_debug(optarg))
2490 do_exit++;
ee56ad50 2491 break;
111e032d 2492 case 'P':
7d8ea970 2493 did_arg = 1;
111e032d
JA
2494 parse_only = 1;
2495 break;
ad0a2735
JA
2496 case 'x': {
2497 size_t new_size;
2498
01f06b63 2499 if (!strcmp(optarg, "global")) {
5ec10eaa
JA
2500 log_err("fio: can't use global as only "
2501 "section\n");
c09823ab
JA
2502 do_exit++;
2503 exit_val = 1;
01f06b63
JA
2504 break;
2505 }
ad0a2735
JA
2506 new_size = (nr_job_sections + 1) * sizeof(char *);
2507 job_sections = realloc(job_sections, new_size);
2508 job_sections[nr_job_sections] = strdup(optarg);
2509 nr_job_sections++;
01f06b63 2510 break;
ad0a2735 2511 }
b26317c9
JA
2512#ifdef CONFIG_ZLIB
2513 case 'X':
2514 exit_val = iolog_file_inflate(optarg);
2515 did_arg++;
2516 do_exit++;
2517 break;
2518#endif
9ac8a797 2519 case 'p':
7d8ea970 2520 did_arg = 1;
4af7c007
JA
2521 if (exec_profile)
2522 free(exec_profile);
07b3232d 2523 exec_profile = strdup(optarg);
9ac8a797 2524 break;
b4692828 2525 case FIO_GETOPT_JOB: {
5ec10eaa 2526 const char *opt = l_opts[lidx].name;
b4692828
JA
2527 char *val = optarg;
2528
c2b1e753 2529 if (!strncmp(opt, "name", 4) && td) {
46bcd498 2530 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
66251554 2531 if (ret)
4a4ac4e3 2532 goto out_free;
c2b1e753 2533 td = NULL;
7d8ea970 2534 did_arg = 1;
c2b1e753 2535 }
b4692828 2536 if (!td) {
01f06b63 2537 int is_section = !strncmp(opt, "name", 4);
3106f220
JA
2538 int global = 0;
2539
01f06b63 2540 if (!is_section || !strncmp(val, "global", 6))
3106f220 2541 global = 1;
c2b1e753 2542
01f06b63
JA
2543 if (is_section && skip_this_section(val))
2544 continue;
2545
ef3d8e53 2546 td = get_new_job(global, &def_thread, 1, NULL);
f6ff24cb
JA
2547 if (!td || ioengine_load(td)) {
2548 if (td) {
2549 put_job(td);
2550 td = NULL;
2551 }
2552 do_exit++;
205a719e 2553 exit_val = 1;
f6ff24cb
JA
2554 break;
2555 }
de890a1e 2556 fio_options_set_ioengine_opts(l_opts, td);
b4692828 2557 }
38d0adb0 2558
9d918187
JA
2559 if ((!val || !strlen(val)) &&
2560 l_opts[lidx].has_arg == required_argument) {
2561 log_err("fio: option %s requires an argument\n", opt);
2562 ret = 1;
2563 } else
2564 ret = fio_cmd_option_parse(td, opt, val);
2565
bfb3ea22
JA
2566 if (ret) {
2567 if (td) {
2568 put_job(td);
2569 td = NULL;
2570 }
a88c8c14 2571 do_exit++;
205a719e 2572 exit_val = 1;
bfb3ea22 2573 }
de890a1e
SL
2574
2575 if (!ret && !strcmp(opt, "ioengine")) {
2576 free_ioengine(td);
f6ff24cb 2577 if (ioengine_load(td)) {
342f570e
JA
2578 put_job(td);
2579 td = NULL;
f6ff24cb 2580 do_exit++;
205a719e 2581 exit_val = 1;
f6ff24cb
JA
2582 break;
2583 }
de890a1e
SL
2584 fio_options_set_ioengine_opts(l_opts, td);
2585 }
2586 break;
2587 }
2588 case FIO_GETOPT_IOENGINE: {
2589 const char *opt = l_opts[lidx].name;
2590 char *val = optarg;
b1ed74e0
JA
2591
2592 if (!td)
2593 break;
2594
de890a1e 2595 ret = fio_cmd_ioengine_option_parse(td, opt, val);
b4692828
JA
2596 break;
2597 }
a9523c6f
JA
2598 case 'w':
2599 warnings_fatal = 1;
2600 break;
fca70358
JA
2601 case 'j':
2602 max_jobs = atoi(optarg);
2603 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2604 log_err("fio: invalid max jobs: %d\n", max_jobs);
2605 do_exit++;
2606 exit_val = 1;
2607 }
2608 break;
50d16976 2609 case 'S':
7d8ea970 2610 did_arg = 1;
c8931876 2611#ifndef CONFIG_NO_SHM
a37f69b7 2612 if (nr_clients) {
132159a5
JA
2613 log_err("fio: can't be both client and server\n");
2614 do_exit++;
2615 exit_val = 1;
2616 break;
2617 }
87aa8f19 2618 if (optarg)
bebe6398 2619 fio_server_set_arg(optarg);
50d16976 2620 is_backend = 1;
81179eec 2621 backend = 1;
c8931876
JA
2622#else
2623 log_err("fio: client/server requires SHM support\n");
2624 do_exit++;
2625 exit_val = 1;
2626#endif
50d16976 2627 break;
e46d8091 2628 case 'D':
6cfe9a8c
JA
2629 if (pid_file)
2630 free(pid_file);
402668f3 2631 pid_file = strdup(optarg);
e46d8091 2632 break;
f2a2ce0e
HL
2633 case 'I':
2634 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2635 /* exit on error and calibration only */
7d8ea970 2636 did_arg = 1;
f2a2ce0e 2637 do_exit++;
7d8ea970 2638 if (ret == -1)
f2a2ce0e
HL
2639 exit_val = 1;
2640 }
2641 break;
132159a5 2642 case 'C':
7d8ea970 2643 did_arg = 1;
132159a5
JA
2644 if (is_backend) {
2645 log_err("fio: can't be both client and server\n");
2646 do_exit++;
2647 exit_val = 1;
2648 break;
2649 }
08633c32
BE
2650 /* if --client parameter contains a pathname */
2651 if (0 == access(optarg, R_OK)) {
2652 /* file contains a list of host addrs or names */
2972708f 2653 char hostaddr[PATH_MAX] = {0};
08633c32
BE
2654 char formatstr[8];
2655 FILE * hostf = fopen(optarg, "r");
2656 if (!hostf) {
2657 log_err("fio: could not open client list file %s for read\n", optarg);
2658 do_exit++;
2659 exit_val = 1;
2660 break;
2661 }
2972708f
JA
2662 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2663 /*
2664 * read at most PATH_MAX-1 chars from each
2665 * record in this file
2666 */
08633c32
BE
2667 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2668 /* expect EVERY host in file to be valid */
2669 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2670 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2671 do_exit++;
2672 exit_val = 1;
2673 break;
2674 }
2675 }
2676 fclose(hostf);
2677 break; /* no possibility of job file for "this client only" */
2678 }
a5276616 2679 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
bebe6398
JA
2680 log_err("fio: failed adding client %s\n", optarg);
2681 do_exit++;
2682 exit_val = 1;
2683 break;
2684 }
14ea90ed
JA
2685 /*
2686 * If the next argument exists and isn't an option,
2687 * assume it's a job file for this client only.
2688 */
2689 while (optind < argc) {
2690 if (!strncmp(argv[optind], "--", 2) ||
2691 !strncmp(argv[optind], "-", 1))
2692 break;
2693
bb781643 2694 if (fio_client_add_ini_file(cur_client, argv[optind], false))
323255cc 2695 break;
14ea90ed
JA
2696 optind++;
2697 }
132159a5 2698 break;
323255cc
JA
2699 case 'R':
2700 did_arg = 1;
bb781643 2701 if (fio_client_add_ini_file(cur_client, optarg, true)) {
323255cc
JA
2702 do_exit++;
2703 exit_val = 1;
2704 }
2705 break;
7d11f871 2706 case 'T':
7d8ea970 2707 did_arg = 1;
7d11f871 2708 do_exit++;
aad918e4 2709 exit_val = fio_monotonic_clocktest(1);
7d11f871 2710 break;
fec0f21c 2711 case 'G':
7d8ea970 2712 did_arg = 1;
fec0f21c
JA
2713 do_exit++;
2714 exit_val = fio_crctest(optarg);
2715 break;
06464907
JA
2716 case 'L': {
2717 long long val;
2718
88038bc7 2719 if (check_str_time(optarg, &val, 1)) {
06464907
JA
2720 log_err("fio: failed parsing time %s\n", optarg);
2721 do_exit++;
2722 exit_val = 1;
2723 break;
2724 }
88038bc7 2725 status_interval = val / 1000;
06464907
JA
2726 break;
2727 }
b63efd30 2728 case 'W':
f9cbae9e
JA
2729 if (trigger_file)
2730 free(trigger_file);
b63efd30
JA
2731 trigger_file = strdup(optarg);
2732 break;
2733 case 'H':
f9cbae9e
JA
2734 if (trigger_cmd)
2735 free(trigger_cmd);
b63efd30
JA
2736 trigger_cmd = strdup(optarg);
2737 break;
2738 case 'J':
f9cbae9e
JA
2739 if (trigger_remote_cmd)
2740 free(trigger_remote_cmd);
b63efd30 2741 trigger_remote_cmd = strdup(optarg);
ca09be4b 2742 break;
d264264a
JA
2743 case 'K':
2744 if (aux_path)
2745 free(aux_path);
2746 aux_path = strdup(optarg);
2747 break;
ca09be4b
JA
2748 case 'B':
2749 if (check_str_time(optarg, &trigger_timeout, 1)) {
2750 log_err("fio: failed parsing time %s\n", optarg);
2751 do_exit++;
2752 exit_val = 1;
2753 }
2754 trigger_timeout /= 1000000;
2755 break;
798827c8
JA
2756 case '?':
2757 log_err("%s: unrecognized option '%s'\n", argv[0],
2758 argv[optind - 1]);
a893c261 2759 show_closest_option(argv[optind - 1]);
b4692828 2760 default:
c09823ab
JA
2761 do_exit++;
2762 exit_val = 1;
b4692828 2763 break;
ebac4655 2764 }
c7d5c941
JA
2765 if (do_exit)
2766 break;
ebac4655 2767 }
c9fad893 2768
0b14f0a8
JA
2769 if (do_exit && !(is_backend || nr_clients))
2770 exit(exit_val);
536582bf 2771
fb296043
JA
2772 if (nr_clients && fio_clients_connect())
2773 exit(1);
132159a5 2774
81179eec 2775 if (is_backend && backend)
402668f3 2776 return fio_start_server(pid_file);
b8ba87ac
JA
2777 else if (pid_file)
2778 free(pid_file);
50d16976 2779
b4692828 2780 if (td) {
7d8ea970 2781 if (!ret) {
46bcd498 2782 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
7d8ea970
JA
2783 if (ret)
2784 did_arg = 1;
2785 }
972cfd25 2786 }
774a6177 2787
7874f8b7 2788 while (!ret && optind < argc) {
b4692828
JA
2789 ini_idx++;
2790 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2791 ini_file[ini_idx - 1] = strdup(argv[optind]);
2792 optind++;
eb8bbf48 2793 }
972cfd25 2794
4a4ac4e3
JA
2795out_free:
2796 if (pid_file)
2797 free(pid_file);
2798
972cfd25 2799 return ini_idx;
ebac4655
JA
2800}
2801
0420ba6a 2802int fio_init_options(void)
ebac4655 2803{
b4692828
JA
2804 f_out = stdout;
2805 f_err = stderr;
2806
4c6107ff 2807 fio_options_fill_optstring();
5ec10eaa 2808 fio_options_dup_and_init(l_opts);
b4692828 2809
9d9eb2e7
JA
2810 atexit(free_shm);
2811
ebac4655
JA
2812 if (fill_def_thread())
2813 return 1;
2814
0420ba6a
JA
2815 return 0;
2816}
2817
51167799
JA
2818extern int fio_check_options(struct thread_options *);
2819
0420ba6a
JA
2820int parse_options(int argc, char *argv[])
2821{
46bcd498 2822 const int type = FIO_CLIENT_TYPE_CLI;
0420ba6a
JA
2823 int job_files, i;
2824
2825 if (fio_init_options())
2826 return 1;
51167799
JA
2827 if (fio_test_cconv(&def_thread.o))
2828 log_err("fio: failed internal cconv test\n");
0420ba6a 2829
46bcd498 2830 job_files = parse_cmd_line(argc, argv, type);
ebac4655 2831
cdf54d85
JA
2832 if (job_files > 0) {
2833 for (i = 0; i < job_files; i++) {
bc4f5ef6 2834 if (i && fill_def_thread())
132159a5 2835 return 1;
cdf54d85
JA
2836 if (nr_clients) {
2837 if (fio_clients_send_ini(ini_file[i]))
2838 return 1;
2839 free(ini_file[i]);
2840 } else if (!is_backend) {
46bcd498 2841 if (parse_jobs_ini(ini_file[i], 0, i, type))
cdf54d85
JA
2842 return 1;
2843 free(ini_file[i]);
2844 }
132159a5 2845 }
14ea90ed
JA
2846 } else if (nr_clients) {
2847 if (fill_def_thread())
2848 return 1;
2849 if (fio_clients_send_ini(NULL))
2850 return 1;
972cfd25 2851 }
ebac4655 2852
88c6ed80 2853 free(ini_file);
7e356b2d 2854 fio_options_free(&def_thread);
bcbfeefa 2855 filesetup_mem_free();
b4692828
JA
2856
2857 if (!thread_number) {
f9633d72 2858 if (parse_dryrun())
cca73aa7 2859 return 0;
07b3232d
JA
2860 if (exec_profile)
2861 return 0;
a37f69b7 2862 if (is_backend || nr_clients)
5c341e9a 2863 return 0;
085399db
JA
2864 if (did_arg)
2865 return 0;
cca73aa7 2866
d65e11c6 2867 log_err("No jobs(s) defined\n\n");
085399db
JA
2868
2869 if (!did_arg) {
2870 usage(argv[0]);
2871 return 1;
2872 }
2873
2874 return 0;
b4692828
JA
2875 }
2876
129fb2d4 2877 if (output_format & FIO_OUTPUT_NORMAL)
3d43382c 2878 log_info("%s\n", fio_version_string);
f6dea4d3 2879
ebac4655
JA
2880 return 0;
2881}
588b7f09
JA
2882
2883void options_default_fill(struct thread_options *o)
2884{
2885 memcpy(o, &def_thread.o, sizeof(*o));
2886}
c2292325 2887
66e19a38 2888struct thread_data *get_global_options(void)
c2292325 2889{
66e19a38 2890 return &def_thread;
c2292325 2891}