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