2 * This file contains job initialization and setup functions.
14 #include <sys/types.h>
20 static char fio_version_string[] = "fio 1.14a";
22 #define FIO_RANDSEED (0xb1899bedUL)
24 static char **ini_file;
25 static int max_jobs = MAX_JOBS;
27 struct thread_data def_thread;
28 struct thread_data *threads = NULL;
30 int exitall_on_terminate = 0;
32 unsigned long long mlock_size = 0;
38 static int def_timeout = 0;
39 static int write_lat_log = 0;
41 static int prev_group_jobs;
44 * Command line options. These will contain the above, plus a few
45 * extra that only pertain to fio itself and not jobs.
47 static struct option long_options[FIO_NR_OPTIONS] = {
50 .has_arg = required_argument,
55 .has_arg = required_argument,
59 .name = "latency-log",
60 .has_arg = required_argument,
64 .name = "bandwidth-log",
65 .has_arg = required_argument,
70 .has_arg = optional_argument,
75 .has_arg = no_argument,
80 .has_arg = no_argument,
85 .has_arg = optional_argument,
104 * Return a free job structure.
106 static struct thread_data *get_new_job(int global, struct thread_data *parent)
108 struct thread_data *td;
112 if (thread_number >= max_jobs)
115 td = &threads[thread_number++];
118 td->thread_number = thread_number;
122 static void put_job(struct thread_data *td)
124 if (td == &def_thread)
128 log_info("fio: %s\n", td->verror);
130 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
134 static int setup_rate(struct thread_data *td)
136 unsigned long nr_reads_per_msec;
137 unsigned long long rate;
140 if (!td->o.rate && !td->o.rate_iops)
144 bs = td->o.rw_min_bs;
145 else if (td_read(td))
146 bs = td->o.min_bs[DDIR_READ];
148 bs = td->o.min_bs[DDIR_WRITE];
152 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
154 nr_reads_per_msec = td->o.rate_iops * 1000UL;
156 if (!nr_reads_per_msec) {
157 log_err("rate lower than supported\n");
161 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
162 td->rate_pending_usleep = 0;
167 * Lazy way of fixing up options that depend on each other. We could also
168 * define option callback handlers, but this is easier.
170 static int fixup_options(struct thread_data *td)
172 struct thread_options *o = &td->o;
174 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
175 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
177 if (o->write_iolog_file && o->read_iolog_file) {
178 log_err("fio: read iolog overrides write_iolog\n");
179 free(o->write_iolog_file);
180 o->write_iolog_file = NULL;
183 if (td->io_ops->flags & FIO_SYNCIO)
187 o->iodepth = o->open_files;
191 * only really works for sequential io for now, and with 1 file
193 if (o->zone_size && td_random(td) && o->open_files == 1)
197 * Reads can do overwrites, we always need to pre-create the file
199 if (td_read(td) || td_rw(td))
202 if (!o->min_bs[DDIR_READ])
203 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
204 if (!o->max_bs[DDIR_READ])
205 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
206 if (!o->min_bs[DDIR_WRITE])
207 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
208 if (!o->max_bs[DDIR_WRITE])
209 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
211 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
213 if (!o->file_size_high)
214 o->file_size_high = o->file_size_low;
216 if (td_read(td) && !td_rw(td))
219 if (o->norandommap && o->verify != VERIFY_NONE) {
220 log_err("fio: norandommap given, verify disabled\n");
221 o->verify = VERIFY_NONE;
223 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
224 log_err("fio: bs_unaligned may not work with raw io\n");
227 * thinktime_spin must be less than thinktime
229 if (o->thinktime_spin > o->thinktime)
230 o->thinktime_spin = o->thinktime;
233 * The low water mark cannot be bigger than the iodepth
235 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
237 * syslet work around - if the workload is sequential,
238 * we want to let the queue drain all the way down to
239 * avoid seeking between async threads
241 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
244 o->iodepth_low = o->iodepth;
248 * If batch number isn't set, default to the same as iodepth
250 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
251 o->iodepth_batch = o->iodepth;
253 if (o->nr_files > td->files_index)
254 o->nr_files = td->files_index;
256 if (o->open_files > o->nr_files || !o->open_files)
257 o->open_files = o->nr_files;
259 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
260 log_err("fio: rate and rate_iops are mutually exclusive\n");
263 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
264 log_err("fio: minimum rate exceeds rate\n");
272 * This function leaks the buffer
274 static char *to_kmg(unsigned int val)
276 char *buf = malloc(32);
277 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
288 snprintf(buf, 31, "%u%c", val, *p);
292 /* External engines are specified by "external:name.o") */
293 static const char *get_engine_name(const char *str)
295 char *p = strstr(str, ":");
301 strip_blank_front(&p);
306 static int exists_and_not_file(const char *filename)
310 if (lstat(filename, &sb) == -1)
313 if (S_ISREG(sb.st_mode))
320 * Initialize the various random states we need (random io, block size ranges,
321 * read/write mix, etc).
323 static int init_random_state(struct thread_data *td)
325 unsigned long seeds[6];
328 fd = open("/dev/urandom", O_RDONLY);
330 td_verror(td, errno, "open");
334 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
335 td_verror(td, EIO, "read");
342 os_random_seed(seeds[0], &td->bsrange_state);
343 os_random_seed(seeds[1], &td->verify_state);
344 os_random_seed(seeds[2], &td->rwmix_state);
346 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
347 os_random_seed(seeds[3], &td->next_file_state);
349 os_random_seed(seeds[5], &td->file_size_state);
354 if (td->o.rand_repeatable)
355 seeds[4] = FIO_RANDSEED * td->thread_number;
357 os_random_seed(seeds[4], &td->random_state);
362 * Adds a job to the list of things todo. Sanitizes the various options
363 * to make sure we don't have conflicts, and initializes various
366 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
368 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
369 "randread", "randwrite", "randrw" };
372 char fname[PATH_MAX];
373 int numjobs, file_alloced;
376 * the def_thread is just for options, it's not a real job
378 if (td == &def_thread)
381 engine = get_engine_name(td->o.ioengine);
382 td->io_ops = load_ioengine(td, engine);
384 log_err("fio: failed to load engine %s\n", engine);
388 if (td->o.use_thread)
394 td->io_ops->flags |= FIO_RAWIO;
397 if (!td->o.filename && !td->files_index) {
400 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
401 add_file(td, jobname);
403 for (i = 0; i < td->o.nr_files; i++) {
404 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
410 if (fixup_options(td))
413 td->mutex = fio_sem_init(0);
415 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
416 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
417 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
418 td->ddir_nr = td->o.ddir_nr;
420 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
421 && prev_group_jobs) {
426 td->groupid = groupid;
429 if (init_random_state(td))
435 if (td->o.write_lat_log) {
436 setup_log(&td->ts.slat_log);
437 setup_log(&td->ts.clat_log);
439 if (td->o.write_bw_log)
440 setup_log(&td->ts.bw_log);
443 td->o.name = strdup(jobname);
447 if (!strcmp(td->io_ops->name, "cpuio"))
448 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
450 char *c1, *c2, *c3, *c4;
452 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
453 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
454 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
455 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
457 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);
464 } else if (job_add_num == 1)
469 * recurse add identical jobs, clear numjobs and stonewall options
470 * as they don't apply to sub-jobs
472 numjobs = td->o.numjobs;
474 struct thread_data *td_new = get_new_job(0, td);
479 td_new->o.numjobs = 1;
480 td_new->o.stonewall = 0;
483 td_new->o.filename = NULL;
484 td_new->files_index = 0;
485 td_new->files = NULL;
488 job_add_num = numjobs - 1;
490 if (add_job(td_new, jobname, job_add_num))
500 static int is_empty_or_comment(char *line)
504 for (i = 0; i < strlen(line); i++) {
509 if (!isspace(line[i]) && !iscntrl(line[i]))
517 * This is our [ini] type file parser.
519 static int parse_jobs_ini(char *file, int stonewall_flag)
522 struct thread_data *td;
527 int ret = 0, stonewall;
529 f = fopen(file, "r");
531 perror("fopen job file");
535 string = malloc(4096);
537 memset(name, 0, 256);
539 stonewall = stonewall_flag;
541 p = fgets(string, 4095, f);
544 if (is_empty_or_comment(p))
546 if (sscanf(p, "[%255s]", name) != 1)
549 global = !strncmp(name, "global", 6);
551 name[strlen(name) - 1] = '\0';
553 td = get_new_job(global, &def_thread);
560 * Seperate multiple job files by a stonewall
562 if (!global && stonewall) {
563 td->o.stonewall = stonewall;
568 while ((p = fgets(string, 4096, f)) != NULL) {
569 if (is_empty_or_comment(p))
572 strip_blank_front(&p);
582 * Don't break here, continue parsing options so we
583 * dump all the bad ones. Makes trial/error fixups
584 * easier on the user.
586 ret |= fio_option_parse(td, p);
591 ret = add_job(td, name, 0);
593 log_err("fio: job %s dropped\n", name);
604 static int fill_def_thread(void)
606 memset(&def_thread, 0, sizeof(def_thread));
608 if (fio_getaffinity(getpid(), &def_thread.o.cpumask) == -1) {
609 perror("sched_getaffinity");
614 * fill default options
616 fio_fill_default_options(&def_thread);
618 def_thread.o.timeout = def_timeout;
619 def_thread.o.write_bw_log = write_bw_log;
620 def_thread.o.write_lat_log = write_lat_log;
622 #ifdef FIO_HAVE_DISK_UTIL
623 def_thread.o.do_disk_util = 1;
629 static void free_shm(void)
631 struct shmid_ds sbuf;
634 shmdt((void *) threads);
636 shmctl(shm_id, IPC_RMID, &sbuf);
641 * The thread area is shared between the main process and the job
642 * threads/processes. So setup a shared memory segment that will hold
645 static int setup_thread_area(void)
648 * 1024 is too much on some machines, scale max_jobs if
649 * we get a failure that looks like too large a shm segment
652 size_t size = max_jobs * sizeof(struct thread_data);
654 shm_id = shmget(0, size, IPC_CREAT | 0600);
657 if (errno != EINVAL) {
668 threads = shmat(shm_id, NULL, 0);
669 if (threads == (void *) -1) {
678 static void usage(void)
680 printf("%s\n", fio_version_string);
681 printf("\t--output\tWrite output to file\n");
682 printf("\t--timeout\tRuntime in seconds\n");
683 printf("\t--latency-log\tGenerate per-job latency logs\n");
684 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
685 printf("\t--minimal\tMinimal (terse) output\n");
686 printf("\t--version\tPrint version info and exit\n");
687 printf("\t--help\t\tPrint this page\n");
688 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
691 static int parse_cmd_line(int argc, char *argv[])
693 struct thread_data *td = NULL;
694 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
696 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
699 def_timeout = atoi(optarg);
708 f_out = fopen(optarg, "w+");
710 perror("fopen output");
722 exit(fio_show_option_help(optarg));
724 printf("%s\n", fio_version_string);
726 case FIO_GETOPT_JOB: {
727 const char *opt = long_options[lidx].name;
730 if (!strncmp(opt, "name", 4) && td) {
731 ret = add_job(td, td->o.name ?: "fio", 0);
739 int global = !strncmp(val, "global", 6);
741 td = get_new_job(global, &def_thread);
746 ret = fio_cmd_option_parse(td, opt, val);
760 ret = add_job(td, td->o.name ?: "fio", 0);
766 while (optind < argc) {
768 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
769 ini_file[ini_idx - 1] = strdup(argv[optind]);
777 int parse_options(int argc, char *argv[])
784 fio_options_dup_and_init(long_options);
786 if (setup_thread_area())
788 if (fill_def_thread())
791 job_files = parse_cmd_line(argc, argv);
793 for (i = 0; i < job_files; i++) {
794 if (fill_def_thread())
796 if (parse_jobs_ini(ini_file[i], i))
803 if (!thread_number) {
804 log_err("No jobs defined(s)\n");