[PATCH] fio: pretty close to compiling/working on FreeBSD now
[disktools.git] / fio.c
diff --git a/fio.c b/fio.c
index a789bd86881f22c8aa0d76134b68113d7de830bd..ca79cb63756bccbf09ffdedaad419463a4ce8fca 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -28,7 +28,6 @@
 #include <time.h>
 #include <math.h>
 #include <assert.h>
-#include <pthread.h>
 #include <dirent.h>
 #include <libgen.h>
 #include <sys/types.h>
@@ -38,9 +37,9 @@
 #include <sys/shm.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
-#include <asm/unistd.h>
 
 #include "fio.h"
+#include "os.h"
 
 #define MASK   (4095)
 
@@ -50,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
@@ -63,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;
                }
@@ -100,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)
@@ -190,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;
@@ -258,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;
@@ -271,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);
@@ -296,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)
@@ -308,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);
@@ -320,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;
 
@@ -418,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;
@@ -592,6 +589,11 @@ 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);
 
@@ -600,12 +602,8 @@ static struct io_u *get_io_u(struct thread_data *td)
        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;
@@ -695,12 +693,12 @@ static void do_sync_verify(struct thread_data *td)
        if (!td->odirect) {
                if (!td->use_mmap) {
                        if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) {
-                               td->error = errno;
+                               td_verror(td, errno);
                                goto out;
                        }
                } else {
                        if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) {
-                               td->error = errno;
+                               td_verror(td, errno);
                                goto out;
                        }
                }
@@ -719,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;
                        }
                }
@@ -727,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;
@@ -770,7 +768,7 @@ 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->error = errno;
+                       td_verror(td, errno);
                        return 1;
                }
        }
@@ -812,7 +810,7 @@ static void do_sync_io(struct thread_data *td)
 
                if (ret < (int) io_u->buflen) {
                        if (ret == -1)
-                               td->error = errno;
+                               td_verror(td, errno);
                        break;
                }
 
@@ -831,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;
                }
 
@@ -863,39 +861,15 @@ static void do_sync_io(struct thread_data *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)
 {
@@ -907,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;
@@ -948,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);
        }
@@ -999,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;
                }
 
@@ -1017,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;
 
@@ -1065,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;
                }
 
@@ -1081,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;
@@ -1099,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;
                }
 
@@ -1123,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)
@@ -1173,7 +1149,7 @@ static int init_io_u(struct thread_data *td)
        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;
@@ -1185,22 +1161,22 @@ static int init_io_u(struct thread_data *td)
        else if (td->mem_type == MEM_SHM) {
                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 | MAP_ANONYMOUS, 0, 0);
+               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->error = errno;
+                       td_verror(td, errno);
                        perror("mmap");
                        td->orig_buffer = NULL;
                        return 1;
@@ -1224,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
@@ -1237,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;
        }
 
@@ -1260,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;
@@ -1273,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);
@@ -1290,32 +1275,17 @@ static int create_file(struct thread_data *td)
        return 0;
 }
 
-static int file_exists(struct thread_data *td)
-{
-       struct stat st;
-
-       if (stat(td->file_name, &st) != -1)
-               return 1;
-
-       return errno != ENOENT;
-}
-
 static int file_size(struct thread_data *td)
 {
        struct stat st;
 
        if (fstat(td->fd, &st) == -1) {
-               td->error = errno;
+               td_verror(td, errno);
                return 1;
        }
 
-       if (td_read(td)) {
-               if (!td->file_size || td->file_size > st.st_size)
-                       td->file_size = st.st_size;
-       } else {
-               if (!td->file_size)
-                       td->file_size = 1024 * 1024 * 1024;
-       }
+       if (!td->file_size)
+               td->file_size = st.st_size;
 
        return 0;
 }
@@ -1325,11 +1295,14 @@ static int bdev_size(struct thread_data *td)
        size_t bytes;
 
        if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) {
-               td->error = errno;
+               td_verror(td, errno);
                return 1;
        }
 
-       if (!td->file_size || (td->file_size > bytes))
+       /*
+        * no extend possibilities, so limit size to device size if too large
+        */
+       if (!td->file_size || td->file_size > bytes)
                td->file_size = bytes;
 
        return 0;
@@ -1348,14 +1321,14 @@ static int get_file_size(struct thread_data *td)
                return ret;
 
        if (td->file_offset > td->file_size) {
-               fprintf(stderr, "Client%d: offset larger than length\n", td->thread_number);
+               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 = 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;
        }
 
@@ -1378,25 +1351,25 @@ static int setup_file_mmap(struct thread_data *td)
        td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
        if (td->mmap == MAP_FAILED) {
                td->mmap = NULL;
-               td->error = errno;
+               td_verror(td, errno);
                return 1;
        }
 
        if (td->invalidate_cache) {
                if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        return 1;
                }
        }
 
        if (td->sequential) {
                if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        return 1;
                }
        } else {
                if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        return 1;
                }
        }
@@ -1408,19 +1381,19 @@ 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->error = errno;
+                       td_verror(td, errno);
                        return 1;
                }
        }
 
        if (td->sequential) {
                if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        return 1;
                }
        } else {
                if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
-                       td->error = errno;
+                       td_verror(td, errno);
                        return 1;
                }
        }
@@ -1430,15 +1403,25 @@ static int setup_file_plain(struct thread_data *td)
 
 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)
@@ -1447,29 +1430,28 @@ 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;
 
                flags |= O_RDWR;
 
-               td->fd = open(td->file_name, flags | O_CREAT, 0600);
+               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;
-               return 1;
-       }
-
        if (!td->use_mmap)
                return setup_file_plain(td);
        else
@@ -1478,7 +1460,7 @@ static int setup_file(struct thread_data *td)
 
 static int check_dev_match(dev_t dev, char *path)
 {
-       int major, minor;
+       unsigned int major, minor;
        char line[256], *p;
        FILE *f;
 
@@ -1552,62 +1534,138 @@ static char *find_block_dir(dev_t dev, char *path)
        return found;
 }
 
-static int get_io_ticks(struct thread_data *td)
+static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
 {
-       int i1, i2, i3, i4, i5, i6, i7, i8, i9;
-       unsigned long long ull1, ull2;
+       unsigned in_flight;
        char line[256];
        FILE *f;
        char *p;
 
-       f = fopen(td->disk_stat_path, "r");
+       f = fopen(du->path, "r");
        if (!f)
-               return 0;
+               return 1;
 
        p = fgets(line, sizeof(line), f);
        if (!p) {
                fclose(f);
-               return 0;
+               return 1;
        }
 
-       if (sscanf(p, "%8u %8u %8llu %8u %8u %8u %8llu %8u %8u %8u %8u", &i1, &i2, &ull1, &i3, &i4, &i5, &ull2, &i6, &i7, &i8, &i9) != 11) {
+       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 0;
+               return 1;
        }
 
        fclose(f);
-       return i8;
+       return 0;
 }
 
-static void finish_disk_stats(struct thread_data *td)
+static void update_io_tick_disk(struct disk_util *du)
 {
-       unsigned long ticks;
+       struct disk_util_stat __dus, *dus, *ldus;
+       struct timeval t;
 
-       ticks = get_io_ticks(td);
-       td->io_ticks = ticks - td->io_ticks;
+       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 int init_disk_stats(struct thread_data *td)
+static void init_disk_util(struct thread_data *td)
 {
        struct stat st;
        char foo[256], tmp[256];
        dev_t dev;
        char *p, *dir;
 
-       if (fstat(td->fd, &st) < 0) {
-               td->error = errno;
-               return 1;
-       }
+       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;
+               }
 
-       if (td->filetype == FIO_TYPE_FILE)
                dev = st.st_dev;
-       else
-               dev = st.st_rdev;
+       }
+
+       if (disk_util_exists(dev))
+               return;
                
        sprintf(foo, "/sys/block");
        dir = find_block_dir(dev, foo);
        if (!dir)
-               return 0;
+               return;
 
        /*
         * if this is inside a partition dir, jump back to parent
@@ -1618,18 +1676,26 @@ static int init_disk_stats(struct thread_data *td)
                sprintf(tmp, "%s/queue", p);
                if (stat(tmp, &st)) {
                        fprintf(stderr, "unknown sysfs layout\n");
-                       return 0;
+                       return;
                }
-               sprintf(td->disk_stat_path, "%s/stat", p);
+               sprintf(foo, "%s", p);
        }
 
-       td->io_ticks = get_io_ticks(td);
-       return 0;
+       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;
@@ -1667,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;
                }
        }
@@ -1688,12 +1754,11 @@ static void *thread_main(void *data)
        if (!td->create_serialize && setup_file(td))
                goto err;
 
-       if (init_disk_stats(td))
-               goto err;
-
        if (init_random_state(td))
                goto err;
 
+       gettimeofday(&td->epoch, NULL);
+
        while (td->loops--) {
                getrusage(RUSAGE_SELF, &td->ru_start);
                gettimeofday(&td->start, NULL);
@@ -1705,12 +1770,12 @@ 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);
@@ -1721,19 +1786,17 @@ static void *thread_main(void *data)
 
                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;
        }
 
        ret = 0;
 
-       finish_disk_stats(td);
-
        if (td->bw_log)
                finish_log(td, td->bw_log, "bw");
        if (td->lat_log)
@@ -1749,7 +1812,7 @@ err:
        }
        if (td->mmap)
                munmap(td->mmap, td->file_size);
-       if (td->use_aio)
+       if (td->io_engine != FIO_SYNCIO)
                cleanup_aio(td);
        cleanup_io_u(td);
        if (ret) {
@@ -1761,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;
@@ -1833,15 +1896,9 @@ 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);
-       
-       if (td->io_ticks) {
-               double disk_util = (double) 100 * td->io_ticks / (double) td->runtime;
-               printf("  disk       : util=%3.2f%%\n", disk_util);
-       }
 }
 
-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)
@@ -1891,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;
 }
 
@@ -1927,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;
@@ -1938,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;
@@ -1947,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;
 
@@ -2017,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;
@@ -2032,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)
@@ -2051,6 +2111,8 @@ static void run_threads(char *argv[])
                reap_threads(&nr_running, &t_rate, &m_rate);
                usleep(10000);
        }
+
+       update_io_ticks();
 }
 
 static void show_group_stats(struct group_run_stats *rs, int id)
@@ -2063,6 +2125,27 @@ 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;
@@ -2084,8 +2167,10 @@ static void show_run_stats(void)
 
                td = &threads[i];
 
-               if (td->error)
+               if (td->error) {
+                       printf("Client%d: %s\n", td->thread_number, td->verror);
                        continue;
+               }
 
                rs = &runstats[td->groupid];
 
@@ -2122,6 +2207,8 @@ static void show_run_stats(void)
 
        for (i = 0; i < groupid + 1; i++)
                show_group_stats(&runstats[i], i);
+
+       show_disk_util();
 }
 
 int main(int argc, char *argv[])
@@ -2136,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;