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