2 * This file contains job initialization and setup functions.
14 #include <sys/types.h>
22 static char fio_version_string[] = "fio 1.20";
24 #define FIO_RANDSEED (0xb1899bedUL)
26 static char **ini_file;
27 static int max_jobs = MAX_JOBS;
28 static int dump_cmdline;
30 struct thread_data def_thread;
31 struct thread_data *threads = NULL;
33 int exitall_on_terminate = 0;
36 unsigned long long mlock_size = 0;
39 char *job_section = NULL;
44 static int def_timeout;
45 static int write_lat_log;
47 static int prev_group_jobs;
49 unsigned long fio_debug = 0;
52 * Command line options. These will contain the above, plus a few
53 * extra that only pertain to fio itself and not jobs.
55 static struct option l_opts[FIO_NR_OPTIONS] = {
58 .has_arg = required_argument,
63 .has_arg = required_argument,
67 .name = "latency-log",
68 .has_arg = required_argument,
72 .name = "bandwidth-log",
73 .has_arg = required_argument,
78 .has_arg = optional_argument,
83 .has_arg = no_argument,
88 .has_arg = no_argument,
93 .has_arg = optional_argument,
98 .has_arg = no_argument,
103 .has_arg = no_argument,
108 .has_arg = required_argument,
113 .has_arg = required_argument,
118 .has_arg = required_argument,
122 .name = "alloc-size",
123 .has_arg = required_argument,
142 * Return a free job structure.
144 static struct thread_data *get_new_job(int global, struct thread_data *parent)
146 struct thread_data *td;
150 if (thread_number >= max_jobs)
153 td = &threads[thread_number++];
156 dup_files(td, parent);
157 options_mem_dupe(td);
159 td->thread_number = thread_number;
163 static void put_job(struct thread_data *td)
165 if (td == &def_thread)
169 log_info("fio: %s\n", td->verror);
171 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
175 static int setup_rate(struct thread_data *td)
177 unsigned long nr_reads_per_msec;
178 unsigned long long rate;
181 if (!td->o.rate && !td->o.rate_iops)
185 bs = td->o.rw_min_bs;
186 else if (td_read(td))
187 bs = td->o.min_bs[DDIR_READ];
189 bs = td->o.min_bs[DDIR_WRITE];
193 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
195 nr_reads_per_msec = td->o.rate_iops * 1000UL;
197 if (!nr_reads_per_msec) {
198 log_err("rate lower than supported\n");
202 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
203 td->rate_pending_usleep = 0;
208 * Lazy way of fixing up options that depend on each other. We could also
209 * define option callback handlers, but this is easier.
211 static int fixup_options(struct thread_data *td)
213 struct thread_options *o = &td->o;
215 if (read_only && td_write(td)) {
216 log_err("fio: job <%s> has write bit set, but fio is in"
217 " read-only mode\n", td->o.name);
221 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
222 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
224 if (o->write_iolog_file && o->read_iolog_file) {
225 log_err("fio: read iolog overrides write_iolog\n");
226 free(o->write_iolog_file);
227 o->write_iolog_file = NULL;
231 * only really works for sequential io for now, and with 1 file
233 if (o->zone_size && td_random(td) && o->open_files == 1)
237 * Reads can do overwrites, we always need to pre-create the file
239 if (td_read(td) || td_rw(td))
242 if (!o->min_bs[DDIR_READ])
243 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
244 if (!o->max_bs[DDIR_READ])
245 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
246 if (!o->min_bs[DDIR_WRITE])
247 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
248 if (!o->max_bs[DDIR_WRITE])
249 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
251 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
253 if (!o->file_size_high)
254 o->file_size_high = o->file_size_low;
256 if (o->norandommap && o->verify != VERIFY_NONE) {
257 log_err("fio: norandommap given, verify disabled\n");
258 o->verify = VERIFY_NONE;
260 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
261 log_err("fio: bs_unaligned may not work with raw io\n");
264 * thinktime_spin must be less than thinktime
266 if (o->thinktime_spin > o->thinktime)
267 o->thinktime_spin = o->thinktime;
270 * The low water mark cannot be bigger than the iodepth
272 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
274 * syslet work around - if the workload is sequential,
275 * we want to let the queue drain all the way down to
276 * avoid seeking between async threads
278 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
281 o->iodepth_low = o->iodepth;
285 * If batch number isn't set, default to the same as iodepth
287 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
288 o->iodepth_batch = o->iodepth;
290 if (o->nr_files > td->files_index)
291 o->nr_files = td->files_index;
293 if (o->open_files > o->nr_files || !o->open_files)
294 o->open_files = o->nr_files;
296 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
297 log_err("fio: rate and rate_iops are mutually exclusive\n");
300 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
301 log_err("fio: minimum rate exceeds rate\n");
305 if (!o->timeout && o->time_based) {
306 log_err("fio: time_based requires a runtime/timeout setting\n");
310 if (o->fill_device && !o->size)
311 o->size = ULONG_LONG_MAX;
313 if (td_rw(td) && td->o.verify != VERIFY_NONE)
314 log_info("fio: mixed read/write workload with verify. May not "
315 "work as expected, unless you pre-populated the file\n");
321 * This function leaks the buffer
323 static char *to_kmg(unsigned int val)
325 char *buf = malloc(32);
326 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
337 snprintf(buf, 31, "%u%c", val, *p);
341 /* External engines are specified by "external:name.o") */
342 static const char *get_engine_name(const char *str)
344 char *p = strstr(str, ":");
350 strip_blank_front(&p);
355 static int exists_and_not_file(const char *filename)
359 if (lstat(filename, &sb) == -1)
362 if (S_ISREG(sb.st_mode))
369 * Initialize the various random states we need (random io, block size ranges,
370 * read/write mix, etc).
372 static int init_random_state(struct thread_data *td)
374 unsigned long seeds[6];
377 fd = open("/dev/urandom", O_RDONLY);
379 td_verror(td, errno, "open");
383 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
384 td_verror(td, EIO, "read");
391 os_random_seed(seeds[0], &td->bsrange_state);
392 os_random_seed(seeds[1], &td->verify_state);
393 os_random_seed(seeds[2], &td->rwmix_state);
395 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
396 os_random_seed(seeds[3], &td->next_file_state);
398 os_random_seed(seeds[5], &td->file_size_state);
403 if (td->o.rand_repeatable)
404 seeds[4] = FIO_RANDSEED * td->thread_number;
406 os_random_seed(seeds[4], &td->random_state);
411 * Adds a job to the list of things todo. Sanitizes the various options
412 * to make sure we don't have conflicts, and initializes various
415 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
417 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
418 "randread", "randwrite", "randrw" };
421 char fname[PATH_MAX];
422 int numjobs, file_alloced;
425 * the def_thread is just for options, it's not a real job
427 if (td == &def_thread)
431 * if we are just dumping the output command line, don't add the job
438 engine = get_engine_name(td->o.ioengine);
439 td->io_ops = load_ioengine(td, engine);
441 log_err("fio: failed to load engine %s\n", engine);
445 if (td->o.use_thread)
451 td->io_ops->flags |= FIO_RAWIO;
454 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
457 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
458 add_file(td, jobname);
460 for (i = 0; i < td->o.nr_files; i++) {
461 sprintf(fname, "%s.%d.%d", jobname,
462 td->thread_number, i);
468 if (fixup_options(td))
471 if (td->io_ops->flags & FIO_DISKLESSIO) {
474 for_each_file(td, f, i)
475 f->real_file_size = -1ULL;
478 td->mutex = fio_mutex_init(0);
480 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
481 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
482 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
483 td->ddir_nr = td->o.ddir_nr;
485 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
486 && prev_group_jobs) {
491 td->groupid = groupid;
494 if (init_random_state(td))
500 if (td->o.write_lat_log) {
501 setup_log(&td->ts.slat_log);
502 setup_log(&td->ts.clat_log);
504 if (td->o.write_bw_log)
505 setup_log(&td->ts.bw_log);
508 td->o.name = strdup(jobname);
512 if (!strcmp(td->io_ops->name, "cpuio")) {
513 log_info("%s: ioengine=cpu, cpuload=%u,"
514 " cpucycle=%u\n", td->o.name,
518 char *c1, *c2, *c3, *c4;
520 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
521 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
522 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
523 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
525 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
526 " ioengine=%s, iodepth=%u\n",
527 td->o.name, td->groupid,
528 ddir_str[td->o.td_ddir],
538 } else if (job_add_num == 1)
543 * recurse add identical jobs, clear numjobs and stonewall options
544 * as they don't apply to sub-jobs
546 numjobs = td->o.numjobs;
548 struct thread_data *td_new = get_new_job(0, td);
553 td_new->o.numjobs = 1;
554 td_new->o.stonewall = 0;
555 td_new->o.new_group = 0;
558 td_new->o.filename = NULL;
559 td_new->files_index = 0;
560 td_new->files = NULL;
563 job_add_num = numjobs - 1;
565 if (add_job(td_new, jobname, job_add_num))
575 static int skip_this_section(const char *name)
579 if (!strncmp(name, "global", 6))
582 return strncmp(job_section, name, strlen(job_section));
585 static int is_empty_or_comment(char *line)
589 for (i = 0; i < strlen(line); i++) {
594 if (!isspace(line[i]) && !iscntrl(line[i]))
602 * This is our [ini] type file parser.
604 static int parse_jobs_ini(char *file, int stonewall_flag)
607 struct thread_data *td;
611 int ret = 0, stonewall;
616 if (!strcmp(file, "-"))
619 f = fopen(file, "r");
622 perror("fopen job file");
626 string = malloc(4096);
629 * it's really 256 + small bit, 280 should suffice
632 memset(name, 0, 280);
634 stonewall = stonewall_flag;
637 * if skip_fgets is set, we already have loaded a line we
641 p = fgets(string, 4095, f);
647 strip_blank_front(&p);
650 if (is_empty_or_comment(p))
652 if (sscanf(p, "[%255s]", name) != 1) {
655 log_err("fio: option <%s> outside of [] job section\n",
660 if (skip_this_section(name)) {
666 global = !strncmp(name, "global", 6);
668 name[strlen(name) - 1] = '\0';
674 log_info("--name=%s ", name);
678 td = get_new_job(global, &def_thread);
685 * Seperate multiple job files by a stonewall
687 if (!global && stonewall) {
688 td->o.stonewall = stonewall;
692 while ((p = fgets(string, 4096, f)) != NULL) {
693 if (is_empty_or_comment(p))
696 strip_blank_front(&p);
699 * new section, break out and make sure we don't
700 * fgets() a new line at the top.
710 * Don't break here, continue parsing options so we
711 * dump all the bad ones. Makes trial/error fixups
712 * easier on the user.
714 ret |= fio_option_parse(td, p);
715 if (!ret && dump_cmdline)
716 log_info("--%s ", p);
720 ret = add_job(td, name, 0);
722 log_err("fio: job %s dropped\n", name);
737 static int fill_def_thread(void)
739 memset(&def_thread, 0, sizeof(def_thread));
741 fio_getaffinity(getpid(), &def_thread.o.cpumask);
744 * fill default options
746 fio_fill_default_options(&def_thread);
748 def_thread.o.timeout = def_timeout;
749 def_thread.o.write_bw_log = write_bw_log;
750 def_thread.o.write_lat_log = write_lat_log;
755 static void free_shm(void)
757 struct shmid_ds sbuf;
760 shmdt((void *) threads);
762 shmctl(shm_id, IPC_RMID, &sbuf);
769 * The thread area is shared between the main process and the job
770 * threads/processes. So setup a shared memory segment that will hold
771 * all the job info. We use the end of the region for keeping track of
772 * open files across jobs, for file sharing.
774 static int setup_thread_area(void)
779 * 1024 is too much on some machines, scale max_jobs if
780 * we get a failure that looks like too large a shm segment
783 size_t size = max_jobs * sizeof(struct thread_data);
785 size += file_hash_size;
787 shm_id = shmget(0, size, IPC_CREAT | 0600);
790 if (errno != EINVAL) {
801 threads = shmat(shm_id, NULL, 0);
802 if (threads == (void *) -1) {
807 memset(threads, 0, max_jobs * sizeof(struct thread_data));
808 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
809 file_hash_init(hash);
814 static void usage(const char *name)
816 printf("%s\n", fio_version_string);
817 printf("%s [options] [job options] <job file(s)>\n", name);
818 printf("\t--debug=options\tEnable debug logging\n");
819 printf("\t--output\tWrite output to file\n");
820 printf("\t--timeout\tRuntime in seconds\n");
821 printf("\t--latency-log\tGenerate per-job latency logs\n");
822 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
823 printf("\t--minimal\tMinimal (terse) output\n");
824 printf("\t--version\tPrint version info and exit\n");
825 printf("\t--help\t\tPrint this page\n");
826 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
828 printf("\t--showcmd\tTurn a job file into command line options\n");
829 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
830 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
831 printf("\t--readonly\tTurn on safety read-only checks, preventing"
833 printf("\t--section=name\tOnly run specified section in job file\n");
834 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
839 struct debug_level debug_levels[] = {
840 { .name = "process", .shift = FD_PROCESS, },
841 { .name = "file", .shift = FD_FILE, },
842 { .name = "io", .shift = FD_IO, },
843 { .name = "mem", .shift = FD_MEM, },
844 { .name = "blktrace", .shift = FD_BLKTRACE },
845 { .name = "verify", .shift = FD_VERIFY },
846 { .name = "random", .shift = FD_RANDOM },
847 { .name = "parse", .shift = FD_PARSE },
848 { .name = "diskutil", .shift = FD_DISKUTIL },
852 static int set_debug(const char *string)
854 struct debug_level *dl;
855 char *p = (char *) string;
859 if (!strcmp(string, "?") || !strcmp(string, "help")) {
862 log_info("fio: dumping debug options:");
863 for (i = 0; debug_levels[i].name; i++) {
864 dl = &debug_levels[i];
865 log_info("%s,", dl->name);
869 } else if (!strcmp(string, "all")) {
874 while ((opt = strsep(&p, ",")) != NULL) {
877 for (i = 0; debug_levels[i].name; i++) {
878 dl = &debug_levels[i];
879 if (!strncmp(opt, dl->name, strlen(opt))) {
880 log_info("fio: set debug option %s\n", opt);
882 fio_debug |= (1UL << dl->shift);
888 log_err("fio: debug mask %s not found\n", opt);
893 static void set_debug(const char *string)
895 log_err("fio: debug tracing not included in build\n");
900 static int parse_cmd_line(int argc, char *argv[])
902 struct thread_data *td = NULL;
903 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
905 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
908 smalloc_pool_size = atoi(optarg);
911 def_timeout = atoi(optarg);
920 f_out = fopen(optarg, "w+");
922 perror("fopen output");
934 exit(fio_show_option_help(optarg));
942 printf("%s\n", fio_version_string);
945 if (!strcmp("always", optarg))
946 eta_print = FIO_ETA_ALWAYS;
947 else if (!strcmp("never", optarg))
948 eta_print = FIO_ETA_NEVER;
951 if (set_debug(optarg))
955 if (!strcmp(optarg, "global")) {
956 log_err("fio: can't use global as only "
964 job_section = strdup(optarg);
966 case FIO_GETOPT_JOB: {
967 const char *opt = l_opts[lidx].name;
970 if (!strncmp(opt, "name", 4) && td) {
971 ret = add_job(td, td->o.name ?: "fio", 0);
979 int is_section = !strncmp(opt, "name", 4);
982 if (!is_section || !strncmp(val, "global", 6))
985 if (is_section && skip_this_section(val))
988 td = get_new_job(global, &def_thread);
993 ret = fio_cmd_option_parse(td, opt, val);
1008 ret = add_job(td, td->o.name ?: "fio", 0);
1013 while (optind < argc) {
1015 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1016 ini_file[ini_idx - 1] = strdup(argv[optind]);
1023 int parse_options(int argc, char *argv[])
1030 fio_options_dup_and_init(l_opts);
1032 if (setup_thread_area())
1034 if (fill_def_thread())
1037 job_files = parse_cmd_line(argc, argv);
1039 for (i = 0; i < job_files; i++) {
1040 if (fill_def_thread())
1042 if (parse_jobs_ini(ini_file[i], i))
1048 options_mem_free(&def_thread);
1050 if (!thread_number) {
1054 log_err("No jobs defined(s)\n");