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 symbol_conf
.use_callchain
= true;
117 while ((tok
= strtok((char *)arg
, ",")) != NULL
) {
118 if (!strncmp(tok
, "none", strlen(tok
))) {
119 callchain_param
.mode
= CHAIN_NONE
;
120 symbol_conf
.use_callchain
= false;
124 if (!parse_callchain_mode(tok
) ||
125 !parse_callchain_order(tok
) ||
126 !parse_callchain_sort_key(tok
) ||
127 !parse_callchain_value(tok
)) {
128 /* parsing ok - move on to the next */
129 try_stack_size
= false;
131 } else if (allow_record_opt
&& !record_opt_set
) {
132 if (parse_callchain_record(tok
, &callchain_param
))
135 /* assume that number followed by 'dwarf' is stack size */
136 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
)
137 try_stack_size
= true;
139 record_opt_set
= true;
144 if (try_stack_size
) {
145 unsigned long size
= 0;
147 if (get_stack_size(tok
, &size
) < 0)
149 callchain_param
.dump_size
= size
;
150 try_stack_size
= false;
151 } else if (!minpcnt_set
) {
152 /* try to get the min percent */
153 callchain_param
.min_percent
= strtod(tok
, &endptr
);
158 /* try print limit at last */
159 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
167 if (callchain_register_param(&callchain_param
) < 0) {
168 pr_err("Can't register callchain params\n");
174 int parse_callchain_report_opt(const char *arg
)
176 return __parse_callchain_report_opt(arg
, false);
179 int parse_callchain_top_opt(const char *arg
)
181 return __parse_callchain_report_opt(arg
, true);
184 int perf_callchain_config(const char *var
, const char *value
)
188 if (prefixcmp(var
, "call-graph."))
190 var
+= sizeof("call-graph.") - 1;
192 if (!strcmp(var
, "record-mode"))
193 return parse_callchain_record_opt(value
, &callchain_param
);
194 #ifdef HAVE_DWARF_UNWIND_SUPPORT
195 if (!strcmp(var
, "dump-size")) {
196 unsigned long size
= 0;
199 ret
= get_stack_size(value
, &size
);
200 callchain_param
.dump_size
= size
;
205 if (!strcmp(var
, "print-type"))
206 return parse_callchain_mode(value
);
207 if (!strcmp(var
, "order"))
208 return parse_callchain_order(value
);
209 if (!strcmp(var
, "sort-key"))
210 return parse_callchain_sort_key(value
);
211 if (!strcmp(var
, "threshold")) {
212 callchain_param
.min_percent
= strtod(value
, &endptr
);
216 if (!strcmp(var
, "print-limit")) {
217 callchain_param
.print_limit
= strtod(value
, &endptr
);
226 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
227 enum chain_mode mode
)
229 struct rb_node
**p
= &root
->rb_node
;
230 struct rb_node
*parent
= NULL
;
231 struct callchain_node
*rnode
;
232 u64 chain_cumul
= callchain_cumul_hits(chain
);
238 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
239 rnode_cumul
= callchain_cumul_hits(rnode
);
244 if (rnode
->hit
< chain
->hit
)
249 case CHAIN_GRAPH_ABS
: /* Falldown */
250 case CHAIN_GRAPH_REL
:
251 if (rnode_cumul
< chain_cumul
)
262 rb_link_node(&chain
->rb_node
, parent
, p
);
263 rb_insert_color(&chain
->rb_node
, root
);
267 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
271 struct callchain_node
*child
;
273 n
= rb_first(&node
->rb_root_in
);
275 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
278 __sort_chain_flat(rb_root
, child
, min_hit
);
281 if (node
->hit
&& node
->hit
>= min_hit
)
282 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
286 * Once we get every callchains from the stream, we can now
290 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
291 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
294 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
297 static void __sort_chain_graph_abs(struct callchain_node
*node
,
301 struct callchain_node
*child
;
303 node
->rb_root
= RB_ROOT
;
304 n
= rb_first(&node
->rb_root_in
);
307 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
310 __sort_chain_graph_abs(child
, min_hit
);
311 if (callchain_cumul_hits(child
) >= min_hit
)
312 rb_insert_callchain(&node
->rb_root
, child
,
318 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
319 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
321 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
322 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
325 static void __sort_chain_graph_rel(struct callchain_node
*node
,
329 struct callchain_node
*child
;
332 node
->rb_root
= RB_ROOT
;
333 min_hit
= ceil(node
->children_hit
* min_percent
);
335 n
= rb_first(&node
->rb_root_in
);
337 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
340 __sort_chain_graph_rel(child
, min_percent
);
341 if (callchain_cumul_hits(child
) >= min_hit
)
342 rb_insert_callchain(&node
->rb_root
, child
,
348 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
349 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
351 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
352 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
355 int callchain_register_param(struct callchain_param
*param
)
357 switch (param
->mode
) {
358 case CHAIN_GRAPH_ABS
:
359 param
->sort
= sort_chain_graph_abs
;
361 case CHAIN_GRAPH_REL
:
362 param
->sort
= sort_chain_graph_rel
;
366 param
->sort
= sort_chain_flat
;
376 * Create a child for a parent. If inherit_children, then the new child
377 * will become the new parent of it's parent children
379 static struct callchain_node
*
380 create_child(struct callchain_node
*parent
, bool inherit_children
)
382 struct callchain_node
*new;
384 new = zalloc(sizeof(*new));
386 perror("not enough memory to create child for code path tree");
389 new->parent
= parent
;
390 INIT_LIST_HEAD(&new->val
);
391 INIT_LIST_HEAD(&new->parent_val
);
393 if (inherit_children
) {
395 struct callchain_node
*child
;
397 new->rb_root_in
= parent
->rb_root_in
;
398 parent
->rb_root_in
= RB_ROOT
;
400 n
= rb_first(&new->rb_root_in
);
402 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
407 /* make it the first child */
408 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
409 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
417 * Fill the node with callchain values
420 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
422 struct callchain_cursor_node
*cursor_node
;
424 node
->val_nr
= cursor
->nr
- cursor
->pos
;
426 pr_warning("Warning: empty node in callchain tree\n");
428 cursor_node
= callchain_cursor_current(cursor
);
430 while (cursor_node
) {
431 struct callchain_list
*call
;
433 call
= zalloc(sizeof(*call
));
435 perror("not enough memory for the code path tree");
438 call
->ip
= cursor_node
->ip
;
439 call
->ms
.sym
= cursor_node
->sym
;
440 call
->ms
.map
= cursor_node
->map
;
441 list_add_tail(&call
->list
, &node
->val
);
443 callchain_cursor_advance(cursor
);
444 cursor_node
= callchain_cursor_current(cursor
);
449 static struct callchain_node
*
450 add_child(struct callchain_node
*parent
,
451 struct callchain_cursor
*cursor
,
454 struct callchain_node
*new;
456 new = create_child(parent
, false);
460 if (fill_node(new, cursor
) < 0) {
461 struct callchain_list
*call
, *tmp
;
463 list_for_each_entry_safe(call
, tmp
, &new->val
, list
) {
464 list_del(&call
->list
);
471 new->children_hit
= 0;
473 new->children_count
= 0;
485 static enum match_result
match_chain(struct callchain_cursor_node
*node
,
486 struct callchain_list
*cnode
)
488 struct symbol
*sym
= node
->sym
;
491 if (cnode
->ms
.sym
&& sym
&&
492 callchain_param
.key
== CCKEY_FUNCTION
) {
493 left
= cnode
->ms
.sym
->start
;
503 return left
> right
? MATCH_GT
: MATCH_LT
;
507 * Split the parent in two parts (a new child is created) and
508 * give a part of its callchain to the created child.
509 * Then create another child to host the given callchain of new branch
512 split_add_child(struct callchain_node
*parent
,
513 struct callchain_cursor
*cursor
,
514 struct callchain_list
*to_split
,
515 u64 idx_parents
, u64 idx_local
, u64 period
)
517 struct callchain_node
*new;
518 struct list_head
*old_tail
;
519 unsigned int idx_total
= idx_parents
+ idx_local
;
522 new = create_child(parent
, true);
526 /* split the callchain and move a part to the new child */
527 old_tail
= parent
->val
.prev
;
528 list_del_range(&to_split
->list
, old_tail
);
529 new->val
.next
= &to_split
->list
;
530 new->val
.prev
= old_tail
;
531 to_split
->list
.prev
= &new->val
;
532 old_tail
->next
= &new->val
;
535 new->hit
= parent
->hit
;
536 new->children_hit
= parent
->children_hit
;
537 parent
->children_hit
= callchain_cumul_hits(new);
538 new->val_nr
= parent
->val_nr
- idx_local
;
539 parent
->val_nr
= idx_local
;
540 new->count
= parent
->count
;
541 new->children_count
= parent
->children_count
;
542 parent
->children_count
= callchain_cumul_counts(new);
544 /* create a new child for the new branch if any */
545 if (idx_total
< cursor
->nr
) {
546 struct callchain_node
*first
;
547 struct callchain_list
*cnode
;
548 struct callchain_cursor_node
*node
;
549 struct rb_node
*p
, **pp
;
552 parent
->children_hit
+= period
;
554 parent
->children_count
+= 1;
556 node
= callchain_cursor_current(cursor
);
557 new = add_child(parent
, cursor
, period
);
562 * This is second child since we moved parent's children
563 * to new (first) child above.
565 p
= parent
->rb_root_in
.rb_node
;
566 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
567 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
570 if (match_chain(node
, cnode
) == MATCH_LT
)
575 rb_link_node(&new->rb_node_in
, p
, pp
);
576 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
578 parent
->hit
= period
;
584 static enum match_result
585 append_chain(struct callchain_node
*root
,
586 struct callchain_cursor
*cursor
,
590 append_chain_children(struct callchain_node
*root
,
591 struct callchain_cursor
*cursor
,
594 struct callchain_node
*rnode
;
595 struct callchain_cursor_node
*node
;
596 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
597 struct rb_node
*parent
= NULL
;
599 node
= callchain_cursor_current(cursor
);
603 /* lookup in childrens */
605 enum match_result ret
;
608 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
610 /* If at least first entry matches, rely to children */
611 ret
= append_chain(rnode
, cursor
, period
);
613 goto inc_children_hit
;
614 if (ret
== MATCH_ERROR
)
618 p
= &parent
->rb_left
;
620 p
= &parent
->rb_right
;
622 /* nothing in children, add to the current node */
623 rnode
= add_child(root
, cursor
, period
);
627 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
628 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
631 root
->children_hit
+= period
;
632 root
->children_count
++;
636 static enum match_result
637 append_chain(struct callchain_node
*root
,
638 struct callchain_cursor
*cursor
,
641 struct callchain_list
*cnode
;
642 u64 start
= cursor
->pos
;
645 enum match_result cmp
= MATCH_ERROR
;
648 * Lookup in the current node
649 * If we have a symbol, then compare the start to match
650 * anywhere inside a function, unless function
653 list_for_each_entry(cnode
, &root
->val
, list
) {
654 struct callchain_cursor_node
*node
;
656 node
= callchain_cursor_current(cursor
);
660 cmp
= match_chain(node
, cnode
);
666 callchain_cursor_advance(cursor
);
669 /* matches not, relay no the parent */
671 WARN_ONCE(cmp
== MATCH_ERROR
, "Chain comparison error\n");
675 matches
= cursor
->pos
- start
;
677 /* we match only a part of the node. Split it and add the new chain */
678 if (matches
< root
->val_nr
) {
679 if (split_add_child(root
, cursor
, cnode
, start
, matches
,
686 /* we match 100% of the path, increment the hit */
687 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
693 /* We match the node and still have a part remaining */
694 if (append_chain_children(root
, cursor
, period
) < 0)
700 int callchain_append(struct callchain_root
*root
,
701 struct callchain_cursor
*cursor
,
707 callchain_cursor_commit(cursor
);
709 if (append_chain_children(&root
->node
, cursor
, period
) < 0)
712 if (cursor
->nr
> root
->max_depth
)
713 root
->max_depth
= cursor
->nr
;
719 merge_chain_branch(struct callchain_cursor
*cursor
,
720 struct callchain_node
*dst
, struct callchain_node
*src
)
722 struct callchain_cursor_node
**old_last
= cursor
->last
;
723 struct callchain_node
*child
;
724 struct callchain_list
*list
, *next_list
;
726 int old_pos
= cursor
->nr
;
729 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
730 callchain_cursor_append(cursor
, list
->ip
,
731 list
->ms
.map
, list
->ms
.sym
);
732 list_del(&list
->list
);
737 callchain_cursor_commit(cursor
);
738 if (append_chain_children(dst
, cursor
, src
->hit
) < 0)
742 n
= rb_first(&src
->rb_root_in
);
744 child
= container_of(n
, struct callchain_node
, rb_node_in
);
746 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
748 err
= merge_chain_branch(cursor
, dst
, child
);
755 cursor
->nr
= old_pos
;
756 cursor
->last
= old_last
;
761 int callchain_merge(struct callchain_cursor
*cursor
,
762 struct callchain_root
*dst
, struct callchain_root
*src
)
764 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
767 int callchain_cursor_append(struct callchain_cursor
*cursor
,
768 u64 ip
, struct map
*map
, struct symbol
*sym
)
770 struct callchain_cursor_node
*node
= *cursor
->last
;
773 node
= calloc(1, sizeof(*node
));
777 *cursor
->last
= node
;
786 cursor
->last
= &node
->next
;
791 int sample__resolve_callchain(struct perf_sample
*sample
, struct symbol
**parent
,
792 struct perf_evsel
*evsel
, struct addr_location
*al
,
795 if (sample
->callchain
== NULL
)
798 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
800 return thread__resolve_callchain(al
->thread
, evsel
, sample
,
801 parent
, al
, max_stack
);
806 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
808 if (!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
)
810 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
813 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
814 bool hide_unresolved
)
819 al
->addr
= node
->map
->map_ip(node
->map
, node
->ip
);
823 if (al
->sym
== NULL
) {
830 if (al
->map
->groups
== &al
->machine
->kmaps
) {
831 if (machine__is_host(al
->machine
)) {
832 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
835 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
839 if (machine__is_host(al
->machine
)) {
840 al
->cpumode
= PERF_RECORD_MISC_USER
;
842 } else if (perf_guest
) {
843 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
846 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
855 char *callchain_list__sym_name(struct callchain_list
*cl
,
856 char *bf
, size_t bfsize
, bool show_dso
)
861 if (callchain_param
.key
== CCKEY_ADDRESS
&&
862 cl
->ms
.map
&& !cl
->srcline
)
863 cl
->srcline
= get_srcline(cl
->ms
.map
->dso
,
864 map__rip_2objdump(cl
->ms
.map
,
868 printed
= scnprintf(bf
, bfsize
, "%s %s",
869 cl
->ms
.sym
->name
, cl
->srcline
);
871 printed
= scnprintf(bf
, bfsize
, "%s", cl
->ms
.sym
->name
);
873 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
876 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
878 cl
->ms
.map
->dso
->short_name
:
884 char *callchain_node__scnprintf_value(struct callchain_node
*node
,
885 char *bf
, size_t bfsize
, u64 total
)
887 double percent
= 0.0;
888 u64 period
= callchain_cumul_hits(node
);
889 unsigned count
= callchain_cumul_counts(node
);
891 if (callchain_param
.mode
== CHAIN_FOLDED
) {
896 switch (callchain_param
.value
) {
898 scnprintf(bf
, bfsize
, "%"PRIu64
, period
);
901 scnprintf(bf
, bfsize
, "%u", count
);
906 percent
= period
* 100.0 / total
;
907 scnprintf(bf
, bfsize
, "%.2f%%", percent
);
913 int callchain_node__fprintf_value(struct callchain_node
*node
,
916 double percent
= 0.0;
917 u64 period
= callchain_cumul_hits(node
);
918 unsigned count
= callchain_cumul_counts(node
);
920 if (callchain_param
.mode
== CHAIN_FOLDED
) {
925 switch (callchain_param
.value
) {
927 return fprintf(fp
, "%"PRIu64
, period
);
929 return fprintf(fp
, "%u", count
);
933 percent
= period
* 100.0 / total
;
934 return percent_color_fprintf(fp
, "%.2f%%", percent
);
939 static void free_callchain_node(struct callchain_node
*node
)
941 struct callchain_list
*list
, *tmp
;
942 struct callchain_node
*child
;
945 list_for_each_entry_safe(list
, tmp
, &node
->parent_val
, list
) {
946 list_del(&list
->list
);
950 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
951 list_del(&list
->list
);
955 n
= rb_first(&node
->rb_root_in
);
957 child
= container_of(n
, struct callchain_node
, rb_node_in
);
959 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
961 free_callchain_node(child
);
966 void free_callchain(struct callchain_root
*root
)
968 if (!symbol_conf
.use_callchain
)
971 free_callchain_node(&root
->node
);
974 static u64
decay_callchain_node(struct callchain_node
*node
)
976 struct callchain_node
*child
;
980 n
= rb_first(&node
->rb_root_in
);
982 child
= container_of(n
, struct callchain_node
, rb_node_in
);
984 child_hits
+= decay_callchain_node(child
);
988 node
->hit
= (node
->hit
* 7) / 8;
989 node
->children_hit
= child_hits
;
994 void decay_callchain(struct callchain_root
*root
)
996 if (!symbol_conf
.use_callchain
)
999 decay_callchain_node(&root
->node
);
1002 int callchain_node__make_parent_list(struct callchain_node
*node
)
1004 struct callchain_node
*parent
= node
->parent
;
1005 struct callchain_list
*chain
, *new;
1009 list_for_each_entry_reverse(chain
, &parent
->val
, list
) {
1010 new = malloc(sizeof(*new));
1014 new->has_children
= false;
1015 list_add_tail(&new->list
, &head
);
1017 parent
= parent
->parent
;
1020 list_for_each_entry_safe_reverse(chain
, new, &head
, list
)
1021 list_move_tail(&chain
->list
, &node
->parent_val
);
1023 if (!list_empty(&node
->parent_val
)) {
1024 chain
= list_first_entry(&node
->parent_val
, struct callchain_list
, list
);
1025 chain
->has_children
= rb_prev(&node
->rb_node
) || rb_next(&node
->rb_node
);
1027 chain
= list_first_entry(&node
->val
, struct callchain_list
, list
);
1028 chain
->has_children
= false;
1033 list_for_each_entry_safe(chain
, new, &head
, list
) {
1034 list_del(&chain
->list
);