]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - tools/perf/util/hist.c
perf unwind: Fix wrongly used regs for aarch64 unwind
[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 81 if (hists__new_col_len(hists, HISTC_COMM, len))
89c7cb2c 82 hists__set_col_len(hists, HISTC_THREAD, len + 8);
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 }
508be0df
AK
120
121 if (h->branch_info->srcline_from)
122 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
123 strlen(h->branch_info->srcline_from));
124 if (h->branch_info->srcline_to)
125 hists__new_col_len(hists, HISTC_SRCLINE_TO,
126 strlen(h->branch_info->srcline_to));
b5387528 127 }
98a3b32c
SE
128
129 if (h->mem_info) {
98a3b32c
SE
130 if (h->mem_info->daddr.sym) {
131 symlen = (int)h->mem_info->daddr.sym->namelen + 4
132 + unresolved_col_width + 2;
133 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
134 symlen);
9b32ba71
DZ
135 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
136 symlen + 1);
98a3b32c
SE
137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
0805909f
JO
141 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
142 symlen);
98a3b32c 143 }
b34b3bf0
JO
144
145 if (h->mem_info->iaddr.sym) {
146 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
147 + unresolved_col_width + 2;
148 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
149 symlen);
150 } else {
151 symlen = unresolved_col_width + 4 + 2;
152 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
153 symlen);
154 }
155
98a3b32c
SE
156 if (h->mem_info->daddr.map) {
157 symlen = dso__name_len(h->mem_info->daddr.map->dso);
158 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
159 symlen);
160 } else {
161 symlen = unresolved_col_width + 4 + 2;
162 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
163 }
164 } else {
165 symlen = unresolved_col_width + 4 + 2;
166 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
b34b3bf0 167 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
98a3b32c
SE
168 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
169 }
170
a4978eca 171 hists__new_col_len(hists, HISTC_CPU, 3);
2e7ea3ab 172 hists__new_col_len(hists, HISTC_SOCKET, 6);
98a3b32c
SE
173 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
174 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
175 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
176 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
177 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
178 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
475eeab9 179
e8e6d37e
ACM
180 if (h->srcline)
181 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
182
31191a85
AK
183 if (h->srcfile)
184 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
185
475eeab9
AK
186 if (h->transaction)
187 hists__new_col_len(hists, HISTC_TRANSACTION,
188 hist_entry__transaction_len());
0c0af78d
NK
189
190 if (h->trace_output)
191 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
8a6c5b26
ACM
192}
193
7ccf4f90
NK
194void hists__output_recalc_col_len(struct hists *hists, int max_rows)
195{
196 struct rb_node *next = rb_first(&hists->entries);
197 struct hist_entry *n;
198 int row = 0;
199
200 hists__reset_col_len(hists);
201
202 while (next && row++ < max_rows) {
203 n = rb_entry(next, struct hist_entry, rb_node);
204 if (!n->filtered)
205 hists__calc_col_len(hists, n);
206 next = rb_next(&n->rb_node);
207 }
208}
209
f39056f9
NK
210static void he_stat__add_cpumode_period(struct he_stat *he_stat,
211 unsigned int cpumode, u64 period)
a1645ce1 212{
28e2a106 213 switch (cpumode) {
a1645ce1 214 case PERF_RECORD_MISC_KERNEL:
f39056f9 215 he_stat->period_sys += period;
a1645ce1
ZY
216 break;
217 case PERF_RECORD_MISC_USER:
f39056f9 218 he_stat->period_us += period;
a1645ce1
ZY
219 break;
220 case PERF_RECORD_MISC_GUEST_KERNEL:
f39056f9 221 he_stat->period_guest_sys += period;
a1645ce1
ZY
222 break;
223 case PERF_RECORD_MISC_GUEST_USER:
f39056f9 224 he_stat->period_guest_us += period;
a1645ce1
ZY
225 break;
226 default:
227 break;
228 }
229}
230
05484298
AK
231static void he_stat__add_period(struct he_stat *he_stat, u64 period,
232 u64 weight)
139c0815 233{
98a3b32c 234
139c0815 235 he_stat->period += period;
05484298 236 he_stat->weight += weight;
139c0815
NK
237 he_stat->nr_events += 1;
238}
239
240static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
241{
242 dest->period += src->period;
243 dest->period_sys += src->period_sys;
244 dest->period_us += src->period_us;
245 dest->period_guest_sys += src->period_guest_sys;
246 dest->period_guest_us += src->period_guest_us;
247 dest->nr_events += src->nr_events;
05484298 248 dest->weight += src->weight;
139c0815
NK
249}
250
f39056f9 251static void he_stat__decay(struct he_stat *he_stat)
ab81f3fd 252{
f39056f9
NK
253 he_stat->period = (he_stat->period * 7) / 8;
254 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
05484298 255 /* XXX need decay for weight too? */
ab81f3fd
ACM
256}
257
5d8200ae
NK
258static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
259
ab81f3fd
ACM
260static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
261{
b24c28f7 262 u64 prev_period = he->stat.period;
3186b681 263 u64 diff;
c64550cf
ACM
264
265 if (prev_period == 0)
df71d95f 266 return true;
c64550cf 267
f39056f9 268 he_stat__decay(&he->stat);
f8be1c8c
NK
269 if (symbol_conf.cumulate_callchain)
270 he_stat__decay(he->stat_acc);
42b276a2 271 decay_callchain(he->callchain);
c64550cf 272
3186b681
NK
273 diff = prev_period - he->stat.period;
274
5d8200ae
NK
275 if (!he->depth) {
276 hists->stats.total_period -= diff;
277 if (!he->filtered)
278 hists->stats.total_non_filtered_period -= diff;
279 }
280
281 if (!he->leaf) {
282 struct hist_entry *child;
283 struct rb_node *node = rb_first(&he->hroot_out);
284 while (node) {
285 child = rb_entry(node, struct hist_entry, rb_node);
286 node = rb_next(node);
287
288 if (hists__decay_entry(hists, child))
289 hists__delete_entry(hists, child);
290 }
291 }
c64550cf 292
b24c28f7 293 return he->stat.period == 0;
ab81f3fd
ACM
294}
295
956b65e1
ACM
296static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
297{
5d8200ae
NK
298 struct rb_root *root_in;
299 struct rb_root *root_out;
300
301 if (he->parent_he) {
302 root_in = &he->parent_he->hroot_in;
303 root_out = &he->parent_he->hroot_out;
304 } else {
52225036 305 if (hists__has(hists, need_collapse))
5d8200ae
NK
306 root_in = &hists->entries_collapsed;
307 else
308 root_in = hists->entries_in;
309 root_out = &hists->entries;
310 }
956b65e1 311
5d8200ae
NK
312 rb_erase(&he->rb_node_in, root_in);
313 rb_erase(&he->rb_node, root_out);
956b65e1
ACM
314
315 --hists->nr_entries;
316 if (!he->filtered)
317 --hists->nr_non_filtered_entries;
318
319 hist_entry__delete(he);
320}
321
3a5714f8 322void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
ab81f3fd
ACM
323{
324 struct rb_node *next = rb_first(&hists->entries);
325 struct hist_entry *n;
326
327 while (next) {
328 n = rb_entry(next, struct hist_entry, rb_node);
329 next = rb_next(&n->rb_node);
b079d4e9
ACM
330 if (((zap_user && n->level == '.') ||
331 (zap_kernel && n->level != '.') ||
4c47f4fc 332 hists__decay_entry(hists, n))) {
956b65e1 333 hists__delete_entry(hists, n);
ab81f3fd
ACM
334 }
335 }
336}
337
701937bd
NK
338void hists__delete_entries(struct hists *hists)
339{
340 struct rb_node *next = rb_first(&hists->entries);
341 struct hist_entry *n;
342
343 while (next) {
344 n = rb_entry(next, struct hist_entry, rb_node);
345 next = rb_next(&n->rb_node);
346
956b65e1 347 hists__delete_entry(hists, n);
701937bd
NK
348 }
349}
350
3d1d07ec 351/*
c82ee828 352 * histogram, sorted on item, collects periods
3d1d07ec
JK
353 */
354
a0b51af3
NK
355static struct hist_entry *hist_entry__new(struct hist_entry *template,
356 bool sample_self)
28e2a106 357{
f8be1c8c
NK
358 size_t callchain_size = 0;
359 struct hist_entry *he;
360
82aa019e 361 if (symbol_conf.use_callchain)
f8be1c8c
NK
362 callchain_size = sizeof(struct callchain_root);
363
364 he = zalloc(sizeof(*he) + callchain_size);
28e2a106 365
12c14278
ACM
366 if (he != NULL) {
367 *he = *template;
c4b35351 368
f8be1c8c
NK
369 if (symbol_conf.cumulate_callchain) {
370 he->stat_acc = malloc(sizeof(he->stat));
371 if (he->stat_acc == NULL) {
372 free(he);
373 return NULL;
374 }
375 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
a0b51af3
NK
376 if (!sample_self)
377 memset(&he->stat, 0, sizeof(he->stat));
f8be1c8c
NK
378 }
379
5c24b67a 380 map__get(he->ms.map);
3cf0cb1f
SE
381
382 if (he->branch_info) {
26353a61
NK
383 /*
384 * This branch info is (a part of) allocated from
644f2df2 385 * sample__resolve_bstack() and will be freed after
26353a61
NK
386 * adding new entries. So we need to save a copy.
387 */
388 he->branch_info = malloc(sizeof(*he->branch_info));
389 if (he->branch_info == NULL) {
5c24b67a 390 map__zput(he->ms.map);
f8be1c8c 391 free(he->stat_acc);
26353a61
NK
392 free(he);
393 return NULL;
394 }
395
396 memcpy(he->branch_info, template->branch_info,
397 sizeof(*he->branch_info));
398
5c24b67a
ACM
399 map__get(he->branch_info->from.map);
400 map__get(he->branch_info->to.map);
3cf0cb1f
SE
401 }
402
98a3b32c 403 if (he->mem_info) {
5c24b67a
ACM
404 map__get(he->mem_info->iaddr.map);
405 map__get(he->mem_info->daddr.map);
98a3b32c
SE
406 }
407
28e2a106 408 if (symbol_conf.use_callchain)
12c14278 409 callchain_init(he->callchain);
b821c732 410
72392834
NK
411 if (he->raw_data) {
412 he->raw_data = memdup(he->raw_data, he->raw_size);
413
414 if (he->raw_data == NULL) {
415 map__put(he->ms.map);
416 if (he->branch_info) {
417 map__put(he->branch_info->from.map);
418 map__put(he->branch_info->to.map);
419 free(he->branch_info);
420 }
421 if (he->mem_info) {
422 map__put(he->mem_info->iaddr.map);
423 map__put(he->mem_info->daddr.map);
424 }
425 free(he->stat_acc);
426 free(he);
427 return NULL;
428 }
429 }
b821c732 430 INIT_LIST_HEAD(&he->pairs.node);
f3b623b8 431 thread__get(he->thread);
aef810ec
NK
432
433 if (!symbol_conf.report_hierarchy)
434 he->leaf = true;
28e2a106
ACM
435 }
436
12c14278 437 return he;
28e2a106
ACM
438}
439
7a007ca9
ACM
440static u8 symbol__parent_filter(const struct symbol *parent)
441{
442 if (symbol_conf.exclude_other && parent == NULL)
443 return 1 << HIST_FILTER__PARENT;
444 return 0;
445}
446
467ef10c
NK
447static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
448{
449 if (!symbol_conf.use_callchain)
450 return;
451
452 he->hists->callchain_period += period;
453 if (!he->filtered)
454 he->hists->callchain_non_filtered_period += period;
455}
456
e7e0efcd
ACM
457static struct hist_entry *hists__findnew_entry(struct hists *hists,
458 struct hist_entry *entry,
459 struct addr_location *al,
460 bool sample_self)
9735abf1 461{
1980c2eb 462 struct rb_node **p;
9735abf1
ACM
463 struct rb_node *parent = NULL;
464 struct hist_entry *he;
354cc40e 465 int64_t cmp;
f1cbf78d
NK
466 u64 period = entry->stat.period;
467 u64 weight = entry->stat.weight;
9735abf1 468
1980c2eb
ACM
469 p = &hists->entries_in->rb_node;
470
9735abf1
ACM
471 while (*p != NULL) {
472 parent = *p;
1980c2eb 473 he = rb_entry(parent, struct hist_entry, rb_node_in);
9735abf1 474
9afcf930
NK
475 /*
476 * Make sure that it receives arguments in a same order as
477 * hist_entry__collapse() so that we can use an appropriate
478 * function when searching an entry regardless which sort
479 * keys were used.
480 */
481 cmp = hist_entry__cmp(he, entry);
9735abf1
ACM
482
483 if (!cmp) {
0f58474e 484 if (sample_self) {
a0b51af3 485 he_stat__add_period(&he->stat, period, weight);
467ef10c 486 hist_entry__add_callchain_period(he, period);
0f58474e 487 }
f8be1c8c
NK
488 if (symbol_conf.cumulate_callchain)
489 he_stat__add_period(he->stat_acc, period, weight);
63fa471d 490
ceb2acbc 491 /*
e80faac0 492 * This mem info was allocated from sample__resolve_mem
ceb2acbc
NK
493 * and will not be used anymore.
494 */
74cf249d 495 zfree(&entry->mem_info);
ceb2acbc 496
63fa471d
DM
497 /* If the map of an existing hist_entry has
498 * become out-of-date due to an exec() or
499 * similar, update it. Otherwise we will
500 * mis-adjust symbol addresses when computing
501 * the history counter to increment.
502 */
503 if (he->ms.map != entry->ms.map) {
5c24b67a
ACM
504 map__put(he->ms.map);
505 he->ms.map = map__get(entry->ms.map);
63fa471d 506 }
28e2a106 507 goto out;
9735abf1
ACM
508 }
509
510 if (cmp < 0)
511 p = &(*p)->rb_left;
512 else
513 p = &(*p)->rb_right;
514 }
515
a0b51af3 516 he = hist_entry__new(entry, sample_self);
9735abf1 517 if (!he)
27a0dcb7 518 return NULL;
1980c2eb 519
0f58474e 520 if (sample_self)
467ef10c
NK
521 hist_entry__add_callchain_period(he, period);
522 hists->nr_entries++;
590cd344 523
1980c2eb
ACM
524 rb_link_node(&he->rb_node_in, parent, p);
525 rb_insert_color(&he->rb_node_in, hists->entries_in);
28e2a106 526out:
a0b51af3
NK
527 if (sample_self)
528 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
f8be1c8c
NK
529 if (symbol_conf.cumulate_callchain)
530 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
9735abf1
ACM
531 return he;
532}
533
0102ef3e
JO
534struct hist_entry *hists__add_entry(struct hists *hists,
535 struct addr_location *al,
536 struct symbol *sym_parent,
537 struct branch_info *bi,
538 struct mem_info *mi,
539 struct perf_sample *sample,
540 bool sample_self)
b5387528
RAV
541{
542 struct hist_entry entry = {
543 .thread = al->thread,
4dfced35 544 .comm = thread__comm(al->thread),
b5387528
RAV
545 .ms = {
546 .map = al->map,
547 .sym = al->sym,
548 },
0c4c4deb 549 .socket = al->socket,
7365be55
DZ
550 .cpu = al->cpu,
551 .cpumode = al->cpumode,
552 .ip = al->addr,
553 .level = al->level,
b24c28f7 554 .stat = {
c4b35351 555 .nr_events = 1,
fd36f3dd
NK
556 .period = sample->period,
557 .weight = sample->weight,
b24c28f7 558 },
b5387528 559 .parent = sym_parent,
2c86c7ca 560 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
c824c433 561 .hists = hists,
41a4e6e2
NK
562 .branch_info = bi,
563 .mem_info = mi,
fd36f3dd 564 .transaction = sample->transaction,
72392834
NK
565 .raw_data = sample->raw_data,
566 .raw_size = sample->raw_size,
b5387528
RAV
567 };
568
e7e0efcd 569 return hists__findnew_entry(hists, &entry, al, sample_self);
b5387528
RAV
570}
571
69bcb019
NK
572static int
573iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
574 struct addr_location *al __maybe_unused)
575{
576 return 0;
577}
578
579static int
580iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
581 struct addr_location *al __maybe_unused)
582{
583 return 0;
584}
585
586static int
587iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
588{
589 struct perf_sample *sample = iter->sample;
590 struct mem_info *mi;
591
592 mi = sample__resolve_mem(sample, al);
593 if (mi == NULL)
594 return -ENOMEM;
595
596 iter->priv = mi;
597 return 0;
598}
599
600static int
601iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
602{
603 u64 cost;
604 struct mem_info *mi = iter->priv;
4ea062ed 605 struct hists *hists = evsel__hists(iter->evsel);
fd36f3dd 606 struct perf_sample *sample = iter->sample;
69bcb019
NK
607 struct hist_entry *he;
608
609 if (mi == NULL)
610 return -EINVAL;
611
fd36f3dd 612 cost = sample->weight;
69bcb019
NK
613 if (!cost)
614 cost = 1;
615
616 /*
617 * must pass period=weight in order to get the correct
618 * sorting from hists__collapse_resort() which is solely
619 * based on periods. We want sorting be done on nr_events * weight
620 * and this is indirectly achieved by passing period=weight here
621 * and the he_stat__add_period() function.
622 */
fd36f3dd
NK
623 sample->period = cost;
624
0102ef3e
JO
625 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
626 sample, true);
69bcb019
NK
627 if (!he)
628 return -ENOMEM;
629
630 iter->he = he;
631 return 0;
632}
633
634static int
9d3c02d7
NK
635iter_finish_mem_entry(struct hist_entry_iter *iter,
636 struct addr_location *al __maybe_unused)
69bcb019
NK
637{
638 struct perf_evsel *evsel = iter->evsel;
4ea062ed 639 struct hists *hists = evsel__hists(evsel);
69bcb019 640 struct hist_entry *he = iter->he;
69bcb019
NK
641 int err = -EINVAL;
642
643 if (he == NULL)
644 goto out;
645
4ea062ed 646 hists__inc_nr_samples(hists, he->filtered);
69bcb019
NK
647
648 err = hist_entry__append_callchain(he, iter->sample);
649
650out:
651 /*
e7e0efcd
ACM
652 * We don't need to free iter->priv (mem_info) here since the mem info
653 * was either already freed in hists__findnew_entry() or passed to a
654 * new hist entry by hist_entry__new().
69bcb019
NK
655 */
656 iter->priv = NULL;
657
658 iter->he = NULL;
659 return err;
660}
661
662static int
663iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
664{
665 struct branch_info *bi;
666 struct perf_sample *sample = iter->sample;
667
668 bi = sample__resolve_bstack(sample, al);
669 if (!bi)
670 return -ENOMEM;
671
672 iter->curr = 0;
673 iter->total = sample->branch_stack->nr;
674
675 iter->priv = bi;
676 return 0;
677}
678
679static int
b8f8eb84 680iter_add_single_branch_entry(struct hist_entry_iter *iter,
69bcb019
NK
681 struct addr_location *al __maybe_unused)
682{
9d3c02d7
NK
683 /* to avoid calling callback function */
684 iter->he = NULL;
685
69bcb019
NK
686 return 0;
687}
688
689static int
690iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
691{
692 struct branch_info *bi = iter->priv;
693 int i = iter->curr;
694
695 if (bi == NULL)
696 return 0;
697
698 if (iter->curr >= iter->total)
699 return 0;
700
701 al->map = bi[i].to.map;
702 al->sym = bi[i].to.sym;
703 al->addr = bi[i].to.addr;
704 return 1;
705}
706
707static int
708iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
709{
9d3c02d7 710 struct branch_info *bi;
69bcb019 711 struct perf_evsel *evsel = iter->evsel;
4ea062ed 712 struct hists *hists = evsel__hists(evsel);
fd36f3dd 713 struct perf_sample *sample = iter->sample;
69bcb019
NK
714 struct hist_entry *he = NULL;
715 int i = iter->curr;
716 int err = 0;
717
718 bi = iter->priv;
719
720 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
721 goto out;
722
723 /*
724 * The report shows the percentage of total branches captured
725 * and not events sampled. Thus we use a pseudo period of 1.
726 */
fd36f3dd
NK
727 sample->period = 1;
728 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
729
0102ef3e
JO
730 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
731 sample, true);
69bcb019
NK
732 if (he == NULL)
733 return -ENOMEM;
734
4ea062ed 735 hists__inc_nr_samples(hists, he->filtered);
69bcb019
NK
736
737out:
738 iter->he = he;
739 iter->curr++;
740 return err;
741}
742
743static int
744iter_finish_branch_entry(struct hist_entry_iter *iter,
745 struct addr_location *al __maybe_unused)
746{
747 zfree(&iter->priv);
748 iter->he = NULL;
749
750 return iter->curr >= iter->total ? 0 : -1;
751}
752
753static int
754iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
755 struct addr_location *al __maybe_unused)
756{
757 return 0;
758}
759
760static int
761iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
762{
763 struct perf_evsel *evsel = iter->evsel;
764 struct perf_sample *sample = iter->sample;
765 struct hist_entry *he;
766
0102ef3e
JO
767 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
768 sample, true);
69bcb019
NK
769 if (he == NULL)
770 return -ENOMEM;
771
772 iter->he = he;
773 return 0;
774}
775
776static int
9d3c02d7
NK
777iter_finish_normal_entry(struct hist_entry_iter *iter,
778 struct addr_location *al __maybe_unused)
69bcb019 779{
69bcb019
NK
780 struct hist_entry *he = iter->he;
781 struct perf_evsel *evsel = iter->evsel;
782 struct perf_sample *sample = iter->sample;
783
784 if (he == NULL)
785 return 0;
786
787 iter->he = NULL;
788
4ea062ed 789 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
69bcb019
NK
790
791 return hist_entry__append_callchain(he, sample);
792}
793
7a13aa28 794static int
96b40f3c 795iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
7a13aa28
NK
796 struct addr_location *al __maybe_unused)
797{
b4d3c8bd
NK
798 struct hist_entry **he_cache;
799
7a13aa28 800 callchain_cursor_commit(&callchain_cursor);
b4d3c8bd
NK
801
802 /*
803 * This is for detecting cycles or recursions so that they're
804 * cumulated only one time to prevent entries more than 100%
805 * overhead.
806 */
96b40f3c 807 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
b4d3c8bd
NK
808 if (he_cache == NULL)
809 return -ENOMEM;
810
811 iter->priv = he_cache;
812 iter->curr = 0;
813
7a13aa28
NK
814 return 0;
815}
816
817static int
818iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
819 struct addr_location *al)
820{
821 struct perf_evsel *evsel = iter->evsel;
4ea062ed 822 struct hists *hists = evsel__hists(evsel);
7a13aa28 823 struct perf_sample *sample = iter->sample;
b4d3c8bd 824 struct hist_entry **he_cache = iter->priv;
7a13aa28
NK
825 struct hist_entry *he;
826 int err = 0;
827
0102ef3e
JO
828 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
829 sample, true);
7a13aa28
NK
830 if (he == NULL)
831 return -ENOMEM;
832
833 iter->he = he;
b4d3c8bd 834 he_cache[iter->curr++] = he;
7a13aa28 835
82aa019e 836 hist_entry__append_callchain(he, sample);
be7f855a
NK
837
838 /*
839 * We need to re-initialize the cursor since callchain_append()
840 * advanced the cursor to the end.
841 */
842 callchain_cursor_commit(&callchain_cursor);
843
4ea062ed 844 hists__inc_nr_samples(hists, he->filtered);
7a13aa28
NK
845
846 return err;
847}
848
849static int
850iter_next_cumulative_entry(struct hist_entry_iter *iter,
851 struct addr_location *al)
852{
853 struct callchain_cursor_node *node;
854
855 node = callchain_cursor_current(&callchain_cursor);
856 if (node == NULL)
857 return 0;
858
c7405d85 859 return fill_callchain_info(al, node, iter->hide_unresolved);
7a13aa28
NK
860}
861
862static int
863iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
864 struct addr_location *al)
865{
866 struct perf_evsel *evsel = iter->evsel;
867 struct perf_sample *sample = iter->sample;
b4d3c8bd 868 struct hist_entry **he_cache = iter->priv;
7a13aa28 869 struct hist_entry *he;
b4d3c8bd 870 struct hist_entry he_tmp = {
5cef8976 871 .hists = evsel__hists(evsel),
b4d3c8bd
NK
872 .cpu = al->cpu,
873 .thread = al->thread,
874 .comm = thread__comm(al->thread),
875 .ip = al->addr,
876 .ms = {
877 .map = al->map,
878 .sym = al->sym,
879 },
880 .parent = iter->parent,
72392834
NK
881 .raw_data = sample->raw_data,
882 .raw_size = sample->raw_size,
b4d3c8bd
NK
883 };
884 int i;
be7f855a
NK
885 struct callchain_cursor cursor;
886
887 callchain_cursor_snapshot(&cursor, &callchain_cursor);
888
889 callchain_cursor_advance(&callchain_cursor);
b4d3c8bd
NK
890
891 /*
892 * Check if there's duplicate entries in the callchain.
893 * It's possible that it has cycles or recursive calls.
894 */
895 for (i = 0; i < iter->curr; i++) {
9d3c02d7
NK
896 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
897 /* to avoid calling callback function */
898 iter->he = NULL;
b4d3c8bd 899 return 0;
9d3c02d7 900 }
b4d3c8bd 901 }
7a13aa28 902
0102ef3e
JO
903 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
904 sample, false);
7a13aa28
NK
905 if (he == NULL)
906 return -ENOMEM;
907
908 iter->he = he;
b4d3c8bd 909 he_cache[iter->curr++] = he;
7a13aa28 910
82aa019e
NK
911 if (symbol_conf.use_callchain)
912 callchain_append(he->callchain, &cursor, sample->period);
7a13aa28
NK
913 return 0;
914}
915
916static int
917iter_finish_cumulative_entry(struct hist_entry_iter *iter,
918 struct addr_location *al __maybe_unused)
919{
b4d3c8bd 920 zfree(&iter->priv);
7a13aa28 921 iter->he = NULL;
b4d3c8bd 922
7a13aa28
NK
923 return 0;
924}
925
69bcb019
NK
926const struct hist_iter_ops hist_iter_mem = {
927 .prepare_entry = iter_prepare_mem_entry,
928 .add_single_entry = iter_add_single_mem_entry,
929 .next_entry = iter_next_nop_entry,
930 .add_next_entry = iter_add_next_nop_entry,
931 .finish_entry = iter_finish_mem_entry,
932};
933
934const struct hist_iter_ops hist_iter_branch = {
935 .prepare_entry = iter_prepare_branch_entry,
936 .add_single_entry = iter_add_single_branch_entry,
937 .next_entry = iter_next_branch_entry,
938 .add_next_entry = iter_add_next_branch_entry,
939 .finish_entry = iter_finish_branch_entry,
940};
941
942const struct hist_iter_ops hist_iter_normal = {
943 .prepare_entry = iter_prepare_normal_entry,
944 .add_single_entry = iter_add_single_normal_entry,
945 .next_entry = iter_next_nop_entry,
946 .add_next_entry = iter_add_next_nop_entry,
947 .finish_entry = iter_finish_normal_entry,
948};
949
7a13aa28
NK
950const struct hist_iter_ops hist_iter_cumulative = {
951 .prepare_entry = iter_prepare_cumulative_entry,
952 .add_single_entry = iter_add_single_cumulative_entry,
953 .next_entry = iter_next_cumulative_entry,
954 .add_next_entry = iter_add_next_cumulative_entry,
955 .finish_entry = iter_finish_cumulative_entry,
956};
957
69bcb019 958int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
9d3c02d7 959 int max_stack_depth, void *arg)
69bcb019
NK
960{
961 int err, err2;
962
91d7b2de 963 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
063bd936 964 iter->evsel, al, max_stack_depth);
69bcb019
NK
965 if (err)
966 return err;
967
96b40f3c
AH
968 iter->max_stack = max_stack_depth;
969
69bcb019
NK
970 err = iter->ops->prepare_entry(iter, al);
971 if (err)
972 goto out;
973
974 err = iter->ops->add_single_entry(iter, al);
975 if (err)
976 goto out;
977
9d3c02d7
NK
978 if (iter->he && iter->add_entry_cb) {
979 err = iter->add_entry_cb(iter, al, true, arg);
980 if (err)
981 goto out;
982 }
983
69bcb019
NK
984 while (iter->ops->next_entry(iter, al)) {
985 err = iter->ops->add_next_entry(iter, al);
986 if (err)
987 break;
9d3c02d7
NK
988
989 if (iter->he && iter->add_entry_cb) {
990 err = iter->add_entry_cb(iter, al, false, arg);
991 if (err)
992 goto out;
993 }
69bcb019
NK
994 }
995
996out:
997 err2 = iter->ops->finish_entry(iter, al);
998 if (!err)
999 err = err2;
1000
1001 return err;
1002}
1003
3d1d07ec
JK
1004int64_t
1005hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1006{
aa6f50af 1007 struct hists *hists = left->hists;
093f0ef3 1008 struct perf_hpp_fmt *fmt;
3d1d07ec
JK
1009 int64_t cmp = 0;
1010
aa6f50af 1011 hists__for_each_sort_list(hists, fmt) {
84b6ee8e
NK
1012 if (perf_hpp__is_dynamic_entry(fmt) &&
1013 !perf_hpp__defined_dynamic_entry(fmt, hists))
1014 continue;
1015
87bbdf76 1016 cmp = fmt->cmp(fmt, left, right);
3d1d07ec
JK
1017 if (cmp)
1018 break;
1019 }
1020
1021 return cmp;
1022}
1023
1024int64_t
1025hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1026{
aa6f50af 1027 struct hists *hists = left->hists;
093f0ef3 1028 struct perf_hpp_fmt *fmt;
3d1d07ec
JK
1029 int64_t cmp = 0;
1030
aa6f50af 1031 hists__for_each_sort_list(hists, fmt) {
84b6ee8e
NK
1032 if (perf_hpp__is_dynamic_entry(fmt) &&
1033 !perf_hpp__defined_dynamic_entry(fmt, hists))
1034 continue;
1035
87bbdf76 1036 cmp = fmt->collapse(fmt, left, right);
3d1d07ec
JK
1037 if (cmp)
1038 break;
1039 }
1040
1041 return cmp;
1042}
1043
6733d1bf 1044void hist_entry__delete(struct hist_entry *he)
3d1d07ec 1045{
f3b623b8 1046 thread__zput(he->thread);
5c24b67a
ACM
1047 map__zput(he->ms.map);
1048
1049 if (he->branch_info) {
1050 map__zput(he->branch_info->from.map);
1051 map__zput(he->branch_info->to.map);
508be0df
AK
1052 free_srcline(he->branch_info->srcline_from);
1053 free_srcline(he->branch_info->srcline_to);
5c24b67a
ACM
1054 zfree(&he->branch_info);
1055 }
1056
1057 if (he->mem_info) {
1058 map__zput(he->mem_info->iaddr.map);
1059 map__zput(he->mem_info->daddr.map);
1060 zfree(&he->mem_info);
1061 }
1062
f8be1c8c 1063 zfree(&he->stat_acc);
f048d548 1064 free_srcline(he->srcline);
31191a85
AK
1065 if (he->srcfile && he->srcfile[0])
1066 free(he->srcfile);
d114960c 1067 free_callchain(he->callchain);
60517d28 1068 free(he->trace_output);
72392834 1069 free(he->raw_data);
3d1d07ec
JK
1070 free(he);
1071}
1072
89fee709
ACM
1073/*
1074 * If this is not the last column, then we need to pad it according to the
1075 * pre-calculated max lenght for this column, otherwise don't bother adding
1076 * spaces because that would break viewing this with, for instance, 'less',
1077 * that would show tons of trailing spaces when a long C++ demangled method
1078 * names is sampled.
1079*/
1080int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1081 struct perf_hpp_fmt *fmt, int printed)
1082{
1083 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
da1b0407 1084 const int width = fmt->width(fmt, hpp, he->hists);
89fee709
ACM
1085 if (printed < width) {
1086 advance_hpp(hpp, printed);
1087 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1088 }
1089 }
1090
1091 return printed;
1092}
1093
3d1d07ec
JK
1094/*
1095 * collapse the histogram
1096 */
1097
aef810ec 1098static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
aec13a7e
NK
1099static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1100 enum hist_filter type);
1101
1102typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1103
1104static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1105{
1106 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1107}
1108
1109static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1110 enum hist_filter type,
1111 fmt_chk_fn check)
1112{
1113 struct perf_hpp_fmt *fmt;
1114 bool type_match = false;
1115 struct hist_entry *parent = he->parent_he;
1116
1117 switch (type) {
1118 case HIST_FILTER__THREAD:
1119 if (symbol_conf.comm_list == NULL &&
1120 symbol_conf.pid_list == NULL &&
1121 symbol_conf.tid_list == NULL)
1122 return;
1123 break;
1124 case HIST_FILTER__DSO:
1125 if (symbol_conf.dso_list == NULL)
1126 return;
1127 break;
1128 case HIST_FILTER__SYMBOL:
1129 if (symbol_conf.sym_list == NULL)
1130 return;
1131 break;
1132 case HIST_FILTER__PARENT:
1133 case HIST_FILTER__GUEST:
1134 case HIST_FILTER__HOST:
1135 case HIST_FILTER__SOCKET:
1136 default:
1137 return;
1138 }
1139
1140 /* if it's filtered by own fmt, it has to have filter bits */
1141 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1142 if (check(fmt)) {
1143 type_match = true;
1144 break;
1145 }
1146 }
1147
1148 if (type_match) {
1149 /*
1150 * If the filter is for current level entry, propagate
1151 * filter marker to parents. The marker bit was
1152 * already set by default so it only needs to clear
1153 * non-filtered entries.
1154 */
1155 if (!(he->filtered & (1 << type))) {
1156 while (parent) {
1157 parent->filtered &= ~(1 << type);
1158 parent = parent->parent_he;
1159 }
1160 }
1161 } else {
1162 /*
1163 * If current entry doesn't have matching formats, set
1164 * filter marker for upper level entries. it will be
1165 * cleared if its lower level entries is not filtered.
1166 *
1167 * For lower-level entries, it inherits parent's
1168 * filter bit so that lower level entries of a
1169 * non-filtered entry won't set the filter marker.
1170 */
1171 if (parent == NULL)
1172 he->filtered |= (1 << type);
1173 else
1174 he->filtered |= (parent->filtered & (1 << type));
1175 }
1176}
1177
1178static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1179{
1180 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1181 check_thread_entry);
1182
1183 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1184 perf_hpp__is_dso_entry);
1185
1186 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1187 perf_hpp__is_sym_entry);
1188
1189 hists__apply_filters(he->hists, he);
1190}
aef810ec
NK
1191
1192static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1193 struct rb_root *root,
1194 struct hist_entry *he,
aec13a7e 1195 struct hist_entry *parent_he,
1b2dbbf4 1196 struct perf_hpp_list *hpp_list)
aef810ec
NK
1197{
1198 struct rb_node **p = &root->rb_node;
1199 struct rb_node *parent = NULL;
1200 struct hist_entry *iter, *new;
1b2dbbf4 1201 struct perf_hpp_fmt *fmt;
aef810ec
NK
1202 int64_t cmp;
1203
1204 while (*p != NULL) {
1205 parent = *p;
1206 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1207
1b2dbbf4
NK
1208 cmp = 0;
1209 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1210 cmp = fmt->collapse(fmt, iter, he);
1211 if (cmp)
1212 break;
1213 }
1214
aef810ec
NK
1215 if (!cmp) {
1216 he_stat__add_stat(&iter->stat, &he->stat);
1217 return iter;
1218 }
1219
1220 if (cmp < 0)
1221 p = &parent->rb_left;
1222 else
1223 p = &parent->rb_right;
1224 }
1225
1226 new = hist_entry__new(he, true);
1227 if (new == NULL)
1228 return NULL;
1229
aef810ec
NK
1230 hists->nr_entries++;
1231
1b2dbbf4
NK
1232 /* save related format list for output */
1233 new->hpp_list = hpp_list;
aec13a7e
NK
1234 new->parent_he = parent_he;
1235
1236 hist_entry__apply_hierarchy_filters(new);
aef810ec
NK
1237
1238 /* some fields are now passed to 'new' */
1b2dbbf4
NK
1239 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1240 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1241 he->trace_output = NULL;
1242 else
1243 new->trace_output = NULL;
aef810ec 1244
1b2dbbf4
NK
1245 if (perf_hpp__is_srcline_entry(fmt))
1246 he->srcline = NULL;
1247 else
1248 new->srcline = NULL;
aef810ec 1249
1b2dbbf4
NK
1250 if (perf_hpp__is_srcfile_entry(fmt))
1251 he->srcfile = NULL;
1252 else
1253 new->srcfile = NULL;
1254 }
aef810ec
NK
1255
1256 rb_link_node(&new->rb_node_in, parent, p);
1257 rb_insert_color(&new->rb_node_in, root);
1258 return new;
1259}
1260
1261static int hists__hierarchy_insert_entry(struct hists *hists,
1262 struct rb_root *root,
1263 struct hist_entry *he)
1264{
1b2dbbf4 1265 struct perf_hpp_list_node *node;
aef810ec
NK
1266 struct hist_entry *new_he = NULL;
1267 struct hist_entry *parent = NULL;
1268 int depth = 0;
1269 int ret = 0;
1270
1b2dbbf4
NK
1271 list_for_each_entry(node, &hists->hpp_formats, list) {
1272 /* skip period (overhead) and elided columns */
1273 if (node->level == 0 || node->skip)
aef810ec
NK
1274 continue;
1275
1276 /* insert copy of 'he' for each fmt into the hierarchy */
aec13a7e 1277 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
aef810ec
NK
1278 if (new_he == NULL) {
1279 ret = -1;
1280 break;
1281 }
1282
1283 root = &new_he->hroot_in;
aef810ec
NK
1284 new_he->depth = depth++;
1285 parent = new_he;
1286 }
1287
1288 if (new_he) {
1289 new_he->leaf = true;
1290
1291 if (symbol_conf.use_callchain) {
1292 callchain_cursor_reset(&callchain_cursor);
1293 if (callchain_merge(&callchain_cursor,
1294 new_he->callchain,
1295 he->callchain) < 0)
1296 ret = -1;
1297 }
1298 }
1299
1300 /* 'he' is no longer used */
1301 hist_entry__delete(he);
1302
1303 /* return 0 (or -1) since it already applied filters */
1304 return ret;
1305}
1306
592dac6f
JO
1307static int hists__collapse_insert_entry(struct hists *hists,
1308 struct rb_root *root,
1309 struct hist_entry *he)
3d1d07ec 1310{
b9bf0892 1311 struct rb_node **p = &root->rb_node;
3d1d07ec
JK
1312 struct rb_node *parent = NULL;
1313 struct hist_entry *iter;
1314 int64_t cmp;
1315
aef810ec
NK
1316 if (symbol_conf.report_hierarchy)
1317 return hists__hierarchy_insert_entry(hists, root, he);
1318
3d1d07ec
JK
1319 while (*p != NULL) {
1320 parent = *p;
1980c2eb 1321 iter = rb_entry(parent, struct hist_entry, rb_node_in);
3d1d07ec
JK
1322
1323 cmp = hist_entry__collapse(iter, he);
1324
1325 if (!cmp) {
bba58cdf
NK
1326 int ret = 0;
1327
139c0815 1328 he_stat__add_stat(&iter->stat, &he->stat);
f8be1c8c
NK
1329 if (symbol_conf.cumulate_callchain)
1330 he_stat__add_stat(iter->stat_acc, he->stat_acc);
9ec60972 1331
1b3a0e95 1332 if (symbol_conf.use_callchain) {
47260645 1333 callchain_cursor_reset(&callchain_cursor);
bba58cdf
NK
1334 if (callchain_merge(&callchain_cursor,
1335 iter->callchain,
1336 he->callchain) < 0)
1337 ret = -1;
1b3a0e95 1338 }
6733d1bf 1339 hist_entry__delete(he);
bba58cdf 1340 return ret;
3d1d07ec
JK
1341 }
1342
1343 if (cmp < 0)
1344 p = &(*p)->rb_left;
1345 else
1346 p = &(*p)->rb_right;
1347 }
740b97f9 1348 hists->nr_entries++;
3d1d07ec 1349
1980c2eb
ACM
1350 rb_link_node(&he->rb_node_in, parent, p);
1351 rb_insert_color(&he->rb_node_in, root);
bba58cdf 1352 return 1;
3d1d07ec
JK
1353}
1354
fc284be9 1355struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
3d1d07ec 1356{
1980c2eb
ACM
1357 struct rb_root *root;
1358
1359 pthread_mutex_lock(&hists->lock);
1360
1361 root = hists->entries_in;
1362 if (++hists->entries_in > &hists->entries_in_array[1])
1363 hists->entries_in = &hists->entries_in_array[0];
1364
1365 pthread_mutex_unlock(&hists->lock);
1366
1367 return root;
1368}
1369
90cf1fb5
ACM
1370static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1371{
1372 hists__filter_entry_by_dso(hists, he);
1373 hists__filter_entry_by_thread(hists, he);
e94d53eb 1374 hists__filter_entry_by_symbol(hists, he);
21394d94 1375 hists__filter_entry_by_socket(hists, he);
90cf1fb5
ACM
1376}
1377
bba58cdf 1378int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1980c2eb
ACM
1379{
1380 struct rb_root *root;
3d1d07ec
JK
1381 struct rb_node *next;
1382 struct hist_entry *n;
bba58cdf 1383 int ret;
3d1d07ec 1384
52225036 1385 if (!hists__has(hists, need_collapse))
bba58cdf 1386 return 0;
3d1d07ec 1387
740b97f9
NK
1388 hists->nr_entries = 0;
1389
1980c2eb 1390 root = hists__get_rotate_entries_in(hists);
740b97f9 1391
1980c2eb 1392 next = rb_first(root);
b9bf0892 1393
3d1d07ec 1394 while (next) {
33e940a2
ACM
1395 if (session_done())
1396 break;
1980c2eb
ACM
1397 n = rb_entry(next, struct hist_entry, rb_node_in);
1398 next = rb_next(&n->rb_node_in);
3d1d07ec 1399
1980c2eb 1400 rb_erase(&n->rb_node_in, root);
bba58cdf
NK
1401 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1402 if (ret < 0)
1403 return -1;
1404
1405 if (ret) {
90cf1fb5
ACM
1406 /*
1407 * If it wasn't combined with one of the entries already
1408 * collapsed, we need to apply the filters that may have
1409 * been set by, say, the hist_browser.
1410 */
1411 hists__apply_filters(hists, n);
90cf1fb5 1412 }
c1fb5651
NK
1413 if (prog)
1414 ui_progress__update(prog, 1);
3d1d07ec 1415 }
bba58cdf 1416 return 0;
1980c2eb 1417}
b9bf0892 1418
043ca389 1419static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
29d720ed 1420{
aa6f50af 1421 struct hists *hists = a->hists;
043ca389
NK
1422 struct perf_hpp_fmt *fmt;
1423 int64_t cmp = 0;
29d720ed 1424
aa6f50af 1425 hists__for_each_sort_list(hists, fmt) {
361459f1 1426 if (perf_hpp__should_skip(fmt, a->hists))
e67d49a7
NK
1427 continue;
1428
87bbdf76 1429 cmp = fmt->sort(fmt, a, b);
043ca389 1430 if (cmp)
29d720ed
NK
1431 break;
1432 }
1433
043ca389 1434 return cmp;
29d720ed
NK
1435}
1436
9283ba9b
NK
1437static void hists__reset_filter_stats(struct hists *hists)
1438{
1439 hists->nr_non_filtered_entries = 0;
1440 hists->stats.total_non_filtered_period = 0;
1441}
1442
1443void hists__reset_stats(struct hists *hists)
1444{
1445 hists->nr_entries = 0;
1446 hists->stats.total_period = 0;
1447
1448 hists__reset_filter_stats(hists);
1449}
1450
1451static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1452{
1453 hists->nr_non_filtered_entries++;
1454 hists->stats.total_non_filtered_period += h->stat.period;
1455}
1456
1457void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1458{
1459 if (!h->filtered)
1460 hists__inc_filter_stats(hists, h);
1461
1462 hists->nr_entries++;
1463 hists->stats.total_period += h->stat.period;
1464}
1465
f7fb538a
NK
1466static void hierarchy_recalc_total_periods(struct hists *hists)
1467{
1468 struct rb_node *node;
1469 struct hist_entry *he;
1470
1471 node = rb_first(&hists->entries);
1472
1473 hists->stats.total_period = 0;
1474 hists->stats.total_non_filtered_period = 0;
1475
1476 /*
1477 * recalculate total period using top-level entries only
1478 * since lower level entries only see non-filtered entries
1479 * but upper level entries have sum of both entries.
1480 */
1481 while (node) {
1482 he = rb_entry(node, struct hist_entry, rb_node);
1483 node = rb_next(node);
1484
1485 hists->stats.total_period += he->stat.period;
1486 if (!he->filtered)
1487 hists->stats.total_non_filtered_period += he->stat.period;
1488 }
1489}
1490
1a3906a7
NK
1491static void hierarchy_insert_output_entry(struct rb_root *root,
1492 struct hist_entry *he)
1493{
1494 struct rb_node **p = &root->rb_node;
1495 struct rb_node *parent = NULL;
1496 struct hist_entry *iter;
1b2dbbf4 1497 struct perf_hpp_fmt *fmt;
1a3906a7
NK
1498
1499 while (*p != NULL) {
1500 parent = *p;
1501 iter = rb_entry(parent, struct hist_entry, rb_node);
1502
1503 if (hist_entry__sort(he, iter) > 0)
1504 p = &parent->rb_left;
1505 else
1506 p = &parent->rb_right;
1507 }
1508
1509 rb_link_node(&he->rb_node, parent, p);
1510 rb_insert_color(&he->rb_node, root);
abab5e7f
NK
1511
1512 /* update column width of dynamic entry */
1b2dbbf4
NK
1513 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1514 if (perf_hpp__is_dynamic_entry(fmt))
1515 fmt->sort(fmt, he, NULL);
1516 }
1a3906a7
NK
1517}
1518
1519static void hists__hierarchy_output_resort(struct hists *hists,
1520 struct ui_progress *prog,
1521 struct rb_root *root_in,
1522 struct rb_root *root_out,
1523 u64 min_callchain_hits,
1524 bool use_callchain)
1525{
1526 struct rb_node *node;
1527 struct hist_entry *he;
1528
1529 *root_out = RB_ROOT;
1530 node = rb_first(root_in);
1531
1532 while (node) {
1533 he = rb_entry(node, struct hist_entry, rb_node_in);
1534 node = rb_next(node);
1535
1536 hierarchy_insert_output_entry(root_out, he);
1537
1538 if (prog)
1539 ui_progress__update(prog, 1);
1540
1541 if (!he->leaf) {
1542 hists__hierarchy_output_resort(hists, prog,
1543 &he->hroot_in,
1544 &he->hroot_out,
1545 min_callchain_hits,
1546 use_callchain);
1547 hists->nr_entries++;
1548 if (!he->filtered) {
1549 hists->nr_non_filtered_entries++;
1550 hists__calc_col_len(hists, he);
1551 }
1552
1553 continue;
1554 }
1555
1a3906a7
NK
1556 if (!use_callchain)
1557 continue;
1558
1559 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1560 u64 total = he->stat.period;
1561
1562 if (symbol_conf.cumulate_callchain)
1563 total = he->stat_acc->period;
1564
1565 min_callchain_hits = total * (callchain_param.min_percent / 100);
1566 }
1567
1568 callchain_param.sort(&he->sorted_chain, he->callchain,
1569 min_callchain_hits, &callchain_param);
1570 }
1571}
1572
1c02c4d2
ACM
1573static void __hists__insert_output_entry(struct rb_root *entries,
1574 struct hist_entry *he,
f9db0d0f
KL
1575 u64 min_callchain_hits,
1576 bool use_callchain)
3d1d07ec 1577{
1c02c4d2 1578 struct rb_node **p = &entries->rb_node;
3d1d07ec
JK
1579 struct rb_node *parent = NULL;
1580 struct hist_entry *iter;
abab5e7f 1581 struct perf_hpp_fmt *fmt;
3d1d07ec 1582
744070e0
NK
1583 if (use_callchain) {
1584 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1585 u64 total = he->stat.period;
1586
1587 if (symbol_conf.cumulate_callchain)
1588 total = he->stat_acc->period;
1589
1590 min_callchain_hits = total * (callchain_param.min_percent / 100);
1591 }
b9fb9304 1592 callchain_param.sort(&he->sorted_chain, he->callchain,
3d1d07ec 1593 min_callchain_hits, &callchain_param);
744070e0 1594 }
3d1d07ec
JK
1595
1596 while (*p != NULL) {
1597 parent = *p;
1598 iter = rb_entry(parent, struct hist_entry, rb_node);
1599
043ca389 1600 if (hist_entry__sort(he, iter) > 0)
3d1d07ec
JK
1601 p = &(*p)->rb_left;
1602 else
1603 p = &(*p)->rb_right;
1604 }
1605
1606 rb_link_node(&he->rb_node, parent, p);
1c02c4d2 1607 rb_insert_color(&he->rb_node, entries);
abab5e7f
NK
1608
1609 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1610 if (perf_hpp__is_dynamic_entry(fmt) &&
1611 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1612 fmt->sort(fmt, he, NULL); /* update column width */
1613 }
3d1d07ec
JK
1614}
1615
01441af5
JO
1616static void output_resort(struct hists *hists, struct ui_progress *prog,
1617 bool use_callchain)
3d1d07ec 1618{
1980c2eb 1619 struct rb_root *root;
3d1d07ec
JK
1620 struct rb_node *next;
1621 struct hist_entry *n;
467ef10c 1622 u64 callchain_total;
3d1d07ec
JK
1623 u64 min_callchain_hits;
1624
467ef10c
NK
1625 callchain_total = hists->callchain_period;
1626 if (symbol_conf.filter_relative)
1627 callchain_total = hists->callchain_non_filtered_period;
1628
1629 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
3d1d07ec 1630
1a3906a7
NK
1631 hists__reset_stats(hists);
1632 hists__reset_col_len(hists);
1633
1634 if (symbol_conf.report_hierarchy) {
f7fb538a
NK
1635 hists__hierarchy_output_resort(hists, prog,
1636 &hists->entries_collapsed,
1637 &hists->entries,
1638 min_callchain_hits,
1639 use_callchain);
1640 hierarchy_recalc_total_periods(hists);
1641 return;
1a3906a7
NK
1642 }
1643
52225036 1644 if (hists__has(hists, need_collapse))
1980c2eb
ACM
1645 root = &hists->entries_collapsed;
1646 else
1647 root = hists->entries_in;
1648
1649 next = rb_first(root);
1650 hists->entries = RB_ROOT;
3d1d07ec
JK
1651
1652 while (next) {
1980c2eb
ACM
1653 n = rb_entry(next, struct hist_entry, rb_node_in);
1654 next = rb_next(&n->rb_node_in);
3d1d07ec 1655
f9db0d0f 1656 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
6263835a 1657 hists__inc_stats(hists, n);
ae993efc
NK
1658
1659 if (!n->filtered)
1660 hists__calc_col_len(hists, n);
740b97f9
NK
1661
1662 if (prog)
1663 ui_progress__update(prog, 1);
3d1d07ec 1664 }
1980c2eb 1665}
b9bf0892 1666
452ce03b 1667void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
01441af5 1668{
01441af5
JO
1669 bool use_callchain;
1670
1671 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1672 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1673 else
1674 use_callchain = symbol_conf.use_callchain;
1675
452ce03b
JO
1676 output_resort(evsel__hists(evsel), prog, use_callchain);
1677}
1678
1679void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1680{
1681 output_resort(hists, prog, symbol_conf.use_callchain);
01441af5
JO
1682}
1683
8c01872f
NK
1684static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1685{
1686 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1687 return false;
1688
1689 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1690 return true;
1691
1692 return false;
1693}
1694
1695struct rb_node *rb_hierarchy_last(struct rb_node *node)
1696{
1697 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1698
1699 while (can_goto_child(he, HMD_NORMAL)) {
1700 node = rb_last(&he->hroot_out);
1701 he = rb_entry(node, struct hist_entry, rb_node);
1702 }
1703 return node;
1704}
1705
1706struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1707{
1708 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1709
1710 if (can_goto_child(he, hmd))
1711 node = rb_first(&he->hroot_out);
1712 else
1713 node = rb_next(node);
1714
1715 while (node == NULL) {
1716 he = he->parent_he;
1717 if (he == NULL)
1718 break;
1719
1720 node = rb_next(&he->rb_node);
1721 }
1722 return node;
1723}
1724
1725struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1726{
1727 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1728
1729 node = rb_prev(node);
1730 if (node)
1731 return rb_hierarchy_last(node);
1732
1733 he = he->parent_he;
1734 if (he == NULL)
1735 return NULL;
1736
1737 return &he->rb_node;
1738}
1739
a7b5895b
NK
1740bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1741{
1742 struct rb_node *node;
1743 struct hist_entry *child;
1744 float percent;
1745
1746 if (he->leaf)
1747 return false;
1748
1749 node = rb_first(&he->hroot_out);
1750 child = rb_entry(node, struct hist_entry, rb_node);
1751
1752 while (node && child->filtered) {
1753 node = rb_next(node);
1754 child = rb_entry(node, struct hist_entry, rb_node);
1755 }
1756
1757 if (node)
1758 percent = hist_entry__get_percent_limit(child);
1759 else
1760 percent = 0;
1761
1762 return node && percent >= limit;
1763}
1764
42b28ac0 1765static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
cc5edb0e
ACM
1766 enum hist_filter filter)
1767{
1768 h->filtered &= ~(1 << filter);
155e9aff
NK
1769
1770 if (symbol_conf.report_hierarchy) {
1771 struct hist_entry *parent = h->parent_he;
1772
1773 while (parent) {
1774 he_stat__add_stat(&parent->stat, &h->stat);
1775
1776 parent->filtered &= ~(1 << filter);
1777
1778 if (parent->filtered)
1779 goto next;
1780
1781 /* force fold unfiltered entry for simplicity */
1782 parent->unfolded = false;
79dded87 1783 parent->has_no_entry = false;
155e9aff
NK
1784 parent->row_offset = 0;
1785 parent->nr_rows = 0;
1786next:
1787 parent = parent->parent_he;
1788 }
1789 }
1790
cc5edb0e
ACM
1791 if (h->filtered)
1792 return;
1793
87e90f43 1794 /* force fold unfiltered entry for simplicity */
3698dab1 1795 h->unfolded = false;
79dded87 1796 h->has_no_entry = false;
0f0cbf7a 1797 h->row_offset = 0;
a8cd1f43 1798 h->nr_rows = 0;
9283ba9b 1799
1ab1fa5d 1800 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
cc5edb0e 1801
9283ba9b 1802 hists__inc_filter_stats(hists, h);
42b28ac0 1803 hists__calc_col_len(hists, h);
cc5edb0e
ACM
1804}
1805
90cf1fb5
ACM
1806
1807static bool hists__filter_entry_by_dso(struct hists *hists,
1808 struct hist_entry *he)
1809{
1810 if (hists->dso_filter != NULL &&
1811 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1812 he->filtered |= (1 << HIST_FILTER__DSO);
1813 return true;
1814 }
1815
1816 return false;
1817}
1818
90cf1fb5
ACM
1819static bool hists__filter_entry_by_thread(struct hists *hists,
1820 struct hist_entry *he)
1821{
1822 if (hists->thread_filter != NULL &&
1823 he->thread != hists->thread_filter) {
1824 he->filtered |= (1 << HIST_FILTER__THREAD);
1825 return true;
1826 }
1827
1828 return false;
1829}
1830
e94d53eb
NK
1831static bool hists__filter_entry_by_symbol(struct hists *hists,
1832 struct hist_entry *he)
1833{
1834 if (hists->symbol_filter_str != NULL &&
1835 (!he->ms.sym || strstr(he->ms.sym->name,
1836 hists->symbol_filter_str) == NULL)) {
1837 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1838 return true;
1839 }
1840
1841 return false;
1842}
1843
21394d94
KL
1844static bool hists__filter_entry_by_socket(struct hists *hists,
1845 struct hist_entry *he)
1846{
1847 if ((hists->socket_filter > -1) &&
1848 (he->socket != hists->socket_filter)) {
1849 he->filtered |= (1 << HIST_FILTER__SOCKET);
1850 return true;
1851 }
1852
1853 return false;
1854}
1855
1f7c2541
NK
1856typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1857
1858static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
84734b06
KL
1859{
1860 struct rb_node *nd;
1861
1862 hists->stats.nr_non_filtered_samples = 0;
1863
1864 hists__reset_filter_stats(hists);
1865 hists__reset_col_len(hists);
1866
1867 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1868 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1869
1f7c2541 1870 if (filter(hists, h))
84734b06
KL
1871 continue;
1872
1f7c2541 1873 hists__remove_entry_filter(hists, h, type);
84734b06
KL
1874 }
1875}
1876
70642850
NK
1877static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1878{
1879 struct rb_node **p = &root->rb_node;
1880 struct rb_node *parent = NULL;
1881 struct hist_entry *iter;
1882 struct rb_root new_root = RB_ROOT;
1883 struct rb_node *nd;
1884
1885 while (*p != NULL) {
1886 parent = *p;
1887 iter = rb_entry(parent, struct hist_entry, rb_node);
1888
1889 if (hist_entry__sort(he, iter) > 0)
1890 p = &(*p)->rb_left;
1891 else
1892 p = &(*p)->rb_right;
1893 }
1894
1895 rb_link_node(&he->rb_node, parent, p);
1896 rb_insert_color(&he->rb_node, root);
1897
1898 if (he->leaf || he->filtered)
1899 return;
1900
1901 nd = rb_first(&he->hroot_out);
1902 while (nd) {
1903 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1904
1905 nd = rb_next(nd);
1906 rb_erase(&h->rb_node, &he->hroot_out);
1907
1908 resort_filtered_entry(&new_root, h);
1909 }
1910
1911 he->hroot_out = new_root;
1912}
1913
155e9aff
NK
1914static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1915{
1916 struct rb_node *nd;
70642850 1917 struct rb_root new_root = RB_ROOT;
155e9aff
NK
1918
1919 hists->stats.nr_non_filtered_samples = 0;
1920
1921 hists__reset_filter_stats(hists);
1922 hists__reset_col_len(hists);
1923
1924 nd = rb_first(&hists->entries);
1925 while (nd) {
1926 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1927 int ret;
1928
1929 ret = hist_entry__filter(h, type, arg);
1930
1931 /*
1932 * case 1. non-matching type
1933 * zero out the period, set filter marker and move to child
1934 */
1935 if (ret < 0) {
1936 memset(&h->stat, 0, sizeof(h->stat));
1937 h->filtered |= (1 << type);
1938
1939 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
1940 }
1941 /*
1942 * case 2. matched type (filter out)
1943 * set filter marker and move to next
1944 */
1945 else if (ret == 1) {
1946 h->filtered |= (1 << type);
1947
1948 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1949 }
1950 /*
1951 * case 3. ok (not filtered)
1952 * add period to hists and parents, erase the filter marker
1953 * and move to next sibling
1954 */
1955 else {
1956 hists__remove_entry_filter(hists, h, type);
1957
1958 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1959 }
1960 }
70642850 1961
f7fb538a
NK
1962 hierarchy_recalc_total_periods(hists);
1963
70642850
NK
1964 /*
1965 * resort output after applying a new filter since filter in a lower
1966 * hierarchy can change periods in a upper hierarchy.
1967 */
1968 nd = rb_first(&hists->entries);
1969 while (nd) {
1970 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1971
1972 nd = rb_next(nd);
1973 rb_erase(&h->rb_node, &hists->entries);
1974
1975 resort_filtered_entry(&new_root, h);
1976 }
1977
1978 hists->entries = new_root;
155e9aff
NK
1979}
1980
1f7c2541
NK
1981void hists__filter_by_thread(struct hists *hists)
1982{
155e9aff
NK
1983 if (symbol_conf.report_hierarchy)
1984 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
1985 hists->thread_filter);
1986 else
1987 hists__filter_by_type(hists, HIST_FILTER__THREAD,
1988 hists__filter_entry_by_thread);
1f7c2541
NK
1989}
1990
1991void hists__filter_by_dso(struct hists *hists)
1992{
155e9aff
NK
1993 if (symbol_conf.report_hierarchy)
1994 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
1995 hists->dso_filter);
1996 else
1997 hists__filter_by_type(hists, HIST_FILTER__DSO,
1998 hists__filter_entry_by_dso);
1f7c2541
NK
1999}
2000
2001void hists__filter_by_symbol(struct hists *hists)
2002{
155e9aff
NK
2003 if (symbol_conf.report_hierarchy)
2004 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2005 hists->symbol_filter_str);
2006 else
2007 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2008 hists__filter_entry_by_symbol);
1f7c2541
NK
2009}
2010
2011void hists__filter_by_socket(struct hists *hists)
2012{
155e9aff
NK
2013 if (symbol_conf.report_hierarchy)
2014 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2015 &hists->socket_filter);
2016 else
2017 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2018 hists__filter_entry_by_socket);
1f7c2541
NK
2019}
2020
28a6b6aa
ACM
2021void events_stats__inc(struct events_stats *stats, u32 type)
2022{
2023 ++stats->nr_events[0];
2024 ++stats->nr_events[type];
2025}
2026
42b28ac0 2027void hists__inc_nr_events(struct hists *hists, u32 type)
c8446b9b 2028{
28a6b6aa 2029 events_stats__inc(&hists->stats, type);
c8446b9b 2030}
95529be4 2031
1844dbcb
NK
2032void hists__inc_nr_samples(struct hists *hists, bool filtered)
2033{
2034 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2035 if (!filtered)
2036 hists->stats.nr_non_filtered_samples++;
2037}
2038
494d70a1
ACM
2039static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2040 struct hist_entry *pair)
2041{
ce74f60e
NK
2042 struct rb_root *root;
2043 struct rb_node **p;
494d70a1
ACM
2044 struct rb_node *parent = NULL;
2045 struct hist_entry *he;
354cc40e 2046 int64_t cmp;
494d70a1 2047
52225036 2048 if (hists__has(hists, need_collapse))
ce74f60e
NK
2049 root = &hists->entries_collapsed;
2050 else
2051 root = hists->entries_in;
2052
2053 p = &root->rb_node;
2054
494d70a1
ACM
2055 while (*p != NULL) {
2056 parent = *p;
ce74f60e 2057 he = rb_entry(parent, struct hist_entry, rb_node_in);
494d70a1 2058
ce74f60e 2059 cmp = hist_entry__collapse(he, pair);
494d70a1
ACM
2060
2061 if (!cmp)
2062 goto out;
2063
2064 if (cmp < 0)
2065 p = &(*p)->rb_left;
2066 else
2067 p = &(*p)->rb_right;
2068 }
2069
a0b51af3 2070 he = hist_entry__new(pair, true);
494d70a1 2071 if (he) {
30193d78
ACM
2072 memset(&he->stat, 0, sizeof(he->stat));
2073 he->hists = hists;
09623d79
KL
2074 if (symbol_conf.cumulate_callchain)
2075 memset(he->stat_acc, 0, sizeof(he->stat));
ce74f60e
NK
2076 rb_link_node(&he->rb_node_in, parent, p);
2077 rb_insert_color(&he->rb_node_in, root);
6263835a 2078 hists__inc_stats(hists, he);
e0af43d2 2079 he->dummy = true;
494d70a1
ACM
2080 }
2081out:
2082 return he;
2083}
2084
95529be4
ACM
2085static struct hist_entry *hists__find_entry(struct hists *hists,
2086 struct hist_entry *he)
2087{
ce74f60e
NK
2088 struct rb_node *n;
2089
52225036 2090 if (hists__has(hists, need_collapse))
ce74f60e
NK
2091 n = hists->entries_collapsed.rb_node;
2092 else
2093 n = hists->entries_in->rb_node;
95529be4
ACM
2094
2095 while (n) {
ce74f60e
NK
2096 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2097 int64_t cmp = hist_entry__collapse(iter, he);
95529be4
ACM
2098
2099 if (cmp < 0)
2100 n = n->rb_left;
2101 else if (cmp > 0)
2102 n = n->rb_right;
2103 else
2104 return iter;
2105 }
2106
2107 return NULL;
2108}
2109
2110/*
2111 * Look for pairs to link to the leader buckets (hist_entries):
2112 */
2113void hists__match(struct hists *leader, struct hists *other)
2114{
ce74f60e 2115 struct rb_root *root;
95529be4
ACM
2116 struct rb_node *nd;
2117 struct hist_entry *pos, *pair;
2118
52225036 2119 if (hists__has(leader, need_collapse))
ce74f60e
NK
2120 root = &leader->entries_collapsed;
2121 else
2122 root = leader->entries_in;
2123
2124 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2125 pos = rb_entry(nd, struct hist_entry, rb_node_in);
95529be4
ACM
2126 pair = hists__find_entry(other, pos);
2127
2128 if (pair)
5fa9041b 2129 hist_entry__add_pair(pair, pos);
95529be4
ACM
2130 }
2131}
494d70a1
ACM
2132
2133/*
2134 * Look for entries in the other hists that are not present in the leader, if
2135 * we find them, just add a dummy entry on the leader hists, with period=0,
2136 * nr_events=0, to serve as the list header.
2137 */
2138int hists__link(struct hists *leader, struct hists *other)
2139{
ce74f60e 2140 struct rb_root *root;
494d70a1
ACM
2141 struct rb_node *nd;
2142 struct hist_entry *pos, *pair;
2143
52225036 2144 if (hists__has(other, need_collapse))
ce74f60e
NK
2145 root = &other->entries_collapsed;
2146 else
2147 root = other->entries_in;
2148
2149 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2150 pos = rb_entry(nd, struct hist_entry, rb_node_in);
494d70a1
ACM
2151
2152 if (!hist_entry__has_pairs(pos)) {
2153 pair = hists__add_dummy_entry(leader, pos);
2154 if (pair == NULL)
2155 return -1;
5fa9041b 2156 hist_entry__add_pair(pos, pair);
494d70a1
ACM
2157 }
2158 }
2159
2160 return 0;
2161}
f2148330 2162
57849998
AK
2163void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2164 struct perf_sample *sample, bool nonany_branch_mode)
2165{
2166 struct branch_info *bi;
2167
2168 /* If we have branch cycles always annotate them. */
2169 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2170 int i;
2171
2172 bi = sample__resolve_bstack(sample, al);
2173 if (bi) {
2174 struct addr_map_symbol *prev = NULL;
2175
2176 /*
2177 * Ignore errors, still want to process the
2178 * other entries.
2179 *
2180 * For non standard branch modes always
2181 * force no IPC (prev == NULL)
2182 *
2183 * Note that perf stores branches reversed from
2184 * program order!
2185 */
2186 for (i = bs->nr - 1; i >= 0; i--) {
2187 addr_map_symbol__account_cycles(&bi[i].from,
2188 nonany_branch_mode ? NULL : prev,
2189 bi[i].flags.cycles);
2190 prev = &bi[i].to;
2191 }
2192 free(bi);
2193 }
2194 }
2195}
2a1731fb
ACM
2196
2197size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2198{
2199 struct perf_evsel *pos;
2200 size_t ret = 0;
2201
2202 evlist__for_each(evlist, pos) {
2203 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2204 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2205 }
2206
2207 return ret;
2208}
2209
2210
f2148330
NK
2211u64 hists__total_period(struct hists *hists)
2212{
2213 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2214 hists->stats.total_period;
2215}
33db4568
NK
2216
2217int parse_filter_percentage(const struct option *opt __maybe_unused,
2218 const char *arg, int unset __maybe_unused)
2219{
2220 if (!strcmp(arg, "relative"))
2221 symbol_conf.filter_relative = true;
2222 else if (!strcmp(arg, "absolute"))
2223 symbol_conf.filter_relative = false;
2224 else
2225 return -1;
2226
2227 return 0;
2228}
0b93da17
NK
2229
2230int perf_hist_config(const char *var, const char *value)
2231{
2232 if (!strcmp(var, "hist.percentage"))
2233 return parse_filter_percentage(NULL, value, 0);
2234
2235 return 0;
2236}
a635fc51 2237
5b65855e 2238int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
a635fc51 2239{
a635fc51
ACM
2240 memset(hists, 0, sizeof(*hists));
2241 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2242 hists->entries_in = &hists->entries_in_array[0];
2243 hists->entries_collapsed = RB_ROOT;
2244 hists->entries = RB_ROOT;
2245 pthread_mutex_init(&hists->lock, NULL);
21394d94 2246 hists->socket_filter = -1;
5b65855e 2247 hists->hpp_list = hpp_list;
c3bc0c43 2248 INIT_LIST_HEAD(&hists->hpp_formats);
a635fc51
ACM
2249 return 0;
2250}
2251
61fa0e94
NK
2252static void hists__delete_remaining_entries(struct rb_root *root)
2253{
2254 struct rb_node *node;
2255 struct hist_entry *he;
2256
2257 while (!RB_EMPTY_ROOT(root)) {
2258 node = rb_first(root);
2259 rb_erase(node, root);
2260
2261 he = rb_entry(node, struct hist_entry, rb_node_in);
2262 hist_entry__delete(he);
2263 }
2264}
2265
2266static void hists__delete_all_entries(struct hists *hists)
2267{
2268 hists__delete_entries(hists);
2269 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2270 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2271 hists__delete_remaining_entries(&hists->entries_collapsed);
2272}
2273
17577dec
MH
2274static void hists_evsel__exit(struct perf_evsel *evsel)
2275{
2276 struct hists *hists = evsel__hists(evsel);
c3bc0c43
NK
2277 struct perf_hpp_fmt *fmt, *pos;
2278 struct perf_hpp_list_node *node, *tmp;
17577dec 2279
61fa0e94 2280 hists__delete_all_entries(hists);
c3bc0c43
NK
2281
2282 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2283 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2284 list_del(&fmt->list);
2285 free(fmt);
2286 }
2287 list_del(&node->list);
2288 free(node);
2289 }
17577dec
MH
2290}
2291
fc284be9
NK
2292static int hists_evsel__init(struct perf_evsel *evsel)
2293{
2294 struct hists *hists = evsel__hists(evsel);
2295
5b65855e 2296 __hists__init(hists, &perf_hpp_list);
fc284be9
NK
2297 return 0;
2298}
2299
a635fc51
ACM
2300/*
2301 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2302 * stored in the rbtree...
2303 */
2304
2305int hists__init(void)
2306{
2307 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
17577dec
MH
2308 hists_evsel__init,
2309 hists_evsel__exit);
a635fc51
ACM
2310 if (err)
2311 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2312
2313 return err;
2314}
94b3dc38
JO
2315
2316void perf_hpp_list__init(struct perf_hpp_list *list)
2317{
2318 INIT_LIST_HEAD(&list->fields);
2319 INIT_LIST_HEAD(&list->sorts);
2320}