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_SEQUENTIAL (1)
21 #define DEF_RAND_REPEAT (1)
22 #define DEF_OVERWRITE (1)
23 #define DEF_CREATE (1)
24 #define DEF_INVALIDATE (1)
25 #define DEF_SYNCIO (0)
26 #define DEF_RANDSEED (0xb1899bedUL)
27 #define DEF_BWAVGTIME (500)
28 #define DEF_CREATE_SER (1)
29 #define DEF_CREATE_FSYNC (1)
31 #define DEF_VERIFY (0)
32 #define DEF_STONEWALL (0)
33 #define DEF_NUMJOBS (1)
34 #define DEF_USE_THREAD (0)
35 #define DEF_USE_MMAP (0)
36 #define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
38 static int repeatable = DEF_RAND_REPEAT;
39 static char *ini_file;
40 static int max_jobs = MAX_JOBS;
42 struct thread_data def_thread;
43 struct thread_data *threads = NULL;
46 int write_lat_log = 0;
48 int exitall_on_terminate = 0;
50 static int setup_rate(struct thread_data *td)
57 if (td->rate < td->ratemin) {
58 fprintf(stderr, "min rate larger than nominal rate\n");
62 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
63 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
64 td->rate_pending_usleep = 0;
68 static void setup_log(struct io_log **log)
70 struct io_log *l = malloc(sizeof(*l));
73 l->max_samples = 1024;
74 l->log = malloc(l->max_samples * sizeof(struct io_sample));
78 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
84 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
85 f = fopen(file_name, "w");
91 for (i = 0; i < log->nr_samples; i++)
92 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
99 static struct thread_data *get_new_job(int global, struct thread_data *parent)
101 struct thread_data *td;
105 if (thread_number >= max_jobs)
108 td = &threads[thread_number++];
109 memset(td, 0, sizeof(*td));
112 td->thread_number = thread_number;
114 td->ddir = parent->ddir;
115 td->ioprio = parent->ioprio;
116 td->sequential = parent->sequential;
118 td->min_bs = parent->min_bs;
119 td->max_bs = parent->max_bs;
120 td->odirect = parent->odirect;
121 td->thinktime = parent->thinktime;
122 td->fsync_blocks = parent->fsync_blocks;
123 td->start_delay = parent->start_delay;
124 td->timeout = parent->timeout;
125 td->io_engine = parent->io_engine;
126 td->create_file = parent->create_file;
127 td->overwrite = parent->overwrite;
128 td->invalidate_cache = parent->invalidate_cache;
129 td->file_size = parent->file_size;
130 td->file_offset = parent->file_offset;
131 td->rate = parent->rate;
132 td->ratemin = parent->ratemin;
133 td->ratecycle = parent->ratecycle;
134 td->aio_depth = parent->aio_depth;
135 td->sync_io = parent->sync_io;
136 td->mem_type = parent->mem_type;
137 td->bw_avg_time = parent->bw_avg_time;
138 td->create_serialize = parent->create_serialize;
139 td->create_fsync = parent->create_fsync;
140 td->loops = parent->loops;
141 td->verify = parent->verify;
142 td->stonewall = parent->stonewall;
143 td->numjobs = parent->numjobs;
144 td->use_thread = parent->use_thread;
145 td->use_mmap = parent->use_mmap;
146 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
151 static void put_job(struct thread_data *td)
153 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
157 static int add_job(struct thread_data *td, const char *jobname, int prioclass,
163 if (td == &def_thread)
166 #ifndef FIO_HAVE_LIBAIO
167 if (td->io_engine == FIO_LIBAIO) {
168 fprintf(stderr, "Linux libaio not available\n");
172 #ifndef FIO_HAVE_POSIXAIO
173 if (td->io_engine == FIO_POSIXAIO) {
174 fprintf(stderr, "posix aio not available\n");
179 td->filetype = FIO_TYPE_FILE;
180 if (!stat(jobname, &sb) && S_ISBLK(sb.st_mode))
181 td->filetype = FIO_TYPE_BD;
183 if (td->filetype == FIO_TYPE_FILE) {
184 if (td->directory[0] != '\0')
185 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->thread_number);
187 sprintf(td->file_name, "%s.%d", jobname, td->thread_number);
189 strcpy(td->file_name, jobname);
191 sem_init(&td->mutex, 1, 0);
192 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
194 td->clat_stat.min_val = ULONG_MAX;
195 td->slat_stat.min_val = ULONG_MAX;
196 td->bw_stat.min_val = ULONG_MAX;
198 run_str[td->thread_number - 1] = 'P';
200 if (td->io_engine != FIO_SYNCIO) {
207 if (td->min_bs == -1U)
209 if (td->max_bs == -1U)
214 if (td->stonewall && td->thread_number > 1)
217 td->groupid = groupid;
223 setup_log(&td->lat_log);
225 setup_log(&td->bw_log);
227 printf("Client%d (g=%d): rw=%d, prio=%d/%d, seq=%d, odir=%d, mmap=%d, bs=%d-%d, rate=%d, ioengine=%d, aio_depth=%d\n", td->thread_number, td->groupid, td->ddir, prioclass, prio, td->sequential, td->odirect, td->use_mmap, td->min_bs, td->max_bs, td->rate, td->io_engine, td->aio_depth);
230 * recurse add identical jobs, clear numjobs and stonewall options
231 * as they don't apply to sub-jobs
233 numjobs = td->numjobs;
235 struct thread_data *td_new = get_new_job(0, td);
241 td_new->stonewall = 0;
243 if (add_job(td_new, jobname, prioclass, prio))
252 int init_random_state(struct thread_data *td)
255 int fd, num_maps, blocks;
257 fd = open("/dev/random", O_RDONLY);
259 td_verror(td, errno);
263 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
271 srand48_r(seed, &td->bsrange_state);
272 srand48_r(seed, &td->verify_state);
280 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
281 num_maps = blocks / BLOCKS_PER_MAP;
282 td->file_map = malloc(num_maps * sizeof(long));
283 td->num_maps = num_maps;
284 memset(td->file_map, 0, num_maps * sizeof(long));
286 srand48_r(seed, &td->random_state);
290 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
292 #ifdef FIO_HAVE_CPU_AFFINITY
297 for (i = 0; i < sizeof(int) * 8; i++) {
299 CPU_SET(i, &cpumask);
304 static unsigned long get_mult(char c)
315 return 1024 * 1024 * 1024;
322 * convert string after '=' into decimal value, noting any size suffix
324 static int str_cnv(char *p, unsigned long long *val)
329 str = strstr(p, "=");
336 *val = strtoul(str, NULL, 10);
337 if (*val == ULONG_MAX && errno == ERANGE)
340 *val *= get_mult(str[len - 2]);
344 static int check_strcnv(char *p, char *name, unsigned long long *val)
346 if (!strstr(p, name))
349 return str_cnv(p, val);
352 static void strip_blank_front(char **p)
360 static void strip_blank_end(char *p)
362 while (isblank(*p)) {
368 typedef int (str_cb_fn)(struct thread_data *, char *);
370 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
372 char *s = strstr(p, name);
382 strip_blank_front(&s);
386 static int check_strstore(char *p, char *name, char *dest)
388 char *s = strstr(p, name);
398 strip_blank_front(&s);
402 s = dest + strlen(dest) - 1;
407 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
412 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
413 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
419 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
420 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
426 sprintf(str, "%s=%%lu-%%lu", name);
427 if (sscanf(p, str, s, e) == 2)
430 sprintf(str, "%s = %%lu-%%lu", name);
431 if (sscanf(p, str, s, e) == 2)
438 static int check_int(char *p, char *name, unsigned int *val)
442 sprintf(str, "%s=%%d", name);
443 if (sscanf(p, str, val) == 1)
446 sprintf(str, "%s = %%d", name);
447 if (sscanf(p, str, val) == 1)
453 static int check_strset(char *p, char *name)
455 return strncmp(p, name, strlen(name));
458 static int is_empty_or_comment(char *line)
462 for (i = 0; i < strlen(line); i++) {
465 if (!isspace(line[i]) && !iscntrl(line[i]))
472 static int str_rw_cb(struct thread_data *td, char *mem)
474 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
475 td->ddir = DDIR_READ;
478 } else if (!strncmp(mem, "randread", 8)) {
479 td->ddir = DDIR_READ;
482 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
483 td->ddir = DDIR_WRITE;
486 } else if (!strncmp(mem, "randwrite", 9)) {
487 td->ddir = DDIR_WRITE;
492 fprintf(stderr, "bad data direction: %s\n", mem);
496 static int str_verify_cb(struct thread_data *td, char *mem)
498 if (!strncmp(mem, "0", 1)) {
499 td->verify = VERIFY_NONE;
501 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
502 td->verify = VERIFY_MD5;
504 } else if (!strncmp(mem, "crc32", 5)) {
505 td->verify = VERIFY_CRC32;
509 fprintf(stderr, "bad verify type: %s\n", mem);
513 static int str_mem_cb(struct thread_data *td, char *mem)
515 if (!strncmp(mem, "malloc", 6)) {
516 td->mem_type = MEM_MALLOC;
518 } else if (!strncmp(mem, "shm", 3)) {
519 td->mem_type = MEM_SHM;
521 } else if (!strncmp(mem, "mmap", 4)) {
522 td->mem_type = MEM_MMAP;
526 fprintf(stderr, "bad mem type: %s\n", mem);
530 static int str_ioengine_cb(struct thread_data *td, char *str)
532 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3)) {
533 td->io_engine = FIO_LIBAIO;
535 } else if (!strncmp(str, "posixaio", 8)) {
536 td->io_engine = FIO_POSIXAIO;
538 } else if (!strncmp(str, "sync", 4)) {
539 td->io_engine = FIO_SYNCIO;
543 fprintf(stderr, "bad ioengine type: %s\n", str);
548 int parse_jobs_ini(char *file)
550 unsigned int prioclass, prio, cpu, global;
551 unsigned long long ull;
552 unsigned long ul1, ul2;
553 struct thread_data *td;
559 f = fopen(file, "r");
565 string = malloc(4096);
568 while ((p = fgets(string, 4096, f)) != NULL) {
569 if (is_empty_or_comment(p))
571 if (sscanf(p, "[%s]", name) != 1)
574 global = !strncmp(name, "global", 6);
576 name[strlen(name) - 1] = '\0';
578 td = get_new_job(global, &def_thread);
586 while ((p = fgets(string, 4096, f)) != NULL) {
587 if (is_empty_or_comment(p))
591 if (!check_int(p, "prio", &prio)) {
595 if (!check_int(p, "prioclass", &prioclass)) {
599 if (!check_int(p, "direct", &td->odirect)) {
603 if (!check_int(p, "rate", &td->rate)) {
607 if (!check_int(p, "ratemin", &td->ratemin)) {
611 if (!check_int(p, "ratecycle", &td->ratecycle)) {
615 if (!check_int(p, "thinktime", &td->thinktime)) {
619 if (!check_int(p, "cpumask", &cpu)) {
620 #ifndef FIO_HAVE_CPU_AFFINITY
621 fprintf(stderr, "cpu affinity not available\n");
624 fill_cpu_mask(td->cpumask, cpu);
628 if (!check_int(p, "fsync", &td->fsync_blocks)) {
632 if (!check_int(p, "startdelay", &td->start_delay)) {
636 if (!check_int(p, "timeout", &td->timeout)) {
640 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
644 if (!check_int(p, "aio_depth", &td->aio_depth)) {
648 if (!check_int(p, "sync", &td->sync_io)) {
652 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
656 if (!check_int(p, "create_serialize", &td->create_serialize)) {
660 if (!check_int(p, "create_fsync", &td->create_fsync)) {
664 if (!check_int(p, "loops", &td->loops)) {
668 if (!check_int(p, "numjobs", &td->numjobs)) {
672 if (!check_int(p, "mmap", &td->use_mmap)) {
676 if (!check_int(p, "overwrite", &td->overwrite)) {
680 if (!check_range(p, "bsrange", &ul1, &ul2)) {
682 printf("bad min block size, must be a multiple of 512\n");
686 printf("bad max block size, must be a multiple of 512\n");
692 if (!check_strcnv(p, "bs", &ull)) {
694 printf("bad block size, must be a multiple of 512\n");
700 if (!check_strcnv(p, "size", &td->file_size)) {
704 if (!check_strcnv(p, "offset", &td->file_offset)) {
708 if (!check_strstore(p, "directory", td->directory)) {
712 if (!check_str(p, "mem", str_mem_cb, td)) {
716 if (!check_str(p, "verify", str_verify_cb, td)) {
720 if (!check_str(p, "rw", str_rw_cb, td)) {
724 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
728 if (!check_strset(p, "create")) {
733 if (!check_strset(p, "exitall")) {
734 exitall_on_terminate = 1;
738 if (!check_strset(p, "stonewall")) {
743 if (!check_strset(p, "thread")) {
749 printf("Client%d: bad option %s\n",td->thread_number,p);
753 if (add_job(td, name, prioclass, prio))
763 static int fill_def_thread(void)
765 memset(&def_thread, 0, sizeof(def_thread));
767 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
768 perror("sched_getaffinity");
775 def_thread.ddir = DDIR_READ;
776 def_thread.bs = DEF_BS;
777 def_thread.min_bs = -1;
778 def_thread.max_bs = -1;
779 def_thread.io_engine = DEF_IO_ENGINE;
780 def_thread.odirect = DEF_ODIRECT;
781 def_thread.ratecycle = DEF_RATE_CYCLE;
782 def_thread.sequential = DEF_SEQUENTIAL;
783 def_thread.timeout = DEF_TIMEOUT;
784 def_thread.create_file = DEF_CREATE;
785 def_thread.overwrite = DEF_OVERWRITE;
786 def_thread.invalidate_cache = DEF_INVALIDATE;
787 def_thread.sync_io = DEF_SYNCIO;
788 def_thread.mem_type = MEM_MALLOC;
789 def_thread.bw_avg_time = DEF_BWAVGTIME;
790 def_thread.create_serialize = DEF_CREATE_SER;
791 def_thread.create_fsync = DEF_CREATE_FSYNC;
792 def_thread.loops = DEF_LOOPS;
793 def_thread.verify = DEF_VERIFY;
794 def_thread.stonewall = DEF_STONEWALL;
795 def_thread.numjobs = DEF_NUMJOBS;
796 def_thread.use_thread = DEF_USE_THREAD;
797 def_thread.use_mmap = DEF_USE_MMAP;
798 #ifdef FIO_HAVE_DISK_UTIL
799 def_thread.do_disk_util = 1;
805 static void parse_cmd_line(int argc, char *argv[])
809 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lw")) != EOF) {
812 def_thread.sequential = !!atoi(optarg);
815 def_thread.bs = atoi(optarg);
816 def_thread.bs <<= 10;
817 if (!def_thread.bs) {
818 printf("bad block size\n");
819 def_thread.bs = DEF_BS;
823 def_thread.timeout = atoi(optarg);
826 repeatable = !!atoi(optarg);
829 rate_quit = !!atoi(optarg);
832 def_thread.odirect = !!atoi(optarg);
835 ini_file = strdup(optarg);
847 static void free_shm(void)
849 struct shmid_ds sbuf;
854 shmctl(shm_id, IPC_RMID, &sbuf);
858 static int setup_thread_area(void)
861 * 1024 is too much on some machines, scale max_jobs if
862 * we get a failure that looks like too large a shm segment
865 int s = max_jobs * sizeof(struct thread_data);
867 shm_id = shmget(0, s, IPC_CREAT | 0600);
870 if (errno != EINVAL) {
881 threads = shmat(shm_id, NULL, 0);
882 if (threads == (void *) -1) {
891 int parse_options(int argc, char *argv[])
893 if (setup_thread_area())
895 if (fill_def_thread())
898 parse_cmd_line(argc, argv);
901 printf("Need job file\n");
905 if (parse_jobs_ini(ini_file))