2 * This file contains the ini and command liner parser. It will create
3 * and initialize the specified jobs.
14 #include <sys/types.h>
23 #define DEF_TIMEOUT (0)
24 #define DEF_RATE_CYCLE (1000)
25 #define DEF_ODIRECT (1)
26 #define DEF_IO_ENGINE (FIO_SYNCIO)
27 #define DEF_IO_ENGINE_NAME "sync"
28 #define DEF_SEQUENTIAL (1)
29 #define DEF_RAND_REPEAT (1)
30 #define DEF_OVERWRITE (1)
31 #define DEF_CREATE (1)
32 #define DEF_INVALIDATE (1)
33 #define DEF_SYNCIO (0)
34 #define DEF_RANDSEED (0xb1899bedUL)
35 #define DEF_BWAVGTIME (500)
36 #define DEF_CREATE_SER (1)
37 #define DEF_CREATE_FSYNC (1)
39 #define DEF_VERIFY (0)
40 #define DEF_STONEWALL (0)
41 #define DEF_NUMJOBS (1)
42 #define DEF_USE_THREAD (0)
43 #define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
44 #define DEF_ZONE_SIZE (0)
45 #define DEF_ZONE_SKIP (0)
46 #define DEF_RWMIX_CYCLE (500)
47 #define DEF_RWMIX_READ (50)
49 #define DEF_NR_FILES (1)
51 static int def_timeout = DEF_TIMEOUT;
53 static char fio_version_string[] = "fio 1.5";
55 static char **ini_file;
56 static int max_jobs = MAX_JOBS;
58 struct thread_data def_thread;
59 struct thread_data *threads = NULL;
62 int write_lat_log = 0;
64 int exitall_on_terminate = 0;
66 unsigned long long mlock_size = 0;
71 * Return a free job structure.
73 static struct thread_data *get_new_job(int global, struct thread_data *parent)
75 struct thread_data *td;
79 if (thread_number >= max_jobs)
82 td = &threads[thread_number++];
86 td->thread_number = thread_number;
90 static void put_job(struct thread_data *td)
92 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
97 * Adds a job to the list of things todo. Sanitizes the various options
98 * to make sure we don't have conflicts, and initializes various
101 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
103 char *ddir_str[] = { "read", "write", "randread", "randwrite",
104 "rw", NULL, "randrw" };
106 int numjobs, ddir, i;
109 #ifndef FIO_HAVE_LIBAIO
110 if (td->io_engine == FIO_LIBAIO) {
111 log_err("Linux libaio not available\n");
115 #ifndef FIO_HAVE_POSIXAIO
116 if (td->io_engine == FIO_POSIXAIO) {
117 log_err("posix aio not available\n");
123 * the def_thread is just for options, it's not a real job
125 if (td == &def_thread)
129 * Set default io engine, if none set
132 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
134 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
139 if (td->io_ops->flags & FIO_SYNCIO)
147 * only really works for sequential io for now, and with 1 file
149 if (td->zone_size && !td->sequential && td->nr_files == 1)
153 * Reads can do overwrites, we always need to pre-create the file
155 if (td_read(td) || td_rw(td))
158 td->filetype = FIO_TYPE_FILE;
159 if (!stat(jobname, &sb)) {
160 if (S_ISBLK(sb.st_mode))
161 td->filetype = FIO_TYPE_BD;
162 else if (S_ISCHR(sb.st_mode))
163 td->filetype = FIO_TYPE_CHAR;
166 if (td->filetype == FIO_TYPE_FILE) {
171 if (td->directory && td->directory[0] != '\0')
172 sprintf(tmp, "%s/", td->directory);
174 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
176 for_each_file(td, f, i) {
177 memset(f, 0, sizeof(*f));
180 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
181 f->file_name = strdup(tmp);
185 td->files = malloc(sizeof(struct fio_file));
188 memset(f, 0, sizeof(*f));
190 f->file_name = strdup(jobname);
193 for_each_file(td, f, i) {
194 f->file_size = td->total_file_size / td->nr_files;
195 f->file_offset = td->start_offset;
198 fio_sem_init(&td->mutex, 0);
200 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
201 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
202 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
204 if (td->min_bs == -1U)
206 if (td->max_bs == -1U)
208 if (td_read(td) && !td_rw(td))
211 if (td->stonewall && td->thread_number > 1)
214 td->groupid = groupid;
220 setup_log(&td->slat_log);
221 setup_log(&td->clat_log);
224 setup_log(&td->bw_log);
226 if (td->name[0] == '\0')
227 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
229 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
233 if (td->io_ops->flags & FIO_CPUIO)
234 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
236 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_ops->name, td->iodepth);
237 } else if (job_add_num == 1)
238 fprintf(f_out, "...\n");
242 * recurse add identical jobs, clear numjobs and stonewall options
243 * as they don't apply to sub-jobs
245 numjobs = td->numjobs;
247 struct thread_data *td_new = get_new_job(0, td);
253 td_new->stonewall = 0;
254 job_add_num = numjobs - 1;
256 if (add_job(td_new, jobname, job_add_num))
266 * Initialize the various random states we need (random io, block size ranges,
267 * read/write mix, etc).
269 int init_random_state(struct thread_data *td)
271 unsigned long seeds[4];
272 int fd, num_maps, blocks, i;
274 fd = open("/dev/urandom", O_RDONLY);
276 td_verror(td, errno);
280 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
288 os_random_seed(seeds[0], &td->bsrange_state);
289 os_random_seed(seeds[1], &td->verify_state);
290 os_random_seed(seeds[2], &td->rwmix_state);
295 if (td->rand_repeatable)
296 seeds[3] = DEF_RANDSEED;
298 for (i = 0; i < td->nr_files; i++) {
299 struct fio_file *f = &td->files[i];
301 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
302 num_maps = blocks / BLOCKS_PER_MAP;
303 f->file_map = malloc(num_maps * sizeof(long));
304 f->num_maps = num_maps;
305 memset(f->file_map, 0, num_maps * sizeof(long));
308 os_random_seed(seeds[3], &td->random_state);
312 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
314 #ifdef FIO_HAVE_CPU_AFFINITY
319 for (i = 0; i < sizeof(int) * 8; i++) {
321 CPU_SET(i, &cpumask);
326 static unsigned long get_mult_time(char c)
343 static unsigned long get_mult_bytes(char c)
354 return 1024 * 1024 * 1024;
361 * convert string after '=' into decimal value, noting any size suffix
363 static int str_to_decimal(char *p, unsigned long long *val, int kilo)
368 str = strchr(p, '=');
375 *val = strtoul(str, NULL, 10);
376 if (*val == ULONG_MAX && errno == ERANGE)
380 *val *= get_mult_bytes(str[len - 1]);
382 *val *= get_mult_time(str[len - 1]);
386 static int check_str_bytes(char *p, char *name, unsigned long long *val)
388 if (strncmp(p, name, strlen(name) - 1))
391 return str_to_decimal(p, val, 1);
394 static int check_str_time(char *p, char *name, unsigned long long *val)
396 if (strncmp(p, name, strlen(name) - 1))
399 return str_to_decimal(p, val, 0);
402 static void strip_blank_front(char **p)
410 static void strip_blank_end(char *p)
412 char *s = p + strlen(p) - 1;
414 while (isspace(*s) || iscntrl(*s))
420 typedef int (str_cb_fn)(struct thread_data *, char *);
422 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
426 if (strncmp(p, name, strlen(name)))
438 strip_blank_front(&s);
442 static int check_strstore(char *p, char *name, char *dest)
446 if (strncmp(p, name, strlen(name)))
458 strip_blank_front(&s);
464 static int __check_range_bytes(char *str, unsigned long *val)
468 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
469 *val *= get_mult_bytes(suffix);
473 if (sscanf(str, "%lu", val) == 1)
479 static int check_range_bytes(char *p, char *name, unsigned long *s,
485 if (strncmp(p, name, strlen(name)))
491 str = strstr(p, name);
497 str = strchr(p, '=');
502 * 'p' now holds whatever is after the '=' sign
507 * terminate p1 at the '-' sign
516 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
522 static int check_int(char *p, char *name, unsigned int *val)
526 if (strncmp(p, name, strlen(name)))
529 str = strstr(p, name);
533 str = strchr(p, '=');
539 if (sscanf(str, "%u", val) == 1)
545 static int check_strset(char *p, char *name)
547 return strncmp(p, name, strlen(name));
550 static int is_empty_or_comment(char *line)
554 for (i = 0; i < strlen(line); i++) {
557 if (!isspace(line[i]) && !iscntrl(line[i]))
564 static int str_rw_cb(struct thread_data *td, char *mem)
566 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
567 td->ddir = DDIR_READ;
570 } else if (!strncmp(mem, "randread", 8)) {
571 td->ddir = DDIR_READ;
574 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
575 td->ddir = DDIR_WRITE;
578 } else if (!strncmp(mem, "randwrite", 9)) {
579 td->ddir = DDIR_WRITE;
582 } else if (!strncmp(mem, "rw", 2)) {
587 } else if (!strncmp(mem, "randrw", 6)) {
594 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
598 static int str_verify_cb(struct thread_data *td, char *mem)
600 if (!strncmp(mem, "0", 1)) {
601 td->verify = VERIFY_NONE;
603 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
604 td->verify = VERIFY_MD5;
606 } else if (!strncmp(mem, "crc32", 5)) {
607 td->verify = VERIFY_CRC32;
611 log_err("fio: verify types: md5, crc32\n");
615 static int str_mem_cb(struct thread_data *td, char *mem)
617 if (!strncmp(mem, "malloc", 6)) {
618 td->mem_type = MEM_MALLOC;
620 } else if (!strncmp(mem, "shm", 3)) {
621 td->mem_type = MEM_SHM;
623 } else if (!strncmp(mem, "mmap", 4)) {
624 td->mem_type = MEM_MMAP;
628 log_err("fio: mem type: malloc, shm, mmap\n");
632 static int str_ioengine_cb(struct thread_data *td, char *str)
634 td->io_ops = load_ioengine(td, str);
638 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
643 * This is our [ini] type file parser.
645 int parse_jobs_ini(char *file, int stonewall_flag)
647 unsigned int prioclass, prio, cpu, global, il;
648 unsigned long long ull;
649 unsigned long ul1, ul2;
650 struct thread_data *td;
651 char *string, *name, *tmpbuf;
655 int ret = 0, stonewall;
657 f = fopen(file, "r");
659 perror("fopen job file");
663 string = malloc(4096);
665 tmpbuf = malloc(4096);
667 stonewall = stonewall_flag;
668 while ((p = fgets(string, 4096, f)) != NULL) {
671 if (is_empty_or_comment(p))
673 if (sscanf(p, "[%s]", name) != 1)
676 global = !strncmp(name, "global", 6);
678 name[strlen(name) - 1] = '\0';
680 td = get_new_job(global, &def_thread);
687 * Seperate multiple job files by a stonewall
689 if (!global && stonewall) {
690 td->stonewall = stonewall;
695 while ((p = fgets(string, 4096, f)) != NULL) {
696 if (is_empty_or_comment(p))
700 strip_blank_front(&p);
703 if (!check_int(p, "prio", &prio)) {
704 #ifndef FIO_HAVE_IOPRIO
705 log_err("io priorities not available\n");
713 if (!check_int(p, "prioclass", &prioclass)) {
714 #ifndef FIO_HAVE_IOPRIO
715 log_err("io priorities not available\n");
719 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
724 if (!check_int(p, "direct", &il)) {
729 if (!check_int(p, "rand_repeatable", &il)) {
730 td->rand_repeatable = il;
734 if (!check_int(p, "rate", &td->rate)) {
738 if (!check_int(p, "ratemin", &td->ratemin)) {
742 if (!check_int(p, "ratecycle", &td->ratecycle)) {
746 if (!check_int(p, "cpuload", &td->cpuload)) {
750 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
754 if (!check_int(p, "thinktime", &td->thinktime)) {
758 if (!check_int(p, "cpumask", &cpu)) {
759 #ifndef FIO_HAVE_CPU_AFFINITY
760 log_err("cpu affinity not available\n");
764 fill_cpu_mask(td->cpumask, cpu);
768 if (!check_int(p, "fsync", &td->fsync_blocks)) {
773 if (!check_int(p, "startdelay", &td->start_delay)) {
777 if (!check_str_time(p, "timeout", &ull)) {
782 if (!check_int(p, "invalidate", &il)) {
783 td->invalidate_cache = il;
787 if (!check_int(p, "iodepth", &td->iodepth)) {
791 if (!check_int(p, "sync", &il)) {
796 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
800 if (!check_int(p, "create_serialize", &il)) {
801 td->create_serialize = il;
805 if (!check_int(p, "create_fsync", &il)) {
806 td->create_fsync = il;
810 if (!check_int(p, "end_fsync", &il)) {
815 if (!check_int(p, "loops", &td->loops)) {
819 if (!check_int(p, "numjobs", &td->numjobs)) {
823 if (!check_int(p, "overwrite", &il)) {
828 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
832 if (!check_int(p, "rwmixread", &il)) {
839 if (!check_int(p, "rwmixwrite", &il)) {
842 td->rwmixread = 100 - il;
846 if (!check_int(p, "nice", &td->nice)) {
850 if (!check_int(p, "nrfiles", &td->nr_files)) {
854 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
865 if (!check_str_bytes(p, "bs", &ull)) {
870 if (!check_str_bytes(p, "size", &td->total_file_size)) {
874 if (!check_str_bytes(p, "offset", &td->start_offset)) {
878 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
882 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
886 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
890 if (!check_strstore(p, "directory", tmpbuf)) {
891 td->directory = strdup(tmpbuf);
895 if (!check_strstore(p, "name", tmpbuf)) {
896 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
900 if (!check_str(p, "mem", str_mem_cb, td)) {
904 if (!check_str(p, "verify", str_verify_cb, td)) {
908 if (!check_str(p, "rw", str_rw_cb, td)) {
912 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
916 if (!check_strset(p, "create")) {
921 if (!check_strset(p, "exitall")) {
922 exitall_on_terminate = 1;
926 if (!check_strset(p, "stonewall")) {
931 if (!check_strset(p, "thread")) {
936 if (!check_strstore(p, "iolog", tmpbuf)) {
937 if (td->write_iolog) {
938 log_err("fio: read iolog overrides given write_iolog\n");
939 free(td->iolog_file);
942 td->iolog_file = strdup(tmpbuf);
947 if (!check_strstore(p, "write_iolog", tmpbuf)) {
948 if (!td->read_iolog) {
949 td->iolog_file = strdup(tmpbuf);
952 log_err("fio: read iolog overrides given write_iolog\n");
956 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
957 td->exec_prerun = strdup(tmpbuf);
961 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
962 td->exec_postrun = strdup(tmpbuf);
966 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
967 #ifndef FIO_HAVE_IOSCHED_SWITCH
968 log_err("io scheduler switching not available\n");
972 td->ioscheduler = strdup(tmpbuf);
979 * Don't break here, continue parsing options so we
980 * dump all the bad ones. Makes trial/error fixups
981 * easier on the user.
983 printf("Client%d: bad option %s\n",td->thread_number,p);
989 ret = add_job(td, name, 0);
1002 static int fill_def_thread(void)
1004 memset(&def_thread, 0, sizeof(def_thread));
1006 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1007 perror("sched_getaffinity");
1014 def_thread.ddir = DDIR_READ;
1015 def_thread.iomix = 0;
1016 def_thread.bs = DEF_BS;
1017 def_thread.min_bs = -1;
1018 def_thread.max_bs = -1;
1019 def_thread.odirect = DEF_ODIRECT;
1020 def_thread.ratecycle = DEF_RATE_CYCLE;
1021 def_thread.sequential = DEF_SEQUENTIAL;
1022 def_thread.timeout = def_timeout;
1023 def_thread.create_file = DEF_CREATE;
1024 def_thread.overwrite = DEF_OVERWRITE;
1025 def_thread.invalidate_cache = DEF_INVALIDATE;
1026 def_thread.sync_io = DEF_SYNCIO;
1027 def_thread.mem_type = MEM_MALLOC;
1028 def_thread.bw_avg_time = DEF_BWAVGTIME;
1029 def_thread.create_serialize = DEF_CREATE_SER;
1030 def_thread.create_fsync = DEF_CREATE_FSYNC;
1031 def_thread.loops = DEF_LOOPS;
1032 def_thread.verify = DEF_VERIFY;
1033 def_thread.stonewall = DEF_STONEWALL;
1034 def_thread.numjobs = DEF_NUMJOBS;
1035 def_thread.use_thread = DEF_USE_THREAD;
1036 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1037 def_thread.rwmixread = DEF_RWMIX_READ;
1038 def_thread.nice = DEF_NICE;
1039 def_thread.rand_repeatable = DEF_RAND_REPEAT;
1040 def_thread.nr_files = DEF_NR_FILES;
1041 #ifdef FIO_HAVE_DISK_UTIL
1042 def_thread.do_disk_util = 1;
1048 static void usage(char *name)
1050 printf("%s\n", fio_version_string);
1051 printf("\t-o Write output to file\n");
1052 printf("\t-t Runtime in seconds\n");
1053 printf("\t-l Generate per-job latency logs\n");
1054 printf("\t-w Generate per-job bandwidth logs\n");
1055 printf("\t-m Minimal (terse) output\n");
1056 printf("\t-v Print version info and exit\n");
1059 static int parse_cmd_line(int argc, char *argv[])
1061 int c, idx = 1, ini_idx = 0;
1063 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
1066 def_timeout = atoi(optarg);
1078 f_out = fopen(optarg, "w+");
1080 perror("fopen output");
1094 printf("%s\n", fio_version_string);
1099 while (idx < argc) {
1101 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1102 ini_file[ini_idx - 1] = strdup(argv[idx]);
1114 static void free_shm(void)
1116 struct shmid_ds sbuf;
1119 shmdt((void *) threads);
1121 shmctl(shm_id, IPC_RMID, &sbuf);
1126 * The thread area is shared between the main process and the job
1127 * threads/processes. So setup a shared memory segment that will hold
1130 static int setup_thread_area(void)
1133 * 1024 is too much on some machines, scale max_jobs if
1134 * we get a failure that looks like too large a shm segment
1137 size_t size = max_jobs * sizeof(struct thread_data);
1139 shm_id = shmget(0, size, IPC_CREAT | 0600);
1142 if (errno != EINVAL) {
1153 threads = shmat(shm_id, NULL, 0);
1154 if (threads == (void *) -1) {
1163 int parse_options(int argc, char *argv[])
1167 if (setup_thread_area())
1169 if (fill_def_thread())
1172 job_files = parse_cmd_line(argc, argv);
1174 log_err("Need job file(s)\n");
1179 for (i = 0; i < job_files; i++) {
1180 if (fill_def_thread())
1182 if (parse_jobs_ini(ini_file[i], i))