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));
135 * Lazy way of fixing up options that depend on each other. We could also
136 * define option callback handlers, but this is easier.
138 static int fixup_options(struct thread_data *td)
140 if (!td->rwmixread && td->rwmixwrite)
141 td->rwmixread = 100 - td->rwmixwrite;
143 if (td->write_iolog_file && td->read_iolog_file) {
144 log_err("fio: read iolog overrides write_iolog\n");
145 free(td->write_iolog_file);
146 td->write_iolog_file = NULL;
149 if (td->io_ops->flags & FIO_SYNCIO)
153 td->iodepth = td->open_files;
157 * only really works for sequential io for now, and with 1 file
159 if (td->zone_size && td_random(td) && td->open_files == 1)
163 * Reads can do overwrites, we always need to pre-create the file
165 if (td_read(td) || td_rw(td))
168 if (!td->min_bs[DDIR_READ])
169 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
170 if (!td->max_bs[DDIR_READ])
171 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
172 if (!td->min_bs[DDIR_WRITE])
173 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
174 if (!td->max_bs[DDIR_WRITE])
175 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
177 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
179 if (!td->file_size_high)
180 td->file_size_high = td->file_size_low;
182 if (td_read(td) && !td_rw(td))
185 if (td->norandommap && td->verify != VERIFY_NONE) {
186 log_err("fio: norandommap given, verify disabled\n");
187 td->verify = VERIFY_NONE;
189 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
190 log_err("fio: bs_unaligned may not work with raw io\n");
193 * thinktime_spin must be less than thinktime
195 if (td->thinktime_spin > td->thinktime)
196 td->thinktime_spin = td->thinktime;
199 * The low water mark cannot be bigger than the iodepth
201 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
203 * syslet work around - if the workload is sequential,
204 * we want to let the queue drain all the way down to
205 * avoid seeking between async threads
207 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
210 td->iodepth_low = td->iodepth;
214 * If batch number isn't set, default to the same as iodepth
216 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
217 td->iodepth_batch = td->iodepth;
219 if (td->nr_files > td->files_index)
220 td->nr_files = td->files_index;
222 if (td->open_files > td->nr_files || !td->open_files)
223 td->open_files = td->nr_files;
225 if ((td->rate && td->rate_iops) || (td->ratemin && td->rate_iops_min)) {
226 log_err("fio: rate and rate_iops are mutually exclusive\n");
229 if ((td->rate < td->ratemin) || (td->rate_iops < td->rate_iops_min)) {
230 log_err("fio: minimum rate exceeds rate\n");
238 * This function leaks the buffer
240 static char *to_kmg(unsigned int val)
242 char *buf = malloc(32);
243 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
254 snprintf(buf, 31, "%u%c", val, *p);
258 /* External engines are specified by "external:name.o") */
259 static const char *get_engine_name(const char *str)
261 char *p = strstr(str, ":");
267 strip_blank_front(&p);
272 static int exists_and_not_file(const char *filename)
276 if (lstat(filename, &sb) == -1)
279 if (S_ISREG(sb.st_mode))
286 * Initialize the various random states we need (random io, block size ranges,
287 * read/write mix, etc).
289 static int init_random_state(struct thread_data *td)
291 unsigned long seeds[6];
292 int fd, num_maps, blocks;
296 fd = open("/dev/urandom", O_RDONLY);
298 td_verror(td, errno, "open");
302 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
303 td_verror(td, EIO, "read");
310 os_random_seed(seeds[0], &td->bsrange_state);
311 os_random_seed(seeds[1], &td->verify_state);
312 os_random_seed(seeds[2], &td->rwmix_state);
314 if (td->file_service_type == FIO_FSERVICE_RANDOM)
315 os_random_seed(seeds[3], &td->next_file_state);
317 os_random_seed(seeds[5], &td->file_size_state);
322 if (td->rand_repeatable)
323 seeds[4] = FIO_RANDSEED * td->thread_number;
325 if (!td->norandommap) {
326 for_each_file(td, f, i) {
327 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
328 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
329 f->file_map = malloc(num_maps * sizeof(long));
331 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
334 f->num_maps = num_maps;
335 memset(f->file_map, 0, num_maps * sizeof(long));
339 os_random_seed(seeds[4], &td->random_state);
345 * Adds a job to the list of things todo. Sanitizes the various options
346 * to make sure we don't have conflicts, and initializes various
349 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
351 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
352 "randread", "randwrite", "randrw" };
356 char fname[PATH_MAX];
357 int numjobs, file_alloced;
360 * the def_thread is just for options, it's not a real job
362 if (td == &def_thread)
365 engine = get_engine_name(td->ioengine);
366 td->io_ops = load_ioengine(td, engine);
368 log_err("fio: failed to load engine %s\n", engine);
378 td->io_ops->flags |= FIO_RAWIO;
381 if (!td->filename && !td->files_index) {
384 if (td->nr_files == 1 && exists_and_not_file(jobname))
385 add_file(td, jobname);
387 for (i = 0; i < td->nr_files; i++) {
388 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
394 if (fixup_options(td))
397 for_each_file(td, f, i) {
398 if (td->directory && f->filetype == FIO_TYPE_FILE) {
399 sprintf(fname, "%s/%s", td->directory, f->file_name);
400 f->file_name = strdup(fname);
404 td->mutex = fio_sem_init(0);
406 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
407 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
408 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
410 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
415 td->groupid = groupid;
418 if (init_random_state(td))
424 if (td->write_lat_log) {
425 setup_log(&td->ts.slat_log);
426 setup_log(&td->ts.clat_log);
428 if (td->write_bw_log)
429 setup_log(&td->ts.bw_log);
432 td->name = strdup(jobname);
436 if (!strcmp(td->io_ops->name, "cpuio"))
437 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
439 char *c1, *c2, *c3, *c4;
441 c1 = to_kmg(td->min_bs[DDIR_READ]);
442 c2 = to_kmg(td->max_bs[DDIR_READ]);
443 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
444 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
446 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);
453 } else if (job_add_num == 1)
458 * recurse add identical jobs, clear numjobs and stonewall options
459 * as they don't apply to sub-jobs
461 numjobs = td->numjobs;
463 struct thread_data *td_new = get_new_job(0, td);
469 td_new->stonewall = 0;
472 td_new->filename = NULL;
473 td_new->files_index = 0;
474 td_new->files = NULL;
477 job_add_num = numjobs - 1;
479 if (add_job(td_new, jobname, job_add_num))
483 if (td->numjobs > 1) {
494 static int is_empty_or_comment(char *line)
498 for (i = 0; i < strlen(line); i++) {
503 if (!isspace(line[i]) && !iscntrl(line[i]))
511 * This is our [ini] type file parser.
513 static int parse_jobs_ini(char *file, int stonewall_flag)
516 struct thread_data *td;
521 int ret = 0, stonewall;
523 f = fopen(file, "r");
525 perror("fopen job file");
529 string = malloc(4096);
531 memset(name, 0, 256);
533 stonewall = stonewall_flag;
535 p = fgets(string, 4095, f);
538 if (is_empty_or_comment(p))
540 if (sscanf(p, "[%255s]", name) != 1)
543 global = !strncmp(name, "global", 6);
545 name[strlen(name) - 1] = '\0';
547 td = get_new_job(global, &def_thread);
554 * Seperate multiple job files by a stonewall
556 if (!global && stonewall) {
557 td->stonewall = stonewall;
562 while ((p = fgets(string, 4096, f)) != NULL) {
563 if (is_empty_or_comment(p))
566 strip_blank_front(&p);
576 * Don't break here, continue parsing options so we
577 * dump all the bad ones. Makes trial/error fixups
578 * easier on the user.
580 ret |= fio_option_parse(td, p);
585 ret = add_job(td, name, 0);
587 log_err("fio: job %s dropped\n", name);
598 static int fill_def_thread(void)
600 memset(&def_thread, 0, sizeof(def_thread));
602 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
603 perror("sched_getaffinity");
608 * fill default options
610 fio_fill_default_options(&def_thread);
612 def_thread.timeout = def_timeout;
613 def_thread.write_bw_log = write_bw_log;
614 def_thread.write_lat_log = write_lat_log;
616 #ifdef FIO_HAVE_DISK_UTIL
617 def_thread.do_disk_util = 1;
623 static void free_shm(void)
625 struct shmid_ds sbuf;
628 shmdt((void *) threads);
630 shmctl(shm_id, IPC_RMID, &sbuf);
635 * The thread area is shared between the main process and the job
636 * threads/processes. So setup a shared memory segment that will hold
639 static int setup_thread_area(void)
642 * 1024 is too much on some machines, scale max_jobs if
643 * we get a failure that looks like too large a shm segment
646 size_t size = max_jobs * sizeof(struct thread_data);
648 shm_id = shmget(0, size, IPC_CREAT | 0600);
651 if (errno != EINVAL) {
662 threads = shmat(shm_id, NULL, 0);
663 if (threads == (void *) -1) {
672 static void usage(void)
674 printf("%s\n", fio_version_string);
675 printf("\t--output\tWrite output to file\n");
676 printf("\t--timeout\tRuntime in seconds\n");
677 printf("\t--latency-log\tGenerate per-job latency logs\n");
678 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
679 printf("\t--minimal\tMinimal (terse) output\n");
680 printf("\t--version\tPrint version info and exit\n");
681 printf("\t--help\t\tPrint this page\n");
682 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
685 static int parse_cmd_line(int argc, char *argv[])
687 struct thread_data *td = NULL;
688 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
690 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
693 def_timeout = atoi(optarg);
702 f_out = fopen(optarg, "w+");
704 perror("fopen output");
716 exit(fio_show_option_help(optarg));
718 printf("%s\n", fio_version_string);
720 case FIO_GETOPT_JOB: {
721 const char *opt = long_options[lidx].name;
724 if (!strncmp(opt, "name", 4) && td) {
725 ret = add_job(td, td->name ?: "fio", 0);
733 int global = !strncmp(val, "global", 6);
735 td = get_new_job(global, &def_thread);
740 ret = fio_cmd_option_parse(td, opt, val);
754 ret = add_job(td, td->name ?: "fio", 0);
760 while (optind < argc) {
762 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
763 ini_file[ini_idx - 1] = strdup(argv[optind]);
771 int parse_options(int argc, char *argv[])
778 fio_options_dup_and_init(long_options);
780 if (setup_thread_area())
782 if (fill_def_thread())
785 job_files = parse_cmd_line(argc, argv);
787 for (i = 0; i < job_files; i++) {
788 if (fill_def_thread())
790 if (parse_jobs_ini(ini_file[i], i))
797 if (!thread_number) {
798 log_err("No jobs defined(s)\n");