2 * This file contains job initialization and setup functions.
12 #include <sys/types.h>
16 #ifndef FIO_NO_HAVE_SHM_H
28 #include "steadystate.h"
30 #include "oslib/getopt.h"
31 #include "oslib/strcasestr.h"
35 const char fio_version_string[] = FIO_VERSION;
37 #define FIO_RANDSEED (0xb1899bedUL)
39 static char **ini_file;
40 static int max_jobs = FIO_MAX_JOBS;
41 static int dump_cmdline;
42 static long long def_timeout;
43 static int parse_only;
45 static struct thread_data def_thread;
46 struct thread_data *threads = NULL;
47 static char **job_sections;
48 static int nr_job_sections;
50 int exitall_on_terminate = 0;
51 int exitall_on_terminate_error = 0;
52 int output_format = FIO_OUTPUT_NORMAL;
53 int eta_print = FIO_ETA_AUTO;
57 char *exec_profile = NULL;
58 int warnings_fatal = 0;
59 int terse_version = 3;
66 int status_interval = 0;
68 char *trigger_file = NULL;
69 long long trigger_timeout = 0;
70 char *trigger_cmd = NULL;
71 char *trigger_remote_cmd = NULL;
73 char *aux_path = NULL;
75 static int prev_group_jobs;
77 unsigned long fio_debug = 0;
78 unsigned int fio_debug_jobno = -1;
79 unsigned int *fio_debug_jobp = NULL;
81 static char cmd_optstr[256];
84 #define FIO_CLIENT_FLAG (1 << 16)
87 * Command line options. These will contain the above, plus a few
88 * extra that only pertain to fio itself and not jobs.
90 static struct option l_opts[FIO_NR_OPTIONS] = {
92 .name = (char *) "output",
93 .has_arg = required_argument,
94 .val = 'o' | FIO_CLIENT_FLAG,
97 .name = (char *) "timeout",
98 .has_arg = required_argument,
99 .val = 't' | FIO_CLIENT_FLAG,
102 .name = (char *) "latency-log",
103 .has_arg = required_argument,
104 .val = 'l' | FIO_CLIENT_FLAG,
107 .name = (char *) "bandwidth-log",
108 .has_arg = required_argument,
109 .val = 'b' | FIO_CLIENT_FLAG,
112 .name = (char *) "minimal",
113 .has_arg = no_argument,
114 .val = 'm' | FIO_CLIENT_FLAG,
117 .name = (char *) "output-format",
118 .has_arg = required_argument,
119 .val = 'F' | FIO_CLIENT_FLAG,
122 .name = (char *) "append-terse",
123 .has_arg = optional_argument,
127 .name = (char *) "version",
128 .has_arg = no_argument,
129 .val = 'v' | FIO_CLIENT_FLAG,
132 .name = (char *) "help",
133 .has_arg = no_argument,
134 .val = 'h' | FIO_CLIENT_FLAG,
137 .name = (char *) "cmdhelp",
138 .has_arg = optional_argument,
139 .val = 'c' | FIO_CLIENT_FLAG,
142 .name = (char *) "enghelp",
143 .has_arg = optional_argument,
144 .val = 'i' | FIO_CLIENT_FLAG,
147 .name = (char *) "showcmd",
148 .has_arg = no_argument,
149 .val = 's' | FIO_CLIENT_FLAG,
152 .name = (char *) "readonly",
153 .has_arg = no_argument,
154 .val = 'r' | FIO_CLIENT_FLAG,
157 .name = (char *) "eta",
158 .has_arg = required_argument,
159 .val = 'e' | FIO_CLIENT_FLAG,
162 .name = (char *) "eta-newline",
163 .has_arg = required_argument,
164 .val = 'E' | FIO_CLIENT_FLAG,
167 .name = (char *) "debug",
168 .has_arg = required_argument,
169 .val = 'd' | FIO_CLIENT_FLAG,
172 .name = (char *) "parse-only",
173 .has_arg = no_argument,
174 .val = 'P' | FIO_CLIENT_FLAG,
177 .name = (char *) "section",
178 .has_arg = required_argument,
179 .val = 'x' | FIO_CLIENT_FLAG,
183 .name = (char *) "inflate-log",
184 .has_arg = required_argument,
185 .val = 'X' | FIO_CLIENT_FLAG,
189 .name = (char *) "alloc-size",
190 .has_arg = required_argument,
191 .val = 'a' | FIO_CLIENT_FLAG,
194 .name = (char *) "profile",
195 .has_arg = required_argument,
196 .val = 'p' | FIO_CLIENT_FLAG,
199 .name = (char *) "warnings-fatal",
200 .has_arg = no_argument,
201 .val = 'w' | FIO_CLIENT_FLAG,
204 .name = (char *) "max-jobs",
205 .has_arg = required_argument,
206 .val = 'j' | FIO_CLIENT_FLAG,
209 .name = (char *) "terse-version",
210 .has_arg = required_argument,
211 .val = 'V' | FIO_CLIENT_FLAG,
214 .name = (char *) "server",
215 .has_arg = optional_argument,
218 { .name = (char *) "daemonize",
219 .has_arg = required_argument,
223 .name = (char *) "client",
224 .has_arg = required_argument,
228 .name = (char *) "remote-config",
229 .has_arg = required_argument,
233 .name = (char *) "cpuclock-test",
234 .has_arg = no_argument,
238 .name = (char *) "crctest",
239 .has_arg = optional_argument,
243 .name = (char *) "idle-prof",
244 .has_arg = required_argument,
248 .name = (char *) "status-interval",
249 .has_arg = required_argument,
253 .name = (char *) "trigger-file",
254 .has_arg = required_argument,
258 .name = (char *) "trigger-timeout",
259 .has_arg = required_argument,
263 .name = (char *) "trigger",
264 .has_arg = required_argument,
268 .name = (char *) "trigger-remote",
269 .has_arg = required_argument,
273 .name = (char *) "aux-path",
274 .has_arg = required_argument,
282 void free_threads_shm(void)
286 #ifndef CONFIG_NO_SHM
287 struct shmid_ds sbuf;
291 shmctl(shm_id, IPC_RMID, &sbuf);
300 static void free_shm(void)
305 fio_debug_jobp = NULL;
311 free(trigger_remote_cmd);
312 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
314 options_free(fio_options, &def_thread);
320 * The thread area is shared between the main process and the job
321 * threads/processes. So setup a shared memory segment that will hold
322 * all the job info. We use the end of the region for keeping track of
323 * open files across jobs, for file sharing.
325 static int setup_thread_area(void)
333 * 1024 is too much on some machines, scale max_jobs if
334 * we get a failure that looks like too large a shm segment
337 size_t size = max_jobs * sizeof(struct thread_data);
339 size += file_hash_size;
340 size += sizeof(unsigned int);
342 #ifndef CONFIG_NO_SHM
343 shm_id = shmget(0, size, IPC_CREAT | 0600);
346 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
351 threads = malloc(size);
359 #ifndef CONFIG_NO_SHM
363 threads = shmat(shm_id, NULL, 0);
364 if (threads == (void *) -1) {
370 memset(threads, 0, max_jobs * sizeof(struct thread_data));
371 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
372 fio_debug_jobp = (void *) hash + file_hash_size;
373 *fio_debug_jobp = -1;
374 file_hash_init(hash);
381 static void set_cmd_options(struct thread_data *td)
383 struct thread_options *o = &td->o;
386 o->timeout = def_timeout;
389 static void dump_print_option(struct print_option *p)
393 if (!strcmp("description", p->name))
398 log_info("--%s%s", p->name, p->value ? "" : " ");
400 log_info("=%s%s%s ", delim, p->value, delim);
403 static void dump_opt_list(struct thread_data *td)
405 struct flist_head *entry;
406 struct print_option *p;
408 if (flist_empty(&td->opt_list))
411 flist_for_each(entry, &td->opt_list) {
412 p = flist_entry(entry, struct print_option, list);
413 dump_print_option(p);
417 static void fio_dump_options_free(struct thread_data *td)
419 while (!flist_empty(&td->opt_list)) {
420 struct print_option *p;
422 p = flist_first_entry(&td->opt_list, struct print_option, list);
423 flist_del_init(&p->list);
430 static void copy_opt_list(struct thread_data *dst, struct thread_data *src)
432 struct flist_head *entry;
434 if (flist_empty(&src->opt_list))
437 flist_for_each(entry, &src->opt_list) {
438 struct print_option *srcp, *dstp;
440 srcp = flist_entry(entry, struct print_option, list);
441 dstp = malloc(sizeof(*dstp));
442 dstp->name = strdup(srcp->name);
444 dstp->value = strdup(srcp->value);
447 flist_add_tail(&dstp->list, &dst->opt_list);
452 * Return a free job structure.
454 static struct thread_data *get_new_job(int global, struct thread_data *parent,
455 int preserve_eo, const char *jobname)
457 struct thread_data *td;
460 set_cmd_options(&def_thread);
463 if (setup_thread_area()) {
464 log_err("error: failed to setup shm segment\n");
467 if (thread_number >= max_jobs) {
468 log_err("error: maximum number of jobs (%d) reached.\n",
473 td = &threads[thread_number++];
476 INIT_FLIST_HEAD(&td->opt_list);
477 if (parent != &def_thread)
478 copy_opt_list(td, parent);
484 td->o.uid = td->o.gid = -1U;
486 dup_files(td, parent);
487 fio_options_mem_dupe(td);
489 profile_add_hooks(td);
491 td->thread_number = thread_number;
492 td->subjob_number = 0;
495 td->o.name = strdup(jobname);
497 if (!parent->o.group_reporting || parent == &def_thread)
504 static void put_job(struct thread_data *td)
506 if (td == &def_thread)
513 log_info("fio: %s\n", td->verror);
515 fio_options_free(td);
516 fio_dump_options_free(td);
523 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
527 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
529 unsigned int bs = td->o.min_bs[ddir];
531 assert(ddir_rw(ddir));
533 if (td->o.rate[ddir])
534 td->rate_bps[ddir] = td->o.rate[ddir];
536 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
538 if (!td->rate_bps[ddir]) {
539 log_err("rate lower than supported\n");
543 td->rate_next_io_time[ddir] = 0;
544 td->rate_io_issue_bytes[ddir] = 0;
549 static int setup_rate(struct thread_data *td)
553 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
554 ret = __setup_rate(td, DDIR_READ);
555 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
556 ret |= __setup_rate(td, DDIR_WRITE);
557 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
558 ret |= __setup_rate(td, DDIR_TRIM);
563 static int fixed_block_size(struct thread_options *o)
565 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
566 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
567 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
568 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
569 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
573 static unsigned long long get_rand_start_delay(struct thread_data *td)
575 unsigned long long delayrange;
579 delayrange = td->o.start_delay_high - td->o.start_delay;
581 frand_max = rand_max(&td->delay_state);
582 r = __rand(&td->delay_state);
583 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
585 delayrange += td->o.start_delay;
590 * Lazy way of fixing up options that depend on each other. We could also
591 * define option callback handlers, but this is easier.
593 static int fixup_options(struct thread_data *td)
595 struct thread_options *o = &td->o;
598 #ifndef FIO_HAVE_PSHARED_MUTEX
599 if (!o->use_thread) {
600 log_info("fio: this platform does not support process shared"
601 " mutexes, forcing use of threads. Use the 'thread'"
602 " option to get rid of this warning.\n");
604 ret = warnings_fatal;
608 if (o->write_iolog_file && o->read_iolog_file) {
609 log_err("fio: read iolog overrides write_iolog\n");
610 free(o->write_iolog_file);
611 o->write_iolog_file = NULL;
612 ret = warnings_fatal;
616 * only really works with 1 file
618 if (o->zone_size && o->open_files > 1)
622 * If zone_range isn't specified, backward compatibility dictates it
623 * should be made equal to zone_size.
625 if (o->zone_size && !o->zone_range)
626 o->zone_range = o->zone_size;
629 * Reads can do overwrites, we always need to pre-create the file
631 if (td_read(td) || td_rw(td))
634 if (!o->min_bs[DDIR_READ])
635 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
636 if (!o->max_bs[DDIR_READ])
637 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
638 if (!o->min_bs[DDIR_WRITE])
639 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
640 if (!o->max_bs[DDIR_WRITE])
641 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
642 if (!o->min_bs[DDIR_TRIM])
643 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
644 if (!o->max_bs[DDIR_TRIM])
645 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
647 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
648 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
651 * For random IO, allow blockalign offset other than min_bs.
653 if (!o->ba[DDIR_READ] || !td_random(td))
654 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
655 if (!o->ba[DDIR_WRITE] || !td_random(td))
656 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
657 if (!o->ba[DDIR_TRIM] || !td_random(td))
658 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
660 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
661 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
662 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
664 log_err("fio: Any use of blockalign= turns off randommap\n");
666 ret = warnings_fatal;
669 if (!o->file_size_high)
670 o->file_size_high = o->file_size_low;
672 if (o->start_delay_high)
673 o->start_delay = get_rand_start_delay(td);
675 if (o->norandommap && o->verify != VERIFY_NONE
676 && !fixed_block_size(o)) {
677 log_err("fio: norandommap given for variable block sizes, "
679 ret = warnings_fatal;
681 if (o->bs_unaligned && (o->odirect || td_ioengine_flagged(td, FIO_RAWIO)))
682 log_err("fio: bs_unaligned may not work with raw io\n");
685 * thinktime_spin must be less than thinktime
687 if (o->thinktime_spin > o->thinktime)
688 o->thinktime_spin = o->thinktime;
691 * The low water mark cannot be bigger than the iodepth
693 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
694 o->iodepth_low = o->iodepth;
697 * If batch number isn't set, default to the same as iodepth
699 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
700 o->iodepth_batch = o->iodepth;
703 * If max batch complete number isn't set or set incorrectly,
704 * default to the same as iodepth_batch_complete_min
706 if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
707 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
709 if (o->nr_files > td->files_index)
710 o->nr_files = td->files_index;
712 if (o->open_files > o->nr_files || !o->open_files)
713 o->open_files = o->nr_files;
715 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
716 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
717 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
718 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
719 log_err("fio: rate and rate_iops are mutually exclusive\n");
722 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
723 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
724 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
725 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
726 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
727 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
728 log_err("fio: minimum rate exceeds rate\n");
732 if (!o->timeout && o->time_based) {
733 log_err("fio: time_based requires a runtime/timeout setting\n");
735 ret = warnings_fatal;
738 if (o->fill_device && !o->size)
741 if (o->verify != VERIFY_NONE) {
742 if (td_write(td) && o->do_verify && o->numjobs > 1) {
743 log_info("Multiple writers may overwrite blocks that "
744 "belong to other jobs. This can cause "
745 "verification failures.\n");
746 ret = warnings_fatal;
749 if (!fio_option_is_set(o, refill_buffers))
750 o->refill_buffers = 1;
752 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
754 o->verify_interval = o->min_bs[DDIR_WRITE];
757 * Verify interval must be smaller or equal to the
760 if (o->verify_interval > o->min_bs[DDIR_WRITE])
761 o->verify_interval = o->min_bs[DDIR_WRITE];
762 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
763 o->verify_interval = o->min_bs[DDIR_READ];
767 o->invalidate_cache = 0;
768 if (td_ioengine_flagged(td, FIO_PIPEIO)) {
769 log_info("fio: cannot pre-read files with an IO engine"
770 " that isn't seekable. Pre-read disabled.\n");
771 ret = warnings_fatal;
776 if (td_ioengine_flagged(td, FIO_BIT_BASED))
782 #ifndef CONFIG_FDATASYNC
783 if (o->fdatasync_blocks) {
784 log_info("fio: this platform does not support fdatasync()"
785 " falling back to using fsync(). Use the 'fsync'"
786 " option instead of 'fdatasync' to get rid of"
788 o->fsync_blocks = o->fdatasync_blocks;
789 o->fdatasync_blocks = 0;
790 ret = warnings_fatal;
796 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
797 * so fail if we're passed those flags
799 if (td_ioengine_flagged(td, FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
800 log_err("fio: Windows does not support direct or non-buffered io with"
801 " the synchronous ioengines. Use the 'windowsaio' ioengine"
802 " with 'direct=1' and 'iodepth=1' instead.\n");
808 * For fully compressible data, just zero them at init time.
809 * It's faster than repeatedly filling it. For non-zero
810 * compression, we should have refill_buffers set. Set it, unless
811 * the job file already changed it.
813 if (o->compress_percentage) {
814 if (o->compress_percentage == 100) {
816 o->compress_percentage = 0;
817 } else if (!fio_option_is_set(o, refill_buffers))
818 o->refill_buffers = 1;
822 * Using a non-uniform random distribution excludes usage of
825 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
826 td->o.norandommap = 1;
829 * If size is set but less than the min block size, complain
831 if (o->size && o->size < td_min_bs(td)) {
832 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
837 * O_ATOMIC implies O_DIRECT
843 * If randseed is set, that overrides randrepeat
845 if (fio_option_is_set(&td->o, rand_seed))
846 td->o.rand_repeatable = 0;
848 if (td_ioengine_flagged(td, FIO_NOEXTEND) && td->o.file_append) {
849 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
853 if (fio_option_is_set(o, gtod_cpu)) {
855 fio_gtod_set_cpu(o->gtod_cpu);
856 fio_gtod_offload = 1;
859 td->loops = o->loops;
863 if (td->o.block_error_hist && td->o.nr_files != 1) {
864 log_err("fio: block error histogram only available "
865 "with a single file per job, but %d files "
866 "provided\n", td->o.nr_files);
874 * This function leaks the buffer
876 char *fio_uint_to_kmg(unsigned int val)
878 char *buf = malloc(32);
879 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
890 snprintf(buf, 32, "%u%c", val, *p);
894 /* External engines are specified by "external:name.o") */
895 static const char *get_engine_name(const char *str)
897 char *p = strstr(str, ":");
903 strip_blank_front(&p);
908 static int exists_and_not_regfile(const char *filename)
912 if (lstat(filename, &sb) == -1)
915 #ifndef WIN32 /* NOT Windows */
916 if (S_ISREG(sb.st_mode))
919 /* \\.\ is the device namespace in Windows, where every file
920 * is a device node */
921 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
928 static void init_rand_file_service(struct thread_data *td)
930 unsigned long nranges = td->o.nr_files << FIO_FSERVICE_SHIFT;
931 const unsigned int seed = td->rand_seeds[FIO_RAND_FILE_OFF];
933 if (td->o.file_service_type == FIO_FSERVICE_ZIPF) {
934 zipf_init(&td->next_file_zipf, nranges, td->zipf_theta, seed);
935 zipf_disable_hash(&td->next_file_zipf);
936 } else if (td->o.file_service_type == FIO_FSERVICE_PARETO) {
937 pareto_init(&td->next_file_zipf, nranges, td->pareto_h, seed);
938 zipf_disable_hash(&td->next_file_zipf);
939 } else if (td->o.file_service_type == FIO_FSERVICE_GAUSS) {
940 gauss_init(&td->next_file_gauss, nranges, td->gauss_dev, seed);
941 gauss_disable_hash(&td->next_file_gauss);
945 void td_fill_verify_state_seed(struct thread_data *td)
949 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
954 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF],
958 static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64)
962 init_rand_seed(&td->bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF], use64);
963 td_fill_verify_state_seed(td);
964 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false);
966 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
967 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
968 else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
969 init_rand_file_service(td);
971 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
972 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
973 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
974 init_rand_seed(&td->poisson_state, td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
975 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false);
976 init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false);
981 if (td->o.rand_repeatable)
982 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
984 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
986 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
987 struct frand_state *s = &td->seq_rand_state[i];
989 init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false);
993 void td_fill_rand_seeds(struct thread_data *td)
997 if (td->o.allrand_repeatable) {
1000 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
1001 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
1005 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
1010 td_fill_rand_seeds_internal(td, use64);
1012 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
1013 frand_copy(&td->buf_state_prev, &td->buf_state);
1017 * Initializes the ioengine configured for a job, if it has not been done so
1020 int ioengine_load(struct thread_data *td)
1025 * Engine has already been loaded.
1029 if (!td->o.ioengine) {
1030 log_err("fio: internal fault, no IO engine specified\n");
1034 engine = get_engine_name(td->o.ioengine);
1035 td->io_ops = load_ioengine(td, engine);
1037 log_err("fio: failed to load engine %s\n", engine);
1041 if (td->io_ops->option_struct_size && td->io_ops->options) {
1043 * In cases where td->eo is set, clone it for a child thread.
1044 * This requires that the parent thread has the same ioengine,
1045 * but that requirement must be enforced by the code which
1046 * cloned the thread.
1048 void *origeo = td->eo;
1050 * Otherwise use the default thread options.
1052 if (!origeo && td != &def_thread && def_thread.eo &&
1053 def_thread.io_ops->options == td->io_ops->options)
1054 origeo = def_thread.eo;
1056 options_init(td->io_ops->options);
1057 td->eo = malloc(td->io_ops->option_struct_size);
1059 * Use the default thread as an option template if this uses the
1060 * same options structure and there are non-default options
1064 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
1065 options_mem_dupe(td->eo, td->io_ops->options);
1067 memset(td->eo, 0, td->io_ops->option_struct_size);
1068 fill_default_options(td->eo, td->io_ops->options);
1070 *(struct thread_data **)td->eo = td;
1074 td->io_ops->flags |= FIO_RAWIO;
1076 td_set_ioengine_flags(td);
1080 static void init_flags(struct thread_data *td)
1082 struct thread_options *o = &td->o;
1084 if (o->verify_backlog)
1085 td->flags |= TD_F_VER_BACKLOG;
1086 if (o->trim_backlog)
1087 td->flags |= TD_F_TRIM_BACKLOG;
1088 if (o->read_iolog_file)
1089 td->flags |= TD_F_READ_IOLOG;
1090 if (o->refill_buffers)
1091 td->flags |= TD_F_REFILL_BUFFERS;
1093 * Always scramble buffers if asked to
1095 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
1096 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1098 * But also scramble buffers, unless we were explicitly asked
1101 if (o->scramble_buffers && !(o->zero_buffers &&
1102 fio_option_is_set(o, zero_buffers)))
1103 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1104 if (o->verify != VERIFY_NONE)
1105 td->flags |= TD_F_VER_NONE;
1107 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
1108 td->flags |= TD_F_NEED_LOCK;
1111 static int setup_random_seeds(struct thread_data *td)
1116 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed))
1117 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
1119 seed = td->o.rand_seed;
1120 for (i = 0; i < 4; i++)
1121 seed *= 0x9e370001UL;
1123 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
1124 td->rand_seeds[i] = seed * td->thread_number + i;
1125 seed *= 0x9e370001UL;
1128 td_fill_rand_seeds(td);
1139 static struct fpre_keyword {
1140 const char *keyword;
1143 } fpre_keywords[] = {
1144 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1145 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1146 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1147 { .keyword = NULL, },
1150 static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
1151 const char *jobname, int jobnum, int filenum)
1153 struct fpre_keyword *f;
1154 char copy[PATH_MAX];
1155 size_t dst_left = PATH_MAX - 1;
1157 if (!o->filename_format || !strlen(o->filename_format)) {
1158 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
1162 for (f = &fpre_keywords[0]; f->keyword; f++)
1163 f->strlen = strlen(f->keyword);
1165 buf[buf_size - 1] = '\0';
1166 strncpy(buf, o->filename_format, buf_size - 1);
1168 memset(copy, 0, sizeof(copy));
1169 for (f = &fpre_keywords[0]; f->keyword; f++) {
1171 size_t pre_len, post_start = 0;
1172 char *str, *dst = copy;
1174 str = strcasestr(buf, f->keyword);
1178 pre_len = str - buf;
1179 if (strlen(str) != f->strlen)
1180 post_start = pre_len + f->strlen;
1183 strncpy(dst, buf, pre_len);
1185 dst_left -= pre_len;
1189 case FPRE_JOBNAME: {
1192 ret = snprintf(dst, dst_left, "%s", jobname);
1195 else if (ret > dst_left) {
1196 log_err("fio: truncated filename\n");
1208 ret = snprintf(dst, dst_left, "%d", jobnum);
1211 else if (ret > dst_left) {
1212 log_err("fio: truncated filename\n");
1221 case FPRE_FILENUM: {
1224 ret = snprintf(dst, dst_left, "%d", filenum);
1227 else if (ret > dst_left) {
1228 log_err("fio: truncated filename\n");
1243 strncpy(dst, buf + post_start, dst_left);
1245 strncpy(buf, copy, buf_size - 1);
1252 int parse_dryrun(void)
1254 return dump_cmdline || parse_only;
1257 static void gen_log_name(char *name, size_t size, const char *logtype,
1258 const char *logname, unsigned int num,
1259 const char *suf, int per_job)
1262 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1264 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1267 static int check_waitees(char *waitee)
1269 struct thread_data *td;
1272 for_each_td(td, i) {
1273 if (td->subjob_number)
1276 ret += !strcmp(td->o.name, waitee);
1282 static bool wait_for_ok(const char *jobname, struct thread_options *o)
1289 if (!strcmp(jobname, o->wait_for)) {
1290 log_err("%s: a job cannot wait for itself (wait_for=%s).\n",
1291 jobname, o->wait_for);
1295 if (!(nw = check_waitees(o->wait_for))) {
1296 log_err("%s: waitee job %s unknown.\n", jobname, o->wait_for);
1301 log_err("%s: multiple waitees %s found,\n"
1302 "please avoid duplicates when using wait_for option.\n",
1303 jobname, o->wait_for);
1311 * Adds a job to the list of things todo. Sanitizes the various options
1312 * to make sure we don't have conflicts, and initializes various
1315 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
1316 int recursed, int client_type)
1319 char fname[PATH_MAX];
1320 int numjobs, file_alloced;
1321 struct thread_options *o = &td->o;
1322 char logname[PATH_MAX + 32];
1325 * the def_thread is just for options, it's not a real job
1327 if (td == &def_thread)
1333 * if we are just dumping the output command line, don't add the job
1335 if (parse_dryrun()) {
1340 td->client_type = client_type;
1342 if (profile_td_init(td))
1345 if (ioengine_load(td))
1349 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1352 if (o->nr_files == 1 && exists_and_not_regfile(jobname))
1353 add_file(td, jobname, job_add_num, 0);
1355 for (i = 0; i < o->nr_files; i++)
1356 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
1360 if (fixup_options(td))
1364 * Belongs to fixup_options, but o->name is not necessarily set as yet
1366 if (!wait_for_ok(jobname, o))
1372 * IO engines only need this for option callbacks, and the address may
1373 * change in subprocesses.
1376 *(struct thread_data **)td->eo = NULL;
1378 if (td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1381 for_each_file(td, f, i)
1382 f->real_file_size = -1ULL;
1385 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1387 td->ts.clat_percentiles = o->clat_percentiles;
1388 td->ts.percentile_precision = o->percentile_precision;
1389 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1391 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1392 td->ts.clat_stat[i].min_val = ULONG_MAX;
1393 td->ts.slat_stat[i].min_val = ULONG_MAX;
1394 td->ts.lat_stat[i].min_val = ULONG_MAX;
1395 td->ts.bw_stat[i].min_val = ULONG_MAX;
1397 td->ddir_seq_nr = o->ddir_seq_nr;
1399 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1400 prev_group_jobs = 0;
1402 if (groupid == INT_MAX) {
1403 log_err("fio: too many groups defined\n");
1408 td->groupid = groupid;
1411 if (setup_random_seeds(td)) {
1412 td_verror(td, errno, "init_random_state");
1419 if (o->lat_log_file) {
1420 struct log_params p = {
1422 .avg_msec = o->log_avg_msec,
1423 .hist_msec = o->log_hist_msec,
1424 .hist_coarseness = o->log_hist_coarseness,
1425 .log_type = IO_LOG_TYPE_LAT,
1426 .log_offset = o->log_offset,
1427 .log_gz = o->log_gz,
1428 .log_gz_store = o->log_gz_store,
1437 gen_log_name(logname, sizeof(logname), "lat", o->lat_log_file,
1438 td->thread_number, suf, o->per_job_logs);
1439 setup_log(&td->lat_log, &p, logname);
1441 gen_log_name(logname, sizeof(logname), "slat", o->lat_log_file,
1442 td->thread_number, suf, o->per_job_logs);
1443 setup_log(&td->slat_log, &p, logname);
1445 gen_log_name(logname, sizeof(logname), "clat", o->lat_log_file,
1446 td->thread_number, suf, o->per_job_logs);
1447 setup_log(&td->clat_log, &p, logname);
1450 if (o->hist_log_file) {
1451 struct log_params p = {
1453 .avg_msec = o->log_avg_msec,
1454 .hist_msec = o->log_hist_msec,
1455 .hist_coarseness = o->log_hist_coarseness,
1456 .log_type = IO_LOG_TYPE_HIST,
1457 .log_offset = o->log_offset,
1458 .log_gz = o->log_gz,
1459 .log_gz_store = o->log_gz_store,
1468 gen_log_name(logname, sizeof(logname), "clat_hist", o->hist_log_file,
1469 td->thread_number, suf, o->per_job_logs);
1470 setup_log(&td->clat_hist_log, &p, logname);
1473 if (o->bw_log_file) {
1474 struct log_params p = {
1476 .avg_msec = o->log_avg_msec,
1477 .hist_msec = o->log_hist_msec,
1478 .hist_coarseness = o->log_hist_coarseness,
1479 .log_type = IO_LOG_TYPE_BW,
1480 .log_offset = o->log_offset,
1481 .log_gz = o->log_gz,
1482 .log_gz_store = o->log_gz_store,
1486 if (fio_option_is_set(o, bw_avg_time))
1487 p.avg_msec = min(o->log_avg_msec, o->bw_avg_time);
1489 o->bw_avg_time = p.avg_msec;
1491 p.hist_msec = o->log_hist_msec;
1492 p.hist_coarseness = o->log_hist_coarseness;
1499 gen_log_name(logname, sizeof(logname), "bw", o->bw_log_file,
1500 td->thread_number, suf, o->per_job_logs);
1501 setup_log(&td->bw_log, &p, logname);
1503 if (o->iops_log_file) {
1504 struct log_params p = {
1506 .avg_msec = o->log_avg_msec,
1507 .hist_msec = o->log_hist_msec,
1508 .hist_coarseness = o->log_hist_coarseness,
1509 .log_type = IO_LOG_TYPE_IOPS,
1510 .log_offset = o->log_offset,
1511 .log_gz = o->log_gz,
1512 .log_gz_store = o->log_gz_store,
1516 if (fio_option_is_set(o, iops_avg_time))
1517 p.avg_msec = min(o->log_avg_msec, o->iops_avg_time);
1519 o->iops_avg_time = p.avg_msec;
1521 p.hist_msec = o->log_hist_msec;
1522 p.hist_coarseness = o->log_hist_coarseness;
1529 gen_log_name(logname, sizeof(logname), "iops", o->iops_log_file,
1530 td->thread_number, suf, o->per_job_logs);
1531 setup_log(&td->iops_log, &p, logname);
1535 o->name = strdup(jobname);
1537 if (output_format & FIO_OUTPUT_NORMAL) {
1539 if (is_backend && !recursed)
1540 fio_server_send_add_job(td);
1542 if (!td_ioengine_flagged(td, FIO_NOIO)) {
1543 char *c1, *c2, *c3, *c4;
1544 char *c5 = NULL, *c6 = NULL;
1546 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1547 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1548 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1549 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1551 if (!o->bs_is_seq_rand) {
1552 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1553 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1556 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1558 ddir_str(o->td_ddir));
1560 if (o->bs_is_seq_rand)
1561 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1564 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1565 c1, c2, c3, c4, c5, c6);
1567 log_info("ioengine=%s, iodepth=%u\n",
1568 td->io_ops->name, o->iodepth);
1577 } else if (job_add_num == 1)
1581 td_steadystate_init(td);
1584 * recurse add identical jobs, clear numjobs and stonewall options
1585 * as they don't apply to sub-jobs
1587 numjobs = o->numjobs;
1589 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
1594 td_new->o.numjobs = 1;
1595 td_new->o.stonewall = 0;
1596 td_new->o.new_group = 0;
1597 td_new->subjob_number = numjobs;
1598 td_new->o.ss_dur = o->ss_dur * 1000000l;
1599 td_new->o.ss_limit = o->ss_limit;
1602 if (td_new->files) {
1604 for_each_file(td_new, f, i) {
1606 sfree(f->file_name);
1609 free(td_new->files);
1610 td_new->files = NULL;
1612 td_new->files_index = 0;
1613 td_new->files_size = 0;
1614 if (td_new->o.filename) {
1615 free(td_new->o.filename);
1616 td_new->o.filename = NULL;
1620 if (add_job(td_new, jobname, numjobs, 1, client_type))
1631 * Parse as if 'o' was a command line
1633 void add_job_opts(const char **o, int client_type)
1635 struct thread_data *td, *td_parent;
1636 int i, in_global = 1;
1640 td_parent = td = NULL;
1642 if (!strncmp(o[i], "name", 4)) {
1645 add_job(td, jobname, 0, 0, client_type);
1647 sprintf(jobname, "%s", o[i] + 5);
1649 if (in_global && !td_parent)
1650 td_parent = get_new_job(1, &def_thread, 0, jobname);
1651 else if (!in_global && !td) {
1653 td_parent = &def_thread;
1654 td = get_new_job(0, td_parent, 0, jobname);
1657 fio_options_parse(td_parent, (char **) &o[i], 1);
1659 fio_options_parse(td, (char **) &o[i], 1);
1664 add_job(td, jobname, 0, 0, client_type);
1667 static int skip_this_section(const char *name)
1671 if (!nr_job_sections)
1673 if (!strncmp(name, "global", 6))
1676 for (i = 0; i < nr_job_sections; i++)
1677 if (!strcmp(job_sections[i], name))
1683 static int is_empty_or_comment(char *line)
1687 for (i = 0; i < strlen(line); i++) {
1692 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1700 * This is our [ini] type file parser.
1702 int __parse_jobs_ini(struct thread_data *td,
1703 char *file, int is_buf, int stonewall_flag, int type,
1704 int nested, char *name, char ***popts, int *aopts, int *nopts)
1706 unsigned int global = 0;
1710 int ret = 0, stonewall;
1713 int inside_skip = 0;
1715 int i, alloc_opts, num_opts;
1717 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1718 assert(td || !nested);
1723 if (!strcmp(file, "-"))
1726 f = fopen(file, "r");
1731 log_err("fio: unable to open '%s' job file\n", file);
1733 td_verror(td, __err, "job file open");
1738 string = malloc(4096);
1741 * it's really 256 + small bit, 280 should suffice
1745 memset(name, 0, 280);
1749 if (nested && popts) {
1751 alloc_opts = *aopts;
1757 opts = malloc(sizeof(char *) * alloc_opts);
1761 stonewall = stonewall_flag;
1764 * if skip_fgets is set, we already have loaded a line we
1769 p = strsep(&file, "\n");
1771 p = fgets(string, 4096, f);
1777 strip_blank_front(&p);
1780 dprint(FD_PARSE, "%s\n", p);
1781 if (is_empty_or_comment(p))
1785 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1789 log_err("fio: option <%s> outside of "
1790 "[] job section\n", p);
1795 name[strlen(name) - 1] = '\0';
1797 if (skip_this_section(name)) {
1803 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1805 global = !strncmp(name, "global", 6);
1811 log_info("--name=%s ", name);
1815 td = get_new_job(global, &def_thread, 0, name);
1822 * Separate multiple job files by a stonewall
1824 if (!global && stonewall) {
1825 td->o.stonewall = stonewall;
1830 memset(opts, 0, alloc_opts * sizeof(char *));
1838 p = strsep(&file, "\n");
1840 p = fgets(string, 4096, f);
1843 dprint(FD_PARSE, "%s", p);
1848 if (is_empty_or_comment(p))
1851 strip_blank_front(&p);
1854 * new section, break out and make sure we don't
1855 * fgets() a new line at the top.
1859 log_err("No new sections in included files\n");
1869 if (!strncmp(p, "include", strlen("include"))) {
1870 char *filename = p + strlen("include") + 1,
1871 *ts, *full_fn = NULL;
1874 * Allow for the include filename
1875 * specification to be relative.
1877 if (access(filename, F_OK) &&
1878 (ts = strrchr(file, '/'))) {
1879 int len = ts - file +
1880 strlen(filename) + 2;
1882 if (!(full_fn = calloc(1, len))) {
1888 file, (ts - file) + 1);
1889 strncpy(full_fn + (ts - file) + 1,
1890 filename, strlen(filename));
1891 full_fn[len - 1] = 0;
1895 ret = __parse_jobs_ini(td, filename, is_buf,
1896 stonewall_flag, type, 1,
1898 &alloc_opts, &num_opts);
1901 log_err("Error %d while parsing "
1902 "include file %s\n",
1915 if (num_opts == alloc_opts) {
1917 opts = realloc(opts,
1918 alloc_opts * sizeof(char *));
1921 opts[num_opts] = strdup(p);
1927 *aopts = alloc_opts;
1932 ret = fio_options_parse(td, opts, num_opts);
1937 ret = add_job(td, name, 0, 0, type);
1939 log_err("fio: job %s dropped\n", name);
1943 for (i = 0; i < num_opts; i++)
1952 while (i < nr_job_sections) {
1953 free(job_sections[i]);
1962 if (!is_buf && f != stdin)
1967 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1969 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
1970 0, NULL, NULL, NULL, NULL);
1973 static int fill_def_thread(void)
1975 memset(&def_thread, 0, sizeof(def_thread));
1976 INIT_FLIST_HEAD(&def_thread.opt_list);
1978 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1979 def_thread.o.error_dump = 1;
1982 * fill default options
1984 fio_fill_default_options(&def_thread);
1988 static void show_debug_categories(void)
1990 #ifdef FIO_INC_DEBUG
1991 struct debug_level *dl = &debug_levels[0];
1992 int curlen, first = 1;
1996 int has_next = (dl + 1)->name != NULL;
1998 if (first || curlen + strlen(dl->name) >= 80) {
2003 curlen += printf("\t\t\t%s", dl->name);
2004 curlen += 3 * (8 - 1);
2006 curlen += printf(",");
2008 curlen += printf("%s", dl->name);
2010 curlen += printf(",");
2019 static void usage(const char *name)
2021 printf("%s\n", fio_version_string);
2022 printf("%s [options] [job options] <job file(s)>\n", name);
2023 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
2024 show_debug_categories();
2025 printf(" --parse-only\t\tParse options only, don't start any IO\n");
2026 printf(" --output\t\tWrite output to file\n");
2027 printf(" --runtime\t\tRuntime in seconds\n");
2028 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
2029 printf(" --minimal\t\tMinimal (terse) output\n");
2030 printf(" --output-format=x\tOutput format (terse,json,json+,normal)\n");
2031 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
2032 printf(" --version\t\tPrint version info and exit\n");
2033 printf(" --help\t\tPrint this page\n");
2034 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
2035 printf(" --crctest\t\tTest speed of checksum functions\n");
2036 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
2038 printf(" --enghelp=engine\tPrint ioengine help, or list"
2039 " available ioengines\n");
2040 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
2042 printf(" --showcmd\t\tTurn a job file into command line options\n");
2043 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
2044 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
2045 printf(" --eta-newline=time\tForce a new line for every 'time'");
2046 printf(" period passed\n");
2047 printf(" --status-interval=t\tForce full status dump every");
2048 printf(" 't' period passed\n");
2049 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
2051 printf(" --section=name\tOnly run specified section in job file\n");
2052 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2054 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
2055 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2056 printf(" --server=args\t\tStart a backend fio server\n");
2057 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
2058 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
2059 printf(" --remote-config=file\tTell fio server to load this local job file\n");
2060 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2061 "\t\t\t(option=system,percpu) or run unit work\n"
2062 "\t\t\tcalibration only (option=calibrate)\n");
2064 printf(" --inflate-log=log\tInflate and output compressed log\n");
2066 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
2067 printf(" --trigger-timeout=t\tExecute trigger af this time\n");
2068 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
2069 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
2070 printf(" --aux-path=path\tUse this path for fio state generated files\n");
2071 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
2072 printf("\n Jens Axboe <jaxboe@fusionio.com>");
2073 printf("\n Jens Axboe <axboe@fb.com>\n");
2076 #ifdef FIO_INC_DEBUG
2077 struct debug_level debug_levels[] = {
2078 { .name = "process",
2079 .help = "Process creation/exit logging",
2080 .shift = FD_PROCESS,
2083 .help = "File related action logging",
2087 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2091 .help = "Memory allocation/freeing logging",
2094 { .name = "blktrace",
2095 .help = "blktrace action logging",
2096 .shift = FD_BLKTRACE,
2099 .help = "IO verification action logging",
2103 .help = "Random generation logging",
2107 .help = "Parser logging",
2110 { .name = "diskutil",
2111 .help = "Disk utility logging actions",
2112 .shift = FD_DISKUTIL,
2115 .help = "Logging related to creating/destroying jobs",
2119 .help = "Mutex logging",
2122 { .name = "profile",
2123 .help = "Logging related to profiles",
2124 .shift = FD_PROFILE,
2127 .help = "Logging related to time keeping functions",
2131 .help = "Network logging",
2135 .help = "Rate logging",
2138 { .name = "compress",
2139 .help = "Log compression logging",
2140 .shift = FD_COMPRESS,
2142 { .name = "steadystate",
2143 .help = "Steady state detection logging",
2144 .shift = FD_STEADYSTATE,
2146 { .name = "helperthread",
2147 .help = "Helper thread logging",
2148 .shift = FD_HELPERTHREAD,
2153 static int set_debug(const char *string)
2155 struct debug_level *dl;
2156 char *p = (char *) string;
2163 if (!strcmp(string, "?") || !strcmp(string, "help")) {
2164 log_info("fio: dumping debug options:");
2165 for (i = 0; debug_levels[i].name; i++) {
2166 dl = &debug_levels[i];
2167 log_info("%s,", dl->name);
2173 while ((opt = strsep(&p, ",")) != NULL) {
2176 if (!strncmp(opt, "all", 3)) {
2177 log_info("fio: set all debug options\n");
2182 for (i = 0; debug_levels[i].name; i++) {
2183 dl = &debug_levels[i];
2184 found = !strncmp(opt, dl->name, strlen(dl->name));
2188 if (dl->shift == FD_JOB) {
2189 opt = strchr(opt, ':');
2191 log_err("fio: missing job number\n");
2195 fio_debug_jobno = atoi(opt);
2196 log_info("fio: set debug jobno %d\n",
2199 log_info("fio: set debug option %s\n", opt);
2200 fio_debug |= (1UL << dl->shift);
2206 log_err("fio: debug mask %s not found\n", opt);
2211 static int set_debug(const char *string)
2213 log_err("fio: debug tracing not included in build\n");
2218 static void fio_options_fill_optstring(void)
2220 char *ostr = cmd_optstr;
2224 while (l_opts[i].name) {
2225 ostr[c++] = l_opts[i].val;
2226 if (l_opts[i].has_arg == required_argument)
2228 else if (l_opts[i].has_arg == optional_argument) {
2237 static int client_flag_set(char c)
2242 while (l_opts[i].name) {
2243 int val = l_opts[i].val;
2245 if (c == (val & 0xff))
2246 return (val & FIO_CLIENT_FLAG);
2254 static void parse_cmd_client(void *client, char *opt)
2256 fio_client_add_cmd_option(client, opt);
2259 static void show_closest_option(const char *name)
2261 int best_option, best_distance;
2264 while (*name == '-')
2268 best_distance = INT_MAX;
2270 while (l_opts[i].name) {
2271 distance = string_distance(name, l_opts[i].name);
2272 if (distance < best_distance) {
2273 best_distance = distance;
2279 if (best_option != -1 && string_distance_ok(name, best_distance))
2280 log_err("Did you mean %s?\n", l_opts[best_option].name);
2283 static int parse_output_format(const char *optarg)
2285 char *p, *orig, *opt;
2288 p = orig = strdup(optarg);
2292 while ((opt = strsep(&p, ",")) != NULL) {
2293 if (!strcmp(opt, "minimal") ||
2294 !strcmp(opt, "terse") ||
2295 !strcmp(opt, "csv"))
2296 output_format |= FIO_OUTPUT_TERSE;
2297 else if (!strcmp(opt, "json"))
2298 output_format |= FIO_OUTPUT_JSON;
2299 else if (!strcmp(opt, "json+"))
2300 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
2301 else if (!strcmp(opt, "normal"))
2302 output_format |= FIO_OUTPUT_NORMAL;
2304 log_err("fio: invalid output format %s\n", opt);
2314 int parse_cmd_line(int argc, char *argv[], int client_type)
2316 struct thread_data *td = NULL;
2317 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
2318 char *ostr = cmd_optstr;
2319 char *pid_file = NULL;
2320 void *cur_client = NULL;
2324 * Reset optind handling, since we may call this multiple times
2329 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2330 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
2331 parse_cmd_client(cur_client, argv[optind - 1]);
2332 c &= ~FIO_CLIENT_FLAG;
2337 smalloc_pool_size = atoi(optarg);
2340 if (check_str_time(optarg, &def_timeout, 1)) {
2341 log_err("fio: failed parsing time %s\n", optarg);
2347 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2355 if (f_out && f_out != stdout)
2358 f_out = fopen(optarg, "w+");
2360 perror("fopen output");
2366 output_format = FIO_OUTPUT_TERSE;
2369 if (parse_output_format(optarg)) {
2370 log_err("fio: failed parsing output-format\n");
2377 output_format |= FIO_OUTPUT_TERSE;
2389 fio_show_option_help(optarg);
2396 fio_show_ioengine_help(optarg);
2410 log_info("%s\n", fio_version_string);
2415 terse_version = atoi(optarg);
2416 if (!(terse_version == 2 || terse_version == 3 ||
2417 terse_version == 4)) {
2418 log_err("fio: bad terse version format\n");
2424 if (!strcmp("always", optarg))
2425 eta_print = FIO_ETA_ALWAYS;
2426 else if (!strcmp("never", optarg))
2427 eta_print = FIO_ETA_NEVER;
2432 if (check_str_time(optarg, &t, 1)) {
2433 log_err("fio: failed parsing eta time %s\n", optarg);
2437 eta_new_line = t / 1000;
2441 if (set_debug(optarg))
2451 if (!strcmp(optarg, "global")) {
2452 log_err("fio: can't use global as only "
2458 new_size = (nr_job_sections + 1) * sizeof(char *);
2459 job_sections = realloc(job_sections, new_size);
2460 job_sections[nr_job_sections] = strdup(optarg);
2466 exit_val = iolog_file_inflate(optarg);
2475 exec_profile = strdup(optarg);
2477 case FIO_GETOPT_JOB: {
2478 const char *opt = l_opts[lidx].name;
2481 if (!strncmp(opt, "name", 4) && td) {
2482 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2489 int is_section = !strncmp(opt, "name", 4);
2492 if (!is_section || !strncmp(val, "global", 6))
2495 if (is_section && skip_this_section(val))
2498 td = get_new_job(global, &def_thread, 1, NULL);
2499 if (!td || ioengine_load(td)) {
2508 fio_options_set_ioengine_opts(l_opts, td);
2511 if ((!val || !strlen(val)) &&
2512 l_opts[lidx].has_arg == required_argument) {
2513 log_err("fio: option %s requires an argument\n", opt);
2516 ret = fio_cmd_option_parse(td, opt, val);
2527 if (!ret && !strcmp(opt, "ioengine")) {
2529 if (ioengine_load(td)) {
2536 fio_options_set_ioengine_opts(l_opts, td);
2540 case FIO_GETOPT_IOENGINE: {
2541 const char *opt = l_opts[lidx].name;
2547 ret = fio_cmd_ioengine_option_parse(td, opt, val);
2554 max_jobs = atoi(optarg);
2555 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2556 log_err("fio: invalid max jobs: %d\n", max_jobs);
2563 #ifndef CONFIG_NO_SHM
2565 log_err("fio: can't be both client and server\n");
2571 fio_server_set_arg(optarg);
2575 log_err("fio: client/server requires SHM support\n");
2583 pid_file = strdup(optarg);
2586 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2587 /* exit on error and calibration only */
2597 log_err("fio: can't be both client and server\n");
2602 /* if --client parameter contains a pathname */
2603 if (0 == access(optarg, R_OK)) {
2604 /* file contains a list of host addrs or names */
2605 char hostaddr[PATH_MAX] = {0};
2607 FILE * hostf = fopen(optarg, "r");
2609 log_err("fio: could not open client list file %s for read\n", optarg);
2614 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2616 * read at most PATH_MAX-1 chars from each
2617 * record in this file
2619 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2620 /* expect EVERY host in file to be valid */
2621 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2622 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2629 break; /* no possibility of job file for "this client only" */
2631 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
2632 log_err("fio: failed adding client %s\n", optarg);
2638 * If the next argument exists and isn't an option,
2639 * assume it's a job file for this client only.
2641 while (optind < argc) {
2642 if (!strncmp(argv[optind], "--", 2) ||
2643 !strncmp(argv[optind], "-", 1))
2646 if (fio_client_add_ini_file(cur_client, argv[optind], false))
2653 if (fio_client_add_ini_file(cur_client, optarg, true)) {
2661 exit_val = fio_monotonic_clocktest(1);
2666 exit_val = fio_crctest(optarg);
2671 if (check_str_time(optarg, &val, 1)) {
2672 log_err("fio: failed parsing time %s\n", optarg);
2677 status_interval = val / 1000;
2683 trigger_file = strdup(optarg);
2688 trigger_cmd = strdup(optarg);
2691 if (trigger_remote_cmd)
2692 free(trigger_remote_cmd);
2693 trigger_remote_cmd = strdup(optarg);
2698 aux_path = strdup(optarg);
2701 if (check_str_time(optarg, &trigger_timeout, 1)) {
2702 log_err("fio: failed parsing time %s\n", optarg);
2706 trigger_timeout /= 1000000;
2709 log_err("%s: unrecognized option '%s'\n", argv[0],
2711 show_closest_option(argv[optind - 1]);
2721 if (do_exit && !(is_backend || nr_clients))
2724 if (nr_clients && fio_clients_connect())
2727 if (is_backend && backend)
2728 return fio_start_server(pid_file);
2734 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2740 while (!ret && optind < argc) {
2742 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2743 ini_file[ini_idx - 1] = strdup(argv[optind]);
2754 int fio_init_options(void)
2759 fio_options_fill_optstring();
2760 fio_options_dup_and_init(l_opts);
2764 if (fill_def_thread())
2770 extern int fio_check_options(struct thread_options *);
2772 int parse_options(int argc, char *argv[])
2774 const int type = FIO_CLIENT_TYPE_CLI;
2777 if (fio_init_options())
2779 if (fio_test_cconv(&def_thread.o))
2780 log_err("fio: failed internal cconv test\n");
2782 job_files = parse_cmd_line(argc, argv, type);
2784 if (job_files > 0) {
2785 for (i = 0; i < job_files; i++) {
2786 if (i && fill_def_thread())
2789 if (fio_clients_send_ini(ini_file[i]))
2792 } else if (!is_backend) {
2793 if (parse_jobs_ini(ini_file[i], 0, i, type))
2798 } else if (nr_clients) {
2799 if (fill_def_thread())
2801 if (fio_clients_send_ini(NULL))
2806 fio_options_free(&def_thread);
2807 filesetup_mem_free();
2809 if (!thread_number) {
2814 if (is_backend || nr_clients)
2819 log_err("No jobs(s) defined\n\n");
2829 if (output_format & FIO_OUTPUT_NORMAL)
2830 log_info("%s\n", fio_version_string);
2835 void options_default_fill(struct thread_options *o)
2837 memcpy(o, &def_thread.o, sizeof(*o));
2840 struct thread_data *get_global_options(void)