2 * This file contains job initialization and setup functions.
12 #include <sys/types.h>
16 #ifndef FIO_NO_HAVE_SHM_H
29 #include "lib/getopt.h"
30 #include "lib/strcasestr.h"
34 const char fio_version_string[] = FIO_VERSION;
36 #define FIO_RANDSEED (0xb1899bedUL)
38 static char **ini_file;
39 static int max_jobs = FIO_MAX_JOBS;
40 static int dump_cmdline;
41 static int def_timeout;
42 static int parse_only;
44 static struct thread_data def_thread;
45 struct thread_data *threads = NULL;
46 static char **job_sections;
47 static int nr_job_sections;
49 int exitall_on_terminate = 0;
50 int output_format = FIO_OUTPUT_NORMAL;
51 int append_terse_output = 0;
52 int eta_print = FIO_ETA_AUTO;
56 char *exec_profile = NULL;
57 int warnings_fatal = 0;
58 int terse_version = 3;
65 int status_interval = 0;
67 static int write_lat_log;
69 static int prev_group_jobs;
71 unsigned long fio_debug = 0;
72 unsigned int fio_debug_jobno = -1;
73 unsigned int *fio_debug_jobp = NULL;
75 static char cmd_optstr[256];
78 #define FIO_CLIENT_FLAG (1 << 16)
81 * Command line options. These will contain the above, plus a few
82 * extra that only pertain to fio itself and not jobs.
84 static struct option l_opts[FIO_NR_OPTIONS] = {
86 .name = (char *) "output",
87 .has_arg = required_argument,
88 .val = 'o' | FIO_CLIENT_FLAG,
91 .name = (char *) "timeout",
92 .has_arg = required_argument,
93 .val = 't' | FIO_CLIENT_FLAG,
96 .name = (char *) "latency-log",
97 .has_arg = required_argument,
98 .val = 'l' | FIO_CLIENT_FLAG,
101 .name = (char *) "bandwidth-log",
102 .has_arg = required_argument,
103 .val = 'b' | FIO_CLIENT_FLAG,
106 .name = (char *) "minimal",
107 .has_arg = no_argument,
108 .val = 'm' | FIO_CLIENT_FLAG,
111 .name = (char *) "output-format",
112 .has_arg = optional_argument,
113 .val = 'F' | FIO_CLIENT_FLAG,
116 .name = (char *) "append-terse",
117 .has_arg = optional_argument,
121 .name = (char *) "version",
122 .has_arg = no_argument,
123 .val = 'v' | FIO_CLIENT_FLAG,
126 .name = (char *) "help",
127 .has_arg = no_argument,
128 .val = 'h' | FIO_CLIENT_FLAG,
131 .name = (char *) "cmdhelp",
132 .has_arg = optional_argument,
133 .val = 'c' | FIO_CLIENT_FLAG,
136 .name = (char *) "enghelp",
137 .has_arg = optional_argument,
138 .val = 'i' | FIO_CLIENT_FLAG,
141 .name = (char *) "showcmd",
142 .has_arg = no_argument,
143 .val = 's' | FIO_CLIENT_FLAG,
146 .name = (char *) "readonly",
147 .has_arg = no_argument,
148 .val = 'r' | FIO_CLIENT_FLAG,
151 .name = (char *) "eta",
152 .has_arg = required_argument,
153 .val = 'e' | FIO_CLIENT_FLAG,
156 .name = (char *) "eta-newline",
157 .has_arg = required_argument,
158 .val = 'E' | FIO_CLIENT_FLAG,
161 .name = (char *) "debug",
162 .has_arg = required_argument,
163 .val = 'd' | FIO_CLIENT_FLAG,
166 .name = (char *) "parse-only",
167 .has_arg = no_argument,
168 .val = 'P' | FIO_CLIENT_FLAG,
171 .name = (char *) "section",
172 .has_arg = required_argument,
173 .val = 'x' | FIO_CLIENT_FLAG,
176 .name = (char *) "alloc-size",
177 .has_arg = required_argument,
178 .val = 'a' | FIO_CLIENT_FLAG,
181 .name = (char *) "profile",
182 .has_arg = required_argument,
183 .val = 'p' | FIO_CLIENT_FLAG,
186 .name = (char *) "warnings-fatal",
187 .has_arg = no_argument,
188 .val = 'w' | FIO_CLIENT_FLAG,
191 .name = (char *) "max-jobs",
192 .has_arg = required_argument,
193 .val = 'j' | FIO_CLIENT_FLAG,
196 .name = (char *) "terse-version",
197 .has_arg = required_argument,
198 .val = 'V' | FIO_CLIENT_FLAG,
201 .name = (char *) "server",
202 .has_arg = optional_argument,
205 { .name = (char *) "daemonize",
206 .has_arg = required_argument,
210 .name = (char *) "client",
211 .has_arg = required_argument,
215 .name = (char *) "cpuclock-test",
216 .has_arg = no_argument,
220 .name = (char *) "crctest",
221 .has_arg = optional_argument,
225 .name = (char *) "idle-prof",
226 .has_arg = required_argument,
230 .name = (char *) "status-interval",
231 .has_arg = required_argument,
239 void free_threads_shm(void)
241 struct shmid_ds sbuf;
248 shmctl(shm_id, IPC_RMID, &sbuf);
253 static void free_shm(void)
258 fio_debug_jobp = NULL;
262 options_free(fio_options, &def_thread);
268 * The thread area is shared between the main process and the job
269 * threads/processes. So setup a shared memory segment that will hold
270 * all the job info. We use the end of the region for keeping track of
271 * open files across jobs, for file sharing.
273 static int setup_thread_area(void)
281 * 1024 is too much on some machines, scale max_jobs if
282 * we get a failure that looks like too large a shm segment
285 size_t size = max_jobs * sizeof(struct thread_data);
287 size += file_hash_size;
288 size += sizeof(unsigned int);
290 shm_id = shmget(0, size, IPC_CREAT | 0600);
293 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
304 threads = shmat(shm_id, NULL, 0);
305 if (threads == (void *) -1) {
310 memset(threads, 0, max_jobs * sizeof(struct thread_data));
311 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
312 fio_debug_jobp = (void *) hash + file_hash_size;
313 *fio_debug_jobp = -1;
314 file_hash_init(hash);
322 * Return a free job structure.
324 static struct thread_data *get_new_job(int global, struct thread_data *parent,
327 struct thread_data *td;
331 if (setup_thread_area()) {
332 log_err("error: failed to setup shm segment\n");
335 if (thread_number >= max_jobs) {
336 log_err("error: maximum number of jobs (%d) reached.\n",
341 td = &threads[thread_number++];
348 td->o.uid = td->o.gid = -1U;
350 dup_files(td, parent);
351 fio_options_mem_dupe(td);
353 profile_add_hooks(td);
355 td->thread_number = thread_number;
357 if (!parent || !parent->o.group_reporting)
363 static void put_job(struct thread_data *td)
365 if (td == &def_thread)
372 log_info("fio: %s\n", td->verror);
374 fio_options_free(td);
378 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
382 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
384 unsigned int bs = td->o.min_bs[ddir];
386 assert(ddir_rw(ddir));
388 if (td->o.rate[ddir])
389 td->rate_bps[ddir] = td->o.rate[ddir];
391 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
393 if (!td->rate_bps[ddir]) {
394 log_err("rate lower than supported\n");
398 td->rate_pending_usleep[ddir] = 0;
402 static int setup_rate(struct thread_data *td)
406 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
407 ret = __setup_rate(td, DDIR_READ);
408 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
409 ret |= __setup_rate(td, DDIR_WRITE);
410 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
411 ret |= __setup_rate(td, DDIR_TRIM);
416 static int fixed_block_size(struct thread_options *o)
418 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
419 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
420 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
421 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
422 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
426 static unsigned long long get_rand_start_delay(struct thread_data *td)
428 unsigned long long delayrange;
431 delayrange = td->o.start_delay_high - td->o.start_delay;
433 if (td->o.use_os_rand) {
434 r = os_random_long(&td->delay_state);
435 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
437 r = __rand(&td->__delay_state);
438 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
441 delayrange += td->o.start_delay;
446 * Lazy way of fixing up options that depend on each other. We could also
447 * define option callback handlers, but this is easier.
449 static int fixup_options(struct thread_data *td)
451 struct thread_options *o = &td->o;
454 #ifndef FIO_HAVE_PSHARED_MUTEX
455 if (!o->use_thread) {
456 log_info("fio: this platform does not support process shared"
457 " mutexes, forcing use of threads. Use the 'thread'"
458 " option to get rid of this warning.\n");
460 ret = warnings_fatal;
464 if (o->write_iolog_file && o->read_iolog_file) {
465 log_err("fio: read iolog overrides write_iolog\n");
466 free(o->write_iolog_file);
467 o->write_iolog_file = NULL;
468 ret = warnings_fatal;
472 * only really works with 1 file
474 if (o->zone_size && o->open_files > 1)
478 * If zone_range isn't specified, backward compatibility dictates it
479 * should be made equal to zone_size.
481 if (o->zone_size && !o->zone_range)
482 o->zone_range = o->zone_size;
485 * Reads can do overwrites, we always need to pre-create the file
487 if (td_read(td) || td_rw(td))
490 if (!o->min_bs[DDIR_READ])
491 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
492 if (!o->max_bs[DDIR_READ])
493 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
494 if (!o->min_bs[DDIR_WRITE])
495 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
496 if (!o->max_bs[DDIR_WRITE])
497 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
498 if (!o->min_bs[DDIR_TRIM])
499 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
500 if (!o->max_bs[DDIR_TRIM])
501 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
504 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
505 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
508 * For random IO, allow blockalign offset other than min_bs.
510 if (!o->ba[DDIR_READ] || !td_random(td))
511 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
512 if (!o->ba[DDIR_WRITE] || !td_random(td))
513 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
514 if (!o->ba[DDIR_TRIM] || !td_random(td))
515 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
517 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
518 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
519 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
521 log_err("fio: Any use of blockalign= turns off randommap\n");
523 ret = warnings_fatal;
526 if (!o->file_size_high)
527 o->file_size_high = o->file_size_low;
529 if (o->start_delay_high)
530 o->start_delay = get_rand_start_delay(td);
532 if (o->norandommap && o->verify != VERIFY_NONE
533 && !fixed_block_size(o)) {
534 log_err("fio: norandommap given for variable block sizes, "
535 "verify disabled\n");
536 o->verify = VERIFY_NONE;
537 ret = warnings_fatal;
539 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
540 log_err("fio: bs_unaligned may not work with raw io\n");
543 * thinktime_spin must be less than thinktime
545 if (o->thinktime_spin > o->thinktime)
546 o->thinktime_spin = o->thinktime;
549 * The low water mark cannot be bigger than the iodepth
551 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
552 o->iodepth_low = o->iodepth;
555 * If batch number isn't set, default to the same as iodepth
557 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
558 o->iodepth_batch = o->iodepth;
560 if (o->nr_files > td->files_index)
561 o->nr_files = td->files_index;
563 if (o->open_files > o->nr_files || !o->open_files)
564 o->open_files = o->nr_files;
566 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
567 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
568 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
569 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
570 log_err("fio: rate and rate_iops are mutually exclusive\n");
573 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
574 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
575 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
576 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
577 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
578 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
579 log_err("fio: minimum rate exceeds rate\n");
583 if (!o->timeout && o->time_based) {
584 log_err("fio: time_based requires a runtime/timeout setting\n");
586 ret = warnings_fatal;
589 if (o->fill_device && !o->size)
592 if (o->verify != VERIFY_NONE) {
593 if (td_write(td) && o->do_verify && o->numjobs > 1) {
594 log_info("Multiple writers may overwrite blocks that "
595 "belong to other jobs. This can cause "
596 "verification failures.\n");
597 ret = warnings_fatal;
600 o->refill_buffers = 1;
601 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
603 o->verify_interval = o->min_bs[DDIR_WRITE];
607 o->invalidate_cache = 0;
608 if (td->io_ops->flags & FIO_PIPEIO) {
609 log_info("fio: cannot pre-read files with an IO engine"
610 " that isn't seekable. Pre-read disabled.\n");
611 ret = warnings_fatal;
616 if (td->io_ops->flags & FIO_BIT_BASED)
622 #ifndef CONFIG_FDATASYNC
623 if (o->fdatasync_blocks) {
624 log_info("fio: this platform does not support fdatasync()"
625 " falling back to using fsync(). Use the 'fsync'"
626 " option instead of 'fdatasync' to get rid of"
628 o->fsync_blocks = o->fdatasync_blocks;
629 o->fdatasync_blocks = 0;
630 ret = warnings_fatal;
636 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
637 * so fail if we're passed those flags
639 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
640 log_err("fio: Windows does not support direct or non-buffered io with"
641 " the synchronous ioengines. Use the 'windowsaio' ioengine"
642 " with 'direct=1' and 'iodepth=1' instead.\n");
648 * For fully compressible data, just zero them at init time.
649 * It's faster than repeatedly filling it.
651 if (td->o.compress_percentage == 100) {
652 td->o.zero_buffers = 1;
653 td->o.compress_percentage = 0;
657 * Using a non-uniform random distribution excludes usage of
660 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
661 td->o.norandommap = 1;
664 * If size is set but less than the min block size, complain
666 if (o->size && o->size < td_min_bs(td)) {
667 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
672 * O_ATOMIC implies O_DIRECT
678 * If randseed is set, that overrides randrepeat
681 td->o.rand_repeatable = 0;
683 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
684 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
692 * This function leaks the buffer
694 char *fio_uint_to_kmg(unsigned int val)
696 char *buf = malloc(32);
697 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
708 snprintf(buf, 32, "%u%c", val, *p);
712 /* External engines are specified by "external:name.o") */
713 static const char *get_engine_name(const char *str)
715 char *p = strstr(str, ":");
721 strip_blank_front(&p);
726 static int exists_and_not_file(const char *filename)
730 if (lstat(filename, &sb) == -1)
733 /* \\.\ is the device namespace in Windows, where every file
734 * is a device node */
735 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
741 static void td_fill_rand_seeds_os(struct thread_data *td)
743 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
744 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
745 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
747 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
748 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
750 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
751 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
752 os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
757 if (td->o.rand_repeatable)
758 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
760 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
762 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
763 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
764 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], &td->seq_rand_state[DDIR_TRIM]);
767 static void td_fill_rand_seeds_internal(struct thread_data *td)
769 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
770 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
771 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
773 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
774 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
776 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
777 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
778 init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
783 if (td->o.rand_repeatable)
784 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
786 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
787 init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
788 init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
789 init_rand_seed(&td->__seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF]);
792 void td_fill_rand_seeds(struct thread_data *td)
794 if (td->o.allrand_repeatable) {
795 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
796 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
800 if (td->o.use_os_rand)
801 td_fill_rand_seeds_os(td);
803 td_fill_rand_seeds_internal(td);
805 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
809 * Initializes the ioengine configured for a job, if it has not been done so
812 int ioengine_load(struct thread_data *td)
817 * Engine has already been loaded.
821 if (!td->o.ioengine) {
822 log_err("fio: internal fault, no IO engine specified\n");
826 engine = get_engine_name(td->o.ioengine);
827 td->io_ops = load_ioengine(td, engine);
829 log_err("fio: failed to load engine %s\n", engine);
833 if (td->io_ops->option_struct_size && td->io_ops->options) {
835 * In cases where td->eo is set, clone it for a child thread.
836 * This requires that the parent thread has the same ioengine,
837 * but that requirement must be enforced by the code which
840 void *origeo = td->eo;
842 * Otherwise use the default thread options.
844 if (!origeo && td != &def_thread && def_thread.eo &&
845 def_thread.io_ops->options == td->io_ops->options)
846 origeo = def_thread.eo;
848 options_init(td->io_ops->options);
849 td->eo = malloc(td->io_ops->option_struct_size);
851 * Use the default thread as an option template if this uses the
852 * same options structure and there are non-default options
856 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
857 options_mem_dupe(td->eo, td->io_ops->options);
859 memset(td->eo, 0, td->io_ops->option_struct_size);
860 fill_default_options(td->eo, td->io_ops->options);
862 *(struct thread_data **)td->eo = td;
868 static void init_flags(struct thread_data *td)
870 struct thread_options *o = &td->o;
872 if (o->verify_backlog)
873 td->flags |= TD_F_VER_BACKLOG;
875 td->flags |= TD_F_TRIM_BACKLOG;
876 if (o->read_iolog_file)
877 td->flags |= TD_F_READ_IOLOG;
878 if (o->refill_buffers)
879 td->flags |= TD_F_REFILL_BUFFERS;
880 if (o->scramble_buffers)
881 td->flags |= TD_F_SCRAMBLE_BUFFERS;
882 if (o->verify != VERIFY_NONE)
883 td->flags |= TD_F_VER_NONE;
886 static int setup_random_seeds(struct thread_data *td)
891 if (!td->o.rand_repeatable && !td->o.rand_seed)
892 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
894 if (!td->o.rand_seed)
897 seed = td->o.rand_seed;
899 for (i = 0; i < 4; i++)
900 seed *= 0x9e370001UL;
902 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
903 td->rand_seeds[i] = seed;
904 seed *= 0x9e370001UL;
907 td_fill_rand_seeds(td);
918 static struct fpre_keyword {
922 } fpre_keywords[] = {
923 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
924 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
925 { .keyword = "$filenum", .key = FPRE_FILENUM, },
926 { .keyword = NULL, },
929 static char *make_filename(char *buf, struct thread_options *o,
930 const char *jobname, int jobnum, int filenum)
932 struct fpre_keyword *f;
935 if (!o->filename_format || !strlen(o->filename_format)) {
936 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
940 for (f = &fpre_keywords[0]; f->keyword; f++)
941 f->strlen = strlen(f->keyword);
943 strcpy(buf, o->filename_format);
944 memset(copy, 0, sizeof(copy));
945 for (f = &fpre_keywords[0]; f->keyword; f++) {
947 size_t pre_len, post_start = 0;
948 char *str, *dst = copy;
950 str = strcasestr(buf, f->keyword);
955 if (strlen(str) != f->strlen)
956 post_start = pre_len + f->strlen;
959 strncpy(dst, buf, pre_len);
965 dst += sprintf(dst, "%s", jobname);
968 dst += sprintf(dst, "%d", jobnum);
971 dst += sprintf(dst, "%d", filenum);
979 strcpy(dst, buf + post_start);
988 int parse_dryrun(void)
990 return dump_cmdline || parse_only;
994 * Adds a job to the list of things todo. Sanitizes the various options
995 * to make sure we don't have conflicts, and initializes various
998 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
999 int recursed, int client_type)
1002 char fname[PATH_MAX];
1003 int numjobs, file_alloced;
1004 struct thread_options *o = &td->o;
1007 * the def_thread is just for options, it's not a real job
1009 if (td == &def_thread)
1015 * if we are just dumping the output command line, don't add the job
1017 if (parse_dryrun()) {
1022 td->client_type = client_type;
1024 if (profile_td_init(td))
1027 if (ioengine_load(td))
1031 td->io_ops->flags |= FIO_RAWIO;
1034 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1037 if (o->nr_files == 1 && exists_and_not_file(jobname))
1038 add_file(td, jobname, job_add_num, 0);
1040 for (i = 0; i < o->nr_files; i++)
1041 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
1045 if (fixup_options(td))
1051 * IO engines only need this for option callbacks, and the address may
1052 * change in subprocesses.
1055 *(struct thread_data **)td->eo = NULL;
1057 if (td->io_ops->flags & FIO_DISKLESSIO) {
1060 for_each_file(td, f, i)
1061 f->real_file_size = -1ULL;
1064 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1066 td->ts.clat_percentiles = o->clat_percentiles;
1067 td->ts.percentile_precision = o->percentile_precision;
1068 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1070 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1071 td->ts.clat_stat[i].min_val = ULONG_MAX;
1072 td->ts.slat_stat[i].min_val = ULONG_MAX;
1073 td->ts.lat_stat[i].min_val = ULONG_MAX;
1074 td->ts.bw_stat[i].min_val = ULONG_MAX;
1076 td->ddir_seq_nr = o->ddir_seq_nr;
1078 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1079 prev_group_jobs = 0;
1083 td->groupid = groupid;
1086 if (setup_random_seeds(td)) {
1087 td_verror(td, errno, "init_random_state");
1094 if (o->lat_log_file || write_lat_log) {
1095 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1096 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1097 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
1099 if (o->bw_log_file || write_bw_log)
1100 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1101 if (o->iops_log_file)
1102 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
1105 o->name = strdup(jobname);
1107 if (output_format == FIO_OUTPUT_NORMAL) {
1109 if (is_backend && !recursed)
1110 fio_server_send_add_job(td);
1112 if (!(td->io_ops->flags & FIO_NOIO)) {
1113 char *c1, *c2, *c3, *c4;
1114 char *c5 = NULL, *c6 = NULL;
1116 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1117 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1118 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1119 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1121 if (!o->bs_is_seq_rand) {
1122 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1123 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1126 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1128 ddir_str(o->td_ddir));
1130 if (o->bs_is_seq_rand)
1131 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1134 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1135 c1, c2, c3, c4, c5, c6);
1137 log_info("ioengine=%s, iodepth=%u\n",
1138 td->io_ops->name, o->iodepth);
1147 } else if (job_add_num == 1)
1152 * recurse add identical jobs, clear numjobs and stonewall options
1153 * as they don't apply to sub-jobs
1155 numjobs = o->numjobs;
1157 struct thread_data *td_new = get_new_job(0, td, 1);
1162 td_new->o.numjobs = 1;
1163 td_new->o.stonewall = 0;
1164 td_new->o.new_group = 0;
1167 if (td_new->files) {
1169 for_each_file(td_new, f, i) {
1171 sfree(f->file_name);
1174 free(td_new->files);
1175 td_new->files = NULL;
1177 td_new->files_index = 0;
1178 td_new->files_size = 0;
1179 if (td_new->o.filename) {
1180 free(td_new->o.filename);
1181 td_new->o.filename = NULL;
1185 if (add_job(td_new, jobname, numjobs, 1, client_type))
1196 * Parse as if 'o' was a command line
1198 void add_job_opts(const char **o, int client_type)
1200 struct thread_data *td, *td_parent;
1201 int i, in_global = 1;
1205 td_parent = td = NULL;
1207 if (!strncmp(o[i], "name", 4)) {
1210 add_job(td, jobname, 0, 0, client_type);
1212 sprintf(jobname, "%s", o[i] + 5);
1214 if (in_global && !td_parent)
1215 td_parent = get_new_job(1, &def_thread, 0);
1216 else if (!in_global && !td) {
1218 td_parent = &def_thread;
1219 td = get_new_job(0, td_parent, 0);
1222 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
1224 fio_options_parse(td, (char **) &o[i], 1, 0);
1229 add_job(td, jobname, 0, 0, client_type);
1232 static int skip_this_section(const char *name)
1236 if (!nr_job_sections)
1238 if (!strncmp(name, "global", 6))
1241 for (i = 0; i < nr_job_sections; i++)
1242 if (!strcmp(job_sections[i], name))
1248 static int is_empty_or_comment(char *line)
1252 for (i = 0; i < strlen(line); i++) {
1257 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1265 * This is our [ini] type file parser.
1267 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1269 unsigned int global;
1270 struct thread_data *td;
1271 char *string, *name;
1274 int ret = 0, stonewall;
1277 int inside_skip = 0;
1279 int i, alloc_opts, num_opts;
1284 if (!strcmp(file, "-"))
1287 f = fopen(file, "r");
1290 perror("fopen job file");
1295 string = malloc(4096);
1298 * it's really 256 + small bit, 280 should suffice
1301 memset(name, 0, 280);
1304 opts = malloc(sizeof(char *) * alloc_opts);
1307 stonewall = stonewall_flag;
1310 * if skip_fgets is set, we already have loaded a line we
1315 p = strsep(&file, "\n");
1317 p = fgets(string, 4096, f);
1323 strip_blank_front(&p);
1326 if (is_empty_or_comment(p))
1328 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1331 log_err("fio: option <%s> outside of [] job section\n",
1336 name[strlen(name) - 1] = '\0';
1338 if (skip_this_section(name)) {
1344 global = !strncmp(name, "global", 6);
1350 log_info("--name=%s ", name);
1354 td = get_new_job(global, &def_thread, 0);
1361 * Separate multiple job files by a stonewall
1363 if (!global && stonewall) {
1364 td->o.stonewall = stonewall;
1369 memset(opts, 0, alloc_opts * sizeof(char *));
1373 p = strsep(&file, "\n");
1375 p = fgets(string, 4096, f);
1379 if (is_empty_or_comment(p))
1382 strip_blank_front(&p);
1385 * new section, break out and make sure we don't
1386 * fgets() a new line at the top.
1395 if (num_opts == alloc_opts) {
1397 opts = realloc(opts,
1398 alloc_opts * sizeof(char *));
1401 opts[num_opts] = strdup(p);
1405 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1407 ret = add_job(td, name, 0, 0, type);
1409 log_err("fio: job %s dropped\n", name);
1413 for (i = 0; i < num_opts; i++)
1422 while (i < nr_job_sections) {
1423 free(job_sections[i]);
1427 for (i = 0; i < num_opts; i++)
1433 if (!is_buf && f != stdin)
1438 static int fill_def_thread(void)
1440 memset(&def_thread, 0, sizeof(def_thread));
1442 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1443 def_thread.o.timeout = def_timeout;
1444 def_thread.o.error_dump = 1;
1446 * fill default options
1448 fio_fill_default_options(&def_thread);
1452 static void usage(const char *name)
1454 printf("%s\n", fio_version_string);
1455 printf("%s [options] [job options] <job file(s)>\n", name);
1456 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1457 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1458 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
1459 printf(" --parse-only\t\tParse options only, don't start any IO\n");
1460 printf(" --output\t\tWrite output to file\n");
1461 printf(" --runtime\t\tRuntime in seconds\n");
1462 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1463 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1464 printf(" --minimal\t\tMinimal (terse) output\n");
1465 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
1466 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
1467 printf(" --version\t\tPrint version info and exit\n");
1468 printf(" --help\t\tPrint this page\n");
1469 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
1470 printf(" --crctest\t\tTest speed of checksum functions\n");
1471 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
1473 printf(" --enghelp=engine\tPrint ioengine help, or list"
1474 " available ioengines\n");
1475 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1477 printf(" --showcmd\t\tTurn a job file into command line options\n");
1478 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1479 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
1480 printf(" --eta-newline=time\tForce a new line for every 'time'");
1481 printf(" period passed\n");
1482 printf(" --status-interval=t\tForce full status dump every");
1483 printf(" 't' period passed\n");
1484 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
1486 printf(" --section=name\tOnly run specified section in job file\n");
1487 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
1489 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1490 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1491 printf(" --server=args\t\tStart a backend fio server\n");
1492 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1493 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
1494 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1495 "\t\t\t(option=system,percpu) or run unit work\n"
1496 "\t\t\tcalibration only (option=calibrate)\n");
1497 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1498 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1499 printf("\n Jens Axboe <axboe@fb.com>\n");
1502 #ifdef FIO_INC_DEBUG
1503 struct debug_level debug_levels[] = {
1504 { .name = "process",
1505 .help = "Process creation/exit logging",
1506 .shift = FD_PROCESS,
1509 .help = "File related action logging",
1513 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1517 .help = "Memory allocation/freeing logging",
1520 { .name = "blktrace",
1521 .help = "blktrace action logging",
1522 .shift = FD_BLKTRACE,
1525 .help = "IO verification action logging",
1529 .help = "Random generation logging",
1533 .help = "Parser logging",
1536 { .name = "diskutil",
1537 .help = "Disk utility logging actions",
1538 .shift = FD_DISKUTIL,
1541 .help = "Logging related to creating/destroying jobs",
1545 .help = "Mutex logging",
1548 { .name = "profile",
1549 .help = "Logging related to profiles",
1550 .shift = FD_PROFILE,
1553 .help = "Logging related to time keeping functions",
1557 .help = "Network logging",
1561 .help = "Rate logging",
1567 static int set_debug(const char *string)
1569 struct debug_level *dl;
1570 char *p = (char *) string;
1574 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1575 log_info("fio: dumping debug options:");
1576 for (i = 0; debug_levels[i].name; i++) {
1577 dl = &debug_levels[i];
1578 log_info("%s,", dl->name);
1584 while ((opt = strsep(&p, ",")) != NULL) {
1587 if (!strncmp(opt, "all", 3)) {
1588 log_info("fio: set all debug options\n");
1593 for (i = 0; debug_levels[i].name; i++) {
1594 dl = &debug_levels[i];
1595 found = !strncmp(opt, dl->name, strlen(dl->name));
1599 if (dl->shift == FD_JOB) {
1600 opt = strchr(opt, ':');
1602 log_err("fio: missing job number\n");
1606 fio_debug_jobno = atoi(opt);
1607 log_info("fio: set debug jobno %d\n",
1610 log_info("fio: set debug option %s\n", opt);
1611 fio_debug |= (1UL << dl->shift);
1617 log_err("fio: debug mask %s not found\n", opt);
1622 static int set_debug(const char *string)
1624 log_err("fio: debug tracing not included in build\n");
1629 static void fio_options_fill_optstring(void)
1631 char *ostr = cmd_optstr;
1635 while (l_opts[i].name) {
1636 ostr[c++] = l_opts[i].val;
1637 if (l_opts[i].has_arg == required_argument)
1639 else if (l_opts[i].has_arg == optional_argument) {
1648 static int client_flag_set(char c)
1653 while (l_opts[i].name) {
1654 int val = l_opts[i].val;
1656 if (c == (val & 0xff))
1657 return (val & FIO_CLIENT_FLAG);
1665 static void parse_cmd_client(void *client, char *opt)
1667 fio_client_add_cmd_option(client, opt);
1670 int parse_cmd_line(int argc, char *argv[], int client_type)
1672 struct thread_data *td = NULL;
1673 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1674 char *ostr = cmd_optstr;
1675 void *pid_file = NULL;
1676 void *cur_client = NULL;
1680 * Reset optind handling, since we may call this multiple times
1685 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1688 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
1689 parse_cmd_client(cur_client, argv[optind - 1]);
1690 c &= ~FIO_CLIENT_FLAG;
1695 smalloc_pool_size = atoi(optarg);
1698 def_timeout = atoi(optarg);
1707 f_out = fopen(optarg, "w+");
1709 perror("fopen output");
1715 output_format = FIO_OUTPUT_TERSE;
1719 log_err("fio: missing --output-format argument\n");
1724 if (!strcmp(optarg, "minimal") ||
1725 !strcmp(optarg, "terse") ||
1726 !strcmp(optarg, "csv"))
1727 output_format = FIO_OUTPUT_TERSE;
1728 else if (!strcmp(optarg, "json"))
1729 output_format = FIO_OUTPUT_JSON;
1731 output_format = FIO_OUTPUT_NORMAL;
1734 append_terse_output = 1;
1744 fio_show_option_help(optarg);
1750 fio_show_ioengine_help(optarg);
1762 log_info("%s\n", fio_version_string);
1767 terse_version = atoi(optarg);
1768 if (!(terse_version == 2 || terse_version == 3 ||
1769 terse_version == 4)) {
1770 log_err("fio: bad terse version format\n");
1776 if (!strcmp("always", optarg))
1777 eta_print = FIO_ETA_ALWAYS;
1778 else if (!strcmp("never", optarg))
1779 eta_print = FIO_ETA_NEVER;
1784 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
1785 log_err("fio: failed parsing eta time %s\n", optarg);
1793 if (set_debug(optarg))
1802 if (!strcmp(optarg, "global")) {
1803 log_err("fio: can't use global as only "
1809 new_size = (nr_job_sections + 1) * sizeof(char *);
1810 job_sections = realloc(job_sections, new_size);
1811 job_sections[nr_job_sections] = strdup(optarg);
1816 exec_profile = strdup(optarg);
1818 case FIO_GETOPT_JOB: {
1819 const char *opt = l_opts[lidx].name;
1822 if (!strncmp(opt, "name", 4) && td) {
1823 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
1829 int is_section = !strncmp(opt, "name", 4);
1832 if (!is_section || !strncmp(val, "global", 6))
1835 if (is_section && skip_this_section(val))
1838 td = get_new_job(global, &def_thread, 1);
1839 if (!td || ioengine_load(td))
1841 fio_options_set_ioengine_opts(l_opts, td);
1844 if ((!val || !strlen(val)) &&
1845 l_opts[lidx].has_arg == required_argument) {
1846 log_err("fio: option %s requires an argument\n", opt);
1849 ret = fio_cmd_option_parse(td, opt, val);
1859 if (!ret && !strcmp(opt, "ioengine")) {
1861 if (ioengine_load(td))
1863 fio_options_set_ioengine_opts(l_opts, td);
1867 case FIO_GETOPT_IOENGINE: {
1868 const char *opt = l_opts[lidx].name;
1870 ret = fio_cmd_ioengine_option_parse(td, opt, val);
1877 max_jobs = atoi(optarg);
1878 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1879 log_err("fio: invalid max jobs: %d\n", max_jobs);
1886 log_err("fio: can't be both client and server\n");
1892 fio_server_set_arg(optarg);
1897 pid_file = strdup(optarg);
1900 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1901 /* exit on error and calibration only */
1909 log_err("fio: can't be both client and server\n");
1914 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
1915 log_err("fio: failed adding client %s\n", optarg);
1921 * If the next argument exists and isn't an option,
1922 * assume it's a job file for this client only.
1924 while (optind < argc) {
1925 if (!strncmp(argv[optind], "--", 2) ||
1926 !strncmp(argv[optind], "-", 1))
1929 fio_client_add_ini_file(cur_client, argv[optind]);
1935 exit_val = fio_monotonic_clocktest();
1939 exit_val = fio_crctest(optarg);
1944 if (check_str_time(optarg, &val, 0)) {
1945 log_err("fio: failed parsing time %s\n", optarg);
1950 status_interval = val * 1000;
1954 log_err("%s: unrecognized option '%s'\n", argv[0],
1965 if (do_exit && !(is_backend || nr_clients))
1968 if (nr_clients && fio_clients_connect())
1971 if (is_backend && backend)
1972 return fio_start_server(pid_file);
1976 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
1979 while (!ret && optind < argc) {
1981 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1982 ini_file[ini_idx - 1] = strdup(argv[optind]);
1989 int fio_init_options(void)
1994 fio_options_fill_optstring();
1995 fio_options_dup_and_init(l_opts);
1999 if (fill_def_thread())
2005 extern int fio_check_options(struct thread_options *);
2007 int parse_options(int argc, char *argv[])
2009 const int type = FIO_CLIENT_TYPE_CLI;
2012 if (fio_init_options())
2014 if (fio_test_cconv(&def_thread.o))
2015 log_err("fio: failed internal cconv test\n");
2017 job_files = parse_cmd_line(argc, argv, type);
2019 if (job_files > 0) {
2020 for (i = 0; i < job_files; i++) {
2021 if (fill_def_thread())
2024 if (fio_clients_send_ini(ini_file[i]))
2027 } else if (!is_backend) {
2028 if (parse_jobs_ini(ini_file[i], 0, i, type))
2033 } else if (nr_clients) {
2034 if (fill_def_thread())
2036 if (fio_clients_send_ini(NULL))
2041 fio_options_free(&def_thread);
2042 filesetup_mem_free();
2044 if (!thread_number) {
2049 if (is_backend || nr_clients)
2054 log_err("No jobs(s) defined\n\n");
2064 if (def_thread.o.gtod_offload) {
2066 fio_gtod_offload = 1;
2067 fio_gtod_cpu = def_thread.o.gtod_cpu;
2070 if (output_format == FIO_OUTPUT_NORMAL)
2071 log_info("%s\n", fio_version_string);
2076 void options_default_fill(struct thread_options *o)
2078 memcpy(o, &def_thread.o, sizeof(*o));