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>
42 #include <asm/types.h>
47 #define MAX_JOBS (1024)
50 * assume we don't have _get either, if _set isn't defined
52 #ifndef __NR_ioprio_set
54 #define __NR_ioprio_set 289
55 #define __NR_ioprio_get 290
56 #elif defined(__powerpc__) || defined(__powerpc64__)
57 #define __NR_ioprio_set 273
58 #define __NR_ioprio_get 274
59 #elif defined(__x86_64__)
60 #define __NR_ioprio_set 251
61 #define __NR_ioprio_get 252
62 #elif defined(__ia64__)
63 #define __NR_ioprio_set 1274
64 #define __NR_ioprio_get 1275
65 #elif defined(__alpha__)
66 #define __NR_ioprio_set 442
67 #define __NR_ioprio_get 443
68 #elif defined(__s390x__) || defined(__s390__)
69 #define __NR_ioprio_set 282
70 #define __NR_ioprio_get 283
72 #error "Unsupported arch"
76 #ifndef __NR_fadvise64
78 #define __NR_fadvise64 250
79 #elif defined(__powerpc__) || defined(__powerpc64__)
80 #define __NR_fadvise64 233
81 #elif defined(__x86_64__)
82 #define __NR_fadvise64 221
83 #elif defined(__ia64__)
84 #define __NR_fadvise64 1234
85 #elif defined(__alpha__)
86 #define __NR_fadvise64 413
87 #elif defined(__s390x__) || defined(__s390__)
88 #define __NR_fadvise64 253
90 #error "Unsupported arch"
94 static int ioprio_set(int which, int who, int ioprio)
96 return syscall(__NR_ioprio_set, which, who, ioprio);
100 * we want fadvise64 really, but it's so tangled... later
102 static int fadvise(int fd, loff_t offset, size_t len, int advice)
105 return syscall(__NR_fadvise64, fd, offset, offset >> 32, len, advice);
107 return posix_fadvise(fd, (off_t) offset, len, advice);
112 IOPRIO_WHO_PROCESS = 1,
117 #define IOPRIO_CLASS_SHIFT 13
121 #define DEF_BS (4096)
122 #define DEF_TIMEOUT (30)
123 #define DEF_RATE_CYCLE (1000)
124 #define DEF_ODIRECT (1)
125 #define DEF_SEQUENTIAL (1)
126 #define DEF_RAND_REPEAT (1)
127 #define DEF_OVERWRITE (0)
128 #define DEF_CREATE (1)
129 #define DEF_INVALIDATE (1)
130 #define DEF_SYNCIO (0)
131 #define DEF_RANDSEED (0xb1899bedUL)
132 #define DEF_BWAVGTIME (500)
133 #define DEF_CREATE_SER (1)
134 #define DEF_CREATE_FSYNC (1)
135 #define DEF_LOOPS (1)
136 #define DEF_VERIFY (0)
138 #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
140 static int repeatable = DEF_RAND_REPEAT;
141 static int rate_quit = 1;
142 static int write_lat_log;
143 static int write_bw_log;
144 static int exitall_on_terminate;
146 static int thread_number;
147 static char *ini_file;
149 static int max_jobs = MAX_JOBS;
151 static char run_str[MAX_JOBS + 1];
182 struct timeval start_time;
183 struct timeval issue_time;
187 unsigned long long offset;
189 struct list_head list;
194 unsigned long val_sq;
195 unsigned long max_val;
196 unsigned long min_val;
197 unsigned long samples;
206 unsigned long nr_samples;
207 unsigned long max_samples;
208 struct io_sample *log;
211 #define FIO_HDR_MAGIC 0xf00baaef
213 struct verify_header {
214 unsigned int fio_magic;
216 char md5_digest[MD5_HASH_WORDS * 4];
219 #define td_read(td) ((td)->ddir == DDIR_READ)
220 #define should_fsync(td) (!td_read(td) && !(td)->odirect)
229 volatile int terminate;
230 volatile int runstate;
233 unsigned int sequential;
237 unsigned int odirect;
238 unsigned int thinktime;
239 unsigned int fsync_blocks;
240 unsigned int start_delay;
241 unsigned int timeout;
242 unsigned int use_aio;
243 unsigned int create_file;
244 unsigned int overwrite;
245 unsigned int invalidate_cache;
246 unsigned int bw_avg_time;
247 unsigned int create_serialize;
248 unsigned int create_fsync;
250 unsigned long long file_size;
251 unsigned long long file_offset;
252 unsigned int sync_io;
253 unsigned int mem_type;
257 struct drand48_data bsrange_state;
258 struct drand48_data verify_state;
264 io_context_t aio_ctx;
265 unsigned int aio_depth;
266 struct io_event *aio_events;
268 unsigned int cur_depth;
269 struct list_head io_u_freelist;
270 struct list_head io_u_busylist;
273 unsigned int ratemin;
274 unsigned int ratecycle;
275 unsigned long rate_usec_cycle;
276 long rate_pending_usleep;
277 unsigned long rate_kb;
278 struct timeval lastrate;
280 unsigned long runtime; /* sec */
282 unsigned long io_blocks;
284 unsigned long this_io_kb;
285 unsigned long last_kb;
287 struct drand48_data random_state;
290 * bandwidth and latency stats
292 struct io_stat clat_stat; /* completion latency */
293 struct io_stat slat_stat; /* submission latency */
295 struct io_stat bw_stat; /* bandwidth stats */
296 unsigned long stat_io_kb;
297 struct timeval stat_sample_time;
299 struct io_log *lat_log;
300 struct io_log *bw_log;
302 struct timeval start;
305 static struct thread_data *threads;
306 static struct thread_data def_thread;
308 static sem_t startup_sem;
310 static void sig_handler(int sig)
314 for (i = 0; i < thread_number; i++) {
315 struct thread_data *td = &threads[i];
322 static int init_random_state(struct thread_data *td)
327 fd = open("/dev/random", O_RDONLY);
333 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
341 srand48_r(seed, &td->bsrange_state);
342 srand48_r(seed, &td->verify_state);
350 srand48_r(seed, &td->random_state);
354 static unsigned long utime_since(struct timeval *s, struct timeval *e)
358 sec = e->tv_sec - s->tv_sec;
359 usec = e->tv_usec - s->tv_usec;
360 if (sec > 0 && usec < 0) {
365 sec *= (double) 1000000;
370 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
374 sec = e->tv_sec - s->tv_sec;
375 usec = e->tv_usec - s->tv_usec;
376 if (sec > 0 && usec < 0) {
381 sec *= (double) 1000;
382 usec /= (double) 1000;
387 static unsigned long mtime_since_now(struct timeval *s)
391 gettimeofday(&t, NULL);
392 return mtime_since(s, &t);
395 static inline unsigned long msec_now(struct timeval *s)
397 return s->tv_sec * 1000 + s->tv_usec / 1000;
400 static unsigned long long get_next_offset(struct thread_data *td)
402 unsigned long long kb;
405 if (!td->sequential) {
406 lrand48_r(&td->random_state, &r);
407 kb = (1+(double) (td->kb-1) * r / (RAND_MAX+1.0));
411 return (kb << 10) + td->file_offset;
414 static unsigned int get_next_buflen(struct thread_data *td)
419 if (td->min_bs == td->max_bs)
422 lrand48_r(&td->bsrange_state, &r);
423 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
424 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
427 if (buflen > ((td->kb - td->this_io_kb) << 10))
428 buflen = (td->kb - td->this_io_kb) << 10;
430 td->last_kb += buflen >> 10;
434 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
437 if (val > is->max_val)
439 if (val < is->min_val)
443 is->val_sq += val * val;
447 static void add_log_sample(struct thread_data *td, struct io_log *log,
450 if (log->nr_samples == log->max_samples) {
451 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
453 log->log = realloc(log->log, new_size);
454 log->max_samples <<= 1;
457 log->log[log->nr_samples].val = val;
458 log->log[log->nr_samples].time = mtime_since_now(&td->start);
462 static void add_clat_sample(struct thread_data *td, unsigned long msec)
464 add_stat_sample(td, &td->clat_stat, msec);
467 add_log_sample(td, td->lat_log, msec);
470 static void add_slat_sample(struct thread_data *td, unsigned long msec)
472 add_stat_sample(td, &td->slat_stat, msec);
475 static void add_bw_sample(struct thread_data *td, unsigned long msec)
477 unsigned long spent = mtime_since_now(&td->stat_sample_time);
480 if (spent < td->bw_avg_time)
483 rate = ((td->this_io_kb - td->stat_io_kb) * 1024) / spent;
484 add_stat_sample(td, &td->bw_stat, rate);
487 add_log_sample(td, td->bw_log, rate);
489 gettimeofday(&td->stat_sample_time, NULL);
490 td->stat_io_kb = td->this_io_kb;
493 static void usec_sleep(int usec)
495 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
499 rem.tv_sec = rem.tv_nsec = 0;
500 nanosleep(&req, &rem);
504 req.tv_nsec = rem.tv_nsec;
508 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
511 unsigned long usec_cycle;
516 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
518 if (time_spent < usec_cycle) {
519 unsigned long s = usec_cycle - time_spent;
521 td->rate_pending_usleep += s;
522 if (td->rate_pending_usleep >= 100000) {
523 usec_sleep(td->rate_pending_usleep);
524 td->rate_pending_usleep = 0;
527 long overtime = time_spent - usec_cycle;
529 td->rate_pending_usleep -= overtime;
533 static int check_min_rate(struct thread_data *td, struct timeval *now)
539 * allow a 2 second settle period in the beginning
541 if (mtime_since(&td->start, now) < 2000)
545 * if rate blocks is set, sample is running
548 spent = mtime_since(&td->lastrate, now);
549 if (spent < td->ratecycle)
552 rate = ((td->this_io_kb - td->rate_kb) * 1024) / spent;
553 if (rate < td->ratemin) {
554 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
561 td->rate_kb = td->this_io_kb;
562 memcpy(&td->lastrate, now, sizeof(*now));
566 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
568 if (mtime_since(&td->start, t) >= td->timeout * 1000)
574 static void fill_random_bytes(struct thread_data *td,
575 unsigned char *p, unsigned int len)
581 lrand48_r(&td->verify_state, &r);
594 static int verify_io_u(struct thread_data *td, struct io_u *io_u)
596 struct verify_header *hdr = (struct verify_header *) io_u->buf;
597 unsigned char *p = (unsigned char *) io_u->buf;
598 struct md5_ctx md5_ctx;
600 if (hdr->fio_magic != FIO_HDR_MAGIC)
603 memset(&md5_ctx, 0, sizeof(md5_ctx));
605 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
607 return memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
610 static int verify_io_us(struct thread_data *td, struct io_u *io_u, int *back)
612 struct verify_header *hdr;
624 hdr = (struct verify_header *) buf;
628 if (hdr->len > left) {
633 if (verify_io_u(td, &i)) {
634 printf("failed verify at offset %lu\n", (unsigned long) off);
647 * fill body of io_u->buf with random data and add a header with the
648 * (eg) sha1sum of that data.
650 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
652 struct md5_ctx md5_ctx;
653 struct verify_header hdr;
654 unsigned char *p = (unsigned char *) io_u->buf;
656 hdr.fio_magic = FIO_HDR_MAGIC;
657 hdr.len = io_u->buflen;
659 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
661 memset(&md5_ctx, 0, sizeof(md5_ctx));
662 md5_update(&md5_ctx, p, io_u->buflen - sizeof(hdr));
663 memcpy(hdr.md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
664 memcpy(io_u->buf, &hdr, sizeof(hdr));
667 static void put_io_u(struct thread_data *td, struct io_u *io_u)
669 list_del(&io_u->list);
670 list_add(&io_u->list, &td->io_u_freelist);
674 static struct io_u *__get_io_u(struct thread_data *td)
678 if (list_empty(&td->io_u_freelist))
681 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
682 list_del(&io_u->list);
683 list_add(&io_u->list, &td->io_u_busylist);
687 static struct io_u *get_io_u(struct thread_data *td)
691 unsigned long long off;
693 io_u = __get_io_u(td);
697 off = get_next_offset(td);
698 len = get_next_buflen(td);
707 if (!td_read(td) && td->verify)
708 populate_io_u(td, io_u);
712 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
714 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
717 gettimeofday(&io_u->start_time, NULL);
722 static int do_sync_verify(struct thread_data *td)
724 struct io_u *io_u = NULL;
728 td->runstate = TD_VERIFYING;
730 io_u = __get_io_u(td);
732 if (lseek(td->fd, 0, SEEK_SET) < 0) {
742 io_u->buflen = td->max_bs;
744 ret = read(td->fd, io_u->buf, io_u->buflen);
745 if (ret < (int) io_u->buflen) {
755 if (verify_io_us(td, io_u, &back))
760 if (lseek(td->fd, -back, SEEK_CUR) < 0) {
770 td->runstate = TD_RUNNING;
773 return td->error == 0;
776 static void do_sync_io(struct thread_data *td)
778 unsigned long msec, usec;
779 struct io_u *io_u = NULL;
785 for (td->this_io_kb = 0; td->this_io_kb < td->kb;) {
795 if (td->cur_off != io_u->offset) {
796 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
803 ret = read(td->fd, io_u->buf, io_u->buflen);
805 ret = write(td->fd, io_u->buf, io_u->buflen);
807 if (ret < (int) io_u->buflen) {
814 td->io_kb += io_u->buflen >> 10;
815 td->this_io_kb += io_u->buflen >> 10;
816 td->cur_off = io_u->offset + io_u->buflen;
818 gettimeofday(&e, NULL);
820 usec = utime_since(&io_u->start_time, &e);
822 rate_throttle(td, usec, io_u->buflen);
824 if (check_min_rate(td, &e)) {
830 add_clat_sample(td, msec);
831 add_bw_sample(td, msec);
833 if (runtime_exceeded(td, &e))
840 usec_sleep(td->thinktime);
842 if (should_fsync(td) && td->fsync_blocks &&
843 (td->io_blocks % td->fsync_blocks) == 0)
850 if (should_fsync(td))
854 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
856 struct iocb *iocb = &io_u->iocb;
860 ret = io_submit(td->aio_ctx, 1, &iocb);
863 else if (ret == EAGAIN)
865 else if (ret == EINTR)
874 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
875 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
877 static int ios_completed(struct thread_data *td, int nr)
884 gettimeofday(&e, NULL);
886 for (i = 0, bytes_done = 0; i < nr; i++) {
887 io_u = ev_to_iou(td->aio_events + i);
890 td->io_kb += io_u->buflen >> 10;
891 td->this_io_kb += io_u->buflen >> 10;
892 bytes_done += io_u->buflen;
894 msec = mtime_since(&io_u->issue_time, &e);
896 add_clat_sample(td, msec);
897 add_bw_sample(td, msec);
905 static void cleanup_pending_aio(struct thread_data *td)
907 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
908 struct list_head *entry, *n;
913 * get immediately available events, if any
915 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
917 ios_completed(td, r);
920 * now cancel remaining active events
922 list_for_each_safe(entry, n, &td->io_u_busylist) {
923 io_u = list_entry(entry, struct io_u, list);
925 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
931 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
933 ios_completed(td, r);
937 static int do_async_verify(struct thread_data *td)
940 * need to implement the async version
942 return do_sync_verify(td);
945 static void do_async_io(struct thread_data *td)
953 for (td->this_io_kb = 0; td->this_io_kb < td->kb;) {
954 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
955 struct timespec *timeout;
956 int ret, min_evts = 0;
958 unsigned int bytes_done;
967 memcpy(&s, &io_u->start_time, sizeof(s));
969 ret = io_u_queue(td, io_u);
976 gettimeofday(&io_u->issue_time, NULL);
977 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
978 if (td->cur_depth < td->aio_depth) {
986 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
993 bytes_done = ios_completed(td, ret);
996 * the rate is batched for now, it should work for batches
997 * of completions except the very first one which may look
1000 gettimeofday(&e, NULL);
1001 usec = utime_since(&s, &e);
1003 rate_throttle(td, usec, bytes_done);
1005 if (check_min_rate(td, &e)) {
1006 td->error = ENODATA;
1010 if (runtime_exceeded(td, &e))
1014 usec_sleep(td->thinktime);
1016 if (should_fsync(td) && td->fsync_blocks &&
1017 (td->io_blocks % td->fsync_blocks) == 0)
1022 cleanup_pending_aio(td);
1024 if (should_fsync(td))
1028 static void cleanup_aio(struct thread_data *td)
1030 io_destroy(td->aio_ctx);
1033 free(td->aio_events);
1036 static int init_aio(struct thread_data *td)
1038 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
1043 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
1047 static void cleanup_io_u(struct thread_data *td)
1049 struct list_head *entry, *n;
1052 list_for_each_safe(entry, n, &td->io_u_freelist) {
1053 io_u = list_entry(entry, struct io_u, list);
1055 list_del(&io_u->list);
1059 if (td->mem_type == MEM_MALLOC)
1060 free(td->orig_buffer);
1061 else if (td->mem_type == MEM_SHM) {
1062 struct shmid_ds sbuf;
1064 shmdt(td->orig_buffer);
1065 shmctl(td->shm_id, IPC_RMID, &sbuf);
1069 static int init_io_u(struct thread_data *td)
1072 int i, max_units, mem_size;
1078 max_units = td->aio_depth;
1080 mem_size = td->max_bs * max_units + MASK;
1082 if (td->mem_type == MEM_MALLOC)
1083 td->orig_buffer = malloc(mem_size);
1084 else if (td->mem_type == MEM_SHM) {
1085 td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
1086 if (td->shm_id < 0) {
1092 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1093 if (td->orig_buffer == (void *) -1) {
1100 INIT_LIST_HEAD(&td->io_u_freelist);
1101 INIT_LIST_HEAD(&td->io_u_busylist);
1103 p = ALIGN(td->orig_buffer);
1104 for (i = 0; i < max_units; i++) {
1105 io_u = malloc(sizeof(*io_u));
1106 memset(io_u, 0, sizeof(*io_u));
1107 INIT_LIST_HEAD(&io_u->list);
1109 io_u->buf = p + td->max_bs * i;
1110 list_add(&io_u->list, &td->io_u_freelist);
1116 static void setup_log(struct io_log **log)
1118 struct io_log *l = malloc(sizeof(*l));
1121 l->max_samples = 1024;
1122 l->log = malloc(l->max_samples * sizeof(struct io_sample));
1126 static void finish_log(struct thread_data *td, struct io_log *log, char *name)
1128 char file_name[128];
1132 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
1133 f = fopen(file_name, "w");
1135 perror("fopen log");
1139 for (i = 0; i < log->nr_samples; i++)
1140 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
1147 static int create_file(struct thread_data *td)
1149 unsigned long long left;
1154 * unless specifically asked for overwrite, let normal io extend it
1156 if (!td_read(td) && !td->overwrite)
1159 if (!td->file_size) {
1160 fprintf(stderr, "Need size for create\n");
1165 printf("Client%d: Laying out IO file\n", td->thread_number);
1167 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1173 td->kb = td->file_size >> 10;
1174 b = malloc(td->max_bs);
1175 memset(b, 0, td->max_bs);
1177 left = td->file_size;
1183 r = write(td->fd, b, bs);
1198 if (td->create_fsync)
1207 static int file_exists(struct thread_data *td)
1211 if (stat(td->file_name, &st) != -1)
1214 return errno != ENOENT;
1217 static int setup_file(struct thread_data *td)
1222 if (!file_exists(td)) {
1223 if (!td->create_file) {
1227 if (create_file(td))
1235 td->fd = open(td->file_name, flags | O_RDONLY);
1246 td->fd = open(td->file_name, flags | O_CREAT, 0600);
1255 if (fstat(td->fd, &st) == -1) {
1260 if (td->file_size > st.st_size)
1261 st.st_size = td->file_size;
1264 td->file_size = 1024 * 1024 * 1024;
1266 st.st_size = td->file_size;
1269 td->kb = (st.st_size - td->file_offset) / 1024;
1271 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1276 if (td->invalidate_cache) {
1277 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
1286 static void *thread_main(int shm_id, int offset, char *argv[])
1288 struct thread_data *td;
1294 data = shmat(shm_id, NULL, 0);
1295 if (data == (void *) -1) {
1300 td = data + offset * sizeof(struct thread_data);
1306 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1311 sprintf(argv[0], "fio%d", offset);
1313 if (td->use_aio && init_aio(td))
1316 if (init_random_state(td))
1320 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1326 sem_post(&startup_sem);
1327 sem_wait(&td->mutex);
1329 if (!td->create_serialize && setup_file(td))
1332 gettimeofday(&td->start, NULL);
1335 memcpy(&td->lastrate, &td->start, sizeof(td->start));
1337 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1339 while (td->loops--) {
1344 if (!do_sync_verify(td))
1351 if (!do_async_verify(td))
1357 td->runtime = mtime_since_now(&td->start);
1361 finish_log(td, td->bw_log, "bw");
1363 finish_log(td, td->lat_log, "lat");
1365 if (exitall_on_terminate)
1377 sem_post(&startup_sem);
1378 sem_wait(&td->mutex);
1380 td->runstate = TD_EXITED;
1385 static void free_shm(void)
1387 struct shmid_ds sbuf;
1392 shmctl(shm_id, IPC_RMID, &sbuf);
1396 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1397 double *mean, double *dev)
1401 if (is->samples == 0)
1407 n = (double) is->samples;
1408 *mean = (double) is->val / n;
1409 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1413 static void show_thread_status(struct thread_data *td)
1415 int prio, prio_class;
1416 unsigned long min, max, bw = 0;
1419 if (!td->io_kb && !td->error)
1423 bw = td->io_kb * 1024 / td->runtime;
1425 prio = td->ioprio & 0xff;
1426 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1428 printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", td->thread_number, td->error, td->io_kb >> 10, bw, td->runtime);
1430 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1431 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1432 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1433 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1434 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1435 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1438 static int setup_rate(struct thread_data *td)
1440 int nr_reads_per_sec;
1445 if (td->rate < td->ratemin) {
1446 fprintf(stderr, "min rate larger than nominal rate\n");
1450 nr_reads_per_sec = td->rate * 1024 / td->min_bs;
1451 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1452 td->rate_pending_usleep = 0;
1456 static struct thread_data *get_new_job(int global)
1458 struct thread_data *td;
1462 if (thread_number >= max_jobs)
1465 td = &threads[thread_number++];
1466 memset(td, 0, sizeof(*td));
1469 td->thread_number = thread_number;
1471 td->ddir = def_thread.ddir;
1472 td->ioprio = def_thread.ioprio;
1473 td->sequential = def_thread.sequential;
1474 td->bs = def_thread.bs;
1475 td->min_bs = def_thread.min_bs;
1476 td->max_bs = def_thread.max_bs;
1477 td->odirect = def_thread.odirect;
1478 td->thinktime = def_thread.thinktime;
1479 td->fsync_blocks = def_thread.fsync_blocks;
1480 td->start_delay = def_thread.start_delay;
1481 td->timeout = def_thread.timeout;
1482 td->use_aio = def_thread.use_aio;
1483 td->create_file = def_thread.create_file;
1484 td->overwrite = def_thread.overwrite;
1485 td->invalidate_cache = def_thread.invalidate_cache;
1486 td->file_size = def_thread.file_size;
1487 td->file_offset = def_thread.file_offset;
1488 td->rate = def_thread.rate;
1489 td->ratemin = def_thread.ratemin;
1490 td->ratecycle = def_thread.ratecycle;
1491 td->aio_depth = def_thread.aio_depth;
1492 td->sync_io = def_thread.sync_io;
1493 td->mem_type = def_thread.mem_type;
1494 td->bw_avg_time = def_thread.bw_avg_time;
1495 td->create_serialize = def_thread.create_serialize;
1496 td->create_fsync = def_thread.create_fsync;
1497 td->loops = def_thread.loops;
1498 td->verify = def_thread.verify;
1499 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
1504 static void put_job(struct thread_data *td)
1506 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1510 static int add_job(struct thread_data *td, const char *filename, int prioclass,
1513 if (td == &def_thread)
1516 strcpy(td->file_name, filename);
1517 sem_init(&td->mutex, 1, 0);
1518 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1520 td->clat_stat.min_val = ULONG_MAX;
1521 td->slat_stat.min_val = ULONG_MAX;
1522 td->bw_stat.min_val = ULONG_MAX;
1524 run_str[td->thread_number - 1] = 'P';
1526 if (td->use_aio && !td->aio_depth)
1529 if (td->min_bs == -1)
1530 td->min_bs = td->bs;
1531 if (td->max_bs == -1)
1532 td->max_bs = td->bs;
1538 setup_log(&td->lat_log);
1540 setup_log(&td->bw_log);
1542 printf("Client%d: file=%s, rw=%d, prio=%d/%d, seq=%d, odir=%d, bs=%d-%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, prioclass, prio, td->sequential, td->odirect, td->min_bs, td->max_bs, td->rate, td->use_aio, td->aio_depth);
1546 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1552 for (i = 0; i < sizeof(int) * 8; i++) {
1554 CPU_SET(i, &cpumask);
1558 unsigned long get_mult(char c)
1569 return 1024 * 1024 * 1024;
1576 * convert string after '=' into decimal value, noting any size suffix
1578 static int str_cnv(char *p, unsigned long long *val)
1583 str = strstr(p, "=");
1590 *val = strtoul(str, NULL, 10);
1591 if (*val == ULONG_MAX && errno == ERANGE)
1594 *val *= get_mult(str[len - 2]);
1598 static int check_strcnv(char *p, char *name, unsigned long long *val)
1600 if (!strstr(p, name))
1603 return str_cnv(p, val);
1606 static int check_str(char *p, char *name, char *option)
1608 char *s = strstr(p, name);
1614 if (strstr(s, option))
1620 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
1625 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
1626 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1632 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
1633 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1639 sprintf(str, "%s=%%lu-%%lu", name);
1640 if (sscanf(p, str, s, e) == 2)
1643 sprintf(str, "%s = %%lu-%%lu", name);
1644 if (sscanf(p, str, s, e) == 2)
1651 static int check_int(char *p, char *name, unsigned int *val)
1655 sprintf(str, "%s=%%d", name);
1656 if (sscanf(p, str, val) == 1)
1659 sprintf(str, "%s = %%d", name);
1660 if (sscanf(p, str, val) == 1)
1666 static int is_empty_or_comment(char *line)
1670 for (i = 0; i < strlen(line); i++) {
1673 if (!isspace(line[i]) && !iscntrl(line[i]))
1680 static int parse_jobs_ini(char *file)
1682 unsigned int prioclass, prio, cpu, global;
1683 unsigned long long ull;
1684 unsigned long ul1, ul2;
1685 struct thread_data *td;
1686 char *string, *name;
1691 f = fopen(file, "r");
1697 string = malloc(4096);
1700 while ((p = fgets(string, 4096, f)) != NULL) {
1701 if (is_empty_or_comment(p))
1703 if (sscanf(p, "[%s]", name) != 1)
1706 global = !strncmp(name, "global", 6);
1708 name[strlen(name) - 1] = '\0';
1710 td = get_new_job(global);
1718 while ((p = fgets(string, 4096, f)) != NULL) {
1719 if (is_empty_or_comment(p))
1723 if (!check_int(p, "rw", &td->ddir)) {
1727 if (!check_int(p, "prio", &prio)) {
1731 if (!check_int(p, "prioclass", &prioclass)) {
1735 if (!check_int(p, "direct", &td->odirect)) {
1739 if (!check_int(p, "rate", &td->rate)) {
1743 if (!check_int(p, "ratemin", &td->ratemin)) {
1747 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1751 if (!check_int(p, "thinktime", &td->thinktime)) {
1755 if (!check_int(p, "cpumask", &cpu)) {
1756 fill_cpu_mask(td->cpumask, cpu);
1760 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1764 if (!check_int(p, "startdelay", &td->start_delay)) {
1768 if (!check_int(p, "timeout", &td->timeout)) {
1772 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1776 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1780 if (!check_int(p, "sync", &td->sync_io)) {
1784 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
1788 if (!check_int(p, "create_serialize", &td->create_serialize)) {
1792 if (!check_int(p, "create_fsync", &td->create_fsync)) {
1796 if (!check_int(p, "loops", &td->loops)) {
1800 if (!check_int(p, "verify", &td->verify)) {
1804 if (!check_range(p, "bsrange", &ul1, &ul2)) {
1810 if (!check_strcnv(p, "bs", &ull)) {
1815 if (!check_strcnv(p, "size", &td->file_size)) {
1819 if (!check_strcnv(p, "offset", &td->file_offset)) {
1823 if (!check_str(p, "mem", "malloc")) {
1824 td->mem_type = MEM_MALLOC;
1828 if (!check_str(p, "mem", "shm")) {
1829 td->mem_type = MEM_SHM;
1833 if (!strncmp(p, "sequential", 10)) {
1838 if (!strncmp(p, "random", 6)) {
1843 if (!strncmp(p, "aio", 3)) {
1848 if (!strncmp(p, "create", 6)) {
1849 td->create_file = 1;
1853 if (!strncmp(p, "overwrite", 9)) {
1858 if (!strncmp(p, "exitall", 7)) {
1859 exitall_on_terminate = 1;
1863 printf("Client%d: bad option %s\n",td->thread_number,p);
1867 if (add_job(td, name, prioclass, prio))
1877 static int parse_options(int argc, char *argv[])
1881 for (i = 1; i < argc; i++) {
1882 char *parm = argv[i];
1891 def_thread.sequential = !!atoi(parm);
1895 def_thread.bs = atoi(parm);
1896 def_thread.bs <<= 10;
1897 if (!def_thread.bs) {
1898 printf("bad block size\n");
1899 def_thread.bs = DEF_BS;
1904 def_thread.timeout = atoi(parm);
1908 repeatable = !!atoi(parm);
1912 rate_quit = !!atoi(parm);
1916 def_thread.odirect = !!atoi(parm);
1919 if (i + 1 >= argc) {
1920 printf("-f needs file as arg\n");
1923 ini_file = strdup(argv[i+1]);
1933 printf("bad option %s\n", argv[i]);
1941 static void print_thread_status(struct thread_data *td, int nr_running,
1942 int t_rate, int m_rate)
1944 printf("Threads now running: %d", nr_running);
1945 if (m_rate || t_rate)
1946 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
1947 printf(" : [%s]\r", run_str);
1951 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1956 * reap exited threads (TD_EXITED -> TD_REAPED)
1958 for (i = 0; i < thread_number; i++) {
1959 struct thread_data *td = &threads[i];
1961 if (td->runstate != TD_EXITED)
1964 td->runstate = TD_REAPED;
1965 run_str[td->thread_number - 1] = '_';
1966 waitpid(td->pid, NULL, 0);
1968 (*m_rate) -= td->ratemin;
1969 (*t_rate) -= td->rate;
1974 print_thread_status(td, *nr_running, *t_rate, *m_rate);
1978 static void run_threads(char *argv[])
1980 struct timeval genesis;
1981 struct thread_data *td;
1982 unsigned long spent;
1983 int i, todo, nr_running, m_rate, t_rate;
1985 printf("Starting %d threads\n", thread_number);
1988 signal(SIGINT, sig_handler);
1990 todo = thread_number;
1992 m_rate = t_rate = 0;
1994 for (i = 0; i < thread_number; i++) {
1997 if (!td->create_serialize)
2001 * do file setup here so it happens sequentially,
2002 * we don't want X number of threads getting their
2003 * client data interspersed on disk
2005 if (setup_file(td)) {
2006 td->runstate = TD_REAPED;
2011 gettimeofday(&genesis, NULL);
2015 * create threads (TD_NOT_CREATED -> TD_CREATED)
2017 for (i = 0; i < thread_number; i++) {
2020 if (td->runstate != TD_NOT_CREATED)
2024 * never got a chance to start, killed by other
2025 * thread for some reason
2027 if (td->terminate) {
2032 if (td->start_delay) {
2033 spent = mtime_since_now(&genesis);
2035 if (td->start_delay * 1000 > spent)
2039 td->runstate = TD_CREATED;
2040 run_str[td->thread_number - 1] = 'C';
2041 sem_init(&startup_sem, 1, 1);
2045 sem_wait(&startup_sem);
2047 thread_main(shm_id, i, argv);
2053 * start created threads (TD_CREATED -> TD_RUNNING)
2055 for (i = 0; i < thread_number; i++) {
2056 struct thread_data *td = &threads[i];
2058 if (td->runstate != TD_CREATED)
2061 td->runstate = TD_RUNNING;
2062 run_str[td->thread_number - 1] = '+';
2064 m_rate += td->ratemin;
2066 sem_post(&td->mutex);
2068 print_thread_status(td, nr_running, t_rate, m_rate);
2071 for (i = 0; i < thread_number; i++) {
2072 struct thread_data *td = &threads[i];
2074 if (td->runstate == TD_RUNNING)
2075 run_str[td->thread_number - 1] = '+';
2076 else if (td->runstate == TD_VERIFYING)
2077 run_str[td->thread_number - 1] = 'V';
2081 print_thread_status(td, nr_running, t_rate, m_rate);
2084 reap_threads(&nr_running, &t_rate, &m_rate);
2090 while (nr_running) {
2091 reap_threads(&nr_running, &t_rate, &m_rate);
2096 int setup_thread_area(void)
2099 * 1024 is too much on some machines, scale max_jobs if
2100 * we get a failure that looks like too large a shm segment
2103 int s = max_jobs * sizeof(struct thread_data);
2105 shm_id = shmget(0, s, IPC_CREAT | 0600);
2108 if (errno != EINVAL) {
2119 threads = shmat(shm_id, NULL, 0);
2120 if (threads == (void *) -1) {
2129 int main(int argc, char *argv[])
2131 static unsigned long max_run[2], min_run[2];
2132 static unsigned long max_bw[2], min_bw[2];
2133 static unsigned long io_mb[2], agg[2];
2136 if (setup_thread_area())
2139 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
2140 perror("sched_getaffinity");
2147 def_thread.ddir = DDIR_READ;
2148 def_thread.bs = DEF_BS;
2149 def_thread.min_bs = -1;
2150 def_thread.max_bs = -1;
2151 def_thread.odirect = DEF_ODIRECT;
2152 def_thread.ratecycle = DEF_RATE_CYCLE;
2153 def_thread.sequential = DEF_SEQUENTIAL;
2154 def_thread.timeout = DEF_TIMEOUT;
2155 def_thread.create_file = DEF_CREATE;
2156 def_thread.overwrite = DEF_OVERWRITE;
2157 def_thread.invalidate_cache = DEF_INVALIDATE;
2158 def_thread.sync_io = DEF_SYNCIO;
2159 def_thread.mem_type = MEM_MALLOC;
2160 def_thread.bw_avg_time = DEF_BWAVGTIME;
2161 def_thread.create_serialize = DEF_CREATE_SER;
2162 def_thread.create_fsync = DEF_CREATE_FSYNC;
2163 def_thread.loops = DEF_LOOPS;
2164 def_thread.verify = DEF_VERIFY;
2166 i = parse_options(argc, argv);
2169 printf("Need job file\n");
2173 if (parse_jobs_ini(ini_file))
2176 if (!thread_number) {
2177 printf("Nothing to do\n");
2183 min_bw[0] = min_run[0] = ~0UL;
2184 min_bw[1] = min_run[1] = ~0UL;
2185 io_mb[0] = io_mb[1] = 0;
2186 agg[0] = agg[1] = 0;
2187 for (i = 0; i < thread_number; i++) {
2188 struct thread_data *td = &threads[i];
2189 unsigned long bw = 0;
2192 if (td->runtime < min_run[td->ddir])
2193 min_run[td->ddir] = td->runtime;
2194 if (td->runtime > max_run[td->ddir])
2195 max_run[td->ddir] = td->runtime;
2198 bw = td->io_kb * 1024 / td->runtime;
2199 if (bw < min_bw[td->ddir])
2200 min_bw[td->ddir] = bw;
2201 if (bw > max_bw[td->ddir])
2202 max_bw[td->ddir] = bw;
2204 io_mb[td->ddir] += td->io_kb >> 10;
2207 show_thread_status(td);
2211 agg[0] = io_mb[0] * 1024 * 1000 / max_run[0];
2213 agg[1] = io_mb[1] * 1024 * 1000 / max_run[1];
2215 printf("\nRun status:\n");
2216 if (max_run[DDIR_READ])
2217 printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[0], agg[0], min_bw[0], max_bw[0], min_run[0], max_run[0]);
2218 if (max_run[DDIR_WRITE])
2219 printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[1], agg[1], min_bw[1], max_bw[1], min_run[1], max_run[1]);