2 * This file contains job initialization and setup functions.
14 #include <sys/types.h>
23 static char fio_version_string[] = "fio 1.36-rc1";
25 #define FIO_RANDSEED (0xb1899bedUL)
27 static char **ini_file;
28 static int max_jobs = MAX_JOBS;
29 static int dump_cmdline;
31 static struct thread_data def_thread;
32 struct thread_data *threads = NULL;
34 int exitall_on_terminate = 0;
37 unsigned long long mlock_size = 0;
40 char *job_section = NULL;
45 static int def_timeout;
46 static int write_lat_log;
48 static int prev_group_jobs;
50 unsigned long fio_debug = 0;
51 unsigned int fio_debug_jobno = -1;
52 unsigned int *fio_debug_jobp = NULL;
55 * Command line options. These will contain the above, plus a few
56 * extra that only pertain to fio itself and not jobs.
58 static struct option l_opts[FIO_NR_OPTIONS] = {
61 .has_arg = required_argument,
66 .has_arg = required_argument,
70 .name = "latency-log",
71 .has_arg = required_argument,
75 .name = "bandwidth-log",
76 .has_arg = required_argument,
81 .has_arg = optional_argument,
86 .has_arg = no_argument,
91 .has_arg = no_argument,
96 .has_arg = optional_argument,
101 .has_arg = no_argument,
106 .has_arg = no_argument,
111 .has_arg = required_argument,
116 .has_arg = required_argument,
121 .has_arg = required_argument,
125 .name = "alloc-size",
126 .has_arg = required_argument,
131 .has_arg = required_argument,
139 static const char *tiobench_opts[] = {
140 "buffered=0", "size=4*1024*$mb_memory", "bs=4k", "timeout=600",
141 "numjobs=4", "group_reporting", "thread", "overwrite=1",
142 "filename=.fio.tio.1:.fio.tio.2:.fio.tio.3:.fio.tio.4",
143 "name=seqwrite", "rw=write", "end_fsync=1",
144 "name=randwrite", "stonewall", "rw=randwrite", "end_fsync=1",
145 "name=seqread", "stonewall", "rw=read",
146 "name=randread", "stonewall", "rw=randread", NULL,
149 static const char **fio_prof_strings[PROFILE_END] = {
154 static const char *profiles[PROFILE_END] = {
170 * Return a free job structure.
172 static struct thread_data *get_new_job(int global, struct thread_data *parent)
174 struct thread_data *td;
178 if (thread_number >= max_jobs)
181 td = &threads[thread_number++];
184 td->o.uid = td->o.gid = -1U;
186 dup_files(td, parent);
187 options_mem_dupe(td);
189 td->thread_number = thread_number;
193 static void put_job(struct thread_data *td)
195 if (td == &def_thread)
199 log_info("fio: %s\n", td->verror);
201 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
205 static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
207 unsigned int bs = td->o.min_bs[ddir];
208 unsigned long long rate;
209 unsigned long ios_per_msec;
211 if (td->o.rate[ddir]) {
212 rate = td->o.rate[ddir];
213 ios_per_msec = (rate * 1000LL) / bs;
215 ios_per_msec = td->o.rate_iops[ddir] * 1000UL;
218 log_err("rate lower than supported\n");
222 td->rate_usec_cycle[ddir] = 1000000000ULL / ios_per_msec;
223 td->rate_pending_usleep[ddir] = 0;
227 static int setup_rate(struct thread_data *td)
231 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
232 ret = __setup_rate(td, DDIR_READ);
233 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
234 ret |= __setup_rate(td, DDIR_WRITE);
239 static int fixed_block_size(struct thread_options *o)
241 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
242 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
243 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
247 * Lazy way of fixing up options that depend on each other. We could also
248 * define option callback handlers, but this is easier.
250 static int fixup_options(struct thread_data *td)
252 struct thread_options *o = &td->o;
254 #ifndef FIO_HAVE_PSHARED_MUTEX
255 if (!td->o.use_thread) {
256 log_info("fio: this platform does not support process shared"
257 " mutexes, forcing use of threads. Use the 'thread'"
258 " option to get rid of this warning.\n");
259 td->o.use_thread = 1;
263 if (o->write_iolog_file && o->read_iolog_file) {
264 log_err("fio: read iolog overrides write_iolog\n");
265 free(o->write_iolog_file);
266 o->write_iolog_file = NULL;
270 * only really works for sequential io for now, and with 1 file
272 if (o->zone_size && td_random(td) && o->open_files == 1)
276 * Reads can do overwrites, we always need to pre-create the file
278 if (td_read(td) || td_rw(td))
281 if (!o->min_bs[DDIR_READ])
282 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
283 if (!o->max_bs[DDIR_READ])
284 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
285 if (!o->min_bs[DDIR_WRITE])
286 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
287 if (!o->max_bs[DDIR_WRITE])
288 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
290 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
293 * For random IO, allow blockalign offset other than min_bs.
295 if (!o->ba[DDIR_READ] || !td_random(td))
296 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
297 if (!o->ba[DDIR_WRITE] || !td_random(td))
298 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
300 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
301 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
302 !td->o.norandommap) {
303 log_err("fio: Any use of blockalign= turns off randommap\n");
304 td->o.norandommap = 1;
307 if (!o->file_size_high)
308 o->file_size_high = o->file_size_low;
310 if (o->norandommap && o->verify != VERIFY_NONE
311 && !fixed_block_size(o)) {
312 log_err("fio: norandommap given for variable block sizes, "
313 "verify disabled\n");
314 o->verify = VERIFY_NONE;
316 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
317 log_err("fio: bs_unaligned may not work with raw io\n");
320 * thinktime_spin must be less than thinktime
322 if (o->thinktime_spin > o->thinktime)
323 o->thinktime_spin = o->thinktime;
326 * The low water mark cannot be bigger than the iodepth
328 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
330 * syslet work around - if the workload is sequential,
331 * we want to let the queue drain all the way down to
332 * avoid seeking between async threads
334 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
337 o->iodepth_low = o->iodepth;
341 * If batch number isn't set, default to the same as iodepth
343 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
344 o->iodepth_batch = o->iodepth;
346 if (o->nr_files > td->files_index)
347 o->nr_files = td->files_index;
349 if (o->open_files > o->nr_files || !o->open_files)
350 o->open_files = o->nr_files;
352 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
353 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
354 o->rate_iops_min[1]))) {
355 log_err("fio: rate and rate_iops are mutually exclusive\n");
358 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
359 (o->rate_iops[0] < o->rate_iops_min[0]) ||
360 (o->rate_iops[1] < o->rate_iops_min[1])) {
361 log_err("fio: minimum rate exceeds rate\n");
365 if (!o->timeout && o->time_based) {
366 log_err("fio: time_based requires a runtime/timeout setting\n");
370 if (o->fill_device && !o->size)
373 if (td_rw(td) && td->o.verify != VERIFY_NONE)
374 log_info("fio: mixed read/write workload with verify. May not "
375 "work as expected, unless you pre-populated the file\n");
377 if (td->o.verify != VERIFY_NONE)
378 td->o.refill_buffers = 1;
380 if (td->o.pre_read) {
381 td->o.invalidate_cache = 0;
382 if (td->io_ops->flags & FIO_PIPEIO)
383 log_info("fio: cannot pre-read files with an IO engine"
384 " that isn't seekable. Pre-read disabled.\n");
391 * This function leaks the buffer
393 static char *to_kmg(unsigned int val)
395 char *buf = malloc(32);
396 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
407 snprintf(buf, 31, "%u%c", val, *p);
411 /* External engines are specified by "external:name.o") */
412 static const char *get_engine_name(const char *str)
414 char *p = strstr(str, ":");
420 strip_blank_front(&p);
425 static int exists_and_not_file(const char *filename)
429 if (lstat(filename, &sb) == -1)
432 if (S_ISREG(sb.st_mode))
438 void td_fill_rand_seeds(struct thread_data *td)
440 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
441 os_random_seed(td->rand_seeds[1], &td->verify_state);
442 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
444 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
445 os_random_seed(td->rand_seeds[3], &td->next_file_state);
447 os_random_seed(td->rand_seeds[5], &td->file_size_state);
452 if (td->o.rand_repeatable)
453 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
455 os_random_seed(td->rand_seeds[4], &td->random_state);
459 * Initialize the various random states we need (random io, block size ranges,
460 * read/write mix, etc).
462 static int init_random_state(struct thread_data *td)
466 fd = open("/dev/urandom", O_RDONLY);
468 td_verror(td, errno, "open");
472 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
473 (int) sizeof(td->rand_seeds)) {
474 td_verror(td, EIO, "read");
480 td_fill_rand_seeds(td);
485 * Adds a job to the list of things todo. Sanitizes the various options
486 * to make sure we don't have conflicts, and initializes various
489 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
491 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
492 "randread", "randwrite", "randrw" };
495 char fname[PATH_MAX];
496 int numjobs, file_alloced;
499 * the def_thread is just for options, it's not a real job
501 if (td == &def_thread)
505 * if we are just dumping the output command line, don't add the job
512 engine = get_engine_name(td->o.ioengine);
513 td->io_ops = load_ioengine(td, engine);
515 log_err("fio: failed to load engine %s\n", engine);
519 if (td->o.use_thread)
525 td->io_ops->flags |= FIO_RAWIO;
528 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
531 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
532 add_file(td, jobname);
534 for (i = 0; i < td->o.nr_files; i++) {
535 sprintf(fname, "%s.%d.%d", jobname,
536 td->thread_number, i);
542 if (fixup_options(td))
545 if (td->io_ops->flags & FIO_DISKLESSIO) {
548 for_each_file(td, f, i)
549 f->real_file_size = -1ULL;
552 td->mutex = fio_mutex_init(0);
554 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
555 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
556 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
557 td->ddir_nr = td->o.ddir_nr;
559 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
564 td->groupid = groupid;
567 if (init_random_state(td))
573 if (td->o.write_lat_log) {
574 setup_log(&td->ts.slat_log);
575 setup_log(&td->ts.clat_log);
577 if (td->o.write_bw_log)
578 setup_log(&td->ts.bw_log);
581 td->o.name = strdup(jobname);
585 if (!strcmp(td->io_ops->name, "cpuio")) {
586 log_info("%s: ioengine=cpu, cpuload=%u,"
587 " cpucycle=%u\n", td->o.name,
591 char *c1, *c2, *c3, *c4;
593 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
594 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
595 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
596 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
598 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
599 " ioengine=%s, iodepth=%u\n",
600 td->o.name, td->groupid,
601 ddir_str[td->o.td_ddir],
611 } else if (job_add_num == 1)
616 * recurse add identical jobs, clear numjobs and stonewall options
617 * as they don't apply to sub-jobs
619 numjobs = td->o.numjobs;
621 struct thread_data *td_new = get_new_job(0, td);
626 td_new->o.numjobs = 1;
627 td_new->o.stonewall = 0;
628 td_new->o.new_group = 0;
631 td_new->o.filename = NULL;
632 td_new->files_index = 0;
633 td_new->files_size = 0;
634 td_new->files = NULL;
637 job_add_num = numjobs - 1;
639 if (add_job(td_new, jobname, job_add_num))
649 static int skip_this_section(const char *name)
653 if (!strncmp(name, "global", 6))
656 return strcmp(job_section, name);
659 static int is_empty_or_comment(char *line)
663 for (i = 0; i < strlen(line); i++) {
668 if (!isspace(line[i]) && !iscntrl(line[i]))
676 * This is our [ini] type file parser.
678 static int parse_jobs_ini(char *file, int stonewall_flag)
681 struct thread_data *td;
685 int ret = 0, stonewall;
690 int i, alloc_opts, num_opts;
692 if (!strcmp(file, "-"))
695 f = fopen(file, "r");
698 perror("fopen job file");
702 string = malloc(4096);
705 * it's really 256 + small bit, 280 should suffice
708 memset(name, 0, 280);
711 opts = malloc(sizeof(char *) * alloc_opts);
714 stonewall = stonewall_flag;
717 * if skip_fgets is set, we already have loaded a line we
721 p = fgets(string, 4095, f);
727 strip_blank_front(&p);
730 if (is_empty_or_comment(p))
732 if (sscanf(p, "[%255s]", name) != 1) {
735 log_err("fio: option <%s> outside of [] job section\n",
740 if (skip_this_section(name)) {
746 global = !strncmp(name, "global", 6);
748 name[strlen(name) - 1] = '\0';
754 log_info("--name=%s ", name);
758 td = get_new_job(global, &def_thread);
765 * Seperate multiple job files by a stonewall
767 if (!global && stonewall) {
768 td->o.stonewall = stonewall;
773 memset(opts, 0, alloc_opts * sizeof(char *));
775 while ((p = fgets(string, 4096, f)) != NULL) {
776 if (is_empty_or_comment(p))
779 strip_blank_front(&p);
782 * new section, break out and make sure we don't
783 * fgets() a new line at the top.
792 if (num_opts == alloc_opts) {
795 alloc_opts * sizeof(char *));
798 opts[num_opts] = strdup(p);
802 ret = fio_options_parse(td, opts, num_opts);
805 for (i = 0; i < num_opts; i++)
806 log_info("--%s ", opts[i]);
808 ret = add_job(td, name, 0);
810 log_err("fio: job %s dropped\n", name);
814 for (i = 0; i < num_opts; i++)
822 for (i = 0; i < num_opts; i++)
833 static int fill_def_thread(void)
835 memset(&def_thread, 0, sizeof(def_thread));
837 fio_getaffinity(getpid(), &def_thread.o.cpumask);
840 * fill default options
842 fio_fill_default_options(&def_thread);
844 def_thread.o.timeout = def_timeout;
845 def_thread.o.write_bw_log = write_bw_log;
846 def_thread.o.write_lat_log = write_lat_log;
851 static void free_shm(void)
853 struct shmid_ds sbuf;
860 fio_debug_jobp = NULL;
862 shmctl(shm_id, IPC_RMID, &sbuf);
869 * The thread area is shared between the main process and the job
870 * threads/processes. So setup a shared memory segment that will hold
871 * all the job info. We use the end of the region for keeping track of
872 * open files across jobs, for file sharing.
874 static int setup_thread_area(void)
879 * 1024 is too much on some machines, scale max_jobs if
880 * we get a failure that looks like too large a shm segment
883 size_t size = max_jobs * sizeof(struct thread_data);
885 size += file_hash_size;
886 size += sizeof(unsigned int);
888 shm_id = shmget(0, size, IPC_CREAT | 0600);
891 if (errno != EINVAL) {
902 threads = shmat(shm_id, NULL, 0);
903 if (threads == (void *) -1) {
908 memset(threads, 0, max_jobs * sizeof(struct thread_data));
909 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
910 fio_debug_jobp = (void *) hash + file_hash_size;
911 *fio_debug_jobp = -1;
912 file_hash_init(hash);
917 static void usage(const char *name)
919 printf("%s\n", fio_version_string);
920 printf("%s [options] [job options] <job file(s)>\n", name);
921 printf("\t--debug=options\tEnable debug logging\n");
922 printf("\t--output\tWrite output to file\n");
923 printf("\t--timeout\tRuntime in seconds\n");
924 printf("\t--latency-log\tGenerate per-job latency logs\n");
925 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
926 printf("\t--minimal\tMinimal (terse) output\n");
927 printf("\t--version\tPrint version info and exit\n");
928 printf("\t--help\t\tPrint this page\n");
929 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
931 printf("\t--showcmd\tTurn a job file into command line options\n");
932 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
933 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
934 printf("\t--readonly\tTurn on safety read-only checks, preventing"
936 printf("\t--section=name\tOnly run specified section in job file\n");
937 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
939 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>\n");
943 struct debug_level debug_levels[] = {
944 { .name = "process", .shift = FD_PROCESS, },
945 { .name = "file", .shift = FD_FILE, },
946 { .name = "io", .shift = FD_IO, },
947 { .name = "mem", .shift = FD_MEM, },
948 { .name = "blktrace", .shift = FD_BLKTRACE },
949 { .name = "verify", .shift = FD_VERIFY },
950 { .name = "random", .shift = FD_RANDOM },
951 { .name = "parse", .shift = FD_PARSE },
952 { .name = "diskutil", .shift = FD_DISKUTIL },
953 { .name = "job", .shift = FD_JOB },
954 { .name = "mutex", .shift = FD_MUTEX },
958 static int set_debug(const char *string)
960 struct debug_level *dl;
961 char *p = (char *) string;
965 if (!strcmp(string, "?") || !strcmp(string, "help")) {
968 log_info("fio: dumping debug options:");
969 for (i = 0; debug_levels[i].name; i++) {
970 dl = &debug_levels[i];
971 log_info("%s,", dl->name);
977 while ((opt = strsep(&p, ",")) != NULL) {
980 if (!strncmp(opt, "all", 3)) {
981 log_info("fio: set all debug options\n");
986 for (i = 0; debug_levels[i].name; i++) {
987 dl = &debug_levels[i];
988 found = !strncmp(opt, dl->name, strlen(dl->name));
992 if (dl->shift == FD_JOB) {
993 opt = strchr(opt, ':');
995 log_err("fio: missing job number\n");
999 fio_debug_jobno = atoi(opt);
1000 log_info("fio: set debug jobno %d\n",
1003 log_info("fio: set debug option %s\n", opt);
1004 fio_debug |= (1UL << dl->shift);
1010 log_err("fio: debug mask %s not found\n", opt);
1015 static int set_debug(const char *string)
1017 log_err("fio: debug tracing not included in build\n");
1022 static int load_profile(const char *profile)
1024 struct thread_data *td, *td_parent;
1026 int i, in_global = 1;
1029 dprint(FD_PARSE, "loading profile %s\n", profile);
1031 for (i = 0; i < PROFILE_END; i++) {
1032 if (!strcmp(profile, profiles[i]))
1036 if (i == PROFILE_END) {
1037 log_err("fio: unknown profile %s\n", profile);
1041 o = fio_prof_strings[i];
1046 td_parent = td = NULL;
1048 if (!strncmp(o[i], "name", 4)) {
1051 add_job(td, jobname, 0);
1053 sprintf(jobname, "%s", o[i] + 5);
1055 if (in_global && !td_parent)
1056 td_parent = get_new_job(1, &def_thread);
1057 else if (!in_global && !td) {
1059 td_parent = &def_thread;
1060 td = get_new_job(0, td_parent);
1063 fio_options_parse(td_parent, (char **) &o[i], 1);
1065 fio_options_parse(td, (char **) &o[i], 1);
1069 add_job(td, jobname, 0);
1073 static int parse_cmd_line(int argc, char *argv[])
1075 struct thread_data *td = NULL;
1076 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1078 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
1081 smalloc_pool_size = atoi(optarg);
1084 def_timeout = atoi(optarg);
1093 f_out = fopen(optarg, "w+");
1095 perror("fopen output");
1107 exit(fio_show_option_help(optarg));
1115 printf("%s\n", fio_version_string);
1118 if (!strcmp("always", optarg))
1119 eta_print = FIO_ETA_ALWAYS;
1120 else if (!strcmp("never", optarg))
1121 eta_print = FIO_ETA_NEVER;
1124 if (set_debug(optarg))
1128 if (!strcmp(optarg, "global")) {
1129 log_err("fio: can't use global as only "
1137 job_section = strdup(optarg);
1140 if (load_profile(optarg))
1143 case FIO_GETOPT_JOB: {
1144 const char *opt = l_opts[lidx].name;
1147 if (!strncmp(opt, "name", 4) && td) {
1148 ret = add_job(td, td->o.name ?: "fio", 0);
1156 int is_section = !strncmp(opt, "name", 4);
1159 if (!is_section || !strncmp(val, "global", 6))
1162 if (is_section && skip_this_section(val))
1165 td = get_new_job(global, &def_thread);
1170 ret = fio_cmd_option_parse(td, opt, val);
1185 ret = add_job(td, td->o.name ?: "fio", 0);
1190 while (optind < argc) {
1192 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1193 ini_file[ini_idx - 1] = strdup(argv[optind]);
1200 int parse_options(int argc, char *argv[])
1207 fio_options_dup_and_init(l_opts);
1209 if (setup_thread_area())
1211 if (fill_def_thread())
1214 job_files = parse_cmd_line(argc, argv);
1216 for (i = 0; i < job_files; i++) {
1217 if (fill_def_thread())
1219 if (parse_jobs_ini(ini_file[i], i))
1225 options_mem_free(&def_thread);
1227 if (!thread_number) {
1231 log_err("No jobs defined(s)\n\n");
1236 if (def_thread.o.gtod_offload) {
1238 fio_gtod_offload = 1;
1239 fio_gtod_cpu = def_thread.o.gtod_cpu;