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