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