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
;
50 static int parse_callchain_order(const char *value
)
52 if (!strncmp(value
, "caller", strlen(value
))) {
53 callchain_param
.order
= ORDER_CALLER
;
54 callchain_param
.order_set
= true;
57 if (!strncmp(value
, "callee", strlen(value
))) {
58 callchain_param
.order
= ORDER_CALLEE
;
59 callchain_param
.order_set
= true;
65 static int parse_callchain_sort_key(const char *value
)
67 if (!strncmp(value
, "function", strlen(value
))) {
68 callchain_param
.key
= CCKEY_FUNCTION
;
71 if (!strncmp(value
, "address", strlen(value
))) {
72 callchain_param
.key
= CCKEY_ADDRESS
;
75 if (!strncmp(value
, "branch", strlen(value
))) {
76 callchain_param
.branch_callstack
= 1;
83 __parse_callchain_report_opt(const char *arg
, bool allow_record_opt
)
87 bool minpcnt_set
= false;
88 bool record_opt_set
= false;
89 bool try_stack_size
= false;
91 symbol_conf
.use_callchain
= true;
96 while ((tok
= strtok((char *)arg
, ",")) != NULL
) {
97 if (!strncmp(tok
, "none", strlen(tok
))) {
98 callchain_param
.mode
= CHAIN_NONE
;
99 symbol_conf
.use_callchain
= false;
103 if (!parse_callchain_mode(tok
) ||
104 !parse_callchain_order(tok
) ||
105 !parse_callchain_sort_key(tok
)) {
106 /* parsing ok - move on to the next */
107 try_stack_size
= false;
109 } else if (allow_record_opt
&& !record_opt_set
) {
110 if (parse_callchain_record(tok
, &callchain_param
))
113 /* assume that number followed by 'dwarf' is stack size */
114 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
)
115 try_stack_size
= true;
117 record_opt_set
= true;
122 if (try_stack_size
) {
123 unsigned long size
= 0;
125 if (get_stack_size(tok
, &size
) < 0)
127 callchain_param
.dump_size
= size
;
128 try_stack_size
= false;
129 } else if (!minpcnt_set
) {
130 /* try to get the min percent */
131 callchain_param
.min_percent
= strtod(tok
, &endptr
);
136 /* try print limit at last */
137 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
145 if (callchain_register_param(&callchain_param
) < 0) {
146 pr_err("Can't register callchain params\n");
152 int parse_callchain_report_opt(const char *arg
)
154 return __parse_callchain_report_opt(arg
, false);
157 int parse_callchain_top_opt(const char *arg
)
159 return __parse_callchain_report_opt(arg
, true);
162 int perf_callchain_config(const char *var
, const char *value
)
166 if (prefixcmp(var
, "call-graph."))
168 var
+= sizeof("call-graph.") - 1;
170 if (!strcmp(var
, "record-mode"))
171 return parse_callchain_record_opt(value
, &callchain_param
);
172 #ifdef HAVE_DWARF_UNWIND_SUPPORT
173 if (!strcmp(var
, "dump-size")) {
174 unsigned long size
= 0;
177 ret
= get_stack_size(value
, &size
);
178 callchain_param
.dump_size
= size
;
183 if (!strcmp(var
, "print-type"))
184 return parse_callchain_mode(value
);
185 if (!strcmp(var
, "order"))
186 return parse_callchain_order(value
);
187 if (!strcmp(var
, "sort-key"))
188 return parse_callchain_sort_key(value
);
189 if (!strcmp(var
, "threshold")) {
190 callchain_param
.min_percent
= strtod(value
, &endptr
);
194 if (!strcmp(var
, "print-limit")) {
195 callchain_param
.print_limit
= strtod(value
, &endptr
);
204 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
205 enum chain_mode mode
)
207 struct rb_node
**p
= &root
->rb_node
;
208 struct rb_node
*parent
= NULL
;
209 struct callchain_node
*rnode
;
210 u64 chain_cumul
= callchain_cumul_hits(chain
);
216 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
217 rnode_cumul
= callchain_cumul_hits(rnode
);
221 if (rnode
->hit
< chain
->hit
)
226 case CHAIN_GRAPH_ABS
: /* Falldown */
227 case CHAIN_GRAPH_REL
:
228 if (rnode_cumul
< chain_cumul
)
239 rb_link_node(&chain
->rb_node
, parent
, p
);
240 rb_insert_color(&chain
->rb_node
, root
);
244 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
248 struct callchain_node
*child
;
250 n
= rb_first(&node
->rb_root_in
);
252 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
255 __sort_chain_flat(rb_root
, child
, min_hit
);
258 if (node
->hit
&& node
->hit
>= min_hit
)
259 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
263 * Once we get every callchains from the stream, we can now
267 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
268 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
270 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
273 static void __sort_chain_graph_abs(struct callchain_node
*node
,
277 struct callchain_node
*child
;
279 node
->rb_root
= RB_ROOT
;
280 n
= rb_first(&node
->rb_root_in
);
283 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
286 __sort_chain_graph_abs(child
, min_hit
);
287 if (callchain_cumul_hits(child
) >= min_hit
)
288 rb_insert_callchain(&node
->rb_root
, child
,
294 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
295 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
297 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
298 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
301 static void __sort_chain_graph_rel(struct callchain_node
*node
,
305 struct callchain_node
*child
;
308 node
->rb_root
= RB_ROOT
;
309 min_hit
= ceil(node
->children_hit
* min_percent
);
311 n
= rb_first(&node
->rb_root_in
);
313 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
316 __sort_chain_graph_rel(child
, min_percent
);
317 if (callchain_cumul_hits(child
) >= min_hit
)
318 rb_insert_callchain(&node
->rb_root
, child
,
324 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
325 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
327 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
328 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
331 int callchain_register_param(struct callchain_param
*param
)
333 switch (param
->mode
) {
334 case CHAIN_GRAPH_ABS
:
335 param
->sort
= sort_chain_graph_abs
;
337 case CHAIN_GRAPH_REL
:
338 param
->sort
= sort_chain_graph_rel
;
341 param
->sort
= sort_chain_flat
;
351 * Create a child for a parent. If inherit_children, then the new child
352 * will become the new parent of it's parent children
354 static struct callchain_node
*
355 create_child(struct callchain_node
*parent
, bool inherit_children
)
357 struct callchain_node
*new;
359 new = zalloc(sizeof(*new));
361 perror("not enough memory to create child for code path tree");
364 new->parent
= parent
;
365 INIT_LIST_HEAD(&new->val
);
367 if (inherit_children
) {
369 struct callchain_node
*child
;
371 new->rb_root_in
= parent
->rb_root_in
;
372 parent
->rb_root_in
= RB_ROOT
;
374 n
= rb_first(&new->rb_root_in
);
376 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
381 /* make it the first child */
382 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
383 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
391 * Fill the node with callchain values
394 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
396 struct callchain_cursor_node
*cursor_node
;
398 node
->val_nr
= cursor
->nr
- cursor
->pos
;
400 pr_warning("Warning: empty node in callchain tree\n");
402 cursor_node
= callchain_cursor_current(cursor
);
404 while (cursor_node
) {
405 struct callchain_list
*call
;
407 call
= zalloc(sizeof(*call
));
409 perror("not enough memory for the code path tree");
412 call
->ip
= cursor_node
->ip
;
413 call
->ms
.sym
= cursor_node
->sym
;
414 call
->ms
.map
= cursor_node
->map
;
415 list_add_tail(&call
->list
, &node
->val
);
417 callchain_cursor_advance(cursor
);
418 cursor_node
= callchain_cursor_current(cursor
);
422 static struct callchain_node
*
423 add_child(struct callchain_node
*parent
,
424 struct callchain_cursor
*cursor
,
427 struct callchain_node
*new;
429 new = create_child(parent
, false);
430 fill_node(new, cursor
);
432 new->children_hit
= 0;
437 static s64
match_chain(struct callchain_cursor_node
*node
,
438 struct callchain_list
*cnode
)
440 struct symbol
*sym
= node
->sym
;
442 if (cnode
->ms
.sym
&& sym
&&
443 callchain_param
.key
== CCKEY_FUNCTION
)
444 return cnode
->ms
.sym
->start
- sym
->start
;
446 return cnode
->ip
- node
->ip
;
450 * Split the parent in two parts (a new child is created) and
451 * give a part of its callchain to the created child.
452 * Then create another child to host the given callchain of new branch
455 split_add_child(struct callchain_node
*parent
,
456 struct callchain_cursor
*cursor
,
457 struct callchain_list
*to_split
,
458 u64 idx_parents
, u64 idx_local
, u64 period
)
460 struct callchain_node
*new;
461 struct list_head
*old_tail
;
462 unsigned int idx_total
= idx_parents
+ idx_local
;
465 new = create_child(parent
, true);
467 /* split the callchain and move a part to the new child */
468 old_tail
= parent
->val
.prev
;
469 list_del_range(&to_split
->list
, old_tail
);
470 new->val
.next
= &to_split
->list
;
471 new->val
.prev
= old_tail
;
472 to_split
->list
.prev
= &new->val
;
473 old_tail
->next
= &new->val
;
476 new->hit
= parent
->hit
;
477 new->children_hit
= parent
->children_hit
;
478 parent
->children_hit
= callchain_cumul_hits(new);
479 new->val_nr
= parent
->val_nr
- idx_local
;
480 parent
->val_nr
= idx_local
;
482 /* create a new child for the new branch if any */
483 if (idx_total
< cursor
->nr
) {
484 struct callchain_node
*first
;
485 struct callchain_list
*cnode
;
486 struct callchain_cursor_node
*node
;
487 struct rb_node
*p
, **pp
;
490 parent
->children_hit
+= period
;
492 node
= callchain_cursor_current(cursor
);
493 new = add_child(parent
, cursor
, period
);
496 * This is second child since we moved parent's children
497 * to new (first) child above.
499 p
= parent
->rb_root_in
.rb_node
;
500 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
501 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
504 if (match_chain(node
, cnode
) < 0)
509 rb_link_node(&new->rb_node_in
, p
, pp
);
510 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
512 parent
->hit
= period
;
517 append_chain(struct callchain_node
*root
,
518 struct callchain_cursor
*cursor
,
522 append_chain_children(struct callchain_node
*root
,
523 struct callchain_cursor
*cursor
,
526 struct callchain_node
*rnode
;
527 struct callchain_cursor_node
*node
;
528 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
529 struct rb_node
*parent
= NULL
;
531 node
= callchain_cursor_current(cursor
);
535 /* lookup in childrens */
540 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
542 /* If at least first entry matches, rely to children */
543 ret
= append_chain(rnode
, cursor
, period
);
545 goto inc_children_hit
;
548 p
= &parent
->rb_left
;
550 p
= &parent
->rb_right
;
552 /* nothing in children, add to the current node */
553 rnode
= add_child(root
, cursor
, period
);
554 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
555 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
558 root
->children_hit
+= period
;
562 append_chain(struct callchain_node
*root
,
563 struct callchain_cursor
*cursor
,
566 struct callchain_list
*cnode
;
567 u64 start
= cursor
->pos
;
573 * Lookup in the current node
574 * If we have a symbol, then compare the start to match
575 * anywhere inside a function, unless function
578 list_for_each_entry(cnode
, &root
->val
, list
) {
579 struct callchain_cursor_node
*node
;
581 node
= callchain_cursor_current(cursor
);
585 cmp
= match_chain(node
, cnode
);
591 callchain_cursor_advance(cursor
);
594 /* matches not, relay no the parent */
596 WARN_ONCE(!cmp
, "Chain comparison error\n");
600 matches
= cursor
->pos
- start
;
602 /* we match only a part of the node. Split it and add the new chain */
603 if (matches
< root
->val_nr
) {
604 split_add_child(root
, cursor
, cnode
, start
, matches
, period
);
608 /* we match 100% of the path, increment the hit */
609 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
614 /* We match the node and still have a part remaining */
615 append_chain_children(root
, cursor
, period
);
620 int callchain_append(struct callchain_root
*root
,
621 struct callchain_cursor
*cursor
,
627 callchain_cursor_commit(cursor
);
629 append_chain_children(&root
->node
, cursor
, period
);
631 if (cursor
->nr
> root
->max_depth
)
632 root
->max_depth
= cursor
->nr
;
638 merge_chain_branch(struct callchain_cursor
*cursor
,
639 struct callchain_node
*dst
, struct callchain_node
*src
)
641 struct callchain_cursor_node
**old_last
= cursor
->last
;
642 struct callchain_node
*child
;
643 struct callchain_list
*list
, *next_list
;
645 int old_pos
= cursor
->nr
;
648 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
649 callchain_cursor_append(cursor
, list
->ip
,
650 list
->ms
.map
, list
->ms
.sym
);
651 list_del(&list
->list
);
656 callchain_cursor_commit(cursor
);
657 append_chain_children(dst
, cursor
, src
->hit
);
660 n
= rb_first(&src
->rb_root_in
);
662 child
= container_of(n
, struct callchain_node
, rb_node_in
);
664 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
666 err
= merge_chain_branch(cursor
, dst
, child
);
673 cursor
->nr
= old_pos
;
674 cursor
->last
= old_last
;
679 int callchain_merge(struct callchain_cursor
*cursor
,
680 struct callchain_root
*dst
, struct callchain_root
*src
)
682 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
685 int callchain_cursor_append(struct callchain_cursor
*cursor
,
686 u64 ip
, struct map
*map
, struct symbol
*sym
)
688 struct callchain_cursor_node
*node
= *cursor
->last
;
691 node
= calloc(1, sizeof(*node
));
695 *cursor
->last
= node
;
704 cursor
->last
= &node
->next
;
709 int sample__resolve_callchain(struct perf_sample
*sample
, struct symbol
**parent
,
710 struct perf_evsel
*evsel
, struct addr_location
*al
,
713 if (sample
->callchain
== NULL
)
716 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
718 return thread__resolve_callchain(al
->thread
, evsel
, sample
,
719 parent
, al
, max_stack
);
724 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
726 if (!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
)
728 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
731 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
732 bool hide_unresolved
)
737 al
->addr
= node
->map
->map_ip(node
->map
, node
->ip
);
741 if (al
->sym
== NULL
) {
748 if (al
->map
->groups
== &al
->machine
->kmaps
) {
749 if (machine__is_host(al
->machine
)) {
750 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
753 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
757 if (machine__is_host(al
->machine
)) {
758 al
->cpumode
= PERF_RECORD_MISC_USER
;
760 } else if (perf_guest
) {
761 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
764 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
773 char *callchain_list__sym_name(struct callchain_list
*cl
,
774 char *bf
, size_t bfsize
, bool show_dso
)
779 if (callchain_param
.key
== CCKEY_ADDRESS
&&
780 cl
->ms
.map
&& !cl
->srcline
)
781 cl
->srcline
= get_srcline(cl
->ms
.map
->dso
,
782 map__rip_2objdump(cl
->ms
.map
,
786 printed
= scnprintf(bf
, bfsize
, "%s %s",
787 cl
->ms
.sym
->name
, cl
->srcline
);
789 printed
= scnprintf(bf
, bfsize
, "%s", cl
->ms
.sym
->name
);
791 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
794 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
796 cl
->ms
.map
->dso
->short_name
:
802 static void free_callchain_node(struct callchain_node
*node
)
804 struct callchain_list
*list
, *tmp
;
805 struct callchain_node
*child
;
808 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
809 list_del(&list
->list
);
813 n
= rb_first(&node
->rb_root_in
);
815 child
= container_of(n
, struct callchain_node
, rb_node_in
);
817 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
819 free_callchain_node(child
);
824 void free_callchain(struct callchain_root
*root
)
826 if (!symbol_conf
.use_callchain
)
829 free_callchain_node(&root
->node
);