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