Commit | Line | Data |
---|---|---|
78f7defe | 1 | #include "annotate.h" |
8a0ecfb8 | 2 | #include "util.h" |
598357eb | 3 | #include "build-id.h" |
3d1d07ec | 4 | #include "hist.h" |
4e4f06e4 ACM |
5 | #include "session.h" |
6 | #include "sort.h" | |
9b33827d | 7 | #include <math.h> |
3d1d07ec | 8 | |
90cf1fb5 ACM |
9 | static bool hists__filter_entry_by_dso(struct hists *hists, |
10 | struct hist_entry *he); | |
11 | static bool hists__filter_entry_by_thread(struct hists *hists, | |
12 | struct hist_entry *he); | |
e94d53eb NK |
13 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
14 | struct hist_entry *he); | |
90cf1fb5 | 15 | |
7a007ca9 ACM |
16 | enum hist_filter { |
17 | HIST_FILTER__DSO, | |
18 | HIST_FILTER__THREAD, | |
19 | HIST_FILTER__PARENT, | |
e94d53eb | 20 | HIST_FILTER__SYMBOL, |
7a007ca9 ACM |
21 | }; |
22 | ||
3d1d07ec JK |
23 | struct callchain_param callchain_param = { |
24 | .mode = CHAIN_GRAPH_REL, | |
d797fdc5 SL |
25 | .min_percent = 0.5, |
26 | .order = ORDER_CALLEE | |
3d1d07ec JK |
27 | }; |
28 | ||
42b28ac0 | 29 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
8a6c5b26 | 30 | { |
42b28ac0 | 31 | return hists->col_len[col]; |
8a6c5b26 ACM |
32 | } |
33 | ||
42b28ac0 | 34 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 35 | { |
42b28ac0 | 36 | hists->col_len[col] = len; |
8a6c5b26 ACM |
37 | } |
38 | ||
42b28ac0 | 39 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
8a6c5b26 | 40 | { |
42b28ac0 ACM |
41 | if (len > hists__col_len(hists, col)) { |
42 | hists__set_col_len(hists, col, len); | |
8a6c5b26 ACM |
43 | return true; |
44 | } | |
45 | return false; | |
46 | } | |
47 | ||
7ccf4f90 | 48 | void hists__reset_col_len(struct hists *hists) |
8a6c5b26 ACM |
49 | { |
50 | enum hist_column col; | |
51 | ||
52 | for (col = 0; col < HISTC_NR_COLS; ++col) | |
42b28ac0 | 53 | hists__set_col_len(hists, col, 0); |
8a6c5b26 ACM |
54 | } |
55 | ||
b5387528 RAV |
56 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
57 | { | |
58 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; | |
59 | ||
60 | if (hists__col_len(hists, dso) < unresolved_col_width && | |
61 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && | |
62 | !symbol_conf.dso_list) | |
63 | hists__set_col_len(hists, dso, unresolved_col_width); | |
64 | } | |
65 | ||
7ccf4f90 | 66 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
8a6c5b26 | 67 | { |
b5387528 | 68 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
8a6c5b26 ACM |
69 | u16 len; |
70 | ||
71 | if (h->ms.sym) | |
b5387528 RAV |
72 | hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4); |
73 | else | |
74 | hists__set_unres_dso_col_len(hists, HISTC_DSO); | |
8a6c5b26 ACM |
75 | |
76 | len = thread__comm_len(h->thread); | |
42b28ac0 ACM |
77 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
78 | hists__set_col_len(hists, HISTC_THREAD, len + 6); | |
8a6c5b26 ACM |
79 | |
80 | if (h->ms.map) { | |
81 | len = dso__name_len(h->ms.map->dso); | |
42b28ac0 | 82 | hists__new_col_len(hists, HISTC_DSO, len); |
8a6c5b26 | 83 | } |
b5387528 | 84 | |
cb993744 NK |
85 | if (h->parent) |
86 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); | |
87 | ||
b5387528 RAV |
88 | if (h->branch_info) { |
89 | int symlen; | |
90 | /* | |
91 | * +4 accounts for '[x] ' priv level info | |
92 | * +2 account of 0x prefix on raw addresses | |
93 | */ | |
94 | if (h->branch_info->from.sym) { | |
95 | symlen = (int)h->branch_info->from.sym->namelen + 4; | |
96 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); | |
97 | ||
98 | symlen = dso__name_len(h->branch_info->from.map->dso); | |
99 | hists__new_col_len(hists, HISTC_DSO_FROM, symlen); | |
100 | } else { | |
101 | symlen = unresolved_col_width + 4 + 2; | |
102 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); | |
103 | hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM); | |
104 | } | |
105 | ||
106 | if (h->branch_info->to.sym) { | |
107 | symlen = (int)h->branch_info->to.sym->namelen + 4; | |
108 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
109 | ||
110 | symlen = dso__name_len(h->branch_info->to.map->dso); | |
111 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); | |
112 | } else { | |
113 | symlen = unresolved_col_width + 4 + 2; | |
114 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
115 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); | |
116 | } | |
117 | } | |
8a6c5b26 ACM |
118 | } |
119 | ||
7ccf4f90 NK |
120 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
121 | { | |
122 | struct rb_node *next = rb_first(&hists->entries); | |
123 | struct hist_entry *n; | |
124 | int row = 0; | |
125 | ||
126 | hists__reset_col_len(hists); | |
127 | ||
128 | while (next && row++ < max_rows) { | |
129 | n = rb_entry(next, struct hist_entry, rb_node); | |
130 | if (!n->filtered) | |
131 | hists__calc_col_len(hists, n); | |
132 | next = rb_next(&n->rb_node); | |
133 | } | |
134 | } | |
135 | ||
12c14278 | 136 | static void hist_entry__add_cpumode_period(struct hist_entry *he, |
c82ee828 | 137 | unsigned int cpumode, u64 period) |
a1645ce1 | 138 | { |
28e2a106 | 139 | switch (cpumode) { |
a1645ce1 | 140 | case PERF_RECORD_MISC_KERNEL: |
b24c28f7 | 141 | he->stat.period_sys += period; |
a1645ce1 ZY |
142 | break; |
143 | case PERF_RECORD_MISC_USER: | |
b24c28f7 | 144 | he->stat.period_us += period; |
a1645ce1 ZY |
145 | break; |
146 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
b24c28f7 | 147 | he->stat.period_guest_sys += period; |
a1645ce1 ZY |
148 | break; |
149 | case PERF_RECORD_MISC_GUEST_USER: | |
b24c28f7 | 150 | he->stat.period_guest_us += period; |
a1645ce1 ZY |
151 | break; |
152 | default: | |
153 | break; | |
154 | } | |
155 | } | |
156 | ||
139c0815 NK |
157 | static void he_stat__add_period(struct he_stat *he_stat, u64 period) |
158 | { | |
159 | he_stat->period += period; | |
160 | he_stat->nr_events += 1; | |
161 | } | |
162 | ||
163 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) | |
164 | { | |
165 | dest->period += src->period; | |
166 | dest->period_sys += src->period_sys; | |
167 | dest->period_us += src->period_us; | |
168 | dest->period_guest_sys += src->period_guest_sys; | |
169 | dest->period_guest_us += src->period_guest_us; | |
170 | dest->nr_events += src->nr_events; | |
171 | } | |
172 | ||
ab81f3fd ACM |
173 | static void hist_entry__decay(struct hist_entry *he) |
174 | { | |
b24c28f7 NK |
175 | he->stat.period = (he->stat.period * 7) / 8; |
176 | he->stat.nr_events = (he->stat.nr_events * 7) / 8; | |
ab81f3fd ACM |
177 | } |
178 | ||
179 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) | |
180 | { | |
b24c28f7 | 181 | u64 prev_period = he->stat.period; |
c64550cf ACM |
182 | |
183 | if (prev_period == 0) | |
df71d95f | 184 | return true; |
c64550cf | 185 | |
ab81f3fd | 186 | hist_entry__decay(he); |
c64550cf ACM |
187 | |
188 | if (!he->filtered) | |
b24c28f7 | 189 | hists->stats.total_period -= prev_period - he->stat.period; |
c64550cf | 190 | |
b24c28f7 | 191 | return he->stat.period == 0; |
ab81f3fd ACM |
192 | } |
193 | ||
b079d4e9 ACM |
194 | static void __hists__decay_entries(struct hists *hists, bool zap_user, |
195 | bool zap_kernel, bool threaded) | |
ab81f3fd ACM |
196 | { |
197 | struct rb_node *next = rb_first(&hists->entries); | |
198 | struct hist_entry *n; | |
199 | ||
200 | while (next) { | |
201 | n = rb_entry(next, struct hist_entry, rb_node); | |
202 | next = rb_next(&n->rb_node); | |
df71d95f ACM |
203 | /* |
204 | * We may be annotating this, for instance, so keep it here in | |
205 | * case some it gets new samples, we'll eventually free it when | |
206 | * the user stops browsing and it agains gets fully decayed. | |
207 | */ | |
b079d4e9 ACM |
208 | if (((zap_user && n->level == '.') || |
209 | (zap_kernel && n->level != '.') || | |
210 | hists__decay_entry(hists, n)) && | |
211 | !n->used) { | |
ab81f3fd ACM |
212 | rb_erase(&n->rb_node, &hists->entries); |
213 | ||
e345fa18 | 214 | if (sort__need_collapse || threaded) |
ab81f3fd ACM |
215 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
216 | ||
217 | hist_entry__free(n); | |
218 | --hists->nr_entries; | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
b079d4e9 | 223 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
e345fa18 | 224 | { |
b079d4e9 | 225 | return __hists__decay_entries(hists, zap_user, zap_kernel, false); |
e345fa18 ACM |
226 | } |
227 | ||
b079d4e9 ACM |
228 | void hists__decay_entries_threaded(struct hists *hists, |
229 | bool zap_user, bool zap_kernel) | |
e345fa18 | 230 | { |
b079d4e9 | 231 | return __hists__decay_entries(hists, zap_user, zap_kernel, true); |
e345fa18 ACM |
232 | } |
233 | ||
3d1d07ec | 234 | /* |
c82ee828 | 235 | * histogram, sorted on item, collects periods |
3d1d07ec JK |
236 | */ |
237 | ||
28e2a106 ACM |
238 | static struct hist_entry *hist_entry__new(struct hist_entry *template) |
239 | { | |
d2009c51 | 240 | size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0; |
12c14278 | 241 | struct hist_entry *he = malloc(sizeof(*he) + callchain_size); |
28e2a106 | 242 | |
12c14278 ACM |
243 | if (he != NULL) { |
244 | *he = *template; | |
c4b35351 | 245 | |
12c14278 ACM |
246 | if (he->ms.map) |
247 | he->ms.map->referenced = true; | |
3cf0cb1f SE |
248 | |
249 | if (he->branch_info) { | |
250 | if (he->branch_info->from.map) | |
251 | he->branch_info->from.map->referenced = true; | |
252 | if (he->branch_info->to.map) | |
253 | he->branch_info->to.map->referenced = true; | |
254 | } | |
255 | ||
28e2a106 | 256 | if (symbol_conf.use_callchain) |
12c14278 | 257 | callchain_init(he->callchain); |
b821c732 ACM |
258 | |
259 | INIT_LIST_HEAD(&he->pairs.node); | |
28e2a106 ACM |
260 | } |
261 | ||
12c14278 | 262 | return he; |
28e2a106 ACM |
263 | } |
264 | ||
66f97ed3 | 265 | void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h) |
fefb0b94 | 266 | { |
8a6c5b26 | 267 | if (!h->filtered) { |
42b28ac0 ACM |
268 | hists__calc_col_len(hists, h); |
269 | ++hists->nr_entries; | |
b24c28f7 | 270 | hists->stats.total_period += h->stat.period; |
8a6c5b26 | 271 | } |
fefb0b94 ACM |
272 | } |
273 | ||
7a007ca9 ACM |
274 | static u8 symbol__parent_filter(const struct symbol *parent) |
275 | { | |
276 | if (symbol_conf.exclude_other && parent == NULL) | |
277 | return 1 << HIST_FILTER__PARENT; | |
278 | return 0; | |
279 | } | |
280 | ||
b5387528 RAV |
281 | static struct hist_entry *add_hist_entry(struct hists *hists, |
282 | struct hist_entry *entry, | |
1c02c4d2 | 283 | struct addr_location *al, |
b5387528 | 284 | u64 period) |
9735abf1 | 285 | { |
1980c2eb | 286 | struct rb_node **p; |
9735abf1 ACM |
287 | struct rb_node *parent = NULL; |
288 | struct hist_entry *he; | |
9735abf1 ACM |
289 | int cmp; |
290 | ||
1980c2eb ACM |
291 | pthread_mutex_lock(&hists->lock); |
292 | ||
293 | p = &hists->entries_in->rb_node; | |
294 | ||
9735abf1 ACM |
295 | while (*p != NULL) { |
296 | parent = *p; | |
1980c2eb | 297 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
9735abf1 | 298 | |
9afcf930 NK |
299 | /* |
300 | * Make sure that it receives arguments in a same order as | |
301 | * hist_entry__collapse() so that we can use an appropriate | |
302 | * function when searching an entry regardless which sort | |
303 | * keys were used. | |
304 | */ | |
305 | cmp = hist_entry__cmp(he, entry); | |
9735abf1 ACM |
306 | |
307 | if (!cmp) { | |
139c0815 | 308 | he_stat__add_period(&he->stat, period); |
63fa471d DM |
309 | |
310 | /* If the map of an existing hist_entry has | |
311 | * become out-of-date due to an exec() or | |
312 | * similar, update it. Otherwise we will | |
313 | * mis-adjust symbol addresses when computing | |
314 | * the history counter to increment. | |
315 | */ | |
316 | if (he->ms.map != entry->ms.map) { | |
317 | he->ms.map = entry->ms.map; | |
318 | if (he->ms.map) | |
319 | he->ms.map->referenced = true; | |
320 | } | |
28e2a106 | 321 | goto out; |
9735abf1 ACM |
322 | } |
323 | ||
324 | if (cmp < 0) | |
325 | p = &(*p)->rb_left; | |
326 | else | |
327 | p = &(*p)->rb_right; | |
328 | } | |
329 | ||
b5387528 | 330 | he = hist_entry__new(entry); |
9735abf1 | 331 | if (!he) |
1980c2eb ACM |
332 | goto out_unlock; |
333 | ||
334 | rb_link_node(&he->rb_node_in, parent, p); | |
335 | rb_insert_color(&he->rb_node_in, hists->entries_in); | |
28e2a106 | 336 | out: |
c82ee828 | 337 | hist_entry__add_cpumode_period(he, al->cpumode, period); |
1980c2eb ACM |
338 | out_unlock: |
339 | pthread_mutex_unlock(&hists->lock); | |
9735abf1 ACM |
340 | return he; |
341 | } | |
342 | ||
b5387528 RAV |
343 | struct hist_entry *__hists__add_branch_entry(struct hists *self, |
344 | struct addr_location *al, | |
345 | struct symbol *sym_parent, | |
346 | struct branch_info *bi, | |
347 | u64 period) | |
348 | { | |
349 | struct hist_entry entry = { | |
350 | .thread = al->thread, | |
351 | .ms = { | |
352 | .map = bi->to.map, | |
353 | .sym = bi->to.sym, | |
354 | }, | |
355 | .cpu = al->cpu, | |
356 | .ip = bi->to.addr, | |
357 | .level = al->level, | |
b24c28f7 NK |
358 | .stat = { |
359 | .period = period, | |
c4b35351 | 360 | .nr_events = 1, |
b24c28f7 | 361 | }, |
b5387528 RAV |
362 | .parent = sym_parent, |
363 | .filtered = symbol__parent_filter(sym_parent), | |
364 | .branch_info = bi, | |
ae359f19 | 365 | .hists = self, |
b5387528 RAV |
366 | }; |
367 | ||
368 | return add_hist_entry(self, &entry, al, period); | |
369 | } | |
370 | ||
371 | struct hist_entry *__hists__add_entry(struct hists *self, | |
372 | struct addr_location *al, | |
373 | struct symbol *sym_parent, u64 period) | |
374 | { | |
375 | struct hist_entry entry = { | |
376 | .thread = al->thread, | |
377 | .ms = { | |
378 | .map = al->map, | |
379 | .sym = al->sym, | |
380 | }, | |
381 | .cpu = al->cpu, | |
382 | .ip = al->addr, | |
383 | .level = al->level, | |
b24c28f7 NK |
384 | .stat = { |
385 | .period = period, | |
c4b35351 | 386 | .nr_events = 1, |
b24c28f7 | 387 | }, |
b5387528 RAV |
388 | .parent = sym_parent, |
389 | .filtered = symbol__parent_filter(sym_parent), | |
ae359f19 | 390 | .hists = self, |
b5387528 RAV |
391 | }; |
392 | ||
393 | return add_hist_entry(self, &entry, al, period); | |
394 | } | |
395 | ||
3d1d07ec JK |
396 | int64_t |
397 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
398 | { | |
399 | struct sort_entry *se; | |
400 | int64_t cmp = 0; | |
401 | ||
402 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
fcd14984 | 403 | cmp = se->se_cmp(left, right); |
3d1d07ec JK |
404 | if (cmp) |
405 | break; | |
406 | } | |
407 | ||
408 | return cmp; | |
409 | } | |
410 | ||
411 | int64_t | |
412 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
413 | { | |
414 | struct sort_entry *se; | |
415 | int64_t cmp = 0; | |
416 | ||
417 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
418 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | |
419 | ||
fcd14984 | 420 | f = se->se_collapse ?: se->se_cmp; |
3d1d07ec JK |
421 | |
422 | cmp = f(left, right); | |
423 | if (cmp) | |
424 | break; | |
425 | } | |
426 | ||
427 | return cmp; | |
428 | } | |
429 | ||
430 | void hist_entry__free(struct hist_entry *he) | |
431 | { | |
580e338d | 432 | free(he->branch_info); |
3d1d07ec JK |
433 | free(he); |
434 | } | |
435 | ||
436 | /* | |
437 | * collapse the histogram | |
438 | */ | |
439 | ||
1d037ca1 | 440 | static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused, |
1b3a0e95 FW |
441 | struct rb_root *root, |
442 | struct hist_entry *he) | |
3d1d07ec | 443 | { |
b9bf0892 | 444 | struct rb_node **p = &root->rb_node; |
3d1d07ec JK |
445 | struct rb_node *parent = NULL; |
446 | struct hist_entry *iter; | |
447 | int64_t cmp; | |
448 | ||
449 | while (*p != NULL) { | |
450 | parent = *p; | |
1980c2eb | 451 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
3d1d07ec JK |
452 | |
453 | cmp = hist_entry__collapse(iter, he); | |
454 | ||
455 | if (!cmp) { | |
139c0815 | 456 | he_stat__add_stat(&iter->stat, &he->stat); |
9ec60972 | 457 | |
1b3a0e95 | 458 | if (symbol_conf.use_callchain) { |
47260645 NK |
459 | callchain_cursor_reset(&callchain_cursor); |
460 | callchain_merge(&callchain_cursor, | |
461 | iter->callchain, | |
1b3a0e95 FW |
462 | he->callchain); |
463 | } | |
3d1d07ec | 464 | hist_entry__free(he); |
fefb0b94 | 465 | return false; |
3d1d07ec JK |
466 | } |
467 | ||
468 | if (cmp < 0) | |
469 | p = &(*p)->rb_left; | |
470 | else | |
471 | p = &(*p)->rb_right; | |
472 | } | |
473 | ||
1980c2eb ACM |
474 | rb_link_node(&he->rb_node_in, parent, p); |
475 | rb_insert_color(&he->rb_node_in, root); | |
fefb0b94 | 476 | return true; |
3d1d07ec JK |
477 | } |
478 | ||
1980c2eb | 479 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
3d1d07ec | 480 | { |
1980c2eb ACM |
481 | struct rb_root *root; |
482 | ||
483 | pthread_mutex_lock(&hists->lock); | |
484 | ||
485 | root = hists->entries_in; | |
486 | if (++hists->entries_in > &hists->entries_in_array[1]) | |
487 | hists->entries_in = &hists->entries_in_array[0]; | |
488 | ||
489 | pthread_mutex_unlock(&hists->lock); | |
490 | ||
491 | return root; | |
492 | } | |
493 | ||
90cf1fb5 ACM |
494 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
495 | { | |
496 | hists__filter_entry_by_dso(hists, he); | |
497 | hists__filter_entry_by_thread(hists, he); | |
e94d53eb | 498 | hists__filter_entry_by_symbol(hists, he); |
90cf1fb5 ACM |
499 | } |
500 | ||
1980c2eb ACM |
501 | static void __hists__collapse_resort(struct hists *hists, bool threaded) |
502 | { | |
503 | struct rb_root *root; | |
3d1d07ec JK |
504 | struct rb_node *next; |
505 | struct hist_entry *n; | |
506 | ||
1980c2eb | 507 | if (!sort__need_collapse && !threaded) |
3d1d07ec JK |
508 | return; |
509 | ||
1980c2eb ACM |
510 | root = hists__get_rotate_entries_in(hists); |
511 | next = rb_first(root); | |
b9bf0892 | 512 | |
3d1d07ec | 513 | while (next) { |
1980c2eb ACM |
514 | n = rb_entry(next, struct hist_entry, rb_node_in); |
515 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 516 | |
1980c2eb | 517 | rb_erase(&n->rb_node_in, root); |
90cf1fb5 ACM |
518 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) { |
519 | /* | |
520 | * If it wasn't combined with one of the entries already | |
521 | * collapsed, we need to apply the filters that may have | |
522 | * been set by, say, the hist_browser. | |
523 | */ | |
524 | hists__apply_filters(hists, n); | |
90cf1fb5 | 525 | } |
3d1d07ec | 526 | } |
1980c2eb | 527 | } |
b9bf0892 | 528 | |
1980c2eb ACM |
529 | void hists__collapse_resort(struct hists *hists) |
530 | { | |
531 | return __hists__collapse_resort(hists, false); | |
532 | } | |
533 | ||
534 | void hists__collapse_resort_threaded(struct hists *hists) | |
535 | { | |
536 | return __hists__collapse_resort(hists, true); | |
3d1d07ec JK |
537 | } |
538 | ||
539 | /* | |
c82ee828 | 540 | * reverse the map, sort on period. |
3d1d07ec JK |
541 | */ |
542 | ||
1c02c4d2 ACM |
543 | static void __hists__insert_output_entry(struct rb_root *entries, |
544 | struct hist_entry *he, | |
545 | u64 min_callchain_hits) | |
3d1d07ec | 546 | { |
1c02c4d2 | 547 | struct rb_node **p = &entries->rb_node; |
3d1d07ec JK |
548 | struct rb_node *parent = NULL; |
549 | struct hist_entry *iter; | |
550 | ||
d599db3f | 551 | if (symbol_conf.use_callchain) |
b9fb9304 | 552 | callchain_param.sort(&he->sorted_chain, he->callchain, |
3d1d07ec JK |
553 | min_callchain_hits, &callchain_param); |
554 | ||
555 | while (*p != NULL) { | |
556 | parent = *p; | |
557 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
558 | ||
b24c28f7 | 559 | if (he->stat.period > iter->stat.period) |
3d1d07ec JK |
560 | p = &(*p)->rb_left; |
561 | else | |
562 | p = &(*p)->rb_right; | |
563 | } | |
564 | ||
565 | rb_link_node(&he->rb_node, parent, p); | |
1c02c4d2 | 566 | rb_insert_color(&he->rb_node, entries); |
3d1d07ec JK |
567 | } |
568 | ||
1980c2eb | 569 | static void __hists__output_resort(struct hists *hists, bool threaded) |
3d1d07ec | 570 | { |
1980c2eb | 571 | struct rb_root *root; |
3d1d07ec JK |
572 | struct rb_node *next; |
573 | struct hist_entry *n; | |
3d1d07ec JK |
574 | u64 min_callchain_hits; |
575 | ||
42b28ac0 | 576 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
3d1d07ec | 577 | |
1980c2eb ACM |
578 | if (sort__need_collapse || threaded) |
579 | root = &hists->entries_collapsed; | |
580 | else | |
581 | root = hists->entries_in; | |
582 | ||
583 | next = rb_first(root); | |
584 | hists->entries = RB_ROOT; | |
3d1d07ec | 585 | |
42b28ac0 | 586 | hists->nr_entries = 0; |
7928631a | 587 | hists->stats.total_period = 0; |
42b28ac0 | 588 | hists__reset_col_len(hists); |
fefb0b94 | 589 | |
3d1d07ec | 590 | while (next) { |
1980c2eb ACM |
591 | n = rb_entry(next, struct hist_entry, rb_node_in); |
592 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 593 | |
1980c2eb | 594 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
42b28ac0 | 595 | hists__inc_nr_entries(hists, n); |
3d1d07ec | 596 | } |
1980c2eb | 597 | } |
b9bf0892 | 598 | |
1980c2eb ACM |
599 | void hists__output_resort(struct hists *hists) |
600 | { | |
601 | return __hists__output_resort(hists, false); | |
602 | } | |
603 | ||
604 | void hists__output_resort_threaded(struct hists *hists) | |
605 | { | |
606 | return __hists__output_resort(hists, true); | |
3d1d07ec | 607 | } |
4ecf84d0 | 608 | |
42b28ac0 | 609 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
cc5edb0e ACM |
610 | enum hist_filter filter) |
611 | { | |
612 | h->filtered &= ~(1 << filter); | |
613 | if (h->filtered) | |
614 | return; | |
615 | ||
42b28ac0 | 616 | ++hists->nr_entries; |
0f0cbf7a | 617 | if (h->ms.unfolded) |
42b28ac0 | 618 | hists->nr_entries += h->nr_rows; |
0f0cbf7a | 619 | h->row_offset = 0; |
b24c28f7 NK |
620 | hists->stats.total_period += h->stat.period; |
621 | hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events; | |
cc5edb0e | 622 | |
42b28ac0 | 623 | hists__calc_col_len(hists, h); |
cc5edb0e ACM |
624 | } |
625 | ||
90cf1fb5 ACM |
626 | |
627 | static bool hists__filter_entry_by_dso(struct hists *hists, | |
628 | struct hist_entry *he) | |
629 | { | |
630 | if (hists->dso_filter != NULL && | |
631 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { | |
632 | he->filtered |= (1 << HIST_FILTER__DSO); | |
633 | return true; | |
634 | } | |
635 | ||
636 | return false; | |
637 | } | |
638 | ||
d7b76f09 | 639 | void hists__filter_by_dso(struct hists *hists) |
b09e0190 ACM |
640 | { |
641 | struct rb_node *nd; | |
642 | ||
42b28ac0 ACM |
643 | hists->nr_entries = hists->stats.total_period = 0; |
644 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
645 | hists__reset_col_len(hists); | |
b09e0190 | 646 | |
42b28ac0 | 647 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
648 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
649 | ||
650 | if (symbol_conf.exclude_other && !h->parent) | |
651 | continue; | |
652 | ||
90cf1fb5 | 653 | if (hists__filter_entry_by_dso(hists, h)) |
b09e0190 | 654 | continue; |
b09e0190 | 655 | |
42b28ac0 | 656 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
b09e0190 ACM |
657 | } |
658 | } | |
659 | ||
90cf1fb5 ACM |
660 | static bool hists__filter_entry_by_thread(struct hists *hists, |
661 | struct hist_entry *he) | |
662 | { | |
663 | if (hists->thread_filter != NULL && | |
664 | he->thread != hists->thread_filter) { | |
665 | he->filtered |= (1 << HIST_FILTER__THREAD); | |
666 | return true; | |
667 | } | |
668 | ||
669 | return false; | |
670 | } | |
671 | ||
d7b76f09 | 672 | void hists__filter_by_thread(struct hists *hists) |
b09e0190 ACM |
673 | { |
674 | struct rb_node *nd; | |
675 | ||
42b28ac0 ACM |
676 | hists->nr_entries = hists->stats.total_period = 0; |
677 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
678 | hists__reset_col_len(hists); | |
b09e0190 | 679 | |
42b28ac0 | 680 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
681 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
682 | ||
90cf1fb5 | 683 | if (hists__filter_entry_by_thread(hists, h)) |
b09e0190 | 684 | continue; |
cc5edb0e | 685 | |
42b28ac0 | 686 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
b09e0190 ACM |
687 | } |
688 | } | |
ef7b93a1 | 689 | |
e94d53eb NK |
690 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
691 | struct hist_entry *he) | |
692 | { | |
693 | if (hists->symbol_filter_str != NULL && | |
694 | (!he->ms.sym || strstr(he->ms.sym->name, | |
695 | hists->symbol_filter_str) == NULL)) { | |
696 | he->filtered |= (1 << HIST_FILTER__SYMBOL); | |
697 | return true; | |
698 | } | |
699 | ||
700 | return false; | |
701 | } | |
702 | ||
703 | void hists__filter_by_symbol(struct hists *hists) | |
704 | { | |
705 | struct rb_node *nd; | |
706 | ||
707 | hists->nr_entries = hists->stats.total_period = 0; | |
708 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
709 | hists__reset_col_len(hists); | |
710 | ||
711 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | |
712 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
713 | ||
714 | if (hists__filter_entry_by_symbol(hists, h)) | |
715 | continue; | |
716 | ||
717 | hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL); | |
718 | } | |
719 | } | |
720 | ||
2f525d01 | 721 | int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip) |
ef7b93a1 | 722 | { |
2f525d01 | 723 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip); |
ef7b93a1 ACM |
724 | } |
725 | ||
ce6f4fab | 726 | int hist_entry__annotate(struct hist_entry *he, size_t privsize) |
ef7b93a1 | 727 | { |
ce6f4fab | 728 | return symbol__annotate(he->ms.sym, he->ms.map, privsize); |
ef7b93a1 | 729 | } |
c8446b9b | 730 | |
28a6b6aa ACM |
731 | void events_stats__inc(struct events_stats *stats, u32 type) |
732 | { | |
733 | ++stats->nr_events[0]; | |
734 | ++stats->nr_events[type]; | |
735 | } | |
736 | ||
42b28ac0 | 737 | void hists__inc_nr_events(struct hists *hists, u32 type) |
c8446b9b | 738 | { |
28a6b6aa | 739 | events_stats__inc(&hists->stats, type); |
c8446b9b | 740 | } |
95529be4 | 741 | |
494d70a1 ACM |
742 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
743 | struct hist_entry *pair) | |
744 | { | |
ce74f60e NK |
745 | struct rb_root *root; |
746 | struct rb_node **p; | |
494d70a1 ACM |
747 | struct rb_node *parent = NULL; |
748 | struct hist_entry *he; | |
749 | int cmp; | |
750 | ||
ce74f60e NK |
751 | if (sort__need_collapse) |
752 | root = &hists->entries_collapsed; | |
753 | else | |
754 | root = hists->entries_in; | |
755 | ||
756 | p = &root->rb_node; | |
757 | ||
494d70a1 ACM |
758 | while (*p != NULL) { |
759 | parent = *p; | |
ce74f60e | 760 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
494d70a1 | 761 | |
ce74f60e | 762 | cmp = hist_entry__collapse(he, pair); |
494d70a1 ACM |
763 | |
764 | if (!cmp) | |
765 | goto out; | |
766 | ||
767 | if (cmp < 0) | |
768 | p = &(*p)->rb_left; | |
769 | else | |
770 | p = &(*p)->rb_right; | |
771 | } | |
772 | ||
773 | he = hist_entry__new(pair); | |
774 | if (he) { | |
30193d78 ACM |
775 | memset(&he->stat, 0, sizeof(he->stat)); |
776 | he->hists = hists; | |
ce74f60e NK |
777 | rb_link_node(&he->rb_node_in, parent, p); |
778 | rb_insert_color(&he->rb_node_in, root); | |
494d70a1 ACM |
779 | hists__inc_nr_entries(hists, he); |
780 | } | |
781 | out: | |
782 | return he; | |
783 | } | |
784 | ||
95529be4 ACM |
785 | static struct hist_entry *hists__find_entry(struct hists *hists, |
786 | struct hist_entry *he) | |
787 | { | |
ce74f60e NK |
788 | struct rb_node *n; |
789 | ||
790 | if (sort__need_collapse) | |
791 | n = hists->entries_collapsed.rb_node; | |
792 | else | |
793 | n = hists->entries_in->rb_node; | |
95529be4 ACM |
794 | |
795 | while (n) { | |
ce74f60e NK |
796 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
797 | int64_t cmp = hist_entry__collapse(iter, he); | |
95529be4 ACM |
798 | |
799 | if (cmp < 0) | |
800 | n = n->rb_left; | |
801 | else if (cmp > 0) | |
802 | n = n->rb_right; | |
803 | else | |
804 | return iter; | |
805 | } | |
806 | ||
807 | return NULL; | |
808 | } | |
809 | ||
810 | /* | |
811 | * Look for pairs to link to the leader buckets (hist_entries): | |
812 | */ | |
813 | void hists__match(struct hists *leader, struct hists *other) | |
814 | { | |
ce74f60e | 815 | struct rb_root *root; |
95529be4 ACM |
816 | struct rb_node *nd; |
817 | struct hist_entry *pos, *pair; | |
818 | ||
ce74f60e NK |
819 | if (sort__need_collapse) |
820 | root = &leader->entries_collapsed; | |
821 | else | |
822 | root = leader->entries_in; | |
823 | ||
824 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
825 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
95529be4 ACM |
826 | pair = hists__find_entry(other, pos); |
827 | ||
828 | if (pair) | |
5fa9041b | 829 | hist_entry__add_pair(pair, pos); |
95529be4 ACM |
830 | } |
831 | } | |
494d70a1 ACM |
832 | |
833 | /* | |
834 | * Look for entries in the other hists that are not present in the leader, if | |
835 | * we find them, just add a dummy entry on the leader hists, with period=0, | |
836 | * nr_events=0, to serve as the list header. | |
837 | */ | |
838 | int hists__link(struct hists *leader, struct hists *other) | |
839 | { | |
ce74f60e | 840 | struct rb_root *root; |
494d70a1 ACM |
841 | struct rb_node *nd; |
842 | struct hist_entry *pos, *pair; | |
843 | ||
ce74f60e NK |
844 | if (sort__need_collapse) |
845 | root = &other->entries_collapsed; | |
846 | else | |
847 | root = other->entries_in; | |
848 | ||
849 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
850 | pos = rb_entry(nd, struct hist_entry, rb_node_in); | |
494d70a1 ACM |
851 | |
852 | if (!hist_entry__has_pairs(pos)) { | |
853 | pair = hists__add_dummy_entry(leader, pos); | |
854 | if (pair == NULL) | |
855 | return -1; | |
5fa9041b | 856 | hist_entry__add_pair(pos, pair); |
494d70a1 ACM |
857 | } |
858 | } | |
859 | ||
860 | return 0; | |
861 | } |