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.
19 #include <linux/string.h>
20 #include <linux/zalloc.h>
31 #include "callchain.h"
37 #define CALLCHAIN_PARAM_DEFAULT \
38 .mode = CHAIN_GRAPH_ABS, \
40 .order = ORDER_CALLEE, \
41 .key = CCKEY_FUNCTION, \
42 .value = CCVAL_PERCENT, \
44 struct callchain_param callchain_param = {
45 CALLCHAIN_PARAM_DEFAULT
49 * Are there any events usind DWARF callchains?
53 * -e cycles/call-graph=dwarf/
55 bool dwarf_callchain_users
;
57 struct callchain_param callchain_param_default
= {
58 CALLCHAIN_PARAM_DEFAULT
61 /* Used for thread-local struct callchain_cursor. */
62 static pthread_key_t callchain_cursor
;
64 int parse_callchain_record_opt(const char *arg
, struct callchain_param
*param
)
66 return parse_callchain_record(arg
, param
);
69 static int parse_callchain_mode(const char *value
)
71 if (!strncmp(value
, "graph", strlen(value
))) {
72 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
75 if (!strncmp(value
, "flat", strlen(value
))) {
76 callchain_param
.mode
= CHAIN_FLAT
;
79 if (!strncmp(value
, "fractal", strlen(value
))) {
80 callchain_param
.mode
= CHAIN_GRAPH_REL
;
83 if (!strncmp(value
, "folded", strlen(value
))) {
84 callchain_param
.mode
= CHAIN_FOLDED
;
90 static int parse_callchain_order(const char *value
)
92 if (!strncmp(value
, "caller", strlen(value
))) {
93 callchain_param
.order
= ORDER_CALLER
;
94 callchain_param
.order_set
= true;
97 if (!strncmp(value
, "callee", strlen(value
))) {
98 callchain_param
.order
= ORDER_CALLEE
;
99 callchain_param
.order_set
= true;
105 static int parse_callchain_sort_key(const char *value
)
107 if (!strncmp(value
, "function", strlen(value
))) {
108 callchain_param
.key
= CCKEY_FUNCTION
;
111 if (!strncmp(value
, "address", strlen(value
))) {
112 callchain_param
.key
= CCKEY_ADDRESS
;
115 if (!strncmp(value
, "srcline", strlen(value
))) {
116 callchain_param
.key
= CCKEY_SRCLINE
;
119 if (!strncmp(value
, "branch", strlen(value
))) {
120 callchain_param
.branch_callstack
= 1;
126 static int parse_callchain_value(const char *value
)
128 if (!strncmp(value
, "percent", strlen(value
))) {
129 callchain_param
.value
= CCVAL_PERCENT
;
132 if (!strncmp(value
, "period", strlen(value
))) {
133 callchain_param
.value
= CCVAL_PERIOD
;
136 if (!strncmp(value
, "count", strlen(value
))) {
137 callchain_param
.value
= CCVAL_COUNT
;
143 static int get_stack_size(const char *str
, unsigned long *_size
)
147 unsigned long max_size
= round_down(USHRT_MAX
, sizeof(u64
));
149 size
= strtoul(str
, &endptr
, 0);
155 size
= round_up(size
, sizeof(u64
));
156 if (!size
|| size
> max_size
)
164 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
170 __parse_callchain_report_opt(const char *arg
, bool allow_record_opt
)
173 char *endptr
, *saveptr
= NULL
;
174 bool minpcnt_set
= false;
175 bool record_opt_set
= false;
176 bool try_stack_size
= false;
178 callchain_param
.enabled
= true;
179 symbol_conf
.use_callchain
= true;
184 while ((tok
= strtok_r((char *)arg
, ",", &saveptr
)) != NULL
) {
185 if (!strncmp(tok
, "none", strlen(tok
))) {
186 callchain_param
.mode
= CHAIN_NONE
;
187 callchain_param
.enabled
= false;
188 symbol_conf
.use_callchain
= false;
192 if (!parse_callchain_mode(tok
) ||
193 !parse_callchain_order(tok
) ||
194 !parse_callchain_sort_key(tok
) ||
195 !parse_callchain_value(tok
)) {
196 /* parsing ok - move on to the next */
197 try_stack_size
= false;
199 } else if (allow_record_opt
&& !record_opt_set
) {
200 if (parse_callchain_record(tok
, &callchain_param
))
203 /* assume that number followed by 'dwarf' is stack size */
204 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
)
205 try_stack_size
= true;
207 record_opt_set
= true;
212 if (try_stack_size
) {
213 unsigned long size
= 0;
215 if (get_stack_size(tok
, &size
) < 0)
217 callchain_param
.dump_size
= size
;
218 try_stack_size
= false;
219 } else if (!minpcnt_set
) {
220 /* try to get the min percent */
221 callchain_param
.min_percent
= strtod(tok
, &endptr
);
226 /* try print limit at last */
227 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
235 if (callchain_register_param(&callchain_param
) < 0) {
236 pr_err("Can't register callchain params\n");
242 int parse_callchain_report_opt(const char *arg
)
244 return __parse_callchain_report_opt(arg
, false);
247 int parse_callchain_top_opt(const char *arg
)
249 return __parse_callchain_report_opt(arg
, true);
252 int parse_callchain_record(const char *arg
, struct callchain_param
*param
)
254 char *tok
, *name
, *saveptr
= NULL
;
258 /* We need buffer that we know we can write to. */
259 buf
= malloc(strlen(arg
) + 1);
265 tok
= strtok_r((char *)buf
, ",", &saveptr
);
266 name
= tok
? : (char *)buf
;
269 /* Framepointer style */
270 if (!strncmp(name
, "fp", sizeof("fp"))) {
272 param
->record_mode
= CALLCHAIN_FP
;
274 tok
= strtok_r(NULL
, ",", &saveptr
);
278 size
= strtoul(tok
, &name
, 0);
279 if (size
< (unsigned) sysctl__max_stack())
280 param
->max_stack
= size
;
285 } else if (!strncmp(name
, "dwarf", sizeof("dwarf"))) {
286 const unsigned long default_stack_dump_size
= 8192;
289 param
->record_mode
= CALLCHAIN_DWARF
;
290 param
->dump_size
= default_stack_dump_size
;
291 dwarf_callchain_users
= true;
293 tok
= strtok_r(NULL
, ",", &saveptr
);
295 unsigned long size
= 0;
297 ret
= get_stack_size(tok
, &size
);
298 param
->dump_size
= size
;
300 } else if (!strncmp(name
, "lbr", sizeof("lbr"))) {
301 if (!strtok_r(NULL
, ",", &saveptr
)) {
302 param
->record_mode
= CALLCHAIN_LBR
;
305 pr_err("callchain: No more arguments "
306 "needed for --call-graph lbr\n");
309 pr_err("callchain: Unknown --call-graph option "
320 int perf_callchain_config(const char *var
, const char *value
)
324 if (!strstarts(var
, "call-graph."))
326 var
+= sizeof("call-graph.") - 1;
328 if (!strcmp(var
, "record-mode"))
329 return parse_callchain_record_opt(value
, &callchain_param
);
330 if (!strcmp(var
, "dump-size")) {
331 unsigned long size
= 0;
334 ret
= get_stack_size(value
, &size
);
335 callchain_param
.dump_size
= size
;
339 if (!strcmp(var
, "print-type")){
341 ret
= parse_callchain_mode(value
);
343 pr_err("Invalid callchain mode: %s\n", value
);
346 if (!strcmp(var
, "order")){
348 ret
= parse_callchain_order(value
);
350 pr_err("Invalid callchain order: %s\n", value
);
353 if (!strcmp(var
, "sort-key")){
355 ret
= parse_callchain_sort_key(value
);
357 pr_err("Invalid callchain sort key: %s\n", value
);
360 if (!strcmp(var
, "threshold")) {
361 callchain_param
.min_percent
= strtod(value
, &endptr
);
362 if (value
== endptr
) {
363 pr_err("Invalid callchain threshold: %s\n", value
);
367 if (!strcmp(var
, "print-limit")) {
368 callchain_param
.print_limit
= strtod(value
, &endptr
);
369 if (value
== endptr
) {
370 pr_err("Invalid callchain print limit: %s\n", value
);
379 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
380 enum chain_mode mode
)
382 struct rb_node
**p
= &root
->rb_node
;
383 struct rb_node
*parent
= NULL
;
384 struct callchain_node
*rnode
;
385 u64 chain_cumul
= callchain_cumul_hits(chain
);
391 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
392 rnode_cumul
= callchain_cumul_hits(rnode
);
397 if (rnode
->hit
< chain
->hit
)
402 case CHAIN_GRAPH_ABS
: /* Falldown */
403 case CHAIN_GRAPH_REL
:
404 if (rnode_cumul
< chain_cumul
)
415 rb_link_node(&chain
->rb_node
, parent
, p
);
416 rb_insert_color(&chain
->rb_node
, root
);
420 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
424 struct callchain_node
*child
;
426 n
= rb_first(&node
->rb_root_in
);
428 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
431 __sort_chain_flat(rb_root
, child
, min_hit
);
434 if (node
->hit
&& node
->hit
>= min_hit
)
435 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
439 * Once we get every callchains from the stream, we can now
443 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
444 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
447 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
450 static void __sort_chain_graph_abs(struct callchain_node
*node
,
454 struct callchain_node
*child
;
456 node
->rb_root
= RB_ROOT
;
457 n
= rb_first(&node
->rb_root_in
);
460 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
463 __sort_chain_graph_abs(child
, min_hit
);
464 if (callchain_cumul_hits(child
) >= min_hit
)
465 rb_insert_callchain(&node
->rb_root
, child
,
471 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
472 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
474 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
475 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
478 static void __sort_chain_graph_rel(struct callchain_node
*node
,
482 struct callchain_node
*child
;
485 node
->rb_root
= RB_ROOT
;
486 min_hit
= ceil(node
->children_hit
* min_percent
);
488 n
= rb_first(&node
->rb_root_in
);
490 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
493 __sort_chain_graph_rel(child
, min_percent
);
494 if (callchain_cumul_hits(child
) >= min_hit
)
495 rb_insert_callchain(&node
->rb_root
, child
,
501 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
502 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
504 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
505 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
508 int callchain_register_param(struct callchain_param
*param
)
510 switch (param
->mode
) {
511 case CHAIN_GRAPH_ABS
:
512 param
->sort
= sort_chain_graph_abs
;
514 case CHAIN_GRAPH_REL
:
515 param
->sort
= sort_chain_graph_rel
;
519 param
->sort
= sort_chain_flat
;
529 * Create a child for a parent. If inherit_children, then the new child
530 * will become the new parent of it's parent children
532 static struct callchain_node
*
533 create_child(struct callchain_node
*parent
, bool inherit_children
)
535 struct callchain_node
*new;
537 new = zalloc(sizeof(*new));
539 perror("not enough memory to create child for code path tree");
542 new->parent
= parent
;
543 INIT_LIST_HEAD(&new->val
);
544 INIT_LIST_HEAD(&new->parent_val
);
546 if (inherit_children
) {
548 struct callchain_node
*child
;
550 new->rb_root_in
= parent
->rb_root_in
;
551 parent
->rb_root_in
= RB_ROOT
;
553 n
= rb_first(&new->rb_root_in
);
555 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
560 /* make it the first child */
561 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
562 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
570 * Fill the node with callchain values
573 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
575 struct callchain_cursor_node
*cursor_node
;
577 node
->val_nr
= cursor
->nr
- cursor
->pos
;
579 pr_warning("Warning: empty node in callchain tree\n");
581 cursor_node
= callchain_cursor_current(cursor
);
583 while (cursor_node
) {
584 struct callchain_list
*call
;
586 call
= zalloc(sizeof(*call
));
588 perror("not enough memory for the code path tree");
591 call
->ip
= cursor_node
->ip
;
592 call
->ms
= cursor_node
->ms
;
593 call
->ms
.map
= map__get(call
->ms
.map
);
594 call
->ms
.maps
= maps__get(call
->ms
.maps
);
595 call
->srcline
= cursor_node
->srcline
;
597 if (cursor_node
->branch
) {
598 call
->branch_count
= 1;
600 if (cursor_node
->branch_from
) {
602 * branch_from is set with value somewhere else
603 * to imply it's "to" of a branch.
605 if (!call
->brtype_stat
) {
606 call
->brtype_stat
= zalloc(sizeof(*call
->brtype_stat
));
607 if (!call
->brtype_stat
) {
608 perror("not enough memory for the code path branch statistics");
609 zfree(&call
->brtype_stat
);
613 call
->brtype_stat
->branch_to
= true;
615 if (cursor_node
->branch_flags
.predicted
)
616 call
->predicted_count
= 1;
618 if (cursor_node
->branch_flags
.abort
)
619 call
->abort_count
= 1;
621 branch_type_count(call
->brtype_stat
,
622 &cursor_node
->branch_flags
,
623 cursor_node
->branch_from
,
627 * It's "from" of a branch
629 if (call
->brtype_stat
&& call
->brtype_stat
->branch_to
)
630 call
->brtype_stat
->branch_to
= false;
632 cursor_node
->branch_flags
.cycles
;
633 call
->iter_count
= cursor_node
->nr_loop_iter
;
634 call
->iter_cycles
= cursor_node
->iter_cycles
;
638 list_add_tail(&call
->list
, &node
->val
);
640 callchain_cursor_advance(cursor
);
641 cursor_node
= callchain_cursor_current(cursor
);
646 static struct callchain_node
*
647 add_child(struct callchain_node
*parent
,
648 struct callchain_cursor
*cursor
,
651 struct callchain_node
*new;
653 new = create_child(parent
, false);
657 if (fill_node(new, cursor
) < 0) {
658 struct callchain_list
*call
, *tmp
;
660 list_for_each_entry_safe(call
, tmp
, &new->val
, list
) {
661 list_del_init(&call
->list
);
662 map_symbol__exit(&call
->ms
);
663 zfree(&call
->brtype_stat
);
670 new->children_hit
= 0;
672 new->children_count
= 0;
684 static enum match_result
match_chain_strings(const char *left
,
687 enum match_result ret
= MATCH_EQ
;
691 cmp
= strcmp(left
, right
);
692 else if (!left
&& right
)
694 else if (left
&& !right
)
700 ret
= cmp
< 0 ? MATCH_LT
: MATCH_GT
;
706 * We need to always use relative addresses because we're aggregating
707 * callchains from multiple threads, i.e. different address spaces, so
708 * comparing absolute addresses make no sense as a symbol in a DSO may end up
709 * in a different address when used in a different binary or even the same
710 * binary but with some sort of address randomization technique, thus we need
711 * to compare just relative addresses. -acme
713 static enum match_result
match_chain_dso_addresses(struct map
*left_map
, u64 left_ip
,
714 struct map
*right_map
, u64 right_ip
)
716 struct dso
*left_dso
= left_map
? map__dso(left_map
) : NULL
;
717 struct dso
*right_dso
= right_map
? map__dso(right_map
) : NULL
;
719 if (left_dso
!= right_dso
)
720 return left_dso
< right_dso
? MATCH_LT
: MATCH_GT
;
722 if (left_ip
!= right_ip
)
723 return left_ip
< right_ip
? MATCH_LT
: MATCH_GT
;
728 static enum match_result
match_chain(struct callchain_cursor_node
*node
,
729 struct callchain_list
*cnode
)
731 enum match_result match
= MATCH_ERROR
;
733 switch (callchain_param
.key
) {
735 match
= match_chain_strings(cnode
->srcline
, node
->srcline
);
736 if (match
!= MATCH_ERROR
)
738 /* otherwise fall-back to symbol-based comparison below */
741 if (node
->ms
.sym
&& cnode
->ms
.sym
) {
743 * Compare inlined frames based on their symbol name
744 * because different inlined frames will have the same
745 * symbol start. Otherwise do a faster comparison based
746 * on the symbol start address.
748 if (cnode
->ms
.sym
->inlined
|| node
->ms
.sym
->inlined
) {
749 match
= match_chain_strings(cnode
->ms
.sym
->name
,
751 if (match
!= MATCH_ERROR
)
754 match
= match_chain_dso_addresses(cnode
->ms
.map
, cnode
->ms
.sym
->start
,
755 node
->ms
.map
, node
->ms
.sym
->start
);
759 /* otherwise fall-back to IP-based comparison below */
763 match
= match_chain_dso_addresses(cnode
->ms
.map
, cnode
->ip
, node
->ms
.map
, node
->ip
);
767 if (match
== MATCH_EQ
&& node
->branch
) {
768 cnode
->branch_count
++;
770 if (node
->branch_from
) {
772 * It's "to" of a branch
774 if (!cnode
->brtype_stat
) {
775 cnode
->brtype_stat
= zalloc(sizeof(*cnode
->brtype_stat
));
776 if (!cnode
->brtype_stat
) {
777 perror("not enough memory for the code path branch statistics");
781 cnode
->brtype_stat
->branch_to
= true;
783 if (node
->branch_flags
.predicted
)
784 cnode
->predicted_count
++;
786 if (node
->branch_flags
.abort
)
787 cnode
->abort_count
++;
789 branch_type_count(cnode
->brtype_stat
,
795 * It's "from" of a branch
797 if (cnode
->brtype_stat
&& cnode
->brtype_stat
->branch_to
)
798 cnode
->brtype_stat
->branch_to
= false;
799 cnode
->cycles_count
+= node
->branch_flags
.cycles
;
800 cnode
->iter_count
+= node
->nr_loop_iter
;
801 cnode
->iter_cycles
+= node
->iter_cycles
;
810 * Split the parent in two parts (a new child is created) and
811 * give a part of its callchain to the created child.
812 * Then create another child to host the given callchain of new branch
815 split_add_child(struct callchain_node
*parent
,
816 struct callchain_cursor
*cursor
,
817 struct callchain_list
*to_split
,
818 u64 idx_parents
, u64 idx_local
, u64 period
)
820 struct callchain_node
*new;
821 struct list_head
*old_tail
;
822 unsigned int idx_total
= idx_parents
+ idx_local
;
825 new = create_child(parent
, true);
829 /* split the callchain and move a part to the new child */
830 old_tail
= parent
->val
.prev
;
831 list_del_range(&to_split
->list
, old_tail
);
832 new->val
.next
= &to_split
->list
;
833 new->val
.prev
= old_tail
;
834 to_split
->list
.prev
= &new->val
;
835 old_tail
->next
= &new->val
;
838 new->hit
= parent
->hit
;
839 new->children_hit
= parent
->children_hit
;
840 parent
->children_hit
= callchain_cumul_hits(new);
841 new->val_nr
= parent
->val_nr
- idx_local
;
842 parent
->val_nr
= idx_local
;
843 new->count
= parent
->count
;
844 new->children_count
= parent
->children_count
;
845 parent
->children_count
= callchain_cumul_counts(new);
847 /* create a new child for the new branch if any */
848 if (idx_total
< cursor
->nr
) {
849 struct callchain_node
*first
;
850 struct callchain_list
*cnode
;
851 struct callchain_cursor_node
*node
;
852 struct rb_node
*p
, **pp
;
855 parent
->children_hit
+= period
;
857 parent
->children_count
+= 1;
859 node
= callchain_cursor_current(cursor
);
860 new = add_child(parent
, cursor
, period
);
865 * This is second child since we moved parent's children
866 * to new (first) child above.
868 p
= parent
->rb_root_in
.rb_node
;
869 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
870 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
873 if (match_chain(node
, cnode
) == MATCH_LT
)
878 rb_link_node(&new->rb_node_in
, p
, pp
);
879 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
881 parent
->hit
= period
;
887 static enum match_result
888 append_chain(struct callchain_node
*root
,
889 struct callchain_cursor
*cursor
,
893 append_chain_children(struct callchain_node
*root
,
894 struct callchain_cursor
*cursor
,
897 struct callchain_node
*rnode
;
898 struct callchain_cursor_node
*node
;
899 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
900 struct rb_node
*parent
= NULL
;
902 node
= callchain_cursor_current(cursor
);
906 /* lookup in children */
908 enum match_result ret
;
911 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
913 /* If at least first entry matches, rely to children */
914 ret
= append_chain(rnode
, cursor
, period
);
916 goto inc_children_hit
;
917 if (ret
== MATCH_ERROR
)
921 p
= &parent
->rb_left
;
923 p
= &parent
->rb_right
;
925 /* nothing in children, add to the current node */
926 rnode
= add_child(root
, cursor
, period
);
930 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
931 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
934 root
->children_hit
+= period
;
935 root
->children_count
++;
939 static enum match_result
940 append_chain(struct callchain_node
*root
,
941 struct callchain_cursor
*cursor
,
944 struct callchain_list
*cnode
;
945 u64 start
= cursor
->pos
;
948 enum match_result cmp
= MATCH_ERROR
;
951 * Lookup in the current node
952 * If we have a symbol, then compare the start to match
953 * anywhere inside a function, unless function
956 list_for_each_entry(cnode
, &root
->val
, list
) {
957 struct callchain_cursor_node
*node
;
959 node
= callchain_cursor_current(cursor
);
963 cmp
= match_chain(node
, cnode
);
969 callchain_cursor_advance(cursor
);
972 /* matches not, relay no the parent */
974 WARN_ONCE(cmp
== MATCH_ERROR
, "Chain comparison error\n");
978 matches
= cursor
->pos
- start
;
980 /* we match only a part of the node. Split it and add the new chain */
981 if (matches
< root
->val_nr
) {
982 if (split_add_child(root
, cursor
, cnode
, start
, matches
,
989 /* we match 100% of the path, increment the hit */
990 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
996 /* We match the node and still have a part remaining */
997 if (append_chain_children(root
, cursor
, period
) < 0)
1003 int callchain_append(struct callchain_root
*root
,
1004 struct callchain_cursor
*cursor
,
1013 callchain_cursor_commit(cursor
);
1015 if (append_chain_children(&root
->node
, cursor
, period
) < 0)
1018 if (cursor
->nr
> root
->max_depth
)
1019 root
->max_depth
= cursor
->nr
;
1025 merge_chain_branch(struct callchain_cursor
*cursor
,
1026 struct callchain_node
*dst
, struct callchain_node
*src
)
1028 struct callchain_cursor_node
**old_last
= cursor
->last
;
1029 struct callchain_node
*child
;
1030 struct callchain_list
*list
, *next_list
;
1032 int old_pos
= cursor
->nr
;
1035 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
1036 struct map_symbol ms
= {
1037 .maps
= maps__get(list
->ms
.maps
),
1038 .map
= map__get(list
->ms
.map
),
1040 callchain_cursor_append(cursor
, list
->ip
, &ms
, false, NULL
, 0, 0, 0, list
->srcline
);
1041 list_del_init(&list
->list
);
1042 map_symbol__exit(&ms
);
1043 map_symbol__exit(&list
->ms
);
1044 zfree(&list
->brtype_stat
);
1049 callchain_cursor_commit(cursor
);
1050 if (append_chain_children(dst
, cursor
, src
->hit
) < 0)
1054 n
= rb_first(&src
->rb_root_in
);
1056 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1058 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
1060 err
= merge_chain_branch(cursor
, dst
, child
);
1067 cursor
->nr
= old_pos
;
1068 cursor
->last
= old_last
;
1073 int callchain_merge(struct callchain_cursor
*cursor
,
1074 struct callchain_root
*dst
, struct callchain_root
*src
)
1076 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
1079 int callchain_cursor_append(struct callchain_cursor
*cursor
,
1080 u64 ip
, struct map_symbol
*ms
,
1081 bool branch
, struct branch_flags
*flags
,
1082 int nr_loop_iter
, u64 iter_cycles
, u64 branch_from
,
1083 const char *srcline
)
1085 struct callchain_cursor_node
*node
= *cursor
->last
;
1088 node
= calloc(1, sizeof(*node
));
1092 *cursor
->last
= node
;
1096 map_symbol__exit(&node
->ms
);
1098 node
->ms
.maps
= maps__get(ms
->maps
);
1099 node
->ms
.map
= map__get(ms
->map
);
1100 node
->branch
= branch
;
1101 node
->nr_loop_iter
= nr_loop_iter
;
1102 node
->iter_cycles
= iter_cycles
;
1103 node
->srcline
= srcline
;
1106 memcpy(&node
->branch_flags
, flags
,
1107 sizeof(struct branch_flags
));
1109 node
->branch_from
= branch_from
;
1112 cursor
->last
= &node
->next
;
1117 int sample__resolve_callchain(struct perf_sample
*sample
,
1118 struct callchain_cursor
*cursor
, struct symbol
**parent
,
1119 struct evsel
*evsel
, struct addr_location
*al
,
1122 if (sample
->callchain
== NULL
&& !symbol_conf
.show_branchflag_count
)
1125 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
1126 perf_hpp_list
.parent
|| symbol_conf
.show_branchflag_count
) {
1127 return thread__resolve_callchain(al
->thread
, cursor
, evsel
, sample
,
1128 parent
, al
, max_stack
);
1133 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
1135 if ((!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
) &&
1136 !symbol_conf
.show_branchflag_count
)
1138 return callchain_append(he
->callchain
, get_tls_callchain_cursor(), sample
->period
);
1141 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
1142 bool hide_unresolved
)
1144 struct machine
*machine
= node
->ms
.maps
? maps__machine(node
->ms
.maps
) : NULL
;
1146 maps__put(al
->maps
);
1147 al
->maps
= maps__get(node
->ms
.maps
);
1149 al
->map
= map__get(node
->ms
.map
);
1150 al
->sym
= node
->ms
.sym
;
1151 al
->srcline
= node
->srcline
;
1152 al
->addr
= node
->ip
;
1154 if (al
->sym
== NULL
) {
1155 if (hide_unresolved
)
1157 if (al
->map
== NULL
)
1160 if (maps__equal(al
->maps
, machine__kernel_maps(machine
))) {
1161 if (machine__is_host(machine
)) {
1162 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
1165 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
1169 if (machine__is_host(machine
)) {
1170 al
->cpumode
= PERF_RECORD_MISC_USER
;
1172 } else if (perf_guest
) {
1173 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
1176 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1185 char *callchain_list__sym_name(struct callchain_list
*cl
,
1186 char *bf
, size_t bfsize
, bool show_dso
)
1188 bool show_addr
= callchain_param
.key
== CCKEY_ADDRESS
;
1189 bool show_srcline
= show_addr
|| callchain_param
.key
== CCKEY_SRCLINE
;
1193 const char *inlined
= cl
->ms
.sym
->inlined
? " (inlined)" : "";
1195 if (show_srcline
&& cl
->srcline
)
1196 printed
= scnprintf(bf
, bfsize
, "%s %s%s",
1197 cl
->ms
.sym
->name
, cl
->srcline
,
1200 printed
= scnprintf(bf
, bfsize
, "%s%s",
1201 cl
->ms
.sym
->name
, inlined
);
1203 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
1206 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
1208 dso__short_name(map__dso(cl
->ms
.map
)) :
1214 char *callchain_node__scnprintf_value(struct callchain_node
*node
,
1215 char *bf
, size_t bfsize
, u64 total
)
1217 double percent
= 0.0;
1218 u64 period
= callchain_cumul_hits(node
);
1219 unsigned count
= callchain_cumul_counts(node
);
1221 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1223 count
= node
->count
;
1226 switch (callchain_param
.value
) {
1228 scnprintf(bf
, bfsize
, "%"PRIu64
, period
);
1231 scnprintf(bf
, bfsize
, "%u", count
);
1236 percent
= period
* 100.0 / total
;
1237 scnprintf(bf
, bfsize
, "%.2f%%", percent
);
1243 int callchain_node__fprintf_value(struct callchain_node
*node
,
1244 FILE *fp
, u64 total
)
1246 double percent
= 0.0;
1247 u64 period
= callchain_cumul_hits(node
);
1248 unsigned count
= callchain_cumul_counts(node
);
1250 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1252 count
= node
->count
;
1255 switch (callchain_param
.value
) {
1257 return fprintf(fp
, "%"PRIu64
, period
);
1259 return fprintf(fp
, "%u", count
);
1263 percent
= period
* 100.0 / total
;
1264 return percent_color_fprintf(fp
, "%.2f%%", percent
);
1269 static void callchain_counts_value(struct callchain_node
*node
,
1270 u64
*branch_count
, u64
*predicted_count
,
1271 u64
*abort_count
, u64
*cycles_count
)
1273 struct callchain_list
*clist
;
1275 list_for_each_entry(clist
, &node
->val
, list
) {
1277 *branch_count
+= clist
->branch_count
;
1279 if (predicted_count
)
1280 *predicted_count
+= clist
->predicted_count
;
1283 *abort_count
+= clist
->abort_count
;
1286 *cycles_count
+= clist
->cycles_count
;
1290 static int callchain_node_branch_counts_cumul(struct callchain_node
*node
,
1292 u64
*predicted_count
,
1296 struct callchain_node
*child
;
1299 n
= rb_first(&node
->rb_root_in
);
1301 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
1304 callchain_node_branch_counts_cumul(child
, branch_count
,
1309 callchain_counts_value(child
, branch_count
,
1310 predicted_count
, abort_count
,
1317 int callchain_branch_counts(struct callchain_root
*root
,
1318 u64
*branch_count
, u64
*predicted_count
,
1319 u64
*abort_count
, u64
*cycles_count
)
1324 if (predicted_count
)
1325 *predicted_count
= 0;
1333 return callchain_node_branch_counts_cumul(&root
->node
,
1340 static int count_pri64_printf(int idx
, const char *str
, u64 value
, char *bf
, int bfsize
)
1342 return scnprintf(bf
, bfsize
, "%s%s:%" PRId64
"", (idx
) ? " " : " (", str
, value
);
1345 static int count_float_printf(int idx
, const char *str
, float value
,
1346 char *bf
, int bfsize
, float threshold
)
1348 if (threshold
!= 0.0 && value
< threshold
)
1351 return scnprintf(bf
, bfsize
, "%s%s:%.1f%%", (idx
) ? " " : " (", str
, value
);
1354 static int branch_to_str(char *bf
, int bfsize
,
1355 u64 branch_count
, u64 predicted_count
,
1357 const struct branch_type_stat
*brtype_stat
)
1361 printed
= branch_type_str(brtype_stat
, bf
, bfsize
);
1365 if (predicted_count
< branch_count
) {
1366 printed
+= count_float_printf(i
++, "predicted",
1367 predicted_count
* 100.0 / branch_count
,
1368 bf
+ printed
, bfsize
- printed
, 0.0);
1372 printed
+= count_float_printf(i
++, "abort",
1373 abort_count
* 100.0 / branch_count
,
1374 bf
+ printed
, bfsize
- printed
, 0.1);
1378 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1383 static int branch_from_str(char *bf
, int bfsize
,
1385 u64 cycles_count
, u64 iter_count
,
1386 u64 iter_cycles
, u64 from_count
)
1388 int printed
= 0, i
= 0;
1391 cycles
= cycles_count
/ branch_count
;
1393 printed
+= count_pri64_printf(i
++, "cycles",
1395 bf
+ printed
, bfsize
- printed
);
1398 if (iter_count
&& from_count
) {
1399 v
= iter_count
/ from_count
;
1401 printed
+= count_pri64_printf(i
++, "iter",
1402 v
, bf
+ printed
, bfsize
- printed
);
1404 printed
+= count_pri64_printf(i
++, "avg_cycles",
1405 iter_cycles
/ iter_count
,
1406 bf
+ printed
, bfsize
- printed
);
1411 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1416 static int counts_str_build(char *bf
, int bfsize
,
1417 u64 branch_count
, u64 predicted_count
,
1418 u64 abort_count
, u64 cycles_count
,
1419 u64 iter_count
, u64 iter_cycles
,
1421 const struct branch_type_stat
*brtype_stat
)
1425 if (branch_count
== 0)
1426 return scnprintf(bf
, bfsize
, " (calltrace)");
1428 if (brtype_stat
->branch_to
) {
1429 printed
= branch_to_str(bf
, bfsize
, branch_count
,
1430 predicted_count
, abort_count
, brtype_stat
);
1432 printed
= branch_from_str(bf
, bfsize
, branch_count
,
1433 cycles_count
, iter_count
, iter_cycles
,
1443 static int callchain_counts_printf(FILE *fp
, char *bf
, int bfsize
,
1444 u64 branch_count
, u64 predicted_count
,
1445 u64 abort_count
, u64 cycles_count
,
1446 u64 iter_count
, u64 iter_cycles
,
1448 const struct branch_type_stat
*brtype_stat
)
1452 counts_str_build(str
, sizeof(str
), branch_count
,
1453 predicted_count
, abort_count
, cycles_count
,
1454 iter_count
, iter_cycles
, from_count
, brtype_stat
);
1457 return fprintf(fp
, "%s", str
);
1459 return scnprintf(bf
, bfsize
, "%s", str
);
1462 int callchain_list_counts__printf_value(struct callchain_list
*clist
,
1463 FILE *fp
, char *bf
, int bfsize
)
1465 static const struct branch_type_stat empty_brtype_stat
= {};
1466 const struct branch_type_stat
*brtype_stat
;
1467 u64 branch_count
, predicted_count
;
1468 u64 abort_count
, cycles_count
;
1469 u64 iter_count
, iter_cycles
;
1472 brtype_stat
= clist
->brtype_stat
?: &empty_brtype_stat
;
1473 branch_count
= clist
->branch_count
;
1474 predicted_count
= clist
->predicted_count
;
1475 abort_count
= clist
->abort_count
;
1476 cycles_count
= clist
->cycles_count
;
1477 iter_count
= clist
->iter_count
;
1478 iter_cycles
= clist
->iter_cycles
;
1479 from_count
= clist
->from_count
;
1481 return callchain_counts_printf(fp
, bf
, bfsize
, branch_count
,
1482 predicted_count
, abort_count
,
1483 cycles_count
, iter_count
, iter_cycles
,
1484 from_count
, brtype_stat
);
1487 static void free_callchain_node(struct callchain_node
*node
)
1489 struct callchain_list
*list
, *tmp
;
1490 struct callchain_node
*child
;
1493 list_for_each_entry_safe(list
, tmp
, &node
->parent_val
, list
) {
1494 list_del_init(&list
->list
);
1495 map_symbol__exit(&list
->ms
);
1496 zfree(&list
->brtype_stat
);
1500 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
1501 list_del_init(&list
->list
);
1502 map_symbol__exit(&list
->ms
);
1503 zfree(&list
->brtype_stat
);
1507 n
= rb_first(&node
->rb_root_in
);
1509 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1511 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
1513 free_callchain_node(child
);
1518 void free_callchain(struct callchain_root
*root
)
1520 if (!symbol_conf
.use_callchain
)
1523 free_callchain_node(&root
->node
);
1526 static u64
decay_callchain_node(struct callchain_node
*node
)
1528 struct callchain_node
*child
;
1532 n
= rb_first(&node
->rb_root_in
);
1534 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1536 child_hits
+= decay_callchain_node(child
);
1540 node
->hit
= (node
->hit
* 7) / 8;
1541 node
->children_hit
= child_hits
;
1546 void decay_callchain(struct callchain_root
*root
)
1548 if (!symbol_conf
.use_callchain
)
1551 decay_callchain_node(&root
->node
);
1554 int callchain_node__make_parent_list(struct callchain_node
*node
)
1556 struct callchain_node
*parent
= node
->parent
;
1557 struct callchain_list
*chain
, *new;
1561 list_for_each_entry_reverse(chain
, &parent
->val
, list
) {
1562 new = malloc(sizeof(*new));
1566 new->has_children
= false;
1567 new->ms
.map
= map__get(new->ms
.map
);
1568 list_add_tail(&new->list
, &head
);
1570 parent
= parent
->parent
;
1573 list_for_each_entry_safe_reverse(chain
, new, &head
, list
)
1574 list_move_tail(&chain
->list
, &node
->parent_val
);
1576 if (!list_empty(&node
->parent_val
)) {
1577 chain
= list_first_entry(&node
->parent_val
, struct callchain_list
, list
);
1578 chain
->has_children
= rb_prev(&node
->rb_node
) || rb_next(&node
->rb_node
);
1580 chain
= list_first_entry(&node
->val
, struct callchain_list
, list
);
1581 chain
->has_children
= false;
1586 list_for_each_entry_safe(chain
, new, &head
, list
) {
1587 list_del_init(&chain
->list
);
1588 map_symbol__exit(&chain
->ms
);
1589 zfree(&chain
->brtype_stat
);
1595 static void callchain_cursor__delete(void *vcursor
)
1597 struct callchain_cursor
*cursor
= vcursor
;
1598 struct callchain_cursor_node
*node
, *next
;
1600 callchain_cursor_reset(cursor
);
1601 for (node
= cursor
->first
; node
!= NULL
; node
= next
) {
1608 static void init_callchain_cursor_key(void)
1610 if (pthread_key_create(&callchain_cursor
, callchain_cursor__delete
)) {
1611 pr_err("callchain cursor creation failed");
1616 struct callchain_cursor
*get_tls_callchain_cursor(void)
1618 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
1619 struct callchain_cursor
*cursor
;
1621 pthread_once(&once_control
, init_callchain_cursor_key
);
1622 cursor
= pthread_getspecific(callchain_cursor
);
1624 cursor
= zalloc(sizeof(*cursor
));
1626 pr_debug3("%s: not enough memory\n", __func__
);
1627 pthread_setspecific(callchain_cursor
, cursor
);
1632 int callchain_cursor__copy(struct callchain_cursor
*dst
,
1633 struct callchain_cursor
*src
)
1637 callchain_cursor_reset(dst
);
1638 callchain_cursor_commit(src
);
1641 struct callchain_cursor_node
*node
;
1643 node
= callchain_cursor_current(src
);
1647 rc
= callchain_cursor_append(dst
, node
->ip
, &node
->ms
,
1648 node
->branch
, &node
->branch_flags
,
1651 node
->branch_from
, node
->srcline
);
1655 callchain_cursor_advance(src
);
1662 * Initialize a cursor before adding entries inside, but keep
1663 * the previously allocated entries as a cache.
1665 void callchain_cursor_reset(struct callchain_cursor
*cursor
)
1667 struct callchain_cursor_node
*node
;
1670 cursor
->last
= &cursor
->first
;
1672 for (node
= cursor
->first
; node
!= NULL
; node
= node
->next
)
1673 map_symbol__exit(&node
->ms
);
1676 void callchain_param_setup(u64 sample_type
, const char *arch
)
1678 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
1679 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
1680 (sample_type
& PERF_SAMPLE_STACK_USER
)) {
1681 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
1682 dwarf_callchain_users
= true;
1683 } else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
1684 callchain_param
.record_mode
= CALLCHAIN_LBR
;
1686 callchain_param
.record_mode
= CALLCHAIN_FP
;
1690 * It's necessary to use libunwind to reliably determine the caller of
1691 * a leaf function on aarch64, as otherwise we cannot know whether to
1692 * start from the LR or FP.
1694 * Always starting from the LR can result in duplicate or entirely
1695 * erroneous entries. Always skipping the LR and starting from the FP
1696 * can result in missing entries.
1698 if (callchain_param
.record_mode
== CALLCHAIN_FP
&& !strcmp(arch
, "arm64"))
1699 dwarf_callchain_users
= true;
1702 static bool chain_match(struct callchain_list
*base_chain
,
1703 struct callchain_list
*pair_chain
)
1705 enum match_result match
;
1707 match
= match_chain_strings(base_chain
->srcline
,
1708 pair_chain
->srcline
);
1709 if (match
!= MATCH_ERROR
)
1710 return match
== MATCH_EQ
;
1712 match
= match_chain_dso_addresses(base_chain
->ms
.map
,
1717 return match
== MATCH_EQ
;
1720 bool callchain_cnode_matched(struct callchain_node
*base_cnode
,
1721 struct callchain_node
*pair_cnode
)
1723 struct callchain_list
*base_chain
, *pair_chain
;
1726 pair_chain
= list_first_entry(&pair_cnode
->val
,
1727 struct callchain_list
,
1730 list_for_each_entry(base_chain
, &base_cnode
->val
, list
) {
1731 if (&pair_chain
->list
== &pair_cnode
->val
)
1734 if (!base_chain
->srcline
|| !pair_chain
->srcline
) {
1735 pair_chain
= list_next_entry(pair_chain
, list
);
1739 match
= chain_match(base_chain
, pair_chain
);
1743 pair_chain
= list_next_entry(pair_chain
, list
);
1747 * Say chain1 is ABC, chain2 is ABCD, we consider they are
1748 * not fully matched.
1750 if (pair_chain
&& (&pair_chain
->list
!= &pair_cnode
->val
))
1756 static u64
count_callchain_hits(struct hist_entry
*he
)
1758 struct rb_root
*root
= &he
->sorted_chain
;
1759 struct rb_node
*rb_node
= rb_first(root
);
1760 struct callchain_node
*node
;
1764 node
= rb_entry(rb_node
, struct callchain_node
, rb_node
);
1765 chain_hits
+= node
->hit
;
1766 rb_node
= rb_next(rb_node
);
1772 u64
callchain_total_hits(struct hists
*hists
)
1774 struct rb_node
*next
= rb_first_cached(&hists
->entries
);
1778 struct hist_entry
*he
= rb_entry(next
, struct hist_entry
,
1781 chain_hits
+= count_callchain_hits(he
);
1782 next
= rb_next(&he
->rb_node
);
1788 s64
callchain_avg_cycles(struct callchain_node
*cnode
)
1790 struct callchain_list
*chain
;
1793 list_for_each_entry(chain
, &cnode
->val
, list
) {
1794 if (chain
->srcline
&& chain
->branch_count
)
1795 cycles
+= chain
->cycles_count
/ chain
->branch_count
;
1801 int sample__for_each_callchain_node(struct thread
*thread
, struct evsel
*evsel
,
1802 struct perf_sample
*sample
, int max_stack
,
1803 bool symbols
, callchain_iter_fn cb
, void *data
)
1805 struct callchain_cursor
*cursor
= get_tls_callchain_cursor();
1811 /* Fill in the callchain. */
1812 ret
= __thread__resolve_callchain(thread
, cursor
, evsel
, sample
,
1813 /*parent=*/NULL
, /*root_al=*/NULL
,
1814 max_stack
, symbols
);
1818 /* Switch from writing the callchain to reading it. */
1819 callchain_cursor_commit(cursor
);
1822 struct callchain_cursor_node
*node
= callchain_cursor_current(cursor
);
1827 ret
= cb(node
, data
);
1831 callchain_cursor_advance(cursor
);