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
;
56 if (!strncmp(value
, "callee", strlen(value
))) {
57 callchain_param
.order
= ORDER_CALLEE
;
63 static int parse_callchain_sort_key(const char *value
)
65 if (!strncmp(value
, "function", strlen(value
))) {
66 callchain_param
.key
= CCKEY_FUNCTION
;
69 if (!strncmp(value
, "address", strlen(value
))) {
70 callchain_param
.key
= CCKEY_ADDRESS
;
73 if (!strncmp(value
, "branch", strlen(value
))) {
74 callchain_param
.branch_callstack
= 1;
81 parse_callchain_report_opt(const char *arg
)
85 bool minpcnt_set
= false;
87 symbol_conf
.use_callchain
= true;
92 while ((tok
= strtok((char *)arg
, ",")) != NULL
) {
93 if (!strncmp(tok
, "none", strlen(tok
))) {
94 callchain_param
.mode
= CHAIN_NONE
;
95 symbol_conf
.use_callchain
= false;
99 if (!parse_callchain_mode(tok
) ||
100 !parse_callchain_order(tok
) ||
101 !parse_callchain_sort_key(tok
)) {
102 /* parsing ok - move on to the next */
103 } else if (!minpcnt_set
) {
104 /* try to get the min percent */
105 callchain_param
.min_percent
= strtod(tok
, &endptr
);
110 /* try print limit at last */
111 callchain_param
.print_limit
= strtoul(tok
, &endptr
, 0);
119 if (callchain_register_param(&callchain_param
) < 0) {
120 pr_err("Can't register callchain params\n");
126 int perf_callchain_config(const char *var
, const char *value
)
130 if (prefixcmp(var
, "call-graph."))
132 var
+= sizeof("call-graph.") - 1;
134 if (!strcmp(var
, "record-mode"))
135 return parse_callchain_record_opt(value
, &callchain_param
);
136 #ifdef HAVE_DWARF_UNWIND_SUPPORT
137 if (!strcmp(var
, "dump-size")) {
138 unsigned long size
= 0;
141 ret
= get_stack_size(value
, &size
);
142 callchain_param
.dump_size
= size
;
147 if (!strcmp(var
, "print-type"))
148 return parse_callchain_mode(value
);
149 if (!strcmp(var
, "order"))
150 return parse_callchain_order(value
);
151 if (!strcmp(var
, "sort-key"))
152 return parse_callchain_sort_key(value
);
153 if (!strcmp(var
, "threshold")) {
154 callchain_param
.min_percent
= strtod(value
, &endptr
);
158 if (!strcmp(var
, "print-limit")) {
159 callchain_param
.print_limit
= strtod(value
, &endptr
);
168 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
169 enum chain_mode mode
)
171 struct rb_node
**p
= &root
->rb_node
;
172 struct rb_node
*parent
= NULL
;
173 struct callchain_node
*rnode
;
174 u64 chain_cumul
= callchain_cumul_hits(chain
);
180 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
181 rnode_cumul
= callchain_cumul_hits(rnode
);
185 if (rnode
->hit
< chain
->hit
)
190 case CHAIN_GRAPH_ABS
: /* Falldown */
191 case CHAIN_GRAPH_REL
:
192 if (rnode_cumul
< chain_cumul
)
203 rb_link_node(&chain
->rb_node
, parent
, p
);
204 rb_insert_color(&chain
->rb_node
, root
);
208 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
212 struct callchain_node
*child
;
214 n
= rb_first(&node
->rb_root_in
);
216 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
219 __sort_chain_flat(rb_root
, child
, min_hit
);
222 if (node
->hit
&& node
->hit
>= min_hit
)
223 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
227 * Once we get every callchains from the stream, we can now
231 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
232 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
234 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
237 static void __sort_chain_graph_abs(struct callchain_node
*node
,
241 struct callchain_node
*child
;
243 node
->rb_root
= RB_ROOT
;
244 n
= rb_first(&node
->rb_root_in
);
247 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
250 __sort_chain_graph_abs(child
, min_hit
);
251 if (callchain_cumul_hits(child
) >= min_hit
)
252 rb_insert_callchain(&node
->rb_root
, child
,
258 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
259 u64 min_hit
, struct callchain_param
*param __maybe_unused
)
261 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
262 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
265 static void __sort_chain_graph_rel(struct callchain_node
*node
,
269 struct callchain_node
*child
;
272 node
->rb_root
= RB_ROOT
;
273 min_hit
= ceil(node
->children_hit
* min_percent
);
275 n
= rb_first(&node
->rb_root_in
);
277 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
280 __sort_chain_graph_rel(child
, min_percent
);
281 if (callchain_cumul_hits(child
) >= min_hit
)
282 rb_insert_callchain(&node
->rb_root
, child
,
288 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
289 u64 min_hit __maybe_unused
, struct callchain_param
*param
)
291 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
292 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
295 int callchain_register_param(struct callchain_param
*param
)
297 switch (param
->mode
) {
298 case CHAIN_GRAPH_ABS
:
299 param
->sort
= sort_chain_graph_abs
;
301 case CHAIN_GRAPH_REL
:
302 param
->sort
= sort_chain_graph_rel
;
305 param
->sort
= sort_chain_flat
;
315 * Create a child for a parent. If inherit_children, then the new child
316 * will become the new parent of it's parent children
318 static struct callchain_node
*
319 create_child(struct callchain_node
*parent
, bool inherit_children
)
321 struct callchain_node
*new;
323 new = zalloc(sizeof(*new));
325 perror("not enough memory to create child for code path tree");
328 new->parent
= parent
;
329 INIT_LIST_HEAD(&new->val
);
331 if (inherit_children
) {
333 struct callchain_node
*child
;
335 new->rb_root_in
= parent
->rb_root_in
;
336 parent
->rb_root_in
= RB_ROOT
;
338 n
= rb_first(&new->rb_root_in
);
340 child
= rb_entry(n
, struct callchain_node
, rb_node_in
);
345 /* make it the first child */
346 rb_link_node(&new->rb_node_in
, NULL
, &parent
->rb_root_in
.rb_node
);
347 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
355 * Fill the node with callchain values
358 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
360 struct callchain_cursor_node
*cursor_node
;
362 node
->val_nr
= cursor
->nr
- cursor
->pos
;
364 pr_warning("Warning: empty node in callchain tree\n");
366 cursor_node
= callchain_cursor_current(cursor
);
368 while (cursor_node
) {
369 struct callchain_list
*call
;
371 call
= zalloc(sizeof(*call
));
373 perror("not enough memory for the code path tree");
376 call
->ip
= cursor_node
->ip
;
377 call
->ms
.sym
= cursor_node
->sym
;
378 call
->ms
.map
= cursor_node
->map
;
379 list_add_tail(&call
->list
, &node
->val
);
381 callchain_cursor_advance(cursor
);
382 cursor_node
= callchain_cursor_current(cursor
);
386 static struct callchain_node
*
387 add_child(struct callchain_node
*parent
,
388 struct callchain_cursor
*cursor
,
391 struct callchain_node
*new;
393 new = create_child(parent
, false);
394 fill_node(new, cursor
);
396 new->children_hit
= 0;
401 static s64
match_chain(struct callchain_cursor_node
*node
,
402 struct callchain_list
*cnode
)
404 struct symbol
*sym
= node
->sym
;
406 if (cnode
->ms
.sym
&& sym
&&
407 callchain_param
.key
== CCKEY_FUNCTION
)
408 return cnode
->ms
.sym
->start
- sym
->start
;
410 return cnode
->ip
- node
->ip
;
414 * Split the parent in two parts (a new child is created) and
415 * give a part of its callchain to the created child.
416 * Then create another child to host the given callchain of new branch
419 split_add_child(struct callchain_node
*parent
,
420 struct callchain_cursor
*cursor
,
421 struct callchain_list
*to_split
,
422 u64 idx_parents
, u64 idx_local
, u64 period
)
424 struct callchain_node
*new;
425 struct list_head
*old_tail
;
426 unsigned int idx_total
= idx_parents
+ idx_local
;
429 new = create_child(parent
, true);
431 /* split the callchain and move a part to the new child */
432 old_tail
= parent
->val
.prev
;
433 list_del_range(&to_split
->list
, old_tail
);
434 new->val
.next
= &to_split
->list
;
435 new->val
.prev
= old_tail
;
436 to_split
->list
.prev
= &new->val
;
437 old_tail
->next
= &new->val
;
440 new->hit
= parent
->hit
;
441 new->children_hit
= parent
->children_hit
;
442 parent
->children_hit
= callchain_cumul_hits(new);
443 new->val_nr
= parent
->val_nr
- idx_local
;
444 parent
->val_nr
= idx_local
;
446 /* create a new child for the new branch if any */
447 if (idx_total
< cursor
->nr
) {
448 struct callchain_node
*first
;
449 struct callchain_list
*cnode
;
450 struct callchain_cursor_node
*node
;
451 struct rb_node
*p
, **pp
;
454 parent
->children_hit
+= period
;
456 node
= callchain_cursor_current(cursor
);
457 new = add_child(parent
, cursor
, period
);
460 * This is second child since we moved parent's children
461 * to new (first) child above.
463 p
= parent
->rb_root_in
.rb_node
;
464 first
= rb_entry(p
, struct callchain_node
, rb_node_in
);
465 cnode
= list_first_entry(&first
->val
, struct callchain_list
,
468 if (match_chain(node
, cnode
) < 0)
473 rb_link_node(&new->rb_node_in
, p
, pp
);
474 rb_insert_color(&new->rb_node_in
, &parent
->rb_root_in
);
476 parent
->hit
= period
;
481 append_chain(struct callchain_node
*root
,
482 struct callchain_cursor
*cursor
,
486 append_chain_children(struct callchain_node
*root
,
487 struct callchain_cursor
*cursor
,
490 struct callchain_node
*rnode
;
491 struct callchain_cursor_node
*node
;
492 struct rb_node
**p
= &root
->rb_root_in
.rb_node
;
493 struct rb_node
*parent
= NULL
;
495 node
= callchain_cursor_current(cursor
);
499 /* lookup in childrens */
504 rnode
= rb_entry(parent
, struct callchain_node
, rb_node_in
);
506 /* If at least first entry matches, rely to children */
507 ret
= append_chain(rnode
, cursor
, period
);
509 goto inc_children_hit
;
512 p
= &parent
->rb_left
;
514 p
= &parent
->rb_right
;
516 /* nothing in children, add to the current node */
517 rnode
= add_child(root
, cursor
, period
);
518 rb_link_node(&rnode
->rb_node_in
, parent
, p
);
519 rb_insert_color(&rnode
->rb_node_in
, &root
->rb_root_in
);
522 root
->children_hit
+= period
;
526 append_chain(struct callchain_node
*root
,
527 struct callchain_cursor
*cursor
,
530 struct callchain_list
*cnode
;
531 u64 start
= cursor
->pos
;
537 * Lookup in the current node
538 * If we have a symbol, then compare the start to match
539 * anywhere inside a function, unless function
542 list_for_each_entry(cnode
, &root
->val
, list
) {
543 struct callchain_cursor_node
*node
;
545 node
= callchain_cursor_current(cursor
);
549 cmp
= match_chain(node
, cnode
);
555 callchain_cursor_advance(cursor
);
558 /* matches not, relay no the parent */
560 WARN_ONCE(!cmp
, "Chain comparison error\n");
564 matches
= cursor
->pos
- start
;
566 /* we match only a part of the node. Split it and add the new chain */
567 if (matches
< root
->val_nr
) {
568 split_add_child(root
, cursor
, cnode
, start
, matches
, period
);
572 /* we match 100% of the path, increment the hit */
573 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
578 /* We match the node and still have a part remaining */
579 append_chain_children(root
, cursor
, period
);
584 int callchain_append(struct callchain_root
*root
,
585 struct callchain_cursor
*cursor
,
591 callchain_cursor_commit(cursor
);
593 append_chain_children(&root
->node
, cursor
, period
);
595 if (cursor
->nr
> root
->max_depth
)
596 root
->max_depth
= cursor
->nr
;
602 merge_chain_branch(struct callchain_cursor
*cursor
,
603 struct callchain_node
*dst
, struct callchain_node
*src
)
605 struct callchain_cursor_node
**old_last
= cursor
->last
;
606 struct callchain_node
*child
;
607 struct callchain_list
*list
, *next_list
;
609 int old_pos
= cursor
->nr
;
612 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
613 callchain_cursor_append(cursor
, list
->ip
,
614 list
->ms
.map
, list
->ms
.sym
);
615 list_del(&list
->list
);
620 callchain_cursor_commit(cursor
);
621 append_chain_children(dst
, cursor
, src
->hit
);
624 n
= rb_first(&src
->rb_root_in
);
626 child
= container_of(n
, struct callchain_node
, rb_node_in
);
628 rb_erase(&child
->rb_node_in
, &src
->rb_root_in
);
630 err
= merge_chain_branch(cursor
, dst
, child
);
637 cursor
->nr
= old_pos
;
638 cursor
->last
= old_last
;
643 int callchain_merge(struct callchain_cursor
*cursor
,
644 struct callchain_root
*dst
, struct callchain_root
*src
)
646 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
649 int callchain_cursor_append(struct callchain_cursor
*cursor
,
650 u64 ip
, struct map
*map
, struct symbol
*sym
)
652 struct callchain_cursor_node
*node
= *cursor
->last
;
655 node
= calloc(1, sizeof(*node
));
659 *cursor
->last
= node
;
668 cursor
->last
= &node
->next
;
673 int sample__resolve_callchain(struct perf_sample
*sample
, struct symbol
**parent
,
674 struct perf_evsel
*evsel
, struct addr_location
*al
,
677 if (sample
->callchain
== NULL
)
680 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
||
682 return thread__resolve_callchain(al
->thread
, evsel
, sample
,
683 parent
, al
, max_stack
);
688 int hist_entry__append_callchain(struct hist_entry
*he
, struct perf_sample
*sample
)
690 if (!symbol_conf
.use_callchain
|| sample
->callchain
== NULL
)
692 return callchain_append(he
->callchain
, &callchain_cursor
, sample
->period
);
695 int fill_callchain_info(struct addr_location
*al
, struct callchain_cursor_node
*node
,
696 bool hide_unresolved
)
701 al
->addr
= node
->map
->map_ip(node
->map
, node
->ip
);
705 if (al
->sym
== NULL
) {
712 if (al
->map
->groups
== &al
->machine
->kmaps
) {
713 if (machine__is_host(al
->machine
)) {
714 al
->cpumode
= PERF_RECORD_MISC_KERNEL
;
717 al
->cpumode
= PERF_RECORD_MISC_GUEST_KERNEL
;
721 if (machine__is_host(al
->machine
)) {
722 al
->cpumode
= PERF_RECORD_MISC_USER
;
724 } else if (perf_guest
) {
725 al
->cpumode
= PERF_RECORD_MISC_GUEST_USER
;
728 al
->cpumode
= PERF_RECORD_MISC_HYPERVISOR
;
737 char *callchain_list__sym_name(struct callchain_list
*cl
,
738 char *bf
, size_t bfsize
, bool show_dso
)
743 if (callchain_param
.key
== CCKEY_ADDRESS
&&
744 cl
->ms
.map
&& !cl
->srcline
)
745 cl
->srcline
= get_srcline(cl
->ms
.map
->dso
,
746 map__rip_2objdump(cl
->ms
.map
,
750 printed
= scnprintf(bf
, bfsize
, "%s %s",
751 cl
->ms
.sym
->name
, cl
->srcline
);
753 printed
= scnprintf(bf
, bfsize
, "%s", cl
->ms
.sym
->name
);
755 printed
= scnprintf(bf
, bfsize
, "%#" PRIx64
, cl
->ip
);
758 scnprintf(bf
+ printed
, bfsize
- printed
, " %s",
760 cl
->ms
.map
->dso
->short_name
:
766 static void free_callchain_node(struct callchain_node
*node
)
768 struct callchain_list
*list
, *tmp
;
769 struct callchain_node
*child
;
772 list_for_each_entry_safe(list
, tmp
, &node
->val
, list
) {
773 list_del(&list
->list
);
777 n
= rb_first(&node
->rb_root_in
);
779 child
= container_of(n
, struct callchain_node
, rb_node_in
);
781 rb_erase(&child
->rb_node_in
, &node
->rb_root_in
);
783 free_callchain_node(child
);
788 void free_callchain(struct callchain_root
*root
)
790 if (!symbol_conf
.use_callchain
)
793 free_callchain_node(&root
->node
);