6 #include "namespaces.h"
13 #include "ui/progress.h"
16 #include <sys/param.h>
18 static bool hists__filter_entry_by_dso(struct hists
*hists
,
19 struct hist_entry
*he
);
20 static bool hists__filter_entry_by_thread(struct hists
*hists
,
21 struct hist_entry
*he
);
22 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
23 struct hist_entry
*he
);
24 static bool hists__filter_entry_by_socket(struct hists
*hists
,
25 struct hist_entry
*he
);
27 u16
hists__col_len(struct hists
*hists
, enum hist_column col
)
29 return hists
->col_len
[col
];
32 void hists__set_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
34 hists
->col_len
[col
] = len
;
37 bool hists__new_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
39 if (len
> hists__col_len(hists
, col
)) {
40 hists__set_col_len(hists
, col
, len
);
46 void hists__reset_col_len(struct hists
*hists
)
50 for (col
= 0; col
< HISTC_NR_COLS
; ++col
)
51 hists__set_col_len(hists
, col
, 0);
54 static void hists__set_unres_dso_col_len(struct hists
*hists
, int dso
)
56 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
58 if (hists__col_len(hists
, dso
) < unresolved_col_width
&&
59 !symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
60 !symbol_conf
.dso_list
)
61 hists__set_col_len(hists
, dso
, unresolved_col_width
);
64 void hists__calc_col_len(struct hists
*hists
, struct hist_entry
*h
)
66 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
71 * +4 accounts for '[x] ' priv level info
72 * +2 accounts for 0x prefix on raw addresses
73 * +3 accounts for ' y ' symtab origin info
76 symlen
= h
->ms
.sym
->namelen
+ 4;
78 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
79 hists__new_col_len(hists
, HISTC_SYMBOL
, symlen
);
81 symlen
= unresolved_col_width
+ 4 + 2;
82 hists__new_col_len(hists
, HISTC_SYMBOL
, symlen
);
83 hists__set_unres_dso_col_len(hists
, HISTC_DSO
);
86 len
= thread__comm_len(h
->thread
);
87 if (hists__new_col_len(hists
, HISTC_COMM
, len
))
88 hists__set_col_len(hists
, HISTC_THREAD
, len
+ 8);
91 len
= dso__name_len(h
->ms
.map
->dso
);
92 hists__new_col_len(hists
, HISTC_DSO
, len
);
96 hists__new_col_len(hists
, HISTC_PARENT
, h
->parent
->namelen
);
99 if (h
->branch_info
->from
.sym
) {
100 symlen
= (int)h
->branch_info
->from
.sym
->namelen
+ 4;
102 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
103 hists__new_col_len(hists
, HISTC_SYMBOL_FROM
, symlen
);
105 symlen
= dso__name_len(h
->branch_info
->from
.map
->dso
);
106 hists__new_col_len(hists
, HISTC_DSO_FROM
, symlen
);
108 symlen
= unresolved_col_width
+ 4 + 2;
109 hists__new_col_len(hists
, HISTC_SYMBOL_FROM
, symlen
);
110 hists__set_unres_dso_col_len(hists
, HISTC_DSO_FROM
);
113 if (h
->branch_info
->to
.sym
) {
114 symlen
= (int)h
->branch_info
->to
.sym
->namelen
+ 4;
116 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
117 hists__new_col_len(hists
, HISTC_SYMBOL_TO
, symlen
);
119 symlen
= dso__name_len(h
->branch_info
->to
.map
->dso
);
120 hists__new_col_len(hists
, HISTC_DSO_TO
, symlen
);
122 symlen
= unresolved_col_width
+ 4 + 2;
123 hists__new_col_len(hists
, HISTC_SYMBOL_TO
, symlen
);
124 hists__set_unres_dso_col_len(hists
, HISTC_DSO_TO
);
127 if (h
->branch_info
->srcline_from
)
128 hists__new_col_len(hists
, HISTC_SRCLINE_FROM
,
129 strlen(h
->branch_info
->srcline_from
));
130 if (h
->branch_info
->srcline_to
)
131 hists__new_col_len(hists
, HISTC_SRCLINE_TO
,
132 strlen(h
->branch_info
->srcline_to
));
136 if (h
->mem_info
->daddr
.sym
) {
137 symlen
= (int)h
->mem_info
->daddr
.sym
->namelen
+ 4
138 + unresolved_col_width
+ 2;
139 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
,
141 hists__new_col_len(hists
, HISTC_MEM_DCACHELINE
,
144 symlen
= unresolved_col_width
+ 4 + 2;
145 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
,
147 hists__new_col_len(hists
, HISTC_MEM_DCACHELINE
,
151 if (h
->mem_info
->iaddr
.sym
) {
152 symlen
= (int)h
->mem_info
->iaddr
.sym
->namelen
+ 4
153 + unresolved_col_width
+ 2;
154 hists__new_col_len(hists
, HISTC_MEM_IADDR_SYMBOL
,
157 symlen
= unresolved_col_width
+ 4 + 2;
158 hists__new_col_len(hists
, HISTC_MEM_IADDR_SYMBOL
,
162 if (h
->mem_info
->daddr
.map
) {
163 symlen
= dso__name_len(h
->mem_info
->daddr
.map
->dso
);
164 hists__new_col_len(hists
, HISTC_MEM_DADDR_DSO
,
167 symlen
= unresolved_col_width
+ 4 + 2;
168 hists__set_unres_dso_col_len(hists
, HISTC_MEM_DADDR_DSO
);
171 symlen
= unresolved_col_width
+ 4 + 2;
172 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
, symlen
);
173 hists__new_col_len(hists
, HISTC_MEM_IADDR_SYMBOL
, symlen
);
174 hists__set_unres_dso_col_len(hists
, HISTC_MEM_DADDR_DSO
);
177 hists__new_col_len(hists
, HISTC_CGROUP_ID
, 20);
178 hists__new_col_len(hists
, HISTC_CPU
, 3);
179 hists__new_col_len(hists
, HISTC_SOCKET
, 6);
180 hists__new_col_len(hists
, HISTC_MEM_LOCKED
, 6);
181 hists__new_col_len(hists
, HISTC_MEM_TLB
, 22);
182 hists__new_col_len(hists
, HISTC_MEM_SNOOP
, 12);
183 hists__new_col_len(hists
, HISTC_MEM_LVL
, 21 + 3);
184 hists__new_col_len(hists
, HISTC_LOCAL_WEIGHT
, 12);
185 hists__new_col_len(hists
, HISTC_GLOBAL_WEIGHT
, 12);
188 len
= MAX(strlen(h
->srcline
), strlen(sort_srcline
.se_header
));
189 hists__new_col_len(hists
, HISTC_SRCLINE
, len
);
193 hists__new_col_len(hists
, HISTC_SRCFILE
, strlen(h
->srcfile
));
196 hists__new_col_len(hists
, HISTC_TRANSACTION
,
197 hist_entry__transaction_len());
200 hists__new_col_len(hists
, HISTC_TRACE
, strlen(h
->trace_output
));
203 void hists__output_recalc_col_len(struct hists
*hists
, int max_rows
)
205 struct rb_node
*next
= rb_first(&hists
->entries
);
206 struct hist_entry
*n
;
209 hists__reset_col_len(hists
);
211 while (next
&& row
++ < max_rows
) {
212 n
= rb_entry(next
, struct hist_entry
, rb_node
);
214 hists__calc_col_len(hists
, n
);
215 next
= rb_next(&n
->rb_node
);
219 static void he_stat__add_cpumode_period(struct he_stat
*he_stat
,
220 unsigned int cpumode
, u64 period
)
223 case PERF_RECORD_MISC_KERNEL
:
224 he_stat
->period_sys
+= period
;
226 case PERF_RECORD_MISC_USER
:
227 he_stat
->period_us
+= period
;
229 case PERF_RECORD_MISC_GUEST_KERNEL
:
230 he_stat
->period_guest_sys
+= period
;
232 case PERF_RECORD_MISC_GUEST_USER
:
233 he_stat
->period_guest_us
+= period
;
240 static void he_stat__add_period(struct he_stat
*he_stat
, u64 period
,
244 he_stat
->period
+= period
;
245 he_stat
->weight
+= weight
;
246 he_stat
->nr_events
+= 1;
249 static void he_stat__add_stat(struct he_stat
*dest
, struct he_stat
*src
)
251 dest
->period
+= src
->period
;
252 dest
->period_sys
+= src
->period_sys
;
253 dest
->period_us
+= src
->period_us
;
254 dest
->period_guest_sys
+= src
->period_guest_sys
;
255 dest
->period_guest_us
+= src
->period_guest_us
;
256 dest
->nr_events
+= src
->nr_events
;
257 dest
->weight
+= src
->weight
;
260 static void he_stat__decay(struct he_stat
*he_stat
)
262 he_stat
->period
= (he_stat
->period
* 7) / 8;
263 he_stat
->nr_events
= (he_stat
->nr_events
* 7) / 8;
264 /* XXX need decay for weight too? */
267 static void hists__delete_entry(struct hists
*hists
, struct hist_entry
*he
);
269 static bool hists__decay_entry(struct hists
*hists
, struct hist_entry
*he
)
271 u64 prev_period
= he
->stat
.period
;
274 if (prev_period
== 0)
277 he_stat__decay(&he
->stat
);
278 if (symbol_conf
.cumulate_callchain
)
279 he_stat__decay(he
->stat_acc
);
280 decay_callchain(he
->callchain
);
282 diff
= prev_period
- he
->stat
.period
;
285 hists
->stats
.total_period
-= diff
;
287 hists
->stats
.total_non_filtered_period
-= diff
;
291 struct hist_entry
*child
;
292 struct rb_node
*node
= rb_first(&he
->hroot_out
);
294 child
= rb_entry(node
, struct hist_entry
, rb_node
);
295 node
= rb_next(node
);
297 if (hists__decay_entry(hists
, child
))
298 hists__delete_entry(hists
, child
);
302 return he
->stat
.period
== 0;
305 static void hists__delete_entry(struct hists
*hists
, struct hist_entry
*he
)
307 struct rb_root
*root_in
;
308 struct rb_root
*root_out
;
311 root_in
= &he
->parent_he
->hroot_in
;
312 root_out
= &he
->parent_he
->hroot_out
;
314 if (hists__has(hists
, need_collapse
))
315 root_in
= &hists
->entries_collapsed
;
317 root_in
= hists
->entries_in
;
318 root_out
= &hists
->entries
;
321 rb_erase(&he
->rb_node_in
, root_in
);
322 rb_erase(&he
->rb_node
, root_out
);
326 --hists
->nr_non_filtered_entries
;
328 hist_entry__delete(he
);
331 void hists__decay_entries(struct hists
*hists
, bool zap_user
, bool zap_kernel
)
333 struct rb_node
*next
= rb_first(&hists
->entries
);
334 struct hist_entry
*n
;
337 n
= rb_entry(next
, struct hist_entry
, rb_node
);
338 next
= rb_next(&n
->rb_node
);
339 if (((zap_user
&& n
->level
== '.') ||
340 (zap_kernel
&& n
->level
!= '.') ||
341 hists__decay_entry(hists
, n
))) {
342 hists__delete_entry(hists
, n
);
347 void hists__delete_entries(struct hists
*hists
)
349 struct rb_node
*next
= rb_first(&hists
->entries
);
350 struct hist_entry
*n
;
353 n
= rb_entry(next
, struct hist_entry
, rb_node
);
354 next
= rb_next(&n
->rb_node
);
356 hists__delete_entry(hists
, n
);
361 * histogram, sorted on item, collects periods
364 static int hist_entry__init(struct hist_entry
*he
,
365 struct hist_entry
*template,
370 if (symbol_conf
.cumulate_callchain
) {
371 he
->stat_acc
= malloc(sizeof(he
->stat
));
372 if (he
->stat_acc
== NULL
)
374 memcpy(he
->stat_acc
, &he
->stat
, sizeof(he
->stat
));
376 memset(&he
->stat
, 0, sizeof(he
->stat
));
379 map__get(he
->ms
.map
);
381 if (he
->branch_info
) {
383 * This branch info is (a part of) allocated from
384 * sample__resolve_bstack() and will be freed after
385 * adding new entries. So we need to save a copy.
387 he
->branch_info
= malloc(sizeof(*he
->branch_info
));
388 if (he
->branch_info
== NULL
) {
389 map__zput(he
->ms
.map
);
394 memcpy(he
->branch_info
, template->branch_info
,
395 sizeof(*he
->branch_info
));
397 map__get(he
->branch_info
->from
.map
);
398 map__get(he
->branch_info
->to
.map
);
402 map__get(he
->mem_info
->iaddr
.map
);
403 map__get(he
->mem_info
->daddr
.map
);
406 if (symbol_conf
.use_callchain
)
407 callchain_init(he
->callchain
);
410 he
->raw_data
= memdup(he
->raw_data
, he
->raw_size
);
412 if (he
->raw_data
== NULL
) {
413 map__put(he
->ms
.map
);
414 if (he
->branch_info
) {
415 map__put(he
->branch_info
->from
.map
);
416 map__put(he
->branch_info
->to
.map
);
417 free(he
->branch_info
);
420 map__put(he
->mem_info
->iaddr
.map
);
421 map__put(he
->mem_info
->daddr
.map
);
427 INIT_LIST_HEAD(&he
->pairs
.node
);
428 thread__get(he
->thread
);
429 he
->hroot_in
= RB_ROOT
;
430 he
->hroot_out
= RB_ROOT
;
432 if (!symbol_conf
.report_hierarchy
)
438 static void *hist_entry__zalloc(size_t size
)
440 return zalloc(size
+ sizeof(struct hist_entry
));
443 static void hist_entry__free(void *ptr
)
448 static struct hist_entry_ops default_ops
= {
449 .new = hist_entry__zalloc
,
450 .free
= hist_entry__free
,
453 static struct hist_entry
*hist_entry__new(struct hist_entry
*template,
456 struct hist_entry_ops
*ops
= template->ops
;
457 size_t callchain_size
= 0;
458 struct hist_entry
*he
;
462 ops
= template->ops
= &default_ops
;
464 if (symbol_conf
.use_callchain
)
465 callchain_size
= sizeof(struct callchain_root
);
467 he
= ops
->new(callchain_size
);
469 err
= hist_entry__init(he
, template, sample_self
);
479 static u8
symbol__parent_filter(const struct symbol
*parent
)
481 if (symbol_conf
.exclude_other
&& parent
== NULL
)
482 return 1 << HIST_FILTER__PARENT
;
486 static void hist_entry__add_callchain_period(struct hist_entry
*he
, u64 period
)
488 if (!symbol_conf
.use_callchain
)
491 he
->hists
->callchain_period
+= period
;
493 he
->hists
->callchain_non_filtered_period
+= period
;
496 static struct hist_entry
*hists__findnew_entry(struct hists
*hists
,
497 struct hist_entry
*entry
,
498 struct addr_location
*al
,
502 struct rb_node
*parent
= NULL
;
503 struct hist_entry
*he
;
505 u64 period
= entry
->stat
.period
;
506 u64 weight
= entry
->stat
.weight
;
508 p
= &hists
->entries_in
->rb_node
;
512 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
515 * Make sure that it receives arguments in a same order as
516 * hist_entry__collapse() so that we can use an appropriate
517 * function when searching an entry regardless which sort
520 cmp
= hist_entry__cmp(he
, entry
);
524 he_stat__add_period(&he
->stat
, period
, weight
);
525 hist_entry__add_callchain_period(he
, period
);
527 if (symbol_conf
.cumulate_callchain
)
528 he_stat__add_period(he
->stat_acc
, period
, weight
);
531 * This mem info was allocated from sample__resolve_mem
532 * and will not be used anymore.
534 zfree(&entry
->mem_info
);
536 /* If the map of an existing hist_entry has
537 * become out-of-date due to an exec() or
538 * similar, update it. Otherwise we will
539 * mis-adjust symbol addresses when computing
540 * the history counter to increment.
542 if (he
->ms
.map
!= entry
->ms
.map
) {
543 map__put(he
->ms
.map
);
544 he
->ms
.map
= map__get(entry
->ms
.map
);
555 he
= hist_entry__new(entry
, sample_self
);
560 hist_entry__add_callchain_period(he
, period
);
563 rb_link_node(&he
->rb_node_in
, parent
, p
);
564 rb_insert_color(&he
->rb_node_in
, hists
->entries_in
);
567 he_stat__add_cpumode_period(&he
->stat
, al
->cpumode
, period
);
568 if (symbol_conf
.cumulate_callchain
)
569 he_stat__add_cpumode_period(he
->stat_acc
, al
->cpumode
, period
);
573 static struct hist_entry
*
574 __hists__add_entry(struct hists
*hists
,
575 struct addr_location
*al
,
576 struct symbol
*sym_parent
,
577 struct branch_info
*bi
,
579 struct perf_sample
*sample
,
581 struct hist_entry_ops
*ops
)
583 struct namespaces
*ns
= thread__namespaces(al
->thread
);
584 struct hist_entry entry
= {
585 .thread
= al
->thread
,
586 .comm
= thread__comm(al
->thread
),
588 .dev
= ns
? ns
->link_info
[CGROUP_NS_INDEX
].dev
: 0,
589 .ino
= ns
? ns
->link_info
[CGROUP_NS_INDEX
].ino
: 0,
595 .socket
= al
->socket
,
597 .cpumode
= al
->cpumode
,
602 .period
= sample
->period
,
603 .weight
= sample
->weight
,
605 .parent
= sym_parent
,
606 .filtered
= symbol__parent_filter(sym_parent
) | al
->filtered
,
610 .transaction
= sample
->transaction
,
611 .raw_data
= sample
->raw_data
,
612 .raw_size
= sample
->raw_size
,
616 return hists__findnew_entry(hists
, &entry
, al
, sample_self
);
619 struct hist_entry
*hists__add_entry(struct hists
*hists
,
620 struct addr_location
*al
,
621 struct symbol
*sym_parent
,
622 struct branch_info
*bi
,
624 struct perf_sample
*sample
,
627 return __hists__add_entry(hists
, al
, sym_parent
, bi
, mi
,
628 sample
, sample_self
, NULL
);
631 struct hist_entry
*hists__add_entry_ops(struct hists
*hists
,
632 struct hist_entry_ops
*ops
,
633 struct addr_location
*al
,
634 struct symbol
*sym_parent
,
635 struct branch_info
*bi
,
637 struct perf_sample
*sample
,
640 return __hists__add_entry(hists
, al
, sym_parent
, bi
, mi
,
641 sample
, sample_self
, ops
);
645 iter_next_nop_entry(struct hist_entry_iter
*iter __maybe_unused
,
646 struct addr_location
*al __maybe_unused
)
652 iter_add_next_nop_entry(struct hist_entry_iter
*iter __maybe_unused
,
653 struct addr_location
*al __maybe_unused
)
659 iter_prepare_mem_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
661 struct perf_sample
*sample
= iter
->sample
;
664 mi
= sample__resolve_mem(sample
, al
);
673 iter_add_single_mem_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
676 struct mem_info
*mi
= iter
->priv
;
677 struct hists
*hists
= evsel__hists(iter
->evsel
);
678 struct perf_sample
*sample
= iter
->sample
;
679 struct hist_entry
*he
;
684 cost
= sample
->weight
;
689 * must pass period=weight in order to get the correct
690 * sorting from hists__collapse_resort() which is solely
691 * based on periods. We want sorting be done on nr_events * weight
692 * and this is indirectly achieved by passing period=weight here
693 * and the he_stat__add_period() function.
695 sample
->period
= cost
;
697 he
= hists__add_entry(hists
, al
, iter
->parent
, NULL
, mi
,
707 iter_finish_mem_entry(struct hist_entry_iter
*iter
,
708 struct addr_location
*al __maybe_unused
)
710 struct perf_evsel
*evsel
= iter
->evsel
;
711 struct hists
*hists
= evsel__hists(evsel
);
712 struct hist_entry
*he
= iter
->he
;
718 hists__inc_nr_samples(hists
, he
->filtered
);
720 err
= hist_entry__append_callchain(he
, iter
->sample
);
724 * We don't need to free iter->priv (mem_info) here since the mem info
725 * was either already freed in hists__findnew_entry() or passed to a
726 * new hist entry by hist_entry__new().
735 iter_prepare_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
737 struct branch_info
*bi
;
738 struct perf_sample
*sample
= iter
->sample
;
740 bi
= sample__resolve_bstack(sample
, al
);
745 iter
->total
= sample
->branch_stack
->nr
;
752 iter_add_single_branch_entry(struct hist_entry_iter
*iter
,
753 struct addr_location
*al __maybe_unused
)
755 /* to avoid calling callback function */
762 iter_next_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
764 struct branch_info
*bi
= iter
->priv
;
770 if (iter
->curr
>= iter
->total
)
773 al
->map
= bi
[i
].to
.map
;
774 al
->sym
= bi
[i
].to
.sym
;
775 al
->addr
= bi
[i
].to
.addr
;
780 iter_add_next_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
782 struct branch_info
*bi
;
783 struct perf_evsel
*evsel
= iter
->evsel
;
784 struct hists
*hists
= evsel__hists(evsel
);
785 struct perf_sample
*sample
= iter
->sample
;
786 struct hist_entry
*he
= NULL
;
792 if (iter
->hide_unresolved
&& !(bi
[i
].from
.sym
&& bi
[i
].to
.sym
))
796 * The report shows the percentage of total branches captured
797 * and not events sampled. Thus we use a pseudo period of 1.
800 sample
->weight
= bi
->flags
.cycles
? bi
->flags
.cycles
: 1;
802 he
= hists__add_entry(hists
, al
, iter
->parent
, &bi
[i
], NULL
,
807 hists__inc_nr_samples(hists
, he
->filtered
);
816 iter_finish_branch_entry(struct hist_entry_iter
*iter
,
817 struct addr_location
*al __maybe_unused
)
822 return iter
->curr
>= iter
->total
? 0 : -1;
826 iter_prepare_normal_entry(struct hist_entry_iter
*iter __maybe_unused
,
827 struct addr_location
*al __maybe_unused
)
833 iter_add_single_normal_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
835 struct perf_evsel
*evsel
= iter
->evsel
;
836 struct perf_sample
*sample
= iter
->sample
;
837 struct hist_entry
*he
;
839 he
= hists__add_entry(evsel__hists(evsel
), al
, iter
->parent
, NULL
, NULL
,
849 iter_finish_normal_entry(struct hist_entry_iter
*iter
,
850 struct addr_location
*al __maybe_unused
)
852 struct hist_entry
*he
= iter
->he
;
853 struct perf_evsel
*evsel
= iter
->evsel
;
854 struct perf_sample
*sample
= iter
->sample
;
861 hists__inc_nr_samples(evsel__hists(evsel
), he
->filtered
);
863 return hist_entry__append_callchain(he
, sample
);
867 iter_prepare_cumulative_entry(struct hist_entry_iter
*iter
,
868 struct addr_location
*al __maybe_unused
)
870 struct hist_entry
**he_cache
;
872 callchain_cursor_commit(&callchain_cursor
);
875 * This is for detecting cycles or recursions so that they're
876 * cumulated only one time to prevent entries more than 100%
879 he_cache
= malloc(sizeof(*he_cache
) * (iter
->max_stack
+ 1));
880 if (he_cache
== NULL
)
883 iter
->priv
= he_cache
;
890 iter_add_single_cumulative_entry(struct hist_entry_iter
*iter
,
891 struct addr_location
*al
)
893 struct perf_evsel
*evsel
= iter
->evsel
;
894 struct hists
*hists
= evsel__hists(evsel
);
895 struct perf_sample
*sample
= iter
->sample
;
896 struct hist_entry
**he_cache
= iter
->priv
;
897 struct hist_entry
*he
;
900 he
= hists__add_entry(hists
, al
, iter
->parent
, NULL
, NULL
,
906 he_cache
[iter
->curr
++] = he
;
908 hist_entry__append_callchain(he
, sample
);
911 * We need to re-initialize the cursor since callchain_append()
912 * advanced the cursor to the end.
914 callchain_cursor_commit(&callchain_cursor
);
916 hists__inc_nr_samples(hists
, he
->filtered
);
922 iter_next_cumulative_entry(struct hist_entry_iter
*iter
,
923 struct addr_location
*al
)
925 struct callchain_cursor_node
*node
;
927 node
= callchain_cursor_current(&callchain_cursor
);
931 return fill_callchain_info(al
, node
, iter
->hide_unresolved
);
935 iter_add_next_cumulative_entry(struct hist_entry_iter
*iter
,
936 struct addr_location
*al
)
938 struct perf_evsel
*evsel
= iter
->evsel
;
939 struct perf_sample
*sample
= iter
->sample
;
940 struct hist_entry
**he_cache
= iter
->priv
;
941 struct hist_entry
*he
;
942 struct hist_entry he_tmp
= {
943 .hists
= evsel__hists(evsel
),
945 .thread
= al
->thread
,
946 .comm
= thread__comm(al
->thread
),
952 .parent
= iter
->parent
,
953 .raw_data
= sample
->raw_data
,
954 .raw_size
= sample
->raw_size
,
957 struct callchain_cursor cursor
;
959 callchain_cursor_snapshot(&cursor
, &callchain_cursor
);
961 callchain_cursor_advance(&callchain_cursor
);
964 * Check if there's duplicate entries in the callchain.
965 * It's possible that it has cycles or recursive calls.
967 for (i
= 0; i
< iter
->curr
; i
++) {
968 if (hist_entry__cmp(he_cache
[i
], &he_tmp
) == 0) {
969 /* to avoid calling callback function */
975 he
= hists__add_entry(evsel__hists(evsel
), al
, iter
->parent
, NULL
, NULL
,
981 he_cache
[iter
->curr
++] = he
;
983 if (symbol_conf
.use_callchain
)
984 callchain_append(he
->callchain
, &cursor
, sample
->period
);
989 iter_finish_cumulative_entry(struct hist_entry_iter
*iter
,
990 struct addr_location
*al __maybe_unused
)
998 const struct hist_iter_ops hist_iter_mem
= {
999 .prepare_entry
= iter_prepare_mem_entry
,
1000 .add_single_entry
= iter_add_single_mem_entry
,
1001 .next_entry
= iter_next_nop_entry
,
1002 .add_next_entry
= iter_add_next_nop_entry
,
1003 .finish_entry
= iter_finish_mem_entry
,
1006 const struct hist_iter_ops hist_iter_branch
= {
1007 .prepare_entry
= iter_prepare_branch_entry
,
1008 .add_single_entry
= iter_add_single_branch_entry
,
1009 .next_entry
= iter_next_branch_entry
,
1010 .add_next_entry
= iter_add_next_branch_entry
,
1011 .finish_entry
= iter_finish_branch_entry
,
1014 const struct hist_iter_ops hist_iter_normal
= {
1015 .prepare_entry
= iter_prepare_normal_entry
,
1016 .add_single_entry
= iter_add_single_normal_entry
,
1017 .next_entry
= iter_next_nop_entry
,
1018 .add_next_entry
= iter_add_next_nop_entry
,
1019 .finish_entry
= iter_finish_normal_entry
,
1022 const struct hist_iter_ops hist_iter_cumulative
= {
1023 .prepare_entry
= iter_prepare_cumulative_entry
,
1024 .add_single_entry
= iter_add_single_cumulative_entry
,
1025 .next_entry
= iter_next_cumulative_entry
,
1026 .add_next_entry
= iter_add_next_cumulative_entry
,
1027 .finish_entry
= iter_finish_cumulative_entry
,
1030 int hist_entry_iter__add(struct hist_entry_iter
*iter
, struct addr_location
*al
,
1031 int max_stack_depth
, void *arg
)
1034 struct map
*alm
= NULL
;
1037 alm
= map__get(al
->map
);
1039 err
= sample__resolve_callchain(iter
->sample
, &callchain_cursor
, &iter
->parent
,
1040 iter
->evsel
, al
, max_stack_depth
);
1044 iter
->max_stack
= max_stack_depth
;
1046 err
= iter
->ops
->prepare_entry(iter
, al
);
1050 err
= iter
->ops
->add_single_entry(iter
, al
);
1054 if (iter
->he
&& iter
->add_entry_cb
) {
1055 err
= iter
->add_entry_cb(iter
, al
, true, arg
);
1060 while (iter
->ops
->next_entry(iter
, al
)) {
1061 err
= iter
->ops
->add_next_entry(iter
, al
);
1065 if (iter
->he
&& iter
->add_entry_cb
) {
1066 err
= iter
->add_entry_cb(iter
, al
, false, arg
);
1073 err2
= iter
->ops
->finish_entry(iter
, al
);
1083 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
1085 struct hists
*hists
= left
->hists
;
1086 struct perf_hpp_fmt
*fmt
;
1089 hists__for_each_sort_list(hists
, fmt
) {
1090 if (perf_hpp__is_dynamic_entry(fmt
) &&
1091 !perf_hpp__defined_dynamic_entry(fmt
, hists
))
1094 cmp
= fmt
->cmp(fmt
, left
, right
);
1103 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
1105 struct hists
*hists
= left
->hists
;
1106 struct perf_hpp_fmt
*fmt
;
1109 hists__for_each_sort_list(hists
, fmt
) {
1110 if (perf_hpp__is_dynamic_entry(fmt
) &&
1111 !perf_hpp__defined_dynamic_entry(fmt
, hists
))
1114 cmp
= fmt
->collapse(fmt
, left
, right
);
1122 void hist_entry__delete(struct hist_entry
*he
)
1124 struct hist_entry_ops
*ops
= he
->ops
;
1126 thread__zput(he
->thread
);
1127 map__zput(he
->ms
.map
);
1129 if (he
->branch_info
) {
1130 map__zput(he
->branch_info
->from
.map
);
1131 map__zput(he
->branch_info
->to
.map
);
1132 free_srcline(he
->branch_info
->srcline_from
);
1133 free_srcline(he
->branch_info
->srcline_to
);
1134 zfree(&he
->branch_info
);
1138 map__zput(he
->mem_info
->iaddr
.map
);
1139 map__zput(he
->mem_info
->daddr
.map
);
1140 zfree(&he
->mem_info
);
1143 if (he
->inline_node
) {
1144 inline_node__delete(he
->inline_node
);
1145 he
->inline_node
= NULL
;
1148 zfree(&he
->stat_acc
);
1149 free_srcline(he
->srcline
);
1150 if (he
->srcfile
&& he
->srcfile
[0])
1152 free_callchain(he
->callchain
);
1153 free(he
->trace_output
);
1159 * If this is not the last column, then we need to pad it according to the
1160 * pre-calculated max lenght for this column, otherwise don't bother adding
1161 * spaces because that would break viewing this with, for instance, 'less',
1162 * that would show tons of trailing spaces when a long C++ demangled method
1165 int hist_entry__snprintf_alignment(struct hist_entry
*he
, struct perf_hpp
*hpp
,
1166 struct perf_hpp_fmt
*fmt
, int printed
)
1168 if (!list_is_last(&fmt
->list
, &he
->hists
->hpp_list
->fields
)) {
1169 const int width
= fmt
->width(fmt
, hpp
, he
->hists
);
1170 if (printed
< width
) {
1171 advance_hpp(hpp
, printed
);
1172 printed
= scnprintf(hpp
->buf
, hpp
->size
, "%-*s", width
- printed
, " ");
1180 * collapse the histogram
1183 static void hists__apply_filters(struct hists
*hists
, struct hist_entry
*he
);
1184 static void hists__remove_entry_filter(struct hists
*hists
, struct hist_entry
*he
,
1185 enum hist_filter type
);
1187 typedef bool (*fmt_chk_fn
)(struct perf_hpp_fmt
*fmt
);
1189 static bool check_thread_entry(struct perf_hpp_fmt
*fmt
)
1191 return perf_hpp__is_thread_entry(fmt
) || perf_hpp__is_comm_entry(fmt
);
1194 static void hist_entry__check_and_remove_filter(struct hist_entry
*he
,
1195 enum hist_filter type
,
1198 struct perf_hpp_fmt
*fmt
;
1199 bool type_match
= false;
1200 struct hist_entry
*parent
= he
->parent_he
;
1203 case HIST_FILTER__THREAD
:
1204 if (symbol_conf
.comm_list
== NULL
&&
1205 symbol_conf
.pid_list
== NULL
&&
1206 symbol_conf
.tid_list
== NULL
)
1209 case HIST_FILTER__DSO
:
1210 if (symbol_conf
.dso_list
== NULL
)
1213 case HIST_FILTER__SYMBOL
:
1214 if (symbol_conf
.sym_list
== NULL
)
1217 case HIST_FILTER__PARENT
:
1218 case HIST_FILTER__GUEST
:
1219 case HIST_FILTER__HOST
:
1220 case HIST_FILTER__SOCKET
:
1221 case HIST_FILTER__C2C
:
1226 /* if it's filtered by own fmt, it has to have filter bits */
1227 perf_hpp_list__for_each_format(he
->hpp_list
, fmt
) {
1236 * If the filter is for current level entry, propagate
1237 * filter marker to parents. The marker bit was
1238 * already set by default so it only needs to clear
1239 * non-filtered entries.
1241 if (!(he
->filtered
& (1 << type
))) {
1243 parent
->filtered
&= ~(1 << type
);
1244 parent
= parent
->parent_he
;
1249 * If current entry doesn't have matching formats, set
1250 * filter marker for upper level entries. it will be
1251 * cleared if its lower level entries is not filtered.
1253 * For lower-level entries, it inherits parent's
1254 * filter bit so that lower level entries of a
1255 * non-filtered entry won't set the filter marker.
1258 he
->filtered
|= (1 << type
);
1260 he
->filtered
|= (parent
->filtered
& (1 << type
));
1264 static void hist_entry__apply_hierarchy_filters(struct hist_entry
*he
)
1266 hist_entry__check_and_remove_filter(he
, HIST_FILTER__THREAD
,
1267 check_thread_entry
);
1269 hist_entry__check_and_remove_filter(he
, HIST_FILTER__DSO
,
1270 perf_hpp__is_dso_entry
);
1272 hist_entry__check_and_remove_filter(he
, HIST_FILTER__SYMBOL
,
1273 perf_hpp__is_sym_entry
);
1275 hists__apply_filters(he
->hists
, he
);
1278 static struct hist_entry
*hierarchy_insert_entry(struct hists
*hists
,
1279 struct rb_root
*root
,
1280 struct hist_entry
*he
,
1281 struct hist_entry
*parent_he
,
1282 struct perf_hpp_list
*hpp_list
)
1284 struct rb_node
**p
= &root
->rb_node
;
1285 struct rb_node
*parent
= NULL
;
1286 struct hist_entry
*iter
, *new;
1287 struct perf_hpp_fmt
*fmt
;
1290 while (*p
!= NULL
) {
1292 iter
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
1295 perf_hpp_list__for_each_sort_list(hpp_list
, fmt
) {
1296 cmp
= fmt
->collapse(fmt
, iter
, he
);
1302 he_stat__add_stat(&iter
->stat
, &he
->stat
);
1307 p
= &parent
->rb_left
;
1309 p
= &parent
->rb_right
;
1312 new = hist_entry__new(he
, true);
1316 hists
->nr_entries
++;
1318 /* save related format list for output */
1319 new->hpp_list
= hpp_list
;
1320 new->parent_he
= parent_he
;
1322 hist_entry__apply_hierarchy_filters(new);
1324 /* some fields are now passed to 'new' */
1325 perf_hpp_list__for_each_sort_list(hpp_list
, fmt
) {
1326 if (perf_hpp__is_trace_entry(fmt
) || perf_hpp__is_dynamic_entry(fmt
))
1327 he
->trace_output
= NULL
;
1329 new->trace_output
= NULL
;
1331 if (perf_hpp__is_srcline_entry(fmt
))
1334 new->srcline
= NULL
;
1336 if (perf_hpp__is_srcfile_entry(fmt
))
1339 new->srcfile
= NULL
;
1342 rb_link_node(&new->rb_node_in
, parent
, p
);
1343 rb_insert_color(&new->rb_node_in
, root
);
1347 static int hists__hierarchy_insert_entry(struct hists
*hists
,
1348 struct rb_root
*root
,
1349 struct hist_entry
*he
)
1351 struct perf_hpp_list_node
*node
;
1352 struct hist_entry
*new_he
= NULL
;
1353 struct hist_entry
*parent
= NULL
;
1357 list_for_each_entry(node
, &hists
->hpp_formats
, list
) {
1358 /* skip period (overhead) and elided columns */
1359 if (node
->level
== 0 || node
->skip
)
1362 /* insert copy of 'he' for each fmt into the hierarchy */
1363 new_he
= hierarchy_insert_entry(hists
, root
, he
, parent
, &node
->hpp
);
1364 if (new_he
== NULL
) {
1369 root
= &new_he
->hroot_in
;
1370 new_he
->depth
= depth
++;
1375 new_he
->leaf
= true;
1377 if (symbol_conf
.use_callchain
) {
1378 callchain_cursor_reset(&callchain_cursor
);
1379 if (callchain_merge(&callchain_cursor
,
1386 /* 'he' is no longer used */
1387 hist_entry__delete(he
);
1389 /* return 0 (or -1) since it already applied filters */
1393 static int hists__collapse_insert_entry(struct hists
*hists
,
1394 struct rb_root
*root
,
1395 struct hist_entry
*he
)
1397 struct rb_node
**p
= &root
->rb_node
;
1398 struct rb_node
*parent
= NULL
;
1399 struct hist_entry
*iter
;
1402 if (symbol_conf
.report_hierarchy
)
1403 return hists__hierarchy_insert_entry(hists
, root
, he
);
1405 while (*p
!= NULL
) {
1407 iter
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
1409 cmp
= hist_entry__collapse(iter
, he
);
1414 he_stat__add_stat(&iter
->stat
, &he
->stat
);
1415 if (symbol_conf
.cumulate_callchain
)
1416 he_stat__add_stat(iter
->stat_acc
, he
->stat_acc
);
1418 if (symbol_conf
.use_callchain
) {
1419 callchain_cursor_reset(&callchain_cursor
);
1420 if (callchain_merge(&callchain_cursor
,
1425 hist_entry__delete(he
);
1432 p
= &(*p
)->rb_right
;
1434 hists
->nr_entries
++;
1436 rb_link_node(&he
->rb_node_in
, parent
, p
);
1437 rb_insert_color(&he
->rb_node_in
, root
);
1441 struct rb_root
*hists__get_rotate_entries_in(struct hists
*hists
)
1443 struct rb_root
*root
;
1445 pthread_mutex_lock(&hists
->lock
);
1447 root
= hists
->entries_in
;
1448 if (++hists
->entries_in
> &hists
->entries_in_array
[1])
1449 hists
->entries_in
= &hists
->entries_in_array
[0];
1451 pthread_mutex_unlock(&hists
->lock
);
1456 static void hists__apply_filters(struct hists
*hists
, struct hist_entry
*he
)
1458 hists__filter_entry_by_dso(hists
, he
);
1459 hists__filter_entry_by_thread(hists
, he
);
1460 hists__filter_entry_by_symbol(hists
, he
);
1461 hists__filter_entry_by_socket(hists
, he
);
1464 int hists__collapse_resort(struct hists
*hists
, struct ui_progress
*prog
)
1466 struct rb_root
*root
;
1467 struct rb_node
*next
;
1468 struct hist_entry
*n
;
1471 if (!hists__has(hists
, need_collapse
))
1474 hists
->nr_entries
= 0;
1476 root
= hists__get_rotate_entries_in(hists
);
1478 next
= rb_first(root
);
1483 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
1484 next
= rb_next(&n
->rb_node_in
);
1486 rb_erase(&n
->rb_node_in
, root
);
1487 ret
= hists__collapse_insert_entry(hists
, &hists
->entries_collapsed
, n
);
1493 * If it wasn't combined with one of the entries already
1494 * collapsed, we need to apply the filters that may have
1495 * been set by, say, the hist_browser.
1497 hists__apply_filters(hists
, n
);
1500 ui_progress__update(prog
, 1);
1505 static int hist_entry__sort(struct hist_entry
*a
, struct hist_entry
*b
)
1507 struct hists
*hists
= a
->hists
;
1508 struct perf_hpp_fmt
*fmt
;
1511 hists__for_each_sort_list(hists
, fmt
) {
1512 if (perf_hpp__should_skip(fmt
, a
->hists
))
1515 cmp
= fmt
->sort(fmt
, a
, b
);
1523 static void hists__reset_filter_stats(struct hists
*hists
)
1525 hists
->nr_non_filtered_entries
= 0;
1526 hists
->stats
.total_non_filtered_period
= 0;
1529 void hists__reset_stats(struct hists
*hists
)
1531 hists
->nr_entries
= 0;
1532 hists
->stats
.total_period
= 0;
1534 hists__reset_filter_stats(hists
);
1537 static void hists__inc_filter_stats(struct hists
*hists
, struct hist_entry
*h
)
1539 hists
->nr_non_filtered_entries
++;
1540 hists
->stats
.total_non_filtered_period
+= h
->stat
.period
;
1543 void hists__inc_stats(struct hists
*hists
, struct hist_entry
*h
)
1546 hists__inc_filter_stats(hists
, h
);
1548 hists
->nr_entries
++;
1549 hists
->stats
.total_period
+= h
->stat
.period
;
1552 static void hierarchy_recalc_total_periods(struct hists
*hists
)
1554 struct rb_node
*node
;
1555 struct hist_entry
*he
;
1557 node
= rb_first(&hists
->entries
);
1559 hists
->stats
.total_period
= 0;
1560 hists
->stats
.total_non_filtered_period
= 0;
1563 * recalculate total period using top-level entries only
1564 * since lower level entries only see non-filtered entries
1565 * but upper level entries have sum of both entries.
1568 he
= rb_entry(node
, struct hist_entry
, rb_node
);
1569 node
= rb_next(node
);
1571 hists
->stats
.total_period
+= he
->stat
.period
;
1573 hists
->stats
.total_non_filtered_period
+= he
->stat
.period
;
1577 static void hierarchy_insert_output_entry(struct rb_root
*root
,
1578 struct hist_entry
*he
)
1580 struct rb_node
**p
= &root
->rb_node
;
1581 struct rb_node
*parent
= NULL
;
1582 struct hist_entry
*iter
;
1583 struct perf_hpp_fmt
*fmt
;
1585 while (*p
!= NULL
) {
1587 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
1589 if (hist_entry__sort(he
, iter
) > 0)
1590 p
= &parent
->rb_left
;
1592 p
= &parent
->rb_right
;
1595 rb_link_node(&he
->rb_node
, parent
, p
);
1596 rb_insert_color(&he
->rb_node
, root
);
1598 /* update column width of dynamic entry */
1599 perf_hpp_list__for_each_sort_list(he
->hpp_list
, fmt
) {
1600 if (perf_hpp__is_dynamic_entry(fmt
))
1601 fmt
->sort(fmt
, he
, NULL
);
1605 static void hists__hierarchy_output_resort(struct hists
*hists
,
1606 struct ui_progress
*prog
,
1607 struct rb_root
*root_in
,
1608 struct rb_root
*root_out
,
1609 u64 min_callchain_hits
,
1612 struct rb_node
*node
;
1613 struct hist_entry
*he
;
1615 *root_out
= RB_ROOT
;
1616 node
= rb_first(root_in
);
1619 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
1620 node
= rb_next(node
);
1622 hierarchy_insert_output_entry(root_out
, he
);
1625 ui_progress__update(prog
, 1);
1627 hists
->nr_entries
++;
1628 if (!he
->filtered
) {
1629 hists
->nr_non_filtered_entries
++;
1630 hists__calc_col_len(hists
, he
);
1634 hists__hierarchy_output_resort(hists
, prog
,
1645 if (callchain_param
.mode
== CHAIN_GRAPH_REL
) {
1646 u64 total
= he
->stat
.period
;
1648 if (symbol_conf
.cumulate_callchain
)
1649 total
= he
->stat_acc
->period
;
1651 min_callchain_hits
= total
* (callchain_param
.min_percent
/ 100);
1654 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
1655 min_callchain_hits
, &callchain_param
);
1659 static void __hists__insert_output_entry(struct rb_root
*entries
,
1660 struct hist_entry
*he
,
1661 u64 min_callchain_hits
,
1664 struct rb_node
**p
= &entries
->rb_node
;
1665 struct rb_node
*parent
= NULL
;
1666 struct hist_entry
*iter
;
1667 struct perf_hpp_fmt
*fmt
;
1669 if (use_callchain
) {
1670 if (callchain_param
.mode
== CHAIN_GRAPH_REL
) {
1671 u64 total
= he
->stat
.period
;
1673 if (symbol_conf
.cumulate_callchain
)
1674 total
= he
->stat_acc
->period
;
1676 min_callchain_hits
= total
* (callchain_param
.min_percent
/ 100);
1678 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
1679 min_callchain_hits
, &callchain_param
);
1682 while (*p
!= NULL
) {
1684 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
1686 if (hist_entry__sort(he
, iter
) > 0)
1689 p
= &(*p
)->rb_right
;
1692 rb_link_node(&he
->rb_node
, parent
, p
);
1693 rb_insert_color(&he
->rb_node
, entries
);
1695 perf_hpp_list__for_each_sort_list(&perf_hpp_list
, fmt
) {
1696 if (perf_hpp__is_dynamic_entry(fmt
) &&
1697 perf_hpp__defined_dynamic_entry(fmt
, he
->hists
))
1698 fmt
->sort(fmt
, he
, NULL
); /* update column width */
1702 static void output_resort(struct hists
*hists
, struct ui_progress
*prog
,
1703 bool use_callchain
, hists__resort_cb_t cb
)
1705 struct rb_root
*root
;
1706 struct rb_node
*next
;
1707 struct hist_entry
*n
;
1708 u64 callchain_total
;
1709 u64 min_callchain_hits
;
1711 callchain_total
= hists
->callchain_period
;
1712 if (symbol_conf
.filter_relative
)
1713 callchain_total
= hists
->callchain_non_filtered_period
;
1715 min_callchain_hits
= callchain_total
* (callchain_param
.min_percent
/ 100);
1717 hists__reset_stats(hists
);
1718 hists__reset_col_len(hists
);
1720 if (symbol_conf
.report_hierarchy
) {
1721 hists__hierarchy_output_resort(hists
, prog
,
1722 &hists
->entries_collapsed
,
1726 hierarchy_recalc_total_periods(hists
);
1730 if (hists__has(hists
, need_collapse
))
1731 root
= &hists
->entries_collapsed
;
1733 root
= hists
->entries_in
;
1735 next
= rb_first(root
);
1736 hists
->entries
= RB_ROOT
;
1739 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
1740 next
= rb_next(&n
->rb_node_in
);
1745 __hists__insert_output_entry(&hists
->entries
, n
, min_callchain_hits
, use_callchain
);
1746 hists__inc_stats(hists
, n
);
1749 hists__calc_col_len(hists
, n
);
1752 ui_progress__update(prog
, 1);
1756 void perf_evsel__output_resort(struct perf_evsel
*evsel
, struct ui_progress
*prog
)
1760 if (evsel
&& symbol_conf
.use_callchain
&& !symbol_conf
.show_ref_callgraph
)
1761 use_callchain
= evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
;
1763 use_callchain
= symbol_conf
.use_callchain
;
1765 output_resort(evsel__hists(evsel
), prog
, use_callchain
, NULL
);
1768 void hists__output_resort(struct hists
*hists
, struct ui_progress
*prog
)
1770 output_resort(hists
, prog
, symbol_conf
.use_callchain
, NULL
);
1773 void hists__output_resort_cb(struct hists
*hists
, struct ui_progress
*prog
,
1774 hists__resort_cb_t cb
)
1776 output_resort(hists
, prog
, symbol_conf
.use_callchain
, cb
);
1779 static bool can_goto_child(struct hist_entry
*he
, enum hierarchy_move_dir hmd
)
1781 if (he
->leaf
|| hmd
== HMD_FORCE_SIBLING
)
1784 if (he
->unfolded
|| hmd
== HMD_FORCE_CHILD
)
1790 struct rb_node
*rb_hierarchy_last(struct rb_node
*node
)
1792 struct hist_entry
*he
= rb_entry(node
, struct hist_entry
, rb_node
);
1794 while (can_goto_child(he
, HMD_NORMAL
)) {
1795 node
= rb_last(&he
->hroot_out
);
1796 he
= rb_entry(node
, struct hist_entry
, rb_node
);
1801 struct rb_node
*__rb_hierarchy_next(struct rb_node
*node
, enum hierarchy_move_dir hmd
)
1803 struct hist_entry
*he
= rb_entry(node
, struct hist_entry
, rb_node
);
1805 if (can_goto_child(he
, hmd
))
1806 node
= rb_first(&he
->hroot_out
);
1808 node
= rb_next(node
);
1810 while (node
== NULL
) {
1815 node
= rb_next(&he
->rb_node
);
1820 struct rb_node
*rb_hierarchy_prev(struct rb_node
*node
)
1822 struct hist_entry
*he
= rb_entry(node
, struct hist_entry
, rb_node
);
1824 node
= rb_prev(node
);
1826 return rb_hierarchy_last(node
);
1832 return &he
->rb_node
;
1835 bool hist_entry__has_hierarchy_children(struct hist_entry
*he
, float limit
)
1837 struct rb_node
*node
;
1838 struct hist_entry
*child
;
1844 node
= rb_first(&he
->hroot_out
);
1845 child
= rb_entry(node
, struct hist_entry
, rb_node
);
1847 while (node
&& child
->filtered
) {
1848 node
= rb_next(node
);
1849 child
= rb_entry(node
, struct hist_entry
, rb_node
);
1853 percent
= hist_entry__get_percent_limit(child
);
1857 return node
&& percent
>= limit
;
1860 static void hists__remove_entry_filter(struct hists
*hists
, struct hist_entry
*h
,
1861 enum hist_filter filter
)
1863 h
->filtered
&= ~(1 << filter
);
1865 if (symbol_conf
.report_hierarchy
) {
1866 struct hist_entry
*parent
= h
->parent_he
;
1869 he_stat__add_stat(&parent
->stat
, &h
->stat
);
1871 parent
->filtered
&= ~(1 << filter
);
1873 if (parent
->filtered
)
1876 /* force fold unfiltered entry for simplicity */
1877 parent
->unfolded
= false;
1878 parent
->has_no_entry
= false;
1879 parent
->row_offset
= 0;
1880 parent
->nr_rows
= 0;
1882 parent
= parent
->parent_he
;
1889 /* force fold unfiltered entry for simplicity */
1890 h
->unfolded
= false;
1891 h
->has_no_entry
= false;
1895 hists
->stats
.nr_non_filtered_samples
+= h
->stat
.nr_events
;
1897 hists__inc_filter_stats(hists
, h
);
1898 hists__calc_col_len(hists
, h
);
1902 static bool hists__filter_entry_by_dso(struct hists
*hists
,
1903 struct hist_entry
*he
)
1905 if (hists
->dso_filter
!= NULL
&&
1906 (he
->ms
.map
== NULL
|| he
->ms
.map
->dso
!= hists
->dso_filter
)) {
1907 he
->filtered
|= (1 << HIST_FILTER__DSO
);
1914 static bool hists__filter_entry_by_thread(struct hists
*hists
,
1915 struct hist_entry
*he
)
1917 if (hists
->thread_filter
!= NULL
&&
1918 he
->thread
!= hists
->thread_filter
) {
1919 he
->filtered
|= (1 << HIST_FILTER__THREAD
);
1926 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
1927 struct hist_entry
*he
)
1929 if (hists
->symbol_filter_str
!= NULL
&&
1930 (!he
->ms
.sym
|| strstr(he
->ms
.sym
->name
,
1931 hists
->symbol_filter_str
) == NULL
)) {
1932 he
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
1939 static bool hists__filter_entry_by_socket(struct hists
*hists
,
1940 struct hist_entry
*he
)
1942 if ((hists
->socket_filter
> -1) &&
1943 (he
->socket
!= hists
->socket_filter
)) {
1944 he
->filtered
|= (1 << HIST_FILTER__SOCKET
);
1951 typedef bool (*filter_fn_t
)(struct hists
*hists
, struct hist_entry
*he
);
1953 static void hists__filter_by_type(struct hists
*hists
, int type
, filter_fn_t filter
)
1957 hists
->stats
.nr_non_filtered_samples
= 0;
1959 hists__reset_filter_stats(hists
);
1960 hists__reset_col_len(hists
);
1962 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
1963 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1965 if (filter(hists
, h
))
1968 hists__remove_entry_filter(hists
, h
, type
);
1972 static void resort_filtered_entry(struct rb_root
*root
, struct hist_entry
*he
)
1974 struct rb_node
**p
= &root
->rb_node
;
1975 struct rb_node
*parent
= NULL
;
1976 struct hist_entry
*iter
;
1977 struct rb_root new_root
= RB_ROOT
;
1980 while (*p
!= NULL
) {
1982 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
1984 if (hist_entry__sort(he
, iter
) > 0)
1987 p
= &(*p
)->rb_right
;
1990 rb_link_node(&he
->rb_node
, parent
, p
);
1991 rb_insert_color(&he
->rb_node
, root
);
1993 if (he
->leaf
|| he
->filtered
)
1996 nd
= rb_first(&he
->hroot_out
);
1998 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
2001 rb_erase(&h
->rb_node
, &he
->hroot_out
);
2003 resort_filtered_entry(&new_root
, h
);
2006 he
->hroot_out
= new_root
;
2009 static void hists__filter_hierarchy(struct hists
*hists
, int type
, const void *arg
)
2012 struct rb_root new_root
= RB_ROOT
;
2014 hists
->stats
.nr_non_filtered_samples
= 0;
2016 hists__reset_filter_stats(hists
);
2017 hists__reset_col_len(hists
);
2019 nd
= rb_first(&hists
->entries
);
2021 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
2024 ret
= hist_entry__filter(h
, type
, arg
);
2027 * case 1. non-matching type
2028 * zero out the period, set filter marker and move to child
2031 memset(&h
->stat
, 0, sizeof(h
->stat
));
2032 h
->filtered
|= (1 << type
);
2034 nd
= __rb_hierarchy_next(&h
->rb_node
, HMD_FORCE_CHILD
);
2037 * case 2. matched type (filter out)
2038 * set filter marker and move to next
2040 else if (ret
== 1) {
2041 h
->filtered
|= (1 << type
);
2043 nd
= __rb_hierarchy_next(&h
->rb_node
, HMD_FORCE_SIBLING
);
2046 * case 3. ok (not filtered)
2047 * add period to hists and parents, erase the filter marker
2048 * and move to next sibling
2051 hists__remove_entry_filter(hists
, h
, type
);
2053 nd
= __rb_hierarchy_next(&h
->rb_node
, HMD_FORCE_SIBLING
);
2057 hierarchy_recalc_total_periods(hists
);
2060 * resort output after applying a new filter since filter in a lower
2061 * hierarchy can change periods in a upper hierarchy.
2063 nd
= rb_first(&hists
->entries
);
2065 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
2068 rb_erase(&h
->rb_node
, &hists
->entries
);
2070 resort_filtered_entry(&new_root
, h
);
2073 hists
->entries
= new_root
;
2076 void hists__filter_by_thread(struct hists
*hists
)
2078 if (symbol_conf
.report_hierarchy
)
2079 hists__filter_hierarchy(hists
, HIST_FILTER__THREAD
,
2080 hists
->thread_filter
);
2082 hists__filter_by_type(hists
, HIST_FILTER__THREAD
,
2083 hists__filter_entry_by_thread
);
2086 void hists__filter_by_dso(struct hists
*hists
)
2088 if (symbol_conf
.report_hierarchy
)
2089 hists__filter_hierarchy(hists
, HIST_FILTER__DSO
,
2092 hists__filter_by_type(hists
, HIST_FILTER__DSO
,
2093 hists__filter_entry_by_dso
);
2096 void hists__filter_by_symbol(struct hists
*hists
)
2098 if (symbol_conf
.report_hierarchy
)
2099 hists__filter_hierarchy(hists
, HIST_FILTER__SYMBOL
,
2100 hists
->symbol_filter_str
);
2102 hists__filter_by_type(hists
, HIST_FILTER__SYMBOL
,
2103 hists__filter_entry_by_symbol
);
2106 void hists__filter_by_socket(struct hists
*hists
)
2108 if (symbol_conf
.report_hierarchy
)
2109 hists__filter_hierarchy(hists
, HIST_FILTER__SOCKET
,
2110 &hists
->socket_filter
);
2112 hists__filter_by_type(hists
, HIST_FILTER__SOCKET
,
2113 hists__filter_entry_by_socket
);
2116 void events_stats__inc(struct events_stats
*stats
, u32 type
)
2118 ++stats
->nr_events
[0];
2119 ++stats
->nr_events
[type
];
2122 void hists__inc_nr_events(struct hists
*hists
, u32 type
)
2124 events_stats__inc(&hists
->stats
, type
);
2127 void hists__inc_nr_samples(struct hists
*hists
, bool filtered
)
2129 events_stats__inc(&hists
->stats
, PERF_RECORD_SAMPLE
);
2131 hists
->stats
.nr_non_filtered_samples
++;
2134 static struct hist_entry
*hists__add_dummy_entry(struct hists
*hists
,
2135 struct hist_entry
*pair
)
2137 struct rb_root
*root
;
2139 struct rb_node
*parent
= NULL
;
2140 struct hist_entry
*he
;
2143 if (hists__has(hists
, need_collapse
))
2144 root
= &hists
->entries_collapsed
;
2146 root
= hists
->entries_in
;
2150 while (*p
!= NULL
) {
2152 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
2154 cmp
= hist_entry__collapse(he
, pair
);
2162 p
= &(*p
)->rb_right
;
2165 he
= hist_entry__new(pair
, true);
2167 memset(&he
->stat
, 0, sizeof(he
->stat
));
2169 if (symbol_conf
.cumulate_callchain
)
2170 memset(he
->stat_acc
, 0, sizeof(he
->stat
));
2171 rb_link_node(&he
->rb_node_in
, parent
, p
);
2172 rb_insert_color(&he
->rb_node_in
, root
);
2173 hists__inc_stats(hists
, he
);
2180 static struct hist_entry
*add_dummy_hierarchy_entry(struct hists
*hists
,
2181 struct rb_root
*root
,
2182 struct hist_entry
*pair
)
2185 struct rb_node
*parent
= NULL
;
2186 struct hist_entry
*he
;
2187 struct perf_hpp_fmt
*fmt
;
2190 while (*p
!= NULL
) {
2194 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
2196 perf_hpp_list__for_each_sort_list(he
->hpp_list
, fmt
) {
2197 cmp
= fmt
->collapse(fmt
, he
, pair
);
2205 p
= &parent
->rb_left
;
2207 p
= &parent
->rb_right
;
2210 he
= hist_entry__new(pair
, true);
2212 rb_link_node(&he
->rb_node_in
, parent
, p
);
2213 rb_insert_color(&he
->rb_node_in
, root
);
2217 memset(&he
->stat
, 0, sizeof(he
->stat
));
2218 hists__inc_stats(hists
, he
);
2224 static struct hist_entry
*hists__find_entry(struct hists
*hists
,
2225 struct hist_entry
*he
)
2229 if (hists__has(hists
, need_collapse
))
2230 n
= hists
->entries_collapsed
.rb_node
;
2232 n
= hists
->entries_in
->rb_node
;
2235 struct hist_entry
*iter
= rb_entry(n
, struct hist_entry
, rb_node_in
);
2236 int64_t cmp
= hist_entry__collapse(iter
, he
);
2249 static struct hist_entry
*hists__find_hierarchy_entry(struct rb_root
*root
,
2250 struct hist_entry
*he
)
2252 struct rb_node
*n
= root
->rb_node
;
2255 struct hist_entry
*iter
;
2256 struct perf_hpp_fmt
*fmt
;
2259 iter
= rb_entry(n
, struct hist_entry
, rb_node_in
);
2260 perf_hpp_list__for_each_sort_list(he
->hpp_list
, fmt
) {
2261 cmp
= fmt
->collapse(fmt
, iter
, he
);
2277 static void hists__match_hierarchy(struct rb_root
*leader_root
,
2278 struct rb_root
*other_root
)
2281 struct hist_entry
*pos
, *pair
;
2283 for (nd
= rb_first(leader_root
); nd
; nd
= rb_next(nd
)) {
2284 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2285 pair
= hists__find_hierarchy_entry(other_root
, pos
);
2288 hist_entry__add_pair(pair
, pos
);
2289 hists__match_hierarchy(&pos
->hroot_in
, &pair
->hroot_in
);
2295 * Look for pairs to link to the leader buckets (hist_entries):
2297 void hists__match(struct hists
*leader
, struct hists
*other
)
2299 struct rb_root
*root
;
2301 struct hist_entry
*pos
, *pair
;
2303 if (symbol_conf
.report_hierarchy
) {
2304 /* hierarchy report always collapses entries */
2305 return hists__match_hierarchy(&leader
->entries_collapsed
,
2306 &other
->entries_collapsed
);
2309 if (hists__has(leader
, need_collapse
))
2310 root
= &leader
->entries_collapsed
;
2312 root
= leader
->entries_in
;
2314 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
2315 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2316 pair
= hists__find_entry(other
, pos
);
2319 hist_entry__add_pair(pair
, pos
);
2323 static int hists__link_hierarchy(struct hists
*leader_hists
,
2324 struct hist_entry
*parent
,
2325 struct rb_root
*leader_root
,
2326 struct rb_root
*other_root
)
2329 struct hist_entry
*pos
, *leader
;
2331 for (nd
= rb_first(other_root
); nd
; nd
= rb_next(nd
)) {
2332 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2334 if (hist_entry__has_pairs(pos
)) {
2337 list_for_each_entry(leader
, &pos
->pairs
.head
, pairs
.node
) {
2338 if (leader
->hists
== leader_hists
) {
2346 leader
= add_dummy_hierarchy_entry(leader_hists
,
2351 /* do not point parent in the pos */
2352 leader
->parent_he
= parent
;
2354 hist_entry__add_pair(pos
, leader
);
2358 if (hists__link_hierarchy(leader_hists
, leader
,
2360 &pos
->hroot_in
) < 0)
2368 * Look for entries in the other hists that are not present in the leader, if
2369 * we find them, just add a dummy entry on the leader hists, with period=0,
2370 * nr_events=0, to serve as the list header.
2372 int hists__link(struct hists
*leader
, struct hists
*other
)
2374 struct rb_root
*root
;
2376 struct hist_entry
*pos
, *pair
;
2378 if (symbol_conf
.report_hierarchy
) {
2379 /* hierarchy report always collapses entries */
2380 return hists__link_hierarchy(leader
, NULL
,
2381 &leader
->entries_collapsed
,
2382 &other
->entries_collapsed
);
2385 if (hists__has(other
, need_collapse
))
2386 root
= &other
->entries_collapsed
;
2388 root
= other
->entries_in
;
2390 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
2391 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
2393 if (!hist_entry__has_pairs(pos
)) {
2394 pair
= hists__add_dummy_entry(leader
, pos
);
2397 hist_entry__add_pair(pos
, pair
);
2404 void hist__account_cycles(struct branch_stack
*bs
, struct addr_location
*al
,
2405 struct perf_sample
*sample
, bool nonany_branch_mode
)
2407 struct branch_info
*bi
;
2409 /* If we have branch cycles always annotate them. */
2410 if (bs
&& bs
->nr
&& bs
->entries
[0].flags
.cycles
) {
2413 bi
= sample__resolve_bstack(sample
, al
);
2415 struct addr_map_symbol
*prev
= NULL
;
2418 * Ignore errors, still want to process the
2421 * For non standard branch modes always
2422 * force no IPC (prev == NULL)
2424 * Note that perf stores branches reversed from
2427 for (i
= bs
->nr
- 1; i
>= 0; i
--) {
2428 addr_map_symbol__account_cycles(&bi
[i
].from
,
2429 nonany_branch_mode
? NULL
: prev
,
2430 bi
[i
].flags
.cycles
);
2438 size_t perf_evlist__fprintf_nr_events(struct perf_evlist
*evlist
, FILE *fp
)
2440 struct perf_evsel
*pos
;
2443 evlist__for_each_entry(evlist
, pos
) {
2444 ret
+= fprintf(fp
, "%s stats:\n", perf_evsel__name(pos
));
2445 ret
+= events_stats__fprintf(&evsel__hists(pos
)->stats
, fp
);
2452 u64
hists__total_period(struct hists
*hists
)
2454 return symbol_conf
.filter_relative
? hists
->stats
.total_non_filtered_period
:
2455 hists
->stats
.total_period
;
2458 int parse_filter_percentage(const struct option
*opt __maybe_unused
,
2459 const char *arg
, int unset __maybe_unused
)
2461 if (!strcmp(arg
, "relative"))
2462 symbol_conf
.filter_relative
= true;
2463 else if (!strcmp(arg
, "absolute"))
2464 symbol_conf
.filter_relative
= false;
2466 pr_debug("Invalid percentage: %s\n", arg
);
2473 int perf_hist_config(const char *var
, const char *value
)
2475 if (!strcmp(var
, "hist.percentage"))
2476 return parse_filter_percentage(NULL
, value
, 0);
2481 int __hists__init(struct hists
*hists
, struct perf_hpp_list
*hpp_list
)
2483 memset(hists
, 0, sizeof(*hists
));
2484 hists
->entries_in_array
[0] = hists
->entries_in_array
[1] = RB_ROOT
;
2485 hists
->entries_in
= &hists
->entries_in_array
[0];
2486 hists
->entries_collapsed
= RB_ROOT
;
2487 hists
->entries
= RB_ROOT
;
2488 pthread_mutex_init(&hists
->lock
, NULL
);
2489 hists
->socket_filter
= -1;
2490 hists
->hpp_list
= hpp_list
;
2491 INIT_LIST_HEAD(&hists
->hpp_formats
);
2495 static void hists__delete_remaining_entries(struct rb_root
*root
)
2497 struct rb_node
*node
;
2498 struct hist_entry
*he
;
2500 while (!RB_EMPTY_ROOT(root
)) {
2501 node
= rb_first(root
);
2502 rb_erase(node
, root
);
2504 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
2505 hist_entry__delete(he
);
2509 static void hists__delete_all_entries(struct hists
*hists
)
2511 hists__delete_entries(hists
);
2512 hists__delete_remaining_entries(&hists
->entries_in_array
[0]);
2513 hists__delete_remaining_entries(&hists
->entries_in_array
[1]);
2514 hists__delete_remaining_entries(&hists
->entries_collapsed
);
2517 static void hists_evsel__exit(struct perf_evsel
*evsel
)
2519 struct hists
*hists
= evsel__hists(evsel
);
2520 struct perf_hpp_fmt
*fmt
, *pos
;
2521 struct perf_hpp_list_node
*node
, *tmp
;
2523 hists__delete_all_entries(hists
);
2525 list_for_each_entry_safe(node
, tmp
, &hists
->hpp_formats
, list
) {
2526 perf_hpp_list__for_each_format_safe(&node
->hpp
, fmt
, pos
) {
2527 list_del(&fmt
->list
);
2530 list_del(&node
->list
);
2535 static int hists_evsel__init(struct perf_evsel
*evsel
)
2537 struct hists
*hists
= evsel__hists(evsel
);
2539 __hists__init(hists
, &perf_hpp_list
);
2544 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2545 * stored in the rbtree...
2548 int hists__init(void)
2550 int err
= perf_evsel__object_config(sizeof(struct hists_evsel
),
2554 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr
);
2559 void perf_hpp_list__init(struct perf_hpp_list
*list
)
2561 INIT_LIST_HEAD(&list
->fields
);
2562 INIT_LIST_HEAD(&list
->sorts
);