9 static bool hists__filter_entry_by_dso(struct hists
*hists
,
10 struct hist_entry
*he
);
11 static bool hists__filter_entry_by_thread(struct hists
*hists
,
12 struct hist_entry
*he
);
13 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
14 struct hist_entry
*he
);
23 struct callchain_param callchain_param
= {
24 .mode
= CHAIN_GRAPH_REL
,
29 u16
hists__col_len(struct hists
*hists
, enum hist_column col
)
31 return hists
->col_len
[col
];
34 void hists__set_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
36 hists
->col_len
[col
] = len
;
39 bool hists__new_col_len(struct hists
*hists
, enum hist_column col
, u16 len
)
41 if (len
> hists__col_len(hists
, col
)) {
42 hists__set_col_len(hists
, col
, len
);
48 void hists__reset_col_len(struct hists
*hists
)
52 for (col
= 0; col
< HISTC_NR_COLS
; ++col
)
53 hists__set_col_len(hists
, col
, 0);
56 static void hists__set_unres_dso_col_len(struct hists
*hists
, int dso
)
58 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
60 if (hists__col_len(hists
, dso
) < unresolved_col_width
&&
61 !symbol_conf
.col_width_list_str
&& !symbol_conf
.field_sep
&&
62 !symbol_conf
.dso_list
)
63 hists__set_col_len(hists
, dso
, unresolved_col_width
);
66 void hists__calc_col_len(struct hists
*hists
, struct hist_entry
*h
)
68 const unsigned int unresolved_col_width
= BITS_PER_LONG
/ 4;
72 hists__new_col_len(hists
, HISTC_SYMBOL
, h
->ms
.sym
->namelen
+ 4);
74 hists__set_unres_dso_col_len(hists
, HISTC_DSO
);
76 len
= thread__comm_len(h
->thread
);
77 if (hists__new_col_len(hists
, HISTC_COMM
, len
))
78 hists__set_col_len(hists
, HISTC_THREAD
, len
+ 6);
81 len
= dso__name_len(h
->ms
.map
->dso
);
82 hists__new_col_len(hists
, HISTC_DSO
, len
);
88 * +4 accounts for '[x] ' priv level info
89 * +2 account of 0x prefix on raw addresses
91 if (h
->branch_info
->from
.sym
) {
92 symlen
= (int)h
->branch_info
->from
.sym
->namelen
+ 4;
93 hists__new_col_len(hists
, HISTC_SYMBOL_FROM
, symlen
);
95 symlen
= dso__name_len(h
->branch_info
->from
.map
->dso
);
96 hists__new_col_len(hists
, HISTC_DSO_FROM
, symlen
);
98 symlen
= unresolved_col_width
+ 4 + 2;
99 hists__new_col_len(hists
, HISTC_SYMBOL_FROM
, symlen
);
100 hists__set_unres_dso_col_len(hists
, HISTC_DSO_FROM
);
103 if (h
->branch_info
->to
.sym
) {
104 symlen
= (int)h
->branch_info
->to
.sym
->namelen
+ 4;
105 hists__new_col_len(hists
, HISTC_SYMBOL_TO
, symlen
);
107 symlen
= dso__name_len(h
->branch_info
->to
.map
->dso
);
108 hists__new_col_len(hists
, HISTC_DSO_TO
, symlen
);
110 symlen
= unresolved_col_width
+ 4 + 2;
111 hists__new_col_len(hists
, HISTC_SYMBOL_TO
, symlen
);
112 hists__set_unres_dso_col_len(hists
, HISTC_DSO_TO
);
117 void hists__output_recalc_col_len(struct hists
*hists
, int max_rows
)
119 struct rb_node
*next
= rb_first(&hists
->entries
);
120 struct hist_entry
*n
;
123 hists__reset_col_len(hists
);
125 while (next
&& row
++ < max_rows
) {
126 n
= rb_entry(next
, struct hist_entry
, rb_node
);
128 hists__calc_col_len(hists
, n
);
129 next
= rb_next(&n
->rb_node
);
133 static void hist_entry__add_cpumode_period(struct hist_entry
*he
,
134 unsigned int cpumode
, u64 period
)
137 case PERF_RECORD_MISC_KERNEL
:
138 he
->stat
.period_sys
+= period
;
140 case PERF_RECORD_MISC_USER
:
141 he
->stat
.period_us
+= period
;
143 case PERF_RECORD_MISC_GUEST_KERNEL
:
144 he
->stat
.period_guest_sys
+= period
;
146 case PERF_RECORD_MISC_GUEST_USER
:
147 he
->stat
.period_guest_us
+= period
;
154 static void hist_entry__decay(struct hist_entry
*he
)
156 he
->stat
.period
= (he
->stat
.period
* 7) / 8;
157 he
->stat
.nr_events
= (he
->stat
.nr_events
* 7) / 8;
160 static bool hists__decay_entry(struct hists
*hists
, struct hist_entry
*he
)
162 u64 prev_period
= he
->stat
.period
;
164 if (prev_period
== 0)
167 hist_entry__decay(he
);
170 hists
->stats
.total_period
-= prev_period
- he
->stat
.period
;
172 return he
->stat
.period
== 0;
175 static void __hists__decay_entries(struct hists
*hists
, bool zap_user
,
176 bool zap_kernel
, bool threaded
)
178 struct rb_node
*next
= rb_first(&hists
->entries
);
179 struct hist_entry
*n
;
182 n
= rb_entry(next
, struct hist_entry
, rb_node
);
183 next
= rb_next(&n
->rb_node
);
185 * We may be annotating this, for instance, so keep it here in
186 * case some it gets new samples, we'll eventually free it when
187 * the user stops browsing and it agains gets fully decayed.
189 if (((zap_user
&& n
->level
== '.') ||
190 (zap_kernel
&& n
->level
!= '.') ||
191 hists__decay_entry(hists
, n
)) &&
193 rb_erase(&n
->rb_node
, &hists
->entries
);
195 if (sort__need_collapse
|| threaded
)
196 rb_erase(&n
->rb_node_in
, &hists
->entries_collapsed
);
204 void hists__decay_entries(struct hists
*hists
, bool zap_user
, bool zap_kernel
)
206 return __hists__decay_entries(hists
, zap_user
, zap_kernel
, false);
209 void hists__decay_entries_threaded(struct hists
*hists
,
210 bool zap_user
, bool zap_kernel
)
212 return __hists__decay_entries(hists
, zap_user
, zap_kernel
, true);
216 * histogram, sorted on item, collects periods
219 static struct hist_entry
*hist_entry__new(struct hist_entry
*template)
221 size_t callchain_size
= symbol_conf
.use_callchain
? sizeof(struct callchain_root
) : 0;
222 struct hist_entry
*he
= malloc(sizeof(*he
) + callchain_size
);
228 he
->ms
.map
->referenced
= true;
229 if (symbol_conf
.use_callchain
)
230 callchain_init(he
->callchain
);
236 static void hists__inc_nr_entries(struct hists
*hists
, struct hist_entry
*h
)
239 hists__calc_col_len(hists
, h
);
241 hists
->stats
.total_period
+= h
->stat
.period
;
245 static u8
symbol__parent_filter(const struct symbol
*parent
)
247 if (symbol_conf
.exclude_other
&& parent
== NULL
)
248 return 1 << HIST_FILTER__PARENT
;
252 static struct hist_entry
*add_hist_entry(struct hists
*hists
,
253 struct hist_entry
*entry
,
254 struct addr_location
*al
,
258 struct rb_node
*parent
= NULL
;
259 struct hist_entry
*he
;
262 pthread_mutex_lock(&hists
->lock
);
264 p
= &hists
->entries_in
->rb_node
;
268 he
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
270 cmp
= hist_entry__cmp(entry
, he
);
273 he
->stat
.period
+= period
;
274 ++he
->stat
.nr_events
;
276 /* If the map of an existing hist_entry has
277 * become out-of-date due to an exec() or
278 * similar, update it. Otherwise we will
279 * mis-adjust symbol addresses when computing
280 * the history counter to increment.
282 if (he
->ms
.map
!= entry
->ms
.map
) {
283 he
->ms
.map
= entry
->ms
.map
;
285 he
->ms
.map
->referenced
= true;
296 he
= hist_entry__new(entry
);
300 rb_link_node(&he
->rb_node_in
, parent
, p
);
301 rb_insert_color(&he
->rb_node_in
, hists
->entries_in
);
303 hist_entry__add_cpumode_period(he
, al
->cpumode
, period
);
305 pthread_mutex_unlock(&hists
->lock
);
309 struct hist_entry
*__hists__add_branch_entry(struct hists
*self
,
310 struct addr_location
*al
,
311 struct symbol
*sym_parent
,
312 struct branch_info
*bi
,
315 struct hist_entry entry
= {
316 .thread
= al
->thread
,
328 .parent
= sym_parent
,
329 .filtered
= symbol__parent_filter(sym_parent
),
334 return add_hist_entry(self
, &entry
, al
, period
);
337 struct hist_entry
*__hists__add_entry(struct hists
*self
,
338 struct addr_location
*al
,
339 struct symbol
*sym_parent
, u64 period
)
341 struct hist_entry entry
= {
342 .thread
= al
->thread
,
354 .parent
= sym_parent
,
355 .filtered
= symbol__parent_filter(sym_parent
),
359 return add_hist_entry(self
, &entry
, al
, period
);
363 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
365 struct sort_entry
*se
;
368 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
369 cmp
= se
->se_cmp(left
, right
);
378 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
380 struct sort_entry
*se
;
383 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
384 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
386 f
= se
->se_collapse
?: se
->se_cmp
;
388 cmp
= f(left
, right
);
396 void hist_entry__free(struct hist_entry
*he
)
402 * collapse the histogram
405 static bool hists__collapse_insert_entry(struct hists
*hists __maybe_unused
,
406 struct rb_root
*root
,
407 struct hist_entry
*he
)
409 struct rb_node
**p
= &root
->rb_node
;
410 struct rb_node
*parent
= NULL
;
411 struct hist_entry
*iter
;
416 iter
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
418 cmp
= hist_entry__collapse(iter
, he
);
421 iter
->stat
.period
+= he
->stat
.period
;
422 iter
->stat
.period_sys
+= he
->stat
.period_sys
;
423 iter
->stat
.period_us
+= he
->stat
.period_us
;
424 iter
->stat
.period_guest_sys
+= he
->stat
.period_guest_sys
;
425 iter
->stat
.period_guest_us
+= he
->stat
.period_guest_us
;
426 iter
->stat
.nr_events
+= he
->stat
.nr_events
;
428 if (symbol_conf
.use_callchain
) {
429 callchain_cursor_reset(&callchain_cursor
);
430 callchain_merge(&callchain_cursor
,
434 hist_entry__free(he
);
444 rb_link_node(&he
->rb_node_in
, parent
, p
);
445 rb_insert_color(&he
->rb_node_in
, root
);
449 static struct rb_root
*hists__get_rotate_entries_in(struct hists
*hists
)
451 struct rb_root
*root
;
453 pthread_mutex_lock(&hists
->lock
);
455 root
= hists
->entries_in
;
456 if (++hists
->entries_in
> &hists
->entries_in_array
[1])
457 hists
->entries_in
= &hists
->entries_in_array
[0];
459 pthread_mutex_unlock(&hists
->lock
);
464 static void hists__apply_filters(struct hists
*hists
, struct hist_entry
*he
)
466 hists__filter_entry_by_dso(hists
, he
);
467 hists__filter_entry_by_thread(hists
, he
);
468 hists__filter_entry_by_symbol(hists
, he
);
471 static void __hists__collapse_resort(struct hists
*hists
, bool threaded
)
473 struct rb_root
*root
;
474 struct rb_node
*next
;
475 struct hist_entry
*n
;
477 if (!sort__need_collapse
&& !threaded
)
480 root
= hists__get_rotate_entries_in(hists
);
481 next
= rb_first(root
);
484 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
485 next
= rb_next(&n
->rb_node_in
);
487 rb_erase(&n
->rb_node_in
, root
);
488 if (hists__collapse_insert_entry(hists
, &hists
->entries_collapsed
, n
)) {
490 * If it wasn't combined with one of the entries already
491 * collapsed, we need to apply the filters that may have
492 * been set by, say, the hist_browser.
494 hists__apply_filters(hists
, n
);
499 void hists__collapse_resort(struct hists
*hists
)
501 return __hists__collapse_resort(hists
, false);
504 void hists__collapse_resort_threaded(struct hists
*hists
)
506 return __hists__collapse_resort(hists
, true);
510 * reverse the map, sort on period.
513 static void __hists__insert_output_entry(struct rb_root
*entries
,
514 struct hist_entry
*he
,
515 u64 min_callchain_hits
)
517 struct rb_node
**p
= &entries
->rb_node
;
518 struct rb_node
*parent
= NULL
;
519 struct hist_entry
*iter
;
521 if (symbol_conf
.use_callchain
)
522 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
523 min_callchain_hits
, &callchain_param
);
527 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
529 if (he
->stat
.period
> iter
->stat
.period
)
535 rb_link_node(&he
->rb_node
, parent
, p
);
536 rb_insert_color(&he
->rb_node
, entries
);
539 static void __hists__output_resort(struct hists
*hists
, bool threaded
)
541 struct rb_root
*root
;
542 struct rb_node
*next
;
543 struct hist_entry
*n
;
544 u64 min_callchain_hits
;
546 min_callchain_hits
= hists
->stats
.total_period
* (callchain_param
.min_percent
/ 100);
548 if (sort__need_collapse
|| threaded
)
549 root
= &hists
->entries_collapsed
;
551 root
= hists
->entries_in
;
553 next
= rb_first(root
);
554 hists
->entries
= RB_ROOT
;
556 hists
->nr_entries
= 0;
557 hists
->stats
.total_period
= 0;
558 hists__reset_col_len(hists
);
561 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
562 next
= rb_next(&n
->rb_node_in
);
564 __hists__insert_output_entry(&hists
->entries
, n
, min_callchain_hits
);
565 hists__inc_nr_entries(hists
, n
);
569 void hists__output_resort(struct hists
*hists
)
571 return __hists__output_resort(hists
, false);
574 void hists__output_resort_threaded(struct hists
*hists
)
576 return __hists__output_resort(hists
, true);
579 static void hists__remove_entry_filter(struct hists
*hists
, struct hist_entry
*h
,
580 enum hist_filter filter
)
582 h
->filtered
&= ~(1 << filter
);
588 hists
->nr_entries
+= h
->nr_rows
;
590 hists
->stats
.total_period
+= h
->stat
.period
;
591 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] += h
->stat
.nr_events
;
593 hists__calc_col_len(hists
, h
);
597 static bool hists__filter_entry_by_dso(struct hists
*hists
,
598 struct hist_entry
*he
)
600 if (hists
->dso_filter
!= NULL
&&
601 (he
->ms
.map
== NULL
|| he
->ms
.map
->dso
!= hists
->dso_filter
)) {
602 he
->filtered
|= (1 << HIST_FILTER__DSO
);
609 void hists__filter_by_dso(struct hists
*hists
)
613 hists
->nr_entries
= hists
->stats
.total_period
= 0;
614 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
615 hists__reset_col_len(hists
);
617 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
618 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
620 if (symbol_conf
.exclude_other
&& !h
->parent
)
623 if (hists__filter_entry_by_dso(hists
, h
))
626 hists__remove_entry_filter(hists
, h
, HIST_FILTER__DSO
);
630 static bool hists__filter_entry_by_thread(struct hists
*hists
,
631 struct hist_entry
*he
)
633 if (hists
->thread_filter
!= NULL
&&
634 he
->thread
!= hists
->thread_filter
) {
635 he
->filtered
|= (1 << HIST_FILTER__THREAD
);
642 void hists__filter_by_thread(struct hists
*hists
)
646 hists
->nr_entries
= hists
->stats
.total_period
= 0;
647 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
648 hists__reset_col_len(hists
);
650 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
651 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
653 if (hists__filter_entry_by_thread(hists
, h
))
656 hists__remove_entry_filter(hists
, h
, HIST_FILTER__THREAD
);
660 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
661 struct hist_entry
*he
)
663 if (hists
->symbol_filter_str
!= NULL
&&
664 (!he
->ms
.sym
|| strstr(he
->ms
.sym
->name
,
665 hists
->symbol_filter_str
) == NULL
)) {
666 he
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
673 void hists__filter_by_symbol(struct hists
*hists
)
677 hists
->nr_entries
= hists
->stats
.total_period
= 0;
678 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
679 hists__reset_col_len(hists
);
681 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
682 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
684 if (hists__filter_entry_by_symbol(hists
, h
))
687 hists__remove_entry_filter(hists
, h
, HIST_FILTER__SYMBOL
);
691 int hist_entry__inc_addr_samples(struct hist_entry
*he
, int evidx
, u64 ip
)
693 return symbol__inc_addr_samples(he
->ms
.sym
, he
->ms
.map
, evidx
, ip
);
696 int hist_entry__annotate(struct hist_entry
*he
, size_t privsize
)
698 return symbol__annotate(he
->ms
.sym
, he
->ms
.map
, privsize
);
701 void hists__inc_nr_events(struct hists
*hists
, u32 type
)
703 ++hists
->stats
.nr_events
[0];
704 ++hists
->stats
.nr_events
[type
];