2 * This file contains job initialization and setup functions.
14 #include <sys/types.h>
21 static char fio_version_string[] = "fio 1.19";
23 #define FIO_RANDSEED (0xb1899bedUL)
25 static char **ini_file;
26 static int max_jobs = MAX_JOBS;
27 static int dump_cmdline;
29 struct thread_data def_thread;
30 struct thread_data *threads = NULL;
32 int exitall_on_terminate = 0;
35 unsigned long long mlock_size = 0;
38 char *job_section = NULL;
43 static int def_timeout = 0;
44 static int write_lat_log = 0;
46 static int prev_group_jobs;
48 unsigned long fio_debug = 0;
51 * Command line options. These will contain the above, plus a few
52 * extra that only pertain to fio itself and not jobs.
54 static struct option long_options[FIO_NR_OPTIONS] = {
57 .has_arg = required_argument,
62 .has_arg = required_argument,
66 .name = "latency-log",
67 .has_arg = required_argument,
71 .name = "bandwidth-log",
72 .has_arg = required_argument,
77 .has_arg = optional_argument,
82 .has_arg = no_argument,
87 .has_arg = no_argument,
92 .has_arg = optional_argument,
97 .has_arg = no_argument,
102 .has_arg = no_argument,
107 .has_arg = required_argument,
112 .has_arg = required_argument,
117 .has_arg = required_argument,
136 * Return a free job structure.
138 static struct thread_data *get_new_job(int global, struct thread_data *parent)
140 struct thread_data *td;
144 if (thread_number >= max_jobs)
147 td = &threads[thread_number++];
150 dup_files(td, parent);
151 options_mem_dupe(td);
153 td->thread_number = thread_number;
157 static void put_job(struct thread_data *td)
159 if (td == &def_thread)
163 log_info("fio: %s\n", td->verror);
165 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
169 static int setup_rate(struct thread_data *td)
171 unsigned long nr_reads_per_msec;
172 unsigned long long rate;
175 if (!td->o.rate && !td->o.rate_iops)
179 bs = td->o.rw_min_bs;
180 else if (td_read(td))
181 bs = td->o.min_bs[DDIR_READ];
183 bs = td->o.min_bs[DDIR_WRITE];
187 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
189 nr_reads_per_msec = td->o.rate_iops * 1000UL;
191 if (!nr_reads_per_msec) {
192 log_err("rate lower than supported\n");
196 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
197 td->rate_pending_usleep = 0;
202 * Lazy way of fixing up options that depend on each other. We could also
203 * define option callback handlers, but this is easier.
205 static int fixup_options(struct thread_data *td)
207 struct thread_options *o = &td->o;
209 if (read_only && td_write(td)) {
210 log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
214 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
215 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
217 if (o->write_iolog_file && o->read_iolog_file) {
218 log_err("fio: read iolog overrides write_iolog\n");
219 free(o->write_iolog_file);
220 o->write_iolog_file = NULL;
224 * only really works for sequential io for now, and with 1 file
226 if (o->zone_size && td_random(td) && o->open_files == 1)
230 * Reads can do overwrites, we always need to pre-create the file
232 if (td_read(td) || td_rw(td))
235 if (!o->min_bs[DDIR_READ])
236 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
237 if (!o->max_bs[DDIR_READ])
238 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
239 if (!o->min_bs[DDIR_WRITE])
240 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
241 if (!o->max_bs[DDIR_WRITE])
242 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
244 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
246 if (!o->file_size_high)
247 o->file_size_high = o->file_size_low;
249 if (o->norandommap && o->verify != VERIFY_NONE) {
250 log_err("fio: norandommap given, verify disabled\n");
251 o->verify = VERIFY_NONE;
253 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
254 log_err("fio: bs_unaligned may not work with raw io\n");
257 * thinktime_spin must be less than thinktime
259 if (o->thinktime_spin > o->thinktime)
260 o->thinktime_spin = o->thinktime;
263 * The low water mark cannot be bigger than the iodepth
265 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
267 * syslet work around - if the workload is sequential,
268 * we want to let the queue drain all the way down to
269 * avoid seeking between async threads
271 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
274 o->iodepth_low = o->iodepth;
278 * If batch number isn't set, default to the same as iodepth
280 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
281 o->iodepth_batch = o->iodepth;
283 if (o->nr_files > td->files_index)
284 o->nr_files = td->files_index;
286 if (o->open_files > o->nr_files || !o->open_files)
287 o->open_files = o->nr_files;
289 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
290 log_err("fio: rate and rate_iops are mutually exclusive\n");
293 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
294 log_err("fio: minimum rate exceeds rate\n");
298 if (!o->timeout && o->time_based) {
299 log_err("fio: time_based requires a runtime/timeout setting\n");
303 if (o->fill_device && !o->size)
304 o->size = ULONG_LONG_MAX;
306 if (td_rw(td) && td->o.verify != VERIFY_NONE)
307 log_info("fio: mixed read/write workload with verify. May not "
308 "work as expected, unless you pre-populated the file\n");
314 * This function leaks the buffer
316 static char *to_kmg(unsigned int val)
318 char *buf = malloc(32);
319 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
330 snprintf(buf, 31, "%u%c", val, *p);
334 /* External engines are specified by "external:name.o") */
335 static const char *get_engine_name(const char *str)
337 char *p = strstr(str, ":");
343 strip_blank_front(&p);
348 static int exists_and_not_file(const char *filename)
352 if (lstat(filename, &sb) == -1)
355 if (S_ISREG(sb.st_mode))
362 * Initialize the various random states we need (random io, block size ranges,
363 * read/write mix, etc).
365 static int init_random_state(struct thread_data *td)
367 unsigned long seeds[6];
370 fd = open("/dev/urandom", O_RDONLY);
372 td_verror(td, errno, "open");
376 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
377 td_verror(td, EIO, "read");
384 os_random_seed(seeds[0], &td->bsrange_state);
385 os_random_seed(seeds[1], &td->verify_state);
386 os_random_seed(seeds[2], &td->rwmix_state);
388 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
389 os_random_seed(seeds[3], &td->next_file_state);
391 os_random_seed(seeds[5], &td->file_size_state);
396 if (td->o.rand_repeatable)
397 seeds[4] = FIO_RANDSEED * td->thread_number;
399 os_random_seed(seeds[4], &td->random_state);
404 * Adds a job to the list of things todo. Sanitizes the various options
405 * to make sure we don't have conflicts, and initializes various
408 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
410 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
411 "randread", "randwrite", "randrw" };
414 char fname[PATH_MAX];
415 int numjobs, file_alloced;
418 * the def_thread is just for options, it's not a real job
420 if (td == &def_thread)
424 * if we are just dumping the output command line, don't add the job
431 engine = get_engine_name(td->o.ioengine);
432 td->io_ops = load_ioengine(td, engine);
434 log_err("fio: failed to load engine %s\n", engine);
438 if (td->o.use_thread)
444 td->io_ops->flags |= FIO_RAWIO;
447 if (!td->o.filename && !td->files_index) {
450 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
451 add_file(td, jobname);
453 for (i = 0; i < td->o.nr_files; i++) {
454 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
460 if (fixup_options(td))
463 if (td->io_ops->flags & FIO_DISKLESSIO) {
466 for_each_file(td, f, i)
467 f->real_file_size = -1ULL;
470 td->mutex = fio_mutex_init(0);
472 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
473 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
474 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
475 td->ddir_nr = td->o.ddir_nr;
477 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
478 && prev_group_jobs) {
483 td->groupid = groupid;
486 if (init_random_state(td))
492 if (td->o.write_lat_log) {
493 setup_log(&td->ts.slat_log);
494 setup_log(&td->ts.clat_log);
496 if (td->o.write_bw_log)
497 setup_log(&td->ts.bw_log);
500 td->o.name = strdup(jobname);
504 if (!strcmp(td->io_ops->name, "cpuio"))
505 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
507 char *c1, *c2, *c3, *c4;
509 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
510 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
511 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
512 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
514 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);
521 } else if (job_add_num == 1)
526 * recurse add identical jobs, clear numjobs and stonewall options
527 * as they don't apply to sub-jobs
529 numjobs = td->o.numjobs;
531 struct thread_data *td_new = get_new_job(0, td);
536 td_new->o.numjobs = 1;
537 td_new->o.stonewall = 0;
538 td_new->o.new_group = 0;
541 td_new->o.filename = NULL;
542 td_new->files_index = 0;
543 td_new->files = NULL;
546 job_add_num = numjobs - 1;
548 if (add_job(td_new, jobname, job_add_num))
558 static int skip_this_section(const char *name)
562 if (!strncmp(name, "global", 6))
565 return strncmp(job_section, name, strlen(job_section));
568 static int is_empty_or_comment(char *line)
572 for (i = 0; i < strlen(line); i++) {
577 if (!isspace(line[i]) && !iscntrl(line[i]))
585 * This is our [ini] type file parser.
587 static int parse_jobs_ini(char *file, int stonewall_flag)
590 struct thread_data *td;
594 int ret = 0, stonewall;
599 if (!strcmp(file, "-"))
602 f = fopen(file, "r");
605 perror("fopen job file");
609 string = malloc(4096);
612 * it's really 256 + small bit, 280 should suffice
615 memset(name, 0, 280);
617 stonewall = stonewall_flag;
620 * if skip_fgets is set, we already have loaded a line we
624 p = fgets(string, 4095, f);
630 strip_blank_front(&p);
633 if (is_empty_or_comment(p))
635 if (sscanf(p, "[%255s]", name) != 1) {
638 log_err("fio: option <%s> outside of [] job section\n", p);
642 if (skip_this_section(name)) {
648 global = !strncmp(name, "global", 6);
650 name[strlen(name) - 1] = '\0';
656 log_info("--name=%s ", name);
660 td = get_new_job(global, &def_thread);
667 * Seperate multiple job files by a stonewall
669 if (!global && stonewall) {
670 td->o.stonewall = stonewall;
674 while ((p = fgets(string, 4096, f)) != NULL) {
675 if (is_empty_or_comment(p))
678 strip_blank_front(&p);
681 * new section, break out and make sure we don't
682 * fgets() a new line at the top.
692 * Don't break here, continue parsing options so we
693 * dump all the bad ones. Makes trial/error fixups
694 * easier on the user.
696 ret |= fio_option_parse(td, p);
697 if (!ret && dump_cmdline)
698 log_info("--%s ", p);
702 ret = add_job(td, name, 0);
704 log_err("fio: job %s dropped\n", name);
719 static int fill_def_thread(void)
721 memset(&def_thread, 0, sizeof(def_thread));
723 fio_getaffinity(getpid(), &def_thread.o.cpumask);
726 * fill default options
728 fio_fill_default_options(&def_thread);
730 def_thread.o.timeout = def_timeout;
731 def_thread.o.write_bw_log = write_bw_log;
732 def_thread.o.write_lat_log = write_lat_log;
737 static void free_shm(void)
739 struct shmid_ds sbuf;
742 shmdt((void *) threads);
744 shmctl(shm_id, IPC_RMID, &sbuf);
751 * The thread area is shared between the main process and the job
752 * threads/processes. So setup a shared memory segment that will hold
755 static int setup_thread_area(void)
758 * 1024 is too much on some machines, scale max_jobs if
759 * we get a failure that looks like too large a shm segment
762 size_t size = max_jobs * sizeof(struct thread_data);
764 shm_id = shmget(0, size, IPC_CREAT | 0600);
767 if (errno != EINVAL) {
778 threads = shmat(shm_id, NULL, 0);
779 if (threads == (void *) -1) {
784 memset(threads, 0, max_jobs * sizeof(struct thread_data));
789 static void usage(const char *name)
791 printf("%s\n", fio_version_string);
792 printf("%s [options] [job options] <job file(s)>\n", name);
793 printf("\t--debug=options\tEnable debug logging\n");
794 printf("\t--output\tWrite output to file\n");
795 printf("\t--timeout\tRuntime in seconds\n");
796 printf("\t--latency-log\tGenerate per-job latency logs\n");
797 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
798 printf("\t--minimal\tMinimal (terse) output\n");
799 printf("\t--version\tPrint version info and exit\n");
800 printf("\t--help\t\tPrint this page\n");
801 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
802 printf("\t--showcmd\tTurn a job file into command line options\n");
803 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
804 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
805 printf("\t--readonly\tTurn on safety read-only checks, preventing writes\n");
806 printf("\t--section=name\tOnly run specified section in job file\n");
810 struct debug_level debug_levels[] = {
811 { .name = "process", .shift = FD_PROCESS, },
812 { .name = "file", .shift = FD_FILE, },
813 { .name = "io", .shift = FD_IO, },
814 { .name = "mem", .shift = FD_MEM, },
815 { .name = "blktrace", .shift = FD_BLKTRACE },
816 { .name = "verify", .shift = FD_VERIFY },
817 { .name = "random", .shift = FD_RANDOM },
818 { .name = "parse", .shift = FD_PARSE },
822 static int set_debug(const char *string)
824 struct debug_level *dl;
825 char *p = (char *) string;
829 if (!strcmp(string, "?") || !strcmp(string, "help")) {
832 log_info("fio: dumping debug options:");
833 for (i = 0; debug_levels[i].name; i++) {
834 dl = &debug_levels[i];
835 log_info("%s,", dl->name);
839 } else if (!strcmp(string, "all")) {
844 while ((opt = strsep(&p, ",")) != NULL) {
847 for (i = 0; debug_levels[i].name; i++) {
848 dl = &debug_levels[i];
849 if (!strncmp(opt, dl->name, strlen(opt))) {
850 log_info("fio: set debug option %s\n", opt);
852 fio_debug |= (1UL << dl->shift);
858 log_err("fio: debug mask %s not found\n", opt);
863 static void set_debug(const char *string)
865 log_err("fio: debug tracing not included in build\n");
870 static int parse_cmd_line(int argc, char *argv[])
872 struct thread_data *td = NULL;
873 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
875 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
878 def_timeout = atoi(optarg);
887 f_out = fopen(optarg, "w+");
889 perror("fopen output");
901 exit(fio_show_option_help(optarg));
909 printf("%s\n", fio_version_string);
912 if (!strcmp("always", optarg))
913 eta_print = FIO_ETA_ALWAYS;
914 else if (!strcmp("never", optarg))
915 eta_print = FIO_ETA_NEVER;
918 if (set_debug(optarg))
922 if (!strcmp(optarg, "global")) {
923 log_err("fio: can't use global as only section\n");
930 job_section = strdup(optarg);
932 case FIO_GETOPT_JOB: {
933 const char *opt = long_options[lidx].name;
936 if (!strncmp(opt, "name", 4) && td) {
937 ret = add_job(td, td->o.name ?: "fio", 0);
945 int is_section = !strncmp(opt, "name", 4);
948 if (!is_section || !strncmp(val, "global", 6))
951 if (is_section && skip_this_section(val))
954 td = get_new_job(global, &def_thread);
959 ret = fio_cmd_option_parse(td, opt, val);
974 ret = add_job(td, td->o.name ?: "fio", 0);
979 while (optind < argc) {
981 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
982 ini_file[ini_idx - 1] = strdup(argv[optind]);
989 int parse_options(int argc, char *argv[])
996 fio_options_dup_and_init(long_options);
998 if (setup_thread_area())
1000 if (fill_def_thread())
1003 job_files = parse_cmd_line(argc, argv);
1005 for (i = 0; i < job_files; i++) {
1006 if (fill_def_thread())
1008 if (parse_jobs_ini(ini_file[i], i))
1014 options_mem_free(&def_thread);
1016 if (!thread_number) {
1020 log_err("No jobs defined(s)\n");