2 * This file contains job initialization and setup functions.
14 #include <sys/types.h>
20 static char fio_version_string[] = "fio 1.18.1";
22 #define FIO_RANDSEED (0xb1899bedUL)
24 static char **ini_file;
25 static int max_jobs = MAX_JOBS;
26 static int dump_cmdline;
28 struct thread_data def_thread;
29 struct thread_data *threads = NULL;
31 int exitall_on_terminate = 0;
34 unsigned long long mlock_size = 0;
37 char *job_section = NULL;
42 static int def_timeout = 0;
43 static int write_lat_log = 0;
45 static int prev_group_jobs;
47 unsigned long fio_debug = 0;
50 * Command line options. These will contain the above, plus a few
51 * extra that only pertain to fio itself and not jobs.
53 static struct option long_options[FIO_NR_OPTIONS] = {
56 .has_arg = required_argument,
61 .has_arg = required_argument,
65 .name = "latency-log",
66 .has_arg = required_argument,
70 .name = "bandwidth-log",
71 .has_arg = required_argument,
76 .has_arg = optional_argument,
81 .has_arg = no_argument,
86 .has_arg = no_argument,
91 .has_arg = optional_argument,
96 .has_arg = no_argument,
101 .has_arg = no_argument,
106 .has_arg = required_argument,
111 .has_arg = required_argument,
116 .has_arg = required_argument,
135 * Return a free job structure.
137 static struct thread_data *get_new_job(int global, struct thread_data *parent)
139 struct thread_data *td;
143 if (thread_number >= max_jobs)
146 td = &threads[thread_number++];
149 dup_files(td, parent);
150 options_mem_dupe(td);
152 td->thread_number = thread_number;
156 static void put_job(struct thread_data *td)
158 if (td == &def_thread)
162 log_info("fio: %s\n", td->verror);
164 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
168 static int setup_rate(struct thread_data *td)
170 unsigned long nr_reads_per_msec;
171 unsigned long long rate;
174 if (!td->o.rate && !td->o.rate_iops)
178 bs = td->o.rw_min_bs;
179 else if (td_read(td))
180 bs = td->o.min_bs[DDIR_READ];
182 bs = td->o.min_bs[DDIR_WRITE];
186 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
188 nr_reads_per_msec = td->o.rate_iops * 1000UL;
190 if (!nr_reads_per_msec) {
191 log_err("rate lower than supported\n");
195 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
196 td->rate_pending_usleep = 0;
201 * Lazy way of fixing up options that depend on each other. We could also
202 * define option callback handlers, but this is easier.
204 static int fixup_options(struct thread_data *td)
206 struct thread_options *o = &td->o;
208 if (read_only && td_write(td)) {
209 log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
213 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
214 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
216 if (o->write_iolog_file && o->read_iolog_file) {
217 log_err("fio: read iolog overrides write_iolog\n");
218 free(o->write_iolog_file);
219 o->write_iolog_file = NULL;
223 * only really works for sequential io for now, and with 1 file
225 if (o->zone_size && td_random(td) && o->open_files == 1)
229 * Reads can do overwrites, we always need to pre-create the file
231 if (td_read(td) || td_rw(td))
234 if (!o->min_bs[DDIR_READ])
235 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
236 if (!o->max_bs[DDIR_READ])
237 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
238 if (!o->min_bs[DDIR_WRITE])
239 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
240 if (!o->max_bs[DDIR_WRITE])
241 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
243 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
245 if (!o->file_size_high)
246 o->file_size_high = o->file_size_low;
248 if (o->norandommap && o->verify != VERIFY_NONE) {
249 log_err("fio: norandommap given, verify disabled\n");
250 o->verify = VERIFY_NONE;
252 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
253 log_err("fio: bs_unaligned may not work with raw io\n");
256 * thinktime_spin must be less than thinktime
258 if (o->thinktime_spin > o->thinktime)
259 o->thinktime_spin = o->thinktime;
262 * The low water mark cannot be bigger than the iodepth
264 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
266 * syslet work around - if the workload is sequential,
267 * we want to let the queue drain all the way down to
268 * avoid seeking between async threads
270 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
273 o->iodepth_low = o->iodepth;
277 * If batch number isn't set, default to the same as iodepth
279 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
280 o->iodepth_batch = o->iodepth;
282 if (o->nr_files > td->files_index)
283 o->nr_files = td->files_index;
285 if (o->open_files > o->nr_files || !o->open_files)
286 o->open_files = o->nr_files;
288 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
289 log_err("fio: rate and rate_iops are mutually exclusive\n");
292 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
293 log_err("fio: minimum rate exceeds rate\n");
297 if (!o->timeout && o->time_based) {
298 log_err("fio: time_based requires a runtime/timeout setting\n");
302 if (o->fill_device && !o->size)
303 o->size = ULONG_LONG_MAX;
305 if (td_rw(td) && td->o.verify != VERIFY_NONE)
306 log_info("fio: mixed read/write workload with verify. May not "
307 "work as expected, unless you pre-populated the file\n");
313 * This function leaks the buffer
315 static char *to_kmg(unsigned int val)
317 char *buf = malloc(32);
318 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
329 snprintf(buf, 31, "%u%c", val, *p);
333 /* External engines are specified by "external:name.o") */
334 static const char *get_engine_name(const char *str)
336 char *p = strstr(str, ":");
342 strip_blank_front(&p);
347 static int exists_and_not_file(const char *filename)
351 if (lstat(filename, &sb) == -1)
354 if (S_ISREG(sb.st_mode))
361 * Initialize the various random states we need (random io, block size ranges,
362 * read/write mix, etc).
364 static int init_random_state(struct thread_data *td)
366 unsigned long seeds[6];
369 fd = open("/dev/urandom", O_RDONLY);
371 td_verror(td, errno, "open");
375 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
376 td_verror(td, EIO, "read");
383 os_random_seed(seeds[0], &td->bsrange_state);
384 os_random_seed(seeds[1], &td->verify_state);
385 os_random_seed(seeds[2], &td->rwmix_state);
387 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
388 os_random_seed(seeds[3], &td->next_file_state);
390 os_random_seed(seeds[5], &td->file_size_state);
395 if (td->o.rand_repeatable)
396 seeds[4] = FIO_RANDSEED * td->thread_number;
398 os_random_seed(seeds[4], &td->random_state);
403 * Adds a job to the list of things todo. Sanitizes the various options
404 * to make sure we don't have conflicts, and initializes various
407 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
409 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
410 "randread", "randwrite", "randrw" };
413 char fname[PATH_MAX];
414 int numjobs, file_alloced;
417 * the def_thread is just for options, it's not a real job
419 if (td == &def_thread)
423 * if we are just dumping the output command line, don't add the job
430 engine = get_engine_name(td->o.ioengine);
431 td->io_ops = load_ioengine(td, engine);
433 log_err("fio: failed to load engine %s\n", engine);
437 if (td->o.use_thread)
443 td->io_ops->flags |= FIO_RAWIO;
446 if (!td->o.filename && !td->files_index) {
449 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
450 add_file(td, jobname);
452 for (i = 0; i < td->o.nr_files; i++) {
453 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
459 if (fixup_options(td))
462 if (td->io_ops->flags & FIO_DISKLESSIO) {
465 for_each_file(td, f, i)
466 f->real_file_size = -1ULL;
469 td->mutex = fio_sem_init(0);
471 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
472 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
473 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
474 td->ddir_nr = td->o.ddir_nr;
476 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
477 && prev_group_jobs) {
482 td->groupid = groupid;
485 if (init_random_state(td))
491 if (td->o.write_lat_log) {
492 setup_log(&td->ts.slat_log);
493 setup_log(&td->ts.clat_log);
495 if (td->o.write_bw_log)
496 setup_log(&td->ts.bw_log);
499 td->o.name = strdup(jobname);
503 if (!strcmp(td->io_ops->name, "cpuio"))
504 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
506 char *c1, *c2, *c3, *c4;
508 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
509 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
510 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
511 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
513 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->o.name, td->groupid, ddir_str[td->o.td_ddir], c1, c2, c3, c4, td->io_ops->name, td->o.iodepth);
520 } else if (job_add_num == 1)
525 * recurse add identical jobs, clear numjobs and stonewall options
526 * as they don't apply to sub-jobs
528 numjobs = td->o.numjobs;
530 struct thread_data *td_new = get_new_job(0, td);
535 td_new->o.numjobs = 1;
536 td_new->o.stonewall = 0;
537 td_new->o.new_group = 0;
540 td_new->o.filename = NULL;
541 td_new->files_index = 0;
542 td_new->files = NULL;
545 job_add_num = numjobs - 1;
547 if (add_job(td_new, jobname, job_add_num))
557 static int skip_this_section(const char *name)
561 if (!strncmp(name, "global", 6))
564 return strncmp(job_section, name, strlen(job_section));
567 static int is_empty_or_comment(char *line)
571 for (i = 0; i < strlen(line); i++) {
576 if (!isspace(line[i]) && !iscntrl(line[i]))
584 * This is our [ini] type file parser.
586 static int parse_jobs_ini(char *file, int stonewall_flag)
589 struct thread_data *td;
593 int ret = 0, stonewall;
598 if (!strcmp(file, "-"))
601 f = fopen(file, "r");
604 perror("fopen job file");
608 string = malloc(4096);
611 * it's really 256 + small bit, 280 should suffice
614 memset(name, 0, 280);
616 stonewall = stonewall_flag;
619 * if skip_fgets is set, we already have loaded a line we
623 p = fgets(string, 4095, f);
629 strip_blank_front(&p);
632 if (is_empty_or_comment(p))
634 if (sscanf(p, "[%255s]", name) != 1) {
637 log_err("fio: option <%s> outside of [] job section\n", p);
641 if (skip_this_section(name)) {
647 global = !strncmp(name, "global", 6);
649 name[strlen(name) - 1] = '\0';
655 log_info("--name=%s ", name);
659 td = get_new_job(global, &def_thread);
666 * Seperate multiple job files by a stonewall
668 if (!global && stonewall) {
669 td->o.stonewall = stonewall;
673 while ((p = fgets(string, 4096, f)) != NULL) {
674 if (is_empty_or_comment(p))
677 strip_blank_front(&p);
680 * new section, break out and make sure we don't
681 * fgets() a new line at the top.
691 * Don't break here, continue parsing options so we
692 * dump all the bad ones. Makes trial/error fixups
693 * easier on the user.
695 ret |= fio_option_parse(td, p);
696 if (!ret && dump_cmdline)
697 log_info("--%s ", p);
701 ret = add_job(td, name, 0);
703 log_err("fio: job %s dropped\n", name);
718 static int fill_def_thread(void)
720 memset(&def_thread, 0, sizeof(def_thread));
722 fio_getaffinity(getpid(), &def_thread.o.cpumask);
725 * fill default options
727 fio_fill_default_options(&def_thread);
729 def_thread.o.timeout = def_timeout;
730 def_thread.o.write_bw_log = write_bw_log;
731 def_thread.o.write_lat_log = write_lat_log;
736 static void free_shm(void)
738 struct shmid_ds sbuf;
741 shmdt((void *) threads);
743 shmctl(shm_id, IPC_RMID, &sbuf);
748 * The thread area is shared between the main process and the job
749 * threads/processes. So setup a shared memory segment that will hold
752 static int setup_thread_area(void)
755 * 1024 is too much on some machines, scale max_jobs if
756 * we get a failure that looks like too large a shm segment
759 size_t size = max_jobs * sizeof(struct thread_data);
761 shm_id = shmget(0, size, IPC_CREAT | 0600);
764 if (errno != EINVAL) {
775 threads = shmat(shm_id, NULL, 0);
776 if (threads == (void *) -1) {
781 memset(threads, 0, max_jobs * sizeof(struct thread_data));
786 static void usage(const char *name)
788 printf("%s\n", fio_version_string);
789 printf("%s [options] [job options] <job file(s)>\n", name);
790 printf("\t--debug=options\tEnable debug logging\n");
791 printf("\t--output\tWrite output to file\n");
792 printf("\t--timeout\tRuntime in seconds\n");
793 printf("\t--latency-log\tGenerate per-job latency logs\n");
794 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
795 printf("\t--minimal\tMinimal (terse) output\n");
796 printf("\t--version\tPrint version info and exit\n");
797 printf("\t--help\t\tPrint this page\n");
798 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
799 printf("\t--showcmd\tTurn a job file into command line options\n");
800 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
801 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
802 printf("\t--readonly\tTurn on safety read-only checks, preventing writes\n");
803 printf("\t--section=name\tOnly run specified section in job file\n");
807 struct debug_level debug_levels[] = {
808 { .name = "process", .shift = FD_PROCESS, },
809 { .name = "file", .shift = FD_FILE, },
810 { .name = "io", .shift = FD_IO, },
811 { .name = "mem", .shift = FD_MEM, },
812 { .name = "blktrace", .shift = FD_BLKTRACE },
813 { .name = "verify", .shift = FD_VERIFY },
814 { .name = "random", .shift = FD_RANDOM },
818 static void set_debug(const char *string)
820 struct debug_level *dl;
821 char *p = (char *) string;
825 if (!strcmp(string, "?") || !strcmp(string, "help")) {
828 log_info("fio: dumping debug options:");
829 for (i = 0; debug_levels[i].name; i++) {
830 dl = &debug_levels[i];
831 log_info("%s,", dl->name);
835 } else if (!strcmp(string, "all")) {
840 while ((opt = strsep(&p, ",")) != NULL) {
843 for (i = 0; debug_levels[i].name; i++) {
844 dl = &debug_levels[i];
845 if (!strncmp(opt, dl->name, strlen(opt))) {
846 log_info("fio: set debug option %s\n", opt);
848 fio_debug |= (1UL << dl->shift);
854 log_err("fio: debug mask %s not found\n", opt);
858 static void set_debug(const char *string)
860 log_err("fio: debug tracing not included in build\n");
864 static int parse_cmd_line(int argc, char *argv[])
866 struct thread_data *td = NULL;
867 int c, ini_idx = 0, lidx, ret = 0, bad_options = 0;
869 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
872 def_timeout = atoi(optarg);
881 f_out = fopen(optarg, "w+");
883 perror("fopen output");
895 exit(fio_show_option_help(optarg));
903 printf("%s\n", fio_version_string);
906 if (!strcmp("always", optarg))
907 eta_print = FIO_ETA_ALWAYS;
908 else if (!strcmp("never", optarg))
909 eta_print = FIO_ETA_NEVER;
915 if (!strcmp(optarg, "global")) {
916 log_err("fio: can't use global as only section\n");
922 job_section = strdup(optarg);
924 case FIO_GETOPT_JOB: {
925 const char *opt = long_options[lidx].name;
928 if (!strncmp(opt, "name", 4) && td) {
929 ret = add_job(td, td->o.name ?: "fio", 0);
937 int is_section = !strncmp(opt, "name", 4);
940 if (!is_section || !strncmp(val, "global", 6))
943 if (is_section && skip_this_section(val))
946 td = get_new_job(global, &def_thread);
951 ret = fio_cmd_option_parse(td, opt, val);
965 ret = add_job(td, td->o.name ?: "fio", 0);
970 while (optind < argc) {
972 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
973 ini_file[ini_idx - 1] = strdup(argv[optind]);
980 int parse_options(int argc, char *argv[])
987 fio_options_dup_and_init(long_options);
989 if (setup_thread_area())
991 if (fill_def_thread())
994 job_files = parse_cmd_line(argc, argv);
996 for (i = 0; i < job_files; i++) {
997 if (fill_def_thread())
999 if (parse_jobs_ini(ini_file[i], i))
1005 options_mem_free(&def_thread);
1007 if (!thread_number) {
1011 log_err("No jobs defined(s)\n");