lib/pattern: add set of functions to parse combined pattern input
[fio.git] / init.c
... / ...
CommitLineData
1/*
2 * This file contains job initialization and setup functions.
3 */
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/types.h>
13#include <sys/stat.h>
14
15#include "fio.h"
16#ifndef FIO_NO_HAVE_SHM_H
17#include <sys/shm.h>
18#endif
19
20#include "parse.h"
21#include "smalloc.h"
22#include "filehash.h"
23#include "verify.h"
24#include "profile.h"
25#include "server.h"
26#include "idletime.h"
27#include "filelock.h"
28
29#include "lib/getopt.h"
30#include "lib/strcasestr.h"
31
32#include "crc/test.h"
33
34const char fio_version_string[] = FIO_VERSION;
35
36#define FIO_RANDSEED (0xb1899bedUL)
37
38static char **ini_file;
39static int max_jobs = FIO_MAX_JOBS;
40static int dump_cmdline;
41static long long def_timeout;
42static int parse_only;
43
44static struct thread_data def_thread;
45struct thread_data *threads = NULL;
46static char **job_sections;
47static int nr_job_sections;
48
49int exitall_on_terminate = 0;
50int output_format = FIO_OUTPUT_NORMAL;
51int append_terse_output = 0;
52int eta_print = FIO_ETA_AUTO;
53int eta_new_line = 0;
54FILE *f_out = NULL;
55FILE *f_err = NULL;
56char *exec_profile = NULL;
57int warnings_fatal = 0;
58int terse_version = 3;
59int is_backend = 0;
60int nr_clients = 0;
61int log_syslog = 0;
62
63int write_bw_log = 0;
64int read_only = 0;
65int status_interval = 0;
66
67char *trigger_file = NULL;
68long long trigger_timeout = 0;
69char *trigger_cmd = NULL;
70char *trigger_remote_cmd = NULL;
71
72char *aux_path = NULL;
73
74static int prev_group_jobs;
75
76unsigned long fio_debug = 0;
77unsigned int fio_debug_jobno = -1;
78unsigned int *fio_debug_jobp = NULL;
79
80static char cmd_optstr[256];
81static int did_arg;
82
83#define FIO_CLIENT_FLAG (1 << 16)
84
85/*
86 * Command line options. These will contain the above, plus a few
87 * extra that only pertain to fio itself and not jobs.
88 */
89static struct option l_opts[FIO_NR_OPTIONS] = {
90 {
91 .name = (char *) "output",
92 .has_arg = required_argument,
93 .val = 'o' | FIO_CLIENT_FLAG,
94 },
95 {
96 .name = (char *) "timeout",
97 .has_arg = required_argument,
98 .val = 't' | FIO_CLIENT_FLAG,
99 },
100 {
101 .name = (char *) "latency-log",
102 .has_arg = required_argument,
103 .val = 'l' | FIO_CLIENT_FLAG,
104 },
105 {
106 .name = (char *) "bandwidth-log",
107 .has_arg = required_argument,
108 .val = 'b' | FIO_CLIENT_FLAG,
109 },
110 {
111 .name = (char *) "minimal",
112 .has_arg = no_argument,
113 .val = 'm' | FIO_CLIENT_FLAG,
114 },
115 {
116 .name = (char *) "output-format",
117 .has_arg = optional_argument,
118 .val = 'F' | FIO_CLIENT_FLAG,
119 },
120 {
121 .name = (char *) "append-terse",
122 .has_arg = optional_argument,
123 .val = 'f',
124 },
125 {
126 .name = (char *) "version",
127 .has_arg = no_argument,
128 .val = 'v' | FIO_CLIENT_FLAG,
129 },
130 {
131 .name = (char *) "help",
132 .has_arg = no_argument,
133 .val = 'h' | FIO_CLIENT_FLAG,
134 },
135 {
136 .name = (char *) "cmdhelp",
137 .has_arg = optional_argument,
138 .val = 'c' | FIO_CLIENT_FLAG,
139 },
140 {
141 .name = (char *) "enghelp",
142 .has_arg = optional_argument,
143 .val = 'i' | FIO_CLIENT_FLAG,
144 },
145 {
146 .name = (char *) "showcmd",
147 .has_arg = no_argument,
148 .val = 's' | FIO_CLIENT_FLAG,
149 },
150 {
151 .name = (char *) "readonly",
152 .has_arg = no_argument,
153 .val = 'r' | FIO_CLIENT_FLAG,
154 },
155 {
156 .name = (char *) "eta",
157 .has_arg = required_argument,
158 .val = 'e' | FIO_CLIENT_FLAG,
159 },
160 {
161 .name = (char *) "eta-newline",
162 .has_arg = required_argument,
163 .val = 'E' | FIO_CLIENT_FLAG,
164 },
165 {
166 .name = (char *) "debug",
167 .has_arg = required_argument,
168 .val = 'd' | FIO_CLIENT_FLAG,
169 },
170 {
171 .name = (char *) "parse-only",
172 .has_arg = no_argument,
173 .val = 'P' | FIO_CLIENT_FLAG,
174 },
175 {
176 .name = (char *) "section",
177 .has_arg = required_argument,
178 .val = 'x' | FIO_CLIENT_FLAG,
179 },
180#ifdef CONFIG_ZLIB
181 {
182 .name = (char *) "inflate-log",
183 .has_arg = required_argument,
184 .val = 'X' | FIO_CLIENT_FLAG,
185 },
186#endif
187 {
188 .name = (char *) "alloc-size",
189 .has_arg = required_argument,
190 .val = 'a' | FIO_CLIENT_FLAG,
191 },
192 {
193 .name = (char *) "profile",
194 .has_arg = required_argument,
195 .val = 'p' | FIO_CLIENT_FLAG,
196 },
197 {
198 .name = (char *) "warnings-fatal",
199 .has_arg = no_argument,
200 .val = 'w' | FIO_CLIENT_FLAG,
201 },
202 {
203 .name = (char *) "max-jobs",
204 .has_arg = required_argument,
205 .val = 'j' | FIO_CLIENT_FLAG,
206 },
207 {
208 .name = (char *) "terse-version",
209 .has_arg = required_argument,
210 .val = 'V' | FIO_CLIENT_FLAG,
211 },
212 {
213 .name = (char *) "server",
214 .has_arg = optional_argument,
215 .val = 'S',
216 },
217 { .name = (char *) "daemonize",
218 .has_arg = required_argument,
219 .val = 'D',
220 },
221 {
222 .name = (char *) "client",
223 .has_arg = required_argument,
224 .val = 'C',
225 },
226 {
227 .name = (char *) "remote-config",
228 .has_arg = required_argument,
229 .val = 'R',
230 },
231 {
232 .name = (char *) "cpuclock-test",
233 .has_arg = no_argument,
234 .val = 'T',
235 },
236 {
237 .name = (char *) "crctest",
238 .has_arg = optional_argument,
239 .val = 'G',
240 },
241 {
242 .name = (char *) "idle-prof",
243 .has_arg = required_argument,
244 .val = 'I',
245 },
246 {
247 .name = (char *) "status-interval",
248 .has_arg = required_argument,
249 .val = 'L',
250 },
251 {
252 .name = (char *) "trigger-file",
253 .has_arg = required_argument,
254 .val = 'W',
255 },
256 {
257 .name = (char *) "trigger-timeout",
258 .has_arg = required_argument,
259 .val = 'B',
260 },
261 {
262 .name = (char *) "trigger",
263 .has_arg = required_argument,
264 .val = 'H',
265 },
266 {
267 .name = (char *) "trigger-remote",
268 .has_arg = required_argument,
269 .val = 'J',
270 },
271 {
272 .name = (char *) "aux-path",
273 .has_arg = required_argument,
274 .val = 'K',
275 },
276 {
277 .name = NULL,
278 },
279};
280
281void free_threads_shm(void)
282{
283 if (threads) {
284 void *tp = threads;
285#ifndef CONFIG_NO_SHM
286 struct shmid_ds sbuf;
287
288 threads = NULL;
289 shmdt(tp);
290 shmctl(shm_id, IPC_RMID, &sbuf);
291 shm_id = -1;
292#else
293 threads = NULL;
294 free(tp);
295#endif
296 }
297}
298
299static void free_shm(void)
300{
301 if (threads) {
302 file_hash_exit();
303 flow_exit();
304 fio_debug_jobp = NULL;
305 free_threads_shm();
306 }
307
308 free(trigger_file);
309 free(trigger_cmd);
310 free(trigger_remote_cmd);
311 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
312
313 options_free(fio_options, &def_thread);
314 fio_filelock_exit();
315 scleanup();
316}
317
318/*
319 * The thread area is shared between the main process and the job
320 * threads/processes. So setup a shared memory segment that will hold
321 * all the job info. We use the end of the region for keeping track of
322 * open files across jobs, for file sharing.
323 */
324static int setup_thread_area(void)
325{
326 void *hash;
327
328 if (threads)
329 return 0;
330
331 /*
332 * 1024 is too much on some machines, scale max_jobs if
333 * we get a failure that looks like too large a shm segment
334 */
335 do {
336 size_t size = max_jobs * sizeof(struct thread_data);
337
338 size += file_hash_size;
339 size += sizeof(unsigned int);
340
341#ifndef CONFIG_NO_SHM
342 shm_id = shmget(0, size, IPC_CREAT | 0600);
343 if (shm_id != -1)
344 break;
345 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
346 perror("shmget");
347 break;
348 }
349#else
350 threads = malloc(size);
351 if (threads)
352 break;
353#endif
354
355 max_jobs >>= 1;
356 } while (max_jobs);
357
358#ifndef CONFIG_NO_SHM
359 if (shm_id == -1)
360 return 1;
361
362 threads = shmat(shm_id, NULL, 0);
363 if (threads == (void *) -1) {
364 perror("shmat");
365 return 1;
366 }
367#endif
368
369 memset(threads, 0, max_jobs * sizeof(struct thread_data));
370 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
371 fio_debug_jobp = (void *) hash + file_hash_size;
372 *fio_debug_jobp = -1;
373 file_hash_init(hash);
374
375 flow_init();
376
377 return 0;
378}
379
380static void set_cmd_options(struct thread_data *td)
381{
382 struct thread_options *o = &td->o;
383
384 if (!o->timeout)
385 o->timeout = def_timeout;
386}
387
388/*
389 * Return a free job structure.
390 */
391static struct thread_data *get_new_job(int global, struct thread_data *parent,
392 int preserve_eo, const char *jobname)
393{
394 struct thread_data *td;
395
396 if (global) {
397 set_cmd_options(&def_thread);
398 return &def_thread;
399 }
400 if (setup_thread_area()) {
401 log_err("error: failed to setup shm segment\n");
402 return NULL;
403 }
404 if (thread_number >= max_jobs) {
405 log_err("error: maximum number of jobs (%d) reached.\n",
406 max_jobs);
407 return NULL;
408 }
409
410 td = &threads[thread_number++];
411 *td = *parent;
412
413 td->io_ops = NULL;
414 if (!preserve_eo)
415 td->eo = NULL;
416
417 td->o.uid = td->o.gid = -1U;
418
419 dup_files(td, parent);
420 fio_options_mem_dupe(td);
421
422 profile_add_hooks(td);
423
424 td->thread_number = thread_number;
425 td->subjob_number = 0;
426
427 if (jobname)
428 td->o.name = strdup(jobname);
429
430 if (!parent->o.group_reporting)
431 stat_number++;
432
433 set_cmd_options(td);
434 return td;
435}
436
437static void put_job(struct thread_data *td)
438{
439 if (td == &def_thread)
440 return;
441
442 profile_td_exit(td);
443 flow_exit_job(td);
444
445 if (td->error)
446 log_info("fio: %s\n", td->verror);
447
448 fio_options_free(td);
449 if (td->io_ops)
450 free_ioengine(td);
451
452 if (td->o.name)
453 free(td->o.name);
454
455 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
456 thread_number--;
457}
458
459static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
460{
461 unsigned int bs = td->o.min_bs[ddir];
462
463 assert(ddir_rw(ddir));
464
465 if (td->o.rate[ddir])
466 td->rate_bps[ddir] = td->o.rate[ddir];
467 else
468 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
469
470 if (!td->rate_bps[ddir]) {
471 log_err("rate lower than supported\n");
472 return -1;
473 }
474
475 td->rate_next_io_time[ddir] = 0;
476 td->rate_io_issue_bytes[ddir] = 0;
477 return 0;
478}
479
480static int setup_rate(struct thread_data *td)
481{
482 int ret = 0;
483
484 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
485 ret = __setup_rate(td, DDIR_READ);
486 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
487 ret |= __setup_rate(td, DDIR_WRITE);
488 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
489 ret |= __setup_rate(td, DDIR_TRIM);
490
491 return ret;
492}
493
494static int fixed_block_size(struct thread_options *o)
495{
496 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
497 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
498 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
499 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
500 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
501}
502
503
504static unsigned long long get_rand_start_delay(struct thread_data *td)
505{
506 unsigned long long delayrange;
507 uint64_t frand_max;
508 unsigned long r;
509
510 delayrange = td->o.start_delay_high - td->o.start_delay;
511
512 frand_max = rand_max(&td->delay_state);
513 r = __rand(&td->delay_state);
514 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
515
516 delayrange += td->o.start_delay;
517 return delayrange;
518}
519
520/*
521 * Lazy way of fixing up options that depend on each other. We could also
522 * define option callback handlers, but this is easier.
523 */
524static int fixup_options(struct thread_data *td)
525{
526 struct thread_options *o = &td->o;
527 int ret = 0;
528
529#ifndef FIO_HAVE_PSHARED_MUTEX
530 if (!o->use_thread) {
531 log_info("fio: this platform does not support process shared"
532 " mutexes, forcing use of threads. Use the 'thread'"
533 " option to get rid of this warning.\n");
534 o->use_thread = 1;
535 ret = warnings_fatal;
536 }
537#endif
538
539 if (o->write_iolog_file && o->read_iolog_file) {
540 log_err("fio: read iolog overrides write_iolog\n");
541 free(o->write_iolog_file);
542 o->write_iolog_file = NULL;
543 ret = warnings_fatal;
544 }
545
546 /*
547 * only really works with 1 file
548 */
549 if (o->zone_size && o->open_files > 1)
550 o->zone_size = 0;
551
552 /*
553 * If zone_range isn't specified, backward compatibility dictates it
554 * should be made equal to zone_size.
555 */
556 if (o->zone_size && !o->zone_range)
557 o->zone_range = o->zone_size;
558
559 /*
560 * Reads can do overwrites, we always need to pre-create the file
561 */
562 if (td_read(td) || td_rw(td))
563 o->overwrite = 1;
564
565 if (!o->min_bs[DDIR_READ])
566 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
567 if (!o->max_bs[DDIR_READ])
568 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
569 if (!o->min_bs[DDIR_WRITE])
570 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
571 if (!o->max_bs[DDIR_WRITE])
572 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
573 if (!o->min_bs[DDIR_TRIM])
574 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
575 if (!o->max_bs[DDIR_TRIM])
576 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
577
578 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
579 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
580
581 /*
582 * For random IO, allow blockalign offset other than min_bs.
583 */
584 if (!o->ba[DDIR_READ] || !td_random(td))
585 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
586 if (!o->ba[DDIR_WRITE] || !td_random(td))
587 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
588 if (!o->ba[DDIR_TRIM] || !td_random(td))
589 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
590
591 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
592 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
593 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
594 !o->norandommap) {
595 log_err("fio: Any use of blockalign= turns off randommap\n");
596 o->norandommap = 1;
597 ret = warnings_fatal;
598 }
599
600 if (!o->file_size_high)
601 o->file_size_high = o->file_size_low;
602
603 if (o->start_delay_high)
604 o->start_delay = get_rand_start_delay(td);
605
606 if (o->norandommap && o->verify != VERIFY_NONE
607 && !fixed_block_size(o)) {
608 log_err("fio: norandommap given for variable block sizes, "
609 "verify limited\n");
610 ret = warnings_fatal;
611 }
612 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
613 log_err("fio: bs_unaligned may not work with raw io\n");
614
615 /*
616 * thinktime_spin must be less than thinktime
617 */
618 if (o->thinktime_spin > o->thinktime)
619 o->thinktime_spin = o->thinktime;
620
621 /*
622 * The low water mark cannot be bigger than the iodepth
623 */
624 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
625 o->iodepth_low = o->iodepth;
626
627 /*
628 * If batch number isn't set, default to the same as iodepth
629 */
630 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
631 o->iodepth_batch = o->iodepth;
632
633 if (o->nr_files > td->files_index)
634 o->nr_files = td->files_index;
635
636 if (o->open_files > o->nr_files || !o->open_files)
637 o->open_files = o->nr_files;
638
639 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
640 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
641 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
642 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
643 log_err("fio: rate and rate_iops are mutually exclusive\n");
644 ret = 1;
645 }
646 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
647 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
648 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
649 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
650 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
651 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
652 log_err("fio: minimum rate exceeds rate\n");
653 ret = 1;
654 }
655
656 if (!o->timeout && o->time_based) {
657 log_err("fio: time_based requires a runtime/timeout setting\n");
658 o->time_based = 0;
659 ret = warnings_fatal;
660 }
661
662 if (o->fill_device && !o->size)
663 o->size = -1ULL;
664
665 if (o->verify != VERIFY_NONE) {
666 if (td_write(td) && o->do_verify && o->numjobs > 1) {
667 log_info("Multiple writers may overwrite blocks that "
668 "belong to other jobs. This can cause "
669 "verification failures.\n");
670 ret = warnings_fatal;
671 }
672
673 if (!fio_option_is_set(o, refill_buffers))
674 o->refill_buffers = 1;
675
676 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
677 !o->verify_interval)
678 o->verify_interval = o->min_bs[DDIR_WRITE];
679
680 /*
681 * Verify interval must be smaller or equal to the
682 * write size.
683 */
684 if (o->verify_interval > o->min_bs[DDIR_WRITE])
685 o->verify_interval = o->min_bs[DDIR_WRITE];
686 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
687 o->verify_interval = o->min_bs[DDIR_READ];
688 }
689
690 if (o->pre_read) {
691 o->invalidate_cache = 0;
692 if (td->io_ops->flags & FIO_PIPEIO) {
693 log_info("fio: cannot pre-read files with an IO engine"
694 " that isn't seekable. Pre-read disabled.\n");
695 ret = warnings_fatal;
696 }
697 }
698
699 if (!o->unit_base) {
700 if (td->io_ops->flags & FIO_BIT_BASED)
701 o->unit_base = 1;
702 else
703 o->unit_base = 8;
704 }
705
706#ifndef CONFIG_FDATASYNC
707 if (o->fdatasync_blocks) {
708 log_info("fio: this platform does not support fdatasync()"
709 " falling back to using fsync(). Use the 'fsync'"
710 " option instead of 'fdatasync' to get rid of"
711 " this warning\n");
712 o->fsync_blocks = o->fdatasync_blocks;
713 o->fdatasync_blocks = 0;
714 ret = warnings_fatal;
715 }
716#endif
717
718#ifdef WIN32
719 /*
720 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
721 * so fail if we're passed those flags
722 */
723 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
724 log_err("fio: Windows does not support direct or non-buffered io with"
725 " the synchronous ioengines. Use the 'windowsaio' ioengine"
726 " with 'direct=1' and 'iodepth=1' instead.\n");
727 ret = 1;
728 }
729#endif
730
731 /*
732 * For fully compressible data, just zero them at init time.
733 * It's faster than repeatedly filling it.
734 */
735 if (td->o.compress_percentage == 100) {
736 td->o.zero_buffers = 1;
737 td->o.compress_percentage = 0;
738 }
739
740 /*
741 * Using a non-uniform random distribution excludes usage of
742 * a random map
743 */
744 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
745 td->o.norandommap = 1;
746
747 /*
748 * If size is set but less than the min block size, complain
749 */
750 if (o->size && o->size < td_min_bs(td)) {
751 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
752 ret = 1;
753 }
754
755 /*
756 * O_ATOMIC implies O_DIRECT
757 */
758 if (td->o.oatomic)
759 td->o.odirect = 1;
760
761 /*
762 * If randseed is set, that overrides randrepeat
763 */
764 if (fio_option_is_set(&td->o, rand_seed))
765 td->o.rand_repeatable = 0;
766
767 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
768 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
769 ret = 1;
770 }
771
772 if (fio_option_is_set(o, gtod_cpu)) {
773 fio_gtod_init();
774 fio_gtod_set_cpu(o->gtod_cpu);
775 fio_gtod_offload = 1;
776 }
777
778 td->loops = o->loops;
779 if (!td->loops)
780 td->loops = 1;
781
782 if (td->o.block_error_hist && td->o.nr_files != 1) {
783 log_err("fio: block error histogram only available with "
784 "with a single file per job, but %d files "
785 "provided\n", td->o.nr_files);
786 ret = 1;
787 }
788
789 return ret;
790}
791
792/*
793 * This function leaks the buffer
794 */
795char *fio_uint_to_kmg(unsigned int val)
796{
797 char *buf = malloc(32);
798 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
799 char *p = post;
800
801 do {
802 if (val & 1023)
803 break;
804
805 val >>= 10;
806 p++;
807 } while (*p);
808
809 snprintf(buf, 32, "%u%c", val, *p);
810 return buf;
811}
812
813/* External engines are specified by "external:name.o") */
814static const char *get_engine_name(const char *str)
815{
816 char *p = strstr(str, ":");
817
818 if (!p)
819 return str;
820
821 p++;
822 strip_blank_front(&p);
823 strip_blank_end(p);
824 return p;
825}
826
827static int exists_and_not_file(const char *filename)
828{
829 struct stat sb;
830
831 if (lstat(filename, &sb) == -1)
832 return 0;
833
834 /* \\.\ is the device namespace in Windows, where every file
835 * is a device node */
836 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
837 return 0;
838
839 return 1;
840}
841
842static void td_fill_rand_seeds_internal(struct thread_data *td, int use64)
843{
844 init_rand_seed(&td->bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF], use64);
845 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF], use64);
846 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], use64);
847
848 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
849 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
850
851 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
852 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
853 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
854
855 if (!td_random(td))
856 return;
857
858 if (td->o.rand_repeatable)
859 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
860
861 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
862 init_rand_seed(&td->seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], use64);
863 init_rand_seed(&td->seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], use64);
864 init_rand_seed(&td->seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], use64);
865}
866
867void td_fill_rand_seeds(struct thread_data *td)
868{
869 int use64;
870
871 if (td->o.allrand_repeatable) {
872 unsigned int i;
873
874 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
875 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
876 + i;
877 }
878
879 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
880 use64 = 1;
881 else
882 use64 = 0;
883
884 td_fill_rand_seeds_internal(td, use64);
885
886 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
887 frand_copy(&td->buf_state_prev, &td->buf_state);
888
889 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], use64);
890}
891
892/*
893 * Initializes the ioengine configured for a job, if it has not been done so
894 * already.
895 */
896int ioengine_load(struct thread_data *td)
897{
898 const char *engine;
899
900 /*
901 * Engine has already been loaded.
902 */
903 if (td->io_ops)
904 return 0;
905 if (!td->o.ioengine) {
906 log_err("fio: internal fault, no IO engine specified\n");
907 return 1;
908 }
909
910 engine = get_engine_name(td->o.ioengine);
911 td->io_ops = load_ioengine(td, engine);
912 if (!td->io_ops) {
913 log_err("fio: failed to load engine %s\n", engine);
914 return 1;
915 }
916
917 if (td->io_ops->option_struct_size && td->io_ops->options) {
918 /*
919 * In cases where td->eo is set, clone it for a child thread.
920 * This requires that the parent thread has the same ioengine,
921 * but that requirement must be enforced by the code which
922 * cloned the thread.
923 */
924 void *origeo = td->eo;
925 /*
926 * Otherwise use the default thread options.
927 */
928 if (!origeo && td != &def_thread && def_thread.eo &&
929 def_thread.io_ops->options == td->io_ops->options)
930 origeo = def_thread.eo;
931
932 options_init(td->io_ops->options);
933 td->eo = malloc(td->io_ops->option_struct_size);
934 /*
935 * Use the default thread as an option template if this uses the
936 * same options structure and there are non-default options
937 * used.
938 */
939 if (origeo) {
940 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
941 options_mem_dupe(td->eo, td->io_ops->options);
942 } else {
943 memset(td->eo, 0, td->io_ops->option_struct_size);
944 fill_default_options(td->eo, td->io_ops->options);
945 }
946 *(struct thread_data **)td->eo = td;
947 }
948
949 return 0;
950}
951
952static void init_flags(struct thread_data *td)
953{
954 struct thread_options *o = &td->o;
955
956 if (o->verify_backlog)
957 td->flags |= TD_F_VER_BACKLOG;
958 if (o->trim_backlog)
959 td->flags |= TD_F_TRIM_BACKLOG;
960 if (o->read_iolog_file)
961 td->flags |= TD_F_READ_IOLOG;
962 if (o->refill_buffers)
963 td->flags |= TD_F_REFILL_BUFFERS;
964 /*
965 * Always scramble buffers if asked to
966 */
967 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
968 td->flags |= TD_F_SCRAMBLE_BUFFERS;
969 /*
970 * But also scramble buffers, unless we were explicitly asked
971 * to zero them.
972 */
973 if (o->scramble_buffers && !(o->zero_buffers &&
974 fio_option_is_set(o, zero_buffers)))
975 td->flags |= TD_F_SCRAMBLE_BUFFERS;
976 if (o->verify != VERIFY_NONE)
977 td->flags |= TD_F_VER_NONE;
978
979 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
980 td->flags |= TD_F_NEED_LOCK;
981}
982
983static int setup_random_seeds(struct thread_data *td)
984{
985 unsigned long seed;
986 unsigned int i;
987
988 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed))
989 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
990
991 seed = td->o.rand_seed;
992 for (i = 0; i < 4; i++)
993 seed *= 0x9e370001UL;
994
995 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
996 td->rand_seeds[i] = seed;
997 seed *= 0x9e370001UL;
998 }
999
1000 td_fill_rand_seeds(td);
1001 return 0;
1002}
1003
1004enum {
1005 FPRE_NONE = 0,
1006 FPRE_JOBNAME,
1007 FPRE_JOBNUM,
1008 FPRE_FILENUM
1009};
1010
1011static struct fpre_keyword {
1012 const char *keyword;
1013 size_t strlen;
1014 int key;
1015} fpre_keywords[] = {
1016 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1017 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1018 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1019 { .keyword = NULL, },
1020 };
1021
1022static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
1023 const char *jobname, int jobnum, int filenum)
1024{
1025 struct fpre_keyword *f;
1026 char copy[PATH_MAX];
1027 size_t dst_left = PATH_MAX - 1;
1028
1029 if (!o->filename_format || !strlen(o->filename_format)) {
1030 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
1031 return NULL;
1032 }
1033
1034 for (f = &fpre_keywords[0]; f->keyword; f++)
1035 f->strlen = strlen(f->keyword);
1036
1037 buf[buf_size - 1] = '\0';
1038 strncpy(buf, o->filename_format, buf_size - 1);
1039
1040 memset(copy, 0, sizeof(copy));
1041 for (f = &fpre_keywords[0]; f->keyword; f++) {
1042 do {
1043 size_t pre_len, post_start = 0;
1044 char *str, *dst = copy;
1045
1046 str = strcasestr(buf, f->keyword);
1047 if (!str)
1048 break;
1049
1050 pre_len = str - buf;
1051 if (strlen(str) != f->strlen)
1052 post_start = pre_len + f->strlen;
1053
1054 if (pre_len) {
1055 strncpy(dst, buf, pre_len);
1056 dst += pre_len;
1057 dst_left -= pre_len;
1058 }
1059
1060 switch (f->key) {
1061 case FPRE_JOBNAME: {
1062 int ret;
1063
1064 ret = snprintf(dst, dst_left, "%s", jobname);
1065 if (ret < 0)
1066 break;
1067 else if (ret > dst_left) {
1068 log_err("fio: truncated filename\n");
1069 dst += dst_left;
1070 dst_left = 0;
1071 } else {
1072 dst += ret;
1073 dst_left -= ret;
1074 }
1075 break;
1076 }
1077 case FPRE_JOBNUM: {
1078 int ret;
1079
1080 ret = snprintf(dst, dst_left, "%d", jobnum);
1081 if (ret < 0)
1082 break;
1083 else if (ret > dst_left) {
1084 log_err("fio: truncated filename\n");
1085 dst += dst_left;
1086 dst_left = 0;
1087 } else {
1088 dst += ret;
1089 dst_left -= ret;
1090 }
1091 break;
1092 }
1093 case FPRE_FILENUM: {
1094 int ret;
1095
1096 ret = snprintf(dst, dst_left, "%d", filenum);
1097 if (ret < 0)
1098 break;
1099 else if (ret > dst_left) {
1100 log_err("fio: truncated filename\n");
1101 dst += dst_left;
1102 dst_left = 0;
1103 } else {
1104 dst += ret;
1105 dst_left -= ret;
1106 }
1107 break;
1108 }
1109 default:
1110 assert(0);
1111 break;
1112 }
1113
1114 if (post_start)
1115 strncpy(dst, buf + post_start, dst_left);
1116
1117 strncpy(buf, copy, buf_size - 1);
1118 } while (1);
1119 }
1120
1121 return buf;
1122}
1123
1124int parse_dryrun(void)
1125{
1126 return dump_cmdline || parse_only;
1127}
1128
1129static void gen_log_name(char *name, size_t size, const char *logtype,
1130 const char *logname, unsigned int num,
1131 const char *suf, int per_job)
1132{
1133 if (per_job)
1134 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1135 else
1136 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1137}
1138
1139/*
1140 * Adds a job to the list of things todo. Sanitizes the various options
1141 * to make sure we don't have conflicts, and initializes various
1142 * members of td.
1143 */
1144static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
1145 int recursed, int client_type)
1146{
1147 unsigned int i;
1148 char fname[PATH_MAX];
1149 int numjobs, file_alloced;
1150 struct thread_options *o = &td->o;
1151 char logname[PATH_MAX + 32];
1152
1153 /*
1154 * the def_thread is just for options, it's not a real job
1155 */
1156 if (td == &def_thread)
1157 return 0;
1158
1159 init_flags(td);
1160
1161 /*
1162 * if we are just dumping the output command line, don't add the job
1163 */
1164 if (parse_dryrun()) {
1165 put_job(td);
1166 return 0;
1167 }
1168
1169 td->client_type = client_type;
1170
1171 if (profile_td_init(td))
1172 goto err;
1173
1174 if (ioengine_load(td))
1175 goto err;
1176
1177 if (o->odirect)
1178 td->io_ops->flags |= FIO_RAWIO;
1179
1180 file_alloced = 0;
1181 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1182 file_alloced = 1;
1183
1184 if (o->nr_files == 1 && exists_and_not_file(jobname))
1185 add_file(td, jobname, job_add_num, 0);
1186 else {
1187 for (i = 0; i < o->nr_files; i++)
1188 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
1189 }
1190 }
1191
1192 if (fixup_options(td))
1193 goto err;
1194
1195 flow_init_job(td);
1196
1197 /*
1198 * IO engines only need this for option callbacks, and the address may
1199 * change in subprocesses.
1200 */
1201 if (td->eo)
1202 *(struct thread_data **)td->eo = NULL;
1203
1204 if (td->io_ops->flags & FIO_DISKLESSIO) {
1205 struct fio_file *f;
1206
1207 for_each_file(td, f, i)
1208 f->real_file_size = -1ULL;
1209 }
1210
1211 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1212
1213 td->ts.clat_percentiles = o->clat_percentiles;
1214 td->ts.percentile_precision = o->percentile_precision;
1215 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1216
1217 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1218 td->ts.clat_stat[i].min_val = ULONG_MAX;
1219 td->ts.slat_stat[i].min_val = ULONG_MAX;
1220 td->ts.lat_stat[i].min_val = ULONG_MAX;
1221 td->ts.bw_stat[i].min_val = ULONG_MAX;
1222 }
1223 td->ddir_seq_nr = o->ddir_seq_nr;
1224
1225 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1226 prev_group_jobs = 0;
1227 groupid++;
1228 }
1229
1230 td->groupid = groupid;
1231 prev_group_jobs++;
1232
1233 if (setup_random_seeds(td)) {
1234 td_verror(td, errno, "init_random_state");
1235 goto err;
1236 }
1237
1238 if (setup_rate(td))
1239 goto err;
1240
1241 if (o->lat_log_file) {
1242 struct log_params p = {
1243 .td = td,
1244 .avg_msec = o->log_avg_msec,
1245 .log_type = IO_LOG_TYPE_LAT,
1246 .log_offset = o->log_offset,
1247 .log_gz = o->log_gz,
1248 .log_gz_store = o->log_gz_store,
1249 };
1250 const char *suf;
1251
1252 if (p.log_gz_store)
1253 suf = "log.fz";
1254 else
1255 suf = "log";
1256
1257 gen_log_name(logname, sizeof(logname), "lat", o->lat_log_file,
1258 td->thread_number, suf, o->per_job_logs);
1259 setup_log(&td->lat_log, &p, logname);
1260
1261 gen_log_name(logname, sizeof(logname), "slat", o->lat_log_file,
1262 td->thread_number, suf, o->per_job_logs);
1263 setup_log(&td->slat_log, &p, logname);
1264
1265 gen_log_name(logname, sizeof(logname), "clat", o->lat_log_file,
1266 td->thread_number, suf, o->per_job_logs);
1267 setup_log(&td->clat_log, &p, logname);
1268 }
1269 if (o->bw_log_file) {
1270 struct log_params p = {
1271 .td = td,
1272 .avg_msec = o->log_avg_msec,
1273 .log_type = IO_LOG_TYPE_BW,
1274 .log_offset = o->log_offset,
1275 .log_gz = o->log_gz,
1276 .log_gz_store = o->log_gz_store,
1277 };
1278 const char *suf;
1279
1280 if (p.log_gz_store)
1281 suf = "log.fz";
1282 else
1283 suf = "log";
1284
1285 gen_log_name(logname, sizeof(logname), "bw", o->bw_log_file,
1286 td->thread_number, suf, o->per_job_logs);
1287 setup_log(&td->bw_log, &p, logname);
1288 }
1289 if (o->iops_log_file) {
1290 struct log_params p = {
1291 .td = td,
1292 .avg_msec = o->log_avg_msec,
1293 .log_type = IO_LOG_TYPE_IOPS,
1294 .log_offset = o->log_offset,
1295 .log_gz = o->log_gz,
1296 .log_gz_store = o->log_gz_store,
1297 };
1298 const char *suf;
1299
1300 if (p.log_gz_store)
1301 suf = "log.fz";
1302 else
1303 suf = "log";
1304
1305 gen_log_name(logname, sizeof(logname), "iops", o->iops_log_file,
1306 td->thread_number, suf, o->per_job_logs);
1307 setup_log(&td->iops_log, &p, logname);
1308 }
1309
1310 if (!o->name)
1311 o->name = strdup(jobname);
1312
1313 if (output_format == FIO_OUTPUT_NORMAL) {
1314 if (!job_add_num) {
1315 if (is_backend && !recursed)
1316 fio_server_send_add_job(td);
1317
1318 if (!(td->io_ops->flags & FIO_NOIO)) {
1319 char *c1, *c2, *c3, *c4;
1320 char *c5 = NULL, *c6 = NULL;
1321
1322 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1323 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1324 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1325 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1326
1327 if (!o->bs_is_seq_rand) {
1328 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1329 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1330 }
1331
1332 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1333 td->groupid,
1334 ddir_str(o->td_ddir));
1335
1336 if (o->bs_is_seq_rand)
1337 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1338 c1, c2, c3, c4);
1339 else
1340 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1341 c1, c2, c3, c4, c5, c6);
1342
1343 log_info("ioengine=%s, iodepth=%u\n",
1344 td->io_ops->name, o->iodepth);
1345
1346 free(c1);
1347 free(c2);
1348 free(c3);
1349 free(c4);
1350 free(c5);
1351 free(c6);
1352 }
1353 } else if (job_add_num == 1)
1354 log_info("...\n");
1355 }
1356
1357 /*
1358 * recurse add identical jobs, clear numjobs and stonewall options
1359 * as they don't apply to sub-jobs
1360 */
1361 numjobs = o->numjobs;
1362 while (--numjobs) {
1363 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
1364
1365 if (!td_new)
1366 goto err;
1367
1368 td_new->o.numjobs = 1;
1369 td_new->o.stonewall = 0;
1370 td_new->o.new_group = 0;
1371 td_new->subjob_number = numjobs;
1372
1373 if (file_alloced) {
1374 if (td_new->files) {
1375 struct fio_file *f;
1376 for_each_file(td_new, f, i) {
1377 if (f->file_name)
1378 sfree(f->file_name);
1379 sfree(f);
1380 }
1381 free(td_new->files);
1382 td_new->files = NULL;
1383 }
1384 td_new->files_index = 0;
1385 td_new->files_size = 0;
1386 if (td_new->o.filename) {
1387 free(td_new->o.filename);
1388 td_new->o.filename = NULL;
1389 }
1390 }
1391
1392 if (add_job(td_new, jobname, numjobs, 1, client_type))
1393 goto err;
1394 }
1395
1396 return 0;
1397err:
1398 put_job(td);
1399 return -1;
1400}
1401
1402/*
1403 * Parse as if 'o' was a command line
1404 */
1405void add_job_opts(const char **o, int client_type)
1406{
1407 struct thread_data *td, *td_parent;
1408 int i, in_global = 1;
1409 char jobname[32];
1410
1411 i = 0;
1412 td_parent = td = NULL;
1413 while (o[i]) {
1414 if (!strncmp(o[i], "name", 4)) {
1415 in_global = 0;
1416 if (td)
1417 add_job(td, jobname, 0, 0, client_type);
1418 td = NULL;
1419 sprintf(jobname, "%s", o[i] + 5);
1420 }
1421 if (in_global && !td_parent)
1422 td_parent = get_new_job(1, &def_thread, 0, jobname);
1423 else if (!in_global && !td) {
1424 if (!td_parent)
1425 td_parent = &def_thread;
1426 td = get_new_job(0, td_parent, 0, jobname);
1427 }
1428 if (in_global)
1429 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
1430 else
1431 fio_options_parse(td, (char **) &o[i], 1, 0);
1432 i++;
1433 }
1434
1435 if (td)
1436 add_job(td, jobname, 0, 0, client_type);
1437}
1438
1439static int skip_this_section(const char *name)
1440{
1441 int i;
1442
1443 if (!nr_job_sections)
1444 return 0;
1445 if (!strncmp(name, "global", 6))
1446 return 0;
1447
1448 for (i = 0; i < nr_job_sections; i++)
1449 if (!strcmp(job_sections[i], name))
1450 return 0;
1451
1452 return 1;
1453}
1454
1455static int is_empty_or_comment(char *line)
1456{
1457 unsigned int i;
1458
1459 for (i = 0; i < strlen(line); i++) {
1460 if (line[i] == ';')
1461 return 1;
1462 if (line[i] == '#')
1463 return 1;
1464 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1465 return 0;
1466 }
1467
1468 return 1;
1469}
1470
1471/*
1472 * This is our [ini] type file parser.
1473 */
1474int __parse_jobs_ini(struct thread_data *td,
1475 char *file, int is_buf, int stonewall_flag, int type,
1476 int nested, char *name, char ***popts, int *aopts, int *nopts)
1477{
1478 unsigned int global = 0;
1479 char *string;
1480 FILE *f;
1481 char *p;
1482 int ret = 0, stonewall;
1483 int first_sect = 1;
1484 int skip_fgets = 0;
1485 int inside_skip = 0;
1486 char **opts;
1487 int i, alloc_opts, num_opts;
1488
1489 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1490 assert(td || !nested);
1491
1492 if (is_buf)
1493 f = NULL;
1494 else {
1495 if (!strcmp(file, "-"))
1496 f = stdin;
1497 else
1498 f = fopen(file, "r");
1499
1500 if (!f) {
1501 int __err = errno;
1502
1503 log_err("fio: unable to open '%s' job file\n", file);
1504 if (td)
1505 td_verror(td, __err, "job file open");
1506 return 1;
1507 }
1508 }
1509
1510 string = malloc(4096);
1511
1512 /*
1513 * it's really 256 + small bit, 280 should suffice
1514 */
1515 if (!nested) {
1516 name = malloc(280);
1517 memset(name, 0, 280);
1518 }
1519
1520 opts = NULL;
1521 if (nested && popts) {
1522 opts = *popts;
1523 alloc_opts = *aopts;
1524 num_opts = *nopts;
1525 }
1526
1527 if (!opts) {
1528 alloc_opts = 8;
1529 opts = malloc(sizeof(char *) * alloc_opts);
1530 num_opts = 0;
1531 }
1532
1533 stonewall = stonewall_flag;
1534 do {
1535 /*
1536 * if skip_fgets is set, we already have loaded a line we
1537 * haven't handled.
1538 */
1539 if (!skip_fgets) {
1540 if (is_buf)
1541 p = strsep(&file, "\n");
1542 else
1543 p = fgets(string, 4096, f);
1544 if (!p)
1545 break;
1546 }
1547
1548 skip_fgets = 0;
1549 strip_blank_front(&p);
1550 strip_blank_end(p);
1551
1552 dprint(FD_PARSE, "%s\n", p);
1553 if (is_empty_or_comment(p))
1554 continue;
1555
1556 if (!nested) {
1557 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1558 if (inside_skip)
1559 continue;
1560
1561 log_err("fio: option <%s> outside of "
1562 "[] job section\n", p);
1563 ret = 1;
1564 break;
1565 }
1566
1567 name[strlen(name) - 1] = '\0';
1568
1569 if (skip_this_section(name)) {
1570 inside_skip = 1;
1571 continue;
1572 } else
1573 inside_skip = 0;
1574
1575 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1576
1577 global = !strncmp(name, "global", 6);
1578
1579 if (dump_cmdline) {
1580 if (first_sect)
1581 log_info("fio ");
1582 if (!global)
1583 log_info("--name=%s ", name);
1584 first_sect = 0;
1585 }
1586
1587 td = get_new_job(global, &def_thread, 0, name);
1588 if (!td) {
1589 ret = 1;
1590 break;
1591 }
1592
1593 /*
1594 * Separate multiple job files by a stonewall
1595 */
1596 if (!global && stonewall) {
1597 td->o.stonewall = stonewall;
1598 stonewall = 0;
1599 }
1600
1601 num_opts = 0;
1602 memset(opts, 0, alloc_opts * sizeof(char *));
1603 }
1604 else
1605 skip_fgets = 1;
1606
1607 while (1) {
1608 if (!skip_fgets) {
1609 if (is_buf)
1610 p = strsep(&file, "\n");
1611 else
1612 p = fgets(string, 4096, f);
1613 if (!p)
1614 break;
1615 dprint(FD_PARSE, "%s", p);
1616 }
1617 else
1618 skip_fgets = 0;
1619
1620 if (is_empty_or_comment(p))
1621 continue;
1622
1623 strip_blank_front(&p);
1624
1625 /*
1626 * new section, break out and make sure we don't
1627 * fgets() a new line at the top.
1628 */
1629 if (p[0] == '[') {
1630 if (nested) {
1631 log_err("No new sections in included files\n");
1632 return 1;
1633 }
1634
1635 skip_fgets = 1;
1636 break;
1637 }
1638
1639 strip_blank_end(p);
1640
1641 if (!strncmp(p, "include", strlen("include"))) {
1642 char *filename = p + strlen("include") + 1;
1643
1644 if ((ret = __parse_jobs_ini(td, filename,
1645 is_buf, stonewall_flag, type, 1,
1646 name, &opts, &alloc_opts, &num_opts))) {
1647 log_err("Error %d while parsing include file %s\n",
1648 ret, filename);
1649 break;
1650 }
1651 continue;
1652 }
1653
1654 if (num_opts == alloc_opts) {
1655 alloc_opts <<= 1;
1656 opts = realloc(opts,
1657 alloc_opts * sizeof(char *));
1658 }
1659
1660 opts[num_opts] = strdup(p);
1661 num_opts++;
1662 }
1663
1664 if (nested) {
1665 *popts = opts;
1666 *aopts = alloc_opts;
1667 *nopts = num_opts;
1668 goto out;
1669 }
1670
1671 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1672 if (!ret)
1673 ret = add_job(td, name, 0, 0, type);
1674 else {
1675 log_err("fio: job %s dropped\n", name);
1676 put_job(td);
1677 }
1678
1679 for (i = 0; i < num_opts; i++)
1680 free(opts[i]);
1681 num_opts = 0;
1682 } while (!ret);
1683
1684 if (dump_cmdline)
1685 log_info("\n");
1686
1687 i = 0;
1688 while (i < nr_job_sections) {
1689 free(job_sections[i]);
1690 i++;
1691 }
1692
1693 free(opts);
1694out:
1695 free(string);
1696 if (!nested)
1697 free(name);
1698 if (!is_buf && f != stdin)
1699 fclose(f);
1700 return ret;
1701}
1702
1703int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1704{
1705 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
1706 0, NULL, NULL, NULL, NULL);
1707}
1708
1709static int fill_def_thread(void)
1710{
1711 memset(&def_thread, 0, sizeof(def_thread));
1712
1713 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1714 def_thread.o.error_dump = 1;
1715
1716 /*
1717 * fill default options
1718 */
1719 fio_fill_default_options(&def_thread);
1720 return 0;
1721}
1722
1723static void show_debug_categories(void)
1724{
1725 struct debug_level *dl = &debug_levels[0];
1726 int curlen, first = 1;
1727
1728 curlen = 0;
1729 while (dl->name) {
1730 int has_next = (dl + 1)->name != NULL;
1731
1732 if (first || curlen + strlen(dl->name) >= 80) {
1733 if (!first) {
1734 printf("\n");
1735 curlen = 0;
1736 }
1737 curlen += printf("\t\t\t%s", dl->name);
1738 curlen += 3 * (8 - 1);
1739 if (has_next)
1740 curlen += printf(",");
1741 } else {
1742 curlen += printf("%s", dl->name);
1743 if (has_next)
1744 curlen += printf(",");
1745 }
1746 dl++;
1747 first = 0;
1748 }
1749 printf("\n");
1750}
1751
1752static void usage(const char *name)
1753{
1754 printf("%s\n", fio_version_string);
1755 printf("%s [options] [job options] <job file(s)>\n", name);
1756 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
1757 show_debug_categories();
1758 printf(" --parse-only\t\tParse options only, don't start any IO\n");
1759 printf(" --output\t\tWrite output to file\n");
1760 printf(" --runtime\t\tRuntime in seconds\n");
1761 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1762 printf(" --minimal\t\tMinimal (terse) output\n");
1763 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
1764 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
1765 printf(" --version\t\tPrint version info and exit\n");
1766 printf(" --help\t\tPrint this page\n");
1767 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
1768 printf(" --crctest\t\tTest speed of checksum functions\n");
1769 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
1770 " them\n");
1771 printf(" --enghelp=engine\tPrint ioengine help, or list"
1772 " available ioengines\n");
1773 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1774 " cmd\n");
1775 printf(" --showcmd\t\tTurn a job file into command line options\n");
1776 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1777 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
1778 printf(" --eta-newline=time\tForce a new line for every 'time'");
1779 printf(" period passed\n");
1780 printf(" --status-interval=t\tForce full status dump every");
1781 printf(" 't' period passed\n");
1782 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
1783 " writes\n");
1784 printf(" --section=name\tOnly run specified section in job file\n");
1785 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
1786 " (def 1024)\n");
1787 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1788 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1789 printf(" --server=args\t\tStart a backend fio server\n");
1790 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1791 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
1792 printf(" --remote-config=file\tTell fio server to load this local job file\n");
1793 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1794 "\t\t\t(option=system,percpu) or run unit work\n"
1795 "\t\t\tcalibration only (option=calibrate)\n");
1796#ifdef CONFIG_ZLIB
1797 printf(" --inflate-log=log\tInflate and output compressed log\n");
1798#endif
1799 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
1800 printf(" --trigger-timeout=t\tExecute trigger af this time\n");
1801 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
1802 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
1803 printf(" --aux-path=path\tUse this path for fio state generated files\n");
1804 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1805 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1806 printf("\n Jens Axboe <axboe@fb.com>\n");
1807}
1808
1809#ifdef FIO_INC_DEBUG
1810struct debug_level debug_levels[] = {
1811 { .name = "process",
1812 .help = "Process creation/exit logging",
1813 .shift = FD_PROCESS,
1814 },
1815 { .name = "file",
1816 .help = "File related action logging",
1817 .shift = FD_FILE,
1818 },
1819 { .name = "io",
1820 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1821 .shift = FD_IO,
1822 },
1823 { .name = "mem",
1824 .help = "Memory allocation/freeing logging",
1825 .shift = FD_MEM,
1826 },
1827 { .name = "blktrace",
1828 .help = "blktrace action logging",
1829 .shift = FD_BLKTRACE,
1830 },
1831 { .name = "verify",
1832 .help = "IO verification action logging",
1833 .shift = FD_VERIFY,
1834 },
1835 { .name = "random",
1836 .help = "Random generation logging",
1837 .shift = FD_RANDOM,
1838 },
1839 { .name = "parse",
1840 .help = "Parser logging",
1841 .shift = FD_PARSE,
1842 },
1843 { .name = "diskutil",
1844 .help = "Disk utility logging actions",
1845 .shift = FD_DISKUTIL,
1846 },
1847 { .name = "job",
1848 .help = "Logging related to creating/destroying jobs",
1849 .shift = FD_JOB,
1850 },
1851 { .name = "mutex",
1852 .help = "Mutex logging",
1853 .shift = FD_MUTEX
1854 },
1855 { .name = "profile",
1856 .help = "Logging related to profiles",
1857 .shift = FD_PROFILE,
1858 },
1859 { .name = "time",
1860 .help = "Logging related to time keeping functions",
1861 .shift = FD_TIME,
1862 },
1863 { .name = "net",
1864 .help = "Network logging",
1865 .shift = FD_NET,
1866 },
1867 { .name = "rate",
1868 .help = "Rate logging",
1869 .shift = FD_RATE,
1870 },
1871 { .name = "compress",
1872 .help = "Log compression logging",
1873 .shift = FD_COMPRESS,
1874 },
1875 { .name = NULL, },
1876};
1877
1878static int set_debug(const char *string)
1879{
1880 struct debug_level *dl;
1881 char *p = (char *) string;
1882 char *opt;
1883 int i;
1884
1885 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1886 log_info("fio: dumping debug options:");
1887 for (i = 0; debug_levels[i].name; i++) {
1888 dl = &debug_levels[i];
1889 log_info("%s,", dl->name);
1890 }
1891 log_info("all\n");
1892 return 1;
1893 }
1894
1895 while ((opt = strsep(&p, ",")) != NULL) {
1896 int found = 0;
1897
1898 if (!strncmp(opt, "all", 3)) {
1899 log_info("fio: set all debug options\n");
1900 fio_debug = ~0UL;
1901 continue;
1902 }
1903
1904 for (i = 0; debug_levels[i].name; i++) {
1905 dl = &debug_levels[i];
1906 found = !strncmp(opt, dl->name, strlen(dl->name));
1907 if (!found)
1908 continue;
1909
1910 if (dl->shift == FD_JOB) {
1911 opt = strchr(opt, ':');
1912 if (!opt) {
1913 log_err("fio: missing job number\n");
1914 break;
1915 }
1916 opt++;
1917 fio_debug_jobno = atoi(opt);
1918 log_info("fio: set debug jobno %d\n",
1919 fio_debug_jobno);
1920 } else {
1921 log_info("fio: set debug option %s\n", opt);
1922 fio_debug |= (1UL << dl->shift);
1923 }
1924 break;
1925 }
1926
1927 if (!found)
1928 log_err("fio: debug mask %s not found\n", opt);
1929 }
1930 return 0;
1931}
1932#else
1933static int set_debug(const char *string)
1934{
1935 log_err("fio: debug tracing not included in build\n");
1936 return 1;
1937}
1938#endif
1939
1940static void fio_options_fill_optstring(void)
1941{
1942 char *ostr = cmd_optstr;
1943 int i, c;
1944
1945 c = i = 0;
1946 while (l_opts[i].name) {
1947 ostr[c++] = l_opts[i].val;
1948 if (l_opts[i].has_arg == required_argument)
1949 ostr[c++] = ':';
1950 else if (l_opts[i].has_arg == optional_argument) {
1951 ostr[c++] = ':';
1952 ostr[c++] = ':';
1953 }
1954 i++;
1955 }
1956 ostr[c] = '\0';
1957}
1958
1959static int client_flag_set(char c)
1960{
1961 int i;
1962
1963 i = 0;
1964 while (l_opts[i].name) {
1965 int val = l_opts[i].val;
1966
1967 if (c == (val & 0xff))
1968 return (val & FIO_CLIENT_FLAG);
1969
1970 i++;
1971 }
1972
1973 return 0;
1974}
1975
1976static void parse_cmd_client(void *client, char *opt)
1977{
1978 fio_client_add_cmd_option(client, opt);
1979}
1980
1981static void show_closest_option(const char *name)
1982{
1983 int best_option, best_distance;
1984 int i, distance;
1985
1986 while (*name == '-')
1987 name++;
1988
1989 best_option = -1;
1990 best_distance = INT_MAX;
1991 i = 0;
1992 while (l_opts[i].name) {
1993 distance = string_distance(name, l_opts[i].name);
1994 if (distance < best_distance) {
1995 best_distance = distance;
1996 best_option = i;
1997 }
1998 i++;
1999 }
2000
2001 if (best_option != -1 && string_distance_ok(name, best_distance))
2002 log_err("Did you mean %s?\n", l_opts[best_option].name);
2003}
2004
2005int parse_cmd_line(int argc, char *argv[], int client_type)
2006{
2007 struct thread_data *td = NULL;
2008 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
2009 char *ostr = cmd_optstr;
2010 void *pid_file = NULL;
2011 void *cur_client = NULL;
2012 int backend = 0;
2013
2014 /*
2015 * Reset optind handling, since we may call this multiple times
2016 * for the backend.
2017 */
2018 optind = 1;
2019
2020 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2021 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
2022 parse_cmd_client(cur_client, argv[optind - 1]);
2023 c &= ~FIO_CLIENT_FLAG;
2024 }
2025
2026 switch (c) {
2027 case 'a':
2028 smalloc_pool_size = atoi(optarg);
2029 break;
2030 case 't':
2031 if (check_str_time(optarg, &def_timeout, 1)) {
2032 log_err("fio: failed parsing time %s\n", optarg);
2033 do_exit++;
2034 exit_val = 1;
2035 }
2036 break;
2037 case 'l':
2038 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2039 do_exit++;
2040 exit_val = 1;
2041 break;
2042 case 'b':
2043 write_bw_log = 1;
2044 break;
2045 case 'o':
2046 if (f_out && f_out != stdout)
2047 fclose(f_out);
2048
2049 f_out = fopen(optarg, "w+");
2050 if (!f_out) {
2051 perror("fopen output");
2052 exit(1);
2053 }
2054 f_err = f_out;
2055 break;
2056 case 'm':
2057 output_format = FIO_OUTPUT_TERSE;
2058 break;
2059 case 'F':
2060 if (!optarg) {
2061 log_err("fio: missing --output-format argument\n");
2062 exit_val = 1;
2063 do_exit++;
2064 break;
2065 }
2066 if (!strcmp(optarg, "minimal") ||
2067 !strcmp(optarg, "terse") ||
2068 !strcmp(optarg, "csv"))
2069 output_format = FIO_OUTPUT_TERSE;
2070 else if (!strcmp(optarg, "json"))
2071 output_format = FIO_OUTPUT_JSON;
2072 else
2073 output_format = FIO_OUTPUT_NORMAL;
2074 break;
2075 case 'f':
2076 append_terse_output = 1;
2077 break;
2078 case 'h':
2079 did_arg = 1;
2080 if (!cur_client) {
2081 usage(argv[0]);
2082 do_exit++;
2083 }
2084 break;
2085 case 'c':
2086 did_arg = 1;
2087 if (!cur_client) {
2088 fio_show_option_help(optarg);
2089 do_exit++;
2090 }
2091 break;
2092 case 'i':
2093 did_arg = 1;
2094 if (!cur_client) {
2095 fio_show_ioengine_help(optarg);
2096 do_exit++;
2097 }
2098 break;
2099 case 's':
2100 did_arg = 1;
2101 dump_cmdline = 1;
2102 break;
2103 case 'r':
2104 read_only = 1;
2105 break;
2106 case 'v':
2107 did_arg = 1;
2108 if (!cur_client) {
2109 log_info("%s\n", fio_version_string);
2110 do_exit++;
2111 }
2112 break;
2113 case 'V':
2114 terse_version = atoi(optarg);
2115 if (!(terse_version == 2 || terse_version == 3 ||
2116 terse_version == 4)) {
2117 log_err("fio: bad terse version format\n");
2118 exit_val = 1;
2119 do_exit++;
2120 }
2121 break;
2122 case 'e':
2123 if (!strcmp("always", optarg))
2124 eta_print = FIO_ETA_ALWAYS;
2125 else if (!strcmp("never", optarg))
2126 eta_print = FIO_ETA_NEVER;
2127 break;
2128 case 'E': {
2129 long long t = 0;
2130
2131 if (check_str_time(optarg, &t, 1)) {
2132 log_err("fio: failed parsing eta time %s\n", optarg);
2133 exit_val = 1;
2134 do_exit++;
2135 }
2136 eta_new_line = t / 1000;
2137 break;
2138 }
2139 case 'd':
2140 if (set_debug(optarg))
2141 do_exit++;
2142 break;
2143 case 'P':
2144 did_arg = 1;
2145 parse_only = 1;
2146 break;
2147 case 'x': {
2148 size_t new_size;
2149
2150 if (!strcmp(optarg, "global")) {
2151 log_err("fio: can't use global as only "
2152 "section\n");
2153 do_exit++;
2154 exit_val = 1;
2155 break;
2156 }
2157 new_size = (nr_job_sections + 1) * sizeof(char *);
2158 job_sections = realloc(job_sections, new_size);
2159 job_sections[nr_job_sections] = strdup(optarg);
2160 nr_job_sections++;
2161 break;
2162 }
2163#ifdef CONFIG_ZLIB
2164 case 'X':
2165 exit_val = iolog_file_inflate(optarg);
2166 did_arg++;
2167 do_exit++;
2168 break;
2169#endif
2170 case 'p':
2171 did_arg = 1;
2172 if (exec_profile)
2173 free(exec_profile);
2174 exec_profile = strdup(optarg);
2175 break;
2176 case FIO_GETOPT_JOB: {
2177 const char *opt = l_opts[lidx].name;
2178 char *val = optarg;
2179
2180 if (!strncmp(opt, "name", 4) && td) {
2181 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2182 if (ret)
2183 goto out_free;
2184 td = NULL;
2185 did_arg = 1;
2186 }
2187 if (!td) {
2188 int is_section = !strncmp(opt, "name", 4);
2189 int global = 0;
2190
2191 if (!is_section || !strncmp(val, "global", 6))
2192 global = 1;
2193
2194 if (is_section && skip_this_section(val))
2195 continue;
2196
2197 td = get_new_job(global, &def_thread, 1, NULL);
2198 if (!td || ioengine_load(td)) {
2199 if (td) {
2200 put_job(td);
2201 td = NULL;
2202 }
2203 do_exit++;
2204 exit_val = 1;
2205 break;
2206 }
2207 fio_options_set_ioengine_opts(l_opts, td);
2208 }
2209
2210 if ((!val || !strlen(val)) &&
2211 l_opts[lidx].has_arg == required_argument) {
2212 log_err("fio: option %s requires an argument\n", opt);
2213 ret = 1;
2214 } else
2215 ret = fio_cmd_option_parse(td, opt, val);
2216
2217 if (ret) {
2218 if (td) {
2219 put_job(td);
2220 td = NULL;
2221 }
2222 do_exit++;
2223 exit_val = 1;
2224 }
2225
2226 if (!ret && !strcmp(opt, "ioengine")) {
2227 free_ioengine(td);
2228 if (ioengine_load(td)) {
2229 put_job(td);
2230 td = NULL;
2231 do_exit++;
2232 exit_val = 1;
2233 break;
2234 }
2235 fio_options_set_ioengine_opts(l_opts, td);
2236 }
2237 break;
2238 }
2239 case FIO_GETOPT_IOENGINE: {
2240 const char *opt = l_opts[lidx].name;
2241 char *val = optarg;
2242
2243 if (!td)
2244 break;
2245
2246 ret = fio_cmd_ioengine_option_parse(td, opt, val);
2247 break;
2248 }
2249 case 'w':
2250 warnings_fatal = 1;
2251 break;
2252 case 'j':
2253 max_jobs = atoi(optarg);
2254 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2255 log_err("fio: invalid max jobs: %d\n", max_jobs);
2256 do_exit++;
2257 exit_val = 1;
2258 }
2259 break;
2260 case 'S':
2261 did_arg = 1;
2262#ifndef CONFIG_NO_SHM
2263 if (nr_clients) {
2264 log_err("fio: can't be both client and server\n");
2265 do_exit++;
2266 exit_val = 1;
2267 break;
2268 }
2269 if (optarg)
2270 fio_server_set_arg(optarg);
2271 is_backend = 1;
2272 backend = 1;
2273#else
2274 log_err("fio: client/server requires SHM support\n");
2275 do_exit++;
2276 exit_val = 1;
2277#endif
2278 break;
2279 case 'D':
2280 if (pid_file)
2281 free(pid_file);
2282 pid_file = strdup(optarg);
2283 break;
2284 case 'I':
2285 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2286 /* exit on error and calibration only */
2287 did_arg = 1;
2288 do_exit++;
2289 if (ret == -1)
2290 exit_val = 1;
2291 }
2292 break;
2293 case 'C':
2294 did_arg = 1;
2295 if (is_backend) {
2296 log_err("fio: can't be both client and server\n");
2297 do_exit++;
2298 exit_val = 1;
2299 break;
2300 }
2301 /* if --client parameter contains a pathname */
2302 if (0 == access(optarg, R_OK)) {
2303 /* file contains a list of host addrs or names */
2304 char hostaddr[PATH_MAX] = {0};
2305 char formatstr[8];
2306 FILE * hostf = fopen(optarg, "r");
2307 if (!hostf) {
2308 log_err("fio: could not open client list file %s for read\n", optarg);
2309 do_exit++;
2310 exit_val = 1;
2311 break;
2312 }
2313 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2314 /*
2315 * read at most PATH_MAX-1 chars from each
2316 * record in this file
2317 */
2318 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2319 /* expect EVERY host in file to be valid */
2320 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2321 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2322 do_exit++;
2323 exit_val = 1;
2324 break;
2325 }
2326 }
2327 fclose(hostf);
2328 break; /* no possibility of job file for "this client only" */
2329 }
2330 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
2331 log_err("fio: failed adding client %s\n", optarg);
2332 do_exit++;
2333 exit_val = 1;
2334 break;
2335 }
2336 /*
2337 * If the next argument exists and isn't an option,
2338 * assume it's a job file for this client only.
2339 */
2340 while (optind < argc) {
2341 if (!strncmp(argv[optind], "--", 2) ||
2342 !strncmp(argv[optind], "-", 1))
2343 break;
2344
2345 if (fio_client_add_ini_file(cur_client, argv[optind], 0))
2346 break;
2347 optind++;
2348 }
2349 break;
2350 case 'R':
2351 did_arg = 1;
2352 if (fio_client_add_ini_file(cur_client, optarg, 1)) {
2353 do_exit++;
2354 exit_val = 1;
2355 }
2356 break;
2357 case 'T':
2358 did_arg = 1;
2359 do_exit++;
2360 exit_val = fio_monotonic_clocktest(1);
2361 break;
2362 case 'G':
2363 did_arg = 1;
2364 do_exit++;
2365 exit_val = fio_crctest(optarg);
2366 break;
2367 case 'L': {
2368 long long val;
2369
2370 if (check_str_time(optarg, &val, 1)) {
2371 log_err("fio: failed parsing time %s\n", optarg);
2372 do_exit++;
2373 exit_val = 1;
2374 break;
2375 }
2376 status_interval = val / 1000;
2377 break;
2378 }
2379 case 'W':
2380 if (trigger_file)
2381 free(trigger_file);
2382 trigger_file = strdup(optarg);
2383 break;
2384 case 'H':
2385 if (trigger_cmd)
2386 free(trigger_cmd);
2387 trigger_cmd = strdup(optarg);
2388 break;
2389 case 'J':
2390 if (trigger_remote_cmd)
2391 free(trigger_remote_cmd);
2392 trigger_remote_cmd = strdup(optarg);
2393 break;
2394 case 'K':
2395 if (aux_path)
2396 free(aux_path);
2397 aux_path = strdup(optarg);
2398 break;
2399 case 'B':
2400 if (check_str_time(optarg, &trigger_timeout, 1)) {
2401 log_err("fio: failed parsing time %s\n", optarg);
2402 do_exit++;
2403 exit_val = 1;
2404 }
2405 trigger_timeout /= 1000000;
2406 break;
2407 case '?':
2408 log_err("%s: unrecognized option '%s'\n", argv[0],
2409 argv[optind - 1]);
2410 show_closest_option(argv[optind - 1]);
2411 default:
2412 do_exit++;
2413 exit_val = 1;
2414 break;
2415 }
2416 if (do_exit)
2417 break;
2418 }
2419
2420 if (do_exit && !(is_backend || nr_clients))
2421 exit(exit_val);
2422
2423 if (nr_clients && fio_clients_connect())
2424 exit(1);
2425
2426 if (is_backend && backend)
2427 return fio_start_server(pid_file);
2428 else if (pid_file)
2429 free(pid_file);
2430
2431 if (td) {
2432 if (!ret) {
2433 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2434 if (ret)
2435 did_arg = 1;
2436 }
2437 }
2438
2439 while (!ret && optind < argc) {
2440 ini_idx++;
2441 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2442 ini_file[ini_idx - 1] = strdup(argv[optind]);
2443 optind++;
2444 }
2445
2446out_free:
2447 if (pid_file)
2448 free(pid_file);
2449
2450 return ini_idx;
2451}
2452
2453int fio_init_options(void)
2454{
2455 f_out = stdout;
2456 f_err = stderr;
2457
2458 fio_options_fill_optstring();
2459 fio_options_dup_and_init(l_opts);
2460
2461 atexit(free_shm);
2462
2463 if (fill_def_thread())
2464 return 1;
2465
2466 return 0;
2467}
2468
2469extern int fio_check_options(struct thread_options *);
2470
2471int parse_options(int argc, char *argv[])
2472{
2473 const int type = FIO_CLIENT_TYPE_CLI;
2474 int job_files, i;
2475
2476 if (fio_init_options())
2477 return 1;
2478 if (fio_test_cconv(&def_thread.o))
2479 log_err("fio: failed internal cconv test\n");
2480
2481 job_files = parse_cmd_line(argc, argv, type);
2482
2483 if (job_files > 0) {
2484 for (i = 0; i < job_files; i++) {
2485 if (i && fill_def_thread())
2486 return 1;
2487 if (nr_clients) {
2488 if (fio_clients_send_ini(ini_file[i]))
2489 return 1;
2490 free(ini_file[i]);
2491 } else if (!is_backend) {
2492 if (parse_jobs_ini(ini_file[i], 0, i, type))
2493 return 1;
2494 free(ini_file[i]);
2495 }
2496 }
2497 } else if (nr_clients) {
2498 if (fill_def_thread())
2499 return 1;
2500 if (fio_clients_send_ini(NULL))
2501 return 1;
2502 }
2503
2504 free(ini_file);
2505 fio_options_free(&def_thread);
2506 filesetup_mem_free();
2507
2508 if (!thread_number) {
2509 if (parse_dryrun())
2510 return 0;
2511 if (exec_profile)
2512 return 0;
2513 if (is_backend || nr_clients)
2514 return 0;
2515 if (did_arg)
2516 return 0;
2517
2518 log_err("No jobs(s) defined\n\n");
2519
2520 if (!did_arg) {
2521 usage(argv[0]);
2522 return 1;
2523 }
2524
2525 return 0;
2526 }
2527
2528 if (output_format == FIO_OUTPUT_NORMAL)
2529 log_info("%s\n", fio_version_string);
2530
2531 return 0;
2532}
2533
2534void options_default_fill(struct thread_options *o)
2535{
2536 memcpy(o, &def_thread.o, sizeof(*o));
2537}