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++];
71 memset(td, 0, sizeof(*td));
74 td->thread_number = thread_number;
76 td->ddir = parent->ddir;
77 td->ioprio = parent->ioprio;
78 td->sequential = parent->sequential;
80 td->min_bs = parent->min_bs;
81 td->max_bs = parent->max_bs;
82 td->odirect = parent->odirect;
83 td->thinktime = parent->thinktime;
84 td->fsync_blocks = parent->fsync_blocks;
85 td->start_delay = parent->start_delay;
86 td->timeout = parent->timeout;
87 td->io_engine = parent->io_engine;
88 td->create_file = parent->create_file;
89 td->overwrite = parent->overwrite;
90 td->invalidate_cache = parent->invalidate_cache;
91 td->file_size = parent->file_size;
92 td->file_offset = parent->file_offset;
93 td->zone_size = parent->zone_size;
94 td->zone_skip = parent->zone_skip;
95 td->rate = parent->rate;
96 td->ratemin = parent->ratemin;
97 td->ratecycle = parent->ratecycle;
98 td->iodepth = parent->iodepth;
99 td->sync_io = parent->sync_io;
100 td->mem_type = parent->mem_type;
101 td->bw_avg_time = parent->bw_avg_time;
102 td->create_serialize = parent->create_serialize;
103 td->create_fsync = parent->create_fsync;
104 td->loops = parent->loops;
105 td->verify = parent->verify;
106 td->stonewall = parent->stonewall;
107 td->numjobs = parent->numjobs;
108 td->use_thread = parent->use_thread;
109 td->do_disk_util = parent->do_disk_util;
110 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
111 strcpy(td->io_engine_name, parent->io_engine_name);
116 static void put_job(struct thread_data *td)
118 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
122 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
124 char *ddir_str[] = { "read", "write", "randread", "randwrite",
125 "rw", NULL, "randrw" };
129 #ifndef FIO_HAVE_LIBAIO
130 if (td->io_engine == FIO_LIBAIO) {
131 fprintf(stderr, "Linux libaio not available\n");
135 #ifndef FIO_HAVE_POSIXAIO
136 if (td->io_engine == FIO_POSIXAIO) {
137 fprintf(stderr, "posix aio not available\n");
143 * the def_thread is just for options, it's not a real job
145 if (td == &def_thread)
148 if (td->io_engine & FIO_SYNCIO)
156 * only really works for sequential io for now
158 if (td->zone_size && !td->sequential)
161 td->filetype = FIO_TYPE_FILE;
162 if (!stat(jobname, &sb)) {
163 if (S_ISBLK(sb.st_mode))
164 td->filetype = FIO_TYPE_BD;
165 else if (S_ISCHR(sb.st_mode))
166 td->filetype = FIO_TYPE_CHAR;
169 if (td->filetype == FIO_TYPE_FILE) {
170 if (td->directory && td->directory[0] != '\0')
171 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
173 sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
175 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
177 sem_init(&td->mutex, 0, 0);
179 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
180 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
181 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
183 if (td->min_bs == -1U)
185 if (td->max_bs == -1U)
187 if (td_read(td) && !td_rw(td))
190 if (td->stonewall && td->thread_number > 1)
193 td->groupid = groupid;
199 setup_log(&td->slat_log);
200 setup_log(&td->clat_log);
203 setup_log(&td->bw_log);
205 if (td->name[0] == '\0')
206 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
208 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
211 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);
212 else if (job_add_num == 1)
216 * recurse add identical jobs, clear numjobs and stonewall options
217 * as they don't apply to sub-jobs
219 numjobs = td->numjobs;
221 struct thread_data *td_new = get_new_job(0, td);
227 td_new->stonewall = 0;
228 td_new->jobnum = numjobs;
229 job_add_num = numjobs - 1;
231 if (add_job(td_new, jobname, job_add_num))
240 int init_random_state(struct thread_data *td)
242 unsigned long seeds[4];
243 int fd, num_maps, blocks;
245 fd = open("/dev/urandom", O_RDONLY);
247 td_verror(td, errno);
251 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
259 srand48_r(seeds[0], &td->bsrange_state);
260 srand48_r(seeds[1], &td->verify_state);
261 srand48_r(seeds[2], &td->rwmix_state);
267 seeds[3] = DEF_RANDSEED;
269 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
270 num_maps = blocks / BLOCKS_PER_MAP;
271 td->file_map = malloc(num_maps * sizeof(long));
272 td->num_maps = num_maps;
273 memset(td->file_map, 0, num_maps * sizeof(long));
275 srand48_r(seeds[3], &td->random_state);
279 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
281 #ifdef FIO_HAVE_CPU_AFFINITY
286 for (i = 0; i < sizeof(int) * 8; i++) {
288 CPU_SET(i, &cpumask);
293 static unsigned long get_mult(char c)
304 return 1024 * 1024 * 1024;
311 * convert string after '=' into decimal value, noting any size suffix
313 static int str_cnv(char *p, unsigned long long *val)
318 str = strchr(p, '=');
325 *val = strtoul(str, NULL, 10);
326 if (*val == ULONG_MAX && errno == ERANGE)
329 *val *= get_mult(str[len - 1]);
333 static int check_strcnv(char *p, char *name, unsigned long long *val)
335 if (strncmp(p, name, strlen(name) - 1))
338 return str_cnv(p, val);
341 static void strip_blank_front(char **p)
349 static void strip_blank_end(char *p)
351 char *s = p + strlen(p) - 1;
353 while (isspace(*s) || iscntrl(*s))
359 typedef int (str_cb_fn)(struct thread_data *, char *);
361 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
365 if (strncmp(p, name, strlen(name)))
377 strip_blank_front(&s);
381 static int check_strstore(char *p, char *name, char *dest)
385 if (strncmp(p, name, strlen(name)))
397 strip_blank_front(&s);
403 static int __check_range(char *str, unsigned long *val)
407 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
408 *val *= get_mult(suffix);
412 if (sscanf(str, "%lu", val) == 1)
418 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
423 if (strncmp(p, name, strlen(name)))
429 str = strstr(p, name);
435 str = strchr(p, '=');
440 * 'p' now holds whatever is after the '=' sign
445 * terminate p1 at the '-' sign
454 if (!__check_range(p1, s) && !__check_range(p2, e))
460 static int check_int(char *p, char *name, unsigned int *val)
464 if (strncmp(p, name, strlen(name)))
467 str = strstr(p, name);
471 str = strchr(p, '=');
477 if (sscanf(str, "%u", val) == 1)
483 static int check_strset(char *p, char *name)
485 return strncmp(p, name, strlen(name));
488 static int is_empty_or_comment(char *line)
492 for (i = 0; i < strlen(line); i++) {
495 if (!isspace(line[i]) && !iscntrl(line[i]))
502 static int str_rw_cb(struct thread_data *td, char *mem)
504 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
505 td->ddir = DDIR_READ;
508 } else if (!strncmp(mem, "randread", 8)) {
509 td->ddir = DDIR_READ;
512 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
513 td->ddir = DDIR_WRITE;
516 } else if (!strncmp(mem, "randwrite", 9)) {
517 td->ddir = DDIR_WRITE;
520 } else if (!strncmp(mem, "rw", 2)) {
525 } else if (!strncmp(mem, "randrw", 6)) {
532 fprintf(stderr, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
536 static int str_verify_cb(struct thread_data *td, char *mem)
538 if (!strncmp(mem, "0", 1)) {
539 td->verify = VERIFY_NONE;
541 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
542 td->verify = VERIFY_MD5;
544 } else if (!strncmp(mem, "crc32", 5)) {
545 td->verify = VERIFY_CRC32;
549 fprintf(stderr, "fio: verify types: md5, crc32\n");
553 static int str_mem_cb(struct thread_data *td, char *mem)
555 if (!strncmp(mem, "malloc", 6)) {
556 td->mem_type = MEM_MALLOC;
558 } else if (!strncmp(mem, "shm", 3)) {
559 td->mem_type = MEM_SHM;
561 } else if (!strncmp(mem, "mmap", 4)) {
562 td->mem_type = MEM_MMAP;
566 fprintf(stderr, "fio: mem type: malloc, shm, mmap\n");
570 static int str_ioengine_cb(struct thread_data *td, char *str)
572 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
573 !strncmp(str, "libaio", 6)) {
574 strcpy(td->io_engine_name, "libaio");
575 td->io_engine = FIO_LIBAIO;
577 } else if (!strncmp(str, "posixaio", 8)) {
578 strcpy(td->io_engine_name, "posixaio");
579 td->io_engine = FIO_POSIXAIO;
581 } else if (!strncmp(str, "sync", 4)) {
582 strcpy(td->io_engine_name, "sync");
583 td->io_engine = FIO_SYNCIO;
585 } else if (!strncmp(str, "mmap", 4)) {
586 strcpy(td->io_engine_name, "mmap");
587 td->io_engine = FIO_MMAPIO;
589 } else if (!strncmp(str, "sgio", 4)) {
590 strcpy(td->io_engine_name, "sgio");
591 td->io_engine = FIO_SGIO;
593 } else if (!strncmp(str, "splice", 6)) {
594 strcpy(td->io_engine_name, "splice");
595 td->io_engine = FIO_SPLICEIO;
599 fprintf(stderr, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
604 * This is our [ini] type file parser.
606 int parse_jobs_ini(char *file)
608 unsigned int prioclass, prio, cpu, global, il;
609 unsigned long long ull;
610 unsigned long ul1, ul2;
611 struct thread_data *td;
612 char *string, *name, *tmpbuf;
618 f = fopen(file, "r");
620 perror("fopen job file");
624 string = malloc(4096);
626 tmpbuf = malloc(4096);
628 while ((p = fgets(string, 4096, f)) != NULL) {
631 if (is_empty_or_comment(p))
633 if (sscanf(p, "[%s]", name) != 1)
636 global = !strncmp(name, "global", 6);
638 name[strlen(name) - 1] = '\0';
640 td = get_new_job(global, &def_thread);
647 while ((p = fgets(string, 4096, f)) != NULL) {
648 if (is_empty_or_comment(p))
652 strip_blank_front(&p);
655 if (!check_int(p, "prio", &prio)) {
656 #ifndef FIO_HAVE_IOPRIO
657 fprintf(stderr, "io priorities not available\n");
665 if (!check_int(p, "prioclass", &prioclass)) {
666 #ifndef FIO_HAVE_IOPRIO
667 fprintf(stderr, "io priorities not available\n");
671 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
675 if (!check_int(p, "direct", &td->odirect)) {
679 if (!check_int(p, "rate", &td->rate)) {
683 if (!check_int(p, "ratemin", &td->ratemin)) {
687 if (!check_int(p, "ratecycle", &td->ratecycle)) {
691 if (!check_int(p, "thinktime", &td->thinktime)) {
695 if (!check_int(p, "cpumask", &cpu)) {
696 #ifndef FIO_HAVE_CPU_AFFINITY
697 fprintf(stderr, "cpu affinity not available\n");
701 fill_cpu_mask(td->cpumask, cpu);
705 if (!check_int(p, "fsync", &td->fsync_blocks)) {
710 if (!check_int(p, "startdelay", &td->start_delay)) {
714 if (!check_int(p, "timeout", &td->timeout)) {
718 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
722 if (!check_int(p, "iodepth", &td->iodepth)) {
726 if (!check_int(p, "sync", &td->sync_io)) {
730 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
734 if (!check_int(p, "create_serialize", &td->create_serialize)) {
738 if (!check_int(p, "create_fsync", &td->create_fsync)) {
742 if (!check_int(p, "end_fsync", &td->end_fsync)) {
746 if (!check_int(p, "loops", &td->loops)) {
750 if (!check_int(p, "numjobs", &td->numjobs)) {
754 if (!check_int(p, "overwrite", &td->overwrite)) {
758 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
762 if (!check_int(p, "rwmixread", &il)) {
769 if (!check_int(p, "rwmixwrite", &il)) {
772 td->rwmixread = 100 - il;
776 if (!check_int(p, "nice", &td->nice)) {
780 if (!check_range(p, "bsrange", &ul1, &ul2)) {
791 if (!check_strcnv(p, "bs", &ull)) {
796 if (!check_strcnv(p, "size", &td->file_size)) {
800 if (!check_strcnv(p, "offset", &td->file_offset)) {
804 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
808 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
812 if (!check_strcnv(p, "lockmem", &mlock_size)) {
816 if (!check_strstore(p, "directory", tmpbuf)) {
817 td->directory = strdup(tmpbuf);
821 if (!check_strstore(p, "name", tmpbuf)) {
822 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
826 if (!check_str(p, "mem", str_mem_cb, td)) {
830 if (!check_str(p, "verify", str_verify_cb, td)) {
834 if (!check_str(p, "rw", str_rw_cb, td)) {
838 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
842 if (!check_strset(p, "create")) {
847 if (!check_strset(p, "exitall")) {
848 exitall_on_terminate = 1;
852 if (!check_strset(p, "stonewall")) {
857 if (!check_strset(p, "thread")) {
862 if (!check_strstore(p, "iolog", tmpbuf)) {
863 td->iolog_file = strdup(tmpbuf);
869 if (!td->read_iolog &&
870 !check_strstore(p, "write_iolog", tmpbuf)) {
871 td->iolog_file = strdup(tmpbuf);
876 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
877 td->exec_prerun = strdup(tmpbuf);
881 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
882 td->exec_postrun = strdup(tmpbuf);
886 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
887 #ifndef FIO_HAVE_IOSCHED_SWITCH
888 fprintf(stderr, "io scheduler switching not available\n");
892 td->ioscheduler = strdup(tmpbuf);
899 * Don't break here, continue parsing options so we
900 * dump all the bad ones. Makes trial/error fixups
901 * easier on the user.
903 printf("Client%d: bad option %s\n",td->thread_number,p);
909 ret = add_job(td, name, 0);
922 static int fill_def_thread(void)
924 memset(&def_thread, 0, sizeof(def_thread));
926 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
927 perror("sched_getaffinity");
934 def_thread.ddir = DDIR_READ;
935 def_thread.iomix = 0;
936 def_thread.bs = DEF_BS;
937 def_thread.min_bs = -1;
938 def_thread.max_bs = -1;
939 def_thread.io_engine = DEF_IO_ENGINE;
940 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
941 def_thread.odirect = DEF_ODIRECT;
942 def_thread.ratecycle = DEF_RATE_CYCLE;
943 def_thread.sequential = DEF_SEQUENTIAL;
944 def_thread.timeout = DEF_TIMEOUT;
945 def_thread.create_file = DEF_CREATE;
946 def_thread.overwrite = DEF_OVERWRITE;
947 def_thread.invalidate_cache = DEF_INVALIDATE;
948 def_thread.sync_io = DEF_SYNCIO;
949 def_thread.mem_type = MEM_MALLOC;
950 def_thread.bw_avg_time = DEF_BWAVGTIME;
951 def_thread.create_serialize = DEF_CREATE_SER;
952 def_thread.create_fsync = DEF_CREATE_FSYNC;
953 def_thread.loops = DEF_LOOPS;
954 def_thread.verify = DEF_VERIFY;
955 def_thread.stonewall = DEF_STONEWALL;
956 def_thread.numjobs = DEF_NUMJOBS;
957 def_thread.use_thread = DEF_USE_THREAD;
958 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
959 def_thread.rwmixread = DEF_RWMIX_READ;
960 def_thread.nice = DEF_NICE;
961 #ifdef FIO_HAVE_DISK_UTIL
962 def_thread.do_disk_util = 1;
968 static void usage(char *name)
970 printf("%s\n", fio_version_string);
971 printf("\t-s IO is sequential\n");
972 printf("\t-b Block size in KiB for each IO\n");
973 printf("\t-t Runtime in seconds\n");
974 printf("\t-R Exit all threads on failure to meet rate goal\n");
975 printf("\t-o Use O_DIRECT\n");
976 printf("\t-l Generate per-job latency logs\n");
977 printf("\t-w Generate per-job bandwidth logs\n");
978 printf("\t-f Job file (Required)\n");
979 printf("\t-v Print version info and exit\n");
982 static void parse_cmd_line(int argc, char *argv[])
986 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
989 def_thread.sequential = !!atoi(optarg);
992 def_thread.bs = atoi(optarg);
993 def_thread.bs <<= 10;
994 if (!def_thread.bs) {
995 printf("bad block size\n");
996 def_thread.bs = DEF_BS;
1000 def_thread.timeout = atoi(optarg);
1003 repeatable = !!atoi(optarg);
1006 rate_quit = !!atoi(optarg);
1009 def_thread.odirect = !!atoi(optarg);
1012 ini_file = strdup(optarg);
1024 printf("%s\n", fio_version_string);
1029 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1030 ini_file = strdup(argv[argc - 1]);
1033 static void free_shm(void)
1035 struct shmid_ds sbuf;
1040 shmctl(shm_id, IPC_RMID, &sbuf);
1044 static int setup_thread_area(void)
1047 * 1024 is too much on some machines, scale max_jobs if
1048 * we get a failure that looks like too large a shm segment
1051 int s = max_jobs * sizeof(struct thread_data);
1053 shm_id = shmget(0, s, IPC_CREAT | 0600);
1056 if (errno != EINVAL) {
1067 threads = shmat(shm_id, NULL, 0);
1068 if (threads == (void *) -1) {
1077 int parse_options(int argc, char *argv[])
1079 if (setup_thread_area())
1081 if (fill_def_thread())
1084 parse_cmd_line(argc, argv);
1087 printf("Need job file\n");
1092 if (parse_jobs_ini(ini_file))