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)
50 #define DEF_UNLINK (0)
52 static int def_timeout = DEF_TIMEOUT;
54 static char fio_version_string[] = "fio 1.5";
56 static char **ini_file;
57 static int max_jobs = MAX_JOBS;
59 struct thread_data def_thread;
60 struct thread_data *threads = NULL;
63 int write_lat_log = 0;
65 int exitall_on_terminate = 0;
67 unsigned long long mlock_size = 0;
72 * Return a free job structure.
74 static struct thread_data *get_new_job(int global, struct thread_data *parent)
76 struct thread_data *td;
80 if (thread_number >= max_jobs)
83 td = &threads[thread_number++];
87 td->thread_number = thread_number;
91 static void put_job(struct thread_data *td)
93 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
98 * Adds a job to the list of things todo. Sanitizes the various options
99 * to make sure we don't have conflicts, and initializes various
102 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
104 char *ddir_str[] = { "read", "write", "randread", "randwrite",
105 "rw", NULL, "randrw" };
107 int numjobs, ddir, i;
110 #ifndef FIO_HAVE_LIBAIO
111 if (td->io_engine == FIO_LIBAIO) {
112 log_err("Linux libaio not available\n");
116 #ifndef FIO_HAVE_POSIXAIO
117 if (td->io_engine == FIO_POSIXAIO) {
118 log_err("posix aio not available\n");
124 * the def_thread is just for options, it's not a real job
126 if (td == &def_thread)
130 * Set default io engine, if none set
133 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
135 log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
140 if (td->io_ops->flags & FIO_SYNCIO)
148 * only really works for sequential io for now, and with 1 file
150 if (td->zone_size && !td->sequential && td->nr_files == 1)
154 * Reads can do overwrites, we always need to pre-create the file
156 if (td_read(td) || td_rw(td))
159 td->filetype = FIO_TYPE_FILE;
160 if (!stat(jobname, &sb)) {
161 if (S_ISBLK(sb.st_mode))
162 td->filetype = FIO_TYPE_BD;
163 else if (S_ISCHR(sb.st_mode))
164 td->filetype = FIO_TYPE_CHAR;
168 td->io_ops->flags |= FIO_RAWIO;
170 if (td->filetype == FIO_TYPE_FILE) {
175 if (td->directory && td->directory[0] != '\0')
176 sprintf(tmp, "%s/", td->directory);
178 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
180 for_each_file(td, f, i) {
181 memset(f, 0, sizeof(*f));
185 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
186 f->file_name = strdup(tmp);
190 td->files = malloc(sizeof(struct fio_file));
193 memset(f, 0, sizeof(*f));
195 f->file_name = strdup(jobname);
198 for_each_file(td, f, i) {
199 f->file_size = td->total_file_size / td->nr_files;
200 f->file_offset = td->start_offset;
203 fio_sem_init(&td->mutex, 0);
205 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
206 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
207 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
209 if (td->min_bs == -1U)
211 if (td->max_bs == -1U)
213 if (td_read(td) && !td_rw(td))
216 if (td->stonewall && td->thread_number > 1)
219 td->groupid = groupid;
225 setup_log(&td->slat_log);
226 setup_log(&td->clat_log);
229 setup_log(&td->bw_log);
231 if (td->name[0] == '\0')
232 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
234 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
238 if (td->io_ops->flags & FIO_CPUIO)
239 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
241 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);
242 } else if (job_add_num == 1)
243 fprintf(f_out, "...\n");
247 * recurse add identical jobs, clear numjobs and stonewall options
248 * as they don't apply to sub-jobs
250 numjobs = td->numjobs;
252 struct thread_data *td_new = get_new_job(0, td);
258 td_new->stonewall = 0;
259 job_add_num = numjobs - 1;
261 if (add_job(td_new, jobname, job_add_num))
271 * Initialize the various random states we need (random io, block size ranges,
272 * read/write mix, etc).
274 int init_random_state(struct thread_data *td)
276 unsigned long seeds[4];
277 int fd, num_maps, blocks, i;
280 fd = open("/dev/urandom", O_RDONLY);
282 td_verror(td, errno);
286 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
294 os_random_seed(seeds[0], &td->bsrange_state);
295 os_random_seed(seeds[1], &td->verify_state);
296 os_random_seed(seeds[2], &td->rwmix_state);
301 if (td->rand_repeatable)
302 seeds[3] = DEF_RANDSEED;
304 for_each_file(td, f, i) {
305 blocks = (f->file_size + td->min_bs - 1) / td->min_bs;
306 num_maps = blocks / BLOCKS_PER_MAP;
307 f->file_map = malloc(num_maps * sizeof(long));
308 f->num_maps = num_maps;
309 memset(f->file_map, 0, num_maps * sizeof(long));
312 os_random_seed(seeds[3], &td->random_state);
316 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
318 #ifdef FIO_HAVE_CPU_AFFINITY
323 for (i = 0; i < sizeof(int) * 8; i++) {
325 CPU_SET(i, &cpumask);
330 static unsigned long get_mult_time(char c)
347 static unsigned long get_mult_bytes(char c)
358 return 1024 * 1024 * 1024;
365 * convert string after '=' into decimal value, noting any size suffix
367 static int str_to_decimal(char *p, unsigned long long *val, int kilo)
372 str = strchr(p, '=');
379 *val = strtoul(str, NULL, 10);
380 if (*val == ULONG_MAX && errno == ERANGE)
384 *val *= get_mult_bytes(str[len - 1]);
386 *val *= get_mult_time(str[len - 1]);
390 static int check_str_bytes(char *p, char *name, unsigned long long *val)
392 if (strncmp(p, name, strlen(name) - 1))
395 return str_to_decimal(p, val, 1);
398 static int check_str_time(char *p, char *name, unsigned long long *val)
400 if (strncmp(p, name, strlen(name) - 1))
403 return str_to_decimal(p, val, 0);
406 static void strip_blank_front(char **p)
414 static void strip_blank_end(char *p)
416 char *s = p + strlen(p) - 1;
418 while (isspace(*s) || iscntrl(*s))
424 typedef int (str_cb_fn)(struct thread_data *, char *);
426 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
430 if (strncmp(p, name, strlen(name)))
442 strip_blank_front(&s);
446 static int check_strstore(char *p, char *name, char *dest)
450 if (strncmp(p, name, strlen(name)))
462 strip_blank_front(&s);
468 static int __check_range_bytes(char *str, unsigned long *val)
472 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
473 *val *= get_mult_bytes(suffix);
477 if (sscanf(str, "%lu", val) == 1)
483 static int check_range_bytes(char *p, char *name, unsigned long *s,
489 if (strncmp(p, name, strlen(name)))
495 str = strstr(p, name);
501 str = strchr(p, '=');
506 * 'p' now holds whatever is after the '=' sign
511 * terminate p1 at the '-' sign
520 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
526 static int check_int(char *p, char *name, unsigned int *val)
530 if (strncmp(p, name, strlen(name)))
533 str = strstr(p, name);
537 str = strchr(p, '=');
543 if (sscanf(str, "%u", val) == 1)
549 static int check_strset(char *p, char *name)
551 return strncmp(p, name, strlen(name));
554 static int is_empty_or_comment(char *line)
558 for (i = 0; i < strlen(line); i++) {
561 if (!isspace(line[i]) && !iscntrl(line[i]))
568 static int str_rw_cb(struct thread_data *td, char *mem)
570 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
571 td->ddir = DDIR_READ;
574 } else if (!strncmp(mem, "randread", 8)) {
575 td->ddir = DDIR_READ;
578 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
579 td->ddir = DDIR_WRITE;
582 } else if (!strncmp(mem, "randwrite", 9)) {
583 td->ddir = DDIR_WRITE;
586 } else if (!strncmp(mem, "rw", 2)) {
591 } else if (!strncmp(mem, "randrw", 6)) {
598 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
602 static int str_verify_cb(struct thread_data *td, char *mem)
604 if (!strncmp(mem, "0", 1)) {
605 td->verify = VERIFY_NONE;
607 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
608 td->verify = VERIFY_MD5;
610 } else if (!strncmp(mem, "crc32", 5)) {
611 td->verify = VERIFY_CRC32;
615 log_err("fio: verify types: md5, crc32\n");
619 static int str_mem_cb(struct thread_data *td, char *mem)
621 if (!strncmp(mem, "malloc", 6)) {
622 td->mem_type = MEM_MALLOC;
624 } else if (!strncmp(mem, "shm", 3)) {
625 td->mem_type = MEM_SHM;
627 } else if (!strncmp(mem, "mmap", 4)) {
628 td->mem_type = MEM_MMAP;
632 log_err("fio: mem type: malloc, shm, mmap\n");
636 static int str_ioengine_cb(struct thread_data *td, char *str)
638 td->io_ops = load_ioengine(td, str);
642 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
647 * This is our [ini] type file parser.
649 int parse_jobs_ini(char *file, int stonewall_flag)
651 unsigned int prioclass, prio, cpu, global, il;
652 unsigned long long ull;
653 unsigned long ul1, ul2;
654 struct thread_data *td;
655 char *string, *name, *tmpbuf;
659 int ret = 0, stonewall;
661 f = fopen(file, "r");
663 perror("fopen job file");
667 string = malloc(4096);
669 tmpbuf = malloc(4096);
671 stonewall = stonewall_flag;
672 while ((p = fgets(string, 4096, f)) != NULL) {
675 if (is_empty_or_comment(p))
677 if (sscanf(p, "[%s]", name) != 1)
680 global = !strncmp(name, "global", 6);
682 name[strlen(name) - 1] = '\0';
684 td = get_new_job(global, &def_thread);
691 * Seperate multiple job files by a stonewall
693 if (!global && stonewall) {
694 td->stonewall = stonewall;
699 while ((p = fgets(string, 4096, f)) != NULL) {
700 if (is_empty_or_comment(p))
704 strip_blank_front(&p);
707 if (!check_int(p, "prio", &prio)) {
708 #ifndef FIO_HAVE_IOPRIO
709 log_err("io priorities not available\n");
717 if (!check_int(p, "prioclass", &prioclass)) {
718 #ifndef FIO_HAVE_IOPRIO
719 log_err("io priorities not available\n");
723 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
728 if (!check_int(p, "direct", &il)) {
733 if (!check_int(p, "rand_repeatable", &il)) {
734 td->rand_repeatable = il;
738 if (!check_int(p, "rate", &td->rate)) {
742 if (!check_int(p, "ratemin", &td->ratemin)) {
746 if (!check_int(p, "ratecycle", &td->ratecycle)) {
750 if (!check_int(p, "cpuload", &td->cpuload)) {
754 if (!check_int(p, "cpuchunks", &td->cpucycle)) {
758 if (!check_int(p, "thinktime", &td->thinktime)) {
762 if (!check_int(p, "cpumask", &cpu)) {
763 #ifndef FIO_HAVE_CPU_AFFINITY
764 log_err("cpu affinity not available\n");
768 fill_cpu_mask(td->cpumask, cpu);
772 if (!check_int(p, "fsync", &td->fsync_blocks)) {
777 if (!check_int(p, "startdelay", &td->start_delay)) {
781 if (!check_str_time(p, "timeout", &ull)) {
786 if (!check_int(p, "invalidate", &il)) {
787 td->invalidate_cache = il;
791 if (!check_int(p, "iodepth", &td->iodepth)) {
795 if (!check_int(p, "sync", &il)) {
800 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
804 if (!check_int(p, "create_serialize", &il)) {
805 td->create_serialize = il;
809 if (!check_int(p, "create_fsync", &il)) {
810 td->create_fsync = il;
814 if (!check_int(p, "end_fsync", &il)) {
819 if (!check_int(p, "loops", &td->loops)) {
823 if (!check_int(p, "numjobs", &td->numjobs)) {
827 if (!check_int(p, "overwrite", &il)) {
832 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
836 if (!check_int(p, "rwmixread", &il)) {
843 if (!check_int(p, "rwmixwrite", &il)) {
846 td->rwmixread = 100 - il;
850 if (!check_int(p, "nice", &td->nice)) {
854 if (!check_int(p, "nrfiles", &td->nr_files)) {
858 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
869 if (!check_str_bytes(p, "bs", &ull)) {
874 if (!check_str_bytes(p, "size", &td->total_file_size)) {
878 if (!check_str_bytes(p, "offset", &td->start_offset)) {
882 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
886 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
890 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
894 if (!check_strstore(p, "directory", tmpbuf)) {
895 td->directory = strdup(tmpbuf);
899 if (!check_strstore(p, "name", tmpbuf)) {
900 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
904 if (!check_str(p, "mem", str_mem_cb, td)) {
908 if (!check_str(p, "verify", str_verify_cb, td)) {
912 if (!check_str(p, "rw", str_rw_cb, td)) {
916 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
920 if (!check_strset(p, "create")) {
925 if (!check_strset(p, "exitall")) {
926 exitall_on_terminate = 1;
930 if (!check_strset(p, "stonewall")) {
935 if (!check_strset(p, "thread")) {
940 if (!check_strset(p, "unlink")) {
945 if (!check_strstore(p, "iolog", tmpbuf)) {
946 if (td->write_iolog) {
947 log_err("fio: read iolog overrides given write_iolog\n");
948 free(td->iolog_file);
951 td->iolog_file = strdup(tmpbuf);
956 if (!check_strstore(p, "write_iolog", tmpbuf)) {
957 if (!td->read_iolog) {
958 td->iolog_file = strdup(tmpbuf);
961 log_err("fio: read iolog overrides given write_iolog\n");
965 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
966 td->exec_prerun = strdup(tmpbuf);
970 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
971 td->exec_postrun = strdup(tmpbuf);
975 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
976 #ifndef FIO_HAVE_IOSCHED_SWITCH
977 log_err("io scheduler switching not available\n");
981 td->ioscheduler = strdup(tmpbuf);
988 * Don't break here, continue parsing options so we
989 * dump all the bad ones. Makes trial/error fixups
990 * easier on the user.
992 printf("Client%d: bad option %s\n",td->thread_number,p);
998 ret = add_job(td, name, 0);
1011 static int fill_def_thread(void)
1013 memset(&def_thread, 0, sizeof(def_thread));
1015 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1016 perror("sched_getaffinity");
1023 def_thread.ddir = DDIR_READ;
1024 def_thread.iomix = 0;
1025 def_thread.bs = DEF_BS;
1026 def_thread.min_bs = -1;
1027 def_thread.max_bs = -1;
1028 def_thread.odirect = DEF_ODIRECT;
1029 def_thread.ratecycle = DEF_RATE_CYCLE;
1030 def_thread.sequential = DEF_SEQUENTIAL;
1031 def_thread.timeout = def_timeout;
1032 def_thread.create_file = DEF_CREATE;
1033 def_thread.overwrite = DEF_OVERWRITE;
1034 def_thread.invalidate_cache = DEF_INVALIDATE;
1035 def_thread.sync_io = DEF_SYNCIO;
1036 def_thread.mem_type = MEM_MALLOC;
1037 def_thread.bw_avg_time = DEF_BWAVGTIME;
1038 def_thread.create_serialize = DEF_CREATE_SER;
1039 def_thread.create_fsync = DEF_CREATE_FSYNC;
1040 def_thread.loops = DEF_LOOPS;
1041 def_thread.verify = DEF_VERIFY;
1042 def_thread.stonewall = DEF_STONEWALL;
1043 def_thread.numjobs = DEF_NUMJOBS;
1044 def_thread.use_thread = DEF_USE_THREAD;
1045 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1046 def_thread.rwmixread = DEF_RWMIX_READ;
1047 def_thread.nice = DEF_NICE;
1048 def_thread.rand_repeatable = DEF_RAND_REPEAT;
1049 def_thread.nr_files = DEF_NR_FILES;
1050 def_thread.unlink = DEF_UNLINK;
1051 #ifdef FIO_HAVE_DISK_UTIL
1052 def_thread.do_disk_util = 1;
1058 static void usage(void)
1060 printf("%s\n", fio_version_string);
1061 printf("\t-o Write output to file\n");
1062 printf("\t-t Runtime in seconds\n");
1063 printf("\t-l Generate per-job latency logs\n");
1064 printf("\t-w Generate per-job bandwidth logs\n");
1065 printf("\t-m Minimal (terse) output\n");
1066 printf("\t-v Print version info and exit\n");
1069 static int parse_cmd_line(int argc, char *argv[])
1071 int c, idx = 1, ini_idx = 0;
1073 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
1076 def_timeout = atoi(optarg);
1088 f_out = fopen(optarg, "w+");
1090 perror("fopen output");
1104 printf("%s\n", fio_version_string);
1109 while (idx < argc) {
1111 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1112 ini_file[ini_idx - 1] = strdup(argv[idx]);
1124 static void free_shm(void)
1126 struct shmid_ds sbuf;
1129 shmdt((void *) threads);
1131 shmctl(shm_id, IPC_RMID, &sbuf);
1136 * The thread area is shared between the main process and the job
1137 * threads/processes. So setup a shared memory segment that will hold
1140 static int setup_thread_area(void)
1143 * 1024 is too much on some machines, scale max_jobs if
1144 * we get a failure that looks like too large a shm segment
1147 size_t size = max_jobs * sizeof(struct thread_data);
1149 shm_id = shmget(0, size, IPC_CREAT | 0600);
1152 if (errno != EINVAL) {
1163 threads = shmat(shm_id, NULL, 0);
1164 if (threads == (void *) -1) {
1173 int parse_options(int argc, char *argv[])
1177 if (setup_thread_area())
1179 if (fill_def_thread())
1182 job_files = parse_cmd_line(argc, argv);
1184 log_err("Need job file(s)\n");
1189 for (i = 0; i < job_files; i++) {
1190 if (fill_def_thread())
1192 if (parse_jobs_ini(ini_file[i], i))