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.3";
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 int setup_rate(struct thread_data *td)
65 if (td->rate < td->ratemin) {
66 fprintf(stderr, "min rate larger than nominal rate\n");
70 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
71 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
72 td->rate_pending_usleep = 0;
76 static void setup_log(struct io_log **log)
78 struct io_log *l = malloc(sizeof(*l));
81 l->max_samples = 1024;
82 l->log = malloc(l->max_samples * sizeof(struct io_sample));
86 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
92 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
93 f = fopen(file_name, "w");
99 for (i = 0; i < log->nr_samples; i++)
100 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
107 static struct thread_data *get_new_job(int global, struct thread_data *parent)
109 struct thread_data *td;
113 if (thread_number >= max_jobs)
116 td = &threads[thread_number++];
120 memset(td, 0, sizeof(*td));
123 td->thread_number = thread_number;
125 td->ddir = parent->ddir;
126 td->ioprio = parent->ioprio;
127 td->sequential = parent->sequential;
129 td->min_bs = parent->min_bs;
130 td->max_bs = parent->max_bs;
131 td->odirect = parent->odirect;
132 td->thinktime = parent->thinktime;
133 td->fsync_blocks = parent->fsync_blocks;
134 td->start_delay = parent->start_delay;
135 td->timeout = parent->timeout;
136 td->io_engine = parent->io_engine;
137 td->create_file = parent->create_file;
138 td->overwrite = parent->overwrite;
139 td->invalidate_cache = parent->invalidate_cache;
140 td->file_size = parent->file_size;
141 td->file_offset = parent->file_offset;
142 td->zone_size = parent->zone_size;
143 td->zone_skip = parent->zone_skip;
144 td->rate = parent->rate;
145 td->ratemin = parent->ratemin;
146 td->ratecycle = parent->ratecycle;
147 td->iodepth = parent->iodepth;
148 td->sync_io = parent->sync_io;
149 td->mem_type = parent->mem_type;
150 td->bw_avg_time = parent->bw_avg_time;
151 td->create_serialize = parent->create_serialize;
152 td->create_fsync = parent->create_fsync;
153 td->loops = parent->loops;
154 td->verify = parent->verify;
155 td->stonewall = parent->stonewall;
156 td->numjobs = parent->numjobs;
157 td->use_thread = parent->use_thread;
158 td->do_disk_util = parent->do_disk_util;
159 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
160 strcpy(td->io_engine_name, parent->io_engine_name);
165 static void put_job(struct thread_data *td)
167 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
171 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
173 char *ddir_str[] = { "read", "write", "randread", "randwrite",
174 "rw", NULL, "randrw" };
178 #ifndef FIO_HAVE_LIBAIO
179 if (td->io_engine == FIO_LIBAIO) {
180 fprintf(stderr, "Linux libaio not available\n");
184 #ifndef FIO_HAVE_POSIXAIO
185 if (td->io_engine == FIO_POSIXAIO) {
186 fprintf(stderr, "posix aio not available\n");
192 * the def_thread is just for options, it's not a real job
194 if (td == &def_thread)
197 if (td->io_engine & FIO_SYNCIO)
205 * only really works for sequential io for now
207 if (td->zone_size && !td->sequential)
210 td->filetype = FIO_TYPE_FILE;
211 if (!stat(jobname, &sb)) {
212 if (S_ISBLK(sb.st_mode))
213 td->filetype = FIO_TYPE_BD;
214 else if (S_ISCHR(sb.st_mode))
215 td->filetype = FIO_TYPE_CHAR;
218 if (td->filetype == FIO_TYPE_FILE) {
219 if (td->directory[0] != '\0')
220 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
222 sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
224 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
226 sem_init(&td->mutex, 0, 0);
228 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
229 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
230 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
232 if (td->min_bs == -1U)
234 if (td->max_bs == -1U)
236 if (td_read(td) && !td_rw(td))
239 if (td->stonewall && td->thread_number > 1)
242 td->groupid = groupid;
248 setup_log(&td->slat_log);
249 setup_log(&td->clat_log);
252 setup_log(&td->bw_log);
254 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
257 printf("Client%d (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->thread_number, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
258 else if (job_add_num == 1)
262 * recurse add identical jobs, clear numjobs and stonewall options
263 * as they don't apply to sub-jobs
265 numjobs = td->numjobs;
267 struct thread_data *td_new = get_new_job(0, td);
273 td_new->stonewall = 0;
274 td_new->jobnum = numjobs;
275 job_add_num = numjobs - 1;
277 if (add_job(td_new, jobname, job_add_num))
286 int init_random_state(struct thread_data *td)
288 unsigned long seeds[4];
289 int fd, num_maps, blocks;
291 fd = open("/dev/urandom", O_RDONLY);
293 td_verror(td, errno);
297 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
305 srand48_r(seeds[0], &td->bsrange_state);
306 srand48_r(seeds[1], &td->verify_state);
307 srand48_r(seeds[2], &td->rwmix_state);
313 seeds[3] = DEF_RANDSEED;
315 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
316 num_maps = blocks / BLOCKS_PER_MAP;
317 td->file_map = malloc(num_maps * sizeof(long));
318 td->num_maps = num_maps;
319 memset(td->file_map, 0, num_maps * sizeof(long));
321 srand48_r(seeds[3], &td->random_state);
325 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
327 #ifdef FIO_HAVE_CPU_AFFINITY
332 for (i = 0; i < sizeof(int) * 8; i++) {
334 CPU_SET(i, &cpumask);
339 static unsigned long get_mult(char c)
350 return 1024 * 1024 * 1024;
357 * convert string after '=' into decimal value, noting any size suffix
359 static int str_cnv(char *p, unsigned long long *val)
364 str = strchr(p, '=');
371 *val = strtoul(str, NULL, 10);
372 if (*val == ULONG_MAX && errno == ERANGE)
375 *val *= get_mult(str[len - 1]);
379 static int check_strcnv(char *p, char *name, unsigned long long *val)
381 if (strncmp(p, name, strlen(name) - 1))
384 return str_cnv(p, val);
387 static void strip_blank_front(char **p)
395 static void strip_blank_end(char *p)
397 char *s = p + strlen(p) - 1;
399 while (isspace(*s) || iscntrl(*s))
405 typedef int (str_cb_fn)(struct thread_data *, char *);
407 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
411 if (strncmp(p, name, strlen(name)))
423 strip_blank_front(&s);
427 static int check_strstore(char *p, char *name, char *dest)
431 if (strncmp(p, name, strlen(name)))
443 strip_blank_front(&s);
449 static int __check_range(char *str, unsigned long *val)
453 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
454 *val *= get_mult(suffix);
458 if (sscanf(str, "%lu", val) == 1)
464 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
469 if (strncmp(p, name, strlen(name)))
475 str = strstr(p, name);
481 str = strchr(p, '=');
486 * 'p' now holds whatever is after the '=' sign
491 * terminate p1 at the '-' sign
500 if (!__check_range(p1, s) && !__check_range(p2, e))
506 static int check_int(char *p, char *name, unsigned int *val)
510 if (strncmp(p, name, strlen(name)))
513 str = strstr(p, name);
517 str = strchr(p, '=');
523 if (sscanf(str, "%u", val) == 1)
529 static int check_strset(char *p, char *name)
531 return strncmp(p, name, strlen(name));
534 static int is_empty_or_comment(char *line)
538 for (i = 0; i < strlen(line); i++) {
541 if (!isspace(line[i]) && !iscntrl(line[i]))
548 static int str_rw_cb(struct thread_data *td, char *mem)
550 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
551 td->ddir = DDIR_READ;
554 } else if (!strncmp(mem, "randread", 8)) {
555 td->ddir = DDIR_READ;
558 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
559 td->ddir = DDIR_WRITE;
562 } else if (!strncmp(mem, "randwrite", 9)) {
563 td->ddir = DDIR_WRITE;
566 } else if (!strncmp(mem, "rw", 2)) {
571 } else if (!strncmp(mem, "randrw", 6)) {
578 fprintf(stderr, "bad data direction: %s\n", mem);
582 static int str_verify_cb(struct thread_data *td, char *mem)
584 if (!strncmp(mem, "0", 1)) {
585 td->verify = VERIFY_NONE;
587 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
588 td->verify = VERIFY_MD5;
590 } else if (!strncmp(mem, "crc32", 5)) {
591 td->verify = VERIFY_CRC32;
595 fprintf(stderr, "bad verify type: %s\n", mem);
599 static int str_mem_cb(struct thread_data *td, char *mem)
601 if (!strncmp(mem, "malloc", 6)) {
602 td->mem_type = MEM_MALLOC;
604 } else if (!strncmp(mem, "shm", 3)) {
605 td->mem_type = MEM_SHM;
607 } else if (!strncmp(mem, "mmap", 4)) {
608 td->mem_type = MEM_MMAP;
612 fprintf(stderr, "bad mem type: %s\n", mem);
616 static int str_ioengine_cb(struct thread_data *td, char *str)
618 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
619 !strncmp(str, "libaio", 6)) {
620 strcpy(td->io_engine_name, "libaio");
621 td->io_engine = FIO_LIBAIO;
623 } else if (!strncmp(str, "posixaio", 8)) {
624 strcpy(td->io_engine_name, "posixaio");
625 td->io_engine = FIO_POSIXAIO;
627 } else if (!strncmp(str, "sync", 4)) {
628 strcpy(td->io_engine_name, "sync");
629 td->io_engine = FIO_SYNCIO;
631 } else if (!strncmp(str, "mmap", 4)) {
632 strcpy(td->io_engine_name, "mmap");
633 td->io_engine = FIO_MMAPIO;
635 } else if (!strncmp(str, "sgio", 4)) {
636 strcpy(td->io_engine_name, "sgio");
637 td->io_engine = FIO_SGIO;
639 } else if (!strncmp(str, "splice", 6)) {
640 strcpy(td->io_engine_name, "splice");
641 td->io_engine = FIO_SPLICEIO;
645 fprintf(stderr, "bad ioengine type: %s\n", str);
649 static int str_iolog_cb(struct thread_data *td, char *file)
651 strncpy(td->iolog_file, file, sizeof(td->iolog_file) - 1);
655 int parse_jobs_ini(char *file)
657 unsigned int prioclass, prio, cpu, global, il;
658 unsigned long long ull;
659 unsigned long ul1, ul2;
660 struct thread_data *td;
666 f = fopen(file, "r");
668 perror("fopen job file");
672 string = malloc(4096);
675 while ((p = fgets(string, 4096, f)) != NULL) {
676 if (is_empty_or_comment(p))
678 if (sscanf(p, "[%s]", name) != 1)
681 global = !strncmp(name, "global", 6);
683 name[strlen(name) - 1] = '\0';
685 td = get_new_job(global, &def_thread);
690 while ((p = fgets(string, 4096, f)) != NULL) {
691 if (is_empty_or_comment(p))
695 strip_blank_front(&p);
698 if (!check_int(p, "prio", &prio)) {
699 #ifndef FIO_HAVE_IOPRIO
700 fprintf(stderr, "io priorities not available\n");
707 if (!check_int(p, "prioclass", &prioclass)) {
708 #ifndef FIO_HAVE_IOPRIO
709 fprintf(stderr, "io priorities not available\n");
712 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
716 if (!check_int(p, "direct", &td->odirect)) {
720 if (!check_int(p, "rate", &td->rate)) {
724 if (!check_int(p, "ratemin", &td->ratemin)) {
728 if (!check_int(p, "ratecycle", &td->ratecycle)) {
732 if (!check_int(p, "thinktime", &td->thinktime)) {
736 if (!check_int(p, "cpumask", &cpu)) {
737 #ifndef FIO_HAVE_CPU_AFFINITY
738 fprintf(stderr, "cpu affinity not available\n");
741 fill_cpu_mask(td->cpumask, cpu);
745 if (!check_int(p, "fsync", &td->fsync_blocks)) {
750 if (!check_int(p, "startdelay", &td->start_delay)) {
754 if (!check_int(p, "timeout", &td->timeout)) {
758 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
762 if (!check_int(p, "iodepth", &td->iodepth)) {
766 if (!check_int(p, "sync", &td->sync_io)) {
770 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
774 if (!check_int(p, "create_serialize", &td->create_serialize)) {
778 if (!check_int(p, "create_fsync", &td->create_fsync)) {
782 if (!check_int(p, "end_fsync", &td->end_fsync)) {
786 if (!check_int(p, "loops", &td->loops)) {
790 if (!check_int(p, "numjobs", &td->numjobs)) {
794 if (!check_int(p, "overwrite", &td->overwrite)) {
798 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
802 if (!check_int(p, "rwmixread", &il)) {
809 if (!check_int(p, "rwmixwrite", &il)) {
812 td->rwmixread = 100 - il;
816 if (!check_int(p, "nice", &td->nice)) {
820 if (!check_range(p, "bsrange", &ul1, &ul2)) {
831 if (!check_strcnv(p, "bs", &ull)) {
836 if (!check_strcnv(p, "size", &td->file_size)) {
840 if (!check_strcnv(p, "offset", &td->file_offset)) {
844 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
848 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
852 if (!check_strcnv(p, "lockmem", &mlock_size)) {
856 if (!check_strstore(p, "directory", td->directory)) {
860 if (!check_str(p, "mem", str_mem_cb, td)) {
864 if (!check_str(p, "verify", str_verify_cb, td)) {
868 if (!check_str(p, "rw", str_rw_cb, td)) {
872 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
876 if (!check_strset(p, "create")) {
881 if (!check_strset(p, "exitall")) {
882 exitall_on_terminate = 1;
886 if (!check_strset(p, "stonewall")) {
891 if (!check_strset(p, "thread")) {
896 if (!check_str(p, "iolog", str_iolog_cb, td)) {
897 printf("got read iolog\n");
903 if (!td->read_iolog &&
904 !check_str(p, "write_iolog", str_iolog_cb, td)) {
905 printf("got write iolog\n");
911 printf("Client%d: bad option %s\n",td->thread_number,p);
916 if (add_job(td, name, 0))
926 static int fill_def_thread(void)
928 memset(&def_thread, 0, sizeof(def_thread));
930 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
931 perror("sched_getaffinity");
938 def_thread.ddir = DDIR_READ;
939 def_thread.iomix = 0;
940 def_thread.bs = DEF_BS;
941 def_thread.min_bs = -1;
942 def_thread.max_bs = -1;
943 def_thread.io_engine = DEF_IO_ENGINE;
944 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
945 def_thread.odirect = DEF_ODIRECT;
946 def_thread.ratecycle = DEF_RATE_CYCLE;
947 def_thread.sequential = DEF_SEQUENTIAL;
948 def_thread.timeout = DEF_TIMEOUT;
949 def_thread.create_file = DEF_CREATE;
950 def_thread.overwrite = DEF_OVERWRITE;
951 def_thread.invalidate_cache = DEF_INVALIDATE;
952 def_thread.sync_io = DEF_SYNCIO;
953 def_thread.mem_type = MEM_MALLOC;
954 def_thread.bw_avg_time = DEF_BWAVGTIME;
955 def_thread.create_serialize = DEF_CREATE_SER;
956 def_thread.create_fsync = DEF_CREATE_FSYNC;
957 def_thread.loops = DEF_LOOPS;
958 def_thread.verify = DEF_VERIFY;
959 def_thread.stonewall = DEF_STONEWALL;
960 def_thread.numjobs = DEF_NUMJOBS;
961 def_thread.use_thread = DEF_USE_THREAD;
962 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
963 def_thread.rwmixread = DEF_RWMIX_READ;
964 def_thread.nice = DEF_NICE;
965 #ifdef FIO_HAVE_DISK_UTIL
966 def_thread.do_disk_util = 1;
972 static void usage(char *name)
974 printf("%s\n", fio_version_string);
975 printf("\t-s IO is sequential\n");
976 printf("\t-b Block size in KiB for each IO\n");
977 printf("\t-t Runtime in seconds\n");
978 printf("\t-R Exit all threads on failure to meet rate goal\n");
979 printf("\t-o Use O_DIRECT\n");
980 printf("\t-l Generate per-job latency logs\n");
981 printf("\t-w Generate per-job bandwidth logs\n");
982 printf("\t-f Job file (Required)\n");
983 printf("\t-v Print version info and exit\n");
986 static void parse_cmd_line(int argc, char *argv[])
990 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
993 def_thread.sequential = !!atoi(optarg);
996 def_thread.bs = atoi(optarg);
997 def_thread.bs <<= 10;
998 if (!def_thread.bs) {
999 printf("bad block size\n");
1000 def_thread.bs = DEF_BS;
1004 def_thread.timeout = atoi(optarg);
1007 repeatable = !!atoi(optarg);
1010 rate_quit = !!atoi(optarg);
1013 def_thread.odirect = !!atoi(optarg);
1016 ini_file = strdup(optarg);
1028 printf("%s\n", fio_version_string);
1033 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1034 ini_file = strdup(argv[argc - 1]);
1037 static void free_shm(void)
1039 struct shmid_ds sbuf;
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[])
1083 if (setup_thread_area())
1085 if (fill_def_thread())
1088 parse_cmd_line(argc, argv);
1091 printf("Need job file\n");
1096 if (parse_jobs_ini(ini_file)) {