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