2 * This file contains job initialization and setup functions.
14 #include <sys/types.h>
22 static char fio_version_string[] = "fio 1.20-rc1";
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 = 0;
45 static int write_lat_log = 0;
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 long_options[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,
137 * Return a free job structure.
139 static struct thread_data *get_new_job(int global, struct thread_data *parent)
141 struct thread_data *td;
145 if (thread_number >= max_jobs)
148 td = &threads[thread_number++];
151 dup_files(td, parent);
152 options_mem_dupe(td);
154 td->thread_number = thread_number;
158 static void put_job(struct thread_data *td)
160 if (td == &def_thread)
164 log_info("fio: %s\n", td->verror);
166 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
170 static int setup_rate(struct thread_data *td)
172 unsigned long nr_reads_per_msec;
173 unsigned long long rate;
176 if (!td->o.rate && !td->o.rate_iops)
180 bs = td->o.rw_min_bs;
181 else if (td_read(td))
182 bs = td->o.min_bs[DDIR_READ];
184 bs = td->o.min_bs[DDIR_WRITE];
188 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
190 nr_reads_per_msec = td->o.rate_iops * 1000UL;
192 if (!nr_reads_per_msec) {
193 log_err("rate lower than supported\n");
197 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
198 td->rate_pending_usleep = 0;
203 * Lazy way of fixing up options that depend on each other. We could also
204 * define option callback handlers, but this is easier.
206 static int fixup_options(struct thread_data *td)
208 struct thread_options *o = &td->o;
210 if (read_only && td_write(td)) {
211 log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
215 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
216 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
218 if (o->write_iolog_file && o->read_iolog_file) {
219 log_err("fio: read iolog overrides write_iolog\n");
220 free(o->write_iolog_file);
221 o->write_iolog_file = NULL;
225 * only really works for sequential io for now, and with 1 file
227 if (o->zone_size && td_random(td) && o->open_files == 1)
231 * Reads can do overwrites, we always need to pre-create the file
233 if (td_read(td) || td_rw(td))
236 if (!o->min_bs[DDIR_READ])
237 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
238 if (!o->max_bs[DDIR_READ])
239 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
240 if (!o->min_bs[DDIR_WRITE])
241 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
242 if (!o->max_bs[DDIR_WRITE])
243 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
245 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
247 if (!o->file_size_high)
248 o->file_size_high = o->file_size_low;
250 if (o->norandommap && o->verify != VERIFY_NONE) {
251 log_err("fio: norandommap given, verify disabled\n");
252 o->verify = VERIFY_NONE;
254 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
255 log_err("fio: bs_unaligned may not work with raw io\n");
258 * thinktime_spin must be less than thinktime
260 if (o->thinktime_spin > o->thinktime)
261 o->thinktime_spin = o->thinktime;
264 * The low water mark cannot be bigger than the iodepth
266 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
268 * syslet work around - if the workload is sequential,
269 * we want to let the queue drain all the way down to
270 * avoid seeking between async threads
272 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
275 o->iodepth_low = o->iodepth;
279 * If batch number isn't set, default to the same as iodepth
281 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
282 o->iodepth_batch = o->iodepth;
284 if (o->nr_files > td->files_index)
285 o->nr_files = td->files_index;
287 if (o->open_files > o->nr_files || !o->open_files)
288 o->open_files = o->nr_files;
290 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
291 log_err("fio: rate and rate_iops are mutually exclusive\n");
294 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
295 log_err("fio: minimum rate exceeds rate\n");
299 if (!o->timeout && o->time_based) {
300 log_err("fio: time_based requires a runtime/timeout setting\n");
304 if (o->fill_device && !o->size)
305 o->size = ULONG_LONG_MAX;
307 if (td_rw(td) && td->o.verify != VERIFY_NONE)
308 log_info("fio: mixed read/write workload with verify. May not "
309 "work as expected, unless you pre-populated the file\n");
315 * This function leaks the buffer
317 static char *to_kmg(unsigned int val)
319 char *buf = malloc(32);
320 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
331 snprintf(buf, 31, "%u%c", val, *p);
335 /* External engines are specified by "external:name.o") */
336 static const char *get_engine_name(const char *str)
338 char *p = strstr(str, ":");
344 strip_blank_front(&p);
349 static int exists_and_not_file(const char *filename)
353 if (lstat(filename, &sb) == -1)
356 if (S_ISREG(sb.st_mode))
363 * Initialize the various random states we need (random io, block size ranges,
364 * read/write mix, etc).
366 static int init_random_state(struct thread_data *td)
368 unsigned long seeds[6];
371 fd = open("/dev/urandom", O_RDONLY);
373 td_verror(td, errno, "open");
377 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
378 td_verror(td, EIO, "read");
385 os_random_seed(seeds[0], &td->bsrange_state);
386 os_random_seed(seeds[1], &td->verify_state);
387 os_random_seed(seeds[2], &td->rwmix_state);
389 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
390 os_random_seed(seeds[3], &td->next_file_state);
392 os_random_seed(seeds[5], &td->file_size_state);
397 if (td->o.rand_repeatable)
398 seeds[4] = FIO_RANDSEED * td->thread_number;
400 os_random_seed(seeds[4], &td->random_state);
405 * Adds a job to the list of things todo. Sanitizes the various options
406 * to make sure we don't have conflicts, and initializes various
409 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
411 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
412 "randread", "randwrite", "randrw" };
415 char fname[PATH_MAX];
416 int numjobs, file_alloced;
419 * the def_thread is just for options, it's not a real job
421 if (td == &def_thread)
425 * if we are just dumping the output command line, don't add the job
432 engine = get_engine_name(td->o.ioengine);
433 td->io_ops = load_ioengine(td, engine);
435 log_err("fio: failed to load engine %s\n", engine);
439 if (td->o.use_thread)
445 td->io_ops->flags |= FIO_RAWIO;
448 if (!td->o.filename && !td->files_index) {
451 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
452 add_file(td, jobname);
454 for (i = 0; i < td->o.nr_files; i++) {
455 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
461 if (fixup_options(td))
464 if (td->io_ops->flags & FIO_DISKLESSIO) {
467 for_each_file(td, f, i)
468 f->real_file_size = -1ULL;
471 td->mutex = fio_mutex_init(0);
473 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
474 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
475 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
476 td->ddir_nr = td->o.ddir_nr;
478 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
479 && prev_group_jobs) {
484 td->groupid = groupid;
487 if (init_random_state(td))
493 if (td->o.write_lat_log) {
494 setup_log(&td->ts.slat_log);
495 setup_log(&td->ts.clat_log);
497 if (td->o.write_bw_log)
498 setup_log(&td->ts.bw_log);
501 td->o.name = strdup(jobname);
505 if (!strcmp(td->io_ops->name, "cpuio"))
506 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
508 char *c1, *c2, *c3, *c4;
510 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
511 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
512 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
513 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
515 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);
522 } else if (job_add_num == 1)
527 * recurse add identical jobs, clear numjobs and stonewall options
528 * as they don't apply to sub-jobs
530 numjobs = td->o.numjobs;
532 struct thread_data *td_new = get_new_job(0, td);
537 td_new->o.numjobs = 1;
538 td_new->o.stonewall = 0;
539 td_new->o.new_group = 0;
542 td_new->o.filename = NULL;
543 td_new->files_index = 0;
544 td_new->files = NULL;
547 job_add_num = numjobs - 1;
549 if (add_job(td_new, jobname, job_add_num))
559 static int skip_this_section(const char *name)
563 if (!strncmp(name, "global", 6))
566 return strncmp(job_section, name, strlen(job_section));
569 static int is_empty_or_comment(char *line)
573 for (i = 0; i < strlen(line); i++) {
578 if (!isspace(line[i]) && !iscntrl(line[i]))
586 * This is our [ini] type file parser.
588 static int parse_jobs_ini(char *file, int stonewall_flag)
591 struct thread_data *td;
595 int ret = 0, stonewall;
600 if (!strcmp(file, "-"))
603 f = fopen(file, "r");
606 perror("fopen job file");
610 string = malloc(4096);
613 * it's really 256 + small bit, 280 should suffice
616 memset(name, 0, 280);
618 stonewall = stonewall_flag;
621 * if skip_fgets is set, we already have loaded a line we
625 p = fgets(string, 4095, f);
631 strip_blank_front(&p);
634 if (is_empty_or_comment(p))
636 if (sscanf(p, "[%255s]", name) != 1) {
639 log_err("fio: option <%s> outside of [] job section\n", p);
643 if (skip_this_section(name)) {
649 global = !strncmp(name, "global", 6);
651 name[strlen(name) - 1] = '\0';
657 log_info("--name=%s ", name);
661 td = get_new_job(global, &def_thread);
668 * Seperate multiple job files by a stonewall
670 if (!global && stonewall) {
671 td->o.stonewall = stonewall;
675 while ((p = fgets(string, 4096, f)) != NULL) {
676 if (is_empty_or_comment(p))
679 strip_blank_front(&p);
682 * new section, break out and make sure we don't
683 * fgets() a new line at the top.
693 * Don't break here, continue parsing options so we
694 * dump all the bad ones. Makes trial/error fixups
695 * easier on the user.
697 ret |= fio_option_parse(td, p);
698 if (!ret && dump_cmdline)
699 log_info("--%s ", p);
703 ret = add_job(td, name, 0);
705 log_err("fio: job %s dropped\n", name);
720 static int fill_def_thread(void)
722 memset(&def_thread, 0, sizeof(def_thread));
724 fio_getaffinity(getpid(), &def_thread.o.cpumask);
727 * fill default options
729 fio_fill_default_options(&def_thread);
731 def_thread.o.timeout = def_timeout;
732 def_thread.o.write_bw_log = write_bw_log;
733 def_thread.o.write_lat_log = write_lat_log;
738 static void free_shm(void)
740 struct shmid_ds sbuf;
743 shmdt((void *) threads);
745 shmctl(shm_id, IPC_RMID, &sbuf);
752 * The thread area is shared between the main process and the job
753 * threads/processes. So setup a shared memory segment that will hold
754 * all the job info. We use the end of the region for keeping track of
755 * open files across jobs, for file sharing.
757 static int setup_thread_area(void)
762 * 1024 is too much on some machines, scale max_jobs if
763 * we get a failure that looks like too large a shm segment
766 size_t size = max_jobs * sizeof(struct thread_data);
768 size += file_hash_size;
770 shm_id = shmget(0, size, IPC_CREAT | 0600);
773 if (errno != EINVAL) {
784 threads = shmat(shm_id, NULL, 0);
785 if (threads == (void *) -1) {
790 memset(threads, 0, max_jobs * sizeof(struct thread_data));
791 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
792 file_hash_init(hash);
797 static void usage(const char *name)
799 printf("%s\n", fio_version_string);
800 printf("%s [options] [job options] <job file(s)>\n", name);
801 printf("\t--debug=options\tEnable debug logging\n");
802 printf("\t--output\tWrite output to file\n");
803 printf("\t--timeout\tRuntime in seconds\n");
804 printf("\t--latency-log\tGenerate per-job latency logs\n");
805 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
806 printf("\t--minimal\tMinimal (terse) output\n");
807 printf("\t--version\tPrint version info and exit\n");
808 printf("\t--help\t\tPrint this page\n");
809 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
810 printf("\t--showcmd\tTurn a job file into command line options\n");
811 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
812 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
813 printf("\t--readonly\tTurn on safety read-only checks, preventing writes\n");
814 printf("\t--section=name\tOnly run specified section in job file\n");
818 struct debug_level debug_levels[] = {
819 { .name = "process", .shift = FD_PROCESS, },
820 { .name = "file", .shift = FD_FILE, },
821 { .name = "io", .shift = FD_IO, },
822 { .name = "mem", .shift = FD_MEM, },
823 { .name = "blktrace", .shift = FD_BLKTRACE },
824 { .name = "verify", .shift = FD_VERIFY },
825 { .name = "random", .shift = FD_RANDOM },
826 { .name = "parse", .shift = FD_PARSE },
830 static int set_debug(const char *string)
832 struct debug_level *dl;
833 char *p = (char *) string;
837 if (!strcmp(string, "?") || !strcmp(string, "help")) {
840 log_info("fio: dumping debug options:");
841 for (i = 0; debug_levels[i].name; i++) {
842 dl = &debug_levels[i];
843 log_info("%s,", dl->name);
847 } else if (!strcmp(string, "all")) {
852 while ((opt = strsep(&p, ",")) != NULL) {
855 for (i = 0; debug_levels[i].name; i++) {
856 dl = &debug_levels[i];
857 if (!strncmp(opt, dl->name, strlen(opt))) {
858 log_info("fio: set debug option %s\n", opt);
860 fio_debug |= (1UL << dl->shift);
866 log_err("fio: debug mask %s not found\n", opt);
871 static void set_debug(const char *string)
873 log_err("fio: debug tracing not included in build\n");
878 static int parse_cmd_line(int argc, char *argv[])
880 struct thread_data *td = NULL;
881 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
883 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
886 def_timeout = atoi(optarg);
895 f_out = fopen(optarg, "w+");
897 perror("fopen output");
909 exit(fio_show_option_help(optarg));
917 printf("%s\n", fio_version_string);
920 if (!strcmp("always", optarg))
921 eta_print = FIO_ETA_ALWAYS;
922 else if (!strcmp("never", optarg))
923 eta_print = FIO_ETA_NEVER;
926 if (set_debug(optarg))
930 if (!strcmp(optarg, "global")) {
931 log_err("fio: can't use global as only section\n");
938 job_section = strdup(optarg);
940 case FIO_GETOPT_JOB: {
941 const char *opt = long_options[lidx].name;
944 if (!strncmp(opt, "name", 4) && td) {
945 ret = add_job(td, td->o.name ?: "fio", 0);
953 int is_section = !strncmp(opt, "name", 4);
956 if (!is_section || !strncmp(val, "global", 6))
959 if (is_section && skip_this_section(val))
962 td = get_new_job(global, &def_thread);
967 ret = fio_cmd_option_parse(td, opt, val);
982 ret = add_job(td, td->o.name ?: "fio", 0);
987 while (optind < argc) {
989 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
990 ini_file[ini_idx - 1] = strdup(argv[optind]);
997 int parse_options(int argc, char *argv[])
1004 fio_options_dup_and_init(long_options);
1006 if (setup_thread_area())
1008 if (fill_def_thread())
1011 job_files = parse_cmd_line(argc, argv);
1013 for (i = 0; i < job_files; i++) {
1014 if (fill_def_thread())
1016 if (parse_jobs_ini(ini_file[i], i))
1022 options_mem_free(&def_thread);
1024 if (!thread_number) {
1028 log_err("No jobs defined(s)\n");