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.
27 #include "callchain.h"
31 #define CALLCHAIN_PARAM_DEFAULT \
32 .mode = CHAIN_GRAPH_ABS, \
34 .order = ORDER_CALLEE, \
35 .key = CCKEY_FUNCTION, \
36 .value = CCVAL_PERCENT, \
38 struct callchain_param callchain_param = {
39 CALLCHAIN_PARAM_DEFAULT
43 * Are there any events usind DWARF callchains?
47 * -e cycles/call-graph=dwarf/
49 bool dwarf_callchain_users
;
51 struct callchain_param callchain_param_default
= {
52 CALLCHAIN_PARAM_DEFAULT
55 __thread
struct callchain_cursor callchain_cursor
;
57 int parse_callchain_record_opt(const char *arg
, struct callchain_param
*param
)
59 return parse_callchain_record(arg
, param
);
62 static int parse_callchain_mode(const char *value
)
64 if (!strncmp(value
, "graph", strlen(value
))) {
65 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
68 if (!strncmp(value
, "flat", strlen(value
))) {
69 callchain_param
.mode
= CHAIN_FLAT
;
72 if (!strncmp(value
, "fractal", strlen(value
))) {
73 callchain_param
.mode
= CHAIN_GRAPH_REL
;
76 if (!strncmp(value
, "folded", strlen(value
))) {
77 callchain_param
.mode
= CHAIN_FOLDED
;
83 static int parse_callchain_order(const char *value
)
85 if (!strncmp(value
, "caller", strlen(value
))) {
86 callchain_param
.order
= ORDER_CALLER
;
87 callchain_param
.order_set
= true;
90 if (!strncmp(value
, "callee", strlen(value
))) {
91 callchain_param
.order
= ORDER_CALLEE
;
92 callchain_param
.order_set
= true;
98 static int parse_callchain_sort_key(const char *value
)
100 if (!strncmp(value
, "function", strlen(value
))) {
101 callchain_param
.key
= CCKEY_FUNCTION
;
104 if (!strncmp(value
, "address", strlen(value
))) {
105 callchain_param
.key
= CCKEY_ADDRESS
;
108 if (!strncmp(value
, "srcline", strlen(value
))) {
109 callchain_param
.key
= CCKEY_SRCLINE
;
112 if (!strncmp(value
, "branch", strlen(value
))) {
113 callchain_param
.branch_callstack
= 1;
119 static int parse_callchain_value(const char *value
)
121 if (!strncmp(value
, "percent", strlen(value
))) {
122 callchain_param
.value
= CCVAL_PERCENT
;
125 if (!strncmp(value
, "period", strlen(value
))) {
126 callchain_param
.value
= CCVAL_PERIOD
;
129 if (!strncmp(value
, "count", strlen(value
))) {
130 callchain_param
.value
= CCVAL_COUNT
;
136 static int get_stack_size(const char *str
, unsigned long *_size
)
140 unsigned long max_size
= round_down(USHRT_MAX
, sizeof(u64
));
142 size
= strtoul(str
, &endptr
, 0);
148 size
= round_up(size
, sizeof(u64
));
149 if (!size
|| size
> max_size
)
157 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
163 __parse_callchain_report_opt(const char *arg
, bool allow_record_opt
)
166 char *endptr
, *saveptr
= NULL
;
167 bool minpcnt_set
= false;
168 bool record_opt_set
= false;
169 bool try_stack_size
= false;
171 callchain_param
.enabled
= true;
172 symbol_conf
.use_callchain
= true;
177 while ((tok
= strtok_r((char *)arg
, ",", &saveptr
)) != NULL
) {
178 if (!strncmp(tok
, "none", strlen(tok
))) {
179 callchain_param
.mode
= CHAIN_NONE
;
180 callchain_param
.enabled
= false;
181 symbol_conf
.use_callchain
= false;
185 if (!parse_callchain_mode(tok
) ||
186 !parse_callchain_order(tok
) ||
187 !parse_callchain_sort_key(tok
) ||
188 !parse_callchain_value(tok
)) {
189 /* parsing ok - move on to the next */
190 try_stack_size
= false;
192 } else if (allow_record_opt
&& !record_opt_set
) {
193 if (parse_callchain_record(tok
, &callchain_param
))
196 /* assume that number followed by 'dwarf' is stack size */
197 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
)
198 try_stack_size
= true;
200 record_opt_set
= true;
205 if (try_stack_size
) {
206 unsigned long size
= 0;
208 if (get_stack_size(tok
, &size
) < 0)
210 callchain_param
.dump_size
= size
;
211 try_stack_size
= false;
212 } else if (!minpcnt_set
) {
213 /* try to get the min percent */
214 callchain_param
.min_percent
= strtod(tok
, &endptr
);
219 /* try print limit at last */
220 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
228 if (callchain_register_param(&callchain_param
) < 0) {
229 pr_err("Can't register callchain params\n");
235 int parse_callchain_report_opt(const char *arg
)
237 return __parse_callchain_report_opt(arg
, false);
240 int parse_callchain_top_opt(const char *arg
)
242 return __parse_callchain_report_opt(arg
, true);
245 int parse_callchain_record(const char *arg
, struct callchain_param
*param
)
247 char *tok
, *name
, *saveptr
= NULL
;
251 /* We need buffer that we know we can write to. */
252 buf
= malloc(strlen(arg
) + 1);
258 tok
= strtok_r((char *)buf
, ",", &saveptr
);
259 name
= tok
? : (char *)buf
;
262 /* Framepointer style */
263 if (!strncmp(name
, "fp", sizeof("fp"))) {
264 if (!strtok_r(NULL
, ",", &saveptr
)) {
265 param
->record_mode
= CALLCHAIN_FP
;
268 pr_err("callchain: No more arguments "
269 "needed for --call-graph fp\n");
273 } else if (!strncmp(name
, "dwarf", sizeof("dwarf"))) {
274 const unsigned long default_stack_dump_size
= 8192;
277 param
->record_mode
= CALLCHAIN_DWARF
;
278 param
->dump_size
= default_stack_dump_size
;
279 dwarf_callchain_users
= true;
281 tok
= strtok_r(NULL
, ",", &saveptr
);
283 unsigned long size
= 0;
285 ret
= get_stack_size(tok
, &size
);
286 param
->dump_size
= size
;
288 } else if (!strncmp(name
, "lbr", sizeof("lbr"))) {
289 if (!strtok_r(NULL
, ",", &saveptr
)) {
290 param
->record_mode
= CALLCHAIN_LBR
;
293 pr_err("callchain: No more arguments "
294 "needed for --call-graph lbr\n");
297 pr_err("callchain: Unknown --call-graph option "
308 int perf_callchain_config(const char *var
, const char *value
)
312 if (!strstarts(var
, "call-graph."))
314 var
+= sizeof("call-graph.") - 1;
316 if (!strcmp(var
, "record-mode"))
317 return parse_callchain_record_opt(value
, &callchain_param
);
318 if (!strcmp(var
, "dump-size")) {
319 unsigned long size
= 0;
322 ret
= get_stack_size(value
, &size
);
323 callchain_param
.dump_size
= size
;
327 if (!strcmp(var
, "print-type")){
329 ret
= parse_callchain_mode(value
);
331 pr_err("Invalid callchain mode: %s\n", value
);
334 if (!strcmp(var
, "order")){
336 ret
= parse_callchain_order(value
);
338 pr_err("Invalid callchain order: %s\n", value
);
341 if (!strcmp(var
, "sort-key")){
343 ret
= parse_callchain_sort_key(value
);
345 pr_err("Invalid callchain sort key: %s\n", value
);
348 if (!strcmp(var
, "threshold")) {
349 callchain_param
.min_percent
= strtod(value
, &endptr
);
350 if (value
== endptr
) {
351 pr_err("Invalid callchain threshold: %s\n", value
);
355 if (!strcmp(var
, "print-limit")) {
356 callchain_param
.print_limit
= strtod(value
, &endptr
);
357 if (value
== endptr
) {
358 pr_err("Invalid callchain print limit: %s\n", value
);
367 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
368 enum chain_mode mode
)
370 struct rb_node
**p
= &root
->rb_node
;
371 struct rb_node
*parent
= NULL
;
372 struct callchain_node
*rnode
;
373 u64 chain_cumul
= callchain_cumul_hits(chain
);
379 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
380 rnode_cumul
= callchain_cumul_hits(rnode
);
385 if (rnode
->hit
< chain
->hit
)
390 case CHAIN_GRAPH_ABS
: /* Falldown */
391 case CHAIN_GRAPH_REL
:
392 if (rnode_cumul
< chain_cumul
)
403 rb_link_node(&chain
->rb_node
, parent
, p
);
404 rb_insert_color(&chain
->rb_node
, root
);
408 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
412 struct callchain_node
*child
;
414 n
= rb_first(&node
->rb_root_in
);
416 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
419 __sort_chain_flat(rb_root
, child
, min_hit
);
422 if (node
->hit
&& node
->hit
>= min_hit
)
423 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
427 * Once we get every callchains from the stream, we can now
431 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
432 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
435 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
438 static void __sort_chain_graph_abs(struct callchain_node
*node
,
442 struct callchain_node
*child
;
444 node
->rb_root
= RB_ROOT
;
445 n
= rb_first(&node
->rb_root_in
);
448 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
451 __sort_chain_graph_abs(child
, min_hit
);
452 if (callchain_cumul_hits(child
) >= min_hit
)
453 rb_insert_callchain(&node
->rb_root
, child
,
459 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
460 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
462 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
463 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
466 static void __sort_chain_graph_rel(struct callchain_node
*node
,
470 struct callchain_node
*child
;
473 node
->rb_root
= RB_ROOT
;
474 min_hit
= ceil(node
->children_hit
* min_percent
);
476 n
= rb_first(&node
->rb_root_in
);
478 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
481 __sort_chain_graph_rel(child
, min_percent
);
482 if (callchain_cumul_hits(child
) >= min_hit
)
483 rb_insert_callchain(&node
->rb_root
, child
,
489 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
490 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
492 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
493 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
496 int callchain_register_param(struct callchain_param
*param
)
498 switch (param
->mode
) {
499 case CHAIN_GRAPH_ABS
:
500 param
->sort
= sort_chain_graph_abs
;
502 case CHAIN_GRAPH_REL
:
503 param
->sort
= sort_chain_graph_rel
;
507 param
->sort
= sort_chain_flat
;
517 * Create a child for a parent. If inherit_children, then the new child
518 * will become the new parent of it's parent children
520 static struct callchain_node
*
521 create_child(struct callchain_node
*parent
, bool inherit_children
)
523 struct callchain_node
*new;
525 new = zalloc(sizeof(*new));
527 perror("not enough memory to create child for code path tree");
530 new->parent
= parent
;
531 INIT_LIST_HEAD(&new->val
);
532 INIT_LIST_HEAD(&new->parent_val
);
534 if (inherit_children
) {
536 struct callchain_node
*child
;
538 new->rb_root_in
= parent
->rb_root_in
;
539 parent
->rb_root_in
= RB_ROOT
;
541 n
= rb_first(&new->rb_root_in
);
543 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
548 /* make it the first child */
549 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
550 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
558 * Fill the node with callchain values
561 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
563 struct callchain_cursor_node
*cursor_node
;
565 node
->val_nr
= cursor
->nr
- cursor
->pos
;
567 pr_warning("Warning: empty node in callchain tree\n");
569 cursor_node
= callchain_cursor_current(cursor
);
571 while (cursor_node
) {
572 struct callchain_list
*call
;
574 call
= zalloc(sizeof(*call
));
576 perror("not enough memory for the code path tree");
579 call
->ip
= cursor_node
->ip
;
580 call
->ms
.sym
= cursor_node
->sym
;
581 call
->ms
.map
= map__get(cursor_node
->map
);
582 call
->srcline
= cursor_node
->srcline
;
584 if (cursor_node
->branch
) {
585 call
->branch_count
= 1;
587 if (cursor_node
->branch_from
) {
589 * branch_from is set with value somewhere else
590 * to imply it's "to" of a branch.
592 call
->brtype_stat
.branch_to
= true;
594 if (cursor_node
->branch_flags
.predicted
)
595 call
->predicted_count
= 1;
597 if (cursor_node
->branch_flags
.abort
)
598 call
->abort_count
= 1;
600 branch_type_count(&call
->brtype_stat
,
601 &cursor_node
->branch_flags
,
602 cursor_node
->branch_from
,
606 * It's "from" of a branch
608 call
->brtype_stat
.branch_to
= false;
610 cursor_node
->branch_flags
.cycles
;
611 call
->iter_count
= cursor_node
->nr_loop_iter
;
612 call
->iter_cycles
= cursor_node
->iter_cycles
;
616 list_add_tail(&call
->list
, &node
->val
);
618 callchain_cursor_advance(cursor
);
619 cursor_node
= callchain_cursor_current(cursor
);
624 static struct callchain_node
*
625 add_child(struct callchain_node
*parent
,
626 struct callchain_cursor
*cursor
,
629 struct callchain_node
*new;
631 new = create_child(parent
, false);
635 if (fill_node(new, cursor
) < 0) {
636 struct callchain_list
*call
, *tmp
;
638 list_for_each_entry_safe(call
, tmp
, &new->val
, list
) {
639 list_del(&call
->list
);
640 map__zput(call
->ms
.map
);
647 new->children_hit
= 0;
649 new->children_count
= 0;
661 static enum match_result
match_chain_strings(const char *left
,
664 enum match_result ret
= MATCH_EQ
;
668 cmp
= strcmp(left
, right
);
669 else if (!left
&& right
)
671 else if (left
&& !right
)
677 ret
= cmp
< 0 ? MATCH_LT
: MATCH_GT
;
683 * We need to always use relative addresses because we're aggregating
684 * callchains from multiple threads, i.e. different address spaces, so
685 * comparing absolute addresses make no sense as a symbol in a DSO may end up
686 * in a different address when used in a different binary or even the same
687 * binary but with some sort of address randomization technique, thus we need
688 * to compare just relative addresses. -acme
690 static enum match_result
match_chain_dso_addresses(struct map
*left_map
, u64 left_ip
,
691 struct map
*right_map
, u64 right_ip
)
693 struct dso
*left_dso
= left_map
? left_map
->dso
: NULL
;
694 struct dso
*right_dso
= right_map
? right_map
->dso
: NULL
;
696 if (left_dso
!= right_dso
)
697 return left_dso
< right_dso
? MATCH_LT
: MATCH_GT
;
699 if (left_ip
!= right_ip
)
700 return left_ip
< right_ip
? MATCH_LT
: MATCH_GT
;
705 static enum match_result
match_chain(struct callchain_cursor_node
*node
,
706 struct callchain_list
*cnode
)
708 enum match_result match
= MATCH_ERROR
;
710 switch (callchain_param
.key
) {
712 match
= match_chain_strings(cnode
->srcline
, node
->srcline
);
713 if (match
!= MATCH_ERROR
)
715 /* otherwise fall-back to symbol-based comparison below */
718 if (node
->sym
&& cnode
->ms
.sym
) {
720 * Compare inlined frames based on their symbol name
721 * because different inlined frames will have the same
722 * symbol start. Otherwise do a faster comparison based
723 * on the symbol start address.
725 if (cnode
->ms
.sym
->inlined
|| node
->sym
->inlined
) {
726 match
= match_chain_strings(cnode
->ms
.sym
->name
,
728 if (match
!= MATCH_ERROR
)
731 match
= match_chain_dso_addresses(cnode
->ms
.map
, cnode
->ms
.sym
->start
,
732 node
->map
, node
->sym
->start
);
736 /* otherwise fall-back to IP-based comparison below */
740 match
= match_chain_dso_addresses(cnode
->ms
.map
, cnode
->ip
, node
->map
, node
->ip
);
744 if (match
== MATCH_EQ
&& node
->branch
) {
745 cnode
->branch_count
++;
747 if (node
->branch_from
) {
749 * It's "to" of a branch
751 cnode
->brtype_stat
.branch_to
= true;
753 if (node
->branch_flags
.predicted
)
754 cnode
->predicted_count
++;
756 if (node
->branch_flags
.abort
)
757 cnode
->abort_count
++;
759 branch_type_count(&cnode
->brtype_stat
,
765 * It's "from" of a branch
767 cnode
->brtype_stat
.branch_to
= false;
768 cnode
->cycles_count
+= node
->branch_flags
.cycles
;
769 cnode
->iter_count
+= node
->nr_loop_iter
;
770 cnode
->iter_cycles
+= node
->iter_cycles
;
779 * Split the parent in two parts (a new child is created) and
780 * give a part of its callchain to the created child.
781 * Then create another child to host the given callchain of new branch
784 split_add_child(struct callchain_node
*parent
,
785 struct callchain_cursor
*cursor
,
786 struct callchain_list
*to_split
,
787 u64 idx_parents
, u64 idx_local
, u64 period
)
789 struct callchain_node
*new;
790 struct list_head
*old_tail
;
791 unsigned int idx_total
= idx_parents
+ idx_local
;
794 new = create_child(parent
, true);
798 /* split the callchain and move a part to the new child */
799 old_tail
= parent
->val
.prev
;
800 list_del_range(&to_split
->list
, old_tail
);
801 new->val
.next
= &to_split
->list
;
802 new->val
.prev
= old_tail
;
803 to_split
->list
.prev
= &new->val
;
804 old_tail
->next
= &new->val
;
807 new->hit
= parent
->hit
;
808 new->children_hit
= parent
->children_hit
;
809 parent
->children_hit
= callchain_cumul_hits(new);
810 new->val_nr
= parent
->val_nr
- idx_local
;
811 parent
->val_nr
= idx_local
;
812 new->count
= parent
->count
;
813 new->children_count
= parent
->children_count
;
814 parent
->children_count
= callchain_cumul_counts(new);
816 /* create a new child for the new branch if any */
817 if (idx_total
< cursor
->nr
) {
818 struct callchain_node
*first
;
819 struct callchain_list
*cnode
;
820 struct callchain_cursor_node
*node
;
821 struct rb_node
*p
, **pp
;
824 parent
->children_hit
+= period
;
826 parent
->children_count
+= 1;
828 node
= callchain_cursor_current(cursor
);
829 new = add_child(parent
, cursor
, period
);
834 * This is second child since we moved parent's children
835 * to new (first) child above.
837 p
= parent
->rb_root_in
.rb_node
;
838 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
839 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
842 if (match_chain(node
, cnode
) == MATCH_LT
)
847 rb_link_node(&new->rb_node_in
, p
, pp
);
848 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
850 parent
->hit
= period
;
856 static enum match_result
857 append_chain(struct callchain_node
*root
,
858 struct callchain_cursor
*cursor
,
862 append_chain_children(struct callchain_node
*root
,
863 struct callchain_cursor
*cursor
,
866 struct callchain_node
*rnode
;
867 struct callchain_cursor_node
*node
;
868 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
869 struct rb_node
*parent
= NULL
;
871 node
= callchain_cursor_current(cursor
);
875 /* lookup in childrens */
877 enum match_result ret
;
880 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
882 /* If at least first entry matches, rely to children */
883 ret
= append_chain(rnode
, cursor
, period
);
885 goto inc_children_hit
;
886 if (ret
== MATCH_ERROR
)
890 p
= &parent
->rb_left
;
892 p
= &parent
->rb_right
;
894 /* nothing in children, add to the current node */
895 rnode
= add_child(root
, cursor
, period
);
899 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
900 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
903 root
->children_hit
+= period
;
904 root
->children_count
++;
908 static enum match_result
909 append_chain(struct callchain_node
*root
,
910 struct callchain_cursor
*cursor
,
913 struct callchain_list
*cnode
;
914 u64 start
= cursor
->pos
;
917 enum match_result cmp
= MATCH_ERROR
;
920 * Lookup in the current node
921 * If we have a symbol, then compare the start to match
922 * anywhere inside a function, unless function
925 list_for_each_entry(cnode
, &root
->val
, list
) {
926 struct callchain_cursor_node
*node
;
928 node
= callchain_cursor_current(cursor
);
932 cmp
= match_chain(node
, cnode
);
938 callchain_cursor_advance(cursor
);
941 /* matches not, relay no the parent */
943 WARN_ONCE(cmp
== MATCH_ERROR
, "Chain comparison error\n");
947 matches
= cursor
->pos
- start
;
949 /* we match only a part of the node. Split it and add the new chain */
950 if (matches
< root
->val_nr
) {
951 if (split_add_child(root
, cursor
, cnode
, start
, matches
,
958 /* we match 100% of the path, increment the hit */
959 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
965 /* We match the node and still have a part remaining */
966 if (append_chain_children(root
, cursor
, period
) < 0)
972 int callchain_append(struct callchain_root
*root
,
973 struct callchain_cursor
*cursor
,
979 callchain_cursor_commit(cursor
);
981 if (append_chain_children(&root
->node
, cursor
, period
) < 0)
984 if (cursor
->nr
> root
->max_depth
)
985 root
->max_depth
= cursor
->nr
;
991 merge_chain_branch(struct callchain_cursor
*cursor
,
992 struct callchain_node
*dst
, struct callchain_node
*src
)
994 struct callchain_cursor_node
**old_last
= cursor
->last
;
995 struct callchain_node
*child
;
996 struct callchain_list
*list
, *next_list
;
998 int old_pos
= cursor
->nr
;
1001 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
1002 callchain_cursor_append(cursor
, list
->ip
,
1003 list
->ms
.map
, list
->ms
.sym
,
1004 false, NULL
, 0, 0, 0, list
->srcline
);
1005 list_del(&list
->list
);
1006 map__zput(list
->ms
.map
);
1011 callchain_cursor_commit(cursor
);
1012 if (append_chain_children(dst
, cursor
, src
->hit
) < 0)
1016 n
= rb_first(&src
->rb_root_in
);
1018 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1020 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
1022 err
= merge_chain_branch(cursor
, dst
, child
);
1029 cursor
->nr
= old_pos
;
1030 cursor
->last
= old_last
;
1035 int callchain_merge(struct callchain_cursor
*cursor
,
1036 struct callchain_root
*dst
, struct callchain_root
*src
)
1038 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
1041 int callchain_cursor_append(struct callchain_cursor
*cursor
,
1042 u64 ip
, struct map
*map
, struct symbol
*sym
,
1043 bool branch
, struct branch_flags
*flags
,
1044 int nr_loop_iter
, u64 iter_cycles
, u64 branch_from
,
1045 const char *srcline
)
1047 struct callchain_cursor_node
*node
= *cursor
->last
;
1050 node
= calloc(1, sizeof(*node
));
1054 *cursor
->last
= node
;
1058 map__zput(node
->map
);
1059 node
->map
= map__get(map
);
1061 node
->branch
= branch
;
1062 node
->nr_loop_iter
= nr_loop_iter
;
1063 node
->iter_cycles
= iter_cycles
;
1064 node
->srcline
= srcline
;
1067 memcpy(&node
->branch_flags
, flags
,
1068 sizeof(struct branch_flags
));
1070 node
->branch_from
= branch_from
;
1073 cursor
->last
= &node
->next
;
1078 int sample__resolve_callchain(struct perf_sample
*sample
,
1079 struct callchain_cursor
*cursor
, struct symbol
**parent
,
1080 struct perf_evsel
*evsel
, struct addr_location
*al
,
1083 if (sample
->callchain
== NULL
&& !symbol_conf
.show_branchflag_count
)
1086 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
1087 perf_hpp_list
.parent
|| symbol_conf
.show_branchflag_count
) {
1088 return thread__resolve_callchain(al
->thread
, cursor
, evsel
, sample
,
1089 parent
, al
, max_stack
);
1094 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
1096 if ((!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
) &&
1097 !symbol_conf
.show_branchflag_count
)
1099 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
1102 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
1103 bool hide_unresolved
)
1105 al
->map
= node
->map
;
1106 al
->sym
= node
->sym
;
1107 al
->srcline
= node
->srcline
;
1108 al
->addr
= node
->ip
;
1110 if (al
->sym
== NULL
) {
1111 if (hide_unresolved
)
1113 if (al
->map
== NULL
)
1117 if (al
->map
->groups
== &al
->machine
->kmaps
) {
1118 if (machine__is_host(al
->machine
)) {
1119 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
1122 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
1126 if (machine__is_host(al
->machine
)) {
1127 al
->cpumode
= PERF_RECORD_MISC_USER
;
1129 } else if (perf_guest
) {
1130 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
1133 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
1142 char *callchain_list__sym_name(struct callchain_list
*cl
,
1143 char *bf
, size_t bfsize
, bool show_dso
)
1145 bool show_addr
= callchain_param
.key
== CCKEY_ADDRESS
;
1146 bool show_srcline
= show_addr
|| callchain_param
.key
== CCKEY_SRCLINE
;
1150 const char *inlined
= cl
->ms
.sym
->inlined
? " (inlined)" : "";
1152 if (show_srcline
&& cl
->srcline
)
1153 printed
= scnprintf(bf
, bfsize
, "%s %s%s",
1154 cl
->ms
.sym
->name
, cl
->srcline
,
1157 printed
= scnprintf(bf
, bfsize
, "%s%s",
1158 cl
->ms
.sym
->name
, inlined
);
1160 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
1163 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
1165 cl
->ms
.map
->dso
->short_name
:
1171 char *callchain_node__scnprintf_value(struct callchain_node
*node
,
1172 char *bf
, size_t bfsize
, u64 total
)
1174 double percent
= 0.0;
1175 u64 period
= callchain_cumul_hits(node
);
1176 unsigned count
= callchain_cumul_counts(node
);
1178 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1180 count
= node
->count
;
1183 switch (callchain_param
.value
) {
1185 scnprintf(bf
, bfsize
, "%"PRIu64
, period
);
1188 scnprintf(bf
, bfsize
, "%u", count
);
1193 percent
= period
* 100.0 / total
;
1194 scnprintf(bf
, bfsize
, "%.2f%%", percent
);
1200 int callchain_node__fprintf_value(struct callchain_node
*node
,
1201 FILE *fp
, u64 total
)
1203 double percent
= 0.0;
1204 u64 period
= callchain_cumul_hits(node
);
1205 unsigned count
= callchain_cumul_counts(node
);
1207 if (callchain_param
.mode
== CHAIN_FOLDED
) {
1209 count
= node
->count
;
1212 switch (callchain_param
.value
) {
1214 return fprintf(fp
, "%"PRIu64
, period
);
1216 return fprintf(fp
, "%u", count
);
1220 percent
= period
* 100.0 / total
;
1221 return percent_color_fprintf(fp
, "%.2f%%", percent
);
1226 static void callchain_counts_value(struct callchain_node
*node
,
1227 u64
*branch_count
, u64
*predicted_count
,
1228 u64
*abort_count
, u64
*cycles_count
)
1230 struct callchain_list
*clist
;
1232 list_for_each_entry(clist
, &node
->val
, list
) {
1234 *branch_count
+= clist
->branch_count
;
1236 if (predicted_count
)
1237 *predicted_count
+= clist
->predicted_count
;
1240 *abort_count
+= clist
->abort_count
;
1243 *cycles_count
+= clist
->cycles_count
;
1247 static int callchain_node_branch_counts_cumul(struct callchain_node
*node
,
1249 u64
*predicted_count
,
1253 struct callchain_node
*child
;
1256 n
= rb_first(&node
->rb_root_in
);
1258 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
1261 callchain_node_branch_counts_cumul(child
, branch_count
,
1266 callchain_counts_value(child
, branch_count
,
1267 predicted_count
, abort_count
,
1274 int callchain_branch_counts(struct callchain_root
*root
,
1275 u64
*branch_count
, u64
*predicted_count
,
1276 u64
*abort_count
, u64
*cycles_count
)
1281 if (predicted_count
)
1282 *predicted_count
= 0;
1290 return callchain_node_branch_counts_cumul(&root
->node
,
1297 static int count_pri64_printf(int idx
, const char *str
, u64 value
, char *bf
, int bfsize
)
1301 printed
= scnprintf(bf
, bfsize
, "%s%s:%" PRId64
"", (idx
) ? " " : " (", str
, value
);
1306 static int count_float_printf(int idx
, const char *str
, float value
,
1307 char *bf
, int bfsize
, float threshold
)
1311 if (threshold
!= 0.0 && value
< threshold
)
1314 printed
= scnprintf(bf
, bfsize
, "%s%s:%.1f%%", (idx
) ? " " : " (", str
, value
);
1319 static int branch_to_str(char *bf
, int bfsize
,
1320 u64 branch_count
, u64 predicted_count
,
1322 struct branch_type_stat
*brtype_stat
)
1326 printed
= branch_type_str(brtype_stat
, bf
, bfsize
);
1330 if (predicted_count
< branch_count
) {
1331 printed
+= count_float_printf(i
++, "predicted",
1332 predicted_count
* 100.0 / branch_count
,
1333 bf
+ printed
, bfsize
- printed
, 0.0);
1337 printed
+= count_float_printf(i
++, "abort",
1338 abort_count
* 100.0 / branch_count
,
1339 bf
+ printed
, bfsize
- printed
, 0.1);
1343 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1348 static int branch_from_str(char *bf
, int bfsize
,
1350 u64 cycles_count
, u64 iter_count
,
1351 u64 iter_cycles
, u64 from_count
)
1353 int printed
= 0, i
= 0;
1356 cycles
= cycles_count
/ branch_count
;
1358 printed
+= count_pri64_printf(i
++, "cycles",
1360 bf
+ printed
, bfsize
- printed
);
1363 if (iter_count
&& from_count
) {
1364 v
= iter_count
/ from_count
;
1366 printed
+= count_pri64_printf(i
++, "iter",
1367 v
, bf
+ printed
, bfsize
- printed
);
1369 printed
+= count_pri64_printf(i
++, "avg_cycles",
1370 iter_cycles
/ iter_count
,
1371 bf
+ printed
, bfsize
- printed
);
1376 printed
+= scnprintf(bf
+ printed
, bfsize
- printed
, ")");
1381 static int counts_str_build(char *bf
, int bfsize
,
1382 u64 branch_count
, u64 predicted_count
,
1383 u64 abort_count
, u64 cycles_count
,
1384 u64 iter_count
, u64 iter_cycles
,
1386 struct branch_type_stat
*brtype_stat
)
1390 if (branch_count
== 0)
1391 return scnprintf(bf
, bfsize
, " (calltrace)");
1393 if (brtype_stat
->branch_to
) {
1394 printed
= branch_to_str(bf
, bfsize
, branch_count
,
1395 predicted_count
, abort_count
, brtype_stat
);
1397 printed
= branch_from_str(bf
, bfsize
, branch_count
,
1398 cycles_count
, iter_count
, iter_cycles
,
1408 static int callchain_counts_printf(FILE *fp
, char *bf
, int bfsize
,
1409 u64 branch_count
, u64 predicted_count
,
1410 u64 abort_count
, u64 cycles_count
,
1411 u64 iter_count
, u64 iter_cycles
,
1413 struct branch_type_stat
*brtype_stat
)
1417 counts_str_build(str
, sizeof(str
), branch_count
,
1418 predicted_count
, abort_count
, cycles_count
,
1419 iter_count
, iter_cycles
, from_count
, brtype_stat
);
1422 return fprintf(fp
, "%s", str
);
1424 return scnprintf(bf
, bfsize
, "%s", str
);
1427 int callchain_list_counts__printf_value(struct callchain_list
*clist
,
1428 FILE *fp
, char *bf
, int bfsize
)
1430 u64 branch_count
, predicted_count
;
1431 u64 abort_count
, cycles_count
;
1432 u64 iter_count
, iter_cycles
;
1435 branch_count
= clist
->branch_count
;
1436 predicted_count
= clist
->predicted_count
;
1437 abort_count
= clist
->abort_count
;
1438 cycles_count
= clist
->cycles_count
;
1439 iter_count
= clist
->iter_count
;
1440 iter_cycles
= clist
->iter_cycles
;
1441 from_count
= clist
->from_count
;
1443 return callchain_counts_printf(fp
, bf
, bfsize
, branch_count
,
1444 predicted_count
, abort_count
,
1445 cycles_count
, iter_count
, iter_cycles
,
1446 from_count
, &clist
->brtype_stat
);
1449 static void free_callchain_node(struct callchain_node
*node
)
1451 struct callchain_list
*list
, *tmp
;
1452 struct callchain_node
*child
;
1455 list_for_each_entry_safe(list
, tmp
, &node
->parent_val
, list
) {
1456 list_del(&list
->list
);
1457 map__zput(list
->ms
.map
);
1461 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
1462 list_del(&list
->list
);
1463 map__zput(list
->ms
.map
);
1467 n
= rb_first(&node
->rb_root_in
);
1469 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1471 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
1473 free_callchain_node(child
);
1478 void free_callchain(struct callchain_root
*root
)
1480 if (!symbol_conf
.use_callchain
)
1483 free_callchain_node(&root
->node
);
1486 static u64
decay_callchain_node(struct callchain_node
*node
)
1488 struct callchain_node
*child
;
1492 n
= rb_first(&node
->rb_root_in
);
1494 child
= container_of(n
, struct callchain_node
, rb_node_in
);
1496 child_hits
+= decay_callchain_node(child
);
1500 node
->hit
= (node
->hit
* 7) / 8;
1501 node
->children_hit
= child_hits
;
1506 void decay_callchain(struct callchain_root
*root
)
1508 if (!symbol_conf
.use_callchain
)
1511 decay_callchain_node(&root
->node
);
1514 int callchain_node__make_parent_list(struct callchain_node
*node
)
1516 struct callchain_node
*parent
= node
->parent
;
1517 struct callchain_list
*chain
, *new;
1521 list_for_each_entry_reverse(chain
, &parent
->val
, list
) {
1522 new = malloc(sizeof(*new));
1526 new->has_children
= false;
1527 map__get(new->ms
.map
);
1528 list_add_tail(&new->list
, &head
);
1530 parent
= parent
->parent
;
1533 list_for_each_entry_safe_reverse(chain
, new, &head
, list
)
1534 list_move_tail(&chain
->list
, &node
->parent_val
);
1536 if (!list_empty(&node
->parent_val
)) {
1537 chain
= list_first_entry(&node
->parent_val
, struct callchain_list
, list
);
1538 chain
->has_children
= rb_prev(&node
->rb_node
) || rb_next(&node
->rb_node
);
1540 chain
= list_first_entry(&node
->val
, struct callchain_list
, list
);
1541 chain
->has_children
= false;
1546 list_for_each_entry_safe(chain
, new, &head
, list
) {
1547 list_del(&chain
->list
);
1548 map__zput(chain
->ms
.map
);
1554 int callchain_cursor__copy(struct callchain_cursor
*dst
,
1555 struct callchain_cursor
*src
)
1559 callchain_cursor_reset(dst
);
1560 callchain_cursor_commit(src
);
1563 struct callchain_cursor_node
*node
;
1565 node
= callchain_cursor_current(src
);
1569 rc
= callchain_cursor_append(dst
, node
->ip
, node
->map
, node
->sym
,
1570 node
->branch
, &node
->branch_flags
,
1573 node
->branch_from
, node
->srcline
);
1577 callchain_cursor_advance(src
);
1584 * Initialize a cursor before adding entries inside, but keep
1585 * the previously allocated entries as a cache.
1587 void callchain_cursor_reset(struct callchain_cursor
*cursor
)
1589 struct callchain_cursor_node
*node
;
1592 cursor
->last
= &cursor
->first
;
1594 for (node
= cursor
->first
; node
!= NULL
; node
= node
->next
)
1595 map__zput(node
->map
);