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
->period_sys
+= period
;
140 case PERF_RECORD_MISC_USER
:
141 he
->period_us
+= period
;
143 case PERF_RECORD_MISC_GUEST_KERNEL
:
144 he
->period_guest_sys
+= period
;
146 case PERF_RECORD_MISC_GUEST_USER
:
147 he
->period_guest_us
+= period
;
154 static void hist_entry__decay(struct hist_entry
*he
)
156 he
->period
= (he
->period
* 7) / 8;
157 he
->nr_events
= (he
->nr_events
* 7) / 8;
160 static bool hists__decay_entry(struct hists
*hists
, struct hist_entry
*he
)
162 u64 prev_period
= he
->period
;
164 if (prev_period
== 0)
167 hist_entry__decay(he
);
170 hists
->stats
.total_period
-= prev_period
- he
->period
;
172 return he
->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
->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
->period
+= period
;
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
,
325 .parent
= sym_parent
,
326 .filtered
= symbol__parent_filter(sym_parent
),
330 return add_hist_entry(self
, &entry
, al
, period
);
333 struct hist_entry
*__hists__add_entry(struct hists
*self
,
334 struct addr_location
*al
,
335 struct symbol
*sym_parent
, u64 period
)
337 struct hist_entry entry
= {
338 .thread
= al
->thread
,
347 .parent
= sym_parent
,
348 .filtered
= symbol__parent_filter(sym_parent
),
351 return add_hist_entry(self
, &entry
, al
, period
);
355 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
357 struct sort_entry
*se
;
360 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
361 cmp
= se
->se_cmp(left
, right
);
370 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
372 struct sort_entry
*se
;
375 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
376 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
378 f
= se
->se_collapse
?: se
->se_cmp
;
380 cmp
= f(left
, right
);
388 void hist_entry__free(struct hist_entry
*he
)
394 * collapse the histogram
397 static bool hists__collapse_insert_entry(struct hists
*hists __maybe_unused
,
398 struct rb_root
*root
,
399 struct hist_entry
*he
)
401 struct rb_node
**p
= &root
->rb_node
;
402 struct rb_node
*parent
= NULL
;
403 struct hist_entry
*iter
;
408 iter
= rb_entry(parent
, struct hist_entry
, rb_node_in
);
410 cmp
= hist_entry__collapse(iter
, he
);
413 iter
->period
+= he
->period
;
414 iter
->nr_events
+= he
->nr_events
;
415 if (symbol_conf
.use_callchain
) {
416 callchain_cursor_reset(&callchain_cursor
);
417 callchain_merge(&callchain_cursor
,
421 hist_entry__free(he
);
431 rb_link_node(&he
->rb_node_in
, parent
, p
);
432 rb_insert_color(&he
->rb_node_in
, root
);
436 static struct rb_root
*hists__get_rotate_entries_in(struct hists
*hists
)
438 struct rb_root
*root
;
440 pthread_mutex_lock(&hists
->lock
);
442 root
= hists
->entries_in
;
443 if (++hists
->entries_in
> &hists
->entries_in_array
[1])
444 hists
->entries_in
= &hists
->entries_in_array
[0];
446 pthread_mutex_unlock(&hists
->lock
);
451 static void hists__apply_filters(struct hists
*hists
, struct hist_entry
*he
)
453 hists__filter_entry_by_dso(hists
, he
);
454 hists__filter_entry_by_thread(hists
, he
);
455 hists__filter_entry_by_symbol(hists
, he
);
458 static void __hists__collapse_resort(struct hists
*hists
, bool threaded
)
460 struct rb_root
*root
;
461 struct rb_node
*next
;
462 struct hist_entry
*n
;
464 if (!sort__need_collapse
&& !threaded
)
467 root
= hists__get_rotate_entries_in(hists
);
468 next
= rb_first(root
);
471 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
472 next
= rb_next(&n
->rb_node_in
);
474 rb_erase(&n
->rb_node_in
, root
);
475 if (hists__collapse_insert_entry(hists
, &hists
->entries_collapsed
, n
)) {
477 * If it wasn't combined with one of the entries already
478 * collapsed, we need to apply the filters that may have
479 * been set by, say, the hist_browser.
481 hists__apply_filters(hists
, n
);
486 void hists__collapse_resort(struct hists
*hists
)
488 return __hists__collapse_resort(hists
, false);
491 void hists__collapse_resort_threaded(struct hists
*hists
)
493 return __hists__collapse_resort(hists
, true);
497 * reverse the map, sort on period.
500 static void __hists__insert_output_entry(struct rb_root
*entries
,
501 struct hist_entry
*he
,
502 u64 min_callchain_hits
)
504 struct rb_node
**p
= &entries
->rb_node
;
505 struct rb_node
*parent
= NULL
;
506 struct hist_entry
*iter
;
508 if (symbol_conf
.use_callchain
)
509 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
510 min_callchain_hits
, &callchain_param
);
514 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
516 if (he
->period
> iter
->period
)
522 rb_link_node(&he
->rb_node
, parent
, p
);
523 rb_insert_color(&he
->rb_node
, entries
);
526 static void __hists__output_resort(struct hists
*hists
, bool threaded
)
528 struct rb_root
*root
;
529 struct rb_node
*next
;
530 struct hist_entry
*n
;
531 u64 min_callchain_hits
;
533 min_callchain_hits
= hists
->stats
.total_period
* (callchain_param
.min_percent
/ 100);
535 if (sort__need_collapse
|| threaded
)
536 root
= &hists
->entries_collapsed
;
538 root
= hists
->entries_in
;
540 next
= rb_first(root
);
541 hists
->entries
= RB_ROOT
;
543 hists
->nr_entries
= 0;
544 hists
->stats
.total_period
= 0;
545 hists__reset_col_len(hists
);
548 n
= rb_entry(next
, struct hist_entry
, rb_node_in
);
549 next
= rb_next(&n
->rb_node_in
);
551 __hists__insert_output_entry(&hists
->entries
, n
, min_callchain_hits
);
552 hists__inc_nr_entries(hists
, n
);
556 void hists__output_resort(struct hists
*hists
)
558 return __hists__output_resort(hists
, false);
561 void hists__output_resort_threaded(struct hists
*hists
)
563 return __hists__output_resort(hists
, true);
566 static void hists__remove_entry_filter(struct hists
*hists
, struct hist_entry
*h
,
567 enum hist_filter filter
)
569 h
->filtered
&= ~(1 << filter
);
575 hists
->nr_entries
+= h
->nr_rows
;
577 hists
->stats
.total_period
+= h
->period
;
578 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] += h
->nr_events
;
580 hists__calc_col_len(hists
, h
);
584 static bool hists__filter_entry_by_dso(struct hists
*hists
,
585 struct hist_entry
*he
)
587 if (hists
->dso_filter
!= NULL
&&
588 (he
->ms
.map
== NULL
|| he
->ms
.map
->dso
!= hists
->dso_filter
)) {
589 he
->filtered
|= (1 << HIST_FILTER__DSO
);
596 void hists__filter_by_dso(struct hists
*hists
)
600 hists
->nr_entries
= hists
->stats
.total_period
= 0;
601 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
602 hists__reset_col_len(hists
);
604 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
605 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
607 if (symbol_conf
.exclude_other
&& !h
->parent
)
610 if (hists__filter_entry_by_dso(hists
, h
))
613 hists__remove_entry_filter(hists
, h
, HIST_FILTER__DSO
);
617 static bool hists__filter_entry_by_thread(struct hists
*hists
,
618 struct hist_entry
*he
)
620 if (hists
->thread_filter
!= NULL
&&
621 he
->thread
!= hists
->thread_filter
) {
622 he
->filtered
|= (1 << HIST_FILTER__THREAD
);
629 void hists__filter_by_thread(struct hists
*hists
)
633 hists
->nr_entries
= hists
->stats
.total_period
= 0;
634 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
635 hists__reset_col_len(hists
);
637 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
638 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
640 if (hists__filter_entry_by_thread(hists
, h
))
643 hists__remove_entry_filter(hists
, h
, HIST_FILTER__THREAD
);
647 static bool hists__filter_entry_by_symbol(struct hists
*hists
,
648 struct hist_entry
*he
)
650 if (hists
->symbol_filter_str
!= NULL
&&
651 (!he
->ms
.sym
|| strstr(he
->ms
.sym
->name
,
652 hists
->symbol_filter_str
) == NULL
)) {
653 he
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
660 void hists__filter_by_symbol(struct hists
*hists
)
664 hists
->nr_entries
= hists
->stats
.total_period
= 0;
665 hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
] = 0;
666 hists__reset_col_len(hists
);
668 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
669 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
671 if (hists__filter_entry_by_symbol(hists
, h
))
674 hists__remove_entry_filter(hists
, h
, HIST_FILTER__SYMBOL
);
678 int hist_entry__inc_addr_samples(struct hist_entry
*he
, int evidx
, u64 ip
)
680 return symbol__inc_addr_samples(he
->ms
.sym
, he
->ms
.map
, evidx
, ip
);
683 int hist_entry__annotate(struct hist_entry
*he
, size_t privsize
)
685 return symbol__annotate(he
->ms
.sym
, he
->ms
.map
, privsize
);
688 void hists__inc_nr_events(struct hists
*hists
, u32 type
)
690 ++hists
->stats
.nr_events
[0];
691 ++hists
->stats
.nr_events
[type
];