blkparse: Initialize and test for undefined request tracking timestamps
[blktrace.git] / iowatcher / blkparse.c
index 2be2f056a8997df574501fb99bf868b4bd99699b..41e20f0c01f9ed986a885916e0e85250a180c83a 100644 (file)
@@ -12,7 +12,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  *  Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
  */
@@ -30,6 +30,7 @@
 #include <sys/mman.h>
 #include <time.h>
 #include <math.h>
+#include <dirent.h>
 
 #include "plot.h"
 #include "blkparse.h"
 static struct list_head io_hash_table[IO_HASH_TABLE_SIZE];
 static u64 ios_in_flight = 0;
 
+#define PROCESS_HASH_TABLE_BITS 7
+#define PROCESS_HASH_TABLE_SIZE (1 << PROCESS_HASH_TABLE_BITS)
+static struct list_head process_hash_table[PROCESS_HASH_TABLE_SIZE];
+
+extern int plot_io_action;
+extern int io_per_process;
 
 /*
  * Trace categories
@@ -152,19 +159,29 @@ struct pending_io {
        /* sector offset of this IO */
        u64 sector;
 
+       /* dev_t for this IO */
+       u32 device;
+
        /* time this IO was dispatched */
        u64 dispatch_time;
        /* time this IO was finished */
        u64 completion_time;
        struct list_head hash_list;
+       /* process which queued this IO */
+       u32 pid;
 };
 
-#define MINORBITS 20
-#define MINORMASK ((1 << MINORBITS) - 1)
-#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 CHECK_MAGIC(t)          (((t)->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
+struct pid_map {
+       struct list_head hash_list;
+       u32 pid;
+       int index;
+       char name[0];
+};
+
+u64 get_record_time(struct trace *trace)
+{
+       return trace->io->time;
+}
 
 void init_io_hash_table(void)
 {
@@ -201,23 +218,24 @@ static inline u64 hash_sector(u64 val)
        return hash >> (64 - IO_HASH_TABLE_BITS);
 }
 
-static int hash_table_insert(struct pending_io *ins_pio)
+static int io_hash_table_insert(struct pending_io *ins_pio)
 {
        u64 sector = ins_pio->sector;
+       u32 dev = ins_pio->device;
        int slot = hash_sector(sector);
        struct list_head *head;
        struct pending_io *pio;
 
        head = io_hash_table + slot;
        list_for_each_entry(pio, head, hash_list) {
-               if (pio->sector == sector)
+               if (pio->sector == sector && pio->device == dev)
                        return -EEXIST;
        }
        list_add_tail(&ins_pio->hash_list, head);
        return 0;
 }
 
-static struct pending_io *hash_table_search(u64 sector)
+static struct pending_io *io_hash_table_search(u64 sector, u32 dev)
 {
        int slot = hash_sector(sector);
        struct list_head *head;
@@ -225,46 +243,132 @@ static struct pending_io *hash_table_search(u64 sector)
 
        head = io_hash_table + slot;
        list_for_each_entry(pio, head, hash_list) {
-               if (pio->sector == sector)
+               if (pio->sector == sector && pio->device == dev)
                        return pio;
        }
        return NULL;
 }
 
-static int hash_dispatched_io(struct blk_io_trace *io)
+static struct pending_io *hash_queued_io(struct blk_io_trace *io)
 {
        struct pending_io *pio;
        int ret;
 
        pio = calloc(1, sizeof(*pio));
        pio->sector = io->sector;
-       pio->dispatch_time = io->time;
+       pio->device = io->device;
+       pio->pid = io->pid;
 
-       ret = hash_table_insert(pio);
-       if (ret == -EEXIST) {
-               /* crud, the IO isn't here */
+       ret = io_hash_table_insert(pio);
+       if (ret < 0) {
+               /* crud, the IO is there already */
                free(pio);
+               return NULL;
        }
-       return ret;
+       return pio;
+}
+
+static struct pending_io *hash_dispatched_io(struct blk_io_trace *io)
+{
+       struct pending_io *pio;
+
+       pio = io_hash_table_search(io->sector, io->device);
+       if (!pio) {
+               pio = hash_queued_io(io);
+               if (!pio)
+                       return NULL;
+       }
+       pio->dispatch_time = io->time;
+       return pio;
 }
 
 static struct pending_io *hash_completed_io(struct blk_io_trace *io)
 {
        struct pending_io *pio;
 
-       pio = hash_table_search(io->sector);
+       pio = io_hash_table_search(io->sector, io->device);
 
        if (!pio)
                return NULL;
        return pio;
 }
 
+void init_process_hash_table(void)
+{
+       int i;
+       struct list_head *head;
+
+       for (i = 0; i < PROCESS_HASH_TABLE_SIZE; i++) {
+               head = process_hash_table + i;
+               INIT_LIST_HEAD(head);
+       }
+}
+
+static u32 hash_pid(u32 pid)
+{
+       u32 hash = pid;
+
+       hash ^= pid >> 3;
+       hash ^= pid >> 3;
+       hash ^= pid >> 4;
+       hash ^= pid >> 6;
+       return (hash & (PROCESS_HASH_TABLE_SIZE - 1));
+}
+
+static struct pid_map *process_hash_search(u32 pid)
+{
+       int slot = hash_pid(pid);
+       struct list_head *head;
+       struct pid_map *pm;
+
+       head = process_hash_table + slot;
+       list_for_each_entry(pm, head, hash_list) {
+               if (pm->pid == pid)
+                       return pm;
+       }
+       return NULL;
+}
+
+static struct pid_map *process_hash_insert(u32 pid, char *name)
+{
+       int slot = hash_pid(pid);
+       struct pid_map *pm;
+       int old_index = 0;
+       char buf[16];
+
+       pm = process_hash_search(pid);
+       if (pm) {
+               /* Entry exists and name shouldn't be changed? */
+               if (!name || !strcmp(name, pm->name))
+                       return pm;
+               list_del(&pm->hash_list);
+               old_index = pm->index;
+               free(pm);
+       }
+       if (!name) {
+               sprintf(buf, "[%u]", pid);
+               name = buf;
+       }
+       pm = malloc(sizeof(struct pid_map) + strlen(name) + 1);
+       pm->pid = pid;
+       pm->index = old_index;
+       strcpy(pm->name, name);
+       list_add_tail(&pm->hash_list, process_hash_table + slot);
+
+       return pm;
+}
+
 static void handle_notify(struct trace *trace)
 {
        struct blk_io_trace *io = trace->io;
        void *payload = (char *)io + sizeof(*io);
        u32 two32[2];
 
+       if (io->action == BLK_TN_PROCESS) {
+               if (io_per_process)
+                       process_hash_insert(io->pid, payload);
+               return;
+       }
 
        if (io->action != BLK_TN_TIMESTAMP)
                return;
@@ -302,7 +406,7 @@ void first_record(struct trace *trace)
        trace->io = (struct blk_io_trace *)trace->cur;
 }
 
-int is_io_event(struct blk_io_trace *test)
+static int is_io_event(struct blk_io_trace *test)
 {
        char *message;
        if (!(test->action & BLK_TC_ACT(BLK_TC_NOTIFY)))
@@ -356,7 +460,7 @@ u64 find_last_time(struct trace *trace)
        return found;
 }
 
-int parse_fio_bank_message(struct trace *trace, u64 *bank_ret, u64 *offset_ret,
+static int parse_fio_bank_message(struct trace *trace, u64 *bank_ret, u64 *offset_ret,
                           u64 *num_banks_ret)
 {
        char *s;
@@ -413,23 +517,87 @@ out:
        return -1;
 }
 
-void find_highest_offset(struct trace *trace, u64 *max_ret, u64 *max_bank_ret,
-                        u64 *max_offset_ret)
+static struct dev_info *lookup_dev(struct trace *trace, struct blk_io_trace *io)
+{
+       u32 dev = io->device;
+       int i;
+       struct dev_info *di = NULL;
+
+       for (i = 0; i < trace->num_devices; i++) {
+               if (trace->devices[i].device == dev) {
+                       di = trace->devices + i;
+                       goto found;
+               }
+       }
+       i = trace->num_devices++;
+       if (i >= MAX_DEVICES_PER_TRACE) {
+               fprintf(stderr, "Trace contains too many devices (%d)\n", i);
+               exit(1);
+       }
+       di = trace->devices + i;
+       di->device = dev;
+found:
+       return di;
+}
+
+static void map_devices(struct trace *trace)
+{
+       struct dev_info *di;
+       u64 found;
+       u64 map_start = 0;
+       int i;
+
+       first_record(trace);
+       while (1) {
+               if (!(trace->io->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
+                       di = lookup_dev(trace, trace->io);
+                       found = trace->io->sector << 9;
+                       if (found < di->min)
+                               di->min = found;
+
+                       found += trace->io->bytes;
+                       if (di->max < found)
+                               di->max = found;
+               }
+               if (next_record(trace))
+                       break;
+       }
+       first_record(trace);
+       for (i = 0; i < trace->num_devices; i++) {
+               di = trace->devices + i;
+               di->map = map_start;
+               map_start += di->max - di->min;
+       }
+}
+
+static u64 map_io(struct trace *trace, struct blk_io_trace *io)
+{
+       struct dev_info *di = lookup_dev(trace, io);
+       u64 val = trace->io->sector << 9;
+       return di->map + val - di->min;
+}
+
+void find_extreme_offsets(struct trace *trace, u64 *min_ret, u64 *max_ret, u64 *max_bank_ret,
+                         u64 *max_offset_ret)
 {
        u64 found = 0;
-       u64 max = 0;
+       u64 max = 0, min = ~(u64)0;
        u64 max_bank = 0;
        u64 max_bank_offset = 0;
        u64 num_banks = 0;
+
+       map_devices(trace);
+
        first_record(trace);
        while (1) {
                if (!(trace->io->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
-                       found = trace->io->sector << 9;
-                       found += trace->io->bytes;
+                       found = map_io(trace, trace->io);
+                       if (found < min)
+                               min = found;
 
-                       if (max < found) {
+                       found += trace->io->bytes;
+                       if (max < found)
                                max = found;
-                       }
                } else {
                        u64 bank;
                        u64 offset;
@@ -445,30 +613,63 @@ void find_highest_offset(struct trace *trace, u64 *max_ret, u64 *max_bank_ret,
                        break;
        }
        first_record(trace);
+       *min_ret = min;
        *max_ret = max;
        *max_bank_ret = max_bank;
        *max_offset_ret = max_bank_offset;
 }
 
-int filter_outliers(struct trace *trace, u64 max_offset,
+static void check_io_types(struct trace *trace)
+{
+       struct blk_io_trace *io = trace->io;
+       int action = io->action & BLK_TA_MASK;
+
+       if (!(io->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
+               switch (action) {
+               case __BLK_TA_COMPLETE:
+                       trace->found_completion = 1;
+                       break;
+               case __BLK_TA_ISSUE:
+                       trace->found_issue = 1;
+                       break;
+               case __BLK_TA_QUEUE:
+                       trace->found_queue = 1;
+                       break;
+               };
+       }
+}
+
+
+int filter_outliers(struct trace *trace, u64 min_offset, u64 max_offset,
                    u64 *yzoom_min, u64 *yzoom_max)
 {
        int hits[11];
        u64 max_per_bucket[11];
-       u64 bytes_per_bucket = max_offset / 10;
+       u64 min_per_bucket[11];
+       u64 bytes_per_bucket = (max_offset - min_offset + 1) / 10;
        int slot;
        int fat_count = 0;
 
        memset(hits, 0, sizeof(int) * 11);
        memset(max_per_bucket, 0, sizeof(u64) * 11);
+       memset(min_per_bucket, 0xff, sizeof(u64) * 11);
        first_record(trace);
        while (1) {
-               if (!(trace->io->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
-                       u64 top = (trace->io->sector << 9) + trace->io->bytes;
-                       slot = (int)(top / bytes_per_bucket);
+               check_io_types(trace);
+               if (!(trace->io->action & BLK_TC_ACT(BLK_TC_NOTIFY)) &&
+                   (trace->io->action & BLK_TA_MASK) == __BLK_TA_QUEUE) {
+                       u64 off = map_io(trace, trace->io) - min_offset;
+
+                       slot = (int)(off / bytes_per_bucket);
+                       hits[slot]++;
+                       if (off < min_per_bucket[slot])
+                               min_per_bucket[slot] = off;
+
+                       off += trace->io->bytes;
+                       slot = (int)(off / bytes_per_bucket);
                        hits[slot]++;
-                       if (top > max_per_bucket[slot])
-                               max_per_bucket[slot] = top;
+                       if (off > max_per_bucket[slot])
+                               max_per_bucket[slot] = off;
                }
                if (next_record(trace))
                        break;
@@ -485,58 +686,216 @@ int filter_outliers(struct trace *trace, u64 max_offset,
                double d = hits[slot];
 
                if (d >= (double)fat_count * .05) {
-                       *yzoom_max = max_per_bucket[slot];
+                       *yzoom_max = max_per_bucket[slot] + min_offset;
                        break;
                }
        }
 
-       *yzoom_min = 0;
+       *yzoom_min = min_offset;
        for (slot = 0; slot < 10; slot++) {
                double d = hits[slot];
 
                if (d >= (double)fat_count * .05) {
-                       *yzoom_min = slot * bytes_per_bucket;
+                       *yzoom_min = min_per_bucket[slot] + min_offset;
                        break;
                }
        }
        return 0;
 }
 
+static char footer[] = ".blktrace.0";
+static int footer_len = sizeof(footer) - 1;
+
+static int match_trace(char *name, int *len)
+{
+       int match_len;
+       int footer_start;
+
+       match_len = strlen(name);
+       if (match_len <= footer_len)
+               return 0;
+
+       footer_start = match_len - footer_len;
+       if (strcmp(name + footer_start, footer) != 0)
+               return 0;
+
+       if (len)
+               *len = match_len;
+       return 1;
+}
+
+struct tracelist {
+       struct tracelist *next;
+       char *name;
+};
+
+static struct tracelist *traces_list(char *dir_name, int *len)
+{
+       int count = 0;
+       struct tracelist *traces = NULL;
+       int dlen = strlen(dir_name);
+       DIR *dir = opendir(dir_name);
+       if (!dir)
+               return NULL;
+
+       while (1) {
+               int n = 0;
+               struct tracelist *tl;
+               struct dirent *d = readdir(dir);
+               if (!d)
+                       break;
+
+               if (!match_trace(d->d_name, &n))
+                       continue;
+
+               n += dlen + 1; /* dir + '/' + file */
+               /* Allocate space for tracelist + filename */
+               tl = calloc(1, sizeof(struct tracelist) + (sizeof(char) * (n + 1)));
+               if (!tl) {
+                       closedir(dir);
+                       return NULL;
+               }
+               tl->next = traces;
+               tl->name = (char *)(tl + 1);
+               snprintf(tl->name, n, "%s/%s", dir_name, d->d_name);
+               traces = tl;
+               count++;
+       }
+
+       closedir(dir);
+
+       if (len)
+               *len = count;
+
+       return traces;
+}
+
+static void traces_free(struct tracelist *traces)
+{
+       while (traces) {
+               struct tracelist *tl = traces;
+               traces = traces->next;
+               free(tl);
+       }
+}
+
+static int dump_traces(struct tracelist *traces, int count, char *dumpfile)
+{
+       struct tracelist *tl;
+       char **argv = NULL;
+       int argc = 0;
+       int i;
+       int err = 0;
+
+       argc = count * 2; /* {"-i", trace } */
+       argc += 4; /* See below */
+       argv = calloc(argc + 1, sizeof(char *));
+       if (!argv)
+               return -errno;
+
+       i = 0;
+       argv[i++] = "blkparse";
+       argv[i++] = "-O";
+       argv[i++] = "-d";
+       argv[i++] = dumpfile;
+       for (tl = traces; tl != NULL; tl = tl->next) {
+               argv[i++] = "-i";
+               argv[i++] = tl->name;
+       }
+
+       err = run_program(argc, argv, 1, NULL, NULL);
+       if (err)
+               fprintf(stderr, "%s exited with %d, expected 0\n", argv[0], err);
+       free(argv);
+       return err;
+}
+
 static char *find_trace_file(char *filename)
 {
        int ret;
        struct stat st;
-       char line[1024];
        char *dot;
-       char *try;
-
+       int found_dir = 0;
+       char *dumpfile;
+       int len = strlen(filename);
+
+       /* look for an exact match of whatever they pass in.
+        * If it is a file, assume it is the dump file.
+        * If a directory, remember that it existed so we
+        * can combine traces in that directory later
+        */
        ret = stat(filename, &st);
-       if (ret == 0)
-               return strdup(filename);
+       if (ret == 0) {
+               if (S_ISREG(st.st_mode))
+                       return strdup(filename);
 
-       snprintf(line, 1024, "%s.%s", filename, "dump");
-       ret = stat(line, &st);
+               if (S_ISDIR(st.st_mode))
+                       found_dir = 1;
+       }
+
+       if (found_dir) {
+               int i;
+               /* Eat up trailing '/'s */
+               for (i = len - 1; filename[i] == '/'; i--)
+                       filename[i] = '\0';
+       }
+
+       /*
+        * try tacking .dump onto the end and see if that already
+        * has been generated
+        */
+       ret = asprintf(&dumpfile, "%s.dump", filename);
+       if (ret == -1) {
+               perror("Error building dump file name");
+               return NULL;
+       }
+       ret = stat(dumpfile, &st);
        if (ret == 0)
-               return strdup(line);
+               return dumpfile;
+
+       /*
+        * try to generate the .dump from all the traces in
+        * a single dir.
+        */
+       if (found_dir) {
+               int count;
+               struct tracelist *traces = traces_list(filename, &count);
+               if (traces) {
+                       ret = dump_traces(traces, count, dumpfile);
+                       traces_free(traces);
+                       if (ret == 0)
+                               return dumpfile;
+               }
+       }
+       free(dumpfile);
 
-       try = strdup(filename);
-       dot = strrchr(try, '.');
+       /*
+        * try to generate the .dump from all the blktrace
+        * files for a named trace
+        */
+       dot = strrchr(filename, '.');
        if (!dot || strcmp(".dump", dot) != 0) {
-               if (dot)
-                       *dot = '\0';
-               snprintf(line, 1024, "%s%s", try, ".blktrace.0");
-               ret = stat(line, &st);
+               struct tracelist trace = {0 ,NULL};
+               if (dot && dot != filename)
+                       len = dot - filename;
+
+               ret = asprintf(&trace.name, "%*s.blktrace.0", len, filename);
+               if (ret == -1)
+                       return NULL;
+               ret = asprintf(&dumpfile, "%*s.dump", len, filename);
+               if (ret == -1) {
+                       free(trace.name);
+                       return NULL;
+               }
+
+               ret = dump_traces(&trace, 1, dumpfile);
                if (ret == 0) {
-                       blktrace_to_dump(try);
-                       snprintf(line, 1024, "%s.%s", try, "dump");
-                       ret = stat(line, &st);
-                       if (ret == 0) {
-                               free(try);
-                               return strdup(line);
-                       }
+                       free(trace.name);
+                       return dumpfile;
                }
+               free(trace.name);
+               free(dumpfile);
        }
-       free(try);
        return NULL;
 }
 struct trace *open_trace(char *filename)
@@ -601,8 +960,23 @@ static inline int tput_event(struct trace *trace)
        return __BLK_TA_COMPLETE;
 }
 
+int action_char_to_num(char action)
+{
+       switch (action) {
+       case 'Q':
+               return __BLK_TA_QUEUE;
+       case 'D':
+               return __BLK_TA_ISSUE;
+       case 'C':
+               return __BLK_TA_COMPLETE;
+       }
+       return -1;
+}
+
 static inline int io_event(struct trace *trace)
 {
+       if (plot_io_action)
+               return plot_io_action;
        if (trace->found_queue)
                return __BLK_TA_QUEUE;
        if (trace->found_issue)
@@ -613,9 +987,11 @@ static inline int io_event(struct trace *trace)
        return __BLK_TA_COMPLETE;
 }
 
-void add_tput(struct trace *trace, struct graph_line_data *gld)
+void add_tput(struct trace *trace, struct graph_line_data *writes_gld,
+             struct graph_line_data *reads_gld)
 {
        struct blk_io_trace *io = trace->io;
+       struct graph_line_data *gld;
        int action = io->action & BLK_TA_MASK;
        int seconds;
 
@@ -625,24 +1001,62 @@ void add_tput(struct trace *trace, struct graph_line_data *gld)
        if (action != tput_event(trace))
                return;
 
-       seconds = SECONDS(io->time);
-       if (seconds > gld->seconds) {
-               fprintf(stderr, "Bad record %d %d %d\n", seconds, gld->seconds, action);
-               abort();
-       }
+       if (BLK_DATADIR(io->action) & BLK_TC_READ)
+               gld = reads_gld;
+       else
+               gld = writes_gld;
 
+       seconds = SECONDS(io->time);
        gld->data[seconds].sum += io->bytes;
+
        gld->data[seconds].count = 1;
        if (gld->data[seconds].sum > gld->max)
                gld->max = gld->data[seconds].sum;
 }
 
-void add_io(struct trace *trace, struct graph_dot_data *gdd_writes,
-           struct graph_dot_data *gdd_reads)
+#define GDD_PTR_ALLOC_STEP 16
+
+static struct pid_map *get_pid_map(struct trace_file *tf, u32 pid)
+{
+       struct pid_map *pm;
+
+       if (!io_per_process) {
+               if (!tf->io_plots)
+                       tf->io_plots = 1;
+               return NULL;
+       }
+
+       pm = process_hash_insert(pid, NULL);
+       /* New entry? */
+       if (!pm->index) {
+               if (tf->io_plots == tf->io_plots_allocated) {
+                       tf->io_plots_allocated += GDD_PTR_ALLOC_STEP;
+                       tf->gdd_reads = realloc(tf->gdd_reads, tf->io_plots_allocated * sizeof(struct graph_dot_data *));
+                       if (!tf->gdd_reads)
+                               abort();
+                       tf->gdd_writes = realloc(tf->gdd_writes, tf->io_plots_allocated * sizeof(struct graph_dot_data *));
+                       if (!tf->gdd_writes)
+                               abort();
+                       memset(tf->gdd_reads + tf->io_plots_allocated - GDD_PTR_ALLOC_STEP,
+                              0, GDD_PTR_ALLOC_STEP * sizeof(struct graph_dot_data *));
+                       memset(tf->gdd_writes + tf->io_plots_allocated - GDD_PTR_ALLOC_STEP,
+                              0, GDD_PTR_ALLOC_STEP * sizeof(struct graph_dot_data *));
+               }
+               pm->index = tf->io_plots++;
+
+               return pm;
+       }
+       return pm;
+}
+
+void add_io(struct trace *trace, struct trace_file *tf)
 {
        struct blk_io_trace *io = trace->io;
        int action = io->action & BLK_TA_MASK;
        u64 offset;
+       int index;
+       char *label;
+       struct pid_map *pm;
 
        if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY))
                return;
@@ -650,40 +1064,87 @@ void add_io(struct trace *trace, struct graph_dot_data *gdd_writes,
        if (action != io_event(trace))
                return;
 
-       offset = io->sector << 9;
+       offset = map_io(trace, io);
 
-       if (BLK_DATADIR(io->action) & BLK_TC_READ)
-               set_gdd_bit(gdd_reads, offset, io->bytes, io->time);
-       else if (BLK_DATADIR(io->action) & BLK_TC_WRITE)
-               set_gdd_bit(gdd_writes, offset, io->bytes, io->time);
+       pm = get_pid_map(tf, io->pid);
+       if (!pm) {
+               index = 0;
+               label = "";
+       } else {
+               index = pm->index;
+               label = pm->name;
+       }
+       if (BLK_DATADIR(io->action) & BLK_TC_READ) {
+               if (!tf->gdd_reads[index])
+                       tf->gdd_reads[index] = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds, pick_color(), strdup(label));
+               set_gdd_bit(tf->gdd_reads[index], offset, io->bytes, io->time);
+       } else if (BLK_DATADIR(io->action) & BLK_TC_WRITE) {
+               if (!tf->gdd_writes[index])
+                       tf->gdd_writes[index] = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds, pick_color(), strdup(label));
+               set_gdd_bit(tf->gdd_writes[index], offset, io->bytes, io->time);
+       }
 }
 
 void add_pending_io(struct trace *trace, struct graph_line_data *gld)
 {
-       int ret;
-       int seconds;
+       unsigned int seconds;
        struct blk_io_trace *io = trace->io;
        int action = io->action & BLK_TA_MASK;
        double avg;
+       struct pending_io *pio;
 
        if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY))
                return;
 
-       if (action != __BLK_TA_ISSUE)
+       if (action == __BLK_TA_QUEUE) {
+               if (io->sector == 0)
+                       return;
+               /*
+                * If D (issue) events are available, use them for I/O
+                * accounting.  Nothing needs to be done for Q.
+                */
+               if (trace->found_issue)
+                       return;
+               /*
+                * If there are no D or C events, then all that can be
+                * done is to account the Q event (and make sure not to
+                * add the I/O to the hash, because it will never be
+                * removed).
+                */
+               if (!trace->found_completion)
+                       goto account_io;
+               /*
+                * When there are no ISSUE events, count depth and
+                * latency from queue events.
+                */
+               pio = hash_queued_io(trace->io);
+               if (pio) {
+                       pio->dispatch_time = io->time;
+                       goto account_io;
+               }
                return;
-
-       seconds = SECONDS(io->time);
-       if (seconds > gld->seconds) {
-               fprintf(stderr, "Bad record %d %d\n", seconds, gld->seconds);
-               abort();
        }
+       if (action == __BLK_TA_REQUEUE) {
+               if (ios_in_flight > 0)
+                       ios_in_flight--;
+               return;
+       }
+       if (action != __BLK_TA_ISSUE)
+               return;
 
-       ret = hash_dispatched_io(trace->io);
-       if (ret)
+       pio = hash_dispatched_io(trace->io);
+       if (!pio)
                return;
 
+       if (!trace->found_completion) {
+               list_del(&pio->hash_list);
+               free(pio);
+       }
+
+account_io:
        ios_in_flight++;
 
+       seconds = SECONDS(io->time);
        gld->data[seconds].sum += ios_in_flight;
        gld->data[seconds].count++;
 
@@ -747,11 +1208,6 @@ void add_iop(struct trace *trace, struct graph_line_data *gld)
                return;
 
        seconds = SECONDS(io->time);
-       if (seconds > gld->seconds) {
-               fprintf(stderr, "Bad record %d %d\n", seconds, gld->seconds);
-               abort();
-       }
-
        gld->data[seconds].sum += 1;
        gld->data[seconds].count = 1;
        if (gld->data[seconds].sum > gld->max)
@@ -760,21 +1216,5 @@ void add_iop(struct trace *trace, struct graph_line_data *gld)
 
 void check_record(struct trace *trace)
 {
-       struct blk_io_trace *io = trace->io;
-       int action = io->action & BLK_TA_MASK;
-
-       if (!(io->action & BLK_TC_ACT(BLK_TC_NOTIFY))) {
-               switch (action) {
-               case __BLK_TA_COMPLETE:
-                       trace->found_completion = 1;
-                       break;
-               case __BLK_TA_ISSUE:
-                       trace->found_issue = 1;
-                       break;
-               case __BLK_TA_QUEUE:
-                       trace->found_queue = 1;
-                       break;
-               };
-       }
        handle_notify(trace);
 }