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 void 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;
227 * This function leaks the buffer
229 static char *to_kmg(unsigned int val)
231 char *buf = malloc(32);
232 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
243 snprintf(buf, 31, "%u%c", val, *p);
247 /* External engines are specified by "external:name.o") */
248 static const char *get_engine_name(const char *str)
250 char *p = strstr(str, ":");
256 strip_blank_front(&p);
261 static int exists_and_not_file(const char *filename)
265 if (lstat(filename, &sb) == -1)
268 if (S_ISREG(sb.st_mode))
275 * Initialize the various random states we need (random io, block size ranges,
276 * read/write mix, etc).
278 static int init_random_state(struct thread_data *td)
280 unsigned long seeds[6];
281 int fd, num_maps, blocks;
285 fd = open("/dev/urandom", O_RDONLY);
287 td_verror(td, errno, "open");
291 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
292 td_verror(td, EIO, "read");
299 os_random_seed(seeds[0], &td->bsrange_state);
300 os_random_seed(seeds[1], &td->verify_state);
301 os_random_seed(seeds[2], &td->rwmix_state);
303 if (td->file_service_type == FIO_FSERVICE_RANDOM)
304 os_random_seed(seeds[3], &td->next_file_state);
306 os_random_seed(seeds[5], &td->file_size_state);
311 if (td->rand_repeatable)
312 seeds[4] = FIO_RANDSEED * td->thread_number;
314 if (!td->norandommap) {
315 for_each_file(td, f, i) {
316 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
317 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
318 f->file_map = malloc(num_maps * sizeof(long));
320 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
323 f->num_maps = num_maps;
324 memset(f->file_map, 0, num_maps * sizeof(long));
328 os_random_seed(seeds[4], &td->random_state);
334 * Adds a job to the list of things todo. Sanitizes the various options
335 * to make sure we don't have conflicts, and initializes various
338 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
340 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
341 "randread", "randwrite", "randrw" };
345 char fname[PATH_MAX];
346 int numjobs, file_alloced;
349 * the def_thread is just for options, it's not a real job
351 if (td == &def_thread)
354 engine = get_engine_name(td->ioengine);
355 td->io_ops = load_ioengine(td, engine);
357 log_err("fio: failed to load engine %s\n", engine);
367 td->io_ops->flags |= FIO_RAWIO;
370 if (!td->filename && !td->files_index) {
373 if (td->nr_files == 1 && exists_and_not_file(jobname))
374 add_file(td, jobname);
376 for (i = 0; i < td->nr_files; i++) {
377 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
385 for_each_file(td, f, i) {
386 if (td->directory && f->filetype == FIO_TYPE_FILE) {
387 sprintf(fname, "%s/%s", td->directory, f->file_name);
388 f->file_name = strdup(fname);
392 td->mutex = fio_sem_init(0);
394 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
395 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
396 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
398 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
403 td->groupid = groupid;
406 if (init_random_state(td))
412 if (td->write_lat_log) {
413 setup_log(&td->ts.slat_log);
414 setup_log(&td->ts.clat_log);
416 if (td->write_bw_log)
417 setup_log(&td->ts.bw_log);
420 td->name = strdup(jobname);
424 if (!strcmp(td->io_ops->name, "cpuio"))
425 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
427 char *c1, *c2, *c3, *c4;
429 c1 = to_kmg(td->min_bs[DDIR_READ]);
430 c2 = to_kmg(td->max_bs[DDIR_READ]);
431 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
432 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
434 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);
441 } else if (job_add_num == 1)
446 * recurse add identical jobs, clear numjobs and stonewall options
447 * as they don't apply to sub-jobs
449 numjobs = td->numjobs;
451 struct thread_data *td_new = get_new_job(0, td);
457 td_new->stonewall = 0;
460 td_new->filename = NULL;
461 td_new->files_index = 0;
462 td_new->files = NULL;
465 job_add_num = numjobs - 1;
467 if (add_job(td_new, jobname, job_add_num))
471 if (td->numjobs > 1) {
482 static int is_empty_or_comment(char *line)
486 for (i = 0; i < strlen(line); i++) {
491 if (!isspace(line[i]) && !iscntrl(line[i]))
499 * This is our [ini] type file parser.
501 static int parse_jobs_ini(char *file, int stonewall_flag)
504 struct thread_data *td;
509 int ret = 0, stonewall;
511 f = fopen(file, "r");
513 perror("fopen job file");
517 string = malloc(4096);
519 memset(name, 0, 256);
521 stonewall = stonewall_flag;
523 p = fgets(string, 4095, f);
526 if (is_empty_or_comment(p))
528 if (sscanf(p, "[%255s]", name) != 1)
531 global = !strncmp(name, "global", 6);
533 name[strlen(name) - 1] = '\0';
535 td = get_new_job(global, &def_thread);
542 * Seperate multiple job files by a stonewall
544 if (!global && stonewall) {
545 td->stonewall = stonewall;
550 while ((p = fgets(string, 4096, f)) != NULL) {
551 if (is_empty_or_comment(p))
554 strip_blank_front(&p);
564 * Don't break here, continue parsing options so we
565 * dump all the bad ones. Makes trial/error fixups
566 * easier on the user.
568 ret |= fio_option_parse(td, p);
573 ret = add_job(td, name, 0);
575 log_err("fio: job %s dropped\n", name);
586 static int fill_def_thread(void)
588 memset(&def_thread, 0, sizeof(def_thread));
590 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
591 perror("sched_getaffinity");
596 * fill default options
598 fio_fill_default_options(&def_thread);
600 def_thread.timeout = def_timeout;
601 def_thread.write_bw_log = write_bw_log;
602 def_thread.write_lat_log = write_lat_log;
604 #ifdef FIO_HAVE_DISK_UTIL
605 def_thread.do_disk_util = 1;
611 static void free_shm(void)
613 struct shmid_ds sbuf;
616 shmdt((void *) threads);
618 shmctl(shm_id, IPC_RMID, &sbuf);
623 * The thread area is shared between the main process and the job
624 * threads/processes. So setup a shared memory segment that will hold
627 static int setup_thread_area(void)
630 * 1024 is too much on some machines, scale max_jobs if
631 * we get a failure that looks like too large a shm segment
634 size_t size = max_jobs * sizeof(struct thread_data);
636 shm_id = shmget(0, size, IPC_CREAT | 0600);
639 if (errno != EINVAL) {
650 threads = shmat(shm_id, NULL, 0);
651 if (threads == (void *) -1) {
660 static void usage(void)
662 printf("%s\n", fio_version_string);
663 printf("\t--output\tWrite output to file\n");
664 printf("\t--timeout\tRuntime in seconds\n");
665 printf("\t--latency-log\tGenerate per-job latency logs\n");
666 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
667 printf("\t--minimal\tMinimal (terse) output\n");
668 printf("\t--version\tPrint version info and exit\n");
669 printf("\t--help\t\tPrint this page\n");
670 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
673 static int parse_cmd_line(int argc, char *argv[])
675 struct thread_data *td = NULL;
676 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
678 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
681 def_timeout = atoi(optarg);
690 f_out = fopen(optarg, "w+");
692 perror("fopen output");
704 exit(fio_show_option_help(optarg));
706 printf("%s\n", fio_version_string);
708 case FIO_GETOPT_JOB: {
709 const char *opt = long_options[lidx].name;
712 if (!strncmp(opt, "name", 4) && td) {
713 ret = add_job(td, td->name ?: "fio", 0);
721 int global = !strncmp(val, "global", 6);
723 td = get_new_job(global, &def_thread);
728 ret = fio_cmd_option_parse(td, opt, val);
742 ret = add_job(td, td->name ?: "fio", 0);
748 while (optind < argc) {
750 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
751 ini_file[ini_idx - 1] = strdup(argv[optind]);
759 int parse_options(int argc, char *argv[])
766 fio_options_dup_and_init(long_options);
768 if (setup_thread_area())
770 if (fill_def_thread())
773 job_files = parse_cmd_line(argc, argv);
775 for (i = 0; i < job_files; i++) {
776 if (fill_def_thread())
778 if (parse_jobs_ini(ini_file[i], i))
785 if (!thread_number) {
786 log_err("No jobs defined(s)\n");