Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1 | #include "annotate.h" |
Frederic Weisbecker | 8a0ecfb | 2010-05-13 19:47:16 +0200 | [diff] [blame] | 2 | #include "util.h" |
Frederic Weisbecker | 598357e | 2010-05-21 12:48:39 +0200 | [diff] [blame] | 3 | #include "build-id.h" |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 4 | #include "hist.h" |
Arnaldo Carvalho de Melo | 4e4f06e | 2009-12-14 13:10:39 -0200 | [diff] [blame] | 5 | #include "session.h" |
| 6 | #include "sort.h" |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 7 | #include <math.h> |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 8 | |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 9 | enum hist_filter { |
| 10 | HIST_FILTER__DSO, |
| 11 | HIST_FILTER__THREAD, |
| 12 | HIST_FILTER__PARENT, |
| 13 | }; |
| 14 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 15 | struct callchain_param callchain_param = { |
| 16 | .mode = CHAIN_GRAPH_REL, |
Sam Liao | d797fdc | 2011-06-07 23:49:46 +0800 | [diff] [blame] | 17 | .min_percent = 0.5, |
| 18 | .order = ORDER_CALLEE |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 19 | }; |
| 20 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 21 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 22 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 23 | return hists->col_len[col]; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 24 | } |
| 25 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 26 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 27 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 28 | hists->col_len[col] = len; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 29 | } |
| 30 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 31 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 32 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 33 | if (len > hists__col_len(hists, col)) { |
| 34 | hists__set_col_len(hists, col, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 35 | return true; |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 40 | static void hists__reset_col_len(struct hists *hists) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 41 | { |
| 42 | enum hist_column col; |
| 43 | |
| 44 | for (col = 0; col < HISTC_NR_COLS; ++col) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 45 | hists__set_col_len(hists, col, 0); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 46 | } |
| 47 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 48 | static void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 49 | { |
| 50 | u16 len; |
| 51 | |
| 52 | if (h->ms.sym) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 53 | hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen); |
Arnaldo Carvalho de Melo | d7603d5 | 2011-03-04 14:51:33 -0300 | [diff] [blame] | 54 | else { |
| 55 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
| 56 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 57 | if (hists__col_len(hists, HISTC_DSO) < unresolved_col_width && |
Arnaldo Carvalho de Melo | d7603d5 | 2011-03-04 14:51:33 -0300 | [diff] [blame] | 58 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 59 | !symbol_conf.dso_list) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 60 | hists__set_col_len(hists, HISTC_DSO, |
Arnaldo Carvalho de Melo | d7603d5 | 2011-03-04 14:51:33 -0300 | [diff] [blame] | 61 | unresolved_col_width); |
| 62 | } |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 63 | |
| 64 | len = thread__comm_len(h->thread); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 65 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
| 66 | hists__set_col_len(hists, HISTC_THREAD, len + 6); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 67 | |
| 68 | if (h->ms.map) { |
| 69 | len = dso__name_len(h->ms.map->dso); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 70 | hists__new_col_len(hists, HISTC_DSO, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 74 | static void hist_entry__add_cpumode_period(struct hist_entry *self, |
| 75 | unsigned int cpumode, u64 period) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 76 | { |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 77 | switch (cpumode) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 78 | case PERF_RECORD_MISC_KERNEL: |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 79 | self->period_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 80 | break; |
| 81 | case PERF_RECORD_MISC_USER: |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 82 | self->period_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 83 | break; |
| 84 | case PERF_RECORD_MISC_GUEST_KERNEL: |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 85 | self->period_guest_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 86 | break; |
| 87 | case PERF_RECORD_MISC_GUEST_USER: |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 88 | self->period_guest_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
Arnaldo Carvalho de Melo | ab81f3fd | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 95 | static void hist_entry__decay(struct hist_entry *he) |
| 96 | { |
| 97 | he->period = (he->period * 7) / 8; |
| 98 | he->nr_events = (he->nr_events * 7) / 8; |
| 99 | } |
| 100 | |
| 101 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) |
| 102 | { |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame^] | 103 | if (he->period == 0) |
| 104 | return true; |
Arnaldo Carvalho de Melo | ab81f3fd | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 105 | hists->stats.total_period -= he->period; |
| 106 | hist_entry__decay(he); |
| 107 | hists->stats.total_period += he->period; |
| 108 | return he->period == 0; |
| 109 | } |
| 110 | |
| 111 | void hists__decay_entries(struct hists *hists) |
| 112 | { |
| 113 | struct rb_node *next = rb_first(&hists->entries); |
| 114 | struct hist_entry *n; |
| 115 | |
| 116 | while (next) { |
| 117 | n = rb_entry(next, struct hist_entry, rb_node); |
| 118 | next = rb_next(&n->rb_node); |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame^] | 119 | /* |
| 120 | * We may be annotating this, for instance, so keep it here in |
| 121 | * case some it gets new samples, we'll eventually free it when |
| 122 | * the user stops browsing and it agains gets fully decayed. |
| 123 | */ |
| 124 | if (hists__decay_entry(hists, n) && !n->used) { |
Arnaldo Carvalho de Melo | ab81f3fd | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 125 | rb_erase(&n->rb_node, &hists->entries); |
| 126 | |
| 127 | if (sort__need_collapse) |
| 128 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
| 129 | |
| 130 | hist_entry__free(n); |
| 131 | --hists->nr_entries; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 136 | /* |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 137 | * histogram, sorted on item, collects periods |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 138 | */ |
| 139 | |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 140 | static struct hist_entry *hist_entry__new(struct hist_entry *template) |
| 141 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 142 | size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0; |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 143 | struct hist_entry *self = malloc(sizeof(*self) + callchain_size); |
| 144 | |
| 145 | if (self != NULL) { |
| 146 | *self = *template; |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 147 | self->nr_events = 1; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 148 | if (self->ms.map) |
| 149 | self->ms.map->referenced = true; |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 150 | if (symbol_conf.use_callchain) |
| 151 | callchain_init(self->callchain); |
| 152 | } |
| 153 | |
| 154 | return self; |
| 155 | } |
| 156 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 157 | static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h) |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 158 | { |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 159 | if (!h->filtered) { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 160 | hists__calc_col_len(hists, h); |
| 161 | ++hists->nr_entries; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 162 | hists->stats.total_period += h->period; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 163 | } |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 164 | } |
| 165 | |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 166 | static u8 symbol__parent_filter(const struct symbol *parent) |
| 167 | { |
| 168 | if (symbol_conf.exclude_other && parent == NULL) |
| 169 | return 1 << HIST_FILTER__PARENT; |
| 170 | return 0; |
| 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 173 | struct hist_entry *__hists__add_entry(struct hists *hists, |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 174 | struct addr_location *al, |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 175 | struct symbol *sym_parent, u64 period) |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 176 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 177 | struct rb_node **p; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 178 | struct rb_node *parent = NULL; |
| 179 | struct hist_entry *he; |
| 180 | struct hist_entry entry = { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 181 | .thread = al->thread, |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 182 | .ms = { |
| 183 | .map = al->map, |
| 184 | .sym = al->sym, |
| 185 | }, |
Arun Sharma | f60f359 | 2010-06-04 11:27:10 -0300 | [diff] [blame] | 186 | .cpu = al->cpu, |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 187 | .ip = al->addr, |
| 188 | .level = al->level, |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 189 | .period = period, |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 190 | .parent = sym_parent, |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 191 | .filtered = symbol__parent_filter(sym_parent), |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 192 | }; |
| 193 | int cmp; |
| 194 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 195 | pthread_mutex_lock(&hists->lock); |
| 196 | |
| 197 | p = &hists->entries_in->rb_node; |
| 198 | |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 199 | while (*p != NULL) { |
| 200 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 201 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 202 | |
| 203 | cmp = hist_entry__cmp(&entry, he); |
| 204 | |
| 205 | if (!cmp) { |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 206 | he->period += period; |
| 207 | ++he->nr_events; |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 208 | goto out; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (cmp < 0) |
| 212 | p = &(*p)->rb_left; |
| 213 | else |
| 214 | p = &(*p)->rb_right; |
| 215 | } |
| 216 | |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 217 | he = hist_entry__new(&entry); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 218 | if (!he) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 219 | goto out_unlock; |
| 220 | |
| 221 | rb_link_node(&he->rb_node_in, parent, p); |
| 222 | rb_insert_color(&he->rb_node_in, hists->entries_in); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 223 | out: |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 224 | hist_entry__add_cpumode_period(he, al->cpumode, period); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 225 | out_unlock: |
| 226 | pthread_mutex_unlock(&hists->lock); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 227 | return he; |
| 228 | } |
| 229 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 230 | int64_t |
| 231 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 232 | { |
| 233 | struct sort_entry *se; |
| 234 | int64_t cmp = 0; |
| 235 | |
| 236 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 237 | cmp = se->se_cmp(left, right); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 238 | if (cmp) |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | return cmp; |
| 243 | } |
| 244 | |
| 245 | int64_t |
| 246 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 247 | { |
| 248 | struct sort_entry *se; |
| 249 | int64_t cmp = 0; |
| 250 | |
| 251 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 252 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 253 | |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 254 | f = se->se_collapse ?: se->se_cmp; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 255 | |
| 256 | cmp = f(left, right); |
| 257 | if (cmp) |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | return cmp; |
| 262 | } |
| 263 | |
| 264 | void hist_entry__free(struct hist_entry *he) |
| 265 | { |
| 266 | free(he); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * collapse the histogram |
| 271 | */ |
| 272 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 273 | static bool hists__collapse_insert_entry(struct hists *hists, |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 274 | struct rb_root *root, |
| 275 | struct hist_entry *he) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 276 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 277 | struct rb_node **p = &root->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 278 | struct rb_node *parent = NULL; |
| 279 | struct hist_entry *iter; |
| 280 | int64_t cmp; |
| 281 | |
| 282 | while (*p != NULL) { |
| 283 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 284 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 285 | |
| 286 | cmp = hist_entry__collapse(iter, he); |
| 287 | |
| 288 | if (!cmp) { |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 289 | iter->period += he->period; |
Stephane Eranian | e39622c | 2011-10-03 11:38:15 +0200 | [diff] [blame] | 290 | iter->nr_events += he->nr_events; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 291 | if (symbol_conf.use_callchain) { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 292 | callchain_cursor_reset(&hists->callchain_cursor); |
| 293 | callchain_merge(&hists->callchain_cursor, iter->callchain, |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 294 | he->callchain); |
| 295 | } |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 296 | hist_entry__free(he); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 297 | return false; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (cmp < 0) |
| 301 | p = &(*p)->rb_left; |
| 302 | else |
| 303 | p = &(*p)->rb_right; |
| 304 | } |
| 305 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 306 | rb_link_node(&he->rb_node_in, parent, p); |
| 307 | rb_insert_color(&he->rb_node_in, root); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 308 | return true; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 311 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
| 312 | { |
| 313 | struct rb_root *root; |
| 314 | |
| 315 | pthread_mutex_lock(&hists->lock); |
| 316 | |
| 317 | root = hists->entries_in; |
| 318 | if (++hists->entries_in > &hists->entries_in_array[1]) |
| 319 | hists->entries_in = &hists->entries_in_array[0]; |
| 320 | |
| 321 | pthread_mutex_unlock(&hists->lock); |
| 322 | |
| 323 | return root; |
| 324 | } |
| 325 | |
| 326 | static void __hists__collapse_resort(struct hists *hists, bool threaded) |
| 327 | { |
| 328 | struct rb_root *root; |
| 329 | struct rb_node *next; |
| 330 | struct hist_entry *n; |
| 331 | |
| 332 | if (!sort__need_collapse && !threaded) |
| 333 | return; |
| 334 | |
| 335 | root = hists__get_rotate_entries_in(hists); |
| 336 | next = rb_first(root); |
| 337 | hists->stats.total_period = 0; |
| 338 | |
| 339 | while (next) { |
| 340 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 341 | next = rb_next(&n->rb_node_in); |
| 342 | |
| 343 | rb_erase(&n->rb_node_in, root); |
| 344 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) |
| 345 | hists__inc_nr_entries(hists, n); |
| 346 | } |
| 347 | } |
| 348 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 349 | void hists__collapse_resort(struct hists *hists) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 350 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 351 | return __hists__collapse_resort(hists, false); |
| 352 | } |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 353 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 354 | void hists__collapse_resort_threaded(struct hists *hists) |
| 355 | { |
| 356 | return __hists__collapse_resort(hists, true); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 360 | * reverse the map, sort on period. |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 361 | */ |
| 362 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 363 | static void __hists__insert_output_entry(struct rb_root *entries, |
| 364 | struct hist_entry *he, |
| 365 | u64 min_callchain_hits) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 366 | { |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 367 | struct rb_node **p = &entries->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 368 | struct rb_node *parent = NULL; |
| 369 | struct hist_entry *iter; |
| 370 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 371 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | b9fb930 | 2010-04-02 09:50:42 -0300 | [diff] [blame] | 372 | callchain_param.sort(&he->sorted_chain, he->callchain, |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 373 | min_callchain_hits, &callchain_param); |
| 374 | |
| 375 | while (*p != NULL) { |
| 376 | parent = *p; |
| 377 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 378 | |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 379 | if (he->period > iter->period) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 380 | p = &(*p)->rb_left; |
| 381 | else |
| 382 | p = &(*p)->rb_right; |
| 383 | } |
| 384 | |
| 385 | rb_link_node(&he->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 386 | rb_insert_color(&he->rb_node, entries); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 389 | static void __hists__output_resort(struct hists *hists, bool threaded) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 390 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 391 | struct rb_root *root; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 392 | struct rb_node *next; |
| 393 | struct hist_entry *n; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 394 | u64 min_callchain_hits; |
| 395 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 396 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 397 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 398 | if (sort__need_collapse || threaded) |
| 399 | root = &hists->entries_collapsed; |
| 400 | else |
| 401 | root = hists->entries_in; |
| 402 | |
| 403 | next = rb_first(root); |
| 404 | hists->entries = RB_ROOT; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 405 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 406 | hists->nr_entries = 0; |
| 407 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 408 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 409 | while (next) { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 410 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 411 | next = rb_next(&n->rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 412 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 413 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 414 | hists__inc_nr_entries(hists, n); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 415 | } |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 416 | } |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 417 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 418 | void hists__output_resort(struct hists *hists) |
| 419 | { |
| 420 | return __hists__output_resort(hists, false); |
| 421 | } |
| 422 | |
| 423 | void hists__output_resort_threaded(struct hists *hists) |
| 424 | { |
| 425 | return __hists__output_resort(hists, true); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 426 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 427 | |
| 428 | static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin) |
| 429 | { |
| 430 | int i; |
| 431 | int ret = fprintf(fp, " "); |
| 432 | |
| 433 | for (i = 0; i < left_margin; i++) |
| 434 | ret += fprintf(fp, " "); |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, |
| 440 | int left_margin) |
| 441 | { |
| 442 | int i; |
| 443 | size_t ret = callchain__fprintf_left_margin(fp, left_margin); |
| 444 | |
| 445 | for (i = 0; i < depth; i++) |
| 446 | if (depth_mask & (1 << i)) |
| 447 | ret += fprintf(fp, "| "); |
| 448 | else |
| 449 | ret += fprintf(fp, " "); |
| 450 | |
| 451 | ret += fprintf(fp, "\n"); |
| 452 | |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 457 | int depth, int depth_mask, int period, |
Frederic Weisbecker | d425de5 | 2011-01-03 16:13:11 +0100 | [diff] [blame] | 458 | u64 total_samples, u64 hits, |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 459 | int left_margin) |
| 460 | { |
| 461 | int i; |
| 462 | size_t ret = 0; |
| 463 | |
| 464 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 465 | for (i = 0; i < depth; i++) { |
| 466 | if (depth_mask & (1 << i)) |
| 467 | ret += fprintf(fp, "|"); |
| 468 | else |
| 469 | ret += fprintf(fp, " "); |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 470 | if (!period && i == depth - 1) { |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 471 | double percent; |
| 472 | |
| 473 | percent = hits * 100.0 / total_samples; |
| 474 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); |
| 475 | } else |
| 476 | ret += fprintf(fp, "%s", " "); |
| 477 | } |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 478 | if (chain->ms.sym) |
| 479 | ret += fprintf(fp, "%s\n", chain->ms.sym->name); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 480 | else |
| 481 | ret += fprintf(fp, "%p\n", (void *)(long)chain->ip); |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | static struct symbol *rem_sq_bracket; |
| 487 | static struct callchain_list rem_hits; |
| 488 | |
| 489 | static void init_rem_hits(void) |
| 490 | { |
| 491 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); |
| 492 | if (!rem_sq_bracket) { |
| 493 | fprintf(stderr, "Not enough memory to display remaining hits\n"); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | strcpy(rem_sq_bracket->name, "[...]"); |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 498 | rem_hits.ms.sym = rem_sq_bracket; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
| 502 | u64 total_samples, int depth, |
| 503 | int depth_mask, int left_margin) |
| 504 | { |
| 505 | struct rb_node *node, *next; |
| 506 | struct callchain_node *child; |
| 507 | struct callchain_list *chain; |
| 508 | int new_depth_mask = depth_mask; |
| 509 | u64 new_total; |
| 510 | u64 remaining; |
| 511 | size_t ret = 0; |
| 512 | int i; |
Arnaldo Carvalho de Melo | 232a5c9 | 2010-05-09 20:28:10 -0300 | [diff] [blame] | 513 | uint entries_printed = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 514 | |
| 515 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 516 | new_total = self->children_hit; |
| 517 | else |
| 518 | new_total = total_samples; |
| 519 | |
| 520 | remaining = new_total; |
| 521 | |
| 522 | node = rb_first(&self->rb_root); |
| 523 | while (node) { |
| 524 | u64 cumul; |
| 525 | |
| 526 | child = rb_entry(node, struct callchain_node, rb_node); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 527 | cumul = callchain_cumul_hits(child); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 528 | remaining -= cumul; |
| 529 | |
| 530 | /* |
| 531 | * The depth mask manages the output of pipes that show |
| 532 | * the depth. We don't want to keep the pipes of the current |
| 533 | * level for the last child of this depth. |
| 534 | * Except if we have remaining filtered hits. They will |
| 535 | * supersede the last child |
| 536 | */ |
| 537 | next = rb_next(node); |
| 538 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) |
| 539 | new_depth_mask &= ~(1 << (depth - 1)); |
| 540 | |
| 541 | /* |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 542 | * But we keep the older depth mask for the line separator |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 543 | * to keep the level link until we reach the last child |
| 544 | */ |
| 545 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, |
| 546 | left_margin); |
| 547 | i = 0; |
| 548 | list_for_each_entry(chain, &child->val, list) { |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 549 | ret += ipchain__fprintf_graph(fp, chain, depth, |
| 550 | new_depth_mask, i++, |
| 551 | new_total, |
| 552 | cumul, |
| 553 | left_margin); |
| 554 | } |
| 555 | ret += __callchain__fprintf_graph(fp, child, new_total, |
| 556 | depth + 1, |
| 557 | new_depth_mask | (1 << depth), |
| 558 | left_margin); |
| 559 | node = next; |
Arnaldo Carvalho de Melo | 232a5c9 | 2010-05-09 20:28:10 -0300 | [diff] [blame] | 560 | if (++entries_printed == callchain_param.print_limit) |
| 561 | break; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | if (callchain_param.mode == CHAIN_GRAPH_REL && |
| 565 | remaining && remaining != new_total) { |
| 566 | |
| 567 | if (!rem_sq_bracket) |
| 568 | return ret; |
| 569 | |
| 570 | new_depth_mask &= ~(1 << (depth - 1)); |
| 571 | |
| 572 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, |
| 573 | new_depth_mask, 0, new_total, |
| 574 | remaining, left_margin); |
| 575 | } |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
| 581 | u64 total_samples, int left_margin) |
| 582 | { |
| 583 | struct callchain_list *chain; |
| 584 | bool printed = false; |
| 585 | int i = 0; |
| 586 | int ret = 0; |
Arnaldo Carvalho de Melo | 232a5c9 | 2010-05-09 20:28:10 -0300 | [diff] [blame] | 587 | u32 entries_printed = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 588 | |
| 589 | list_for_each_entry(chain, &self->val, list) { |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 590 | if (!i++ && sort__first_dimension == SORT_SYM) |
| 591 | continue; |
| 592 | |
| 593 | if (!printed) { |
| 594 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 595 | ret += fprintf(fp, "|\n"); |
| 596 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 597 | ret += fprintf(fp, "---"); |
| 598 | |
| 599 | left_margin += 3; |
| 600 | printed = true; |
| 601 | } else |
| 602 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 603 | |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 604 | if (chain->ms.sym) |
| 605 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 606 | else |
| 607 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); |
Arnaldo Carvalho de Melo | 232a5c9 | 2010-05-09 20:28:10 -0300 | [diff] [blame] | 608 | |
| 609 | if (++entries_printed == callchain_param.print_limit) |
| 610 | break; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin); |
| 614 | |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self, |
| 619 | u64 total_samples) |
| 620 | { |
| 621 | struct callchain_list *chain; |
| 622 | size_t ret = 0; |
| 623 | |
| 624 | if (!self) |
| 625 | return 0; |
| 626 | |
| 627 | ret += callchain__fprintf_flat(fp, self->parent, total_samples); |
| 628 | |
| 629 | |
| 630 | list_for_each_entry(chain, &self->val, list) { |
| 631 | if (chain->ip >= PERF_CONTEXT_MAX) |
| 632 | continue; |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 633 | if (chain->ms.sym) |
| 634 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 635 | else |
| 636 | ret += fprintf(fp, " %p\n", |
| 637 | (void *)(long)chain->ip); |
| 638 | } |
| 639 | |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, |
| 644 | u64 total_samples, int left_margin) |
| 645 | { |
| 646 | struct rb_node *rb_node; |
| 647 | struct callchain_node *chain; |
| 648 | size_t ret = 0; |
Arnaldo Carvalho de Melo | 232a5c9 | 2010-05-09 20:28:10 -0300 | [diff] [blame] | 649 | u32 entries_printed = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 650 | |
| 651 | rb_node = rb_first(&self->sorted_chain); |
| 652 | while (rb_node) { |
| 653 | double percent; |
| 654 | |
| 655 | chain = rb_entry(rb_node, struct callchain_node, rb_node); |
| 656 | percent = chain->hit * 100.0 / total_samples; |
| 657 | switch (callchain_param.mode) { |
| 658 | case CHAIN_FLAT: |
| 659 | ret += percent_color_fprintf(fp, " %6.2f%%\n", |
| 660 | percent); |
| 661 | ret += callchain__fprintf_flat(fp, chain, total_samples); |
| 662 | break; |
| 663 | case CHAIN_GRAPH_ABS: /* Falldown */ |
| 664 | case CHAIN_GRAPH_REL: |
| 665 | ret += callchain__fprintf_graph(fp, chain, total_samples, |
| 666 | left_margin); |
| 667 | case CHAIN_NONE: |
| 668 | default: |
| 669 | break; |
| 670 | } |
| 671 | ret += fprintf(fp, "\n"); |
Arnaldo Carvalho de Melo | 232a5c9 | 2010-05-09 20:28:10 -0300 | [diff] [blame] | 672 | if (++entries_printed == callchain_param.print_limit) |
| 673 | break; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 674 | rb_node = rb_next(rb_node); |
| 675 | } |
| 676 | |
| 677 | return ret; |
| 678 | } |
| 679 | |
Arnaldo Carvalho de Melo | ab81f3fd | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 680 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
| 681 | { |
| 682 | struct rb_node *next = rb_first(&hists->entries); |
| 683 | struct hist_entry *n; |
| 684 | int row = 0; |
| 685 | |
| 686 | hists__reset_col_len(hists); |
| 687 | |
| 688 | while (next && row++ < max_rows) { |
| 689 | n = rb_entry(next, struct hist_entry, rb_node); |
| 690 | hists__calc_col_len(hists, n); |
| 691 | next = rb_next(&n->rb_node); |
| 692 | } |
| 693 | } |
| 694 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 695 | int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size, |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 696 | struct hists *hists, struct hists *pair_hists, |
| 697 | bool show_displacement, long displacement, |
| 698 | bool color, u64 session_total) |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 699 | { |
| 700 | struct sort_entry *se; |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 701 | u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us; |
Arnaldo Carvalho de Melo | fec9cbd | 2011-02-17 10:37:23 -0200 | [diff] [blame] | 702 | u64 nr_events; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 703 | const char *sep = symbol_conf.field_sep; |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 704 | int ret; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 705 | |
| 706 | if (symbol_conf.exclude_other && !self->parent) |
| 707 | return 0; |
| 708 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 709 | if (pair_hists) { |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 710 | period = self->pair ? self->pair->period : 0; |
Arnaldo Carvalho de Melo | fec9cbd | 2011-02-17 10:37:23 -0200 | [diff] [blame] | 711 | nr_events = self->pair ? self->pair->nr_events : 0; |
Arnaldo Carvalho de Melo | cee75ac | 2010-05-14 13:16:55 -0300 | [diff] [blame] | 712 | total = pair_hists->stats.total_period; |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 713 | period_sys = self->pair ? self->pair->period_sys : 0; |
| 714 | period_us = self->pair ? self->pair->period_us : 0; |
| 715 | period_guest_sys = self->pair ? self->pair->period_guest_sys : 0; |
| 716 | period_guest_us = self->pair ? self->pair->period_guest_us : 0; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 717 | } else { |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 718 | period = self->period; |
Arnaldo Carvalho de Melo | fec9cbd | 2011-02-17 10:37:23 -0200 | [diff] [blame] | 719 | nr_events = self->nr_events; |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 720 | total = session_total; |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 721 | period_sys = self->period_sys; |
| 722 | period_us = self->period_us; |
| 723 | period_guest_sys = self->period_guest_sys; |
| 724 | period_guest_us = self->period_guest_us; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 725 | } |
| 726 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 727 | if (total) { |
| 728 | if (color) |
| 729 | ret = percent_color_snprintf(s, size, |
| 730 | sep ? "%.2f" : " %6.2f%%", |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 731 | (period * 100.0) / total); |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 732 | else |
| 733 | ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%", |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 734 | (period * 100.0) / total); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 735 | if (symbol_conf.show_cpu_utilization) { |
| 736 | ret += percent_color_snprintf(s + ret, size - ret, |
| 737 | sep ? "%.2f" : " %6.2f%%", |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 738 | (period_sys * 100.0) / total); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 739 | ret += percent_color_snprintf(s + ret, size - ret, |
| 740 | sep ? "%.2f" : " %6.2f%%", |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 741 | (period_us * 100.0) / total); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 742 | if (perf_guest) { |
| 743 | ret += percent_color_snprintf(s + ret, |
| 744 | size - ret, |
| 745 | sep ? "%.2f" : " %6.2f%%", |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 746 | (period_guest_sys * 100.0) / |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 747 | total); |
| 748 | ret += percent_color_snprintf(s + ret, |
| 749 | size - ret, |
| 750 | sep ? "%.2f" : " %6.2f%%", |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 751 | (period_guest_us * 100.0) / |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 752 | total); |
| 753 | } |
| 754 | } |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 755 | } else |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 756 | ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 757 | |
| 758 | if (symbol_conf.show_nr_samples) { |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 759 | if (sep) |
Arnaldo Carvalho de Melo | fec9cbd | 2011-02-17 10:37:23 -0200 | [diff] [blame] | 760 | ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 761 | else |
Arnaldo Carvalho de Melo | fec9cbd | 2011-02-17 10:37:23 -0200 | [diff] [blame] | 762 | ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 763 | } |
| 764 | |
Arnaldo Carvalho de Melo | 3f2728b | 2011-10-05 16:10:06 -0300 | [diff] [blame] | 765 | if (symbol_conf.show_total_period) { |
| 766 | if (sep) |
| 767 | ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period); |
| 768 | else |
| 769 | ret += snprintf(s + ret, size - ret, " %12" PRIu64, period); |
| 770 | } |
| 771 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 772 | if (pair_hists) { |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 773 | char bf[32]; |
| 774 | double old_percent = 0, new_percent = 0, diff; |
| 775 | |
| 776 | if (total > 0) |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 777 | old_percent = (period * 100.0) / total; |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 778 | if (session_total > 0) |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 779 | new_percent = (self->period * 100.0) / session_total; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 780 | |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 781 | diff = new_percent - old_percent; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 782 | |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 783 | if (fabs(diff) >= 0.01) |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 784 | snprintf(bf, sizeof(bf), "%+4.2F%%", diff); |
| 785 | else |
| 786 | snprintf(bf, sizeof(bf), " "); |
| 787 | |
| 788 | if (sep) |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 789 | ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 790 | else |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 791 | ret += snprintf(s + ret, size - ret, "%11.11s", bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 792 | |
| 793 | if (show_displacement) { |
| 794 | if (displacement) |
| 795 | snprintf(bf, sizeof(bf), "%+4ld", displacement); |
| 796 | else |
| 797 | snprintf(bf, sizeof(bf), " "); |
| 798 | |
| 799 | if (sep) |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 800 | ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 801 | else |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 802 | ret += snprintf(s + ret, size - ret, "%6.6s", bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 803 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 807 | if (se->elide) |
| 808 | continue; |
| 809 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 810 | ret += snprintf(s + ret, size - ret, "%s", sep ?: " "); |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 811 | ret += se->se_snprintf(self, s + ret, size - ret, |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 812 | hists__col_len(hists, se->se_width_idx)); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 813 | } |
| 814 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 815 | return ret; |
| 816 | } |
| 817 | |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 818 | int hist_entry__fprintf(struct hist_entry *he, size_t size, struct hists *hists, |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 819 | struct hists *pair_hists, bool show_displacement, |
| 820 | long displacement, FILE *fp, u64 session_total) |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 821 | { |
| 822 | char bf[512]; |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 823 | |
| 824 | if (size == 0 || size > sizeof(bf)) |
| 825 | size = sizeof(bf); |
| 826 | |
| 827 | hist_entry__snprintf(he, bf, size, hists, pair_hists, |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 828 | show_displacement, displacement, |
| 829 | true, session_total); |
| 830 | return fprintf(fp, "%s\n", bf); |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 831 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 832 | |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 833 | static size_t hist_entry__fprintf_callchain(struct hist_entry *self, |
| 834 | struct hists *hists, FILE *fp, |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 835 | u64 session_total) |
| 836 | { |
| 837 | int left_margin = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 838 | |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 839 | if (sort__first_dimension == SORT_COMM) { |
| 840 | struct sort_entry *se = list_first_entry(&hist_entry__sort_list, |
| 841 | typeof(*se), list); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 842 | left_margin = hists__col_len(hists, se->se_width_idx); |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 843 | left_margin -= thread__comm_len(self->thread); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 844 | } |
| 845 | |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 846 | return hist_entry_callchain__fprintf(fp, self, session_total, |
| 847 | left_margin); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 848 | } |
| 849 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 850 | size_t hists__fprintf(struct hists *hists, struct hists *pair, |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 851 | bool show_displacement, bool show_header, int max_rows, |
| 852 | int max_cols, FILE *fp) |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 853 | { |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 854 | struct sort_entry *se; |
| 855 | struct rb_node *nd; |
| 856 | size_t ret = 0; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 857 | unsigned long position = 1; |
| 858 | long displacement = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 859 | unsigned int width; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 860 | const char *sep = symbol_conf.field_sep; |
Arnaldo Carvalho de Melo | edb7c60 | 2010-05-17 16:22:41 -0300 | [diff] [blame] | 861 | const char *col_width = symbol_conf.col_width_list_str; |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 862 | int nr_rows = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 863 | |
| 864 | init_rem_hits(); |
| 865 | |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 866 | if (!show_header) |
| 867 | goto print_entries; |
| 868 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 869 | fprintf(fp, "# %s", pair ? "Baseline" : "Overhead"); |
| 870 | |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 871 | if (symbol_conf.show_nr_samples) { |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 872 | if (sep) |
| 873 | fprintf(fp, "%cSamples", *sep); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 874 | else |
| 875 | fputs(" Samples ", fp); |
| 876 | } |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 877 | |
Arnaldo Carvalho de Melo | 3f2728b | 2011-10-05 16:10:06 -0300 | [diff] [blame] | 878 | if (symbol_conf.show_total_period) { |
| 879 | if (sep) |
| 880 | ret += fprintf(fp, "%cPeriod", *sep); |
| 881 | else |
| 882 | ret += fprintf(fp, " Period "); |
| 883 | } |
| 884 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 885 | if (symbol_conf.show_cpu_utilization) { |
| 886 | if (sep) { |
| 887 | ret += fprintf(fp, "%csys", *sep); |
| 888 | ret += fprintf(fp, "%cus", *sep); |
| 889 | if (perf_guest) { |
| 890 | ret += fprintf(fp, "%cguest sys", *sep); |
| 891 | ret += fprintf(fp, "%cguest us", *sep); |
| 892 | } |
| 893 | } else { |
| 894 | ret += fprintf(fp, " sys "); |
| 895 | ret += fprintf(fp, " us "); |
| 896 | if (perf_guest) { |
| 897 | ret += fprintf(fp, " guest sys "); |
| 898 | ret += fprintf(fp, " guest us "); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 903 | if (pair) { |
| 904 | if (sep) |
| 905 | ret += fprintf(fp, "%cDelta", *sep); |
| 906 | else |
| 907 | ret += fprintf(fp, " Delta "); |
| 908 | |
| 909 | if (show_displacement) { |
| 910 | if (sep) |
| 911 | ret += fprintf(fp, "%cDisplacement", *sep); |
| 912 | else |
| 913 | ret += fprintf(fp, " Displ"); |
| 914 | } |
| 915 | } |
| 916 | |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 917 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 918 | if (se->elide) |
| 919 | continue; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 920 | if (sep) { |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 921 | fprintf(fp, "%c%s", *sep, se->se_header); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 922 | continue; |
| 923 | } |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 924 | width = strlen(se->se_header); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 925 | if (symbol_conf.col_width_list_str) { |
| 926 | if (col_width) { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 927 | hists__set_col_len(hists, se->se_width_idx, |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 928 | atoi(col_width)); |
| 929 | col_width = strchr(col_width, ','); |
| 930 | if (col_width) |
| 931 | ++col_width; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 932 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 933 | } |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 934 | if (!hists__new_col_len(hists, se->se_width_idx, width)) |
| 935 | width = hists__col_len(hists, se->se_width_idx); |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 936 | fprintf(fp, " %*s", width, se->se_header); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 937 | } |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 938 | |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 939 | fprintf(fp, "\n"); |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 940 | if (max_rows && ++nr_rows >= max_rows) |
| 941 | goto out; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 942 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 943 | if (sep) |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 944 | goto print_entries; |
| 945 | |
| 946 | fprintf(fp, "# ........"); |
| 947 | if (symbol_conf.show_nr_samples) |
| 948 | fprintf(fp, " .........."); |
Arnaldo Carvalho de Melo | 3f2728b | 2011-10-05 16:10:06 -0300 | [diff] [blame] | 949 | if (symbol_conf.show_total_period) |
| 950 | fprintf(fp, " ............"); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 951 | if (pair) { |
| 952 | fprintf(fp, " .........."); |
| 953 | if (show_displacement) |
| 954 | fprintf(fp, " ....."); |
| 955 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 956 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 957 | unsigned int i; |
| 958 | |
| 959 | if (se->elide) |
| 960 | continue; |
| 961 | |
| 962 | fprintf(fp, " "); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 963 | width = hists__col_len(hists, se->se_width_idx); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 964 | if (width == 0) |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 965 | width = strlen(se->se_header); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 966 | for (i = 0; i < width; i++) |
| 967 | fprintf(fp, "."); |
| 968 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 969 | |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 970 | fprintf(fp, "\n"); |
| 971 | if (max_rows && ++nr_rows >= max_rows) |
| 972 | goto out; |
| 973 | |
| 974 | fprintf(fp, "#\n"); |
| 975 | if (max_rows && ++nr_rows >= max_rows) |
| 976 | goto out; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 977 | |
| 978 | print_entries: |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 979 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 980 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 981 | |
Frederic Weisbecker | e84d212 | 2011-06-29 22:23:03 +0200 | [diff] [blame] | 982 | if (h->filtered) |
| 983 | continue; |
| 984 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 985 | if (show_displacement) { |
| 986 | if (h->pair != NULL) |
| 987 | displacement = ((long)h->pair->position - |
| 988 | (long)position); |
| 989 | else |
| 990 | displacement = 0; |
| 991 | ++position; |
| 992 | } |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 993 | ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement, |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 994 | displacement, fp, hists->stats.total_period); |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 995 | |
| 996 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 997 | ret += hist_entry__fprintf_callchain(h, hists, fp, |
| 998 | hists->stats.total_period); |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 999 | if (max_rows && ++nr_rows >= max_rows) |
| 1000 | goto out; |
| 1001 | |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 1002 | if (h->ms.map == NULL && verbose > 1) { |
Arnaldo Carvalho de Melo | 65f2ed2 | 2010-03-09 15:58:17 -0300 | [diff] [blame] | 1003 | __map_groups__fprintf_maps(&h->thread->mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 1004 | MAP__FUNCTION, verbose, fp); |
Arnaldo Carvalho de Melo | 65f2ed2 | 2010-03-09 15:58:17 -0300 | [diff] [blame] | 1005 | fprintf(fp, "%.10s end\n", graph_dotted_line); |
| 1006 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 1007 | } |
Arnaldo Carvalho de Melo | ef9dfe6 | 2011-09-26 12:46:11 -0300 | [diff] [blame] | 1008 | out: |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 1009 | free(rem_sq_bracket); |
| 1010 | |
| 1011 | return ret; |
| 1012 | } |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1013 | |
Arnaldo Carvalho de Melo | 06daaab | 2010-07-21 17:58:25 -0300 | [diff] [blame] | 1014 | /* |
| 1015 | * See hists__fprintf to match the column widths |
| 1016 | */ |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1017 | unsigned int hists__sort_list_width(struct hists *hists) |
Arnaldo Carvalho de Melo | 06daaab | 2010-07-21 17:58:25 -0300 | [diff] [blame] | 1018 | { |
| 1019 | struct sort_entry *se; |
| 1020 | int ret = 9; /* total % */ |
| 1021 | |
| 1022 | if (symbol_conf.show_cpu_utilization) { |
| 1023 | ret += 7; /* count_sys % */ |
| 1024 | ret += 6; /* count_us % */ |
| 1025 | if (perf_guest) { |
| 1026 | ret += 13; /* count_guest_sys % */ |
| 1027 | ret += 12; /* count_guest_us % */ |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if (symbol_conf.show_nr_samples) |
| 1032 | ret += 11; |
| 1033 | |
Arnaldo Carvalho de Melo | 3f2728b | 2011-10-05 16:10:06 -0300 | [diff] [blame] | 1034 | if (symbol_conf.show_total_period) |
| 1035 | ret += 13; |
| 1036 | |
Arnaldo Carvalho de Melo | 06daaab | 2010-07-21 17:58:25 -0300 | [diff] [blame] | 1037 | list_for_each_entry(se, &hist_entry__sort_list, list) |
| 1038 | if (!se->elide) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1039 | ret += 2 + hists__col_len(hists, se->se_width_idx); |
Arnaldo Carvalho de Melo | 06daaab | 2010-07-21 17:58:25 -0300 | [diff] [blame] | 1040 | |
Arnaldo Carvalho de Melo | 903cce6 | 2010-08-05 19:15:48 -0300 | [diff] [blame] | 1041 | if (verbose) /* Addr + origin */ |
| 1042 | ret += 3 + BITS_PER_LONG / 4; |
| 1043 | |
Arnaldo Carvalho de Melo | 06daaab | 2010-07-21 17:58:25 -0300 | [diff] [blame] | 1044 | return ret; |
| 1045 | } |
| 1046 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1047 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1048 | enum hist_filter filter) |
| 1049 | { |
| 1050 | h->filtered &= ~(1 << filter); |
| 1051 | if (h->filtered) |
| 1052 | return; |
| 1053 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1054 | ++hists->nr_entries; |
Arnaldo Carvalho de Melo | 0f0cbf7 | 2010-07-26 17:13:40 -0300 | [diff] [blame] | 1055 | if (h->ms.unfolded) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1056 | hists->nr_entries += h->nr_rows; |
Arnaldo Carvalho de Melo | 0f0cbf7 | 2010-07-26 17:13:40 -0300 | [diff] [blame] | 1057 | h->row_offset = 0; |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1058 | hists->stats.total_period += h->period; |
| 1059 | hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events; |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1060 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1061 | hists__calc_col_len(hists, h); |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1062 | } |
| 1063 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1064 | void hists__filter_by_dso(struct hists *hists, const struct dso *dso) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1065 | { |
| 1066 | struct rb_node *nd; |
| 1067 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1068 | hists->nr_entries = hists->stats.total_period = 0; |
| 1069 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; |
| 1070 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1071 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1072 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1073 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1074 | |
| 1075 | if (symbol_conf.exclude_other && !h->parent) |
| 1076 | continue; |
| 1077 | |
| 1078 | if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) { |
| 1079 | h->filtered |= (1 << HIST_FILTER__DSO); |
| 1080 | continue; |
| 1081 | } |
| 1082 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1083 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1087 | void hists__filter_by_thread(struct hists *hists, const struct thread *thread) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1088 | { |
| 1089 | struct rb_node *nd; |
| 1090 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1091 | hists->nr_entries = hists->stats.total_period = 0; |
| 1092 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; |
| 1093 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1094 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1095 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1096 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1097 | |
| 1098 | if (thread != NULL && h->thread != thread) { |
| 1099 | h->filtered |= (1 << HIST_FILTER__THREAD); |
| 1100 | continue; |
| 1101 | } |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1102 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1103 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1104 | } |
| 1105 | } |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 1106 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 1107 | int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip) |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 1108 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 1109 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip); |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 1110 | } |
| 1111 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1112 | int hist_entry__annotate(struct hist_entry *he, size_t privsize) |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 1113 | { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 1114 | return symbol__annotate(he->ms.sym, he->ms.map, privsize); |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 1115 | } |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1116 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1117 | void hists__inc_nr_events(struct hists *hists, u32 type) |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1118 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1119 | ++hists->stats.nr_events[0]; |
| 1120 | ++hists->stats.nr_events[type]; |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1121 | } |
| 1122 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1123 | size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp) |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1124 | { |
| 1125 | int i; |
| 1126 | size_t ret = 0; |
| 1127 | |
| 1128 | for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { |
Arnaldo Carvalho de Melo | e248de3 | 2011-03-05 21:40:06 -0300 | [diff] [blame] | 1129 | const char *name; |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 1130 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1131 | if (hists->stats.nr_events[i] == 0) |
Arnaldo Carvalho de Melo | e248de3 | 2011-03-05 21:40:06 -0300 | [diff] [blame] | 1132 | continue; |
| 1133 | |
| 1134 | name = perf_event__name(i); |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 1135 | if (!strcmp(name, "UNKNOWN")) |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1136 | continue; |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 1137 | |
| 1138 | ret += fprintf(fp, "%16s events: %10d\n", name, |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1139 | hists->stats.nr_events[i]); |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | return ret; |
| 1143 | } |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1144 | |
| 1145 | void hists__init(struct hists *hists) |
| 1146 | { |
| 1147 | memset(hists, 0, sizeof(*hists)); |
| 1148 | hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT; |
| 1149 | hists->entries_in = &hists->entries_in_array[0]; |
| 1150 | hists->entries_collapsed = RB_ROOT; |
| 1151 | hists->entries = RB_ROOT; |
| 1152 | pthread_mutex_init(&hists->lock, NULL); |
| 1153 | } |