]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - tools/perf/util/hist.c
perf diff: Use perf_hpp__register_sort_field interface
[mirror_ubuntu-zesty-kernel.git] / tools / perf / util / hist.c
CommitLineData
8a0ecfb8 1#include "util.h"
598357eb 2#include "build-id.h"
3d1d07ec 3#include "hist.h"
4e4f06e4
ACM
4#include "session.h"
5#include "sort.h"
2a1731fb 6#include "evlist.h"
29d720ed 7#include "evsel.h"
69bcb019 8#include "annotate.h"
740b97f9 9#include "ui/progress.h"
9b33827d 10#include <math.h>
3d1d07ec 11
90cf1fb5
ACM
12static bool hists__filter_entry_by_dso(struct hists *hists,
13 struct hist_entry *he);
14static bool hists__filter_entry_by_thread(struct hists *hists,
15 struct hist_entry *he);
e94d53eb
NK
16static bool hists__filter_entry_by_symbol(struct hists *hists,
17 struct hist_entry *he);
21394d94
KL
18static bool hists__filter_entry_by_socket(struct hists *hists,
19 struct hist_entry *he);
90cf1fb5 20
42b28ac0 21u16 hists__col_len(struct hists *hists, enum hist_column col)
8a6c5b26 22{
42b28ac0 23 return hists->col_len[col];
8a6c5b26
ACM
24}
25
42b28ac0 26void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
8a6c5b26 27{
42b28ac0 28 hists->col_len[col] = len;
8a6c5b26
ACM
29}
30
42b28ac0 31bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
8a6c5b26 32{
42b28ac0
ACM
33 if (len > hists__col_len(hists, col)) {
34 hists__set_col_len(hists, col, len);
8a6c5b26
ACM
35 return true;
36 }
37 return false;
38}
39
7ccf4f90 40void hists__reset_col_len(struct hists *hists)
8a6c5b26
ACM
41{
42 enum hist_column col;
43
44 for (col = 0; col < HISTC_NR_COLS; ++col)
42b28ac0 45 hists__set_col_len(hists, col, 0);
8a6c5b26
ACM
46}
47
b5387528
RAV
48static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
49{
50 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
51
52 if (hists__col_len(hists, dso) < unresolved_col_width &&
53 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
54 !symbol_conf.dso_list)
55 hists__set_col_len(hists, dso, unresolved_col_width);
56}
57
7ccf4f90 58void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
8a6c5b26 59{
b5387528 60 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
98a3b32c 61 int symlen;
8a6c5b26
ACM
62 u16 len;
63
ded19d57
NK
64 /*
65 * +4 accounts for '[x] ' priv level info
66 * +2 accounts for 0x prefix on raw addresses
67 * +3 accounts for ' y ' symtab origin info
68 */
69 if (h->ms.sym) {
70 symlen = h->ms.sym->namelen + 4;
71 if (verbose)
72 symlen += BITS_PER_LONG / 4 + 2 + 3;
73 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
74 } else {
98a3b32c
SE
75 symlen = unresolved_col_width + 4 + 2;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
b5387528 77 hists__set_unres_dso_col_len(hists, HISTC_DSO);
98a3b32c 78 }
8a6c5b26
ACM
79
80 len = thread__comm_len(h->thread);
42b28ac0
ACM
81 if (hists__new_col_len(hists, HISTC_COMM, len))
82 hists__set_col_len(hists, HISTC_THREAD, len + 6);
8a6c5b26
ACM
83
84 if (h->ms.map) {
85 len = dso__name_len(h->ms.map->dso);
42b28ac0 86 hists__new_col_len(hists, HISTC_DSO, len);
8a6c5b26 87 }
b5387528 88
cb993744
NK
89 if (h->parent)
90 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
91
b5387528 92 if (h->branch_info) {
b5387528
RAV
93 if (h->branch_info->from.sym) {
94 symlen = (int)h->branch_info->from.sym->namelen + 4;
ded19d57
NK
95 if (verbose)
96 symlen += BITS_PER_LONG / 4 + 2 + 3;
b5387528
RAV
97 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
98
99 symlen = dso__name_len(h->branch_info->from.map->dso);
100 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
101 } else {
102 symlen = unresolved_col_width + 4 + 2;
103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
105 }
106
107 if (h->branch_info->to.sym) {
108 symlen = (int)h->branch_info->to.sym->namelen + 4;
ded19d57
NK
109 if (verbose)
110 symlen += BITS_PER_LONG / 4 + 2 + 3;
b5387528
RAV
111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112
113 symlen = dso__name_len(h->branch_info->to.map->dso);
114 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
115 } else {
116 symlen = unresolved_col_width + 4 + 2;
117 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
119 }
120 }
98a3b32c
SE
121
122 if (h->mem_info) {
98a3b32c
SE
123 if (h->mem_info->daddr.sym) {
124 symlen = (int)h->mem_info->daddr.sym->namelen + 4
125 + unresolved_col_width + 2;
126 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
127 symlen);
9b32ba71
DZ
128 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
129 symlen + 1);
98a3b32c
SE
130 } else {
131 symlen = unresolved_col_width + 4 + 2;
132 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
133 symlen);
134 }
b34b3bf0
JO
135
136 if (h->mem_info->iaddr.sym) {
137 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
138 + unresolved_col_width + 2;
139 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
140 symlen);
141 } else {
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
144 symlen);
145 }
146
98a3b32c
SE
147 if (h->mem_info->daddr.map) {
148 symlen = dso__name_len(h->mem_info->daddr.map->dso);
149 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
150 symlen);
151 } else {
152 symlen = unresolved_col_width + 4 + 2;
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
154 }
155 } else {
156 symlen = unresolved_col_width + 4 + 2;
157 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
b34b3bf0 158 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
98a3b32c
SE
159 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
160 }
161
a4978eca 162 hists__new_col_len(hists, HISTC_CPU, 3);
2e7ea3ab 163 hists__new_col_len(hists, HISTC_SOCKET, 6);
98a3b32c
SE
164 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
165 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
166 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
167 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
168 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
169 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
475eeab9 170
e8e6d37e
ACM
171 if (h->srcline)
172 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
173
31191a85
AK
174 if (h->srcfile)
175 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
176
475eeab9
AK
177 if (h->transaction)
178 hists__new_col_len(hists, HISTC_TRANSACTION,
179 hist_entry__transaction_len());
8a6c5b26
ACM
180}
181
7ccf4f90
NK
182void hists__output_recalc_col_len(struct hists *hists, int max_rows)
183{
184 struct rb_node *next = rb_first(&hists->entries);
185 struct hist_entry *n;
186 int row = 0;
187
188 hists__reset_col_len(hists);
189
190 while (next && row++ < max_rows) {
191 n = rb_entry(next, struct hist_entry, rb_node);
192 if (!n->filtered)
193 hists__calc_col_len(hists, n);
194 next = rb_next(&n->rb_node);
195 }
196}
197
f39056f9
NK
198static void he_stat__add_cpumode_period(struct he_stat *he_stat,
199 unsigned int cpumode, u64 period)
a1645ce1 200{
28e2a106 201 switch (cpumode) {
a1645ce1 202 case PERF_RECORD_MISC_KERNEL:
f39056f9 203 he_stat->period_sys += period;
a1645ce1
ZY
204 break;
205 case PERF_RECORD_MISC_USER:
f39056f9 206 he_stat->period_us += period;
a1645ce1
ZY
207 break;
208 case PERF_RECORD_MISC_GUEST_KERNEL:
f39056f9 209 he_stat->period_guest_sys += period;
a1645ce1
ZY
210 break;
211 case PERF_RECORD_MISC_GUEST_USER:
f39056f9 212 he_stat->period_guest_us += period;
a1645ce1
ZY
213 break;
214 default:
215 break;
216 }
217}
218
05484298
AK
219static void he_stat__add_period(struct he_stat *he_stat, u64 period,
220 u64 weight)
139c0815 221{
98a3b32c 222
139c0815 223 he_stat->period += period;
05484298 224 he_stat->weight += weight;
139c0815
NK
225 he_stat->nr_events += 1;
226}
227
228static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
229{
230 dest->period += src->period;
231 dest->period_sys += src->period_sys;
232 dest->period_us += src->period_us;
233 dest->period_guest_sys += src->period_guest_sys;
234 dest->period_guest_us += src->period_guest_us;
235 dest->nr_events += src->nr_events;
05484298 236 dest->weight += src->weight;
139c0815
NK
237}
238
f39056f9 239static void he_stat__decay(struct he_stat *he_stat)
ab81f3fd 240{
f39056f9
NK
241 he_stat->period = (he_stat->period * 7) / 8;
242 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
05484298 243 /* XXX need decay for weight too? */
ab81f3fd
ACM
244}
245
246static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
247{
b24c28f7 248 u64 prev_period = he->stat.period;
3186b681 249 u64 diff;
c64550cf
ACM
250
251 if (prev_period == 0)
df71d95f 252 return true;
c64550cf 253
f39056f9 254 he_stat__decay(&he->stat);
f8be1c8c
NK
255 if (symbol_conf.cumulate_callchain)
256 he_stat__decay(he->stat_acc);
42b276a2 257 decay_callchain(he->callchain);
c64550cf 258
3186b681
NK
259 diff = prev_period - he->stat.period;
260
261 hists->stats.total_period -= diff;
c64550cf 262 if (!he->filtered)
3186b681 263 hists->stats.total_non_filtered_period -= diff;
c64550cf 264
b24c28f7 265 return he->stat.period == 0;
ab81f3fd
ACM
266}
267
956b65e1
ACM
268static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
269{
270 rb_erase(&he->rb_node, &hists->entries);
271
272 if (sort__need_collapse)
273 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
61fa0e94
NK
274 else
275 rb_erase(&he->rb_node_in, hists->entries_in);
956b65e1
ACM
276
277 --hists->nr_entries;
278 if (!he->filtered)
279 --hists->nr_non_filtered_entries;
280
281 hist_entry__delete(he);
282}
283
3a5714f8 284void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
ab81f3fd
ACM
285{
286 struct rb_node *next = rb_first(&hists->entries);
287 struct hist_entry *n;
288
289 while (next) {
290 n = rb_entry(next, struct hist_entry, rb_node);
291 next = rb_next(&n->rb_node);
b079d4e9
ACM
292 if (((zap_user && n->level == '.') ||
293 (zap_kernel && n->level != '.') ||
4c47f4fc 294 hists__decay_entry(hists, n))) {
956b65e1 295 hists__delete_entry(hists, n);
ab81f3fd
ACM
296 }
297 }
298}
299
701937bd
NK
300void hists__delete_entries(struct hists *hists)
301{
302 struct rb_node *next = rb_first(&hists->entries);
303 struct hist_entry *n;
304
305 while (next) {
306 n = rb_entry(next, struct hist_entry, rb_node);
307 next = rb_next(&n->rb_node);
308
956b65e1 309 hists__delete_entry(hists, n);
701937bd
NK
310 }
311}
312
3d1d07ec 313/*
c82ee828 314 * histogram, sorted on item, collects periods
3d1d07ec
JK
315 */
316
a0b51af3
NK
317static struct hist_entry *hist_entry__new(struct hist_entry *template,
318 bool sample_self)
28e2a106 319{
f8be1c8c
NK
320 size_t callchain_size = 0;
321 struct hist_entry *he;
322
82aa019e 323 if (symbol_conf.use_callchain)
f8be1c8c
NK
324 callchain_size = sizeof(struct callchain_root);
325
326 he = zalloc(sizeof(*he) + callchain_size);
28e2a106 327
12c14278
ACM
328 if (he != NULL) {
329 *he = *template;
c4b35351 330
f8be1c8c
NK
331 if (symbol_conf.cumulate_callchain) {
332 he->stat_acc = malloc(sizeof(he->stat));
333 if (he->stat_acc == NULL) {
334 free(he);
335 return NULL;
336 }
337 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
a0b51af3
NK
338 if (!sample_self)
339 memset(&he->stat, 0, sizeof(he->stat));
f8be1c8c
NK
340 }
341
5c24b67a 342 map__get(he->ms.map);
3cf0cb1f
SE
343
344 if (he->branch_info) {
26353a61
NK
345 /*
346 * This branch info is (a part of) allocated from
644f2df2 347 * sample__resolve_bstack() and will be freed after
26353a61
NK
348 * adding new entries. So we need to save a copy.
349 */
350 he->branch_info = malloc(sizeof(*he->branch_info));
351 if (he->branch_info == NULL) {
5c24b67a 352 map__zput(he->ms.map);
f8be1c8c 353 free(he->stat_acc);
26353a61
NK
354 free(he);
355 return NULL;
356 }
357
358 memcpy(he->branch_info, template->branch_info,
359 sizeof(*he->branch_info));
360
5c24b67a
ACM
361 map__get(he->branch_info->from.map);
362 map__get(he->branch_info->to.map);
3cf0cb1f
SE
363 }
364
98a3b32c 365 if (he->mem_info) {
5c24b67a
ACM
366 map__get(he->mem_info->iaddr.map);
367 map__get(he->mem_info->daddr.map);
98a3b32c
SE
368 }
369
28e2a106 370 if (symbol_conf.use_callchain)
12c14278 371 callchain_init(he->callchain);
b821c732 372
72392834
NK
373 if (he->raw_data) {
374 he->raw_data = memdup(he->raw_data, he->raw_size);
375
376 if (he->raw_data == NULL) {
377 map__put(he->ms.map);
378 if (he->branch_info) {
379 map__put(he->branch_info->from.map);
380 map__put(he->branch_info->to.map);
381 free(he->branch_info);
382 }
383 if (he->mem_info) {
384 map__put(he->mem_info->iaddr.map);
385 map__put(he->mem_info->daddr.map);
386 }
387 free(he->stat_acc);
388 free(he);
389 return NULL;
390 }
391 }
b821c732 392 INIT_LIST_HEAD(&he->pairs.node);
f3b623b8 393 thread__get(he->thread);
28e2a106
ACM
394 }
395
12c14278 396 return he;
28e2a106
ACM
397}
398
7a007ca9
ACM
399static u8 symbol__parent_filter(const struct symbol *parent)
400{
401 if (symbol_conf.exclude_other && parent == NULL)
402 return 1 << HIST_FILTER__PARENT;
403 return 0;
404}
405
e7e0efcd
ACM
406static struct hist_entry *hists__findnew_entry(struct hists *hists,
407 struct hist_entry *entry,
408 struct addr_location *al,
409 bool sample_self)
9735abf1 410{
1980c2eb 411 struct rb_node **p;
9735abf1
ACM
412 struct rb_node *parent = NULL;
413 struct hist_entry *he;
354cc40e 414 int64_t cmp;
f1cbf78d
NK
415 u64 period = entry->stat.period;
416 u64 weight = entry->stat.weight;
9735abf1 417
1980c2eb
ACM
418 p = &hists->entries_in->rb_node;
419
9735abf1
ACM
420 while (*p != NULL) {
421 parent = *p;
1980c2eb 422 he = rb_entry(parent, struct hist_entry, rb_node_in);
9735abf1 423
9afcf930
NK
424 /*
425 * Make sure that it receives arguments in a same order as
426 * hist_entry__collapse() so that we can use an appropriate
427 * function when searching an entry regardless which sort
428 * keys were used.
429 */
430 cmp = hist_entry__cmp(he, entry);
9735abf1
ACM
431
432 if (!cmp) {
a0b51af3
NK
433 if (sample_self)
434 he_stat__add_period(&he->stat, period, weight);
f8be1c8c
NK
435 if (symbol_conf.cumulate_callchain)
436 he_stat__add_period(he->stat_acc, period, weight);
63fa471d 437
ceb2acbc 438 /*
e80faac0 439 * This mem info was allocated from sample__resolve_mem
ceb2acbc
NK
440 * and will not be used anymore.
441 */
74cf249d 442 zfree(&entry->mem_info);
ceb2acbc 443
63fa471d
DM
444 /* If the map of an existing hist_entry has
445 * become out-of-date due to an exec() or
446 * similar, update it. Otherwise we will
447 * mis-adjust symbol addresses when computing
448 * the history counter to increment.
449 */
450 if (he->ms.map != entry->ms.map) {
5c24b67a
ACM
451 map__put(he->ms.map);
452 he->ms.map = map__get(entry->ms.map);
63fa471d 453 }
28e2a106 454 goto out;
9735abf1
ACM
455 }
456
457 if (cmp < 0)
458 p = &(*p)->rb_left;
459 else
460 p = &(*p)->rb_right;
461 }
462
a0b51af3 463 he = hist_entry__new(entry, sample_self);
9735abf1 464 if (!he)
27a0dcb7 465 return NULL;
1980c2eb 466
590cd344
NK
467 hists->nr_entries++;
468
1980c2eb
ACM
469 rb_link_node(&he->rb_node_in, parent, p);
470 rb_insert_color(&he->rb_node_in, hists->entries_in);
28e2a106 471out:
a0b51af3
NK
472 if (sample_self)
473 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
f8be1c8c
NK
474 if (symbol_conf.cumulate_callchain)
475 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
9735abf1
ACM
476 return he;
477}
478
c824c433 479struct hist_entry *__hists__add_entry(struct hists *hists,
b5387528 480 struct addr_location *al,
41a4e6e2
NK
481 struct symbol *sym_parent,
482 struct branch_info *bi,
483 struct mem_info *mi,
fd36f3dd 484 struct perf_sample *sample,
a0b51af3 485 bool sample_self)
b5387528
RAV
486{
487 struct hist_entry entry = {
488 .thread = al->thread,
4dfced35 489 .comm = thread__comm(al->thread),
b5387528
RAV
490 .ms = {
491 .map = al->map,
492 .sym = al->sym,
493 },
0c4c4deb 494 .socket = al->socket,
7365be55
DZ
495 .cpu = al->cpu,
496 .cpumode = al->cpumode,
497 .ip = al->addr,
498 .level = al->level,
b24c28f7 499 .stat = {
c4b35351 500 .nr_events = 1,
fd36f3dd
NK
501 .period = sample->period,
502 .weight = sample->weight,
b24c28f7 503 },
b5387528 504 .parent = sym_parent,
2c86c7ca 505 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
c824c433 506 .hists = hists,
41a4e6e2
NK
507 .branch_info = bi,
508 .mem_info = mi,
fd36f3dd 509 .transaction = sample->transaction,
72392834
NK
510 .raw_data = sample->raw_data,
511 .raw_size = sample->raw_size,
b5387528
RAV
512 };
513
e7e0efcd 514 return hists__findnew_entry(hists, &entry, al, sample_self);
b5387528
RAV
515}
516
69bcb019
NK
517static int
518iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
519 struct addr_location *al __maybe_unused)
520{
521 return 0;
522}
523
524static int
525iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
526 struct addr_location *al __maybe_unused)
527{
528 return 0;
529}
530
531static int
532iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
533{
534 struct perf_sample *sample = iter->sample;
535 struct mem_info *mi;
536
537 mi = sample__resolve_mem(sample, al);
538 if (mi == NULL)
539 return -ENOMEM;
540
541 iter->priv = mi;
542 return 0;
543}
544
545static int
546iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
547{
548 u64 cost;
549 struct mem_info *mi = iter->priv;
4ea062ed 550 struct hists *hists = evsel__hists(iter->evsel);
fd36f3dd 551 struct perf_sample *sample = iter->sample;
69bcb019
NK
552 struct hist_entry *he;
553
554 if (mi == NULL)
555 return -EINVAL;
556
fd36f3dd 557 cost = sample->weight;
69bcb019
NK
558 if (!cost)
559 cost = 1;
560
561 /*
562 * must pass period=weight in order to get the correct
563 * sorting from hists__collapse_resort() which is solely
564 * based on periods. We want sorting be done on nr_events * weight
565 * and this is indirectly achieved by passing period=weight here
566 * and the he_stat__add_period() function.
567 */
fd36f3dd
NK
568 sample->period = cost;
569
4ea062ed 570 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
fd36f3dd 571 sample, true);
69bcb019
NK
572 if (!he)
573 return -ENOMEM;
574
575 iter->he = he;
576 return 0;
577}
578
579static int
9d3c02d7
NK
580iter_finish_mem_entry(struct hist_entry_iter *iter,
581 struct addr_location *al __maybe_unused)
69bcb019
NK
582{
583 struct perf_evsel *evsel = iter->evsel;
4ea062ed 584 struct hists *hists = evsel__hists(evsel);
69bcb019 585 struct hist_entry *he = iter->he;
69bcb019
NK
586 int err = -EINVAL;
587
588 if (he == NULL)
589 goto out;
590
4ea062ed 591 hists__inc_nr_samples(hists, he->filtered);
69bcb019
NK
592
593 err = hist_entry__append_callchain(he, iter->sample);
594
595out:
596 /*
e7e0efcd
ACM
597 * We don't need to free iter->priv (mem_info) here since the mem info
598 * was either already freed in hists__findnew_entry() or passed to a
599 * new hist entry by hist_entry__new().
69bcb019
NK
600 */
601 iter->priv = NULL;
602
603 iter->he = NULL;
604 return err;
605}
606
607static int
608iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
609{
610 struct branch_info *bi;
611 struct perf_sample *sample = iter->sample;
612
613 bi = sample__resolve_bstack(sample, al);
614 if (!bi)
615 return -ENOMEM;
616
617 iter->curr = 0;
618 iter->total = sample->branch_stack->nr;
619
620 iter->priv = bi;
621 return 0;
622}
623
624static int
625iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
626 struct addr_location *al __maybe_unused)
627{
9d3c02d7
NK
628 /* to avoid calling callback function */
629 iter->he = NULL;
630
69bcb019
NK
631 return 0;
632}
633
634static int
635iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
636{
637 struct branch_info *bi = iter->priv;
638 int i = iter->curr;
639
640 if (bi == NULL)
641 return 0;
642
643 if (iter->curr >= iter->total)
644 return 0;
645
646 al->map = bi[i].to.map;
647 al->sym = bi[i].to.sym;
648 al->addr = bi[i].to.addr;
649 return 1;
650}
651
652static int
653iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
654{
9d3c02d7 655 struct branch_info *bi;
69bcb019 656 struct perf_evsel *evsel = iter->evsel;
4ea062ed 657 struct hists *hists = evsel__hists(evsel);
fd36f3dd 658 struct perf_sample *sample = iter->sample;
69bcb019
NK
659 struct hist_entry *he = NULL;
660 int i = iter->curr;
661 int err = 0;
662
663 bi = iter->priv;
664
665 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
666 goto out;
667
668 /*
669 * The report shows the percentage of total branches captured
670 * and not events sampled. Thus we use a pseudo period of 1.
671 */
fd36f3dd
NK
672 sample->period = 1;
673 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
674
4ea062ed 675 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
fd36f3dd 676 sample, true);
69bcb019
NK
677 if (he == NULL)
678 return -ENOMEM;
679
4ea062ed 680 hists__inc_nr_samples(hists, he->filtered);
69bcb019
NK
681
682out:
683 iter->he = he;
684 iter->curr++;
685 return err;
686}
687
688static int
689iter_finish_branch_entry(struct hist_entry_iter *iter,
690 struct addr_location *al __maybe_unused)
691{
692 zfree(&iter->priv);
693 iter->he = NULL;
694
695 return iter->curr >= iter->total ? 0 : -1;
696}
697
698static int
699iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
700 struct addr_location *al __maybe_unused)
701{
702 return 0;
703}
704
705static int
706iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
707{
708 struct perf_evsel *evsel = iter->evsel;
709 struct perf_sample *sample = iter->sample;
710 struct hist_entry *he;
711
4ea062ed 712 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
fd36f3dd 713 sample, true);
69bcb019
NK
714 if (he == NULL)
715 return -ENOMEM;
716
717 iter->he = he;
718 return 0;
719}
720
721static int
9d3c02d7
NK
722iter_finish_normal_entry(struct hist_entry_iter *iter,
723 struct addr_location *al __maybe_unused)
69bcb019 724{
69bcb019
NK
725 struct hist_entry *he = iter->he;
726 struct perf_evsel *evsel = iter->evsel;
727 struct perf_sample *sample = iter->sample;
728
729 if (he == NULL)
730 return 0;
731
732 iter->he = NULL;
733
4ea062ed 734 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
69bcb019
NK
735
736 return hist_entry__append_callchain(he, sample);
737}
738
7a13aa28 739static int
96b40f3c 740iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
7a13aa28
NK
741 struct addr_location *al __maybe_unused)
742{
b4d3c8bd
NK
743 struct hist_entry **he_cache;
744
7a13aa28 745 callchain_cursor_commit(&callchain_cursor);
b4d3c8bd
NK
746
747 /*
748 * This is for detecting cycles or recursions so that they're
749 * cumulated only one time to prevent entries more than 100%
750 * overhead.
751 */
96b40f3c 752 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
b4d3c8bd
NK
753 if (he_cache == NULL)
754 return -ENOMEM;
755
756 iter->priv = he_cache;
757 iter->curr = 0;
758
7a13aa28
NK
759 return 0;
760}
761
762static int
763iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
764 struct addr_location *al)
765{
766 struct perf_evsel *evsel = iter->evsel;
4ea062ed 767 struct hists *hists = evsel__hists(evsel);
7a13aa28 768 struct perf_sample *sample = iter->sample;
b4d3c8bd 769 struct hist_entry **he_cache = iter->priv;
7a13aa28
NK
770 struct hist_entry *he;
771 int err = 0;
772
4ea062ed 773 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
fd36f3dd 774 sample, true);
7a13aa28
NK
775 if (he == NULL)
776 return -ENOMEM;
777
778 iter->he = he;
b4d3c8bd 779 he_cache[iter->curr++] = he;
7a13aa28 780
82aa019e 781 hist_entry__append_callchain(he, sample);
be7f855a
NK
782
783 /*
784 * We need to re-initialize the cursor since callchain_append()
785 * advanced the cursor to the end.
786 */
787 callchain_cursor_commit(&callchain_cursor);
788
4ea062ed 789 hists__inc_nr_samples(hists, he->filtered);
7a13aa28
NK
790
791 return err;
792}
793
794static int
795iter_next_cumulative_entry(struct hist_entry_iter *iter,
796 struct addr_location *al)
797{
798 struct callchain_cursor_node *node;
799
800 node = callchain_cursor_current(&callchain_cursor);
801 if (node == NULL)
802 return 0;
803
c7405d85 804 return fill_callchain_info(al, node, iter->hide_unresolved);
7a13aa28
NK
805}
806
807static int
808iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
809 struct addr_location *al)
810{
811 struct perf_evsel *evsel = iter->evsel;
812 struct perf_sample *sample = iter->sample;
b4d3c8bd 813 struct hist_entry **he_cache = iter->priv;
7a13aa28 814 struct hist_entry *he;
b4d3c8bd 815 struct hist_entry he_tmp = {
5cef8976 816 .hists = evsel__hists(evsel),
b4d3c8bd
NK
817 .cpu = al->cpu,
818 .thread = al->thread,
819 .comm = thread__comm(al->thread),
820 .ip = al->addr,
821 .ms = {
822 .map = al->map,
823 .sym = al->sym,
824 },
825 .parent = iter->parent,
72392834
NK
826 .raw_data = sample->raw_data,
827 .raw_size = sample->raw_size,
b4d3c8bd
NK
828 };
829 int i;
be7f855a
NK
830 struct callchain_cursor cursor;
831
832 callchain_cursor_snapshot(&cursor, &callchain_cursor);
833
834 callchain_cursor_advance(&callchain_cursor);
b4d3c8bd
NK
835
836 /*
837 * Check if there's duplicate entries in the callchain.
838 * It's possible that it has cycles or recursive calls.
839 */
840 for (i = 0; i < iter->curr; i++) {
9d3c02d7
NK
841 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
842 /* to avoid calling callback function */
843 iter->he = NULL;
b4d3c8bd 844 return 0;
9d3c02d7 845 }
b4d3c8bd 846 }
7a13aa28 847
4ea062ed 848 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
fd36f3dd 849 sample, false);
7a13aa28
NK
850 if (he == NULL)
851 return -ENOMEM;
852
853 iter->he = he;
b4d3c8bd 854 he_cache[iter->curr++] = he;
7a13aa28 855
82aa019e
NK
856 if (symbol_conf.use_callchain)
857 callchain_append(he->callchain, &cursor, sample->period);
7a13aa28
NK
858 return 0;
859}
860
861static int
862iter_finish_cumulative_entry(struct hist_entry_iter *iter,
863 struct addr_location *al __maybe_unused)
864{
b4d3c8bd 865 zfree(&iter->priv);
7a13aa28 866 iter->he = NULL;
b4d3c8bd 867
7a13aa28
NK
868 return 0;
869}
870
69bcb019
NK
871const struct hist_iter_ops hist_iter_mem = {
872 .prepare_entry = iter_prepare_mem_entry,
873 .add_single_entry = iter_add_single_mem_entry,
874 .next_entry = iter_next_nop_entry,
875 .add_next_entry = iter_add_next_nop_entry,
876 .finish_entry = iter_finish_mem_entry,
877};
878
879const struct hist_iter_ops hist_iter_branch = {
880 .prepare_entry = iter_prepare_branch_entry,
881 .add_single_entry = iter_add_single_branch_entry,
882 .next_entry = iter_next_branch_entry,
883 .add_next_entry = iter_add_next_branch_entry,
884 .finish_entry = iter_finish_branch_entry,
885};
886
887const struct hist_iter_ops hist_iter_normal = {
888 .prepare_entry = iter_prepare_normal_entry,
889 .add_single_entry = iter_add_single_normal_entry,
890 .next_entry = iter_next_nop_entry,
891 .add_next_entry = iter_add_next_nop_entry,
892 .finish_entry = iter_finish_normal_entry,
893};
894
7a13aa28
NK
895const struct hist_iter_ops hist_iter_cumulative = {
896 .prepare_entry = iter_prepare_cumulative_entry,
897 .add_single_entry = iter_add_single_cumulative_entry,
898 .next_entry = iter_next_cumulative_entry,
899 .add_next_entry = iter_add_next_cumulative_entry,
900 .finish_entry = iter_finish_cumulative_entry,
901};
902
69bcb019 903int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
9d3c02d7 904 int max_stack_depth, void *arg)
69bcb019
NK
905{
906 int err, err2;
907
063bd936
NK
908 err = sample__resolve_callchain(iter->sample, &iter->parent,
909 iter->evsel, al, max_stack_depth);
69bcb019
NK
910 if (err)
911 return err;
912
96b40f3c
AH
913 iter->max_stack = max_stack_depth;
914
69bcb019
NK
915 err = iter->ops->prepare_entry(iter, al);
916 if (err)
917 goto out;
918
919 err = iter->ops->add_single_entry(iter, al);
920 if (err)
921 goto out;
922
9d3c02d7
NK
923 if (iter->he && iter->add_entry_cb) {
924 err = iter->add_entry_cb(iter, al, true, arg);
925 if (err)
926 goto out;
927 }
928
69bcb019
NK
929 while (iter->ops->next_entry(iter, al)) {
930 err = iter->ops->add_next_entry(iter, al);
931 if (err)
932 break;
9d3c02d7
NK
933
934 if (iter->he && iter->add_entry_cb) {
935 err = iter->add_entry_cb(iter, al, false, arg);
936 if (err)
937 goto out;
938 }
69bcb019
NK
939 }
940
941out:
942 err2 = iter->ops->finish_entry(iter, al);
943 if (!err)
944 err = err2;
945
946 return err;
947}
948
3d1d07ec
JK
949int64_t
950hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
951{
093f0ef3 952 struct perf_hpp_fmt *fmt;
3d1d07ec
JK
953 int64_t cmp = 0;
954
093f0ef3 955 perf_hpp__for_each_sort_list(fmt) {
87bbdf76 956 cmp = fmt->cmp(fmt, left, right);
3d1d07ec
JK
957 if (cmp)
958 break;
959 }
960
961 return cmp;
962}
963
964int64_t
965hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
966{
093f0ef3 967 struct perf_hpp_fmt *fmt;
3d1d07ec
JK
968 int64_t cmp = 0;
969
093f0ef3 970 perf_hpp__for_each_sort_list(fmt) {
87bbdf76 971 cmp = fmt->collapse(fmt, left, right);
3d1d07ec
JK
972 if (cmp)
973 break;
974 }
975
976 return cmp;
977}
978
6733d1bf 979void hist_entry__delete(struct hist_entry *he)
3d1d07ec 980{
f3b623b8 981 thread__zput(he->thread);
5c24b67a
ACM
982 map__zput(he->ms.map);
983
984 if (he->branch_info) {
985 map__zput(he->branch_info->from.map);
986 map__zput(he->branch_info->to.map);
987 zfree(&he->branch_info);
988 }
989
990 if (he->mem_info) {
991 map__zput(he->mem_info->iaddr.map);
992 map__zput(he->mem_info->daddr.map);
993 zfree(&he->mem_info);
994 }
995
f8be1c8c 996 zfree(&he->stat_acc);
f048d548 997 free_srcline(he->srcline);
31191a85
AK
998 if (he->srcfile && he->srcfile[0])
999 free(he->srcfile);
d114960c 1000 free_callchain(he->callchain);
60517d28 1001 free(he->trace_output);
72392834 1002 free(he->raw_data);
3d1d07ec
JK
1003 free(he);
1004}
1005
1006/*
1007 * collapse the histogram
1008 */
1009
1d037ca1 1010static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
1b3a0e95
FW
1011 struct rb_root *root,
1012 struct hist_entry *he)
3d1d07ec 1013{
b9bf0892 1014 struct rb_node **p = &root->rb_node;
3d1d07ec
JK
1015 struct rb_node *parent = NULL;
1016 struct hist_entry *iter;
1017 int64_t cmp;
1018
1019 while (*p != NULL) {
1020 parent = *p;
1980c2eb 1021 iter = rb_entry(parent, struct hist_entry, rb_node_in);
3d1d07ec
JK
1022
1023 cmp = hist_entry__collapse(iter, he);
1024
1025 if (!cmp) {
139c0815 1026 he_stat__add_stat(&iter->stat, &he->stat);
f8be1c8c
NK
1027 if (symbol_conf.cumulate_callchain)
1028 he_stat__add_stat(iter->stat_acc, he->stat_acc);
9ec60972 1029
1b3a0e95 1030 if (symbol_conf.use_callchain) {
47260645
NK
1031 callchain_cursor_reset(&callchain_cursor);
1032 callchain_merge(&callchain_cursor,
1033 iter->callchain,
1b3a0e95
FW
1034 he->callchain);
1035 }
6733d1bf 1036 hist_entry__delete(he);
fefb0b94 1037 return false;
3d1d07ec
JK
1038 }
1039
1040 if (cmp < 0)
1041 p = &(*p)->rb_left;
1042 else
1043 p = &(*p)->rb_right;
1044 }
740b97f9 1045 hists->nr_entries++;
3d1d07ec 1046
1980c2eb
ACM
1047 rb_link_node(&he->rb_node_in, parent, p);
1048 rb_insert_color(&he->rb_node_in, root);
fefb0b94 1049 return true;
3d1d07ec
JK
1050}
1051
1980c2eb 1052static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
3d1d07ec 1053{
1980c2eb
ACM
1054 struct rb_root *root;
1055
1056 pthread_mutex_lock(&hists->lock);
1057
1058 root = hists->entries_in;
1059 if (++hists->entries_in > &hists->entries_in_array[1])
1060 hists->entries_in = &hists->entries_in_array[0];
1061
1062 pthread_mutex_unlock(&hists->lock);
1063
1064 return root;
1065}
1066
90cf1fb5
ACM
1067static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1068{
1069 hists__filter_entry_by_dso(hists, he);
1070 hists__filter_entry_by_thread(hists, he);
e94d53eb 1071 hists__filter_entry_by_symbol(hists, he);
21394d94 1072 hists__filter_entry_by_socket(hists, he);
90cf1fb5
ACM
1073}
1074
c1fb5651 1075void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1980c2eb
ACM
1076{
1077 struct rb_root *root;
3d1d07ec
JK
1078 struct rb_node *next;
1079 struct hist_entry *n;
1080
3a5714f8 1081 if (!sort__need_collapse)
3d1d07ec
JK
1082 return;
1083
740b97f9
NK
1084 hists->nr_entries = 0;
1085
1980c2eb 1086 root = hists__get_rotate_entries_in(hists);
740b97f9 1087
1980c2eb 1088 next = rb_first(root);
b9bf0892 1089
3d1d07ec 1090 while (next) {
33e940a2
ACM
1091 if (session_done())
1092 break;
1980c2eb
ACM
1093 n = rb_entry(next, struct hist_entry, rb_node_in);
1094 next = rb_next(&n->rb_node_in);
3d1d07ec 1095
1980c2eb 1096 rb_erase(&n->rb_node_in, root);
90cf1fb5
ACM
1097 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1098 /*
1099 * If it wasn't combined with one of the entries already
1100 * collapsed, we need to apply the filters that may have
1101 * been set by, say, the hist_browser.
1102 */
1103 hists__apply_filters(hists, n);
90cf1fb5 1104 }
c1fb5651
NK
1105 if (prog)
1106 ui_progress__update(prog, 1);
3d1d07ec 1107 }
1980c2eb 1108}
b9bf0892 1109
043ca389 1110static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
29d720ed 1111{
043ca389
NK
1112 struct perf_hpp_fmt *fmt;
1113 int64_t cmp = 0;
29d720ed 1114
26d8b338 1115 perf_hpp__for_each_sort_list(fmt) {
361459f1 1116 if (perf_hpp__should_skip(fmt, a->hists))
e67d49a7
NK
1117 continue;
1118
87bbdf76 1119 cmp = fmt->sort(fmt, a, b);
043ca389 1120 if (cmp)
29d720ed
NK
1121 break;
1122 }
1123
043ca389 1124 return cmp;
29d720ed
NK
1125}
1126
9283ba9b
NK
1127static void hists__reset_filter_stats(struct hists *hists)
1128{
1129 hists->nr_non_filtered_entries = 0;
1130 hists->stats.total_non_filtered_period = 0;
1131}
1132
1133void hists__reset_stats(struct hists *hists)
1134{
1135 hists->nr_entries = 0;
1136 hists->stats.total_period = 0;
1137
1138 hists__reset_filter_stats(hists);
1139}
1140
1141static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1142{
1143 hists->nr_non_filtered_entries++;
1144 hists->stats.total_non_filtered_period += h->stat.period;
1145}
1146
1147void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1148{
1149 if (!h->filtered)
1150 hists__inc_filter_stats(hists, h);
1151
1152 hists->nr_entries++;
1153 hists->stats.total_period += h->stat.period;
1154}
1155
1c02c4d2
ACM
1156static void __hists__insert_output_entry(struct rb_root *entries,
1157 struct hist_entry *he,
f9db0d0f
KL
1158 u64 min_callchain_hits,
1159 bool use_callchain)
3d1d07ec 1160{
1c02c4d2 1161 struct rb_node **p = &entries->rb_node;
3d1d07ec
JK
1162 struct rb_node *parent = NULL;
1163 struct hist_entry *iter;
1164
f9db0d0f 1165 if (use_callchain)
b9fb9304 1166 callchain_param.sort(&he->sorted_chain, he->callchain,
3d1d07ec
JK
1167 min_callchain_hits, &callchain_param);
1168
1169 while (*p != NULL) {
1170 parent = *p;
1171 iter = rb_entry(parent, struct hist_entry, rb_node);
1172
043ca389 1173 if (hist_entry__sort(he, iter) > 0)
3d1d07ec
JK
1174 p = &(*p)->rb_left;
1175 else
1176 p = &(*p)->rb_right;
1177 }
1178
1179 rb_link_node(&he->rb_node, parent, p);
1c02c4d2 1180 rb_insert_color(&he->rb_node, entries);
3d1d07ec
JK
1181}
1182
740b97f9 1183void hists__output_resort(struct hists *hists, struct ui_progress *prog)
3d1d07ec 1184{
1980c2eb 1185 struct rb_root *root;
3d1d07ec
JK
1186 struct rb_node *next;
1187 struct hist_entry *n;
3d1d07ec 1188 u64 min_callchain_hits;
f9db0d0f 1189 struct perf_evsel *evsel = hists_to_evsel(hists);
9e207ddf
KL
1190 bool use_callchain;
1191
208e7607 1192 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
9e207ddf
KL
1193 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1194 else
1195 use_callchain = symbol_conf.use_callchain;
3d1d07ec 1196
42b28ac0 1197 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
3d1d07ec 1198
3a5714f8 1199 if (sort__need_collapse)
1980c2eb
ACM
1200 root = &hists->entries_collapsed;
1201 else
1202 root = hists->entries_in;
1203
1204 next = rb_first(root);
1205 hists->entries = RB_ROOT;
3d1d07ec 1206
9283ba9b 1207 hists__reset_stats(hists);
42b28ac0 1208 hists__reset_col_len(hists);
fefb0b94 1209
3d1d07ec 1210 while (next) {
1980c2eb
ACM
1211 n = rb_entry(next, struct hist_entry, rb_node_in);
1212 next = rb_next(&n->rb_node_in);
3d1d07ec 1213
f9db0d0f 1214 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
6263835a 1215 hists__inc_stats(hists, n);
ae993efc
NK
1216
1217 if (!n->filtered)
1218 hists__calc_col_len(hists, n);
740b97f9
NK
1219
1220 if (prog)
1221 ui_progress__update(prog, 1);
3d1d07ec 1222 }
1980c2eb 1223}
b9bf0892 1224
42b28ac0 1225static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
cc5edb0e
ACM
1226 enum hist_filter filter)
1227{
1228 h->filtered &= ~(1 << filter);
1229 if (h->filtered)
1230 return;
1231
87e90f43 1232 /* force fold unfiltered entry for simplicity */
3698dab1 1233 h->unfolded = false;
0f0cbf7a 1234 h->row_offset = 0;
a8cd1f43 1235 h->nr_rows = 0;
9283ba9b 1236
1ab1fa5d 1237 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
cc5edb0e 1238
9283ba9b 1239 hists__inc_filter_stats(hists, h);
42b28ac0 1240 hists__calc_col_len(hists, h);
cc5edb0e
ACM
1241}
1242
90cf1fb5
ACM
1243
1244static bool hists__filter_entry_by_dso(struct hists *hists,
1245 struct hist_entry *he)
1246{
1247 if (hists->dso_filter != NULL &&
1248 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1249 he->filtered |= (1 << HIST_FILTER__DSO);
1250 return true;
1251 }
1252
1253 return false;
1254}
1255
d7b76f09 1256void hists__filter_by_dso(struct hists *hists)
b09e0190
ACM
1257{
1258 struct rb_node *nd;
1259
1ab1fa5d 1260 hists->stats.nr_non_filtered_samples = 0;
9283ba9b
NK
1261
1262 hists__reset_filter_stats(hists);
42b28ac0 1263 hists__reset_col_len(hists);
b09e0190 1264
42b28ac0 1265 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
b09e0190
ACM
1266 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1267
1268 if (symbol_conf.exclude_other && !h->parent)
1269 continue;
1270
90cf1fb5 1271 if (hists__filter_entry_by_dso(hists, h))
b09e0190 1272 continue;
b09e0190 1273
42b28ac0 1274 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
b09e0190
ACM
1275 }
1276}
1277
90cf1fb5
ACM
1278static bool hists__filter_entry_by_thread(struct hists *hists,
1279 struct hist_entry *he)
1280{
1281 if (hists->thread_filter != NULL &&
1282 he->thread != hists->thread_filter) {
1283 he->filtered |= (1 << HIST_FILTER__THREAD);
1284 return true;
1285 }
1286
1287 return false;
1288}
1289
d7b76f09 1290void hists__filter_by_thread(struct hists *hists)
b09e0190
ACM
1291{
1292 struct rb_node *nd;
1293
1ab1fa5d 1294 hists->stats.nr_non_filtered_samples = 0;
9283ba9b
NK
1295
1296 hists__reset_filter_stats(hists);
42b28ac0 1297 hists__reset_col_len(hists);
b09e0190 1298
42b28ac0 1299 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
b09e0190
ACM
1300 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1301
90cf1fb5 1302 if (hists__filter_entry_by_thread(hists, h))
b09e0190 1303 continue;
cc5edb0e 1304
42b28ac0 1305 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
b09e0190
ACM
1306 }
1307}
ef7b93a1 1308
e94d53eb
NK
1309static bool hists__filter_entry_by_symbol(struct hists *hists,
1310 struct hist_entry *he)
1311{
1312 if (hists->symbol_filter_str != NULL &&
1313 (!he->ms.sym || strstr(he->ms.sym->name,
1314 hists->symbol_filter_str) == NULL)) {
1315 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1316 return true;
1317 }
1318
1319 return false;
1320}
1321
1322void hists__filter_by_symbol(struct hists *hists)
1323{
1324 struct rb_node *nd;
1325
1ab1fa5d 1326 hists->stats.nr_non_filtered_samples = 0;
9283ba9b
NK
1327
1328 hists__reset_filter_stats(hists);
e94d53eb
NK
1329 hists__reset_col_len(hists);
1330
1331 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1332 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1333
1334 if (hists__filter_entry_by_symbol(hists, h))
1335 continue;
1336
1337 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1338 }
1339}
1340
21394d94
KL
1341static bool hists__filter_entry_by_socket(struct hists *hists,
1342 struct hist_entry *he)
1343{
1344 if ((hists->socket_filter > -1) &&
1345 (he->socket != hists->socket_filter)) {
1346 he->filtered |= (1 << HIST_FILTER__SOCKET);
1347 return true;
1348 }
1349
1350 return false;
1351}
1352
84734b06
KL
1353void hists__filter_by_socket(struct hists *hists)
1354{
1355 struct rb_node *nd;
1356
1357 hists->stats.nr_non_filtered_samples = 0;
1358
1359 hists__reset_filter_stats(hists);
1360 hists__reset_col_len(hists);
1361
1362 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1363 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1364
1365 if (hists__filter_entry_by_socket(hists, h))
1366 continue;
1367
1368 hists__remove_entry_filter(hists, h, HIST_FILTER__SOCKET);
1369 }
1370}
1371
28a6b6aa
ACM
1372void events_stats__inc(struct events_stats *stats, u32 type)
1373{
1374 ++stats->nr_events[0];
1375 ++stats->nr_events[type];
1376}
1377
42b28ac0 1378void hists__inc_nr_events(struct hists *hists, u32 type)
c8446b9b 1379{
28a6b6aa 1380 events_stats__inc(&hists->stats, type);
c8446b9b 1381}
95529be4 1382
1844dbcb
NK
1383void hists__inc_nr_samples(struct hists *hists, bool filtered)
1384{
1385 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1386 if (!filtered)
1387 hists->stats.nr_non_filtered_samples++;
1388}
1389
494d70a1
ACM
1390static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1391 struct hist_entry *pair)
1392{
ce74f60e
NK
1393 struct rb_root *root;
1394 struct rb_node **p;
494d70a1
ACM
1395 struct rb_node *parent = NULL;
1396 struct hist_entry *he;
354cc40e 1397 int64_t cmp;
494d70a1 1398
ce74f60e
NK
1399 if (sort__need_collapse)
1400 root = &hists->entries_collapsed;
1401 else
1402 root = hists->entries_in;
1403
1404 p = &root->rb_node;
1405
494d70a1
ACM
1406 while (*p != NULL) {
1407 parent = *p;
ce74f60e 1408 he = rb_entry(parent, struct hist_entry, rb_node_in);
494d70a1 1409
ce74f60e 1410 cmp = hist_entry__collapse(he, pair);
494d70a1
ACM
1411
1412 if (!cmp)
1413 goto out;
1414
1415 if (cmp < 0)
1416 p = &(*p)->rb_left;
1417 else
1418 p = &(*p)->rb_right;
1419 }
1420
a0b51af3 1421 he = hist_entry__new(pair, true);
494d70a1 1422 if (he) {
30193d78
ACM
1423 memset(&he->stat, 0, sizeof(he->stat));
1424 he->hists = hists;
ce74f60e
NK
1425 rb_link_node(&he->rb_node_in, parent, p);
1426 rb_insert_color(&he->rb_node_in, root);
6263835a 1427 hists__inc_stats(hists, he);
e0af43d2 1428 he->dummy = true;
494d70a1
ACM
1429 }
1430out:
1431 return he;
1432}
1433
95529be4
ACM
1434static struct hist_entry *hists__find_entry(struct hists *hists,
1435 struct hist_entry *he)
1436{
ce74f60e
NK
1437 struct rb_node *n;
1438
1439 if (sort__need_collapse)
1440 n = hists->entries_collapsed.rb_node;
1441 else
1442 n = hists->entries_in->rb_node;
95529be4
ACM
1443
1444 while (n) {
ce74f60e
NK
1445 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1446 int64_t cmp = hist_entry__collapse(iter, he);
95529be4
ACM
1447
1448 if (cmp < 0)
1449 n = n->rb_left;
1450 else if (cmp > 0)
1451 n = n->rb_right;
1452 else
1453 return iter;
1454 }
1455
1456 return NULL;
1457}
1458
1459/*
1460 * Look for pairs to link to the leader buckets (hist_entries):
1461 */
1462void hists__match(struct hists *leader, struct hists *other)
1463{
ce74f60e 1464 struct rb_root *root;
95529be4
ACM
1465 struct rb_node *nd;
1466 struct hist_entry *pos, *pair;
1467
ce74f60e
NK
1468 if (sort__need_collapse)
1469 root = &leader->entries_collapsed;
1470 else
1471 root = leader->entries_in;
1472
1473 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1474 pos = rb_entry(nd, struct hist_entry, rb_node_in);
95529be4
ACM
1475 pair = hists__find_entry(other, pos);
1476
1477 if (pair)
5fa9041b 1478 hist_entry__add_pair(pair, pos);
95529be4
ACM
1479 }
1480}
494d70a1
ACM
1481
1482/*
1483 * Look for entries in the other hists that are not present in the leader, if
1484 * we find them, just add a dummy entry on the leader hists, with period=0,
1485 * nr_events=0, to serve as the list header.
1486 */
1487int hists__link(struct hists *leader, struct hists *other)
1488{
ce74f60e 1489 struct rb_root *root;
494d70a1
ACM
1490 struct rb_node *nd;
1491 struct hist_entry *pos, *pair;
1492
ce74f60e
NK
1493 if (sort__need_collapse)
1494 root = &other->entries_collapsed;
1495 else
1496 root = other->entries_in;
1497
1498 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1499 pos = rb_entry(nd, struct hist_entry, rb_node_in);
494d70a1
ACM
1500
1501 if (!hist_entry__has_pairs(pos)) {
1502 pair = hists__add_dummy_entry(leader, pos);
1503 if (pair == NULL)
1504 return -1;
5fa9041b 1505 hist_entry__add_pair(pos, pair);
494d70a1
ACM
1506 }
1507 }
1508
1509 return 0;
1510}
f2148330 1511
57849998
AK
1512void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1513 struct perf_sample *sample, bool nonany_branch_mode)
1514{
1515 struct branch_info *bi;
1516
1517 /* If we have branch cycles always annotate them. */
1518 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1519 int i;
1520
1521 bi = sample__resolve_bstack(sample, al);
1522 if (bi) {
1523 struct addr_map_symbol *prev = NULL;
1524
1525 /*
1526 * Ignore errors, still want to process the
1527 * other entries.
1528 *
1529 * For non standard branch modes always
1530 * force no IPC (prev == NULL)
1531 *
1532 * Note that perf stores branches reversed from
1533 * program order!
1534 */
1535 for (i = bs->nr - 1; i >= 0; i--) {
1536 addr_map_symbol__account_cycles(&bi[i].from,
1537 nonany_branch_mode ? NULL : prev,
1538 bi[i].flags.cycles);
1539 prev = &bi[i].to;
1540 }
1541 free(bi);
1542 }
1543 }
1544}
2a1731fb
ACM
1545
1546size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1547{
1548 struct perf_evsel *pos;
1549 size_t ret = 0;
1550
1551 evlist__for_each(evlist, pos) {
1552 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1553 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1554 }
1555
1556 return ret;
1557}
1558
1559
f2148330
NK
1560u64 hists__total_period(struct hists *hists)
1561{
1562 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1563 hists->stats.total_period;
1564}
33db4568
NK
1565
1566int parse_filter_percentage(const struct option *opt __maybe_unused,
1567 const char *arg, int unset __maybe_unused)
1568{
1569 if (!strcmp(arg, "relative"))
1570 symbol_conf.filter_relative = true;
1571 else if (!strcmp(arg, "absolute"))
1572 symbol_conf.filter_relative = false;
1573 else
1574 return -1;
1575
1576 return 0;
1577}
0b93da17
NK
1578
1579int perf_hist_config(const char *var, const char *value)
1580{
1581 if (!strcmp(var, "hist.percentage"))
1582 return parse_filter_percentage(NULL, value, 0);
1583
1584 return 0;
1585}
a635fc51
ACM
1586
1587static int hists_evsel__init(struct perf_evsel *evsel)
1588{
1589 struct hists *hists = evsel__hists(evsel);
1590
1591 memset(hists, 0, sizeof(*hists));
1592 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1593 hists->entries_in = &hists->entries_in_array[0];
1594 hists->entries_collapsed = RB_ROOT;
1595 hists->entries = RB_ROOT;
1596 pthread_mutex_init(&hists->lock, NULL);
21394d94 1597 hists->socket_filter = -1;
a635fc51
ACM
1598 return 0;
1599}
1600
61fa0e94
NK
1601static void hists__delete_remaining_entries(struct rb_root *root)
1602{
1603 struct rb_node *node;
1604 struct hist_entry *he;
1605
1606 while (!RB_EMPTY_ROOT(root)) {
1607 node = rb_first(root);
1608 rb_erase(node, root);
1609
1610 he = rb_entry(node, struct hist_entry, rb_node_in);
1611 hist_entry__delete(he);
1612 }
1613}
1614
1615static void hists__delete_all_entries(struct hists *hists)
1616{
1617 hists__delete_entries(hists);
1618 hists__delete_remaining_entries(&hists->entries_in_array[0]);
1619 hists__delete_remaining_entries(&hists->entries_in_array[1]);
1620 hists__delete_remaining_entries(&hists->entries_collapsed);
1621}
1622
17577dec
MH
1623static void hists_evsel__exit(struct perf_evsel *evsel)
1624{
1625 struct hists *hists = evsel__hists(evsel);
1626
61fa0e94 1627 hists__delete_all_entries(hists);
17577dec
MH
1628}
1629
a635fc51
ACM
1630/*
1631 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1632 * stored in the rbtree...
1633 */
1634
1635int hists__init(void)
1636{
1637 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
17577dec
MH
1638 hists_evsel__init,
1639 hists_evsel__exit);
a635fc51
ACM
1640 if (err)
1641 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1642
1643 return err;
1644}