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->rate && !td->rate_iops)
145 else if (td_read(td))
146 bs = td->min_bs[DDIR_READ];
148 bs = td->min_bs[DDIR_WRITE];
152 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
154 nr_reads_per_msec = td->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 if (!td->rwmixread && td->rwmixwrite)
173 td->rwmixread = 100 - td->rwmixwrite;
175 if (td->write_iolog_file && td->read_iolog_file) {
176 log_err("fio: read iolog overrides write_iolog\n");
177 free(td->write_iolog_file);
178 td->write_iolog_file = NULL;
181 if (td->io_ops->flags & FIO_SYNCIO)
185 td->iodepth = td->open_files;
189 * only really works for sequential io for now, and with 1 file
191 if (td->zone_size && td_random(td) && td->open_files == 1)
195 * Reads can do overwrites, we always need to pre-create the file
197 if (td_read(td) || td_rw(td))
200 if (!td->min_bs[DDIR_READ])
201 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
202 if (!td->max_bs[DDIR_READ])
203 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
204 if (!td->min_bs[DDIR_WRITE])
205 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
206 if (!td->max_bs[DDIR_WRITE])
207 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
209 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
211 if (!td->file_size_high)
212 td->file_size_high = td->file_size_low;
214 if (td_read(td) && !td_rw(td))
217 if (td->norandommap && td->verify != VERIFY_NONE) {
218 log_err("fio: norandommap given, verify disabled\n");
219 td->verify = VERIFY_NONE;
221 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
222 log_err("fio: bs_unaligned may not work with raw io\n");
225 * thinktime_spin must be less than thinktime
227 if (td->thinktime_spin > td->thinktime)
228 td->thinktime_spin = td->thinktime;
231 * The low water mark cannot be bigger than the iodepth
233 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
235 * syslet work around - if the workload is sequential,
236 * we want to let the queue drain all the way down to
237 * avoid seeking between async threads
239 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
242 td->iodepth_low = td->iodepth;
246 * If batch number isn't set, default to the same as iodepth
248 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
249 td->iodepth_batch = td->iodepth;
251 if (td->nr_files > td->files_index)
252 td->nr_files = td->files_index;
254 if (td->open_files > td->nr_files || !td->open_files)
255 td->open_files = td->nr_files;
257 if ((td->rate && td->rate_iops) || (td->ratemin && td->rate_iops_min)) {
258 log_err("fio: rate and rate_iops are mutually exclusive\n");
261 if ((td->rate < td->ratemin) || (td->rate_iops < td->rate_iops_min)) {
262 log_err("fio: minimum rate exceeds rate\n");
270 * This function leaks the buffer
272 static char *to_kmg(unsigned int val)
274 char *buf = malloc(32);
275 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
286 snprintf(buf, 31, "%u%c", val, *p);
290 /* External engines are specified by "external:name.o") */
291 static const char *get_engine_name(const char *str)
293 char *p = strstr(str, ":");
299 strip_blank_front(&p);
304 static int exists_and_not_file(const char *filename)
308 if (lstat(filename, &sb) == -1)
311 if (S_ISREG(sb.st_mode))
318 * Initialize the various random states we need (random io, block size ranges,
319 * read/write mix, etc).
321 static int init_random_state(struct thread_data *td)
323 unsigned long seeds[6];
324 int fd, num_maps, blocks;
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->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->rand_repeatable)
355 seeds[4] = FIO_RANDSEED * td->thread_number;
357 if (!td->norandommap) {
358 for_each_file(td, f, i) {
359 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
360 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
361 f->file_map = malloc(num_maps * sizeof(long));
363 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
366 f->num_maps = num_maps;
367 memset(f->file_map, 0, num_maps * sizeof(long));
371 os_random_seed(seeds[4], &td->random_state);
377 * Adds a job to the list of things todo. Sanitizes the various options
378 * to make sure we don't have conflicts, and initializes various
381 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
383 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
384 "randread", "randwrite", "randrw" };
388 char fname[PATH_MAX];
389 int numjobs, file_alloced;
392 * the def_thread is just for options, it's not a real job
394 if (td == &def_thread)
397 engine = get_engine_name(td->ioengine);
398 td->io_ops = load_ioengine(td, engine);
400 log_err("fio: failed to load engine %s\n", engine);
410 td->io_ops->flags |= FIO_RAWIO;
413 if (!td->filename && !td->files_index) {
416 if (td->nr_files == 1 && exists_and_not_file(jobname))
417 add_file(td, jobname);
419 for (i = 0; i < td->nr_files; i++) {
420 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
426 if (fixup_options(td))
429 for_each_file(td, f, i) {
430 if (td->directory && f->filetype == FIO_TYPE_FILE) {
431 sprintf(fname, "%s/%s", td->directory, f->file_name);
432 f->file_name = strdup(fname);
436 td->mutex = fio_sem_init(0);
438 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
439 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
440 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
442 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
447 td->groupid = groupid;
450 if (init_random_state(td))
456 if (td->write_lat_log) {
457 setup_log(&td->ts.slat_log);
458 setup_log(&td->ts.clat_log);
460 if (td->write_bw_log)
461 setup_log(&td->ts.bw_log);
464 td->name = strdup(jobname);
468 if (!strcmp(td->io_ops->name, "cpuio"))
469 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
471 char *c1, *c2, *c3, *c4;
473 c1 = to_kmg(td->min_bs[DDIR_READ]);
474 c2 = to_kmg(td->max_bs[DDIR_READ]);
475 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
476 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
478 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[td->td_ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
485 } else if (job_add_num == 1)
490 * recurse add identical jobs, clear numjobs and stonewall options
491 * as they don't apply to sub-jobs
493 numjobs = td->numjobs;
495 struct thread_data *td_new = get_new_job(0, td);
501 td_new->stonewall = 0;
504 td_new->filename = NULL;
505 td_new->files_index = 0;
506 td_new->files = NULL;
509 job_add_num = numjobs - 1;
511 if (add_job(td_new, jobname, job_add_num))
515 if (td->numjobs > 1) {
526 static int is_empty_or_comment(char *line)
530 for (i = 0; i < strlen(line); i++) {
535 if (!isspace(line[i]) && !iscntrl(line[i]))
543 * This is our [ini] type file parser.
545 static int parse_jobs_ini(char *file, int stonewall_flag)
548 struct thread_data *td;
553 int ret = 0, stonewall;
555 f = fopen(file, "r");
557 perror("fopen job file");
561 string = malloc(4096);
563 memset(name, 0, 256);
565 stonewall = stonewall_flag;
567 p = fgets(string, 4095, f);
570 if (is_empty_or_comment(p))
572 if (sscanf(p, "[%255s]", name) != 1)
575 global = !strncmp(name, "global", 6);
577 name[strlen(name) - 1] = '\0';
579 td = get_new_job(global, &def_thread);
586 * Seperate multiple job files by a stonewall
588 if (!global && stonewall) {
589 td->stonewall = stonewall;
594 while ((p = fgets(string, 4096, f)) != NULL) {
595 if (is_empty_or_comment(p))
598 strip_blank_front(&p);
608 * Don't break here, continue parsing options so we
609 * dump all the bad ones. Makes trial/error fixups
610 * easier on the user.
612 ret |= fio_option_parse(td, p);
617 ret = add_job(td, name, 0);
619 log_err("fio: job %s dropped\n", name);
630 static int fill_def_thread(void)
632 memset(&def_thread, 0, sizeof(def_thread));
634 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
635 perror("sched_getaffinity");
640 * fill default options
642 fio_fill_default_options(&def_thread);
644 def_thread.timeout = def_timeout;
645 def_thread.write_bw_log = write_bw_log;
646 def_thread.write_lat_log = write_lat_log;
648 #ifdef FIO_HAVE_DISK_UTIL
649 def_thread.do_disk_util = 1;
655 static void free_shm(void)
657 struct shmid_ds sbuf;
660 shmdt((void *) threads);
662 shmctl(shm_id, IPC_RMID, &sbuf);
667 * The thread area is shared between the main process and the job
668 * threads/processes. So setup a shared memory segment that will hold
671 static int setup_thread_area(void)
674 * 1024 is too much on some machines, scale max_jobs if
675 * we get a failure that looks like too large a shm segment
678 size_t size = max_jobs * sizeof(struct thread_data);
680 shm_id = shmget(0, size, IPC_CREAT | 0600);
683 if (errno != EINVAL) {
694 threads = shmat(shm_id, NULL, 0);
695 if (threads == (void *) -1) {
704 static void usage(void)
706 printf("%s\n", fio_version_string);
707 printf("\t--output\tWrite output to file\n");
708 printf("\t--timeout\tRuntime in seconds\n");
709 printf("\t--latency-log\tGenerate per-job latency logs\n");
710 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
711 printf("\t--minimal\tMinimal (terse) output\n");
712 printf("\t--version\tPrint version info and exit\n");
713 printf("\t--help\t\tPrint this page\n");
714 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
717 static int parse_cmd_line(int argc, char *argv[])
719 struct thread_data *td = NULL;
720 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
722 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
725 def_timeout = atoi(optarg);
734 f_out = fopen(optarg, "w+");
736 perror("fopen output");
748 exit(fio_show_option_help(optarg));
750 printf("%s\n", fio_version_string);
752 case FIO_GETOPT_JOB: {
753 const char *opt = long_options[lidx].name;
756 if (!strncmp(opt, "name", 4) && td) {
757 ret = add_job(td, td->name ?: "fio", 0);
765 int global = !strncmp(val, "global", 6);
767 td = get_new_job(global, &def_thread);
772 ret = fio_cmd_option_parse(td, opt, val);
786 ret = add_job(td, td->name ?: "fio", 0);
792 while (optind < argc) {
794 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
795 ini_file[ini_idx - 1] = strdup(argv[optind]);
803 int parse_options(int argc, char *argv[])
810 fio_options_dup_and_init(long_options);
812 if (setup_thread_area())
814 if (fill_def_thread())
817 job_files = parse_cmd_line(argc, argv);
819 for (i = 0; i < job_files; i++) {
820 if (fill_def_thread())
822 if (parse_jobs_ini(ini_file[i], i))
829 if (!thread_number) {
830 log_err("No jobs defined(s)\n");