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 char fio_version_string[] = "fio 1.4";
45 static int repeatable = DEF_RAND_REPEAT;
46 static char *ini_file;
47 static int max_jobs = MAX_JOBS;
49 struct thread_data def_thread;
50 struct thread_data *threads = NULL;
53 int write_lat_log = 0;
55 int exitall_on_terminate = 0;
56 unsigned long long mlock_size = 0;
58 static struct thread_data *get_new_job(int global, struct thread_data *parent)
60 struct thread_data *td;
64 if (thread_number >= max_jobs)
67 td = &threads[thread_number++];
72 td->thread_number = thread_number;
77 static void put_job(struct thread_data *td)
79 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
83 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
85 char *ddir_str[] = { "read", "write", "randread", "randwrite",
86 "rw", NULL, "randrw" };
90 #ifndef FIO_HAVE_LIBAIO
91 if (td->io_engine == FIO_LIBAIO) {
92 fprintf(stderr, "Linux libaio not available\n");
96 #ifndef FIO_HAVE_POSIXAIO
97 if (td->io_engine == FIO_POSIXAIO) {
98 fprintf(stderr, "posix aio not available\n");
104 * the def_thread is just for options, it's not a real job
106 if (td == &def_thread)
109 if (td->io_engine & FIO_SYNCIO)
117 * only really works for sequential io for now
119 if (td->zone_size && !td->sequential)
122 td->filetype = FIO_TYPE_FILE;
123 if (!stat(jobname, &sb)) {
124 if (S_ISBLK(sb.st_mode))
125 td->filetype = FIO_TYPE_BD;
126 else if (S_ISCHR(sb.st_mode))
127 td->filetype = FIO_TYPE_CHAR;
130 if (td->filetype == FIO_TYPE_FILE) {
133 if (td->directory && td->directory[0] != '\0')
134 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->jobnum);
136 sprintf(tmp, "%s.%d", jobname, td->jobnum);
137 td->file_name = strdup(tmp);
139 td->file_name = strdup(jobname);
141 fio_sem_init(&td->mutex, 0);
143 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
144 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
145 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
147 if (td->min_bs == -1U)
149 if (td->max_bs == -1U)
151 if (td_read(td) && !td_rw(td))
154 if (td->stonewall && td->thread_number > 1)
157 td->groupid = groupid;
163 setup_log(&td->slat_log);
164 setup_log(&td->clat_log);
167 setup_log(&td->bw_log);
169 if (td->name[0] == '\0')
170 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
172 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
175 printf("%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);
176 else if (job_add_num == 1)
180 * recurse add identical jobs, clear numjobs and stonewall options
181 * as they don't apply to sub-jobs
183 numjobs = td->numjobs;
185 struct thread_data *td_new = get_new_job(0, td);
191 td_new->stonewall = 0;
192 td_new->jobnum = numjobs;
193 job_add_num = numjobs - 1;
195 if (add_job(td_new, jobname, job_add_num))
204 int init_random_state(struct thread_data *td)
206 unsigned long seeds[4];
207 int fd, num_maps, blocks;
209 fd = open("/dev/urandom", O_RDONLY);
211 td_verror(td, errno);
215 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
223 os_random_seed(seeds[0], &td->bsrange_state);
224 os_random_seed(seeds[1], &td->verify_state);
225 os_random_seed(seeds[2], &td->rwmix_state);
231 seeds[3] = DEF_RANDSEED;
233 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
234 num_maps = blocks / BLOCKS_PER_MAP;
235 td->file_map = malloc(num_maps * sizeof(long));
236 td->num_maps = num_maps;
237 memset(td->file_map, 0, num_maps * sizeof(long));
239 os_random_seed(seeds[3], &td->random_state);
243 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
245 #ifdef FIO_HAVE_CPU_AFFINITY
250 for (i = 0; i < sizeof(int) * 8; i++) {
252 CPU_SET(i, &cpumask);
257 static unsigned long get_mult(char c)
268 return 1024 * 1024 * 1024;
275 * convert string after '=' into decimal value, noting any size suffix
277 static int str_cnv(char *p, unsigned long long *val)
282 str = strchr(p, '=');
289 *val = strtoul(str, NULL, 10);
290 if (*val == ULONG_MAX && errno == ERANGE)
293 *val *= get_mult(str[len - 1]);
297 static int check_strcnv(char *p, char *name, unsigned long long *val)
299 if (strncmp(p, name, strlen(name) - 1))
302 return str_cnv(p, val);
305 static void strip_blank_front(char **p)
313 static void strip_blank_end(char *p)
315 char *s = p + strlen(p) - 1;
317 while (isspace(*s) || iscntrl(*s))
323 typedef int (str_cb_fn)(struct thread_data *, char *);
325 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
329 if (strncmp(p, name, strlen(name)))
341 strip_blank_front(&s);
345 static int check_strstore(char *p, char *name, char *dest)
349 if (strncmp(p, name, strlen(name)))
361 strip_blank_front(&s);
367 static int __check_range(char *str, unsigned long *val)
371 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
372 *val *= get_mult(suffix);
376 if (sscanf(str, "%lu", val) == 1)
382 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
387 if (strncmp(p, name, strlen(name)))
393 str = strstr(p, name);
399 str = strchr(p, '=');
404 * 'p' now holds whatever is after the '=' sign
409 * terminate p1 at the '-' sign
418 if (!__check_range(p1, s) && !__check_range(p2, e))
424 static int check_int(char *p, char *name, unsigned int *val)
428 if (strncmp(p, name, strlen(name)))
431 str = strstr(p, name);
435 str = strchr(p, '=');
441 if (sscanf(str, "%u", val) == 1)
447 static int check_strset(char *p, char *name)
449 return strncmp(p, name, strlen(name));
452 static int is_empty_or_comment(char *line)
456 for (i = 0; i < strlen(line); i++) {
459 if (!isspace(line[i]) && !iscntrl(line[i]))
466 static int str_rw_cb(struct thread_data *td, char *mem)
468 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
469 td->ddir = DDIR_READ;
472 } else if (!strncmp(mem, "randread", 8)) {
473 td->ddir = DDIR_READ;
476 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
477 td->ddir = DDIR_WRITE;
480 } else if (!strncmp(mem, "randwrite", 9)) {
481 td->ddir = DDIR_WRITE;
484 } else if (!strncmp(mem, "rw", 2)) {
489 } else if (!strncmp(mem, "randrw", 6)) {
496 fprintf(stderr, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
500 static int str_verify_cb(struct thread_data *td, char *mem)
502 if (!strncmp(mem, "0", 1)) {
503 td->verify = VERIFY_NONE;
505 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
506 td->verify = VERIFY_MD5;
508 } else if (!strncmp(mem, "crc32", 5)) {
509 td->verify = VERIFY_CRC32;
513 fprintf(stderr, "fio: verify types: md5, crc32\n");
517 static int str_mem_cb(struct thread_data *td, char *mem)
519 if (!strncmp(mem, "malloc", 6)) {
520 td->mem_type = MEM_MALLOC;
522 } else if (!strncmp(mem, "shm", 3)) {
523 td->mem_type = MEM_SHM;
525 } else if (!strncmp(mem, "mmap", 4)) {
526 td->mem_type = MEM_MMAP;
530 fprintf(stderr, "fio: mem type: malloc, shm, mmap\n");
534 static int str_ioengine_cb(struct thread_data *td, char *str)
536 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
537 !strncmp(str, "libaio", 6)) {
538 strcpy(td->io_engine_name, "libaio");
539 td->io_engine = FIO_LIBAIO;
541 } else if (!strncmp(str, "posixaio", 8)) {
542 strcpy(td->io_engine_name, "posixaio");
543 td->io_engine = FIO_POSIXAIO;
545 } else if (!strncmp(str, "sync", 4)) {
546 strcpy(td->io_engine_name, "sync");
547 td->io_engine = FIO_SYNCIO;
549 } else if (!strncmp(str, "mmap", 4)) {
550 strcpy(td->io_engine_name, "mmap");
551 td->io_engine = FIO_MMAPIO;
553 } else if (!strncmp(str, "sgio", 4)) {
554 strcpy(td->io_engine_name, "sgio");
555 td->io_engine = FIO_SGIO;
557 } else if (!strncmp(str, "splice", 6)) {
558 strcpy(td->io_engine_name, "splice");
559 td->io_engine = FIO_SPLICEIO;
563 fprintf(stderr, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
568 * This is our [ini] type file parser.
570 int parse_jobs_ini(char *file)
572 unsigned int prioclass, prio, cpu, global, il;
573 unsigned long long ull;
574 unsigned long ul1, ul2;
575 struct thread_data *td;
576 char *string, *name, *tmpbuf;
582 f = fopen(file, "r");
584 perror("fopen job file");
588 string = malloc(4096);
590 tmpbuf = malloc(4096);
592 while ((p = fgets(string, 4096, f)) != NULL) {
595 if (is_empty_or_comment(p))
597 if (sscanf(p, "[%s]", name) != 1)
600 global = !strncmp(name, "global", 6);
602 name[strlen(name) - 1] = '\0';
604 td = get_new_job(global, &def_thread);
611 while ((p = fgets(string, 4096, f)) != NULL) {
612 if (is_empty_or_comment(p))
616 strip_blank_front(&p);
619 if (!check_int(p, "prio", &prio)) {
620 #ifndef FIO_HAVE_IOPRIO
621 fprintf(stderr, "io priorities not available\n");
629 if (!check_int(p, "prioclass", &prioclass)) {
630 #ifndef FIO_HAVE_IOPRIO
631 fprintf(stderr, "io priorities not available\n");
635 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
640 if (!check_int(p, "direct", &il)) {
645 if (!check_int(p, "rate", &td->rate)) {
649 if (!check_int(p, "ratemin", &td->ratemin)) {
653 if (!check_int(p, "ratecycle", &td->ratecycle)) {
657 if (!check_int(p, "thinktime", &td->thinktime)) {
661 if (!check_int(p, "cpumask", &cpu)) {
662 #ifndef FIO_HAVE_CPU_AFFINITY
663 fprintf(stderr, "cpu affinity not available\n");
667 fill_cpu_mask(td->cpumask, cpu);
671 if (!check_int(p, "fsync", &td->fsync_blocks)) {
676 if (!check_int(p, "startdelay", &td->start_delay)) {
680 if (!check_int(p, "timeout", &td->timeout)) {
684 if (!check_int(p, "invalidate", &il)) {
685 td->invalidate_cache = il;
689 if (!check_int(p, "iodepth", &td->iodepth)) {
693 if (!check_int(p, "sync", &il)) {
698 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
702 if (!check_int(p, "create_serialize", &il)) {
703 td->create_serialize = il;
707 if (!check_int(p, "create_fsync", &il)) {
708 td->create_fsync = il;
712 if (!check_int(p, "end_fsync", &il)) {
717 if (!check_int(p, "loops", &td->loops)) {
721 if (!check_int(p, "numjobs", &td->numjobs)) {
725 if (!check_int(p, "overwrite", &il)) {
730 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
734 if (!check_int(p, "rwmixread", &il)) {
741 if (!check_int(p, "rwmixwrite", &il)) {
744 td->rwmixread = 100 - il;
748 if (!check_int(p, "nice", &td->nice)) {
752 if (!check_range(p, "bsrange", &ul1, &ul2)) {
763 if (!check_strcnv(p, "bs", &ull)) {
768 if (!check_strcnv(p, "size", &td->file_size)) {
772 if (!check_strcnv(p, "offset", &td->file_offset)) {
776 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
780 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
784 if (!check_strcnv(p, "lockmem", &mlock_size)) {
788 if (!check_strstore(p, "directory", tmpbuf)) {
789 td->directory = strdup(tmpbuf);
793 if (!check_strstore(p, "name", tmpbuf)) {
794 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
798 if (!check_str(p, "mem", str_mem_cb, td)) {
802 if (!check_str(p, "verify", str_verify_cb, td)) {
806 if (!check_str(p, "rw", str_rw_cb, td)) {
810 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
814 if (!check_strset(p, "create")) {
819 if (!check_strset(p, "exitall")) {
820 exitall_on_terminate = 1;
824 if (!check_strset(p, "stonewall")) {
829 if (!check_strset(p, "thread")) {
834 if (!check_strstore(p, "iolog", tmpbuf)) {
835 if (td->write_iolog) {
836 fprintf(stderr, "fio: read iolog overrides given write_iolog\n");
837 free(td->iolog_file);
840 td->iolog_file = strdup(tmpbuf);
845 if (!check_strstore(p, "write_iolog", tmpbuf)) {
846 if (!td->read_iolog) {
847 td->iolog_file = strdup(tmpbuf);
850 fprintf(stderr, "fio: read iolog overrides given write_iolog\n");
854 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
855 td->exec_prerun = strdup(tmpbuf);
859 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
860 td->exec_postrun = strdup(tmpbuf);
864 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
865 #ifndef FIO_HAVE_IOSCHED_SWITCH
866 fprintf(stderr, "io scheduler switching not available\n");
870 td->ioscheduler = strdup(tmpbuf);
877 * Don't break here, continue parsing options so we
878 * dump all the bad ones. Makes trial/error fixups
879 * easier on the user.
881 printf("Client%d: bad option %s\n",td->thread_number,p);
887 ret = add_job(td, name, 0);
900 static int fill_def_thread(void)
902 memset(&def_thread, 0, sizeof(def_thread));
904 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
905 perror("sched_getaffinity");
912 def_thread.ddir = DDIR_READ;
913 def_thread.iomix = 0;
914 def_thread.bs = DEF_BS;
915 def_thread.min_bs = -1;
916 def_thread.max_bs = -1;
917 def_thread.io_engine = DEF_IO_ENGINE;
918 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
919 def_thread.odirect = DEF_ODIRECT;
920 def_thread.ratecycle = DEF_RATE_CYCLE;
921 def_thread.sequential = DEF_SEQUENTIAL;
922 def_thread.timeout = DEF_TIMEOUT;
923 def_thread.create_file = DEF_CREATE;
924 def_thread.overwrite = DEF_OVERWRITE;
925 def_thread.invalidate_cache = DEF_INVALIDATE;
926 def_thread.sync_io = DEF_SYNCIO;
927 def_thread.mem_type = MEM_MALLOC;
928 def_thread.bw_avg_time = DEF_BWAVGTIME;
929 def_thread.create_serialize = DEF_CREATE_SER;
930 def_thread.create_fsync = DEF_CREATE_FSYNC;
931 def_thread.loops = DEF_LOOPS;
932 def_thread.verify = DEF_VERIFY;
933 def_thread.stonewall = DEF_STONEWALL;
934 def_thread.numjobs = DEF_NUMJOBS;
935 def_thread.use_thread = DEF_USE_THREAD;
936 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
937 def_thread.rwmixread = DEF_RWMIX_READ;
938 def_thread.nice = DEF_NICE;
939 #ifdef FIO_HAVE_DISK_UTIL
940 def_thread.do_disk_util = 1;
946 static void usage(char *name)
948 printf("%s\n", fio_version_string);
949 printf("\t-s IO is sequential\n");
950 printf("\t-b Block size in KiB for each IO\n");
951 printf("\t-t Runtime in seconds\n");
952 printf("\t-R Exit all threads on failure to meet rate goal\n");
953 printf("\t-o Use O_DIRECT\n");
954 printf("\t-l Generate per-job latency logs\n");
955 printf("\t-w Generate per-job bandwidth logs\n");
956 printf("\t-f Job file (Required)\n");
957 printf("\t-v Print version info and exit\n");
960 static void parse_cmd_line(int argc, char *argv[])
964 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
967 def_thread.sequential = !!atoi(optarg);
970 def_thread.bs = atoi(optarg);
971 def_thread.bs <<= 10;
972 if (!def_thread.bs) {
973 printf("bad block size\n");
974 def_thread.bs = DEF_BS;
978 def_thread.timeout = atoi(optarg);
981 repeatable = !!atoi(optarg);
984 rate_quit = !!atoi(optarg);
987 def_thread.odirect = !!atoi(optarg);
990 ini_file = strdup(optarg);
1002 printf("%s\n", fio_version_string);
1007 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1008 ini_file = strdup(argv[argc - 1]);
1011 static void free_shm(void)
1013 struct shmid_ds sbuf;
1016 shmdt((void *) threads);
1018 shmctl(shm_id, IPC_RMID, &sbuf);
1022 static int setup_thread_area(void)
1025 * 1024 is too much on some machines, scale max_jobs if
1026 * we get a failure that looks like too large a shm segment
1029 int s = max_jobs * sizeof(struct thread_data);
1031 shm_id = shmget(0, s, IPC_CREAT | 0600);
1034 if (errno != EINVAL) {
1045 threads = shmat(shm_id, NULL, 0);
1046 if (threads == (void *) -1) {
1055 int parse_options(int argc, char *argv[])
1057 if (setup_thread_area())
1059 if (fill_def_thread())
1062 parse_cmd_line(argc, argv);
1065 printf("Need job file\n");
1070 if (parse_jobs_ini(ini_file))