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