[PATCH] fio: pretty close to compiling/working on FreeBSD now
[disktools.git] / fio.c
diff --git a/fio.c b/fio.c
index 8fc2f779392b66871943778dd1755de8c8a16d24..ca79cb63756bccbf09ffdedaad419463a4ce8fca 100644 (file)
--- a/fio.c
+++ b/fio.c
 #include <time.h>
 #include <math.h>
 #include <assert.h>
-#include <pthread.h>
+#include <dirent.h>
+#include <libgen.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
 #include <sys/ioctl.h>
-#include <asm/unistd.h>
+#include <sys/mman.h>
 
 #include "fio.h"
+#include "os.h"
 
 #define MASK   (4095)
 
@@ -47,6 +49,10 @@ int groupid = 0;
 int thread_number = 0;
 char run_str[MAX_JOBS + 1];
 int shm_id = 0;
+static LIST_HEAD(disk_list);
+
+static void update_io_ticks(void);
+static void disk_util_timer_arm(void);
 
 /*
  * thread life cycle
@@ -60,35 +66,20 @@ enum {
        TD_REAPED,
 };
 
-/*
- * The io unit
- */
-struct io_u {
-       struct iocb iocb;
-       struct timeval start_time;
-       struct timeval issue_time;
-
-       char *buf;
-       unsigned int buflen;
-       unsigned long long offset;
-
-       struct list_head list;
-};
-
 #define should_fsync(td)       (td_write(td) && !(td)->odirect)
 
 static sem_t startup_sem;
 
 #define TERMINATE_ALL          (-1)
 
-static void terminate_threads(int groupid)
+static void terminate_threads(int group_id)
 {
        int i;
 
        for (i = 0; i < thread_number; i++) {
                struct thread_data *td = &threads[i];
 
-               if (groupid == TERMINATE_ALL || groupid == td->groupid) {
+               if (group_id == TERMINATE_ALL || groupid == td->groupid) {
                        td->terminate = 1;
                        td->start_delay = 0;
                }
@@ -97,7 +88,17 @@ static void terminate_threads(int groupid)
 
 static void sig_handler(int sig)
 {
-       terminate_threads(TERMINATE_ALL);
+       switch (sig) {
+               case SIGALRM:
+                       update_io_ticks();
+                       disk_util_timer_arm();
+                       break;
+               default:
+                       printf("\nfio: terminating on signal\n");
+                       fflush(stdout);
+                       terminate_threads(TERMINATE_ALL);
+                       break;
+       }
 }
 
 static unsigned long utime_since(struct timeval *s, struct timeval *e)
@@ -187,7 +188,7 @@ static void mark_random_map(struct thread_data *td, struct io_u *io_u)
        unsigned int blocks = 0;
 
        while (blocks < (io_u->buflen / td->min_bs)) {
-               int idx, bit;
+               unsigned int idx, bit;
 
                if (!random_map_free(td, block))
                        break;
@@ -255,8 +256,7 @@ static unsigned int get_next_buflen(struct thread_data *td)
        return buflen;
 }
 
-static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
-                                  unsigned long val)
+static inline void add_stat_sample(struct io_stat *is, unsigned long val)
 {
        if (val > is->max_val)
                is->max_val = val;
@@ -268,24 +268,24 @@ static inline void add_stat_sample(struct thread_data *td, struct io_stat *is,
        is->samples++;
 }
 
-static void add_log_sample(struct thread_data *td, struct io_log *log,
+static void add_log_sample(struct thread_data *td, struct io_log *iolog,
                           unsigned long val)
 {
-       if (log->nr_samples == log->max_samples) {
-               int new_size = sizeof(struct io_sample) * log->max_samples * 2;
+       if (iolog->nr_samples == iolog->max_samples) {
+               int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
 
-               log->log = realloc(log->log, new_size);
-               log->max_samples <<= 1;
+               iolog->log = realloc(iolog->log, new_size);
+               iolog->max_samples <<= 1;
        }
 
-       log->log[log->nr_samples].val = val;
-       log->log[log->nr_samples].time = mtime_since_now(&td->start);
-       log->nr_samples++;
+       iolog->log[iolog->nr_samples].val = val;
+       iolog->log[iolog->nr_samples].time = mtime_since_now(&td->start);
+       iolog->nr_samples++;
 }
 
 static void add_clat_sample(struct thread_data *td, unsigned long msec)
 {
-       add_stat_sample(td, &td->clat_stat, msec);
+       add_stat_sample(&td->clat_stat, msec);
 
        if (td->lat_log)
                add_log_sample(td, td->lat_log, msec);
@@ -293,7 +293,7 @@ static void add_clat_sample(struct thread_data *td, unsigned long msec)
 
 static void add_slat_sample(struct thread_data *td, unsigned long msec)
 {
-       add_stat_sample(td, &td->slat_stat, msec);
+       add_stat_sample(&td->slat_stat, msec);
 }
 
 static void add_bw_sample(struct thread_data *td)
@@ -305,7 +305,7 @@ static void add_bw_sample(struct thread_data *td)
                return;
 
        rate = (td->this_io_bytes - td->stat_io_bytes) / spent;
-       add_stat_sample(td, &td->bw_stat, rate);
+       add_stat_sample(&td->bw_stat, rate);
 
        if (td->bw_log)
                add_log_sample(td, td->bw_log, rate);
@@ -317,7 +317,7 @@ static void add_bw_sample(struct thread_data *td)
 /*
  * busy looping version for the last few usec
  */
-static void __usec_sleep(int usec)
+static void __usec_sleep(unsigned int usec)
 {
        struct timeval start;
 
@@ -326,24 +326,31 @@ static void __usec_sleep(int usec)
                nop;
 }
 
-static void usec_sleep(int usec)
+static void usec_sleep(struct thread_data *td, unsigned long usec)
 {
-       struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 };
-       struct timespec rem;
+       struct timespec req, rem;
+
+       req.tv_sec = usec / 1000000;
+       req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
 
        do {
                if (usec < 5000) {
                        __usec_sleep(usec);
                        break;
                }
+
                rem.tv_sec = rem.tv_nsec = 0;
-               nanosleep(&req, &rem);
-               if (!rem.tv_nsec)
+               if (nanosleep(&req, &rem) < 0)
+                       break;
+
+               if ((rem.tv_sec + rem.tv_nsec) == 0)
                        break;
 
                req.tv_nsec = rem.tv_nsec;
-               usec = rem.tv_nsec * 1000;
-       } while (1);
+               req.tv_sec = rem.tv_sec;
+
+               usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
+       } while (!td->terminate);
 }
 
 static void rate_throttle(struct thread_data *td, unsigned long time_spent,
@@ -361,7 +368,7 @@ static void rate_throttle(struct thread_data *td, unsigned long time_spent,
 
                td->rate_pending_usleep += s;
                if (td->rate_pending_usleep >= 100000) {
-                       usec_sleep(td->rate_pending_usleep);
+                       usec_sleep(td, td->rate_pending_usleep);
                        td->rate_pending_usleep = 0;
                }
        } else {
@@ -408,7 +415,7 @@ static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
 {
        if (!td->timeout)
                return 0;
-       if (mtime_since(&td->start, t) >= td->timeout * 1000)
+       if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
                return 1;
 
        return 0;
@@ -448,16 +455,23 @@ static void hexdump(void *buffer, int len)
        printf("\n");
 }
 
-static int verify_io_u(struct io_u *io_u)
+static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
+{
+       unsigned char *p = (unsigned char *) io_u->buf;
+       unsigned long c;
+
+       p += sizeof(*hdr);
+       c = crc32(p, hdr->len - sizeof(*hdr));
+
+       return c != hdr->crc32;
+}
+
+static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
 {
-       struct verify_header *hdr = (struct verify_header *) io_u->buf;
        unsigned char *p = (unsigned char *) io_u->buf;
        struct md5_ctx md5_ctx;
        int ret;
 
-       if (hdr->fio_magic != FIO_HDR_MAGIC)
-               return 1;
-
        memset(&md5_ctx, 0, sizeof(md5_ctx));
        p += sizeof(*hdr);
        md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
@@ -471,24 +485,62 @@ static int verify_io_u(struct io_u *io_u)
        return ret;
 }
 
+static int verify_io_u(struct io_u *io_u)
+{
+       struct verify_header *hdr = (struct verify_header *) io_u->buf;
+       int ret;
+
+       if (hdr->fio_magic != FIO_HDR_MAGIC)
+               return 1;
+
+       if (hdr->verify_type == VERIFY_MD5)
+               ret = verify_io_u_md5(hdr, io_u);
+       else if (hdr->verify_type == VERIFY_CRC32)
+               ret = verify_io_u_crc32(hdr, io_u);
+       else {
+               fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
+               ret = 1;
+       }
+
+       return ret;
+}
+
+static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
+{
+       hdr->crc32 = crc32(p, len);
+}
+
+static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
+{
+       struct md5_ctx md5_ctx;
+
+       memset(&md5_ctx, 0, sizeof(md5_ctx));
+       md5_update(&md5_ctx, p, len);
+       memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
+}
+
 /*
  * fill body of io_u->buf with random data and add a header with the
  * (eg) sha1sum of that data.
  */
 static void populate_io_u(struct thread_data *td, struct io_u *io_u)
 {
-       struct md5_ctx md5_ctx;
-       struct verify_header hdr;
        unsigned char *p = (unsigned char *) io_u->buf;
+       struct verify_header hdr;
 
        hdr.fio_magic = FIO_HDR_MAGIC;
        hdr.len = io_u->buflen;
        p += sizeof(hdr);
        fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
 
-       memset(&md5_ctx, 0, sizeof(md5_ctx));
-       md5_update(&md5_ctx, p, io_u->buflen - sizeof(hdr));
-       memcpy(hdr.md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
+       if (td->verify == VERIFY_MD5) {
+               fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
+               hdr.verify_type = VERIFY_MD5;
+       } else {
+               fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
+               hdr.verify_type = VERIFY_CRC32;
+       }
+
        memcpy(io_u->buf, &hdr, sizeof(hdr));
 }
 
@@ -537,20 +589,21 @@ static struct io_u *get_io_u(struct thread_data *td)
        if (io_u->buflen + io_u->offset > td->file_size)
                io_u->buflen = td->file_size - io_u->offset;
 
+       if (!io_u->buflen) {
+               put_io_u(td, io_u);
+               return NULL;
+       }
+
        if (!td->sequential)
                mark_random_map(td, io_u);
 
        td->last_bytes += io_u->buflen;
 
-       if (td->verify)
+       if (td->verify != VERIFY_NONE)
                populate_io_u(td, io_u);
 
-       if (td->use_aio) {
-               if (td_read(td))
-                       io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
-               else
-                       io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
-       }
+       if (td->io_prep)
+               td->io_prep(td, io_u, td_read(td));
 
        gettimeofday(&io_u->start_time, NULL);
        return io_u;
@@ -638,9 +691,16 @@ static void do_sync_verify(struct thread_data *td)
        io_u = __get_io_u(td);
 
        if (!td->odirect) {
-               if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) {
-                       td->error = errno;
-                       goto out;
+               if (!td->use_mmap) {
+                       if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) {
+                               td_verror(td, errno);
+                               goto out;
+                       }
+               } else {
+                       if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
+                               td_verror(td, errno);
+                               goto out;
+                       }
                }
        }
 
@@ -657,7 +717,7 @@ static void do_sync_verify(struct thread_data *td)
 
                if (td->cur_off != io_u->offset) {
                        if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
-                               td->error = errno;
+                               td_verror(td, errno);
                                break;
                        }
                }
@@ -665,7 +725,7 @@ static void do_sync_verify(struct thread_data *td)
                ret = read(td->fd, io_u->buf, io_u->buflen);
                if (ret < (int) io_u->buflen) {
                        if (ret == -1) {
-                               td->error = errno;
+                               td_verror(td, errno);
                                break;
                        } else if (!ret)
                                break;
@@ -684,6 +744,49 @@ out:
        put_io_u(td, io_u);
 }
 
+static int __do_sync_mmap(struct thread_data *td, struct io_u *io_u)
+{
+       unsigned long long real_off = io_u->offset - td->file_offset;
+
+       if (td_read(td))
+               memcpy(io_u->buf, td->mmap + real_off, io_u->buflen);
+       else
+               memcpy(td->mmap + real_off, io_u->buf, io_u->buflen);
+       
+       /*
+        * not really direct, but should drop the pages from the cache
+        */
+       if (td->odirect) {
+               msync(td->mmap + real_off, io_u->buflen, MS_SYNC);
+               madvise(td->mmap + real_off, io_u->buflen,  MADV_DONTNEED);
+       }
+
+       return io_u->buflen;
+}
+
+static int __do_sync_rw(struct thread_data *td, struct io_u *io_u)
+{
+       if (td->cur_off != io_u->offset) {
+               if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
+                       td_verror(td, errno);
+                       return 1;
+               }
+       }
+
+       if (td_read(td))
+               return read(td->fd, io_u->buf, io_u->buflen);
+       else
+               return write(td->fd, io_u->buf, io_u->buflen);
+}
+
+static void sync_td(struct thread_data *td)
+{
+       if (!td->use_mmap)
+               fsync(td->fd);
+       else
+               msync(td->mmap, td->file_size, MS_SYNC);
+}
+
 static void do_sync_io(struct thread_data *td)
 {
        unsigned long msec, usec;
@@ -700,21 +803,14 @@ static void do_sync_io(struct thread_data *td)
                if (!io_u)
                        break;
 
-               if (td->cur_off != io_u->offset) {
-                       if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) {
-                               td->error = errno;
-                               break;
-                       }
-               }
-
-               if (td_read(td))
-                       ret = read(td->fd, io_u->buf, io_u->buflen);
+               if (!td->use_mmap)
+                       ret = __do_sync_rw(td, io_u);
                else
-                       ret = write(td->fd, io_u->buf, io_u->buflen);
+                       ret = __do_sync_mmap(td, io_u);
 
                if (ret < (int) io_u->buflen) {
                        if (ret == -1)
-                               td->error = errno;
+                               td_verror(td, errno);
                        break;
                }
 
@@ -733,7 +829,7 @@ static void do_sync_io(struct thread_data *td)
                rate_throttle(td, usec, io_u->buflen);
 
                if (check_min_rate(td, &e)) {
-                       td->error = ENODATA;
+                       td_verror(td, ENOMEM);
                        break;
                }
 
@@ -748,56 +844,32 @@ static void do_sync_io(struct thread_data *td)
                io_u = NULL;
 
                if (td->thinktime)
-                       usec_sleep(td->thinktime);
+                       usec_sleep(td, td->thinktime);
 
                if (should_fsync(td) && td->fsync_blocks &&
                    (td->io_blocks % td->fsync_blocks) == 0)
-                       fsync(td->fd);
+                       sync_td(td);
        }
 
        if (io_u)
                put_io_u(td, io_u);
 
        if (should_fsync(td))
-               fsync(td->fd);
+               sync_td(td);
 }
 
 static int io_u_getevents(struct thread_data *td, int min, int max,
                          struct timespec *t)
 {
-       int r;
-
-       do {
-               r = io_getevents(td->aio_ctx, min, max, td->aio_events, t);
-               if (r != -EAGAIN && r != -EINTR)
-                       break;
-       } while (1);
-
-       return r;
+       return td->io_getevents(td, min, max, t);
 }
 
 static int io_u_queue(struct thread_data *td, struct io_u *io_u)
 {
-       struct iocb *iocb = &io_u->iocb;
-       int ret;
-
-       do {
-               ret = io_submit(td->aio_ctx, 1, &iocb);
-               if (ret == 1)
-                       return 0;
-               else if (ret == -EAGAIN)
-                       usleep(100);
-               else if (ret == -EINTR)
-                       continue;
-               else
-                       break;
-       } while (1);
-
-       return ret;
+       return td->io_queue(td, io_u);
 }
 
 #define iocb_time(iocb)        ((unsigned long) (iocb)->data)
-#define ev_to_iou(ev)  (struct io_u *) ((unsigned long) (ev)->obj)
 
 static int ios_completed(struct thread_data *td, int nr)
 {
@@ -809,7 +881,7 @@ static int ios_completed(struct thread_data *td, int nr)
        gettimeofday(&e, NULL);
 
        for (i = 0, bytes_done = 0; i < nr; i++) {
-               io_u = ev_to_iou(td->aio_events + i);
+               io_u = td->io_event(td, i);
 
                td->io_blocks++;
                td->io_bytes += io_u->buflen;
@@ -850,7 +922,7 @@ static void cleanup_pending_aio(struct thread_data *td)
        list_for_each_safe(entry, n, &td->io_u_busylist) {
                io_u = list_entry(entry, struct io_u, list);
 
-               r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events);
+               r = td->io_cancel(td, io_u);
                if (!r)
                        put_io_u(td, io_u);
        }
@@ -901,11 +973,12 @@ static void do_async_verify(struct thread_data *td)
                        break;
                }
 
-               io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
+               td->io_prep(td, io_u, 1);
+
                ret = io_u_queue(td, io_u);
                if (ret) {
                        put_io_u(td, io_u);
-                       td->error = ret;
+                       td_verror(td, ret);
                        break;
                }
 
@@ -919,11 +992,11 @@ static void do_async_verify(struct thread_data *td)
                ret = io_u_getevents(td, 1, 1, NULL);
                if (ret != 1) {
                        if (ret < 0)
-                               td->error = ret;
+                               td_verror(td, ret);
                        break;
                }
 
-               v_io_u = ev_to_iou(td->aio_events);
+               v_io_u = td->io_event(td, 0);
 
                td->cur_off = v_io_u->offset + v_io_u->buflen;
 
@@ -967,7 +1040,7 @@ static void do_async_io(struct thread_data *td)
                ret = io_u_queue(td, io_u);
                if (ret) {
                        put_io_u(td, io_u);
-                       td->error = ret;
+                       td_verror(td, ret);
                        break;
                }
 
@@ -983,7 +1056,7 @@ static void do_async_io(struct thread_data *td)
 
                ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
                if (ret < 0) {
-                       td->error = ret;
+                       td_verror(td, ret);
                        break;
                } else if (!ret)
                        continue;
@@ -1001,7 +1074,7 @@ static void do_async_io(struct thread_data *td)
                rate_throttle(td, usec, bytes_done);
 
                if (check_min_rate(td, &e)) {
-                       td->error = ENODATA;
+                       td_verror(td, ENOMEM);
                        break;
                }
 
@@ -1009,7 +1082,7 @@ static void do_async_io(struct thread_data *td)
                        break;
 
                if (td->thinktime)
-                       usec_sleep(td->thinktime);
+                       usec_sleep(td, td->thinktime);
 
                if (should_fsync(td) && td->fsync_blocks &&
                    (td->io_blocks % td->fsync_blocks) == 0)
@@ -1025,21 +1098,22 @@ static void do_async_io(struct thread_data *td)
 
 static void cleanup_aio(struct thread_data *td)
 {
-       io_destroy(td->aio_ctx);
-
-       if (td->aio_events)
-               free(td->aio_events);
+       if (td->io_engine == FIO_LIBAIO)
+               fio_libaio_cleanup(td);
+       else if (td->io_engine == FIO_POSIXAIO)
+               fio_posixaio_cleanup(td);
 }
 
 static int init_aio(struct thread_data *td)
 {
-       if (io_queue_init(td->aio_depth, &td->aio_ctx)) {
-               td->error = errno;
+       if (td->io_engine == FIO_LIBAIO)
+               return fio_libaio_init(td);
+       else if (td->io_engine == FIO_POSIXAIO)
+               return fio_posixaio_init(td);
+       else {
+               fprintf(stderr, "bad io_engine %d\n", td->io_engine);
                return 1;
        }
-
-       td->aio_events = malloc(td->aio_depth * sizeof(struct io_event));
-       return 0;
 }
 
 static void cleanup_io_u(struct thread_data *td)
@@ -1061,36 +1135,50 @@ static void cleanup_io_u(struct thread_data *td)
 
                shmdt(td->orig_buffer);
                shmctl(td->shm_id, IPC_RMID, &sbuf);
-       }
+       } else if (td->mem_type == MEM_MMAP)
+               munmap(td->orig_buffer, td->orig_buffer_size);
+       else
+               fprintf(stderr, "Bad memory type %d\n", td->mem_type);
+
+       td->orig_buffer = NULL;
 }
 
 static int init_io_u(struct thread_data *td)
 {
        struct io_u *io_u;
-       int i, max_units, mem_size;
+       int i, max_units;
        char *p;
 
-       if (!td->use_aio)
+       if (td->io_engine == FIO_SYNCIO)
                max_units = 1;
        else
                max_units = td->aio_depth;
 
-       mem_size = td->max_bs * max_units + MASK;
+       td->orig_buffer_size = td->max_bs * max_units + MASK;
 
        if (td->mem_type == MEM_MALLOC)
-               td->orig_buffer = malloc(mem_size);
+               td->orig_buffer = malloc(td->orig_buffer_size);
        else if (td->mem_type == MEM_SHM) {
-               td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
+               td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
                if (td->shm_id < 0) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        perror("shmget");
                        return 1;
                }
 
                td->orig_buffer = shmat(td->shm_id, NULL, 0);
                if (td->orig_buffer == (void *) -1) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        perror("shmat");
+                       td->orig_buffer = NULL;
+                       return 1;
+               }
+       } else if (td->mem_type == MEM_MMAP) {
+               td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
+               if (td->orig_buffer == MAP_FAILED) {
+                       td_verror(td, errno);
+                       perror("mmap");
+                       td->orig_buffer = NULL;
                        return 1;
                }
        }
@@ -1112,12 +1200,13 @@ static int init_io_u(struct thread_data *td)
        return 0;
 }
 
-static int create_file(struct thread_data *td)
+static int create_file(struct thread_data *td, unsigned long long size,
+                      int extend)
 {
        unsigned long long left;
        unsigned int bs;
+       int r, oflags;
        char *b;
-       int r;
 
        /*
         * unless specifically asked for overwrite, let normal io extend it
@@ -1125,22 +1214,28 @@ static int create_file(struct thread_data *td)
        if (td_write(td) && !td->overwrite)
                return 0;
 
-       if (!td->file_size) {
+       if (!size) {
                fprintf(stderr, "Need size for create\n");
-               td->error = EINVAL;
+               td_verror(td, EINVAL);
                return 1;
        }
 
-       printf("Client%d: Laying out IO file\n", td->thread_number);
+       if (!extend) {
+               oflags = O_CREAT | O_TRUNC;
+               printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
+       } else {
+               oflags = O_APPEND;
+               printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
+       }
 
-       td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+       td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
        if (td->fd < 0) {
-               td->error = errno;
+               td_verror(td, errno);
                return 1;
        }
 
-       if (ftruncate(td->fd, td->file_size) == -1) {
-               td->error = errno;
+       if (!extend && ftruncate(td->fd, td->file_size) == -1) {
+               td_verror(td, errno);
                return 1;
        }
 
@@ -1148,8 +1243,8 @@ static int create_file(struct thread_data *td)
        b = malloc(td->max_bs);
        memset(b, 0, td->max_bs);
 
-       left = td->file_size;
-       while (left) {
+       left = size;
+       while (left && !td->terminate) {
                bs = td->max_bs;
                if (bs > left)
                        bs = left;
@@ -1161,15 +1256,17 @@ static int create_file(struct thread_data *td)
                        continue;
                } else {
                        if (r < 0)
-                               td->error = errno;
+                               td_verror(td, errno);
                        else
-                               td->error = EIO;
+                               td_verror(td, EIO);
 
                        break;
                }
        }
 
-       if (td->create_fsync)
+       if (td->terminate)
+               unlink(td->file_name);
+       else if (td->create_fsync)
                fsync(td->fd);
 
        close(td->fd);
@@ -1178,77 +1275,153 @@ static int create_file(struct thread_data *td)
        return 0;
 }
 
-static int file_exists(struct thread_data *td)
+static int file_size(struct thread_data *td)
 {
        struct stat st;
 
-       if (stat(td->file_name, &st) != -1)
+       if (fstat(td->fd, &st) == -1) {
+               td_verror(td, errno);
                return 1;
+       }
+
+       if (!td->file_size)
+               td->file_size = st.st_size;
 
-       return errno != ENOENT;
+       return 0;
 }
 
-static int get_file_size(struct thread_data *td)
+static int bdev_size(struct thread_data *td)
 {
-       size_t bytes = 0;
-       struct stat st;
+       size_t bytes;
 
-       if (fstat(td->fd, &st) == -1) {
-               td->error = errno;
+       if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
+               td_verror(td, errno);
                return 1;
        }
 
        /*
-        * if block device, get size via BLKGETSIZE64 ioctl. try that as well
-        * if this is a link, fall back to st.st_size if it fails
+        * no extend possibilities, so limit size to device size if too large
         */
-       if (S_ISBLK(st.st_mode) || S_ISLNK(st.st_mode)) {
-               if (ioctl(td->fd, BLKGETSIZE64, &bytes)) {
-                       if (S_ISBLK(st.st_mode)) {
-                               td->error = errno;
-                               return 1;
-                       } else
-                               bytes = st.st_size;
-               }
-       } else
-               bytes = st.st_size;
+       if (!td->file_size || td->file_size > bytes)
+               td->file_size = bytes;
 
-       if (td_read(td)) {
-               if (td->file_size > bytes)
-                       bytes = td->file_size;
-       } else {
-               if (!td->file_size)
-                       td->file_size = 1024 * 1024 * 1024;
+       return 0;
+}
 
-               bytes = td->file_size;
-       }
+static int get_file_size(struct thread_data *td)
+{
+       int ret;
 
-       if (td->file_offset > bytes) {
-               fprintf(stderr, "Client%d: offset larger than length\n", td->thread_number);
+       if (td->filetype == FIO_TYPE_FILE)
+               ret = file_size(td);
+       else
+               ret = bdev_size(td);
+
+       if (ret)
+               return ret;
+
+       if (td->file_offset > td->file_size) {
+               fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size);
                return 1;
        }
 
-       td->io_size = bytes - td->file_offset;
+       td->io_size = td->file_size - td->file_offset;
        if (td->io_size == 0) {
                fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
-               td->error = EINVAL;
+               td_verror(td, EINVAL);
                return 1;
        }
 
        return 0;
 }
 
+static int setup_file_mmap(struct thread_data *td)
+{
+       int flags;
+
+       if (td_read(td))
+               flags = PROT_READ;
+       else {
+               flags = PROT_WRITE;
+
+               if (td->verify != VERIFY_NONE)
+                       flags |= PROT_READ;
+       }
+
+       td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
+       if (td->mmap == MAP_FAILED) {
+               td->mmap = NULL;
+               td_verror(td, errno);
+               return 1;
+       }
+
+       if (td->invalidate_cache) {
+               if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
+                       td_verror(td, errno);
+                       return 1;
+               }
+       }
+
+       if (td->sequential) {
+               if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
+                       td_verror(td, errno);
+                       return 1;
+               }
+       } else {
+               if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
+                       td_verror(td, errno);
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+static int setup_file_plain(struct thread_data *td)
+{
+       if (td->invalidate_cache) {
+               if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
+                       td_verror(td, errno);
+                       return 1;
+               }
+       }
+
+       if (td->sequential) {
+               if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
+                       td_verror(td, errno);
+                       return 1;
+               }
+       } else {
+               if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
+                       td_verror(td, errno);
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 static int setup_file(struct thread_data *td)
 {
+       struct stat st;
        int flags = 0;
 
-       if (!file_exists(td)) {
+       if (stat(td->file_name, &st) == -1) {
+               if (errno != ENOENT) {
+                       td_verror(td, errno);
+                       return 1;
+               }
                if (!td->create_file) {
-                       td->error = ENOENT;
+                       td_verror(td, ENOENT);
                        return 1;
                }
-               if (create_file(td))
+               if (create_file(td, td->file_size, 0))
                        return 1;
+       } else if (td->filetype == FIO_TYPE_FILE) {
+               if (st.st_size < td->file_size) {
+                       if (create_file(td, td->file_size - st.st_size, 1))
+                               return 1;
+               }
        }
 
        if (td->odirect)
@@ -1257,44 +1430,272 @@ static int setup_file(struct thread_data *td)
        if (td_read(td))
                td->fd = open(td->file_name, flags | O_RDONLY);
        else {
-               if (!td->overwrite)
-                       flags |= O_TRUNC;
+               if (td->filetype == FIO_TYPE_FILE) {
+                       if (!td->overwrite)
+                               flags |= O_TRUNC;
+
+                       flags |= O_CREAT;
+               }
                if (td->sync_io)
                        flags |= O_SYNC;
-               if (td->verify)
-                       flags |= O_RDWR;
-               else
-                       flags |= O_WRONLY;
 
-               td->fd = open(td->file_name, flags | O_CREAT, 0600);
+               flags |= O_RDWR;
+
+               td->fd = open(td->file_name, flags, 0600);
        }
 
        if (td->fd == -1) {
-               td->error = errno;
+               td_verror(td, errno);
                return 1;
        }
 
        if (get_file_size(td))
                return 1;
 
-       if (td_write(td) && ftruncate(td->fd, td->file_size) == -1) {
-               td->error = errno;
+       if (!td->use_mmap)
+               return setup_file_plain(td);
+       else
+               return setup_file_mmap(td);
+}
+
+static int check_dev_match(dev_t dev, char *path)
+{
+       unsigned int major, minor;
+       char line[256], *p;
+       FILE *f;
+
+       f = fopen(path, "r");
+       if (!f) {
+               perror("open path");
                return 1;
        }
 
-       if (td->invalidate_cache) {
-               if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
-                       td->error = errno;
-                       return 1;
+       p = fgets(line, sizeof(line), f);
+       if (!p) {
+               fclose(f);
+               return 1;
+       }
+
+       if (sscanf(p, "%u:%u", &major, &minor) != 2) {
+               fclose(f);
+               return 1;
+       }
+
+       if (((major << 8) | minor) == dev) {
+               fclose(f);
+               return 0;
+       }
+
+       fclose(f);
+       return 1;
+}
+
+static char *find_block_dir(dev_t dev, char *path)
+{
+       struct dirent *dir;
+       char *found = NULL;
+       struct stat st;
+       DIR *D;
+
+       D = opendir(path);
+       if (!D)
+               return NULL;
+
+       while ((dir = readdir(D)) != NULL) {
+               char full_path[256];
+
+               if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
+                       continue;
+               if (!strcmp(dir->d_name, "device"))
+                       continue;
+
+               sprintf(full_path, "%s/%s", path, dir->d_name);
+
+               if (!strcmp(dir->d_name, "dev")) {
+                       if (!check_dev_match(dev, full_path)) {
+                               found = path;
+                               break;
+                       }
                }
+
+               if (stat(full_path, &st) == -1) {
+                       perror("stat");
+                       break;
+               }
+
+               if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
+                       continue;
+
+               if ((found = find_block_dir(dev, full_path)) != NULL)
+                       break;
        }
 
+       closedir(D);
+       return found;
+}
+
+static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
+{
+       unsigned in_flight;
+       char line[256];
+       FILE *f;
+       char *p;
+
+       f = fopen(du->path, "r");
+       if (!f)
+               return 1;
+
+       p = fgets(line, sizeof(line), f);
+       if (!p) {
+               fclose(f);
+               return 1;
+       }
+
+       if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
+               fclose(f);
+               return 1;
+       }
+
+       fclose(f);
        return 0;
 }
 
+static void update_io_tick_disk(struct disk_util *du)
+{
+       struct disk_util_stat __dus, *dus, *ldus;
+       struct timeval t;
+
+       if (get_io_ticks(du, &__dus))
+               return;
+
+       dus = &du->dus;
+       ldus = &du->last_dus;
+
+       dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
+       dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
+       dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
+       dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
+       dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
+       dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
+       dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
+       dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
+       dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
+       dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
+
+       gettimeofday(&t, NULL);
+       du->msec += mtime_since(&du->time, &t);
+       memcpy(&du->time, &t, sizeof(t));
+       memcpy(ldus, &__dus, sizeof(__dus));
+}
+
+static void update_io_ticks(void)
+{
+       struct list_head *entry;
+       struct disk_util *du;
+
+       list_for_each(entry, &disk_list) {
+               du = list_entry(entry, struct disk_util, list);
+               update_io_tick_disk(du);
+       }
+}
+
+static int disk_util_exists(dev_t dev)
+{
+       struct list_head *entry;
+       struct disk_util *du;
+
+       list_for_each(entry, &disk_list) {
+               du = list_entry(entry, struct disk_util, list);
+
+               if (du->dev == dev)
+                       return 1;
+       }
+
+       return 0;
+}
+
+static void disk_util_add(dev_t dev, char *path)
+{
+       struct disk_util *du = malloc(sizeof(*du));
+
+       memset(du, 0, sizeof(*du));
+       INIT_LIST_HEAD(&du->list);
+       sprintf(du->path, "%s/stat", path);
+       du->name = strdup(basename(path));
+       du->dev = dev;
+
+       gettimeofday(&du->time, NULL);
+       get_io_ticks(du, &du->last_dus);
+
+       list_add_tail(&du->list, &disk_list);
+}
+
+static void init_disk_util(struct thread_data *td)
+{
+       struct stat st;
+       char foo[256], tmp[256];
+       dev_t dev;
+       char *p, *dir;
+
+       if (!td->do_disk_util)
+               return;
+
+       if (!stat(td->file_name, &st)) {
+               if (S_ISBLK(st.st_mode))
+                       dev = st.st_rdev;
+               else
+                       dev = st.st_dev;
+       } else {
+               /*
+                * must be a file, open "." in that path
+                */
+               strcpy(foo, td->file_name);
+               p = dirname(foo);
+               if (stat(p, &st)) {
+                       perror("disk util stat");
+                       return;
+               }
+
+               dev = st.st_dev;
+       }
+
+       if (disk_util_exists(dev))
+               return;
+               
+       sprintf(foo, "/sys/block");
+       dir = find_block_dir(dev, foo);
+       if (!dir)
+               return;
+
+       /*
+        * if this is inside a partition dir, jump back to parent
+        */
+       sprintf(tmp, "%s/queue", dir);
+       if (stat(tmp, &st)) {
+               p = dirname(dir);
+               sprintf(tmp, "%s/queue", p);
+               if (stat(tmp, &st)) {
+                       fprintf(stderr, "unknown sysfs layout\n");
+                       return;
+               }
+               sprintf(foo, "%s", p);
+       }
+
+       disk_util_add(dev, foo);
+}
+
+static void disk_util_timer_arm(void)
+{
+       struct itimerval itimer;
+
+       itimer.it_value.tv_sec = 0;
+       itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
+       setitimer(ITIMER_REAL, &itimer, NULL);
+}
+
 static void clear_io_state(struct thread_data *td)
 {
-       if (!td->use_aio)
+       if (td->io_engine == FIO_SYNCIO)
                lseek(td->fd, SEEK_SET, 0);
 
        td->cur_off = 0;
@@ -1332,17 +1733,17 @@ static void *thread_main(void *data)
        if (init_io_u(td))
                goto err;
 
-       if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) {
-               td->error = errno;
+       if (fio_setaffinity(td) == -1) {
+               td_verror(td, errno);
                goto err;
        }
 
-       if (td->use_aio && init_aio(td))
+       if ((td->io_engine != FIO_SYNCIO) && init_aio(td))
                goto err;
 
        if (td->ioprio) {
                if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        goto err;
                }
        }
@@ -1356,6 +1757,8 @@ static void *thread_main(void *data)
        if (init_random_state(td))
                goto err;
 
+       gettimeofday(&td->epoch, NULL);
+
        while (td->loops--) {
                getrusage(RUSAGE_SELF, &td->ru_start);
                gettimeofday(&td->start, NULL);
@@ -1367,28 +1770,28 @@ static void *thread_main(void *data)
                clear_io_state(td);
                prune_io_piece_log(td);
 
-               if (!td->use_aio)
+               if (td->io_engine == FIO_SYNCIO)
                        do_sync_io(td);
                else
                        do_async_io(td);
 
-               if (td->error)
+               if (td->error || td->terminate)
                        break;
 
                td->runtime += mtime_since_now(&td->start);
                update_rusage_stat(td);
 
-               if (!td->verify)
+               if (td->verify == VERIFY_NONE)
                        continue;
 
                clear_io_state(td);
 
-               if (!td->use_aio)
+               if (td->io_engine == FIO_SYNCIO)
                        do_sync_verify(td);
                else
                        do_async_verify(td);
 
-               if (td->error)
+               if (td->error || td->terminate)
                        break;
        }
 
@@ -1407,7 +1810,9 @@ err:
                close(td->fd);
                td->fd = -1;
        }
-       if (td->use_aio)
+       if (td->mmap)
+               munmap(td->mmap, td->file_size);
+       if (td->io_engine != FIO_SYNCIO)
                cleanup_aio(td);
        cleanup_io_u(td);
        if (ret) {
@@ -1419,12 +1824,12 @@ err:
 
 }
 
-static void *fork_main(int shm_id, int offset)
+static void *fork_main(int shmid, int offset)
 {
        struct thread_data *td;
        void *data;
 
-       data = shmat(shm_id, NULL, 0);
+       data = shmat(shmid, NULL, 0);
        if (data == (void *) -1) {
                perror("shmat");
                return NULL;
@@ -1453,7 +1858,8 @@ static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
        return 1;
 }
 
-static void show_thread_status(struct thread_data *td)
+static void show_thread_status(struct thread_data *td,
+                              struct group_run_stats *rs)
 {
        int prio, prio_class;
        unsigned long min, max, bw = 0;
@@ -1474,8 +1880,12 @@ static void show_thread_status(struct thread_data *td)
                printf("  slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
        if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev))
                printf("  clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
-       if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev))
-               printf("  bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
+       if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) {
+               double p_of_agg;
+
+               p_of_agg = mean * 100 / (double) rs->agg[td->ddir];
+               printf("  bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev);
+       }
 
        if (td->runtime) {
                usr_cpu = (double) td->usr_time * 100 / (double) td->runtime;
@@ -1488,8 +1898,7 @@ static void show_thread_status(struct thread_data *td)
        printf("  cpu        : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
 }
 
-static void print_thread_status(struct thread_data *td, int nr_running,
-                               int t_rate, int m_rate)
+static void print_thread_status(int nr_running, int t_rate, int m_rate)
 {
        printf("Threads now running: %d", nr_running);
        if (m_rate || t_rate)
@@ -1539,7 +1948,7 @@ static void check_str_update(struct thread_data *td, int n, int t, int m)
        }
 
        run_str[td->thread_number - 1] = c;
-       print_thread_status(td, n, t, m);
+       print_thread_status(n, t, m);
        td->old_runstate = td->runstate;
 }
 
@@ -1575,7 +1984,7 @@ static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
        }
 }
 
-static void run_threads(char *argv[])
+static void run_threads(void)
 {
        struct timeval genesis;
        struct thread_data *td;
@@ -1586,6 +1995,7 @@ static void run_threads(char *argv[])
        fflush(stdout);
 
        signal(SIGINT, sig_handler);
+       signal(SIGALRM, sig_handler);
 
        todo = thread_number;
        nr_running = 0;
@@ -1595,6 +2005,8 @@ static void run_threads(char *argv[])
        for (i = 0; i < thread_number; i++) {
                td = &threads[i];
 
+               init_disk_util(td);
+
                if (!td->create_serialize)
                        continue;
 
@@ -1665,7 +2077,7 @@ static void run_threads(char *argv[])
                 * start created threads (TD_CREATED -> TD_RUNNING)
                 */
                for (i = 0; i < thread_number; i++) {
-                       struct thread_data *td = &threads[i];
+                       td = &threads[i];
 
                        if (td->runstate != TD_CREATED)
                                continue;
@@ -1680,7 +2092,7 @@ static void run_threads(char *argv[])
                }
 
                for (i = 0; i < thread_number; i++) {
-                       struct thread_data *td = &threads[i];
+                       td = &threads[i];
 
                        if (td->runstate != TD_RUNNING &&
                            td->runstate != TD_VERIFYING)
@@ -1699,14 +2111,9 @@ static void run_threads(char *argv[])
                reap_threads(&nr_running, &t_rate, &m_rate);
                usleep(10000);
        }
-}
 
-struct group_run_stats {
-       unsigned long max_run[2], min_run[2];
-       unsigned long max_bw[2], min_bw[2];
-       unsigned long io_mb[2];
-       unsigned long agg[2];
-};
+       update_io_ticks();
+}
 
 static void show_group_stats(struct group_run_stats *rs, int id)
 {
@@ -1718,9 +2125,31 @@ static void show_group_stats(struct group_run_stats *rs, int id)
                printf("  WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]);
 }
 
+static void show_disk_util(void)
+{
+       struct disk_util_stat *dus;
+       struct list_head *entry;
+       struct disk_util *du;
+       double util;
+
+       printf("\nDisk stats (read/write):\n");
+
+       list_for_each(entry, &disk_list) {
+               du = list_entry(entry, struct disk_util, list);
+               dus = &du->dus;
+
+               util = (double) 100 * du->dus.io_ticks / (double) du->msec;
+               if (util > 100.0)
+                       util = 100.0;
+
+               printf("  %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util);
+       }
+}
+
 static void show_run_stats(void)
 {
        struct group_run_stats *runstats, *rs;
+       struct thread_data *td;
        int i;
 
        runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
@@ -1734,30 +2163,32 @@ static void show_run_stats(void)
        }
 
        for (i = 0; i < thread_number; i++) {
-               struct thread_data *td = &threads[i];
                unsigned long bw = 0;
 
-               rs = &runstats[td->groupid];
+               td = &threads[i];
 
-               if (!td->error) {
-                       if (td->runtime < rs->min_run[td->ddir])
-                               rs->min_run[td->ddir] = td->runtime;
-                       if (td->runtime > rs->max_run[td->ddir])
-                               rs->max_run[td->ddir] = td->runtime;
+               if (td->error) {
+                       printf("Client%d: %s\n", td->thread_number, td->verror);
+                       continue;
+               }
 
-                       if (td->runtime)
-                               bw = td->io_bytes / td->runtime;
-                       if (bw < rs->min_bw[td->ddir])
-                               rs->min_bw[td->ddir] = bw;
-                       if (bw > rs->max_bw[td->ddir])
-                               rs->max_bw[td->ddir] = bw;
+               rs = &runstats[td->groupid];
 
-                       rs->io_mb[td->ddir] += td->io_bytes >> 20;
-               }
+               if (td->runtime < rs->min_run[td->ddir])
+                       rs->min_run[td->ddir] = td->runtime;
+               if (td->runtime > rs->max_run[td->ddir])
+                       rs->max_run[td->ddir] = td->runtime;
+
+               if (td->runtime)
+                       bw = td->io_bytes / td->runtime;
+               if (bw < rs->min_bw[td->ddir])
+                       rs->min_bw[td->ddir] = bw;
+               if (bw > rs->max_bw[td->ddir])
+                       rs->max_bw[td->ddir] = bw;
 
-               show_thread_status(td);
+               rs->io_mb[td->ddir] += td->io_bytes >> 20;
        }
-       
+
        for (i = 0; i < groupid + 1; i++) {
                rs = &runstats[i];
 
@@ -1765,9 +2196,19 @@ static void show_run_stats(void)
                        rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
                if (rs->max_run[1])
                        rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
+       }
+
+       for (i = 0; i < thread_number; i++) {
+               td = &threads[i];
+               rs = &runstats[td->groupid];
 
-               show_group_stats(rs, i);
+               show_thread_status(td, rs);
        }
+
+       for (i = 0; i < groupid + 1; i++)
+               show_group_stats(&runstats[i], i);
+
+       show_disk_util();
 }
 
 int main(int argc, char *argv[])
@@ -1782,7 +2223,9 @@ int main(int argc, char *argv[])
                return 1;
        }
 
-       run_threads(argv);
+       disk_util_timer_arm();
+
+       run_threads();
        show_run_stats();
 
        return 0;