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;
60 static struct thread_data *get_new_job(int global, struct thread_data *parent)
62 struct thread_data *td;
66 if (thread_number >= max_jobs)
69 td = &threads[thread_number++];
74 td->thread_number = thread_number;
79 static void put_job(struct thread_data *td)
81 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
85 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
87 char *ddir_str[] = { "read", "write", "randread", "randwrite",
88 "rw", NULL, "randrw" };
92 #ifndef FIO_HAVE_LIBAIO
93 if (td->io_engine == FIO_LIBAIO) {
94 fprintf(f_err, "Linux libaio not available\n");
98 #ifndef FIO_HAVE_POSIXAIO
99 if (td->io_engine == FIO_POSIXAIO) {
100 fprintf(f_err, "posix aio not available\n");
106 * the def_thread is just for options, it's not a real job
108 if (td == &def_thread)
111 if (td->io_engine & FIO_SYNCIO)
119 * only really works for sequential io for now
121 if (td->zone_size && !td->sequential)
124 td->filetype = FIO_TYPE_FILE;
125 if (!stat(jobname, &sb)) {
126 if (S_ISBLK(sb.st_mode))
127 td->filetype = FIO_TYPE_BD;
128 else if (S_ISCHR(sb.st_mode))
129 td->filetype = FIO_TYPE_CHAR;
132 if (td->filetype == FIO_TYPE_FILE) {
135 if (td->directory && td->directory[0] != '\0')
136 sprintf(tmp, "%s/%s.%d", td->directory, jobname, td->jobnum);
138 sprintf(tmp, "%s.%d", jobname, td->jobnum);
139 td->file_name = strdup(tmp);
141 td->file_name = strdup(jobname);
143 fio_sem_init(&td->mutex, 0);
145 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
146 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
147 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
149 if (td->min_bs == -1U)
151 if (td->max_bs == -1U)
153 if (td_read(td) && !td_rw(td))
156 if (td->stonewall && td->thread_number > 1)
159 td->groupid = groupid;
165 setup_log(&td->slat_log);
166 setup_log(&td->clat_log);
169 setup_log(&td->bw_log);
171 if (td->name[0] == '\0')
172 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
174 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
177 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);
178 else if (job_add_num == 1)
179 fprintf(f_out, "...\n");
182 * recurse add identical jobs, clear numjobs and stonewall options
183 * as they don't apply to sub-jobs
185 numjobs = td->numjobs;
187 struct thread_data *td_new = get_new_job(0, td);
193 td_new->stonewall = 0;
194 td_new->jobnum = numjobs;
195 job_add_num = numjobs - 1;
197 if (add_job(td_new, jobname, job_add_num))
206 int init_random_state(struct thread_data *td)
208 unsigned long seeds[4];
209 int fd, num_maps, blocks;
211 fd = open("/dev/urandom", O_RDONLY);
213 td_verror(td, errno);
217 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
225 os_random_seed(seeds[0], &td->bsrange_state);
226 os_random_seed(seeds[1], &td->verify_state);
227 os_random_seed(seeds[2], &td->rwmix_state);
233 seeds[3] = DEF_RANDSEED;
235 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
236 num_maps = blocks / BLOCKS_PER_MAP;
237 td->file_map = malloc(num_maps * sizeof(long));
238 td->num_maps = num_maps;
239 memset(td->file_map, 0, num_maps * sizeof(long));
241 os_random_seed(seeds[3], &td->random_state);
245 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
247 #ifdef FIO_HAVE_CPU_AFFINITY
252 for (i = 0; i < sizeof(int) * 8; i++) {
254 CPU_SET(i, &cpumask);
259 static unsigned long get_mult(char c)
270 return 1024 * 1024 * 1024;
277 * convert string after '=' into decimal value, noting any size suffix
279 static int str_cnv(char *p, unsigned long long *val)
284 str = strchr(p, '=');
291 *val = strtoul(str, NULL, 10);
292 if (*val == ULONG_MAX && errno == ERANGE)
295 *val *= get_mult(str[len - 1]);
299 static int check_strcnv(char *p, char *name, unsigned long long *val)
301 if (strncmp(p, name, strlen(name) - 1))
304 return str_cnv(p, val);
307 static void strip_blank_front(char **p)
315 static void strip_blank_end(char *p)
317 char *s = p + strlen(p) - 1;
319 while (isspace(*s) || iscntrl(*s))
325 typedef int (str_cb_fn)(struct thread_data *, char *);
327 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
331 if (strncmp(p, name, strlen(name)))
343 strip_blank_front(&s);
347 static int check_strstore(char *p, char *name, char *dest)
351 if (strncmp(p, name, strlen(name)))
363 strip_blank_front(&s);
369 static int __check_range(char *str, unsigned long *val)
373 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
374 *val *= get_mult(suffix);
378 if (sscanf(str, "%lu", val) == 1)
384 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
389 if (strncmp(p, name, strlen(name)))
395 str = strstr(p, name);
401 str = strchr(p, '=');
406 * 'p' now holds whatever is after the '=' sign
411 * terminate p1 at the '-' sign
420 if (!__check_range(p1, s) && !__check_range(p2, e))
426 static int check_int(char *p, char *name, unsigned int *val)
430 if (strncmp(p, name, strlen(name)))
433 str = strstr(p, name);
437 str = strchr(p, '=');
443 if (sscanf(str, "%u", val) == 1)
449 static int check_strset(char *p, char *name)
451 return strncmp(p, name, strlen(name));
454 static int is_empty_or_comment(char *line)
458 for (i = 0; i < strlen(line); i++) {
461 if (!isspace(line[i]) && !iscntrl(line[i]))
468 static int str_rw_cb(struct thread_data *td, char *mem)
470 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
471 td->ddir = DDIR_READ;
474 } else if (!strncmp(mem, "randread", 8)) {
475 td->ddir = DDIR_READ;
478 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
479 td->ddir = DDIR_WRITE;
482 } else if (!strncmp(mem, "randwrite", 9)) {
483 td->ddir = DDIR_WRITE;
486 } else if (!strncmp(mem, "rw", 2)) {
491 } else if (!strncmp(mem, "randrw", 6)) {
498 fprintf(f_err, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
502 static int str_verify_cb(struct thread_data *td, char *mem)
504 if (!strncmp(mem, "0", 1)) {
505 td->verify = VERIFY_NONE;
507 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
508 td->verify = VERIFY_MD5;
510 } else if (!strncmp(mem, "crc32", 5)) {
511 td->verify = VERIFY_CRC32;
515 fprintf(f_err, "fio: verify types: md5, crc32\n");
519 static int str_mem_cb(struct thread_data *td, char *mem)
521 if (!strncmp(mem, "malloc", 6)) {
522 td->mem_type = MEM_MALLOC;
524 } else if (!strncmp(mem, "shm", 3)) {
525 td->mem_type = MEM_SHM;
527 } else if (!strncmp(mem, "mmap", 4)) {
528 td->mem_type = MEM_MMAP;
532 fprintf(f_err, "fio: mem type: malloc, shm, mmap\n");
536 static int str_ioengine_cb(struct thread_data *td, char *str)
538 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
539 !strncmp(str, "libaio", 6)) {
540 strcpy(td->io_engine_name, "libaio");
541 td->io_engine = FIO_LIBAIO;
543 } else if (!strncmp(str, "posixaio", 8)) {
544 strcpy(td->io_engine_name, "posixaio");
545 td->io_engine = FIO_POSIXAIO;
547 } else if (!strncmp(str, "sync", 4)) {
548 strcpy(td->io_engine_name, "sync");
549 td->io_engine = FIO_SYNCIO;
551 } else if (!strncmp(str, "mmap", 4)) {
552 strcpy(td->io_engine_name, "mmap");
553 td->io_engine = FIO_MMAPIO;
555 } else if (!strncmp(str, "sgio", 4)) {
556 strcpy(td->io_engine_name, "sgio");
557 td->io_engine = FIO_SGIO;
559 } else if (!strncmp(str, "splice", 6)) {
560 strcpy(td->io_engine_name, "splice");
561 td->io_engine = FIO_SPLICEIO;
565 fprintf(f_err, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
570 * This is our [ini] type file parser.
572 int parse_jobs_ini(char *file)
574 unsigned int prioclass, prio, cpu, global, il;
575 unsigned long long ull;
576 unsigned long ul1, ul2;
577 struct thread_data *td;
578 char *string, *name, *tmpbuf;
584 f = fopen(file, "r");
586 perror("fopen job file");
590 string = malloc(4096);
592 tmpbuf = malloc(4096);
594 while ((p = fgets(string, 4096, f)) != NULL) {
597 if (is_empty_or_comment(p))
599 if (sscanf(p, "[%s]", name) != 1)
602 global = !strncmp(name, "global", 6);
604 name[strlen(name) - 1] = '\0';
606 td = get_new_job(global, &def_thread);
613 while ((p = fgets(string, 4096, f)) != NULL) {
614 if (is_empty_or_comment(p))
618 strip_blank_front(&p);
621 if (!check_int(p, "prio", &prio)) {
622 #ifndef FIO_HAVE_IOPRIO
623 fprintf(f_err, "io priorities not available\n");
631 if (!check_int(p, "prioclass", &prioclass)) {
632 #ifndef FIO_HAVE_IOPRIO
633 fprintf(f_err, "io priorities not available\n");
637 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
642 if (!check_int(p, "direct", &il)) {
647 if (!check_int(p, "rate", &td->rate)) {
651 if (!check_int(p, "ratemin", &td->ratemin)) {
655 if (!check_int(p, "ratecycle", &td->ratecycle)) {
659 if (!check_int(p, "thinktime", &td->thinktime)) {
663 if (!check_int(p, "cpumask", &cpu)) {
664 #ifndef FIO_HAVE_CPU_AFFINITY
665 fprintf(f_err, "cpu affinity not available\n");
669 fill_cpu_mask(td->cpumask, cpu);
673 if (!check_int(p, "fsync", &td->fsync_blocks)) {
678 if (!check_int(p, "startdelay", &td->start_delay)) {
682 if (!check_int(p, "timeout", &td->timeout)) {
686 if (!check_int(p, "invalidate", &il)) {
687 td->invalidate_cache = il;
691 if (!check_int(p, "iodepth", &td->iodepth)) {
695 if (!check_int(p, "sync", &il)) {
700 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
704 if (!check_int(p, "create_serialize", &il)) {
705 td->create_serialize = il;
709 if (!check_int(p, "create_fsync", &il)) {
710 td->create_fsync = il;
714 if (!check_int(p, "end_fsync", &il)) {
719 if (!check_int(p, "loops", &td->loops)) {
723 if (!check_int(p, "numjobs", &td->numjobs)) {
727 if (!check_int(p, "overwrite", &il)) {
732 if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
736 if (!check_int(p, "rwmixread", &il)) {
743 if (!check_int(p, "rwmixwrite", &il)) {
746 td->rwmixread = 100 - il;
750 if (!check_int(p, "nice", &td->nice)) {
754 if (!check_range(p, "bsrange", &ul1, &ul2)) {
765 if (!check_strcnv(p, "bs", &ull)) {
770 if (!check_strcnv(p, "size", &td->file_size)) {
774 if (!check_strcnv(p, "offset", &td->file_offset)) {
778 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
782 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
786 if (!check_strcnv(p, "lockmem", &mlock_size)) {
790 if (!check_strstore(p, "directory", tmpbuf)) {
791 td->directory = strdup(tmpbuf);
795 if (!check_strstore(p, "name", tmpbuf)) {
796 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
800 if (!check_str(p, "mem", str_mem_cb, td)) {
804 if (!check_str(p, "verify", str_verify_cb, td)) {
808 if (!check_str(p, "rw", str_rw_cb, td)) {
812 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
816 if (!check_strset(p, "create")) {
821 if (!check_strset(p, "exitall")) {
822 exitall_on_terminate = 1;
826 if (!check_strset(p, "stonewall")) {
831 if (!check_strset(p, "thread")) {
836 if (!check_strstore(p, "iolog", tmpbuf)) {
837 if (td->write_iolog) {
838 fprintf(f_err, "fio: read iolog overrides given write_iolog\n");
839 free(td->iolog_file);
842 td->iolog_file = strdup(tmpbuf);
847 if (!check_strstore(p, "write_iolog", tmpbuf)) {
848 if (!td->read_iolog) {
849 td->iolog_file = strdup(tmpbuf);
852 fprintf(f_err, "fio: read iolog overrides given write_iolog\n");
856 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
857 td->exec_prerun = strdup(tmpbuf);
861 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
862 td->exec_postrun = strdup(tmpbuf);
866 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
867 #ifndef FIO_HAVE_IOSCHED_SWITCH
868 fprintf(f_err, "io scheduler switching not available\n");
872 td->ioscheduler = strdup(tmpbuf);
879 * Don't break here, continue parsing options so we
880 * dump all the bad ones. Makes trial/error fixups
881 * easier on the user.
883 printf("Client%d: bad option %s\n",td->thread_number,p);
889 ret = add_job(td, name, 0);
902 static int fill_def_thread(void)
904 memset(&def_thread, 0, sizeof(def_thread));
906 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
907 perror("sched_getaffinity");
914 def_thread.ddir = DDIR_READ;
915 def_thread.iomix = 0;
916 def_thread.bs = DEF_BS;
917 def_thread.min_bs = -1;
918 def_thread.max_bs = -1;
919 def_thread.io_engine = DEF_IO_ENGINE;
920 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
921 def_thread.odirect = DEF_ODIRECT;
922 def_thread.ratecycle = DEF_RATE_CYCLE;
923 def_thread.sequential = DEF_SEQUENTIAL;
924 def_thread.timeout = DEF_TIMEOUT;
925 def_thread.create_file = DEF_CREATE;
926 def_thread.overwrite = DEF_OVERWRITE;
927 def_thread.invalidate_cache = DEF_INVALIDATE;
928 def_thread.sync_io = DEF_SYNCIO;
929 def_thread.mem_type = MEM_MALLOC;
930 def_thread.bw_avg_time = DEF_BWAVGTIME;
931 def_thread.create_serialize = DEF_CREATE_SER;
932 def_thread.create_fsync = DEF_CREATE_FSYNC;
933 def_thread.loops = DEF_LOOPS;
934 def_thread.verify = DEF_VERIFY;
935 def_thread.stonewall = DEF_STONEWALL;
936 def_thread.numjobs = DEF_NUMJOBS;
937 def_thread.use_thread = DEF_USE_THREAD;
938 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
939 def_thread.rwmixread = DEF_RWMIX_READ;
940 def_thread.nice = DEF_NICE;
941 #ifdef FIO_HAVE_DISK_UTIL
942 def_thread.do_disk_util = 1;
948 static void usage(char *name)
950 printf("%s\n", fio_version_string);
951 printf("\t-s IO is sequential\n");
952 printf("\t-b Block size in KiB for each IO\n");
953 printf("\t-t Runtime in seconds\n");
954 printf("\t-R Exit all threads on failure to meet rate goal\n");
955 printf("\t-o Use O_DIRECT\n");
956 printf("\t-l Generate per-job latency logs\n");
957 printf("\t-w Generate per-job bandwidth logs\n");
958 printf("\t-f Job file (Required)\n");
959 printf("\t-v Print version info and exit\n");
962 static void parse_cmd_line(int argc, char *argv[])
966 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvhO:")) != EOF) {
969 def_thread.sequential = !!atoi(optarg);
972 def_thread.bs = atoi(optarg);
973 def_thread.bs <<= 10;
974 if (!def_thread.bs) {
975 printf("bad block size\n");
976 def_thread.bs = DEF_BS;
980 def_thread.timeout = atoi(optarg);
983 repeatable = !!atoi(optarg);
986 rate_quit = !!atoi(optarg);
989 def_thread.odirect = !!atoi(optarg);
992 ini_file = strdup(optarg);
1001 f_out = fopen(optarg, "w+");
1003 perror("fopen output");
1012 printf("%s\n", fio_version_string);
1017 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1018 ini_file = strdup(argv[argc - 1]);
1025 static void free_shm(void)
1027 struct shmid_ds sbuf;
1030 shmdt((void *) threads);
1032 shmctl(shm_id, IPC_RMID, &sbuf);
1036 static int setup_thread_area(void)
1039 * 1024 is too much on some machines, scale max_jobs if
1040 * we get a failure that looks like too large a shm segment
1043 int s = max_jobs * sizeof(struct thread_data);
1045 shm_id = shmget(0, s, IPC_CREAT | 0600);
1048 if (errno != EINVAL) {
1059 threads = shmat(shm_id, NULL, 0);
1060 if (threads == (void *) -1) {
1069 int parse_options(int argc, char *argv[])
1071 if (setup_thread_area())
1073 if (fill_def_thread())
1076 parse_cmd_line(argc, argv);
1079 fprintf(f_err, "Need job file\n");
1084 if (parse_jobs_ini(ini_file))