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;
231 volatile int old_runstate;
234 unsigned int sequential;
238 unsigned int odirect;
239 unsigned int thinktime;
240 unsigned int fsync_blocks;
241 unsigned int start_delay;
242 unsigned int timeout;
243 unsigned int use_aio;
244 unsigned int create_file;
245 unsigned int overwrite;
246 unsigned int invalidate_cache;
247 unsigned int bw_avg_time;
248 unsigned int create_serialize;
249 unsigned int create_fsync;
251 unsigned long long file_size;
252 unsigned long long file_offset;
253 unsigned int sync_io;
254 unsigned int mem_type;
258 struct drand48_data bsrange_state;
259 struct drand48_data verify_state;
265 io_context_t aio_ctx;
266 unsigned int aio_depth;
267 struct io_event *aio_events;
269 unsigned int cur_depth;
270 struct list_head io_u_freelist;
271 struct list_head io_u_busylist;
274 unsigned int ratemin;
275 unsigned int ratecycle;
276 unsigned long rate_usec_cycle;
277 long rate_pending_usleep;
278 unsigned long rate_kb;
279 struct timeval lastrate;
281 unsigned long runtime; /* sec */
283 unsigned long io_blocks;
285 unsigned long this_io_kb;
286 unsigned long last_kb;
288 struct drand48_data random_state;
291 * bandwidth and latency stats
293 struct io_stat clat_stat; /* completion latency */
294 struct io_stat slat_stat; /* submission latency */
296 struct io_stat bw_stat; /* bandwidth stats */
297 unsigned long stat_io_kb;
298 struct timeval stat_sample_time;
300 struct io_log *lat_log;
301 struct io_log *bw_log;
303 struct timeval start;
306 static struct thread_data *threads;
307 static struct thread_data def_thread;
309 static sem_t startup_sem;
311 static void sig_handler(int sig)
315 for (i = 0; i < thread_number; i++) {
316 struct thread_data *td = &threads[i];
323 static int init_random_state(struct thread_data *td)
328 fd = open("/dev/random", O_RDONLY);
334 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
342 srand48_r(seed, &td->bsrange_state);
343 srand48_r(seed, &td->verify_state);
351 srand48_r(seed, &td->random_state);
355 static unsigned long utime_since(struct timeval *s, struct timeval *e)
359 sec = e->tv_sec - s->tv_sec;
360 usec = e->tv_usec - s->tv_usec;
361 if (sec > 0 && usec < 0) {
366 sec *= (double) 1000000;
371 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
375 sec = e->tv_sec - s->tv_sec;
376 usec = e->tv_usec - s->tv_usec;
377 if (sec > 0 && usec < 0) {
382 sec *= (double) 1000;
383 usec /= (double) 1000;
388 static unsigned long mtime_since_now(struct timeval *s)
392 gettimeofday(&t, NULL);
393 return mtime_since(s, &t);
396 static inline unsigned long msec_now(struct timeval *s)
398 return s->tv_sec * 1000 + s->tv_usec / 1000;
401 static unsigned long long get_next_offset(struct thread_data *td)
403 unsigned long long kb;
406 if (!td->sequential) {
407 drand48_r(&td->random_state, &r);
408 kb = (1+(double) (td->kb-1) * r / (RAND_MAX+1.0));
412 return (kb << 10) + td->file_offset;
415 static unsigned int get_next_buflen(struct thread_data *td)
420 if (td->min_bs == td->max_bs)
423 drand48_r(&td->bsrange_state, &r);
424 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
425 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
428 if (buflen > ((td->kb - td->this_io_kb) << 10))
429 buflen = (td->kb - td->this_io_kb) << 10;
431 td->last_kb += buflen >> 10;
435 static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
438 if (val > is->max_val)
440 if (val < is->min_val)
444 is->val_sq += val * val;
448 static void add_log_sample(struct thread_data *td, struct io_log *log,
451 if (log->nr_samples == log->max_samples) {
452 int new_size = sizeof(struct io_sample) * log->max_samples * 2;
454 log->log = realloc(log->log, new_size);
455 log->max_samples <<= 1;
458 log->log[log->nr_samples].val = val;
459 log->log[log->nr_samples].time = mtime_since_now(&td->start);
463 static void add_clat_sample(struct thread_data *td, unsigned long msec)
465 add_stat_sample(td, &td->clat_stat, msec);
468 add_log_sample(td, td->lat_log, msec);
471 static void add_slat_sample(struct thread_data *td, unsigned long msec)
473 add_stat_sample(td, &td->slat_stat, msec);
476 static void add_bw_sample(struct thread_data *td, unsigned long msec)
478 unsigned long spent = mtime_since_now(&td->stat_sample_time);
481 if (spent < td->bw_avg_time)
484 rate = ((td->this_io_kb - td->stat_io_kb) * 1024) / spent;
485 add_stat_sample(td, &td->bw_stat, rate);
488 add_log_sample(td, td->bw_log, rate);
490 gettimeofday(&td->stat_sample_time, NULL);
491 td->stat_io_kb = td->this_io_kb;
494 static void usec_sleep(int usec)
496 struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
500 rem.tv_sec = rem.tv_nsec = 0;
501 nanosleep(&req, &rem);
505 req.tv_nsec = rem.tv_nsec;
509 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
512 unsigned long usec_cycle;
517 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
519 if (time_spent < usec_cycle) {
520 unsigned long s = usec_cycle - time_spent;
522 td->rate_pending_usleep += s;
523 if (td->rate_pending_usleep >= 100000) {
524 usec_sleep(td->rate_pending_usleep);
525 td->rate_pending_usleep = 0;
528 long overtime = time_spent - usec_cycle;
530 td->rate_pending_usleep -= overtime;
534 static int check_min_rate(struct thread_data *td, struct timeval *now)
540 * allow a 2 second settle period in the beginning
542 if (mtime_since(&td->start, now) < 2000)
546 * if rate blocks is set, sample is running
549 spent = mtime_since(&td->lastrate, now);
550 if (spent < td->ratecycle)
553 rate = ((td->this_io_kb - td->rate_kb) * 1024) / spent;
554 if (rate < td->ratemin) {
555 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
562 td->rate_kb = td->this_io_kb;
563 memcpy(&td->lastrate, now, sizeof(*now));
567 static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
569 if (mtime_since(&td->start, t) >= td->timeout * 1000)
575 static void fill_random_bytes(struct thread_data *td,
576 unsigned char *p, unsigned int len)
582 drand48_r(&td->verify_state, &r);
585 * lrand48_r seems to be broken and only fill the bottom
586 * 32-bits, even on 64-bit archs with 64-bit longs
599 static int verify_io_u(struct thread_data *td, struct io_u *io_u)
601 struct verify_header *hdr = (struct verify_header *) io_u->buf;
602 unsigned char *p = (unsigned char *) io_u->buf;
603 struct md5_ctx md5_ctx;
605 if (hdr->fio_magic != FIO_HDR_MAGIC)
608 memset(&md5_ctx, 0, sizeof(md5_ctx));
610 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
612 return memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
615 static int verify_io_us(struct thread_data *td, struct io_u *io_u, int *back)
617 struct verify_header *hdr;
630 hdr = (struct verify_header *) buf;
634 if (hdr->len > left) {
640 if (verify_io_u(td, &i)) {
641 printf("failed verify at offset %lu\n", (unsigned long) off);
655 * fill body of io_u->buf with random data and add a header with the
656 * (eg) sha1sum of that data.
658 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
660 struct md5_ctx md5_ctx;
661 struct verify_header hdr;
662 unsigned char *p = (unsigned char *) io_u->buf;
664 hdr.fio_magic = FIO_HDR_MAGIC;
665 hdr.len = io_u->buflen;
667 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
669 memset(&md5_ctx, 0, sizeof(md5_ctx));
670 md5_update(&md5_ctx, p, io_u->buflen - sizeof(hdr));
671 memcpy(hdr.md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
672 memcpy(io_u->buf, &hdr, sizeof(hdr));
675 static void put_io_u(struct thread_data *td, struct io_u *io_u)
677 list_del(&io_u->list);
678 list_add(&io_u->list, &td->io_u_freelist);
682 #define queue_full(td) (list_empty(&(td)->io_u_freelist))
684 static struct io_u *__get_io_u(struct thread_data *td)
691 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
692 list_del(&io_u->list);
693 list_add(&io_u->list, &td->io_u_busylist);
698 static struct io_u *get_io_u(struct thread_data *td)
702 io_u = __get_io_u(td);
706 io_u->offset = get_next_offset(td);
707 io_u->buflen = get_next_buflen(td);
713 if (!td_read(td) && td->verify)
714 populate_io_u(td, io_u);
718 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
720 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
723 gettimeofday(&io_u->start_time, NULL);
727 static inline void td_set_runstate(struct thread_data *td, int runstate)
729 td->old_runstate = td->runstate;
730 td->runstate = runstate;
733 static int do_sync_verify(struct thread_data *td)
736 struct io_u *io_u = NULL;
740 td_set_runstate(td, TD_VERIFYING);
742 io_u = __get_io_u(td);
744 if (lseek(td->fd, 0, SEEK_SET) < 0) {
750 unsigned long size = td->kb << 10;
752 if (fadvise(td->fd, 0, size, POSIX_FADV_DONTNEED) < 0) {
762 gettimeofday(&t, NULL);
763 if (runtime_exceeded(td, &t))
767 io_u->buflen = td->max_bs;
769 ret = read(td->fd, io_u->buf, io_u->buflen);
770 if (ret < (int) io_u->buflen) {
780 if (verify_io_us(td, io_u, &back))
785 if (lseek(td->fd, -back, SEEK_CUR) < 0) {
795 td_set_runstate(td, TD_RUNNING);
798 return td->error == 0;
801 static void do_sync_io(struct thread_data *td)
803 unsigned long msec, usec;
804 struct io_u *io_u = NULL;
810 for (td->this_io_kb = 0; td->this_io_kb < td->kb;) {
820 if (td->cur_off != io_u->offset) {
821 if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
828 ret = read(td->fd, io_u->buf, io_u->buflen);
830 ret = write(td->fd, io_u->buf, io_u->buflen);
832 if (ret < (int) io_u->buflen) {
839 td->io_kb += io_u->buflen >> 10;
840 td->this_io_kb += io_u->buflen >> 10;
841 td->cur_off = io_u->offset + io_u->buflen;
843 gettimeofday(&e, NULL);
845 usec = utime_since(&io_u->start_time, &e);
847 rate_throttle(td, usec, io_u->buflen);
849 if (check_min_rate(td, &e)) {
855 add_clat_sample(td, msec);
856 add_bw_sample(td, msec);
858 if (runtime_exceeded(td, &e))
865 usec_sleep(td->thinktime);
867 if (should_fsync(td) && td->fsync_blocks &&
868 (td->io_blocks % td->fsync_blocks) == 0)
875 if (should_fsync(td))
879 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
881 struct iocb *iocb = &io_u->iocb;
885 ret = io_submit(td->aio_ctx, 1, &iocb);
888 else if (ret == EAGAIN)
890 else if (ret == EINTR)
899 #define iocb_time(iocb) ((unsigned long) (iocb)->data)
900 #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
902 static int ios_completed(struct thread_data *td, int nr)
909 gettimeofday(&e, NULL);
911 for (i = 0, bytes_done = 0; i < nr; i++) {
912 io_u = ev_to_iou(td->aio_events + i);
915 td->io_kb += io_u->buflen >> 10;
916 td->this_io_kb += io_u->buflen >> 10;
918 msec = mtime_since(&io_u->issue_time, &e);
920 add_clat_sample(td, msec);
921 add_bw_sample(td, msec);
923 bytes_done += io_u->buflen;
930 static void cleanup_pending_aio(struct thread_data *td)
932 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
933 struct list_head *entry, *n;
938 * get immediately available events, if any
940 r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts);
942 ios_completed(td, r);
945 * now cancel remaining active events
947 list_for_each_safe(entry, n, &td->io_u_busylist) {
948 io_u = list_entry(entry, struct io_u, list);
950 r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
956 r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL);
958 ios_completed(td, r);
962 static int do_async_verify(struct thread_data *td)
965 struct io_u *io_u, *v_io_u;
966 struct verify_header *hdr;
970 td_set_runstate(td, TD_VERIFYING);
980 gettimeofday(&t, NULL);
981 if (runtime_exceeded(td, &t))
984 io_u = __get_io_u(td);
988 io_u->offset = td->cur_off;
989 io_u->buflen = td->max_bs;
991 if (io_u->offset + io_u->buflen > (td->kb << 10)) {
992 io_u->buflen = (td->kb << 10) - io_u->offset;
999 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
1000 ret = io_u_queue(td, io_u);
1008 * we have one pending to verify, do that while the next
1009 * we are doing io on the next one
1012 ret = verify_io_us(td, v_io_u, NULL);
1013 put_io_u(td, v_io_u);
1019 ret = io_getevents(td->aio_ctx, 1, 1, td->aio_events, NULL);
1027 * got our io_u to verify, find back offset so we can
1028 * submit the next one before verifying this one
1030 v_io_u = ev_to_iou(td->aio_events);
1032 back = v_io_u->buflen;
1034 hdr = (struct verify_header *) p;
1036 if (hdr->len > back)
1043 td->cur_off += (v_io_u->buflen - back);
1046 * if max depth is 1, we need to verify now
1048 if (queue_full(td)) {
1049 ret = verify_io_us(td, v_io_u, NULL);
1050 put_io_u(td, v_io_u);
1058 verify_io_us(td, v_io_u, NULL);
1059 put_io_u(td, v_io_u);
1063 cleanup_pending_aio(td);
1065 td_set_runstate(td, TD_RUNNING);
1066 return td->error == 0;
1069 static void do_async_io(struct thread_data *td)
1071 struct timeval s, e;
1077 for (td->this_io_kb = 0; td->this_io_kb < td->kb;) {
1078 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1079 struct timespec *timeout;
1080 int ret, min_evts = 0;
1082 unsigned int bytes_done;
1087 io_u = get_io_u(td);
1091 memcpy(&s, &io_u->start_time, sizeof(s));
1093 ret = io_u_queue(td, io_u);
1100 gettimeofday(&io_u->issue_time, NULL);
1101 add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time));
1102 if (td->cur_depth < td->aio_depth) {
1110 ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout);
1117 bytes_done = ios_completed(td, ret);
1120 * the rate is batched for now, it should work for batches
1121 * of completions except the very first one which may look
1124 gettimeofday(&e, NULL);
1125 usec = utime_since(&s, &e);
1127 rate_throttle(td, usec, bytes_done);
1129 if (check_min_rate(td, &e)) {
1130 td->error = ENODATA;
1134 if (runtime_exceeded(td, &e))
1138 usec_sleep(td->thinktime);
1140 if (should_fsync(td) && td->fsync_blocks &&
1141 (td->io_blocks % td->fsync_blocks) == 0)
1146 cleanup_pending_aio(td);
1148 if (should_fsync(td))
1152 static void cleanup_aio(struct thread_data *td)
1154 io_destroy(td->aio_ctx);
1157 free(td->aio_events);
1160 static int init_aio(struct thread_data *td)
1162 if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
1167 td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
1171 static void cleanup_io_u(struct thread_data *td)
1173 struct list_head *entry, *n;
1176 list_for_each_safe(entry, n, &td->io_u_freelist) {
1177 io_u = list_entry(entry, struct io_u, list);
1179 list_del(&io_u->list);
1183 if (td->mem_type == MEM_MALLOC)
1184 free(td->orig_buffer);
1185 else if (td->mem_type == MEM_SHM) {
1186 struct shmid_ds sbuf;
1188 shmdt(td->orig_buffer);
1189 shmctl(td->shm_id, IPC_RMID, &sbuf);
1193 static int init_io_u(struct thread_data *td)
1196 int i, max_units, mem_size;
1202 max_units = td->aio_depth;
1204 mem_size = td->max_bs * max_units + MASK;
1206 if (td->mem_type == MEM_MALLOC)
1207 td->orig_buffer = malloc(mem_size);
1208 else if (td->mem_type == MEM_SHM) {
1209 td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
1210 if (td->shm_id < 0) {
1216 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1217 if (td->orig_buffer == (void *) -1) {
1224 INIT_LIST_HEAD(&td->io_u_freelist);
1225 INIT_LIST_HEAD(&td->io_u_busylist);
1227 p = ALIGN(td->orig_buffer);
1228 for (i = 0; i < max_units; i++) {
1229 io_u = malloc(sizeof(*io_u));
1230 memset(io_u, 0, sizeof(*io_u));
1231 INIT_LIST_HEAD(&io_u->list);
1233 io_u->buf = p + td->max_bs * i;
1234 list_add(&io_u->list, &td->io_u_freelist);
1240 static void setup_log(struct io_log **log)
1242 struct io_log *l = malloc(sizeof(*l));
1245 l->max_samples = 1024;
1246 l->log = malloc(l->max_samples * sizeof(struct io_sample));
1250 static void finish_log(struct thread_data *td, struct io_log *log, char *name)
1252 char file_name[128];
1256 sprintf(file_name, "client%d_%s.log", td->thread_number, name);
1257 f = fopen(file_name, "w");
1259 perror("fopen log");
1263 for (i = 0; i < log->nr_samples; i++)
1264 fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val);
1271 static int create_file(struct thread_data *td)
1273 unsigned long long left;
1278 * unless specifically asked for overwrite, let normal io extend it
1280 if (!td_read(td) && !td->overwrite)
1283 if (!td->file_size) {
1284 fprintf(stderr, "Need size for create\n");
1289 printf("Client%d: Laying out IO file\n", td->thread_number);
1291 td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1297 td->kb = td->file_size >> 10;
1298 b = malloc(td->max_bs);
1299 memset(b, 0, td->max_bs);
1301 left = td->file_size;
1307 r = write(td->fd, b, bs);
1322 if (td->create_fsync)
1331 static int file_exists(struct thread_data *td)
1335 if (stat(td->file_name, &st) != -1)
1338 return errno != ENOENT;
1341 static int setup_file(struct thread_data *td)
1346 if (!file_exists(td)) {
1347 if (!td->create_file) {
1351 if (create_file(td))
1359 td->fd = open(td->file_name, flags | O_RDONLY);
1370 td->fd = open(td->file_name, flags | O_CREAT, 0600);
1379 if (fstat(td->fd, &st) == -1) {
1384 if (td->file_size > st.st_size)
1385 st.st_size = td->file_size;
1388 td->file_size = 1024 * 1024 * 1024;
1390 st.st_size = td->file_size;
1393 td->kb = (st.st_size - td->file_offset) / 1024;
1395 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1400 if (td->invalidate_cache) {
1401 if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) {
1410 static void *thread_main(int shm_id, int offset, char *argv[])
1412 struct thread_data *td;
1418 data = shmat(shm_id, NULL, 0);
1419 if (data == (void *) -1) {
1424 td = data + offset * sizeof(struct thread_data);
1430 if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
1435 sprintf(argv[0], "fio%d", offset);
1437 if (td->use_aio && init_aio(td))
1440 if (init_random_state(td))
1444 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1450 sem_post(&startup_sem);
1451 sem_wait(&td->mutex);
1453 if (!td->create_serialize && setup_file(td))
1456 gettimeofday(&td->start, NULL);
1459 memcpy(&td->lastrate, &td->start, sizeof(td->start));
1461 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1463 while (td->loops--) {
1468 if (!do_sync_verify(td))
1475 if (!do_async_verify(td))
1481 td->runtime = mtime_since_now(&td->start);
1485 finish_log(td, td->bw_log, "bw");
1487 finish_log(td, td->lat_log, "lat");
1489 if (exitall_on_terminate)
1501 sem_post(&startup_sem);
1502 sem_wait(&td->mutex);
1504 td_set_runstate(td, TD_EXITED);
1509 static void free_shm(void)
1511 struct shmid_ds sbuf;
1516 shmctl(shm_id, IPC_RMID, &sbuf);
1520 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1521 double *mean, double *dev)
1525 if (is->samples == 0)
1531 n = (double) is->samples;
1532 *mean = (double) is->val / n;
1533 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1537 static void show_thread_status(struct thread_data *td)
1539 int prio, prio_class;
1540 unsigned long min, max, bw = 0;
1543 if (!td->io_kb && !td->error)
1547 bw = td->io_kb * 1024 / td->runtime;
1549 prio = td->ioprio & 0xff;
1550 prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT;
1552 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);
1554 if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev))
1555 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1556 if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
1557 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1558 if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
1559 printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
1562 static int setup_rate(struct thread_data *td)
1564 int nr_reads_per_sec;
1569 if (td->rate < td->ratemin) {
1570 fprintf(stderr, "min rate larger than nominal rate\n");
1574 nr_reads_per_sec = td->rate * 1024 / td->min_bs;
1575 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
1576 td->rate_pending_usleep = 0;
1580 static struct thread_data *get_new_job(int global)
1582 struct thread_data *td;
1586 if (thread_number >= max_jobs)
1589 td = &threads[thread_number++];
1590 memset(td, 0, sizeof(*td));
1593 td->thread_number = thread_number;
1595 td->ddir = def_thread.ddir;
1596 td->ioprio = def_thread.ioprio;
1597 td->sequential = def_thread.sequential;
1598 td->bs = def_thread.bs;
1599 td->min_bs = def_thread.min_bs;
1600 td->max_bs = def_thread.max_bs;
1601 td->odirect = def_thread.odirect;
1602 td->thinktime = def_thread.thinktime;
1603 td->fsync_blocks = def_thread.fsync_blocks;
1604 td->start_delay = def_thread.start_delay;
1605 td->timeout = def_thread.timeout;
1606 td->use_aio = def_thread.use_aio;
1607 td->create_file = def_thread.create_file;
1608 td->overwrite = def_thread.overwrite;
1609 td->invalidate_cache = def_thread.invalidate_cache;
1610 td->file_size = def_thread.file_size;
1611 td->file_offset = def_thread.file_offset;
1612 td->rate = def_thread.rate;
1613 td->ratemin = def_thread.ratemin;
1614 td->ratecycle = def_thread.ratecycle;
1615 td->aio_depth = def_thread.aio_depth;
1616 td->sync_io = def_thread.sync_io;
1617 td->mem_type = def_thread.mem_type;
1618 td->bw_avg_time = def_thread.bw_avg_time;
1619 td->create_serialize = def_thread.create_serialize;
1620 td->create_fsync = def_thread.create_fsync;
1621 td->loops = def_thread.loops;
1622 td->verify = def_thread.verify;
1623 memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask));
1628 static void put_job(struct thread_data *td)
1630 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
1634 static int add_job(struct thread_data *td, const char *filename, int prioclass,
1637 if (td == &def_thread)
1640 strcpy(td->file_name, filename);
1641 sem_init(&td->mutex, 1, 0);
1642 td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
1644 td->clat_stat.min_val = ULONG_MAX;
1645 td->slat_stat.min_val = ULONG_MAX;
1646 td->bw_stat.min_val = ULONG_MAX;
1648 run_str[td->thread_number - 1] = 'P';
1650 if (td->use_aio && !td->aio_depth)
1653 if (td->min_bs == -1)
1654 td->min_bs = td->bs;
1655 if (td->max_bs == -1)
1656 td->max_bs = td->bs;
1662 setup_log(&td->lat_log);
1664 setup_log(&td->bw_log);
1666 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);
1670 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
1676 for (i = 0; i < sizeof(int) * 8; i++) {
1678 CPU_SET(i, &cpumask);
1682 unsigned long get_mult(char c)
1693 return 1024 * 1024 * 1024;
1700 * convert string after '=' into decimal value, noting any size suffix
1702 static int str_cnv(char *p, unsigned long long *val)
1707 str = strstr(p, "=");
1714 *val = strtoul(str, NULL, 10);
1715 if (*val == ULONG_MAX && errno == ERANGE)
1718 *val *= get_mult(str[len - 2]);
1722 static int check_strcnv(char *p, char *name, unsigned long long *val)
1724 if (!strstr(p, name))
1727 return str_cnv(p, val);
1730 static int check_str(char *p, char *name, char *option)
1732 char *s = strstr(p, name);
1738 if (strstr(s, option))
1744 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
1749 sprintf(str, "%s=%%lu%%c-%%lu%%c", name);
1750 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1756 sprintf(str, "%s = %%lu%%c-%%lu%%c", name);
1757 if (sscanf(p, str, s, &s1, e, &s2) == 4) {
1763 sprintf(str, "%s=%%lu-%%lu", name);
1764 if (sscanf(p, str, s, e) == 2)
1767 sprintf(str, "%s = %%lu-%%lu", name);
1768 if (sscanf(p, str, s, e) == 2)
1775 static int check_int(char *p, char *name, unsigned int *val)
1779 sprintf(str, "%s=%%d", name);
1780 if (sscanf(p, str, val) == 1)
1783 sprintf(str, "%s = %%d", name);
1784 if (sscanf(p, str, val) == 1)
1790 static int is_empty_or_comment(char *line)
1794 for (i = 0; i < strlen(line); i++) {
1797 if (!isspace(line[i]) && !iscntrl(line[i]))
1804 static int parse_jobs_ini(char *file)
1806 unsigned int prioclass, prio, cpu, global;
1807 unsigned long long ull;
1808 unsigned long ul1, ul2;
1809 struct thread_data *td;
1810 char *string, *name;
1815 f = fopen(file, "r");
1821 string = malloc(4096);
1824 while ((p = fgets(string, 4096, f)) != NULL) {
1825 if (is_empty_or_comment(p))
1827 if (sscanf(p, "[%s]", name) != 1)
1830 global = !strncmp(name, "global", 6);
1832 name[strlen(name) - 1] = '\0';
1834 td = get_new_job(global);
1842 while ((p = fgets(string, 4096, f)) != NULL) {
1843 if (is_empty_or_comment(p))
1847 if (!check_int(p, "rw", &td->ddir)) {
1851 if (!check_int(p, "prio", &prio)) {
1855 if (!check_int(p, "prioclass", &prioclass)) {
1859 if (!check_int(p, "direct", &td->odirect)) {
1863 if (!check_int(p, "rate", &td->rate)) {
1867 if (!check_int(p, "ratemin", &td->ratemin)) {
1871 if (!check_int(p, "ratecycle", &td->ratecycle)) {
1875 if (!check_int(p, "thinktime", &td->thinktime)) {
1879 if (!check_int(p, "cpumask", &cpu)) {
1880 fill_cpu_mask(td->cpumask, cpu);
1884 if (!check_int(p, "fsync", &td->fsync_blocks)) {
1888 if (!check_int(p, "startdelay", &td->start_delay)) {
1892 if (!check_int(p, "timeout", &td->timeout)) {
1896 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
1900 if (!check_int(p, "aio_depth", &td->aio_depth)) {
1904 if (!check_int(p, "sync", &td->sync_io)) {
1908 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
1912 if (!check_int(p, "create_serialize", &td->create_serialize)) {
1916 if (!check_int(p, "create_fsync", &td->create_fsync)) {
1920 if (!check_int(p, "loops", &td->loops)) {
1924 if (!check_int(p, "verify", &td->verify)) {
1928 if (!check_range(p, "bsrange", &ul1, &ul2)) {
1934 if (!check_strcnv(p, "bs", &ull)) {
1939 if (!check_strcnv(p, "size", &td->file_size)) {
1943 if (!check_strcnv(p, "offset", &td->file_offset)) {
1947 if (!check_str(p, "mem", "malloc")) {
1948 td->mem_type = MEM_MALLOC;
1952 if (!check_str(p, "mem", "shm")) {
1953 td->mem_type = MEM_SHM;
1957 if (!strncmp(p, "sequential", 10)) {
1962 if (!strncmp(p, "random", 6)) {
1967 if (!strncmp(p, "aio", 3)) {
1972 if (!strncmp(p, "create", 6)) {
1973 td->create_file = 1;
1977 if (!strncmp(p, "overwrite", 9)) {
1982 if (!strncmp(p, "exitall", 7)) {
1983 exitall_on_terminate = 1;
1987 printf("Client%d: bad option %s\n",td->thread_number,p);
1991 if (add_job(td, name, prioclass, prio))
2001 static int parse_options(int argc, char *argv[])
2005 for (i = 1; i < argc; i++) {
2006 char *parm = argv[i];
2015 def_thread.sequential = !!atoi(parm);
2019 def_thread.bs = atoi(parm);
2020 def_thread.bs <<= 10;
2021 if (!def_thread.bs) {
2022 printf("bad block size\n");
2023 def_thread.bs = DEF_BS;
2028 def_thread.timeout = atoi(parm);
2032 repeatable = !!atoi(parm);
2036 rate_quit = !!atoi(parm);
2040 def_thread.odirect = !!atoi(parm);
2043 if (i + 1 >= argc) {
2044 printf("-f needs file as arg\n");
2047 ini_file = strdup(argv[i+1]);
2057 printf("bad option %s\n", argv[i]);
2065 static void print_thread_status(struct thread_data *td, int nr_running,
2066 int t_rate, int m_rate)
2068 printf("Threads now running: %d", nr_running);
2069 if (m_rate || t_rate)
2070 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
2071 printf(" : [%s]\r", run_str);
2075 static void check_str_update(struct thread_data *td, int n, int t, int m)
2077 char c = run_str[td->thread_number - 1];
2079 if (td->runstate == td->old_runstate)
2082 switch (td->runstate) {
2098 case TD_NOT_CREATED:
2102 printf("state %d\n", td->runstate);
2105 run_str[td->thread_number - 1] = c;
2106 print_thread_status(td, n, t, m);
2107 td->old_runstate = td->runstate;
2110 static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
2115 * reap exited threads (TD_EXITED -> TD_REAPED)
2117 for (i = 0; i < thread_number; i++) {
2118 struct thread_data *td = &threads[i];
2120 check_str_update(td, *nr_running, *t_rate, *m_rate);
2122 if (td->runstate != TD_EXITED)
2125 td_set_runstate(td, TD_REAPED);
2126 waitpid(td->pid, NULL, 0);
2128 (*m_rate) -= td->ratemin;
2129 (*t_rate) -= td->rate;
2130 check_str_update(td, *nr_running, *t_rate, *m_rate);
2137 static void run_threads(char *argv[])
2139 struct timeval genesis;
2140 struct thread_data *td;
2141 unsigned long spent;
2142 int i, todo, nr_running, m_rate, t_rate;
2144 printf("Starting %d threads\n", thread_number);
2147 signal(SIGINT, sig_handler);
2149 todo = thread_number;
2151 m_rate = t_rate = 0;
2153 for (i = 0; i < thread_number; i++) {
2156 if (!td->create_serialize)
2160 * do file setup here so it happens sequentially,
2161 * we don't want X number of threads getting their
2162 * client data interspersed on disk
2164 if (setup_file(td)) {
2165 td_set_runstate(td, TD_REAPED);
2170 gettimeofday(&genesis, NULL);
2174 * create threads (TD_NOT_CREATED -> TD_CREATED)
2176 for (i = 0; i < thread_number; i++) {
2179 if (td->runstate != TD_NOT_CREATED)
2183 * never got a chance to start, killed by other
2184 * thread for some reason
2186 if (td->terminate) {
2191 if (td->start_delay) {
2192 spent = mtime_since_now(&genesis);
2194 if (td->start_delay * 1000 > spent)
2198 td_set_runstate(td, TD_CREATED);
2199 check_str_update(td, nr_running, t_rate, m_rate);
2200 sem_init(&startup_sem, 1, 1);
2204 sem_wait(&startup_sem);
2206 thread_main(shm_id, i, argv);
2212 * start created threads (TD_CREATED -> TD_RUNNING)
2214 for (i = 0; i < thread_number; i++) {
2215 struct thread_data *td = &threads[i];
2217 if (td->runstate != TD_CREATED)
2220 td_set_runstate(td, TD_RUNNING);
2222 m_rate += td->ratemin;
2224 check_str_update(td, nr_running, t_rate, m_rate);
2225 sem_post(&td->mutex);
2228 for (i = 0; i < thread_number; i++) {
2229 struct thread_data *td = &threads[i];
2231 if (td->runstate == TD_RUNNING)
2232 run_str[td->thread_number - 1] = '+';
2233 else if (td->runstate == TD_VERIFYING)
2234 run_str[td->thread_number - 1] = 'V';
2238 check_str_update(td, nr_running, t_rate, m_rate);
2241 reap_threads(&nr_running, &t_rate, &m_rate);
2247 while (nr_running) {
2248 reap_threads(&nr_running, &t_rate, &m_rate);
2253 int setup_thread_area(void)
2256 * 1024 is too much on some machines, scale max_jobs if
2257 * we get a failure that looks like too large a shm segment
2260 int s = max_jobs * sizeof(struct thread_data);
2262 shm_id = shmget(0, s, IPC_CREAT | 0600);
2265 if (errno != EINVAL) {
2276 threads = shmat(shm_id, NULL, 0);
2277 if (threads == (void *) -1) {
2286 int main(int argc, char *argv[])
2288 static unsigned long max_run[2], min_run[2];
2289 static unsigned long max_bw[2], min_bw[2];
2290 static unsigned long io_mb[2], agg[2];
2293 if (setup_thread_area())
2296 if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) {
2297 perror("sched_getaffinity");
2304 def_thread.ddir = DDIR_READ;
2305 def_thread.bs = DEF_BS;
2306 def_thread.min_bs = -1;
2307 def_thread.max_bs = -1;
2308 def_thread.odirect = DEF_ODIRECT;
2309 def_thread.ratecycle = DEF_RATE_CYCLE;
2310 def_thread.sequential = DEF_SEQUENTIAL;
2311 def_thread.timeout = DEF_TIMEOUT;
2312 def_thread.create_file = DEF_CREATE;
2313 def_thread.overwrite = DEF_OVERWRITE;
2314 def_thread.invalidate_cache = DEF_INVALIDATE;
2315 def_thread.sync_io = DEF_SYNCIO;
2316 def_thread.mem_type = MEM_MALLOC;
2317 def_thread.bw_avg_time = DEF_BWAVGTIME;
2318 def_thread.create_serialize = DEF_CREATE_SER;
2319 def_thread.create_fsync = DEF_CREATE_FSYNC;
2320 def_thread.loops = DEF_LOOPS;
2321 def_thread.verify = DEF_VERIFY;
2323 i = parse_options(argc, argv);
2326 printf("Need job file\n");
2330 if (parse_jobs_ini(ini_file))
2333 if (!thread_number) {
2334 printf("Nothing to do\n");
2340 min_bw[0] = min_run[0] = ~0UL;
2341 min_bw[1] = min_run[1] = ~0UL;
2342 io_mb[0] = io_mb[1] = 0;
2343 agg[0] = agg[1] = 0;
2344 for (i = 0; i < thread_number; i++) {
2345 struct thread_data *td = &threads[i];
2346 unsigned long bw = 0;
2349 if (td->runtime < min_run[td->ddir])
2350 min_run[td->ddir] = td->runtime;
2351 if (td->runtime > max_run[td->ddir])
2352 max_run[td->ddir] = td->runtime;
2355 bw = td->io_kb * 1024 / td->runtime;
2356 if (bw < min_bw[td->ddir])
2357 min_bw[td->ddir] = bw;
2358 if (bw > max_bw[td->ddir])
2359 max_bw[td->ddir] = bw;
2361 io_mb[td->ddir] += td->io_kb >> 10;
2364 show_thread_status(td);
2368 agg[0] = io_mb[0] * 1024 * 1000 / max_run[0];
2370 agg[1] = io_mb[1] * 1024 * 1000 / max_run[1];
2372 printf("\nRun status:\n");
2373 if (max_run[DDIR_READ])
2374 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]);
2375 if (max_run[DDIR_WRITE])
2376 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]);