perf report: Fix alignment of symbol column when -v is given
[deliverable/linux.git] / tools / perf / util / hist.c
CommitLineData
78f7defe 1#include "annotate.h"
8a0ecfb8 2#include "util.h"
598357eb 3#include "build-id.h"
3d1d07ec 4#include "hist.h"
4e4f06e4
ACM
5#include "session.h"
6#include "sort.h"
29d720ed 7#include "evsel.h"
9b33827d 8#include <math.h>
3d1d07ec 9
90cf1fb5
ACM
10static bool hists__filter_entry_by_dso(struct hists *hists,
11 struct hist_entry *he);
12static bool hists__filter_entry_by_thread(struct hists *hists,
13 struct hist_entry *he);
e94d53eb
NK
14static bool hists__filter_entry_by_symbol(struct hists *hists,
15 struct hist_entry *he);
90cf1fb5 16
7a007ca9
ACM
17enum hist_filter {
18 HIST_FILTER__DSO,
19 HIST_FILTER__THREAD,
20 HIST_FILTER__PARENT,
e94d53eb 21 HIST_FILTER__SYMBOL,
7a007ca9
ACM
22};
23
3d1d07ec
JK
24struct callchain_param callchain_param = {
25 .mode = CHAIN_GRAPH_REL,
d797fdc5
SL
26 .min_percent = 0.5,
27 .order = ORDER_CALLEE
3d1d07ec
JK
28};
29
42b28ac0 30u16 hists__col_len(struct hists *hists, enum hist_column col)
8a6c5b26 31{
42b28ac0 32 return hists->col_len[col];
8a6c5b26
ACM
33}
34
42b28ac0 35void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
8a6c5b26 36{
42b28ac0 37 hists->col_len[col] = len;
8a6c5b26
ACM
38}
39
42b28ac0 40bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
8a6c5b26 41{
42b28ac0
ACM
42 if (len > hists__col_len(hists, col)) {
43 hists__set_col_len(hists, col, len);
8a6c5b26
ACM
44 return true;
45 }
46 return false;
47}
48
7ccf4f90 49void hists__reset_col_len(struct hists *hists)
8a6c5b26
ACM
50{
51 enum hist_column col;
52
53 for (col = 0; col < HISTC_NR_COLS; ++col)
42b28ac0 54 hists__set_col_len(hists, col, 0);
8a6c5b26
ACM
55}
56
b5387528
RAV
57static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
58{
59 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
60
61 if (hists__col_len(hists, dso) < unresolved_col_width &&
62 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
63 !symbol_conf.dso_list)
64 hists__set_col_len(hists, dso, unresolved_col_width);
65}
66
7ccf4f90 67void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
8a6c5b26 68{
b5387528 69 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
98a3b32c 70 int symlen;
8a6c5b26
ACM
71 u16 len;
72
ded19d57
NK
73 /*
74 * +4 accounts for '[x] ' priv level info
75 * +2 accounts for 0x prefix on raw addresses
76 * +3 accounts for ' y ' symtab origin info
77 */
78 if (h->ms.sym) {
79 symlen = h->ms.sym->namelen + 4;
80 if (verbose)
81 symlen += BITS_PER_LONG / 4 + 2 + 3;
82 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
83 } else {
98a3b32c
SE
84 symlen = unresolved_col_width + 4 + 2;
85 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
b5387528 86 hists__set_unres_dso_col_len(hists, HISTC_DSO);
98a3b32c 87 }
8a6c5b26
ACM
88
89 len = thread__comm_len(h->thread);
42b28ac0
ACM
90 if (hists__new_col_len(hists, HISTC_COMM, len))
91 hists__set_col_len(hists, HISTC_THREAD, len + 6);
8a6c5b26
ACM
92
93 if (h->ms.map) {
94 len = dso__name_len(h->ms.map->dso);
42b28ac0 95 hists__new_col_len(hists, HISTC_DSO, len);
8a6c5b26 96 }
b5387528 97
cb993744
NK
98 if (h->parent)
99 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
100
b5387528 101 if (h->branch_info) {
b5387528
RAV
102 if (h->branch_info->from.sym) {
103 symlen = (int)h->branch_info->from.sym->namelen + 4;
ded19d57
NK
104 if (verbose)
105 symlen += BITS_PER_LONG / 4 + 2 + 3;
b5387528
RAV
106 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
107
108 symlen = dso__name_len(h->branch_info->from.map->dso);
109 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
110 } else {
111 symlen = unresolved_col_width + 4 + 2;
112 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
113 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
114 }
115
116 if (h->branch_info->to.sym) {
117 symlen = (int)h->branch_info->to.sym->namelen + 4;
ded19d57
NK
118 if (verbose)
119 symlen += BITS_PER_LONG / 4 + 2 + 3;
b5387528
RAV
120 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
121
122 symlen = dso__name_len(h->branch_info->to.map->dso);
123 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
124 } else {
125 symlen = unresolved_col_width + 4 + 2;
126 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
127 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
128 }
129 }
98a3b32c
SE
130
131 if (h->mem_info) {
98a3b32c
SE
132 if (h->mem_info->daddr.sym) {
133 symlen = (int)h->mem_info->daddr.sym->namelen + 4
134 + unresolved_col_width + 2;
135 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
136 symlen);
137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
141 }
142 if (h->mem_info->daddr.map) {
143 symlen = dso__name_len(h->mem_info->daddr.map->dso);
144 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
145 symlen);
146 } else {
147 symlen = unresolved_col_width + 4 + 2;
148 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
149 }
150 } else {
151 symlen = unresolved_col_width + 4 + 2;
152 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
154 }
155
156 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
157 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
158 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
159 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
160 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
161 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
8a6c5b26
ACM
162}
163
7ccf4f90
NK
164void hists__output_recalc_col_len(struct hists *hists, int max_rows)
165{
166 struct rb_node *next = rb_first(&hists->entries);
167 struct hist_entry *n;
168 int row = 0;
169
170 hists__reset_col_len(hists);
171
172 while (next && row++ < max_rows) {
173 n = rb_entry(next, struct hist_entry, rb_node);
174 if (!n->filtered)
175 hists__calc_col_len(hists, n);
176 next = rb_next(&n->rb_node);
177 }
178}
179
12c14278 180static void hist_entry__add_cpumode_period(struct hist_entry *he,
c82ee828 181 unsigned int cpumode, u64 period)
a1645ce1 182{
28e2a106 183 switch (cpumode) {
a1645ce1 184 case PERF_RECORD_MISC_KERNEL:
b24c28f7 185 he->stat.period_sys += period;
a1645ce1
ZY
186 break;
187 case PERF_RECORD_MISC_USER:
b24c28f7 188 he->stat.period_us += period;
a1645ce1
ZY
189 break;
190 case PERF_RECORD_MISC_GUEST_KERNEL:
b24c28f7 191 he->stat.period_guest_sys += period;
a1645ce1
ZY
192 break;
193 case PERF_RECORD_MISC_GUEST_USER:
b24c28f7 194 he->stat.period_guest_us += period;
a1645ce1
ZY
195 break;
196 default:
197 break;
198 }
199}
200
05484298
AK
201static void he_stat__add_period(struct he_stat *he_stat, u64 period,
202 u64 weight)
139c0815 203{
98a3b32c 204
139c0815 205 he_stat->period += period;
05484298 206 he_stat->weight += weight;
139c0815
NK
207 he_stat->nr_events += 1;
208}
209
210static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
211{
212 dest->period += src->period;
213 dest->period_sys += src->period_sys;
214 dest->period_us += src->period_us;
215 dest->period_guest_sys += src->period_guest_sys;
216 dest->period_guest_us += src->period_guest_us;
217 dest->nr_events += src->nr_events;
05484298 218 dest->weight += src->weight;
139c0815
NK
219}
220
ab81f3fd
ACM
221static void hist_entry__decay(struct hist_entry *he)
222{
b24c28f7
NK
223 he->stat.period = (he->stat.period * 7) / 8;
224 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
05484298 225 /* XXX need decay for weight too? */
ab81f3fd
ACM
226}
227
228static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
229{
b24c28f7 230 u64 prev_period = he->stat.period;
c64550cf
ACM
231
232 if (prev_period == 0)
df71d95f 233 return true;
c64550cf 234
ab81f3fd 235 hist_entry__decay(he);
c64550cf
ACM
236
237 if (!he->filtered)
b24c28f7 238 hists->stats.total_period -= prev_period - he->stat.period;
c64550cf 239
b24c28f7 240 return he->stat.period == 0;
ab81f3fd
ACM
241}
242
b079d4e9
ACM
243static void __hists__decay_entries(struct hists *hists, bool zap_user,
244 bool zap_kernel, bool threaded)
ab81f3fd
ACM
245{
246 struct rb_node *next = rb_first(&hists->entries);
247 struct hist_entry *n;
248
249 while (next) {
250 n = rb_entry(next, struct hist_entry, rb_node);
251 next = rb_next(&n->rb_node);
df71d95f
ACM
252 /*
253 * We may be annotating this, for instance, so keep it here in
254 * case some it gets new samples, we'll eventually free it when
255 * the user stops browsing and it agains gets fully decayed.
256 */
b079d4e9
ACM
257 if (((zap_user && n->level == '.') ||
258 (zap_kernel && n->level != '.') ||
259 hists__decay_entry(hists, n)) &&
260 !n->used) {
ab81f3fd
ACM
261 rb_erase(&n->rb_node, &hists->entries);
262
e345fa18 263 if (sort__need_collapse || threaded)
ab81f3fd
ACM
264 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
265
266 hist_entry__free(n);
267 --hists->nr_entries;
268 }
269 }
270}
271
b079d4e9 272void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
e345fa18 273{
b079d4e9 274 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
e345fa18
ACM
275}
276
b079d4e9
ACM
277void hists__decay_entries_threaded(struct hists *hists,
278 bool zap_user, bool zap_kernel)
e345fa18 279{
b079d4e9 280 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
e345fa18
ACM
281}
282
3d1d07ec 283/*
c82ee828 284 * histogram, sorted on item, collects periods
3d1d07ec
JK
285 */
286
28e2a106
ACM
287static struct hist_entry *hist_entry__new(struct hist_entry *template)
288{
d2009c51 289 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
98a3b32c 290 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
28e2a106 291
12c14278
ACM
292 if (he != NULL) {
293 *he = *template;
c4b35351 294
12c14278
ACM
295 if (he->ms.map)
296 he->ms.map->referenced = true;
3cf0cb1f
SE
297
298 if (he->branch_info) {
26353a61
NK
299 /*
300 * This branch info is (a part of) allocated from
301 * machine__resolve_bstack() and will be freed after
302 * adding new entries. So we need to save a copy.
303 */
304 he->branch_info = malloc(sizeof(*he->branch_info));
305 if (he->branch_info == NULL) {
306 free(he);
307 return NULL;
308 }
309
310 memcpy(he->branch_info, template->branch_info,
311 sizeof(*he->branch_info));
312
3cf0cb1f
SE
313 if (he->branch_info->from.map)
314 he->branch_info->from.map->referenced = true;
315 if (he->branch_info->to.map)
316 he->branch_info->to.map->referenced = true;
317 }
318
98a3b32c
SE
319 if (he->mem_info) {
320 if (he->mem_info->iaddr.map)
321 he->mem_info->iaddr.map->referenced = true;
322 if (he->mem_info->daddr.map)
323 he->mem_info->daddr.map->referenced = true;
324 }
325
28e2a106 326 if (symbol_conf.use_callchain)
12c14278 327 callchain_init(he->callchain);
b821c732
ACM
328
329 INIT_LIST_HEAD(&he->pairs.node);
28e2a106
ACM
330 }
331
12c14278 332 return he;
28e2a106
ACM
333}
334
66f97ed3 335void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
fefb0b94 336{
8a6c5b26 337 if (!h->filtered) {
42b28ac0
ACM
338 hists__calc_col_len(hists, h);
339 ++hists->nr_entries;
b24c28f7 340 hists->stats.total_period += h->stat.period;
8a6c5b26 341 }
fefb0b94
ACM
342}
343
7a007ca9
ACM
344static u8 symbol__parent_filter(const struct symbol *parent)
345{
346 if (symbol_conf.exclude_other && parent == NULL)
347 return 1 << HIST_FILTER__PARENT;
348 return 0;
349}
350
b5387528
RAV
351static struct hist_entry *add_hist_entry(struct hists *hists,
352 struct hist_entry *entry,
1c02c4d2 353 struct addr_location *al,
05484298
AK
354 u64 period,
355 u64 weight)
9735abf1 356{
1980c2eb 357 struct rb_node **p;
9735abf1
ACM
358 struct rb_node *parent = NULL;
359 struct hist_entry *he;
9735abf1
ACM
360 int cmp;
361
1980c2eb
ACM
362 pthread_mutex_lock(&hists->lock);
363
364 p = &hists->entries_in->rb_node;
365
9735abf1
ACM
366 while (*p != NULL) {
367 parent = *p;
1980c2eb 368 he = rb_entry(parent, struct hist_entry, rb_node_in);
9735abf1 369
9afcf930
NK
370 /*
371 * Make sure that it receives arguments in a same order as
372 * hist_entry__collapse() so that we can use an appropriate
373 * function when searching an entry regardless which sort
374 * keys were used.
375 */
376 cmp = hist_entry__cmp(he, entry);
9735abf1
ACM
377
378 if (!cmp) {
05484298 379 he_stat__add_period(&he->stat, period, weight);
63fa471d 380
ceb2acbc
NK
381 /*
382 * This mem info was allocated from machine__resolve_mem
383 * and will not be used anymore.
384 */
385 free(entry->mem_info);
386
63fa471d
DM
387 /* If the map of an existing hist_entry has
388 * become out-of-date due to an exec() or
389 * similar, update it. Otherwise we will
390 * mis-adjust symbol addresses when computing
391 * the history counter to increment.
392 */
393 if (he->ms.map != entry->ms.map) {
394 he->ms.map = entry->ms.map;
395 if (he->ms.map)
396 he->ms.map->referenced = true;
397 }
28e2a106 398 goto out;
9735abf1
ACM
399 }
400
401 if (cmp < 0)
402 p = &(*p)->rb_left;
403 else
404 p = &(*p)->rb_right;
405 }
406
b5387528 407 he = hist_entry__new(entry);
9735abf1 408 if (!he)
1980c2eb
ACM
409 goto out_unlock;
410
411 rb_link_node(&he->rb_node_in, parent, p);
412 rb_insert_color(&he->rb_node_in, hists->entries_in);
28e2a106 413out:
c82ee828 414 hist_entry__add_cpumode_period(he, al->cpumode, period);
1980c2eb
ACM
415out_unlock:
416 pthread_mutex_unlock(&hists->lock);
9735abf1
ACM
417 return he;
418}
419
98a3b32c
SE
420struct hist_entry *__hists__add_mem_entry(struct hists *self,
421 struct addr_location *al,
422 struct symbol *sym_parent,
423 struct mem_info *mi,
424 u64 period,
425 u64 weight)
426{
427 struct hist_entry entry = {
428 .thread = al->thread,
429 .ms = {
430 .map = al->map,
431 .sym = al->sym,
432 },
433 .stat = {
434 .period = period,
435 .weight = weight,
436 .nr_events = 1,
437 },
438 .cpu = al->cpu,
439 .ip = al->addr,
440 .level = al->level,
441 .parent = sym_parent,
442 .filtered = symbol__parent_filter(sym_parent),
443 .hists = self,
444 .mem_info = mi,
445 .branch_info = NULL,
446 };
447 return add_hist_entry(self, &entry, al, period, weight);
448}
449
b5387528
RAV
450struct hist_entry *__hists__add_branch_entry(struct hists *self,
451 struct addr_location *al,
452 struct symbol *sym_parent,
453 struct branch_info *bi,
05484298
AK
454 u64 period,
455 u64 weight)
b5387528
RAV
456{
457 struct hist_entry entry = {
458 .thread = al->thread,
459 .ms = {
460 .map = bi->to.map,
461 .sym = bi->to.sym,
462 },
463 .cpu = al->cpu,
464 .ip = bi->to.addr,
465 .level = al->level,
b24c28f7
NK
466 .stat = {
467 .period = period,
c4b35351 468 .nr_events = 1,
05484298 469 .weight = weight,
b24c28f7 470 },
b5387528
RAV
471 .parent = sym_parent,
472 .filtered = symbol__parent_filter(sym_parent),
473 .branch_info = bi,
ae359f19 474 .hists = self,
98a3b32c 475 .mem_info = NULL,
b5387528
RAV
476 };
477
05484298 478 return add_hist_entry(self, &entry, al, period, weight);
b5387528
RAV
479}
480
481struct hist_entry *__hists__add_entry(struct hists *self,
482 struct addr_location *al,
05484298
AK
483 struct symbol *sym_parent, u64 period,
484 u64 weight)
b5387528
RAV
485{
486 struct hist_entry entry = {
487 .thread = al->thread,
488 .ms = {
489 .map = al->map,
490 .sym = al->sym,
491 },
492 .cpu = al->cpu,
493 .ip = al->addr,
494 .level = al->level,
b24c28f7
NK
495 .stat = {
496 .period = period,
c4b35351 497 .nr_events = 1,
05484298 498 .weight = weight,
b24c28f7 499 },
b5387528
RAV
500 .parent = sym_parent,
501 .filtered = symbol__parent_filter(sym_parent),
ae359f19 502 .hists = self,
98a3b32c
SE
503 .branch_info = NULL,
504 .mem_info = NULL,
b5387528
RAV
505 };
506
05484298 507 return add_hist_entry(self, &entry, al, period, weight);
b5387528
RAV
508}
509
3d1d07ec
JK
510int64_t
511hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
512{
513 struct sort_entry *se;
514 int64_t cmp = 0;
515
516 list_for_each_entry(se, &hist_entry__sort_list, list) {
fcd14984 517 cmp = se->se_cmp(left, right);
3d1d07ec
JK
518 if (cmp)
519 break;
520 }
521
522 return cmp;
523}
524
525int64_t
526hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
527{
528 struct sort_entry *se;
529 int64_t cmp = 0;
530
531 list_for_each_entry(se, &hist_entry__sort_list, list) {
532 int64_t (*f)(struct hist_entry *, struct hist_entry *);
533
fcd14984 534 f = se->se_collapse ?: se->se_cmp;
3d1d07ec
JK
535
536 cmp = f(left, right);
537 if (cmp)
538 break;
539 }
540
541 return cmp;
542}
543
544void hist_entry__free(struct hist_entry *he)
545{
580e338d 546 free(he->branch_info);
028f12ee 547 free(he->mem_info);
3d1d07ec
JK
548 free(he);
549}
550
551/*
552 * collapse the histogram
553 */
554
1d037ca1 555static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
1b3a0e95
FW
556 struct rb_root *root,
557 struct hist_entry *he)
3d1d07ec 558{
b9bf0892 559 struct rb_node **p = &root->rb_node;
3d1d07ec
JK
560 struct rb_node *parent = NULL;
561 struct hist_entry *iter;
562 int64_t cmp;
563
564 while (*p != NULL) {
565 parent = *p;
1980c2eb 566 iter = rb_entry(parent, struct hist_entry, rb_node_in);
3d1d07ec
JK
567
568 cmp = hist_entry__collapse(iter, he);
569
570 if (!cmp) {
139c0815 571 he_stat__add_stat(&iter->stat, &he->stat);
9ec60972 572
1b3a0e95 573 if (symbol_conf.use_callchain) {
47260645
NK
574 callchain_cursor_reset(&callchain_cursor);
575 callchain_merge(&callchain_cursor,
576 iter->callchain,
1b3a0e95
FW
577 he->callchain);
578 }
3d1d07ec 579 hist_entry__free(he);
fefb0b94 580 return false;
3d1d07ec
JK
581 }
582
583 if (cmp < 0)
584 p = &(*p)->rb_left;
585 else
586 p = &(*p)->rb_right;
587 }
588
1980c2eb
ACM
589 rb_link_node(&he->rb_node_in, parent, p);
590 rb_insert_color(&he->rb_node_in, root);
fefb0b94 591 return true;
3d1d07ec
JK
592}
593
1980c2eb 594static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
3d1d07ec 595{
1980c2eb
ACM
596 struct rb_root *root;
597
598 pthread_mutex_lock(&hists->lock);
599
600 root = hists->entries_in;
601 if (++hists->entries_in > &hists->entries_in_array[1])
602 hists->entries_in = &hists->entries_in_array[0];
603
604 pthread_mutex_unlock(&hists->lock);
605
606 return root;
607}
608
90cf1fb5
ACM
609static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
610{
611 hists__filter_entry_by_dso(hists, he);
612 hists__filter_entry_by_thread(hists, he);
e94d53eb 613 hists__filter_entry_by_symbol(hists, he);
90cf1fb5
ACM
614}
615
1980c2eb
ACM
616static void __hists__collapse_resort(struct hists *hists, bool threaded)
617{
618 struct rb_root *root;
3d1d07ec
JK
619 struct rb_node *next;
620 struct hist_entry *n;
621
1980c2eb 622 if (!sort__need_collapse && !threaded)
3d1d07ec
JK
623 return;
624
1980c2eb
ACM
625 root = hists__get_rotate_entries_in(hists);
626 next = rb_first(root);
b9bf0892 627
3d1d07ec 628 while (next) {
1980c2eb
ACM
629 n = rb_entry(next, struct hist_entry, rb_node_in);
630 next = rb_next(&n->rb_node_in);
3d1d07ec 631
1980c2eb 632 rb_erase(&n->rb_node_in, root);
90cf1fb5
ACM
633 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
634 /*
635 * If it wasn't combined with one of the entries already
636 * collapsed, we need to apply the filters that may have
637 * been set by, say, the hist_browser.
638 */
639 hists__apply_filters(hists, n);
90cf1fb5 640 }
3d1d07ec 641 }
1980c2eb 642}
b9bf0892 643
1980c2eb
ACM
644void hists__collapse_resort(struct hists *hists)
645{
646 return __hists__collapse_resort(hists, false);
647}
648
649void hists__collapse_resort_threaded(struct hists *hists)
650{
651 return __hists__collapse_resort(hists, true);
3d1d07ec
JK
652}
653
654/*
c82ee828 655 * reverse the map, sort on period.
3d1d07ec
JK
656 */
657
29d720ed
NK
658static int period_cmp(u64 period_a, u64 period_b)
659{
660 if (period_a > period_b)
661 return 1;
662 if (period_a < period_b)
663 return -1;
664 return 0;
665}
666
667static int hist_entry__sort_on_period(struct hist_entry *a,
668 struct hist_entry *b)
669{
670 int ret;
671 int i, nr_members;
672 struct perf_evsel *evsel;
673 struct hist_entry *pair;
674 u64 *periods_a, *periods_b;
675
676 ret = period_cmp(a->stat.period, b->stat.period);
677 if (ret || !symbol_conf.event_group)
678 return ret;
679
680 evsel = hists_to_evsel(a->hists);
681 nr_members = evsel->nr_members;
682 if (nr_members <= 1)
683 return ret;
684
685 periods_a = zalloc(sizeof(periods_a) * nr_members);
686 periods_b = zalloc(sizeof(periods_b) * nr_members);
687
688 if (!periods_a || !periods_b)
689 goto out;
690
691 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
692 evsel = hists_to_evsel(pair->hists);
693 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
694 }
695
696 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
697 evsel = hists_to_evsel(pair->hists);
698 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
699 }
700
701 for (i = 1; i < nr_members; i++) {
702 ret = period_cmp(periods_a[i], periods_b[i]);
703 if (ret)
704 break;
705 }
706
707out:
708 free(periods_a);
709 free(periods_b);
710
711 return ret;
712}
713
1c02c4d2
ACM
714static void __hists__insert_output_entry(struct rb_root *entries,
715 struct hist_entry *he,
716 u64 min_callchain_hits)
3d1d07ec 717{
1c02c4d2 718 struct rb_node **p = &entries->rb_node;
3d1d07ec
JK
719 struct rb_node *parent = NULL;
720 struct hist_entry *iter;
721
d599db3f 722 if (symbol_conf.use_callchain)
b9fb9304 723 callchain_param.sort(&he->sorted_chain, he->callchain,
3d1d07ec
JK
724 min_callchain_hits, &callchain_param);
725
726 while (*p != NULL) {
727 parent = *p;
728 iter = rb_entry(parent, struct hist_entry, rb_node);
729
29d720ed 730 if (hist_entry__sort_on_period(he, iter) > 0)
3d1d07ec
JK
731 p = &(*p)->rb_left;
732 else
733 p = &(*p)->rb_right;
734 }
735
736 rb_link_node(&he->rb_node, parent, p);
1c02c4d2 737 rb_insert_color(&he->rb_node, entries);
3d1d07ec
JK
738}
739
1980c2eb 740static void __hists__output_resort(struct hists *hists, bool threaded)
3d1d07ec 741{
1980c2eb 742 struct rb_root *root;
3d1d07ec
JK
743 struct rb_node *next;
744 struct hist_entry *n;
3d1d07ec
JK
745 u64 min_callchain_hits;
746
42b28ac0 747 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
3d1d07ec 748
1980c2eb
ACM
749 if (sort__need_collapse || threaded)
750 root = &hists->entries_collapsed;
751 else
752 root = hists->entries_in;
753
754 next = rb_first(root);
755 hists->entries = RB_ROOT;
3d1d07ec 756
42b28ac0 757 hists->nr_entries = 0;
7928631a 758 hists->stats.total_period = 0;
42b28ac0 759 hists__reset_col_len(hists);
fefb0b94 760
3d1d07ec 761 while (next) {
1980c2eb
ACM
762 n = rb_entry(next, struct hist_entry, rb_node_in);
763 next = rb_next(&n->rb_node_in);
3d1d07ec 764
1980c2eb 765 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
42b28ac0 766 hists__inc_nr_entries(hists, n);
3d1d07ec 767 }
1980c2eb 768}
b9bf0892 769
1980c2eb
ACM
770void hists__output_resort(struct hists *hists)
771{
772 return __hists__output_resort(hists, false);
773}
774
775void hists__output_resort_threaded(struct hists *hists)
776{
777 return __hists__output_resort(hists, true);
3d1d07ec 778}
4ecf84d0 779
42b28ac0 780static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
cc5edb0e
ACM
781 enum hist_filter filter)
782{
783 h->filtered &= ~(1 << filter);
784 if (h->filtered)
785 return;
786
42b28ac0 787 ++hists->nr_entries;
0f0cbf7a 788 if (h->ms.unfolded)
42b28ac0 789 hists->nr_entries += h->nr_rows;
0f0cbf7a 790 h->row_offset = 0;
b24c28f7
NK
791 hists->stats.total_period += h->stat.period;
792 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
cc5edb0e 793
42b28ac0 794 hists__calc_col_len(hists, h);
cc5edb0e
ACM
795}
796
90cf1fb5
ACM
797
798static bool hists__filter_entry_by_dso(struct hists *hists,
799 struct hist_entry *he)
800{
801 if (hists->dso_filter != NULL &&
802 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
803 he->filtered |= (1 << HIST_FILTER__DSO);
804 return true;
805 }
806
807 return false;
808}
809
d7b76f09 810void hists__filter_by_dso(struct hists *hists)
b09e0190
ACM
811{
812 struct rb_node *nd;
813
42b28ac0
ACM
814 hists->nr_entries = hists->stats.total_period = 0;
815 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
816 hists__reset_col_len(hists);
b09e0190 817
42b28ac0 818 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
b09e0190
ACM
819 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
820
821 if (symbol_conf.exclude_other && !h->parent)
822 continue;
823
90cf1fb5 824 if (hists__filter_entry_by_dso(hists, h))
b09e0190 825 continue;
b09e0190 826
42b28ac0 827 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
b09e0190
ACM
828 }
829}
830
90cf1fb5
ACM
831static bool hists__filter_entry_by_thread(struct hists *hists,
832 struct hist_entry *he)
833{
834 if (hists->thread_filter != NULL &&
835 he->thread != hists->thread_filter) {
836 he->filtered |= (1 << HIST_FILTER__THREAD);
837 return true;
838 }
839
840 return false;
841}
842
d7b76f09 843void hists__filter_by_thread(struct hists *hists)
b09e0190
ACM
844{
845 struct rb_node *nd;
846
42b28ac0
ACM
847 hists->nr_entries = hists->stats.total_period = 0;
848 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
849 hists__reset_col_len(hists);
b09e0190 850
42b28ac0 851 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
b09e0190
ACM
852 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
853
90cf1fb5 854 if (hists__filter_entry_by_thread(hists, h))
b09e0190 855 continue;
cc5edb0e 856
42b28ac0 857 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
b09e0190
ACM
858 }
859}
ef7b93a1 860
e94d53eb
NK
861static bool hists__filter_entry_by_symbol(struct hists *hists,
862 struct hist_entry *he)
863{
864 if (hists->symbol_filter_str != NULL &&
865 (!he->ms.sym || strstr(he->ms.sym->name,
866 hists->symbol_filter_str) == NULL)) {
867 he->filtered |= (1 << HIST_FILTER__SYMBOL);
868 return true;
869 }
870
871 return false;
872}
873
874void hists__filter_by_symbol(struct hists *hists)
875{
876 struct rb_node *nd;
877
878 hists->nr_entries = hists->stats.total_period = 0;
879 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
880 hists__reset_col_len(hists);
881
882 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
883 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
884
885 if (hists__filter_entry_by_symbol(hists, h))
886 continue;
887
888 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
889 }
890}
891
2f525d01 892int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
ef7b93a1 893{
2f525d01 894 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
ef7b93a1
ACM
895}
896
ce6f4fab 897int hist_entry__annotate(struct hist_entry *he, size_t privsize)
ef7b93a1 898{
ce6f4fab 899 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
ef7b93a1 900}
c8446b9b 901
28a6b6aa
ACM
902void events_stats__inc(struct events_stats *stats, u32 type)
903{
904 ++stats->nr_events[0];
905 ++stats->nr_events[type];
906}
907
42b28ac0 908void hists__inc_nr_events(struct hists *hists, u32 type)
c8446b9b 909{
28a6b6aa 910 events_stats__inc(&hists->stats, type);
c8446b9b 911}
95529be4 912
494d70a1
ACM
913static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
914 struct hist_entry *pair)
915{
ce74f60e
NK
916 struct rb_root *root;
917 struct rb_node **p;
494d70a1
ACM
918 struct rb_node *parent = NULL;
919 struct hist_entry *he;
920 int cmp;
921
ce74f60e
NK
922 if (sort__need_collapse)
923 root = &hists->entries_collapsed;
924 else
925 root = hists->entries_in;
926
927 p = &root->rb_node;
928
494d70a1
ACM
929 while (*p != NULL) {
930 parent = *p;
ce74f60e 931 he = rb_entry(parent, struct hist_entry, rb_node_in);
494d70a1 932
ce74f60e 933 cmp = hist_entry__collapse(he, pair);
494d70a1
ACM
934
935 if (!cmp)
936 goto out;
937
938 if (cmp < 0)
939 p = &(*p)->rb_left;
940 else
941 p = &(*p)->rb_right;
942 }
943
944 he = hist_entry__new(pair);
945 if (he) {
30193d78
ACM
946 memset(&he->stat, 0, sizeof(he->stat));
947 he->hists = hists;
ce74f60e
NK
948 rb_link_node(&he->rb_node_in, parent, p);
949 rb_insert_color(&he->rb_node_in, root);
494d70a1
ACM
950 hists__inc_nr_entries(hists, he);
951 }
952out:
953 return he;
954}
955
95529be4
ACM
956static struct hist_entry *hists__find_entry(struct hists *hists,
957 struct hist_entry *he)
958{
ce74f60e
NK
959 struct rb_node *n;
960
961 if (sort__need_collapse)
962 n = hists->entries_collapsed.rb_node;
963 else
964 n = hists->entries_in->rb_node;
95529be4
ACM
965
966 while (n) {
ce74f60e
NK
967 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
968 int64_t cmp = hist_entry__collapse(iter, he);
95529be4
ACM
969
970 if (cmp < 0)
971 n = n->rb_left;
972 else if (cmp > 0)
973 n = n->rb_right;
974 else
975 return iter;
976 }
977
978 return NULL;
979}
980
981/*
982 * Look for pairs to link to the leader buckets (hist_entries):
983 */
984void hists__match(struct hists *leader, struct hists *other)
985{
ce74f60e 986 struct rb_root *root;
95529be4
ACM
987 struct rb_node *nd;
988 struct hist_entry *pos, *pair;
989
ce74f60e
NK
990 if (sort__need_collapse)
991 root = &leader->entries_collapsed;
992 else
993 root = leader->entries_in;
994
995 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
996 pos = rb_entry(nd, struct hist_entry, rb_node_in);
95529be4
ACM
997 pair = hists__find_entry(other, pos);
998
999 if (pair)
5fa9041b 1000 hist_entry__add_pair(pair, pos);
95529be4
ACM
1001 }
1002}
494d70a1
ACM
1003
1004/*
1005 * Look for entries in the other hists that are not present in the leader, if
1006 * we find them, just add a dummy entry on the leader hists, with period=0,
1007 * nr_events=0, to serve as the list header.
1008 */
1009int hists__link(struct hists *leader, struct hists *other)
1010{
ce74f60e 1011 struct rb_root *root;
494d70a1
ACM
1012 struct rb_node *nd;
1013 struct hist_entry *pos, *pair;
1014
ce74f60e
NK
1015 if (sort__need_collapse)
1016 root = &other->entries_collapsed;
1017 else
1018 root = other->entries_in;
1019
1020 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1021 pos = rb_entry(nd, struct hist_entry, rb_node_in);
494d70a1
ACM
1022
1023 if (!hist_entry__has_pairs(pos)) {
1024 pair = hists__add_dummy_entry(leader, pos);
1025 if (pair == NULL)
1026 return -1;
5fa9041b 1027 hist_entry__add_pair(pos, pair);
494d70a1
ACM
1028 }
1029 }
1030
1031 return 0;
1032}
This page took 0.286078 seconds and 5 git commands to generate.