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)
50 static int def_timeout = DEF_TIMEOUT;
52 static char fio_version_string[] = "fio 1.5";
54 static char **ini_file;
55 static int max_jobs = MAX_JOBS;
57 struct thread_data def_thread;
58 struct thread_data *threads = NULL;
61 int write_lat_log = 0;
63 int exitall_on_terminate = 0;
65 unsigned long long mlock_size = 0;
70 * Return a free job structure.
72 static struct thread_data *get_new_job(int global, struct thread_data *parent)
74 struct thread_data *td;
78 if (thread_number >= max_jobs)
81 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" };
108 #ifndef FIO_HAVE_LIBAIO
109 if (td->io_engine == FIO_LIBAIO) {
110 log_err("Linux libaio not available\n");
114 #ifndef FIO_HAVE_POSIXAIO
115 if (td->io_engine == FIO_POSIXAIO) {
116 log_err("posix aio not available\n");
122 * the def_thread is just for options, it's not a real job
124 if (td == &def_thread)
127 if (td->io_engine & FIO_SYNCIO)
135 * only really works for sequential io for now
137 if (td->zone_size && !td->sequential)
141 * Reads can do overwrites, we always need to pre-create the file
143 if (td_read(td) || td_rw(td))
146 td->filetype = FIO_TYPE_FILE;
147 if (!stat(jobname, &sb)) {
148 if (S_ISBLK(sb.st_mode))
149 td->filetype = FIO_TYPE_BD;
150 else if (S_ISCHR(sb.st_mode))
151 td->filetype = FIO_TYPE_CHAR;
154 if (td->filetype == FIO_TYPE_FILE) {
157 if (td->directory && td->directory[0] != '\0')
158 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->thread_number);
160 sprintf(tmp, "%s.%d", jobname, td->thread_number);
161 td->file_name = strdup(tmp);
163 td->file_name = strdup(jobname);
165 fio_sem_init(&td->mutex, 0);
167 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
168 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
169 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
171 if (td->min_bs == -1U)
173 if (td->max_bs == -1U)
175 if (td_read(td) && !td_rw(td))
178 if (td->stonewall && td->thread_number > 1)
181 td->groupid = groupid;
187 setup_log(&td->slat_log);
188 setup_log(&td->clat_log);
191 setup_log(&td->bw_log);
193 if (td->name[0] == '\0')
194 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
196 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
200 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_engine_name, td->iodepth);
201 else if (job_add_num == 1)
202 fprintf(f_out, "...\n");
206 * recurse add identical jobs, clear numjobs and stonewall options
207 * as they don't apply to sub-jobs
209 numjobs = td->numjobs;
211 struct thread_data *td_new = get_new_job(0, td);
217 td_new->stonewall = 0;
218 job_add_num = numjobs - 1;
220 if (add_job(td_new, jobname, job_add_num))
230 * Initialize the various random states we need (random io, block size ranges,
231 * read/write mix, etc).
233 int init_random_state(struct thread_data *td)
235 unsigned long seeds[4];
236 int fd, num_maps, blocks;
238 fd = open("/dev/urandom", O_RDONLY);
240 td_verror(td, errno);
244 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
252 os_random_seed(seeds[0], &td->bsrange_state);
253 os_random_seed(seeds[1], &td->verify_state);
254 os_random_seed(seeds[2], &td->rwmix_state);
259 if (td->rand_repeatable)
260 seeds[3] = DEF_RANDSEED;
262 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
263 num_maps = blocks / BLOCKS_PER_MAP;
264 td->file_map = malloc(num_maps * sizeof(long));
265 td->num_maps = num_maps;
266 memset(td->file_map, 0, num_maps * sizeof(long));
268 os_random_seed(seeds[3], &td->random_state);
272 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
274 #ifdef FIO_HAVE_CPU_AFFINITY
279 for (i = 0; i < sizeof(int) * 8; i++) {
281 CPU_SET(i, &cpumask);
286 static unsigned long get_mult_time(char c)
303 static unsigned long get_mult_bytes(char c)
314 return 1024 * 1024 * 1024;
321 * convert string after '=' into decimal value, noting any size suffix
323 static int str_to_decimal(char *p, unsigned long long *val, int kilo)
328 str = strchr(p, '=');
335 *val = strtoul(str, NULL, 10);
336 if (*val == ULONG_MAX && errno == ERANGE)
340 *val *= get_mult_bytes(str[len - 1]);
342 *val *= get_mult_time(str[len - 1]);
346 static int check_str_bytes(char *p, char *name, unsigned long long *val)
348 if (strncmp(p, name, strlen(name) - 1))
351 return str_to_decimal(p, val, 1);
354 static int check_str_time(char *p, char *name, unsigned long long *val)
356 if (strncmp(p, name, strlen(name) - 1))
359 return str_to_decimal(p, val, 0);
362 static void strip_blank_front(char **p)
370 static void strip_blank_end(char *p)
372 char *s = p + strlen(p) - 1;
374 while (isspace(*s) || iscntrl(*s))
380 typedef int (str_cb_fn)(struct thread_data *, char *);
382 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
386 if (strncmp(p, name, strlen(name)))
398 strip_blank_front(&s);
402 static int check_strstore(char *p, char *name, char *dest)
406 if (strncmp(p, name, strlen(name)))
418 strip_blank_front(&s);
424 static int __check_range_bytes(char *str, unsigned long *val)
428 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
429 *val *= get_mult_bytes(suffix);
433 if (sscanf(str, "%lu", val) == 1)
439 static int check_range_bytes(char *p, char *name, unsigned long *s,
445 if (strncmp(p, name, strlen(name)))
451 str = strstr(p, name);
457 str = strchr(p, '=');
462 * 'p' now holds whatever is after the '=' sign
467 * terminate p1 at the '-' sign
476 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
482 static int check_int(char *p, char *name, unsigned int *val)
486 if (strncmp(p, name, strlen(name)))
489 str = strstr(p, name);
493 str = strchr(p, '=');
499 if (sscanf(str, "%u", val) == 1)
505 static int check_strset(char *p, char *name)
507 return strncmp(p, name, strlen(name));
510 static int is_empty_or_comment(char *line)
514 for (i = 0; i < strlen(line); i++) {
517 if (!isspace(line[i]) && !iscntrl(line[i]))
524 static int str_rw_cb(struct thread_data *td, char *mem)
526 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
527 td->ddir = DDIR_READ;
530 } else if (!strncmp(mem, "randread", 8)) {
531 td->ddir = DDIR_READ;
534 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
535 td->ddir = DDIR_WRITE;
538 } else if (!strncmp(mem, "randwrite", 9)) {
539 td->ddir = DDIR_WRITE;
542 } else if (!strncmp(mem, "rw", 2)) {
547 } else if (!strncmp(mem, "randrw", 6)) {
554 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
558 static int str_verify_cb(struct thread_data *td, char *mem)
560 if (!strncmp(mem, "0", 1)) {
561 td->verify = VERIFY_NONE;
563 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
564 td->verify = VERIFY_MD5;
566 } else if (!strncmp(mem, "crc32", 5)) {
567 td->verify = VERIFY_CRC32;
571 log_err("fio: verify types: md5, crc32\n");
575 static int str_mem_cb(struct thread_data *td, char *mem)
577 if (!strncmp(mem, "malloc", 6)) {
578 td->mem_type = MEM_MALLOC;
580 } else if (!strncmp(mem, "shm", 3)) {
581 td->mem_type = MEM_SHM;
583 } else if (!strncmp(mem, "mmap", 4)) {
584 td->mem_type = MEM_MMAP;
588 log_err("fio: mem type: malloc, shm, mmap\n");
592 static int str_ioengine_cb(struct thread_data *td, char *str)
594 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
595 !strncmp(str, "libaio", 6)) {
596 strcpy(td->io_engine_name, "libaio");
597 td->io_engine = FIO_LIBAIO;
599 } else if (!strncmp(str, "posixaio", 8)) {
600 strcpy(td->io_engine_name, "posixaio");
601 td->io_engine = FIO_POSIXAIO;
603 } else if (!strncmp(str, "sync", 4)) {
604 strcpy(td->io_engine_name, "sync");
605 td->io_engine = FIO_SYNCIO;
607 } else if (!strncmp(str, "mmap", 4)) {
608 strcpy(td->io_engine_name, "mmap");
609 td->io_engine = FIO_MMAPIO;
611 } else if (!strncmp(str, "sgio", 4)) {
612 strcpy(td->io_engine_name, "sgio");
613 td->io_engine = FIO_SGIO;
615 } else if (!strncmp(str, "splice", 6)) {
616 strcpy(td->io_engine_name, "splice");
617 td->io_engine = FIO_SPLICEIO;
621 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
626 * This is our [ini] type file parser.
628 int parse_jobs_ini(char *file, int stonewall_flag)
630 unsigned int prioclass, prio, cpu, global, il;
631 unsigned long long ull;
632 unsigned long ul1, ul2;
633 struct thread_data *td;
634 char *string, *name, *tmpbuf;
638 int ret = 0, stonewall;
640 f = fopen(file, "r");
642 perror("fopen job file");
646 string = malloc(4096);
648 tmpbuf = malloc(4096);
650 stonewall = stonewall_flag;
651 while ((p = fgets(string, 4096, f)) != NULL) {
654 if (is_empty_or_comment(p))
656 if (sscanf(p, "[%s]", name) != 1)
659 global = !strncmp(name, "global", 6);
661 name[strlen(name) - 1] = '\0';
663 td = get_new_job(global, &def_thread);
670 * Seperate multiple job files by a stonewall
672 if (!global && stonewall) {
673 td->stonewall = stonewall;
678 while ((p = fgets(string, 4096, f)) != NULL) {
679 if (is_empty_or_comment(p))
683 strip_blank_front(&p);
686 if (!check_int(p, "prio", &prio)) {
687 #ifndef FIO_HAVE_IOPRIO
688 log_err("io priorities not available\n");
696 if (!check_int(p, "prioclass", &prioclass)) {
697 #ifndef FIO_HAVE_IOPRIO
698 log_err("io priorities not available\n");
702 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
707 if (!check_int(p, "direct", &il)) {
712 if (!check_int(p, "rand_repeatable", &il)) {
713 td->rand_repeatable = il;
717 if (!check_int(p, "rate", &td->rate)) {
721 if (!check_int(p, "ratemin", &td->ratemin)) {
725 if (!check_int(p, "ratecycle", &td->ratecycle)) {
729 if (!check_int(p, "thinktime", &td->thinktime)) {
733 if (!check_int(p, "cpumask", &cpu)) {
734 #ifndef FIO_HAVE_CPU_AFFINITY
735 log_err("cpu affinity not available\n");
739 fill_cpu_mask(td->cpumask, cpu);
743 if (!check_int(p, "fsync", &td->fsync_blocks)) {
748 if (!check_int(p, "startdelay", &td->start_delay)) {
752 if (!check_str_time(p, "timeout", &ull)) {
757 if (!check_int(p, "invalidate", &il)) {
758 td->invalidate_cache = il;
762 if (!check_int(p, "iodepth", &td->iodepth)) {
766 if (!check_int(p, "sync", &il)) {
771 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
775 if (!check_int(p, "create_serialize", &il)) {
776 td->create_serialize = il;
780 if (!check_int(p, "create_fsync", &il)) {
781 td->create_fsync = il;
785 if (!check_int(p, "end_fsync", &il)) {
790 if (!check_int(p, "loops", &td->loops)) {
794 if (!check_int(p, "numjobs", &td->numjobs)) {
798 if (!check_int(p, "overwrite", &il)) {
803 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
807 if (!check_int(p, "rwmixread", &il)) {
814 if (!check_int(p, "rwmixwrite", &il)) {
817 td->rwmixread = 100 - il;
821 if (!check_int(p, "nice", &td->nice)) {
825 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
836 if (!check_str_bytes(p, "bs", &ull)) {
841 if (!check_str_bytes(p, "size", &td->file_size)) {
845 if (!check_str_bytes(p, "offset", &td->file_offset)) {
849 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
853 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
857 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
861 if (!check_strstore(p, "directory", tmpbuf)) {
862 td->directory = strdup(tmpbuf);
866 if (!check_strstore(p, "name", tmpbuf)) {
867 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
871 if (!check_str(p, "mem", str_mem_cb, td)) {
875 if (!check_str(p, "verify", str_verify_cb, td)) {
879 if (!check_str(p, "rw", str_rw_cb, td)) {
883 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
887 if (!check_strset(p, "create")) {
892 if (!check_strset(p, "exitall")) {
893 exitall_on_terminate = 1;
897 if (!check_strset(p, "stonewall")) {
902 if (!check_strset(p, "thread")) {
907 if (!check_strstore(p, "iolog", tmpbuf)) {
908 if (td->write_iolog) {
909 log_err("fio: read iolog overrides given write_iolog\n");
910 free(td->iolog_file);
913 td->iolog_file = strdup(tmpbuf);
918 if (!check_strstore(p, "write_iolog", tmpbuf)) {
919 if (!td->read_iolog) {
920 td->iolog_file = strdup(tmpbuf);
923 log_err("fio: read iolog overrides given write_iolog\n");
927 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
928 td->exec_prerun = strdup(tmpbuf);
932 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
933 td->exec_postrun = strdup(tmpbuf);
937 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
938 #ifndef FIO_HAVE_IOSCHED_SWITCH
939 log_err("io scheduler switching not available\n");
943 td->ioscheduler = strdup(tmpbuf);
950 * Don't break here, continue parsing options so we
951 * dump all the bad ones. Makes trial/error fixups
952 * easier on the user.
954 printf("Client%d: bad option %s\n",td->thread_number,p);
960 ret = add_job(td, name, 0);
973 static int fill_def_thread(void)
975 memset(&def_thread, 0, sizeof(def_thread));
977 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
978 perror("sched_getaffinity");
985 def_thread.ddir = DDIR_READ;
986 def_thread.iomix = 0;
987 def_thread.bs = DEF_BS;
988 def_thread.min_bs = -1;
989 def_thread.max_bs = -1;
990 def_thread.io_engine = DEF_IO_ENGINE;
991 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
992 def_thread.odirect = DEF_ODIRECT;
993 def_thread.ratecycle = DEF_RATE_CYCLE;
994 def_thread.sequential = DEF_SEQUENTIAL;
995 def_thread.timeout = def_timeout;
996 def_thread.create_file = DEF_CREATE;
997 def_thread.overwrite = DEF_OVERWRITE;
998 def_thread.invalidate_cache = DEF_INVALIDATE;
999 def_thread.sync_io = DEF_SYNCIO;
1000 def_thread.mem_type = MEM_MALLOC;
1001 def_thread.bw_avg_time = DEF_BWAVGTIME;
1002 def_thread.create_serialize = DEF_CREATE_SER;
1003 def_thread.create_fsync = DEF_CREATE_FSYNC;
1004 def_thread.loops = DEF_LOOPS;
1005 def_thread.verify = DEF_VERIFY;
1006 def_thread.stonewall = DEF_STONEWALL;
1007 def_thread.numjobs = DEF_NUMJOBS;
1008 def_thread.use_thread = DEF_USE_THREAD;
1009 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1010 def_thread.rwmixread = DEF_RWMIX_READ;
1011 def_thread.nice = DEF_NICE;
1012 def_thread.rand_repeatable = DEF_RAND_REPEAT;
1013 #ifdef FIO_HAVE_DISK_UTIL
1014 def_thread.do_disk_util = 1;
1020 static void usage(char *name)
1022 printf("%s\n", fio_version_string);
1023 printf("\t-o Write output to file\n");
1024 printf("\t-t Runtime in seconds\n");
1025 printf("\t-l Generate per-job latency logs\n");
1026 printf("\t-w Generate per-job bandwidth logs\n");
1027 printf("\t-m Minimal (terse) output\n");
1028 printf("\t-f Job file (Required)\n");
1029 printf("\t-v Print version info and exit\n");
1032 static int parse_cmd_line(int argc, char *argv[])
1034 int c, idx = 1, ini_idx = 0;
1036 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
1039 def_timeout = atoi(optarg);
1051 f_out = fopen(optarg, "w+");
1053 perror("fopen output");
1067 printf("%s\n", fio_version_string);
1072 while (idx < argc) {
1074 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1075 ini_file[ini_idx - 1] = strdup(argv[idx]);
1087 static void free_shm(void)
1089 struct shmid_ds sbuf;
1092 shmdt((void *) threads);
1094 shmctl(shm_id, IPC_RMID, &sbuf);
1099 * The thread area is shared between the main process and the job
1100 * threads/processes. So setup a shared memory segment that will hold
1103 static int setup_thread_area(void)
1106 * 1024 is too much on some machines, scale max_jobs if
1107 * we get a failure that looks like too large a shm segment
1110 size_t size = max_jobs * sizeof(struct thread_data);
1112 shm_id = shmget(0, size, IPC_CREAT | 0600);
1115 if (errno != EINVAL) {
1126 threads = shmat(shm_id, NULL, 0);
1127 if (threads == (void *) -1) {
1136 int parse_options(int argc, char *argv[])
1140 if (setup_thread_area())
1142 if (fill_def_thread())
1145 job_files = parse_cmd_line(argc, argv);
1147 log_err("Need job file(s)\n");
1152 for (i = 0; i < job_files; i++) {
1153 if (fill_def_thread())
1155 if (parse_jobs_ini(ini_file[i], i))