11 static bool hists__filter_entry_by_dso(struct hists
*hists
,
12 struct hist_entry
*he
);
13 static bool hists__filter_entry_by_thread(struct hists
*hists
,
14 struct hist_entry
*he
);
15 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
16 struct hist_entry
*he
);
18 u16
hists__col_len(struct hists
*hists
, enum hist_column col
)
20 return hists
->col_len
[col
];
23 void hists__set_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
25 hists
->col_len
[col
] = len
;
28 bool hists__new_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
30 if (len
> hists__col_len(hists
, col
)) {
31 hists__set_col_len(hists
, col
, len
);
37 void hists__reset_col_len(struct hists
*hists
)
41 for (col
= 0; col
< HISTC_NR_COLS
; ++col
)
42 hists__set_col_len(hists
, col
, 0);
45 static void hists__set_unres_dso_col_len(struct hists
*hists
, int dso
)
47 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
49 if (hists__col_len(hists
, dso
) < unresolved_col_width
&&
50 !symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
51 !symbol_conf
.dso_list
)
52 hists__set_col_len(hists
, dso
, unresolved_col_width
);
55 void hists__calc_col_len(struct hists
*hists
, struct hist_entry
*h
)
57 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
62 * +4 accounts for '[x] ' priv level info
63 * +2 accounts for 0x prefix on raw addresses
64 * +3 accounts for ' y ' symtab origin info
67 symlen
= h
->ms
.sym
->namelen
+ 4;
69 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
70 hists__new_col_len(hists
, HISTC_SYMBOL
, symlen
);
72 symlen
= unresolved_col_width
+ 4 + 2;
73 hists__new_col_len(hists
, HISTC_SYMBOL
, symlen
);
74 hists__set_unres_dso_col_len(hists
, HISTC_DSO
);
77 len
= thread__comm_len(h
->thread
);
78 if (hists__new_col_len(hists
, HISTC_COMM
, len
))
79 hists__set_col_len(hists
, HISTC_THREAD
, len
+ 6);
82 len
= dso__name_len(h
->ms
.map
->dso
);
83 hists__new_col_len(hists
, HISTC_DSO
, len
);
87 hists__new_col_len(hists
, HISTC_PARENT
, h
->parent
->namelen
);
90 if (h
->branch_info
->from
.sym
) {
91 symlen
= (int)h
->branch_info
->from
.sym
->namelen
+ 4;
93 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
94 hists__new_col_len(hists
, HISTC_SYMBOL_FROM
, symlen
);
96 symlen
= dso__name_len(h
->branch_info
->from
.map
->dso
);
97 hists__new_col_len(hists
, HISTC_DSO_FROM
, symlen
);
99 symlen
= unresolved_col_width
+ 4 + 2;
100 hists__new_col_len(hists
, HISTC_SYMBOL_FROM
, symlen
);
101 hists__set_unres_dso_col_len(hists
, HISTC_DSO_FROM
);
104 if (h
->branch_info
->to
.sym
) {
105 symlen
= (int)h
->branch_info
->to
.sym
->namelen
+ 4;
107 symlen
+= BITS_PER_LONG
/ 4 + 2 + 3;
108 hists__new_col_len(hists
, HISTC_SYMBOL_TO
, symlen
);
110 symlen
= dso__name_len(h
->branch_info
->to
.map
->dso
);
111 hists__new_col_len(hists
, HISTC_DSO_TO
, symlen
);
113 symlen
= unresolved_col_width
+ 4 + 2;
114 hists__new_col_len(hists
, HISTC_SYMBOL_TO
, symlen
);
115 hists__set_unres_dso_col_len(hists
, HISTC_DSO_TO
);
120 if (h
->mem_info
->daddr
.sym
) {
121 symlen
= (int)h
->mem_info
->daddr
.sym
->namelen
+ 4
122 + unresolved_col_width
+ 2;
123 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
,
125 hists__new_col_len(hists
, HISTC_MEM_DCACHELINE
,
128 symlen
= unresolved_col_width
+ 4 + 2;
129 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
,
132 if (h
->mem_info
->daddr
.map
) {
133 symlen
= dso__name_len(h
->mem_info
->daddr
.map
->dso
);
134 hists__new_col_len(hists
, HISTC_MEM_DADDR_DSO
,
137 symlen
= unresolved_col_width
+ 4 + 2;
138 hists__set_unres_dso_col_len(hists
, HISTC_MEM_DADDR_DSO
);
141 symlen
= unresolved_col_width
+ 4 + 2;
142 hists__new_col_len(hists
, HISTC_MEM_DADDR_SYMBOL
, symlen
);
143 hists__set_unres_dso_col_len(hists
, HISTC_MEM_DADDR_DSO
);
146 hists__new_col_len(hists
, HISTC_MEM_LOCKED
, 6);
147 hists__new_col_len(hists
, HISTC_MEM_TLB
, 22);
148 hists__new_col_len(hists
, HISTC_MEM_SNOOP
, 12);
149 hists__new_col_len(hists
, HISTC_MEM_LVL
, 21 + 3);
150 hists__new_col_len(hists
, HISTC_LOCAL_WEIGHT
, 12);
151 hists__new_col_len(hists
, HISTC_GLOBAL_WEIGHT
, 12);
154 hists__new_col_len(hists
, HISTC_TRANSACTION
,
155 hist_entry__transaction_len());
158 void hists__output_recalc_col_len(struct hists
*hists
, int max_rows
)
160 struct rb_node
*next
= rb_first(&hists
->entries
);
161 struct hist_entry
*n
;
164 hists__reset_col_len(hists
);
166 while (next
&& row
++ < max_rows
) {
167 n
= rb_entry(next
, struct hist_entry
, rb_node
);
169 hists__calc_col_len(hists
, n
);
170 next
= rb_next(&n
->rb_node
);
174 static void he_stat__add_cpumode_period(struct he_stat
*he_stat
,
175 unsigned int cpumode
, u64 period
)
178 case PERF_RECORD_MISC_KERNEL
:
179 he_stat
->period_sys
+= period
;
181 case PERF_RECORD_MISC_USER
:
182 he_stat
->period_us
+= period
;
184 case PERF_RECORD_MISC_GUEST_KERNEL
:
185 he_stat
->period_guest_sys
+= period
;
187 case PERF_RECORD_MISC_GUEST_USER
:
188 he_stat
->period_guest_us
+= period
;
195 static void he_stat__add_period(struct he_stat
*he_stat
, u64 period
,
199 he_stat
->period
+= period
;
200 he_stat
->weight
+= weight
;
201 he_stat
->nr_events
+= 1;
204 static void he_stat__add_stat(struct he_stat
*dest
, struct he_stat
*src
)
206 dest
->period
+= src
->period
;
207 dest
->period_sys
+= src
->period_sys
;
208 dest
->period_us
+= src
->period_us
;
209 dest
->period_guest_sys
+= src
->period_guest_sys
;
210 dest
->period_guest_us
+= src
->period_guest_us
;
211 dest
->nr_events
+= src
->nr_events
;
212 dest
->weight
+= src
->weight
;
215 static void he_stat__decay(struct he_stat
*he_stat
)
217 he_stat
->period
= (he_stat
->period
* 7) / 8;
218 he_stat
->nr_events
= (he_stat
->nr_events
* 7) / 8;
219 /* XXX need decay for weight too? */
222 static bool hists__decay_entry(struct hists
*hists
, struct hist_entry
*he
)
224 u64 prev_period
= he
->stat
.period
;
227 if (prev_period
== 0)
230 he_stat__decay(&he
->stat
);
231 if (symbol_conf
.cumulate_callchain
)
232 he_stat__decay(he
->stat_acc
);
234 diff
= prev_period
- he
->stat
.period
;
236 hists
->stats
.total_period
-= diff
;
238 hists
->stats
.total_non_filtered_period
-= diff
;
240 return he
->stat
.period
== 0;
243 void hists__decay_entries(struct hists
*hists
, bool zap_user
, bool zap_kernel
)
245 struct rb_node
*next
= rb_first(&hists
->entries
);
246 struct hist_entry
*n
;
249 n
= rb_entry(next
, struct hist_entry
, rb_node
);
250 next
= rb_next(&n
->rb_node
);
252 * We may be annotating this, for instance, so keep it here in
253 * case some it gets new samples, we'll eventually free it when
254 * the user stops browsing and it agains gets fully decayed.
256 if (((zap_user
&& n
->level
== '.') ||
257 (zap_kernel
&& n
->level
!= '.') ||
258 hists__decay_entry(hists
, n
)) &&
260 rb_erase(&n
->rb_node
, &hists
->entries
);
262 if (sort__need_collapse
)
263 rb_erase(&n
->rb_node_in
, &hists
->entries_collapsed
);
267 --hists
->nr_non_filtered_entries
;
274 void hists__delete_entries(struct hists
*hists
)
276 struct rb_node
*next
= rb_first(&hists
->entries
);
277 struct hist_entry
*n
;
280 n
= rb_entry(next
, struct hist_entry
, rb_node
);
281 next
= rb_next(&n
->rb_node
);
283 rb_erase(&n
->rb_node
, &hists
->entries
);
285 if (sort__need_collapse
)
286 rb_erase(&n
->rb_node_in
, &hists
->entries_collapsed
);
290 --hists
->nr_non_filtered_entries
;
297 * histogram, sorted on item, collects periods
300 static struct hist_entry
*hist_entry__new(struct hist_entry
*template,
303 size_t callchain_size
= 0;
304 struct hist_entry
*he
;
306 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
)
307 callchain_size
= sizeof(struct callchain_root
);
309 he
= zalloc(sizeof(*he
) + callchain_size
);
314 if (symbol_conf
.cumulate_callchain
) {
315 he
->stat_acc
= malloc(sizeof(he
->stat
));
316 if (he
->stat_acc
== NULL
) {
320 memcpy(he
->stat_acc
, &he
->stat
, sizeof(he
->stat
));
322 memset(&he
->stat
, 0, sizeof(he
->stat
));
326 he
->ms
.map
->referenced
= true;
328 if (he
->branch_info
) {
330 * This branch info is (a part of) allocated from
331 * sample__resolve_bstack() and will be freed after
332 * adding new entries. So we need to save a copy.
334 he
->branch_info
= malloc(sizeof(*he
->branch_info
));
335 if (he
->branch_info
== NULL
) {
341 memcpy(he
->branch_info
, template->branch_info
,
342 sizeof(*he
->branch_info
));
344 if (he
->branch_info
->from
.map
)
345 he
->branch_info
->from
.map
->referenced
= true;
346 if (he
->branch_info
->to
.map
)
347 he
->branch_info
->to
.map
->referenced
= true;
351 if (he
->mem_info
->iaddr
.map
)
352 he
->mem_info
->iaddr
.map
->referenced
= true;
353 if (he
->mem_info
->daddr
.map
)
354 he
->mem_info
->daddr
.map
->referenced
= true;
357 if (symbol_conf
.use_callchain
)
358 callchain_init(he
->callchain
);
360 INIT_LIST_HEAD(&he
->pairs
.node
);
366 static u8
symbol__parent_filter(const struct symbol
*parent
)
368 if (symbol_conf
.exclude_other
&& parent
== NULL
)
369 return 1 << HIST_FILTER__PARENT
;
373 static struct hist_entry
*add_hist_entry(struct hists
*hists
,
374 struct hist_entry
*entry
,
375 struct addr_location
*al
,
379 struct rb_node
*parent
= NULL
;
380 struct hist_entry
*he
;
382 u64 period
= entry
->stat
.period
;
383 u64 weight
= entry
->stat
.weight
;
385 p
= &hists
->entries_in
->rb_node
;
389 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
392 * Make sure that it receives arguments in a same order as
393 * hist_entry__collapse() so that we can use an appropriate
394 * function when searching an entry regardless which sort
397 cmp
= hist_entry__cmp(he
, entry
);
401 he_stat__add_period(&he
->stat
, period
, weight
);
402 if (symbol_conf
.cumulate_callchain
)
403 he_stat__add_period(he
->stat_acc
, period
, weight
);
406 * This mem info was allocated from sample__resolve_mem
407 * and will not be used anymore.
409 zfree(&entry
->mem_info
);
411 /* If the map of an existing hist_entry has
412 * become out-of-date due to an exec() or
413 * similar, update it. Otherwise we will
414 * mis-adjust symbol addresses when computing
415 * the history counter to increment.
417 if (he
->ms
.map
!= entry
->ms
.map
) {
418 he
->ms
.map
= entry
->ms
.map
;
420 he
->ms
.map
->referenced
= true;
431 he
= hist_entry__new(entry
, sample_self
);
435 rb_link_node(&he
->rb_node_in
, parent
, p
);
436 rb_insert_color(&he
->rb_node_in
, hists
->entries_in
);
439 he_stat__add_cpumode_period(&he
->stat
, al
->cpumode
, period
);
440 if (symbol_conf
.cumulate_callchain
)
441 he_stat__add_cpumode_period(he
->stat_acc
, al
->cpumode
, period
);
445 struct hist_entry
*__hists__add_entry(struct hists
*hists
,
446 struct addr_location
*al
,
447 struct symbol
*sym_parent
,
448 struct branch_info
*bi
,
450 u64 period
, u64 weight
, u64 transaction
,
453 struct hist_entry entry
= {
454 .thread
= al
->thread
,
455 .comm
= thread__comm(al
->thread
),
461 .cpumode
= al
->cpumode
,
469 .parent
= sym_parent
,
470 .filtered
= symbol__parent_filter(sym_parent
) | al
->filtered
,
474 .transaction
= transaction
,
477 return add_hist_entry(hists
, &entry
, al
, sample_self
);
481 iter_next_nop_entry(struct hist_entry_iter
*iter __maybe_unused
,
482 struct addr_location
*al __maybe_unused
)
488 iter_add_next_nop_entry(struct hist_entry_iter
*iter __maybe_unused
,
489 struct addr_location
*al __maybe_unused
)
495 iter_prepare_mem_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
497 struct perf_sample
*sample
= iter
->sample
;
500 mi
= sample__resolve_mem(sample
, al
);
509 iter_add_single_mem_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
512 struct mem_info
*mi
= iter
->priv
;
513 struct hists
*hists
= evsel__hists(iter
->evsel
);
514 struct hist_entry
*he
;
519 cost
= iter
->sample
->weight
;
524 * must pass period=weight in order to get the correct
525 * sorting from hists__collapse_resort() which is solely
526 * based on periods. We want sorting be done on nr_events * weight
527 * and this is indirectly achieved by passing period=weight here
528 * and the he_stat__add_period() function.
530 he
= __hists__add_entry(hists
, al
, iter
->parent
, NULL
, mi
,
531 cost
, cost
, 0, true);
540 iter_finish_mem_entry(struct hist_entry_iter
*iter
,
541 struct addr_location
*al __maybe_unused
)
543 struct perf_evsel
*evsel
= iter
->evsel
;
544 struct hists
*hists
= evsel__hists(evsel
);
545 struct hist_entry
*he
= iter
->he
;
551 hists__inc_nr_samples(hists
, he
->filtered
);
553 err
= hist_entry__append_callchain(he
, iter
->sample
);
557 * We don't need to free iter->priv (mem_info) here since
558 * the mem info was either already freed in add_hist_entry() or
559 * passed to a new hist entry by hist_entry__new().
568 iter_prepare_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
570 struct branch_info
*bi
;
571 struct perf_sample
*sample
= iter
->sample
;
573 bi
= sample__resolve_bstack(sample
, al
);
578 iter
->total
= sample
->branch_stack
->nr
;
585 iter_add_single_branch_entry(struct hist_entry_iter
*iter __maybe_unused
,
586 struct addr_location
*al __maybe_unused
)
588 /* to avoid calling callback function */
595 iter_next_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
597 struct branch_info
*bi
= iter
->priv
;
603 if (iter
->curr
>= iter
->total
)
606 al
->map
= bi
[i
].to
.map
;
607 al
->sym
= bi
[i
].to
.sym
;
608 al
->addr
= bi
[i
].to
.addr
;
613 iter_add_next_branch_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
615 struct branch_info
*bi
;
616 struct perf_evsel
*evsel
= iter
->evsel
;
617 struct hists
*hists
= evsel__hists(evsel
);
618 struct hist_entry
*he
= NULL
;
624 if (iter
->hide_unresolved
&& !(bi
[i
].from
.sym
&& bi
[i
].to
.sym
))
628 * The report shows the percentage of total branches captured
629 * and not events sampled. Thus we use a pseudo period of 1.
631 he
= __hists__add_entry(hists
, al
, iter
->parent
, &bi
[i
], NULL
,
636 hists__inc_nr_samples(hists
, he
->filtered
);
645 iter_finish_branch_entry(struct hist_entry_iter
*iter
,
646 struct addr_location
*al __maybe_unused
)
651 return iter
->curr
>= iter
->total
? 0 : -1;
655 iter_prepare_normal_entry(struct hist_entry_iter
*iter __maybe_unused
,
656 struct addr_location
*al __maybe_unused
)
662 iter_add_single_normal_entry(struct hist_entry_iter
*iter
, struct addr_location
*al
)
664 struct perf_evsel
*evsel
= iter
->evsel
;
665 struct perf_sample
*sample
= iter
->sample
;
666 struct hist_entry
*he
;
668 he
= __hists__add_entry(evsel__hists(evsel
), al
, iter
->parent
, NULL
, NULL
,
669 sample
->period
, sample
->weight
,
670 sample
->transaction
, true);
679 iter_finish_normal_entry(struct hist_entry_iter
*iter
,
680 struct addr_location
*al __maybe_unused
)
682 struct hist_entry
*he
= iter
->he
;
683 struct perf_evsel
*evsel
= iter
->evsel
;
684 struct perf_sample
*sample
= iter
->sample
;
691 hists__inc_nr_samples(evsel__hists(evsel
), he
->filtered
);
693 return hist_entry__append_callchain(he
, sample
);
697 iter_prepare_cumulative_entry(struct hist_entry_iter
*iter __maybe_unused
,
698 struct addr_location
*al __maybe_unused
)
700 struct hist_entry
**he_cache
;
702 callchain_cursor_commit(&callchain_cursor
);
705 * This is for detecting cycles or recursions so that they're
706 * cumulated only one time to prevent entries more than 100%
709 he_cache
= malloc(sizeof(*he_cache
) * (PERF_MAX_STACK_DEPTH
+ 1));
710 if (he_cache
== NULL
)
713 iter
->priv
= he_cache
;
720 iter_add_single_cumulative_entry(struct hist_entry_iter
*iter
,
721 struct addr_location
*al
)
723 struct perf_evsel
*evsel
= iter
->evsel
;
724 struct hists
*hists
= evsel__hists(evsel
);
725 struct perf_sample
*sample
= iter
->sample
;
726 struct hist_entry
**he_cache
= iter
->priv
;
727 struct hist_entry
*he
;
730 he
= __hists__add_entry(hists
, al
, iter
->parent
, NULL
, NULL
,
731 sample
->period
, sample
->weight
,
732 sample
->transaction
, true);
737 he_cache
[iter
->curr
++] = he
;
739 callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
742 * We need to re-initialize the cursor since callchain_append()
743 * advanced the cursor to the end.
745 callchain_cursor_commit(&callchain_cursor
);
747 hists__inc_nr_samples(hists
, he
->filtered
);
753 iter_next_cumulative_entry(struct hist_entry_iter
*iter
,
754 struct addr_location
*al
)
756 struct callchain_cursor_node
*node
;
758 node
= callchain_cursor_current(&callchain_cursor
);
762 return fill_callchain_info(al
, node
, iter
->hide_unresolved
);
766 iter_add_next_cumulative_entry(struct hist_entry_iter
*iter
,
767 struct addr_location
*al
)
769 struct perf_evsel
*evsel
= iter
->evsel
;
770 struct perf_sample
*sample
= iter
->sample
;
771 struct hist_entry
**he_cache
= iter
->priv
;
772 struct hist_entry
*he
;
773 struct hist_entry he_tmp
= {
775 .thread
= al
->thread
,
776 .comm
= thread__comm(al
->thread
),
782 .parent
= iter
->parent
,
785 struct callchain_cursor cursor
;
787 callchain_cursor_snapshot(&cursor
, &callchain_cursor
);
789 callchain_cursor_advance(&callchain_cursor
);
792 * Check if there's duplicate entries in the callchain.
793 * It's possible that it has cycles or recursive calls.
795 for (i
= 0; i
< iter
->curr
; i
++) {
796 if (hist_entry__cmp(he_cache
[i
], &he_tmp
) == 0) {
797 /* to avoid calling callback function */
803 he
= __hists__add_entry(evsel__hists(evsel
), al
, iter
->parent
, NULL
, NULL
,
804 sample
->period
, sample
->weight
,
805 sample
->transaction
, false);
810 he_cache
[iter
->curr
++] = he
;
812 callchain_append(he
->callchain
, &cursor
, sample
->period
);
817 iter_finish_cumulative_entry(struct hist_entry_iter
*iter
,
818 struct addr_location
*al __maybe_unused
)
826 const struct hist_iter_ops hist_iter_mem
= {
827 .prepare_entry
= iter_prepare_mem_entry
,
828 .add_single_entry
= iter_add_single_mem_entry
,
829 .next_entry
= iter_next_nop_entry
,
830 .add_next_entry
= iter_add_next_nop_entry
,
831 .finish_entry
= iter_finish_mem_entry
,
834 const struct hist_iter_ops hist_iter_branch
= {
835 .prepare_entry
= iter_prepare_branch_entry
,
836 .add_single_entry
= iter_add_single_branch_entry
,
837 .next_entry
= iter_next_branch_entry
,
838 .add_next_entry
= iter_add_next_branch_entry
,
839 .finish_entry
= iter_finish_branch_entry
,
842 const struct hist_iter_ops hist_iter_normal
= {
843 .prepare_entry
= iter_prepare_normal_entry
,
844 .add_single_entry
= iter_add_single_normal_entry
,
845 .next_entry
= iter_next_nop_entry
,
846 .add_next_entry
= iter_add_next_nop_entry
,
847 .finish_entry
= iter_finish_normal_entry
,
850 const struct hist_iter_ops hist_iter_cumulative
= {
851 .prepare_entry
= iter_prepare_cumulative_entry
,
852 .add_single_entry
= iter_add_single_cumulative_entry
,
853 .next_entry
= iter_next_cumulative_entry
,
854 .add_next_entry
= iter_add_next_cumulative_entry
,
855 .finish_entry
= iter_finish_cumulative_entry
,
858 int hist_entry_iter__add(struct hist_entry_iter
*iter
, struct addr_location
*al
,
859 struct perf_evsel
*evsel
, struct perf_sample
*sample
,
860 int max_stack_depth
, void *arg
)
864 err
= sample__resolve_callchain(sample
, &iter
->parent
, evsel
, al
,
870 iter
->sample
= sample
;
872 err
= iter
->ops
->prepare_entry(iter
, al
);
876 err
= iter
->ops
->add_single_entry(iter
, al
);
880 if (iter
->he
&& iter
->add_entry_cb
) {
881 err
= iter
->add_entry_cb(iter
, al
, true, arg
);
886 while (iter
->ops
->next_entry(iter
, al
)) {
887 err
= iter
->ops
->add_next_entry(iter
, al
);
891 if (iter
->he
&& iter
->add_entry_cb
) {
892 err
= iter
->add_entry_cb(iter
, al
, false, arg
);
899 err2
= iter
->ops
->finish_entry(iter
, al
);
907 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
909 struct perf_hpp_fmt
*fmt
;
912 perf_hpp__for_each_sort_list(fmt
) {
913 if (perf_hpp__should_skip(fmt
))
916 cmp
= fmt
->cmp(left
, right
);
925 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
927 struct perf_hpp_fmt
*fmt
;
930 perf_hpp__for_each_sort_list(fmt
) {
931 if (perf_hpp__should_skip(fmt
))
934 cmp
= fmt
->collapse(left
, right
);
942 void hist_entry__free(struct hist_entry
*he
)
944 zfree(&he
->branch_info
);
945 zfree(&he
->mem_info
);
946 zfree(&he
->stat_acc
);
947 free_srcline(he
->srcline
);
952 * collapse the histogram
955 static bool hists__collapse_insert_entry(struct hists
*hists __maybe_unused
,
956 struct rb_root
*root
,
957 struct hist_entry
*he
)
959 struct rb_node
**p
= &root
->rb_node
;
960 struct rb_node
*parent
= NULL
;
961 struct hist_entry
*iter
;
966 iter
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
968 cmp
= hist_entry__collapse(iter
, he
);
971 he_stat__add_stat(&iter
->stat
, &he
->stat
);
972 if (symbol_conf
.cumulate_callchain
)
973 he_stat__add_stat(iter
->stat_acc
, he
->stat_acc
);
975 if (symbol_conf
.use_callchain
) {
976 callchain_cursor_reset(&callchain_cursor
);
977 callchain_merge(&callchain_cursor
,
981 hist_entry__free(he
);
991 rb_link_node(&he
->rb_node_in
, parent
, p
);
992 rb_insert_color(&he
->rb_node_in
, root
);
996 static struct rb_root
*hists__get_rotate_entries_in(struct hists
*hists
)
998 struct rb_root
*root
;
1000 pthread_mutex_lock(&hists
->lock
);
1002 root
= hists
->entries_in
;
1003 if (++hists
->entries_in
> &hists
->entries_in_array
[1])
1004 hists
->entries_in
= &hists
->entries_in_array
[0];
1006 pthread_mutex_unlock(&hists
->lock
);
1011 static void hists__apply_filters(struct hists
*hists
, struct hist_entry
*he
)
1013 hists__filter_entry_by_dso(hists
, he
);
1014 hists__filter_entry_by_thread(hists
, he
);
1015 hists__filter_entry_by_symbol(hists
, he
);
1018 void hists__collapse_resort(struct hists
*hists
, struct ui_progress
*prog
)
1020 struct rb_root
*root
;
1021 struct rb_node
*next
;
1022 struct hist_entry
*n
;
1024 if (!sort__need_collapse
)
1027 root
= hists__get_rotate_entries_in(hists
);
1028 next
= rb_first(root
);
1033 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
1034 next
= rb_next(&n
->rb_node_in
);
1036 rb_erase(&n
->rb_node_in
, root
);
1037 if (hists__collapse_insert_entry(hists
, &hists
->entries_collapsed
, n
)) {
1039 * If it wasn't combined with one of the entries already
1040 * collapsed, we need to apply the filters that may have
1041 * been set by, say, the hist_browser.
1043 hists__apply_filters(hists
, n
);
1046 ui_progress__update(prog
, 1);
1050 static int hist_entry__sort(struct hist_entry
*a
, struct hist_entry
*b
)
1052 struct perf_hpp_fmt
*fmt
;
1055 perf_hpp__for_each_sort_list(fmt
) {
1056 if (perf_hpp__should_skip(fmt
))
1059 cmp
= fmt
->sort(a
, b
);
1067 static void hists__reset_filter_stats(struct hists
*hists
)
1069 hists
->nr_non_filtered_entries
= 0;
1070 hists
->stats
.total_non_filtered_period
= 0;
1073 void hists__reset_stats(struct hists
*hists
)
1075 hists
->nr_entries
= 0;
1076 hists
->stats
.total_period
= 0;
1078 hists__reset_filter_stats(hists
);
1081 static void hists__inc_filter_stats(struct hists
*hists
, struct hist_entry
*h
)
1083 hists
->nr_non_filtered_entries
++;
1084 hists
->stats
.total_non_filtered_period
+= h
->stat
.period
;
1087 void hists__inc_stats(struct hists
*hists
, struct hist_entry
*h
)
1090 hists__inc_filter_stats(hists
, h
);
1092 hists
->nr_entries
++;
1093 hists
->stats
.total_period
+= h
->stat
.period
;
1096 static void __hists__insert_output_entry(struct rb_root
*entries
,
1097 struct hist_entry
*he
,
1098 u64 min_callchain_hits
)
1100 struct rb_node
**p
= &entries
->rb_node
;
1101 struct rb_node
*parent
= NULL
;
1102 struct hist_entry
*iter
;
1104 if (symbol_conf
.use_callchain
)
1105 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
1106 min_callchain_hits
, &callchain_param
);
1108 while (*p
!= NULL
) {
1110 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
1112 if (hist_entry__sort(he
, iter
) > 0)
1115 p
= &(*p
)->rb_right
;
1118 rb_link_node(&he
->rb_node
, parent
, p
);
1119 rb_insert_color(&he
->rb_node
, entries
);
1122 void hists__output_resort(struct hists
*hists
)
1124 struct rb_root
*root
;
1125 struct rb_node
*next
;
1126 struct hist_entry
*n
;
1127 u64 min_callchain_hits
;
1129 min_callchain_hits
= hists
->stats
.total_period
* (callchain_param
.min_percent
/ 100);
1131 if (sort__need_collapse
)
1132 root
= &hists
->entries_collapsed
;
1134 root
= hists
->entries_in
;
1136 next
= rb_first(root
);
1137 hists
->entries
= RB_ROOT
;
1139 hists__reset_stats(hists
);
1140 hists__reset_col_len(hists
);
1143 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
1144 next
= rb_next(&n
->rb_node_in
);
1146 __hists__insert_output_entry(&hists
->entries
, n
, min_callchain_hits
);
1147 hists__inc_stats(hists
, n
);
1150 hists__calc_col_len(hists
, n
);
1154 static void hists__remove_entry_filter(struct hists
*hists
, struct hist_entry
*h
,
1155 enum hist_filter filter
)
1157 h
->filtered
&= ~(1 << filter
);
1161 /* force fold unfiltered entry for simplicity */
1162 h
->ms
.unfolded
= false;
1165 hists
->stats
.nr_non_filtered_samples
+= h
->stat
.nr_events
;
1167 hists__inc_filter_stats(hists
, h
);
1168 hists__calc_col_len(hists
, h
);
1172 static bool hists__filter_entry_by_dso(struct hists
*hists
,
1173 struct hist_entry
*he
)
1175 if (hists
->dso_filter
!= NULL
&&
1176 (he
->ms
.map
== NULL
|| he
->ms
.map
->dso
!= hists
->dso_filter
)) {
1177 he
->filtered
|= (1 << HIST_FILTER__DSO
);
1184 void hists__filter_by_dso(struct hists
*hists
)
1188 hists
->stats
.nr_non_filtered_samples
= 0;
1190 hists__reset_filter_stats(hists
);
1191 hists__reset_col_len(hists
);
1193 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
1194 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1196 if (symbol_conf
.exclude_other
&& !h
->parent
)
1199 if (hists__filter_entry_by_dso(hists
, h
))
1202 hists__remove_entry_filter(hists
, h
, HIST_FILTER__DSO
);
1206 static bool hists__filter_entry_by_thread(struct hists
*hists
,
1207 struct hist_entry
*he
)
1209 if (hists
->thread_filter
!= NULL
&&
1210 he
->thread
!= hists
->thread_filter
) {
1211 he
->filtered
|= (1 << HIST_FILTER__THREAD
);
1218 void hists__filter_by_thread(struct hists
*hists
)
1222 hists
->stats
.nr_non_filtered_samples
= 0;
1224 hists__reset_filter_stats(hists
);
1225 hists__reset_col_len(hists
);
1227 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
1228 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1230 if (hists__filter_entry_by_thread(hists
, h
))
1233 hists__remove_entry_filter(hists
, h
, HIST_FILTER__THREAD
);
1237 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
1238 struct hist_entry
*he
)
1240 if (hists
->symbol_filter_str
!= NULL
&&
1241 (!he
->ms
.sym
|| strstr(he
->ms
.sym
->name
,
1242 hists
->symbol_filter_str
) == NULL
)) {
1243 he
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
1250 void hists__filter_by_symbol(struct hists
*hists
)
1254 hists
->stats
.nr_non_filtered_samples
= 0;
1256 hists__reset_filter_stats(hists
);
1257 hists__reset_col_len(hists
);
1259 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
1260 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1262 if (hists__filter_entry_by_symbol(hists
, h
))
1265 hists__remove_entry_filter(hists
, h
, HIST_FILTER__SYMBOL
);
1269 void events_stats__inc(struct events_stats
*stats
, u32 type
)
1271 ++stats
->nr_events
[0];
1272 ++stats
->nr_events
[type
];
1275 void hists__inc_nr_events(struct hists
*hists
, u32 type
)
1277 events_stats__inc(&hists
->stats
, type
);
1280 void hists__inc_nr_samples(struct hists
*hists
, bool filtered
)
1282 events_stats__inc(&hists
->stats
, PERF_RECORD_SAMPLE
);
1284 hists
->stats
.nr_non_filtered_samples
++;
1287 static struct hist_entry
*hists__add_dummy_entry(struct hists
*hists
,
1288 struct hist_entry
*pair
)
1290 struct rb_root
*root
;
1292 struct rb_node
*parent
= NULL
;
1293 struct hist_entry
*he
;
1296 if (sort__need_collapse
)
1297 root
= &hists
->entries_collapsed
;
1299 root
= hists
->entries_in
;
1303 while (*p
!= NULL
) {
1305 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
1307 cmp
= hist_entry__collapse(he
, pair
);
1315 p
= &(*p
)->rb_right
;
1318 he
= hist_entry__new(pair
, true);
1320 memset(&he
->stat
, 0, sizeof(he
->stat
));
1322 rb_link_node(&he
->rb_node_in
, parent
, p
);
1323 rb_insert_color(&he
->rb_node_in
, root
);
1324 hists__inc_stats(hists
, he
);
1331 static struct hist_entry
*hists__find_entry(struct hists
*hists
,
1332 struct hist_entry
*he
)
1336 if (sort__need_collapse
)
1337 n
= hists
->entries_collapsed
.rb_node
;
1339 n
= hists
->entries_in
->rb_node
;
1342 struct hist_entry
*iter
= rb_entry(n
, struct hist_entry
, rb_node_in
);
1343 int64_t cmp
= hist_entry__collapse(iter
, he
);
1357 * Look for pairs to link to the leader buckets (hist_entries):
1359 void hists__match(struct hists
*leader
, struct hists
*other
)
1361 struct rb_root
*root
;
1363 struct hist_entry
*pos
, *pair
;
1365 if (sort__need_collapse
)
1366 root
= &leader
->entries_collapsed
;
1368 root
= leader
->entries_in
;
1370 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
1371 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
1372 pair
= hists__find_entry(other
, pos
);
1375 hist_entry__add_pair(pair
, pos
);
1380 * Look for entries in the other hists that are not present in the leader, if
1381 * we find them, just add a dummy entry on the leader hists, with period=0,
1382 * nr_events=0, to serve as the list header.
1384 int hists__link(struct hists
*leader
, struct hists
*other
)
1386 struct rb_root
*root
;
1388 struct hist_entry
*pos
, *pair
;
1390 if (sort__need_collapse
)
1391 root
= &other
->entries_collapsed
;
1393 root
= other
->entries_in
;
1395 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
1396 pos
= rb_entry(nd
, struct hist_entry
, rb_node_in
);
1398 if (!hist_entry__has_pairs(pos
)) {
1399 pair
= hists__add_dummy_entry(leader
, pos
);
1402 hist_entry__add_pair(pos
, pair
);
1410 size_t perf_evlist__fprintf_nr_events(struct perf_evlist
*evlist
, FILE *fp
)
1412 struct perf_evsel
*pos
;
1415 evlist__for_each(evlist
, pos
) {
1416 ret
+= fprintf(fp
, "%s stats:\n", perf_evsel__name(pos
));
1417 ret
+= events_stats__fprintf(&evsel__hists(pos
)->stats
, fp
);
1424 u64
hists__total_period(struct hists
*hists
)
1426 return symbol_conf
.filter_relative
? hists
->stats
.total_non_filtered_period
:
1427 hists
->stats
.total_period
;
1430 int parse_filter_percentage(const struct option
*opt __maybe_unused
,
1431 const char *arg
, int unset __maybe_unused
)
1433 if (!strcmp(arg
, "relative"))
1434 symbol_conf
.filter_relative
= true;
1435 else if (!strcmp(arg
, "absolute"))
1436 symbol_conf
.filter_relative
= false;
1443 int perf_hist_config(const char *var
, const char *value
)
1445 if (!strcmp(var
, "hist.percentage"))
1446 return parse_filter_percentage(NULL
, value
, 0);
1451 static int hists_evsel__init(struct perf_evsel
*evsel
)
1453 struct hists
*hists
= evsel__hists(evsel
);
1455 memset(hists
, 0, sizeof(*hists
));
1456 hists
->entries_in_array
[0] = hists
->entries_in_array
[1] = RB_ROOT
;
1457 hists
->entries_in
= &hists
->entries_in_array
[0];
1458 hists
->entries_collapsed
= RB_ROOT
;
1459 hists
->entries
= RB_ROOT
;
1460 pthread_mutex_init(&hists
->lock
, NULL
);
1465 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1466 * stored in the rbtree...
1469 int hists__init(void)
1471 int err
= perf_evsel__object_config(sizeof(struct hists_evsel
),
1472 hists_evsel__init
, NULL
);
1474 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr
);