2 * This file contains job initialization and setup functions.
12 #include <sys/types.h>
16 #ifndef FIO_NO_HAVE_SHM_H
28 #include "lib/getopt.h"
29 #include "lib/strcasestr.h"
33 const char fio_version_string[] = FIO_VERSION;
35 #define FIO_RANDSEED (0xb1899bedUL)
37 static char **ini_file;
38 static int max_jobs = FIO_MAX_JOBS;
39 static int dump_cmdline;
40 static int def_timeout;
41 static int parse_only;
43 static struct thread_data def_thread;
44 struct thread_data *threads = NULL;
45 static char **job_sections;
46 static int nr_job_sections;
48 int exitall_on_terminate = 0;
49 int output_format = FIO_OUTPUT_NORMAL;
50 int append_terse_output = 0;
51 int eta_print = FIO_ETA_AUTO;
55 char *exec_profile = NULL;
56 int warnings_fatal = 0;
57 int terse_version = 3;
64 int status_interval = 0;
66 static int write_lat_log;
68 static int prev_group_jobs;
70 unsigned long fio_debug = 0;
71 unsigned int fio_debug_jobno = -1;
72 unsigned int *fio_debug_jobp = NULL;
74 static char cmd_optstr[256];
77 #define FIO_CLIENT_FLAG (1 << 16)
80 * Command line options. These will contain the above, plus a few
81 * extra that only pertain to fio itself and not jobs.
83 static struct option l_opts[FIO_NR_OPTIONS] = {
85 .name = (char *) "output",
86 .has_arg = required_argument,
87 .val = 'o' | FIO_CLIENT_FLAG,
90 .name = (char *) "timeout",
91 .has_arg = required_argument,
92 .val = 't' | FIO_CLIENT_FLAG,
95 .name = (char *) "latency-log",
96 .has_arg = required_argument,
97 .val = 'l' | FIO_CLIENT_FLAG,
100 .name = (char *) "bandwidth-log",
101 .has_arg = required_argument,
102 .val = 'b' | FIO_CLIENT_FLAG,
105 .name = (char *) "minimal",
106 .has_arg = no_argument,
107 .val = 'm' | FIO_CLIENT_FLAG,
110 .name = (char *) "output-format",
111 .has_arg = optional_argument,
112 .val = 'F' | FIO_CLIENT_FLAG,
115 .name = (char *) "append-terse",
116 .has_arg = optional_argument,
120 .name = (char *) "version",
121 .has_arg = no_argument,
122 .val = 'v' | FIO_CLIENT_FLAG,
125 .name = (char *) "help",
126 .has_arg = no_argument,
127 .val = 'h' | FIO_CLIENT_FLAG,
130 .name = (char *) "cmdhelp",
131 .has_arg = optional_argument,
132 .val = 'c' | FIO_CLIENT_FLAG,
135 .name = (char *) "enghelp",
136 .has_arg = optional_argument,
137 .val = 'i' | FIO_CLIENT_FLAG,
140 .name = (char *) "showcmd",
141 .has_arg = no_argument,
142 .val = 's' | FIO_CLIENT_FLAG,
145 .name = (char *) "readonly",
146 .has_arg = no_argument,
147 .val = 'r' | FIO_CLIENT_FLAG,
150 .name = (char *) "eta",
151 .has_arg = required_argument,
152 .val = 'e' | FIO_CLIENT_FLAG,
155 .name = (char *) "eta-newline",
156 .has_arg = required_argument,
157 .val = 'E' | FIO_CLIENT_FLAG,
160 .name = (char *) "debug",
161 .has_arg = required_argument,
162 .val = 'd' | FIO_CLIENT_FLAG,
165 .name = (char *) "parse-only",
166 .has_arg = no_argument,
167 .val = 'P' | FIO_CLIENT_FLAG,
170 .name = (char *) "section",
171 .has_arg = required_argument,
172 .val = 'x' | FIO_CLIENT_FLAG,
175 .name = (char *) "alloc-size",
176 .has_arg = required_argument,
177 .val = 'a' | FIO_CLIENT_FLAG,
180 .name = (char *) "profile",
181 .has_arg = required_argument,
182 .val = 'p' | FIO_CLIENT_FLAG,
185 .name = (char *) "warnings-fatal",
186 .has_arg = no_argument,
187 .val = 'w' | FIO_CLIENT_FLAG,
190 .name = (char *) "max-jobs",
191 .has_arg = required_argument,
192 .val = 'j' | FIO_CLIENT_FLAG,
195 .name = (char *) "terse-version",
196 .has_arg = required_argument,
197 .val = 'V' | FIO_CLIENT_FLAG,
200 .name = (char *) "server",
201 .has_arg = optional_argument,
204 { .name = (char *) "daemonize",
205 .has_arg = required_argument,
209 .name = (char *) "client",
210 .has_arg = required_argument,
214 .name = (char *) "cpuclock-test",
215 .has_arg = no_argument,
219 .name = (char *) "crctest",
220 .has_arg = optional_argument,
224 .name = (char *) "idle-prof",
225 .has_arg = required_argument,
229 .name = (char *) "status-interval",
230 .has_arg = required_argument,
238 void free_threads_shm(void)
240 struct shmid_ds sbuf;
247 shmctl(shm_id, IPC_RMID, &sbuf);
252 static void free_shm(void)
257 fio_debug_jobp = NULL;
261 options_free(fio_options, &def_thread);
266 * The thread area is shared between the main process and the job
267 * threads/processes. So setup a shared memory segment that will hold
268 * all the job info. We use the end of the region for keeping track of
269 * open files across jobs, for file sharing.
271 static int setup_thread_area(void)
279 * 1024 is too much on some machines, scale max_jobs if
280 * we get a failure that looks like too large a shm segment
283 size_t size = max_jobs * sizeof(struct thread_data);
285 size += file_hash_size;
286 size += sizeof(unsigned int);
288 shm_id = shmget(0, size, IPC_CREAT | 0600);
291 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
302 threads = shmat(shm_id, NULL, 0);
303 if (threads == (void *) -1) {
308 memset(threads, 0, max_jobs * sizeof(struct thread_data));
309 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
310 fio_debug_jobp = (void *) hash + file_hash_size;
311 *fio_debug_jobp = -1;
312 file_hash_init(hash);
320 * Return a free job structure.
322 static struct thread_data *get_new_job(int global, struct thread_data *parent,
325 struct thread_data *td;
329 if (setup_thread_area()) {
330 log_err("error: failed to setup shm segment\n");
333 if (thread_number >= max_jobs) {
334 log_err("error: maximum number of jobs (%d) reached.\n",
339 td = &threads[thread_number++];
346 td->o.uid = td->o.gid = -1U;
348 dup_files(td, parent);
349 fio_options_mem_dupe(td);
351 profile_add_hooks(td);
353 td->thread_number = thread_number;
355 if (!parent || !parent->o.group_reporting)
361 static void put_job(struct thread_data *td)
363 if (td == &def_thread)
370 log_info("fio: %s\n", td->verror);
372 fio_options_free(td);
376 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
380 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
382 unsigned int bs = td->o.min_bs[ddir];
384 assert(ddir_rw(ddir));
386 if (td->o.rate[ddir])
387 td->rate_bps[ddir] = td->o.rate[ddir];
389 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
391 if (!td->rate_bps[ddir]) {
392 log_err("rate lower than supported\n");
396 td->rate_pending_usleep[ddir] = 0;
400 static int setup_rate(struct thread_data *td)
404 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
405 ret = __setup_rate(td, DDIR_READ);
406 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
407 ret |= __setup_rate(td, DDIR_WRITE);
408 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
409 ret |= __setup_rate(td, DDIR_TRIM);
414 static int fixed_block_size(struct thread_options *o)
416 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
417 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
418 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
419 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
420 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
424 static unsigned long long get_rand_start_delay(struct thread_data *td)
426 unsigned long long delayrange;
429 delayrange = td->o.start_delay_high - td->o.start_delay;
431 if (td->o.use_os_rand) {
432 r = os_random_long(&td->delay_state);
433 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
435 r = __rand(&td->__delay_state);
436 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
439 delayrange += td->o.start_delay;
444 * Lazy way of fixing up options that depend on each other. We could also
445 * define option callback handlers, but this is easier.
447 static int fixup_options(struct thread_data *td)
449 struct thread_options *o = &td->o;
452 #ifndef FIO_HAVE_PSHARED_MUTEX
453 if (!o->use_thread) {
454 log_info("fio: this platform does not support process shared"
455 " mutexes, forcing use of threads. Use the 'thread'"
456 " option to get rid of this warning.\n");
458 ret = warnings_fatal;
462 if (o->write_iolog_file && o->read_iolog_file) {
463 log_err("fio: read iolog overrides write_iolog\n");
464 free(o->write_iolog_file);
465 o->write_iolog_file = NULL;
466 ret = warnings_fatal;
470 * only really works with 1 file
472 if (o->zone_size && o->open_files > 1)
476 * If zone_range isn't specified, backward compatibility dictates it
477 * should be made equal to zone_size.
479 if (o->zone_size && !o->zone_range)
480 o->zone_range = o->zone_size;
483 * Reads can do overwrites, we always need to pre-create the file
485 if (td_read(td) || td_rw(td))
488 if (!o->min_bs[DDIR_READ])
489 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
490 if (!o->max_bs[DDIR_READ])
491 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
492 if (!o->min_bs[DDIR_WRITE])
493 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
494 if (!o->max_bs[DDIR_WRITE])
495 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
496 if (!o->min_bs[DDIR_TRIM])
497 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
498 if (!o->max_bs[DDIR_TRIM])
499 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
502 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
503 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
506 * For random IO, allow blockalign offset other than min_bs.
508 if (!o->ba[DDIR_READ] || !td_random(td))
509 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
510 if (!o->ba[DDIR_WRITE] || !td_random(td))
511 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
512 if (!o->ba[DDIR_TRIM] || !td_random(td))
513 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
515 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
516 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
517 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
519 log_err("fio: Any use of blockalign= turns off randommap\n");
521 ret = warnings_fatal;
524 if (!o->file_size_high)
525 o->file_size_high = o->file_size_low;
527 if (o->start_delay_high)
528 o->start_delay = get_rand_start_delay(td);
530 if (o->norandommap && o->verify != VERIFY_NONE
531 && !fixed_block_size(o)) {
532 log_err("fio: norandommap given for variable block sizes, "
533 "verify disabled\n");
534 o->verify = VERIFY_NONE;
535 ret = warnings_fatal;
537 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
538 log_err("fio: bs_unaligned may not work with raw io\n");
541 * thinktime_spin must be less than thinktime
543 if (o->thinktime_spin > o->thinktime)
544 o->thinktime_spin = o->thinktime;
547 * The low water mark cannot be bigger than the iodepth
549 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
550 o->iodepth_low = o->iodepth;
553 * If batch number isn't set, default to the same as iodepth
555 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
556 o->iodepth_batch = o->iodepth;
558 if (o->nr_files > td->files_index)
559 o->nr_files = td->files_index;
561 if (o->open_files > o->nr_files || !o->open_files)
562 o->open_files = o->nr_files;
564 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
565 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
566 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
567 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
568 log_err("fio: rate and rate_iops are mutually exclusive\n");
571 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
572 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
573 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
574 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
575 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
576 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
577 log_err("fio: minimum rate exceeds rate\n");
581 if (!o->timeout && o->time_based) {
582 log_err("fio: time_based requires a runtime/timeout setting\n");
584 ret = warnings_fatal;
587 if (o->fill_device && !o->size)
590 if (o->verify != VERIFY_NONE) {
591 if (td_write(td) && o->do_verify && o->numjobs > 1) {
592 log_info("Multiple writers may overwrite blocks that "
593 "belong to other jobs. This can cause "
594 "verification failures.\n");
595 ret = warnings_fatal;
598 o->refill_buffers = 1;
599 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
601 o->verify_interval = o->min_bs[DDIR_WRITE];
605 o->invalidate_cache = 0;
606 if (td->io_ops->flags & FIO_PIPEIO) {
607 log_info("fio: cannot pre-read files with an IO engine"
608 " that isn't seekable. Pre-read disabled.\n");
609 ret = warnings_fatal;
614 if (td->io_ops->flags & FIO_BIT_BASED)
620 #ifndef CONFIG_FDATASYNC
621 if (o->fdatasync_blocks) {
622 log_info("fio: this platform does not support fdatasync()"
623 " falling back to using fsync(). Use the 'fsync'"
624 " option instead of 'fdatasync' to get rid of"
626 o->fsync_blocks = o->fdatasync_blocks;
627 o->fdatasync_blocks = 0;
628 ret = warnings_fatal;
634 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
635 * so fail if we're passed those flags
637 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
638 log_err("fio: Windows does not support direct or non-buffered io with"
639 " the synchronous ioengines. Use the 'windowsaio' ioengine"
640 " with 'direct=1' and 'iodepth=1' instead.\n");
646 * For fully compressible data, just zero them at init time.
647 * It's faster than repeatedly filling it.
649 if (td->o.compress_percentage == 100) {
650 td->o.zero_buffers = 1;
651 td->o.compress_percentage = 0;
655 * Using a non-uniform random distribution excludes usage of
658 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
659 td->o.norandommap = 1;
662 * If size is set but less than the min block size, complain
664 if (o->size && o->size < td_min_bs(td)) {
665 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
670 * O_ATOMIC implies O_DIRECT
676 * If randseed is set, that overrides randrepeat
679 td->o.rand_repeatable = 0;
681 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
682 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
690 * This function leaks the buffer
692 char *fio_uint_to_kmg(unsigned int val)
694 char *buf = malloc(32);
695 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
706 snprintf(buf, 32, "%u%c", val, *p);
710 /* External engines are specified by "external:name.o") */
711 static const char *get_engine_name(const char *str)
713 char *p = strstr(str, ":");
719 strip_blank_front(&p);
724 static int exists_and_not_file(const char *filename)
728 if (lstat(filename, &sb) == -1)
731 /* \\.\ is the device namespace in Windows, where every file
732 * is a device node */
733 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
739 static void td_fill_rand_seeds_os(struct thread_data *td)
741 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
742 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
743 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
745 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
746 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
748 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
749 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
750 os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
755 if (td->o.rand_repeatable)
756 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
758 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
760 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
761 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
762 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], &td->seq_rand_state[DDIR_TRIM]);
765 static void td_fill_rand_seeds_internal(struct thread_data *td)
767 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
768 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
769 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
771 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
772 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
774 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
775 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
776 init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
781 if (td->o.rand_repeatable)
782 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
784 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
785 init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
786 init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
787 init_rand_seed(&td->__seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF]);
790 void td_fill_rand_seeds(struct thread_data *td)
792 if (td->o.allrand_repeatable) {
793 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
794 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
798 if (td->o.use_os_rand)
799 td_fill_rand_seeds_os(td);
801 td_fill_rand_seeds_internal(td);
803 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
807 * Initializes the ioengine configured for a job, if it has not been done so
810 int ioengine_load(struct thread_data *td)
815 * Engine has already been loaded.
819 if (!td->o.ioengine) {
820 log_err("fio: internal fault, no IO engine specified\n");
824 engine = get_engine_name(td->o.ioengine);
825 td->io_ops = load_ioengine(td, engine);
827 log_err("fio: failed to load engine %s\n", engine);
831 if (td->io_ops->option_struct_size && td->io_ops->options) {
833 * In cases where td->eo is set, clone it for a child thread.
834 * This requires that the parent thread has the same ioengine,
835 * but that requirement must be enforced by the code which
838 void *origeo = td->eo;
840 * Otherwise use the default thread options.
842 if (!origeo && td != &def_thread && def_thread.eo &&
843 def_thread.io_ops->options == td->io_ops->options)
844 origeo = def_thread.eo;
846 options_init(td->io_ops->options);
847 td->eo = malloc(td->io_ops->option_struct_size);
849 * Use the default thread as an option template if this uses the
850 * same options structure and there are non-default options
854 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
855 options_mem_dupe(td->eo, td->io_ops->options);
857 memset(td->eo, 0, td->io_ops->option_struct_size);
858 fill_default_options(td->eo, td->io_ops->options);
860 *(struct thread_data **)td->eo = td;
866 static void init_flags(struct thread_data *td)
868 struct thread_options *o = &td->o;
870 if (o->verify_backlog)
871 td->flags |= TD_F_VER_BACKLOG;
873 td->flags |= TD_F_TRIM_BACKLOG;
874 if (o->read_iolog_file)
875 td->flags |= TD_F_READ_IOLOG;
876 if (o->refill_buffers)
877 td->flags |= TD_F_REFILL_BUFFERS;
878 if (o->scramble_buffers)
879 td->flags |= TD_F_SCRAMBLE_BUFFERS;
880 if (o->verify != VERIFY_NONE)
881 td->flags |= TD_F_VER_NONE;
884 static int setup_random_seeds(struct thread_data *td)
889 if (!td->o.rand_repeatable && !td->o.rand_seed)
890 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
892 if (!td->o.rand_seed)
895 seed = td->o.rand_seed;
897 for (i = 0; i < 4; i++)
898 seed *= 0x9e370001UL;
900 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
901 td->rand_seeds[i] = seed;
902 seed *= 0x9e370001UL;
905 td_fill_rand_seeds(td);
916 static struct fpre_keyword {
920 } fpre_keywords[] = {
921 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
922 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
923 { .keyword = "$filenum", .key = FPRE_FILENUM, },
924 { .keyword = NULL, },
927 static char *make_filename(char *buf, struct thread_options *o,
928 const char *jobname, int jobnum, int filenum)
930 struct fpre_keyword *f;
933 if (!o->filename_format || !strlen(o->filename_format)) {
934 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
938 for (f = &fpre_keywords[0]; f->keyword; f++)
939 f->strlen = strlen(f->keyword);
941 strcpy(buf, o->filename_format);
942 memset(copy, 0, sizeof(copy));
943 for (f = &fpre_keywords[0]; f->keyword; f++) {
945 size_t pre_len, post_start = 0;
946 char *str, *dst = copy;
948 str = strcasestr(buf, f->keyword);
953 if (strlen(str) != f->strlen)
954 post_start = pre_len + f->strlen;
957 strncpy(dst, buf, pre_len);
963 dst += sprintf(dst, "%s", jobname);
966 dst += sprintf(dst, "%d", jobnum);
969 dst += sprintf(dst, "%d", filenum);
977 strcpy(dst, buf + post_start);
986 int parse_dryrun(void)
988 return dump_cmdline || parse_only;
992 * Adds a job to the list of things todo. Sanitizes the various options
993 * to make sure we don't have conflicts, and initializes various
996 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
997 int recursed, int client_type)
1000 char fname[PATH_MAX];
1001 int numjobs, file_alloced;
1002 struct thread_options *o = &td->o;
1005 * the def_thread is just for options, it's not a real job
1007 if (td == &def_thread)
1013 * if we are just dumping the output command line, don't add the job
1015 if (parse_dryrun()) {
1020 td->client_type = client_type;
1022 if (profile_td_init(td))
1025 if (ioengine_load(td))
1029 td->io_ops->flags |= FIO_RAWIO;
1032 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1035 if (o->nr_files == 1 && exists_and_not_file(jobname))
1036 add_file(td, jobname, job_add_num, 0);
1038 for (i = 0; i < o->nr_files; i++)
1039 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
1043 if (fixup_options(td))
1049 * IO engines only need this for option callbacks, and the address may
1050 * change in subprocesses.
1053 *(struct thread_data **)td->eo = NULL;
1055 if (td->io_ops->flags & FIO_DISKLESSIO) {
1058 for_each_file(td, f, i)
1059 f->real_file_size = -1ULL;
1062 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1064 td->ts.clat_percentiles = o->clat_percentiles;
1065 td->ts.percentile_precision = o->percentile_precision;
1066 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1068 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1069 td->ts.clat_stat[i].min_val = ULONG_MAX;
1070 td->ts.slat_stat[i].min_val = ULONG_MAX;
1071 td->ts.lat_stat[i].min_val = ULONG_MAX;
1072 td->ts.bw_stat[i].min_val = ULONG_MAX;
1074 td->ddir_seq_nr = o->ddir_seq_nr;
1076 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1077 prev_group_jobs = 0;
1081 td->groupid = groupid;
1084 if (setup_random_seeds(td)) {
1085 td_verror(td, errno, "init_random_state");
1092 if (o->lat_log_file || write_lat_log) {
1093 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1094 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1095 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
1097 if (o->bw_log_file || write_bw_log)
1098 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1099 if (o->iops_log_file)
1100 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
1103 o->name = strdup(jobname);
1105 if (output_format == FIO_OUTPUT_NORMAL) {
1107 if (is_backend && !recursed)
1108 fio_server_send_add_job(td);
1110 if (!(td->io_ops->flags & FIO_NOIO)) {
1111 char *c1, *c2, *c3, *c4;
1112 char *c5 = NULL, *c6 = NULL;
1114 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1115 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1116 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1117 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1119 if (!o->bs_is_seq_rand) {
1120 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1121 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1124 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1126 ddir_str(o->td_ddir));
1128 if (o->bs_is_seq_rand)
1129 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1132 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1133 c1, c2, c3, c4, c5, c6);
1135 log_info("ioengine=%s, iodepth=%u\n",
1136 td->io_ops->name, o->iodepth);
1145 } else if (job_add_num == 1)
1150 * recurse add identical jobs, clear numjobs and stonewall options
1151 * as they don't apply to sub-jobs
1153 numjobs = o->numjobs;
1155 struct thread_data *td_new = get_new_job(0, td, 1);
1160 td_new->o.numjobs = 1;
1161 td_new->o.stonewall = 0;
1162 td_new->o.new_group = 0;
1165 if (td_new->files) {
1167 for_each_file(td_new, f, i) {
1169 sfree(f->file_name);
1172 free(td_new->files);
1173 td_new->files = NULL;
1175 td_new->files_index = 0;
1176 td_new->files_size = 0;
1177 if (td_new->o.filename) {
1178 free(td_new->o.filename);
1179 td_new->o.filename = NULL;
1183 if (add_job(td_new, jobname, numjobs, 1, client_type))
1194 * Parse as if 'o' was a command line
1196 void add_job_opts(const char **o, int client_type)
1198 struct thread_data *td, *td_parent;
1199 int i, in_global = 1;
1203 td_parent = td = NULL;
1205 if (!strncmp(o[i], "name", 4)) {
1208 add_job(td, jobname, 0, 0, client_type);
1210 sprintf(jobname, "%s", o[i] + 5);
1212 if (in_global && !td_parent)
1213 td_parent = get_new_job(1, &def_thread, 0);
1214 else if (!in_global && !td) {
1216 td_parent = &def_thread;
1217 td = get_new_job(0, td_parent, 0);
1220 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
1222 fio_options_parse(td, (char **) &o[i], 1, 0);
1227 add_job(td, jobname, 0, 0, client_type);
1230 static int skip_this_section(const char *name)
1234 if (!nr_job_sections)
1236 if (!strncmp(name, "global", 6))
1239 for (i = 0; i < nr_job_sections; i++)
1240 if (!strcmp(job_sections[i], name))
1246 static int is_empty_or_comment(char *line)
1250 for (i = 0; i < strlen(line); i++) {
1255 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1263 * This is our [ini] type file parser.
1265 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1267 unsigned int global;
1268 struct thread_data *td;
1269 char *string, *name;
1272 int ret = 0, stonewall;
1275 int inside_skip = 0;
1277 int i, alloc_opts, num_opts;
1282 if (!strcmp(file, "-"))
1285 f = fopen(file, "r");
1288 perror("fopen job file");
1293 string = malloc(4096);
1296 * it's really 256 + small bit, 280 should suffice
1299 memset(name, 0, 280);
1302 opts = malloc(sizeof(char *) * alloc_opts);
1305 stonewall = stonewall_flag;
1308 * if skip_fgets is set, we already have loaded a line we
1313 p = strsep(&file, "\n");
1315 p = fgets(string, 4096, f);
1321 strip_blank_front(&p);
1324 if (is_empty_or_comment(p))
1326 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1329 log_err("fio: option <%s> outside of [] job section\n",
1334 name[strlen(name) - 1] = '\0';
1336 if (skip_this_section(name)) {
1342 global = !strncmp(name, "global", 6);
1348 log_info("--name=%s ", name);
1352 td = get_new_job(global, &def_thread, 0);
1359 * Separate multiple job files by a stonewall
1361 if (!global && stonewall) {
1362 td->o.stonewall = stonewall;
1367 memset(opts, 0, alloc_opts * sizeof(char *));
1371 p = strsep(&file, "\n");
1373 p = fgets(string, 4096, f);
1377 if (is_empty_or_comment(p))
1380 strip_blank_front(&p);
1383 * new section, break out and make sure we don't
1384 * fgets() a new line at the top.
1393 if (num_opts == alloc_opts) {
1395 opts = realloc(opts,
1396 alloc_opts * sizeof(char *));
1399 opts[num_opts] = strdup(p);
1403 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1405 ret = add_job(td, name, 0, 0, type);
1407 log_err("fio: job %s dropped\n", name);
1411 for (i = 0; i < num_opts; i++)
1420 while (i < nr_job_sections) {
1421 free(job_sections[i]);
1425 for (i = 0; i < num_opts; i++)
1431 if (!is_buf && f != stdin)
1436 static int fill_def_thread(void)
1438 memset(&def_thread, 0, sizeof(def_thread));
1440 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1441 def_thread.o.timeout = def_timeout;
1442 def_thread.o.error_dump = 1;
1444 * fill default options
1446 fio_fill_default_options(&def_thread);
1450 static void usage(const char *name)
1452 printf("%s\n", fio_version_string);
1453 printf("%s [options] [job options] <job file(s)>\n", name);
1454 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1455 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1456 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
1457 printf(" --parse-only\t\tParse options only, don't start any IO\n");
1458 printf(" --output\t\tWrite output to file\n");
1459 printf(" --runtime\t\tRuntime in seconds\n");
1460 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1461 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1462 printf(" --minimal\t\tMinimal (terse) output\n");
1463 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
1464 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
1465 printf(" --version\t\tPrint version info and exit\n");
1466 printf(" --help\t\tPrint this page\n");
1467 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
1468 printf(" --crctest\t\tTest speed of checksum functions\n");
1469 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
1471 printf(" --enghelp=engine\tPrint ioengine help, or list"
1472 " available ioengines\n");
1473 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1475 printf(" --showcmd\t\tTurn a job file into command line options\n");
1476 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1477 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
1478 printf(" --eta-newline=time\tForce a new line for every 'time'");
1479 printf(" period passed\n");
1480 printf(" --status-interval=t\tForce full status dump every");
1481 printf(" 't' period passed\n");
1482 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
1484 printf(" --section=name\tOnly run specified section in job file\n");
1485 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
1487 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1488 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1489 printf(" --server=args\t\tStart a backend fio server\n");
1490 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1491 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
1492 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1493 "\t\t\t(option=system,percpu) or run unit work\n"
1494 "\t\t\tcalibration only (option=calibrate)\n");
1495 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1496 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1497 printf("\n Jens Axboe <axboe@fb.com>\n");
1500 #ifdef FIO_INC_DEBUG
1501 struct debug_level debug_levels[] = {
1502 { .name = "process",
1503 .help = "Process creation/exit logging",
1504 .shift = FD_PROCESS,
1507 .help = "File related action logging",
1511 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1515 .help = "Memory allocation/freeing logging",
1518 { .name = "blktrace",
1519 .help = "blktrace action logging",
1520 .shift = FD_BLKTRACE,
1523 .help = "IO verification action logging",
1527 .help = "Random generation logging",
1531 .help = "Parser logging",
1534 { .name = "diskutil",
1535 .help = "Disk utility logging actions",
1536 .shift = FD_DISKUTIL,
1539 .help = "Logging related to creating/destroying jobs",
1543 .help = "Mutex logging",
1546 { .name = "profile",
1547 .help = "Logging related to profiles",
1548 .shift = FD_PROFILE,
1551 .help = "Logging related to time keeping functions",
1555 .help = "Network logging",
1559 .help = "Rate logging",
1565 static int set_debug(const char *string)
1567 struct debug_level *dl;
1568 char *p = (char *) string;
1572 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1573 log_info("fio: dumping debug options:");
1574 for (i = 0; debug_levels[i].name; i++) {
1575 dl = &debug_levels[i];
1576 log_info("%s,", dl->name);
1582 while ((opt = strsep(&p, ",")) != NULL) {
1585 if (!strncmp(opt, "all", 3)) {
1586 log_info("fio: set all debug options\n");
1591 for (i = 0; debug_levels[i].name; i++) {
1592 dl = &debug_levels[i];
1593 found = !strncmp(opt, dl->name, strlen(dl->name));
1597 if (dl->shift == FD_JOB) {
1598 opt = strchr(opt, ':');
1600 log_err("fio: missing job number\n");
1604 fio_debug_jobno = atoi(opt);
1605 log_info("fio: set debug jobno %d\n",
1608 log_info("fio: set debug option %s\n", opt);
1609 fio_debug |= (1UL << dl->shift);
1615 log_err("fio: debug mask %s not found\n", opt);
1620 static int set_debug(const char *string)
1622 log_err("fio: debug tracing not included in build\n");
1627 static void fio_options_fill_optstring(void)
1629 char *ostr = cmd_optstr;
1633 while (l_opts[i].name) {
1634 ostr[c++] = l_opts[i].val;
1635 if (l_opts[i].has_arg == required_argument)
1637 else if (l_opts[i].has_arg == optional_argument) {
1646 static int client_flag_set(char c)
1651 while (l_opts[i].name) {
1652 int val = l_opts[i].val;
1654 if (c == (val & 0xff))
1655 return (val & FIO_CLIENT_FLAG);
1663 static void parse_cmd_client(void *client, char *opt)
1665 fio_client_add_cmd_option(client, opt);
1668 int parse_cmd_line(int argc, char *argv[], int client_type)
1670 struct thread_data *td = NULL;
1671 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1672 char *ostr = cmd_optstr;
1673 void *pid_file = NULL;
1674 void *cur_client = NULL;
1678 * Reset optind handling, since we may call this multiple times
1683 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1686 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
1687 parse_cmd_client(cur_client, argv[optind - 1]);
1688 c &= ~FIO_CLIENT_FLAG;
1693 smalloc_pool_size = atoi(optarg);
1696 def_timeout = atoi(optarg);
1705 f_out = fopen(optarg, "w+");
1707 perror("fopen output");
1713 output_format = FIO_OUTPUT_TERSE;
1717 log_err("fio: missing --output-format argument\n");
1722 if (!strcmp(optarg, "minimal") ||
1723 !strcmp(optarg, "terse") ||
1724 !strcmp(optarg, "csv"))
1725 output_format = FIO_OUTPUT_TERSE;
1726 else if (!strcmp(optarg, "json"))
1727 output_format = FIO_OUTPUT_JSON;
1729 output_format = FIO_OUTPUT_NORMAL;
1732 append_terse_output = 1;
1742 fio_show_option_help(optarg);
1748 fio_show_ioengine_help(optarg);
1760 log_info("%s\n", fio_version_string);
1765 terse_version = atoi(optarg);
1766 if (!(terse_version == 2 || terse_version == 3 ||
1767 terse_version == 4)) {
1768 log_err("fio: bad terse version format\n");
1774 if (!strcmp("always", optarg))
1775 eta_print = FIO_ETA_ALWAYS;
1776 else if (!strcmp("never", optarg))
1777 eta_print = FIO_ETA_NEVER;
1782 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
1783 log_err("fio: failed parsing eta time %s\n", optarg);
1791 if (set_debug(optarg))
1800 if (!strcmp(optarg, "global")) {
1801 log_err("fio: can't use global as only "
1807 new_size = (nr_job_sections + 1) * sizeof(char *);
1808 job_sections = realloc(job_sections, new_size);
1809 job_sections[nr_job_sections] = strdup(optarg);
1814 exec_profile = strdup(optarg);
1816 case FIO_GETOPT_JOB: {
1817 const char *opt = l_opts[lidx].name;
1820 if (!strncmp(opt, "name", 4) && td) {
1821 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
1827 int is_section = !strncmp(opt, "name", 4);
1830 if (!is_section || !strncmp(val, "global", 6))
1833 if (is_section && skip_this_section(val))
1836 td = get_new_job(global, &def_thread, 1);
1837 if (!td || ioengine_load(td))
1839 fio_options_set_ioengine_opts(l_opts, td);
1842 if ((!val || !strlen(val)) &&
1843 l_opts[lidx].has_arg == required_argument) {
1844 log_err("fio: option %s requires an argument\n", opt);
1847 ret = fio_cmd_option_parse(td, opt, val);
1857 if (!ret && !strcmp(opt, "ioengine")) {
1859 if (ioengine_load(td))
1861 fio_options_set_ioengine_opts(l_opts, td);
1865 case FIO_GETOPT_IOENGINE: {
1866 const char *opt = l_opts[lidx].name;
1868 ret = fio_cmd_ioengine_option_parse(td, opt, val);
1875 max_jobs = atoi(optarg);
1876 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1877 log_err("fio: invalid max jobs: %d\n", max_jobs);
1884 log_err("fio: can't be both client and server\n");
1890 fio_server_set_arg(optarg);
1895 pid_file = strdup(optarg);
1898 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1899 /* exit on error and calibration only */
1907 log_err("fio: can't be both client and server\n");
1912 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
1913 log_err("fio: failed adding client %s\n", optarg);
1919 * If the next argument exists and isn't an option,
1920 * assume it's a job file for this client only.
1922 while (optind < argc) {
1923 if (!strncmp(argv[optind], "--", 2) ||
1924 !strncmp(argv[optind], "-", 1))
1927 fio_client_add_ini_file(cur_client, argv[optind]);
1933 exit_val = fio_monotonic_clocktest();
1937 exit_val = fio_crctest(optarg);
1942 if (check_str_time(optarg, &val, 0)) {
1943 log_err("fio: failed parsing time %s\n", optarg);
1948 status_interval = val * 1000;
1952 log_err("%s: unrecognized option '%s'\n", argv[0],
1963 if (do_exit && !(is_backend || nr_clients))
1966 if (nr_clients && fio_clients_connect())
1969 if (is_backend && backend)
1970 return fio_start_server(pid_file);
1974 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
1977 while (!ret && optind < argc) {
1979 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1980 ini_file[ini_idx - 1] = strdup(argv[optind]);
1987 int fio_init_options(void)
1992 fio_options_fill_optstring();
1993 fio_options_dup_and_init(l_opts);
1997 if (fill_def_thread())
2003 extern int fio_check_options(struct thread_options *);
2005 int parse_options(int argc, char *argv[])
2007 const int type = FIO_CLIENT_TYPE_CLI;
2010 if (fio_init_options())
2012 if (fio_test_cconv(&def_thread.o))
2013 log_err("fio: failed internal cconv test\n");
2015 job_files = parse_cmd_line(argc, argv, type);
2017 if (job_files > 0) {
2018 for (i = 0; i < job_files; i++) {
2019 if (fill_def_thread())
2022 if (fio_clients_send_ini(ini_file[i]))
2025 } else if (!is_backend) {
2026 if (parse_jobs_ini(ini_file[i], 0, i, type))
2031 } else if (nr_clients) {
2032 if (fill_def_thread())
2034 if (fio_clients_send_ini(NULL))
2039 fio_options_free(&def_thread);
2040 filesetup_mem_free();
2042 if (!thread_number) {
2047 if (is_backend || nr_clients)
2052 log_err("No jobs(s) defined\n\n");
2062 if (def_thread.o.gtod_offload) {
2064 fio_gtod_offload = 1;
2065 fio_gtod_cpu = def_thread.o.gtod_cpu;
2068 if (output_format == FIO_OUTPUT_NORMAL)
2069 log_info("%s\n", fio_version_string);
2074 void options_default_fill(struct thread_options *o)
2076 memcpy(o, &def_thread.o, sizeof(*o));