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 RAV |
84 | |
85 | if (h->branch_info) { | |
86 | int symlen; | |
87 | /* | |
88 | * +4 accounts for '[x] ' priv level info | |
89 | * +2 account of 0x prefix on raw addresses | |
90 | */ | |
91 | if (h->branch_info->from.sym) { | |
92 | symlen = (int)h->branch_info->from.sym->namelen + 4; | |
93 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); | |
94 | ||
95 | symlen = dso__name_len(h->branch_info->from.map->dso); | |
96 | hists__new_col_len(hists, HISTC_DSO_FROM, symlen); | |
97 | } else { | |
98 | symlen = unresolved_col_width + 4 + 2; | |
99 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); | |
100 | hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM); | |
101 | } | |
102 | ||
103 | if (h->branch_info->to.sym) { | |
104 | symlen = (int)h->branch_info->to.sym->namelen + 4; | |
105 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
106 | ||
107 | symlen = dso__name_len(h->branch_info->to.map->dso); | |
108 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); | |
109 | } else { | |
110 | symlen = unresolved_col_width + 4 + 2; | |
111 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); | |
112 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); | |
113 | } | |
114 | } | |
8a6c5b26 ACM |
115 | } |
116 | ||
7ccf4f90 NK |
117 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
118 | { | |
119 | struct rb_node *next = rb_first(&hists->entries); | |
120 | struct hist_entry *n; | |
121 | int row = 0; | |
122 | ||
123 | hists__reset_col_len(hists); | |
124 | ||
125 | while (next && row++ < max_rows) { | |
126 | n = rb_entry(next, struct hist_entry, rb_node); | |
127 | if (!n->filtered) | |
128 | hists__calc_col_len(hists, n); | |
129 | next = rb_next(&n->rb_node); | |
130 | } | |
131 | } | |
132 | ||
12c14278 | 133 | static void hist_entry__add_cpumode_period(struct hist_entry *he, |
c82ee828 | 134 | unsigned int cpumode, u64 period) |
a1645ce1 | 135 | { |
28e2a106 | 136 | switch (cpumode) { |
a1645ce1 | 137 | case PERF_RECORD_MISC_KERNEL: |
b24c28f7 | 138 | he->stat.period_sys += period; |
a1645ce1 ZY |
139 | break; |
140 | case PERF_RECORD_MISC_USER: | |
b24c28f7 | 141 | he->stat.period_us += period; |
a1645ce1 ZY |
142 | break; |
143 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
b24c28f7 | 144 | he->stat.period_guest_sys += period; |
a1645ce1 ZY |
145 | break; |
146 | case PERF_RECORD_MISC_GUEST_USER: | |
b24c28f7 | 147 | he->stat.period_guest_us += period; |
a1645ce1 ZY |
148 | break; |
149 | default: | |
150 | break; | |
151 | } | |
152 | } | |
153 | ||
139c0815 NK |
154 | static void he_stat__add_period(struct he_stat *he_stat, u64 period) |
155 | { | |
156 | he_stat->period += period; | |
157 | he_stat->nr_events += 1; | |
158 | } | |
159 | ||
160 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) | |
161 | { | |
162 | dest->period += src->period; | |
163 | dest->period_sys += src->period_sys; | |
164 | dest->period_us += src->period_us; | |
165 | dest->period_guest_sys += src->period_guest_sys; | |
166 | dest->period_guest_us += src->period_guest_us; | |
167 | dest->nr_events += src->nr_events; | |
168 | } | |
169 | ||
ab81f3fd ACM |
170 | static void hist_entry__decay(struct hist_entry *he) |
171 | { | |
b24c28f7 NK |
172 | he->stat.period = (he->stat.period * 7) / 8; |
173 | he->stat.nr_events = (he->stat.nr_events * 7) / 8; | |
ab81f3fd ACM |
174 | } |
175 | ||
176 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) | |
177 | { | |
b24c28f7 | 178 | u64 prev_period = he->stat.period; |
c64550cf ACM |
179 | |
180 | if (prev_period == 0) | |
df71d95f | 181 | return true; |
c64550cf | 182 | |
ab81f3fd | 183 | hist_entry__decay(he); |
c64550cf ACM |
184 | |
185 | if (!he->filtered) | |
b24c28f7 | 186 | hists->stats.total_period -= prev_period - he->stat.period; |
c64550cf | 187 | |
b24c28f7 | 188 | return he->stat.period == 0; |
ab81f3fd ACM |
189 | } |
190 | ||
b079d4e9 ACM |
191 | static void __hists__decay_entries(struct hists *hists, bool zap_user, |
192 | bool zap_kernel, bool threaded) | |
ab81f3fd ACM |
193 | { |
194 | struct rb_node *next = rb_first(&hists->entries); | |
195 | struct hist_entry *n; | |
196 | ||
197 | while (next) { | |
198 | n = rb_entry(next, struct hist_entry, rb_node); | |
199 | next = rb_next(&n->rb_node); | |
df71d95f ACM |
200 | /* |
201 | * We may be annotating this, for instance, so keep it here in | |
202 | * case some it gets new samples, we'll eventually free it when | |
203 | * the user stops browsing and it agains gets fully decayed. | |
204 | */ | |
b079d4e9 ACM |
205 | if (((zap_user && n->level == '.') || |
206 | (zap_kernel && n->level != '.') || | |
207 | hists__decay_entry(hists, n)) && | |
208 | !n->used) { | |
ab81f3fd ACM |
209 | rb_erase(&n->rb_node, &hists->entries); |
210 | ||
e345fa18 | 211 | if (sort__need_collapse || threaded) |
ab81f3fd ACM |
212 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
213 | ||
214 | hist_entry__free(n); | |
215 | --hists->nr_entries; | |
216 | } | |
217 | } | |
218 | } | |
219 | ||
b079d4e9 | 220 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
e345fa18 | 221 | { |
b079d4e9 | 222 | return __hists__decay_entries(hists, zap_user, zap_kernel, false); |
e345fa18 ACM |
223 | } |
224 | ||
b079d4e9 ACM |
225 | void hists__decay_entries_threaded(struct hists *hists, |
226 | bool zap_user, bool zap_kernel) | |
e345fa18 | 227 | { |
b079d4e9 | 228 | return __hists__decay_entries(hists, zap_user, zap_kernel, true); |
e345fa18 ACM |
229 | } |
230 | ||
3d1d07ec | 231 | /* |
c82ee828 | 232 | * histogram, sorted on item, collects periods |
3d1d07ec JK |
233 | */ |
234 | ||
28e2a106 ACM |
235 | static struct hist_entry *hist_entry__new(struct hist_entry *template) |
236 | { | |
d2009c51 | 237 | size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0; |
12c14278 | 238 | struct hist_entry *he = malloc(sizeof(*he) + callchain_size); |
28e2a106 | 239 | |
12c14278 ACM |
240 | if (he != NULL) { |
241 | *he = *template; | |
c4b35351 | 242 | |
12c14278 ACM |
243 | if (he->ms.map) |
244 | he->ms.map->referenced = true; | |
28e2a106 | 245 | if (symbol_conf.use_callchain) |
12c14278 | 246 | callchain_init(he->callchain); |
28e2a106 ACM |
247 | } |
248 | ||
12c14278 | 249 | return he; |
28e2a106 ACM |
250 | } |
251 | ||
42b28ac0 | 252 | static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h) |
fefb0b94 | 253 | { |
8a6c5b26 | 254 | if (!h->filtered) { |
42b28ac0 ACM |
255 | hists__calc_col_len(hists, h); |
256 | ++hists->nr_entries; | |
b24c28f7 | 257 | hists->stats.total_period += h->stat.period; |
8a6c5b26 | 258 | } |
fefb0b94 ACM |
259 | } |
260 | ||
7a007ca9 ACM |
261 | static u8 symbol__parent_filter(const struct symbol *parent) |
262 | { | |
263 | if (symbol_conf.exclude_other && parent == NULL) | |
264 | return 1 << HIST_FILTER__PARENT; | |
265 | return 0; | |
266 | } | |
267 | ||
b5387528 RAV |
268 | static struct hist_entry *add_hist_entry(struct hists *hists, |
269 | struct hist_entry *entry, | |
1c02c4d2 | 270 | struct addr_location *al, |
b5387528 | 271 | u64 period) |
9735abf1 | 272 | { |
1980c2eb | 273 | struct rb_node **p; |
9735abf1 ACM |
274 | struct rb_node *parent = NULL; |
275 | struct hist_entry *he; | |
9735abf1 ACM |
276 | int cmp; |
277 | ||
1980c2eb ACM |
278 | pthread_mutex_lock(&hists->lock); |
279 | ||
280 | p = &hists->entries_in->rb_node; | |
281 | ||
9735abf1 ACM |
282 | while (*p != NULL) { |
283 | parent = *p; | |
1980c2eb | 284 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
9735abf1 | 285 | |
b5387528 | 286 | cmp = hist_entry__cmp(entry, he); |
9735abf1 ACM |
287 | |
288 | if (!cmp) { | |
139c0815 | 289 | he_stat__add_period(&he->stat, period); |
63fa471d DM |
290 | |
291 | /* If the map of an existing hist_entry has | |
292 | * become out-of-date due to an exec() or | |
293 | * similar, update it. Otherwise we will | |
294 | * mis-adjust symbol addresses when computing | |
295 | * the history counter to increment. | |
296 | */ | |
297 | if (he->ms.map != entry->ms.map) { | |
298 | he->ms.map = entry->ms.map; | |
299 | if (he->ms.map) | |
300 | he->ms.map->referenced = true; | |
301 | } | |
28e2a106 | 302 | goto out; |
9735abf1 ACM |
303 | } |
304 | ||
305 | if (cmp < 0) | |
306 | p = &(*p)->rb_left; | |
307 | else | |
308 | p = &(*p)->rb_right; | |
309 | } | |
310 | ||
b5387528 | 311 | he = hist_entry__new(entry); |
9735abf1 | 312 | if (!he) |
1980c2eb ACM |
313 | goto out_unlock; |
314 | ||
315 | rb_link_node(&he->rb_node_in, parent, p); | |
316 | rb_insert_color(&he->rb_node_in, hists->entries_in); | |
28e2a106 | 317 | out: |
c82ee828 | 318 | hist_entry__add_cpumode_period(he, al->cpumode, period); |
1980c2eb ACM |
319 | out_unlock: |
320 | pthread_mutex_unlock(&hists->lock); | |
9735abf1 ACM |
321 | return he; |
322 | } | |
323 | ||
b5387528 RAV |
324 | struct hist_entry *__hists__add_branch_entry(struct hists *self, |
325 | struct addr_location *al, | |
326 | struct symbol *sym_parent, | |
327 | struct branch_info *bi, | |
328 | u64 period) | |
329 | { | |
330 | struct hist_entry entry = { | |
331 | .thread = al->thread, | |
332 | .ms = { | |
333 | .map = bi->to.map, | |
334 | .sym = bi->to.sym, | |
335 | }, | |
336 | .cpu = al->cpu, | |
337 | .ip = bi->to.addr, | |
338 | .level = al->level, | |
b24c28f7 NK |
339 | .stat = { |
340 | .period = period, | |
c4b35351 | 341 | .nr_events = 1, |
b24c28f7 | 342 | }, |
b5387528 RAV |
343 | .parent = sym_parent, |
344 | .filtered = symbol__parent_filter(sym_parent), | |
345 | .branch_info = bi, | |
ae359f19 | 346 | .hists = self, |
b5387528 RAV |
347 | }; |
348 | ||
349 | return add_hist_entry(self, &entry, al, period); | |
350 | } | |
351 | ||
352 | struct hist_entry *__hists__add_entry(struct hists *self, | |
353 | struct addr_location *al, | |
354 | struct symbol *sym_parent, u64 period) | |
355 | { | |
356 | struct hist_entry entry = { | |
357 | .thread = al->thread, | |
358 | .ms = { | |
359 | .map = al->map, | |
360 | .sym = al->sym, | |
361 | }, | |
362 | .cpu = al->cpu, | |
363 | .ip = al->addr, | |
364 | .level = al->level, | |
b24c28f7 NK |
365 | .stat = { |
366 | .period = period, | |
c4b35351 | 367 | .nr_events = 1, |
b24c28f7 | 368 | }, |
b5387528 RAV |
369 | .parent = sym_parent, |
370 | .filtered = symbol__parent_filter(sym_parent), | |
ae359f19 | 371 | .hists = self, |
b5387528 RAV |
372 | }; |
373 | ||
374 | return add_hist_entry(self, &entry, al, period); | |
375 | } | |
376 | ||
3d1d07ec JK |
377 | int64_t |
378 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
379 | { | |
380 | struct sort_entry *se; | |
381 | int64_t cmp = 0; | |
382 | ||
383 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
fcd14984 | 384 | cmp = se->se_cmp(left, right); |
3d1d07ec JK |
385 | if (cmp) |
386 | break; | |
387 | } | |
388 | ||
389 | return cmp; | |
390 | } | |
391 | ||
392 | int64_t | |
393 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
394 | { | |
395 | struct sort_entry *se; | |
396 | int64_t cmp = 0; | |
397 | ||
398 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
399 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | |
400 | ||
fcd14984 | 401 | f = se->se_collapse ?: se->se_cmp; |
3d1d07ec JK |
402 | |
403 | cmp = f(left, right); | |
404 | if (cmp) | |
405 | break; | |
406 | } | |
407 | ||
408 | return cmp; | |
409 | } | |
410 | ||
411 | void hist_entry__free(struct hist_entry *he) | |
412 | { | |
580e338d | 413 | free(he->branch_info); |
3d1d07ec JK |
414 | free(he); |
415 | } | |
416 | ||
417 | /* | |
418 | * collapse the histogram | |
419 | */ | |
420 | ||
1d037ca1 | 421 | static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused, |
1b3a0e95 FW |
422 | struct rb_root *root, |
423 | struct hist_entry *he) | |
3d1d07ec | 424 | { |
b9bf0892 | 425 | struct rb_node **p = &root->rb_node; |
3d1d07ec JK |
426 | struct rb_node *parent = NULL; |
427 | struct hist_entry *iter; | |
428 | int64_t cmp; | |
429 | ||
430 | while (*p != NULL) { | |
431 | parent = *p; | |
1980c2eb | 432 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
3d1d07ec JK |
433 | |
434 | cmp = hist_entry__collapse(iter, he); | |
435 | ||
436 | if (!cmp) { | |
139c0815 | 437 | he_stat__add_stat(&iter->stat, &he->stat); |
9ec60972 | 438 | |
1b3a0e95 | 439 | if (symbol_conf.use_callchain) { |
47260645 NK |
440 | callchain_cursor_reset(&callchain_cursor); |
441 | callchain_merge(&callchain_cursor, | |
442 | iter->callchain, | |
1b3a0e95 FW |
443 | he->callchain); |
444 | } | |
3d1d07ec | 445 | hist_entry__free(he); |
fefb0b94 | 446 | return false; |
3d1d07ec JK |
447 | } |
448 | ||
449 | if (cmp < 0) | |
450 | p = &(*p)->rb_left; | |
451 | else | |
452 | p = &(*p)->rb_right; | |
453 | } | |
454 | ||
1980c2eb ACM |
455 | rb_link_node(&he->rb_node_in, parent, p); |
456 | rb_insert_color(&he->rb_node_in, root); | |
fefb0b94 | 457 | return true; |
3d1d07ec JK |
458 | } |
459 | ||
1980c2eb | 460 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
3d1d07ec | 461 | { |
1980c2eb ACM |
462 | struct rb_root *root; |
463 | ||
464 | pthread_mutex_lock(&hists->lock); | |
465 | ||
466 | root = hists->entries_in; | |
467 | if (++hists->entries_in > &hists->entries_in_array[1]) | |
468 | hists->entries_in = &hists->entries_in_array[0]; | |
469 | ||
470 | pthread_mutex_unlock(&hists->lock); | |
471 | ||
472 | return root; | |
473 | } | |
474 | ||
90cf1fb5 ACM |
475 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
476 | { | |
477 | hists__filter_entry_by_dso(hists, he); | |
478 | hists__filter_entry_by_thread(hists, he); | |
e94d53eb | 479 | hists__filter_entry_by_symbol(hists, he); |
90cf1fb5 ACM |
480 | } |
481 | ||
1980c2eb ACM |
482 | static void __hists__collapse_resort(struct hists *hists, bool threaded) |
483 | { | |
484 | struct rb_root *root; | |
3d1d07ec JK |
485 | struct rb_node *next; |
486 | struct hist_entry *n; | |
487 | ||
1980c2eb | 488 | if (!sort__need_collapse && !threaded) |
3d1d07ec JK |
489 | return; |
490 | ||
1980c2eb ACM |
491 | root = hists__get_rotate_entries_in(hists); |
492 | next = rb_first(root); | |
b9bf0892 | 493 | |
3d1d07ec | 494 | while (next) { |
1980c2eb ACM |
495 | n = rb_entry(next, struct hist_entry, rb_node_in); |
496 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 497 | |
1980c2eb | 498 | rb_erase(&n->rb_node_in, root); |
90cf1fb5 ACM |
499 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) { |
500 | /* | |
501 | * If it wasn't combined with one of the entries already | |
502 | * collapsed, we need to apply the filters that may have | |
503 | * been set by, say, the hist_browser. | |
504 | */ | |
505 | hists__apply_filters(hists, n); | |
90cf1fb5 | 506 | } |
3d1d07ec | 507 | } |
1980c2eb | 508 | } |
b9bf0892 | 509 | |
1980c2eb ACM |
510 | void hists__collapse_resort(struct hists *hists) |
511 | { | |
512 | return __hists__collapse_resort(hists, false); | |
513 | } | |
514 | ||
515 | void hists__collapse_resort_threaded(struct hists *hists) | |
516 | { | |
517 | return __hists__collapse_resort(hists, true); | |
3d1d07ec JK |
518 | } |
519 | ||
520 | /* | |
c82ee828 | 521 | * reverse the map, sort on period. |
3d1d07ec JK |
522 | */ |
523 | ||
1c02c4d2 ACM |
524 | static void __hists__insert_output_entry(struct rb_root *entries, |
525 | struct hist_entry *he, | |
526 | u64 min_callchain_hits) | |
3d1d07ec | 527 | { |
1c02c4d2 | 528 | struct rb_node **p = &entries->rb_node; |
3d1d07ec JK |
529 | struct rb_node *parent = NULL; |
530 | struct hist_entry *iter; | |
531 | ||
d599db3f | 532 | if (symbol_conf.use_callchain) |
b9fb9304 | 533 | callchain_param.sort(&he->sorted_chain, he->callchain, |
3d1d07ec JK |
534 | min_callchain_hits, &callchain_param); |
535 | ||
536 | while (*p != NULL) { | |
537 | parent = *p; | |
538 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
539 | ||
b24c28f7 | 540 | if (he->stat.period > iter->stat.period) |
3d1d07ec JK |
541 | p = &(*p)->rb_left; |
542 | else | |
543 | p = &(*p)->rb_right; | |
544 | } | |
545 | ||
546 | rb_link_node(&he->rb_node, parent, p); | |
1c02c4d2 | 547 | rb_insert_color(&he->rb_node, entries); |
3d1d07ec JK |
548 | } |
549 | ||
1980c2eb | 550 | static void __hists__output_resort(struct hists *hists, bool threaded) |
3d1d07ec | 551 | { |
1980c2eb | 552 | struct rb_root *root; |
3d1d07ec JK |
553 | struct rb_node *next; |
554 | struct hist_entry *n; | |
3d1d07ec JK |
555 | u64 min_callchain_hits; |
556 | ||
42b28ac0 | 557 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
3d1d07ec | 558 | |
1980c2eb ACM |
559 | if (sort__need_collapse || threaded) |
560 | root = &hists->entries_collapsed; | |
561 | else | |
562 | root = hists->entries_in; | |
563 | ||
564 | next = rb_first(root); | |
565 | hists->entries = RB_ROOT; | |
3d1d07ec | 566 | |
42b28ac0 | 567 | hists->nr_entries = 0; |
7928631a | 568 | hists->stats.total_period = 0; |
42b28ac0 | 569 | hists__reset_col_len(hists); |
fefb0b94 | 570 | |
3d1d07ec | 571 | while (next) { |
1980c2eb ACM |
572 | n = rb_entry(next, struct hist_entry, rb_node_in); |
573 | next = rb_next(&n->rb_node_in); | |
3d1d07ec | 574 | |
1980c2eb | 575 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
42b28ac0 | 576 | hists__inc_nr_entries(hists, n); |
3d1d07ec | 577 | } |
1980c2eb | 578 | } |
b9bf0892 | 579 | |
1980c2eb ACM |
580 | void hists__output_resort(struct hists *hists) |
581 | { | |
582 | return __hists__output_resort(hists, false); | |
583 | } | |
584 | ||
585 | void hists__output_resort_threaded(struct hists *hists) | |
586 | { | |
587 | return __hists__output_resort(hists, true); | |
3d1d07ec | 588 | } |
4ecf84d0 | 589 | |
42b28ac0 | 590 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
cc5edb0e ACM |
591 | enum hist_filter filter) |
592 | { | |
593 | h->filtered &= ~(1 << filter); | |
594 | if (h->filtered) | |
595 | return; | |
596 | ||
42b28ac0 | 597 | ++hists->nr_entries; |
0f0cbf7a | 598 | if (h->ms.unfolded) |
42b28ac0 | 599 | hists->nr_entries += h->nr_rows; |
0f0cbf7a | 600 | h->row_offset = 0; |
b24c28f7 NK |
601 | hists->stats.total_period += h->stat.period; |
602 | hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events; | |
cc5edb0e | 603 | |
42b28ac0 | 604 | hists__calc_col_len(hists, h); |
cc5edb0e ACM |
605 | } |
606 | ||
90cf1fb5 ACM |
607 | |
608 | static bool hists__filter_entry_by_dso(struct hists *hists, | |
609 | struct hist_entry *he) | |
610 | { | |
611 | if (hists->dso_filter != NULL && | |
612 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { | |
613 | he->filtered |= (1 << HIST_FILTER__DSO); | |
614 | return true; | |
615 | } | |
616 | ||
617 | return false; | |
618 | } | |
619 | ||
d7b76f09 | 620 | void hists__filter_by_dso(struct hists *hists) |
b09e0190 ACM |
621 | { |
622 | struct rb_node *nd; | |
623 | ||
42b28ac0 ACM |
624 | hists->nr_entries = hists->stats.total_period = 0; |
625 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
626 | hists__reset_col_len(hists); | |
b09e0190 | 627 | |
42b28ac0 | 628 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
629 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
630 | ||
631 | if (symbol_conf.exclude_other && !h->parent) | |
632 | continue; | |
633 | ||
90cf1fb5 | 634 | if (hists__filter_entry_by_dso(hists, h)) |
b09e0190 | 635 | continue; |
b09e0190 | 636 | |
42b28ac0 | 637 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
b09e0190 ACM |
638 | } |
639 | } | |
640 | ||
90cf1fb5 ACM |
641 | static bool hists__filter_entry_by_thread(struct hists *hists, |
642 | struct hist_entry *he) | |
643 | { | |
644 | if (hists->thread_filter != NULL && | |
645 | he->thread != hists->thread_filter) { | |
646 | he->filtered |= (1 << HIST_FILTER__THREAD); | |
647 | return true; | |
648 | } | |
649 | ||
650 | return false; | |
651 | } | |
652 | ||
d7b76f09 | 653 | void hists__filter_by_thread(struct hists *hists) |
b09e0190 ACM |
654 | { |
655 | struct rb_node *nd; | |
656 | ||
42b28ac0 ACM |
657 | hists->nr_entries = hists->stats.total_period = 0; |
658 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
659 | hists__reset_col_len(hists); | |
b09e0190 | 660 | |
42b28ac0 | 661 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
b09e0190 ACM |
662 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
663 | ||
90cf1fb5 | 664 | if (hists__filter_entry_by_thread(hists, h)) |
b09e0190 | 665 | continue; |
cc5edb0e | 666 | |
42b28ac0 | 667 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
b09e0190 ACM |
668 | } |
669 | } | |
ef7b93a1 | 670 | |
e94d53eb NK |
671 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
672 | struct hist_entry *he) | |
673 | { | |
674 | if (hists->symbol_filter_str != NULL && | |
675 | (!he->ms.sym || strstr(he->ms.sym->name, | |
676 | hists->symbol_filter_str) == NULL)) { | |
677 | he->filtered |= (1 << HIST_FILTER__SYMBOL); | |
678 | return true; | |
679 | } | |
680 | ||
681 | return false; | |
682 | } | |
683 | ||
684 | void hists__filter_by_symbol(struct hists *hists) | |
685 | { | |
686 | struct rb_node *nd; | |
687 | ||
688 | hists->nr_entries = hists->stats.total_period = 0; | |
689 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; | |
690 | hists__reset_col_len(hists); | |
691 | ||
692 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | |
693 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
694 | ||
695 | if (hists__filter_entry_by_symbol(hists, h)) | |
696 | continue; | |
697 | ||
698 | hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL); | |
699 | } | |
700 | } | |
701 | ||
2f525d01 | 702 | int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip) |
ef7b93a1 | 703 | { |
2f525d01 | 704 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip); |
ef7b93a1 ACM |
705 | } |
706 | ||
ce6f4fab | 707 | int hist_entry__annotate(struct hist_entry *he, size_t privsize) |
ef7b93a1 | 708 | { |
ce6f4fab | 709 | return symbol__annotate(he->ms.sym, he->ms.map, privsize); |
ef7b93a1 | 710 | } |
c8446b9b | 711 | |
42b28ac0 | 712 | void hists__inc_nr_events(struct hists *hists, u32 type) |
c8446b9b | 713 | { |
42b28ac0 ACM |
714 | ++hists->stats.nr_events[0]; |
715 | ++hists->stats.nr_events[type]; | |
c8446b9b | 716 | } |