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