perf hist: Only allocate callchain_node if processing callchains
[safe/jmp/linux-2.6] / tools / perf / util / hist.c
index 6e416a6..18cf8b3 100644 (file)
@@ -1,6 +1,7 @@
 #include "hist.h"
 #include "session.h"
 #include "sort.h"
+#include <math.h>
 
 struct callchain_param callchain_param = {
        .mode   = CHAIN_GRAPH_REL,
@@ -11,18 +12,20 @@ struct callchain_param      callchain_param = {
  * histogram, sorted on item, collects counts
  */
 
-struct hist_entry *__perf_session__add_hist_entry(struct perf_session *self,
+struct hist_entry *__perf_session__add_hist_entry(struct rb_root *hists,
                                                  struct addr_location *al,
                                                  struct symbol *sym_parent,
                                                  u64 count, bool *hit)
 {
-       struct rb_node **p = &self->hists.rb_node;
+       struct rb_node **p = &hists->rb_node;
        struct rb_node *parent = NULL;
        struct hist_entry *he;
        struct hist_entry entry = {
                .thread = al->thread,
-               .map    = al->map,
-               .sym    = al->sym,
+               .ms = {
+                       .map    = al->map,
+                       .sym    = al->sym,
+               },
                .ip     = al->addr,
                .level  = al->level,
                .count  = count,
@@ -47,12 +50,13 @@ struct hist_entry *__perf_session__add_hist_entry(struct perf_session *self,
                        p = &(*p)->rb_right;
        }
 
-       he = malloc(sizeof(*he));
+       he = malloc(sizeof(*he) + (symbol_conf.use_callchain ?
+                                   sizeof(struct callchain_node) : 0));
        if (!he)
                return NULL;
        *he = entry;
        rb_link_node(&he->rb_node, parent, p);
-       rb_insert_color(&he->rb_node, &self->hists);
+       rb_insert_color(&he->rb_node, hists);
        *hit = false;
        return he;
 }
@@ -129,7 +133,7 @@ static void collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
        rb_insert_color(&he->rb_node, root);
 }
 
-void perf_session__collapse_resort(struct perf_session *self)
+void perf_session__collapse_resort(struct rb_root *hists)
 {
        struct rb_root tmp;
        struct rb_node *next;
@@ -139,17 +143,17 @@ void perf_session__collapse_resort(struct perf_session *self)
                return;
 
        tmp = RB_ROOT;
-       next = rb_first(&self->hists);
+       next = rb_first(hists);
 
        while (next) {
                n = rb_entry(next, struct hist_entry, rb_node);
                next = rb_next(&n->rb_node);
 
-               rb_erase(&n->rb_node, &self->hists);
+               rb_erase(&n->rb_node, hists);
                collapse__insert_entry(&tmp, n);
        }
 
-       self->hists = tmp;
+       *hists = tmp;
 }
 
 /*
@@ -165,7 +169,7 @@ static void perf_session__insert_output_hist_entry(struct rb_root *root,
        struct hist_entry *iter;
 
        if (symbol_conf.use_callchain)
-               callchain_param.sort(&he->sorted_chain, &he->callchain,
+               callchain_param.sort(&he->sorted_chain, he->callchain,
                                      min_callchain_hits, &callchain_param);
 
        while (*p != NULL) {
@@ -182,29 +186,32 @@ static void perf_session__insert_output_hist_entry(struct rb_root *root,
        rb_insert_color(&he->rb_node, root);
 }
 
-void perf_session__output_resort(struct perf_session *self, u64 total_samples)
+u64 perf_session__output_resort(struct rb_root *hists, u64 total_samples)
 {
        struct rb_root tmp;
        struct rb_node *next;
        struct hist_entry *n;
        u64 min_callchain_hits;
+       u64 nr_hists = 0;
 
        min_callchain_hits =
                total_samples * (callchain_param.min_percent / 100);
 
        tmp = RB_ROOT;
-       next = rb_first(&self->hists);
+       next = rb_first(hists);
 
        while (next) {
                n = rb_entry(next, struct hist_entry, rb_node);
                next = rb_next(&n->rb_node);
 
-               rb_erase(&n->rb_node, &self->hists);
+               rb_erase(&n->rb_node, hists);
                perf_session__insert_output_hist_entry(&tmp, n,
                                                       min_callchain_hits);
+               ++nr_hists;
        }
 
-       self->hists = tmp;
+       *hists = tmp;
+       return nr_hists;
 }
 
 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
@@ -257,8 +264,8 @@ static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
                } else
                        ret += fprintf(fp, "%s", "          ");
        }
-       if (chain->sym)
-               ret += fprintf(fp, "%s\n", chain->sym->name);
+       if (chain->ms.sym)
+               ret += fprintf(fp, "%s\n", chain->ms.sym->name);
        else
                ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
 
@@ -277,7 +284,7 @@ static void init_rem_hits(void)
        }
 
        strcpy(rem_sq_bracket->name, "[...]");
-       rem_hits.sym = rem_sq_bracket;
+       rem_hits.ms.sym = rem_sq_bracket;
 }
 
 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
@@ -320,15 +327,13 @@ static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
                        new_depth_mask &= ~(1 << (depth - 1));
 
                /*
-                * But we keep the older depth mask for the line seperator
+                * But we keep the older depth mask for the line separator
                 * to keep the level link until we reach the last child
                 */
                ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
                                                   left_margin);
                i = 0;
                list_for_each_entry(chain, &child->val, list) {
-                       if (chain->ip >= PERF_CONTEXT_MAX)
-                               continue;
                        ret += ipchain__fprintf_graph(fp, chain, depth,
                                                      new_depth_mask, i++,
                                                      new_total,
@@ -367,9 +372,6 @@ static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
        int ret = 0;
 
        list_for_each_entry(chain, &self->val, list) {
-               if (chain->ip >= PERF_CONTEXT_MAX)
-                       continue;
-
                if (!i++ && sort__first_dimension == SORT_SYM)
                        continue;
 
@@ -384,8 +386,8 @@ static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
                } else
                        ret += callchain__fprintf_left_margin(fp, left_margin);
 
-               if (chain->sym)
-                       ret += fprintf(fp, " %s\n", chain->sym->name);
+               if (chain->ms.sym)
+                       ret += fprintf(fp, " %s\n", chain->ms.sym->name);
                else
                        ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
        }
@@ -410,8 +412,8 @@ static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
        list_for_each_entry(chain, &self->val, list) {
                if (chain->ip >= PERF_CONTEXT_MAX)
                        continue;
-               if (chain->sym)
-                       ret += fprintf(fp, "                %s\n", chain->sym->name);
+               if (chain->ms.sym)
+                       ret += fprintf(fp, "                %s\n", chain->ms.sym->name);
                else
                        ret += fprintf(fp, "                %p\n",
                                        (void *)(long)chain->ip);
@@ -454,79 +456,166 @@ static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
        return ret;
 }
 
-static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
-                                 struct perf_session *session)
+int hist_entry__snprintf(struct hist_entry *self,
+                          char *s, size_t size,
+                          struct perf_session *pair_session,
+                          bool show_displacement,
+                          long displacement, bool color,
+                          u64 session_total)
 {
        struct sort_entry *se;
-       size_t ret;
+       u64 count, total;
+       const char *sep = symbol_conf.field_sep;
+       int ret;
 
        if (symbol_conf.exclude_other && !self->parent)
                return 0;
 
-       if (session->events_stats.total)
-               ret = percent_color_fprintf(fp,
-                                           symbol_conf.field_sep ? "%.2f" : "   %6.2f%%",
-                                       (self->count * 100.0) / session->events_stats.total);
-       else
-               ret = fprintf(fp, symbol_conf.field_sep ? "%lld" : "%12lld ", self->count);
+       if (pair_session) {
+               count = self->pair ? self->pair->count : 0;
+               total = pair_session->events_stats.total;
+       } else {
+               count = self->count;
+               total = session_total;
+       }
+
+       if (total) {
+               if (color)
+                       ret = percent_color_snprintf(s, size,
+                                                    sep ? "%.2f" : "   %6.2f%%",
+                                                    (count * 100.0) / total);
+               else
+                       ret = snprintf(s, size, sep ? "%.2f" : "   %6.2f%%",
+                                      (count * 100.0) / total);
+       } else
+               ret = snprintf(s, size, sep ? "%lld" : "%12lld ", count);
 
        if (symbol_conf.show_nr_samples) {
-               if (symbol_conf.field_sep)
-                       fprintf(fp, "%c%lld", *symbol_conf.field_sep, self->count);
+               if (sep)
+                       ret += snprintf(s + ret, size - ret, "%c%lld", *sep, count);
                else
-                       fprintf(fp, "%11lld", self->count);
+                       ret += snprintf(s + ret, size - ret, "%11lld", count);
+       }
+
+       if (pair_session) {
+               char bf[32];
+               double old_percent = 0, new_percent = 0, diff;
+
+               if (total > 0)
+                       old_percent = (count * 100.0) / total;
+               if (session_total > 0)
+                       new_percent = (self->count * 100.0) / session_total;
+
+               diff = new_percent - old_percent;
+
+               if (fabs(diff) >= 0.01)
+                       snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
+               else
+                       snprintf(bf, sizeof(bf), " ");
+
+               if (sep)
+                       ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
+               else
+                       ret += snprintf(s + ret, size - ret, "%11.11s", bf);
+
+               if (show_displacement) {
+                       if (displacement)
+                               snprintf(bf, sizeof(bf), "%+4ld", displacement);
+                       else
+                               snprintf(bf, sizeof(bf), " ");
+
+                       if (sep)
+                               ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
+                       else
+                               ret += snprintf(s + ret, size - ret, "%6.6s", bf);
+               }
        }
 
        list_for_each_entry(se, &hist_entry__sort_list, list) {
                if (se->elide)
                        continue;
 
-               fprintf(fp, "%s", symbol_conf.field_sep ?: "  ");
-               ret += se->print(fp, self, se->width ? *se->width : 0);
+               ret += snprintf(s + ret, size - ret, "%s", sep ?: "  ");
+               ret += se->snprintf(self, s + ret, size - ret,
+                                   se->width ? *se->width : 0);
        }
 
-       ret += fprintf(fp, "\n");
+       return ret;
+}
 
-       if (symbol_conf.use_callchain) {
-               int left_margin = 0;
+int hist_entry__fprintf(struct hist_entry *self,
+                       struct perf_session *pair_session,
+                       bool show_displacement,
+                       long displacement, FILE *fp,
+                       u64 session_total)
+{
+       char bf[512];
+       hist_entry__snprintf(self, bf, sizeof(bf), pair_session,
+                            show_displacement, displacement,
+                            true, session_total);
+       return fprintf(fp, "%s\n", bf);
+}
 
-               if (sort__first_dimension == SORT_COMM) {
-                       se = list_first_entry(&hist_entry__sort_list, typeof(*se),
-                                               list);
-                       left_margin = se->width ? *se->width : 0;
-                       left_margin -= thread__comm_len(self->thread);
-               }
+static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
+                                           u64 session_total)
+{
+       int left_margin = 0;
 
-               hist_entry_callchain__fprintf(fp, self, session->events_stats.total,
-                                             left_margin);
+       if (sort__first_dimension == SORT_COMM) {
+               struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
+                                                        typeof(*se), list);
+               left_margin = se->width ? *se->width : 0;
+               left_margin -= thread__comm_len(self->thread);
        }
 
-       return ret;
+       return hist_entry_callchain__fprintf(fp, self, session_total,
+                                            left_margin);
 }
 
-size_t perf_session__fprintf_hists(struct perf_session *self, FILE *fp)
+size_t perf_session__fprintf_hists(struct rb_root *hists,
+                                  struct perf_session *pair,
+                                  bool show_displacement, FILE *fp,
+                                  u64 session_total)
 {
-       struct hist_entry *pos;
        struct sort_entry *se;
        struct rb_node *nd;
        size_t ret = 0;
+       unsigned long position = 1;
+       long displacement = 0;
        unsigned int width;
+       const char *sep = symbol_conf.field_sep;
        char *col_width = symbol_conf.col_width_list_str;
 
        init_rem_hits();
 
-       fprintf(fp, "# Overhead");
+       fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
+
        if (symbol_conf.show_nr_samples) {
-               if (symbol_conf.field_sep)
-                       fprintf(fp, "%cSamples", *symbol_conf.field_sep);
+               if (sep)
+                       fprintf(fp, "%cSamples", *sep);
                else
                        fputs("  Samples  ", fp);
        }
+
+       if (pair) {
+               if (sep)
+                       ret += fprintf(fp, "%cDelta", *sep);
+               else
+                       ret += fprintf(fp, "  Delta    ");
+
+               if (show_displacement) {
+                       if (sep)
+                               ret += fprintf(fp, "%cDisplacement", *sep);
+                       else
+                               ret += fprintf(fp, " Displ");
+               }
+       }
+
        list_for_each_entry(se, &hist_entry__sort_list, list) {
                if (se->elide)
                        continue;
-               if (symbol_conf.field_sep) {
-                       fprintf(fp, "%c%s", *symbol_conf.field_sep, se->header);
+               if (sep) {
+                       fprintf(fp, "%c%s", *sep, se->header);
                        continue;
                }
                width = strlen(se->header);
@@ -545,12 +634,17 @@ size_t perf_session__fprintf_hists(struct perf_session *self, FILE *fp)
        }
        fprintf(fp, "\n");
 
-       if (symbol_conf.field_sep)
+       if (sep)
                goto print_entries;
 
        fprintf(fp, "# ........");
        if (symbol_conf.show_nr_samples)
                fprintf(fp, " ..........");
+       if (pair) {
+               fprintf(fp, " ..........");
+               if (show_displacement)
+                       fprintf(fp, " .....");
+       }
        list_for_each_entry(se, &hist_entry__sort_list, list) {
                unsigned int i;
 
@@ -565,14 +659,32 @@ size_t perf_session__fprintf_hists(struct perf_session *self, FILE *fp)
                for (i = 0; i < width; i++)
                        fprintf(fp, ".");
        }
-       fprintf(fp, "\n");
 
-       fprintf(fp, "#\n");
+       fprintf(fp, "\n#\n");
 
 print_entries:
-       for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
-               pos = rb_entry(nd, struct hist_entry, rb_node);
-               ret += hist_entry__fprintf(fp, pos, self);
+       for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
+               struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
+
+               if (show_displacement) {
+                       if (h->pair != NULL)
+                               displacement = ((long)h->pair->position -
+                                               (long)position);
+                       else
+                               displacement = 0;
+                       ++position;
+               }
+               ret += hist_entry__fprintf(h, pair, show_displacement,
+                                          displacement, fp, session_total);
+
+               if (symbol_conf.use_callchain)
+                       ret += hist_entry__fprintf_callchain(h, fp, session_total);
+
+               if (h->ms.map == NULL && verbose > 1) {
+                       __map_groups__fprintf_maps(&h->thread->mg,
+                                                  MAP__FUNCTION, verbose, fp);
+                       fprintf(fp, "%.10s end\n", graph_dotted_line);
+               }
        }
 
        free(rem_sq_bracket);