[PATCH] blkparse: another stab at stopwatch_end fixing
[blktrace.git] / blkparse.c
index de5c9a62128b7dd2cbe9aadd5fbd7ea21ffa9853..c9b37593d161ab28d2bf33c1b418c3c762e162bc 100644 (file)
@@ -59,6 +59,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
@@ -225,9 +226,6 @@ static inline int ppi_hash(struct per_process_info *ppi)
        if (ppi_hash_by_pid)
                return ppi_hash_pid(ppi->pid);
 
-       if (ppi->name[0] == 0)
-               fprintf(stderr, "bad\n");
-
        return ppi_hash_name(ppi->name);
 }
 
@@ -280,10 +278,16 @@ static struct per_process_info *find_process_by_pid(__u32 pid)
 
 static struct per_process_info *find_process(__u32 pid, char *name)
 {
+       struct per_process_info *ppi;
+
        if (ppi_hash_by_pid)
                return find_process_by_pid(pid);
 
-       return find_process_by_name(name);
+       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)
@@ -1034,10 +1038,10 @@ static void show_process_stats(void)
        while (ppi) {
                char name[64];
 
-               if (ppi_hash_by_pid)
-                       sprintf(name, "%s (%u)", ppi->name, ppi->pid);
-               else
+               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);
@@ -1105,43 +1109,6 @@ static void show_device_and_cpu_stats(void)
        }
 }
 
-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 int sort_entries(void)
-{
-       struct trace *t;
-       int nr = 0;
-
-       if (!genesis_time)
-               find_genesis();
-
-       while ((t = trace_list) != NULL) {
-               trace_list = t->next;
-
-               if (verify_trace(t->bit))
-                       continue;
-
-               t->bit->time -= genesis_time;
-
-               if (trace_rb_insert(t))
-                       break;
-               nr++;
-       }
-
-       return nr;
-}
-
 /*
  * struct trace and blktrace allocation cache, we do potentially
  * millions of mallocs for these structures while only using at most
@@ -1187,6 +1154,61 @@ static inline struct blk_io_trace *bit_alloc(void)
        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;
+}
+
+static int sort_entries(void)
+{
+       struct trace *t;
+       int nr = 0;
+
+       if (!genesis_time)
+               find_genesis();
+
+       while ((t = trace_list) != NULL) {
+               struct blk_io_trace *bit = t->bit;
+
+               trace_list = t->next;
+
+               if (verify_trace(bit))
+                       continue;
+
+               bit->time -= genesis_time;
+
+               if (check_stopwatch(bit)) {
+                       bit_free(bit);
+                       t_free(t);
+                       continue;
+               }
+
+               if (trace_rb_insert(t))
+                       break;
+
+               nr++;
+       }
+
+       return nr;
+}
+
 static void show_entries_rb(int force)
 {
        struct per_dev_info *pdi = NULL;
@@ -1244,22 +1266,17 @@ static void show_entries_rb(int force)
                }
 
 ok:
-               if (bit->time >= stopwatch_end)
-                       break;
-
                if (!force && bit->time > last_allowed_time)
                        break;
 
                pdi->last_sequence = bit->sequence;
 
-               if (bit->time >= stopwatch_start) {
-                       check_time(pdi, bit);
+               check_time(pdi, bit);
 
-                       if (!pci || pci->cpu != bit->cpu)
-                               pci = get_cpu_info(pdi, bit->cpu);
+               if (!pci || pci->cpu != bit->cpu)
+                       pci = get_cpu_info(pdi, bit->cpu);
 
-                       dump_trace(bit, pci, pdi);
-               }
+               dump_trace(bit, pci, pdi);
 
                rb_erase(&t->rb_node, &rb_sort_root);
                rb_sort_entries--;
@@ -1337,6 +1354,7 @@ static int read_events(int fd, int always_block)
                t->bit = bit;
 
                trace_to_cpu(bit);
+
                t->next = trace_list;
                trace_list = t;
 
@@ -1575,7 +1593,7 @@ int main(int argc, char *argv[])
                                return 1;
                        break;
                case 'n':
-                       ppi_hash_by_pid = 1;
+                       ppi_hash_by_pid = 0;
                        break;
                case 'v':
                        printf("%s version %s\n", argv[0], blkparse_version);