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)
38 static char fio_version_string[] = "fio 1.1";
40 static int repeatable = DEF_RAND_REPEAT;
41 static char *ini_file;
42 static int max_jobs = MAX_JOBS;
44 struct thread_data def_thread;
45 struct thread_data *threads = NULL;
48 int write_lat_log = 0;
50 int exitall_on_terminate = 0;
52 static int setup_rate(struct thread_data *td)
59 if (td->rate < td->ratemin) {
60 fprintf(stderr, "min rate larger than nominal rate\n");
64 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
65 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
66 td->rate_pending_usleep = 0;
70 static void setup_log(struct io_log **log)
72 struct io_log *l = malloc(sizeof(*l));
75 l->max_samples = 1024;
76 l->log = malloc(l->max_samples * sizeof(struct io_sample));
80 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
86 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
87 f = fopen(file_name, "w");
93 for (i = 0; i < log->nr_samples; i++)
94 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
101 static struct thread_data *get_new_job(int global, struct thread_data *parent)
103 struct thread_data *td;
107 if (thread_number >= max_jobs)
110 td = &threads[thread_number++];
111 memset(td, 0, sizeof(*td));
114 td->thread_number = thread_number;
116 td->ddir = parent->ddir;
117 td->ioprio = parent->ioprio;
118 td->sequential = parent->sequential;
120 td->min_bs = parent->min_bs;
121 td->max_bs = parent->max_bs;
122 td->odirect = parent->odirect;
123 td->thinktime = parent->thinktime;
124 td->fsync_blocks = parent->fsync_blocks;
125 td->start_delay = parent->start_delay;
126 td->timeout = parent->timeout;
127 td->io_engine = parent->io_engine;
128 td->create_file = parent->create_file;
129 td->overwrite = parent->overwrite;
130 td->invalidate_cache = parent->invalidate_cache;
131 td->file_size = parent->file_size;
132 td->file_offset = parent->file_offset;
133 td->rate = parent->rate;
134 td->ratemin = parent->ratemin;
135 td->ratecycle = parent->ratecycle;
136 td->iodepth = parent->iodepth;
137 td->sync_io = parent->sync_io;
138 td->mem_type = parent->mem_type;
139 td->bw_avg_time = parent->bw_avg_time;
140 td->create_serialize = parent->create_serialize;
141 td->create_fsync = parent->create_fsync;
142 td->loops = parent->loops;
143 td->verify = parent->verify;
144 td->stonewall = parent->stonewall;
145 td->numjobs = parent->numjobs;
146 td->use_thread = parent->use_thread;
147 td->do_disk_util = parent->do_disk_util;
148 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
149 strcpy(td->io_engine_name, parent->io_engine_name);
154 static void put_job(struct thread_data *td)
156 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
160 static int add_job(struct thread_data *td, const char *jobname, int prioclass,
163 char *ddir_str[] = { "read", "write", "randread", "randwrite" };
167 #ifndef FIO_HAVE_LIBAIO
168 if (td->io_engine == FIO_LIBAIO) {
169 fprintf(stderr, "Linux libaio not available\n");
173 #ifndef FIO_HAVE_POSIXAIO
174 if (td->io_engine == FIO_POSIXAIO) {
175 fprintf(stderr, "posix aio not available\n");
179 #ifdef FIO_HAVE_IOPRIO
180 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
184 * the def_thread is just for options, it's not a real job
186 if (td == &def_thread)
189 if (td->io_engine & FIO_SYNCIO)
196 td->filetype = FIO_TYPE_FILE;
197 if (!stat(jobname, &sb) && S_ISBLK(sb.st_mode))
198 td->filetype = FIO_TYPE_BD;
200 if (td->filetype == FIO_TYPE_FILE) {
201 if (td->directory[0] != '\0')
202 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->thread_number);
204 sprintf(td->file_name, "%s.%d", jobname, td->thread_number);
206 strcpy(td->file_name, jobname);
208 sem_init(&td->mutex, 0, 0);
210 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
211 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
212 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
214 if (td->min_bs == -1U)
216 if (td->max_bs == -1U)
221 if (td->stonewall && td->thread_number > 1)
224 td->groupid = groupid;
230 setup_log(&td->slat_log);
231 setup_log(&td->clat_log);
234 setup_log(&td->bw_log);
236 ddir = td->ddir + (!td->sequential << 1);
237 printf("Client%d (g=%d): rw=%s, prio=%d/%d, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->thread_number, td->groupid, ddir_str[ddir], prioclass, prio, td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
240 * recurse add identical jobs, clear numjobs and stonewall options
241 * as they don't apply to sub-jobs
243 numjobs = td->numjobs;
245 struct thread_data *td_new = get_new_job(0, td);
251 td_new->stonewall = 0;
253 if (add_job(td_new, jobname, prioclass, prio))
262 int init_random_state(struct thread_data *td)
265 int fd, num_maps, blocks;
267 fd = open("/dev/random", O_RDONLY);
269 td_verror(td, errno);
273 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
281 srand48_r(seed, &td->bsrange_state);
282 srand48_r(seed, &td->verify_state);
290 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
291 num_maps = blocks / BLOCKS_PER_MAP;
292 td->file_map = malloc(num_maps * sizeof(long));
293 td->num_maps = num_maps;
294 memset(td->file_map, 0, num_maps * sizeof(long));
296 srand48_r(seed, &td->random_state);
300 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
302 #ifdef FIO_HAVE_CPU_AFFINITY
307 for (i = 0; i < sizeof(int) * 8; i++) {
309 CPU_SET(i, &cpumask);
314 static unsigned long get_mult(char c)
325 return 1024 * 1024 * 1024;
332 * convert string after '=' into decimal value, noting any size suffix
334 static int str_cnv(char *p, unsigned long long *val)
339 str = strstr(p, "=");
346 *val = strtoul(str, NULL, 10);
347 if (*val == ULONG_MAX && errno == ERANGE)
350 *val *= get_mult(str[len - 2]);
354 static int check_strcnv(char *p, char *name, unsigned long long *val)
356 if (!strstr(p, name))
359 return str_cnv(p, val);
362 static void strip_blank_front(char **p)
370 static void strip_blank_end(char *p)
372 while (isblank(*p)) {
378 typedef int (str_cb_fn)(struct thread_data *, char *);
380 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
382 char *s = strstr(p, name);
392 strip_blank_front(&s);
396 static int check_strstore(char *p, char *name, char *dest)
398 char *s = strstr(p, name);
408 strip_blank_front(&s);
412 s = dest + strlen(dest) - 1;
417 static int __check_range(char *str, unsigned long *val)
421 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
422 *val *= get_mult(suffix);
426 if (sscanf(str, "%lu", val) == 1)
432 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
440 str = strstr(p, name);
446 str = strchr(p, '=');
451 * 'p' now holds whatever is after the '=' sign
456 * terminate p1 at the '-' sign
465 if (!__check_range(p1, s) && !__check_range(p2, e))
471 static int check_int(char *p, char *name, unsigned int *val)
475 sprintf(str, "%s=%%d", name);
476 if (sscanf(p, str, val) == 1)
479 sprintf(str, "%s = %%d", name);
480 if (sscanf(p, str, val) == 1)
486 static int check_strset(char *p, char *name)
488 return strncmp(p, name, strlen(name));
491 static int is_empty_or_comment(char *line)
495 for (i = 0; i < strlen(line); i++) {
498 if (!isspace(line[i]) && !iscntrl(line[i]))
505 static int str_rw_cb(struct thread_data *td, char *mem)
507 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
508 td->ddir = DDIR_READ;
511 } else if (!strncmp(mem, "randread", 8)) {
512 td->ddir = DDIR_READ;
515 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
516 td->ddir = DDIR_WRITE;
519 } else if (!strncmp(mem, "randwrite", 9)) {
520 td->ddir = DDIR_WRITE;
525 fprintf(stderr, "bad data direction: %s\n", mem);
529 static int str_verify_cb(struct thread_data *td, char *mem)
531 if (!strncmp(mem, "0", 1)) {
532 td->verify = VERIFY_NONE;
534 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
535 td->verify = VERIFY_MD5;
537 } else if (!strncmp(mem, "crc32", 5)) {
538 td->verify = VERIFY_CRC32;
542 fprintf(stderr, "bad verify type: %s\n", mem);
546 static int str_mem_cb(struct thread_data *td, char *mem)
548 if (!strncmp(mem, "malloc", 6)) {
549 td->mem_type = MEM_MALLOC;
551 } else if (!strncmp(mem, "shm", 3)) {
552 td->mem_type = MEM_SHM;
554 } else if (!strncmp(mem, "mmap", 4)) {
555 td->mem_type = MEM_MMAP;
559 fprintf(stderr, "bad mem type: %s\n", mem);
563 static int str_ioengine_cb(struct thread_data *td, char *str)
565 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
566 !strncmp(str, "libaio", 6)) {
567 strcpy(td->io_engine_name, "libaio");
568 td->io_engine = FIO_LIBAIO;
570 } else if (!strncmp(str, "posixaio", 8)) {
571 strcpy(td->io_engine_name, "posixaio");
572 td->io_engine = FIO_POSIXAIO;
574 } else if (!strncmp(str, "sync", 4)) {
575 strcpy(td->io_engine_name, "sync");
576 td->io_engine = FIO_SYNCIO;
578 } else if (!strncmp(str, "mmap", 4)) {
579 strcpy(td->io_engine_name, "mmap");
580 td->io_engine = FIO_MMAPIO;
582 } else if (!strncmp(str, "sgio", 4)) {
583 strcpy(td->io_engine_name, "sgio");
584 td->io_engine = FIO_SGIO;
588 fprintf(stderr, "bad ioengine type: %s\n", str);
593 int parse_jobs_ini(char *file)
595 unsigned int prioclass, prio, cpu, global;
596 unsigned long long ull;
597 unsigned long ul1, ul2;
598 struct thread_data *td;
604 f = fopen(file, "r");
610 string = malloc(4096);
613 while ((p = fgets(string, 4096, f)) != NULL) {
614 if (is_empty_or_comment(p))
616 if (sscanf(p, "[%s]", name) != 1)
619 global = !strncmp(name, "global", 6);
621 name[strlen(name) - 1] = '\0';
623 td = get_new_job(global, &def_thread);
631 while ((p = fgets(string, 4096, f)) != NULL) {
632 if (is_empty_or_comment(p))
636 if (!check_int(p, "prio", &prio)) {
637 #ifndef FIO_HAVE_IOPRIO
638 fprintf(stderr, "io priorities not available\n");
644 if (!check_int(p, "prioclass", &prioclass)) {
645 #ifndef FIO_HAVE_IOPRIO
646 fprintf(stderr, "io priorities not available\n");
652 if (!check_int(p, "direct", &td->odirect)) {
656 if (!check_int(p, "rate", &td->rate)) {
660 if (!check_int(p, "ratemin", &td->ratemin)) {
664 if (!check_int(p, "ratecycle", &td->ratecycle)) {
668 if (!check_int(p, "thinktime", &td->thinktime)) {
672 if (!check_int(p, "cpumask", &cpu)) {
673 #ifndef FIO_HAVE_CPU_AFFINITY
674 fprintf(stderr, "cpu affinity not available\n");
677 fill_cpu_mask(td->cpumask, cpu);
681 if (!check_int(p, "fsync", &td->fsync_blocks)) {
685 if (!check_int(p, "startdelay", &td->start_delay)) {
689 if (!check_int(p, "timeout", &td->timeout)) {
693 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
697 if (!check_int(p, "iodepth", &td->iodepth)) {
701 if (!check_int(p, "sync", &td->sync_io)) {
705 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
709 if (!check_int(p, "create_serialize", &td->create_serialize)) {
713 if (!check_int(p, "create_fsync", &td->create_fsync)) {
717 if (!check_int(p, "loops", &td->loops)) {
721 if (!check_int(p, "numjobs", &td->numjobs)) {
725 if (!check_int(p, "overwrite", &td->overwrite)) {
729 if (!check_range(p, "bsrange", &ul1, &ul2)) {
740 if (!check_strcnv(p, "bs", &ull)) {
745 if (!check_strcnv(p, "size", &td->file_size)) {
749 if (!check_strcnv(p, "offset", &td->file_offset)) {
753 if (!check_strstore(p, "directory", td->directory)) {
757 if (!check_str(p, "mem", str_mem_cb, td)) {
761 if (!check_str(p, "verify", str_verify_cb, td)) {
765 if (!check_str(p, "rw", str_rw_cb, td)) {
769 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
773 if (!check_strset(p, "create")) {
778 if (!check_strset(p, "exitall")) {
779 exitall_on_terminate = 1;
783 if (!check_strset(p, "stonewall")) {
788 if (!check_strset(p, "thread")) {
794 printf("Client%d: bad option %s\n",td->thread_number,p);
798 if (add_job(td, name, prioclass, prio))
808 static int fill_def_thread(void)
810 memset(&def_thread, 0, sizeof(def_thread));
812 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
813 perror("sched_getaffinity");
820 def_thread.ddir = DDIR_READ;
821 def_thread.bs = DEF_BS;
822 def_thread.min_bs = -1;
823 def_thread.max_bs = -1;
824 def_thread.io_engine = DEF_IO_ENGINE;
825 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
826 def_thread.odirect = DEF_ODIRECT;
827 def_thread.ratecycle = DEF_RATE_CYCLE;
828 def_thread.sequential = DEF_SEQUENTIAL;
829 def_thread.timeout = DEF_TIMEOUT;
830 def_thread.create_file = DEF_CREATE;
831 def_thread.overwrite = DEF_OVERWRITE;
832 def_thread.invalidate_cache = DEF_INVALIDATE;
833 def_thread.sync_io = DEF_SYNCIO;
834 def_thread.mem_type = MEM_MALLOC;
835 def_thread.bw_avg_time = DEF_BWAVGTIME;
836 def_thread.create_serialize = DEF_CREATE_SER;
837 def_thread.create_fsync = DEF_CREATE_FSYNC;
838 def_thread.loops = DEF_LOOPS;
839 def_thread.verify = DEF_VERIFY;
840 def_thread.stonewall = DEF_STONEWALL;
841 def_thread.numjobs = DEF_NUMJOBS;
842 def_thread.use_thread = DEF_USE_THREAD;
843 #ifdef FIO_HAVE_DISK_UTIL
844 def_thread.do_disk_util = 1;
850 static void parse_cmd_line(int argc, char *argv[])
854 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwv")) != EOF) {
857 def_thread.sequential = !!atoi(optarg);
860 def_thread.bs = atoi(optarg);
861 def_thread.bs <<= 10;
862 if (!def_thread.bs) {
863 printf("bad block size\n");
864 def_thread.bs = DEF_BS;
868 def_thread.timeout = atoi(optarg);
871 repeatable = !!atoi(optarg);
874 rate_quit = !!atoi(optarg);
877 def_thread.odirect = !!atoi(optarg);
880 ini_file = strdup(optarg);
889 printf("%s\n", fio_version_string);
895 static void free_shm(void)
897 struct shmid_ds sbuf;
902 shmctl(shm_id, IPC_RMID, &sbuf);
906 static int setup_thread_area(void)
909 * 1024 is too much on some machines, scale max_jobs if
910 * we get a failure that looks like too large a shm segment
913 int s = max_jobs * sizeof(struct thread_data);
915 shm_id = shmget(0, s, IPC_CREAT | 0600);
918 if (errno != EINVAL) {
929 threads = shmat(shm_id, NULL, 0);
930 if (threads == (void *) -1) {
939 int parse_options(int argc, char *argv[])
941 if (setup_thread_area())
943 if (fill_def_thread())
946 parse_cmd_line(argc, argv);
949 printf("Need job file\n");
953 if (parse_jobs_ini(ini_file))