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