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