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
40 struct callchain_param callchain_param_default
= {
41 CALLCHAIN_PARAM_DEFAULT
44 __thread
struct callchain_cursor callchain_cursor
;
46 int parse_callchain_record_opt(const char *arg
, struct callchain_param
*param
)
48 return parse_callchain_record(arg
, param
);
51 static int parse_callchain_mode(const char *value
)
53 if (!strncmp(value
, "graph", strlen(value
))) {
54 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
57 if (!strncmp(value
, "flat", strlen(value
))) {
58 callchain_param
.mode
= CHAIN_FLAT
;
61 if (!strncmp(value
, "fractal", strlen(value
))) {
62 callchain_param
.mode
= CHAIN_GRAPH_REL
;
65 if (!strncmp(value
, "folded", strlen(value
))) {
66 callchain_param
.mode
= CHAIN_FOLDED
;
72 static int parse_callchain_order(const char *value
)
74 if (!strncmp(value
, "caller", strlen(value
))) {
75 callchain_param
.order
= ORDER_CALLER
;
76 callchain_param
.order_set
= true;
79 if (!strncmp(value
, "callee", strlen(value
))) {
80 callchain_param
.order
= ORDER_CALLEE
;
81 callchain_param
.order_set
= true;
87 static int parse_callchain_sort_key(const char *value
)
89 if (!strncmp(value
, "function", strlen(value
))) {
90 callchain_param
.key
= CCKEY_FUNCTION
;
93 if (!strncmp(value
, "address", strlen(value
))) {
94 callchain_param
.key
= CCKEY_ADDRESS
;
97 if (!strncmp(value
, "srcline", strlen(value
))) {
98 callchain_param
.key
= CCKEY_SRCLINE
;
101 if (!strncmp(value
, "branch", strlen(value
))) {
102 callchain_param
.branch_callstack
= 1;
108 static int parse_callchain_value(const char *value
)
110 if (!strncmp(value
, "percent", strlen(value
))) {
111 callchain_param
.value
= CCVAL_PERCENT
;
114 if (!strncmp(value
, "period", strlen(value
))) {
115 callchain_param
.value
= CCVAL_PERIOD
;
118 if (!strncmp(value
, "count", strlen(value
))) {
119 callchain_param
.value
= CCVAL_COUNT
;
125 static int get_stack_size(const char *str
, unsigned long *_size
)
129 unsigned long max_size
= round_down(USHRT_MAX
, sizeof(u64
));
131 size
= strtoul(str
, &endptr
, 0);
137 size
= round_up(size
, sizeof(u64
));
138 if (!size
|| size
> max_size
)
146 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
152 __parse_callchain_report_opt(const char *arg
, bool allow_record_opt
)
155 char *endptr
, *saveptr
= NULL
;
156 bool minpcnt_set
= false;
157 bool record_opt_set
= false;
158 bool try_stack_size
= false;
160 callchain_param
.enabled
= true;
161 symbol_conf
.use_callchain
= true;
166 while ((tok
= strtok_r((char *)arg
, ",", &saveptr
)) != NULL
) {
167 if (!strncmp(tok
, "none", strlen(tok
))) {
168 callchain_param
.mode
= CHAIN_NONE
;
169 callchain_param
.enabled
= false;
170 symbol_conf
.use_callchain
= false;
174 if (!parse_callchain_mode(tok
) ||
175 !parse_callchain_order(tok
) ||
176 !parse_callchain_sort_key(tok
) ||
177 !parse_callchain_value(tok
)) {
178 /* parsing ok - move on to the next */
179 try_stack_size
= false;
181 } else if (allow_record_opt
&& !record_opt_set
) {
182 if (parse_callchain_record(tok
, &callchain_param
))
185 /* assume that number followed by 'dwarf' is stack size */
186 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
)
187 try_stack_size
= true;
189 record_opt_set
= true;
194 if (try_stack_size
) {
195 unsigned long size
= 0;
197 if (get_stack_size(tok
, &size
) < 0)
199 callchain_param
.dump_size
= size
;
200 try_stack_size
= false;
201 } else if (!minpcnt_set
) {
202 /* try to get the min percent */
203 callchain_param
.min_percent
= strtod(tok
, &endptr
);
208 /* try print limit at last */
209 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
217 if (callchain_register_param(&callchain_param
) < 0) {
218 pr_err("Can't register callchain params\n");
224 int parse_callchain_report_opt(const char *arg
)
226 return __parse_callchain_report_opt(arg
, false);
229 int parse_callchain_top_opt(const char *arg
)
231 return __parse_callchain_report_opt(arg
, true);
234 int parse_callchain_record(const char *arg
, struct callchain_param
*param
)
236 char *tok
, *name
, *saveptr
= NULL
;
240 /* We need buffer that we know we can write to. */
241 buf
= malloc(strlen(arg
) + 1);
247 tok
= strtok_r((char *)buf
, ",", &saveptr
);
248 name
= tok
? : (char *)buf
;
251 /* Framepointer style */
252 if (!strncmp(name
, "fp", sizeof("fp"))) {
253 if (!strtok_r(NULL
, ",", &saveptr
)) {
254 param
->record_mode
= CALLCHAIN_FP
;
257 pr_err("callchain: No more arguments "
258 "needed for --call-graph fp\n");
262 } else if (!strncmp(name
, "dwarf", sizeof("dwarf"))) {
263 const unsigned long default_stack_dump_size
= 8192;
266 param
->record_mode
= CALLCHAIN_DWARF
;
267 param
->dump_size
= default_stack_dump_size
;
269 tok
= strtok_r(NULL
, ",", &saveptr
);
271 unsigned long size
= 0;
273 ret
= get_stack_size(tok
, &size
);
274 param
->dump_size
= size
;
276 } else if (!strncmp(name
, "lbr", sizeof("lbr"))) {
277 if (!strtok_r(NULL
, ",", &saveptr
)) {
278 param
->record_mode
= CALLCHAIN_LBR
;
281 pr_err("callchain: No more arguments "
282 "needed for --call-graph lbr\n");
285 pr_err("callchain: Unknown --call-graph option "
296 int perf_callchain_config(const char *var
, const char *value
)
300 if (!strstarts(var
, "call-graph."))
302 var
+= sizeof("call-graph.") - 1;
304 if (!strcmp(var
, "record-mode"))
305 return parse_callchain_record_opt(value
, &callchain_param
);
306 if (!strcmp(var
, "dump-size")) {
307 unsigned long size
= 0;
310 ret
= get_stack_size(value
, &size
);
311 callchain_param
.dump_size
= size
;
315 if (!strcmp(var
, "print-type")){
317 ret
= parse_callchain_mode(value
);
319 pr_err("Invalid callchain mode: %s\n", value
);
322 if (!strcmp(var
, "order")){
324 ret
= parse_callchain_order(value
);
326 pr_err("Invalid callchain order: %s\n", value
);
329 if (!strcmp(var
, "sort-key")){
331 ret
= parse_callchain_sort_key(value
);
333 pr_err("Invalid callchain sort key: %s\n", value
);
336 if (!strcmp(var
, "threshold")) {
337 callchain_param
.min_percent
= strtod(value
, &endptr
);
338 if (value
== endptr
) {
339 pr_err("Invalid callchain threshold: %s\n", value
);
343 if (!strcmp(var
, "print-limit")) {
344 callchain_param
.print_limit
= strtod(value
, &endptr
);
345 if (value
== endptr
) {
346 pr_err("Invalid callchain print limit: %s\n", value
);
355 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
356 enum chain_mode mode
)
358 struct rb_node
**p
= &root
->rb_node
;
359 struct rb_node
*parent
= NULL
;
360 struct callchain_node
*rnode
;
361 u64 chain_cumul
= callchain_cumul_hits(chain
);
367 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
368 rnode_cumul
= callchain_cumul_hits(rnode
);
373 if (rnode
->hit
< chain
->hit
)
378 case CHAIN_GRAPH_ABS
: /* Falldown */
379 case CHAIN_GRAPH_REL
:
380 if (rnode_cumul
< chain_cumul
)
391 rb_link_node(&chain
->rb_node
, parent
, p
);
392 rb_insert_color(&chain
->rb_node
, root
);
396 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
400 struct callchain_node
*child
;
402 n
= rb_first(&node
->rb_root_in
);
404 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
407 __sort_chain_flat(rb_root
, child
, min_hit
);
410 if (node
->hit
&& node
->hit
>= min_hit
)
411 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
415 * Once we get every callchains from the stream, we can now
419 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
420 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
423 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
426 static void __sort_chain_graph_abs(struct callchain_node
*node
,
430 struct callchain_node
*child
;
432 node
->rb_root
= RB_ROOT
;
433 n
= rb_first(&node
->rb_root_in
);
436 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
439 __sort_chain_graph_abs(child
, min_hit
);
440 if (callchain_cumul_hits(child
) >= min_hit
)
441 rb_insert_callchain(&node
->rb_root
, child
,
447 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
448 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
450 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
451 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
454 static void __sort_chain_graph_rel(struct callchain_node
*node
,
458 struct callchain_node
*child
;
461 node
->rb_root
= RB_ROOT
;
462 min_hit
= ceil(node
->children_hit
* min_percent
);
464 n
= rb_first(&node
->rb_root_in
);
466 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
469 __sort_chain_graph_rel(child
, min_percent
);
470 if (callchain_cumul_hits(child
) >= min_hit
)
471 rb_insert_callchain(&node
->rb_root
, child
,
477 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
478 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
480 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
481 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
484 int callchain_register_param(struct callchain_param
*param
)
486 switch (param
->mode
) {
487 case CHAIN_GRAPH_ABS
:
488 param
->sort
= sort_chain_graph_abs
;
490 case CHAIN_GRAPH_REL
:
491 param
->sort
= sort_chain_graph_rel
;
495 param
->sort
= sort_chain_flat
;
505 * Create a child for a parent. If inherit_children, then the new child
506 * will become the new parent of it's parent children
508 static struct callchain_node
*
509 create_child(struct callchain_node
*parent
, bool inherit_children
)
511 struct callchain_node
*new;
513 new = zalloc(sizeof(*new));
515 perror("not enough memory to create child for code path tree");
518 new->parent
= parent
;
519 INIT_LIST_HEAD(&new->val
);
520 INIT_LIST_HEAD(&new->parent_val
);
522 if (inherit_children
) {
524 struct callchain_node
*child
;
526 new->rb_root_in
= parent
->rb_root_in
;
527 parent
->rb_root_in
= RB_ROOT
;
529 n
= rb_first(&new->rb_root_in
);
531 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
536 /* make it the first child */
537 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
538 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
546 * Fill the node with callchain values
549 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
551 struct callchain_cursor_node
*cursor_node
;
553 node
->val_nr
= cursor
->nr
- cursor
->pos
;
555 pr_warning("Warning: empty node in callchain tree\n");
557 cursor_node
= callchain_cursor_current(cursor
);
559 while (cursor_node
) {
560 struct callchain_list
*call
;
562 call
= zalloc(sizeof(*call
));
564 perror("not enough memory for the code path tree");
567 call
->ip
= cursor_node
->ip
;
568 call
->ms
.sym
= cursor_node
->sym
;
569 call
->ms
.map
= map__get(cursor_node
->map
);
571 if (cursor_node
->branch
) {
572 call
->branch_count
= 1;
574 if (cursor_node
->branch_from
) {
576 * branch_from is set with value somewhere else
577 * to imply it's "to" of a branch.
579 call
->brtype_stat
.branch_to
= true;
581 if (cursor_node
->branch_flags
.predicted
)
582 call
->predicted_count
= 1;
584 if (cursor_node
->branch_flags
.abort
)
585 call
->abort_count
= 1;
587 branch_type_count(&call
->brtype_stat
,
588 &cursor_node
->branch_flags
,
589 cursor_node
->branch_from
,
593 * It's "from" of a branch
595 call
->brtype_stat
.branch_to
= false;
597 cursor_node
->branch_flags
.cycles
;
598 call
->iter_count
= cursor_node
->nr_loop_iter
;
599 call
->iter_cycles
= cursor_node
->iter_cycles
;
603 list_add_tail(&call
->list
, &node
->val
);
605 callchain_cursor_advance(cursor
);
606 cursor_node
= callchain_cursor_current(cursor
);
611 static struct callchain_node
*
612 add_child(struct callchain_node
*parent
,
613 struct callchain_cursor
*cursor
,
616 struct callchain_node
*new;
618 new = create_child(parent
, false);
622 if (fill_node(new, cursor
) < 0) {
623 struct callchain_list
*call
, *tmp
;
625 list_for_each_entry_safe(call
, tmp
, &new->val
, list
) {
626 list_del(&call
->list
);
627 map__zput(call
->ms
.map
);
634 new->children_hit
= 0;
636 new->children_count
= 0;
648 static enum match_result
match_chain_srcline(struct callchain_cursor_node
*node
,
649 struct callchain_list
*cnode
)
653 enum match_result ret
= MATCH_EQ
;
657 left
= get_srcline(cnode
->ms
.map
->dso
,
658 map__rip_2objdump(cnode
->ms
.map
, cnode
->ip
),
659 cnode
->ms
.sym
, true, false);
661 right
= get_srcline(node
->map
->dso
,
662 map__rip_2objdump(node
->map
, node
->ip
),
663 node
->sym
, true, false);
666 cmp
= strcmp(left
, right
);
667 else if (!left
&& right
)
669 else if (left
&& !right
)
671 else if (cnode
->ip
== node
->ip
)
674 cmp
= (cnode
->ip
< node
->ip
) ? -1 : 1;
677 ret
= cmp
< 0 ? MATCH_LT
: MATCH_GT
;
684 static enum match_result
match_chain(struct callchain_cursor_node
*node
,
685 struct callchain_list
*cnode
)
687 struct symbol
*sym
= node
->sym
;
689 struct dso
*left_dso
= NULL
;
690 struct dso
*right_dso
= NULL
;
692 if (callchain_param
.key
== CCKEY_SRCLINE
) {
693 enum match_result match
= match_chain_srcline(node
, cnode
);
695 if (match
!= MATCH_ERROR
)
699 if (cnode
->ms
.sym
&& sym
&& callchain_param
.key
== CCKEY_FUNCTION
) {
700 left
= cnode
->ms
.sym
->start
;
702 left_dso
= cnode
->ms
.map
->dso
;
703 right_dso
= node
->map
->dso
;
709 if (left
== right
&& left_dso
== right_dso
) {
711 cnode
->branch_count
++;
713 if (node
->branch_from
) {
715 * It's "to" of a branch
717 cnode
->brtype_stat
.branch_to
= true;
719 if (node
->branch_flags
.predicted
)
720 cnode
->predicted_count
++;
722 if (node
->branch_flags
.abort
)
723 cnode
->abort_count
++;
725 branch_type_count(&cnode
->brtype_stat
,
731 * It's "from" of a branch
733 cnode
->brtype_stat
.branch_to
= false;
734 cnode
->cycles_count
+=
735 node
->branch_flags
.cycles
;
736 cnode
->iter_count
+= node
->nr_loop_iter
;
737 cnode
->iter_cycles
+= node
->iter_cycles
;
744 return left
> right
? MATCH_GT
: MATCH_LT
;
748 * Split the parent in two parts (a new child is created) and
749 * give a part of its callchain to the created child.
750 * Then create another child to host the given callchain of new branch
753 split_add_child(struct callchain_node
*parent
,
754 struct callchain_cursor
*cursor
,
755 struct callchain_list
*to_split
,
756 u64 idx_parents
, u64 idx_local
, u64 period
)
758 struct callchain_node
*new;
759 struct list_head
*old_tail
;
760 unsigned int idx_total
= idx_parents
+ idx_local
;
763 new = create_child(parent
, true);
767 /* split the callchain and move a part to the new child */
768 old_tail
= parent
->val
.prev
;
769 list_del_range(&to_split
->list
, old_tail
);
770 new->val
.next
= &to_split
->list
;
771 new->val
.prev
= old_tail
;
772 to_split
->list
.prev
= &new->val
;
773 old_tail
->next
= &new->val
;
776 new->hit
= parent
->hit
;
777 new->children_hit
= parent
->children_hit
;
778 parent
->children_hit
= callchain_cumul_hits(new);
779 new->val_nr
= parent
->val_nr
- idx_local
;
780 parent
->val_nr
= idx_local
;
781 new->count
= parent
->count
;
782 new->children_count
= parent
->children_count
;
783 parent
->children_count
= callchain_cumul_counts(new);
785 /* create a new child for the new branch if any */
786 if (idx_total
< cursor
->nr
) {
787 struct callchain_node
*first
;
788 struct callchain_list
*cnode
;
789 struct callchain_cursor_node
*node
;
790 struct rb_node
*p
, **pp
;
793 parent
->children_hit
+= period
;
795 parent
->children_count
+= 1;
797 node
= callchain_cursor_current(cursor
);
798 new = add_child(parent
, cursor
, period
);
803 * This is second child since we moved parent's children
804 * to new (first) child above.
806 p
= parent
->rb_root_in
.rb_node
;
807 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
808 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
811 if (match_chain(node
, cnode
) == MATCH_LT
)
816 rb_link_node(&new->rb_node_in
, p
, pp
);
817 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
819 parent
->hit
= period
;
825 static enum match_result
826 append_chain(struct callchain_node
*root
,
827 struct callchain_cursor
*cursor
,
831 append_chain_children(struct callchain_node
*root
,
832 struct callchain_cursor
*cursor
,
835 struct callchain_node
*rnode
;
836 struct callchain_cursor_node
*node
;
837 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
838 struct rb_node
*parent
= NULL
;
840 node
= callchain_cursor_current(cursor
);
844 /* lookup in childrens */
846 enum match_result ret
;
849 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
851 /* If at least first entry matches, rely to children */
852 ret
= append_chain(rnode
, cursor
, period
);
854 goto inc_children_hit
;
855 if (ret
== MATCH_ERROR
)
859 p
= &parent
->rb_left
;
861 p
= &parent
->rb_right
;
863 /* nothing in children, add to the current node */
864 rnode
= add_child(root
, cursor
, period
);
868 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
869 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
872 root
->children_hit
+= period
;
873 root
->children_count
++;
877 static enum match_result
878 append_chain(struct callchain_node
*root
,
879 struct callchain_cursor
*cursor
,
882 struct callchain_list
*cnode
;
883 u64 start
= cursor
->pos
;
886 enum match_result cmp
= MATCH_ERROR
;
889 * Lookup in the current node
890 * If we have a symbol, then compare the start to match
891 * anywhere inside a function, unless function
894 list_for_each_entry(cnode
, &root
->val
, list
) {
895 struct callchain_cursor_node
*node
;
897 node
= callchain_cursor_current(cursor
);
901 cmp
= match_chain(node
, cnode
);
907 callchain_cursor_advance(cursor
);
910 /* matches not, relay no the parent */
912 WARN_ONCE(cmp
== MATCH_ERROR
, "Chain comparison error\n");
916 matches
= cursor
->pos
- start
;
918 /* we match only a part of the node. Split it and add the new chain */
919 if (matches
< root
->val_nr
) {
920 if (split_add_child(root
, cursor
, cnode
, start
, matches
,
927 /* we match 100% of the path, increment the hit */
928 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
934 /* We match the node and still have a part remaining */
935 if (append_chain_children(root
, cursor
, period
) < 0)
941 int callchain_append(struct callchain_root
*root
,
942 struct callchain_cursor
*cursor
,
948 callchain_cursor_commit(cursor
);
950 if (append_chain_children(&root
->node
, cursor
, period
) < 0)
953 if (cursor
->nr
> root
->max_depth
)
954 root
->max_depth
= cursor
->nr
;
960 merge_chain_branch(struct callchain_cursor
*cursor
,
961 struct callchain_node
*dst
, struct callchain_node
*src
)
963 struct callchain_cursor_node
**old_last
= cursor
->last
;
964 struct callchain_node
*child
;
965 struct callchain_list
*list
, *next_list
;
967 int old_pos
= cursor
->nr
;
970 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
971 callchain_cursor_append(cursor
, list
->ip
,
972 list
->ms
.map
, list
->ms
.sym
,
973 false, NULL
, 0, 0, 0);
974 list_del(&list
->list
);
975 map__zput(list
->ms
.map
);
980 callchain_cursor_commit(cursor
);
981 if (append_chain_children(dst
, cursor
, src
->hit
) < 0)
985 n
= rb_first(&src
->rb_root_in
);
987 child
= container_of(n
, struct callchain_node
, rb_node_in
);
989 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
991 err
= merge_chain_branch(cursor
, dst
, child
);
998 cursor
->nr
= old_pos
;
999 cursor
->last
= old_last
;
1004 int callchain_merge(struct callchain_cursor
*cursor
,
1005 struct callchain_root
*dst
, struct callchain_root
*src
)
1007 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
1010 int callchain_cursor_append(struct callchain_cursor
*cursor
,
1011 u64 ip
, struct map
*map
, struct symbol
*sym
,
1012 bool branch
, struct branch_flags
*flags
,
1013 int nr_loop_iter
, u64 iter_cycles
, u64 branch_from
)
1015 struct callchain_cursor_node
*node
= *cursor
->last
;
1018 node
= calloc(1, sizeof(*node
));
1022 *cursor
->last
= node
;
1026 map__zput(node
->map
);
1027 node
->map
= map__get(map
);
1029 node
->branch
= branch
;
1030 node
->nr_loop_iter
= nr_loop_iter
;
1031 node
->iter_cycles
= iter_cycles
;
1034 memcpy(&node
->branch_flags
, flags
,
1035 sizeof(struct branch_flags
));
1037 node
->branch_from
= branch_from
;
1040 cursor
->last
= &node
->next
;
1045 int sample__resolve_callchain(struct perf_sample
*sample
,
1046 struct callchain_cursor
*cursor
, struct symbol
**parent
,
1047 struct perf_evsel
*evsel
, struct addr_location
*al
,
1050 if (sample
->callchain
== NULL
&& !symbol_conf
.show_branchflag_count
)
1053 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
1054 perf_hpp_list
.parent
|| symbol_conf
.show_branchflag_count
) {
1055 return thread__resolve_callchain(al
->thread
, cursor
, evsel
, sample
,
1056 parent
, al
, max_stack
);
1061 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
1063 if ((!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
) &&
1064 !symbol_conf
.show_branchflag_count
)
1066 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
1069 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
1070 bool hide_unresolved
)
1072 al
->map
= node
->map
;
1073 al
->sym
= node
->sym
;
1075 al
->addr
= node
->map
->map_ip(node
->map
, node
->ip
);
1077 al
->addr
= node
->ip
;
1079 if (al
->sym
== NULL
) {
1080 if (hide_unresolved
)
1082 if (al
->map
== NULL
)
1086 if (al
->map
->groups
== &al
->machine
->kmaps
) {
1087 if (machine__is_host(al
->machine
)) {
1088 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
1091 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
1095 if (machine__is_host(al
->machine
)) {
1096 al
->cpumode
= PERF_RECORD_MISC_USER
;
1098 } else if (perf_guest
) {
1099 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
1102 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1111 char *callchain_list__sym_name(struct callchain_list
*cl
,
1112 char *bf
, size_t bfsize
, bool show_dso
)
1114 bool show_addr
= callchain_param
.key
== CCKEY_ADDRESS
;
1115 bool show_srcline
= show_addr
|| callchain_param
.key
== CCKEY_SRCLINE
;
1119 if (show_srcline
&& cl
->ms
.map
&& !cl
->srcline
)
1120 cl
->srcline
= get_srcline(cl
->ms
.map
->dso
,
1121 map__rip_2objdump(cl
->ms
.map
,
1123 cl
->ms
.sym
, false, show_addr
);
1125 printed
= scnprintf(bf
, bfsize
, "%s %s",
1126 cl
->ms
.sym
->name
, cl
->srcline
);
1128 printed
= scnprintf(bf
, bfsize
, "%s", cl
->ms
.sym
->name
);
1130 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
1133 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
1135 cl
->ms
.map
->dso
->short_name
:
1141 char *callchain_node__scnprintf_value(struct callchain_node
*node
,
1142 char *bf
, size_t bfsize
, u64 total
)
1144 double percent
= 0.0;
1145 u64 period
= callchain_cumul_hits(node
);
1146 unsigned count
= callchain_cumul_counts(node
);
1148 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1150 count
= node
->count
;
1153 switch (callchain_param
.value
) {
1155 scnprintf(bf
, bfsize
, "%"PRIu64
, period
);
1158 scnprintf(bf
, bfsize
, "%u", count
);
1163 percent
= period
* 100.0 / total
;
1164 scnprintf(bf
, bfsize
, "%.2f%%", percent
);
1170 int callchain_node__fprintf_value(struct callchain_node
*node
,
1171 FILE *fp
, u64 total
)
1173 double percent
= 0.0;
1174 u64 period
= callchain_cumul_hits(node
);
1175 unsigned count
= callchain_cumul_counts(node
);
1177 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1179 count
= node
->count
;
1182 switch (callchain_param
.value
) {
1184 return fprintf(fp
, "%"PRIu64
, period
);
1186 return fprintf(fp
, "%u", count
);
1190 percent
= period
* 100.0 / total
;
1191 return percent_color_fprintf(fp
, "%.2f%%", percent
);
1196 static void callchain_counts_value(struct callchain_node
*node
,
1197 u64
*branch_count
, u64
*predicted_count
,
1198 u64
*abort_count
, u64
*cycles_count
)
1200 struct callchain_list
*clist
;
1202 list_for_each_entry(clist
, &node
->val
, list
) {
1204 *branch_count
+= clist
->branch_count
;
1206 if (predicted_count
)
1207 *predicted_count
+= clist
->predicted_count
;
1210 *abort_count
+= clist
->abort_count
;
1213 *cycles_count
+= clist
->cycles_count
;
1217 static int callchain_node_branch_counts_cumul(struct callchain_node
*node
,
1219 u64
*predicted_count
,
1223 struct callchain_node
*child
;
1226 n
= rb_first(&node
->rb_root_in
);
1228 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
1231 callchain_node_branch_counts_cumul(child
, branch_count
,
1236 callchain_counts_value(child
, branch_count
,
1237 predicted_count
, abort_count
,
1244 int callchain_branch_counts(struct callchain_root
*root
,
1245 u64
*branch_count
, u64
*predicted_count
,
1246 u64
*abort_count
, u64
*cycles_count
)
1251 if (predicted_count
)
1252 *predicted_count
= 0;
1260 return callchain_node_branch_counts_cumul(&root
->node
,
1267 static int count_pri64_printf(int idx
, const char *str
, u64 value
, char *bf
, int bfsize
)
1271 printed
= scnprintf(bf
, bfsize
, "%s%s:%" PRId64
"", (idx
) ? " " : " (", str
, value
);
1276 static int count_float_printf(int idx
, const char *str
, float value
,
1277 char *bf
, int bfsize
, float threshold
)
1281 if (threshold
!= 0.0 && value
< threshold
)
1284 printed
= scnprintf(bf
, bfsize
, "%s%s:%.1f%%", (idx
) ? " " : " (", str
, value
);
1289 static int branch_to_str(char *bf
, int bfsize
,
1290 u64 branch_count
, u64 predicted_count
,
1292 struct branch_type_stat
*brtype_stat
)
1296 printed
= branch_type_str(brtype_stat
, bf
, bfsize
);
1300 if (predicted_count
< branch_count
) {
1301 printed
+= count_float_printf(i
++, "predicted",
1302 predicted_count
* 100.0 / branch_count
,
1303 bf
+ printed
, bfsize
- printed
, 0.0);
1307 printed
+= count_float_printf(i
++, "abort",
1308 abort_count
* 100.0 / branch_count
,
1309 bf
+ printed
, bfsize
- printed
, 0.1);
1313 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1318 static int branch_from_str(char *bf
, int bfsize
,
1320 u64 cycles_count
, u64 iter_count
,
1323 int printed
= 0, i
= 0;
1326 cycles
= cycles_count
/ branch_count
;
1328 printed
+= count_pri64_printf(i
++, "cycles",
1330 bf
+ printed
, bfsize
- printed
);
1334 printed
+= count_pri64_printf(i
++, "iter",
1336 bf
+ printed
, bfsize
- printed
);
1338 printed
+= count_pri64_printf(i
++, "avg_cycles",
1339 iter_cycles
/ iter_count
,
1340 bf
+ printed
, bfsize
- printed
);
1344 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1349 static int counts_str_build(char *bf
, int bfsize
,
1350 u64 branch_count
, u64 predicted_count
,
1351 u64 abort_count
, u64 cycles_count
,
1352 u64 iter_count
, u64 iter_cycles
,
1353 struct branch_type_stat
*brtype_stat
)
1357 if (branch_count
== 0)
1358 return scnprintf(bf
, bfsize
, " (calltrace)");
1360 if (brtype_stat
->branch_to
) {
1361 printed
= branch_to_str(bf
, bfsize
, branch_count
,
1362 predicted_count
, abort_count
, brtype_stat
);
1364 printed
= branch_from_str(bf
, bfsize
, branch_count
,
1365 cycles_count
, iter_count
, iter_cycles
);
1374 static int callchain_counts_printf(FILE *fp
, char *bf
, int bfsize
,
1375 u64 branch_count
, u64 predicted_count
,
1376 u64 abort_count
, u64 cycles_count
,
1377 u64 iter_count
, u64 iter_cycles
,
1378 struct branch_type_stat
*brtype_stat
)
1382 counts_str_build(str
, sizeof(str
), branch_count
,
1383 predicted_count
, abort_count
, cycles_count
,
1384 iter_count
, iter_cycles
, brtype_stat
);
1387 return fprintf(fp
, "%s", str
);
1389 return scnprintf(bf
, bfsize
, "%s", str
);
1392 int callchain_list_counts__printf_value(struct callchain_list
*clist
,
1393 FILE *fp
, char *bf
, int bfsize
)
1395 u64 branch_count
, predicted_count
;
1396 u64 abort_count
, cycles_count
;
1397 u64 iter_count
, iter_cycles
;
1399 branch_count
= clist
->branch_count
;
1400 predicted_count
= clist
->predicted_count
;
1401 abort_count
= clist
->abort_count
;
1402 cycles_count
= clist
->cycles_count
;
1403 iter_count
= clist
->iter_count
;
1404 iter_cycles
= clist
->iter_cycles
;
1406 return callchain_counts_printf(fp
, bf
, bfsize
, branch_count
,
1407 predicted_count
, abort_count
,
1408 cycles_count
, iter_count
, iter_cycles
,
1409 &clist
->brtype_stat
);
1412 static void free_callchain_node(struct callchain_node
*node
)
1414 struct callchain_list
*list
, *tmp
;
1415 struct callchain_node
*child
;
1418 list_for_each_entry_safe(list
, tmp
, &node
->parent_val
, list
) {
1419 list_del(&list
->list
);
1420 map__zput(list
->ms
.map
);
1424 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
1425 list_del(&list
->list
);
1426 map__zput(list
->ms
.map
);
1430 n
= rb_first(&node
->rb_root_in
);
1432 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1434 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
1436 free_callchain_node(child
);
1441 void free_callchain(struct callchain_root
*root
)
1443 if (!symbol_conf
.use_callchain
)
1446 free_callchain_node(&root
->node
);
1449 static u64
decay_callchain_node(struct callchain_node
*node
)
1451 struct callchain_node
*child
;
1455 n
= rb_first(&node
->rb_root_in
);
1457 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1459 child_hits
+= decay_callchain_node(child
);
1463 node
->hit
= (node
->hit
* 7) / 8;
1464 node
->children_hit
= child_hits
;
1469 void decay_callchain(struct callchain_root
*root
)
1471 if (!symbol_conf
.use_callchain
)
1474 decay_callchain_node(&root
->node
);
1477 int callchain_node__make_parent_list(struct callchain_node
*node
)
1479 struct callchain_node
*parent
= node
->parent
;
1480 struct callchain_list
*chain
, *new;
1484 list_for_each_entry_reverse(chain
, &parent
->val
, list
) {
1485 new = malloc(sizeof(*new));
1489 new->has_children
= false;
1490 map__get(new->ms
.map
);
1491 list_add_tail(&new->list
, &head
);
1493 parent
= parent
->parent
;
1496 list_for_each_entry_safe_reverse(chain
, new, &head
, list
)
1497 list_move_tail(&chain
->list
, &node
->parent_val
);
1499 if (!list_empty(&node
->parent_val
)) {
1500 chain
= list_first_entry(&node
->parent_val
, struct callchain_list
, list
);
1501 chain
->has_children
= rb_prev(&node
->rb_node
) || rb_next(&node
->rb_node
);
1503 chain
= list_first_entry(&node
->val
, struct callchain_list
, list
);
1504 chain
->has_children
= false;
1509 list_for_each_entry_safe(chain
, new, &head
, list
) {
1510 list_del(&chain
->list
);
1511 map__zput(chain
->ms
.map
);
1517 int callchain_cursor__copy(struct callchain_cursor
*dst
,
1518 struct callchain_cursor
*src
)
1522 callchain_cursor_reset(dst
);
1523 callchain_cursor_commit(src
);
1526 struct callchain_cursor_node
*node
;
1528 node
= callchain_cursor_current(src
);
1532 rc
= callchain_cursor_append(dst
, node
->ip
, node
->map
, node
->sym
,
1533 node
->branch
, &node
->branch_flags
,
1540 callchain_cursor_advance(src
);