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_INVALIDATE (1)
32 #define DEF_SYNCIO (0)
33 #define DEF_RANDSEED (0xb1899bedUL)
34 #define DEF_BWAVGTIME (500)
35 #define DEF_CREATE_SER (1)
36 #define DEF_CREATE_FSYNC (1)
38 #define DEF_VERIFY (0)
39 #define DEF_STONEWALL (0)
40 #define DEF_NUMJOBS (1)
41 #define DEF_USE_THREAD (0)
42 #define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
43 #define DEF_ZONE_SIZE (0)
44 #define DEF_ZONE_SKIP (0)
45 #define DEF_RWMIX_CYCLE (500)
46 #define DEF_RWMIX_READ (50)
48 #define DEF_NR_FILES (1)
49 #define DEF_UNLINK (0)
50 #define DEF_WRITE_BW_LOG (0)
51 #define DEF_WRITE_LAT_LOG (0)
53 static int def_timeout = DEF_TIMEOUT;
55 static char fio_version_string[] = "fio 1.5";
57 static char **ini_file;
58 static int max_jobs = MAX_JOBS;
60 struct thread_data def_thread;
61 struct thread_data *threads = NULL;
64 int exitall_on_terminate = 0;
66 unsigned long long mlock_size = 0;
70 static int write_lat_log = DEF_WRITE_LAT_LOG;
71 static int write_bw_log = DEF_WRITE_BW_LOG;
74 * Return a free job structure.
76 static struct thread_data *get_new_job(int global, struct thread_data *parent)
78 struct thread_data *td;
82 if (thread_number >= max_jobs)
85 td = &threads[thread_number++];
89 td->thread_number = thread_number;
93 static void put_job(struct thread_data *td)
95 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
100 * Adds a job to the list of things todo. Sanitizes the various options
101 * to make sure we don't have conflicts, and initializes various
104 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
106 char *ddir_str[] = { "read", "write", "randread", "randwrite",
107 "rw", NULL, "randrw" };
109 int numjobs, ddir, i;
112 #ifndef FIO_HAVE_LIBAIO
113 if (td->io_engine == FIO_LIBAIO) {
114 log_err("Linux libaio not available\n");
118 #ifndef FIO_HAVE_POSIXAIO
119 if (td->io_engine == FIO_POSIXAIO) {
120 log_err("posix aio not available\n");
126 * the def_thread is just for options, it's not a real job
128 if (td == &def_thread)
132 * Set default io engine, if none set
135 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
137 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
142 if (td->io_ops->flags & FIO_SYNCIO)
146 td->iodepth = td->nr_files;
150 * only really works for sequential io for now, and with 1 file
152 if (td->zone_size && !td->sequential && td->nr_files == 1)
156 * Reads can do overwrites, we always need to pre-create the file
158 if (td_read(td) || td_rw(td))
161 td->filetype = FIO_TYPE_FILE;
162 if (!stat(jobname, &sb)) {
163 if (S_ISBLK(sb.st_mode))
164 td->filetype = FIO_TYPE_BD;
165 else if (S_ISCHR(sb.st_mode))
166 td->filetype = FIO_TYPE_CHAR;
170 td->io_ops->flags |= FIO_RAWIO;
172 if (td->filetype == FIO_TYPE_FILE) {
177 if (td->directory && td->directory[0] != '\0')
178 sprintf(tmp, "%s/", td->directory);
180 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
182 for_each_file(td, f, i) {
183 memset(f, 0, sizeof(*f));
187 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
188 f->file_name = strdup(tmp);
192 td->files = malloc(sizeof(struct fio_file));
195 memset(f, 0, sizeof(*f));
197 f->file_name = strdup(jobname);
200 for_each_file(td, f, i) {
201 f->file_size = td->total_file_size / td->nr_files;
202 f->file_offset = td->start_offset;
205 fio_sem_init(&td->mutex, 0);
207 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
208 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
209 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
211 if (td->min_bs == -1U)
213 if (td->max_bs == -1U)
215 if (td_read(td) && !td_rw(td))
218 if (td->stonewall && td->thread_number > 1)
221 td->groupid = groupid;
226 if (td->write_lat_log) {
227 setup_log(&td->slat_log);
228 setup_log(&td->clat_log);
230 if (td->write_bw_log)
231 setup_log(&td->bw_log);
233 if (td->name[0] == '\0')
234 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
236 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
240 if (td->io_ops->flags & FIO_CPUIO)
241 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
243 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);
244 } else if (job_add_num == 1)
245 fprintf(f_out, "...\n");
249 * recurse add identical jobs, clear numjobs and stonewall options
250 * as they don't apply to sub-jobs
252 numjobs = td->numjobs;
254 struct thread_data *td_new = get_new_job(0, td);
260 td_new->stonewall = 0;
261 job_add_num = numjobs - 1;
263 if (add_job(td_new, jobname, job_add_num))
273 * Initialize the various random states we need (random io, block size ranges,
274 * read/write mix, etc).
276 int init_random_state(struct thread_data *td)
278 unsigned long seeds[4];
279 int fd, num_maps, blocks, i;
282 fd = open("/dev/urandom", O_RDONLY);
284 td_verror(td, errno);
288 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
296 os_random_seed(seeds[0], &td->bsrange_state);
297 os_random_seed(seeds[1], &td->verify_state);
298 os_random_seed(seeds[2], &td->rwmix_state);
303 if (td->rand_repeatable)
304 seeds[3] = DEF_RANDSEED;
306 for_each_file(td, f, i) {
307 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
308 num_maps = blocks / BLOCKS_PER_MAP;
309 f->file_map = malloc(num_maps * sizeof(long));
310 f->num_maps = num_maps;
311 memset(f->file_map, 0, num_maps * sizeof(long));
314 os_random_seed(seeds[3], &td->random_state);
318 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
320 #ifdef FIO_HAVE_CPU_AFFINITY
325 for (i = 0; i < sizeof(int) * 8; i++) {
327 CPU_SET(i, &cpumask);
332 static unsigned long get_mult_time(char c)
349 static unsigned long get_mult_bytes(char c)
360 return 1024 * 1024 * 1024;
367 * convert string after '=' into decimal value, noting any size suffix
369 static int str_to_decimal(char *p, unsigned long long *val, int kilo)
374 str = strchr(p, '=');
381 *val = strtoul(str, NULL, 10);
382 if (*val == ULONG_MAX && errno == ERANGE)
386 *val *= get_mult_bytes(str[len - 1]);
388 *val *= get_mult_time(str[len - 1]);
392 static int check_str_bytes(char *p, char *name, unsigned long long *val)
394 if (strncmp(p, name, strlen(name) - 1))
397 return str_to_decimal(p, val, 1);
400 static int check_str_time(char *p, char *name, unsigned long long *val)
402 if (strncmp(p, name, strlen(name) - 1))
405 return str_to_decimal(p, val, 0);
408 static void strip_blank_front(char **p)
416 static void strip_blank_end(char *p)
418 char *s = p + strlen(p) - 1;
420 while (isspace(*s) || iscntrl(*s))
426 typedef int (str_cb_fn)(struct thread_data *, char *);
428 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
432 if (strncmp(p, name, strlen(name)))
444 strip_blank_front(&s);
448 static int check_strstore(char *p, char *name, char *dest)
452 if (strncmp(p, name, strlen(name)))
464 strip_blank_front(&s);
470 static int __check_range_bytes(char *str, unsigned long *val)
474 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
475 *val *= get_mult_bytes(suffix);
479 if (sscanf(str, "%lu", val) == 1)
485 static int check_range_bytes(char *p, char *name, unsigned long *s,
491 if (strncmp(p, name, strlen(name)))
497 str = strstr(p, name);
503 str = strchr(p, '=');
508 * 'p' now holds whatever is after the '=' sign
513 * terminate p1 at the '-' sign
522 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
528 static int check_int(char *p, char *name, unsigned int *val)
532 if (strncmp(p, name, strlen(name)))
535 str = strstr(p, name);
539 str = strchr(p, '=');
545 if (sscanf(str, "%u", val) == 1)
551 static int check_strset(char *p, char *name)
553 return strncmp(p, name, strlen(name));
556 static int is_empty_or_comment(char *line)
560 for (i = 0; i < strlen(line); i++) {
563 if (!isspace(line[i]) && !iscntrl(line[i]))
570 static int str_rw_cb(struct thread_data *td, char *mem)
572 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
573 td->ddir = DDIR_READ;
576 } else if (!strncmp(mem, "randread", 8)) {
577 td->ddir = DDIR_READ;
580 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
581 td->ddir = DDIR_WRITE;
584 } else if (!strncmp(mem, "randwrite", 9)) {
585 td->ddir = DDIR_WRITE;
588 } else if (!strncmp(mem, "rw", 2)) {
593 } else if (!strncmp(mem, "randrw", 6)) {
600 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
604 static int str_verify_cb(struct thread_data *td, char *mem)
606 if (!strncmp(mem, "0", 1)) {
607 td->verify = VERIFY_NONE;
609 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
610 td->verify = VERIFY_MD5;
612 } else if (!strncmp(mem, "crc32", 5)) {
613 td->verify = VERIFY_CRC32;
617 log_err("fio: verify types: md5, crc32\n");
621 static int str_mem_cb(struct thread_data *td, char *mem)
623 if (!strncmp(mem, "malloc", 6)) {
624 td->mem_type = MEM_MALLOC;
626 } else if (!strncmp(mem, "shm", 3)) {
627 td->mem_type = MEM_SHM;
629 } else if (!strncmp(mem, "mmap", 4)) {
630 td->mem_type = MEM_MMAP;
634 log_err("fio: mem type: malloc, shm, mmap\n");
638 static int str_ioengine_cb(struct thread_data *td, char *str)
640 td->io_ops = load_ioengine(td, str);
644 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
649 * This is our [ini] type file parser.
651 int parse_jobs_ini(char *file, int stonewall_flag)
653 unsigned int prioclass, prio, cpu, global, il;
654 unsigned long long ull;
655 unsigned long ul1, ul2;
656 struct thread_data *td;
657 char *string, *name, *tmpbuf;
661 int ret = 0, stonewall;
663 f = fopen(file, "r");
665 perror("fopen job file");
669 string = malloc(4096);
671 tmpbuf = malloc(4096);
673 stonewall = stonewall_flag;
674 while ((p = fgets(string, 4096, f)) != NULL) {
677 if (is_empty_or_comment(p))
679 if (sscanf(p, "[%s]", name) != 1)
682 global = !strncmp(name, "global", 6);
684 name[strlen(name) - 1] = '\0';
686 td = get_new_job(global, &def_thread);
693 * Seperate multiple job files by a stonewall
695 if (!global && stonewall) {
696 td->stonewall = stonewall;
701 while ((p = fgets(string, 4096, f)) != NULL) {
702 if (is_empty_or_comment(p))
706 strip_blank_front(&p);
709 if (!check_int(p, "prio", &prio)) {
710 #ifndef FIO_HAVE_IOPRIO
711 log_err("io priorities not available\n");
719 if (!check_int(p, "prioclass", &prioclass)) {
720 #ifndef FIO_HAVE_IOPRIO
721 log_err("io priorities not available\n");
725 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
730 if (!check_int(p, "direct", &il)) {
735 if (!check_int(p, "rand_repeatable", &il)) {
736 td->rand_repeatable = il;
740 if (!check_int(p, "rate", &td->rate)) {
744 if (!check_int(p, "ratemin", &td->ratemin)) {
748 if (!check_int(p, "ratecycle", &td->ratecycle)) {
752 if (!check_int(p, "cpuload", &td->cpuload)) {
756 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
760 if (!check_int(p, "thinktime", &td->thinktime)) {
764 if (!check_int(p, "cpumask", &cpu)) {
765 #ifndef FIO_HAVE_CPU_AFFINITY
766 log_err("cpu affinity not available\n");
770 fill_cpu_mask(td->cpumask, cpu);
774 if (!check_int(p, "fsync", &td->fsync_blocks)) {
779 if (!check_int(p, "startdelay", &td->start_delay)) {
783 if (!check_str_time(p, "timeout", &ull)) {
788 if (!check_int(p, "invalidate", &il)) {
789 td->invalidate_cache = il;
793 if (!check_int(p, "iodepth", &td->iodepth)) {
797 if (!check_int(p, "sync", &il)) {
802 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
806 if (!check_int(p, "create_serialize", &il)) {
807 td->create_serialize = il;
811 if (!check_int(p, "create_fsync", &il)) {
812 td->create_fsync = il;
816 if (!check_int(p, "end_fsync", &il)) {
821 if (!check_int(p, "loops", &td->loops)) {
825 if (!check_int(p, "numjobs", &td->numjobs)) {
829 if (!check_int(p, "overwrite", &il)) {
834 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
838 if (!check_int(p, "rwmixread", &il)) {
845 if (!check_int(p, "rwmixwrite", &il)) {
848 td->rwmixread = 100 - il;
852 if (!check_int(p, "nice", &td->nice)) {
856 if (!check_int(p, "nrfiles", &td->nr_files)) {
860 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
871 if (!check_str_bytes(p, "bs", &ull)) {
876 if (!check_str_bytes(p, "size", &td->total_file_size)) {
880 if (!check_str_bytes(p, "offset", &td->start_offset)) {
884 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
888 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
892 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
896 if (!check_strstore(p, "directory", tmpbuf)) {
897 td->directory = strdup(tmpbuf);
901 if (!check_strstore(p, "name", tmpbuf)) {
902 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
906 if (!check_str(p, "mem", str_mem_cb, td)) {
910 if (!check_str(p, "verify", str_verify_cb, td)) {
914 if (!check_str(p, "rw", str_rw_cb, td)) {
918 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
922 if (!check_strset(p, "exitall")) {
923 exitall_on_terminate = 1;
927 if (!check_strset(p, "stonewall")) {
932 if (!check_strset(p, "thread")) {
937 if (!check_strset(p, "unlink")) {
942 if (!check_strset(p, "write_bw_log")) {
943 td->write_bw_log = 1;
947 if (!check_strset(p, "write_lat_log")) {
948 td->write_lat_log = 1;
952 if (!check_strstore(p, "iolog", tmpbuf)) {
953 if (td->write_iolog) {
954 log_err("fio: read iolog overrides given write_iolog\n");
955 free(td->iolog_file);
958 td->iolog_file = strdup(tmpbuf);
963 if (!check_strstore(p, "write_iolog", tmpbuf)) {
964 if (!td->read_iolog) {
965 td->iolog_file = strdup(tmpbuf);
968 log_err("fio: read iolog overrides given write_iolog\n");
972 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
973 td->exec_prerun = strdup(tmpbuf);
977 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
978 td->exec_postrun = strdup(tmpbuf);
982 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
983 #ifndef FIO_HAVE_IOSCHED_SWITCH
984 log_err("io scheduler switching not available\n");
988 td->ioscheduler = strdup(tmpbuf);
995 * Don't break here, continue parsing options so we
996 * dump all the bad ones. Makes trial/error fixups
997 * easier on the user.
999 printf("Client%d: bad option %s\n",td->thread_number,p);
1005 ret = add_job(td, name, 0);
1018 static int fill_def_thread(void)
1020 memset(&def_thread, 0, sizeof(def_thread));
1022 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1023 perror("sched_getaffinity");
1030 def_thread.ddir = DDIR_READ;
1031 def_thread.iomix = 0;
1032 def_thread.bs = DEF_BS;
1033 def_thread.min_bs = -1;
1034 def_thread.max_bs = -1;
1035 def_thread.odirect = DEF_ODIRECT;
1036 def_thread.ratecycle = DEF_RATE_CYCLE;
1037 def_thread.sequential = DEF_SEQUENTIAL;
1038 def_thread.timeout = def_timeout;
1039 def_thread.overwrite = DEF_OVERWRITE;
1040 def_thread.invalidate_cache = DEF_INVALIDATE;
1041 def_thread.sync_io = DEF_SYNCIO;
1042 def_thread.mem_type = MEM_MALLOC;
1043 def_thread.bw_avg_time = DEF_BWAVGTIME;
1044 def_thread.create_serialize = DEF_CREATE_SER;
1045 def_thread.create_fsync = DEF_CREATE_FSYNC;
1046 def_thread.loops = DEF_LOOPS;
1047 def_thread.verify = DEF_VERIFY;
1048 def_thread.stonewall = DEF_STONEWALL;
1049 def_thread.numjobs = DEF_NUMJOBS;
1050 def_thread.use_thread = DEF_USE_THREAD;
1051 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1052 def_thread.rwmixread = DEF_RWMIX_READ;
1053 def_thread.nice = DEF_NICE;
1054 def_thread.rand_repeatable = DEF_RAND_REPEAT;
1055 def_thread.nr_files = DEF_NR_FILES;
1056 def_thread.unlink = DEF_UNLINK;
1057 def_thread.write_bw_log = write_bw_log;
1058 def_thread.write_lat_log = write_lat_log;
1059 #ifdef FIO_HAVE_DISK_UTIL
1060 def_thread.do_disk_util = 1;
1066 static void usage(void)
1068 printf("%s\n", fio_version_string);
1069 printf("\t-o Write output to file\n");
1070 printf("\t-t Runtime in seconds\n");
1071 printf("\t-l Generate per-job latency logs\n");
1072 printf("\t-w Generate per-job bandwidth logs\n");
1073 printf("\t-m Minimal (terse) output\n");
1074 printf("\t-v Print version info and exit\n");
1077 static int parse_cmd_line(int argc, char *argv[])
1079 int c, idx = 1, ini_idx = 0;
1081 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
1084 def_timeout = atoi(optarg);
1096 f_out = fopen(optarg, "w+");
1098 perror("fopen output");
1112 printf("%s\n", fio_version_string);
1117 while (idx < argc) {
1119 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1120 ini_file[ini_idx - 1] = strdup(argv[idx]);
1132 static void free_shm(void)
1134 struct shmid_ds sbuf;
1137 shmdt((void *) threads);
1139 shmctl(shm_id, IPC_RMID, &sbuf);
1144 * The thread area is shared between the main process and the job
1145 * threads/processes. So setup a shared memory segment that will hold
1148 static int setup_thread_area(void)
1151 * 1024 is too much on some machines, scale max_jobs if
1152 * we get a failure that looks like too large a shm segment
1155 size_t size = max_jobs * sizeof(struct thread_data);
1157 shm_id = shmget(0, size, IPC_CREAT | 0600);
1160 if (errno != EINVAL) {
1171 threads = shmat(shm_id, NULL, 0);
1172 if (threads == (void *) -1) {
1181 int parse_options(int argc, char *argv[])
1185 if (setup_thread_area())
1187 if (fill_def_thread())
1190 job_files = parse_cmd_line(argc, argv);
1192 log_err("Need job file(s)\n");
1197 for (i = 0; i < job_files; i++) {
1198 if (fill_def_thread())
1200 if (parse_jobs_ini(ini_file[i], i))