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_sequential = DEF_SEQUENTIAL;
44 static int def_bs = DEF_BS;
45 static int def_timeout = DEF_TIMEOUT;
46 static int def_repeatable = DEF_RAND_REPEAT;
47 static int def_odirect = DEF_ODIRECT;
49 static char fio_version_string[] = "fio 1.4";
51 static char **ini_file;
52 static int max_jobs = MAX_JOBS;
54 struct thread_data def_thread;
55 struct thread_data *threads = NULL;
58 int write_lat_log = 0;
60 int exitall_on_terminate = 0;
61 unsigned long long mlock_size = 0;
65 static struct thread_data *get_new_job(int global, struct thread_data *parent)
67 struct thread_data *td;
71 if (thread_number >= max_jobs)
74 td = &threads[thread_number++];
79 td->thread_number = thread_number;
83 static void put_job(struct thread_data *td)
85 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
89 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
91 char *ddir_str[] = { "read", "write", "randread", "randwrite",
92 "rw", NULL, "randrw" };
96 #ifndef FIO_HAVE_LIBAIO
97 if (td->io_engine == FIO_LIBAIO) {
98 log_err("Linux libaio not available\n");
102 #ifndef FIO_HAVE_POSIXAIO
103 if (td->io_engine == FIO_POSIXAIO) {
104 log_err("posix aio not available\n");
110 * the def_thread is just for options, it's not a real job
112 if (td == &def_thread)
115 if (td->io_engine & FIO_SYNCIO)
123 * only really works for sequential io for now
125 if (td->zone_size && !td->sequential)
128 td->filetype = FIO_TYPE_FILE;
129 if (!stat(jobname, &sb)) {
130 if (S_ISBLK(sb.st_mode))
131 td->filetype = FIO_TYPE_BD;
132 else if (S_ISCHR(sb.st_mode))
133 td->filetype = FIO_TYPE_CHAR;
136 if (td->filetype == FIO_TYPE_FILE) {
139 if (td->directory && td->directory[0] != '\0')
140 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->thread_number);
142 sprintf(tmp, "%s.%d", jobname, td->thread_number);
143 td->file_name = strdup(tmp);
145 td->file_name = strdup(jobname);
147 fio_sem_init(&td->mutex, 0);
149 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
150 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
151 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
153 if (td->min_bs == -1U)
155 if (td->max_bs == -1U)
157 if (td_read(td) && !td_rw(td))
160 if (td->stonewall && td->thread_number > 1)
163 td->groupid = groupid;
169 setup_log(&td->slat_log);
170 setup_log(&td->clat_log);
173 setup_log(&td->bw_log);
175 if (td->name[0] == '\0')
176 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
178 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
181 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);
182 else if (job_add_num == 1)
183 fprintf(f_out, "...\n");
186 * recurse add identical jobs, clear numjobs and stonewall options
187 * as they don't apply to sub-jobs
189 numjobs = td->numjobs;
191 struct thread_data *td_new = get_new_job(0, td);
197 td_new->stonewall = 0;
198 job_add_num = numjobs - 1;
200 if (add_job(td_new, jobname, job_add_num))
209 int init_random_state(struct thread_data *td)
211 unsigned long seeds[4];
212 int fd, num_maps, blocks;
214 fd = open("/dev/urandom", O_RDONLY);
216 td_verror(td, errno);
220 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
228 os_random_seed(seeds[0], &td->bsrange_state);
229 os_random_seed(seeds[1], &td->verify_state);
230 os_random_seed(seeds[2], &td->rwmix_state);
236 seeds[3] = DEF_RANDSEED;
238 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
239 num_maps = blocks / BLOCKS_PER_MAP;
240 td->file_map = malloc(num_maps * sizeof(long));
241 td->num_maps = num_maps;
242 memset(td->file_map, 0, num_maps * sizeof(long));
244 os_random_seed(seeds[3], &td->random_state);
248 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
250 #ifdef FIO_HAVE_CPU_AFFINITY
255 for (i = 0; i < sizeof(int) * 8; i++) {
257 CPU_SET(i, &cpumask);
262 static unsigned long get_mult(char c)
273 return 1024 * 1024 * 1024;
280 * convert string after '=' into decimal value, noting any size suffix
282 static int str_cnv(char *p, unsigned long long *val)
287 str = strchr(p, '=');
294 *val = strtoul(str, NULL, 10);
295 if (*val == ULONG_MAX && errno == ERANGE)
298 *val *= get_mult(str[len - 1]);
302 static int check_strcnv(char *p, char *name, unsigned long long *val)
304 if (strncmp(p, name, strlen(name) - 1))
307 return str_cnv(p, val);
310 static void strip_blank_front(char **p)
318 static void strip_blank_end(char *p)
320 char *s = p + strlen(p) - 1;
322 while (isspace(*s) || iscntrl(*s))
328 typedef int (str_cb_fn)(struct thread_data *, char *);
330 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
334 if (strncmp(p, name, strlen(name)))
346 strip_blank_front(&s);
350 static int check_strstore(char *p, char *name, char *dest)
354 if (strncmp(p, name, strlen(name)))
366 strip_blank_front(&s);
372 static int __check_range(char *str, unsigned long *val)
376 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
377 *val *= get_mult(suffix);
381 if (sscanf(str, "%lu", val) == 1)
387 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
392 if (strncmp(p, name, strlen(name)))
398 str = strstr(p, name);
404 str = strchr(p, '=');
409 * 'p' now holds whatever is after the '=' sign
414 * terminate p1 at the '-' sign
423 if (!__check_range(p1, s) && !__check_range(p2, e))
429 static int check_int(char *p, char *name, unsigned int *val)
433 if (strncmp(p, name, strlen(name)))
436 str = strstr(p, name);
440 str = strchr(p, '=');
446 if (sscanf(str, "%u", val) == 1)
452 static int check_strset(char *p, char *name)
454 return strncmp(p, name, strlen(name));
457 static int is_empty_or_comment(char *line)
461 for (i = 0; i < strlen(line); i++) {
464 if (!isspace(line[i]) && !iscntrl(line[i]))
471 static int str_rw_cb(struct thread_data *td, char *mem)
473 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
474 td->ddir = DDIR_READ;
477 } else if (!strncmp(mem, "randread", 8)) {
478 td->ddir = DDIR_READ;
481 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
482 td->ddir = DDIR_WRITE;
485 } else if (!strncmp(mem, "randwrite", 9)) {
486 td->ddir = DDIR_WRITE;
489 } else if (!strncmp(mem, "rw", 2)) {
494 } else if (!strncmp(mem, "randrw", 6)) {
501 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
505 static int str_verify_cb(struct thread_data *td, char *mem)
507 if (!strncmp(mem, "0", 1)) {
508 td->verify = VERIFY_NONE;
510 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
511 td->verify = VERIFY_MD5;
513 } else if (!strncmp(mem, "crc32", 5)) {
514 td->verify = VERIFY_CRC32;
518 log_err("fio: verify types: md5, crc32\n");
522 static int str_mem_cb(struct thread_data *td, char *mem)
524 if (!strncmp(mem, "malloc", 6)) {
525 td->mem_type = MEM_MALLOC;
527 } else if (!strncmp(mem, "shm", 3)) {
528 td->mem_type = MEM_SHM;
530 } else if (!strncmp(mem, "mmap", 4)) {
531 td->mem_type = MEM_MMAP;
535 log_err("fio: mem type: malloc, shm, mmap\n");
539 static int str_ioengine_cb(struct thread_data *td, char *str)
541 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
542 !strncmp(str, "libaio", 6)) {
543 strcpy(td->io_engine_name, "libaio");
544 td->io_engine = FIO_LIBAIO;
546 } else if (!strncmp(str, "posixaio", 8)) {
547 strcpy(td->io_engine_name, "posixaio");
548 td->io_engine = FIO_POSIXAIO;
550 } else if (!strncmp(str, "sync", 4)) {
551 strcpy(td->io_engine_name, "sync");
552 td->io_engine = FIO_SYNCIO;
554 } else if (!strncmp(str, "mmap", 4)) {
555 strcpy(td->io_engine_name, "mmap");
556 td->io_engine = FIO_MMAPIO;
558 } else if (!strncmp(str, "sgio", 4)) {
559 strcpy(td->io_engine_name, "sgio");
560 td->io_engine = FIO_SGIO;
562 } else if (!strncmp(str, "splice", 6)) {
563 strcpy(td->io_engine_name, "splice");
564 td->io_engine = FIO_SPLICEIO;
568 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
573 * This is our [ini] type file parser.
575 int parse_jobs_ini(char *file)
577 unsigned int prioclass, prio, cpu, global, il;
578 unsigned long long ull;
579 unsigned long ul1, ul2;
580 struct thread_data *td;
581 char *string, *name, *tmpbuf;
585 int ret = 0, stonewall = 1;
587 f = fopen(file, "r");
589 perror("fopen job file");
593 string = malloc(4096);
595 tmpbuf = malloc(4096);
597 while ((p = fgets(string, 4096, f)) != NULL) {
600 if (is_empty_or_comment(p))
602 if (sscanf(p, "[%s]", name) != 1)
605 global = !strncmp(name, "global", 6);
607 name[strlen(name) - 1] = '\0';
609 td = get_new_job(global, &def_thread);
616 * Seperate multiple job files by a stonewall
618 if (!global && stonewall) {
619 td->stonewall = stonewall;
624 while ((p = fgets(string, 4096, f)) != NULL) {
625 if (is_empty_or_comment(p))
629 strip_blank_front(&p);
632 if (!check_int(p, "prio", &prio)) {
633 #ifndef FIO_HAVE_IOPRIO
634 log_err("io priorities not available\n");
642 if (!check_int(p, "prioclass", &prioclass)) {
643 #ifndef FIO_HAVE_IOPRIO
644 log_err("io priorities not available\n");
648 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
653 if (!check_int(p, "direct", &il)) {
658 if (!check_int(p, "rate", &td->rate)) {
662 if (!check_int(p, "ratemin", &td->ratemin)) {
666 if (!check_int(p, "ratecycle", &td->ratecycle)) {
670 if (!check_int(p, "thinktime", &td->thinktime)) {
674 if (!check_int(p, "cpumask", &cpu)) {
675 #ifndef FIO_HAVE_CPU_AFFINITY
676 log_err("cpu affinity not available\n");
680 fill_cpu_mask(td->cpumask, cpu);
684 if (!check_int(p, "fsync", &td->fsync_blocks)) {
689 if (!check_int(p, "startdelay", &td->start_delay)) {
693 if (!check_int(p, "timeout", &td->timeout)) {
697 if (!check_int(p, "invalidate", &il)) {
698 td->invalidate_cache = il;
702 if (!check_int(p, "iodepth", &td->iodepth)) {
706 if (!check_int(p, "sync", &il)) {
711 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
715 if (!check_int(p, "create_serialize", &il)) {
716 td->create_serialize = il;
720 if (!check_int(p, "create_fsync", &il)) {
721 td->create_fsync = il;
725 if (!check_int(p, "end_fsync", &il)) {
730 if (!check_int(p, "loops", &td->loops)) {
734 if (!check_int(p, "numjobs", &td->numjobs)) {
738 if (!check_int(p, "overwrite", &il)) {
743 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
747 if (!check_int(p, "rwmixread", &il)) {
754 if (!check_int(p, "rwmixwrite", &il)) {
757 td->rwmixread = 100 - il;
761 if (!check_int(p, "nice", &td->nice)) {
765 if (!check_range(p, "bsrange", &ul1, &ul2)) {
776 if (!check_strcnv(p, "bs", &ull)) {
781 if (!check_strcnv(p, "size", &td->file_size)) {
785 if (!check_strcnv(p, "offset", &td->file_offset)) {
789 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
793 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
797 if (!check_strcnv(p, "lockmem", &mlock_size)) {
801 if (!check_strstore(p, "directory", tmpbuf)) {
802 td->directory = strdup(tmpbuf);
806 if (!check_strstore(p, "name", tmpbuf)) {
807 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
811 if (!check_str(p, "mem", str_mem_cb, td)) {
815 if (!check_str(p, "verify", str_verify_cb, td)) {
819 if (!check_str(p, "rw", str_rw_cb, td)) {
823 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
827 if (!check_strset(p, "create")) {
832 if (!check_strset(p, "exitall")) {
833 exitall_on_terminate = 1;
837 if (!check_strset(p, "stonewall")) {
842 if (!check_strset(p, "thread")) {
847 if (!check_strstore(p, "iolog", tmpbuf)) {
848 if (td->write_iolog) {
849 log_err("fio: read iolog overrides given write_iolog\n");
850 free(td->iolog_file);
853 td->iolog_file = strdup(tmpbuf);
858 if (!check_strstore(p, "write_iolog", tmpbuf)) {
859 if (!td->read_iolog) {
860 td->iolog_file = strdup(tmpbuf);
863 log_err("fio: read iolog overrides given write_iolog\n");
867 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
868 td->exec_prerun = strdup(tmpbuf);
872 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
873 td->exec_postrun = strdup(tmpbuf);
877 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
878 #ifndef FIO_HAVE_IOSCHED_SWITCH
879 log_err("io scheduler switching not available\n");
883 td->ioscheduler = strdup(tmpbuf);
890 * Don't break here, continue parsing options so we
891 * dump all the bad ones. Makes trial/error fixups
892 * easier on the user.
894 printf("Client%d: bad option %s\n",td->thread_number,p);
900 ret = add_job(td, name, 0);
913 static int fill_def_thread(void)
915 memset(&def_thread, 0, sizeof(def_thread));
917 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
918 perror("sched_getaffinity");
925 def_thread.ddir = DDIR_READ;
926 def_thread.iomix = 0;
927 def_thread.bs = def_bs;
928 def_thread.min_bs = -1;
929 def_thread.max_bs = -1;
930 def_thread.io_engine = DEF_IO_ENGINE;
931 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
932 def_thread.odirect = def_odirect;
933 def_thread.ratecycle = DEF_RATE_CYCLE;
934 def_thread.sequential = def_sequential;
935 def_thread.timeout = def_timeout;
936 def_thread.create_file = DEF_CREATE;
937 def_thread.overwrite = DEF_OVERWRITE;
938 def_thread.invalidate_cache = DEF_INVALIDATE;
939 def_thread.sync_io = DEF_SYNCIO;
940 def_thread.mem_type = MEM_MALLOC;
941 def_thread.bw_avg_time = DEF_BWAVGTIME;
942 def_thread.create_serialize = DEF_CREATE_SER;
943 def_thread.create_fsync = DEF_CREATE_FSYNC;
944 def_thread.loops = DEF_LOOPS;
945 def_thread.verify = DEF_VERIFY;
946 def_thread.stonewall = DEF_STONEWALL;
947 def_thread.numjobs = DEF_NUMJOBS;
948 def_thread.use_thread = DEF_USE_THREAD;
949 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
950 def_thread.rwmixread = DEF_RWMIX_READ;
951 def_thread.nice = DEF_NICE;
952 #ifdef FIO_HAVE_DISK_UTIL
953 def_thread.do_disk_util = 1;
959 static void usage(char *name)
961 printf("%s\n", fio_version_string);
962 printf("\t-s IO is sequential\n");
963 printf("\t-b Block size in KiB for each IO\n");
964 printf("\t-t Runtime in seconds\n");
965 printf("\t-R Exit all threads on failure to meet rate goal\n");
966 printf("\t-o Use O_DIRECT\n");
967 printf("\t-l Generate per-job latency logs\n");
968 printf("\t-w Generate per-job bandwidth logs\n");
969 printf("\t-f Job file (Required)\n");
970 printf("\t-v Print version info and exit\n");
973 static int parse_cmd_line(int argc, char *argv[])
975 int c, idx = 1, ini_idx = 0;
977 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvhO:")) != EOF) {
980 def_sequential = !!atoi(optarg);
984 def_bs = atoi(optarg);
987 printf("bad block size\n");
993 def_timeout = atoi(optarg);
997 def_repeatable = !!atoi(optarg);
1001 rate_quit = !!atoi(optarg);
1005 def_odirect = !!atoi(optarg);
1010 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1011 ini_file[ini_idx - 1] = strdup(optarg);
1023 f_out = fopen(optarg, "w+");
1025 perror("fopen output");
1035 printf("%s\n", fio_version_string);
1040 while (idx < argc) {
1042 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1043 ini_file[ini_idx - 1] = strdup(argv[idx]);
1055 static void free_shm(void)
1057 struct shmid_ds sbuf;
1060 shmdt((void *) threads);
1062 shmctl(shm_id, IPC_RMID, &sbuf);
1066 static int setup_thread_area(void)
1069 * 1024 is too much on some machines, scale max_jobs if
1070 * we get a failure that looks like too large a shm segment
1073 int s = max_jobs * sizeof(struct thread_data);
1075 shm_id = shmget(0, s, IPC_CREAT | 0600);
1078 if (errno != EINVAL) {
1089 threads = shmat(shm_id, NULL, 0);
1090 if (threads == (void *) -1) {
1099 int parse_options(int argc, char *argv[])
1103 if (setup_thread_area())
1105 if (fill_def_thread())
1108 job_files = parse_cmd_line(argc, argv);
1110 log_err("Need job file(s)\n");
1115 for (i = 0; i < job_files; i++) {
1116 if (fill_def_thread())
1118 if (parse_jobs_ini(ini_file[i]))