2 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree.
7 * Using a radix for code path provides a fast retrieval and factorizes
8 * memory use. Also that lets us use the paths in a hierarchical graph view.
24 #include "callchain.h"
26 __thread
struct callchain_cursor callchain_cursor
;
28 int parse_callchain_record_opt(const char *arg
, struct callchain_param
*param
)
30 return parse_callchain_record(arg
, param
);
33 static int parse_callchain_mode(const char *value
)
35 if (!strncmp(value
, "graph", strlen(value
))) {
36 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
39 if (!strncmp(value
, "flat", strlen(value
))) {
40 callchain_param
.mode
= CHAIN_FLAT
;
43 if (!strncmp(value
, "fractal", strlen(value
))) {
44 callchain_param
.mode
= CHAIN_GRAPH_REL
;
47 if (!strncmp(value
, "folded", strlen(value
))) {
48 callchain_param
.mode
= CHAIN_FOLDED
;
54 static int parse_callchain_order(const char *value
)
56 if (!strncmp(value
, "caller", strlen(value
))) {
57 callchain_param
.order
= ORDER_CALLER
;
58 callchain_param
.order_set
= true;
61 if (!strncmp(value
, "callee", strlen(value
))) {
62 callchain_param
.order
= ORDER_CALLEE
;
63 callchain_param
.order_set
= true;
69 static int parse_callchain_sort_key(const char *value
)
71 if (!strncmp(value
, "function", strlen(value
))) {
72 callchain_param
.key
= CCKEY_FUNCTION
;
75 if (!strncmp(value
, "address", strlen(value
))) {
76 callchain_param
.key
= CCKEY_ADDRESS
;
79 if (!strncmp(value
, "branch", strlen(value
))) {
80 callchain_param
.branch_callstack
= 1;
86 static int parse_callchain_value(const char *value
)
88 if (!strncmp(value
, "percent", strlen(value
))) {
89 callchain_param
.value
= CCVAL_PERCENT
;
92 if (!strncmp(value
, "period", strlen(value
))) {
93 callchain_param
.value
= CCVAL_PERIOD
;
96 if (!strncmp(value
, "count", strlen(value
))) {
97 callchain_param
.value
= CCVAL_COUNT
;
104 __parse_callchain_report_opt(const char *arg
, bool allow_record_opt
)
108 bool minpcnt_set
= false;
109 bool record_opt_set
= false;
110 bool try_stack_size
= false;
112 callchain_param
.enabled
= true;
113 symbol_conf
.use_callchain
= true;
118 while ((tok
= strtok((char *)arg
, ",")) != NULL
) {
119 if (!strncmp(tok
, "none", strlen(tok
))) {
120 callchain_param
.mode
= CHAIN_NONE
;
121 callchain_param
.enabled
= false;
122 symbol_conf
.use_callchain
= false;
126 if (!parse_callchain_mode(tok
) ||
127 !parse_callchain_order(tok
) ||
128 !parse_callchain_sort_key(tok
) ||
129 !parse_callchain_value(tok
)) {
130 /* parsing ok - move on to the next */
131 try_stack_size
= false;
133 } else if (allow_record_opt
&& !record_opt_set
) {
134 if (parse_callchain_record(tok
, &callchain_param
))
137 /* assume that number followed by 'dwarf' is stack size */
138 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
)
139 try_stack_size
= true;
141 record_opt_set
= true;
146 if (try_stack_size
) {
147 unsigned long size
= 0;
149 if (get_stack_size(tok
, &size
) < 0)
151 callchain_param
.dump_size
= size
;
152 try_stack_size
= false;
153 } else if (!minpcnt_set
) {
154 /* try to get the min percent */
155 callchain_param
.min_percent
= strtod(tok
, &endptr
);
160 /* try print limit at last */
161 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
169 if (callchain_register_param(&callchain_param
) < 0) {
170 pr_err("Can't register callchain params\n");
176 int parse_callchain_report_opt(const char *arg
)
178 return __parse_callchain_report_opt(arg
, false);
181 int parse_callchain_top_opt(const char *arg
)
183 return __parse_callchain_report_opt(arg
, true);
186 int perf_callchain_config(const char *var
, const char *value
)
190 if (prefixcmp(var
, "call-graph."))
192 var
+= sizeof("call-graph.") - 1;
194 if (!strcmp(var
, "record-mode"))
195 return parse_callchain_record_opt(value
, &callchain_param
);
196 #ifdef HAVE_DWARF_UNWIND_SUPPORT
197 if (!strcmp(var
, "dump-size")) {
198 unsigned long size
= 0;
201 ret
= get_stack_size(value
, &size
);
202 callchain_param
.dump_size
= size
;
207 if (!strcmp(var
, "print-type"))
208 return parse_callchain_mode(value
);
209 if (!strcmp(var
, "order"))
210 return parse_callchain_order(value
);
211 if (!strcmp(var
, "sort-key"))
212 return parse_callchain_sort_key(value
);
213 if (!strcmp(var
, "threshold")) {
214 callchain_param
.min_percent
= strtod(value
, &endptr
);
218 if (!strcmp(var
, "print-limit")) {
219 callchain_param
.print_limit
= strtod(value
, &endptr
);
228 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
229 enum chain_mode mode
)
231 struct rb_node
**p
= &root
->rb_node
;
232 struct rb_node
*parent
= NULL
;
233 struct callchain_node
*rnode
;
234 u64 chain_cumul
= callchain_cumul_hits(chain
);
240 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
241 rnode_cumul
= callchain_cumul_hits(rnode
);
246 if (rnode
->hit
< chain
->hit
)
251 case CHAIN_GRAPH_ABS
: /* Falldown */
252 case CHAIN_GRAPH_REL
:
253 if (rnode_cumul
< chain_cumul
)
264 rb_link_node(&chain
->rb_node
, parent
, p
);
265 rb_insert_color(&chain
->rb_node
, root
);
269 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
273 struct callchain_node
*child
;
275 n
= rb_first(&node
->rb_root_in
);
277 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
280 __sort_chain_flat(rb_root
, child
, min_hit
);
283 if (node
->hit
&& node
->hit
>= min_hit
)
284 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
288 * Once we get every callchains from the stream, we can now
292 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
293 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
296 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
299 static void __sort_chain_graph_abs(struct callchain_node
*node
,
303 struct callchain_node
*child
;
305 node
->rb_root
= RB_ROOT
;
306 n
= rb_first(&node
->rb_root_in
);
309 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
312 __sort_chain_graph_abs(child
, min_hit
);
313 if (callchain_cumul_hits(child
) >= min_hit
)
314 rb_insert_callchain(&node
->rb_root
, child
,
320 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
321 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
323 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
324 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
327 static void __sort_chain_graph_rel(struct callchain_node
*node
,
331 struct callchain_node
*child
;
334 node
->rb_root
= RB_ROOT
;
335 min_hit
= ceil(node
->children_hit
* min_percent
);
337 n
= rb_first(&node
->rb_root_in
);
339 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
342 __sort_chain_graph_rel(child
, min_percent
);
343 if (callchain_cumul_hits(child
) >= min_hit
)
344 rb_insert_callchain(&node
->rb_root
, child
,
350 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
351 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
353 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
354 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
357 int callchain_register_param(struct callchain_param
*param
)
359 switch (param
->mode
) {
360 case CHAIN_GRAPH_ABS
:
361 param
->sort
= sort_chain_graph_abs
;
363 case CHAIN_GRAPH_REL
:
364 param
->sort
= sort_chain_graph_rel
;
368 param
->sort
= sort_chain_flat
;
378 * Create a child for a parent. If inherit_children, then the new child
379 * will become the new parent of it's parent children
381 static struct callchain_node
*
382 create_child(struct callchain_node
*parent
, bool inherit_children
)
384 struct callchain_node
*new;
386 new = zalloc(sizeof(*new));
388 perror("not enough memory to create child for code path tree");
391 new->parent
= parent
;
392 INIT_LIST_HEAD(&new->val
);
393 INIT_LIST_HEAD(&new->parent_val
);
395 if (inherit_children
) {
397 struct callchain_node
*child
;
399 new->rb_root_in
= parent
->rb_root_in
;
400 parent
->rb_root_in
= RB_ROOT
;
402 n
= rb_first(&new->rb_root_in
);
404 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
409 /* make it the first child */
410 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
411 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
419 * Fill the node with callchain values
422 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
424 struct callchain_cursor_node
*cursor_node
;
426 node
->val_nr
= cursor
->nr
- cursor
->pos
;
428 pr_warning("Warning: empty node in callchain tree\n");
430 cursor_node
= callchain_cursor_current(cursor
);
432 while (cursor_node
) {
433 struct callchain_list
*call
;
435 call
= zalloc(sizeof(*call
));
437 perror("not enough memory for the code path tree");
440 call
->ip
= cursor_node
->ip
;
441 call
->ms
.sym
= cursor_node
->sym
;
442 call
->ms
.map
= cursor_node
->map
;
443 list_add_tail(&call
->list
, &node
->val
);
445 callchain_cursor_advance(cursor
);
446 cursor_node
= callchain_cursor_current(cursor
);
451 static struct callchain_node
*
452 add_child(struct callchain_node
*parent
,
453 struct callchain_cursor
*cursor
,
456 struct callchain_node
*new;
458 new = create_child(parent
, false);
462 if (fill_node(new, cursor
) < 0) {
463 struct callchain_list
*call
, *tmp
;
465 list_for_each_entry_safe(call
, tmp
, &new->val
, list
) {
466 list_del(&call
->list
);
473 new->children_hit
= 0;
475 new->children_count
= 0;
487 static enum match_result
match_chain(struct callchain_cursor_node
*node
,
488 struct callchain_list
*cnode
)
490 struct symbol
*sym
= node
->sym
;
493 if (cnode
->ms
.sym
&& sym
&&
494 callchain_param
.key
== CCKEY_FUNCTION
) {
495 left
= cnode
->ms
.sym
->start
;
505 return left
> right
? MATCH_GT
: MATCH_LT
;
509 * Split the parent in two parts (a new child is created) and
510 * give a part of its callchain to the created child.
511 * Then create another child to host the given callchain of new branch
514 split_add_child(struct callchain_node
*parent
,
515 struct callchain_cursor
*cursor
,
516 struct callchain_list
*to_split
,
517 u64 idx_parents
, u64 idx_local
, u64 period
)
519 struct callchain_node
*new;
520 struct list_head
*old_tail
;
521 unsigned int idx_total
= idx_parents
+ idx_local
;
524 new = create_child(parent
, true);
528 /* split the callchain and move a part to the new child */
529 old_tail
= parent
->val
.prev
;
530 list_del_range(&to_split
->list
, old_tail
);
531 new->val
.next
= &to_split
->list
;
532 new->val
.prev
= old_tail
;
533 to_split
->list
.prev
= &new->val
;
534 old_tail
->next
= &new->val
;
537 new->hit
= parent
->hit
;
538 new->children_hit
= parent
->children_hit
;
539 parent
->children_hit
= callchain_cumul_hits(new);
540 new->val_nr
= parent
->val_nr
- idx_local
;
541 parent
->val_nr
= idx_local
;
542 new->count
= parent
->count
;
543 new->children_count
= parent
->children_count
;
544 parent
->children_count
= callchain_cumul_counts(new);
546 /* create a new child for the new branch if any */
547 if (idx_total
< cursor
->nr
) {
548 struct callchain_node
*first
;
549 struct callchain_list
*cnode
;
550 struct callchain_cursor_node
*node
;
551 struct rb_node
*p
, **pp
;
554 parent
->children_hit
+= period
;
556 parent
->children_count
+= 1;
558 node
= callchain_cursor_current(cursor
);
559 new = add_child(parent
, cursor
, period
);
564 * This is second child since we moved parent's children
565 * to new (first) child above.
567 p
= parent
->rb_root_in
.rb_node
;
568 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
569 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
572 if (match_chain(node
, cnode
) == MATCH_LT
)
577 rb_link_node(&new->rb_node_in
, p
, pp
);
578 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
580 parent
->hit
= period
;
586 static enum match_result
587 append_chain(struct callchain_node
*root
,
588 struct callchain_cursor
*cursor
,
592 append_chain_children(struct callchain_node
*root
,
593 struct callchain_cursor
*cursor
,
596 struct callchain_node
*rnode
;
597 struct callchain_cursor_node
*node
;
598 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
599 struct rb_node
*parent
= NULL
;
601 node
= callchain_cursor_current(cursor
);
605 /* lookup in childrens */
607 enum match_result ret
;
610 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
612 /* If at least first entry matches, rely to children */
613 ret
= append_chain(rnode
, cursor
, period
);
615 goto inc_children_hit
;
616 if (ret
== MATCH_ERROR
)
620 p
= &parent
->rb_left
;
622 p
= &parent
->rb_right
;
624 /* nothing in children, add to the current node */
625 rnode
= add_child(root
, cursor
, period
);
629 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
630 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
633 root
->children_hit
+= period
;
634 root
->children_count
++;
638 static enum match_result
639 append_chain(struct callchain_node
*root
,
640 struct callchain_cursor
*cursor
,
643 struct callchain_list
*cnode
;
644 u64 start
= cursor
->pos
;
647 enum match_result cmp
= MATCH_ERROR
;
650 * Lookup in the current node
651 * If we have a symbol, then compare the start to match
652 * anywhere inside a function, unless function
655 list_for_each_entry(cnode
, &root
->val
, list
) {
656 struct callchain_cursor_node
*node
;
658 node
= callchain_cursor_current(cursor
);
662 cmp
= match_chain(node
, cnode
);
668 callchain_cursor_advance(cursor
);
671 /* matches not, relay no the parent */
673 WARN_ONCE(cmp
== MATCH_ERROR
, "Chain comparison error\n");
677 matches
= cursor
->pos
- start
;
679 /* we match only a part of the node. Split it and add the new chain */
680 if (matches
< root
->val_nr
) {
681 if (split_add_child(root
, cursor
, cnode
, start
, matches
,
688 /* we match 100% of the path, increment the hit */
689 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
695 /* We match the node and still have a part remaining */
696 if (append_chain_children(root
, cursor
, period
) < 0)
702 int callchain_append(struct callchain_root
*root
,
703 struct callchain_cursor
*cursor
,
709 callchain_cursor_commit(cursor
);
711 if (append_chain_children(&root
->node
, cursor
, period
) < 0)
714 if (cursor
->nr
> root
->max_depth
)
715 root
->max_depth
= cursor
->nr
;
721 merge_chain_branch(struct callchain_cursor
*cursor
,
722 struct callchain_node
*dst
, struct callchain_node
*src
)
724 struct callchain_cursor_node
**old_last
= cursor
->last
;
725 struct callchain_node
*child
;
726 struct callchain_list
*list
, *next_list
;
728 int old_pos
= cursor
->nr
;
731 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
732 callchain_cursor_append(cursor
, list
->ip
,
733 list
->ms
.map
, list
->ms
.sym
);
734 list_del(&list
->list
);
739 callchain_cursor_commit(cursor
);
740 if (append_chain_children(dst
, cursor
, src
->hit
) < 0)
744 n
= rb_first(&src
->rb_root_in
);
746 child
= container_of(n
, struct callchain_node
, rb_node_in
);
748 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
750 err
= merge_chain_branch(cursor
, dst
, child
);
757 cursor
->nr
= old_pos
;
758 cursor
->last
= old_last
;
763 int callchain_merge(struct callchain_cursor
*cursor
,
764 struct callchain_root
*dst
, struct callchain_root
*src
)
766 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
769 int callchain_cursor_append(struct callchain_cursor
*cursor
,
770 u64 ip
, struct map
*map
, struct symbol
*sym
)
772 struct callchain_cursor_node
*node
= *cursor
->last
;
775 node
= calloc(1, sizeof(*node
));
779 *cursor
->last
= node
;
788 cursor
->last
= &node
->next
;
793 int sample__resolve_callchain(struct perf_sample
*sample
,
794 struct callchain_cursor
*cursor
, struct symbol
**parent
,
795 struct perf_evsel
*evsel
, struct addr_location
*al
,
798 if (sample
->callchain
== NULL
)
801 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
802 perf_hpp_list
.parent
) {
803 return thread__resolve_callchain(al
->thread
, cursor
, evsel
, sample
,
804 parent
, al
, max_stack
);
809 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
811 if (!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
)
813 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
816 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
817 bool hide_unresolved
)
822 al
->addr
= node
->map
->map_ip(node
->map
, node
->ip
);
826 if (al
->sym
== NULL
) {
833 if (al
->map
->groups
== &al
->machine
->kmaps
) {
834 if (machine__is_host(al
->machine
)) {
835 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
838 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
842 if (machine__is_host(al
->machine
)) {
843 al
->cpumode
= PERF_RECORD_MISC_USER
;
845 } else if (perf_guest
) {
846 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
849 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
858 char *callchain_list__sym_name(struct callchain_list
*cl
,
859 char *bf
, size_t bfsize
, bool show_dso
)
864 if (callchain_param
.key
== CCKEY_ADDRESS
&&
865 cl
->ms
.map
&& !cl
->srcline
)
866 cl
->srcline
= get_srcline(cl
->ms
.map
->dso
,
867 map__rip_2objdump(cl
->ms
.map
,
871 printed
= scnprintf(bf
, bfsize
, "%s %s",
872 cl
->ms
.sym
->name
, cl
->srcline
);
874 printed
= scnprintf(bf
, bfsize
, "%s", cl
->ms
.sym
->name
);
876 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
879 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
881 cl
->ms
.map
->dso
->short_name
:
887 char *callchain_node__scnprintf_value(struct callchain_node
*node
,
888 char *bf
, size_t bfsize
, u64 total
)
890 double percent
= 0.0;
891 u64 period
= callchain_cumul_hits(node
);
892 unsigned count
= callchain_cumul_counts(node
);
894 if (callchain_param
.mode
== CHAIN_FOLDED
) {
899 switch (callchain_param
.value
) {
901 scnprintf(bf
, bfsize
, "%"PRIu64
, period
);
904 scnprintf(bf
, bfsize
, "%u", count
);
909 percent
= period
* 100.0 / total
;
910 scnprintf(bf
, bfsize
, "%.2f%%", percent
);
916 int callchain_node__fprintf_value(struct callchain_node
*node
,
919 double percent
= 0.0;
920 u64 period
= callchain_cumul_hits(node
);
921 unsigned count
= callchain_cumul_counts(node
);
923 if (callchain_param
.mode
== CHAIN_FOLDED
) {
928 switch (callchain_param
.value
) {
930 return fprintf(fp
, "%"PRIu64
, period
);
932 return fprintf(fp
, "%u", count
);
936 percent
= period
* 100.0 / total
;
937 return percent_color_fprintf(fp
, "%.2f%%", percent
);
942 static void free_callchain_node(struct callchain_node
*node
)
944 struct callchain_list
*list
, *tmp
;
945 struct callchain_node
*child
;
948 list_for_each_entry_safe(list
, tmp
, &node
->parent_val
, list
) {
949 list_del(&list
->list
);
953 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
954 list_del(&list
->list
);
958 n
= rb_first(&node
->rb_root_in
);
960 child
= container_of(n
, struct callchain_node
, rb_node_in
);
962 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
964 free_callchain_node(child
);
969 void free_callchain(struct callchain_root
*root
)
971 if (!symbol_conf
.use_callchain
)
974 free_callchain_node(&root
->node
);
977 static u64
decay_callchain_node(struct callchain_node
*node
)
979 struct callchain_node
*child
;
983 n
= rb_first(&node
->rb_root_in
);
985 child
= container_of(n
, struct callchain_node
, rb_node_in
);
987 child_hits
+= decay_callchain_node(child
);
991 node
->hit
= (node
->hit
* 7) / 8;
992 node
->children_hit
= child_hits
;
997 void decay_callchain(struct callchain_root
*root
)
999 if (!symbol_conf
.use_callchain
)
1002 decay_callchain_node(&root
->node
);
1005 int callchain_node__make_parent_list(struct callchain_node
*node
)
1007 struct callchain_node
*parent
= node
->parent
;
1008 struct callchain_list
*chain
, *new;
1012 list_for_each_entry_reverse(chain
, &parent
->val
, list
) {
1013 new = malloc(sizeof(*new));
1017 new->has_children
= false;
1018 list_add_tail(&new->list
, &head
);
1020 parent
= parent
->parent
;
1023 list_for_each_entry_safe_reverse(chain
, new, &head
, list
)
1024 list_move_tail(&chain
->list
, &node
->parent_val
);
1026 if (!list_empty(&node
->parent_val
)) {
1027 chain
= list_first_entry(&node
->parent_val
, struct callchain_list
, list
);
1028 chain
->has_children
= rb_prev(&node
->rb_node
) || rb_next(&node
->rb_node
);
1030 chain
= list_first_entry(&node
->val
, struct callchain_list
, list
);
1031 chain
->has_children
= false;
1036 list_for_each_entry_safe(chain
, new, &head
, list
) {
1037 list_del(&chain
->list
);