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