2 * This file contains job initialization and setup functions.
11 #include <sys/types.h>
13 #ifdef CONFIG_VALGRIND_DEV
14 #include <valgrind/drd.h>
16 #define DRD_IGNORE_VAR(x) do { } while (0)
20 #ifndef FIO_NO_HAVE_SHM_H
32 #include "steadystate.h"
34 #include "oslib/getopt.h"
35 #include "oslib/strcasestr.h"
39 #include "lib/memcpy.h"
41 const char fio_version_string[] = FIO_VERSION;
43 #define FIO_RANDSEED (0xb1899bedUL)
45 static char **ini_file;
46 static int max_jobs = FIO_MAX_JOBS;
47 static int dump_cmdline;
48 static int parse_only;
50 static struct thread_data def_thread;
51 struct thread_data *threads = NULL;
52 static char **job_sections;
53 static int nr_job_sections;
55 int exitall_on_terminate = 0;
56 int output_format = FIO_OUTPUT_NORMAL;
57 int eta_print = FIO_ETA_AUTO;
58 unsigned int eta_interval_msec = 1000;
62 char *exec_profile = NULL;
63 int warnings_fatal = 0;
64 int terse_version = 3;
71 int status_interval = 0;
73 char *trigger_file = NULL;
74 long long trigger_timeout = 0;
75 char *trigger_cmd = NULL;
76 char *trigger_remote_cmd = NULL;
78 char *aux_path = NULL;
80 static int prev_group_jobs;
82 unsigned long fio_debug = 0;
83 unsigned int fio_debug_jobno = -1;
84 unsigned int *fio_debug_jobp = NULL;
85 unsigned int *fio_warned = NULL;
87 static char cmd_optstr[256];
90 #define FIO_CLIENT_FLAG (1 << 16)
93 * Command line options. These will contain the above, plus a few
94 * extra that only pertain to fio itself and not jobs.
96 static struct option l_opts[FIO_NR_OPTIONS] = {
98 .name = (char *) "output",
99 .has_arg = required_argument,
100 .val = 'o' | FIO_CLIENT_FLAG,
103 .name = (char *) "latency-log",
104 .has_arg = required_argument,
105 .val = 'l' | FIO_CLIENT_FLAG,
108 .name = (char *) "bandwidth-log",
109 .has_arg = no_argument,
110 .val = 'b' | FIO_CLIENT_FLAG,
113 .name = (char *) "minimal",
114 .has_arg = no_argument,
115 .val = 'm' | FIO_CLIENT_FLAG,
118 .name = (char *) "output-format",
119 .has_arg = required_argument,
120 .val = 'F' | FIO_CLIENT_FLAG,
123 .name = (char *) "append-terse",
124 .has_arg = optional_argument,
128 .name = (char *) "version",
129 .has_arg = no_argument,
130 .val = 'v' | FIO_CLIENT_FLAG,
133 .name = (char *) "help",
134 .has_arg = no_argument,
135 .val = 'h' | FIO_CLIENT_FLAG,
138 .name = (char *) "cmdhelp",
139 .has_arg = optional_argument,
140 .val = 'c' | FIO_CLIENT_FLAG,
143 .name = (char *) "enghelp",
144 .has_arg = optional_argument,
145 .val = 'i' | FIO_CLIENT_FLAG,
148 .name = (char *) "showcmd",
149 .has_arg = no_argument,
150 .val = 's' | FIO_CLIENT_FLAG,
153 .name = (char *) "readonly",
154 .has_arg = no_argument,
155 .val = 'r' | FIO_CLIENT_FLAG,
158 .name = (char *) "eta",
159 .has_arg = required_argument,
160 .val = 'e' | FIO_CLIENT_FLAG,
163 .name = (char *) "eta-interval",
164 .has_arg = required_argument,
165 .val = 'O' | FIO_CLIENT_FLAG,
168 .name = (char *) "eta-newline",
169 .has_arg = required_argument,
170 .val = 'E' | FIO_CLIENT_FLAG,
173 .name = (char *) "debug",
174 .has_arg = required_argument,
175 .val = 'd' | FIO_CLIENT_FLAG,
178 .name = (char *) "parse-only",
179 .has_arg = no_argument,
180 .val = 'P' | FIO_CLIENT_FLAG,
183 .name = (char *) "section",
184 .has_arg = required_argument,
185 .val = 'x' | FIO_CLIENT_FLAG,
189 .name = (char *) "inflate-log",
190 .has_arg = required_argument,
191 .val = 'X' | FIO_CLIENT_FLAG,
195 .name = (char *) "alloc-size",
196 .has_arg = required_argument,
197 .val = 'a' | FIO_CLIENT_FLAG,
200 .name = (char *) "profile",
201 .has_arg = required_argument,
202 .val = 'p' | FIO_CLIENT_FLAG,
205 .name = (char *) "warnings-fatal",
206 .has_arg = no_argument,
207 .val = 'w' | FIO_CLIENT_FLAG,
210 .name = (char *) "max-jobs",
211 .has_arg = required_argument,
212 .val = 'j' | FIO_CLIENT_FLAG,
215 .name = (char *) "terse-version",
216 .has_arg = required_argument,
217 .val = 'V' | FIO_CLIENT_FLAG,
220 .name = (char *) "server",
221 .has_arg = optional_argument,
224 { .name = (char *) "daemonize",
225 .has_arg = required_argument,
229 .name = (char *) "client",
230 .has_arg = required_argument,
234 .name = (char *) "remote-config",
235 .has_arg = required_argument,
239 .name = (char *) "cpuclock-test",
240 .has_arg = no_argument,
244 .name = (char *) "crctest",
245 .has_arg = optional_argument,
249 .name = (char *) "memcpytest",
250 .has_arg = optional_argument,
254 .name = (char *) "idle-prof",
255 .has_arg = required_argument,
259 .name = (char *) "status-interval",
260 .has_arg = required_argument,
264 .name = (char *) "trigger-file",
265 .has_arg = required_argument,
269 .name = (char *) "trigger-timeout",
270 .has_arg = required_argument,
274 .name = (char *) "trigger",
275 .has_arg = required_argument,
279 .name = (char *) "trigger-remote",
280 .has_arg = required_argument,
284 .name = (char *) "aux-path",
285 .has_arg = required_argument,
293 void free_threads_shm(void)
297 #ifndef CONFIG_NO_SHM
298 struct shmid_ds sbuf;
302 shmctl(shm_id, IPC_RMID, &sbuf);
311 static void free_shm(void)
315 fio_debug_jobp = NULL;
322 free(trigger_remote_cmd);
323 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
325 options_free(fio_options, &def_thread.o);
332 * The thread area is shared between the main process and the job
333 * threads/processes. So setup a shared memory segment that will hold
334 * all the job info. We use the end of the region for keeping track of
335 * open files across jobs, for file sharing.
337 static int setup_thread_area(void)
345 * 1024 is too much on some machines, scale max_jobs if
346 * we get a failure that looks like too large a shm segment
349 size_t size = max_jobs * sizeof(struct thread_data);
351 size += 2 * sizeof(unsigned int);
353 #ifndef CONFIG_NO_SHM
354 shm_id = shmget(0, size, IPC_CREAT | 0600);
357 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
362 threads = malloc(size);
370 #ifndef CONFIG_NO_SHM
374 threads = shmat(shm_id, NULL, 0);
375 if (threads == (void *) -1) {
379 if (shm_attach_to_open_removed())
380 shmctl(shm_id, IPC_RMID, NULL);
383 memset(threads, 0, max_jobs * sizeof(struct thread_data));
384 for (i = 0; i < max_jobs; i++)
385 DRD_IGNORE_VAR(threads[i]);
386 fio_debug_jobp = (unsigned int *)(threads + max_jobs);
387 *fio_debug_jobp = -1;
388 fio_warned = fio_debug_jobp + 1;
396 static void dump_print_option(struct print_option *p)
400 if (!strcmp("description", p->name))
405 log_info("--%s%s", p->name, p->value ? "" : " ");
407 log_info("=%s%s%s ", delim, p->value, delim);
410 static void dump_opt_list(struct thread_data *td)
412 struct flist_head *entry;
413 struct print_option *p;
415 if (flist_empty(&td->opt_list))
418 flist_for_each(entry, &td->opt_list) {
419 p = flist_entry(entry, struct print_option, list);
420 dump_print_option(p);
424 static void fio_dump_options_free(struct thread_data *td)
426 while (!flist_empty(&td->opt_list)) {
427 struct print_option *p;
429 p = flist_first_entry(&td->opt_list, struct print_option, list);
430 flist_del_init(&p->list);
437 static void copy_opt_list(struct thread_data *dst, struct thread_data *src)
439 struct flist_head *entry;
441 if (flist_empty(&src->opt_list))
444 flist_for_each(entry, &src->opt_list) {
445 struct print_option *srcp, *dstp;
447 srcp = flist_entry(entry, struct print_option, list);
448 dstp = malloc(sizeof(*dstp));
449 dstp->name = strdup(srcp->name);
451 dstp->value = strdup(srcp->value);
454 flist_add_tail(&dstp->list, &dst->opt_list);
459 * Return a free job structure.
461 static struct thread_data *get_new_job(bool global, struct thread_data *parent,
462 bool preserve_eo, const char *jobname)
464 struct thread_data *td;
468 if (setup_thread_area()) {
469 log_err("error: failed to setup shm segment\n");
472 if (thread_number >= max_jobs) {
473 log_err("error: maximum number of jobs (%d) reached.\n",
478 td = &threads[thread_number++];
481 INIT_FLIST_HEAD(&td->opt_list);
482 if (parent != &def_thread)
483 copy_opt_list(td, parent);
490 td->o.uid = td->o.gid = -1U;
492 dup_files(td, parent);
493 fio_options_mem_dupe(td);
495 profile_add_hooks(td);
497 td->thread_number = thread_number;
498 td->subjob_number = 0;
501 td->o.name = strdup(jobname);
503 if (!parent->o.group_reporting || parent == &def_thread)
509 static void put_job(struct thread_data *td)
511 if (td == &def_thread)
518 log_info("fio: %s\n", td->verror);
520 fio_options_free(td);
521 fio_dump_options_free(td);
528 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
532 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
534 unsigned int bs = td->o.min_bs[ddir];
536 assert(ddir_rw(ddir));
538 if (td->o.rate[ddir])
539 td->rate_bps[ddir] = td->o.rate[ddir];
541 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
543 if (!td->rate_bps[ddir]) {
544 log_err("rate lower than supported\n");
548 td->rate_next_io_time[ddir] = 0;
549 td->rate_io_issue_bytes[ddir] = 0;
550 td->last_usec[ddir] = 0;
554 static int setup_rate(struct thread_data *td)
558 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
559 ret = __setup_rate(td, DDIR_READ);
560 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
561 ret |= __setup_rate(td, DDIR_WRITE);
562 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
563 ret |= __setup_rate(td, DDIR_TRIM);
568 static int fixed_block_size(struct thread_options *o)
570 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
571 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
572 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
573 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
574 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
578 static unsigned long long get_rand_start_delay(struct thread_data *td)
580 unsigned long long delayrange;
584 delayrange = td->o.start_delay_high - td->o.start_delay;
586 frand_max = rand_max(&td->delay_state);
587 r = __rand(&td->delay_state);
588 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
590 delayrange += td->o.start_delay;
597 static unsigned int gcd(unsigned int m, unsigned int n)
602 return gcd(n, m % n);
606 * Lazy way of fixing up options that depend on each other. We could also
607 * define option callback handlers, but this is easier.
609 static int fixup_options(struct thread_data *td)
611 struct thread_options *o = &td->o;
614 #ifndef CONFIG_PSHARED
615 if (!o->use_thread) {
616 log_info("fio: this platform does not support process shared"
617 " mutexes, forcing use of threads. Use the 'thread'"
618 " option to get rid of this warning.\n");
620 ret = warnings_fatal;
624 if (o->write_iolog_file && o->read_iolog_file) {
625 log_err("fio: read iolog overrides write_iolog\n");
626 free(o->write_iolog_file);
627 o->write_iolog_file = NULL;
628 ret = warnings_fatal;
632 * only really works with 1 file
634 if (o->zone_size && o->open_files > 1)
638 * If zone_range isn't specified, backward compatibility dictates it
639 * should be made equal to zone_size.
641 if (o->zone_size && !o->zone_range)
642 o->zone_range = o->zone_size;
645 * Reads can do overwrites, we always need to pre-create the file
650 if (!o->min_bs[DDIR_READ])
651 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
652 if (!o->max_bs[DDIR_READ])
653 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
654 if (!o->min_bs[DDIR_WRITE])
655 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
656 if (!o->max_bs[DDIR_WRITE])
657 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
658 if (!o->min_bs[DDIR_TRIM])
659 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
660 if (!o->max_bs[DDIR_TRIM])
661 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
663 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
664 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
667 * For random IO, allow blockalign offset other than min_bs.
669 if (!o->ba[DDIR_READ] || !td_random(td))
670 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
671 if (!o->ba[DDIR_WRITE] || !td_random(td))
672 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
673 if (!o->ba[DDIR_TRIM] || !td_random(td))
674 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
676 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
677 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
678 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
680 log_err("fio: Any use of blockalign= turns off randommap\n");
682 ret = warnings_fatal;
685 if (!o->file_size_high)
686 o->file_size_high = o->file_size_low;
688 if (o->start_delay_high)
689 o->start_delay = get_rand_start_delay(td);
691 if (o->norandommap && o->verify != VERIFY_NONE
692 && !fixed_block_size(o)) {
693 log_err("fio: norandommap given for variable block sizes, "
695 ret = warnings_fatal;
697 if (o->bs_unaligned && (o->odirect || td_ioengine_flagged(td, FIO_RAWIO)))
698 log_err("fio: bs_unaligned may not work with raw io\n");
701 * thinktime_spin must be less than thinktime
703 if (o->thinktime_spin > o->thinktime)
704 o->thinktime_spin = o->thinktime;
707 * The low water mark cannot be bigger than the iodepth
709 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
710 o->iodepth_low = o->iodepth;
713 * If batch number isn't set, default to the same as iodepth
715 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
716 o->iodepth_batch = o->iodepth;
719 * If max batch complete number isn't set or set incorrectly,
720 * default to the same as iodepth_batch_complete_min
722 if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
723 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
726 * There's no need to check for in-flight overlapping IOs if the job
727 * isn't changing data or the maximum iodepth is guaranteed to be 1
729 if (o->serialize_overlap && !(td->flags & TD_F_READ_IOLOG) &&
730 (!(td_write(td) || td_trim(td)) || o->iodepth == 1))
731 o->serialize_overlap = 0;
733 * Currently can't check for overlaps in offload mode
735 if (o->serialize_overlap && o->io_submit_mode == IO_MODE_OFFLOAD) {
736 log_err("fio: checking for in-flight overlaps when the "
737 "io_submit_mode is offload is not supported\n");
738 o->serialize_overlap = 0;
739 ret = warnings_fatal;
742 if (o->nr_files > td->files_index)
743 o->nr_files = td->files_index;
745 if (o->open_files > o->nr_files || !o->open_files)
746 o->open_files = o->nr_files;
748 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
749 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
750 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
751 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
752 log_err("fio: rate and rate_iops are mutually exclusive\n");
755 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
756 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
757 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
758 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
759 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
760 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
761 log_err("fio: minimum rate exceeds rate\n");
765 if (!o->timeout && o->time_based) {
766 log_err("fio: time_based requires a runtime/timeout setting\n");
768 ret = warnings_fatal;
771 if (o->fill_device && !o->size)
774 if (o->verify != VERIFY_NONE) {
775 if (td_write(td) && o->do_verify && o->numjobs > 1 &&
777 !(o->unique_filename &&
778 strstr(o->filename_format, "$jobname") &&
779 strstr(o->filename_format, "$jobnum") &&
780 strstr(o->filename_format, "$filenum")))) {
781 log_info("fio: multiple writers may overwrite blocks "
782 "that belong to other jobs. This can cause "
783 "verification failures.\n");
784 ret = warnings_fatal;
788 * Warn if verification is requested but no verification of any
789 * kind can be started due to time constraints
791 if (td_write(td) && o->do_verify && o->timeout &&
792 o->time_based && !td_read(td) && !o->verify_backlog) {
793 log_info("fio: verification read phase will never "
794 "start because write phase uses all of "
796 ret = warnings_fatal;
799 if (!fio_option_is_set(o, refill_buffers))
800 o->refill_buffers = 1;
802 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
804 o->verify_interval = o->min_bs[DDIR_WRITE];
807 * Verify interval must be smaller or equal to the
810 if (o->verify_interval > o->min_bs[DDIR_WRITE])
811 o->verify_interval = o->min_bs[DDIR_WRITE];
812 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
813 o->verify_interval = o->min_bs[DDIR_READ];
816 * Verify interval must be a factor of both min and max
819 if (!o->verify_interval ||
820 (o->min_bs[DDIR_WRITE] % o->verify_interval) ||
821 (o->max_bs[DDIR_WRITE] % o->verify_interval))
822 o->verify_interval = gcd(o->min_bs[DDIR_WRITE],
823 o->max_bs[DDIR_WRITE]);
827 if (o->invalidate_cache)
828 o->invalidate_cache = 0;
829 if (td_ioengine_flagged(td, FIO_PIPEIO)) {
830 log_info("fio: cannot pre-read files with an IO engine"
831 " that isn't seekable. Pre-read disabled.\n");
832 ret = warnings_fatal;
836 if (o->unit_base == N2S_NONE) {
837 if (td_ioengine_flagged(td, FIO_BIT_BASED))
838 o->unit_base = N2S_BITPERSEC;
840 o->unit_base = N2S_BYTEPERSEC;
843 #ifndef FIO_HAVE_ANY_FALLOCATE
844 /* Platform doesn't support any fallocate so force it to none */
845 o->fallocate_mode = FIO_FALLOCATE_NONE;
848 #ifndef CONFIG_FDATASYNC
849 if (o->fdatasync_blocks) {
850 log_info("fio: this platform does not support fdatasync()"
851 " falling back to using fsync(). Use the 'fsync'"
852 " option instead of 'fdatasync' to get rid of"
854 o->fsync_blocks = o->fdatasync_blocks;
855 o->fdatasync_blocks = 0;
856 ret = warnings_fatal;
862 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
863 * so fail if we're passed those flags
865 if (td_ioengine_flagged(td, FIO_SYNCIO) && (o->odirect || o->sync_io)) {
866 log_err("fio: Windows does not support direct or non-buffered io with"
867 " the synchronous ioengines. Use the 'windowsaio' ioengine"
868 " with 'direct=1' and 'iodepth=1' instead.\n");
874 * For fully compressible data, just zero them at init time.
875 * It's faster than repeatedly filling it. For non-zero
876 * compression, we should have refill_buffers set. Set it, unless
877 * the job file already changed it.
879 if (o->compress_percentage) {
880 if (o->compress_percentage == 100) {
882 o->compress_percentage = 0;
883 } else if (!fio_option_is_set(o, refill_buffers)) {
884 o->refill_buffers = 1;
885 td->flags |= TD_F_REFILL_BUFFERS;
890 * Using a non-uniform random distribution excludes usage of
893 if (o->random_distribution != FIO_RAND_DIST_RANDOM)
897 * If size is set but less than the min block size, complain
899 if (o->size && o->size < td_min_bs(td)) {
900 log_err("fio: size too small, must not be less than minimum block size: %llu < %u\n",
901 (unsigned long long) o->size, td_min_bs(td));
906 * O_ATOMIC implies O_DIRECT
912 * If randseed is set, that overrides randrepeat
914 if (fio_option_is_set(o, rand_seed))
915 o->rand_repeatable = 0;
917 if (td_ioengine_flagged(td, FIO_NOEXTEND) && o->file_append) {
918 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
922 if (fio_option_is_set(o, gtod_cpu)) {
924 fio_gtod_set_cpu(o->gtod_cpu);
925 fio_gtod_offload = 1;
928 td->loops = o->loops;
932 if (o->block_error_hist && o->nr_files != 1) {
933 log_err("fio: block error histogram only available "
934 "with a single file per job, but %d files "
935 "provided\n", o->nr_files);
939 if (fio_option_is_set(o, clat_percentiles) &&
940 !fio_option_is_set(o, lat_percentiles)) {
941 o->lat_percentiles = !o->clat_percentiles;
942 } else if (fio_option_is_set(o, lat_percentiles) &&
943 !fio_option_is_set(o, clat_percentiles)) {
944 o->clat_percentiles = !o->lat_percentiles;
945 } else if (fio_option_is_set(o, lat_percentiles) &&
946 fio_option_is_set(o, clat_percentiles) &&
947 o->lat_percentiles && o->clat_percentiles) {
948 log_err("fio: lat_percentiles and clat_percentiles are "
949 "mutually exclusive\n");
954 o->lat_percentiles = 0;
956 o->clat_percentiles = 0;
959 * Fix these up to be nsec internally
961 o->max_latency *= 1000ULL;
962 o->latency_target *= 1000ULL;
963 o->latency_window *= 1000ULL;
968 static void init_rand_file_service(struct thread_data *td)
970 unsigned long nranges = td->o.nr_files << FIO_FSERVICE_SHIFT;
971 const unsigned int seed = td->rand_seeds[FIO_RAND_FILE_OFF];
973 if (td->o.file_service_type == FIO_FSERVICE_ZIPF) {
974 zipf_init(&td->next_file_zipf, nranges, td->zipf_theta, seed);
975 zipf_disable_hash(&td->next_file_zipf);
976 } else if (td->o.file_service_type == FIO_FSERVICE_PARETO) {
977 pareto_init(&td->next_file_zipf, nranges, td->pareto_h, seed);
978 zipf_disable_hash(&td->next_file_zipf);
979 } else if (td->o.file_service_type == FIO_FSERVICE_GAUSS) {
980 gauss_init(&td->next_file_gauss, nranges, td->gauss_dev, seed);
981 gauss_disable_hash(&td->next_file_gauss);
985 void td_fill_verify_state_seed(struct thread_data *td)
989 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
994 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF],
998 static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64)
1000 unsigned int read_seed = td->rand_seeds[FIO_RAND_BS_OFF];
1001 unsigned int write_seed = td->rand_seeds[FIO_RAND_BS1_OFF];
1002 unsigned int trim_seed = td->rand_seeds[FIO_RAND_BS2_OFF];
1006 * trimwrite is special in that we need to generate the same
1007 * offsets to get the "write after trim" effect. If we are
1008 * using bssplit to set buffer length distributions, ensure that
1009 * we seed the trim and write generators identically. Ditto for
1010 * verify, read and writes must have the same seed, if we are doing
1013 if (td->o.verify != VERIFY_NONE)
1014 write_seed = read_seed;
1015 if (td_trimwrite(td))
1016 trim_seed = write_seed;
1017 init_rand_seed(&td->bsrange_state[DDIR_READ], read_seed, use64);
1018 init_rand_seed(&td->bsrange_state[DDIR_WRITE], write_seed, use64);
1019 init_rand_seed(&td->bsrange_state[DDIR_TRIM], trim_seed, use64);
1021 td_fill_verify_state_seed(td);
1022 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false);
1024 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
1025 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
1026 else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
1027 init_rand_file_service(td);
1029 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
1030 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
1031 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
1032 init_rand_seed(&td->poisson_state[0], td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
1033 init_rand_seed(&td->poisson_state[1], td->rand_seeds[FIO_RAND_POISSON2_OFF], 0);
1034 init_rand_seed(&td->poisson_state[2], td->rand_seeds[FIO_RAND_POISSON3_OFF], 0);
1035 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false);
1036 init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false);
1041 if (td->o.rand_repeatable)
1042 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
1044 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
1046 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1047 struct frand_state *s = &td->seq_rand_state[i];
1049 init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false);
1053 void td_fill_rand_seeds(struct thread_data *td)
1057 if (td->o.allrand_repeatable) {
1060 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
1061 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
1065 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
1070 td_fill_rand_seeds_internal(td, use64);
1072 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
1073 frand_copy(&td->buf_state_prev, &td->buf_state);
1077 * Initializes the ioengine configured for a job, if it has not been done so
1080 int ioengine_load(struct thread_data *td)
1082 if (!td->o.ioengine) {
1083 log_err("fio: internal fault, no IO engine specified\n");
1088 struct ioengine_ops *ops;
1091 /* An engine is loaded, but the requested ioengine
1094 if (!strcmp(td->io_ops->name, td->o.ioengine)) {
1095 /* The right engine is already loaded */
1100 * Name of file and engine may be different, load ops
1101 * for this name and see if they match. If they do, then
1102 * the engine is unchanged.
1104 dlhandle = td->io_ops_dlhandle;
1105 ops = load_ioengine(td);
1106 if (ops == td->io_ops && dlhandle == td->io_ops_dlhandle) {
1112 if (dlhandle && dlhandle != td->io_ops_dlhandle)
1115 /* Unload the old engine. */
1119 td->io_ops = load_ioengine(td);
1121 log_err("fio: failed to load engine\n");
1125 if (td->io_ops->option_struct_size && td->io_ops->options) {
1127 * In cases where td->eo is set, clone it for a child thread.
1128 * This requires that the parent thread has the same ioengine,
1129 * but that requirement must be enforced by the code which
1130 * cloned the thread.
1132 void *origeo = td->eo;
1134 * Otherwise use the default thread options.
1136 if (!origeo && td != &def_thread && def_thread.eo &&
1137 def_thread.io_ops->options == td->io_ops->options)
1138 origeo = def_thread.eo;
1140 options_init(td->io_ops->options);
1141 td->eo = malloc(td->io_ops->option_struct_size);
1143 * Use the default thread as an option template if this uses the
1144 * same options structure and there are non-default options
1148 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
1149 options_mem_dupe(td->io_ops->options, td->eo);
1151 memset(td->eo, 0, td->io_ops->option_struct_size);
1152 fill_default_options(td->eo, td->io_ops->options);
1154 *(struct thread_data **)td->eo = td;
1158 td->io_ops->flags |= FIO_RAWIO;
1160 td_set_ioengine_flags(td);
1164 static void init_flags(struct thread_data *td)
1166 struct thread_options *o = &td->o;
1169 if (o->verify_backlog)
1170 td->flags |= TD_F_VER_BACKLOG;
1171 if (o->trim_backlog)
1172 td->flags |= TD_F_TRIM_BACKLOG;
1173 if (o->read_iolog_file)
1174 td->flags |= TD_F_READ_IOLOG;
1175 if (o->refill_buffers)
1176 td->flags |= TD_F_REFILL_BUFFERS;
1178 * Always scramble buffers if asked to
1180 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
1181 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1183 * But also scramble buffers, unless we were explicitly asked
1186 if (o->scramble_buffers && !(o->zero_buffers &&
1187 fio_option_is_set(o, zero_buffers)))
1188 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1189 if (o->verify != VERIFY_NONE)
1190 td->flags |= TD_F_DO_VERIFY;
1192 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
1193 td->flags |= TD_F_NEED_LOCK;
1195 if (o->mem_type == MEM_CUDA_MALLOC)
1196 td->flags &= ~TD_F_SCRAMBLE_BUFFERS;
1198 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1199 if (option_check_rate(td, i)) {
1200 td->flags |= TD_F_CHECK_RATE;
1206 static int setup_random_seeds(struct thread_data *td)
1211 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed)) {
1212 int ret = init_random_seeds(td->rand_seeds, sizeof(td->rand_seeds));
1214 td_fill_rand_seeds(td);
1218 seed = td->o.rand_seed;
1219 for (i = 0; i < 4; i++)
1220 seed *= 0x9e370001UL;
1222 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
1223 td->rand_seeds[i] = seed * td->thread_number + i;
1224 seed *= 0x9e370001UL;
1227 td_fill_rand_seeds(td);
1238 static struct fpre_keyword {
1239 const char *keyword;
1242 } fpre_keywords[] = {
1243 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1244 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1245 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1246 { .keyword = NULL, },
1249 static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
1250 const char *jobname, int jobnum, int filenum)
1252 struct fpre_keyword *f;
1253 char copy[PATH_MAX];
1254 size_t dst_left = PATH_MAX - 1;
1256 if (!o->filename_format || !strlen(o->filename_format)) {
1257 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
1261 for (f = &fpre_keywords[0]; f->keyword; f++)
1262 f->strlen = strlen(f->keyword);
1264 buf[buf_size - 1] = '\0';
1265 strncpy(buf, o->filename_format, buf_size - 1);
1267 memset(copy, 0, sizeof(copy));
1268 for (f = &fpre_keywords[0]; f->keyword; f++) {
1270 size_t pre_len, post_start = 0;
1271 char *str, *dst = copy;
1273 str = strcasestr(buf, f->keyword);
1277 pre_len = str - buf;
1278 if (strlen(str) != f->strlen)
1279 post_start = pre_len + f->strlen;
1282 strncpy(dst, buf, pre_len);
1284 dst_left -= pre_len;
1288 case FPRE_JOBNAME: {
1291 ret = snprintf(dst, dst_left, "%s", jobname);
1294 else if (ret > dst_left) {
1295 log_err("fio: truncated filename\n");
1307 ret = snprintf(dst, dst_left, "%d", jobnum);
1310 else if (ret > dst_left) {
1311 log_err("fio: truncated filename\n");
1320 case FPRE_FILENUM: {
1323 ret = snprintf(dst, dst_left, "%d", filenum);
1326 else if (ret > dst_left) {
1327 log_err("fio: truncated filename\n");
1342 strncpy(dst, buf + post_start, dst_left);
1344 strncpy(buf, copy, buf_size - 1);
1351 bool parse_dryrun(void)
1353 return dump_cmdline || parse_only;
1356 static void gen_log_name(char *name, size_t size, const char *logtype,
1357 const char *logname, unsigned int num,
1358 const char *suf, int per_job)
1361 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1363 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1366 static int check_waitees(char *waitee)
1368 struct thread_data *td;
1371 for_each_td(td, i) {
1372 if (td->subjob_number)
1375 ret += !strcmp(td->o.name, waitee);
1381 static bool wait_for_ok(const char *jobname, struct thread_options *o)
1388 if (!strcmp(jobname, o->wait_for)) {
1389 log_err("%s: a job cannot wait for itself (wait_for=%s).\n",
1390 jobname, o->wait_for);
1394 if (!(nw = check_waitees(o->wait_for))) {
1395 log_err("%s: waitee job %s unknown.\n", jobname, o->wait_for);
1400 log_err("%s: multiple waitees %s found,\n"
1401 "please avoid duplicates when using wait_for option.\n",
1402 jobname, o->wait_for);
1410 * Adds a job to the list of things todo. Sanitizes the various options
1411 * to make sure we don't have conflicts, and initializes various
1414 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
1415 int recursed, int client_type)
1418 char fname[PATH_MAX];
1419 int numjobs, file_alloced;
1420 struct thread_options *o = &td->o;
1421 char logname[PATH_MAX + 32];
1424 * the def_thread is just for options, it's not a real job
1426 if (td == &def_thread)
1432 * if we are just dumping the output command line, don't add the job
1434 if (parse_dryrun()) {
1439 td->client_type = client_type;
1441 if (profile_td_init(td))
1444 if (ioengine_load(td))
1448 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1451 if (o->nr_files == 1 && exists_and_not_regfile(jobname))
1452 add_file(td, jobname, job_add_num, 0);
1454 for (i = 0; i < o->nr_files; i++)
1455 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
1459 if (fixup_options(td))
1463 * Belongs to fixup_options, but o->name is not necessarily set as yet
1465 if (!wait_for_ok(jobname, o))
1471 * IO engines only need this for option callbacks, and the address may
1472 * change in subprocesses.
1475 *(struct thread_data **)td->eo = NULL;
1477 if (td_ioengine_flagged(td, FIO_DISKLESSIO)) {
1480 for_each_file(td, f, i)
1481 f->real_file_size = -1ULL;
1484 td->sem = fio_sem_init(FIO_SEM_LOCKED);
1486 td->ts.clat_percentiles = o->clat_percentiles;
1487 td->ts.lat_percentiles = o->lat_percentiles;
1488 td->ts.percentile_precision = o->percentile_precision;
1489 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1490 td->ts.sig_figs = o->sig_figs;
1492 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1493 td->ts.clat_stat[i].min_val = ULONG_MAX;
1494 td->ts.slat_stat[i].min_val = ULONG_MAX;
1495 td->ts.lat_stat[i].min_val = ULONG_MAX;
1496 td->ts.bw_stat[i].min_val = ULONG_MAX;
1497 td->ts.iops_stat[i].min_val = ULONG_MAX;
1499 td->ts.sync_stat.min_val = ULONG_MAX;
1500 td->ddir_seq_nr = o->ddir_seq_nr;
1502 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1503 prev_group_jobs = 0;
1505 if (groupid == INT_MAX) {
1506 log_err("fio: too many groups defined\n");
1511 td->groupid = groupid;
1514 if (setup_random_seeds(td)) {
1515 td_verror(td, errno, "setup_random_seeds");
1522 if (o->write_lat_log) {
1523 struct log_params p = {
1525 .avg_msec = o->log_avg_msec,
1526 .hist_msec = o->log_hist_msec,
1527 .hist_coarseness = o->log_hist_coarseness,
1528 .log_type = IO_LOG_TYPE_LAT,
1529 .log_offset = o->log_offset,
1530 .log_gz = o->log_gz,
1531 .log_gz_store = o->log_gz_store,
1533 const char *pre = o->lat_log_file ? o->lat_log_file : o->name;
1541 gen_log_name(logname, sizeof(logname), "lat", pre,
1542 td->thread_number, suf, o->per_job_logs);
1543 setup_log(&td->lat_log, &p, logname);
1545 gen_log_name(logname, sizeof(logname), "slat", pre,
1546 td->thread_number, suf, o->per_job_logs);
1547 setup_log(&td->slat_log, &p, logname);
1549 gen_log_name(logname, sizeof(logname), "clat", pre,
1550 td->thread_number, suf, o->per_job_logs);
1551 setup_log(&td->clat_log, &p, logname);
1554 if (o->write_hist_log) {
1555 struct log_params p = {
1557 .avg_msec = o->log_avg_msec,
1558 .hist_msec = o->log_hist_msec,
1559 .hist_coarseness = o->log_hist_coarseness,
1560 .log_type = IO_LOG_TYPE_HIST,
1561 .log_offset = o->log_offset,
1562 .log_gz = o->log_gz,
1563 .log_gz_store = o->log_gz_store,
1565 const char *pre = o->hist_log_file ? o->hist_log_file : o->name;
1569 if (td->client_type) {
1570 log_err("fio: --write_hist_log requires zlib in client/server mode\n");
1580 gen_log_name(logname, sizeof(logname), "clat_hist", pre,
1581 td->thread_number, suf, o->per_job_logs);
1582 setup_log(&td->clat_hist_log, &p, logname);
1585 if (o->write_bw_log) {
1586 struct log_params p = {
1588 .avg_msec = o->log_avg_msec,
1589 .hist_msec = o->log_hist_msec,
1590 .hist_coarseness = o->log_hist_coarseness,
1591 .log_type = IO_LOG_TYPE_BW,
1592 .log_offset = o->log_offset,
1593 .log_gz = o->log_gz,
1594 .log_gz_store = o->log_gz_store,
1596 const char *pre = o->bw_log_file ? o->bw_log_file : o->name;
1599 if (fio_option_is_set(o, bw_avg_time))
1600 p.avg_msec = min(o->log_avg_msec, o->bw_avg_time);
1602 o->bw_avg_time = p.avg_msec;
1604 p.hist_msec = o->log_hist_msec;
1605 p.hist_coarseness = o->log_hist_coarseness;
1612 gen_log_name(logname, sizeof(logname), "bw", pre,
1613 td->thread_number, suf, o->per_job_logs);
1614 setup_log(&td->bw_log, &p, logname);
1616 if (o->write_iops_log) {
1617 struct log_params p = {
1619 .avg_msec = o->log_avg_msec,
1620 .hist_msec = o->log_hist_msec,
1621 .hist_coarseness = o->log_hist_coarseness,
1622 .log_type = IO_LOG_TYPE_IOPS,
1623 .log_offset = o->log_offset,
1624 .log_gz = o->log_gz,
1625 .log_gz_store = o->log_gz_store,
1627 const char *pre = o->iops_log_file ? o->iops_log_file : o->name;
1630 if (fio_option_is_set(o, iops_avg_time))
1631 p.avg_msec = min(o->log_avg_msec, o->iops_avg_time);
1633 o->iops_avg_time = p.avg_msec;
1635 p.hist_msec = o->log_hist_msec;
1636 p.hist_coarseness = o->log_hist_coarseness;
1643 gen_log_name(logname, sizeof(logname), "iops", pre,
1644 td->thread_number, suf, o->per_job_logs);
1645 setup_log(&td->iops_log, &p, logname);
1649 o->name = strdup(jobname);
1651 if (output_format & FIO_OUTPUT_NORMAL) {
1653 if (is_backend && !recursed)
1654 fio_server_send_add_job(td);
1656 if (!td_ioengine_flagged(td, FIO_NOIO)) {
1657 char *c1, *c2, *c3, *c4;
1658 char *c5 = NULL, *c6 = NULL;
1659 int i2p = is_power_of_2(o->kb_base);
1661 c1 = num2str(o->min_bs[DDIR_READ], o->sig_figs, 1, i2p, N2S_BYTE);
1662 c2 = num2str(o->max_bs[DDIR_READ], o->sig_figs, 1, i2p, N2S_BYTE);
1663 c3 = num2str(o->min_bs[DDIR_WRITE], o->sig_figs, 1, i2p, N2S_BYTE);
1664 c4 = num2str(o->max_bs[DDIR_WRITE], o->sig_figs, 1, i2p, N2S_BYTE);
1666 if (!o->bs_is_seq_rand) {
1667 c5 = num2str(o->min_bs[DDIR_TRIM], o->sig_figs, 1, i2p, N2S_BYTE);
1668 c6 = num2str(o->max_bs[DDIR_TRIM], o->sig_figs, 1, i2p, N2S_BYTE);
1671 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1673 ddir_str(o->td_ddir));
1675 if (o->bs_is_seq_rand)
1676 log_info("bs=(R) %s-%s, (W) %s-%s, bs_is_seq_rand, ",
1679 log_info("bs=(R) %s-%s, (W) %s-%s, (T) %s-%s, ",
1680 c1, c2, c3, c4, c5, c6);
1682 log_info("ioengine=%s, iodepth=%u\n",
1683 td->io_ops->name, o->iodepth);
1692 } else if (job_add_num == 1)
1696 if (td_steadystate_init(td))
1700 * recurse add identical jobs, clear numjobs and stonewall options
1701 * as they don't apply to sub-jobs
1703 numjobs = o->numjobs;
1705 struct thread_data *td_new = get_new_job(false, td, true, jobname);
1710 td_new->o.numjobs = 1;
1711 td_new->o.stonewall = 0;
1712 td_new->o.new_group = 0;
1713 td_new->subjob_number = numjobs;
1714 td_new->o.ss_dur = o->ss_dur * 1000000l;
1715 td_new->o.ss_limit = o->ss_limit;
1718 if (td_new->files) {
1720 for_each_file(td_new, f, i) {
1722 sfree(f->file_name);
1725 free(td_new->files);
1726 td_new->files = NULL;
1728 td_new->files_index = 0;
1729 td_new->files_size = 0;
1730 if (td_new->o.filename) {
1731 free(td_new->o.filename);
1732 td_new->o.filename = NULL;
1736 if (add_job(td_new, jobname, numjobs, 1, client_type))
1747 * Parse as if 'o' was a command line
1749 void add_job_opts(const char **o, int client_type)
1751 struct thread_data *td, *td_parent;
1752 int i, in_global = 1;
1756 td_parent = td = NULL;
1758 if (!strncmp(o[i], "name", 4)) {
1761 add_job(td, jobname, 0, 0, client_type);
1763 sprintf(jobname, "%s", o[i] + 5);
1765 if (in_global && !td_parent)
1766 td_parent = get_new_job(true, &def_thread, false, jobname);
1767 else if (!in_global && !td) {
1769 td_parent = &def_thread;
1770 td = get_new_job(false, td_parent, false, jobname);
1773 fio_options_parse(td_parent, (char **) &o[i], 1);
1775 fio_options_parse(td, (char **) &o[i], 1);
1780 add_job(td, jobname, 0, 0, client_type);
1783 static int skip_this_section(const char *name)
1787 if (!nr_job_sections)
1789 if (!strncmp(name, "global", 6))
1792 for (i = 0; i < nr_job_sections; i++)
1793 if (!strcmp(job_sections[i], name))
1799 static int is_empty_or_comment(char *line)
1803 for (i = 0; i < strlen(line); i++) {
1808 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1816 * This is our [ini] type file parser.
1818 static int __parse_jobs_ini(struct thread_data *td,
1819 char *file, int is_buf, int stonewall_flag, int type,
1820 int nested, char *name, char ***popts, int *aopts, int *nopts)
1822 bool global = false;
1826 int ret = 0, stonewall;
1829 int inside_skip = 0;
1831 int i, alloc_opts, num_opts;
1833 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1834 assert(td || !nested);
1839 if (!strcmp(file, "-"))
1842 f = fopen(file, "r");
1847 log_err("fio: unable to open '%s' job file\n", file);
1849 td_verror(td, __err, "job file open");
1854 string = malloc(4096);
1857 * it's really 256 + small bit, 280 should suffice
1861 memset(name, 0, 280);
1865 if (nested && popts) {
1867 alloc_opts = *aopts;
1873 opts = malloc(sizeof(char *) * alloc_opts);
1877 stonewall = stonewall_flag;
1880 * if skip_fgets is set, we already have loaded a line we
1885 p = strsep(&file, "\n");
1887 p = fgets(string, 4096, f);
1893 strip_blank_front(&p);
1896 dprint(FD_PARSE, "%s\n", p);
1897 if (is_empty_or_comment(p))
1901 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1905 log_err("fio: option <%s> outside of "
1906 "[] job section\n", p);
1911 name[strlen(name) - 1] = '\0';
1913 if (skip_this_section(name)) {
1919 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1921 global = !strncmp(name, "global", 6);
1927 log_info("--name=%s ", name);
1931 td = get_new_job(global, &def_thread, false, name);
1938 * Separate multiple job files by a stonewall
1940 if (!global && stonewall) {
1941 td->o.stonewall = stonewall;
1946 memset(opts, 0, alloc_opts * sizeof(char *));
1954 p = strsep(&file, "\n");
1956 p = fgets(string, 4096, f);
1959 dprint(FD_PARSE, "%s", p);
1964 if (is_empty_or_comment(p))
1967 strip_blank_front(&p);
1970 * new section, break out and make sure we don't
1971 * fgets() a new line at the top.
1975 log_err("No new sections in included files\n");
1986 if (!strncmp(p, "include", strlen("include"))) {
1987 char *filename = p + strlen("include") + 1,
1988 *ts, *full_fn = NULL;
1991 * Allow for the include filename
1992 * specification to be relative.
1994 if (access(filename, F_OK) &&
1995 (ts = strrchr(file, '/'))) {
1996 int len = ts - file +
1997 strlen(filename) + 2;
1999 if (!(full_fn = calloc(1, len))) {
2005 file, (ts - file) + 1);
2006 strncpy(full_fn + (ts - file) + 1,
2007 filename, strlen(filename));
2008 full_fn[len - 1] = 0;
2012 ret = __parse_jobs_ini(td, filename, is_buf,
2013 stonewall_flag, type, 1,
2015 &alloc_opts, &num_opts);
2018 log_err("Error %d while parsing "
2019 "include file %s\n",
2032 if (num_opts == alloc_opts) {
2034 opts = realloc(opts,
2035 alloc_opts * sizeof(char *));
2038 opts[num_opts] = strdup(p);
2044 *aopts = alloc_opts;
2049 ret = fio_options_parse(td, opts, num_opts);
2054 ret = add_job(td, name, 0, 0, type);
2056 log_err("fio: job %s dropped\n", name);
2060 for (i = 0; i < num_opts; i++)
2069 while (i < nr_job_sections) {
2070 free(job_sections[i]);
2079 if (!is_buf && f != stdin)
2084 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
2086 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
2087 0, NULL, NULL, NULL, NULL);
2090 static int fill_def_thread(void)
2092 memset(&def_thread, 0, sizeof(def_thread));
2093 INIT_FLIST_HEAD(&def_thread.opt_list);
2095 fio_getaffinity(getpid(), &def_thread.o.cpumask);
2096 def_thread.o.error_dump = 1;
2099 * fill default options
2101 fio_fill_default_options(&def_thread);
2105 static void show_debug_categories(void)
2107 #ifdef FIO_INC_DEBUG
2108 const struct debug_level *dl = &debug_levels[0];
2109 int curlen, first = 1;
2113 int has_next = (dl + 1)->name != NULL;
2115 if (first || curlen + strlen(dl->name) >= 80) {
2120 curlen += printf("\t\t\t%s", dl->name);
2121 curlen += 3 * (8 - 1);
2123 curlen += printf(",");
2125 curlen += printf("%s", dl->name);
2127 curlen += printf(",");
2137 * Following options aren't printed by usage().
2138 * --append-terse - Equivalent to --output-format=terse, see f6a7df53.
2139 * --latency-log - Deprecated option.
2141 static void usage(const char *name)
2143 printf("%s\n", fio_version_string);
2144 printf("%s [options] [job options] <job file(s)>\n", name);
2145 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
2146 show_debug_categories();
2147 printf(" --parse-only\t\tParse options only, don't start any IO\n");
2148 printf(" --output\t\tWrite output to file\n");
2149 printf(" --bandwidth-log\tGenerate aggregate bandwidth logs\n");
2150 printf(" --minimal\t\tMinimal (terse) output\n");
2151 printf(" --output-format=type\tOutput format (terse,json,json+,normal)\n");
2152 printf(" --terse-version=type\tSet terse version output format"
2153 " (default 3, or 2 or 4)\n");
2154 printf(" --version\t\tPrint version info and exit\n");
2155 printf(" --help\t\tPrint this page\n");
2156 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
2157 printf(" --crctest=[type]\tTest speed of checksum functions\n");
2158 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
2160 printf(" --enghelp=engine\tPrint ioengine help, or list"
2161 " available ioengines\n");
2162 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
2164 printf(" --showcmd\t\tTurn a job file into command line options\n");
2165 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
2166 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
2167 printf(" --eta-newline=time\tForce a new line for every 'time'");
2168 printf(" period passed\n");
2169 printf(" --status-interval=t\tForce full status dump every");
2170 printf(" 't' period passed\n");
2171 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
2173 printf(" --section=name\tOnly run specified section in job file,"
2174 " multiple sections can be specified\n");
2175 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2177 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
2178 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2179 printf(" --server=args\t\tStart a backend fio server\n");
2180 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
2181 printf(" --client=hostname\tTalk to remote backend(s) fio server at hostname\n");
2182 printf(" --remote-config=file\tTell fio server to load this local job file\n");
2183 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2184 "\t\t\t(option=system,percpu) or run unit work\n"
2185 "\t\t\tcalibration only (option=calibrate)\n");
2187 printf(" --inflate-log=log\tInflate and output compressed log\n");
2189 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
2190 printf(" --trigger-timeout=t\tExecute trigger at this time\n");
2191 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
2192 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
2193 printf(" --aux-path=path\tUse this path for fio state generated files\n");
2194 printf("\nFio was written by Jens Axboe <axboe@kernel.dk>\n");
2197 #ifdef FIO_INC_DEBUG
2198 const struct debug_level debug_levels[] = {
2199 { .name = "process",
2200 .help = "Process creation/exit logging",
2201 .shift = FD_PROCESS,
2204 .help = "File related action logging",
2208 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2212 .help = "Memory allocation/freeing logging",
2215 { .name = "blktrace",
2216 .help = "blktrace action logging",
2217 .shift = FD_BLKTRACE,
2220 .help = "IO verification action logging",
2224 .help = "Random generation logging",
2228 .help = "Parser logging",
2231 { .name = "diskutil",
2232 .help = "Disk utility logging actions",
2233 .shift = FD_DISKUTIL,
2236 .help = "Logging related to creating/destroying jobs",
2240 .help = "Mutex logging",
2243 { .name = "profile",
2244 .help = "Logging related to profiles",
2245 .shift = FD_PROFILE,
2248 .help = "Logging related to time keeping functions",
2252 .help = "Network logging",
2256 .help = "Rate logging",
2259 { .name = "compress",
2260 .help = "Log compression logging",
2261 .shift = FD_COMPRESS,
2263 { .name = "steadystate",
2264 .help = "Steady state detection logging",
2265 .shift = FD_STEADYSTATE,
2267 { .name = "helperthread",
2268 .help = "Helper thread logging",
2269 .shift = FD_HELPERTHREAD,
2274 static int set_debug(const char *string)
2276 const struct debug_level *dl;
2277 char *p = (char *) string;
2284 if (!strcmp(string, "?") || !strcmp(string, "help")) {
2285 log_info("fio: dumping debug options:");
2286 for (i = 0; debug_levels[i].name; i++) {
2287 dl = &debug_levels[i];
2288 log_info("%s,", dl->name);
2294 while ((opt = strsep(&p, ",")) != NULL) {
2297 if (!strncmp(opt, "all", 3)) {
2298 log_info("fio: set all debug options\n");
2303 for (i = 0; debug_levels[i].name; i++) {
2304 dl = &debug_levels[i];
2305 found = !strncmp(opt, dl->name, strlen(dl->name));
2309 if (dl->shift == FD_JOB) {
2310 opt = strchr(opt, ':');
2312 log_err("fio: missing job number\n");
2316 fio_debug_jobno = atoi(opt);
2317 log_info("fio: set debug jobno %d\n",
2320 log_info("fio: set debug option %s\n", opt);
2321 fio_debug |= (1UL << dl->shift);
2327 log_err("fio: debug mask %s not found\n", opt);
2332 static int set_debug(const char *string)
2334 log_err("fio: debug tracing not included in build\n");
2339 static void fio_options_fill_optstring(void)
2341 char *ostr = cmd_optstr;
2345 while (l_opts[i].name) {
2346 ostr[c++] = l_opts[i].val;
2347 if (l_opts[i].has_arg == required_argument)
2349 else if (l_opts[i].has_arg == optional_argument) {
2358 static int client_flag_set(char c)
2363 while (l_opts[i].name) {
2364 int val = l_opts[i].val;
2366 if (c == (val & 0xff))
2367 return (val & FIO_CLIENT_FLAG);
2375 static void parse_cmd_client(void *client, char *opt)
2377 fio_client_add_cmd_option(client, opt);
2380 static void show_closest_option(const char *name)
2382 int best_option, best_distance;
2385 while (*name == '-')
2389 best_distance = INT_MAX;
2391 while (l_opts[i].name) {
2392 distance = string_distance(name, l_opts[i].name);
2393 if (distance < best_distance) {
2394 best_distance = distance;
2400 if (best_option != -1 && string_distance_ok(name, best_distance))
2401 log_err("Did you mean %s?\n", l_opts[best_option].name);
2404 static int parse_output_format(const char *optarg)
2406 char *p, *orig, *opt;
2409 p = orig = strdup(optarg);
2413 while ((opt = strsep(&p, ",")) != NULL) {
2414 if (!strcmp(opt, "minimal") ||
2415 !strcmp(opt, "terse") ||
2416 !strcmp(opt, "csv"))
2417 output_format |= FIO_OUTPUT_TERSE;
2418 else if (!strcmp(opt, "json"))
2419 output_format |= FIO_OUTPUT_JSON;
2420 else if (!strcmp(opt, "json+"))
2421 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
2422 else if (!strcmp(opt, "normal"))
2423 output_format |= FIO_OUTPUT_NORMAL;
2425 log_err("fio: invalid output format %s\n", opt);
2435 int parse_cmd_line(int argc, char *argv[], int client_type)
2437 struct thread_data *td = NULL;
2438 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
2439 char *ostr = cmd_optstr;
2440 char *pid_file = NULL;
2441 void *cur_client = NULL;
2445 * Reset optind handling, since we may call this multiple times
2450 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2451 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
2452 parse_cmd_client(cur_client, argv[optind - 1]);
2453 c &= ~FIO_CLIENT_FLAG;
2458 smalloc_pool_size = atoi(optarg);
2459 smalloc_pool_size <<= 10;
2463 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2473 if (f_out && f_out != stdout)
2476 tmp = fopen(optarg, "w+");
2478 log_err("fio: output file open error: %s\n", strerror(errno));
2483 f_err = f_out = tmp;
2487 output_format = FIO_OUTPUT_TERSE;
2490 if (parse_output_format(optarg)) {
2491 log_err("fio: failed parsing output-format\n");
2498 output_format |= FIO_OUTPUT_TERSE;
2510 fio_show_option_help(optarg);
2517 fio_show_ioengine_help(optarg);
2531 log_info("%s\n", fio_version_string);
2536 terse_version = atoi(optarg);
2537 if (!(terse_version >= 2 && terse_version <= 5)) {
2538 log_err("fio: bad terse version format\n");
2544 if (!strcmp("always", optarg))
2545 eta_print = FIO_ETA_ALWAYS;
2546 else if (!strcmp("never", optarg))
2547 eta_print = FIO_ETA_NEVER;
2552 if (check_str_time(optarg, &t, 1)) {
2553 log_err("fio: failed parsing eta time %s\n", optarg);
2558 eta_new_line = t / 1000;
2559 if (!eta_new_line) {
2560 log_err("fio: eta new line time too short\n");
2569 if (check_str_time(optarg, &t, 1)) {
2570 log_err("fio: failed parsing eta interval %s\n", optarg);
2575 eta_interval_msec = t / 1000;
2576 if (eta_interval_msec < DISK_UTIL_MSEC) {
2577 log_err("fio: eta interval time too short (%umsec min)\n", DISK_UTIL_MSEC);
2584 if (set_debug(optarg))
2594 if (!strcmp(optarg, "global")) {
2595 log_err("fio: can't use global as only "
2601 new_size = (nr_job_sections + 1) * sizeof(char *);
2602 job_sections = realloc(job_sections, new_size);
2603 job_sections[nr_job_sections] = strdup(optarg);
2609 exit_val = iolog_file_inflate(optarg);
2618 exec_profile = strdup(optarg);
2620 case FIO_GETOPT_JOB: {
2621 const char *opt = l_opts[lidx].name;
2624 if (!strncmp(opt, "name", 4) && td) {
2625 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2632 int is_section = !strncmp(opt, "name", 4);
2635 if (!is_section || !strncmp(val, "global", 6))
2638 if (is_section && skip_this_section(val))
2641 td = get_new_job(global, &def_thread, true, NULL);
2642 if (!td || ioengine_load(td)) {
2651 fio_options_set_ioengine_opts(l_opts, td);
2654 if ((!val || !strlen(val)) &&
2655 l_opts[lidx].has_arg == required_argument) {
2656 log_err("fio: option %s requires an argument\n", opt);
2659 ret = fio_cmd_option_parse(td, opt, val);
2670 if (!ret && !strcmp(opt, "ioengine")) {
2671 if (ioengine_load(td)) {
2678 fio_options_set_ioengine_opts(l_opts, td);
2682 case FIO_GETOPT_IOENGINE: {
2683 const char *opt = l_opts[lidx].name;
2689 ret = fio_cmd_ioengine_option_parse(td, opt, val);
2696 max_jobs = atoi(optarg);
2697 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2698 log_err("fio: invalid max jobs: %d\n", max_jobs);
2705 #ifndef CONFIG_NO_SHM
2707 log_err("fio: can't be both client and server\n");
2713 fio_server_set_arg(optarg);
2717 log_err("fio: client/server requires SHM support\n");
2725 pid_file = strdup(optarg);
2728 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2729 /* exit on error and calibration only */
2739 log_err("fio: can't be both client and server\n");
2744 /* if --client parameter contains a pathname */
2745 if (0 == access(optarg, R_OK)) {
2746 /* file contains a list of host addrs or names */
2747 char hostaddr[PATH_MAX] = {0};
2749 FILE * hostf = fopen(optarg, "r");
2751 log_err("fio: could not open client list file %s for read\n", optarg);
2756 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2758 * read at most PATH_MAX-1 chars from each
2759 * record in this file
2761 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2762 /* expect EVERY host in file to be valid */
2763 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2764 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2771 break; /* no possibility of job file for "this client only" */
2773 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
2774 log_err("fio: failed adding client %s\n", optarg);
2780 * If the next argument exists and isn't an option,
2781 * assume it's a job file for this client only.
2783 while (optind < argc) {
2784 if (!strncmp(argv[optind], "--", 2) ||
2785 !strncmp(argv[optind], "-", 1))
2788 if (fio_client_add_ini_file(cur_client, argv[optind], false))
2795 if (fio_client_add_ini_file(cur_client, optarg, true)) {
2803 exit_val = fio_monotonic_clocktest(1);
2808 exit_val = fio_crctest(optarg);
2813 exit_val = fio_memcpy_test(optarg);
2818 if (check_str_time(optarg, &val, 1)) {
2819 log_err("fio: failed parsing time %s\n", optarg);
2825 log_err("fio: status interval too small\n");
2829 status_interval = val / 1000;
2835 trigger_file = strdup(optarg);
2840 trigger_cmd = strdup(optarg);
2843 if (trigger_remote_cmd)
2844 free(trigger_remote_cmd);
2845 trigger_remote_cmd = strdup(optarg);
2850 aux_path = strdup(optarg);
2853 if (check_str_time(optarg, &trigger_timeout, 1)) {
2854 log_err("fio: failed parsing time %s\n", optarg);
2858 trigger_timeout /= 1000000;
2861 log_err("%s: unrecognized option '%s'\n", argv[0],
2863 show_closest_option(argv[optind - 1]);
2873 if (do_exit && !(is_backend || nr_clients))
2876 if (nr_clients && fio_clients_connect())
2879 if (is_backend && backend)
2880 return fio_start_server(pid_file);
2886 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2892 while (!ret && optind < argc) {
2894 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2895 ini_file[ini_idx - 1] = strdup(argv[optind]);
2903 int fio_init_options(void)
2908 fio_options_fill_optstring();
2909 fio_options_dup_and_init(l_opts);
2913 if (fill_def_thread())
2919 extern int fio_check_options(struct thread_options *);
2921 int parse_options(int argc, char *argv[])
2923 const int type = FIO_CLIENT_TYPE_CLI;
2926 if (fio_init_options())
2928 if (fio_test_cconv(&def_thread.o))
2929 log_err("fio: failed internal cconv test\n");
2931 job_files = parse_cmd_line(argc, argv, type);
2933 if (job_files > 0) {
2934 for (i = 0; i < job_files; i++) {
2935 if (i && fill_def_thread())
2938 if (fio_clients_send_ini(ini_file[i]))
2941 } else if (!is_backend) {
2942 if (parse_jobs_ini(ini_file[i], 0, i, type))
2947 } else if (nr_clients) {
2948 if (fill_def_thread())
2950 if (fio_clients_send_ini(NULL))
2955 fio_options_free(&def_thread);
2956 filesetup_mem_free();
2958 if (!thread_number) {
2963 if (is_backend || nr_clients)
2968 log_err("No job(s) defined\n\n");
2973 if (output_format & FIO_OUTPUT_NORMAL)
2974 log_info("%s\n", fio_version_string);
2979 void options_default_fill(struct thread_options *o)
2981 memcpy(o, &def_thread.o, sizeof(*o));
2984 struct thread_data *get_global_options(void)