9 #include "ui/progress.h"
12 static bool hists__filter_entry_by_dso(struct hists
*hists
,
13 struct hist_entry
*he
);
14 static bool hists__filter_entry_by_thread(struct hists
*hists
,
15 struct hist_entry
*he
);
16 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
17 struct hist_entry
*he
);
18 static bool hists__filter_entry_by_socket(struct hists
*hists
,
19 struct hist_entry
*he
);
21 u16
hists__col_len(struct hists
*hists
, enum hist_column col
)
23 return hists
->col_len
[col
];
26 void hists__set_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
28 hists
->col_len
[col
] = len
;
31 bool hists__new_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
33 if (len
> hists__col_len(hists
, col
)) {
34 hists__set_col_len(hists
, col
, len
);
40 void hists__reset_col_len(struct hists
*hists
)
44 for (col
= 0; col
< HISTC_NR_COLS
; ++col
)
45 hists__set_col_len(hists
, col
, 0);
48 static void hists__set_unres_dso_col_len(struct hists
*hists
, int dso
)
50 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
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
);
58 void hists__calc_col_len(struct hists
*hists
, struct hist_entry
*h
)
60 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
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
70 symlen
= h
->ms
.sym
->namelen
+ 4;
72 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
73 hists__new_col_len(hists
, HISTC_SYMBOL
, symlen
);
75 symlen
= unresolved_col_width
+ 4 + 2;
76 hists__new_col_len(hists
, HISTC_SYMBOL
, symlen
);
77 hists__set_unres_dso_col_len(hists
, HISTC_DSO
);
80 len
= thread__comm_len(h
->thread
);
81 if (hists__new_col_len(hists
, HISTC_COMM
, len
))
82 hists__set_col_len(hists
, HISTC_THREAD
, len
+ 8);
85 len
= dso__name_len(h
->ms
.map
->dso
);
86 hists__new_col_len(hists
, HISTC_DSO
, len
);
90 hists__new_col_len(hists
, HISTC_PARENT
, h
->parent
->namelen
);
93 if (h
->branch_info
->from
.sym
) {
94 symlen
= (int)h
->branch_info
->from
.sym
->namelen
+ 4;
96 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
97 hists__new_col_len(hists
, HISTC_SYMBOL_FROM
, symlen
);
99 symlen
= dso__name_len(h
->branch_info
->from
.map
->dso
);
100 hists__new_col_len(hists
, HISTC_DSO_FROM
, symlen
);
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
);
107 if (h
->branch_info
->to
.sym
) {
108 symlen
= (int)h
->branch_info
->to
.sym
->namelen
+ 4;
110 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
111 hists__new_col_len(hists
, HISTC_SYMBOL_TO
, symlen
);
113 symlen
= dso__name_len(h
->branch_info
->to
.map
->dso
);
114 hists__new_col_len(hists
, HISTC_DSO_TO
, symlen
);
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
);
121 if (h
->branch_info
->srcline_from
)
122 hists__new_col_len(hists
, HISTC_SRCLINE_FROM
,
123 strlen(h
->branch_info
->srcline_from
));
124 if (h
->branch_info
->srcline_to
)
125 hists__new_col_len(hists
, HISTC_SRCLINE_TO
,
126 strlen(h
->branch_info
->srcline_to
));
130 if (h
->mem_info
->daddr
.sym
) {
131 symlen
= (int)h
->mem_info
->daddr
.sym
->namelen
+ 4
132 + unresolved_col_width
+ 2;
133 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
,
135 hists__new_col_len(hists
, HISTC_MEM_DCACHELINE
,
138 symlen
= unresolved_col_width
+ 4 + 2;
139 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
,
141 hists__new_col_len(hists
, HISTC_MEM_DCACHELINE
,
145 if (h
->mem_info
->iaddr
.sym
) {
146 symlen
= (int)h
->mem_info
->iaddr
.sym
->namelen
+ 4
147 + unresolved_col_width
+ 2;
148 hists__new_col_len(hists
, HISTC_MEM_IADDR_SYMBOL
,
151 symlen
= unresolved_col_width
+ 4 + 2;
152 hists__new_col_len(hists
, HISTC_MEM_IADDR_SYMBOL
,
156 if (h
->mem_info
->daddr
.map
) {
157 symlen
= dso__name_len(h
->mem_info
->daddr
.map
->dso
);
158 hists__new_col_len(hists
, HISTC_MEM_DADDR_DSO
,
161 symlen
= unresolved_col_width
+ 4 + 2;
162 hists__set_unres_dso_col_len(hists
, HISTC_MEM_DADDR_DSO
);
165 symlen
= unresolved_col_width
+ 4 + 2;
166 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
, symlen
);
167 hists__new_col_len(hists
, HISTC_MEM_IADDR_SYMBOL
, symlen
);
168 hists__set_unres_dso_col_len(hists
, HISTC_MEM_DADDR_DSO
);
171 hists__new_col_len(hists
, HISTC_CPU
, 3);
172 hists__new_col_len(hists
, HISTC_SOCKET
, 6);
173 hists__new_col_len(hists
, HISTC_MEM_LOCKED
, 6);
174 hists__new_col_len(hists
, HISTC_MEM_TLB
, 22);
175 hists__new_col_len(hists
, HISTC_MEM_SNOOP
, 12);
176 hists__new_col_len(hists
, HISTC_MEM_LVL
, 21 + 3);
177 hists__new_col_len(hists
, HISTC_LOCAL_WEIGHT
, 12);
178 hists__new_col_len(hists
, HISTC_GLOBAL_WEIGHT
, 12);
181 len
= MAX(strlen(h
->srcline
), strlen(sort_srcline
.se_header
));
182 hists__new_col_len(hists
, HISTC_SRCLINE
, len
);
186 hists__new_col_len(hists
, HISTC_SRCFILE
, strlen(h
->srcfile
));
189 hists__new_col_len(hists
, HISTC_TRANSACTION
,
190 hist_entry__transaction_len());
193 hists__new_col_len(hists
, HISTC_TRACE
, strlen(h
->trace_output
));
196 void hists__output_recalc_col_len(struct hists
*hists
, int max_rows
)
198 struct rb_node
*next
= rb_first(&hists
->entries
);
199 struct hist_entry
*n
;
202 hists__reset_col_len(hists
);
204 while (next
&& row
++ < max_rows
) {
205 n
= rb_entry(next
, struct hist_entry
, rb_node
);
207 hists__calc_col_len(hists
, n
);
208 next
= rb_next(&n
->rb_node
);
212 static void he_stat__add_cpumode_period(struct he_stat
*he_stat
,
213 unsigned int cpumode
, u64 period
)
216 case PERF_RECORD_MISC_KERNEL
:
217 he_stat
->period_sys
+= period
;
219 case PERF_RECORD_MISC_USER
:
220 he_stat
->period_us
+= period
;
222 case PERF_RECORD_MISC_GUEST_KERNEL
:
223 he_stat
->period_guest_sys
+= period
;
225 case PERF_RECORD_MISC_GUEST_USER
:
226 he_stat
->period_guest_us
+= period
;
233 static void he_stat__add_period(struct he_stat
*he_stat
, u64 period
,
237 he_stat
->period
+= period
;
238 he_stat
->weight
+= weight
;
239 he_stat
->nr_events
+= 1;
242 static void he_stat__add_stat(struct he_stat
*dest
, struct he_stat
*src
)
244 dest
->period
+= src
->period
;
245 dest
->period_sys
+= src
->period_sys
;
246 dest
->period_us
+= src
->period_us
;
247 dest
->period_guest_sys
+= src
->period_guest_sys
;
248 dest
->period_guest_us
+= src
->period_guest_us
;
249 dest
->nr_events
+= src
->nr_events
;
250 dest
->weight
+= src
->weight
;
253 static void he_stat__decay(struct he_stat
*he_stat
)
255 he_stat
->period
= (he_stat
->period
* 7) / 8;
256 he_stat
->nr_events
= (he_stat
->nr_events
* 7) / 8;
257 /* XXX need decay for weight too? */
260 static void hists__delete_entry(struct hists
*hists
, struct hist_entry
*he
);
262 static bool hists__decay_entry(struct hists
*hists
, struct hist_entry
*he
)
264 u64 prev_period
= he
->stat
.period
;
267 if (prev_period
== 0)
270 he_stat__decay(&he
->stat
);
271 if (symbol_conf
.cumulate_callchain
)
272 he_stat__decay(he
->stat_acc
);
273 decay_callchain(he
->callchain
);
275 diff
= prev_period
- he
->stat
.period
;
278 hists
->stats
.total_period
-= diff
;
280 hists
->stats
.total_non_filtered_period
-= diff
;
284 struct hist_entry
*child
;
285 struct rb_node
*node
= rb_first(&he
->hroot_out
);
287 child
= rb_entry(node
, struct hist_entry
, rb_node
);
288 node
= rb_next(node
);
290 if (hists__decay_entry(hists
, child
))
291 hists__delete_entry(hists
, child
);
295 return he
->stat
.period
== 0;
298 static void hists__delete_entry(struct hists
*hists
, struct hist_entry
*he
)
300 struct rb_root
*root_in
;
301 struct rb_root
*root_out
;
304 root_in
= &he
->parent_he
->hroot_in
;
305 root_out
= &he
->parent_he
->hroot_out
;
307 if (hists__has(hists
, need_collapse
))
308 root_in
= &hists
->entries_collapsed
;
310 root_in
= hists
->entries_in
;
311 root_out
= &hists
->entries
;
314 rb_erase(&he
->rb_node_in
, root_in
);
315 rb_erase(&he
->rb_node
, root_out
);
319 --hists
->nr_non_filtered_entries
;
321 hist_entry__delete(he
);
324 void hists__decay_entries(struct hists
*hists
, bool zap_user
, bool zap_kernel
)
326 struct rb_node
*next
= rb_first(&hists
->entries
);
327 struct hist_entry
*n
;
330 n
= rb_entry(next
, struct hist_entry
, rb_node
);
331 next
= rb_next(&n
->rb_node
);
332 if (((zap_user
&& n
->level
== '.') ||
333 (zap_kernel
&& n
->level
!= '.') ||
334 hists__decay_entry(hists
, n
))) {
335 hists__delete_entry(hists
, n
);
340 void hists__delete_entries(struct hists
*hists
)
342 struct rb_node
*next
= rb_first(&hists
->entries
);
343 struct hist_entry
*n
;
346 n
= rb_entry(next
, struct hist_entry
, rb_node
);
347 next
= rb_next(&n
->rb_node
);
349 hists__delete_entry(hists
, n
);
354 * histogram, sorted on item, collects periods
357 static int hist_entry__init(struct hist_entry
*he
,
358 struct hist_entry
*template,
363 if (symbol_conf
.cumulate_callchain
) {
364 he
->stat_acc
= malloc(sizeof(he
->stat
));
365 if (he
->stat_acc
== NULL
)
367 memcpy(he
->stat_acc
, &he
->stat
, sizeof(he
->stat
));
369 memset(&he
->stat
, 0, sizeof(he
->stat
));
372 map__get(he
->ms
.map
);
374 if (he
->branch_info
) {
376 * This branch info is (a part of) allocated from
377 * sample__resolve_bstack() and will be freed after
378 * adding new entries. So we need to save a copy.
380 he
->branch_info
= malloc(sizeof(*he
->branch_info
));
381 if (he
->branch_info
== NULL
) {
382 map__zput(he
->ms
.map
);
387 memcpy(he
->branch_info
, template->branch_info
,
388 sizeof(*he
->branch_info
));
390 map__get(he
->branch_info
->from
.map
);
391 map__get(he
->branch_info
->to
.map
);
395 map__get(he
->mem_info
->iaddr
.map
);
396 map__get(he
->mem_info
->daddr
.map
);
399 if (symbol_conf
.use_callchain
)
400 callchain_init(he
->callchain
);
403 he
->raw_data
= memdup(he
->raw_data
, he
->raw_size
);
405 if (he
->raw_data
== NULL
) {
406 map__put(he
->ms
.map
);
407 if (he
->branch_info
) {
408 map__put(he
->branch_info
->from
.map
);
409 map__put(he
->branch_info
->to
.map
);
410 free(he
->branch_info
);
413 map__put(he
->mem_info
->iaddr
.map
);
414 map__put(he
->mem_info
->daddr
.map
);
420 INIT_LIST_HEAD(&he
->pairs
.node
);
421 thread__get(he
->thread
);
422 he
->hroot_in
= RB_ROOT
;
423 he
->hroot_out
= RB_ROOT
;
425 if (!symbol_conf
.report_hierarchy
)
431 static void *hist_entry__zalloc(size_t size
)
433 return zalloc(size
+ sizeof(struct hist_entry
));
436 static void hist_entry__free(void *ptr
)
441 static struct hist_entry_ops default_ops
= {
442 .new = hist_entry__zalloc
,
443 .free
= hist_entry__free
,
446 static struct hist_entry
*hist_entry__new(struct hist_entry
*template,
449 struct hist_entry_ops
*ops
= template->ops
;
450 size_t callchain_size
= 0;
451 struct hist_entry
*he
;
455 ops
= template->ops
= &default_ops
;
457 if (symbol_conf
.use_callchain
)
458 callchain_size
= sizeof(struct callchain_root
);
460 he
= ops
->new(callchain_size
);
462 err
= hist_entry__init(he
, template, sample_self
);
472 static u8
symbol__parent_filter(const struct symbol
*parent
)
474 if (symbol_conf
.exclude_other
&& parent
== NULL
)
475 return 1 << HIST_FILTER__PARENT
;
479 static void hist_entry__add_callchain_period(struct hist_entry
*he
, u64 period
)
481 if (!symbol_conf
.use_callchain
)
484 he
->hists
->callchain_period
+= period
;
486 he
->hists
->callchain_non_filtered_period
+= period
;
489 static struct hist_entry
*hists__findnew_entry(struct hists
*hists
,
490 struct hist_entry
*entry
,
491 struct addr_location
*al
,
495 struct rb_node
*parent
= NULL
;
496 struct hist_entry
*he
;
498 u64 period
= entry
->stat
.period
;
499 u64 weight
= entry
->stat
.weight
;
501 p
= &hists
->entries_in
->rb_node
;
505 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
508 * Make sure that it receives arguments in a same order as
509 * hist_entry__collapse() so that we can use an appropriate
510 * function when searching an entry regardless which sort
513 cmp
= hist_entry__cmp(he
, entry
);
517 he_stat__add_period(&he
->stat
, period
, weight
);
518 hist_entry__add_callchain_period(he
, period
);
520 if (symbol_conf
.cumulate_callchain
)
521 he_stat__add_period(he
->stat_acc
, period
, weight
);
524 * This mem info was allocated from sample__resolve_mem
525 * and will not be used anymore.
527 zfree(&entry
->mem_info
);
529 /* If the map of an existing hist_entry has
530 * become out-of-date due to an exec() or
531 * similar, update it. Otherwise we will
532 * mis-adjust symbol addresses when computing
533 * the history counter to increment.
535 if (he
->ms
.map
!= entry
->ms
.map
) {
536 map__put(he
->ms
.map
);
537 he
->ms
.map
= map__get(entry
->ms
.map
);
548 he
= hist_entry__new(entry
, sample_self
);
553 hist_entry__add_callchain_period(he
, period
);
556 rb_link_node(&he
->rb_node_in
, parent
, p
);
557 rb_insert_color(&he
->rb_node_in
, hists
->entries_in
);
560 he_stat__add_cpumode_period(&he
->stat
, al
->cpumode
, period
);
561 if (symbol_conf
.cumulate_callchain
)
562 he_stat__add_cpumode_period(he
->stat_acc
, al
->cpumode
, period
);
566 static struct hist_entry
*
567 __hists__add_entry(struct hists
*hists
,
568 struct addr_location
*al
,
569 struct symbol
*sym_parent
,
570 struct branch_info
*bi
,
572 struct perf_sample
*sample
,
574 struct hist_entry_ops
*ops
)
576 struct hist_entry entry
= {
577 .thread
= al
->thread
,
578 .comm
= thread__comm(al
->thread
),
583 .socket
= al
->socket
,
585 .cpumode
= al
->cpumode
,
590 .period
= sample
->period
,
591 .weight
= sample
->weight
,
593 .parent
= sym_parent
,
594 .filtered
= symbol__parent_filter(sym_parent
) | al
->filtered
,
598 .transaction
= sample
->transaction
,
599 .raw_data
= sample
->raw_data
,
600 .raw_size
= sample
->raw_size
,
604 return hists__findnew_entry(hists
, &entry
, al
, sample_self
);
607 struct hist_entry
*hists__add_entry(struct hists
*hists
,
608 struct addr_location
*al
,
609 struct symbol
*sym_parent
,
610 struct branch_info
*bi
,
612 struct perf_sample
*sample
,
615 return __hists__add_entry(hists
, al
, sym_parent
, bi
, mi
,
616 sample
, sample_self
, NULL
);
619 struct hist_entry
*hists__add_entry_ops(struct hists
*hists
,
620 struct hist_entry_ops
*ops
,
621 struct addr_location
*al
,
622 struct symbol
*sym_parent
,
623 struct branch_info
*bi
,
625 struct perf_sample
*sample
,
628 return __hists__add_entry(hists
, al
, sym_parent
, bi
, mi
,
629 sample
, sample_self
, ops
);
633 iter_next_nop_entry(struct hist_entry_iter
*iter __maybe_unused
,
634 struct addr_location
*al __maybe_unused
)
640 iter_add_next_nop_entry(struct hist_entry_iter
*iter __maybe_unused
,
641 struct addr_location
*al __maybe_unused
)
647 iter_prepare_mem_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
649 struct perf_sample
*sample
= iter
->sample
;
652 mi
= sample__resolve_mem(sample
, al
);
661 iter_add_single_mem_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
664 struct mem_info
*mi
= iter
->priv
;
665 struct hists
*hists
= evsel__hists(iter
->evsel
);
666 struct perf_sample
*sample
= iter
->sample
;
667 struct hist_entry
*he
;
672 cost
= sample
->weight
;
677 * must pass period=weight in order to get the correct
678 * sorting from hists__collapse_resort() which is solely
679 * based on periods. We want sorting be done on nr_events * weight
680 * and this is indirectly achieved by passing period=weight here
681 * and the he_stat__add_period() function.
683 sample
->period
= cost
;
685 he
= hists__add_entry(hists
, al
, iter
->parent
, NULL
, mi
,
695 iter_finish_mem_entry(struct hist_entry_iter
*iter
,
696 struct addr_location
*al __maybe_unused
)
698 struct perf_evsel
*evsel
= iter
->evsel
;
699 struct hists
*hists
= evsel__hists(evsel
);
700 struct hist_entry
*he
= iter
->he
;
706 hists__inc_nr_samples(hists
, he
->filtered
);
708 err
= hist_entry__append_callchain(he
, iter
->sample
);
712 * We don't need to free iter->priv (mem_info) here since the mem info
713 * was either already freed in hists__findnew_entry() or passed to a
714 * new hist entry by hist_entry__new().
723 iter_prepare_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
725 struct branch_info
*bi
;
726 struct perf_sample
*sample
= iter
->sample
;
728 bi
= sample__resolve_bstack(sample
, al
);
733 iter
->total
= sample
->branch_stack
->nr
;
740 iter_add_single_branch_entry(struct hist_entry_iter
*iter
,
741 struct addr_location
*al __maybe_unused
)
743 /* to avoid calling callback function */
750 iter_next_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
752 struct branch_info
*bi
= iter
->priv
;
758 if (iter
->curr
>= iter
->total
)
761 al
->map
= bi
[i
].to
.map
;
762 al
->sym
= bi
[i
].to
.sym
;
763 al
->addr
= bi
[i
].to
.addr
;
768 iter_add_next_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
770 struct branch_info
*bi
;
771 struct perf_evsel
*evsel
= iter
->evsel
;
772 struct hists
*hists
= evsel__hists(evsel
);
773 struct perf_sample
*sample
= iter
->sample
;
774 struct hist_entry
*he
= NULL
;
780 if (iter
->hide_unresolved
&& !(bi
[i
].from
.sym
&& bi
[i
].to
.sym
))
784 * The report shows the percentage of total branches captured
785 * and not events sampled. Thus we use a pseudo period of 1.
788 sample
->weight
= bi
->flags
.cycles
? bi
->flags
.cycles
: 1;
790 he
= hists__add_entry(hists
, al
, iter
->parent
, &bi
[i
], NULL
,
795 hists__inc_nr_samples(hists
, he
->filtered
);
804 iter_finish_branch_entry(struct hist_entry_iter
*iter
,
805 struct addr_location
*al __maybe_unused
)
810 return iter
->curr
>= iter
->total
? 0 : -1;
814 iter_prepare_normal_entry(struct hist_entry_iter
*iter __maybe_unused
,
815 struct addr_location
*al __maybe_unused
)
821 iter_add_single_normal_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
823 struct perf_evsel
*evsel
= iter
->evsel
;
824 struct perf_sample
*sample
= iter
->sample
;
825 struct hist_entry
*he
;
827 he
= hists__add_entry(evsel__hists(evsel
), al
, iter
->parent
, NULL
, NULL
,
837 iter_finish_normal_entry(struct hist_entry_iter
*iter
,
838 struct addr_location
*al __maybe_unused
)
840 struct hist_entry
*he
= iter
->he
;
841 struct perf_evsel
*evsel
= iter
->evsel
;
842 struct perf_sample
*sample
= iter
->sample
;
849 hists__inc_nr_samples(evsel__hists(evsel
), he
->filtered
);
851 return hist_entry__append_callchain(he
, sample
);
855 iter_prepare_cumulative_entry(struct hist_entry_iter
*iter
,
856 struct addr_location
*al __maybe_unused
)
858 struct hist_entry
**he_cache
;
860 callchain_cursor_commit(&callchain_cursor
);
863 * This is for detecting cycles or recursions so that they're
864 * cumulated only one time to prevent entries more than 100%
867 he_cache
= malloc(sizeof(*he_cache
) * (iter
->max_stack
+ 1));
868 if (he_cache
== NULL
)
871 iter
->priv
= he_cache
;
878 iter_add_single_cumulative_entry(struct hist_entry_iter
*iter
,
879 struct addr_location
*al
)
881 struct perf_evsel
*evsel
= iter
->evsel
;
882 struct hists
*hists
= evsel__hists(evsel
);
883 struct perf_sample
*sample
= iter
->sample
;
884 struct hist_entry
**he_cache
= iter
->priv
;
885 struct hist_entry
*he
;
888 he
= hists__add_entry(hists
, al
, iter
->parent
, NULL
, NULL
,
894 he_cache
[iter
->curr
++] = he
;
896 hist_entry__append_callchain(he
, sample
);
899 * We need to re-initialize the cursor since callchain_append()
900 * advanced the cursor to the end.
902 callchain_cursor_commit(&callchain_cursor
);
904 hists__inc_nr_samples(hists
, he
->filtered
);
910 iter_next_cumulative_entry(struct hist_entry_iter
*iter
,
911 struct addr_location
*al
)
913 struct callchain_cursor_node
*node
;
915 node
= callchain_cursor_current(&callchain_cursor
);
919 return fill_callchain_info(al
, node
, iter
->hide_unresolved
);
923 iter_add_next_cumulative_entry(struct hist_entry_iter
*iter
,
924 struct addr_location
*al
)
926 struct perf_evsel
*evsel
= iter
->evsel
;
927 struct perf_sample
*sample
= iter
->sample
;
928 struct hist_entry
**he_cache
= iter
->priv
;
929 struct hist_entry
*he
;
930 struct hist_entry he_tmp
= {
931 .hists
= evsel__hists(evsel
),
933 .thread
= al
->thread
,
934 .comm
= thread__comm(al
->thread
),
940 .parent
= iter
->parent
,
941 .raw_data
= sample
->raw_data
,
942 .raw_size
= sample
->raw_size
,
945 struct callchain_cursor cursor
;
947 callchain_cursor_snapshot(&cursor
, &callchain_cursor
);
949 callchain_cursor_advance(&callchain_cursor
);
952 * Check if there's duplicate entries in the callchain.
953 * It's possible that it has cycles or recursive calls.
955 for (i
= 0; i
< iter
->curr
; i
++) {
956 if (hist_entry__cmp(he_cache
[i
], &he_tmp
) == 0) {
957 /* to avoid calling callback function */
963 he
= hists__add_entry(evsel__hists(evsel
), al
, iter
->parent
, NULL
, NULL
,
969 he_cache
[iter
->curr
++] = he
;
971 if (symbol_conf
.use_callchain
)
972 callchain_append(he
->callchain
, &cursor
, sample
->period
);
977 iter_finish_cumulative_entry(struct hist_entry_iter
*iter
,
978 struct addr_location
*al __maybe_unused
)
986 const struct hist_iter_ops hist_iter_mem
= {
987 .prepare_entry
= iter_prepare_mem_entry
,
988 .add_single_entry
= iter_add_single_mem_entry
,
989 .next_entry
= iter_next_nop_entry
,
990 .add_next_entry
= iter_add_next_nop_entry
,
991 .finish_entry
= iter_finish_mem_entry
,
994 const struct hist_iter_ops hist_iter_branch
= {
995 .prepare_entry
= iter_prepare_branch_entry
,
996 .add_single_entry
= iter_add_single_branch_entry
,
997 .next_entry
= iter_next_branch_entry
,
998 .add_next_entry
= iter_add_next_branch_entry
,
999 .finish_entry
= iter_finish_branch_entry
,
1002 const struct hist_iter_ops hist_iter_normal
= {
1003 .prepare_entry
= iter_prepare_normal_entry
,
1004 .add_single_entry
= iter_add_single_normal_entry
,
1005 .next_entry
= iter_next_nop_entry
,
1006 .add_next_entry
= iter_add_next_nop_entry
,
1007 .finish_entry
= iter_finish_normal_entry
,
1010 const struct hist_iter_ops hist_iter_cumulative
= {
1011 .prepare_entry
= iter_prepare_cumulative_entry
,
1012 .add_single_entry
= iter_add_single_cumulative_entry
,
1013 .next_entry
= iter_next_cumulative_entry
,
1014 .add_next_entry
= iter_add_next_cumulative_entry
,
1015 .finish_entry
= iter_finish_cumulative_entry
,
1018 int hist_entry_iter__add(struct hist_entry_iter
*iter
, struct addr_location
*al
,
1019 int max_stack_depth
, void *arg
)
1023 err
= sample__resolve_callchain(iter
->sample
, &callchain_cursor
, &iter
->parent
,
1024 iter
->evsel
, al
, max_stack_depth
);
1028 iter
->max_stack
= max_stack_depth
;
1030 err
= iter
->ops
->prepare_entry(iter
, al
);
1034 err
= iter
->ops
->add_single_entry(iter
, al
);
1038 if (iter
->he
&& iter
->add_entry_cb
) {
1039 err
= iter
->add_entry_cb(iter
, al
, true, arg
);
1044 while (iter
->ops
->next_entry(iter
, al
)) {
1045 err
= iter
->ops
->add_next_entry(iter
, al
);
1049 if (iter
->he
&& iter
->add_entry_cb
) {
1050 err
= iter
->add_entry_cb(iter
, al
, false, arg
);
1057 err2
= iter
->ops
->finish_entry(iter
, al
);
1065 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1067 struct hists
*hists
= left
->hists
;
1068 struct perf_hpp_fmt
*fmt
;
1071 hists__for_each_sort_list(hists
, fmt
) {
1072 if (perf_hpp__is_dynamic_entry(fmt
) &&
1073 !perf_hpp__defined_dynamic_entry(fmt
, hists
))
1076 cmp
= fmt
->cmp(fmt
, left
, right
);
1085 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
1087 struct hists
*hists
= left
->hists
;
1088 struct perf_hpp_fmt
*fmt
;
1091 hists__for_each_sort_list(hists
, fmt
) {
1092 if (perf_hpp__is_dynamic_entry(fmt
) &&
1093 !perf_hpp__defined_dynamic_entry(fmt
, hists
))
1096 cmp
= fmt
->collapse(fmt
, left
, right
);
1104 void hist_entry__delete(struct hist_entry
*he
)
1106 struct hist_entry_ops
*ops
= he
->ops
;
1108 thread__zput(he
->thread
);
1109 map__zput(he
->ms
.map
);
1111 if (he
->branch_info
) {
1112 map__zput(he
->branch_info
->from
.map
);
1113 map__zput(he
->branch_info
->to
.map
);
1114 free_srcline(he
->branch_info
->srcline_from
);
1115 free_srcline(he
->branch_info
->srcline_to
);
1116 zfree(&he
->branch_info
);
1120 map__zput(he
->mem_info
->iaddr
.map
);
1121 map__zput(he
->mem_info
->daddr
.map
);
1122 zfree(&he
->mem_info
);
1125 zfree(&he
->stat_acc
);
1126 free_srcline(he
->srcline
);
1127 if (he
->srcfile
&& he
->srcfile
[0])
1129 free_callchain(he
->callchain
);
1130 free(he
->trace_output
);
1136 * If this is not the last column, then we need to pad it according to the
1137 * pre-calculated max lenght for this column, otherwise don't bother adding
1138 * spaces because that would break viewing this with, for instance, 'less',
1139 * that would show tons of trailing spaces when a long C++ demangled method
1142 int hist_entry__snprintf_alignment(struct hist_entry
*he
, struct perf_hpp
*hpp
,
1143 struct perf_hpp_fmt
*fmt
, int printed
)
1145 if (!list_is_last(&fmt
->list
, &he
->hists
->hpp_list
->fields
)) {
1146 const int width
= fmt
->width(fmt
, hpp
, he
->hists
);
1147 if (printed
< width
) {
1148 advance_hpp(hpp
, printed
);
1149 printed
= scnprintf(hpp
->buf
, hpp
->size
, "%-*s", width
- printed
, " ");
1157 * collapse the histogram
1160 static void hists__apply_filters(struct hists
*hists
, struct hist_entry
*he
);
1161 static void hists__remove_entry_filter(struct hists
*hists
, struct hist_entry
*he
,
1162 enum hist_filter type
);
1164 typedef bool (*fmt_chk_fn
)(struct perf_hpp_fmt
*fmt
);
1166 static bool check_thread_entry(struct perf_hpp_fmt
*fmt
)
1168 return perf_hpp__is_thread_entry(fmt
) || perf_hpp__is_comm_entry(fmt
);
1171 static void hist_entry__check_and_remove_filter(struct hist_entry
*he
,
1172 enum hist_filter type
,
1175 struct perf_hpp_fmt
*fmt
;
1176 bool type_match
= false;
1177 struct hist_entry
*parent
= he
->parent_he
;
1180 case HIST_FILTER__THREAD
:
1181 if (symbol_conf
.comm_list
== NULL
&&
1182 symbol_conf
.pid_list
== NULL
&&
1183 symbol_conf
.tid_list
== NULL
)
1186 case HIST_FILTER__DSO
:
1187 if (symbol_conf
.dso_list
== NULL
)
1190 case HIST_FILTER__SYMBOL
:
1191 if (symbol_conf
.sym_list
== NULL
)
1194 case HIST_FILTER__PARENT
:
1195 case HIST_FILTER__GUEST
:
1196 case HIST_FILTER__HOST
:
1197 case HIST_FILTER__SOCKET
:
1198 case HIST_FILTER__C2C
:
1203 /* if it's filtered by own fmt, it has to have filter bits */
1204 perf_hpp_list__for_each_format(he
->hpp_list
, fmt
) {
1213 * If the filter is for current level entry, propagate
1214 * filter marker to parents. The marker bit was
1215 * already set by default so it only needs to clear
1216 * non-filtered entries.
1218 if (!(he
->filtered
& (1 << type
))) {
1220 parent
->filtered
&= ~(1 << type
);
1221 parent
= parent
->parent_he
;
1226 * If current entry doesn't have matching formats, set
1227 * filter marker for upper level entries. it will be
1228 * cleared if its lower level entries is not filtered.
1230 * For lower-level entries, it inherits parent's
1231 * filter bit so that lower level entries of a
1232 * non-filtered entry won't set the filter marker.
1235 he
->filtered
|= (1 << type
);
1237 he
->filtered
|= (parent
->filtered
& (1 << type
));
1241 static void hist_entry__apply_hierarchy_filters(struct hist_entry
*he
)
1243 hist_entry__check_and_remove_filter(he
, HIST_FILTER__THREAD
,
1244 check_thread_entry
);
1246 hist_entry__check_and_remove_filter(he
, HIST_FILTER__DSO
,
1247 perf_hpp__is_dso_entry
);
1249 hist_entry__check_and_remove_filter(he
, HIST_FILTER__SYMBOL
,
1250 perf_hpp__is_sym_entry
);
1252 hists__apply_filters(he
->hists
, he
);
1255 static struct hist_entry
*hierarchy_insert_entry(struct hists
*hists
,
1256 struct rb_root
*root
,
1257 struct hist_entry
*he
,
1258 struct hist_entry
*parent_he
,
1259 struct perf_hpp_list
*hpp_list
)
1261 struct rb_node
**p
= &root
->rb_node
;
1262 struct rb_node
*parent
= NULL
;
1263 struct hist_entry
*iter
, *new;
1264 struct perf_hpp_fmt
*fmt
;
1267 while (*p
!= NULL
) {
1269 iter
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
1272 perf_hpp_list__for_each_sort_list(hpp_list
, fmt
) {
1273 cmp
= fmt
->collapse(fmt
, iter
, he
);
1279 he_stat__add_stat(&iter
->stat
, &he
->stat
);
1284 p
= &parent
->rb_left
;
1286 p
= &parent
->rb_right
;
1289 new = hist_entry__new(he
, true);
1293 hists
->nr_entries
++;
1295 /* save related format list for output */
1296 new->hpp_list
= hpp_list
;
1297 new->parent_he
= parent_he
;
1299 hist_entry__apply_hierarchy_filters(new);
1301 /* some fields are now passed to 'new' */
1302 perf_hpp_list__for_each_sort_list(hpp_list
, fmt
) {
1303 if (perf_hpp__is_trace_entry(fmt
) || perf_hpp__is_dynamic_entry(fmt
))
1304 he
->trace_output
= NULL
;
1306 new->trace_output
= NULL
;
1308 if (perf_hpp__is_srcline_entry(fmt
))
1311 new->srcline
= NULL
;
1313 if (perf_hpp__is_srcfile_entry(fmt
))
1316 new->srcfile
= NULL
;
1319 rb_link_node(&new->rb_node_in
, parent
, p
);
1320 rb_insert_color(&new->rb_node_in
, root
);
1324 static int hists__hierarchy_insert_entry(struct hists
*hists
,
1325 struct rb_root
*root
,
1326 struct hist_entry
*he
)
1328 struct perf_hpp_list_node
*node
;
1329 struct hist_entry
*new_he
= NULL
;
1330 struct hist_entry
*parent
= NULL
;
1334 list_for_each_entry(node
, &hists
->hpp_formats
, list
) {
1335 /* skip period (overhead) and elided columns */
1336 if (node
->level
== 0 || node
->skip
)
1339 /* insert copy of 'he' for each fmt into the hierarchy */
1340 new_he
= hierarchy_insert_entry(hists
, root
, he
, parent
, &node
->hpp
);
1341 if (new_he
== NULL
) {
1346 root
= &new_he
->hroot_in
;
1347 new_he
->depth
= depth
++;
1352 new_he
->leaf
= true;
1354 if (symbol_conf
.use_callchain
) {
1355 callchain_cursor_reset(&callchain_cursor
);
1356 if (callchain_merge(&callchain_cursor
,
1363 /* 'he' is no longer used */
1364 hist_entry__delete(he
);
1366 /* return 0 (or -1) since it already applied filters */
1370 static int hists__collapse_insert_entry(struct hists
*hists
,
1371 struct rb_root
*root
,
1372 struct hist_entry
*he
)
1374 struct rb_node
**p
= &root
->rb_node
;
1375 struct rb_node
*parent
= NULL
;
1376 struct hist_entry
*iter
;
1379 if (symbol_conf
.report_hierarchy
)
1380 return hists__hierarchy_insert_entry(hists
, root
, he
);
1382 while (*p
!= NULL
) {
1384 iter
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
1386 cmp
= hist_entry__collapse(iter
, he
);
1391 he_stat__add_stat(&iter
->stat
, &he
->stat
);
1392 if (symbol_conf
.cumulate_callchain
)
1393 he_stat__add_stat(iter
->stat_acc
, he
->stat_acc
);
1395 if (symbol_conf
.use_callchain
) {
1396 callchain_cursor_reset(&callchain_cursor
);
1397 if (callchain_merge(&callchain_cursor
,
1402 hist_entry__delete(he
);
1409 p
= &(*p
)->rb_right
;
1411 hists
->nr_entries
++;
1413 rb_link_node(&he
->rb_node_in
, parent
, p
);
1414 rb_insert_color(&he
->rb_node_in
, root
);
1418 struct rb_root
*hists__get_rotate_entries_in(struct hists
*hists
)
1420 struct rb_root
*root
;
1422 pthread_mutex_lock(&hists
->lock
);
1424 root
= hists
->entries_in
;
1425 if (++hists
->entries_in
> &hists
->entries_in_array
[1])
1426 hists
->entries_in
= &hists
->entries_in_array
[0];
1428 pthread_mutex_unlock(&hists
->lock
);
1433 static void hists__apply_filters(struct hists
*hists
, struct hist_entry
*he
)
1435 hists__filter_entry_by_dso(hists
, he
);
1436 hists__filter_entry_by_thread(hists
, he
);
1437 hists__filter_entry_by_symbol(hists
, he
);
1438 hists__filter_entry_by_socket(hists
, he
);
1441 int hists__collapse_resort(struct hists
*hists
, struct ui_progress
*prog
)
1443 struct rb_root
*root
;
1444 struct rb_node
*next
;
1445 struct hist_entry
*n
;
1448 if (!hists__has(hists
, need_collapse
))
1451 hists
->nr_entries
= 0;
1453 root
= hists__get_rotate_entries_in(hists
);
1455 next
= rb_first(root
);
1460 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
1461 next
= rb_next(&n
->rb_node_in
);
1463 rb_erase(&n
->rb_node_in
, root
);
1464 ret
= hists__collapse_insert_entry(hists
, &hists
->entries_collapsed
, n
);
1470 * If it wasn't combined with one of the entries already
1471 * collapsed, we need to apply the filters that may have
1472 * been set by, say, the hist_browser.
1474 hists__apply_filters(hists
, n
);
1477 ui_progress__update(prog
, 1);
1482 static int hist_entry__sort(struct hist_entry
*a
, struct hist_entry
*b
)
1484 struct hists
*hists
= a
->hists
;
1485 struct perf_hpp_fmt
*fmt
;
1488 hists__for_each_sort_list(hists
, fmt
) {
1489 if (perf_hpp__should_skip(fmt
, a
->hists
))
1492 cmp
= fmt
->sort(fmt
, a
, b
);
1500 static void hists__reset_filter_stats(struct hists
*hists
)
1502 hists
->nr_non_filtered_entries
= 0;
1503 hists
->stats
.total_non_filtered_period
= 0;
1506 void hists__reset_stats(struct hists
*hists
)
1508 hists
->nr_entries
= 0;
1509 hists
->stats
.total_period
= 0;
1511 hists__reset_filter_stats(hists
);
1514 static void hists__inc_filter_stats(struct hists
*hists
, struct hist_entry
*h
)
1516 hists
->nr_non_filtered_entries
++;
1517 hists
->stats
.total_non_filtered_period
+= h
->stat
.period
;
1520 void hists__inc_stats(struct hists
*hists
, struct hist_entry
*h
)
1523 hists__inc_filter_stats(hists
, h
);
1525 hists
->nr_entries
++;
1526 hists
->stats
.total_period
+= h
->stat
.period
;
1529 static void hierarchy_recalc_total_periods(struct hists
*hists
)
1531 struct rb_node
*node
;
1532 struct hist_entry
*he
;
1534 node
= rb_first(&hists
->entries
);
1536 hists
->stats
.total_period
= 0;
1537 hists
->stats
.total_non_filtered_period
= 0;
1540 * recalculate total period using top-level entries only
1541 * since lower level entries only see non-filtered entries
1542 * but upper level entries have sum of both entries.
1545 he
= rb_entry(node
, struct hist_entry
, rb_node
);
1546 node
= rb_next(node
);
1548 hists
->stats
.total_period
+= he
->stat
.period
;
1550 hists
->stats
.total_non_filtered_period
+= he
->stat
.period
;
1554 static void hierarchy_insert_output_entry(struct rb_root
*root
,
1555 struct hist_entry
*he
)
1557 struct rb_node
**p
= &root
->rb_node
;
1558 struct rb_node
*parent
= NULL
;
1559 struct hist_entry
*iter
;
1560 struct perf_hpp_fmt
*fmt
;
1562 while (*p
!= NULL
) {
1564 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
1566 if (hist_entry__sort(he
, iter
) > 0)
1567 p
= &parent
->rb_left
;
1569 p
= &parent
->rb_right
;
1572 rb_link_node(&he
->rb_node
, parent
, p
);
1573 rb_insert_color(&he
->rb_node
, root
);
1575 /* update column width of dynamic entry */
1576 perf_hpp_list__for_each_sort_list(he
->hpp_list
, fmt
) {
1577 if (perf_hpp__is_dynamic_entry(fmt
))
1578 fmt
->sort(fmt
, he
, NULL
);
1582 static void hists__hierarchy_output_resort(struct hists
*hists
,
1583 struct ui_progress
*prog
,
1584 struct rb_root
*root_in
,
1585 struct rb_root
*root_out
,
1586 u64 min_callchain_hits
,
1589 struct rb_node
*node
;
1590 struct hist_entry
*he
;
1592 *root_out
= RB_ROOT
;
1593 node
= rb_first(root_in
);
1596 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
1597 node
= rb_next(node
);
1599 hierarchy_insert_output_entry(root_out
, he
);
1602 ui_progress__update(prog
, 1);
1604 hists
->nr_entries
++;
1605 if (!he
->filtered
) {
1606 hists
->nr_non_filtered_entries
++;
1607 hists__calc_col_len(hists
, he
);
1611 hists__hierarchy_output_resort(hists
, prog
,
1622 if (callchain_param
.mode
== CHAIN_GRAPH_REL
) {
1623 u64 total
= he
->stat
.period
;
1625 if (symbol_conf
.cumulate_callchain
)
1626 total
= he
->stat_acc
->period
;
1628 min_callchain_hits
= total
* (callchain_param
.min_percent
/ 100);
1631 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
1632 min_callchain_hits
, &callchain_param
);
1636 static void __hists__insert_output_entry(struct rb_root
*entries
,
1637 struct hist_entry
*he
,
1638 u64 min_callchain_hits
,
1641 struct rb_node
**p
= &entries
->rb_node
;
1642 struct rb_node
*parent
= NULL
;
1643 struct hist_entry
*iter
;
1644 struct perf_hpp_fmt
*fmt
;
1646 if (use_callchain
) {
1647 if (callchain_param
.mode
== CHAIN_GRAPH_REL
) {
1648 u64 total
= he
->stat
.period
;
1650 if (symbol_conf
.cumulate_callchain
)
1651 total
= he
->stat_acc
->period
;
1653 min_callchain_hits
= total
* (callchain_param
.min_percent
/ 100);
1655 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
1656 min_callchain_hits
, &callchain_param
);
1659 while (*p
!= NULL
) {
1661 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
1663 if (hist_entry__sort(he
, iter
) > 0)
1666 p
= &(*p
)->rb_right
;
1669 rb_link_node(&he
->rb_node
, parent
, p
);
1670 rb_insert_color(&he
->rb_node
, entries
);
1672 perf_hpp_list__for_each_sort_list(&perf_hpp_list
, fmt
) {
1673 if (perf_hpp__is_dynamic_entry(fmt
) &&
1674 perf_hpp__defined_dynamic_entry(fmt
, he
->hists
))
1675 fmt
->sort(fmt
, he
, NULL
); /* update column width */
1679 static void output_resort(struct hists
*hists
, struct ui_progress
*prog
,
1680 bool use_callchain
, hists__resort_cb_t cb
)
1682 struct rb_root
*root
;
1683 struct rb_node
*next
;
1684 struct hist_entry
*n
;
1685 u64 callchain_total
;
1686 u64 min_callchain_hits
;
1688 callchain_total
= hists
->callchain_period
;
1689 if (symbol_conf
.filter_relative
)
1690 callchain_total
= hists
->callchain_non_filtered_period
;
1692 min_callchain_hits
= callchain_total
* (callchain_param
.min_percent
/ 100);
1694 hists__reset_stats(hists
);
1695 hists__reset_col_len(hists
);
1697 if (symbol_conf
.report_hierarchy
) {
1698 hists__hierarchy_output_resort(hists
, prog
,
1699 &hists
->entries_collapsed
,
1703 hierarchy_recalc_total_periods(hists
);
1707 if (hists__has(hists
, need_collapse
))
1708 root
= &hists
->entries_collapsed
;
1710 root
= hists
->entries_in
;
1712 next
= rb_first(root
);
1713 hists
->entries
= RB_ROOT
;
1716 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
1717 next
= rb_next(&n
->rb_node_in
);
1722 __hists__insert_output_entry(&hists
->entries
, n
, min_callchain_hits
, use_callchain
);
1723 hists__inc_stats(hists
, n
);
1726 hists__calc_col_len(hists
, n
);
1729 ui_progress__update(prog
, 1);
1733 void perf_evsel__output_resort(struct perf_evsel
*evsel
, struct ui_progress
*prog
)
1737 if (evsel
&& symbol_conf
.use_callchain
&& !symbol_conf
.show_ref_callgraph
)
1738 use_callchain
= evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
;
1740 use_callchain
= symbol_conf
.use_callchain
;
1742 output_resort(evsel__hists(evsel
), prog
, use_callchain
, NULL
);
1745 void hists__output_resort(struct hists
*hists
, struct ui_progress
*prog
)
1747 output_resort(hists
, prog
, symbol_conf
.use_callchain
, NULL
);
1750 void hists__output_resort_cb(struct hists
*hists
, struct ui_progress
*prog
,
1751 hists__resort_cb_t cb
)
1753 output_resort(hists
, prog
, symbol_conf
.use_callchain
, cb
);
1756 static bool can_goto_child(struct hist_entry
*he
, enum hierarchy_move_dir hmd
)
1758 if (he
->leaf
|| hmd
== HMD_FORCE_SIBLING
)
1761 if (he
->unfolded
|| hmd
== HMD_FORCE_CHILD
)
1767 struct rb_node
*rb_hierarchy_last(struct rb_node
*node
)
1769 struct hist_entry
*he
= rb_entry(node
, struct hist_entry
, rb_node
);
1771 while (can_goto_child(he
, HMD_NORMAL
)) {
1772 node
= rb_last(&he
->hroot_out
);
1773 he
= rb_entry(node
, struct hist_entry
, rb_node
);
1778 struct rb_node
*__rb_hierarchy_next(struct rb_node
*node
, enum hierarchy_move_dir hmd
)
1780 struct hist_entry
*he
= rb_entry(node
, struct hist_entry
, rb_node
);
1782 if (can_goto_child(he
, hmd
))
1783 node
= rb_first(&he
->hroot_out
);
1785 node
= rb_next(node
);
1787 while (node
== NULL
) {
1792 node
= rb_next(&he
->rb_node
);
1797 struct rb_node
*rb_hierarchy_prev(struct rb_node
*node
)
1799 struct hist_entry
*he
= rb_entry(node
, struct hist_entry
, rb_node
);
1801 node
= rb_prev(node
);
1803 return rb_hierarchy_last(node
);
1809 return &he
->rb_node
;
1812 bool hist_entry__has_hierarchy_children(struct hist_entry
*he
, float limit
)
1814 struct rb_node
*node
;
1815 struct hist_entry
*child
;
1821 node
= rb_first(&he
->hroot_out
);
1822 child
= rb_entry(node
, struct hist_entry
, rb_node
);
1824 while (node
&& child
->filtered
) {
1825 node
= rb_next(node
);
1826 child
= rb_entry(node
, struct hist_entry
, rb_node
);
1830 percent
= hist_entry__get_percent_limit(child
);
1834 return node
&& percent
>= limit
;
1837 static void hists__remove_entry_filter(struct hists
*hists
, struct hist_entry
*h
,
1838 enum hist_filter filter
)
1840 h
->filtered
&= ~(1 << filter
);
1842 if (symbol_conf
.report_hierarchy
) {
1843 struct hist_entry
*parent
= h
->parent_he
;
1846 he_stat__add_stat(&parent
->stat
, &h
->stat
);
1848 parent
->filtered
&= ~(1 << filter
);
1850 if (parent
->filtered
)
1853 /* force fold unfiltered entry for simplicity */
1854 parent
->unfolded
= false;
1855 parent
->has_no_entry
= false;
1856 parent
->row_offset
= 0;
1857 parent
->nr_rows
= 0;
1859 parent
= parent
->parent_he
;
1866 /* force fold unfiltered entry for simplicity */
1867 h
->unfolded
= false;
1868 h
->has_no_entry
= false;
1872 hists
->stats
.nr_non_filtered_samples
+= h
->stat
.nr_events
;
1874 hists__inc_filter_stats(hists
, h
);
1875 hists__calc_col_len(hists
, h
);
1879 static bool hists__filter_entry_by_dso(struct hists
*hists
,
1880 struct hist_entry
*he
)
1882 if (hists
->dso_filter
!= NULL
&&
1883 (he
->ms
.map
== NULL
|| he
->ms
.map
->dso
!= hists
->dso_filter
)) {
1884 he
->filtered
|= (1 << HIST_FILTER__DSO
);
1891 static bool hists__filter_entry_by_thread(struct hists
*hists
,
1892 struct hist_entry
*he
)
1894 if (hists
->thread_filter
!= NULL
&&
1895 he
->thread
!= hists
->thread_filter
) {
1896 he
->filtered
|= (1 << HIST_FILTER__THREAD
);
1903 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
1904 struct hist_entry
*he
)
1906 if (hists
->symbol_filter_str
!= NULL
&&
1907 (!he
->ms
.sym
|| strstr(he
->ms
.sym
->name
,
1908 hists
->symbol_filter_str
) == NULL
)) {
1909 he
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
1916 static bool hists__filter_entry_by_socket(struct hists
*hists
,
1917 struct hist_entry
*he
)
1919 if ((hists
->socket_filter
> -1) &&
1920 (he
->socket
!= hists
->socket_filter
)) {
1921 he
->filtered
|= (1 << HIST_FILTER__SOCKET
);
1928 typedef bool (*filter_fn_t
)(struct hists
*hists
, struct hist_entry
*he
);
1930 static void hists__filter_by_type(struct hists
*hists
, int type
, filter_fn_t filter
)
1934 hists
->stats
.nr_non_filtered_samples
= 0;
1936 hists__reset_filter_stats(hists
);
1937 hists__reset_col_len(hists
);
1939 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
1940 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1942 if (filter(hists
, h
))
1945 hists__remove_entry_filter(hists
, h
, type
);
1949 static void resort_filtered_entry(struct rb_root
*root
, struct hist_entry
*he
)
1951 struct rb_node
**p
= &root
->rb_node
;
1952 struct rb_node
*parent
= NULL
;
1953 struct hist_entry
*iter
;
1954 struct rb_root new_root
= RB_ROOT
;
1957 while (*p
!= NULL
) {
1959 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
1961 if (hist_entry__sort(he
, iter
) > 0)
1964 p
= &(*p
)->rb_right
;
1967 rb_link_node(&he
->rb_node
, parent
, p
);
1968 rb_insert_color(&he
->rb_node
, root
);
1970 if (he
->leaf
|| he
->filtered
)
1973 nd
= rb_first(&he
->hroot_out
);
1975 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1978 rb_erase(&h
->rb_node
, &he
->hroot_out
);
1980 resort_filtered_entry(&new_root
, h
);
1983 he
->hroot_out
= new_root
;
1986 static void hists__filter_hierarchy(struct hists
*hists
, int type
, const void *arg
)
1989 struct rb_root new_root
= RB_ROOT
;
1991 hists
->stats
.nr_non_filtered_samples
= 0;
1993 hists__reset_filter_stats(hists
);
1994 hists__reset_col_len(hists
);
1996 nd
= rb_first(&hists
->entries
);
1998 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
2001 ret
= hist_entry__filter(h
, type
, arg
);
2004 * case 1. non-matching type
2005 * zero out the period, set filter marker and move to child
2008 memset(&h
->stat
, 0, sizeof(h
->stat
));
2009 h
->filtered
|= (1 << type
);
2011 nd
= __rb_hierarchy_next(&h
->rb_node
, HMD_FORCE_CHILD
);
2014 * case 2. matched type (filter out)
2015 * set filter marker and move to next
2017 else if (ret
== 1) {
2018 h
->filtered
|= (1 << type
);
2020 nd
= __rb_hierarchy_next(&h
->rb_node
, HMD_FORCE_SIBLING
);
2023 * case 3. ok (not filtered)
2024 * add period to hists and parents, erase the filter marker
2025 * and move to next sibling
2028 hists__remove_entry_filter(hists
, h
, type
);
2030 nd
= __rb_hierarchy_next(&h
->rb_node
, HMD_FORCE_SIBLING
);
2034 hierarchy_recalc_total_periods(hists
);
2037 * resort output after applying a new filter since filter in a lower
2038 * hierarchy can change periods in a upper hierarchy.
2040 nd
= rb_first(&hists
->entries
);
2042 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
2045 rb_erase(&h
->rb_node
, &hists
->entries
);
2047 resort_filtered_entry(&new_root
, h
);
2050 hists
->entries
= new_root
;
2053 void hists__filter_by_thread(struct hists
*hists
)
2055 if (symbol_conf
.report_hierarchy
)
2056 hists__filter_hierarchy(hists
, HIST_FILTER__THREAD
,
2057 hists
->thread_filter
);
2059 hists__filter_by_type(hists
, HIST_FILTER__THREAD
,
2060 hists__filter_entry_by_thread
);
2063 void hists__filter_by_dso(struct hists
*hists
)
2065 if (symbol_conf
.report_hierarchy
)
2066 hists__filter_hierarchy(hists
, HIST_FILTER__DSO
,
2069 hists__filter_by_type(hists
, HIST_FILTER__DSO
,
2070 hists__filter_entry_by_dso
);
2073 void hists__filter_by_symbol(struct hists
*hists
)
2075 if (symbol_conf
.report_hierarchy
)
2076 hists__filter_hierarchy(hists
, HIST_FILTER__SYMBOL
,
2077 hists
->symbol_filter_str
);
2079 hists__filter_by_type(hists
, HIST_FILTER__SYMBOL
,
2080 hists__filter_entry_by_symbol
);
2083 void hists__filter_by_socket(struct hists
*hists
)
2085 if (symbol_conf
.report_hierarchy
)
2086 hists__filter_hierarchy(hists
, HIST_FILTER__SOCKET
,
2087 &hists
->socket_filter
);
2089 hists__filter_by_type(hists
, HIST_FILTER__SOCKET
,
2090 hists__filter_entry_by_socket
);
2093 void events_stats__inc(struct events_stats
*stats
, u32 type
)
2095 ++stats
->nr_events
[0];
2096 ++stats
->nr_events
[type
];
2099 void hists__inc_nr_events(struct hists
*hists
, u32 type
)
2101 events_stats__inc(&hists
->stats
, type
);
2104 void hists__inc_nr_samples(struct hists
*hists
, bool filtered
)
2106 events_stats__inc(&hists
->stats
, PERF_RECORD_SAMPLE
);
2108 hists
->stats
.nr_non_filtered_samples
++;
2111 static struct hist_entry
*hists__add_dummy_entry(struct hists
*hists
,
2112 struct hist_entry
*pair
)
2114 struct rb_root
*root
;
2116 struct rb_node
*parent
= NULL
;
2117 struct hist_entry
*he
;
2120 if (hists__has(hists
, need_collapse
))
2121 root
= &hists
->entries_collapsed
;
2123 root
= hists
->entries_in
;
2127 while (*p
!= NULL
) {
2129 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
2131 cmp
= hist_entry__collapse(he
, pair
);
2139 p
= &(*p
)->rb_right
;
2142 he
= hist_entry__new(pair
, true);
2144 memset(&he
->stat
, 0, sizeof(he
->stat
));
2146 if (symbol_conf
.cumulate_callchain
)
2147 memset(he
->stat_acc
, 0, sizeof(he
->stat
));
2148 rb_link_node(&he
->rb_node_in
, parent
, p
);
2149 rb_insert_color(&he
->rb_node_in
, root
);
2150 hists__inc_stats(hists
, he
);
2157 static struct hist_entry
*add_dummy_hierarchy_entry(struct hists
*hists
,
2158 struct rb_root
*root
,
2159 struct hist_entry
*pair
)
2162 struct rb_node
*parent
= NULL
;
2163 struct hist_entry
*he
;
2164 struct perf_hpp_fmt
*fmt
;
2167 while (*p
!= NULL
) {
2171 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
2173 perf_hpp_list__for_each_sort_list(he
->hpp_list
, fmt
) {
2174 cmp
= fmt
->collapse(fmt
, he
, pair
);
2182 p
= &parent
->rb_left
;
2184 p
= &parent
->rb_right
;
2187 he
= hist_entry__new(pair
, true);
2189 rb_link_node(&he
->rb_node_in
, parent
, p
);
2190 rb_insert_color(&he
->rb_node_in
, root
);
2194 memset(&he
->stat
, 0, sizeof(he
->stat
));
2195 hists__inc_stats(hists
, he
);
2201 static struct hist_entry
*hists__find_entry(struct hists
*hists
,
2202 struct hist_entry
*he
)
2206 if (hists__has(hists
, need_collapse
))
2207 n
= hists
->entries_collapsed
.rb_node
;
2209 n
= hists
->entries_in
->rb_node
;
2212 struct hist_entry
*iter
= rb_entry(n
, struct hist_entry
, rb_node_in
);
2213 int64_t cmp
= hist_entry__collapse(iter
, he
);
2226 static struct hist_entry
*hists__find_hierarchy_entry(struct rb_root
*root
,
2227 struct hist_entry
*he
)
2229 struct rb_node
*n
= root
->rb_node
;
2232 struct hist_entry
*iter
;
2233 struct perf_hpp_fmt
*fmt
;
2236 iter
= rb_entry(n
, struct hist_entry
, rb_node_in
);
2237 perf_hpp_list__for_each_sort_list(he
->hpp_list
, fmt
) {
2238 cmp
= fmt
->collapse(fmt
, iter
, he
);
2254 static void hists__match_hierarchy(struct rb_root
*leader_root
,
2255 struct rb_root
*other_root
)
2258 struct hist_entry
*pos
, *pair
;
2260 for (nd
= rb_first(leader_root
); nd
; nd
= rb_next(nd
)) {
2261 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2262 pair
= hists__find_hierarchy_entry(other_root
, pos
);
2265 hist_entry__add_pair(pair
, pos
);
2266 hists__match_hierarchy(&pos
->hroot_in
, &pair
->hroot_in
);
2272 * Look for pairs to link to the leader buckets (hist_entries):
2274 void hists__match(struct hists
*leader
, struct hists
*other
)
2276 struct rb_root
*root
;
2278 struct hist_entry
*pos
, *pair
;
2280 if (symbol_conf
.report_hierarchy
) {
2281 /* hierarchy report always collapses entries */
2282 return hists__match_hierarchy(&leader
->entries_collapsed
,
2283 &other
->entries_collapsed
);
2286 if (hists__has(leader
, need_collapse
))
2287 root
= &leader
->entries_collapsed
;
2289 root
= leader
->entries_in
;
2291 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
2292 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2293 pair
= hists__find_entry(other
, pos
);
2296 hist_entry__add_pair(pair
, pos
);
2300 static int hists__link_hierarchy(struct hists
*leader_hists
,
2301 struct hist_entry
*parent
,
2302 struct rb_root
*leader_root
,
2303 struct rb_root
*other_root
)
2306 struct hist_entry
*pos
, *leader
;
2308 for (nd
= rb_first(other_root
); nd
; nd
= rb_next(nd
)) {
2309 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2311 if (hist_entry__has_pairs(pos
)) {
2314 list_for_each_entry(leader
, &pos
->pairs
.head
, pairs
.node
) {
2315 if (leader
->hists
== leader_hists
) {
2323 leader
= add_dummy_hierarchy_entry(leader_hists
,
2328 /* do not point parent in the pos */
2329 leader
->parent_he
= parent
;
2331 hist_entry__add_pair(pos
, leader
);
2335 if (hists__link_hierarchy(leader_hists
, leader
,
2337 &pos
->hroot_in
) < 0)
2345 * Look for entries in the other hists that are not present in the leader, if
2346 * we find them, just add a dummy entry on the leader hists, with period=0,
2347 * nr_events=0, to serve as the list header.
2349 int hists__link(struct hists
*leader
, struct hists
*other
)
2351 struct rb_root
*root
;
2353 struct hist_entry
*pos
, *pair
;
2355 if (symbol_conf
.report_hierarchy
) {
2356 /* hierarchy report always collapses entries */
2357 return hists__link_hierarchy(leader
, NULL
,
2358 &leader
->entries_collapsed
,
2359 &other
->entries_collapsed
);
2362 if (hists__has(other
, need_collapse
))
2363 root
= &other
->entries_collapsed
;
2365 root
= other
->entries_in
;
2367 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
2368 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2370 if (!hist_entry__has_pairs(pos
)) {
2371 pair
= hists__add_dummy_entry(leader
, pos
);
2374 hist_entry__add_pair(pos
, pair
);
2381 void hist__account_cycles(struct branch_stack
*bs
, struct addr_location
*al
,
2382 struct perf_sample
*sample
, bool nonany_branch_mode
)
2384 struct branch_info
*bi
;
2386 /* If we have branch cycles always annotate them. */
2387 if (bs
&& bs
->nr
&& bs
->entries
[0].flags
.cycles
) {
2390 bi
= sample__resolve_bstack(sample
, al
);
2392 struct addr_map_symbol
*prev
= NULL
;
2395 * Ignore errors, still want to process the
2398 * For non standard branch modes always
2399 * force no IPC (prev == NULL)
2401 * Note that perf stores branches reversed from
2404 for (i
= bs
->nr
- 1; i
>= 0; i
--) {
2405 addr_map_symbol__account_cycles(&bi
[i
].from
,
2406 nonany_branch_mode
? NULL
: prev
,
2407 bi
[i
].flags
.cycles
);
2415 size_t perf_evlist__fprintf_nr_events(struct perf_evlist
*evlist
, FILE *fp
)
2417 struct perf_evsel
*pos
;
2420 evlist__for_each_entry(evlist
, pos
) {
2421 ret
+= fprintf(fp
, "%s stats:\n", perf_evsel__name(pos
));
2422 ret
+= events_stats__fprintf(&evsel__hists(pos
)->stats
, fp
);
2429 u64
hists__total_period(struct hists
*hists
)
2431 return symbol_conf
.filter_relative
? hists
->stats
.total_non_filtered_period
:
2432 hists
->stats
.total_period
;
2435 int parse_filter_percentage(const struct option
*opt __maybe_unused
,
2436 const char *arg
, int unset __maybe_unused
)
2438 if (!strcmp(arg
, "relative"))
2439 symbol_conf
.filter_relative
= true;
2440 else if (!strcmp(arg
, "absolute"))
2441 symbol_conf
.filter_relative
= false;
2448 int perf_hist_config(const char *var
, const char *value
)
2450 if (!strcmp(var
, "hist.percentage"))
2451 return parse_filter_percentage(NULL
, value
, 0);
2456 int __hists__init(struct hists
*hists
, struct perf_hpp_list
*hpp_list
)
2458 memset(hists
, 0, sizeof(*hists
));
2459 hists
->entries_in_array
[0] = hists
->entries_in_array
[1] = RB_ROOT
;
2460 hists
->entries_in
= &hists
->entries_in_array
[0];
2461 hists
->entries_collapsed
= RB_ROOT
;
2462 hists
->entries
= RB_ROOT
;
2463 pthread_mutex_init(&hists
->lock
, NULL
);
2464 hists
->socket_filter
= -1;
2465 hists
->hpp_list
= hpp_list
;
2466 INIT_LIST_HEAD(&hists
->hpp_formats
);
2470 static void hists__delete_remaining_entries(struct rb_root
*root
)
2472 struct rb_node
*node
;
2473 struct hist_entry
*he
;
2475 while (!RB_EMPTY_ROOT(root
)) {
2476 node
= rb_first(root
);
2477 rb_erase(node
, root
);
2479 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
2480 hist_entry__delete(he
);
2484 static void hists__delete_all_entries(struct hists
*hists
)
2486 hists__delete_entries(hists
);
2487 hists__delete_remaining_entries(&hists
->entries_in_array
[0]);
2488 hists__delete_remaining_entries(&hists
->entries_in_array
[1]);
2489 hists__delete_remaining_entries(&hists
->entries_collapsed
);
2492 static void hists_evsel__exit(struct perf_evsel
*evsel
)
2494 struct hists
*hists
= evsel__hists(evsel
);
2495 struct perf_hpp_fmt
*fmt
, *pos
;
2496 struct perf_hpp_list_node
*node
, *tmp
;
2498 hists__delete_all_entries(hists
);
2500 list_for_each_entry_safe(node
, tmp
, &hists
->hpp_formats
, list
) {
2501 perf_hpp_list__for_each_format_safe(&node
->hpp
, fmt
, pos
) {
2502 list_del(&fmt
->list
);
2505 list_del(&node
->list
);
2510 static int hists_evsel__init(struct perf_evsel
*evsel
)
2512 struct hists
*hists
= evsel__hists(evsel
);
2514 __hists__init(hists
, &perf_hpp_list
);
2519 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2520 * stored in the rbtree...
2523 int hists__init(void)
2525 int err
= perf_evsel__object_config(sizeof(struct hists_evsel
),
2529 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr
);
2534 void perf_hpp_list__init(struct perf_hpp_list
*list
)
2536 INIT_LIST_HEAD(&list
->fields
);
2537 INIT_LIST_HEAD(&list
->sorts
);