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