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