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