[PATCH] blkparse: Add option to print missing entries
[blktrace.git] / blkparse.c
index 087c7993e07a0fe0b9d244ec8e9730bcc4d230c2..a1ebcd2a54b3985abf33fc926b058592dfde0e93 100644 (file)
 #include <signal.h>
 #include <locale.h>
 #include <limits.h>
-#include <ctype.h>
 
 #include "blktrace.h"
 #include "rbtree.h"
+#include "jhash.h"
 
-#define SECONDS(x)             ((unsigned long long)(x) / 1000000000)
-#define NANO_SECONDS(x)                ((unsigned long long)(x) % 1000000000)
-#define DOUBLE_TO_NANO_ULL(d)  ((unsigned long long)((d) * 1000000000))
-
-#define MINORBITS      20
-#define MINORMASK      ((1U << MINORBITS) - 1)
-#define MAJOR(dev)     ((unsigned int) ((dev) >> MINORBITS))
-#define MINOR(dev)     ((unsigned int) ((dev) & MINORMASK))
-
-#define min(a, b)      ((a) < (b) ? (a) : (b))
-
-#define HEADER         "%D %2c %8s %5T.%9t %5p %2a %3d "
-
-struct io_stats {
-       unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
-       unsigned long ireads, iwrites;
-       unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
-       unsigned long long iread_kb, iwrite_kb;
-       unsigned long io_unplugs, timer_unplugs;
-};
-
-struct per_cpu_info {
-       int cpu;
-       int nelems;
-
-       int fd;
-       char fname[128];
-
-       struct io_stats io_stats;
-};
+static char blkparse_version[] = "0.90";
 
 struct per_dev_info {
-       dev_t id;
+       dev_t dev;
        char *name;
 
        int backwards;
        unsigned long long events;
        unsigned long long last_reported_time;
+       unsigned long long last_read_time;
        struct io_stats io_stats;
        unsigned long last_sequence;
        unsigned long skips;
 
+       struct rb_root rb_last;
+       unsigned long rb_last_entries;
+
+       struct rb_root rb_track;
+
+       int nfiles;
        int ncpus;
        struct per_cpu_info *cpus;
 };
@@ -86,6 +64,7 @@ struct per_process_info {
        __u32 pid;
        struct io_stats io_stats;
        struct per_process_info *hash_next, *list_next;
+       int more_than_one;
 
        /*
         * individual io stats
@@ -96,11 +75,13 @@ struct per_process_info {
 };
 
 #define PPI_HASH_SHIFT (8)
-static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
+#define PPI_HASH_SIZE  (1 << PPI_HASH_SHIFT)
+#define PPI_HASH_MASK  (PPI_HASH_SIZE - 1)
+static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
 static struct per_process_info *ppi_list;
 static int ppi_list_entries;
 
-#define S_OPTS "i:o:b:stqw:f:F:"
+#define S_OPTS "i:o:b:stqw:f:F:vnm"
 static struct option l_opts[] = {
        {
                .name = "input",
@@ -156,6 +137,24 @@ static struct option l_opts[] = {
                .flag = NULL,
                .val = 'F'
        },
+       {
+               .name = "hash by name",
+               .has_arg = no_argument,
+               .flag = NULL,
+               .val = 'n'
+       },
+       {
+               .name = "missing",
+               .has_arg = no_argument,
+               .flag = NULL,
+               .val = 'm'
+       },
+       {
+               .name = "version",
+               .has_arg = no_argument,
+               .flag = NULL,
+               .val = 'v'
+       },
 };
 
 /*
@@ -165,23 +164,28 @@ struct trace {
        struct blk_io_trace *bit;
        struct rb_node rb_node;
        struct trace *next;
-       int skipped;
 };
 
 static struct rb_root rb_sort_root;
-static struct rb_root rb_track_root;
+static unsigned long rb_sort_entries;
 
 static struct trace *trace_list;
 
+/*
+ * allocation cache
+ */
+static struct blk_io_trace *bit_alloc_list;
+static struct trace *t_alloc_list;
+
 /*
  * for tracking individual ios
  */
 struct io_track {
        struct rb_node rb_node;
 
-       dev_t device;
        __u64 sector;
        __u32 pid;
+       char comm[16];
        unsigned long long allocation_time;
        unsigned long long queue_time;
        unsigned long long dispatch_time;
@@ -192,43 +196,57 @@ static int ndevices;
 static struct per_dev_info *devices;
 static char *get_dev_name(struct per_dev_info *, char *, int);
 
-static FILE *ofp;
+FILE *ofp = NULL;
 static char *output_name;
 
 static unsigned long long genesis_time;
+static unsigned long long last_allowed_time;
+static unsigned int smallest_seq_read;
 static unsigned long long stopwatch_start;     /* start from zero by default */
 static unsigned long long stopwatch_end = ULONG_LONG_MAX;      /* "infinity" */
 
 static int per_process_stats;
 static int track_ios;
+static int ppi_hash_by_pid = 1;
+static int print_missing;
+
+static unsigned int t_alloc_cache;
+static unsigned int bit_alloc_cache;
 
-#define RB_BATCH_DEFAULT       (1024)
-static int rb_batch = RB_BATCH_DEFAULT;
+#define RB_BATCH_DEFAULT       (512)
+static unsigned int rb_batch = RB_BATCH_DEFAULT;
 
 static int pipeline;
 
 #define is_done()      (*(volatile int *)(&done))
 static volatile int done;
 
-static inline unsigned long hash_long(unsigned long val)
+#define JHASH_RANDOM   (0x3af5f2ee)
+
+static inline int ppi_hash_pid(__u32 pid)
 {
-#if __WORDSIZE == 32
-       val *= 0x9e370001UL;
-#elif __WORDSIZE == 64
-       val *= 0x9e37fffffffc0001UL;
-#else
-#error unknown word size
-#endif
+       return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
+}
 
-       return val >> (__WORDSIZE - PPI_HASH_SHIFT);
+static inline int ppi_hash_name(const char *name)
+{
+       return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
+}
+
+static inline int ppi_hash(struct per_process_info *ppi)
+{
+       if (ppi_hash_by_pid)
+               return ppi_hash_pid(ppi->pid);
+
+       return ppi_hash_name(ppi->name);
 }
 
 static inline void add_process_to_hash(struct per_process_info *ppi)
 {
-       const int hash_idx = hash_long(ppi->pid);
+       const int hash_idx = ppi_hash(ppi);
 
-       ppi->hash_next = ppi_hash[hash_idx];
-       ppi_hash[hash_idx] = ppi;
+       ppi->hash_next = ppi_hash_table[hash_idx];
+       ppi_hash_table[hash_idx] = ppi;
 }
 
 static inline void add_process_to_list(struct per_process_info *ppi)
@@ -238,12 +256,28 @@ static inline void add_process_to_list(struct per_process_info *ppi)
        ppi_list_entries++;
 }
 
+static struct per_process_info *find_process_by_name(char *name)
+{
+       const int hash_idx = ppi_hash_name(name);
+       struct per_process_info *ppi;
+
+       ppi = ppi_hash_table[hash_idx];
+       while (ppi) {
+               if (!strcmp(ppi->name, name))
+                       return ppi;
+
+               ppi = ppi->hash_next;
+       }
+
+       return NULL;
+}
+
 static struct per_process_info *find_process_by_pid(__u32 pid)
 {
-       const int hash_idx = hash_long(pid);
+       const int hash_idx = ppi_hash_pid(pid);
        struct per_process_info *ppi;
 
-       ppi = ppi_hash[hash_idx];
+       ppi = ppi_hash_table[hash_idx];
        while (ppi) {
                if (ppi->pid == pid)
                        return ppi;
@@ -254,24 +288,42 @@ static struct per_process_info *find_process_by_pid(__u32 pid)
        return NULL;
 }
 
-static inline int trace_rb_insert(struct trace *t)
+static struct per_process_info *find_process(__u32 pid, char *name)
 {
-       struct rb_node **p = &rb_sort_root.rb_node;
+       struct per_process_info *ppi;
+
+       if (ppi_hash_by_pid)
+               return find_process_by_pid(pid);
+
+       ppi = find_process_by_name(name);
+       if (ppi && ppi->pid != pid)
+               ppi->more_than_one = 1;
+
+       return ppi;
+}
+
+static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
+                                 int check_time)
+{
+       struct rb_node **p = &root->rb_node;
        struct rb_node *parent = NULL;
        struct trace *__t;
 
-       if (genesis_time == 0 || t->bit->time < genesis_time)
-               genesis_time = t->bit->time;
-
        while (*p) {
                parent = *p;
+
                __t = rb_entry(parent, struct trace, rb_node);
 
-               if (t->bit->time < __t->bit->time)
-                       p = &(*p)->rb_left;
-               else if (t->bit->time > __t->bit->time)
-                       p = &(*p)->rb_right;
-               else if (t->bit->device < __t->bit->device)
+               if (check_time) {
+                       if (t->bit->time < __t->bit->time) {
+                               p = &(*p)->rb_left;
+                               continue;
+                       } else if (t->bit->time > __t->bit->time) {
+                               p = &(*p)->rb_right;
+                               continue;
+                       }
+               }
+               if (t->bit->device < __t->bit->device)
                        p = &(*p)->rb_left;
                else if (t->bit->device > __t->bit->device)
                        p = &(*p)->rb_right;
@@ -289,26 +341,99 @@ static inline int trace_rb_insert(struct trace *t)
        }
 
        rb_link_node(&t->rb_node, parent, p);
-       rb_insert_color(&t->rb_node, &rb_sort_root);
+       rb_insert_color(&t->rb_node, root);
        return 0;
 }
 
-static inline int track_rb_insert(struct io_track *iot)
+static inline int trace_rb_insert_sort(struct trace *t)
 {
-       struct rb_node **p = &rb_track_root.rb_node;
+       if (!trace_rb_insert(t, &rb_sort_root, 1)) {
+               rb_sort_entries++;
+               return 0;
+       }
+
+       return 1;
+}
+
+static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
+{
+       if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
+               pdi->rb_last_entries++;
+               return 0;
+       }
+
+       return 1;
+}
+
+static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
+                                  struct rb_root *root, int order)
+{
+       struct rb_node *n = root->rb_node;
+       struct rb_node *prev = NULL;
+       struct trace *__t;
+
+       while (n) {
+               __t = rb_entry(n, struct trace, rb_node);
+               prev = n;
+
+               if (device < __t->bit->device)
+                       n = n->rb_left;
+               else if (device > __t->bit->device)
+                       n = n->rb_right;
+               else if (sequence < __t->bit->sequence)
+                       n = n->rb_left;
+               else if (sequence > __t->bit->sequence)
+                       n = n->rb_right;
+               else
+                       return __t;
+       }
+
+       /*
+        * hack - the list may not be sequence ordered because some
+        * events don't have sequence and time matched. so we end up
+        * being a little off in the rb lookup here, because we don't
+        * know the time we are looking for. compensate by browsing
+        * a little ahead from the last entry to find the match
+        */
+       if (order && prev) {
+               int max = 5;
+
+               while (((n = rb_next(prev)) != NULL) && max--) {
+                       __t = rb_entry(n, struct trace, rb_node);
+                       
+                       if (__t->bit->device == device &&
+                           __t->bit->sequence == sequence)
+                               return __t;
+
+                       prev = n;
+               }
+       }
+                       
+       return NULL;
+}
+
+static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
+{
+       return trace_rb_find(dev, seq, &rb_sort_root, 1);
+}
+
+static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
+                                              unsigned long seq)
+{
+       return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
+}
+
+static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
+{
+       struct rb_node **p = &pdi->rb_track.rb_node;
        struct rb_node *parent = NULL;
        struct io_track *__iot;
 
        while (*p) {
                parent = *p;
-
                __iot = rb_entry(parent, struct io_track, rb_node);
 
-               if (iot->device < __iot->device)
-                       p = &(*p)->rb_left;
-               else if (iot->device > __iot->device)
-                       p = &(*p)->rb_right;
-               else if (iot->sector < __iot->sector)
+               if (iot->sector < __iot->sector)
                        p = &(*p)->rb_left;
                else if (iot->sector > __iot->sector)
                        p = &(*p)->rb_right;
@@ -316,35 +441,28 @@ static inline int track_rb_insert(struct io_track *iot)
                        fprintf(stderr,
                                "sector alias (%Lu) on device %d,%d!\n",
                                (unsigned long long) iot->sector,
-                               MAJOR(iot->device), MINOR(iot->device));
+                               MAJOR(pdi->dev), MINOR(pdi->dev));
                        return 1;
                }
        }
 
        rb_link_node(&iot->rb_node, parent, p);
-       rb_insert_color(&iot->rb_node, &rb_track_root);
+       rb_insert_color(&iot->rb_node, &pdi->rb_track);
        return 0;
 }
 
-static struct io_track *__find_track(dev_t device, __u64 sector)
+static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
 {
-       struct rb_node **p = &rb_track_root.rb_node;
-       struct rb_node *parent = NULL;
+       struct rb_node *n = pdi->rb_track.rb_node;
        struct io_track *__iot;
 
-       while (*p) {
-               parent = *p;
-               
-               __iot = rb_entry(parent, struct io_track, rb_node);
+       while (n) {
+               __iot = rb_entry(n, struct io_track, rb_node);
 
-               if (device < __iot->device)
-                       p = &(*p)->rb_left;
-               else if (device > __iot->device)
-                       p = &(*p)->rb_right;
-               else if (sector < __iot->sector)
-                       p = &(*p)->rb_left;
+               if (sector < __iot->sector)
+                       n = n->rb_left;
                else if (sector > __iot->sector)
-                       p = &(*p)->rb_right;
+                       n = n->rb_right;
                else
                        return __iot;
        }
@@ -352,56 +470,60 @@ static struct io_track *__find_track(dev_t device, __u64 sector)
        return NULL;
 }
 
-static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
+static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
+                                  char *comm, __u64 sector)
 {
        struct io_track *iot;
 
-       iot = __find_track(device, sector);
+       iot = __find_track(pdi, sector);
        if (!iot) {
                iot = malloc(sizeof(*iot));
                iot->pid = pid;
-               iot->device = device;
+               memcpy(iot->comm, comm, sizeof(iot->comm));
                iot->sector = sector;
-               track_rb_insert(iot);
+               track_rb_insert(pdi, iot);
        }
 
        return iot;
 }
 
-static void log_track_frontmerge(struct blk_io_trace *t)
+static void log_track_frontmerge(struct per_dev_info *pdi,
+                                struct blk_io_trace *t)
 {
        struct io_track *iot;
 
        if (!track_ios)
                return;
 
-       iot = __find_track(t->device, t->sector + (t->bytes >> 9));
+       iot = __find_track(pdi, t->sector + (t->bytes >> 9));
        if (!iot) {
-               fprintf(stderr, "failed to find mergeable event\n");
+               fprintf(stderr, "merge not found for (%d,%d): %llu\n",
+                       MAJOR(pdi->dev), MINOR(pdi->dev),
+                       (unsigned long long) t->sector + (t->bytes >> 9));
                return;
        }
 
-       rb_erase(&iot->rb_node, &rb_track_root);
+       rb_erase(&iot->rb_node, &pdi->rb_track);
        iot->sector -= t->bytes >> 9;
-       track_rb_insert(iot);
+       track_rb_insert(pdi, iot);
 }
 
-static void log_track_getrq(struct blk_io_trace *t)
+static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
 {
        struct io_track *iot;
 
        if (!track_ios)
                return;
 
-       iot = find_track(t->pid, t->device, t->sector);
+       iot = find_track(pdi, t->pid, t->comm, t->sector);
        iot->allocation_time = t->time;
 }
 
-
 /*
- * return time between rq allocation and queue
+ * return time between rq allocation and insertion
  */
-static unsigned long long log_track_queue(struct blk_io_trace *t)
+static unsigned long long log_track_insert(struct per_dev_info *pdi,
+                                          struct blk_io_trace *t)
 {
        unsigned long long elapsed;
        struct io_track *iot;
@@ -409,12 +531,16 @@ static unsigned long long log_track_queue(struct blk_io_trace *t)
        if (!track_ios)
                return -1;
 
-       iot = find_track(t->pid, t->device, t->sector);
+       iot = find_track(pdi, t->pid, t->comm, t->sector);
        iot->queue_time = t->time;
+
+       if (!iot->allocation_time)
+               return -1;
+
        elapsed = iot->queue_time - iot->allocation_time;
 
        if (per_process_stats) {
-               struct per_process_info *ppi = find_process_by_pid(iot->pid);
+               struct per_process_info *ppi = find_process(iot->pid,iot->comm);
                int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
 
                if (ppi && elapsed > ppi->longest_allocation_wait[w])
@@ -427,7 +553,8 @@ static unsigned long long log_track_queue(struct blk_io_trace *t)
 /*
  * return time between queue and issue
  */
-static unsigned long long log_track_issue(struct blk_io_trace *t)
+static unsigned long long log_track_issue(struct per_dev_info *pdi,
+                                         struct blk_io_trace *t)
 {
        unsigned long long elapsed;
        struct io_track *iot;
@@ -437,9 +564,11 @@ static unsigned long long log_track_issue(struct blk_io_trace *t)
        if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
                return -1;
 
-       iot = __find_track(t->device, t->sector);
+       iot = __find_track(pdi, t->sector);
        if (!iot) {
-               fprintf(stderr, "failed to find issue event\n");
+               fprintf(stderr, "issue not found for (%d,%d): %llu\n",
+                       MAJOR(pdi->dev), MINOR(pdi->dev),
+                       (unsigned long long) t->sector);
                return -1;
        }
 
@@ -447,7 +576,7 @@ static unsigned long long log_track_issue(struct blk_io_trace *t)
        elapsed = iot->dispatch_time - iot->queue_time;
 
        if (per_process_stats) {
-               struct per_process_info *ppi = find_process_by_pid(iot->pid);
+               struct per_process_info *ppi = find_process(iot->pid,iot->comm);
                int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
 
                if (ppi && elapsed > ppi->longest_dispatch_wait[w])
@@ -460,7 +589,8 @@ static unsigned long long log_track_issue(struct blk_io_trace *t)
 /*
  * return time between dispatch and complete
  */
-static unsigned long long log_track_complete(struct blk_io_trace *t)
+static unsigned long long log_track_complete(struct per_dev_info *pdi,
+                                            struct blk_io_trace *t)
 {
        unsigned long long elapsed;
        struct io_track *iot;
@@ -470,9 +600,11 @@ static unsigned long long log_track_complete(struct blk_io_trace *t)
        if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
                return -1;
 
-       iot = __find_track(t->device, t->sector);
+       iot = __find_track(pdi, t->sector);
        if (!iot) {
-               fprintf(stderr, "failed to find complete event\n");
+               fprintf(stderr, "complete not found for (%d,%d): %llu\n",
+                       MAJOR(pdi->dev), MINOR(pdi->dev),
+                       (unsigned long long) t->sector);
                return -1;
        }
 
@@ -480,7 +612,7 @@ static unsigned long long log_track_complete(struct blk_io_trace *t)
        elapsed = iot->completion_time - iot->dispatch_time;
 
        if (per_process_stats) {
-               struct per_process_info *ppi = find_process_by_pid(iot->pid);
+               struct per_process_info *ppi = find_process(iot->pid,iot->comm);
                int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
 
                if (ppi && elapsed > ppi->longest_completion_wait[w])
@@ -490,7 +622,7 @@ static unsigned long long log_track_complete(struct blk_io_trace *t)
        /*
         * kill the trace, we don't need it after completion
         */
-       rb_erase(&iot->rb_node, &rb_track_root);
+       rb_erase(&iot->rb_node, &pdi->rb_track);
        free(iot);
 
        return elapsed;
@@ -499,12 +631,12 @@ static unsigned long long log_track_complete(struct blk_io_trace *t)
 
 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
 {
-       struct per_process_info *ppi = find_process_by_pid(pid);
+       struct per_process_info *ppi = find_process(pid, name);
 
        if (!ppi) {
                ppi = malloc(sizeof(*ppi));
                memset(ppi, 0, sizeof(*ppi));
-               strncpy(ppi->name, name, sizeof(ppi->name));
+               memcpy(ppi->name, name, 16);
                ppi->pid = pid;
                add_process_to_hash(ppi);
                add_process_to_list(ppi);
@@ -513,7 +645,6 @@ static struct io_stats *find_process_io_stats(__u32 pid, char *name)
        return &ppi->io_stats;
 }
 
-
 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
 {
        struct per_cpu_info *cpus = pdi->cpus;
@@ -567,20 +698,27 @@ static int resize_devices(char *name)
        return 0;
 }
 
-static struct per_dev_info *get_dev_info(dev_t id)
+static struct per_dev_info *get_dev_info(dev_t dev)
 {
        struct per_dev_info *pdi;
        int i;
 
-       for (i = 0; i < ndevices; i++)
-               if (devices[i].id == id)
+       for (i = 0; i < ndevices; i++) {
+               if (!devices[i].dev)
+                       devices[i].dev = dev;
+               if (devices[i].dev == dev)
                        return &devices[i];
+       }
 
-       if (resize_devices(NULL) != 0)
+       if (resize_devices(NULL))
                return NULL;
 
        pdi = &devices[ndevices - 1];
-       pdi->id = id;
+       pdi->dev = dev;
+       pdi->last_sequence = -1;
+       pdi->last_read_time = 0;
+       memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
+       pdi->rb_last_entries = 0;
        return pdi;
 }
 
@@ -589,7 +727,7 @@ static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
        if (pdi->name)
                snprintf(buffer, size, "%s", pdi->name);
        else
-               snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
+               snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
        return buffer;
 }
 
@@ -626,8 +764,8 @@ static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
        }
 }
 
-static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
-                              int rw)
+static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
+                                  int rw)
 {
        if (rw) {
                ios->qwrites++;
@@ -638,15 +776,15 @@ static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
        }
 }
 
-static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
-                            int rw)
+static inline void account_queue(struct blk_io_trace *t,
+                                struct per_cpu_info *pci, int rw)
 {
-       __account_q(&pci->io_stats, t, rw);
+       __account_queue(&pci->io_stats, t, rw);
 
        if (per_process_stats) {
                struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
 
-               __account_q(ios, t, rw);
+               __account_queue(ios, t, rw);
        }
 }
 
@@ -673,7 +811,8 @@ static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
        }
 }
 
-static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
+static inline void __account_issue(struct io_stats *ios, int rw,
+                                  unsigned int bytes)
 {
        if (rw) {
                ios->iwrites++;
@@ -684,15 +823,15 @@ static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
        }
 }
 
-static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
-                            int rw)
+static inline void account_issue(struct blk_io_trace *t,
+                                struct per_cpu_info *pci, int rw)
 {
-       __account_i(&pci->io_stats, rw, t->bytes);
+       __account_issue(&pci->io_stats, rw, t->bytes);
 
        if (per_process_stats) {
                struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
 
-               __account_i(ios, rw, t->bytes);
+               __account_issue(ios, rw, t->bytes);
        }
 }
 
@@ -716,317 +855,35 @@ static inline void account_unplug(struct blk_io_trace *t,
        }
 }
 
-#define VALID_SPECS    "BCDFGMPQRSTU"
-static char *override_format[256];
-static inline int valid_spec(int spec)
-{
-       return strchr(VALID_SPECS, spec) != NULL;
-}
-
-static void set_all_format_specs(char *optarg)
+static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
+                        struct blk_io_trace *t, char *act)
 {
-       char *p;
-
-       for (p = VALID_SPECS; *p; p++)
-               if (override_format[(int)(*p)] == NULL)
-                       override_format[(int)(*p)] = strdup(optarg);
-}
-
-static int add_format_spec(char *optarg)
-{
-       int spec = optarg[0];
-
-       if (!valid_spec(spec)) {
-               fprintf(stderr,"Bad format specifier %c\n", spec);
-               return 1;
-       }
-       if (optarg[1] != ',') {
-               fprintf(stderr,"Bad format specifier - need ',' %s\n", optarg);
-               return 1;
-       }
-       optarg += 2;
-       if (*optarg == '\0') {
-               fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg);
-               return 1;
-       }
-
-       /*
-        * Set both merges (front and back)
-        */
-       if (spec == 'M') {
-               override_format['B'] = strdup(optarg);
-               override_format['M'] = strdup(optarg);
-       } else
-               override_format[spec] = strdup(optarg);
-
-       return 0;
+       process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
 }
 
-static void print_field(char *act, struct per_cpu_info *pci,
-                       struct blk_io_trace *t, unsigned long long elapsed,
-                       int pdu_len, unsigned char *pdu_buf, char field,
-                       int minus, int has_w, int width)
+static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
+                      struct blk_io_trace *t, char *act)
 {
-       char format[64];
-
-       if (has_w) {
-               if (minus)
-                       sprintf(format, "%%-%d", width);
-               else
-                       sprintf(format, "%%%d", width);
-       } else
-               sprintf(format, "%%");
-
-       switch (field) {
-       case 'a':
-               fprintf(ofp, strcat(format, "s"), act);
-               break;
-       case 'c':
-               fprintf(ofp, strcat(format, "d"), pci->cpu);
-               break;
-       case 'C':
-               fprintf(ofp, strcat(format, "s"), t->comm);
-               break;
-       case 'd': {
-               char rwbs[4];
-               int i = 0;
-               int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
-               int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
-               int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
-               if (w)
-                       rwbs[i++] = 'W';
-               else
-                       rwbs[i++] = 'R';
-               if (b)
-                       rwbs[i++] = 'B';
-               if (s)
-                       rwbs[i++] = 'S';
-               rwbs[i] = '\0';
-               fprintf(ofp, strcat(format, "s"), rwbs);
-               break;
-       }
-       case 'D':       /* format width ignored */
-               fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
-               break;
-       case 'e':
-               fprintf(ofp, strcat(format, "d"), t->error);
-               break;
-       case 'M':
-               fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
-               break;
-       case 'm':
-               fprintf(ofp, strcat(format, "d"), MINOR(t->device));
-               break;
-       case 'n':
-               fprintf(ofp, strcat(format, "u"), t->bytes >> 9);
-               break;
-       case 'p':
-               fprintf(ofp, strcat(format, "u"), t->pid);
-               break;
-       case 'P':       /* format width ignored */
-               if ((pdu_len > 0) && (pdu_buf != NULL)) {
-                       int i;
-                       unsigned char *p = pdu_buf;
-                       for (i = 0; i < pdu_len; i++)
-                               fprintf(ofp, "%02x ", *p++);
-               }
-               break;
-       case 's':
-               fprintf(ofp, strcat(format, "ld"), t->sequence);
-               break;
-       case 'S':
-               fprintf(ofp, strcat(format, "lu"), t->sector);
-               break;
-       case 't':
-               sprintf(format, "%%0%dlu", has_w ? width : 9);
-               fprintf(ofp, format, NANO_SECONDS(t->time));
-               break;
-       case 'T':
-               fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
-               break;
-       case 'u':
-               if (elapsed == -1ULL) {
-                       fprintf(stderr, "Expecting elapsed value\n");
-                       exit(1);
-               }
-               fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
-               break;
-       case 'U': {
-               __u64 *depth = (__u64 *) ((char *) t + sizeof(*t));
-               fprintf(ofp, strcat(format, "u"),
-                                       (unsigned int) be64_to_cpu(*depth));
-               break;
-       }
-       default:
-               fprintf(ofp,strcat(format, "c"), field);
-               break;
-       }
-}
-
-static char *parse_field(char *act, struct per_cpu_info *pci, 
-                        struct blk_io_trace *t, unsigned long long elapsed, 
-                        int pdu_len, unsigned char *pdu_buf, 
-                        char *master_format)
-{
-       int minus = 0;
-       int has_w = 0;
-       int width = 0;
-       char *p = master_format;
-
-       if (*p == '-') {
-               minus = 1;
-               p++;
-       }
-       if (isdigit(*p)) {
-               has_w = 1;
-               do {
-                       width = (width * 10) + (*p++ - '0');
-               } while ((*p) && (isdigit(*p)));
-       }
-       if (*p) {
-               print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
-                           minus, has_w, width);
-       }
-       return p;
-}
-
-static char *fmt_select(int fmt_spec, struct blk_io_trace *t,
-                       unsigned long long elapsed)
-{
-       char *fmt;
-       char scratch_format[1024];
-
-       if (override_format[fmt_spec] != NULL)
-               return override_format[fmt_spec];
-
-       switch (fmt_spec) {
-       case 'C':       /* Complete */
-               if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
-                       strcpy(scratch_format, HEADER);
-                       strcat(scratch_format, "%P");
-               } else {
-                       strcpy(scratch_format, HEADER "%S + %n ");
-                       if (elapsed != -1ULL)
-                               strcat(scratch_format, "(%8u) ");
-               }
-               strcat(scratch_format, "[%e]\n");
-               fmt = scratch_format;
-               break;
-
-       case 'D':       /* Issue */
-               if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
-                       strcpy(scratch_format, HEADER);
-                       strcat(scratch_format, "%P");
-               } else {
-                       strcpy(scratch_format, HEADER "%S + %n ");
-                       if (elapsed != -1ULL)
-                               strcat(scratch_format, "(%8u) ");
-               }
-               strcat(scratch_format,"[%C]\n");
-               fmt = scratch_format;
-               break;
-
-       case 'Q':       /* Queue */
-               strcpy(scratch_format, HEADER "%S + %n ");
-               if (elapsed != -1ULL)
-                       strcat(scratch_format, "(%8u) ");
-               strcat(scratch_format,"[%C]\n");
-               fmt = scratch_format;
-               break;
-
-       case 'B':       /* Back merge */
-       case 'F':       /* Front merge */
-       case 'M':       /* Front or back merge */
-               fmt = HEADER "%S + %n [%C]\n";
-               break;
-
-       case 'P':       /* Plug */
-               fmt = HEADER "[%C]\n";
-               break;
-
-       case 'G':       /* Get request */
-       case 'S':       /* Sleep request */
-               fmt = HEADER "%S + %n [%C]\n";
-               break;
-
-       case 'U':       /* Unplug IO */
-       case 'T':       /* Unplug timer */
-               fmt = HEADER "[%C] %U\n";
-               break;
-
-       default:
-               fprintf(stderr,"FATAL: Invalid format spec %c\n", fmt_spec);
-               exit(1);
-               /*NOTREACHED*/
-       }
-
-       return fmt;
-}
-
-static void process_fmt(char *act, struct per_cpu_info *pci,
-                          struct blk_io_trace *t, unsigned long long elapsed,
-                          int pdu_len, unsigned char *pdu_buf)
-{
-       char *p = fmt_select(act[0], t, elapsed);
-
-       while (*p) {
-               switch (*p) {
-               case '%':       /* Field specifier */
-                       p++;
-                       if (*p == '%')
-                               fprintf(ofp, "%c", *p++);
-                       else if (!*p)
-                               fprintf(ofp, "%c", '%');
-                       else
-                               p = parse_field(act, pci, t, elapsed,
-                                               pdu_len, pdu_buf, p);
-                       break;
-               case '\\': {    /* escape */
-                       switch (p[1]) {
-                       case 'b': fprintf(ofp, "\b"); break;
-                       case 'n': fprintf(ofp, "\n"); break;
-                       case 'r': fprintf(ofp, "\r"); break;
-                       case 't': fprintf(ofp, "\t"); break;
-                       default:
-                               fprintf(stderr, 
-                                       "Invalid escape char in format %c\n",
-                                       p[1]);
-                               exit(1);
-                               /*NOTREACHED*/
-                       }
-                       p += 2;
-                       break;
-               }
-               default:
-                       fprintf(ofp, "%c", *p++);
-                       break;
-               }
-       }
-}
-
-static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
-                        char *act)
-{
-       process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
+       process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
 }
 
 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
                      char *act)
 {
-       process_fmt(act, pci, t, log_track_queue(t), 0, NULL);
+       process_fmt(act, pci, t, -1, 0, NULL);
 }
 
-static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
-                     char *act)
+static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
+                     struct blk_io_trace *t, char *act)
 {
-       process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
+       process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
 }
 
-static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
-                     char *act)
+static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
+                     struct blk_io_trace *t, char *act)
 {
        if (act[0] == 'F')
-               log_track_frontmerge(t);
+               log_track_frontmerge(pdi, t);
 
        process_fmt(act, pci, t, -1ULL, 0, NULL);
 }
@@ -1049,6 +906,12 @@ static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
        process_fmt(act, pci, t, -1ULL, 0, NULL);
 }
 
+static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
+                     char *act)
+{
+       process_fmt(act, pci, t, -1ULL, 0, NULL);
+}
+
 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
 {
        unsigned char *buf = (unsigned char *) t + sizeof(*t);
@@ -1056,11 +919,11 @@ static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
        process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
 }
 
-static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
+static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
 {
-       int ret = 0;
+       int act = t->action & 0xffff;
 
-       switch (t->action & 0xffff) {
+       switch (act) {
                case __BLK_TA_QUEUE:
                        log_generic(pci, t, "Q");
                        break;
@@ -1079,35 +942,39 @@ static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
                case __BLK_TA_COMPLETE:
                        log_pc(pci, t, "C");
                        break;
+               case __BLK_TA_INSERT:
+                       log_pc(pci, t, "I");
+                       break;
                default:
-                       fprintf(stderr, "Bad pc action %x\n", t->action);
-                       ret = 1;
+                       fprintf(stderr, "Bad pc action %x\n", act);
                        break;
        }
-       
-       return 0;
 }
 
-static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
+static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
+                         struct per_cpu_info *pci)
 {
        int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
        int act = t->action & 0xffff;
 
        switch (act) {
                case __BLK_TA_QUEUE:
-                       account_q(t, pci, w);
+                       account_queue(t, pci, w);
                        log_queue(pci, t, "Q");
                        break;
+               case __BLK_TA_INSERT:
+                       log_insert(pdi, pci, t, "I");
+                       break;
                case __BLK_TA_BACKMERGE:
                        account_m(t, pci, w);
-                       log_merge(pci, t, "M");
+                       log_merge(pdi, pci, t, "M");
                        break;
                case __BLK_TA_FRONTMERGE:
                        account_m(t, pci, w);
-                       log_merge(pci, t, "F");
+                       log_merge(pdi, pci, t, "F");
                        break;
                case __BLK_TA_GETRQ:
-                       log_track_getrq(t);
+                       log_track_getrq(pdi, t);
                        log_generic(pci, t, "G");
                        break;
                case __BLK_TA_SLEEPRQ:
@@ -1118,12 +985,12 @@ static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
                        log_queue(pci, t, "R");
                        break;
                case __BLK_TA_ISSUE:
-                       account_i(t, pci, w);
-                       log_issue(pci, t, "D");
+                       account_issue(t, pci, w);
+                       log_issue(pdi, pci, t, "D");
                        break;
                case __BLK_TA_COMPLETE:
                        account_c(t, pci, w, t->bytes);
-                       log_complete(pci, t, "C");
+                       log_complete(pdi, pci, t, "C");
                        break;
                case __BLK_TA_PLUG:
                        log_action(pci, t, "P");
@@ -1136,24 +1003,27 @@ static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
                        account_unplug(t, pci, 1);
                        log_unplug(pci, t, "UT");
                        break;
+               case __BLK_TA_SPLIT:
+                       log_split(pci, t, "X");
+                       break;
+               case __BLK_TA_BOUNCE:
+                       log_generic(pci, t, "B");
+                       break;
                default:
                        fprintf(stderr, "Bad fs action %x\n", t->action);
                        break;
        }
 }
 
-static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
-                       struct per_dev_info *pdi)
+static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
+                      struct per_dev_info *pdi)
 {
-       int ret = 0;
-
        if (t->action & BLK_TC_ACT(BLK_TC_PC))
-               ret = dump_trace_pc(t, pci);
+               dump_trace_pc(t, pci);
        else
-               dump_trace_fs(t, pci);
+               dump_trace_fs(t, pdi, pci);
 
        pdi->events++;
-       return ret;
 }
 
 static void dump_io_stats(struct io_stats *ios, char *msg)
@@ -1242,7 +1112,11 @@ static void show_process_stats(void)
        while (ppi) {
                char name[64];
 
-               snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid);
+               if (ppi->more_than_one)
+                       sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
+               else
+                       sprintf(name, "%s (%u)", ppi->name, ppi->pid);
+
                dump_io_stats(&ppi->io_stats, name);
                dump_wait_stats(ppi);
                ppi = ppi->list_next;
@@ -1309,114 +1183,230 @@ static void show_device_and_cpu_stats(void)
        }
 }
 
-static struct blk_io_trace *find_trace(void *p, unsigned long offset)
+/*
+ * struct trace and blktrace allocation cache, we do potentially
+ * millions of mallocs for these structures while only using at most
+ * a few thousand at the time
+ */
+static inline void t_free(struct trace *t)
 {
-       unsigned long max_offset = offset;
-       unsigned long off;
-       struct blk_io_trace *bit;
-       __u32 magic;
+       if (t_alloc_cache < 1024) {
+               t->next = t_alloc_list;
+               t_alloc_list = t;
+               t_alloc_cache++;
+       } else
+               free(t);
+}
 
-       for (off = 0; off < max_offset; off++) {
-               bit = p + off;
+static inline struct trace *t_alloc(void)
+{
+       struct trace *t = t_alloc_list;
 
-               magic = be32_to_cpu(bit->magic);
-               if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
-                       return bit;
+       if (t) {
+               t_alloc_list = t->next;
+               t_alloc_cache--;
+               return t;
        }
 
-       return NULL;
+       return malloc(sizeof(*t));
 }
 
-static int sort_entries(void)
+static inline void bit_free(struct blk_io_trace *bit)
+{
+       if (bit_alloc_cache < 1024) {
+               /*
+                * abuse a 64-bit field for a next pointer for the free item
+                */
+               bit->time = (__u64) (unsigned long) bit_alloc_list;
+               bit_alloc_list = (struct blk_io_trace *) bit;
+               bit_alloc_cache++;
+       } else
+               free(bit);
+}
+
+static inline struct blk_io_trace *bit_alloc(void)
+{
+       struct blk_io_trace *bit = bit_alloc_list;
+
+       if (bit) {
+               bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
+                                bit->time;
+               bit_alloc_cache--;
+               return bit;
+       }
+
+       return malloc(sizeof(*bit));
+}
+
+static void find_genesis(void)
+{
+       struct trace *t = trace_list;
+
+       genesis_time = -1ULL;
+       while (t != NULL) {
+               if (t->bit->time < genesis_time)
+                       genesis_time = t->bit->time;
+
+               t = t->next;
+       }
+}
+
+static inline int check_stopwatch(struct blk_io_trace *bit)
+{
+       if (bit->time < stopwatch_end &&
+           bit->time >= stopwatch_start)
+               return 0;
+
+       return 1;
+}
+
+/*
+ * return youngest entry read
+ */
+static int sort_entries(unsigned long long *youngest)
 {
-       struct per_dev_info *pdi;
-       struct per_cpu_info *pci;
-       struct blk_io_trace *bit;
        struct trace *t;
-       int nr = 0;
 
+       if (!genesis_time)
+               find_genesis();
+
+       *youngest = 0;
        while ((t = trace_list) != NULL) {
+               struct blk_io_trace *bit = t->bit;
 
                trace_list = t->next;
-               bit = t->bit;
 
-               memset(&t->rb_node, 0, sizeof(t->rb_node));
+               bit->time -= genesis_time;
 
-               if (verify_trace(bit))
-                       break;
-               if (trace_rb_insert(t))
+               if (bit->time < *youngest || !*youngest)
+                       *youngest = bit->time;
+
+               if (check_stopwatch(bit)) {
+                       bit_free(bit);
+                       t_free(t);
+                       continue;
+               }
+
+               if (trace_rb_insert_sort(t))
                        return -1;
 
-               pdi = get_dev_info(bit->device);
-               pci = get_cpu_info(pdi, bit->cpu);
-               pci->nelems++;
+               if (bit->sequence < smallest_seq_read)
+                       smallest_seq_read = bit->sequence;
+       }
+
+       return 0;
+}
+
+static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
+{
+       rb_erase(&t->rb_node, &pdi->rb_last);
+       pdi->rb_last_entries--;
 
-               nr++;
+       bit_free(t->bit);
+       t_free(t);
+}
+
+static void put_trace(struct per_dev_info *pdi, struct trace *t)
+{
+       rb_erase(&t->rb_node, &rb_sort_root);
+       rb_sort_entries--;
+
+       trace_rb_insert_last(pdi, t);
+
+       if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
+               struct rb_node *n = rb_first(&pdi->rb_last);
+
+               t = rb_entry(n, struct trace, rb_node);
+               __put_trace_last(pdi, t);
        }
+}
+
+static int check_sequence(struct per_dev_info *pdi, struct blk_io_trace *bit,
+                         int force)
+{
+       unsigned long expected_sequence = pdi->last_sequence + 1;
+       struct trace *t;
+       
+       /*
+        * first entry, always ok
+        */
+       if (!expected_sequence)
+               return 0;
 
-       return nr;
+       if (bit->sequence == expected_sequence)
+               return 0;
+
+       /*
+        * we may not have seen that sequence yet. if we are not doing
+        * the final run, break and wait for more entries.
+        */
+       if (expected_sequence < smallest_seq_read) {
+               t = trace_rb_find_last(pdi, expected_sequence);
+               if (!t)
+                       goto skip;
+
+               __put_trace_last(pdi, t);
+               return 0;
+       } else if (!force)
+               return 1;
+       else {
+skip:
+               if (print_missing) {
+                       fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
+                               MAJOR(pdi->dev), MINOR(pdi->dev),
+                               pdi->last_sequence, bit->sequence);
+               }
+               pdi->skips++;
+               return 0;
+       }
 }
 
-static void show_entries_rb(int piped)
+static void show_entries_rb(int force)
 {
-       struct per_dev_info *pdi;
+       struct per_dev_info *pdi = NULL;
+       struct per_cpu_info *pci = NULL;
        struct blk_io_trace *bit;
        struct rb_node *n;
        struct trace *t;
-       int cpu;
+
+       if (force) {
+               n = rb_first(&rb_sort_root);
+               t = rb_entry(n, struct trace, rb_node);
+               fprintf(stderr, "first force %u\n", t->bit->sequence);
+       }
 
        while ((n = rb_first(&rb_sort_root)) != NULL) {
+               if (done)
+                       break;
 
                t = rb_entry(n, struct trace, rb_node);
                bit = t->bit;
 
-               pdi = get_dev_info(bit->device);
+               if (!pdi || pdi->dev != bit->device)
+                       pdi = get_dev_info(bit->device);
+
                if (!pdi) {
                        fprintf(stderr, "Unknown device ID? (%d,%d)\n",
                                MAJOR(bit->device), MINOR(bit->device));
                        break;
                }
-               cpu = bit->cpu;
-               if (cpu > pdi->ncpus) {
-                       fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
-                               cpu, MAJOR(bit->device), MINOR(bit->device));
+
+               if (check_sequence(pdi, bit, force))
                        break;
-               }
 
-               /*
-                * back off displaying more info if we are out of sync
-                * on SMP systems. to prevent stalling on lost events,
-                * only allow an event to us a few times
-                */
-               if (bit->sequence != (pdi->last_sequence + 1)) {
-                       if (piped && t->skipped < 5) {
-                               t->skipped++;
-                               break;
-                       } else {
-                               fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
-                               pdi->skips++;
-                       }
-               }
+               if (!force && bit->time > last_allowed_time)
+                       break;
 
                pdi->last_sequence = bit->sequence;
 
-               bit->time -= genesis_time;
-               if (bit->time < stopwatch_start)
-                       continue;
-               if (bit->time >= stopwatch_end)
-                       break;
-
                check_time(pdi, bit);
 
-               if (dump_trace(bit, &pdi->cpus[cpu], pdi))
-                       break;
+               if (!pci || pci->cpu != bit->cpu)
+                       pci = get_cpu_info(pdi, bit->cpu);
 
-               rb_erase(&t->rb_node, &rb_sort_root);
+               dump_trace(bit, pci, pdi);
 
-               if (piped) {
-                       free(bit);
-                       free(t);
-               }
+               put_trace(pdi, t);
        }
 }
 
@@ -1441,6 +1431,7 @@ static int read_data(int fd, void *buffer, int bytes, int block)
                else if (ret < 0) {
                        if (errno != EAGAIN)
                                perror("read");
+
                        return -1;
                } else {
                        p += ret;
@@ -1451,169 +1442,178 @@ static int read_data(int fd, void *buffer, int bytes, int block)
        return 0;
 }
 
-/*
- * Find the traces in 'tb' and add them to the list for sorting and
- * displaying
- */
-static int find_entries(void *tb, unsigned long size)
+static int read_events(int fd, int always_block)
 {
-       struct blk_io_trace *bit;
-       struct trace *t;
-       void *start = tb;
+       struct per_dev_info *pdi = NULL;
+       unsigned int events = 0;
+
+       while (!is_done() && events < rb_batch) {
+               struct blk_io_trace *bit;
+               struct trace *t;
+               int pdu_len;
+               __u32 magic;
 
-       while (tb - start <= size - sizeof(*bit)) {
-               bit = find_trace(tb, size - (tb - start));
-               if (!bit)
+               bit = bit_alloc();
+
+               if (read_data(fd, bit, sizeof(*bit), !events || always_block))
                        break;
 
-               t = malloc(sizeof(*t));
+               magic = be32_to_cpu(bit->magic);
+               if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
+                       fprintf(stderr, "Bad magic %x\n", magic);
+                       break;
+               }
+
+               pdu_len = be16_to_cpu(bit->pdu_len);
+               if (pdu_len) {
+                       void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
+
+                       if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
+                               break;
+
+                       bit = ptr;
+               }
+
+               trace_to_cpu(bit);
+
+               if (verify_trace(bit)) {
+                       bit_free(bit);
+                       continue;
+               }
+
+               t = t_alloc();
                memset(t, 0, sizeof(*t));
                t->bit = bit;
 
-               trace_to_cpu(bit);
                t->next = trace_list;
                trace_list = t;
 
-               tb += sizeof(*bit) + bit->pdu_len;
+               if (!pdi || pdi->dev != bit->device)
+                       pdi = get_dev_info(bit->device);
+
+               if (bit->time > pdi->last_read_time)
+                       pdi->last_read_time = bit->time;
+
+               events++;
        }
 
-       return 0;
+       return events;
 }
 
 static int do_file(void)
 {
-       int i, j, nfiles = 0, nelems;
+       struct per_cpu_info *pci;
+       struct per_dev_info *pdi;
+       int i, j, events, events_added;
 
+       /*
+        * first prepare all files for reading
+        */
        for (i = 0; i < ndevices; i++) {
-               for (j = 0;; j++, nfiles++) {
-                       struct per_dev_info *pdi;
-                       struct per_cpu_info *pci;
+               pdi = &devices[i];
+               pdi->nfiles = 0;
+               pdi->last_sequence = -1;
+
+               for (j = 0;; j++) {
                        struct stat st;
-                       void *tb;
 
-                       pdi = &devices[i];
                        pci = get_cpu_info(pdi, j);
                        pci->cpu = j;
+                       pci->fd = -1;
 
                        snprintf(pci->fname, sizeof(pci->fname)-1,
-                                "%s.%d", pdi->name, j);
+                                "%s.blktrace.%d", pdi->name, pci->cpu);
                        if (stat(pci->fname, &st) < 0)
                                break;
-                       if (!st.st_size)
-                               continue;
-
-                       printf("Processing %s\n", pci->fname);
-
-                       tb = malloc(st.st_size);
-                       if (!tb) {
-                               fprintf(stderr, "Out of memory, skip file %s\n",
-                                       pci->fname);
-                               continue;
+                       if (st.st_size) {
+                               pci->fd = open(pci->fname, O_RDONLY);
+                               if (pci->fd < 0) {
+                                       perror(pci->fname);
+                                       continue;
+                               }
                        }
 
-                       pci->fd = open(pci->fname, O_RDONLY);
-                       if (pci->fd < 0) {
-                               perror(pci->fname);
-                               free(tb);
-                               continue;
-                       }
+                       printf("Input file %s added\n", pci->fname);
+                       pdi->nfiles++;
+               }
+       }
 
-                       if (read_data(pci->fd, tb, st.st_size, 1)) {
-                               close(pci->fd);
-                               free(tb);
-                               continue;
-                       }
+       /*
+        * now loop over the files reading in the data
+        */
+       do {
+               unsigned long long youngest;
 
-                       if (find_entries(tb, st.st_size)) {
-                               close(pci->fd);
-                               free(tb);
-                       }
+               events_added = 0;
+               last_allowed_time = -1ULL;
+               smallest_seq_read = -1U;
 
-                       nelems = sort_entries();
-                       if (nelems == -1) {
-                               close(pci->fd);
-                               free(tb);
-                               continue;
-                       }
+               for (i = 0; i < ndevices; i++) {
+                       pdi = &devices[i];
 
-                       printf("Completed %s (CPU%d %d, entries)\n",
-                               pci->fname, j, nelems);
-                       close(pci->fd);
-               }
-       }
+                       for (j = 0; j < pdi->nfiles; j++) {
 
-       if (!nfiles) {
-               fprintf(stderr, "No files found\n");
-               return 1;
-       }
+                               pci = get_cpu_info(pdi, j);
 
-       show_entries_rb(0);
-       return 0;
-}
+                               if (pci->fd == -1)
+                                       continue;
 
-static int read_sort_events(int fd)
-{
-       int events = 0;
+                               events = read_events(pci->fd, 1);
+                               if (!events) {
+                                       close(pci->fd);
+                                       pci->fd = -1;
+                                       continue;
+                               }
 
-       do {
-               struct blk_io_trace *bit;
-               struct trace *t;
-               int pdu_len;
-               __u32 magic;
+                               if (pdi->last_read_time < last_allowed_time)
+                                       last_allowed_time = pdi->last_read_time;
 
-               bit = malloc(sizeof(*bit));
+                               events_added += events;
+                       }
+               }
 
-               if (read_data(fd, bit, sizeof(*bit), !events))
+               if (sort_entries(&youngest))
                        break;
 
-               magic = be32_to_cpu(bit->magic);
-               if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
-                       fprintf(stderr, "Bad magic %x\n", magic);
+               if (youngest > stopwatch_end)
                        break;
-               }
 
-               pdu_len = be16_to_cpu(bit->pdu_len);
-               if (pdu_len) {
-                       void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
+               show_entries_rb(0);
 
-                       if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
-                               break;
+       } while (events_added);
 
-                       bit = ptr;
-               }
-
-               t = malloc(sizeof(*t));
-               memset(t, 0, sizeof(*t));
-               t->bit = bit;
-
-               trace_to_cpu(bit);
-               t->next = trace_list;
-               trace_list = t;
-
-               events++;
-       } while (!is_done() && events < rb_batch);
+       if (rb_sort_entries)
+               show_entries_rb(1);
 
-       return events;
+       return 0;
 }
 
 static int do_stdin(void)
 {
+       unsigned long long youngest;
        int fd;
 
+       last_allowed_time = -1ULL;
        fd = dup(STDIN_FILENO);
        do {
                int events;
 
-               events = read_sort_events(fd);
+               events = read_events(fd, 0);
                if (!events)
                        break;
        
-               if (sort_entries() == -1)
+               if (sort_entries(&youngest))
                        break;
 
-               show_entries_rb(1);
+               if (youngest > stopwatch_end)
+                       break;
+
+               show_entries_rb(0);
        } while (1);
 
+       if (rb_sort_entries)
+               show_entries_rb(1);
+
        close(fd);
        return 0;
 }
@@ -1623,7 +1623,7 @@ static void flush_output(void)
        fflush(ofp);
 }
 
-static void handle_sigint(int sig)
+static void handle_sigint(__attribute__((__unused__)) int sig)
 {
        done = 1;
        flush_output();
@@ -1657,15 +1657,38 @@ static int find_stopwatch_interval(char *string)
                fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
                return 1;
        }
-       stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
+       stopwatch_end = DOUBLE_TO_NANO_ULL(value);
+       if (stopwatch_end <= stopwatch_start) {
+               fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
+                       stopwatch_start, stopwatch_end);
+               return 1;
+       }
+
        return 0;
 }
 
+static char usage_str[] = \
+       "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
+       "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
+       "\t-i Input file containing trace data, or '-' for stdin\n" \
+       "\t-o Output file. If not given, output is stdout\n" \
+       "\t-b stdin read batching\n" \
+       "\t-s Show per-program io statistics\n" \
+       "\t-n Hash processes by name, not pid\n" \
+       "\t-t Track individual ios. Will tell you the time a request took\n" \
+       "\t   to get queued, to get dispatched, and to get completed\n" \
+       "\t-q Quiet. Don't display any stats at the end of the trace\n" \
+       "\t-w Only parse data between the given time interval in seconds.\n" \
+       "\t   If 'start' isn't given, blkparse defaults the start time to 0\n" \
+       "\t -f Output format. Customize the output format. The format field\n" \
+       "\t    identifies can be found in the documentation\n" \
+       "\t-F Format specification. Can be found in the documentation\n" \
+       "\t-m Print missing entries\n" \
+       "\t-v Print program version info\n\n";
+
 static void usage(char *prog)
 {
-       fprintf(stderr, "Usage: %s "
-               "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
-               prog);
+       fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
 }
 
 int main(int argc, char *argv[])
@@ -1710,6 +1733,15 @@ int main(int argc, char *argv[])
                        if (add_format_spec(optarg) != 0)
                                return 1;
                        break;
+               case 'n':
+                       ppi_hash_by_pid = 0;
+                       break;
+               case 'm':
+                       print_missing = 1;
+                       break;
+               case 'v':
+                       printf("%s version %s\n", argv[0], blkparse_version);
+                       return 0;
                default:
                        usage(argv[0]);
                        return 1;
@@ -1730,7 +1762,6 @@ int main(int argc, char *argv[])
        }
 
        memset(&rb_sort_root, 0, sizeof(rb_sort_root));
-       memset(&rb_track_root, 0, sizeof(rb_track_root));
 
        signal(SIGINT, handle_sigint);
        signal(SIGHUP, handle_sigint);
@@ -1744,7 +1775,7 @@ int main(int argc, char *argv[])
        } else {
                char ofname[128];
 
-               snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
+               snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
                ofp = fopen(ofname, "w");
                mode = _IOFBF;
        }