2 * This file contains job initialization and setup functions.
15 #include <sys/types.h>
21 #define FIO_RANDSEED (0xb1899bedUL)
23 #define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
25 static int str_mem_cb(void *, const char *);
26 static int str_lockmem_cb(void *, unsigned long *);
27 #ifdef FIO_HAVE_IOPRIO
28 static int str_prio_cb(void *, unsigned int *);
29 static int str_prioclass_cb(void *, unsigned int *);
31 static int str_exitall_cb(void);
32 static int str_cpumask_cb(void *, unsigned int *);
33 static int str_fst_cb(void *, const char *);
34 static int str_filename_cb(void *, const char *);
35 static int str_directory_cb(void *, const char *);
36 static int str_opendir_cb(void *, const char *);
38 #define __stringify_1(x) #x
39 #define __stringify(x) __stringify_1(x)
42 * Map of job/command line options
44 static struct fio_option options[] = {
46 .name = "description",
47 .type = FIO_OPT_STR_STORE,
48 .off1 = td_var_offset(description),
49 .help = "Text job description",
53 .type = FIO_OPT_STR_STORE,
54 .off1 = td_var_offset(name),
55 .help = "Name of this job",
59 .type = FIO_OPT_STR_STORE,
60 .off1 = td_var_offset(directory),
61 .cb = str_directory_cb,
62 .help = "Directory to store files in",
66 .type = FIO_OPT_STR_STORE,
67 .off1 = td_var_offset(filename),
68 .cb = str_filename_cb,
69 .help = "File(s) to use for the workload",
73 .type = FIO_OPT_STR_STORE,
74 .off1 = td_var_offset(opendir),
76 .help = "Recursively add files from this directory and down",
81 .off1 = td_var_offset(td_ddir),
82 .help = "IO direction",
87 .help = "Sequential read",
90 .oval = TD_DDIR_WRITE,
91 .help = "Sequential write",
94 .oval = TD_DDIR_RANDREAD,
95 .help = "Random read",
97 { .ival = "randwrite",
98 .oval = TD_DDIR_RANDWRITE,
99 .help = "Random write",
103 .help = "Sequential read and write mix",
106 .oval = TD_DDIR_RANDRW,
107 .help = "Random read and write mix"
113 .type = FIO_OPT_STR_STORE,
114 .off1 = td_var_offset(ioengine),
115 .help = "IO engine to use",
119 .help = "Use read/write",
121 #ifdef FIO_HAVE_LIBAIO
123 .help = "Linux native asynchronous IO",
126 #ifdef FIO_HAVE_POSIXAIO
127 { .ival = "posixaio",
128 .help = "POSIX asynchronous IO",
132 .help = "Memory mapped IO",
134 #ifdef FIO_HAVE_SPLICE
136 .help = "splice/vmsplice based IO",
141 .help = "SCSI generic v3 IO",
145 .help = "Testing engine (no data transfer)",
148 .help = "Network IO",
150 #ifdef FIO_HAVE_SYSLET
151 { .ival = "syslet-rw",
152 .help = "syslet enabled async pread/pwrite IO",
156 .help = "CPU cycler burner engine",
158 { .ival = "external",
159 .help = "Load external engine (append name)",
166 .off1 = td_var_offset(iodepth),
167 .help = "Amount of IO buffers to keep in flight",
171 .name = "iodepth_batch",
173 .off1 = td_var_offset(iodepth_batch),
174 .help = "Number of IO to submit in one go",
177 .name = "iodepth_low",
179 .off1 = td_var_offset(iodepth_low),
180 .help = "Low water mark for queuing depth",
184 .type = FIO_OPT_STR_VAL,
185 .off1 = td_var_offset(total_file_size),
186 .help = "Size of device or file",
190 .type = FIO_OPT_STR_VAL_INT,
191 .off1 = td_var_offset(bs[DDIR_READ]),
192 .off2 = td_var_offset(bs[DDIR_WRITE]),
193 .help = "Block size unit",
198 .type = FIO_OPT_RANGE,
199 .off1 = td_var_offset(min_bs[DDIR_READ]),
200 .off2 = td_var_offset(max_bs[DDIR_READ]),
201 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
202 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
203 .help = "Set block size range (in more detail than bs)",
206 .name = "bs_unaligned",
207 .type = FIO_OPT_STR_SET,
208 .off1 = td_var_offset(bs_unaligned),
209 .help = "Don't sector align IO buffer sizes",
213 .type = FIO_OPT_STR_VAL,
214 .off1 = td_var_offset(start_offset),
215 .help = "Start IO from this offset",
219 .name = "randrepeat",
220 .type = FIO_OPT_BOOL,
221 .off1 = td_var_offset(rand_repeatable),
222 .help = "Use repeatable random IO pattern",
226 .name = "norandommap",
227 .type = FIO_OPT_STR_SET,
228 .off1 = td_var_offset(norandommap),
229 .help = "Accept potential duplicate random blocks",
234 .off1 = td_var_offset(nr_files),
235 .help = "Split job workload between this number of files",
241 .off1 = td_var_offset(open_files),
242 .help = "Number of files to keep open at the same time",
245 .name = "file_service_type",
248 .off1 = td_var_offset(file_service_type),
249 .help = "How to select which file to service next",
253 .oval = FIO_FSERVICE_RANDOM,
254 .help = "Choose a file at random",
256 { .ival = "roundrobin",
257 .oval = FIO_FSERVICE_RR,
258 .help = "Round robin select files",
265 .off1 = td_var_offset(fsync_blocks),
266 .help = "Issue fsync for writes every given number of blocks",
271 .type = FIO_OPT_BOOL,
272 .off1 = td_var_offset(odirect),
273 .help = "Use O_DIRECT IO (negates buffered)",
278 .type = FIO_OPT_BOOL,
279 .off1 = td_var_offset(odirect),
281 .help = "Use buffered IO (negates direct)",
286 .type = FIO_OPT_BOOL,
287 .off1 = td_var_offset(overwrite),
288 .help = "When writing, set whether to overwrite current data",
294 .off1 = td_var_offset(loops),
295 .help = "Number of times to run the job",
301 .off1 = td_var_offset(numjobs),
302 .help = "Duplicate this job this many times",
306 .name = "startdelay",
308 .off1 = td_var_offset(start_delay),
309 .help = "Only start job when this period has passed",
315 .type = FIO_OPT_STR_VAL_TIME,
316 .off1 = td_var_offset(timeout),
317 .help = "Stop workload when this amount of time has passed",
324 .off1 = td_var_offset(mem_type),
325 .help = "Backing type for IO buffers",
330 .help = "Use malloc(3) for IO buffers",
334 .help = "Use shared memory segments for IO buffers",
336 #ifdef FIO_HAVE_HUGETLB
339 .help = "Like shm, but use huge pages",
344 .help = "Use mmap(2) (file or anon) for IO buffers",
346 #ifdef FIO_HAVE_HUGETLB
347 { .ival = "mmaphuge",
348 .oval = MEM_MMAPHUGE,
349 .help = "Like mmap, but use huge pages",
357 .off1 = td_var_offset(verify),
358 .help = "Verify data written",
363 .help = "Don't do IO verification",
366 .oval = VERIFY_CRC32,
367 .help = "Use crc32 checksums for verification",
371 .help = "Use md5 checksums for verification",
376 .name = "write_iolog",
377 .type = FIO_OPT_STR_STORE,
378 .off1 = td_var_offset(write_iolog_file),
379 .help = "Store IO pattern to file",
382 .name = "read_iolog",
383 .type = FIO_OPT_STR_STORE,
384 .off1 = td_var_offset(read_iolog_file),
385 .help = "Playback IO pattern from file",
388 .name = "exec_prerun",
389 .type = FIO_OPT_STR_STORE,
390 .off1 = td_var_offset(exec_prerun),
391 .help = "Execute this file prior to running job",
394 .name = "exec_postrun",
395 .type = FIO_OPT_STR_STORE,
396 .off1 = td_var_offset(exec_postrun),
397 .help = "Execute this file after running job",
399 #ifdef FIO_HAVE_IOSCHED_SWITCH
401 .name = "ioscheduler",
402 .type = FIO_OPT_STR_STORE,
403 .off1 = td_var_offset(ioscheduler),
404 .help = "Use this IO scheduler on the backing device",
409 .type = FIO_OPT_STR_VAL,
410 .off1 = td_var_offset(zone_size),
411 .help = "Give size of an IO zone",
416 .type = FIO_OPT_STR_VAL,
417 .off1 = td_var_offset(zone_skip),
418 .help = "Space between IO zones",
423 .type = FIO_OPT_STR_VAL,
424 .cb = str_lockmem_cb,
425 .help = "Lock down this amount of memory",
429 .name = "rwmixcycle",
431 .off1 = td_var_offset(rwmixcycle),
432 .help = "Cycle period for mixed read/write workloads (msec)",
438 .off1 = td_var_offset(rwmixread),
440 .help = "Percentage of mixed workload that is reads",
444 .name = "rwmixwrite",
446 .off1 = td_var_offset(rwmixwrite),
448 .help = "Percentage of mixed workload that is writes",
454 .off1 = td_var_offset(nice),
455 .help = "Set job CPU nice value",
460 #ifdef FIO_HAVE_IOPRIO
465 .help = "Set job IO priority value",
472 .cb = str_prioclass_cb,
473 .help = "Set job IO priority class",
481 .off1 = td_var_offset(thinktime),
482 .help = "Idle time between IO buffers (usec)",
486 .name = "thinktime_spin",
488 .off1 = td_var_offset(thinktime_spin),
489 .help = "Start think time by spinning this amount (usec)",
493 .name = "thinktime_blocks",
495 .off1 = td_var_offset(thinktime_blocks),
496 .help = "IO buffer period between 'thinktime'",
502 .off1 = td_var_offset(rate),
503 .help = "Set bandwidth rate",
508 .off1 = td_var_offset(ratemin),
509 .help = "The bottom limit accepted",
514 .off1 = td_var_offset(ratecycle),
515 .help = "Window average for rate limits (msec)",
519 .name = "invalidate",
520 .type = FIO_OPT_BOOL,
521 .off1 = td_var_offset(invalidate_cache),
522 .help = "Invalidate buffer/page cache prior to running job",
527 .type = FIO_OPT_BOOL,
528 .off1 = td_var_offset(sync_io),
529 .help = "Use O_SYNC for buffered writes",
535 .off1 = td_var_offset(bw_avg_time),
536 .help = "Time window over which to calculate bandwidth (msec)",
540 .name = "create_serialize",
541 .type = FIO_OPT_BOOL,
542 .off1 = td_var_offset(create_serialize),
543 .help = "Serialize creating of job files",
547 .name = "create_fsync",
548 .type = FIO_OPT_BOOL,
549 .off1 = td_var_offset(create_fsync),
550 .help = "Fsync file after creation",
556 .off1 = td_var_offset(cpuload),
557 .help = "Use this percentage of CPU",
562 .off1 = td_var_offset(cpucycle),
563 .help = "Length of the CPU burn cycles (usecs)",
566 #ifdef FIO_HAVE_CPU_AFFINITY
570 .cb = str_cpumask_cb,
571 .help = "CPU affinity mask",
576 .type = FIO_OPT_BOOL,
577 .off1 = td_var_offset(end_fsync),
578 .help = "Include fsync at the end of job",
582 .name = "fsync_on_close",
583 .type = FIO_OPT_BOOL,
584 .off1 = td_var_offset(fsync_on_close),
585 .help = "fsync files on close",
590 .type = FIO_OPT_BOOL,
591 .off1 = td_var_offset(unlink),
592 .help = "Unlink created files after job has completed",
597 .type = FIO_OPT_STR_SET,
598 .cb = str_exitall_cb,
599 .help = "Terminate all jobs when one exits",
603 .type = FIO_OPT_STR_SET,
604 .off1 = td_var_offset(stonewall),
605 .help = "Insert a hard barrier between this job and previous",
609 .type = FIO_OPT_STR_SET,
610 .off1 = td_var_offset(use_thread),
611 .help = "Use threads instead of forks",
614 .name = "write_bw_log",
615 .type = FIO_OPT_STR_SET,
616 .off1 = td_var_offset(write_bw_log),
617 .help = "Write log of bandwidth during run",
620 .name = "write_lat_log",
621 .type = FIO_OPT_STR_SET,
622 .off1 = td_var_offset(write_lat_log),
623 .help = "Write log of latency during run",
626 .name = "hugepage-size",
627 .type = FIO_OPT_STR_VAL,
628 .off1 = td_var_offset(hugepage_size),
629 .help = "When using hugepages, specify size of each page",
630 .def = __stringify(FIO_HUGE_PAGE),
633 .name = "group_reporting",
634 .type = FIO_OPT_STR_SET,
635 .off1 = td_var_offset(group_reporting),
636 .help = "Do reporting on a per-group basis",
643 #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
644 #define FIO_CMD_OPTS (16)
645 #define FIO_GETOPT_JOB (0x89988998)
648 * Command line options. These will contain the above, plus a few
649 * extra that only pertain to fio itself and not jobs.
651 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
654 .has_arg = required_argument,
659 .has_arg = required_argument,
663 .name = "latency-log",
664 .has_arg = required_argument,
668 .name = "bandwidth-log",
669 .has_arg = required_argument,
674 .has_arg = optional_argument,
679 .has_arg = no_argument,
684 .has_arg = no_argument,
689 .has_arg = optional_argument,
697 static int def_timeout = 0;
699 static char fio_version_string[] = "fio 1.14a";
701 static char **ini_file;
702 static int max_jobs = MAX_JOBS;
704 struct thread_data def_thread;
705 struct thread_data *threads = NULL;
707 int exitall_on_terminate = 0;
708 int terse_output = 0;
709 unsigned long long mlock_size = 0;
713 static int write_lat_log = 0;
714 int write_bw_log = 0;
716 static int prev_group_jobs;
729 * Return a free job structure.
731 static struct thread_data *get_new_job(int global, struct thread_data *parent)
733 struct thread_data *td;
737 if (thread_number >= max_jobs)
740 td = &threads[thread_number++];
743 td->thread_number = thread_number;
747 static void put_job(struct thread_data *td)
749 if (td == &def_thread)
753 fprintf(f_out, "fio: %s\n", td->verror);
755 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
760 * Lazy way of fixing up options that depend on each other. We could also
761 * define option callback handlers, but this is easier.
763 static void fixup_options(struct thread_data *td)
765 if (!td->rwmixread && td->rwmixwrite)
766 td->rwmixread = 100 - td->rwmixwrite;
768 if (td->write_iolog_file && td->read_iolog_file) {
769 log_err("fio: read iolog overrides write_iolog\n");
770 free(td->write_iolog_file);
771 td->write_iolog_file = NULL;
774 if (td->io_ops->flags & FIO_SYNCIO)
778 td->iodepth = td->open_files;
782 * only really works for sequential io for now, and with 1 file
784 if (td->zone_size && td_random(td) && td->open_files == 1)
788 * Reads can do overwrites, we always need to pre-create the file
790 if (td_read(td) || td_rw(td))
793 if (!td->min_bs[DDIR_READ])
794 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
795 if (!td->max_bs[DDIR_READ])
796 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
797 if (!td->min_bs[DDIR_WRITE])
798 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
799 if (!td->max_bs[DDIR_WRITE])
800 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
802 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
804 if (td_read(td) && !td_rw(td))
807 if (td->norandommap && td->verify != VERIFY_NONE) {
808 log_err("fio: norandommap given, verify disabled\n");
809 td->verify = VERIFY_NONE;
811 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
812 log_err("fio: bs_unaligned may not work with raw io\n");
815 * thinktime_spin must be less than thinktime
817 if (td->thinktime_spin > td->thinktime)
818 td->thinktime_spin = td->thinktime;
821 * The low water mark cannot be bigger than the iodepth
823 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
825 * syslet work around - if the workload is sequential,
826 * we want to let the queue drain all the way down to
827 * avoid seeking between async threads
829 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
832 td->iodepth_low = td->iodepth;
836 * If batch number isn't set, default to the same as iodepth
838 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
839 td->iodepth_batch = td->iodepth;
841 if (td->nr_files > td->files_index)
842 td->nr_files = td->files_index;
844 if (td->open_files > td->nr_files || !td->open_files)
845 td->open_files = td->nr_files;
849 * This function leaks the buffer
851 static char *to_kmg(unsigned int val)
853 char *buf = malloc(32);
854 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
865 snprintf(buf, 31, "%u%c", val, *p);
869 /* External engines are specified by "external:name.o") */
870 static const char *get_engine_name(const char *str)
872 char *p = strstr(str, ":");
878 strip_blank_front(&p);
883 static int exists_and_not_file(const char *filename)
887 if (lstat(filename, &sb) == -1)
890 if (S_ISREG(sb.st_mode))
897 * Adds a job to the list of things todo. Sanitizes the various options
898 * to make sure we don't have conflicts, and initializes various
901 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
903 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
904 "randread", "randwrite", "randrw" };
908 char fname[PATH_MAX];
909 int numjobs, file_alloced;
912 * the def_thread is just for options, it's not a real job
914 if (td == &def_thread)
917 engine = get_engine_name(td->ioengine);
918 td->io_ops = load_ioengine(td, engine);
920 log_err("fio: failed to load engine %s\n", engine);
930 td->io_ops->flags |= FIO_RAWIO;
933 if (!td->filename && !td->files_index) {
936 if (td->nr_files == 1 && exists_and_not_file(jobname))
937 add_file(td, jobname);
939 for (i = 0; i < td->nr_files; i++) {
940 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
948 for_each_file(td, f, i) {
949 if (td->directory && f->filetype == FIO_TYPE_FILE) {
950 sprintf(fname, "%s/%s", td->directory, f->file_name);
951 f->file_name = strdup(fname);
955 td->mutex = fio_sem_init(0);
957 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
958 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
959 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
961 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
966 td->groupid = groupid;
972 if (td->write_lat_log) {
973 setup_log(&td->ts.slat_log);
974 setup_log(&td->ts.clat_log);
976 if (td->write_bw_log)
977 setup_log(&td->ts.bw_log);
980 td->name = strdup(jobname);
984 if (!strcmp(td->io_ops->name, "cpuio"))
985 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
987 char *c1, *c2, *c3, *c4;
989 c1 = to_kmg(td->min_bs[DDIR_READ]);
990 c2 = to_kmg(td->max_bs[DDIR_READ]);
991 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
992 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
994 fprintf(f_out, "%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);
1001 } else if (job_add_num == 1)
1002 fprintf(f_out, "...\n");
1006 * recurse add identical jobs, clear numjobs and stonewall options
1007 * as they don't apply to sub-jobs
1009 numjobs = td->numjobs;
1011 struct thread_data *td_new = get_new_job(0, td);
1016 td_new->numjobs = 1;
1017 td_new->stonewall = 0;
1020 td_new->filename = NULL;
1021 td_new->files_index = 0;
1022 td_new->files = NULL;
1025 job_add_num = numjobs - 1;
1027 if (add_job(td_new, jobname, job_add_num))
1031 if (td->numjobs > 1) {
1033 prev_group_jobs = 0;
1043 * Initialize the various random states we need (random io, block size ranges,
1044 * read/write mix, etc).
1046 int init_random_state(struct thread_data *td)
1048 unsigned long seeds[5];
1049 int fd, num_maps, blocks;
1053 if (td->io_ops->flags & FIO_DISKLESSIO)
1056 fd = open("/dev/urandom", O_RDONLY);
1058 td_verror(td, errno, "open");
1062 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
1063 td_verror(td, EIO, "read");
1070 os_random_seed(seeds[0], &td->bsrange_state);
1071 os_random_seed(seeds[1], &td->verify_state);
1072 os_random_seed(seeds[2], &td->rwmix_state);
1074 if (td->file_service_type == FIO_FSERVICE_RANDOM)
1075 os_random_seed(seeds[3], &td->next_file_state);
1080 if (td->rand_repeatable)
1081 seeds[4] = FIO_RANDSEED * td->thread_number;
1083 if (!td->norandommap) {
1084 for_each_file(td, f, i) {
1085 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
1086 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
1087 f->file_map = malloc(num_maps * sizeof(long));
1089 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
1092 f->num_maps = num_maps;
1093 memset(f->file_map, 0, num_maps * sizeof(long));
1097 os_random_seed(seeds[4], &td->random_state);
1101 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1103 #ifdef FIO_HAVE_CPU_AFFINITY
1108 for (i = 0; i < sizeof(int) * 8; i++) {
1110 CPU_SET(i, &cpumask);
1115 static int is_empty_or_comment(char *line)
1119 for (i = 0; i < strlen(line); i++) {
1124 if (!isspace(line[i]) && !iscntrl(line[i]))
1132 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1134 static char *get_opt_postfix(const char *str)
1136 char *p = strstr(str, ":");
1142 strip_blank_front(&p);
1147 static int str_mem_cb(void *data, const char *mem)
1149 struct thread_data *td = data;
1151 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
1152 td->mmapfile = get_opt_postfix(mem);
1153 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
1154 log_err("fio: mmaphuge:/path/to/file\n");
1162 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1168 #ifdef FIO_HAVE_IOPRIO
1169 static int str_prioclass_cb(void *data, unsigned int *val)
1171 struct thread_data *td = data;
1173 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1177 static int str_prio_cb(void *data, unsigned int *val)
1179 struct thread_data *td = data;
1186 static int str_exitall_cb(void)
1188 exitall_on_terminate = 1;
1192 static int str_cpumask_cb(void *data, unsigned int *val)
1194 struct thread_data *td = data;
1196 fill_cpu_mask(td->cpumask, *val);
1200 static int str_fst_cb(void *data, const char *str)
1202 struct thread_data *td = data;
1203 char *nr = get_opt_postfix(str);
1205 td->file_service_nr = 1;
1207 td->file_service_nr = atoi(nr);
1212 static int str_filename_cb(void *data, const char *input)
1214 struct thread_data *td = data;
1215 char *fname, *str, *p;
1217 p = str = strdup(input);
1219 strip_blank_front(&str);
1220 strip_blank_end(str);
1222 if (!td->files_index)
1225 while ((fname = strsep(&str, ":")) != NULL) {
1228 add_file(td, fname);
1236 static int str_directory_cb(void *data, const char fio_unused *str)
1238 struct thread_data *td = data;
1241 if (lstat(td->directory, &sb) < 0) {
1242 log_err("fio: %s is not a directory\n", td->directory);
1243 td_verror(td, errno, "lstat");
1246 if (!S_ISDIR(sb.st_mode)) {
1247 log_err("fio: %s is not a directory\n", td->directory);
1254 static int str_opendir_cb(void *data, const char fio_unused *str)
1256 struct thread_data *td = data;
1258 if (!td->files_index)
1261 return add_dir_files(td, td->opendir);
1265 * This is our [ini] type file parser.
1267 static int parse_jobs_ini(char *file, int stonewall_flag)
1269 unsigned int global;
1270 struct thread_data *td;
1271 char *string, *name;
1275 int ret = 0, stonewall;
1277 f = fopen(file, "r");
1279 perror("fopen job file");
1283 string = malloc(4096);
1285 memset(name, 0, 256);
1287 stonewall = stonewall_flag;
1289 p = fgets(string, 4095, f);
1292 if (is_empty_or_comment(p))
1294 if (sscanf(p, "[%255s]", name) != 1)
1297 global = !strncmp(name, "global", 6);
1299 name[strlen(name) - 1] = '\0';
1301 td = get_new_job(global, &def_thread);
1308 * Seperate multiple job files by a stonewall
1310 if (!global && stonewall) {
1311 td->stonewall = stonewall;
1316 while ((p = fgets(string, 4096, f)) != NULL) {
1317 if (is_empty_or_comment(p))
1320 strip_blank_front(&p);
1330 * Don't break here, continue parsing options so we
1331 * dump all the bad ones. Makes trial/error fixups
1332 * easier on the user.
1334 ret |= parse_option(p, options, td);
1339 ret = add_job(td, name, 0);
1341 log_err("fio: job %s dropped\n", name);
1352 static int fill_def_thread(void)
1354 memset(&def_thread, 0, sizeof(def_thread));
1356 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1357 perror("sched_getaffinity");
1362 * fill default options
1364 fill_default_options(&def_thread, options);
1366 def_thread.timeout = def_timeout;
1367 def_thread.write_bw_log = write_bw_log;
1368 def_thread.write_lat_log = write_lat_log;
1370 #ifdef FIO_HAVE_DISK_UTIL
1371 def_thread.do_disk_util = 1;
1377 static void usage(void)
1379 printf("%s\n", fio_version_string);
1380 printf("\t--output\tWrite output to file\n");
1381 printf("\t--timeout\tRuntime in seconds\n");
1382 printf("\t--latency-log\tGenerate per-job latency logs\n");
1383 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1384 printf("\t--minimal\tMinimal (terse) output\n");
1385 printf("\t--version\tPrint version info and exit\n");
1386 printf("\t--help\t\tPrint this page\n");
1387 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1390 static int parse_cmd_line(int argc, char *argv[])
1392 struct thread_data *td = NULL;
1393 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
1395 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
1398 def_timeout = atoi(optarg);
1407 f_out = fopen(optarg, "w+");
1409 perror("fopen output");
1421 ret = show_cmd_help(options, optarg);
1424 printf("%s\n", fio_version_string);
1426 case FIO_GETOPT_JOB: {
1427 const char *opt = long_options[lidx].name;
1430 if (!strncmp(opt, "name", 4) && td) {
1431 ret = add_job(td, td->name ?: "fio", 0);
1439 int global = !strncmp(val, "global", 6);
1441 td = get_new_job(global, &def_thread);
1446 ret = parse_cmd_option(opt, val, options, td);
1460 ret = add_job(td, td->name ?: "fio", 0);
1466 while (optind < argc) {
1468 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1469 ini_file[ini_idx - 1] = strdup(argv[optind]);
1476 static void free_shm(void)
1478 struct shmid_ds sbuf;
1481 shmdt((void *) threads);
1483 shmctl(shm_id, IPC_RMID, &sbuf);
1488 * The thread area is shared between the main process and the job
1489 * threads/processes. So setup a shared memory segment that will hold
1492 static int setup_thread_area(void)
1495 * 1024 is too much on some machines, scale max_jobs if
1496 * we get a failure that looks like too large a shm segment
1499 size_t size = max_jobs * sizeof(struct thread_data);
1501 shm_id = shmget(0, size, IPC_CREAT | 0600);
1504 if (errno != EINVAL) {
1515 threads = shmat(shm_id, NULL, 0);
1516 if (threads == (void *) -1) {
1526 * Copy the fio options into the long options map, so we mirror
1527 * job and cmd line options.
1529 static void dupe_job_options(void)
1531 struct fio_option *o;
1535 while (long_options[i].name)
1540 long_options[i].name = o->name;
1541 long_options[i].val = FIO_GETOPT_JOB;
1542 if (o->type == FIO_OPT_STR_SET)
1543 long_options[i].has_arg = no_argument;
1545 long_options[i].has_arg = required_argument;
1549 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1553 int parse_options(int argc, char *argv[])
1560 options_init(options);
1564 if (setup_thread_area())
1566 if (fill_def_thread())
1569 job_files = parse_cmd_line(argc, argv);
1571 for (i = 0; i < job_files; i++) {
1572 if (fill_def_thread())
1574 if (parse_jobs_ini(ini_file[i], i))
1581 if (!thread_number) {
1582 log_err("No jobs defined(s)\n");