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