1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
5 * Handle the callchains from the stream in an ad-hoc radix tree and then
6 * sort them in an rbtree.
8 * Using a radix for code path provides a fast retrieval and factorizes
9 * memory use. Also that lets us use the paths in a hierarchical graph view.
26 #include "callchain.h"
29 #define CALLCHAIN_PARAM_DEFAULT \
30 .mode = CHAIN_GRAPH_ABS, \
32 .order = ORDER_CALLEE, \
33 .key = CCKEY_FUNCTION, \
34 .value = CCVAL_PERCENT, \
36 struct callchain_param callchain_param = {
37 CALLCHAIN_PARAM_DEFAULT
41 * Are there any events usind DWARF callchains?
45 * -e cycles/call-graph=dwarf/
47 bool dwarf_callchain_users
;
49 struct callchain_param callchain_param_default
= {
50 CALLCHAIN_PARAM_DEFAULT
53 __thread
struct callchain_cursor callchain_cursor
;
55 int parse_callchain_record_opt(const char *arg
, struct callchain_param
*param
)
57 return parse_callchain_record(arg
, param
);
60 static int parse_callchain_mode(const char *value
)
62 if (!strncmp(value
, "graph", strlen(value
))) {
63 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
66 if (!strncmp(value
, "flat", strlen(value
))) {
67 callchain_param
.mode
= CHAIN_FLAT
;
70 if (!strncmp(value
, "fractal", strlen(value
))) {
71 callchain_param
.mode
= CHAIN_GRAPH_REL
;
74 if (!strncmp(value
, "folded", strlen(value
))) {
75 callchain_param
.mode
= CHAIN_FOLDED
;
81 static int parse_callchain_order(const char *value
)
83 if (!strncmp(value
, "caller", strlen(value
))) {
84 callchain_param
.order
= ORDER_CALLER
;
85 callchain_param
.order_set
= true;
88 if (!strncmp(value
, "callee", strlen(value
))) {
89 callchain_param
.order
= ORDER_CALLEE
;
90 callchain_param
.order_set
= true;
96 static int parse_callchain_sort_key(const char *value
)
98 if (!strncmp(value
, "function", strlen(value
))) {
99 callchain_param
.key
= CCKEY_FUNCTION
;
102 if (!strncmp(value
, "address", strlen(value
))) {
103 callchain_param
.key
= CCKEY_ADDRESS
;
106 if (!strncmp(value
, "srcline", strlen(value
))) {
107 callchain_param
.key
= CCKEY_SRCLINE
;
110 if (!strncmp(value
, "branch", strlen(value
))) {
111 callchain_param
.branch_callstack
= 1;
117 static int parse_callchain_value(const char *value
)
119 if (!strncmp(value
, "percent", strlen(value
))) {
120 callchain_param
.value
= CCVAL_PERCENT
;
123 if (!strncmp(value
, "period", strlen(value
))) {
124 callchain_param
.value
= CCVAL_PERIOD
;
127 if (!strncmp(value
, "count", strlen(value
))) {
128 callchain_param
.value
= CCVAL_COUNT
;
134 static int get_stack_size(const char *str
, unsigned long *_size
)
138 unsigned long max_size
= round_down(USHRT_MAX
, sizeof(u64
));
140 size
= strtoul(str
, &endptr
, 0);
146 size
= round_up(size
, sizeof(u64
));
147 if (!size
|| size
> max_size
)
155 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
161 __parse_callchain_report_opt(const char *arg
, bool allow_record_opt
)
164 char *endptr
, *saveptr
= NULL
;
165 bool minpcnt_set
= false;
166 bool record_opt_set
= false;
167 bool try_stack_size
= false;
169 callchain_param
.enabled
= true;
170 symbol_conf
.use_callchain
= true;
175 while ((tok
= strtok_r((char *)arg
, ",", &saveptr
)) != NULL
) {
176 if (!strncmp(tok
, "none", strlen(tok
))) {
177 callchain_param
.mode
= CHAIN_NONE
;
178 callchain_param
.enabled
= false;
179 symbol_conf
.use_callchain
= false;
183 if (!parse_callchain_mode(tok
) ||
184 !parse_callchain_order(tok
) ||
185 !parse_callchain_sort_key(tok
) ||
186 !parse_callchain_value(tok
)) {
187 /* parsing ok - move on to the next */
188 try_stack_size
= false;
190 } else if (allow_record_opt
&& !record_opt_set
) {
191 if (parse_callchain_record(tok
, &callchain_param
))
194 /* assume that number followed by 'dwarf' is stack size */
195 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
)
196 try_stack_size
= true;
198 record_opt_set
= true;
203 if (try_stack_size
) {
204 unsigned long size
= 0;
206 if (get_stack_size(tok
, &size
) < 0)
208 callchain_param
.dump_size
= size
;
209 try_stack_size
= false;
210 } else if (!minpcnt_set
) {
211 /* try to get the min percent */
212 callchain_param
.min_percent
= strtod(tok
, &endptr
);
217 /* try print limit at last */
218 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
226 if (callchain_register_param(&callchain_param
) < 0) {
227 pr_err("Can't register callchain params\n");
233 int parse_callchain_report_opt(const char *arg
)
235 return __parse_callchain_report_opt(arg
, false);
238 int parse_callchain_top_opt(const char *arg
)
240 return __parse_callchain_report_opt(arg
, true);
243 int parse_callchain_record(const char *arg
, struct callchain_param
*param
)
245 char *tok
, *name
, *saveptr
= NULL
;
249 /* We need buffer that we know we can write to. */
250 buf
= malloc(strlen(arg
) + 1);
256 tok
= strtok_r((char *)buf
, ",", &saveptr
);
257 name
= tok
? : (char *)buf
;
260 /* Framepointer style */
261 if (!strncmp(name
, "fp", sizeof("fp"))) {
262 if (!strtok_r(NULL
, ",", &saveptr
)) {
263 param
->record_mode
= CALLCHAIN_FP
;
266 pr_err("callchain: No more arguments "
267 "needed for --call-graph fp\n");
271 } else if (!strncmp(name
, "dwarf", sizeof("dwarf"))) {
272 const unsigned long default_stack_dump_size
= 8192;
275 param
->record_mode
= CALLCHAIN_DWARF
;
276 param
->dump_size
= default_stack_dump_size
;
277 dwarf_callchain_users
= true;
279 tok
= strtok_r(NULL
, ",", &saveptr
);
281 unsigned long size
= 0;
283 ret
= get_stack_size(tok
, &size
);
284 param
->dump_size
= size
;
286 } else if (!strncmp(name
, "lbr", sizeof("lbr"))) {
287 if (!strtok_r(NULL
, ",", &saveptr
)) {
288 param
->record_mode
= CALLCHAIN_LBR
;
291 pr_err("callchain: No more arguments "
292 "needed for --call-graph lbr\n");
295 pr_err("callchain: Unknown --call-graph option "
306 int perf_callchain_config(const char *var
, const char *value
)
310 if (!strstarts(var
, "call-graph."))
312 var
+= sizeof("call-graph.") - 1;
314 if (!strcmp(var
, "record-mode"))
315 return parse_callchain_record_opt(value
, &callchain_param
);
316 if (!strcmp(var
, "dump-size")) {
317 unsigned long size
= 0;
320 ret
= get_stack_size(value
, &size
);
321 callchain_param
.dump_size
= size
;
325 if (!strcmp(var
, "print-type")){
327 ret
= parse_callchain_mode(value
);
329 pr_err("Invalid callchain mode: %s\n", value
);
332 if (!strcmp(var
, "order")){
334 ret
= parse_callchain_order(value
);
336 pr_err("Invalid callchain order: %s\n", value
);
339 if (!strcmp(var
, "sort-key")){
341 ret
= parse_callchain_sort_key(value
);
343 pr_err("Invalid callchain sort key: %s\n", value
);
346 if (!strcmp(var
, "threshold")) {
347 callchain_param
.min_percent
= strtod(value
, &endptr
);
348 if (value
== endptr
) {
349 pr_err("Invalid callchain threshold: %s\n", value
);
353 if (!strcmp(var
, "print-limit")) {
354 callchain_param
.print_limit
= strtod(value
, &endptr
);
355 if (value
== endptr
) {
356 pr_err("Invalid callchain print limit: %s\n", value
);
365 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
366 enum chain_mode mode
)
368 struct rb_node
**p
= &root
->rb_node
;
369 struct rb_node
*parent
= NULL
;
370 struct callchain_node
*rnode
;
371 u64 chain_cumul
= callchain_cumul_hits(chain
);
377 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
378 rnode_cumul
= callchain_cumul_hits(rnode
);
383 if (rnode
->hit
< chain
->hit
)
388 case CHAIN_GRAPH_ABS
: /* Falldown */
389 case CHAIN_GRAPH_REL
:
390 if (rnode_cumul
< chain_cumul
)
401 rb_link_node(&chain
->rb_node
, parent
, p
);
402 rb_insert_color(&chain
->rb_node
, root
);
406 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
410 struct callchain_node
*child
;
412 n
= rb_first(&node
->rb_root_in
);
414 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
417 __sort_chain_flat(rb_root
, child
, min_hit
);
420 if (node
->hit
&& node
->hit
>= min_hit
)
421 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
425 * Once we get every callchains from the stream, we can now
429 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
430 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
433 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
436 static void __sort_chain_graph_abs(struct callchain_node
*node
,
440 struct callchain_node
*child
;
442 node
->rb_root
= RB_ROOT
;
443 n
= rb_first(&node
->rb_root_in
);
446 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
449 __sort_chain_graph_abs(child
, min_hit
);
450 if (callchain_cumul_hits(child
) >= min_hit
)
451 rb_insert_callchain(&node
->rb_root
, child
,
457 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
458 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
460 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
461 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
464 static void __sort_chain_graph_rel(struct callchain_node
*node
,
468 struct callchain_node
*child
;
471 node
->rb_root
= RB_ROOT
;
472 min_hit
= ceil(node
->children_hit
* min_percent
);
474 n
= rb_first(&node
->rb_root_in
);
476 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
479 __sort_chain_graph_rel(child
, min_percent
);
480 if (callchain_cumul_hits(child
) >= min_hit
)
481 rb_insert_callchain(&node
->rb_root
, child
,
487 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
488 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
490 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
491 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
494 int callchain_register_param(struct callchain_param
*param
)
496 switch (param
->mode
) {
497 case CHAIN_GRAPH_ABS
:
498 param
->sort
= sort_chain_graph_abs
;
500 case CHAIN_GRAPH_REL
:
501 param
->sort
= sort_chain_graph_rel
;
505 param
->sort
= sort_chain_flat
;
515 * Create a child for a parent. If inherit_children, then the new child
516 * will become the new parent of it's parent children
518 static struct callchain_node
*
519 create_child(struct callchain_node
*parent
, bool inherit_children
)
521 struct callchain_node
*new;
523 new = zalloc(sizeof(*new));
525 perror("not enough memory to create child for code path tree");
528 new->parent
= parent
;
529 INIT_LIST_HEAD(&new->val
);
530 INIT_LIST_HEAD(&new->parent_val
);
532 if (inherit_children
) {
534 struct callchain_node
*child
;
536 new->rb_root_in
= parent
->rb_root_in
;
537 parent
->rb_root_in
= RB_ROOT
;
539 n
= rb_first(&new->rb_root_in
);
541 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
546 /* make it the first child */
547 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
548 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
556 * Fill the node with callchain values
559 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
561 struct callchain_cursor_node
*cursor_node
;
563 node
->val_nr
= cursor
->nr
- cursor
->pos
;
565 pr_warning("Warning: empty node in callchain tree\n");
567 cursor_node
= callchain_cursor_current(cursor
);
569 while (cursor_node
) {
570 struct callchain_list
*call
;
572 call
= zalloc(sizeof(*call
));
574 perror("not enough memory for the code path tree");
577 call
->ip
= cursor_node
->ip
;
578 call
->ms
.sym
= cursor_node
->sym
;
579 call
->ms
.map
= map__get(cursor_node
->map
);
580 call
->srcline
= cursor_node
->srcline
;
582 if (cursor_node
->branch
) {
583 call
->branch_count
= 1;
585 if (cursor_node
->branch_from
) {
587 * branch_from is set with value somewhere else
588 * to imply it's "to" of a branch.
590 call
->brtype_stat
.branch_to
= true;
592 if (cursor_node
->branch_flags
.predicted
)
593 call
->predicted_count
= 1;
595 if (cursor_node
->branch_flags
.abort
)
596 call
->abort_count
= 1;
598 branch_type_count(&call
->brtype_stat
,
599 &cursor_node
->branch_flags
,
600 cursor_node
->branch_from
,
604 * It's "from" of a branch
606 call
->brtype_stat
.branch_to
= false;
608 cursor_node
->branch_flags
.cycles
;
609 call
->iter_count
= cursor_node
->nr_loop_iter
;
610 call
->iter_cycles
= cursor_node
->iter_cycles
;
614 list_add_tail(&call
->list
, &node
->val
);
616 callchain_cursor_advance(cursor
);
617 cursor_node
= callchain_cursor_current(cursor
);
622 static struct callchain_node
*
623 add_child(struct callchain_node
*parent
,
624 struct callchain_cursor
*cursor
,
627 struct callchain_node
*new;
629 new = create_child(parent
, false);
633 if (fill_node(new, cursor
) < 0) {
634 struct callchain_list
*call
, *tmp
;
636 list_for_each_entry_safe(call
, tmp
, &new->val
, list
) {
637 list_del(&call
->list
);
638 map__zput(call
->ms
.map
);
645 new->children_hit
= 0;
647 new->children_count
= 0;
659 static enum match_result
match_chain_strings(const char *left
,
662 enum match_result ret
= MATCH_EQ
;
666 cmp
= strcmp(left
, right
);
667 else if (!left
&& right
)
669 else if (left
&& !right
)
675 ret
= cmp
< 0 ? MATCH_LT
: MATCH_GT
;
681 * We need to always use relative addresses because we're aggregating
682 * callchains from multiple threads, i.e. different address spaces, so
683 * comparing absolute addresses make no sense as a symbol in a DSO may end up
684 * in a different address when used in a different binary or even the same
685 * binary but with some sort of address randomization technique, thus we need
686 * to compare just relative addresses. -acme
688 static enum match_result
match_chain_dso_addresses(struct map
*left_map
, u64 left_ip
,
689 struct map
*right_map
, u64 right_ip
)
691 struct dso
*left_dso
= left_map
? left_map
->dso
: NULL
;
692 struct dso
*right_dso
= right_map
? right_map
->dso
: NULL
;
694 if (left_dso
!= right_dso
)
695 return left_dso
< right_dso
? MATCH_LT
: MATCH_GT
;
697 if (left_ip
!= right_ip
)
698 return left_ip
< right_ip
? MATCH_LT
: MATCH_GT
;
703 static enum match_result
match_chain(struct callchain_cursor_node
*node
,
704 struct callchain_list
*cnode
)
706 enum match_result match
= MATCH_ERROR
;
708 switch (callchain_param
.key
) {
710 match
= match_chain_strings(cnode
->srcline
, node
->srcline
);
711 if (match
!= MATCH_ERROR
)
713 /* otherwise fall-back to symbol-based comparison below */
716 if (node
->sym
&& cnode
->ms
.sym
) {
718 * Compare inlined frames based on their symbol name
719 * because different inlined frames will have the same
720 * symbol start. Otherwise do a faster comparison based
721 * on the symbol start address.
723 if (cnode
->ms
.sym
->inlined
|| node
->sym
->inlined
) {
724 match
= match_chain_strings(cnode
->ms
.sym
->name
,
726 if (match
!= MATCH_ERROR
)
729 match
= match_chain_dso_addresses(cnode
->ms
.map
, cnode
->ms
.sym
->start
,
730 node
->map
, node
->sym
->start
);
734 /* otherwise fall-back to IP-based comparison below */
738 match
= match_chain_dso_addresses(cnode
->ms
.map
, cnode
->ip
, node
->map
, node
->ip
);
742 if (match
== MATCH_EQ
&& node
->branch
) {
743 cnode
->branch_count
++;
745 if (node
->branch_from
) {
747 * It's "to" of a branch
749 cnode
->brtype_stat
.branch_to
= true;
751 if (node
->branch_flags
.predicted
)
752 cnode
->predicted_count
++;
754 if (node
->branch_flags
.abort
)
755 cnode
->abort_count
++;
757 branch_type_count(&cnode
->brtype_stat
,
763 * It's "from" of a branch
765 cnode
->brtype_stat
.branch_to
= false;
766 cnode
->cycles_count
+= node
->branch_flags
.cycles
;
767 cnode
->iter_count
+= node
->nr_loop_iter
;
768 cnode
->iter_cycles
+= node
->iter_cycles
;
776 * Split the parent in two parts (a new child is created) and
777 * give a part of its callchain to the created child.
778 * Then create another child to host the given callchain of new branch
781 split_add_child(struct callchain_node
*parent
,
782 struct callchain_cursor
*cursor
,
783 struct callchain_list
*to_split
,
784 u64 idx_parents
, u64 idx_local
, u64 period
)
786 struct callchain_node
*new;
787 struct list_head
*old_tail
;
788 unsigned int idx_total
= idx_parents
+ idx_local
;
791 new = create_child(parent
, true);
795 /* split the callchain and move a part to the new child */
796 old_tail
= parent
->val
.prev
;
797 list_del_range(&to_split
->list
, old_tail
);
798 new->val
.next
= &to_split
->list
;
799 new->val
.prev
= old_tail
;
800 to_split
->list
.prev
= &new->val
;
801 old_tail
->next
= &new->val
;
804 new->hit
= parent
->hit
;
805 new->children_hit
= parent
->children_hit
;
806 parent
->children_hit
= callchain_cumul_hits(new);
807 new->val_nr
= parent
->val_nr
- idx_local
;
808 parent
->val_nr
= idx_local
;
809 new->count
= parent
->count
;
810 new->children_count
= parent
->children_count
;
811 parent
->children_count
= callchain_cumul_counts(new);
813 /* create a new child for the new branch if any */
814 if (idx_total
< cursor
->nr
) {
815 struct callchain_node
*first
;
816 struct callchain_list
*cnode
;
817 struct callchain_cursor_node
*node
;
818 struct rb_node
*p
, **pp
;
821 parent
->children_hit
+= period
;
823 parent
->children_count
+= 1;
825 node
= callchain_cursor_current(cursor
);
826 new = add_child(parent
, cursor
, period
);
831 * This is second child since we moved parent's children
832 * to new (first) child above.
834 p
= parent
->rb_root_in
.rb_node
;
835 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
836 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
839 if (match_chain(node
, cnode
) == MATCH_LT
)
844 rb_link_node(&new->rb_node_in
, p
, pp
);
845 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
847 parent
->hit
= period
;
853 static enum match_result
854 append_chain(struct callchain_node
*root
,
855 struct callchain_cursor
*cursor
,
859 append_chain_children(struct callchain_node
*root
,
860 struct callchain_cursor
*cursor
,
863 struct callchain_node
*rnode
;
864 struct callchain_cursor_node
*node
;
865 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
866 struct rb_node
*parent
= NULL
;
868 node
= callchain_cursor_current(cursor
);
872 /* lookup in childrens */
874 enum match_result ret
;
877 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
879 /* If at least first entry matches, rely to children */
880 ret
= append_chain(rnode
, cursor
, period
);
882 goto inc_children_hit
;
883 if (ret
== MATCH_ERROR
)
887 p
= &parent
->rb_left
;
889 p
= &parent
->rb_right
;
891 /* nothing in children, add to the current node */
892 rnode
= add_child(root
, cursor
, period
);
896 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
897 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
900 root
->children_hit
+= period
;
901 root
->children_count
++;
905 static enum match_result
906 append_chain(struct callchain_node
*root
,
907 struct callchain_cursor
*cursor
,
910 struct callchain_list
*cnode
;
911 u64 start
= cursor
->pos
;
914 enum match_result cmp
= MATCH_ERROR
;
917 * Lookup in the current node
918 * If we have a symbol, then compare the start to match
919 * anywhere inside a function, unless function
922 list_for_each_entry(cnode
, &root
->val
, list
) {
923 struct callchain_cursor_node
*node
;
925 node
= callchain_cursor_current(cursor
);
929 cmp
= match_chain(node
, cnode
);
935 callchain_cursor_advance(cursor
);
938 /* matches not, relay no the parent */
940 WARN_ONCE(cmp
== MATCH_ERROR
, "Chain comparison error\n");
944 matches
= cursor
->pos
- start
;
946 /* we match only a part of the node. Split it and add the new chain */
947 if (matches
< root
->val_nr
) {
948 if (split_add_child(root
, cursor
, cnode
, start
, matches
,
955 /* we match 100% of the path, increment the hit */
956 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
962 /* We match the node and still have a part remaining */
963 if (append_chain_children(root
, cursor
, period
) < 0)
969 int callchain_append(struct callchain_root
*root
,
970 struct callchain_cursor
*cursor
,
976 callchain_cursor_commit(cursor
);
978 if (append_chain_children(&root
->node
, cursor
, period
) < 0)
981 if (cursor
->nr
> root
->max_depth
)
982 root
->max_depth
= cursor
->nr
;
988 merge_chain_branch(struct callchain_cursor
*cursor
,
989 struct callchain_node
*dst
, struct callchain_node
*src
)
991 struct callchain_cursor_node
**old_last
= cursor
->last
;
992 struct callchain_node
*child
;
993 struct callchain_list
*list
, *next_list
;
995 int old_pos
= cursor
->nr
;
998 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
999 callchain_cursor_append(cursor
, list
->ip
,
1000 list
->ms
.map
, list
->ms
.sym
,
1001 false, NULL
, 0, 0, 0, list
->srcline
);
1002 list_del(&list
->list
);
1003 map__zput(list
->ms
.map
);
1008 callchain_cursor_commit(cursor
);
1009 if (append_chain_children(dst
, cursor
, src
->hit
) < 0)
1013 n
= rb_first(&src
->rb_root_in
);
1015 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1017 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
1019 err
= merge_chain_branch(cursor
, dst
, child
);
1026 cursor
->nr
= old_pos
;
1027 cursor
->last
= old_last
;
1032 int callchain_merge(struct callchain_cursor
*cursor
,
1033 struct callchain_root
*dst
, struct callchain_root
*src
)
1035 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
1038 int callchain_cursor_append(struct callchain_cursor
*cursor
,
1039 u64 ip
, struct map
*map
, struct symbol
*sym
,
1040 bool branch
, struct branch_flags
*flags
,
1041 int nr_loop_iter
, u64 iter_cycles
, u64 branch_from
,
1042 const char *srcline
)
1044 struct callchain_cursor_node
*node
= *cursor
->last
;
1047 node
= calloc(1, sizeof(*node
));
1051 *cursor
->last
= node
;
1055 map__zput(node
->map
);
1056 node
->map
= map__get(map
);
1058 node
->branch
= branch
;
1059 node
->nr_loop_iter
= nr_loop_iter
;
1060 node
->iter_cycles
= iter_cycles
;
1061 node
->srcline
= srcline
;
1064 memcpy(&node
->branch_flags
, flags
,
1065 sizeof(struct branch_flags
));
1067 node
->branch_from
= branch_from
;
1070 cursor
->last
= &node
->next
;
1075 int sample__resolve_callchain(struct perf_sample
*sample
,
1076 struct callchain_cursor
*cursor
, struct symbol
**parent
,
1077 struct perf_evsel
*evsel
, struct addr_location
*al
,
1080 if (sample
->callchain
== NULL
&& !symbol_conf
.show_branchflag_count
)
1083 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
1084 perf_hpp_list
.parent
|| symbol_conf
.show_branchflag_count
) {
1085 return thread__resolve_callchain(al
->thread
, cursor
, evsel
, sample
,
1086 parent
, al
, max_stack
);
1091 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
1093 if ((!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
) &&
1094 !symbol_conf
.show_branchflag_count
)
1096 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
1099 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
1100 bool hide_unresolved
)
1102 al
->map
= node
->map
;
1103 al
->sym
= node
->sym
;
1104 al
->srcline
= node
->srcline
;
1105 al
->addr
= node
->ip
;
1107 if (al
->sym
== NULL
) {
1108 if (hide_unresolved
)
1110 if (al
->map
== NULL
)
1114 if (al
->map
->groups
== &al
->machine
->kmaps
) {
1115 if (machine__is_host(al
->machine
)) {
1116 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
1119 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
1123 if (machine__is_host(al
->machine
)) {
1124 al
->cpumode
= PERF_RECORD_MISC_USER
;
1126 } else if (perf_guest
) {
1127 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
1130 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1139 char *callchain_list__sym_name(struct callchain_list
*cl
,
1140 char *bf
, size_t bfsize
, bool show_dso
)
1142 bool show_addr
= callchain_param
.key
== CCKEY_ADDRESS
;
1143 bool show_srcline
= show_addr
|| callchain_param
.key
== CCKEY_SRCLINE
;
1147 const char *inlined
= cl
->ms
.sym
->inlined
? " (inlined)" : "";
1149 if (show_srcline
&& cl
->srcline
)
1150 printed
= scnprintf(bf
, bfsize
, "%s %s%s",
1151 cl
->ms
.sym
->name
, cl
->srcline
,
1154 printed
= scnprintf(bf
, bfsize
, "%s%s",
1155 cl
->ms
.sym
->name
, inlined
);
1157 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
1160 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
1162 cl
->ms
.map
->dso
->short_name
:
1168 char *callchain_node__scnprintf_value(struct callchain_node
*node
,
1169 char *bf
, size_t bfsize
, u64 total
)
1171 double percent
= 0.0;
1172 u64 period
= callchain_cumul_hits(node
);
1173 unsigned count
= callchain_cumul_counts(node
);
1175 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1177 count
= node
->count
;
1180 switch (callchain_param
.value
) {
1182 scnprintf(bf
, bfsize
, "%"PRIu64
, period
);
1185 scnprintf(bf
, bfsize
, "%u", count
);
1190 percent
= period
* 100.0 / total
;
1191 scnprintf(bf
, bfsize
, "%.2f%%", percent
);
1197 int callchain_node__fprintf_value(struct callchain_node
*node
,
1198 FILE *fp
, u64 total
)
1200 double percent
= 0.0;
1201 u64 period
= callchain_cumul_hits(node
);
1202 unsigned count
= callchain_cumul_counts(node
);
1204 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1206 count
= node
->count
;
1209 switch (callchain_param
.value
) {
1211 return fprintf(fp
, "%"PRIu64
, period
);
1213 return fprintf(fp
, "%u", count
);
1217 percent
= period
* 100.0 / total
;
1218 return percent_color_fprintf(fp
, "%.2f%%", percent
);
1223 static void callchain_counts_value(struct callchain_node
*node
,
1224 u64
*branch_count
, u64
*predicted_count
,
1225 u64
*abort_count
, u64
*cycles_count
)
1227 struct callchain_list
*clist
;
1229 list_for_each_entry(clist
, &node
->val
, list
) {
1231 *branch_count
+= clist
->branch_count
;
1233 if (predicted_count
)
1234 *predicted_count
+= clist
->predicted_count
;
1237 *abort_count
+= clist
->abort_count
;
1240 *cycles_count
+= clist
->cycles_count
;
1244 static int callchain_node_branch_counts_cumul(struct callchain_node
*node
,
1246 u64
*predicted_count
,
1250 struct callchain_node
*child
;
1253 n
= rb_first(&node
->rb_root_in
);
1255 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
1258 callchain_node_branch_counts_cumul(child
, branch_count
,
1263 callchain_counts_value(child
, branch_count
,
1264 predicted_count
, abort_count
,
1271 int callchain_branch_counts(struct callchain_root
*root
,
1272 u64
*branch_count
, u64
*predicted_count
,
1273 u64
*abort_count
, u64
*cycles_count
)
1278 if (predicted_count
)
1279 *predicted_count
= 0;
1287 return callchain_node_branch_counts_cumul(&root
->node
,
1294 static int count_pri64_printf(int idx
, const char *str
, u64 value
, char *bf
, int bfsize
)
1298 printed
= scnprintf(bf
, bfsize
, "%s%s:%" PRId64
"", (idx
) ? " " : " (", str
, value
);
1303 static int count_float_printf(int idx
, const char *str
, float value
,
1304 char *bf
, int bfsize
, float threshold
)
1308 if (threshold
!= 0.0 && value
< threshold
)
1311 printed
= scnprintf(bf
, bfsize
, "%s%s:%.1f%%", (idx
) ? " " : " (", str
, value
);
1316 static int branch_to_str(char *bf
, int bfsize
,
1317 u64 branch_count
, u64 predicted_count
,
1319 struct branch_type_stat
*brtype_stat
)
1323 printed
= branch_type_str(brtype_stat
, bf
, bfsize
);
1327 if (predicted_count
< branch_count
) {
1328 printed
+= count_float_printf(i
++, "predicted",
1329 predicted_count
* 100.0 / branch_count
,
1330 bf
+ printed
, bfsize
- printed
, 0.0);
1334 printed
+= count_float_printf(i
++, "abort",
1335 abort_count
* 100.0 / branch_count
,
1336 bf
+ printed
, bfsize
- printed
, 0.1);
1340 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1345 static int branch_from_str(char *bf
, int bfsize
,
1347 u64 cycles_count
, u64 iter_count
,
1350 int printed
= 0, i
= 0;
1353 cycles
= cycles_count
/ branch_count
;
1355 printed
+= count_pri64_printf(i
++, "cycles",
1357 bf
+ printed
, bfsize
- printed
);
1361 printed
+= count_pri64_printf(i
++, "iter",
1363 bf
+ printed
, bfsize
- printed
);
1365 printed
+= count_pri64_printf(i
++, "avg_cycles",
1366 iter_cycles
/ iter_count
,
1367 bf
+ printed
, bfsize
- printed
);
1371 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1376 static int counts_str_build(char *bf
, int bfsize
,
1377 u64 branch_count
, u64 predicted_count
,
1378 u64 abort_count
, u64 cycles_count
,
1379 u64 iter_count
, u64 iter_cycles
,
1380 struct branch_type_stat
*brtype_stat
)
1384 if (branch_count
== 0)
1385 return scnprintf(bf
, bfsize
, " (calltrace)");
1387 if (brtype_stat
->branch_to
) {
1388 printed
= branch_to_str(bf
, bfsize
, branch_count
,
1389 predicted_count
, abort_count
, brtype_stat
);
1391 printed
= branch_from_str(bf
, bfsize
, branch_count
,
1392 cycles_count
, iter_count
, iter_cycles
);
1401 static int callchain_counts_printf(FILE *fp
, char *bf
, int bfsize
,
1402 u64 branch_count
, u64 predicted_count
,
1403 u64 abort_count
, u64 cycles_count
,
1404 u64 iter_count
, u64 iter_cycles
,
1405 struct branch_type_stat
*brtype_stat
)
1409 counts_str_build(str
, sizeof(str
), branch_count
,
1410 predicted_count
, abort_count
, cycles_count
,
1411 iter_count
, iter_cycles
, brtype_stat
);
1414 return fprintf(fp
, "%s", str
);
1416 return scnprintf(bf
, bfsize
, "%s", str
);
1419 int callchain_list_counts__printf_value(struct callchain_list
*clist
,
1420 FILE *fp
, char *bf
, int bfsize
)
1422 u64 branch_count
, predicted_count
;
1423 u64 abort_count
, cycles_count
;
1424 u64 iter_count
, iter_cycles
;
1426 branch_count
= clist
->branch_count
;
1427 predicted_count
= clist
->predicted_count
;
1428 abort_count
= clist
->abort_count
;
1429 cycles_count
= clist
->cycles_count
;
1430 iter_count
= clist
->iter_count
;
1431 iter_cycles
= clist
->iter_cycles
;
1433 return callchain_counts_printf(fp
, bf
, bfsize
, branch_count
,
1434 predicted_count
, abort_count
,
1435 cycles_count
, iter_count
, iter_cycles
,
1436 &clist
->brtype_stat
);
1439 static void free_callchain_node(struct callchain_node
*node
)
1441 struct callchain_list
*list
, *tmp
;
1442 struct callchain_node
*child
;
1445 list_for_each_entry_safe(list
, tmp
, &node
->parent_val
, list
) {
1446 list_del(&list
->list
);
1447 map__zput(list
->ms
.map
);
1451 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
1452 list_del(&list
->list
);
1453 map__zput(list
->ms
.map
);
1457 n
= rb_first(&node
->rb_root_in
);
1459 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1461 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
1463 free_callchain_node(child
);
1468 void free_callchain(struct callchain_root
*root
)
1470 if (!symbol_conf
.use_callchain
)
1473 free_callchain_node(&root
->node
);
1476 static u64
decay_callchain_node(struct callchain_node
*node
)
1478 struct callchain_node
*child
;
1482 n
= rb_first(&node
->rb_root_in
);
1484 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1486 child_hits
+= decay_callchain_node(child
);
1490 node
->hit
= (node
->hit
* 7) / 8;
1491 node
->children_hit
= child_hits
;
1496 void decay_callchain(struct callchain_root
*root
)
1498 if (!symbol_conf
.use_callchain
)
1501 decay_callchain_node(&root
->node
);
1504 int callchain_node__make_parent_list(struct callchain_node
*node
)
1506 struct callchain_node
*parent
= node
->parent
;
1507 struct callchain_list
*chain
, *new;
1511 list_for_each_entry_reverse(chain
, &parent
->val
, list
) {
1512 new = malloc(sizeof(*new));
1516 new->has_children
= false;
1517 map__get(new->ms
.map
);
1518 list_add_tail(&new->list
, &head
);
1520 parent
= parent
->parent
;
1523 list_for_each_entry_safe_reverse(chain
, new, &head
, list
)
1524 list_move_tail(&chain
->list
, &node
->parent_val
);
1526 if (!list_empty(&node
->parent_val
)) {
1527 chain
= list_first_entry(&node
->parent_val
, struct callchain_list
, list
);
1528 chain
->has_children
= rb_prev(&node
->rb_node
) || rb_next(&node
->rb_node
);
1530 chain
= list_first_entry(&node
->val
, struct callchain_list
, list
);
1531 chain
->has_children
= false;
1536 list_for_each_entry_safe(chain
, new, &head
, list
) {
1537 list_del(&chain
->list
);
1538 map__zput(chain
->ms
.map
);
1544 int callchain_cursor__copy(struct callchain_cursor
*dst
,
1545 struct callchain_cursor
*src
)
1549 callchain_cursor_reset(dst
);
1550 callchain_cursor_commit(src
);
1553 struct callchain_cursor_node
*node
;
1555 node
= callchain_cursor_current(src
);
1559 rc
= callchain_cursor_append(dst
, node
->ip
, node
->map
, node
->sym
,
1560 node
->branch
, &node
->branch_flags
,
1563 node
->branch_from
, node
->srcline
);
1567 callchain_cursor_advance(src
);