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"
31 const char fio_version_string[] = FIO_VERSION;
33 #define FIO_RANDSEED (0xb1899bedUL)
35 static char **ini_file;
36 static int max_jobs = FIO_MAX_JOBS;
37 static int dump_cmdline;
38 static int def_timeout;
39 static int parse_only;
41 static struct thread_data def_thread;
42 struct thread_data *threads = NULL;
44 int exitall_on_terminate = 0;
45 int output_format = FIO_OUTPUT_NORMAL;
46 int append_terse_output = 0;
47 int eta_print = FIO_ETA_AUTO;
51 char **job_sections = NULL;
52 int nr_job_sections = 0;
53 char *exec_profile = NULL;
54 int warnings_fatal = 0;
55 int terse_version = 3;
62 int status_interval = 0;
64 static int write_lat_log;
66 static int prev_group_jobs;
68 unsigned long fio_debug = 0;
69 unsigned int fio_debug_jobno = -1;
70 unsigned int *fio_debug_jobp = NULL;
72 static char cmd_optstr[256];
75 #define FIO_CLIENT_FLAG (1 << 16)
78 * Command line options. These will contain the above, plus a few
79 * extra that only pertain to fio itself and not jobs.
81 static struct option l_opts[FIO_NR_OPTIONS] = {
83 .name = (char *) "output",
84 .has_arg = required_argument,
85 .val = 'o' | FIO_CLIENT_FLAG,
88 .name = (char *) "timeout",
89 .has_arg = required_argument,
90 .val = 't' | FIO_CLIENT_FLAG,
93 .name = (char *) "latency-log",
94 .has_arg = required_argument,
95 .val = 'l' | FIO_CLIENT_FLAG,
98 .name = (char *) "bandwidth-log",
99 .has_arg = required_argument,
100 .val = 'b' | FIO_CLIENT_FLAG,
103 .name = (char *) "minimal",
104 .has_arg = no_argument,
105 .val = 'm' | FIO_CLIENT_FLAG,
108 .name = (char *) "output-format",
109 .has_arg = optional_argument,
110 .val = 'F' | FIO_CLIENT_FLAG,
113 .name = (char *) "append-terse",
114 .has_arg = optional_argument,
118 .name = (char *) "version",
119 .has_arg = no_argument,
120 .val = 'v' | FIO_CLIENT_FLAG,
123 .name = (char *) "help",
124 .has_arg = no_argument,
125 .val = 'h' | FIO_CLIENT_FLAG,
128 .name = (char *) "cmdhelp",
129 .has_arg = optional_argument,
130 .val = 'c' | FIO_CLIENT_FLAG,
133 .name = (char *) "enghelp",
134 .has_arg = optional_argument,
135 .val = 'i' | FIO_CLIENT_FLAG,
138 .name = (char *) "showcmd",
139 .has_arg = no_argument,
140 .val = 's' | FIO_CLIENT_FLAG,
143 .name = (char *) "readonly",
144 .has_arg = no_argument,
145 .val = 'r' | FIO_CLIENT_FLAG,
148 .name = (char *) "eta",
149 .has_arg = required_argument,
150 .val = 'e' | FIO_CLIENT_FLAG,
153 .name = (char *) "eta-newline",
154 .has_arg = required_argument,
155 .val = 'E' | FIO_CLIENT_FLAG,
158 .name = (char *) "debug",
159 .has_arg = required_argument,
160 .val = 'd' | FIO_CLIENT_FLAG,
163 .name = (char *) "parse-only",
164 .has_arg = no_argument,
165 .val = 'P' | FIO_CLIENT_FLAG,
168 .name = (char *) "section",
169 .has_arg = required_argument,
170 .val = 'x' | FIO_CLIENT_FLAG,
173 .name = (char *) "alloc-size",
174 .has_arg = required_argument,
175 .val = 'a' | FIO_CLIENT_FLAG,
178 .name = (char *) "profile",
179 .has_arg = required_argument,
180 .val = 'p' | FIO_CLIENT_FLAG,
183 .name = (char *) "warnings-fatal",
184 .has_arg = no_argument,
185 .val = 'w' | FIO_CLIENT_FLAG,
188 .name = (char *) "max-jobs",
189 .has_arg = required_argument,
190 .val = 'j' | FIO_CLIENT_FLAG,
193 .name = (char *) "terse-version",
194 .has_arg = required_argument,
195 .val = 'V' | FIO_CLIENT_FLAG,
198 .name = (char *) "server",
199 .has_arg = optional_argument,
202 { .name = (char *) "daemonize",
203 .has_arg = required_argument,
207 .name = (char *) "client",
208 .has_arg = required_argument,
212 .name = (char *) "cpuclock-test",
213 .has_arg = no_argument,
217 .name = (char *) "crctest",
218 .has_arg = optional_argument,
222 .name = (char *) "idle-prof",
223 .has_arg = required_argument,
227 .name = (char *) "status-interval",
228 .has_arg = required_argument,
236 void free_threads_shm(void)
238 struct shmid_ds sbuf;
245 shmctl(shm_id, IPC_RMID, &sbuf);
255 fio_debug_jobp = NULL;
259 options_free(fio_options, &def_thread);
264 * The thread area is shared between the main process and the job
265 * threads/processes. So setup a shared memory segment that will hold
266 * all the job info. We use the end of the region for keeping track of
267 * open files across jobs, for file sharing.
269 static int setup_thread_area(void)
277 * 1024 is too much on some machines, scale max_jobs if
278 * we get a failure that looks like too large a shm segment
281 size_t size = max_jobs * sizeof(struct thread_data);
283 size += file_hash_size;
284 size += sizeof(unsigned int);
286 shm_id = shmget(0, size, IPC_CREAT | 0600);
289 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
300 threads = shmat(shm_id, NULL, 0);
301 if (threads == (void *) -1) {
306 memset(threads, 0, max_jobs * sizeof(struct thread_data));
307 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
308 fio_debug_jobp = (void *) hash + file_hash_size;
309 *fio_debug_jobp = -1;
310 file_hash_init(hash);
318 * Return a free job structure.
320 static struct thread_data *get_new_job(int global, struct thread_data *parent,
323 struct thread_data *td;
327 if (setup_thread_area()) {
328 log_err("error: failed to setup shm segment\n");
331 if (thread_number >= max_jobs) {
332 log_err("error: maximum number of jobs (%d) reached.\n",
337 td = &threads[thread_number++];
344 td->o.uid = td->o.gid = -1U;
346 dup_files(td, parent);
347 fio_options_mem_dupe(td);
349 profile_add_hooks(td);
351 td->thread_number = thread_number;
353 if (!parent || !parent->o.group_reporting)
359 static void put_job(struct thread_data *td)
361 if (td == &def_thread)
368 log_info("fio: %s\n", td->verror);
370 fio_options_free(td);
374 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
378 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
380 unsigned int bs = td->o.min_bs[ddir];
382 assert(ddir_rw(ddir));
384 if (td->o.rate[ddir])
385 td->rate_bps[ddir] = td->o.rate[ddir];
387 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
389 if (!td->rate_bps[ddir]) {
390 log_err("rate lower than supported\n");
394 td->rate_pending_usleep[ddir] = 0;
398 static int setup_rate(struct thread_data *td)
402 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
403 ret = __setup_rate(td, DDIR_READ);
404 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
405 ret |= __setup_rate(td, DDIR_WRITE);
406 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
407 ret |= __setup_rate(td, DDIR_TRIM);
412 static int fixed_block_size(struct thread_options *o)
414 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
415 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
416 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
417 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
418 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
422 static unsigned long long get_rand_start_delay(struct thread_data *td)
424 unsigned long long delayrange;
427 delayrange = td->o.start_delay_high - td->o.start_delay;
429 if (td->o.use_os_rand) {
430 r = os_random_long(&td->delay_state);
431 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
433 r = __rand(&td->__delay_state);
434 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
437 delayrange += td->o.start_delay;
442 * Lazy way of fixing up options that depend on each other. We could also
443 * define option callback handlers, but this is easier.
445 static int fixup_options(struct thread_data *td)
447 struct thread_options *o = &td->o;
450 #ifndef FIO_HAVE_PSHARED_MUTEX
451 if (!o->use_thread) {
452 log_info("fio: this platform does not support process shared"
453 " mutexes, forcing use of threads. Use the 'thread'"
454 " option to get rid of this warning.\n");
456 ret = warnings_fatal;
460 if (o->write_iolog_file && o->read_iolog_file) {
461 log_err("fio: read iolog overrides write_iolog\n");
462 free(o->write_iolog_file);
463 o->write_iolog_file = NULL;
464 ret = warnings_fatal;
468 * only really works with 1 file
470 if (o->zone_size && o->open_files > 1)
474 * If zone_range isn't specified, backward compatibility dictates it
475 * should be made equal to zone_size.
477 if (o->zone_size && !o->zone_range)
478 o->zone_range = o->zone_size;
481 * Reads can do overwrites, we always need to pre-create the file
483 if (td_read(td) || td_rw(td))
486 if (!o->min_bs[DDIR_READ])
487 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
488 if (!o->max_bs[DDIR_READ])
489 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
490 if (!o->min_bs[DDIR_WRITE])
491 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
492 if (!o->max_bs[DDIR_WRITE])
493 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
494 if (!o->min_bs[DDIR_TRIM])
495 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
496 if (!o->max_bs[DDIR_TRIM])
497 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
500 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
501 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
504 * For random IO, allow blockalign offset other than min_bs.
506 if (!o->ba[DDIR_READ] || !td_random(td))
507 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
508 if (!o->ba[DDIR_WRITE] || !td_random(td))
509 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
510 if (!o->ba[DDIR_TRIM] || !td_random(td))
511 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
513 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
514 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
515 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
517 log_err("fio: Any use of blockalign= turns off randommap\n");
519 ret = warnings_fatal;
522 if (!o->file_size_high)
523 o->file_size_high = o->file_size_low;
525 if (o->start_delay_high)
526 o->start_delay = get_rand_start_delay(td);
528 if (o->norandommap && o->verify != VERIFY_NONE
529 && !fixed_block_size(o)) {
530 log_err("fio: norandommap given for variable block sizes, "
531 "verify disabled\n");
532 o->verify = VERIFY_NONE;
533 ret = warnings_fatal;
535 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
536 log_err("fio: bs_unaligned may not work with raw io\n");
539 * thinktime_spin must be less than thinktime
541 if (o->thinktime_spin > o->thinktime)
542 o->thinktime_spin = o->thinktime;
545 * The low water mark cannot be bigger than the iodepth
547 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
548 o->iodepth_low = o->iodepth;
551 * If batch number isn't set, default to the same as iodepth
553 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
554 o->iodepth_batch = o->iodepth;
556 if (o->nr_files > td->files_index)
557 o->nr_files = td->files_index;
559 if (o->open_files > o->nr_files || !o->open_files)
560 o->open_files = o->nr_files;
562 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
563 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
564 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
565 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
566 log_err("fio: rate and rate_iops are mutually exclusive\n");
569 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
570 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
571 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
572 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
573 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
574 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
575 log_err("fio: minimum rate exceeds rate\n");
579 if (!o->timeout && o->time_based) {
580 log_err("fio: time_based requires a runtime/timeout setting\n");
582 ret = warnings_fatal;
585 if (o->fill_device && !o->size)
588 if (o->verify != VERIFY_NONE) {
589 if (td_write(td) && o->do_verify && o->numjobs > 1) {
590 log_info("Multiple writers may overwrite blocks that "
591 "belong to other jobs. This can cause "
592 "verification failures.\n");
593 ret = warnings_fatal;
596 o->refill_buffers = 1;
597 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
599 o->verify_interval = o->min_bs[DDIR_WRITE];
603 o->invalidate_cache = 0;
604 if (td->io_ops->flags & FIO_PIPEIO) {
605 log_info("fio: cannot pre-read files with an IO engine"
606 " that isn't seekable. Pre-read disabled.\n");
607 ret = warnings_fatal;
612 if (td->io_ops->flags & FIO_BIT_BASED)
618 #ifndef CONFIG_FDATASYNC
619 if (o->fdatasync_blocks) {
620 log_info("fio: this platform does not support fdatasync()"
621 " falling back to using fsync(). Use the 'fsync'"
622 " option instead of 'fdatasync' to get rid of"
624 o->fsync_blocks = o->fdatasync_blocks;
625 o->fdatasync_blocks = 0;
626 ret = warnings_fatal;
632 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
633 * so fail if we're passed those flags
635 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
636 log_err("fio: Windows does not support direct or non-buffered io with"
637 " the synchronous ioengines. Use the 'windowsaio' ioengine"
638 " with 'direct=1' and 'iodepth=1' instead.\n");
644 * For fully compressible data, just zero them at init time.
645 * It's faster than repeatedly filling it.
647 if (td->o.compress_percentage == 100) {
648 td->o.zero_buffers = 1;
649 td->o.compress_percentage = 0;
653 * Using a non-uniform random distribution excludes usage of
656 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
657 td->o.norandommap = 1;
660 * If size is set but less than the min block size, complain
662 if (o->size && o->size < td_min_bs(td)) {
663 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
668 * O_ATOMIC implies O_DIRECT
674 * If randseed is set, that overrides randrepeat
677 td->o.rand_repeatable = 0;
679 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
680 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
688 * This function leaks the buffer
690 char *fio_uint_to_kmg(unsigned int val)
692 char *buf = malloc(32);
693 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
704 snprintf(buf, 32, "%u%c", val, *p);
708 /* External engines are specified by "external:name.o") */
709 static const char *get_engine_name(const char *str)
711 char *p = strstr(str, ":");
717 strip_blank_front(&p);
722 static int exists_and_not_file(const char *filename)
726 if (lstat(filename, &sb) == -1)
729 /* \\.\ is the device namespace in Windows, where every file
730 * is a device node */
731 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
737 static void td_fill_rand_seeds_os(struct thread_data *td)
739 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
740 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
741 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
743 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
744 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
746 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
747 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
748 os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
753 if (td->o.rand_repeatable)
754 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
756 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
758 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
759 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
760 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], &td->seq_rand_state[DDIR_TRIM]);
763 static void td_fill_rand_seeds_internal(struct thread_data *td)
765 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
766 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
767 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
769 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
770 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
772 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
773 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
774 init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
779 if (td->o.rand_repeatable)
780 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
782 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
783 init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
784 init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
785 init_rand_seed(&td->__seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF]);
788 void td_fill_rand_seeds(struct thread_data *td)
790 if (td->o.allrand_repeatable) {
791 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
792 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
796 if (td->o.use_os_rand)
797 td_fill_rand_seeds_os(td);
799 td_fill_rand_seeds_internal(td);
801 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
805 * Initializes the ioengine configured for a job, if it has not been done so
808 int ioengine_load(struct thread_data *td)
813 * Engine has already been loaded.
817 if (!td->o.ioengine) {
818 log_err("fio: internal fault, no IO engine specified\n");
822 engine = get_engine_name(td->o.ioengine);
823 td->io_ops = load_ioengine(td, engine);
825 log_err("fio: failed to load engine %s\n", engine);
829 if (td->io_ops->option_struct_size && td->io_ops->options) {
831 * In cases where td->eo is set, clone it for a child thread.
832 * This requires that the parent thread has the same ioengine,
833 * but that requirement must be enforced by the code which
836 void *origeo = td->eo;
838 * Otherwise use the default thread options.
840 if (!origeo && td != &def_thread && def_thread.eo &&
841 def_thread.io_ops->options == td->io_ops->options)
842 origeo = def_thread.eo;
844 options_init(td->io_ops->options);
845 td->eo = malloc(td->io_ops->option_struct_size);
847 * Use the default thread as an option template if this uses the
848 * same options structure and there are non-default options
852 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
853 options_mem_dupe(td->eo, td->io_ops->options);
855 memset(td->eo, 0, td->io_ops->option_struct_size);
856 fill_default_options(td->eo, td->io_ops->options);
858 *(struct thread_data **)td->eo = td;
864 static void init_flags(struct thread_data *td)
866 struct thread_options *o = &td->o;
868 if (o->verify_backlog)
869 td->flags |= TD_F_VER_BACKLOG;
871 td->flags |= TD_F_TRIM_BACKLOG;
872 if (o->read_iolog_file)
873 td->flags |= TD_F_READ_IOLOG;
874 if (o->refill_buffers)
875 td->flags |= TD_F_REFILL_BUFFERS;
876 if (o->scramble_buffers)
877 td->flags |= TD_F_SCRAMBLE_BUFFERS;
878 if (o->verify != VERIFY_NONE)
879 td->flags |= TD_F_VER_NONE;
882 static int setup_random_seeds(struct thread_data *td)
887 if (!td->o.rand_repeatable && !td->o.rand_seed)
888 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
890 if (!td->o.rand_seed)
893 seed = td->o.rand_seed;
895 for (i = 0; i < 4; i++)
896 seed *= 0x9e370001UL;
898 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
899 td->rand_seeds[i] = seed;
900 seed *= 0x9e370001UL;
903 td_fill_rand_seeds(td);
914 static struct fpre_keyword {
918 } fpre_keywords[] = {
919 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
920 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
921 { .keyword = "$filenum", .key = FPRE_FILENUM, },
922 { .keyword = NULL, },
925 static char *make_filename(char *buf, struct thread_options *o,
926 const char *jobname, int jobnum, int filenum)
928 struct fpre_keyword *f;
931 if (!o->filename_format || !strlen(o->filename_format)) {
932 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
936 for (f = &fpre_keywords[0]; f->keyword; f++)
937 f->strlen = strlen(f->keyword);
939 strcpy(buf, o->filename_format);
940 memset(copy, 0, sizeof(copy));
941 for (f = &fpre_keywords[0]; f->keyword; f++) {
943 size_t pre_len, post_start = 0;
944 char *str, *dst = copy;
946 str = strcasestr(buf, f->keyword);
951 if (strlen(str) != f->strlen)
952 post_start = pre_len + f->strlen;
955 strncpy(dst, buf, pre_len);
961 dst += sprintf(dst, "%s", jobname);
964 dst += sprintf(dst, "%d", jobnum);
967 dst += sprintf(dst, "%d", filenum);
975 strcpy(dst, buf + post_start);
984 int parse_dryrun(void)
986 return dump_cmdline || parse_only;
990 * Adds a job to the list of things todo. Sanitizes the various options
991 * to make sure we don't have conflicts, and initializes various
994 static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
995 int recursed, int client_type)
998 char fname[PATH_MAX];
999 int numjobs, file_alloced;
1000 struct thread_options *o = &td->o;
1003 * the def_thread is just for options, it's not a real job
1005 if (td == &def_thread)
1011 * if we are just dumping the output command line, don't add the job
1013 if (parse_dryrun()) {
1018 td->client_type = client_type;
1020 if (profile_td_init(td))
1023 if (ioengine_load(td))
1027 td->io_ops->flags |= FIO_RAWIO;
1030 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1033 if (o->nr_files == 1 && exists_and_not_file(jobname))
1034 add_file(td, jobname, job_add_num, 0);
1036 for (i = 0; i < o->nr_files; i++)
1037 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
1041 if (fixup_options(td))
1047 * IO engines only need this for option callbacks, and the address may
1048 * change in subprocesses.
1051 *(struct thread_data **)td->eo = NULL;
1053 if (td->io_ops->flags & FIO_DISKLESSIO) {
1056 for_each_file(td, f, i)
1057 f->real_file_size = -1ULL;
1060 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1062 td->ts.clat_percentiles = o->clat_percentiles;
1063 td->ts.percentile_precision = o->percentile_precision;
1064 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1066 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1067 td->ts.clat_stat[i].min_val = ULONG_MAX;
1068 td->ts.slat_stat[i].min_val = ULONG_MAX;
1069 td->ts.lat_stat[i].min_val = ULONG_MAX;
1070 td->ts.bw_stat[i].min_val = ULONG_MAX;
1072 td->ddir_seq_nr = o->ddir_seq_nr;
1074 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1075 prev_group_jobs = 0;
1079 td->groupid = groupid;
1082 if (setup_random_seeds(td)) {
1083 td_verror(td, errno, "init_random_state");
1090 if (o->lat_log_file) {
1091 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1092 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1093 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
1096 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1097 if (o->iops_log_file)
1098 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
1101 o->name = strdup(jobname);
1103 if (output_format == FIO_OUTPUT_NORMAL) {
1105 if (is_backend && !recursed)
1106 fio_server_send_add_job(td);
1108 if (!(td->io_ops->flags & FIO_NOIO)) {
1109 char *c1, *c2, *c3, *c4;
1110 char *c5 = NULL, *c6 = NULL;
1112 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1113 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1114 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1115 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1117 if (!o->bs_is_seq_rand) {
1118 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1119 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1122 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1124 ddir_str(o->td_ddir));
1126 if (o->bs_is_seq_rand)
1127 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1130 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1131 c1, c2, c3, c4, c5, c6);
1133 log_info("ioengine=%s, iodepth=%u\n",
1134 td->io_ops->name, o->iodepth);
1143 } else if (job_add_num == 1)
1148 * recurse add identical jobs, clear numjobs and stonewall options
1149 * as they don't apply to sub-jobs
1151 numjobs = o->numjobs;
1153 struct thread_data *td_new = get_new_job(0, td, 1);
1158 td_new->o.numjobs = 1;
1159 td_new->o.stonewall = 0;
1160 td_new->o.new_group = 0;
1163 td_new->files_index = 0;
1164 td_new->files_size = 0;
1165 if (td_new->files) {
1167 for_each_file(td_new, f, i) {
1172 td_new->files = NULL;
1174 if (td_new->o.filename) {
1175 free(td_new->o.filename);
1176 td_new->o.filename = NULL;
1180 if (add_job(td_new, jobname, numjobs, 1, client_type))
1191 * Parse as if 'o' was a command line
1193 void add_job_opts(const char **o, int client_type)
1195 struct thread_data *td, *td_parent;
1196 int i, in_global = 1;
1200 td_parent = td = NULL;
1202 if (!strncmp(o[i], "name", 4)) {
1205 add_job(td, jobname, 0, 0, client_type);
1207 sprintf(jobname, "%s", o[i] + 5);
1209 if (in_global && !td_parent)
1210 td_parent = get_new_job(1, &def_thread, 0);
1211 else if (!in_global && !td) {
1213 td_parent = &def_thread;
1214 td = get_new_job(0, td_parent, 0);
1217 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
1219 fio_options_parse(td, (char **) &o[i], 1, 0);
1224 add_job(td, jobname, 0, 0, client_type);
1227 static int skip_this_section(const char *name)
1231 if (!nr_job_sections)
1233 if (!strncmp(name, "global", 6))
1236 for (i = 0; i < nr_job_sections; i++)
1237 if (!strcmp(job_sections[i], name))
1243 static int is_empty_or_comment(char *line)
1247 for (i = 0; i < strlen(line); i++) {
1252 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1260 * This is our [ini] type file parser.
1262 int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1264 unsigned int global;
1265 struct thread_data *td;
1266 char *string, *name;
1269 int ret = 0, stonewall;
1272 int inside_skip = 0;
1274 int i, alloc_opts, num_opts;
1279 if (!strcmp(file, "-"))
1282 f = fopen(file, "r");
1285 perror("fopen job file");
1290 string = malloc(4096);
1293 * it's really 256 + small bit, 280 should suffice
1296 memset(name, 0, 280);
1299 opts = malloc(sizeof(char *) * alloc_opts);
1302 stonewall = stonewall_flag;
1305 * if skip_fgets is set, we already have loaded a line we
1310 p = strsep(&file, "\n");
1312 p = fgets(string, 4096, f);
1318 strip_blank_front(&p);
1321 if (is_empty_or_comment(p))
1323 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1326 log_err("fio: option <%s> outside of [] job section\n",
1331 name[strlen(name) - 1] = '\0';
1333 if (skip_this_section(name)) {
1339 global = !strncmp(name, "global", 6);
1345 log_info("--name=%s ", name);
1349 td = get_new_job(global, &def_thread, 0);
1356 * Separate multiple job files by a stonewall
1358 if (!global && stonewall) {
1359 td->o.stonewall = stonewall;
1364 memset(opts, 0, alloc_opts * sizeof(char *));
1368 p = strsep(&file, "\n");
1370 p = fgets(string, 4096, f);
1374 if (is_empty_or_comment(p))
1377 strip_blank_front(&p);
1380 * new section, break out and make sure we don't
1381 * fgets() a new line at the top.
1390 if (num_opts == alloc_opts) {
1392 opts = realloc(opts,
1393 alloc_opts * sizeof(char *));
1396 opts[num_opts] = strdup(p);
1400 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1402 ret = add_job(td, name, 0, 0, type);
1404 log_err("fio: job %s dropped\n", name);
1408 for (i = 0; i < num_opts; i++)
1417 while (i < nr_job_sections) {
1418 free(job_sections[i]);
1422 for (i = 0; i < num_opts; i++)
1428 if (!is_buf && f != stdin)
1433 static int fill_def_thread(void)
1435 memset(&def_thread, 0, sizeof(def_thread));
1437 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1438 def_thread.o.timeout = def_timeout;
1439 def_thread.o.error_dump = 1;
1441 * fill default options
1443 fio_fill_default_options(&def_thread);
1447 static void usage(const char *name)
1449 printf("%s\n", fio_version_string);
1450 printf("%s [options] [job options] <job file(s)>\n", name);
1451 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1452 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1453 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
1454 printf(" --parse-only\t\tParse options only, don't start any IO\n");
1455 printf(" --output\t\tWrite output to file\n");
1456 printf(" --runtime\t\tRuntime in seconds\n");
1457 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1458 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1459 printf(" --minimal\t\tMinimal (terse) output\n");
1460 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
1461 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
1462 printf(" --version\t\tPrint version info and exit\n");
1463 printf(" --help\t\tPrint this page\n");
1464 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
1465 printf(" --crctest\t\tTest speed of checksum functions\n");
1466 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
1468 printf(" --enghelp=engine\tPrint ioengine help, or list"
1469 " available ioengines\n");
1470 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1472 printf(" --showcmd\t\tTurn a job file into command line options\n");
1473 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1474 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
1475 printf(" --eta-newline=time\tForce a new line for every 'time'");
1476 printf(" period passed\n");
1477 printf(" --status-interval=t\tForce full status dump every");
1478 printf(" 't' period passed\n");
1479 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
1481 printf(" --section=name\tOnly run specified section in job file\n");
1482 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
1484 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1485 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1486 printf(" --server=args\t\tStart a backend fio server\n");
1487 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1488 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
1489 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1490 "\t\t\t(option=system,percpu) or run unit work\n"
1491 "\t\t\tcalibration only (option=calibrate)\n");
1492 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1493 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1494 printf("\n Jens Axboe <axboe@fb.com>\n");
1497 #ifdef FIO_INC_DEBUG
1498 struct debug_level debug_levels[] = {
1499 { .name = "process",
1500 .help = "Process creation/exit logging",
1501 .shift = FD_PROCESS,
1504 .help = "File related action logging",
1508 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1512 .help = "Memory allocation/freeing logging",
1515 { .name = "blktrace",
1516 .help = "blktrace action logging",
1517 .shift = FD_BLKTRACE,
1520 .help = "IO verification action logging",
1524 .help = "Random generation logging",
1528 .help = "Parser logging",
1531 { .name = "diskutil",
1532 .help = "Disk utility logging actions",
1533 .shift = FD_DISKUTIL,
1536 .help = "Logging related to creating/destroying jobs",
1540 .help = "Mutex logging",
1543 { .name = "profile",
1544 .help = "Logging related to profiles",
1545 .shift = FD_PROFILE,
1548 .help = "Logging related to time keeping functions",
1552 .help = "Network logging",
1556 .help = "Rate logging",
1562 static int set_debug(const char *string)
1564 struct debug_level *dl;
1565 char *p = (char *) string;
1569 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1570 log_info("fio: dumping debug options:");
1571 for (i = 0; debug_levels[i].name; i++) {
1572 dl = &debug_levels[i];
1573 log_info("%s,", dl->name);
1579 while ((opt = strsep(&p, ",")) != NULL) {
1582 if (!strncmp(opt, "all", 3)) {
1583 log_info("fio: set all debug options\n");
1588 for (i = 0; debug_levels[i].name; i++) {
1589 dl = &debug_levels[i];
1590 found = !strncmp(opt, dl->name, strlen(dl->name));
1594 if (dl->shift == FD_JOB) {
1595 opt = strchr(opt, ':');
1597 log_err("fio: missing job number\n");
1601 fio_debug_jobno = atoi(opt);
1602 log_info("fio: set debug jobno %d\n",
1605 log_info("fio: set debug option %s\n", opt);
1606 fio_debug |= (1UL << dl->shift);
1612 log_err("fio: debug mask %s not found\n", opt);
1617 static int set_debug(const char *string)
1619 log_err("fio: debug tracing not included in build\n");
1624 static void fio_options_fill_optstring(void)
1626 char *ostr = cmd_optstr;
1630 while (l_opts[i].name) {
1631 ostr[c++] = l_opts[i].val;
1632 if (l_opts[i].has_arg == required_argument)
1634 else if (l_opts[i].has_arg == optional_argument) {
1643 static int client_flag_set(char c)
1648 while (l_opts[i].name) {
1649 int val = l_opts[i].val;
1651 if (c == (val & 0xff))
1652 return (val & FIO_CLIENT_FLAG);
1660 void parse_cmd_client(void *client, char *opt)
1662 fio_client_add_cmd_option(client, opt);
1665 extern int fio_crctest(const char *);
1667 int parse_cmd_line(int argc, char *argv[], int client_type)
1669 struct thread_data *td = NULL;
1670 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1671 char *ostr = cmd_optstr;
1672 void *pid_file = NULL;
1673 void *cur_client = NULL;
1677 * Reset optind handling, since we may call this multiple times
1682 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1685 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
1686 parse_cmd_client(cur_client, argv[optind - 1]);
1687 c &= ~FIO_CLIENT_FLAG;
1692 smalloc_pool_size = atoi(optarg);
1695 def_timeout = atoi(optarg);
1704 f_out = fopen(optarg, "w+");
1706 perror("fopen output");
1712 output_format = FIO_OUTPUT_TERSE;
1716 log_err("fio: missing --output-format argument\n");
1721 if (!strcmp(optarg, "minimal") ||
1722 !strcmp(optarg, "terse") ||
1723 !strcmp(optarg, "csv"))
1724 output_format = FIO_OUTPUT_TERSE;
1725 else if (!strcmp(optarg, "json"))
1726 output_format = FIO_OUTPUT_JSON;
1728 output_format = FIO_OUTPUT_NORMAL;
1731 append_terse_output = 1;
1741 fio_show_option_help(optarg);
1747 fio_show_ioengine_help(optarg);
1759 log_info("%s\n", fio_version_string);
1764 terse_version = atoi(optarg);
1765 if (!(terse_version == 2 || terse_version == 3 ||
1766 terse_version == 4)) {
1767 log_err("fio: bad terse version format\n");
1773 if (!strcmp("always", optarg))
1774 eta_print = FIO_ETA_ALWAYS;
1775 else if (!strcmp("never", optarg))
1776 eta_print = FIO_ETA_NEVER;
1781 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
1782 log_err("fio: failed parsing eta time %s\n", optarg);
1790 if (set_debug(optarg))
1799 if (!strcmp(optarg, "global")) {
1800 log_err("fio: can't use global as only "
1806 new_size = (nr_job_sections + 1) * sizeof(char *);
1807 job_sections = realloc(job_sections, new_size);
1808 job_sections[nr_job_sections] = strdup(optarg);
1813 exec_profile = strdup(optarg);
1815 case FIO_GETOPT_JOB: {
1816 const char *opt = l_opts[lidx].name;
1819 if (!strncmp(opt, "name", 4) && td) {
1820 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
1826 int is_section = !strncmp(opt, "name", 4);
1829 if (!is_section || !strncmp(val, "global", 6))
1832 if (is_section && skip_this_section(val))
1835 td = get_new_job(global, &def_thread, 1);
1836 if (!td || ioengine_load(td))
1838 fio_options_set_ioengine_opts(l_opts, td);
1841 if ((!val || !strlen(val)) &&
1842 l_opts[lidx].has_arg == required_argument) {
1843 log_err("fio: option %s requires an argument\n", opt);
1846 ret = fio_cmd_option_parse(td, opt, val);
1856 if (!ret && !strcmp(opt, "ioengine")) {
1858 if (ioengine_load(td))
1860 fio_options_set_ioengine_opts(l_opts, td);
1864 case FIO_GETOPT_IOENGINE: {
1865 const char *opt = l_opts[lidx].name;
1867 ret = fio_cmd_ioengine_option_parse(td, opt, val);
1874 max_jobs = atoi(optarg);
1875 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1876 log_err("fio: invalid max jobs: %d\n", max_jobs);
1883 log_err("fio: can't be both client and server\n");
1889 fio_server_set_arg(optarg);
1894 pid_file = strdup(optarg);
1897 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1898 /* exit on error and calibration only */
1906 log_err("fio: can't be both client and server\n");
1911 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
1912 log_err("fio: failed adding client %s\n", optarg);
1918 * If the next argument exists and isn't an option,
1919 * assume it's a job file for this client only.
1921 while (optind < argc) {
1922 if (!strncmp(argv[optind], "--", 2) ||
1923 !strncmp(argv[optind], "-", 1))
1926 fio_client_add_ini_file(cur_client, argv[optind]);
1932 exit_val = fio_monotonic_clocktest();
1936 exit_val = fio_crctest(optarg);
1941 if (check_str_time(optarg, &val, 1)) {
1942 log_err("fio: failed parsing time %s\n", optarg);
1947 status_interval = val * 1000;
1951 log_err("%s: unrecognized option '%s'\n", argv[0],
1962 if (do_exit && !(is_backend || nr_clients))
1965 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));