Fio 1.59
[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>
12#include <sys/shm.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15
16#include "fio.h"
cb2c86fd 17#include "parse.h"
2e5cdb11 18#include "smalloc.h"
380065aa 19#include "filehash.h"
4f5af7b2 20#include "verify.h"
79d16311 21#include "profile.h"
ebac4655 22
bf2e821a
CC
23#include "lib/getopt.h"
24
d386ed1d 25static char fio_version_string[] = "fio 1.59";
214e1eca 26
ee738499 27#define FIO_RANDSEED (0xb1899bedUL)
ebac4655 28
214e1eca 29static char **ini_file;
fca70358 30static int max_jobs = FIO_MAX_JOBS;
cca73aa7 31static int dump_cmdline;
8c029376 32static int def_timeout;
e1f36503 33
be4ecfdf 34static struct thread_data def_thread;
214e1eca 35struct thread_data *threads = NULL;
e1f36503 36
214e1eca
JA
37int exitall_on_terminate = 0;
38int terse_output = 0;
e592a06b 39int eta_print;
214e1eca
JA
40unsigned long long mlock_size = 0;
41FILE *f_out = NULL;
42FILE *f_err = NULL;
ad0a2735
JA
43char **job_sections = NULL;
44int nr_job_sections = 0;
07b3232d 45char *exec_profile = NULL;
a9523c6f 46int warnings_fatal = 0;
f57a9c59 47int terse_version = 2;
ee738499 48
214e1eca 49int write_bw_log = 0;
4241ea8f 50int read_only = 0;
214e1eca 51
5ec10eaa 52static int write_lat_log;
e1f36503 53
214e1eca 54static int prev_group_jobs;
b4692828 55
ee56ad50 56unsigned long fio_debug = 0;
5e1d306e
JA
57unsigned int fio_debug_jobno = -1;
58unsigned int *fio_debug_jobp = NULL;
ee56ad50 59
4c6107ff
JA
60static char cmd_optstr[256];
61
b4692828
JA
62/*
63 * Command line options. These will contain the above, plus a few
64 * extra that only pertain to fio itself and not jobs.
65 */
5ec10eaa 66static struct option l_opts[FIO_NR_OPTIONS] = {
b4692828 67 {
08d2a19c 68 .name = (char *) "output",
b4692828
JA
69 .has_arg = required_argument,
70 .val = 'o',
71 },
72 {
08d2a19c 73 .name = (char *) "timeout",
b4692828
JA
74 .has_arg = required_argument,
75 .val = 't',
76 },
77 {
08d2a19c 78 .name = (char *) "latency-log",
b4692828
JA
79 .has_arg = required_argument,
80 .val = 'l',
81 },
82 {
08d2a19c 83 .name = (char *) "bandwidth-log",
b4692828
JA
84 .has_arg = required_argument,
85 .val = 'b',
86 },
87 {
08d2a19c 88 .name = (char *) "minimal",
b4692828
JA
89 .has_arg = optional_argument,
90 .val = 'm',
91 },
92 {
08d2a19c 93 .name = (char *) "version",
b4692828
JA
94 .has_arg = no_argument,
95 .val = 'v',
96 },
fd28ca49 97 {
08d2a19c 98 .name = (char *) "help",
fd28ca49
JA
99 .has_arg = no_argument,
100 .val = 'h',
101 },
102 {
08d2a19c 103 .name = (char *) "cmdhelp",
320beefe 104 .has_arg = optional_argument,
fd28ca49
JA
105 .val = 'c',
106 },
cca73aa7 107 {
08d2a19c 108 .name = (char *) "showcmd",
cca73aa7 109 .has_arg = no_argument,
724e4435
JA
110 .val = 's',
111 },
112 {
08d2a19c 113 .name = (char *) "readonly",
724e4435
JA
114 .has_arg = no_argument,
115 .val = 'r',
cca73aa7 116 },
e592a06b 117 {
08d2a19c 118 .name = (char *) "eta",
e592a06b
AC
119 .has_arg = required_argument,
120 .val = 'e',
121 },
ee56ad50 122 {
08d2a19c 123 .name = (char *) "debug",
ee56ad50
JA
124 .has_arg = required_argument,
125 .val = 'd',
126 },
01f06b63 127 {
08d2a19c 128 .name = (char *) "section",
01f06b63
JA
129 .has_arg = required_argument,
130 .val = 'x',
131 },
2b386d25 132 {
08d2a19c 133 .name = (char *) "alloc-size",
2b386d25
JA
134 .has_arg = required_argument,
135 .val = 'a',
136 },
9ac8a797 137 {
08d2a19c 138 .name = (char *) "profile",
9ac8a797
JA
139 .has_arg = required_argument,
140 .val = 'p',
141 },
a9523c6f 142 {
08d2a19c 143 .name = (char *) "warnings-fatal",
a9523c6f
JA
144 .has_arg = no_argument,
145 .val = 'w',
146 },
fca70358
JA
147 {
148 .name = (char *) "max-jobs",
149 .has_arg = required_argument,
150 .val = 'j',
151 },
f57a9c59
JA
152 {
153 .name = (char *) "terse-version",
154 .has_arg = required_argument,
155 .val = 'V',
156 },
b4692828
JA
157 {
158 .name = NULL,
159 },
160};
161
9728ce37
JB
162FILE *get_f_out()
163{
164 return f_out;
165}
166
167FILE *get_f_err()
168{
169 return f_err;
170}
171
906c8d75
JA
172/*
173 * Return a free job structure.
174 */
ebac4655
JA
175static struct thread_data *get_new_job(int global, struct thread_data *parent)
176{
177 struct thread_data *td;
178
179 if (global)
180 return &def_thread;
e61f1ec8
BC
181 if (thread_number >= max_jobs) {
182 log_err("error: maximum number of jobs (%d) reached.\n",
183 max_jobs);
ebac4655 184 return NULL;
e61f1ec8 185 }
ebac4655
JA
186
187 td = &threads[thread_number++];
ddaeaa5a 188 *td = *parent;
ebac4655 189
e0b0d892
JA
190 td->o.uid = td->o.gid = -1U;
191
cade3ef4 192 dup_files(td, parent);
d23bb327 193 options_mem_dupe(td);
cade3ef4 194
15dc1934
JA
195 profile_add_hooks(td);
196
ebac4655 197 td->thread_number = thread_number;
ebac4655
JA
198 return td;
199}
200
201static void put_job(struct thread_data *td)
202{
549577a7
JA
203 if (td == &def_thread)
204 return;
84dd1886 205
58c55ba0 206 profile_td_exit(td);
549577a7 207
16edf25d 208 if (td->error)
6d86144d 209 log_info("fio: %s\n", td->verror);
16edf25d 210
ebac4655
JA
211 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
212 thread_number--;
213}
214
581e7141 215static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
127f6865 216{
581e7141 217 unsigned int bs = td->o.min_bs[ddir];
ba3e4e0c 218 unsigned long long bytes_per_sec;
127f6865 219
ff58fced
JA
220 assert(ddir_rw(ddir));
221
ba3e4e0c
RR
222 if (td->o.rate[ddir])
223 bytes_per_sec = td->o.rate[ddir];
224 else
225 bytes_per_sec = td->o.rate_iops[ddir] * bs;
127f6865 226
ba3e4e0c 227 if (!bytes_per_sec) {
127f6865
JA
228 log_err("rate lower than supported\n");
229 return -1;
230 }
231
ba3e4e0c 232 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
581e7141 233 td->rate_pending_usleep[ddir] = 0;
127f6865
JA
234 return 0;
235}
236
581e7141
JA
237static int setup_rate(struct thread_data *td)
238{
239 int ret = 0;
240
241 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
242 ret = __setup_rate(td, DDIR_READ);
243 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
244 ret |= __setup_rate(td, DDIR_WRITE);
245
246 return ret;
247}
248
8347239a
JA
249static int fixed_block_size(struct thread_options *o)
250{
251 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
252 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
253 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
254}
255
dad915e3
JA
256/*
257 * Lazy way of fixing up options that depend on each other. We could also
258 * define option callback handlers, but this is easier.
259 */
4e991c23 260static int fixup_options(struct thread_data *td)
e1f36503 261{
2dc1bbeb 262 struct thread_options *o = &td->o;
ff217451 263 int ret = 0;
dad915e3 264
f356d01d 265#ifndef FIO_HAVE_PSHARED_MUTEX
9bbf57cc 266 if (!o->use_thread) {
f356d01d
JA
267 log_info("fio: this platform does not support process shared"
268 " mutexes, forcing use of threads. Use the 'thread'"
269 " option to get rid of this warning.\n");
9bbf57cc 270 o->use_thread = 1;
a9523c6f 271 ret = warnings_fatal;
f356d01d
JA
272 }
273#endif
274
2dc1bbeb 275 if (o->write_iolog_file && o->read_iolog_file) {
076efc7c 276 log_err("fio: read iolog overrides write_iolog\n");
2dc1bbeb
JA
277 free(o->write_iolog_file);
278 o->write_iolog_file = NULL;
a9523c6f 279 ret = warnings_fatal;
076efc7c 280 }
16b462ae 281
16b462ae
JA
282 /*
283 * only really works for sequential io for now, and with 1 file
284 */
2dc1bbeb
JA
285 if (o->zone_size && td_random(td) && o->open_files == 1)
286 o->zone_size = 0;
16b462ae
JA
287
288 /*
289 * Reads can do overwrites, we always need to pre-create the file
290 */
291 if (td_read(td) || td_rw(td))
2dc1bbeb 292 o->overwrite = 1;
16b462ae 293
2dc1bbeb 294 if (!o->min_bs[DDIR_READ])
5ec10eaa 295 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
2dc1bbeb
JA
296 if (!o->max_bs[DDIR_READ])
297 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
298 if (!o->min_bs[DDIR_WRITE])
5ec10eaa 299 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
2dc1bbeb
JA
300 if (!o->max_bs[DDIR_WRITE])
301 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
a00735e6 302
2dc1bbeb 303 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
a00735e6 304
2b7a01d0
JA
305 /*
306 * For random IO, allow blockalign offset other than min_bs.
307 */
308 if (!o->ba[DDIR_READ] || !td_random(td))
309 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
310 if (!o->ba[DDIR_WRITE] || !td_random(td))
311 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
312
313 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
314 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
9bbf57cc 315 !o->norandommap) {
2b7a01d0 316 log_err("fio: Any use of blockalign= turns off randommap\n");
9bbf57cc 317 o->norandommap = 1;
a9523c6f 318 ret = warnings_fatal;
2b7a01d0
JA
319 }
320
2dc1bbeb
JA
321 if (!o->file_size_high)
322 o->file_size_high = o->file_size_low;
9c60ce64 323
8347239a
JA
324 if (o->norandommap && o->verify != VERIFY_NONE
325 && !fixed_block_size(o)) {
326 log_err("fio: norandommap given for variable block sizes, "
327 "verify disabled\n");
2dc1bbeb 328 o->verify = VERIFY_NONE;
a9523c6f 329 ret = warnings_fatal;
bb8895e0 330 }
2dc1bbeb 331 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
690adba3 332 log_err("fio: bs_unaligned may not work with raw io\n");
e0a22335 333
48097d5c
JA
334 /*
335 * thinktime_spin must be less than thinktime
336 */
2dc1bbeb
JA
337 if (o->thinktime_spin > o->thinktime)
338 o->thinktime_spin = o->thinktime;
e916b390
JA
339
340 /*
341 * The low water mark cannot be bigger than the iodepth
342 */
2dc1bbeb 343 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
9467b77c
JA
344 /*
345 * syslet work around - if the workload is sequential,
346 * we want to let the queue drain all the way down to
347 * avoid seeking between async threads
348 */
349 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
2dc1bbeb 350 o->iodepth_low = 1;
9467b77c 351 else
2dc1bbeb 352 o->iodepth_low = o->iodepth;
9467b77c 353 }
cb5ab512
JA
354
355 /*
356 * If batch number isn't set, default to the same as iodepth
357 */
2dc1bbeb
JA
358 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
359 o->iodepth_batch = o->iodepth;
b5af8293 360
2dc1bbeb
JA
361 if (o->nr_files > td->files_index)
362 o->nr_files = td->files_index;
9f9214f2 363
2dc1bbeb
JA
364 if (o->open_files > o->nr_files || !o->open_files)
365 o->open_files = o->nr_files;
4e991c23 366
581e7141
JA
367 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
368 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
369 o->rate_iops_min[1]))) {
4e991c23 370 log_err("fio: rate and rate_iops are mutually exclusive\n");
a9523c6f 371 ret = 1;
4e991c23 372 }
581e7141
JA
373 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
374 (o->rate_iops[0] < o->rate_iops_min[0]) ||
375 (o->rate_iops[1] < o->rate_iops_min[1])) {
4e991c23 376 log_err("fio: minimum rate exceeds rate\n");
a9523c6f 377 ret = 1;
4e991c23
JA
378 }
379
cf4464ca
JA
380 if (!o->timeout && o->time_based) {
381 log_err("fio: time_based requires a runtime/timeout setting\n");
382 o->time_based = 0;
a9523c6f 383 ret = warnings_fatal;
cf4464ca
JA
384 }
385
aa31f1f1 386 if (o->fill_device && !o->size)
5921e80c 387 o->size = -1ULL;
5ec10eaa 388
9bbf57cc 389 if (o->verify != VERIFY_NONE) {
996936f9 390 if (td_write(td) && o->do_verify && o->numjobs > 1) {
a9523c6f
JA
391 log_info("Multiple writers may overwrite blocks that "
392 "belong to other jobs. This can cause "
393 "verification failures.\n");
394 ret = warnings_fatal;
395 }
396
9bbf57cc 397 o->refill_buffers = 1;
2f1b8e8b
JA
398 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
399 !o->verify_interval)
400 o->verify_interval = o->min_bs[DDIR_WRITE];
d990588e 401 }
41ccd845 402
9bbf57cc
JA
403 if (o->pre_read) {
404 o->invalidate_cache = 0;
a9523c6f 405 if (td->io_ops->flags & FIO_PIPEIO) {
9c0d2241
JA
406 log_info("fio: cannot pre-read files with an IO engine"
407 " that isn't seekable. Pre-read disabled.\n");
a9523c6f
JA
408 ret = warnings_fatal;
409 }
9c0d2241 410 }
34f1c044 411
e72fa4d4 412#ifndef FIO_HAVE_FDATASYNC
9bbf57cc 413 if (o->fdatasync_blocks) {
e72fa4d4
JA
414 log_info("fio: this platform does not support fdatasync()"
415 " falling back to using fsync(). Use the 'fsync'"
416 " option instead of 'fdatasync' to get rid of"
417 " this warning\n");
9bbf57cc
JA
418 o->fsync_blocks = o->fdatasync_blocks;
419 o->fdatasync_blocks = 0;
a9523c6f 420 ret = warnings_fatal;
e72fa4d4
JA
421 }
422#endif
423
a9523c6f 424 return ret;
e1f36503
JA
425}
426
f8977ee6
JA
427/*
428 * This function leaks the buffer
429 */
430static char *to_kmg(unsigned int val)
431{
432 char *buf = malloc(32);
f3502ba2 433 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
f8977ee6
JA
434 char *p = post;
435
245142ff 436 do {
f8977ee6
JA
437 if (val & 1023)
438 break;
439
440 val >>= 10;
441 p++;
245142ff 442 } while (*p);
f8977ee6
JA
443
444 snprintf(buf, 31, "%u%c", val, *p);
445 return buf;
446}
447
09629a90
JA
448/* External engines are specified by "external:name.o") */
449static const char *get_engine_name(const char *str)
450{
451 char *p = strstr(str, ":");
452
453 if (!p)
454 return str;
455
456 p++;
457 strip_blank_front(&p);
458 strip_blank_end(p);
459 return p;
460}
461
e132cbae
JA
462static int exists_and_not_file(const char *filename)
463{
464 struct stat sb;
465
466 if (lstat(filename, &sb) == -1)
467 return 0;
468
ecc314ba
BC
469 /* \\.\ is the device namespace in Windows, where every file
470 * is a device node */
471 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
e132cbae
JA
472 return 0;
473
474 return 1;
475}
476
4c07ad86 477static void td_fill_rand_seeds_os(struct thread_data *td)
5bfc35d7
JA
478{
479 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
480 os_random_seed(td->rand_seeds[1], &td->verify_state);
481 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
482
483 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
484 os_random_seed(td->rand_seeds[3], &td->next_file_state);
485
486 os_random_seed(td->rand_seeds[5], &td->file_size_state);
0d29de83 487 os_random_seed(td->rand_seeds[6], &td->trim_state);
5bfc35d7
JA
488
489 if (!td_random(td))
490 return;
491
492 if (td->o.rand_repeatable)
493 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
494
495 os_random_seed(td->rand_seeds[4], &td->random_state);
4c07ad86
JA
496}
497
498static void td_fill_rand_seeds_internal(struct thread_data *td)
499{
500 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
501 init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
502 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
503
504 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
505 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
506
507 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
508 init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
509
510 if (!td_random(td))
511 return;
512
513 if (td->o.rand_repeatable)
514 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
515
2615cc4b 516 init_rand_seed(&td->__random_state, td->rand_seeds[4]);
5bfc35d7
JA
517}
518
4c07ad86
JA
519void td_fill_rand_seeds(struct thread_data *td)
520{
521 if (td->o.use_os_rand)
522 td_fill_rand_seeds_os(td);
523 else
524 td_fill_rand_seeds_internal(td);
3545a109
JA
525
526 init_rand_seed(&td->buf_state, td->rand_seeds[7]);
4c07ad86
JA
527}
528
9c60ce64
JA
529/*
530 * Initialize the various random states we need (random io, block size ranges,
531 * read/write mix, etc).
532 */
533static int init_random_state(struct thread_data *td)
534{
68727076 535 int fd;
9c60ce64
JA
536
537 fd = open("/dev/urandom", O_RDONLY);
538 if (fd == -1) {
539 td_verror(td, errno, "open");
540 return 1;
541 }
542
5bfc35d7
JA
543 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
544 (int) sizeof(td->rand_seeds)) {
9c60ce64
JA
545 td_verror(td, EIO, "read");
546 close(fd);
547 return 1;
548 }
549
550 close(fd);
5bfc35d7 551 td_fill_rand_seeds(td);
9c60ce64
JA
552 return 0;
553}
554
906c8d75
JA
555/*
556 * Adds a job to the list of things todo. Sanitizes the various options
557 * to make sure we don't have conflicts, and initializes various
558 * members of td.
559 */
75154845 560static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
ebac4655 561{
413dd459
JA
562 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
563 "randread", "randwrite", "randrw" };
af52b345 564 unsigned int i;
09629a90 565 const char *engine;
af52b345 566 char fname[PATH_MAX];
e132cbae 567 int numjobs, file_alloced;
ebac4655 568
ebac4655
JA
569 /*
570 * the def_thread is just for options, it's not a real job
571 */
572 if (td == &def_thread)
573 return 0;
574
cca73aa7
JA
575 /*
576 * if we are just dumping the output command line, don't add the job
577 */
578 if (dump_cmdline) {
579 put_job(td);
580 return 0;
581 }
582
58c55ba0 583 if (profile_td_init(td))
66251554 584 goto err;
58c55ba0 585
2dc1bbeb 586 engine = get_engine_name(td->o.ioengine);
09629a90
JA
587 td->io_ops = load_ioengine(td, engine);
588 if (!td->io_ops) {
589 log_err("fio: failed to load engine %s\n", engine);
205927a3 590 goto err;
09629a90 591 }
df64119d 592
2dc1bbeb 593 if (td->o.use_thread)
9cedf167
JA
594 nr_thread++;
595 else
596 nr_process++;
597
2dc1bbeb 598 if (td->o.odirect)
690adba3
JA
599 td->io_ops->flags |= FIO_RAWIO;
600
e132cbae 601 file_alloced = 0;
691c8fb0 602 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
e132cbae 603 file_alloced = 1;
80be24f4 604
2dc1bbeb 605 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
e132cbae 606 add_file(td, jobname);
7b05a215 607 else {
2dc1bbeb 608 for (i = 0; i < td->o.nr_files; i++) {
5ec10eaa
JA
609 sprintf(fname, "%s.%d.%d", jobname,
610 td->thread_number, i);
7b05a215
JA
611 add_file(td, fname);
612 }
af52b345 613 }
0af7b542 614 }
ebac4655 615
4e991c23
JA
616 if (fixup_options(td))
617 goto err;
e0a22335 618
07eb79df
JA
619 if (td->io_ops->flags & FIO_DISKLESSIO) {
620 struct fio_file *f;
621
622 for_each_file(td, f, i)
623 f->real_file_size = -1ULL;
624 }
625
cdd18ad8 626 td->mutex = fio_mutex_init(0);
ebac4655 627
83349190
YH
628 td->ts.clat_percentiles = td->o.clat_percentiles;
629 if (td->o.overwrite_plist)
630 td->ts.percentile_list = td->o.percentile_list;
631 else
632 td->ts.percentile_list = NULL;
633
756867bd
JA
634 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
635 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
02af0988 636 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX;
756867bd 637 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
cdd5411e 638 td->ddir_seq_nr = td->o.ddir_seq_nr;
ebac4655 639
755c3189 640 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
3c5df6fa 641 prev_group_jobs = 0;
ebac4655 642 groupid++;
3c5df6fa 643 }
ebac4655
JA
644
645 td->groupid = groupid;
3c5df6fa 646 prev_group_jobs++;
ebac4655 647
9c60ce64
JA
648 if (init_random_state(td))
649 goto err;
650
ebac4655
JA
651 if (setup_rate(td))
652 goto err;
653
2dc1bbeb 654 if (td->o.write_lat_log) {
02af0988 655 setup_log(&td->ts.lat_log);
756867bd
JA
656 setup_log(&td->ts.slat_log);
657 setup_log(&td->ts.clat_log);
ebac4655 658 }
2dc1bbeb 659 if (td->o.write_bw_log)
756867bd 660 setup_log(&td->ts.bw_log);
ebac4655 661
2dc1bbeb
JA
662 if (!td->o.name)
663 td->o.name = strdup(jobname);
01452055 664
c6ae0a5b 665 if (!terse_output) {
b990b5c0 666 if (!job_add_num) {
5ec10eaa
JA
667 if (!strcmp(td->io_ops->name, "cpuio")) {
668 log_info("%s: ioengine=cpu, cpuload=%u,"
669 " cpucycle=%u\n", td->o.name,
670 td->o.cpuload,
671 td->o.cpucycle);
672 } else {
f8977ee6
JA
673 char *c1, *c2, *c3, *c4;
674
2dc1bbeb
JA
675 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
676 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
677 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
678 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
f8977ee6 679
5ec10eaa
JA
680 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
681 " ioengine=%s, iodepth=%u\n",
682 td->o.name, td->groupid,
683 ddir_str[td->o.td_ddir],
684 c1, c2, c3, c4,
685 td->io_ops->name,
686 td->o.iodepth);
f8977ee6
JA
687
688 free(c1);
689 free(c2);
690 free(c3);
691 free(c4);
692 }
b990b5c0 693 } else if (job_add_num == 1)
6d86144d 694 log_info("...\n");
c6ae0a5b 695 }
ebac4655
JA
696
697 /*
698 * recurse add identical jobs, clear numjobs and stonewall options
699 * as they don't apply to sub-jobs
700 */
2dc1bbeb 701 numjobs = td->o.numjobs;
ebac4655
JA
702 while (--numjobs) {
703 struct thread_data *td_new = get_new_job(0, td);
704
705 if (!td_new)
706 goto err;
707
2dc1bbeb
JA
708 td_new->o.numjobs = 1;
709 td_new->o.stonewall = 0;
92c1d41f 710 td_new->o.new_group = 0;
e132cbae
JA
711
712 if (file_alloced) {
2dc1bbeb 713 td_new->o.filename = NULL;
e132cbae 714 td_new->files_index = 0;
4e6ea2f1 715 td_new->files_size = 0;
e132cbae
JA
716 td_new->files = NULL;
717 }
718
75154845 719 job_add_num = numjobs - 1;
ebac4655 720
75154845 721 if (add_job(td_new, jobname, job_add_num))
ebac4655
JA
722 goto err;
723 }
3c5df6fa 724
ebac4655
JA
725 return 0;
726err:
727 put_job(td);
728 return -1;
729}
730
79d16311
JA
731/*
732 * Parse as if 'o' was a command line
733 */
734void add_job_opts(const char **o)
735{
736 struct thread_data *td, *td_parent;
737 int i, in_global = 1;
738 char jobname[32];
739
740 i = 0;
741 td_parent = td = NULL;
742 while (o[i]) {
743 if (!strncmp(o[i], "name", 4)) {
744 in_global = 0;
745 if (td)
746 add_job(td, jobname, 0);
747 td = NULL;
748 sprintf(jobname, "%s", o[i] + 5);
749 }
750 if (in_global && !td_parent)
751 td_parent = get_new_job(1, &def_thread);
752 else if (!in_global && !td) {
753 if (!td_parent)
754 td_parent = &def_thread;
755 td = get_new_job(0, td_parent);
756 }
757 if (in_global)
758 fio_options_parse(td_parent, (char **) &o[i], 1);
759 else
760 fio_options_parse(td, (char **) &o[i], 1);
761 i++;
762 }
763
764 if (td)
765 add_job(td, jobname, 0);
766}
767
01f06b63
JA
768static int skip_this_section(const char *name)
769{
ad0a2735
JA
770 int i;
771
772 if (!nr_job_sections)
01f06b63
JA
773 return 0;
774 if (!strncmp(name, "global", 6))
775 return 0;
776
ad0a2735
JA
777 for (i = 0; i < nr_job_sections; i++)
778 if (!strcmp(job_sections[i], name))
779 return 0;
780
781 return 1;
01f06b63
JA
782}
783
ebac4655
JA
784static int is_empty_or_comment(char *line)
785{
786 unsigned int i;
787
788 for (i = 0; i < strlen(line); i++) {
789 if (line[i] == ';')
790 return 1;
5cc2da30
IM
791 if (line[i] == '#')
792 return 1;
76cd9378 793 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
ebac4655
JA
794 return 0;
795 }
796
797 return 1;
798}
799
07261983
JA
800/*
801 * This is our [ini] type file parser.
802 */
1e97cce9 803static int parse_jobs_ini(char *file, int stonewall_flag)
ebac4655 804{
e1f36503 805 unsigned int global;
ebac4655 806 struct thread_data *td;
fee3bb48 807 char *string, *name;
ebac4655
JA
808 FILE *f;
809 char *p;
0c7e37a0 810 int ret = 0, stonewall;
097b2991 811 int first_sect = 1;
ccf8f127 812 int skip_fgets = 0;
01f06b63 813 int inside_skip = 0;
3b8b7135
JA
814 char **opts;
815 int i, alloc_opts, num_opts;
ebac4655 816
5a729cbe
AC
817 if (!strcmp(file, "-"))
818 f = stdin;
819 else
820 f = fopen(file, "r");
821
ebac4655 822 if (!f) {
aea47d44 823 perror("fopen job file");
ebac4655
JA
824 return 1;
825 }
826
827 string = malloc(4096);
7f7e6e59
JA
828
829 /*
830 * it's really 256 + small bit, 280 should suffice
831 */
832 name = malloc(280);
833 memset(name, 0, 280);
ebac4655 834
3b8b7135
JA
835 alloc_opts = 8;
836 opts = malloc(sizeof(char *) * alloc_opts);
b7c6302d 837 num_opts = 0;
3b8b7135 838
0c7e37a0 839 stonewall = stonewall_flag;
7c124ac1 840 do {
ccf8f127
JA
841 /*
842 * if skip_fgets is set, we already have loaded a line we
843 * haven't handled.
844 */
845 if (!skip_fgets) {
846 p = fgets(string, 4095, f);
847 if (!p)
848 break;
849 }
cdc7f193 850
ccf8f127 851 skip_fgets = 0;
cdc7f193 852 strip_blank_front(&p);
6c7c7da1 853 strip_blank_end(p);
cdc7f193 854
ebac4655
JA
855 if (is_empty_or_comment(p))
856 continue;
84dd1886 857 if (sscanf(p, "[%255[^\n]]", name) != 1) {
01f06b63
JA
858 if (inside_skip)
859 continue;
5ec10eaa
JA
860 log_err("fio: option <%s> outside of [] job section\n",
861 p);
088b4207 862 break;
6c7c7da1 863 }
ebac4655 864
7a4b80a1
JA
865 name[strlen(name) - 1] = '\0';
866
01f06b63
JA
867 if (skip_this_section(name)) {
868 inside_skip = 1;
869 continue;
870 } else
871 inside_skip = 0;
872
ebac4655
JA
873 global = !strncmp(name, "global", 6);
874
cca73aa7 875 if (dump_cmdline) {
097b2991
JA
876 if (first_sect)
877 log_info("fio ");
cca73aa7
JA
878 if (!global)
879 log_info("--name=%s ", name);
097b2991 880 first_sect = 0;
cca73aa7
JA
881 }
882
ebac4655 883 td = get_new_job(global, &def_thread);
45410acb
JA
884 if (!td) {
885 ret = 1;
886 break;
887 }
ebac4655 888
972cfd25
JA
889 /*
890 * Seperate multiple job files by a stonewall
891 */
f9481919 892 if (!global && stonewall) {
2dc1bbeb 893 td->o.stonewall = stonewall;
972cfd25
JA
894 stonewall = 0;
895 }
896
3b8b7135
JA
897 num_opts = 0;
898 memset(opts, 0, alloc_opts * sizeof(char *));
899
ebac4655
JA
900 while ((p = fgets(string, 4096, f)) != NULL) {
901 if (is_empty_or_comment(p))
902 continue;
e1f36503 903
b6754f9d 904 strip_blank_front(&p);
7c124ac1 905
ccf8f127
JA
906 /*
907 * new section, break out and make sure we don't
908 * fgets() a new line at the top.
909 */
910 if (p[0] == '[') {
911 skip_fgets = 1;
7c124ac1 912 break;
ccf8f127 913 }
7c124ac1 914
4ae3f763 915 strip_blank_end(p);
aea47d44 916
3b8b7135
JA
917 if (num_opts == alloc_opts) {
918 alloc_opts <<= 1;
919 opts = realloc(opts,
920 alloc_opts * sizeof(char *));
921 }
922
923 opts[num_opts] = strdup(p);
924 num_opts++;
ebac4655 925 }
ebac4655 926
3b8b7135
JA
927 ret = fio_options_parse(td, opts, num_opts);
928 if (!ret) {
929 if (dump_cmdline)
930 for (i = 0; i < num_opts; i++)
931 log_info("--%s ", opts[i]);
932
45410acb 933 ret = add_job(td, name, 0);
3b8b7135 934 } else {
b1508cf9
JA
935 log_err("fio: job %s dropped\n", name);
936 put_job(td);
45410acb 937 }
3b8b7135
JA
938
939 for (i = 0; i < num_opts; i++)
940 free(opts[i]);
941 num_opts = 0;
7c124ac1 942 } while (!ret);
ebac4655 943
cca73aa7
JA
944 if (dump_cmdline)
945 log_info("\n");
946
3b8b7135
JA
947 for (i = 0; i < num_opts; i++)
948 free(opts[i]);
949
ebac4655
JA
950 free(string);
951 free(name);
2577594d 952 free(opts);
5a729cbe
AC
953 if (f != stdin)
954 fclose(f);
45410acb 955 return ret;
ebac4655
JA
956}
957
958static int fill_def_thread(void)
959{
960 memset(&def_thread, 0, sizeof(def_thread));
961
375b2695 962 fio_getaffinity(getpid(), &def_thread.o.cpumask);
8c029376 963 def_thread.o.timeout = def_timeout;
ebac4655
JA
964
965 /*
ee738499 966 * fill default options
ebac4655 967 */
214e1eca 968 fio_fill_default_options(&def_thread);
ebac4655
JA
969 return 0;
970}
971
214e1eca
JA
972static void free_shm(void)
973{
974 struct shmid_ds sbuf;
975
976 if (threads) {
5e1d306e
JA
977 void *tp = threads;
978
214e1eca 979 threads = NULL;
5e1d306e
JA
980 file_hash_exit();
981 fio_debug_jobp = NULL;
982 shmdt(tp);
214e1eca
JA
983 shmctl(shm_id, IPC_RMID, &sbuf);
984 }
2e5cdb11
JA
985
986 scleanup();
214e1eca
JA
987}
988
989/*
990 * The thread area is shared between the main process and the job
991 * threads/processes. So setup a shared memory segment that will hold
380065aa
JA
992 * all the job info. We use the end of the region for keeping track of
993 * open files across jobs, for file sharing.
214e1eca
JA
994 */
995static int setup_thread_area(void)
996{
380065aa
JA
997 void *hash;
998
214e1eca
JA
999 /*
1000 * 1024 is too much on some machines, scale max_jobs if
1001 * we get a failure that looks like too large a shm segment
1002 */
1003 do {
1004 size_t size = max_jobs * sizeof(struct thread_data);
1005
380065aa 1006 size += file_hash_size;
5e1d306e 1007 size += sizeof(unsigned int);
380065aa 1008
214e1eca
JA
1009 shm_id = shmget(0, size, IPC_CREAT | 0600);
1010 if (shm_id != -1)
1011 break;
1012 if (errno != EINVAL) {
1013 perror("shmget");
1014 break;
1015 }
1016
1017 max_jobs >>= 1;
1018 } while (max_jobs);
1019
1020 if (shm_id == -1)
1021 return 1;
1022
1023 threads = shmat(shm_id, NULL, 0);
1024 if (threads == (void *) -1) {
1025 perror("shmat");
1026 return 1;
1027 }
1028
1f809d15 1029 memset(threads, 0, max_jobs * sizeof(struct thread_data));
380065aa 1030 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
5e1d306e
JA
1031 fio_debug_jobp = (void *) hash + file_hash_size;
1032 *fio_debug_jobp = -1;
380065aa 1033 file_hash_init(hash);
214e1eca
JA
1034 atexit(free_shm);
1035 return 0;
1036}
1037
45378b35 1038static void usage(const char *name)
4785f995 1039{
43617628 1040 printf("%s\n", fio_version_string);
45378b35 1041 printf("%s [options] [job options] <job file(s)>\n", name);
ee56ad50 1042 printf("\t--debug=options\tEnable debug logging\n");
b4692828
JA
1043 printf("\t--output\tWrite output to file\n");
1044 printf("\t--timeout\tRuntime in seconds\n");
1045 printf("\t--latency-log\tGenerate per-job latency logs\n");
1046 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1047 printf("\t--minimal\tMinimal (terse) output\n");
1048 printf("\t--version\tPrint version info and exit\n");
f57a9c59 1049 printf("\t--terse-version=x Terse version output format\n");
fd28ca49 1050 printf("\t--help\t\tPrint this page\n");
5ec10eaa
JA
1051 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
1052 " them\n");
cca73aa7 1053 printf("\t--showcmd\tTurn a job file into command line options\n");
e592a06b
AC
1054 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
1055 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
5ec10eaa
JA
1056 printf("\t--readonly\tTurn on safety read-only checks, preventing"
1057 " writes\n");
01f06b63 1058 printf("\t--section=name\tOnly run specified section in job file\n");
2b386d25
JA
1059 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
1060 " (def 1024)\n");
a9523c6f 1061 printf("\t--warnings-fatal Fio parser warnings are fatal\n");
fca70358 1062 printf("\t--max-jobs\tMaximum number of threads/processes to support\n");
aa58d252
JA
1063 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1064 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
4785f995
JA
1065}
1066
79e48f72 1067#ifdef FIO_INC_DEBUG
ee56ad50 1068struct debug_level debug_levels[] = {
bd6f78b2
JA
1069 { .name = "process", .shift = FD_PROCESS, },
1070 { .name = "file", .shift = FD_FILE, },
1071 { .name = "io", .shift = FD_IO, },
1072 { .name = "mem", .shift = FD_MEM, },
1073 { .name = "blktrace", .shift = FD_BLKTRACE },
1074 { .name = "verify", .shift = FD_VERIFY },
84422acd 1075 { .name = "random", .shift = FD_RANDOM },
a3d741fa 1076 { .name = "parse", .shift = FD_PARSE },
cd991b9e 1077 { .name = "diskutil", .shift = FD_DISKUTIL },
5e1d306e 1078 { .name = "job", .shift = FD_JOB },
29adda3c 1079 { .name = "mutex", .shift = FD_MUTEX },
79d16311 1080 { .name = "profile", .shift = FD_PROFILE },
c223da83 1081 { .name = "time", .shift = FD_TIME },
02444ad1 1082 { .name = NULL, },
ee56ad50
JA
1083};
1084
c09823ab 1085static int set_debug(const char *string)
ee56ad50
JA
1086{
1087 struct debug_level *dl;
1088 char *p = (char *) string;
1089 char *opt;
1090 int i;
1091
1092 if (!strcmp(string, "?") || !strcmp(string, "help")) {
ee56ad50
JA
1093 log_info("fio: dumping debug options:");
1094 for (i = 0; debug_levels[i].name; i++) {
1095 dl = &debug_levels[i];
1096 log_info("%s,", dl->name);
1097 }
bd6f78b2 1098 log_info("all\n");
c09823ab 1099 return 1;
ee56ad50
JA
1100 }
1101
1102 while ((opt = strsep(&p, ",")) != NULL) {
1103 int found = 0;
1104
5e1d306e
JA
1105 if (!strncmp(opt, "all", 3)) {
1106 log_info("fio: set all debug options\n");
1107 fio_debug = ~0UL;
1108 continue;
1109 }
1110
ee56ad50
JA
1111 for (i = 0; debug_levels[i].name; i++) {
1112 dl = &debug_levels[i];
5e1d306e
JA
1113 found = !strncmp(opt, dl->name, strlen(dl->name));
1114 if (!found)
1115 continue;
1116
1117 if (dl->shift == FD_JOB) {
1118 opt = strchr(opt, ':');
1119 if (!opt) {
1120 log_err("fio: missing job number\n");
1121 break;
1122 }
1123 opt++;
1124 fio_debug_jobno = atoi(opt);
1125 log_info("fio: set debug jobno %d\n",
1126 fio_debug_jobno);
1127 } else {
ee56ad50 1128 log_info("fio: set debug option %s\n", opt);
bd6f78b2 1129 fio_debug |= (1UL << dl->shift);
ee56ad50 1130 }
5e1d306e 1131 break;
ee56ad50
JA
1132 }
1133
1134 if (!found)
1135 log_err("fio: debug mask %s not found\n", opt);
1136 }
c09823ab 1137 return 0;
ee56ad50 1138}
79e48f72 1139#else
69b98d4c 1140static int set_debug(const char *string)
79e48f72
JA
1141{
1142 log_err("fio: debug tracing not included in build\n");
c09823ab 1143 return 1;
79e48f72
JA
1144}
1145#endif
9ac8a797 1146
4c6107ff
JA
1147static void fio_options_fill_optstring(void)
1148{
1149 char *ostr = cmd_optstr;
1150 int i, c;
1151
1152 c = i = 0;
1153 while (l_opts[i].name) {
1154 ostr[c++] = l_opts[i].val;
1155 if (l_opts[i].has_arg == required_argument)
1156 ostr[c++] = ':';
1157 else if (l_opts[i].has_arg == optional_argument) {
1158 ostr[c++] = ':';
1159 ostr[c++] = ':';
1160 }
1161 i++;
1162 }
1163 ostr[c] = '\0';
1164}
1165
972cfd25 1166static int parse_cmd_line(int argc, char *argv[])
ebac4655 1167{
b4692828 1168 struct thread_data *td = NULL;
c09823ab 1169 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
4c6107ff 1170 char *ostr = cmd_optstr;
ebac4655 1171
4c6107ff 1172 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
ebac4655 1173 switch (c) {
2b386d25
JA
1174 case 'a':
1175 smalloc_pool_size = atoi(optarg);
1176 break;
b4692828 1177 case 't':
8c029376 1178 def_timeout = atoi(optarg);
b4692828
JA
1179 break;
1180 case 'l':
1181 write_lat_log = 1;
1182 break;
3d73e5a9 1183 case 'b':
b4692828
JA
1184 write_bw_log = 1;
1185 break;
1186 case 'o':
1187 f_out = fopen(optarg, "w+");
1188 if (!f_out) {
1189 perror("fopen output");
1190 exit(1);
1191 }
1192 f_err = f_out;
1193 break;
1194 case 'm':
1195 terse_output = 1;
1196 break;
1197 case 'h':
45378b35 1198 usage(argv[0]);
b4692828 1199 exit(0);
fd28ca49 1200 case 'c':
214e1eca 1201 exit(fio_show_option_help(optarg));
cca73aa7
JA
1202 case 's':
1203 dump_cmdline = 1;
1204 break;
724e4435
JA
1205 case 'r':
1206 read_only = 1;
1207 break;
b4692828 1208 case 'v':
ca740f4e 1209 log_info("%s\n", fio_version_string);
b4692828 1210 exit(0);
f57a9c59
JA
1211 case 'V':
1212 terse_version = atoi(optarg);
1213 if (terse_version != 2) {
1214 log_err("fio: bad terse version format\n");
1215 exit_val = 1;
1216 do_exit++;
1217 }
1218 break;
e592a06b
AC
1219 case 'e':
1220 if (!strcmp("always", optarg))
1221 eta_print = FIO_ETA_ALWAYS;
1222 else if (!strcmp("never", optarg))
1223 eta_print = FIO_ETA_NEVER;
1224 break;
ee56ad50 1225 case 'd':
c09823ab
JA
1226 if (set_debug(optarg))
1227 do_exit++;
ee56ad50 1228 break;
ad0a2735
JA
1229 case 'x': {
1230 size_t new_size;
1231
01f06b63 1232 if (!strcmp(optarg, "global")) {
5ec10eaa
JA
1233 log_err("fio: can't use global as only "
1234 "section\n");
c09823ab
JA
1235 do_exit++;
1236 exit_val = 1;
01f06b63
JA
1237 break;
1238 }
ad0a2735
JA
1239 new_size = (nr_job_sections + 1) * sizeof(char *);
1240 job_sections = realloc(job_sections, new_size);
1241 job_sections[nr_job_sections] = strdup(optarg);
1242 nr_job_sections++;
01f06b63 1243 break;
ad0a2735 1244 }
9ac8a797 1245 case 'p':
07b3232d 1246 exec_profile = strdup(optarg);
9ac8a797 1247 break;
b4692828 1248 case FIO_GETOPT_JOB: {
5ec10eaa 1249 const char *opt = l_opts[lidx].name;
b4692828
JA
1250 char *val = optarg;
1251
c2b1e753 1252 if (!strncmp(opt, "name", 4) && td) {
2dc1bbeb 1253 ret = add_job(td, td->o.name ?: "fio", 0);
66251554 1254 if (ret)
c2b1e753 1255 return 0;
c2b1e753
JA
1256 td = NULL;
1257 }
b4692828 1258 if (!td) {
01f06b63 1259 int is_section = !strncmp(opt, "name", 4);
3106f220
JA
1260 int global = 0;
1261
01f06b63 1262 if (!is_section || !strncmp(val, "global", 6))
3106f220 1263 global = 1;
c2b1e753 1264
01f06b63
JA
1265 if (is_section && skip_this_section(val))
1266 continue;
1267
c2b1e753 1268 td = get_new_job(global, &def_thread);
b4692828
JA
1269 if (!td)
1270 return 0;
1271 }
38d0adb0 1272
214e1eca 1273 ret = fio_cmd_option_parse(td, opt, val);
b4692828
JA
1274 break;
1275 }
a9523c6f
JA
1276 case 'w':
1277 warnings_fatal = 1;
1278 break;
fca70358
JA
1279 case 'j':
1280 max_jobs = atoi(optarg);
1281 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1282 log_err("fio: invalid max jobs: %d\n", max_jobs);
1283 do_exit++;
1284 exit_val = 1;
1285 }
1286 break;
b4692828 1287 default:
c09823ab
JA
1288 do_exit++;
1289 exit_val = 1;
b4692828 1290 break;
ebac4655
JA
1291 }
1292 }
c9fad893 1293
c09823ab
JA
1294 if (do_exit)
1295 exit(exit_val);
536582bf 1296
b4692828 1297 if (td) {
7d6a8904 1298 if (!ret)
2dc1bbeb 1299 ret = add_job(td, td->o.name ?: "fio", 0);
972cfd25 1300 }
774a6177 1301
b4692828
JA
1302 while (optind < argc) {
1303 ini_idx++;
1304 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1305 ini_file[ini_idx - 1] = strdup(argv[optind]);
1306 optind++;
eb8bbf48 1307 }
972cfd25
JA
1308
1309 return ini_idx;
ebac4655
JA
1310}
1311
ebac4655
JA
1312int parse_options(int argc, char *argv[])
1313{
972cfd25
JA
1314 int job_files, i;
1315
b4692828
JA
1316 f_out = stdout;
1317 f_err = stderr;
1318
4c6107ff 1319 fio_options_fill_optstring();
5ec10eaa 1320 fio_options_dup_and_init(l_opts);
b4692828 1321
ebac4655
JA
1322 if (setup_thread_area())
1323 return 1;
1324 if (fill_def_thread())
1325 return 1;
1326
972cfd25 1327 job_files = parse_cmd_line(argc, argv);
ebac4655 1328
972cfd25
JA
1329 for (i = 0; i < job_files; i++) {
1330 if (fill_def_thread())
1331 return 1;
0c7e37a0 1332 if (parse_jobs_ini(ini_file[i], i))
972cfd25 1333 return 1;
88c6ed80 1334 free(ini_file[i]);
972cfd25 1335 }
ebac4655 1336
88c6ed80 1337 free(ini_file);
d23bb327 1338 options_mem_free(&def_thread);
b4692828
JA
1339
1340 if (!thread_number) {
cca73aa7
JA
1341 if (dump_cmdline)
1342 return 0;
07b3232d
JA
1343 if (exec_profile)
1344 return 0;
cca73aa7 1345
03e20d68 1346 log_err("No jobs(s) defined\n\n");
45378b35 1347 usage(argv[0]);
b4692828
JA
1348 return 1;
1349 }
1350
be4ecfdf
JA
1351 if (def_thread.o.gtod_offload) {
1352 fio_gtod_init();
1353 fio_gtod_offload = 1;
1354 fio_gtod_cpu = def_thread.o.gtod_cpu;
1355 }
1356
ca740f4e 1357 log_info("%s\n", fio_version_string);
ebac4655
JA
1358 return 0;
1359}