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)
40 static char fio_version_string[] = "fio 1.3";
42 static int repeatable = DEF_RAND_REPEAT;
43 static char *ini_file;
44 static int max_jobs = MAX_JOBS;
46 struct thread_data def_thread;
47 struct thread_data *threads = NULL;
50 int write_lat_log = 0;
52 int exitall_on_terminate = 0;
53 unsigned long long mlock_size = 0;
55 static int setup_rate(struct thread_data *td)
62 if (td->rate < td->ratemin) {
63 fprintf(stderr, "min rate larger than nominal rate\n");
67 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
68 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
69 td->rate_pending_usleep = 0;
73 static void setup_log(struct io_log **log)
75 struct io_log *l = malloc(sizeof(*l));
78 l->max_samples = 1024;
79 l->log = malloc(l->max_samples * sizeof(struct io_sample));
83 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
89 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
90 f = fopen(file_name, "w");
96 for (i = 0; i < log->nr_samples; i++)
97 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
104 static struct thread_data *get_new_job(int global, struct thread_data *parent)
106 struct thread_data *td;
110 if (thread_number >= max_jobs)
113 td = &threads[thread_number++];
117 memset(td, 0, sizeof(*td));
120 td->thread_number = thread_number;
122 td->ddir = parent->ddir;
123 td->ioprio = parent->ioprio;
124 td->sequential = parent->sequential;
126 td->min_bs = parent->min_bs;
127 td->max_bs = parent->max_bs;
128 td->odirect = parent->odirect;
129 td->thinktime = parent->thinktime;
130 td->fsync_blocks = parent->fsync_blocks;
131 td->start_delay = parent->start_delay;
132 td->timeout = parent->timeout;
133 td->io_engine = parent->io_engine;
134 td->create_file = parent->create_file;
135 td->overwrite = parent->overwrite;
136 td->invalidate_cache = parent->invalidate_cache;
137 td->file_size = parent->file_size;
138 td->file_offset = parent->file_offset;
139 td->zone_size = parent->zone_size;
140 td->zone_skip = parent->zone_skip;
141 td->rate = parent->rate;
142 td->ratemin = parent->ratemin;
143 td->ratecycle = parent->ratecycle;
144 td->iodepth = parent->iodepth;
145 td->sync_io = parent->sync_io;
146 td->mem_type = parent->mem_type;
147 td->bw_avg_time = parent->bw_avg_time;
148 td->create_serialize = parent->create_serialize;
149 td->create_fsync = parent->create_fsync;
150 td->loops = parent->loops;
151 td->verify = parent->verify;
152 td->stonewall = parent->stonewall;
153 td->numjobs = parent->numjobs;
154 td->use_thread = parent->use_thread;
155 td->do_disk_util = parent->do_disk_util;
156 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
157 strcpy(td->io_engine_name, parent->io_engine_name);
162 static void put_job(struct thread_data *td)
164 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
168 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
170 char *ddir_str[] = { "read", "write", "randread", "randwrite",
171 "rw", NULL, "randrw" };
175 #ifndef FIO_HAVE_LIBAIO
176 if (td->io_engine == FIO_LIBAIO) {
177 fprintf(stderr, "Linux libaio not available\n");
181 #ifndef FIO_HAVE_POSIXAIO
182 if (td->io_engine == FIO_POSIXAIO) {
183 fprintf(stderr, "posix aio not available\n");
189 * the def_thread is just for options, it's not a real job
191 if (td == &def_thread)
194 if (td->io_engine & FIO_SYNCIO)
202 * only really works for sequential io for now
204 if (td->zone_size && !td->sequential)
207 td->filetype = FIO_TYPE_FILE;
208 if (!stat(jobname, &sb)) {
209 if (S_ISBLK(sb.st_mode))
210 td->filetype = FIO_TYPE_BD;
211 else if (S_ISCHR(sb.st_mode))
212 td->filetype = FIO_TYPE_CHAR;
215 if (td->filetype == FIO_TYPE_FILE) {
216 if (td->directory[0] != '\0')
217 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
219 sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
221 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
223 sem_init(&td->mutex, 0, 0);
225 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
226 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
227 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
229 if (td->min_bs == -1U)
231 if (td->max_bs == -1U)
233 if (td_read(td) && !td_rw(td))
236 if (td->stonewall && td->thread_number > 1)
239 td->groupid = groupid;
245 setup_log(&td->slat_log);
246 setup_log(&td->clat_log);
249 setup_log(&td->bw_log);
251 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
254 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);
255 else if (job_add_num == 1)
259 * recurse add identical jobs, clear numjobs and stonewall options
260 * as they don't apply to sub-jobs
262 numjobs = td->numjobs;
264 struct thread_data *td_new = get_new_job(0, td);
270 td_new->stonewall = 0;
271 td_new->jobnum = numjobs;
272 job_add_num = numjobs - 1;
274 if (add_job(td_new, jobname, job_add_num))
283 int init_random_state(struct thread_data *td)
286 int fd, num_maps, blocks;
288 fd = open("/dev/urandom", O_RDONLY);
290 td_verror(td, errno);
294 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
302 srand48_r(seed, &td->bsrange_state);
303 srand48_r(seed, &td->verify_state);
311 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
312 num_maps = blocks / BLOCKS_PER_MAP;
313 td->file_map = malloc(num_maps * sizeof(long));
314 td->num_maps = num_maps;
315 memset(td->file_map, 0, num_maps * sizeof(long));
317 srand48_r(seed, &td->random_state);
321 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
323 #ifdef FIO_HAVE_CPU_AFFINITY
328 for (i = 0; i < sizeof(int) * 8; i++) {
330 CPU_SET(i, &cpumask);
335 static unsigned long get_mult(char c)
346 return 1024 * 1024 * 1024;
353 * convert string after '=' into decimal value, noting any size suffix
355 static int str_cnv(char *p, unsigned long long *val)
360 str = strchr(p, '=');
367 *val = strtoul(str, NULL, 10);
368 if (*val == ULONG_MAX && errno == ERANGE)
371 *val *= get_mult(str[len - 1]);
375 static int check_strcnv(char *p, char *name, unsigned long long *val)
377 if (strncmp(p, name, strlen(name) - 1))
380 return str_cnv(p, val);
383 static void strip_blank_front(char **p)
391 static void strip_blank_end(char *p)
393 char *s = p + strlen(p) - 1;
395 while (isspace(*s) || iscntrl(*s))
401 typedef int (str_cb_fn)(struct thread_data *, char *);
403 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
405 char *s = strstr(p, name);
415 strip_blank_front(&s);
419 static int check_strstore(char *p, char *name, char *dest)
421 char *s = strstr(p, name);
431 strip_blank_front(&s);
437 static int __check_range(char *str, unsigned long *val)
441 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
442 *val *= get_mult(suffix);
446 if (sscanf(str, "%lu", val) == 1)
452 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
460 str = strstr(p, name);
466 str = strchr(p, '=');
471 * 'p' now holds whatever is after the '=' sign
476 * terminate p1 at the '-' sign
485 if (!__check_range(p1, s) && !__check_range(p2, e))
491 static int check_int(char *p, char *name, unsigned int *val)
495 if (strncmp(p, name, strlen(name)))
498 str = strstr(p, name);
502 str = strchr(p, '=');
508 if (sscanf(str, "%u", val) == 1)
514 static int check_strset(char *p, char *name)
516 return strncmp(p, name, strlen(name));
519 static int is_empty_or_comment(char *line)
523 for (i = 0; i < strlen(line); i++) {
526 if (!isspace(line[i]) && !iscntrl(line[i]))
533 static int str_rw_cb(struct thread_data *td, char *mem)
535 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
536 td->ddir = DDIR_READ;
539 } else if (!strncmp(mem, "randread", 8)) {
540 td->ddir = DDIR_READ;
543 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
544 td->ddir = DDIR_WRITE;
547 } else if (!strncmp(mem, "randwrite", 9)) {
548 td->ddir = DDIR_WRITE;
551 } else if (!strncmp(mem, "rw", 2)) {
556 } else if (!strncmp(mem, "randrw", 6)) {
563 fprintf(stderr, "bad data direction: %s\n", mem);
567 static int str_verify_cb(struct thread_data *td, char *mem)
569 if (!strncmp(mem, "0", 1)) {
570 td->verify = VERIFY_NONE;
572 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
573 td->verify = VERIFY_MD5;
575 } else if (!strncmp(mem, "crc32", 5)) {
576 td->verify = VERIFY_CRC32;
580 fprintf(stderr, "bad verify type: %s\n", mem);
584 static int str_mem_cb(struct thread_data *td, char *mem)
586 if (!strncmp(mem, "malloc", 6)) {
587 td->mem_type = MEM_MALLOC;
589 } else if (!strncmp(mem, "shm", 3)) {
590 td->mem_type = MEM_SHM;
592 } else if (!strncmp(mem, "mmap", 4)) {
593 td->mem_type = MEM_MMAP;
597 fprintf(stderr, "bad mem type: %s\n", mem);
601 static int str_ioengine_cb(struct thread_data *td, char *str)
603 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
604 !strncmp(str, "libaio", 6)) {
605 strcpy(td->io_engine_name, "libaio");
606 td->io_engine = FIO_LIBAIO;
608 } else if (!strncmp(str, "posixaio", 8)) {
609 strcpy(td->io_engine_name, "posixaio");
610 td->io_engine = FIO_POSIXAIO;
612 } else if (!strncmp(str, "sync", 4)) {
613 strcpy(td->io_engine_name, "sync");
614 td->io_engine = FIO_SYNCIO;
616 } else if (!strncmp(str, "mmap", 4)) {
617 strcpy(td->io_engine_name, "mmap");
618 td->io_engine = FIO_MMAPIO;
620 } else if (!strncmp(str, "sgio", 4)) {
621 strcpy(td->io_engine_name, "sgio");
622 td->io_engine = FIO_SGIO;
624 } else if (!strncmp(str, "splice", 6)) {
625 strcpy(td->io_engine_name, "splice");
626 td->io_engine = FIO_SPLICEIO;
630 fprintf(stderr, "bad ioengine type: %s\n", str);
634 static int str_iolog_cb(struct thread_data *td, char *file)
636 strncpy(td->iolog_file, file, sizeof(td->iolog_file) - 1);
641 int parse_jobs_ini(char *file)
643 unsigned int prioclass, prio, cpu, global;
644 unsigned long long ull;
645 unsigned long ul1, ul2;
646 struct thread_data *td;
652 f = fopen(file, "r");
654 perror("fopen job file");
658 string = malloc(4096);
661 while ((p = fgets(string, 4096, f)) != NULL) {
662 if (is_empty_or_comment(p))
664 if (sscanf(p, "[%s]", name) != 1)
667 global = !strncmp(name, "global", 6);
669 name[strlen(name) - 1] = '\0';
671 td = get_new_job(global, &def_thread);
676 while ((p = fgets(string, 4096, f)) != NULL) {
677 if (is_empty_or_comment(p))
681 strip_blank_front(&p);
684 if (!check_int(p, "prio", &prio)) {
685 #ifndef FIO_HAVE_IOPRIO
686 fprintf(stderr, "io priorities not available\n");
693 if (!check_int(p, "prioclass", &prioclass)) {
694 #ifndef FIO_HAVE_IOPRIO
695 fprintf(stderr, "io priorities not available\n");
698 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
702 if (!check_int(p, "direct", &td->odirect)) {
706 if (!check_int(p, "rate", &td->rate)) {
710 if (!check_int(p, "ratemin", &td->ratemin)) {
714 if (!check_int(p, "ratecycle", &td->ratecycle)) {
718 if (!check_int(p, "thinktime", &td->thinktime)) {
722 if (!check_int(p, "cpumask", &cpu)) {
723 #ifndef FIO_HAVE_CPU_AFFINITY
724 fprintf(stderr, "cpu affinity not available\n");
727 fill_cpu_mask(td->cpumask, cpu);
731 if (!check_int(p, "fsync", &td->fsync_blocks)) {
736 if (!check_int(p, "startdelay", &td->start_delay)) {
740 if (!check_int(p, "timeout", &td->timeout)) {
744 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
748 if (!check_int(p, "iodepth", &td->iodepth)) {
752 if (!check_int(p, "sync", &td->sync_io)) {
756 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
760 if (!check_int(p, "create_serialize", &td->create_serialize)) {
764 if (!check_int(p, "create_fsync", &td->create_fsync)) {
768 if (!check_int(p, "end_fsync", &td->end_fsync)) {
772 if (!check_int(p, "loops", &td->loops)) {
776 if (!check_int(p, "numjobs", &td->numjobs)) {
780 if (!check_int(p, "overwrite", &td->overwrite)) {
784 if (!check_range(p, "bsrange", &ul1, &ul2)) {
795 if (!check_strcnv(p, "bs", &ull)) {
800 if (!check_strcnv(p, "size", &td->file_size)) {
804 if (!check_strcnv(p, "offset", &td->file_offset)) {
808 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
812 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
816 if (!check_strcnv(p, "lockmem", &mlock_size)) {
820 if (!check_strstore(p, "directory", td->directory)) {
824 if (!check_str(p, "mem", str_mem_cb, td)) {
828 if (!check_str(p, "verify", str_verify_cb, td)) {
832 if (!check_str(p, "rw", str_rw_cb, td)) {
836 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
840 if (!check_strset(p, "create")) {
845 if (!check_strset(p, "exitall")) {
846 exitall_on_terminate = 1;
850 if (!check_strset(p, "stonewall")) {
855 if (!check_strset(p, "thread")) {
860 if (!check_str(p, "iolog", str_iolog_cb, td)) {
866 printf("Client%d: bad option %s\n",td->thread_number,p);
871 if (add_job(td, name, 0))
881 static int fill_def_thread(void)
883 memset(&def_thread, 0, sizeof(def_thread));
885 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
886 perror("sched_getaffinity");
893 def_thread.ddir = DDIR_READ;
894 def_thread.iomix = 0;
895 def_thread.bs = DEF_BS;
896 def_thread.min_bs = -1;
897 def_thread.max_bs = -1;
898 def_thread.io_engine = DEF_IO_ENGINE;
899 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
900 def_thread.odirect = DEF_ODIRECT;
901 def_thread.ratecycle = DEF_RATE_CYCLE;
902 def_thread.sequential = DEF_SEQUENTIAL;
903 def_thread.timeout = DEF_TIMEOUT;
904 def_thread.create_file = DEF_CREATE;
905 def_thread.overwrite = DEF_OVERWRITE;
906 def_thread.invalidate_cache = DEF_INVALIDATE;
907 def_thread.sync_io = DEF_SYNCIO;
908 def_thread.mem_type = MEM_MALLOC;
909 def_thread.bw_avg_time = DEF_BWAVGTIME;
910 def_thread.create_serialize = DEF_CREATE_SER;
911 def_thread.create_fsync = DEF_CREATE_FSYNC;
912 def_thread.loops = DEF_LOOPS;
913 def_thread.verify = DEF_VERIFY;
914 def_thread.stonewall = DEF_STONEWALL;
915 def_thread.numjobs = DEF_NUMJOBS;
916 def_thread.use_thread = DEF_USE_THREAD;
917 #ifdef FIO_HAVE_DISK_UTIL
918 def_thread.do_disk_util = 1;
924 static void usage(char *name)
926 printf("%s\n", fio_version_string);
927 printf("\t-s IO is sequential\n");
928 printf("\t-b Block size in KiB for each IO\n");
929 printf("\t-t Runtime in seconds\n");
930 printf("\t-R Exit all threads on failure to meet rate goal\n");
931 printf("\t-o Use O_DIRECT\n");
932 printf("\t-l Generate per-job latency logs\n");
933 printf("\t-w Generate per-job bandwidth logs\n");
934 printf("\t-f Job file (Required)\n");
935 printf("\t-v Print version info and exit\n");
938 static void parse_cmd_line(int argc, char *argv[])
942 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
945 def_thread.sequential = !!atoi(optarg);
948 def_thread.bs = atoi(optarg);
949 def_thread.bs <<= 10;
950 if (!def_thread.bs) {
951 printf("bad block size\n");
952 def_thread.bs = DEF_BS;
956 def_thread.timeout = atoi(optarg);
959 repeatable = !!atoi(optarg);
962 rate_quit = !!atoi(optarg);
965 def_thread.odirect = !!atoi(optarg);
968 ini_file = strdup(optarg);
980 printf("%s\n", fio_version_string);
985 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
986 ini_file = strdup(argv[argc - 1]);
989 static void free_shm(void)
991 struct shmid_ds sbuf;
996 shmctl(shm_id, IPC_RMID, &sbuf);
1000 static int setup_thread_area(void)
1003 * 1024 is too much on some machines, scale max_jobs if
1004 * we get a failure that looks like too large a shm segment
1007 int s = max_jobs * sizeof(struct thread_data);
1009 shm_id = shmget(0, s, IPC_CREAT | 0600);
1012 if (errno != EINVAL) {
1023 threads = shmat(shm_id, NULL, 0);
1024 if (threads == (void *) -1) {
1033 int parse_options(int argc, char *argv[])
1035 if (setup_thread_area())
1037 if (fill_def_thread())
1040 parse_cmd_line(argc, argv);
1043 printf("Need job file\n");
1048 if (parse_jobs_ini(ini_file)) {