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 #include "lib/memcpy.h"
37 const char fio_version_string[] = FIO_VERSION;
39 #define FIO_RANDSEED (0xb1899bedUL)
41 static char **ini_file;
42 static int max_jobs = FIO_MAX_JOBS;
43 static int dump_cmdline;
44 static int parse_only;
46 static struct thread_data def_thread;
47 struct thread_data *threads = NULL;
48 static char **job_sections;
49 static int nr_job_sections;
51 int exitall_on_terminate = 0;
52 int output_format = FIO_OUTPUT_NORMAL;
53 int eta_print = FIO_ETA_AUTO;
54 unsigned int eta_interval_msec = 1000;
58 char *exec_profile = NULL;
59 int warnings_fatal = 0;
60 int terse_version = 3;
67 int status_interval = 0;
69 char *trigger_file = NULL;
70 long long trigger_timeout = 0;
71 char *trigger_cmd = NULL;
72 char *trigger_remote_cmd = NULL;
74 char *aux_path = NULL;
76 static int prev_group_jobs;
78 unsigned long fio_debug = 0;
79 unsigned int fio_debug_jobno = -1;
80 unsigned int *fio_debug_jobp = NULL;
82 static char cmd_optstr[256];
85 #define FIO_CLIENT_FLAG (1 << 16)
88 * Command line options. These will contain the above, plus a few
89 * extra that only pertain to fio itself and not jobs.
91 static struct option l_opts[FIO_NR_OPTIONS] = {
93 .name = (char *) "output",
94 .has_arg = required_argument,
95 .val = 'o' | FIO_CLIENT_FLAG,
98 .name = (char *) "latency-log",
99 .has_arg = required_argument,
100 .val = 'l' | FIO_CLIENT_FLAG,
103 .name = (char *) "bandwidth-log",
104 .has_arg = no_argument,
105 .val = 'b' | FIO_CLIENT_FLAG,
108 .name = (char *) "minimal",
109 .has_arg = no_argument,
110 .val = 'm' | FIO_CLIENT_FLAG,
113 .name = (char *) "output-format",
114 .has_arg = required_argument,
115 .val = 'F' | FIO_CLIENT_FLAG,
118 .name = (char *) "append-terse",
119 .has_arg = optional_argument,
123 .name = (char *) "version",
124 .has_arg = no_argument,
125 .val = 'v' | FIO_CLIENT_FLAG,
128 .name = (char *) "help",
129 .has_arg = no_argument,
130 .val = 'h' | FIO_CLIENT_FLAG,
133 .name = (char *) "cmdhelp",
134 .has_arg = optional_argument,
135 .val = 'c' | FIO_CLIENT_FLAG,
138 .name = (char *) "enghelp",
139 .has_arg = optional_argument,
140 .val = 'i' | FIO_CLIENT_FLAG,
143 .name = (char *) "showcmd",
144 .has_arg = no_argument,
145 .val = 's' | FIO_CLIENT_FLAG,
148 .name = (char *) "readonly",
149 .has_arg = no_argument,
150 .val = 'r' | FIO_CLIENT_FLAG,
153 .name = (char *) "eta",
154 .has_arg = required_argument,
155 .val = 'e' | FIO_CLIENT_FLAG,
158 .name = (char *) "eta-interval",
159 .has_arg = required_argument,
160 .val = 'O' | FIO_CLIENT_FLAG,
163 .name = (char *) "eta-newline",
164 .has_arg = required_argument,
165 .val = 'E' | FIO_CLIENT_FLAG,
168 .name = (char *) "debug",
169 .has_arg = required_argument,
170 .val = 'd' | FIO_CLIENT_FLAG,
173 .name = (char *) "parse-only",
174 .has_arg = no_argument,
175 .val = 'P' | FIO_CLIENT_FLAG,
178 .name = (char *) "section",
179 .has_arg = required_argument,
180 .val = 'x' | FIO_CLIENT_FLAG,
184 .name = (char *) "inflate-log",
185 .has_arg = required_argument,
186 .val = 'X' | FIO_CLIENT_FLAG,
190 .name = (char *) "alloc-size",
191 .has_arg = required_argument,
192 .val = 'a' | FIO_CLIENT_FLAG,
195 .name = (char *) "profile",
196 .has_arg = required_argument,
197 .val = 'p' | FIO_CLIENT_FLAG,
200 .name = (char *) "warnings-fatal",
201 .has_arg = no_argument,
202 .val = 'w' | FIO_CLIENT_FLAG,
205 .name = (char *) "max-jobs",
206 .has_arg = required_argument,
207 .val = 'j' | FIO_CLIENT_FLAG,
210 .name = (char *) "terse-version",
211 .has_arg = required_argument,
212 .val = 'V' | FIO_CLIENT_FLAG,
215 .name = (char *) "server",
216 .has_arg = optional_argument,
219 { .name = (char *) "daemonize",
220 .has_arg = required_argument,
224 .name = (char *) "client",
225 .has_arg = required_argument,
229 .name = (char *) "remote-config",
230 .has_arg = required_argument,
234 .name = (char *) "cpuclock-test",
235 .has_arg = no_argument,
239 .name = (char *) "crctest",
240 .has_arg = optional_argument,
244 .name = (char *) "memcpytest",
245 .has_arg = optional_argument,
249 .name = (char *) "idle-prof",
250 .has_arg = required_argument,
254 .name = (char *) "status-interval",
255 .has_arg = required_argument,
259 .name = (char *) "trigger-file",
260 .has_arg = required_argument,
264 .name = (char *) "trigger-timeout",
265 .has_arg = required_argument,
269 .name = (char *) "trigger",
270 .has_arg = required_argument,
274 .name = (char *) "trigger-remote",
275 .has_arg = required_argument,
279 .name = (char *) "aux-path",
280 .has_arg = required_argument,
288 void free_threads_shm(void)
292 #ifndef CONFIG_NO_SHM
293 struct shmid_ds sbuf;
297 shmctl(shm_id, IPC_RMID, &sbuf);
306 static void free_shm(void)
310 fio_debug_jobp = NULL;
316 free(trigger_remote_cmd);
317 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
319 options_free(fio_options, &def_thread.o);
326 * The thread area is shared between the main process and the job
327 * threads/processes. So setup a shared memory segment that will hold
328 * all the job info. We use the end of the region for keeping track of
329 * open files across jobs, for file sharing.
331 static int setup_thread_area(void)
337 * 1024 is too much on some machines, scale max_jobs if
338 * we get a failure that looks like too large a shm segment
341 size_t size = max_jobs * sizeof(struct thread_data);
343 size += sizeof(unsigned int);
345 #ifndef CONFIG_NO_SHM
346 shm_id = shmget(0, size, IPC_CREAT | 0600);
349 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
354 threads = malloc(size);
362 #ifndef CONFIG_NO_SHM
366 threads = shmat(shm_id, NULL, 0);
367 if (threads == (void *) -1) {
371 if (shm_attach_to_open_removed())
372 shmctl(shm_id, IPC_RMID, NULL);
375 memset(threads, 0, max_jobs * sizeof(struct thread_data));
376 fio_debug_jobp = (unsigned int *)(threads + max_jobs);
377 *fio_debug_jobp = -1;
384 static void dump_print_option(struct print_option *p)
388 if (!strcmp("description", p->name))
393 log_info("--%s%s", p->name, p->value ? "" : " ");
395 log_info("=%s%s%s ", delim, p->value, delim);
398 static void dump_opt_list(struct thread_data *td)
400 struct flist_head *entry;
401 struct print_option *p;
403 if (flist_empty(&td->opt_list))
406 flist_for_each(entry, &td->opt_list) {
407 p = flist_entry(entry, struct print_option, list);
408 dump_print_option(p);
412 static void fio_dump_options_free(struct thread_data *td)
414 while (!flist_empty(&td->opt_list)) {
415 struct print_option *p;
417 p = flist_first_entry(&td->opt_list, struct print_option, list);
418 flist_del_init(&p->list);
425 static void copy_opt_list(struct thread_data *dst, struct thread_data *src)
427 struct flist_head *entry;
429 if (flist_empty(&src->opt_list))
432 flist_for_each(entry, &src->opt_list) {
433 struct print_option *srcp, *dstp;
435 srcp = flist_entry(entry, struct print_option, list);
436 dstp = malloc(sizeof(*dstp));
437 dstp->name = strdup(srcp->name);
439 dstp->value = strdup(srcp->value);
442 flist_add_tail(&dstp->list, &dst->opt_list);
447 * Return a free job structure.
449 static struct thread_data *get_new_job(bool global, struct thread_data *parent,
450 bool preserve_eo, const char *jobname)
452 struct thread_data *td;
456 if (setup_thread_area()) {
457 log_err("error: failed to setup shm segment\n");
460 if (thread_number >= max_jobs) {
461 log_err("error: maximum number of jobs (%d) reached.\n",
466 td = &threads[thread_number++];
469 INIT_FLIST_HEAD(&td->opt_list);
470 if (parent != &def_thread)
471 copy_opt_list(td, parent);
478 td->o.uid = td->o.gid = -1U;
480 dup_files(td, parent);
481 fio_options_mem_dupe(td);
483 profile_add_hooks(td);
485 td->thread_number = thread_number;
486 td->subjob_number = 0;
489 td->o.name = strdup(jobname);
491 if (!parent->o.group_reporting || parent == &def_thread)
497 static void put_job(struct thread_data *td)
499 if (td == &def_thread)
506 log_info("fio: %s\n", td->verror);
508 fio_options_free(td);
509 fio_dump_options_free(td);
516 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
520 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
522 unsigned int bs = td->o.min_bs[ddir];
524 assert(ddir_rw(ddir));
526 if (td->o.rate[ddir])
527 td->rate_bps[ddir] = td->o.rate[ddir];
529 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
531 if (!td->rate_bps[ddir]) {
532 log_err("rate lower than supported\n");
536 td->rate_next_io_time[ddir] = 0;
537 td->rate_io_issue_bytes[ddir] = 0;
538 td->last_usec[ddir] = 0;
542 static int setup_rate(struct thread_data *td)
546 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
547 ret = __setup_rate(td, DDIR_READ);
548 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
549 ret |= __setup_rate(td, DDIR_WRITE);
550 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
551 ret |= __setup_rate(td, DDIR_TRIM);
556 static int fixed_block_size(struct thread_options *o)
558 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
559 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
560 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
561 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
562 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
566 static unsigned long long get_rand_start_delay(struct thread_data *td)
568 unsigned long long delayrange;
572 delayrange = td->o.start_delay_high - td->o.start_delay;
574 frand_max = rand_max(&td->delay_state);
575 r = __rand(&td->delay_state);
576 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
578 delayrange += td->o.start_delay;
585 static unsigned int gcd(unsigned int m, unsigned int n)
590 return gcd(n, m % n);
594 * Lazy way of fixing up options that depend on each other. We could also
595 * define option callback handlers, but this is easier.
597 static int fixup_options(struct thread_data *td)
599 struct thread_options *o = &td->o;
602 #ifndef CONFIG_PSHARED
603 if (!o->use_thread) {
604 log_info("fio: this platform does not support process shared"
605 " mutexes, forcing use of threads. Use the 'thread'"
606 " option to get rid of this warning.\n");
608 ret = warnings_fatal;
612 if (o->write_iolog_file && o->read_iolog_file) {
613 log_err("fio: read iolog overrides write_iolog\n");
614 free(o->write_iolog_file);
615 o->write_iolog_file = NULL;
616 ret = warnings_fatal;
620 * only really works with 1 file
622 if (o->zone_size && o->open_files > 1)
626 * If zone_range isn't specified, backward compatibility dictates it
627 * should be made equal to zone_size.
629 if (o->zone_size && !o->zone_range)
630 o->zone_range = o->zone_size;
633 * Reads can do overwrites, we always need to pre-create the file
638 if (!o->min_bs[DDIR_READ])
639 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
640 if (!o->max_bs[DDIR_READ])
641 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
642 if (!o->min_bs[DDIR_WRITE])
643 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
644 if (!o->max_bs[DDIR_WRITE])
645 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
646 if (!o->min_bs[DDIR_TRIM])
647 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
648 if (!o->max_bs[DDIR_TRIM])
649 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
651 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
652 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
655 * For random IO, allow blockalign offset other than min_bs.
657 if (!o->ba[DDIR_READ] || !td_random(td))
658 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
659 if (!o->ba[DDIR_WRITE] || !td_random(td))
660 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
661 if (!o->ba[DDIR_TRIM] || !td_random(td))
662 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
664 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
665 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
666 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
668 log_err("fio: Any use of blockalign= turns off randommap\n");
670 ret = warnings_fatal;
673 if (!o->file_size_high)
674 o->file_size_high = o->file_size_low;
676 if (o->start_delay_high)
677 o->start_delay = get_rand_start_delay(td);
679 if (o->norandommap && o->verify != VERIFY_NONE
680 && !fixed_block_size(o)) {
681 log_err("fio: norandommap given for variable block sizes, "
683 ret = warnings_fatal;
685 if (o->bs_unaligned && (o->odirect || td_ioengine_flagged(td, FIO_RAWIO)))
686 log_err("fio: bs_unaligned may not work with raw io\n");
689 * thinktime_spin must be less than thinktime
691 if (o->thinktime_spin > o->thinktime)
692 o->thinktime_spin = o->thinktime;
695 * The low water mark cannot be bigger than the iodepth
697 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
698 o->iodepth_low = o->iodepth;
701 * If batch number isn't set, default to the same as iodepth
703 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
704 o->iodepth_batch = o->iodepth;
707 * If max batch complete number isn't set or set incorrectly,
708 * default to the same as iodepth_batch_complete_min
710 if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
711 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
714 * There's no need to check for in-flight overlapping IOs if the job
715 * isn't changing data or the maximum iodepth is guaranteed to be 1
717 if (o->serialize_overlap && !(td->flags & TD_F_READ_IOLOG) &&
718 (!(td_write(td) || td_trim(td)) || o->iodepth == 1))
719 o->serialize_overlap = 0;
721 * Currently can't check for overlaps in offload mode
723 if (o->serialize_overlap && o->io_submit_mode == IO_MODE_OFFLOAD) {
724 log_err("fio: checking for in-flight overlaps when the "
725 "io_submit_mode is offload is not supported\n");
726 o->serialize_overlap = 0;
727 ret = warnings_fatal;
730 if (o->nr_files > td->files_index)
731 o->nr_files = td->files_index;
733 if (o->open_files > o->nr_files || !o->open_files)
734 o->open_files = o->nr_files;
736 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
737 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
738 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
739 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
740 log_err("fio: rate and rate_iops are mutually exclusive\n");
743 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
744 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
745 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
746 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
747 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
748 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
749 log_err("fio: minimum rate exceeds rate\n");
753 if (!o->timeout && o->time_based) {
754 log_err("fio: time_based requires a runtime/timeout setting\n");
756 ret = warnings_fatal;
759 if (o->fill_device && !o->size)
762 if (o->verify != VERIFY_NONE) {
763 if (td_write(td) && o->do_verify && o->numjobs > 1 &&
765 !(o->unique_filename &&
766 strstr(o->filename_format, "$jobname") &&
767 strstr(o->filename_format, "$jobnum") &&
768 strstr(o->filename_format, "$filenum")))) {
769 log_info("fio: multiple writers may overwrite blocks "
770 "that belong to other jobs. This can cause "
771 "verification failures.\n");
772 ret = warnings_fatal;
776 * Warn if verification is requested but no verification of any
777 * kind can be started due to time constraints
779 if (td_write(td) && o->do_verify && o->timeout &&
780 o->time_based && !td_read(td) && !o->verify_backlog) {
781 log_info("fio: verification read phase will never "
782 "start because write phase uses all of "
784 ret = warnings_fatal;
787 if (!fio_option_is_set(o, refill_buffers))
788 o->refill_buffers = 1;
790 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
792 o->verify_interval = o->min_bs[DDIR_WRITE];
795 * Verify interval must be smaller or equal to the
798 if (o->verify_interval > o->min_bs[DDIR_WRITE])
799 o->verify_interval = o->min_bs[DDIR_WRITE];
800 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
801 o->verify_interval = o->min_bs[DDIR_READ];
804 * Verify interval must be a factor or both min and max
807 if (o->verify_interval % o->min_bs[DDIR_WRITE] ||
808 o->verify_interval % o->max_bs[DDIR_WRITE])
809 o->verify_interval = gcd(o->min_bs[DDIR_WRITE],
810 o->max_bs[DDIR_WRITE]);
814 if (o->invalidate_cache)
815 o->invalidate_cache = 0;
816 if (td_ioengine_flagged(td, FIO_PIPEIO)) {
817 log_info("fio: cannot pre-read files with an IO engine"
818 " that isn't seekable. Pre-read disabled.\n");
819 ret = warnings_fatal;
824 if (td_ioengine_flagged(td, FIO_BIT_BASED))
830 #ifndef FIO_HAVE_ANY_FALLOCATE
831 /* Platform doesn't support any fallocate so force it to none */
832 o->fallocate_mode = FIO_FALLOCATE_NONE;
835 #ifndef CONFIG_FDATASYNC
836 if (o->fdatasync_blocks) {
837 log_info("fio: this platform does not support fdatasync()"
838 " falling back to using fsync(). Use the 'fsync'"
839 " option instead of 'fdatasync' to get rid of"
841 o->fsync_blocks = o->fdatasync_blocks;
842 o->fdatasync_blocks = 0;
843 ret = warnings_fatal;
849 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
850 * so fail if we're passed those flags
852 if (td_ioengine_flagged(td, FIO_SYNCIO) && (o->odirect || o->sync_io)) {
853 log_err("fio: Windows does not support direct or non-buffered io with"
854 " the synchronous ioengines. Use the 'windowsaio' ioengine"
855 " with 'direct=1' and 'iodepth=1' instead.\n");
861 * For fully compressible data, just zero them at init time.
862 * It's faster than repeatedly filling it. For non-zero
863 * compression, we should have refill_buffers set. Set it, unless
864 * the job file already changed it.
866 if (o->compress_percentage) {
867 if (o->compress_percentage == 100) {
869 o->compress_percentage = 0;
870 } else if (!fio_option_is_set(o, refill_buffers)) {
871 o->refill_buffers = 1;
872 td->flags |= TD_F_REFILL_BUFFERS;
877 * Using a non-uniform random distribution excludes usage of
880 if (o->random_distribution != FIO_RAND_DIST_RANDOM)
884 * If size is set but less than the min block size, complain
886 if (o->size && o->size < td_min_bs(td)) {
887 log_err("fio: size too small, must not be less than minimum block size: %llu < %u\n",
888 (unsigned long long) o->size, td_min_bs(td));
893 * O_ATOMIC implies O_DIRECT
899 * If randseed is set, that overrides randrepeat
901 if (fio_option_is_set(o, rand_seed))
902 o->rand_repeatable = 0;
904 if (td_ioengine_flagged(td, FIO_NOEXTEND) && o->file_append) {
905 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
909 if (fio_option_is_set(o, gtod_cpu)) {
911 fio_gtod_set_cpu(o->gtod_cpu);
912 fio_gtod_offload = 1;
915 td->loops = o->loops;
919 if (o->block_error_hist && o->nr_files != 1) {
920 log_err("fio: block error histogram only available "
921 "with a single file per job, but %d files "
922 "provided\n", o->nr_files);
926 if (fio_option_is_set(o, clat_percentiles) &&
927 !fio_option_is_set(o, lat_percentiles)) {
928 o->lat_percentiles = !o->clat_percentiles;
929 } else if (fio_option_is_set(o, lat_percentiles) &&
930 !fio_option_is_set(o, clat_percentiles)) {
931 o->clat_percentiles = !o->lat_percentiles;
932 } else if (fio_option_is_set(o, lat_percentiles) &&
933 fio_option_is_set(o, clat_percentiles) &&
934 o->lat_percentiles && o->clat_percentiles) {
935 log_err("fio: lat_percentiles and clat_percentiles are "
936 "mutually exclusive\n");
941 * Fix these up to be nsec internally
943 o->max_latency *= 1000ULL;
944 o->latency_target *= 1000ULL;
945 o->latency_window *= 1000ULL;
950 static void init_rand_file_service(struct thread_data *td)
952 unsigned long nranges = td->o.nr_files << FIO_FSERVICE_SHIFT;
953 const unsigned int seed = td->rand_seeds[FIO_RAND_FILE_OFF];
955 if (td->o.file_service_type == FIO_FSERVICE_ZIPF) {
956 zipf_init(&td->next_file_zipf, nranges, td->zipf_theta, seed);
957 zipf_disable_hash(&td->next_file_zipf);
958 } else if (td->o.file_service_type == FIO_FSERVICE_PARETO) {
959 pareto_init(&td->next_file_zipf, nranges, td->pareto_h, seed);
960 zipf_disable_hash(&td->next_file_zipf);
961 } else if (td->o.file_service_type == FIO_FSERVICE_GAUSS) {
962 gauss_init(&td->next_file_gauss, nranges, td->gauss_dev, seed);
963 gauss_disable_hash(&td->next_file_gauss);
967 void td_fill_verify_state_seed(struct thread_data *td)
971 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
976 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF],
980 static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64)
985 * trimwrite is special in that we need to generate the same
986 * offsets to get the "write after trim" effect. If we are
987 * using bssplit to set buffer length distributions, ensure that
988 * we seed the trim and write generators identically.
990 if (td_trimwrite(td)) {
991 init_rand_seed(&td->bsrange_state[DDIR_READ], td->rand_seeds[FIO_RAND_BS_OFF], use64);
992 init_rand_seed(&td->bsrange_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_BS1_OFF], use64);
993 init_rand_seed(&td->bsrange_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_BS1_OFF], use64);
995 init_rand_seed(&td->bsrange_state[DDIR_READ], td->rand_seeds[FIO_RAND_BS_OFF], use64);
996 init_rand_seed(&td->bsrange_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_BS1_OFF], use64);
997 init_rand_seed(&td->bsrange_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_BS2_OFF], use64);
1000 td_fill_verify_state_seed(td);
1001 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false);
1003 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
1004 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
1005 else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
1006 init_rand_file_service(td);
1008 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
1009 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
1010 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
1011 init_rand_seed(&td->poisson_state[0], td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
1012 init_rand_seed(&td->poisson_state[1], td->rand_seeds[FIO_RAND_POISSON2_OFF], 0);
1013 init_rand_seed(&td->poisson_state[2], td->rand_seeds[FIO_RAND_POISSON3_OFF], 0);
1014 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false);
1015 init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false);
1020 if (td->o.rand_repeatable)
1021 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
1023 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
1025 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1026 struct frand_state *s = &td->seq_rand_state[i];
1028 init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false);
1032 void td_fill_rand_seeds(struct thread_data *td)
1036 if (td->o.allrand_repeatable) {
1039 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
1040 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
1044 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
1049 td_fill_rand_seeds_internal(td, use64);
1051 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
1052 frand_copy(&td->buf_state_prev, &td->buf_state);
1056 * Initializes the ioengine configured for a job, if it has not been done so
1059 int ioengine_load(struct thread_data *td)
1061 if (!td->o.ioengine) {
1062 log_err("fio: internal fault, no IO engine specified\n");
1067 /* An engine is loaded, but the requested ioengine
1070 if (!strcmp(td->io_ops->name, td->o.ioengine)) {
1071 /* The right engine is already loaded */
1075 /* Unload the old engine. */
1079 td->io_ops = load_ioengine(td);
1081 log_err("fio: failed to load engine\n");
1085 if (td->io_ops->option_struct_size && td->io_ops->options) {
1087 * In cases where td->eo is set, clone it for a child thread.
1088 * This requires that the parent thread has the same ioengine,
1089 * but that requirement must be enforced by the code which
1090 * cloned the thread.
1092 void *origeo = td->eo;
1094 * Otherwise use the default thread options.
1096 if (!origeo && td != &def_thread && def_thread.eo &&
1097 def_thread.io_ops->options == td->io_ops->options)
1098 origeo = def_thread.eo;
1100 options_init(td->io_ops->options);
1101 td->eo = malloc(td->io_ops->option_struct_size);
1103 * Use the default thread as an option template if this uses the
1104 * same options structure and there are non-default options
1108 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
1109 options_mem_dupe(td->io_ops->options, td->eo);
1111 memset(td->eo, 0, td->io_ops->option_struct_size);
1112 fill_default_options(td->eo, td->io_ops->options);
1114 *(struct thread_data **)td->eo = td;
1118 td->io_ops->flags |= FIO_RAWIO;
1120 td_set_ioengine_flags(td);
1124 static void init_flags(struct thread_data *td)
1126 struct thread_options *o = &td->o;
1129 if (o->verify_backlog)
1130 td->flags |= TD_F_VER_BACKLOG;
1131 if (o->trim_backlog)
1132 td->flags |= TD_F_TRIM_BACKLOG;
1133 if (o->read_iolog_file)
1134 td->flags |= TD_F_READ_IOLOG;
1135 if (o->refill_buffers)
1136 td->flags |= TD_F_REFILL_BUFFERS;
1138 * Always scramble buffers if asked to
1140 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
1141 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1143 * But also scramble buffers, unless we were explicitly asked
1146 if (o->scramble_buffers && !(o->zero_buffers &&
1147 fio_option_is_set(o, zero_buffers)))
1148 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1149 if (o->verify != VERIFY_NONE)
1150 td->flags |= TD_F_VER_NONE;
1152 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
1153 td->flags |= TD_F_NEED_LOCK;
1155 if (o->mem_type == MEM_CUDA_MALLOC)
1156 td->flags &= ~TD_F_SCRAMBLE_BUFFERS;
1158 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1159 if (option_check_rate(td, i)) {
1160 td->flags |= TD_F_CHECK_RATE;
1166 static int setup_random_seeds(struct thread_data *td)
1171 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed)) {
1172 int ret = init_random_seeds(td->rand_seeds, sizeof(td->rand_seeds));
1174 td_fill_rand_seeds(td);
1178 seed = td->o.rand_seed;
1179 for (i = 0; i < 4; i++)
1180 seed *= 0x9e370001UL;
1182 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
1183 td->rand_seeds[i] = seed * td->thread_number + i;
1184 seed *= 0x9e370001UL;
1187 td_fill_rand_seeds(td);
1198 static struct fpre_keyword {
1199 const char *keyword;
1202 } fpre_keywords[] = {
1203 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1204 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1205 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1206 { .keyword = NULL, },
1209 static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
1210 const char *jobname, int jobnum, int filenum)
1212 struct fpre_keyword *f;
1213 char copy[PATH_MAX];
1214 size_t dst_left = PATH_MAX - 1;
1216 if (!o->filename_format || !strlen(o->filename_format)) {
1217 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
1221 for (f = &fpre_keywords[0]; f->keyword; f++)
1222 f->strlen = strlen(f->keyword);
1224 buf[buf_size - 1] = '\0';
1225 strncpy(buf, o->filename_format, buf_size - 1);
1227 memset(copy, 0, sizeof(copy));
1228 for (f = &fpre_keywords[0]; f->keyword; f++) {
1230 size_t pre_len, post_start = 0;
1231 char *str, *dst = copy;
1233 str = strcasestr(buf, f->keyword);
1237 pre_len = str - buf;
1238 if (strlen(str) != f->strlen)
1239 post_start = pre_len + f->strlen;
1242 strncpy(dst, buf, pre_len);
1244 dst_left -= pre_len;
1248 case FPRE_JOBNAME: {
1251 ret = snprintf(dst, dst_left, "%s", jobname);
1254 else if (ret > dst_left) {
1255 log_err("fio: truncated filename\n");
1267 ret = snprintf(dst, dst_left, "%d", jobnum);
1270 else if (ret > dst_left) {
1271 log_err("fio: truncated filename\n");
1280 case FPRE_FILENUM: {
1283 ret = snprintf(dst, dst_left, "%d", filenum);
1286 else if (ret > dst_left) {
1287 log_err("fio: truncated filename\n");
1302 strncpy(dst, buf + post_start, dst_left);
1304 strncpy(buf, copy, buf_size - 1);
1311 bool parse_dryrun(void)
1313 return dump_cmdline || parse_only;
1316 static void gen_log_name(char *name, size_t size, const char *logtype,
1317 const char *logname, unsigned int num,
1318 const char *suf, int per_job)
1321 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1323 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1326 static int check_waitees(char *waitee)
1328 struct thread_data *td;
1331 for_each_td(td, i) {
1332 if (td->subjob_number)
1335 ret += !strcmp(td->o.name, waitee);
1341 static bool wait_for_ok(const char *jobname, struct thread_options *o)
1348 if (!strcmp(jobname, o->wait_for)) {
1349 log_err("%s: a job cannot wait for itself (wait_for=%s).\n",
1350 jobname, o->wait_for);
1354 if (!(nw = check_waitees(o->wait_for))) {
1355 log_err("%s: waitee job %s unknown.\n", jobname, o->wait_for);
1360 log_err("%s: multiple waitees %s found,\n"
1361 "please avoid duplicates when using wait_for option.\n",
1362 jobname, o->wait_for);
1370 * Adds a job to the list of things todo. Sanitizes the various options
1371 * to make sure we don't have conflicts, and initializes various
1374 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
1375 int recursed, int client_type)
1378 char fname[PATH_MAX];
1379 int numjobs, file_alloced;
1380 struct thread_options *o = &td->o;
1381 char logname[PATH_MAX + 32];
1384 * the def_thread is just for options, it's not a real job
1386 if (td == &def_thread)
1392 * if we are just dumping the output command line, don't add the job
1394 if (parse_dryrun()) {
1399 td->client_type = client_type;
1401 if (profile_td_init(td))
1404 if (ioengine_load(td))
1408 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1411 if (o->nr_files == 1 && exists_and_not_regfile(jobname))
1412 add_file(td, jobname, job_add_num, 0);
1414 for (i = 0; i < o->nr_files; i++)
1415 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
1419 if (fixup_options(td))
1423 * Belongs to fixup_options, but o->name is not necessarily set as yet
1425 if (!wait_for_ok(jobname, o))
1431 * IO engines only need this for option callbacks, and the address may
1432 * change in subprocesses.
1435 *(struct thread_data **)td->eo = NULL;
1437 if (td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1440 for_each_file(td, f, i)
1441 f->real_file_size = -1ULL;
1444 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1446 td->ts.clat_percentiles = o->clat_percentiles;
1447 td->ts.lat_percentiles = o->lat_percentiles;
1448 td->ts.percentile_precision = o->percentile_precision;
1449 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1451 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1452 td->ts.clat_stat[i].min_val = ULONG_MAX;
1453 td->ts.slat_stat[i].min_val = ULONG_MAX;
1454 td->ts.lat_stat[i].min_val = ULONG_MAX;
1455 td->ts.bw_stat[i].min_val = ULONG_MAX;
1456 td->ts.iops_stat[i].min_val = ULONG_MAX;
1458 td->ddir_seq_nr = o->ddir_seq_nr;
1460 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1461 prev_group_jobs = 0;
1463 if (groupid == INT_MAX) {
1464 log_err("fio: too many groups defined\n");
1469 td->groupid = groupid;
1472 if (setup_random_seeds(td)) {
1473 td_verror(td, errno, "setup_random_seeds");
1480 if (o->write_lat_log) {
1481 struct log_params p = {
1483 .avg_msec = o->log_avg_msec,
1484 .hist_msec = o->log_hist_msec,
1485 .hist_coarseness = o->log_hist_coarseness,
1486 .log_type = IO_LOG_TYPE_LAT,
1487 .log_offset = o->log_offset,
1488 .log_gz = o->log_gz,
1489 .log_gz_store = o->log_gz_store,
1491 const char *pre = o->lat_log_file ? o->lat_log_file : o->name;
1499 gen_log_name(logname, sizeof(logname), "lat", pre,
1500 td->thread_number, suf, o->per_job_logs);
1501 setup_log(&td->lat_log, &p, logname);
1503 gen_log_name(logname, sizeof(logname), "slat", pre,
1504 td->thread_number, suf, o->per_job_logs);
1505 setup_log(&td->slat_log, &p, logname);
1507 gen_log_name(logname, sizeof(logname), "clat", pre,
1508 td->thread_number, suf, o->per_job_logs);
1509 setup_log(&td->clat_log, &p, logname);
1512 if (o->write_hist_log) {
1513 struct log_params p = {
1515 .avg_msec = o->log_avg_msec,
1516 .hist_msec = o->log_hist_msec,
1517 .hist_coarseness = o->log_hist_coarseness,
1518 .log_type = IO_LOG_TYPE_HIST,
1519 .log_offset = o->log_offset,
1520 .log_gz = o->log_gz,
1521 .log_gz_store = o->log_gz_store,
1523 const char *pre = o->hist_log_file ? o->hist_log_file : o->name;
1527 if (td->client_type) {
1528 log_err("fio: --write_hist_log requires zlib in client/server mode\n");
1538 gen_log_name(logname, sizeof(logname), "clat_hist", pre,
1539 td->thread_number, suf, o->per_job_logs);
1540 setup_log(&td->clat_hist_log, &p, logname);
1543 if (o->write_bw_log) {
1544 struct log_params p = {
1546 .avg_msec = o->log_avg_msec,
1547 .hist_msec = o->log_hist_msec,
1548 .hist_coarseness = o->log_hist_coarseness,
1549 .log_type = IO_LOG_TYPE_BW,
1550 .log_offset = o->log_offset,
1551 .log_gz = o->log_gz,
1552 .log_gz_store = o->log_gz_store,
1554 const char *pre = o->bw_log_file ? o->bw_log_file : o->name;
1557 if (fio_option_is_set(o, bw_avg_time))
1558 p.avg_msec = min(o->log_avg_msec, o->bw_avg_time);
1560 o->bw_avg_time = p.avg_msec;
1562 p.hist_msec = o->log_hist_msec;
1563 p.hist_coarseness = o->log_hist_coarseness;
1570 gen_log_name(logname, sizeof(logname), "bw", pre,
1571 td->thread_number, suf, o->per_job_logs);
1572 setup_log(&td->bw_log, &p, logname);
1574 if (o->write_iops_log) {
1575 struct log_params p = {
1577 .avg_msec = o->log_avg_msec,
1578 .hist_msec = o->log_hist_msec,
1579 .hist_coarseness = o->log_hist_coarseness,
1580 .log_type = IO_LOG_TYPE_IOPS,
1581 .log_offset = o->log_offset,
1582 .log_gz = o->log_gz,
1583 .log_gz_store = o->log_gz_store,
1585 const char *pre = o->iops_log_file ? o->iops_log_file : o->name;
1588 if (fio_option_is_set(o, iops_avg_time))
1589 p.avg_msec = min(o->log_avg_msec, o->iops_avg_time);
1591 o->iops_avg_time = p.avg_msec;
1593 p.hist_msec = o->log_hist_msec;
1594 p.hist_coarseness = o->log_hist_coarseness;
1601 gen_log_name(logname, sizeof(logname), "iops", pre,
1602 td->thread_number, suf, o->per_job_logs);
1603 setup_log(&td->iops_log, &p, logname);
1607 o->name = strdup(jobname);
1609 if (output_format & FIO_OUTPUT_NORMAL) {
1611 if (is_backend && !recursed)
1612 fio_server_send_add_job(td);
1614 if (!td_ioengine_flagged(td, FIO_NOIO)) {
1615 char *c1, *c2, *c3, *c4;
1616 char *c5 = NULL, *c6 = NULL;
1617 int i2p = is_power_of_2(o->kb_base);
1619 c1 = num2str(o->min_bs[DDIR_READ], o->sig_figs, 1, i2p, N2S_BYTE);
1620 c2 = num2str(o->max_bs[DDIR_READ], o->sig_figs, 1, i2p, N2S_BYTE);
1621 c3 = num2str(o->min_bs[DDIR_WRITE], o->sig_figs, 1, i2p, N2S_BYTE);
1622 c4 = num2str(o->max_bs[DDIR_WRITE], o->sig_figs, 1, i2p, N2S_BYTE);
1624 if (!o->bs_is_seq_rand) {
1625 c5 = num2str(o->min_bs[DDIR_TRIM], o->sig_figs, 1, i2p, N2S_BYTE);
1626 c6 = num2str(o->max_bs[DDIR_TRIM], o->sig_figs, 1, i2p, N2S_BYTE);
1629 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1631 ddir_str(o->td_ddir));
1633 if (o->bs_is_seq_rand)
1634 log_info("bs=(R) %s-%s, (W) %s-%s, bs_is_seq_rand, ",
1637 log_info("bs=(R) %s-%s, (W) %s-%s, (T) %s-%s, ",
1638 c1, c2, c3, c4, c5, c6);
1640 log_info("ioengine=%s, iodepth=%u\n",
1641 td->io_ops->name, o->iodepth);
1650 } else if (job_add_num == 1)
1654 if (td_steadystate_init(td))
1658 * recurse add identical jobs, clear numjobs and stonewall options
1659 * as they don't apply to sub-jobs
1661 numjobs = o->numjobs;
1663 struct thread_data *td_new = get_new_job(false, td, true, jobname);
1668 td_new->o.numjobs = 1;
1669 td_new->o.stonewall = 0;
1670 td_new->o.new_group = 0;
1671 td_new->subjob_number = numjobs;
1672 td_new->o.ss_dur = o->ss_dur * 1000000l;
1673 td_new->o.ss_limit = o->ss_limit;
1676 if (td_new->files) {
1678 for_each_file(td_new, f, i) {
1680 sfree(f->file_name);
1683 free(td_new->files);
1684 td_new->files = NULL;
1686 td_new->files_index = 0;
1687 td_new->files_size = 0;
1688 if (td_new->o.filename) {
1689 free(td_new->o.filename);
1690 td_new->o.filename = NULL;
1694 if (add_job(td_new, jobname, numjobs, 1, client_type))
1705 * Parse as if 'o' was a command line
1707 void add_job_opts(const char **o, int client_type)
1709 struct thread_data *td, *td_parent;
1710 int i, in_global = 1;
1714 td_parent = td = NULL;
1716 if (!strncmp(o[i], "name", 4)) {
1719 add_job(td, jobname, 0, 0, client_type);
1721 sprintf(jobname, "%s", o[i] + 5);
1723 if (in_global && !td_parent)
1724 td_parent = get_new_job(true, &def_thread, false, jobname);
1725 else if (!in_global && !td) {
1727 td_parent = &def_thread;
1728 td = get_new_job(false, td_parent, false, jobname);
1731 fio_options_parse(td_parent, (char **) &o[i], 1);
1733 fio_options_parse(td, (char **) &o[i], 1);
1738 add_job(td, jobname, 0, 0, client_type);
1741 static int skip_this_section(const char *name)
1745 if (!nr_job_sections)
1747 if (!strncmp(name, "global", 6))
1750 for (i = 0; i < nr_job_sections; i++)
1751 if (!strcmp(job_sections[i], name))
1757 static int is_empty_or_comment(char *line)
1761 for (i = 0; i < strlen(line); i++) {
1766 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1774 * This is our [ini] type file parser.
1776 static int __parse_jobs_ini(struct thread_data *td,
1777 char *file, int is_buf, int stonewall_flag, int type,
1778 int nested, char *name, char ***popts, int *aopts, int *nopts)
1780 bool global = false;
1784 int ret = 0, stonewall;
1787 int inside_skip = 0;
1789 int i, alloc_opts, num_opts;
1791 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1792 assert(td || !nested);
1797 if (!strcmp(file, "-"))
1800 f = fopen(file, "r");
1805 log_err("fio: unable to open '%s' job file\n", file);
1807 td_verror(td, __err, "job file open");
1812 string = malloc(4096);
1815 * it's really 256 + small bit, 280 should suffice
1819 memset(name, 0, 280);
1823 if (nested && popts) {
1825 alloc_opts = *aopts;
1831 opts = malloc(sizeof(char *) * alloc_opts);
1835 stonewall = stonewall_flag;
1838 * if skip_fgets is set, we already have loaded a line we
1843 p = strsep(&file, "\n");
1845 p = fgets(string, 4096, f);
1851 strip_blank_front(&p);
1854 dprint(FD_PARSE, "%s\n", p);
1855 if (is_empty_or_comment(p))
1859 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1863 log_err("fio: option <%s> outside of "
1864 "[] job section\n", p);
1869 name[strlen(name) - 1] = '\0';
1871 if (skip_this_section(name)) {
1877 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1879 global = !strncmp(name, "global", 6);
1885 log_info("--name=%s ", name);
1889 td = get_new_job(global, &def_thread, false, name);
1896 * Separate multiple job files by a stonewall
1898 if (!global && stonewall) {
1899 td->o.stonewall = stonewall;
1904 memset(opts, 0, alloc_opts * sizeof(char *));
1912 p = strsep(&file, "\n");
1914 p = fgets(string, 4096, f);
1917 dprint(FD_PARSE, "%s", p);
1922 if (is_empty_or_comment(p))
1925 strip_blank_front(&p);
1928 * new section, break out and make sure we don't
1929 * fgets() a new line at the top.
1933 log_err("No new sections in included files\n");
1943 if (!strncmp(p, "include", strlen("include"))) {
1944 char *filename = p + strlen("include") + 1,
1945 *ts, *full_fn = NULL;
1948 * Allow for the include filename
1949 * specification to be relative.
1951 if (access(filename, F_OK) &&
1952 (ts = strrchr(file, '/'))) {
1953 int len = ts - file +
1954 strlen(filename) + 2;
1956 if (!(full_fn = calloc(1, len))) {
1962 file, (ts - file) + 1);
1963 strncpy(full_fn + (ts - file) + 1,
1964 filename, strlen(filename));
1965 full_fn[len - 1] = 0;
1969 ret = __parse_jobs_ini(td, filename, is_buf,
1970 stonewall_flag, type, 1,
1972 &alloc_opts, &num_opts);
1975 log_err("Error %d while parsing "
1976 "include file %s\n",
1989 if (num_opts == alloc_opts) {
1991 opts = realloc(opts,
1992 alloc_opts * sizeof(char *));
1995 opts[num_opts] = strdup(p);
2001 *aopts = alloc_opts;
2006 ret = fio_options_parse(td, opts, num_opts);
2011 ret = add_job(td, name, 0, 0, type);
2013 log_err("fio: job %s dropped\n", name);
2017 for (i = 0; i < num_opts; i++)
2026 while (i < nr_job_sections) {
2027 free(job_sections[i]);
2036 if (!is_buf && f != stdin)
2041 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
2043 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
2044 0, NULL, NULL, NULL, NULL);
2047 static int fill_def_thread(void)
2049 memset(&def_thread, 0, sizeof(def_thread));
2050 INIT_FLIST_HEAD(&def_thread.opt_list);
2052 fio_getaffinity(getpid(), &def_thread.o.cpumask);
2053 def_thread.o.error_dump = 1;
2056 * fill default options
2058 fio_fill_default_options(&def_thread);
2062 static void show_debug_categories(void)
2064 #ifdef FIO_INC_DEBUG
2065 struct debug_level *dl = &debug_levels[0];
2066 int curlen, first = 1;
2070 int has_next = (dl + 1)->name != NULL;
2072 if (first || curlen + strlen(dl->name) >= 80) {
2077 curlen += printf("\t\t\t%s", dl->name);
2078 curlen += 3 * (8 - 1);
2080 curlen += printf(",");
2082 curlen += printf("%s", dl->name);
2084 curlen += printf(",");
2094 * Following options aren't printed by usage().
2095 * --append-terse - Equivalent to --output-format=terse, see f6a7df53.
2096 * --latency-log - Deprecated option.
2098 static void usage(const char *name)
2100 printf("%s\n", fio_version_string);
2101 printf("%s [options] [job options] <job file(s)>\n", name);
2102 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
2103 show_debug_categories();
2104 printf(" --parse-only\t\tParse options only, don't start any IO\n");
2105 printf(" --output\t\tWrite output to file\n");
2106 printf(" --bandwidth-log\tGenerate aggregate bandwidth logs\n");
2107 printf(" --minimal\t\tMinimal (terse) output\n");
2108 printf(" --output-format=type\tOutput format (terse,json,json+,normal)\n");
2109 printf(" --terse-version=type\tSet terse version output format"
2110 " (default 3, or 2 or 4)\n");
2111 printf(" --version\t\tPrint version info and exit\n");
2112 printf(" --help\t\tPrint this page\n");
2113 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
2114 printf(" --crctest=[type]\tTest speed of checksum functions\n");
2115 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
2117 printf(" --enghelp=engine\tPrint ioengine help, or list"
2118 " available ioengines\n");
2119 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
2121 printf(" --showcmd\t\tTurn a job file into command line options\n");
2122 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
2123 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
2124 printf(" --eta-newline=time\tForce a new line for every 'time'");
2125 printf(" period passed\n");
2126 printf(" --status-interval=t\tForce full status dump every");
2127 printf(" 't' period passed\n");
2128 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
2130 printf(" --section=name\tOnly run specified section in job file,"
2131 " multiple sections can be specified\n");
2132 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2134 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
2135 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2136 printf(" --server=args\t\tStart a backend fio server\n");
2137 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
2138 printf(" --client=hostname\tTalk to remote backend(s) fio server at hostname\n");
2139 printf(" --remote-config=file\tTell fio server to load this local job file\n");
2140 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2141 "\t\t\t(option=system,percpu) or run unit work\n"
2142 "\t\t\tcalibration only (option=calibrate)\n");
2144 printf(" --inflate-log=log\tInflate and output compressed log\n");
2146 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
2147 printf(" --trigger-timeout=t\tExecute trigger at this time\n");
2148 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
2149 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
2150 printf(" --aux-path=path\tUse this path for fio state generated files\n");
2151 printf("\nFio was written by Jens Axboe <axboe@kernel.dk>\n");
2154 #ifdef FIO_INC_DEBUG
2155 struct debug_level debug_levels[] = {
2156 { .name = "process",
2157 .help = "Process creation/exit logging",
2158 .shift = FD_PROCESS,
2161 .help = "File related action logging",
2165 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2169 .help = "Memory allocation/freeing logging",
2172 { .name = "blktrace",
2173 .help = "blktrace action logging",
2174 .shift = FD_BLKTRACE,
2177 .help = "IO verification action logging",
2181 .help = "Random generation logging",
2185 .help = "Parser logging",
2188 { .name = "diskutil",
2189 .help = "Disk utility logging actions",
2190 .shift = FD_DISKUTIL,
2193 .help = "Logging related to creating/destroying jobs",
2197 .help = "Mutex logging",
2200 { .name = "profile",
2201 .help = "Logging related to profiles",
2202 .shift = FD_PROFILE,
2205 .help = "Logging related to time keeping functions",
2209 .help = "Network logging",
2213 .help = "Rate logging",
2216 { .name = "compress",
2217 .help = "Log compression logging",
2218 .shift = FD_COMPRESS,
2220 { .name = "steadystate",
2221 .help = "Steady state detection logging",
2222 .shift = FD_STEADYSTATE,
2224 { .name = "helperthread",
2225 .help = "Helper thread logging",
2226 .shift = FD_HELPERTHREAD,
2231 static int set_debug(const char *string)
2233 struct debug_level *dl;
2234 char *p = (char *) string;
2241 if (!strcmp(string, "?") || !strcmp(string, "help")) {
2242 log_info("fio: dumping debug options:");
2243 for (i = 0; debug_levels[i].name; i++) {
2244 dl = &debug_levels[i];
2245 log_info("%s,", dl->name);
2251 while ((opt = strsep(&p, ",")) != NULL) {
2254 if (!strncmp(opt, "all", 3)) {
2255 log_info("fio: set all debug options\n");
2260 for (i = 0; debug_levels[i].name; i++) {
2261 dl = &debug_levels[i];
2262 found = !strncmp(opt, dl->name, strlen(dl->name));
2266 if (dl->shift == FD_JOB) {
2267 opt = strchr(opt, ':');
2269 log_err("fio: missing job number\n");
2273 fio_debug_jobno = atoi(opt);
2274 log_info("fio: set debug jobno %d\n",
2277 log_info("fio: set debug option %s\n", opt);
2278 fio_debug |= (1UL << dl->shift);
2284 log_err("fio: debug mask %s not found\n", opt);
2289 static int set_debug(const char *string)
2291 log_err("fio: debug tracing not included in build\n");
2296 static void fio_options_fill_optstring(void)
2298 char *ostr = cmd_optstr;
2302 while (l_opts[i].name) {
2303 ostr[c++] = l_opts[i].val;
2304 if (l_opts[i].has_arg == required_argument)
2306 else if (l_opts[i].has_arg == optional_argument) {
2315 static int client_flag_set(char c)
2320 while (l_opts[i].name) {
2321 int val = l_opts[i].val;
2323 if (c == (val & 0xff))
2324 return (val & FIO_CLIENT_FLAG);
2332 static void parse_cmd_client(void *client, char *opt)
2334 fio_client_add_cmd_option(client, opt);
2337 static void show_closest_option(const char *name)
2339 int best_option, best_distance;
2342 while (*name == '-')
2346 best_distance = INT_MAX;
2348 while (l_opts[i].name) {
2349 distance = string_distance(name, l_opts[i].name);
2350 if (distance < best_distance) {
2351 best_distance = distance;
2357 if (best_option != -1 && string_distance_ok(name, best_distance))
2358 log_err("Did you mean %s?\n", l_opts[best_option].name);
2361 static int parse_output_format(const char *optarg)
2363 char *p, *orig, *opt;
2366 p = orig = strdup(optarg);
2370 while ((opt = strsep(&p, ",")) != NULL) {
2371 if (!strcmp(opt, "minimal") ||
2372 !strcmp(opt, "terse") ||
2373 !strcmp(opt, "csv"))
2374 output_format |= FIO_OUTPUT_TERSE;
2375 else if (!strcmp(opt, "json"))
2376 output_format |= FIO_OUTPUT_JSON;
2377 else if (!strcmp(opt, "json+"))
2378 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
2379 else if (!strcmp(opt, "normal"))
2380 output_format |= FIO_OUTPUT_NORMAL;
2382 log_err("fio: invalid output format %s\n", opt);
2392 int parse_cmd_line(int argc, char *argv[], int client_type)
2394 struct thread_data *td = NULL;
2395 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
2396 char *ostr = cmd_optstr;
2397 char *pid_file = NULL;
2398 void *cur_client = NULL;
2402 * Reset optind handling, since we may call this multiple times
2407 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2408 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
2409 parse_cmd_client(cur_client, argv[optind - 1]);
2410 c &= ~FIO_CLIENT_FLAG;
2415 smalloc_pool_size = atoi(optarg);
2416 smalloc_pool_size <<= 10;
2420 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2430 if (f_out && f_out != stdout)
2433 tmp = fopen(optarg, "w+");
2435 log_err("fio: output file open error: %s\n", strerror(errno));
2440 f_err = f_out = tmp;
2444 output_format = FIO_OUTPUT_TERSE;
2447 if (parse_output_format(optarg)) {
2448 log_err("fio: failed parsing output-format\n");
2455 output_format |= FIO_OUTPUT_TERSE;
2467 fio_show_option_help(optarg);
2474 fio_show_ioengine_help(optarg);
2488 log_info("%s\n", fio_version_string);
2493 terse_version = atoi(optarg);
2494 if (!(terse_version >= 2 && terse_version <= 5)) {
2495 log_err("fio: bad terse version format\n");
2501 if (!strcmp("always", optarg))
2502 eta_print = FIO_ETA_ALWAYS;
2503 else if (!strcmp("never", optarg))
2504 eta_print = FIO_ETA_NEVER;
2509 if (check_str_time(optarg, &t, 1)) {
2510 log_err("fio: failed parsing eta time %s\n", optarg);
2515 eta_new_line = t / 1000;
2516 if (!eta_new_line) {
2517 log_err("fio: eta new line time too short\n");
2526 if (check_str_time(optarg, &t, 1)) {
2527 log_err("fio: failed parsing eta interval %s\n", optarg);
2532 eta_interval_msec = t / 1000;
2533 if (eta_interval_msec < DISK_UTIL_MSEC) {
2534 log_err("fio: eta interval time too short (%umsec min)\n", DISK_UTIL_MSEC);
2541 if (set_debug(optarg))
2551 if (!strcmp(optarg, "global")) {
2552 log_err("fio: can't use global as only "
2558 new_size = (nr_job_sections + 1) * sizeof(char *);
2559 job_sections = realloc(job_sections, new_size);
2560 job_sections[nr_job_sections] = strdup(optarg);
2566 exit_val = iolog_file_inflate(optarg);
2575 exec_profile = strdup(optarg);
2577 case FIO_GETOPT_JOB: {
2578 const char *opt = l_opts[lidx].name;
2581 if (!strncmp(opt, "name", 4) && td) {
2582 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2589 int is_section = !strncmp(opt, "name", 4);
2592 if (!is_section || !strncmp(val, "global", 6))
2595 if (is_section && skip_this_section(val))
2598 td = get_new_job(global, &def_thread, true, NULL);
2599 if (!td || ioengine_load(td)) {
2608 fio_options_set_ioengine_opts(l_opts, td);
2611 if ((!val || !strlen(val)) &&
2612 l_opts[lidx].has_arg == required_argument) {
2613 log_err("fio: option %s requires an argument\n", opt);
2616 ret = fio_cmd_option_parse(td, opt, val);
2627 if (!ret && !strcmp(opt, "ioengine")) {
2628 if (ioengine_load(td)) {
2635 fio_options_set_ioengine_opts(l_opts, td);
2639 case FIO_GETOPT_IOENGINE: {
2640 const char *opt = l_opts[lidx].name;
2646 ret = fio_cmd_ioengine_option_parse(td, opt, val);
2653 max_jobs = atoi(optarg);
2654 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2655 log_err("fio: invalid max jobs: %d\n", max_jobs);
2662 #ifndef CONFIG_NO_SHM
2664 log_err("fio: can't be both client and server\n");
2670 fio_server_set_arg(optarg);
2674 log_err("fio: client/server requires SHM support\n");
2682 pid_file = strdup(optarg);
2685 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2686 /* exit on error and calibration only */
2696 log_err("fio: can't be both client and server\n");
2701 /* if --client parameter contains a pathname */
2702 if (0 == access(optarg, R_OK)) {
2703 /* file contains a list of host addrs or names */
2704 char hostaddr[PATH_MAX] = {0};
2706 FILE * hostf = fopen(optarg, "r");
2708 log_err("fio: could not open client list file %s for read\n", optarg);
2713 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2715 * read at most PATH_MAX-1 chars from each
2716 * record in this file
2718 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2719 /* expect EVERY host in file to be valid */
2720 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2721 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2728 break; /* no possibility of job file for "this client only" */
2730 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
2731 log_err("fio: failed adding client %s\n", optarg);
2737 * If the next argument exists and isn't an option,
2738 * assume it's a job file for this client only.
2740 while (optind < argc) {
2741 if (!strncmp(argv[optind], "--", 2) ||
2742 !strncmp(argv[optind], "-", 1))
2745 if (fio_client_add_ini_file(cur_client, argv[optind], false))
2752 if (fio_client_add_ini_file(cur_client, optarg, true)) {
2760 exit_val = fio_monotonic_clocktest(1);
2765 exit_val = fio_crctest(optarg);
2770 exit_val = fio_memcpy_test(optarg);
2775 if (check_str_time(optarg, &val, 1)) {
2776 log_err("fio: failed parsing time %s\n", optarg);
2782 log_err("fio: status interval too small\n");
2786 status_interval = val / 1000;
2792 trigger_file = strdup(optarg);
2797 trigger_cmd = strdup(optarg);
2800 if (trigger_remote_cmd)
2801 free(trigger_remote_cmd);
2802 trigger_remote_cmd = strdup(optarg);
2807 aux_path = strdup(optarg);
2810 if (check_str_time(optarg, &trigger_timeout, 1)) {
2811 log_err("fio: failed parsing time %s\n", optarg);
2815 trigger_timeout /= 1000000;
2818 log_err("%s: unrecognized option '%s'\n", argv[0],
2820 show_closest_option(argv[optind - 1]);
2830 if (do_exit && !(is_backend || nr_clients))
2833 if (nr_clients && fio_clients_connect())
2836 if (is_backend && backend)
2837 return fio_start_server(pid_file);
2843 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2849 while (!ret && optind < argc) {
2851 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2852 ini_file[ini_idx - 1] = strdup(argv[optind]);
2860 int fio_init_options(void)
2865 fio_options_fill_optstring();
2866 fio_options_dup_and_init(l_opts);
2870 if (fill_def_thread())
2876 extern int fio_check_options(struct thread_options *);
2878 int parse_options(int argc, char *argv[])
2880 const int type = FIO_CLIENT_TYPE_CLI;
2883 if (fio_init_options())
2885 if (fio_test_cconv(&def_thread.o))
2886 log_err("fio: failed internal cconv test\n");
2888 job_files = parse_cmd_line(argc, argv, type);
2890 if (job_files > 0) {
2891 for (i = 0; i < job_files; i++) {
2892 if (i && fill_def_thread())
2895 if (fio_clients_send_ini(ini_file[i]))
2898 } else if (!is_backend) {
2899 if (parse_jobs_ini(ini_file[i], 0, i, type))
2904 } else if (nr_clients) {
2905 if (fill_def_thread())
2907 if (fio_clients_send_ini(NULL))
2912 fio_options_free(&def_thread);
2913 filesetup_mem_free();
2915 if (!thread_number) {
2920 if (is_backend || nr_clients)
2925 log_err("No job(s) defined\n\n");
2930 if (output_format & FIO_OUTPUT_NORMAL)
2931 log_info("%s\n", fio_version_string);
2936 void options_default_fill(struct thread_options *o)
2938 memcpy(o, &def_thread.o, sizeof(*o));
2941 struct thread_data *get_global_options(void)