2 * fio - the flexible io tester
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <sys/types.h>
38 #include <semaphore.h>
41 #include <asm/unistd.h>
45 #define MAX_JOBS (1024)
48 * assume we don't have _get either, if _set isn't defined
50 #ifndef __NR_ioprio_set
52 #define __NR_ioprio_set 289
53 #define __NR_ioprio_get 290
54 #elif defined(__powerpc__) || defined(__powerpc64__)
55 #define __NR_ioprio_set 273
56 #define __NR_ioprio_get 274
57 #elif defined(__x86_64__)
58 #define __NR_ioprio_set 251
59 #define __NR_ioprio_get 252
60 #elif defined(__ia64__)
61 #define __NR_ioprio_set 1274
62 #define __NR_ioprio_get 1275
63 #elif defined(__alpha__)
64 #define __NR_ioprio_set 442
65 #define __NR_ioprio_get 443
66 #elif defined(__s390x__) || defined(__s390__)
67 #define __NR_ioprio_set 282
68 #define __NR_ioprio_get 283
70 #error "Unsupported arch"
74 #ifndef __NR_fadvise64
76 #define __NR_fadvise64 250
77 #elif defined(__powerpc__) || defined(__powerpc64__)
78 #define __NR_fadvise64 233
79 #elif defined(__x86_64__)
80 #define __NR_fadvise64 221
81 #elif defined(__ia64__)
82 #define __NR_fadvise64 1234
83 #elif defined(__alpha__)
84 #define __NR_fadvise64 413
85 #elif defined(__s390x__) || defined(__s390__)
86 #define __NR_fadvise64 253
88 #error "Unsupported arch"
92 static int ioprio_set(int which, int who, int ioprio)
94 return syscall(__NR_ioprio_set, which, who, ioprio);
98 * we want fadvise64 really, but it's so tangled... later
100 static int fadvise(int fd, loff_t offset, size_t len, int advice)
103 return syscall(__NR_fadvise64, fd, offset, offset >> 32, len, advice);
105 return posix_fadvise(fd, (off_t) offset, len, advice);
110 IOPRIO_WHO_PROCESS = 1,
115 #define IOPRIO_CLASS_SHIFT 13
119 #define DEF_BS (4096)
120 #define DEF_TIMEOUT (30)
121 #define DEF_RATE_CYCLE (1000)
122 #define DEF_ODIRECT (1)
123 #define DEF_SEQUENTIAL (1)
124 #define DEF_RAND_REPEAT (1)
125 #define DEF_OVERWRITE (0)
126 #define DEF_CREATE (1)
127 #define DEF_INVALIDATE (1)
129 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
131 static int repeatable = DEF_RAND_REPEAT;
132 static int rate_quit = 1;
134 static int thread_number;
135 static char *ini_file;
137 static int max_jobs = MAX_JOBS;
139 static char run_str[MAX_JOBS + 1];
164 struct timeval start_time;
165 struct timeval issue_time;
172 struct list_head list;
177 unsigned long val_sq;
178 unsigned long max_val;
179 unsigned long min_val;
180 unsigned long samples;
183 #define td_read(td) ((td)->ddir == DDIR_READ)
184 #define should_fsync(td) (!td_read(td) && !(td)->odirect)
192 volatile int terminate;
193 volatile int runstate;
196 unsigned int sequential;
198 unsigned int odirect;
199 unsigned int delay_sleep;
200 unsigned int fsync_blocks;
201 unsigned int start_delay;
202 unsigned int timeout;
203 unsigned int use_aio;
204 unsigned int create_file;
205 unsigned int overwrite;
206 unsigned int invalidate_cache;
207 unsigned long long file_size;
208 unsigned long long file_offset;
213 io_context_t aio_ctx;
214 unsigned int aio_depth;
215 struct io_event *aio_events;
217 unsigned int cur_depth;
218 struct list_head io_u_freelist;
219 struct list_head io_u_busylist;
222 unsigned int ratemin;
223 unsigned int ratecycle;
224 unsigned long rate_usec_cycle;
225 long rate_pending_usleep;
226 unsigned long rate_blocks;
227 struct timeval lastrate;
229 unsigned long runtime; /* sec */
230 unsigned long blocks;
231 unsigned long io_blocks;
232 unsigned long last_block;
234 struct drand48_data random_state;
237 * bandwidth and latency stats
239 struct io_stat clat_stat; /* completion latency */
240 struct io_stat slat_stat; /* submission latency */
242 struct io_stat bw_stat; /* bandwidth stats */
243 unsigned long stat_io_blocks;
244 struct timeval stat_sample_time;
246 struct timeval start;
249 static struct thread_data *threads;
250 static struct thread_data def_thread;
252 static sem_t startup_sem;
254 static void sig_handler(int sig)
258 for (i = 0; i < thread_number; i++) {
259 struct thread_data *td = &threads[i];
266 static int init_random_state(struct thread_data *td)
268 unsigned long seed = 123;
274 int fd = open("/dev/random", O_RDONLY);
281 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
290 srand48_r(seed, &td->random_state);
294 static unsigned long utime_since(struct timeval *s, struct timeval *e)
298 sec = e->tv_sec - s->tv_sec;
299 usec = e->tv_usec - s->tv_usec;
300 if (sec > 0 && usec < 0) {
305 sec *= (double) 1000000;
310 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
314 sec = e->tv_sec - s->tv_sec;
315 usec = e->tv_usec - s->tv_usec;
316 if (sec > 0 && usec < 0) {
321 sec *= (double) 1000;
322 usec /= (double) 1000;
327 static unsigned long mtime_since_now(struct timeval *s)
331 gettimeofday(&t, NULL);
332 return mtime_since(s, &t);
335 static inline unsigned long msec_now(struct timeval *s)
337 return s->tv_sec * 1000 + s->tv_usec / 1000;
340 static unsigned long get_next_offset(struct thread_data *td)
345 if (!td->sequential) {
346 lrand48_r(&td->random_state, &r);
347 b = (1+(double) (td->blocks-1) * r / (RAND_MAX+1.0));
353 return b * td->bs + td->file_offset;
356 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
359 if (val > is->max_val)
361 if (val < is->min_val)
365 is->val_sq += val * val;
369 static void add_clat_sample(struct thread_data *td, unsigned long msec)
371 add_stat_sample(td, &td->clat_stat, msec);
374 static void add_slat_sample(struct thread_data *td, unsigned long msec)
376 add_stat_sample(td, &td->slat_stat, msec);
379 static void add_bw_sample(struct thread_data *td, unsigned long msec)
381 unsigned long spent = mtime_since_now(&td->stat_sample_time);
387 rate = ((td->io_blocks - td->stat_io_blocks) * td->bs) / spent;
388 add_stat_sample(td, &td->bw_stat, rate);
390 gettimeofday(&td->stat_sample_time, NULL);
391 td->stat_io_blocks = td->io_blocks;
394 static void usec_sleep(int usec)
396 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
400 rem.tv_sec = rem.tv_nsec = 0;
401 nanosleep(&req, &rem);
405 req.tv_nsec = rem.tv_nsec;
409 static void rate_throttle(struct thread_data *td, unsigned long time_spent)
414 if (time_spent < td->rate_usec_cycle) {
415 unsigned long s = td->rate_usec_cycle - time_spent;
417 td->rate_pending_usleep += s;
418 if (td->rate_pending_usleep >= 100000) {
419 usec_sleep(td->rate_pending_usleep);
420 td->rate_pending_usleep = 0;
423 long overtime = time_spent - td->rate_usec_cycle;
425 td->rate_pending_usleep -= overtime;
429 static int check_min_rate(struct thread_data *td, struct timeval *now)
435 * allow a 2 second settle period in the beginning
437 if (mtime_since(&td->start, now) < 2000)
441 * if rate blocks is set, sample is running
443 if (td->rate_blocks) {
444 spent = mtime_since(&td->lastrate, now);
445 if (spent < td->ratecycle)
448 rate = ((td->io_blocks - td->rate_blocks) * td->bs) / spent;
449 if (rate < td->ratemin) {
450 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
457 td->rate_blocks = td->io_blocks;
458 memcpy(&td->lastrate, now, sizeof(*now));
462 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
464 if (mtime_since(&td->start, t) >= td->timeout * 1000)
470 static void put_io_u(struct thread_data *td, struct io_u *io_u)
472 list_del(&io_u->list);
473 list_add(&io_u->list, &td->io_u_freelist);
477 static struct io_u *get_io_u(struct thread_data *td)
481 if (list_empty(&td->io_u_freelist))
484 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
485 list_del(&io_u->list);
486 list_add(&io_u->list, &td->io_u_busylist);
488 io_u->offset = get_next_offset(td);
492 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
494 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
497 gettimeofday(&io_u->start_time, NULL);
502 static void do_sync_io(struct thread_data *td)
504 unsigned long blocks, msec, usec;
509 for (blocks = 0; blocks < td->blocks; blocks++) {
518 if (td->cur_off != io_u->offset) {
519 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
526 usec_sleep(td->delay_sleep);
529 ret = read(td->fd, io_u->buf, io_u->buflen);
531 ret = write(td->fd, io_u->buf, io_u->buflen);
533 if (ret < (int) io_u->buflen) {
540 td->cur_off = io_u->offset + io_u->buflen;
542 if (should_fsync(td) && td->fsync_blocks &&
543 (td->io_blocks % td->fsync_blocks) == 0)
546 gettimeofday(&e, NULL);
548 usec = utime_since(&io_u->start_time, &e);
550 rate_throttle(td, usec);
552 if (check_min_rate(td, &e)) {
558 add_clat_sample(td, msec);
559 add_bw_sample(td, msec);
561 if (runtime_exceeded(td, &e))
567 if (should_fsync(td))
571 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
573 struct iocb *iocb = &io_u->iocb;
577 ret = io_submit(td->aio_ctx, 1, &iocb);
580 else if (ret == EAGAIN)
582 else if (ret == EINTR)
591 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
592 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
594 static void ios_completed(struct thread_data *td, int nr)
601 gettimeofday(&e, NULL);
603 for (i = 0; i < nr; i++) {
606 io_u = ev_to_iou(td->aio_events + i);
608 msec = mtime_since(&io_u->issue_time, &e);
610 add_clat_sample(td, msec);
611 add_bw_sample(td, msec);
617 static void cleanup_pending_aio(struct thread_data *td)
619 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
620 struct list_head *entry, *n;
625 * get immediately available events, if any
627 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
629 ios_completed(td, r);
632 * now cancel remaining active events
634 list_for_each_safe(entry, n, &td->io_u_busylist) {
635 io_u = list_entry(entry, struct io_u, list);
637 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
643 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
645 ios_completed(td, r);
649 static void do_async_io(struct thread_data *td)
652 unsigned long blocks, usec;
654 for (blocks = 0; blocks < td->blocks; blocks++) {
655 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
656 struct timespec *timeout;
657 int ret, min_evts = 0;
664 usec_sleep(td->delay_sleep);
668 memcpy(&s, &io_u->start_time, sizeof(s));
670 ret = io_u_queue(td, io_u);
677 gettimeofday(&io_u->issue_time, NULL);
678 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
680 if (td->cur_depth < td->aio_depth) {
688 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
695 ios_completed(td, ret);
698 * the rate is batched for now, it should work for batches
699 * of completions except the very first one which may look
702 gettimeofday(&e, NULL);
703 usec = utime_since(&s, &e);
705 rate_throttle(td, usec);
707 if (check_min_rate(td, &e)) {
712 if (runtime_exceeded(td, &e))
717 cleanup_pending_aio(td);
720 static void cleanup_aio(struct thread_data *td)
722 io_destroy(td->aio_ctx);
725 free(td->aio_events);
728 static int init_aio(struct thread_data *td)
730 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
735 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
739 static void cleanup_io_u(struct thread_data *td)
741 struct list_head *entry, *n;
744 list_for_each_safe(entry, n, &td->io_u_freelist) {
745 io_u = list_entry(entry, struct io_u, list);
747 list_del(&io_u->list);
753 static void init_io_u(struct thread_data *td)
761 max_units = td->aio_depth;
763 INIT_LIST_HEAD(&td->io_u_freelist);
764 INIT_LIST_HEAD(&td->io_u_busylist);
766 for (i = 0; i < max_units; i++) {
767 io_u = malloc(sizeof(*io_u));
768 memset(io_u, 0, sizeof(*io_u));
769 INIT_LIST_HEAD(&io_u->list);
771 io_u->mem = malloc(td->bs + MASK);
772 io_u->buf = ALIGN(io_u->mem);
773 io_u->buflen = td->bs;
775 list_add(&io_u->list, &td->io_u_freelist);
779 static int create_file(struct thread_data *td)
785 * unless specifically asked for overwrite, let normal io extend it
787 if (!td_read(td) && !td->overwrite)
790 if (!td->file_size) {
791 fprintf(stderr, "Need size for create\n");
796 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
802 td->blocks = td->file_size / td->bs;
804 memset(b, 0, td->bs);
806 for (i = 0; i < td->blocks; i++) {
807 int r = write(td->fd, b, td->bs);
828 static int file_exists(struct thread_data *td)
832 if (stat(td->file_name, &st) != -1)
835 return errno != ENOENT;
838 static int setup_file(struct thread_data *td)
843 if (!file_exists(td)) {
844 if (!td->create_file) {
856 td->fd = open(td->file_name, flags | O_RDONLY);
861 td->fd = open(td->file_name, flags | O_WRONLY | O_CREAT, 0600);
870 if (fstat(td->fd, &st) == -1) {
875 if (td->file_size > st.st_size)
876 st.st_size = td->file_size;
879 td->file_size = 1024 * 1024 * 1024;
881 st.st_size = td->file_size;
884 td->blocks = (st.st_size - td->file_offset) / td->bs;
886 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
891 if (td->invalidate_cache) {
892 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
901 static void *thread_main(int shm_id, int offset, char *argv[])
903 struct thread_data *td;
909 data = shmat(shm_id, NULL, 0);
910 td = data + offset * sizeof(struct thread_data);
915 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
920 sprintf(argv[0], "fio%d", offset);
922 if (td->use_aio && init_aio(td))
925 if (init_random_state(td))
929 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
938 sem_post(&startup_sem);
939 sem_wait(&td->mutex);
941 gettimeofday(&td->start, NULL);
944 memcpy(&td->lastrate, &td->start, sizeof(td->start));
946 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
953 td->runtime = mtime_since_now(&td->start);
964 sem_post(&startup_sem);
965 sem_wait(&td->mutex);
967 td->runstate = TD_EXITED;
972 static void free_shm(void)
977 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
978 double *mean, double *dev)
982 if (is->samples == 0)
988 n = (double) is->samples;
989 *mean = (double) is->val / n;
990 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
994 static void show_thread_status(struct thread_data *td)
996 int prio, prio_class;
997 unsigned long min, max, bw = 0;
1000 if (!td->io_blocks && !td->error)
1004 bw = (td->io_blocks * td->bs) / td->runtime;
1006 prio = td->ioprio & 0xff;
1007 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1009 printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s\n", td->thread_number, td->error, td->io_blocks * td->bs >> 20, bw);
1011 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1012 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1013 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1014 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1015 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1016 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1019 static int setup_rate(struct thread_data *td)
1021 int nr_reads_per_sec;
1026 if (td->rate < td->ratemin) {
1027 fprintf(stderr, "min rate larger than nominal rate\n");
1031 nr_reads_per_sec = td->rate * 1024 / td->bs;
1032 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1033 td->rate_pending_usleep = 0;
1037 static struct thread_data *get_new_job(int global)
1039 struct thread_data *td;
1043 if (thread_number >= max_jobs)
1046 td = &threads[thread_number++];
1047 memset(td, 0, sizeof(*td));
1050 td->thread_number = thread_number;
1052 td->ddir = def_thread.ddir;
1053 td->ioprio = def_thread.ioprio;
1054 td->sequential = def_thread.sequential;
1055 td->bs = def_thread.bs;
1056 td->odirect = def_thread.odirect;
1057 td->delay_sleep = def_thread.delay_sleep;
1058 td->fsync_blocks = def_thread.fsync_blocks;
1059 td->start_delay = def_thread.start_delay;
1060 td->timeout = def_thread.timeout;
1061 td->use_aio = def_thread.use_aio;
1062 td->create_file = def_thread.create_file;
1063 td->overwrite = def_thread.overwrite;
1064 td->invalidate_cache = def_thread.invalidate_cache;
1065 td->file_size = def_thread.file_size;
1066 td->file_size = def_thread.file_offset;
1067 td->rate = def_thread.rate;
1068 td->ratemin = def_thread.ratemin;
1069 td->ratecycle = def_thread.ratecycle;
1070 td->aio_depth = def_thread.aio_depth;
1071 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
1076 static void put_job(struct thread_data *td)
1078 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1082 static int add_job(struct thread_data *td, const char *filename, int prioclass,
1085 if (td == &def_thread)
1088 strcpy(td->file_name, filename);
1089 sem_init(&td->mutex, 1, 0);
1090 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1092 td->clat_stat.min_val = ULONG_MAX;
1093 td->slat_stat.min_val = ULONG_MAX;
1094 td->bw_stat.min_val = ULONG_MAX;
1096 run_str[td->thread_number - 1] = 'P';
1103 * no buffered aio support for now
1112 printf("Client%d: file=%s, rw=%d, prio=%d/%d, seq=%d, odir=%d, bs=%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, prioclass, prio, td->sequential, td->odirect, td->bs, td->rate, td->use_aio, td->aio_depth);
1116 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1122 for (i = 0; i < sizeof(int) * 8; i++) {
1124 CPU_SET(i, &cpumask);
1128 static void fill_option(const char *input, char *output)
1133 while (input[i] != ',' && input[i] != '}' && input[i] != '\0') {
1134 output[i] = input[i];
1142 * convert string after '=' into decimal value, noting any size suffix
1144 static int str_cnv(char *p, unsigned long long *val)
1150 str = strstr(p, "=");
1158 switch (str[len - 2]) {
1169 mult = 1024 * 1024 * 1024;
1173 *val = strtoul(str, NULL, 10);
1174 if (*val == ULONG_MAX && errno == ERANGE)
1190 static void parse_jobs_cmd(int argc, char *argv[], int index)
1192 struct thread_data *td;
1193 unsigned int prio, prioclass, cpu;
1194 char *string, *filename, *p, *c;
1197 string = malloc(256);
1198 filename = malloc(256);
1200 for (i = index; i < argc; i++) {
1203 c = strpbrk(p, "{");
1209 td = get_new_job(0);
1216 c = strstr(p, "rw=");
1220 td->ddir = DDIR_READ;
1222 td->ddir = DDIR_WRITE;
1225 c = strstr(p, "prio=");
1231 c = strstr(p, "prioclass=");
1234 prioclass = *c - '0';
1237 c = strstr(p, "file=");
1240 fill_option(c, filename);
1243 c = strstr(p, "bs=");
1246 fill_option(c, string);
1247 td->bs = strtoul(string, NULL, 10);
1251 c = strstr(p, "direct=");
1260 c = strstr(p, "delay=");
1263 fill_option(c, string);
1264 td->delay_sleep = strtoul(string, NULL, 10);
1267 c = strstr(p, "rate=");
1270 fill_option(c, string);
1271 td->rate = strtoul(string, NULL, 10);
1274 c = strstr(p, "ratemin=");
1277 fill_option(c, string);
1278 td->ratemin = strtoul(string, NULL, 10);
1281 c = strstr(p, "ratecycle=");
1284 fill_option(c, string);
1285 td->ratecycle = strtoul(string, NULL, 10);
1288 c = strstr(p, "cpumask=");
1291 fill_option(c, string);
1292 cpu = strtoul(string, NULL, 10);
1293 fill_cpu_mask(td->cpumask, cpu);
1296 c = strstr(p, "fsync=");
1299 fill_option(c, string);
1300 td->fsync_blocks = strtoul(string, NULL, 10);
1303 c = strstr(p, "startdelay=");
1306 fill_option(c, string);
1307 td->start_delay = strtoul(string, NULL, 10);
1310 c = strstr(p, "timeout=");
1313 fill_option(c, string);
1314 td->timeout = strtoul(string, NULL, 10);
1317 c = strstr(p, "invalidate=");
1321 td->invalidate_cache = 1;
1323 td->invalidate_cache = 0;
1326 c = strstr(p, "size=");
1329 str_cnv(c, &td->file_size);
1332 c = strstr(p, "offset=");
1335 str_cnv(c, &td->file_offset);
1338 c = strstr(p, "aio_depth=");
1341 fill_option(c, string);
1342 td->aio_depth = strtoul(string, NULL, 10);
1345 c = strstr(p, "aio");
1349 c = strstr(p, "create");
1351 td->create_file = 1;
1353 c = strstr(p, "overwrite");
1357 c = strstr(p, "random");
1360 c = strstr(p, "sequential");
1364 if (add_job(td, filename, prioclass, prio))
1372 static int check_strcnv(char *p, char *name, unsigned long long *val)
1374 if (!strstr(p, name))
1377 return str_cnv(p, val);
1380 static int check_int(char *p, char *name, unsigned int *val)
1384 sprintf(str, "%s=%%d", name);
1385 if (sscanf(p, str, val) == 1)
1388 sprintf(str, "%s = %%d", name);
1389 if (sscanf(p, str, val) == 1)
1395 static int is_empty_or_comment(char *line)
1399 for (i = 0; i < strlen(line); i++) {
1402 if (!isspace(line[i]) && !iscntrl(line[i]))
1409 static int parse_jobs_ini(char *file)
1411 unsigned int prioclass, prio, cpu, global;
1412 struct thread_data *td;
1413 char *string, *name;
1418 f = fopen(file, "r");
1424 string = malloc(4096);
1427 while ((p = fgets(string, 4096, f)) != NULL) {
1428 if (is_empty_or_comment(p))
1430 if (sscanf(p, "[%s]", name) != 1)
1433 global = !strncmp(name, "global", 6);
1435 name[strlen(name) - 1] = '\0';
1437 td = get_new_job(global);
1445 while ((p = fgets(string, 4096, f)) != NULL) {
1446 if (is_empty_or_comment(p))
1450 if (!check_int(p, "bs", &td->bs)) {
1455 if (!check_int(p, "rw", &td->ddir)) {
1459 if (!check_int(p, "prio", &prio)) {
1463 if (!check_int(p, "prioclass", &prioclass)) {
1467 if (!check_int(p, "direct", &td->odirect)) {
1471 if (!check_int(p, "rate", &td->rate)) {
1475 if (!check_int(p, "ratemin", &td->ratemin)) {
1479 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1483 if (!check_int(p, "delay", &td->delay_sleep)) {
1487 if (!check_int(p, "cpumask", &cpu)) {
1488 fill_cpu_mask(td->cpumask, cpu);
1492 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1496 if (!check_int(p, "startdelay", &td->start_delay)) {
1500 if (!check_int(p, "timeout", &td->timeout)) {
1504 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1508 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1512 if (!check_strcnv(p, "size", &td->file_size)) {
1516 if (!check_strcnv(p, "offset", &td->file_offset)) {
1520 if (!strncmp(p, "sequential", 10)) {
1525 if (!strncmp(p, "random", 6)) {
1530 if (!strncmp(p, "aio", 3)) {
1535 if (!strncmp(p, "create", 6)) {
1536 td->create_file = 1;
1540 if (!strncmp(p, "overwrite", 9)) {
1545 printf("Client%d: bad option %s\n",td->thread_number,p);
1549 if (add_job(td, name, prioclass, prio))
1559 static int parse_options(int argc, char *argv[])
1563 for (i = 1; i < argc; i++) {
1564 char *parm = argv[i];
1573 def_thread.sequential = !!atoi(parm);
1577 def_thread.bs = atoi(parm);
1578 def_thread.bs <<= 10;
1579 if (!def_thread.bs) {
1580 printf("bad block size\n");
1581 def_thread.bs = DEF_BS;
1586 def_thread.timeout = atoi(parm);
1590 repeatable = !!atoi(parm);
1594 rate_quit = !!atoi(parm);
1598 def_thread.odirect = !!atoi(parm);
1601 if (i + 1 >= argc) {
1602 printf("-f needs file as arg\n");
1605 ini_file = strdup(argv[i+1]);
1609 printf("bad option %s\n", argv[i]);
1617 static void print_thread_status(struct thread_data *td, int nr_running,
1618 int t_rate, int m_rate)
1620 printf("Threads now running: %d", nr_running);
1621 if (m_rate || t_rate)
1622 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1623 printf(" : [%s]\r", run_str);
1627 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1632 * reap exited threads (TD_EXITED -> TD_REAPED)
1634 for (i = 0; i < thread_number; i++) {
1635 struct thread_data *td = &threads[i];
1637 if (td->runstate != TD_EXITED)
1640 td->runstate = TD_REAPED;
1641 run_str[td->thread_number - 1] = '_';
1642 waitpid(td->pid, NULL, 0);
1644 (*m_rate) -= td->ratemin;
1645 (*t_rate) -= td->rate;
1650 print_thread_status(td, *nr_running, *t_rate, *m_rate);
1654 static void run_threads(char *argv[])
1656 struct timeval genesis;
1657 struct thread_data *td;
1658 unsigned long spent;
1659 int i, todo, nr_running, m_rate, t_rate;
1661 gettimeofday(&genesis, NULL);
1663 printf("Starting %d threads\n", thread_number);
1666 signal(SIGINT, sig_handler);
1668 todo = thread_number;
1670 m_rate = t_rate = 0;
1674 * create threads (TD_NOT_CREATED -> TD_CREATED)
1676 for (i = 0; i < thread_number; i++) {
1679 if (td->runstate != TD_NOT_CREATED)
1683 * never got a chance to start, killed by other
1684 * thread for some reason
1686 if (td->terminate) {
1691 if (td->start_delay) {
1692 spent = mtime_since_now(&genesis);
1694 if (td->start_delay * 1000 > spent)
1698 td->runstate = TD_CREATED;
1699 run_str[td->thread_number - 1] = 'C';
1700 sem_init(&startup_sem, 1, 1);
1704 sem_wait(&startup_sem);
1706 thread_main(shm_id, i, argv);
1712 * start created threads (TD_CREATED -> TD_STARTED)
1714 for (i = 0; i < thread_number; i++) {
1715 struct thread_data *td = &threads[i];
1717 if (td->runstate != TD_CREATED)
1720 td->runstate = TD_STARTED;
1721 run_str[td->thread_number - 1] = '+';
1723 m_rate += td->ratemin;
1725 sem_post(&td->mutex);
1727 print_thread_status(td, nr_running, t_rate, m_rate);
1730 reap_threads(&nr_running, &t_rate, &m_rate);
1736 while (nr_running) {
1737 reap_threads(&nr_running, &t_rate, &m_rate);
1742 int setup_thread_area(void)
1745 * 1024 is too much on some machines, scale max_jobs if
1746 * we get a failure that looks like too large a shm segment
1749 int s = max_jobs * sizeof(struct thread_data);
1751 shm_id = shmget(0, s, IPC_CREAT | 0600);
1754 if (errno != EINVAL) {
1765 threads = shmat(shm_id, NULL, 0);
1766 if (threads == (void *) -1) {
1775 int main(int argc, char *argv[])
1777 static unsigned long max_run[2], min_run[2], total_blocks[2];
1778 static unsigned long max_bw[2], min_bw[2];
1779 static unsigned long read_mb, write_mb, read_agg, write_agg;
1782 if (setup_thread_area())
1785 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
1786 perror("sched_getaffinity");
1793 def_thread.ddir = DDIR_READ;
1794 def_thread.bs = DEF_BS;
1795 def_thread.odirect = DEF_ODIRECT;
1796 def_thread.ratecycle = DEF_RATE_CYCLE;
1797 def_thread.sequential = DEF_SEQUENTIAL;
1798 def_thread.timeout = DEF_TIMEOUT;
1799 def_thread.create_file = DEF_CREATE;
1800 def_thread.overwrite = DEF_OVERWRITE;
1801 def_thread.invalidate_cache = DEF_INVALIDATE;
1803 i = parse_options(argc, argv);
1806 if (parse_jobs_ini(ini_file))
1809 parse_jobs_cmd(argc, argv, i);
1811 if (!thread_number) {
1812 printf("Nothing to do\n");
1818 min_bw[0] = min_run[0] = ~0UL;
1819 min_bw[1] = min_run[1] = ~0UL;
1820 for (i = 0; i < thread_number; i++) {
1821 struct thread_data *td = &threads[i];
1822 unsigned long bw = 0;
1827 if (td->runtime < min_run[td->ddir])
1828 min_run[td->ddir] = td->runtime;
1829 if (td->runtime > max_run[td->ddir])
1830 max_run[td->ddir] = td->runtime;
1833 bw = (td->io_blocks * td->bs) / td->runtime;
1834 if (bw < min_bw[td->ddir])
1835 min_bw[td->ddir] = bw;
1836 if (bw > max_bw[td->ddir])
1837 max_bw[td->ddir] = bw;
1839 total_blocks[td->ddir] += td->io_blocks;
1842 read_mb += (td->bs * td->io_blocks) >> 20;
1844 read_agg += (td->io_blocks * td->bs) / td->runtime;
1846 write_mb += (td->bs * td->io_blocks) >> 20;
1848 write_agg += (td->io_blocks * td->bs) / td->runtime;
1852 show_thread_status(td);
1855 printf("\nRun status:\n");
1856 if (max_run[DDIR_READ])
1857 printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", read_mb, read_agg, min_bw[0], max_bw[0], min_run[0], max_run[0]);
1858 if (max_run[DDIR_WRITE])
1859 printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", write_mb, write_agg, min_bw[1], max_bw[1], min_run[1], max_run[1]);