10 #include <sys/types.h>
16 #define DEF_TIMEOUT (0)
17 #define DEF_RATE_CYCLE (1000)
18 #define DEF_ODIRECT (1)
19 #define DEF_IO_ENGINE (FIO_SYNCIO)
20 #define DEF_IO_ENGINE_NAME "sync"
21 #define DEF_SEQUENTIAL (1)
22 #define DEF_RAND_REPEAT (1)
23 #define DEF_OVERWRITE (1)
24 #define DEF_CREATE (1)
25 #define DEF_INVALIDATE (1)
26 #define DEF_SYNCIO (0)
27 #define DEF_RANDSEED (0xb1899bedUL)
28 #define DEF_BWAVGTIME (500)
29 #define DEF_CREATE_SER (1)
30 #define DEF_CREATE_FSYNC (1)
32 #define DEF_VERIFY (0)
33 #define DEF_STONEWALL (0)
34 #define DEF_NUMJOBS (1)
35 #define DEF_USE_THREAD (0)
36 #define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
37 #define DEF_ZONE_SIZE (0)
38 #define DEF_ZONE_SKIP (0)
39 #define DEF_RWMIX_CYCLE (500)
40 #define DEF_RWMIX_READ (50)
43 static int def_timeout = DEF_TIMEOUT;
45 static char fio_version_string[] = "fio 1.4";
47 static char **ini_file;
48 static int max_jobs = MAX_JOBS;
50 struct thread_data def_thread;
51 struct thread_data *threads = NULL;
54 int write_lat_log = 0;
56 int exitall_on_terminate = 0;
58 unsigned long long mlock_size = 0;
62 static struct thread_data *get_new_job(int global, struct thread_data *parent)
64 struct thread_data *td;
68 if (thread_number >= max_jobs)
71 td = &threads[thread_number++];
76 td->thread_number = thread_number;
80 static void put_job(struct thread_data *td)
82 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
86 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
88 char *ddir_str[] = { "read", "write", "randread", "randwrite",
89 "rw", NULL, "randrw" };
93 #ifndef FIO_HAVE_LIBAIO
94 if (td->io_engine == FIO_LIBAIO) {
95 log_err("Linux libaio not available\n");
99 #ifndef FIO_HAVE_POSIXAIO
100 if (td->io_engine == FIO_POSIXAIO) {
101 log_err("posix aio not available\n");
107 * the def_thread is just for options, it's not a real job
109 if (td == &def_thread)
112 if (td->io_engine & FIO_SYNCIO)
120 * only really works for sequential io for now
122 if (td->zone_size && !td->sequential)
125 td->filetype = FIO_TYPE_FILE;
126 if (!stat(jobname, &sb)) {
127 if (S_ISBLK(sb.st_mode))
128 td->filetype = FIO_TYPE_BD;
129 else if (S_ISCHR(sb.st_mode))
130 td->filetype = FIO_TYPE_CHAR;
133 if (td->filetype == FIO_TYPE_FILE) {
136 if (td->directory && td->directory[0] != '\0')
137 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->thread_number);
139 sprintf(tmp, "%s.%d", jobname, td->thread_number);
140 td->file_name = strdup(tmp);
142 td->file_name = strdup(jobname);
144 fio_sem_init(&td->mutex, 0);
146 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
147 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
148 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
150 if (td->min_bs == -1U)
152 if (td->max_bs == -1U)
154 if (td_read(td) && !td_rw(td))
157 if (td->stonewall && td->thread_number > 1)
160 td->groupid = groupid;
166 setup_log(&td->slat_log);
167 setup_log(&td->clat_log);
170 setup_log(&td->bw_log);
172 if (td->name[0] == '\0')
173 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
175 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
179 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);
180 else if (job_add_num == 1)
181 fprintf(f_out, "...\n");
185 * recurse add identical jobs, clear numjobs and stonewall options
186 * as they don't apply to sub-jobs
188 numjobs = td->numjobs;
190 struct thread_data *td_new = get_new_job(0, td);
196 td_new->stonewall = 0;
197 job_add_num = numjobs - 1;
199 if (add_job(td_new, jobname, job_add_num))
208 int init_random_state(struct thread_data *td)
210 unsigned long seeds[4];
211 int fd, num_maps, blocks;
213 fd = open("/dev/urandom", O_RDONLY);
215 td_verror(td, errno);
219 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
227 os_random_seed(seeds[0], &td->bsrange_state);
228 os_random_seed(seeds[1], &td->verify_state);
229 os_random_seed(seeds[2], &td->rwmix_state);
234 if (td->rand_repeatable)
235 seeds[3] = DEF_RANDSEED;
237 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
238 num_maps = blocks / BLOCKS_PER_MAP;
239 td->file_map = malloc(num_maps * sizeof(long));
240 td->num_maps = num_maps;
241 memset(td->file_map, 0, num_maps * sizeof(long));
243 os_random_seed(seeds[3], &td->random_state);
247 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
249 #ifdef FIO_HAVE_CPU_AFFINITY
254 for (i = 0; i < sizeof(int) * 8; i++) {
256 CPU_SET(i, &cpumask);
261 static unsigned long get_mult(char c)
272 return 1024 * 1024 * 1024;
279 * convert string after '=' into decimal value, noting any size suffix
281 static int str_cnv(char *p, unsigned long long *val)
286 str = strchr(p, '=');
293 *val = strtoul(str, NULL, 10);
294 if (*val == ULONG_MAX && errno == ERANGE)
297 *val *= get_mult(str[len - 1]);
301 static int check_strcnv(char *p, char *name, unsigned long long *val)
303 if (strncmp(p, name, strlen(name) - 1))
306 return str_cnv(p, val);
309 static void strip_blank_front(char **p)
317 static void strip_blank_end(char *p)
319 char *s = p + strlen(p) - 1;
321 while (isspace(*s) || iscntrl(*s))
327 typedef int (str_cb_fn)(struct thread_data *, char *);
329 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
333 if (strncmp(p, name, strlen(name)))
345 strip_blank_front(&s);
349 static int check_strstore(char *p, char *name, char *dest)
353 if (strncmp(p, name, strlen(name)))
365 strip_blank_front(&s);
371 static int __check_range(char *str, unsigned long *val)
375 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
376 *val *= get_mult(suffix);
380 if (sscanf(str, "%lu", val) == 1)
386 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
391 if (strncmp(p, name, strlen(name)))
397 str = strstr(p, name);
403 str = strchr(p, '=');
408 * 'p' now holds whatever is after the '=' sign
413 * terminate p1 at the '-' sign
422 if (!__check_range(p1, s) && !__check_range(p2, e))
428 static int check_int(char *p, char *name, unsigned int *val)
432 if (strncmp(p, name, strlen(name)))
435 str = strstr(p, name);
439 str = strchr(p, '=');
445 if (sscanf(str, "%u", val) == 1)
451 static int check_strset(char *p, char *name)
453 return strncmp(p, name, strlen(name));
456 static int is_empty_or_comment(char *line)
460 for (i = 0; i < strlen(line); i++) {
463 if (!isspace(line[i]) && !iscntrl(line[i]))
470 static int str_rw_cb(struct thread_data *td, char *mem)
472 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
473 td->ddir = DDIR_READ;
476 } else if (!strncmp(mem, "randread", 8)) {
477 td->ddir = DDIR_READ;
480 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
481 td->ddir = DDIR_WRITE;
484 } else if (!strncmp(mem, "randwrite", 9)) {
485 td->ddir = DDIR_WRITE;
488 } else if (!strncmp(mem, "rw", 2)) {
493 } else if (!strncmp(mem, "randrw", 6)) {
500 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
504 static int str_verify_cb(struct thread_data *td, char *mem)
506 if (!strncmp(mem, "0", 1)) {
507 td->verify = VERIFY_NONE;
509 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
510 td->verify = VERIFY_MD5;
512 } else if (!strncmp(mem, "crc32", 5)) {
513 td->verify = VERIFY_CRC32;
517 log_err("fio: verify types: md5, crc32\n");
521 static int str_mem_cb(struct thread_data *td, char *mem)
523 if (!strncmp(mem, "malloc", 6)) {
524 td->mem_type = MEM_MALLOC;
526 } else if (!strncmp(mem, "shm", 3)) {
527 td->mem_type = MEM_SHM;
529 } else if (!strncmp(mem, "mmap", 4)) {
530 td->mem_type = MEM_MMAP;
534 log_err("fio: mem type: malloc, shm, mmap\n");
538 static int str_ioengine_cb(struct thread_data *td, char *str)
540 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
541 !strncmp(str, "libaio", 6)) {
542 strcpy(td->io_engine_name, "libaio");
543 td->io_engine = FIO_LIBAIO;
545 } else if (!strncmp(str, "posixaio", 8)) {
546 strcpy(td->io_engine_name, "posixaio");
547 td->io_engine = FIO_POSIXAIO;
549 } else if (!strncmp(str, "sync", 4)) {
550 strcpy(td->io_engine_name, "sync");
551 td->io_engine = FIO_SYNCIO;
553 } else if (!strncmp(str, "mmap", 4)) {
554 strcpy(td->io_engine_name, "mmap");
555 td->io_engine = FIO_MMAPIO;
557 } else if (!strncmp(str, "sgio", 4)) {
558 strcpy(td->io_engine_name, "sgio");
559 td->io_engine = FIO_SGIO;
561 } else if (!strncmp(str, "splice", 6)) {
562 strcpy(td->io_engine_name, "splice");
563 td->io_engine = FIO_SPLICEIO;
567 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
572 * This is our [ini] type file parser.
574 int parse_jobs_ini(char *file)
576 unsigned int prioclass, prio, cpu, global, il;
577 unsigned long long ull;
578 unsigned long ul1, ul2;
579 struct thread_data *td;
580 char *string, *name, *tmpbuf;
584 int ret = 0, stonewall = 1;
586 f = fopen(file, "r");
588 perror("fopen job file");
592 string = malloc(4096);
594 tmpbuf = malloc(4096);
596 while ((p = fgets(string, 4096, f)) != NULL) {
599 if (is_empty_or_comment(p))
601 if (sscanf(p, "[%s]", name) != 1)
604 global = !strncmp(name, "global", 6);
606 name[strlen(name) - 1] = '\0';
608 td = get_new_job(global, &def_thread);
615 * Seperate multiple job files by a stonewall
617 if (!global && stonewall) {
618 td->stonewall = stonewall;
623 while ((p = fgets(string, 4096, f)) != NULL) {
624 if (is_empty_or_comment(p))
628 strip_blank_front(&p);
631 if (!check_int(p, "prio", &prio)) {
632 #ifndef FIO_HAVE_IOPRIO
633 log_err("io priorities not available\n");
641 if (!check_int(p, "prioclass", &prioclass)) {
642 #ifndef FIO_HAVE_IOPRIO
643 log_err("io priorities not available\n");
647 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
652 if (!check_int(p, "direct", &il)) {
657 if (!check_int(p, "rand_repeatable", &il)) {
658 td->rand_repeatable = il;
662 if (!check_int(p, "rate", &td->rate)) {
666 if (!check_int(p, "ratemin", &td->ratemin)) {
670 if (!check_int(p, "ratecycle", &td->ratecycle)) {
674 if (!check_int(p, "thinktime", &td->thinktime)) {
678 if (!check_int(p, "cpumask", &cpu)) {
679 #ifndef FIO_HAVE_CPU_AFFINITY
680 log_err("cpu affinity not available\n");
684 fill_cpu_mask(td->cpumask, cpu);
688 if (!check_int(p, "fsync", &td->fsync_blocks)) {
693 if (!check_int(p, "startdelay", &td->start_delay)) {
697 if (!check_int(p, "timeout", &td->timeout)) {
701 if (!check_int(p, "invalidate", &il)) {
702 td->invalidate_cache = il;
706 if (!check_int(p, "iodepth", &td->iodepth)) {
710 if (!check_int(p, "sync", &il)) {
715 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
719 if (!check_int(p, "create_serialize", &il)) {
720 td->create_serialize = il;
724 if (!check_int(p, "create_fsync", &il)) {
725 td->create_fsync = il;
729 if (!check_int(p, "end_fsync", &il)) {
734 if (!check_int(p, "loops", &td->loops)) {
738 if (!check_int(p, "numjobs", &td->numjobs)) {
742 if (!check_int(p, "overwrite", &il)) {
747 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
751 if (!check_int(p, "rwmixread", &il)) {
758 if (!check_int(p, "rwmixwrite", &il)) {
761 td->rwmixread = 100 - il;
765 if (!check_int(p, "nice", &td->nice)) {
769 if (!check_range(p, "bsrange", &ul1, &ul2)) {
780 if (!check_strcnv(p, "bs", &ull)) {
785 if (!check_strcnv(p, "size", &td->file_size)) {
789 if (!check_strcnv(p, "offset", &td->file_offset)) {
793 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
797 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
801 if (!check_strcnv(p, "lockmem", &mlock_size)) {
805 if (!check_strstore(p, "directory", tmpbuf)) {
806 td->directory = strdup(tmpbuf);
810 if (!check_strstore(p, "name", tmpbuf)) {
811 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
815 if (!check_str(p, "mem", str_mem_cb, td)) {
819 if (!check_str(p, "verify", str_verify_cb, td)) {
823 if (!check_str(p, "rw", str_rw_cb, td)) {
827 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
831 if (!check_strset(p, "create")) {
836 if (!check_strset(p, "exitall")) {
837 exitall_on_terminate = 1;
841 if (!check_strset(p, "stonewall")) {
846 if (!check_strset(p, "thread")) {
851 if (!check_strstore(p, "iolog", tmpbuf)) {
852 if (td->write_iolog) {
853 log_err("fio: read iolog overrides given write_iolog\n");
854 free(td->iolog_file);
857 td->iolog_file = strdup(tmpbuf);
862 if (!check_strstore(p, "write_iolog", tmpbuf)) {
863 if (!td->read_iolog) {
864 td->iolog_file = strdup(tmpbuf);
867 log_err("fio: read iolog overrides given write_iolog\n");
871 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
872 td->exec_prerun = strdup(tmpbuf);
876 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
877 td->exec_postrun = strdup(tmpbuf);
881 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
882 #ifndef FIO_HAVE_IOSCHED_SWITCH
883 log_err("io scheduler switching not available\n");
887 td->ioscheduler = strdup(tmpbuf);
894 * Don't break here, continue parsing options so we
895 * dump all the bad ones. Makes trial/error fixups
896 * easier on the user.
898 printf("Client%d: bad option %s\n",td->thread_number,p);
904 ret = add_job(td, name, 0);
917 static int fill_def_thread(void)
919 memset(&def_thread, 0, sizeof(def_thread));
921 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
922 perror("sched_getaffinity");
929 def_thread.ddir = DDIR_READ;
930 def_thread.iomix = 0;
931 def_thread.bs = DEF_BS;
932 def_thread.min_bs = -1;
933 def_thread.max_bs = -1;
934 def_thread.io_engine = DEF_IO_ENGINE;
935 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
936 def_thread.odirect = DEF_ODIRECT;
937 def_thread.ratecycle = DEF_RATE_CYCLE;
938 def_thread.sequential = DEF_SEQUENTIAL;
939 def_thread.timeout = def_timeout;
940 def_thread.create_file = DEF_CREATE;
941 def_thread.overwrite = DEF_OVERWRITE;
942 def_thread.invalidate_cache = DEF_INVALIDATE;
943 def_thread.sync_io = DEF_SYNCIO;
944 def_thread.mem_type = MEM_MALLOC;
945 def_thread.bw_avg_time = DEF_BWAVGTIME;
946 def_thread.create_serialize = DEF_CREATE_SER;
947 def_thread.create_fsync = DEF_CREATE_FSYNC;
948 def_thread.loops = DEF_LOOPS;
949 def_thread.verify = DEF_VERIFY;
950 def_thread.stonewall = DEF_STONEWALL;
951 def_thread.numjobs = DEF_NUMJOBS;
952 def_thread.use_thread = DEF_USE_THREAD;
953 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
954 def_thread.rwmixread = DEF_RWMIX_READ;
955 def_thread.nice = DEF_NICE;
956 def_thread.rand_repeatable = DEF_RAND_REPEAT;
957 #ifdef FIO_HAVE_DISK_UTIL
958 def_thread.do_disk_util = 1;
964 static void usage(char *name)
966 printf("%s\n", fio_version_string);
967 printf("\t-o Write output to file\n");
968 printf("\t-t Runtime in seconds\n");
969 printf("\t-l Generate per-job latency logs\n");
970 printf("\t-w Generate per-job bandwidth logs\n");
971 printf("\t-m Minimal (terse) output\n");
972 printf("\t-f Job file (Required)\n");
973 printf("\t-v Print version info and exit\n");
976 static int parse_cmd_line(int argc, char *argv[])
978 int c, idx = 1, ini_idx = 0;
980 while ((c = getopt(argc, argv, "t:o:f:lwvhm")) != EOF) {
983 def_timeout = atoi(optarg);
988 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
989 ini_file[ini_idx - 1] = strdup(optarg);
1001 f_out = fopen(optarg, "w+");
1003 perror("fopen output");
1017 printf("%s\n", fio_version_string);
1022 while (idx < argc) {
1024 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1025 ini_file[ini_idx - 1] = strdup(argv[idx]);
1037 static void free_shm(void)
1039 struct shmid_ds sbuf;
1042 shmdt((void *) threads);
1044 shmctl(shm_id, IPC_RMID, &sbuf);
1048 static int setup_thread_area(void)
1051 * 1024 is too much on some machines, scale max_jobs if
1052 * we get a failure that looks like too large a shm segment
1055 int s = max_jobs * sizeof(struct thread_data);
1057 shm_id = shmget(0, s, IPC_CREAT | 0600);
1060 if (errno != EINVAL) {
1071 threads = shmat(shm_id, NULL, 0);
1072 if (threads == (void *) -1) {
1081 int parse_options(int argc, char *argv[])
1085 if (setup_thread_area())
1087 if (fill_def_thread())
1090 job_files = parse_cmd_line(argc, argv);
1092 log_err("Need job file(s)\n");
1097 for (i = 0; i < job_files; i++) {
1098 if (fill_def_thread())
1100 if (parse_jobs_ini(ini_file[i]))