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 #ifdef HAVE_DWARF_UNWIND_SUPPORT
29 static int get_stack_size(const char *str
, unsigned long *_size
)
33 unsigned long max_size
= round_down(USHRT_MAX
, sizeof(u64
));
35 size
= strtoul(str
, &endptr
, 0);
41 size
= round_up(size
, sizeof(u64
));
42 if (!size
|| size
> max_size
)
50 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
54 #endif /* HAVE_DWARF_UNWIND_SUPPORT */
56 int parse_callchain_record_opt(const char *arg
)
58 char *tok
, *name
, *saveptr
= NULL
;
62 /* We need buffer that we know we can write to. */
63 buf
= malloc(strlen(arg
) + 1);
69 tok
= strtok_r((char *)buf
, ",", &saveptr
);
70 name
= tok
? : (char *)buf
;
73 /* Framepointer style */
74 if (!strncmp(name
, "fp", sizeof("fp"))) {
75 if (!strtok_r(NULL
, ",", &saveptr
)) {
76 callchain_param
.record_mode
= CALLCHAIN_FP
;
79 pr_err("callchain: No more arguments "
80 "needed for --call-graph fp\n");
83 #ifdef HAVE_DWARF_UNWIND_SUPPORT
85 } else if (!strncmp(name
, "dwarf", sizeof("dwarf"))) {
86 const unsigned long default_stack_dump_size
= 8192;
89 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
90 callchain_param
.dump_size
= default_stack_dump_size
;
92 tok
= strtok_r(NULL
, ",", &saveptr
);
94 unsigned long size
= 0;
96 ret
= get_stack_size(tok
, &size
);
97 callchain_param
.dump_size
= size
;
99 #endif /* HAVE_DWARF_UNWIND_SUPPORT */
101 pr_err("callchain: Unknown --call-graph option "
112 static int parse_callchain_mode(const char *value
)
114 if (!strncmp(value
, "graph", strlen(value
))) {
115 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
118 if (!strncmp(value
, "flat", strlen(value
))) {
119 callchain_param
.mode
= CHAIN_FLAT
;
122 if (!strncmp(value
, "fractal", strlen(value
))) {
123 callchain_param
.mode
= CHAIN_GRAPH_REL
;
129 static int parse_callchain_order(const char *value
)
131 if (!strncmp(value
, "caller", strlen(value
))) {
132 callchain_param
.order
= ORDER_CALLER
;
135 if (!strncmp(value
, "callee", strlen(value
))) {
136 callchain_param
.order
= ORDER_CALLEE
;
142 static int parse_callchain_sort_key(const char *value
)
144 if (!strncmp(value
, "function", strlen(value
))) {
145 callchain_param
.key
= CCKEY_FUNCTION
;
148 if (!strncmp(value
, "address", strlen(value
))) {
149 callchain_param
.key
= CCKEY_ADDRESS
;
152 if (!strncmp(value
, "branch", strlen(value
))) {
153 callchain_param
.branch_callstack
= 1;
160 parse_callchain_report_opt(const char *arg
)
164 bool minpcnt_set
= false;
166 symbol_conf
.use_callchain
= true;
171 while ((tok
= strtok((char *)arg
, ",")) != NULL
) {
172 if (!strncmp(tok
, "none", strlen(tok
))) {
173 callchain_param
.mode
= CHAIN_NONE
;
174 symbol_conf
.use_callchain
= false;
178 if (!parse_callchain_mode(tok
) ||
179 !parse_callchain_order(tok
) ||
180 !parse_callchain_sort_key(tok
)) {
181 /* parsing ok - move on to the next */
182 } else if (!minpcnt_set
) {
183 /* try to get the min percent */
184 callchain_param
.min_percent
= strtod(tok
, &endptr
);
189 /* try print limit at last */
190 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
198 if (callchain_register_param(&callchain_param
) < 0) {
199 pr_err("Can't register callchain params\n");
205 int perf_callchain_config(const char *var
, const char *value
)
209 if (prefixcmp(var
, "call-graph."))
211 var
+= sizeof("call-graph.") - 1;
213 if (!strcmp(var
, "record-mode"))
214 return parse_callchain_record_opt(value
);
215 #ifdef HAVE_DWARF_UNWIND_SUPPORT
216 if (!strcmp(var
, "dump-size")) {
217 unsigned long size
= 0;
220 ret
= get_stack_size(value
, &size
);
221 callchain_param
.dump_size
= size
;
226 if (!strcmp(var
, "print-type"))
227 return parse_callchain_mode(value
);
228 if (!strcmp(var
, "order"))
229 return parse_callchain_order(value
);
230 if (!strcmp(var
, "sort-key"))
231 return parse_callchain_sort_key(value
);
232 if (!strcmp(var
, "threshold")) {
233 callchain_param
.min_percent
= strtod(value
, &endptr
);
237 if (!strcmp(var
, "print-limit")) {
238 callchain_param
.print_limit
= strtod(value
, &endptr
);
247 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
248 enum chain_mode mode
)
250 struct rb_node
**p
= &root
->rb_node
;
251 struct rb_node
*parent
= NULL
;
252 struct callchain_node
*rnode
;
253 u64 chain_cumul
= callchain_cumul_hits(chain
);
259 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
260 rnode_cumul
= callchain_cumul_hits(rnode
);
264 if (rnode
->hit
< chain
->hit
)
269 case CHAIN_GRAPH_ABS
: /* Falldown */
270 case CHAIN_GRAPH_REL
:
271 if (rnode_cumul
< chain_cumul
)
282 rb_link_node(&chain
->rb_node
, parent
, p
);
283 rb_insert_color(&chain
->rb_node
, root
);
287 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
291 struct callchain_node
*child
;
293 n
= rb_first(&node
->rb_root_in
);
295 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
298 __sort_chain_flat(rb_root
, child
, min_hit
);
301 if (node
->hit
&& node
->hit
>= min_hit
)
302 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
306 * Once we get every callchains from the stream, we can now
310 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
311 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
313 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
316 static void __sort_chain_graph_abs(struct callchain_node
*node
,
320 struct callchain_node
*child
;
322 node
->rb_root
= RB_ROOT
;
323 n
= rb_first(&node
->rb_root_in
);
326 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
329 __sort_chain_graph_abs(child
, min_hit
);
330 if (callchain_cumul_hits(child
) >= min_hit
)
331 rb_insert_callchain(&node
->rb_root
, child
,
337 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
338 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
340 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
341 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
344 static void __sort_chain_graph_rel(struct callchain_node
*node
,
348 struct callchain_node
*child
;
351 node
->rb_root
= RB_ROOT
;
352 min_hit
= ceil(node
->children_hit
* min_percent
);
354 n
= rb_first(&node
->rb_root_in
);
356 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
359 __sort_chain_graph_rel(child
, min_percent
);
360 if (callchain_cumul_hits(child
) >= min_hit
)
361 rb_insert_callchain(&node
->rb_root
, child
,
367 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
368 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
370 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
371 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
374 int callchain_register_param(struct callchain_param
*param
)
376 switch (param
->mode
) {
377 case CHAIN_GRAPH_ABS
:
378 param
->sort
= sort_chain_graph_abs
;
380 case CHAIN_GRAPH_REL
:
381 param
->sort
= sort_chain_graph_rel
;
384 param
->sort
= sort_chain_flat
;
394 * Create a child for a parent. If inherit_children, then the new child
395 * will become the new parent of it's parent children
397 static struct callchain_node
*
398 create_child(struct callchain_node
*parent
, bool inherit_children
)
400 struct callchain_node
*new;
402 new = zalloc(sizeof(*new));
404 perror("not enough memory to create child for code path tree");
407 new->parent
= parent
;
408 INIT_LIST_HEAD(&new->val
);
410 if (inherit_children
) {
412 struct callchain_node
*child
;
414 new->rb_root_in
= parent
->rb_root_in
;
415 parent
->rb_root_in
= RB_ROOT
;
417 n
= rb_first(&new->rb_root_in
);
419 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
424 /* make it the first child */
425 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
426 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
434 * Fill the node with callchain values
437 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
439 struct callchain_cursor_node
*cursor_node
;
441 node
->val_nr
= cursor
->nr
- cursor
->pos
;
443 pr_warning("Warning: empty node in callchain tree\n");
445 cursor_node
= callchain_cursor_current(cursor
);
447 while (cursor_node
) {
448 struct callchain_list
*call
;
450 call
= zalloc(sizeof(*call
));
452 perror("not enough memory for the code path tree");
455 call
->ip
= cursor_node
->ip
;
456 call
->ms
.sym
= cursor_node
->sym
;
457 call
->ms
.map
= cursor_node
->map
;
458 list_add_tail(&call
->list
, &node
->val
);
460 callchain_cursor_advance(cursor
);
461 cursor_node
= callchain_cursor_current(cursor
);
465 static struct callchain_node
*
466 add_child(struct callchain_node
*parent
,
467 struct callchain_cursor
*cursor
,
470 struct callchain_node
*new;
472 new = create_child(parent
, false);
473 fill_node(new, cursor
);
475 new->children_hit
= 0;
480 static s64
match_chain(struct callchain_cursor_node
*node
,
481 struct callchain_list
*cnode
)
483 struct symbol
*sym
= node
->sym
;
485 if (cnode
->ms
.sym
&& sym
&&
486 callchain_param
.key
== CCKEY_FUNCTION
)
487 return cnode
->ms
.sym
->start
- sym
->start
;
489 return cnode
->ip
- node
->ip
;
493 * Split the parent in two parts (a new child is created) and
494 * give a part of its callchain to the created child.
495 * Then create another child to host the given callchain of new branch
498 split_add_child(struct callchain_node
*parent
,
499 struct callchain_cursor
*cursor
,
500 struct callchain_list
*to_split
,
501 u64 idx_parents
, u64 idx_local
, u64 period
)
503 struct callchain_node
*new;
504 struct list_head
*old_tail
;
505 unsigned int idx_total
= idx_parents
+ idx_local
;
508 new = create_child(parent
, true);
510 /* split the callchain and move a part to the new child */
511 old_tail
= parent
->val
.prev
;
512 list_del_range(&to_split
->list
, old_tail
);
513 new->val
.next
= &to_split
->list
;
514 new->val
.prev
= old_tail
;
515 to_split
->list
.prev
= &new->val
;
516 old_tail
->next
= &new->val
;
519 new->hit
= parent
->hit
;
520 new->children_hit
= parent
->children_hit
;
521 parent
->children_hit
= callchain_cumul_hits(new);
522 new->val_nr
= parent
->val_nr
- idx_local
;
523 parent
->val_nr
= idx_local
;
525 /* create a new child for the new branch if any */
526 if (idx_total
< cursor
->nr
) {
527 struct callchain_node
*first
;
528 struct callchain_list
*cnode
;
529 struct callchain_cursor_node
*node
;
530 struct rb_node
*p
, **pp
;
533 parent
->children_hit
+= period
;
535 node
= callchain_cursor_current(cursor
);
536 new = add_child(parent
, cursor
, period
);
539 * This is second child since we moved parent's children
540 * to new (first) child above.
542 p
= parent
->rb_root_in
.rb_node
;
543 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
544 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
547 if (match_chain(node
, cnode
) < 0)
552 rb_link_node(&new->rb_node_in
, p
, pp
);
553 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
555 parent
->hit
= period
;
560 append_chain(struct callchain_node
*root
,
561 struct callchain_cursor
*cursor
,
565 append_chain_children(struct callchain_node
*root
,
566 struct callchain_cursor
*cursor
,
569 struct callchain_node
*rnode
;
570 struct callchain_cursor_node
*node
;
571 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
572 struct rb_node
*parent
= NULL
;
574 node
= callchain_cursor_current(cursor
);
578 /* lookup in childrens */
583 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
585 /* If at least first entry matches, rely to children */
586 ret
= append_chain(rnode
, cursor
, period
);
588 goto inc_children_hit
;
591 p
= &parent
->rb_left
;
593 p
= &parent
->rb_right
;
595 /* nothing in children, add to the current node */
596 rnode
= add_child(root
, cursor
, period
);
597 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
598 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
601 root
->children_hit
+= period
;
605 append_chain(struct callchain_node
*root
,
606 struct callchain_cursor
*cursor
,
609 struct callchain_list
*cnode
;
610 u64 start
= cursor
->pos
;
616 * Lookup in the current node
617 * If we have a symbol, then compare the start to match
618 * anywhere inside a function, unless function
621 list_for_each_entry(cnode
, &root
->val
, list
) {
622 struct callchain_cursor_node
*node
;
624 node
= callchain_cursor_current(cursor
);
628 cmp
= match_chain(node
, cnode
);
634 callchain_cursor_advance(cursor
);
637 /* matches not, relay no the parent */
639 WARN_ONCE(!cmp
, "Chain comparison error\n");
643 matches
= cursor
->pos
- start
;
645 /* we match only a part of the node. Split it and add the new chain */
646 if (matches
< root
->val_nr
) {
647 split_add_child(root
, cursor
, cnode
, start
, matches
, period
);
651 /* we match 100% of the path, increment the hit */
652 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
657 /* We match the node and still have a part remaining */
658 append_chain_children(root
, cursor
, period
);
663 int callchain_append(struct callchain_root
*root
,
664 struct callchain_cursor
*cursor
,
670 callchain_cursor_commit(cursor
);
672 append_chain_children(&root
->node
, cursor
, period
);
674 if (cursor
->nr
> root
->max_depth
)
675 root
->max_depth
= cursor
->nr
;
681 merge_chain_branch(struct callchain_cursor
*cursor
,
682 struct callchain_node
*dst
, struct callchain_node
*src
)
684 struct callchain_cursor_node
**old_last
= cursor
->last
;
685 struct callchain_node
*child
;
686 struct callchain_list
*list
, *next_list
;
688 int old_pos
= cursor
->nr
;
691 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
692 callchain_cursor_append(cursor
, list
->ip
,
693 list
->ms
.map
, list
->ms
.sym
);
694 list_del(&list
->list
);
699 callchain_cursor_commit(cursor
);
700 append_chain_children(dst
, cursor
, src
->hit
);
703 n
= rb_first(&src
->rb_root_in
);
705 child
= container_of(n
, struct callchain_node
, rb_node_in
);
707 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
709 err
= merge_chain_branch(cursor
, dst
, child
);
716 cursor
->nr
= old_pos
;
717 cursor
->last
= old_last
;
722 int callchain_merge(struct callchain_cursor
*cursor
,
723 struct callchain_root
*dst
, struct callchain_root
*src
)
725 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
728 int callchain_cursor_append(struct callchain_cursor
*cursor
,
729 u64 ip
, struct map
*map
, struct symbol
*sym
)
731 struct callchain_cursor_node
*node
= *cursor
->last
;
734 node
= calloc(1, sizeof(*node
));
738 *cursor
->last
= node
;
747 cursor
->last
= &node
->next
;
752 int sample__resolve_callchain(struct perf_sample
*sample
, struct symbol
**parent
,
753 struct perf_evsel
*evsel
, struct addr_location
*al
,
756 if (sample
->callchain
== NULL
)
759 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
761 return thread__resolve_callchain(al
->thread
, evsel
, sample
,
762 parent
, al
, max_stack
);
767 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
769 if (!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
)
771 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
774 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
775 bool hide_unresolved
)
780 al
->addr
= node
->map
->map_ip(node
->map
, node
->ip
);
784 if (al
->sym
== NULL
) {
791 if (al
->map
->groups
== &al
->machine
->kmaps
) {
792 if (machine__is_host(al
->machine
)) {
793 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
796 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
800 if (machine__is_host(al
->machine
)) {
801 al
->cpumode
= PERF_RECORD_MISC_USER
;
803 } else if (perf_guest
) {
804 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
807 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
816 char *callchain_list__sym_name(struct callchain_list
*cl
,
817 char *bf
, size_t bfsize
, bool show_dso
)
822 if (callchain_param
.key
== CCKEY_ADDRESS
&&
823 cl
->ms
.map
&& !cl
->srcline
)
824 cl
->srcline
= get_srcline(cl
->ms
.map
->dso
,
825 map__rip_2objdump(cl
->ms
.map
,
829 printed
= scnprintf(bf
, bfsize
, "%s %s",
830 cl
->ms
.sym
->name
, cl
->srcline
);
832 printed
= scnprintf(bf
, bfsize
, "%s", cl
->ms
.sym
->name
);
834 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
837 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
839 cl
->ms
.map
->dso
->short_name
:
845 static void free_callchain_node(struct callchain_node
*node
)
847 struct callchain_list
*list
, *tmp
;
848 struct callchain_node
*child
;
851 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
852 list_del(&list
->list
);
856 n
= rb_first(&node
->rb_root_in
);
858 child
= container_of(n
, struct callchain_node
, rb_node_in
);
860 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
862 free_callchain_node(child
);
867 void free_callchain(struct callchain_root
*root
)
869 if (!symbol_conf
.use_callchain
)
872 free_callchain_node(&root
->node
);