2 * This file contains job initialization and setup functions.
12 #include <sys/types.h>
16 #ifndef FIO_NO_HAVE_SHM_H
29 #include "oslib/getopt.h"
30 #include "oslib/strcasestr.h"
34 const char fio_version_string[] = FIO_VERSION;
36 #define FIO_RANDSEED (0xb1899bedUL)
38 static char **ini_file;
39 static int max_jobs = FIO_MAX_JOBS;
40 static int dump_cmdline;
41 static long long def_timeout;
42 static int parse_only;
44 static struct thread_data def_thread;
45 struct thread_data *threads = NULL;
46 static char **job_sections;
47 static int nr_job_sections;
49 int exitall_on_terminate = 0;
50 int output_format = FIO_OUTPUT_NORMAL;
51 int eta_print = FIO_ETA_AUTO;
55 char *exec_profile = NULL;
56 int warnings_fatal = 0;
57 int terse_version = 3;
64 int status_interval = 0;
66 char *trigger_file = NULL;
67 long long trigger_timeout = 0;
68 char *trigger_cmd = NULL;
69 char *trigger_remote_cmd = NULL;
71 char *aux_path = NULL;
73 static int prev_group_jobs;
75 unsigned long fio_debug = 0;
76 unsigned int fio_debug_jobno = -1;
77 unsigned int *fio_debug_jobp = NULL;
79 static char cmd_optstr[256];
82 #define FIO_CLIENT_FLAG (1 << 16)
85 * Command line options. These will contain the above, plus a few
86 * extra that only pertain to fio itself and not jobs.
88 static struct option l_opts[FIO_NR_OPTIONS] = {
90 .name = (char *) "output",
91 .has_arg = required_argument,
92 .val = 'o' | FIO_CLIENT_FLAG,
95 .name = (char *) "timeout",
96 .has_arg = required_argument,
97 .val = 't' | FIO_CLIENT_FLAG,
100 .name = (char *) "latency-log",
101 .has_arg = required_argument,
102 .val = 'l' | FIO_CLIENT_FLAG,
105 .name = (char *) "bandwidth-log",
106 .has_arg = required_argument,
107 .val = 'b' | FIO_CLIENT_FLAG,
110 .name = (char *) "minimal",
111 .has_arg = no_argument,
112 .val = 'm' | FIO_CLIENT_FLAG,
115 .name = (char *) "output-format",
116 .has_arg = required_argument,
117 .val = 'F' | FIO_CLIENT_FLAG,
120 .name = (char *) "append-terse",
121 .has_arg = optional_argument,
125 .name = (char *) "version",
126 .has_arg = no_argument,
127 .val = 'v' | FIO_CLIENT_FLAG,
130 .name = (char *) "help",
131 .has_arg = no_argument,
132 .val = 'h' | FIO_CLIENT_FLAG,
135 .name = (char *) "cmdhelp",
136 .has_arg = optional_argument,
137 .val = 'c' | FIO_CLIENT_FLAG,
140 .name = (char *) "enghelp",
141 .has_arg = optional_argument,
142 .val = 'i' | FIO_CLIENT_FLAG,
145 .name = (char *) "showcmd",
146 .has_arg = no_argument,
147 .val = 's' | FIO_CLIENT_FLAG,
150 .name = (char *) "readonly",
151 .has_arg = no_argument,
152 .val = 'r' | FIO_CLIENT_FLAG,
155 .name = (char *) "eta",
156 .has_arg = required_argument,
157 .val = 'e' | FIO_CLIENT_FLAG,
160 .name = (char *) "eta-newline",
161 .has_arg = required_argument,
162 .val = 'E' | FIO_CLIENT_FLAG,
165 .name = (char *) "debug",
166 .has_arg = required_argument,
167 .val = 'd' | FIO_CLIENT_FLAG,
170 .name = (char *) "parse-only",
171 .has_arg = no_argument,
172 .val = 'P' | FIO_CLIENT_FLAG,
175 .name = (char *) "section",
176 .has_arg = required_argument,
177 .val = 'x' | FIO_CLIENT_FLAG,
181 .name = (char *) "inflate-log",
182 .has_arg = required_argument,
183 .val = 'X' | FIO_CLIENT_FLAG,
187 .name = (char *) "alloc-size",
188 .has_arg = required_argument,
189 .val = 'a' | FIO_CLIENT_FLAG,
192 .name = (char *) "profile",
193 .has_arg = required_argument,
194 .val = 'p' | FIO_CLIENT_FLAG,
197 .name = (char *) "warnings-fatal",
198 .has_arg = no_argument,
199 .val = 'w' | FIO_CLIENT_FLAG,
202 .name = (char *) "max-jobs",
203 .has_arg = required_argument,
204 .val = 'j' | FIO_CLIENT_FLAG,
207 .name = (char *) "terse-version",
208 .has_arg = required_argument,
209 .val = 'V' | FIO_CLIENT_FLAG,
212 .name = (char *) "server",
213 .has_arg = optional_argument,
216 { .name = (char *) "daemonize",
217 .has_arg = required_argument,
221 .name = (char *) "client",
222 .has_arg = required_argument,
226 .name = (char *) "remote-config",
227 .has_arg = required_argument,
231 .name = (char *) "cpuclock-test",
232 .has_arg = no_argument,
236 .name = (char *) "crctest",
237 .has_arg = optional_argument,
241 .name = (char *) "idle-prof",
242 .has_arg = required_argument,
246 .name = (char *) "status-interval",
247 .has_arg = required_argument,
251 .name = (char *) "trigger-file",
252 .has_arg = required_argument,
256 .name = (char *) "trigger-timeout",
257 .has_arg = required_argument,
261 .name = (char *) "trigger",
262 .has_arg = required_argument,
266 .name = (char *) "trigger-remote",
267 .has_arg = required_argument,
271 .name = (char *) "aux-path",
272 .has_arg = required_argument,
280 void free_threads_shm(void)
284 #ifndef CONFIG_NO_SHM
285 struct shmid_ds sbuf;
289 shmctl(shm_id, IPC_RMID, &sbuf);
298 static void free_shm(void)
303 fio_debug_jobp = NULL;
309 free(trigger_remote_cmd);
310 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
312 options_free(fio_options, &def_thread.o);
318 * The thread area is shared between the main process and the job
319 * threads/processes. So setup a shared memory segment that will hold
320 * all the job info. We use the end of the region for keeping track of
321 * open files across jobs, for file sharing.
323 static int setup_thread_area(void)
331 * 1024 is too much on some machines, scale max_jobs if
332 * we get a failure that looks like too large a shm segment
335 size_t size = max_jobs * sizeof(struct thread_data);
337 size += sizeof(unsigned int);
339 #ifndef CONFIG_NO_SHM
340 shm_id = shmget(0, size, IPC_CREAT | 0600);
343 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
348 threads = malloc(size);
356 #ifndef CONFIG_NO_SHM
360 threads = shmat(shm_id, NULL, 0);
361 if (threads == (void *) -1) {
367 memset(threads, 0, max_jobs * sizeof(struct thread_data));
368 fio_debug_jobp = (void *) threads + max_jobs * sizeof(struct thread_data);
369 *fio_debug_jobp = -1;
377 static void set_cmd_options(struct thread_data *td)
379 struct thread_options *o = &td->o;
382 o->timeout = def_timeout;
385 static void dump_print_option(struct print_option *p)
389 if (!strcmp("description", p->name))
394 log_info("--%s%s", p->name, p->value ? "" : " ");
396 log_info("=%s%s%s ", delim, p->value, delim);
399 static void dump_opt_list(struct thread_data *td)
401 struct flist_head *entry;
402 struct print_option *p;
404 if (flist_empty(&td->opt_list))
407 flist_for_each(entry, &td->opt_list) {
408 p = flist_entry(entry, struct print_option, list);
409 dump_print_option(p);
413 static void fio_dump_options_free(struct thread_data *td)
415 while (!flist_empty(&td->opt_list)) {
416 struct print_option *p;
418 p = flist_first_entry(&td->opt_list, struct print_option, list);
419 flist_del_init(&p->list);
426 static void copy_opt_list(struct thread_data *dst, struct thread_data *src)
428 struct flist_head *entry;
430 if (flist_empty(&src->opt_list))
433 flist_for_each(entry, &src->opt_list) {
434 struct print_option *srcp, *dstp;
436 srcp = flist_entry(entry, struct print_option, list);
437 dstp = malloc(sizeof(*dstp));
438 dstp->name = strdup(srcp->name);
440 dstp->value = strdup(srcp->value);
443 flist_add_tail(&dstp->list, &dst->opt_list);
448 * Return a free job structure.
450 static struct thread_data *get_new_job(int global, struct thread_data *parent,
451 int preserve_eo, const char *jobname)
453 struct thread_data *td;
456 set_cmd_options(&def_thread);
459 if (setup_thread_area()) {
460 log_err("error: failed to setup shm segment\n");
463 if (thread_number >= max_jobs) {
464 log_err("error: maximum number of jobs (%d) reached.\n",
469 td = &threads[thread_number++];
472 INIT_FLIST_HEAD(&td->opt_list);
473 if (parent != &def_thread)
474 copy_opt_list(td, parent);
480 td->o.uid = td->o.gid = -1U;
482 dup_files(td, parent);
483 fio_options_mem_dupe(td);
485 profile_add_hooks(td);
487 td->thread_number = thread_number;
488 td->subjob_number = 0;
491 td->o.name = strdup(jobname);
493 if (!parent->o.group_reporting || parent == &def_thread)
500 static void put_job(struct thread_data *td)
502 if (td == &def_thread)
509 log_info("fio: %s\n", td->verror);
511 fio_options_free(td);
512 fio_dump_options_free(td);
519 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
523 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
525 unsigned int bs = td->o.min_bs[ddir];
527 assert(ddir_rw(ddir));
529 if (td->o.rate[ddir])
530 td->rate_bps[ddir] = td->o.rate[ddir];
532 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
534 if (!td->rate_bps[ddir]) {
535 log_err("rate lower than supported\n");
539 td->rate_next_io_time[ddir] = 0;
540 td->rate_io_issue_bytes[ddir] = 0;
545 static int setup_rate(struct thread_data *td)
549 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
550 ret = __setup_rate(td, DDIR_READ);
551 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
552 ret |= __setup_rate(td, DDIR_WRITE);
553 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
554 ret |= __setup_rate(td, DDIR_TRIM);
559 static int fixed_block_size(struct thread_options *o)
561 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
562 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
563 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
564 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
565 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
569 static unsigned long long get_rand_start_delay(struct thread_data *td)
571 unsigned long long delayrange;
575 delayrange = td->o.start_delay_high - td->o.start_delay;
577 frand_max = rand_max(&td->delay_state);
578 r = __rand(&td->delay_state);
579 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
581 delayrange += td->o.start_delay;
586 * Lazy way of fixing up options that depend on each other. We could also
587 * define option callback handlers, but this is easier.
589 static int fixup_options(struct thread_data *td)
591 struct thread_options *o = &td->o;
594 #ifndef FIO_HAVE_PSHARED_MUTEX
595 if (!o->use_thread) {
596 log_info("fio: this platform does not support process shared"
597 " mutexes, forcing use of threads. Use the 'thread'"
598 " option to get rid of this warning.\n");
600 ret = warnings_fatal;
604 if (o->write_iolog_file && o->read_iolog_file) {
605 log_err("fio: read iolog overrides write_iolog\n");
606 free(o->write_iolog_file);
607 o->write_iolog_file = NULL;
608 ret = warnings_fatal;
612 * only really works with 1 file
614 if (o->zone_size && o->open_files > 1)
618 * If zone_range isn't specified, backward compatibility dictates it
619 * should be made equal to zone_size.
621 if (o->zone_size && !o->zone_range)
622 o->zone_range = o->zone_size;
625 * Reads can do overwrites, we always need to pre-create the file
627 if (td_read(td) || td_rw(td))
630 if (!o->min_bs[DDIR_READ])
631 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
632 if (!o->max_bs[DDIR_READ])
633 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
634 if (!o->min_bs[DDIR_WRITE])
635 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
636 if (!o->max_bs[DDIR_WRITE])
637 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
638 if (!o->min_bs[DDIR_TRIM])
639 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
640 if (!o->max_bs[DDIR_TRIM])
641 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
643 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
644 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
647 * For random IO, allow blockalign offset other than min_bs.
649 if (!o->ba[DDIR_READ] || !td_random(td))
650 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
651 if (!o->ba[DDIR_WRITE] || !td_random(td))
652 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
653 if (!o->ba[DDIR_TRIM] || !td_random(td))
654 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
656 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
657 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
658 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
660 log_err("fio: Any use of blockalign= turns off randommap\n");
662 ret = warnings_fatal;
665 if (!o->file_size_high)
666 o->file_size_high = o->file_size_low;
668 if (o->start_delay_high)
669 o->start_delay = get_rand_start_delay(td);
671 if (o->norandommap && o->verify != VERIFY_NONE
672 && !fixed_block_size(o)) {
673 log_err("fio: norandommap given for variable block sizes, "
675 ret = warnings_fatal;
677 if (o->bs_unaligned && (o->odirect || td_ioengine_flagged(td, FIO_RAWIO)))
678 log_err("fio: bs_unaligned may not work with raw io\n");
681 * thinktime_spin must be less than thinktime
683 if (o->thinktime_spin > o->thinktime)
684 o->thinktime_spin = o->thinktime;
687 * The low water mark cannot be bigger than the iodepth
689 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
690 o->iodepth_low = o->iodepth;
693 * If batch number isn't set, default to the same as iodepth
695 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
696 o->iodepth_batch = o->iodepth;
699 * If max batch complete number isn't set or set incorrectly,
700 * default to the same as iodepth_batch_complete_min
702 if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
703 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
705 if (o->nr_files > td->files_index)
706 o->nr_files = td->files_index;
708 if (o->open_files > o->nr_files || !o->open_files)
709 o->open_files = o->nr_files;
711 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
712 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
713 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
714 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
715 log_err("fio: rate and rate_iops are mutually exclusive\n");
718 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
719 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
720 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
721 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
722 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
723 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
724 log_err("fio: minimum rate exceeds rate\n");
728 if (!o->timeout && o->time_based) {
729 log_err("fio: time_based requires a runtime/timeout setting\n");
731 ret = warnings_fatal;
734 if (o->fill_device && !o->size)
737 if (o->verify != VERIFY_NONE) {
738 if (td_write(td) && o->do_verify && o->numjobs > 1) {
739 log_info("Multiple writers may overwrite blocks that "
740 "belong to other jobs. This can cause "
741 "verification failures.\n");
742 ret = warnings_fatal;
745 if (!fio_option_is_set(o, refill_buffers))
746 o->refill_buffers = 1;
748 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
750 o->verify_interval = o->min_bs[DDIR_WRITE];
753 * Verify interval must be smaller or equal to the
756 if (o->verify_interval > o->min_bs[DDIR_WRITE])
757 o->verify_interval = o->min_bs[DDIR_WRITE];
758 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
759 o->verify_interval = o->min_bs[DDIR_READ];
763 o->invalidate_cache = 0;
764 if (td_ioengine_flagged(td, FIO_PIPEIO)) {
765 log_info("fio: cannot pre-read files with an IO engine"
766 " that isn't seekable. Pre-read disabled.\n");
767 ret = warnings_fatal;
772 if (td_ioengine_flagged(td, FIO_BIT_BASED))
778 #ifndef CONFIG_FDATASYNC
779 if (o->fdatasync_blocks) {
780 log_info("fio: this platform does not support fdatasync()"
781 " falling back to using fsync(). Use the 'fsync'"
782 " option instead of 'fdatasync' to get rid of"
784 o->fsync_blocks = o->fdatasync_blocks;
785 o->fdatasync_blocks = 0;
786 ret = warnings_fatal;
792 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
793 * so fail if we're passed those flags
795 if (td_ioengine_flagged(td, FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
796 log_err("fio: Windows does not support direct or non-buffered io with"
797 " the synchronous ioengines. Use the 'windowsaio' ioengine"
798 " with 'direct=1' and 'iodepth=1' instead.\n");
804 * For fully compressible data, just zero them at init time.
805 * It's faster than repeatedly filling it. For non-zero
806 * compression, we should have refill_buffers set. Set it, unless
807 * the job file already changed it.
809 if (o->compress_percentage) {
810 if (o->compress_percentage == 100) {
812 o->compress_percentage = 0;
813 } else if (!fio_option_is_set(o, refill_buffers))
814 o->refill_buffers = 1;
818 * Using a non-uniform random distribution excludes usage of
821 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
822 td->o.norandommap = 1;
825 * If size is set but less than the min block size, complain
827 if (o->size && o->size < td_min_bs(td)) {
828 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
833 * O_ATOMIC implies O_DIRECT
839 * If randseed is set, that overrides randrepeat
841 if (fio_option_is_set(&td->o, rand_seed))
842 td->o.rand_repeatable = 0;
844 if (td_ioengine_flagged(td, FIO_NOEXTEND) && td->o.file_append) {
845 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
849 if (fio_option_is_set(o, gtod_cpu)) {
851 fio_gtod_set_cpu(o->gtod_cpu);
852 fio_gtod_offload = 1;
855 td->loops = o->loops;
859 if (td->o.block_error_hist && td->o.nr_files != 1) {
860 log_err("fio: block error histogram only available "
861 "with a single file per job, but %d files "
862 "provided\n", td->o.nr_files);
870 * This function leaks the buffer
872 char *fio_uint_to_kmg(unsigned int val)
874 char *buf = malloc(32);
875 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
886 snprintf(buf, 32, "%u%c", val, *p);
890 /* External engines are specified by "external:name.o") */
891 static const char *get_engine_name(const char *str)
893 char *p = strstr(str, ":");
899 strip_blank_front(&p);
904 static void init_rand_file_service(struct thread_data *td)
906 unsigned long nranges = td->o.nr_files << FIO_FSERVICE_SHIFT;
907 const unsigned int seed = td->rand_seeds[FIO_RAND_FILE_OFF];
909 if (td->o.file_service_type == FIO_FSERVICE_ZIPF) {
910 zipf_init(&td->next_file_zipf, nranges, td->zipf_theta, seed);
911 zipf_disable_hash(&td->next_file_zipf);
912 } else if (td->o.file_service_type == FIO_FSERVICE_PARETO) {
913 pareto_init(&td->next_file_zipf, nranges, td->pareto_h, seed);
914 zipf_disable_hash(&td->next_file_zipf);
915 } else if (td->o.file_service_type == FIO_FSERVICE_GAUSS) {
916 gauss_init(&td->next_file_gauss, nranges, td->gauss_dev, seed);
917 gauss_disable_hash(&td->next_file_gauss);
921 void td_fill_verify_state_seed(struct thread_data *td)
925 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
930 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF],
934 static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64)
938 init_rand_seed(&td->bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF], use64);
939 td_fill_verify_state_seed(td);
940 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false);
942 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
943 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
944 else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
945 init_rand_file_service(td);
947 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
948 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
949 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
950 init_rand_seed(&td->poisson_state, td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
951 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false);
952 init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false);
957 if (td->o.rand_repeatable)
958 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
960 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
962 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
963 struct frand_state *s = &td->seq_rand_state[i];
965 init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false);
969 void td_fill_rand_seeds(struct thread_data *td)
973 if (td->o.allrand_repeatable) {
976 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
977 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
981 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
986 td_fill_rand_seeds_internal(td, use64);
988 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
989 frand_copy(&td->buf_state_prev, &td->buf_state);
993 * Initializes the ioengine configured for a job, if it has not been done so
996 int ioengine_load(struct thread_data *td)
1001 * Engine has already been loaded.
1005 if (!td->o.ioengine) {
1006 log_err("fio: internal fault, no IO engine specified\n");
1010 engine = get_engine_name(td->o.ioengine);
1011 td->io_ops = load_ioengine(td, engine);
1013 log_err("fio: failed to load engine %s\n", engine);
1017 if (td->io_ops->option_struct_size && td->io_ops->options) {
1019 * In cases where td->eo is set, clone it for a child thread.
1020 * This requires that the parent thread has the same ioengine,
1021 * but that requirement must be enforced by the code which
1022 * cloned the thread.
1024 void *origeo = td->eo;
1026 * Otherwise use the default thread options.
1028 if (!origeo && td != &def_thread && def_thread.eo &&
1029 def_thread.io_ops->options == td->io_ops->options)
1030 origeo = def_thread.eo;
1032 options_init(td->io_ops->options);
1033 td->eo = malloc(td->io_ops->option_struct_size);
1035 * Use the default thread as an option template if this uses the
1036 * same options structure and there are non-default options
1040 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
1041 options_mem_dupe(td->eo, td->io_ops->options);
1043 memset(td->eo, 0, td->io_ops->option_struct_size);
1044 fill_default_options(td->eo, td->io_ops->options);
1046 *(struct thread_data **)td->eo = td;
1050 td->io_ops->flags |= FIO_RAWIO;
1052 td_set_ioengine_flags(td);
1056 static void init_flags(struct thread_data *td)
1058 struct thread_options *o = &td->o;
1060 if (o->verify_backlog)
1061 td->flags |= TD_F_VER_BACKLOG;
1062 if (o->trim_backlog)
1063 td->flags |= TD_F_TRIM_BACKLOG;
1064 if (o->read_iolog_file)
1065 td->flags |= TD_F_READ_IOLOG;
1066 if (o->refill_buffers)
1067 td->flags |= TD_F_REFILL_BUFFERS;
1069 * Always scramble buffers if asked to
1071 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
1072 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1074 * But also scramble buffers, unless we were explicitly asked
1077 if (o->scramble_buffers && !(o->zero_buffers &&
1078 fio_option_is_set(o, zero_buffers)))
1079 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1080 if (o->verify != VERIFY_NONE)
1081 td->flags |= TD_F_VER_NONE;
1083 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
1084 td->flags |= TD_F_NEED_LOCK;
1087 static int setup_random_seeds(struct thread_data *td)
1092 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed))
1093 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
1095 seed = td->o.rand_seed;
1096 for (i = 0; i < 4; i++)
1097 seed *= 0x9e370001UL;
1099 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
1100 td->rand_seeds[i] = seed * td->thread_number + i;
1101 seed *= 0x9e370001UL;
1104 td_fill_rand_seeds(td);
1115 static struct fpre_keyword {
1116 const char *keyword;
1119 } fpre_keywords[] = {
1120 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1121 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1122 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1123 { .keyword = NULL, },
1126 static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
1127 const char *jobname, int jobnum, int filenum)
1129 struct fpre_keyword *f;
1130 char copy[PATH_MAX];
1131 size_t dst_left = PATH_MAX - 1;
1133 if (!o->filename_format || !strlen(o->filename_format)) {
1134 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
1138 for (f = &fpre_keywords[0]; f->keyword; f++)
1139 f->strlen = strlen(f->keyword);
1141 buf[buf_size - 1] = '\0';
1142 strncpy(buf, o->filename_format, buf_size - 1);
1144 memset(copy, 0, sizeof(copy));
1145 for (f = &fpre_keywords[0]; f->keyword; f++) {
1147 size_t pre_len, post_start = 0;
1148 char *str, *dst = copy;
1150 str = strcasestr(buf, f->keyword);
1154 pre_len = str - buf;
1155 if (strlen(str) != f->strlen)
1156 post_start = pre_len + f->strlen;
1159 strncpy(dst, buf, pre_len);
1161 dst_left -= pre_len;
1165 case FPRE_JOBNAME: {
1168 ret = snprintf(dst, dst_left, "%s", jobname);
1171 else if (ret > dst_left) {
1172 log_err("fio: truncated filename\n");
1184 ret = snprintf(dst, dst_left, "%d", jobnum);
1187 else if (ret > dst_left) {
1188 log_err("fio: truncated filename\n");
1197 case FPRE_FILENUM: {
1200 ret = snprintf(dst, dst_left, "%d", filenum);
1203 else if (ret > dst_left) {
1204 log_err("fio: truncated filename\n");
1219 strncpy(dst, buf + post_start, dst_left);
1221 strncpy(buf, copy, buf_size - 1);
1228 bool parse_dryrun(void)
1230 return dump_cmdline || parse_only;
1233 static void gen_log_name(char *name, size_t size, const char *logtype,
1234 const char *logname, unsigned int num,
1235 const char *suf, int per_job)
1238 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1240 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1243 static int check_waitees(char *waitee)
1245 struct thread_data *td;
1248 for_each_td(td, i) {
1249 if (td->subjob_number)
1252 ret += !strcmp(td->o.name, waitee);
1258 static bool wait_for_ok(const char *jobname, struct thread_options *o)
1265 if (!strcmp(jobname, o->wait_for)) {
1266 log_err("%s: a job cannot wait for itself (wait_for=%s).\n",
1267 jobname, o->wait_for);
1271 if (!(nw = check_waitees(o->wait_for))) {
1272 log_err("%s: waitee job %s unknown.\n", jobname, o->wait_for);
1277 log_err("%s: multiple waitees %s found,\n"
1278 "please avoid duplicates when using wait_for option.\n",
1279 jobname, o->wait_for);
1287 * Adds a job to the list of things todo. Sanitizes the various options
1288 * to make sure we don't have conflicts, and initializes various
1291 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
1292 int recursed, int client_type)
1295 char fname[PATH_MAX];
1296 int numjobs, file_alloced;
1297 struct thread_options *o = &td->o;
1298 char logname[PATH_MAX + 32];
1301 * the def_thread is just for options, it's not a real job
1303 if (td == &def_thread)
1309 * if we are just dumping the output command line, don't add the job
1311 if (parse_dryrun()) {
1316 td->client_type = client_type;
1318 if (profile_td_init(td))
1321 if (ioengine_load(td))
1325 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1328 if (o->nr_files == 1 && exists_and_not_regfile(jobname))
1329 add_file(td, jobname, job_add_num, 0);
1331 for (i = 0; i < o->nr_files; i++)
1332 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
1336 if (fixup_options(td))
1340 * Belongs to fixup_options, but o->name is not necessarily set as yet
1342 if (!wait_for_ok(jobname, o))
1348 * IO engines only need this for option callbacks, and the address may
1349 * change in subprocesses.
1352 *(struct thread_data **)td->eo = NULL;
1354 if (td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1357 for_each_file(td, f, i)
1358 f->real_file_size = -1ULL;
1361 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1363 td->ts.clat_percentiles = o->clat_percentiles;
1364 td->ts.percentile_precision = o->percentile_precision;
1365 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1367 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1368 td->ts.clat_stat[i].min_val = ULONG_MAX;
1369 td->ts.slat_stat[i].min_val = ULONG_MAX;
1370 td->ts.lat_stat[i].min_val = ULONG_MAX;
1371 td->ts.bw_stat[i].min_val = ULONG_MAX;
1373 td->ddir_seq_nr = o->ddir_seq_nr;
1375 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1376 prev_group_jobs = 0;
1378 if (groupid == INT_MAX) {
1379 log_err("fio: too many groups defined\n");
1384 td->groupid = groupid;
1387 if (setup_random_seeds(td)) {
1388 td_verror(td, errno, "init_random_state");
1395 if (o->lat_log_file) {
1396 struct log_params p = {
1398 .avg_msec = o->log_avg_msec,
1399 .hist_msec = o->log_hist_msec,
1400 .hist_coarseness = o->log_hist_coarseness,
1401 .log_type = IO_LOG_TYPE_LAT,
1402 .log_offset = o->log_offset,
1403 .log_gz = o->log_gz,
1404 .log_gz_store = o->log_gz_store,
1413 gen_log_name(logname, sizeof(logname), "lat", o->lat_log_file,
1414 td->thread_number, suf, o->per_job_logs);
1415 setup_log(&td->lat_log, &p, logname);
1417 gen_log_name(logname, sizeof(logname), "slat", o->lat_log_file,
1418 td->thread_number, suf, o->per_job_logs);
1419 setup_log(&td->slat_log, &p, logname);
1421 gen_log_name(logname, sizeof(logname), "clat", o->lat_log_file,
1422 td->thread_number, suf, o->per_job_logs);
1423 setup_log(&td->clat_log, &p, logname);
1426 if (o->hist_log_file) {
1427 struct log_params p = {
1429 .avg_msec = o->log_avg_msec,
1430 .hist_msec = o->log_hist_msec,
1431 .hist_coarseness = o->log_hist_coarseness,
1432 .log_type = IO_LOG_TYPE_HIST,
1433 .log_offset = o->log_offset,
1434 .log_gz = o->log_gz,
1435 .log_gz_store = o->log_gz_store,
1440 if (td->client_type) {
1441 log_err("fio: --write_hist_log requires zlib in client/server mode\n");
1451 gen_log_name(logname, sizeof(logname), "clat_hist", o->hist_log_file,
1452 td->thread_number, suf, o->per_job_logs);
1453 setup_log(&td->clat_hist_log, &p, logname);
1456 if (o->bw_log_file) {
1457 struct log_params p = {
1459 .avg_msec = o->log_avg_msec,
1460 .hist_msec = o->log_hist_msec,
1461 .hist_coarseness = o->log_hist_coarseness,
1462 .log_type = IO_LOG_TYPE_BW,
1463 .log_offset = o->log_offset,
1464 .log_gz = o->log_gz,
1465 .log_gz_store = o->log_gz_store,
1469 if (fio_option_is_set(o, bw_avg_time))
1470 p.avg_msec = min(o->log_avg_msec, o->bw_avg_time);
1472 o->bw_avg_time = p.avg_msec;
1474 p.hist_msec = o->log_hist_msec;
1475 p.hist_coarseness = o->log_hist_coarseness;
1482 gen_log_name(logname, sizeof(logname), "bw", o->bw_log_file,
1483 td->thread_number, suf, o->per_job_logs);
1484 setup_log(&td->bw_log, &p, logname);
1486 if (o->iops_log_file) {
1487 struct log_params p = {
1489 .avg_msec = o->log_avg_msec,
1490 .hist_msec = o->log_hist_msec,
1491 .hist_coarseness = o->log_hist_coarseness,
1492 .log_type = IO_LOG_TYPE_IOPS,
1493 .log_offset = o->log_offset,
1494 .log_gz = o->log_gz,
1495 .log_gz_store = o->log_gz_store,
1499 if (fio_option_is_set(o, iops_avg_time))
1500 p.avg_msec = min(o->log_avg_msec, o->iops_avg_time);
1502 o->iops_avg_time = p.avg_msec;
1504 p.hist_msec = o->log_hist_msec;
1505 p.hist_coarseness = o->log_hist_coarseness;
1512 gen_log_name(logname, sizeof(logname), "iops", o->iops_log_file,
1513 td->thread_number, suf, o->per_job_logs);
1514 setup_log(&td->iops_log, &p, logname);
1518 o->name = strdup(jobname);
1520 if (output_format & FIO_OUTPUT_NORMAL) {
1522 if (is_backend && !recursed)
1523 fio_server_send_add_job(td);
1525 if (!td_ioengine_flagged(td, FIO_NOIO)) {
1526 char *c1, *c2, *c3, *c4;
1527 char *c5 = NULL, *c6 = NULL;
1529 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1530 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1531 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1532 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1534 if (!o->bs_is_seq_rand) {
1535 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1536 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1539 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1541 ddir_str(o->td_ddir));
1543 if (o->bs_is_seq_rand)
1544 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1547 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1548 c1, c2, c3, c4, c5, c6);
1550 log_info("ioengine=%s, iodepth=%u\n",
1551 td->io_ops->name, o->iodepth);
1560 } else if (job_add_num == 1)
1565 * recurse add identical jobs, clear numjobs and stonewall options
1566 * as they don't apply to sub-jobs
1568 numjobs = o->numjobs;
1570 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
1575 td_new->o.numjobs = 1;
1576 td_new->o.stonewall = 0;
1577 td_new->o.new_group = 0;
1578 td_new->subjob_number = numjobs;
1581 if (td_new->files) {
1583 for_each_file(td_new, f, i) {
1585 sfree(f->file_name);
1588 free(td_new->files);
1589 td_new->files = NULL;
1591 td_new->files_index = 0;
1592 td_new->files_size = 0;
1593 if (td_new->o.filename) {
1594 free(td_new->o.filename);
1595 td_new->o.filename = NULL;
1599 if (add_job(td_new, jobname, numjobs, 1, client_type))
1610 * Parse as if 'o' was a command line
1612 void add_job_opts(const char **o, int client_type)
1614 struct thread_data *td, *td_parent;
1615 int i, in_global = 1;
1619 td_parent = td = NULL;
1621 if (!strncmp(o[i], "name", 4)) {
1624 add_job(td, jobname, 0, 0, client_type);
1626 sprintf(jobname, "%s", o[i] + 5);
1628 if (in_global && !td_parent)
1629 td_parent = get_new_job(1, &def_thread, 0, jobname);
1630 else if (!in_global && !td) {
1632 td_parent = &def_thread;
1633 td = get_new_job(0, td_parent, 0, jobname);
1636 fio_options_parse(td_parent, (char **) &o[i], 1);
1638 fio_options_parse(td, (char **) &o[i], 1);
1643 add_job(td, jobname, 0, 0, client_type);
1646 static int skip_this_section(const char *name)
1650 if (!nr_job_sections)
1652 if (!strncmp(name, "global", 6))
1655 for (i = 0; i < nr_job_sections; i++)
1656 if (!strcmp(job_sections[i], name))
1662 static int is_empty_or_comment(char *line)
1666 for (i = 0; i < strlen(line); i++) {
1671 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1679 * This is our [ini] type file parser.
1681 int __parse_jobs_ini(struct thread_data *td,
1682 char *file, int is_buf, int stonewall_flag, int type,
1683 int nested, char *name, char ***popts, int *aopts, int *nopts)
1685 unsigned int global = 0;
1689 int ret = 0, stonewall;
1692 int inside_skip = 0;
1694 int i, alloc_opts, num_opts;
1696 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1697 assert(td || !nested);
1702 if (!strcmp(file, "-"))
1705 f = fopen(file, "r");
1710 log_err("fio: unable to open '%s' job file\n", file);
1712 td_verror(td, __err, "job file open");
1717 string = malloc(4096);
1720 * it's really 256 + small bit, 280 should suffice
1724 memset(name, 0, 280);
1728 if (nested && popts) {
1730 alloc_opts = *aopts;
1736 opts = malloc(sizeof(char *) * alloc_opts);
1740 stonewall = stonewall_flag;
1743 * if skip_fgets is set, we already have loaded a line we
1748 p = strsep(&file, "\n");
1750 p = fgets(string, 4096, f);
1756 strip_blank_front(&p);
1759 dprint(FD_PARSE, "%s\n", p);
1760 if (is_empty_or_comment(p))
1764 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1768 log_err("fio: option <%s> outside of "
1769 "[] job section\n", p);
1774 name[strlen(name) - 1] = '\0';
1776 if (skip_this_section(name)) {
1782 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1784 global = !strncmp(name, "global", 6);
1790 log_info("--name=%s ", name);
1794 td = get_new_job(global, &def_thread, 0, name);
1801 * Separate multiple job files by a stonewall
1803 if (!global && stonewall) {
1804 td->o.stonewall = stonewall;
1809 memset(opts, 0, alloc_opts * sizeof(char *));
1817 p = strsep(&file, "\n");
1819 p = fgets(string, 4096, f);
1822 dprint(FD_PARSE, "%s", p);
1827 if (is_empty_or_comment(p))
1830 strip_blank_front(&p);
1833 * new section, break out and make sure we don't
1834 * fgets() a new line at the top.
1838 log_err("No new sections in included files\n");
1848 if (!strncmp(p, "include", strlen("include"))) {
1849 char *filename = p + strlen("include") + 1,
1850 *ts, *full_fn = NULL;
1853 * Allow for the include filename
1854 * specification to be relative.
1856 if (access(filename, F_OK) &&
1857 (ts = strrchr(file, '/'))) {
1858 int len = ts - file +
1859 strlen(filename) + 2;
1861 if (!(full_fn = calloc(1, len))) {
1867 file, (ts - file) + 1);
1868 strncpy(full_fn + (ts - file) + 1,
1869 filename, strlen(filename));
1870 full_fn[len - 1] = 0;
1874 ret = __parse_jobs_ini(td, filename, is_buf,
1875 stonewall_flag, type, 1,
1877 &alloc_opts, &num_opts);
1880 log_err("Error %d while parsing "
1881 "include file %s\n",
1894 if (num_opts == alloc_opts) {
1896 opts = realloc(opts,
1897 alloc_opts * sizeof(char *));
1900 opts[num_opts] = strdup(p);
1906 *aopts = alloc_opts;
1911 ret = fio_options_parse(td, opts, num_opts);
1916 ret = add_job(td, name, 0, 0, type);
1918 log_err("fio: job %s dropped\n", name);
1922 for (i = 0; i < num_opts; i++)
1931 while (i < nr_job_sections) {
1932 free(job_sections[i]);
1941 if (!is_buf && f != stdin)
1946 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1948 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
1949 0, NULL, NULL, NULL, NULL);
1952 static int fill_def_thread(void)
1954 memset(&def_thread, 0, sizeof(def_thread));
1955 INIT_FLIST_HEAD(&def_thread.opt_list);
1957 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1958 def_thread.o.error_dump = 1;
1961 * fill default options
1963 fio_fill_default_options(&def_thread);
1967 static void show_debug_categories(void)
1969 #ifdef FIO_INC_DEBUG
1970 struct debug_level *dl = &debug_levels[0];
1971 int curlen, first = 1;
1975 int has_next = (dl + 1)->name != NULL;
1977 if (first || curlen + strlen(dl->name) >= 80) {
1982 curlen += printf("\t\t\t%s", dl->name);
1983 curlen += 3 * (8 - 1);
1985 curlen += printf(",");
1987 curlen += printf("%s", dl->name);
1989 curlen += printf(",");
1998 static void usage(const char *name)
2000 printf("%s\n", fio_version_string);
2001 printf("%s [options] [job options] <job file(s)>\n", name);
2002 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
2003 show_debug_categories();
2004 printf(" --parse-only\t\tParse options only, don't start any IO\n");
2005 printf(" --output\t\tWrite output to file\n");
2006 printf(" --runtime\t\tRuntime in seconds\n");
2007 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
2008 printf(" --minimal\t\tMinimal (terse) output\n");
2009 printf(" --output-format=x\tOutput format (terse,json,json+,normal)\n");
2010 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
2011 printf(" --version\t\tPrint version info and exit\n");
2012 printf(" --help\t\tPrint this page\n");
2013 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
2014 printf(" --crctest\t\tTest speed of checksum functions\n");
2015 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
2017 printf(" --enghelp=engine\tPrint ioengine help, or list"
2018 " available ioengines\n");
2019 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
2021 printf(" --showcmd\t\tTurn a job file into command line options\n");
2022 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
2023 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
2024 printf(" --eta-newline=time\tForce a new line for every 'time'");
2025 printf(" period passed\n");
2026 printf(" --status-interval=t\tForce full status dump every");
2027 printf(" 't' period passed\n");
2028 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
2030 printf(" --section=name\tOnly run specified section in job file\n");
2031 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2033 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
2034 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2035 printf(" --server=args\t\tStart a backend fio server\n");
2036 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
2037 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
2038 printf(" --remote-config=file\tTell fio server to load this local job file\n");
2039 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2040 "\t\t\t(option=system,percpu) or run unit work\n"
2041 "\t\t\tcalibration only (option=calibrate)\n");
2043 printf(" --inflate-log=log\tInflate and output compressed log\n");
2045 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
2046 printf(" --trigger-timeout=t\tExecute trigger af this time\n");
2047 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
2048 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
2049 printf(" --aux-path=path\tUse this path for fio state generated files\n");
2050 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
2051 printf("\n Jens Axboe <jaxboe@fusionio.com>");
2052 printf("\n Jens Axboe <axboe@fb.com>\n");
2055 #ifdef FIO_INC_DEBUG
2056 struct debug_level debug_levels[] = {
2057 { .name = "process",
2058 .help = "Process creation/exit logging",
2059 .shift = FD_PROCESS,
2062 .help = "File related action logging",
2066 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2070 .help = "Memory allocation/freeing logging",
2073 { .name = "blktrace",
2074 .help = "blktrace action logging",
2075 .shift = FD_BLKTRACE,
2078 .help = "IO verification action logging",
2082 .help = "Random generation logging",
2086 .help = "Parser logging",
2089 { .name = "diskutil",
2090 .help = "Disk utility logging actions",
2091 .shift = FD_DISKUTIL,
2094 .help = "Logging related to creating/destroying jobs",
2098 .help = "Mutex logging",
2101 { .name = "profile",
2102 .help = "Logging related to profiles",
2103 .shift = FD_PROFILE,
2106 .help = "Logging related to time keeping functions",
2110 .help = "Network logging",
2114 .help = "Rate logging",
2117 { .name = "compress",
2118 .help = "Log compression logging",
2119 .shift = FD_COMPRESS,
2124 static int set_debug(const char *string)
2126 struct debug_level *dl;
2127 char *p = (char *) string;
2134 if (!strcmp(string, "?") || !strcmp(string, "help")) {
2135 log_info("fio: dumping debug options:");
2136 for (i = 0; debug_levels[i].name; i++) {
2137 dl = &debug_levels[i];
2138 log_info("%s,", dl->name);
2144 while ((opt = strsep(&p, ",")) != NULL) {
2147 if (!strncmp(opt, "all", 3)) {
2148 log_info("fio: set all debug options\n");
2153 for (i = 0; debug_levels[i].name; i++) {
2154 dl = &debug_levels[i];
2155 found = !strncmp(opt, dl->name, strlen(dl->name));
2159 if (dl->shift == FD_JOB) {
2160 opt = strchr(opt, ':');
2162 log_err("fio: missing job number\n");
2166 fio_debug_jobno = atoi(opt);
2167 log_info("fio: set debug jobno %d\n",
2170 log_info("fio: set debug option %s\n", opt);
2171 fio_debug |= (1UL << dl->shift);
2177 log_err("fio: debug mask %s not found\n", opt);
2182 static int set_debug(const char *string)
2184 log_err("fio: debug tracing not included in build\n");
2189 static void fio_options_fill_optstring(void)
2191 char *ostr = cmd_optstr;
2195 while (l_opts[i].name) {
2196 ostr[c++] = l_opts[i].val;
2197 if (l_opts[i].has_arg == required_argument)
2199 else if (l_opts[i].has_arg == optional_argument) {
2208 static int client_flag_set(char c)
2213 while (l_opts[i].name) {
2214 int val = l_opts[i].val;
2216 if (c == (val & 0xff))
2217 return (val & FIO_CLIENT_FLAG);
2225 static void parse_cmd_client(void *client, char *opt)
2227 fio_client_add_cmd_option(client, opt);
2230 static void show_closest_option(const char *name)
2232 int best_option, best_distance;
2235 while (*name == '-')
2239 best_distance = INT_MAX;
2241 while (l_opts[i].name) {
2242 distance = string_distance(name, l_opts[i].name);
2243 if (distance < best_distance) {
2244 best_distance = distance;
2250 if (best_option != -1 && string_distance_ok(name, best_distance))
2251 log_err("Did you mean %s?\n", l_opts[best_option].name);
2254 static int parse_output_format(const char *optarg)
2256 char *p, *orig, *opt;
2259 p = orig = strdup(optarg);
2263 while ((opt = strsep(&p, ",")) != NULL) {
2264 if (!strcmp(opt, "minimal") ||
2265 !strcmp(opt, "terse") ||
2266 !strcmp(opt, "csv"))
2267 output_format |= FIO_OUTPUT_TERSE;
2268 else if (!strcmp(opt, "json"))
2269 output_format |= FIO_OUTPUT_JSON;
2270 else if (!strcmp(opt, "json+"))
2271 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
2272 else if (!strcmp(opt, "normal"))
2273 output_format |= FIO_OUTPUT_NORMAL;
2275 log_err("fio: invalid output format %s\n", opt);
2285 int parse_cmd_line(int argc, char *argv[], int client_type)
2287 struct thread_data *td = NULL;
2288 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
2289 char *ostr = cmd_optstr;
2290 char *pid_file = NULL;
2291 void *cur_client = NULL;
2295 * Reset optind handling, since we may call this multiple times
2300 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2301 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
2302 parse_cmd_client(cur_client, argv[optind - 1]);
2303 c &= ~FIO_CLIENT_FLAG;
2308 smalloc_pool_size = atoi(optarg);
2309 smalloc_pool_size <<= 10;
2313 if (check_str_time(optarg, &def_timeout, 1)) {
2314 log_err("fio: failed parsing time %s\n", optarg);
2320 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2328 if (f_out && f_out != stdout)
2331 f_out = fopen(optarg, "w+");
2333 perror("fopen output");
2339 output_format = FIO_OUTPUT_TERSE;
2342 if (parse_output_format(optarg)) {
2343 log_err("fio: failed parsing output-format\n");
2350 output_format |= FIO_OUTPUT_TERSE;
2362 fio_show_option_help(optarg);
2369 fio_show_ioengine_help(optarg);
2383 log_info("%s\n", fio_version_string);
2388 terse_version = atoi(optarg);
2389 if (!(terse_version == 2 || terse_version == 3 ||
2390 terse_version == 4)) {
2391 log_err("fio: bad terse version format\n");
2397 if (!strcmp("always", optarg))
2398 eta_print = FIO_ETA_ALWAYS;
2399 else if (!strcmp("never", optarg))
2400 eta_print = FIO_ETA_NEVER;
2405 if (check_str_time(optarg, &t, 1)) {
2406 log_err("fio: failed parsing eta time %s\n", optarg);
2410 eta_new_line = t / 1000;
2414 if (set_debug(optarg))
2424 if (!strcmp(optarg, "global")) {
2425 log_err("fio: can't use global as only "
2431 new_size = (nr_job_sections + 1) * sizeof(char *);
2432 job_sections = realloc(job_sections, new_size);
2433 job_sections[nr_job_sections] = strdup(optarg);
2439 exit_val = iolog_file_inflate(optarg);
2448 exec_profile = strdup(optarg);
2450 case FIO_GETOPT_JOB: {
2451 const char *opt = l_opts[lidx].name;
2454 if (!strncmp(opt, "name", 4) && td) {
2455 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2462 int is_section = !strncmp(opt, "name", 4);
2465 if (!is_section || !strncmp(val, "global", 6))
2468 if (is_section && skip_this_section(val))
2471 td = get_new_job(global, &def_thread, 1, NULL);
2472 if (!td || ioengine_load(td)) {
2481 fio_options_set_ioengine_opts(l_opts, td);
2484 if ((!val || !strlen(val)) &&
2485 l_opts[lidx].has_arg == required_argument) {
2486 log_err("fio: option %s requires an argument\n", opt);
2489 ret = fio_cmd_option_parse(td, opt, val);
2500 if (!ret && !strcmp(opt, "ioengine")) {
2502 if (ioengine_load(td)) {
2509 fio_options_set_ioengine_opts(l_opts, td);
2513 case FIO_GETOPT_IOENGINE: {
2514 const char *opt = l_opts[lidx].name;
2520 ret = fio_cmd_ioengine_option_parse(td, opt, val);
2527 max_jobs = atoi(optarg);
2528 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2529 log_err("fio: invalid max jobs: %d\n", max_jobs);
2536 #ifndef CONFIG_NO_SHM
2538 log_err("fio: can't be both client and server\n");
2544 fio_server_set_arg(optarg);
2548 log_err("fio: client/server requires SHM support\n");
2556 pid_file = strdup(optarg);
2559 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2560 /* exit on error and calibration only */
2570 log_err("fio: can't be both client and server\n");
2575 /* if --client parameter contains a pathname */
2576 if (0 == access(optarg, R_OK)) {
2577 /* file contains a list of host addrs or names */
2578 char hostaddr[PATH_MAX] = {0};
2580 FILE * hostf = fopen(optarg, "r");
2582 log_err("fio: could not open client list file %s for read\n", optarg);
2587 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2589 * read at most PATH_MAX-1 chars from each
2590 * record in this file
2592 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2593 /* expect EVERY host in file to be valid */
2594 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2595 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2602 break; /* no possibility of job file for "this client only" */
2604 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
2605 log_err("fio: failed adding client %s\n", optarg);
2611 * If the next argument exists and isn't an option,
2612 * assume it's a job file for this client only.
2614 while (optind < argc) {
2615 if (!strncmp(argv[optind], "--", 2) ||
2616 !strncmp(argv[optind], "-", 1))
2619 if (fio_client_add_ini_file(cur_client, argv[optind], false))
2626 if (fio_client_add_ini_file(cur_client, optarg, true)) {
2634 exit_val = fio_monotonic_clocktest(1);
2639 exit_val = fio_crctest(optarg);
2644 if (check_str_time(optarg, &val, 1)) {
2645 log_err("fio: failed parsing time %s\n", optarg);
2650 status_interval = val / 1000;
2656 trigger_file = strdup(optarg);
2661 trigger_cmd = strdup(optarg);
2664 if (trigger_remote_cmd)
2665 free(trigger_remote_cmd);
2666 trigger_remote_cmd = strdup(optarg);
2671 aux_path = strdup(optarg);
2674 if (check_str_time(optarg, &trigger_timeout, 1)) {
2675 log_err("fio: failed parsing time %s\n", optarg);
2679 trigger_timeout /= 1000000;
2682 log_err("%s: unrecognized option '%s'\n", argv[0],
2684 show_closest_option(argv[optind - 1]);
2694 if (do_exit && !(is_backend || nr_clients))
2697 if (nr_clients && fio_clients_connect())
2700 if (is_backend && backend)
2701 return fio_start_server(pid_file);
2707 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2713 while (!ret && optind < argc) {
2715 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2716 ini_file[ini_idx - 1] = strdup(argv[optind]);
2727 int fio_init_options(void)
2732 fio_options_fill_optstring();
2733 fio_options_dup_and_init(l_opts);
2737 if (fill_def_thread())
2743 extern int fio_check_options(struct thread_options *);
2745 int parse_options(int argc, char *argv[])
2747 const int type = FIO_CLIENT_TYPE_CLI;
2750 if (fio_init_options())
2752 if (fio_test_cconv(&def_thread.o))
2753 log_err("fio: failed internal cconv test\n");
2755 job_files = parse_cmd_line(argc, argv, type);
2757 if (job_files > 0) {
2758 for (i = 0; i < job_files; i++) {
2759 if (i && fill_def_thread())
2762 if (fio_clients_send_ini(ini_file[i]))
2765 } else if (!is_backend) {
2766 if (parse_jobs_ini(ini_file[i], 0, i, type))
2771 } else if (nr_clients) {
2772 if (fill_def_thread())
2774 if (fio_clients_send_ini(NULL))
2779 fio_options_free(&def_thread);
2780 filesetup_mem_free();
2782 if (!thread_number) {
2787 if (is_backend || nr_clients)
2792 log_err("No jobs(s) defined\n\n");
2802 if (output_format & FIO_OUTPUT_NORMAL)
2803 log_info("%s\n", fio_version_string);
2808 void options_default_fill(struct thread_options *o)
2810 memcpy(o, &def_thread.o, sizeof(*o));
2813 struct thread_data *get_global_options(void)