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;
173 td->nr_uniq_files = 1;
175 td->nr_uniq_files = td->nr_files;
177 if (td->filetype == FIO_TYPE_FILE || td->filename) {
182 if (td->directory && td->directory[0] != '\0')
183 sprintf(tmp, "%s/", td->directory);
185 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
187 for_each_file(td, f, i) {
188 memset(f, 0, sizeof(*f));
192 sprintf(tmp + len, "%s", td->filename);
194 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
195 f->file_name = strdup(tmp);
199 td->files = malloc(sizeof(struct fio_file));
202 memset(f, 0, sizeof(*f));
204 f->file_name = strdup(jobname);
207 for_each_file(td, f, i) {
208 f->file_size = td->total_file_size / td->nr_files;
209 f->file_offset = td->start_offset;
212 fio_sem_init(&td->mutex, 0);
214 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
215 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
216 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
218 if (td->min_bs == -1U)
220 if (td->max_bs == -1U)
222 if (td_read(td) && !td_rw(td))
225 if (td->stonewall && td->thread_number > 1)
228 td->groupid = groupid;
233 if (td->write_lat_log) {
234 setup_log(&td->slat_log);
235 setup_log(&td->clat_log);
237 if (td->write_bw_log)
238 setup_log(&td->bw_log);
240 if (td->name[0] == '\0')
241 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
243 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
247 if (td->io_ops->flags & FIO_CPUIO)
248 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
250 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);
251 } else if (job_add_num == 1)
252 fprintf(f_out, "...\n");
256 * recurse add identical jobs, clear numjobs and stonewall options
257 * as they don't apply to sub-jobs
259 numjobs = td->numjobs;
261 struct thread_data *td_new = get_new_job(0, td);
267 td_new->stonewall = 0;
268 job_add_num = numjobs - 1;
270 if (add_job(td_new, jobname, job_add_num))
280 * Initialize the various random states we need (random io, block size ranges,
281 * read/write mix, etc).
283 int init_random_state(struct thread_data *td)
285 unsigned long seeds[4];
286 int fd, num_maps, blocks, i;
289 fd = open("/dev/urandom", O_RDONLY);
291 td_verror(td, errno);
295 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
303 os_random_seed(seeds[0], &td->bsrange_state);
304 os_random_seed(seeds[1], &td->verify_state);
305 os_random_seed(seeds[2], &td->rwmix_state);
310 if (td->rand_repeatable)
311 seeds[3] = DEF_RANDSEED;
313 for_each_file(td, f, i) {
314 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
315 num_maps = blocks / BLOCKS_PER_MAP;
316 f->file_map = malloc(num_maps * sizeof(long));
317 f->num_maps = num_maps;
318 memset(f->file_map, 0, num_maps * sizeof(long));
321 os_random_seed(seeds[3], &td->random_state);
325 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
327 #ifdef FIO_HAVE_CPU_AFFINITY
332 for (i = 0; i < sizeof(int) * 8; i++) {
334 CPU_SET(i, &cpumask);
339 static unsigned long get_mult_time(char c)
356 static unsigned long get_mult_bytes(char c)
367 return 1024 * 1024 * 1024;
374 * convert string after '=' into decimal value, noting any size suffix
376 static int str_to_decimal(char *p, unsigned long long *val, int kilo)
381 str = strchr(p, '=');
388 *val = strtoul(str, NULL, 10);
389 if (*val == ULONG_MAX && errno == ERANGE)
393 *val *= get_mult_bytes(str[len - 1]);
395 *val *= get_mult_time(str[len - 1]);
399 static int check_str_bytes(char *p, char *name, unsigned long long *val)
401 if (strncmp(p, name, strlen(name) - 1))
404 return str_to_decimal(p, val, 1);
407 static int check_str_time(char *p, char *name, unsigned long long *val)
409 if (strncmp(p, name, strlen(name) - 1))
412 return str_to_decimal(p, val, 0);
415 static void strip_blank_front(char **p)
423 static void strip_blank_end(char *p)
425 char *s = p + strlen(p) - 1;
427 while (isspace(*s) || iscntrl(*s))
433 typedef int (str_cb_fn)(struct thread_data *, char *);
435 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
439 if (strncmp(p, name, strlen(name)))
451 strip_blank_front(&s);
455 static int check_strstore(char *p, char *name, char *dest)
459 if (strncmp(p, name, strlen(name)))
471 strip_blank_front(&s);
477 static int __check_range_bytes(char *str, unsigned long *val)
481 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
482 *val *= get_mult_bytes(suffix);
486 if (sscanf(str, "%lu", val) == 1)
492 static int check_range_bytes(char *p, char *name, unsigned long *s,
498 if (strncmp(p, name, strlen(name)))
504 str = strstr(p, name);
510 str = strchr(p, '=');
515 * 'p' now holds whatever is after the '=' sign
520 * terminate p1 at the '-' sign
529 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
535 static int check_int(char *p, char *name, unsigned int *val)
539 if (strncmp(p, name, strlen(name)))
542 str = strstr(p, name);
546 str = strchr(p, '=');
552 if (sscanf(str, "%u", val) == 1)
558 static int check_strset(char *p, char *name)
560 return strncmp(p, name, strlen(name));
563 static int is_empty_or_comment(char *line)
567 for (i = 0; i < strlen(line); i++) {
570 if (!isspace(line[i]) && !iscntrl(line[i]))
577 static int str_rw_cb(struct thread_data *td, char *mem)
579 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
580 td->ddir = DDIR_READ;
583 } else if (!strncmp(mem, "randread", 8)) {
584 td->ddir = DDIR_READ;
587 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
588 td->ddir = DDIR_WRITE;
591 } else if (!strncmp(mem, "randwrite", 9)) {
592 td->ddir = DDIR_WRITE;
595 } else if (!strncmp(mem, "rw", 2)) {
600 } else if (!strncmp(mem, "randrw", 6)) {
607 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
611 static int str_verify_cb(struct thread_data *td, char *mem)
613 if (!strncmp(mem, "0", 1)) {
614 td->verify = VERIFY_NONE;
616 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
617 td->verify = VERIFY_MD5;
619 } else if (!strncmp(mem, "crc32", 5)) {
620 td->verify = VERIFY_CRC32;
624 log_err("fio: verify types: md5, crc32\n");
628 static int str_mem_cb(struct thread_data *td, char *mem)
630 if (!strncmp(mem, "malloc", 6)) {
631 td->mem_type = MEM_MALLOC;
633 } else if (!strncmp(mem, "shm", 3)) {
634 td->mem_type = MEM_SHM;
636 } else if (!strncmp(mem, "mmap", 4)) {
637 td->mem_type = MEM_MMAP;
641 log_err("fio: mem type: malloc, shm, mmap\n");
645 static int str_ioengine_cb(struct thread_data *td, char *str)
647 td->io_ops = load_ioengine(td, str);
651 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
656 * This is our [ini] type file parser.
658 int parse_jobs_ini(char *file, int stonewall_flag)
660 unsigned int prioclass, prio, cpu, global, il;
661 unsigned long long ull;
662 unsigned long ul1, ul2;
663 struct thread_data *td;
664 char *string, *name, *tmpbuf;
668 int ret = 0, stonewall;
670 f = fopen(file, "r");
672 perror("fopen job file");
676 string = malloc(4096);
678 tmpbuf = malloc(4096);
680 stonewall = stonewall_flag;
681 while ((p = fgets(string, 4096, f)) != NULL) {
684 if (is_empty_or_comment(p))
686 if (sscanf(p, "[%s]", name) != 1)
689 global = !strncmp(name, "global", 6);
691 name[strlen(name) - 1] = '\0';
693 td = get_new_job(global, &def_thread);
700 * Seperate multiple job files by a stonewall
702 if (!global && stonewall) {
703 td->stonewall = stonewall;
708 while ((p = fgets(string, 4096, f)) != NULL) {
709 if (is_empty_or_comment(p))
713 strip_blank_front(&p);
716 if (!check_int(p, "prio", &prio)) {
717 #ifndef FIO_HAVE_IOPRIO
718 log_err("io priorities not available\n");
726 if (!check_int(p, "prioclass", &prioclass)) {
727 #ifndef FIO_HAVE_IOPRIO
728 log_err("io priorities not available\n");
732 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
737 if (!check_int(p, "direct", &il)) {
742 if (!check_int(p, "rand_repeatable", &il)) {
743 td->rand_repeatable = il;
747 if (!check_int(p, "rate", &td->rate)) {
751 if (!check_int(p, "ratemin", &td->ratemin)) {
755 if (!check_int(p, "ratecycle", &td->ratecycle)) {
759 if (!check_int(p, "cpuload", &td->cpuload)) {
763 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
767 if (!check_int(p, "thinktime", &td->thinktime)) {
771 if (!check_int(p, "cpumask", &cpu)) {
772 #ifndef FIO_HAVE_CPU_AFFINITY
773 log_err("cpu affinity not available\n");
777 fill_cpu_mask(td->cpumask, cpu);
781 if (!check_int(p, "fsync", &td->fsync_blocks)) {
786 if (!check_int(p, "startdelay", &td->start_delay)) {
790 if (!check_str_time(p, "timeout", &ull)) {
795 if (!check_int(p, "invalidate", &il)) {
796 td->invalidate_cache = il;
800 if (!check_int(p, "iodepth", &td->iodepth)) {
804 if (!check_int(p, "sync", &il)) {
809 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
813 if (!check_int(p, "create_serialize", &il)) {
814 td->create_serialize = il;
818 if (!check_int(p, "create_fsync", &il)) {
819 td->create_fsync = il;
823 if (!check_int(p, "end_fsync", &il)) {
828 if (!check_int(p, "loops", &td->loops)) {
832 if (!check_int(p, "numjobs", &td->numjobs)) {
836 if (!check_int(p, "overwrite", &il)) {
841 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
845 if (!check_int(p, "rwmixread", &il)) {
852 if (!check_int(p, "rwmixwrite", &il)) {
855 td->rwmixread = 100 - il;
859 if (!check_int(p, "nice", &td->nice)) {
863 if (!check_int(p, "nrfiles", &td->nr_files)) {
867 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
878 if (!check_str_bytes(p, "bs", &ull)) {
883 if (!check_str_bytes(p, "size", &td->total_file_size)) {
887 if (!check_str_bytes(p, "offset", &td->start_offset)) {
891 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
895 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
899 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
903 if (!check_strstore(p, "directory", tmpbuf)) {
904 td->directory = strdup(tmpbuf);
908 if (!check_strstore(p, "filename", tmpbuf)) {
909 td->filename = strdup(tmpbuf);
913 if (!check_strstore(p, "name", tmpbuf)) {
914 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
918 if (!check_str(p, "mem", str_mem_cb, td)) {
922 if (!check_str(p, "verify", str_verify_cb, td)) {
926 if (!check_str(p, "rw", str_rw_cb, td)) {
930 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
934 if (!check_strset(p, "exitall")) {
935 exitall_on_terminate = 1;
939 if (!check_strset(p, "stonewall")) {
944 if (!check_strset(p, "thread")) {
949 if (!check_strset(p, "unlink")) {
954 if (!check_strset(p, "write_bw_log")) {
955 td->write_bw_log = 1;
959 if (!check_strset(p, "write_lat_log")) {
960 td->write_lat_log = 1;
964 if (!check_strstore(p, "iolog", tmpbuf)) {
965 if (td->write_iolog) {
966 log_err("fio: read iolog overrides given write_iolog\n");
967 free(td->iolog_file);
970 td->iolog_file = strdup(tmpbuf);
975 if (!check_strstore(p, "write_iolog", tmpbuf)) {
976 if (!td->read_iolog) {
977 td->iolog_file = strdup(tmpbuf);
980 log_err("fio: read iolog overrides given write_iolog\n");
984 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
985 td->exec_prerun = strdup(tmpbuf);
989 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
990 td->exec_postrun = strdup(tmpbuf);
994 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
995 #ifndef FIO_HAVE_IOSCHED_SWITCH
996 log_err("io scheduler switching not available\n");
1000 td->ioscheduler = strdup(tmpbuf);
1007 * Don't break here, continue parsing options so we
1008 * dump all the bad ones. Makes trial/error fixups
1009 * easier on the user.
1011 printf("Client%d: bad option %s\n",td->thread_number,p);
1017 ret = add_job(td, name, 0);
1030 static int fill_def_thread(void)
1032 memset(&def_thread, 0, sizeof(def_thread));
1034 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1035 perror("sched_getaffinity");
1042 def_thread.ddir = DDIR_READ;
1043 def_thread.iomix = 0;
1044 def_thread.bs = DEF_BS;
1045 def_thread.min_bs = -1;
1046 def_thread.max_bs = -1;
1047 def_thread.odirect = DEF_ODIRECT;
1048 def_thread.ratecycle = DEF_RATE_CYCLE;
1049 def_thread.sequential = DEF_SEQUENTIAL;
1050 def_thread.timeout = def_timeout;
1051 def_thread.overwrite = DEF_OVERWRITE;
1052 def_thread.invalidate_cache = DEF_INVALIDATE;
1053 def_thread.sync_io = DEF_SYNCIO;
1054 def_thread.mem_type = MEM_MALLOC;
1055 def_thread.bw_avg_time = DEF_BWAVGTIME;
1056 def_thread.create_serialize = DEF_CREATE_SER;
1057 def_thread.create_fsync = DEF_CREATE_FSYNC;
1058 def_thread.loops = DEF_LOOPS;
1059 def_thread.verify = DEF_VERIFY;
1060 def_thread.stonewall = DEF_STONEWALL;
1061 def_thread.numjobs = DEF_NUMJOBS;
1062 def_thread.use_thread = DEF_USE_THREAD;
1063 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1064 def_thread.rwmixread = DEF_RWMIX_READ;
1065 def_thread.nice = DEF_NICE;
1066 def_thread.rand_repeatable = DEF_RAND_REPEAT;
1067 def_thread.nr_files = DEF_NR_FILES;
1068 def_thread.unlink = DEF_UNLINK;
1069 def_thread.write_bw_log = write_bw_log;
1070 def_thread.write_lat_log = write_lat_log;
1071 #ifdef FIO_HAVE_DISK_UTIL
1072 def_thread.do_disk_util = 1;
1078 static void usage(void)
1080 printf("%s\n", fio_version_string);
1081 printf("\t-o Write output to file\n");
1082 printf("\t-t Runtime in seconds\n");
1083 printf("\t-l Generate per-job latency logs\n");
1084 printf("\t-w Generate per-job bandwidth logs\n");
1085 printf("\t-m Minimal (terse) output\n");
1086 printf("\t-v Print version info and exit\n");
1089 static int parse_cmd_line(int argc, char *argv[])
1091 int c, idx = 1, ini_idx = 0;
1093 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
1096 def_timeout = atoi(optarg);
1108 f_out = fopen(optarg, "w+");
1110 perror("fopen output");
1124 printf("%s\n", fio_version_string);
1129 while (idx < argc) {
1131 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1132 ini_file[ini_idx - 1] = strdup(argv[idx]);
1144 static void free_shm(void)
1146 struct shmid_ds sbuf;
1149 shmdt((void *) threads);
1151 shmctl(shm_id, IPC_RMID, &sbuf);
1156 * The thread area is shared between the main process and the job
1157 * threads/processes. So setup a shared memory segment that will hold
1160 static int setup_thread_area(void)
1163 * 1024 is too much on some machines, scale max_jobs if
1164 * we get a failure that looks like too large a shm segment
1167 size_t size = max_jobs * sizeof(struct thread_data);
1169 shm_id = shmget(0, size, IPC_CREAT | 0600);
1172 if (errno != EINVAL) {
1183 threads = shmat(shm_id, NULL, 0);
1184 if (threads == (void *) -1) {
1193 int parse_options(int argc, char *argv[])
1197 if (setup_thread_area())
1199 if (fill_def_thread())
1202 job_files = parse_cmd_line(argc, argv);
1204 log_err("Need job file(s)\n");
1209 for (i = 0; i < job_files; i++) {
1210 if (fill_def_thread())
1212 if (parse_jobs_ini(ini_file[i], i))