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, bool allow_record_opt)
85 bool minpcnt_set = false;
86 bool record_opt_set = false;
87 bool try_stack_size = false;
89 symbol_conf.use_callchain = true;
94 while ((tok = strtok((char *)arg, ",")) != NULL) {
95 if (!strncmp(tok, "none", strlen(tok))) {
96 callchain_param.mode = CHAIN_NONE;
97 symbol_conf.use_callchain = false;
101 if (!parse_callchain_mode(tok) ||
102 !parse_callchain_order(tok) ||
103 !parse_callchain_sort_key(tok)) {
104 /* parsing ok - move on to the next */
105 try_stack_size = false;
107 } else if (allow_record_opt && !record_opt_set) {
108 if (parse_callchain_record(tok, &callchain_param))
111 /* assume that number followed by 'dwarf' is stack size */
112 if (callchain_param.record_mode == CALLCHAIN_DWARF)
113 try_stack_size = true;
115 record_opt_set = true;
120 if (try_stack_size) {
121 unsigned long size = 0;
123 if (get_stack_size(tok, &size) < 0)
125 callchain_param.dump_size = size;
126 try_stack_size = false;
127 } else if (!minpcnt_set) {
128 /* try to get the min percent */
129 callchain_param.min_percent = strtod(tok, &endptr);
134 /* try print limit at last */
135 callchain_param.print_limit = strtoul(tok, &endptr, 0);
143 if (callchain_register_param(&callchain_param) < 0) {
144 pr_err("Can't register callchain params\n");
150 int parse_callchain_report_opt(const char *arg)
152 return __parse_callchain_report_opt(arg, false);
155 int parse_callchain_top_opt(const char *arg)
157 return __parse_callchain_report_opt(arg, true);
160 int perf_callchain_config(const char *var, const char *value)
164 if (prefixcmp(var, "call-graph."))
166 var += sizeof("call-graph.") - 1;
168 if (!strcmp(var, "record-mode"))
169 return parse_callchain_record_opt(value, &callchain_param);
170 #ifdef HAVE_DWARF_UNWIND_SUPPORT
171 if (!strcmp(var, "dump-size")) {
172 unsigned long size = 0;
175 ret = get_stack_size(value, &size);
176 callchain_param.dump_size = size;
181 if (!strcmp(var, "print-type"))
182 return parse_callchain_mode(value);
183 if (!strcmp(var, "order"))
184 return parse_callchain_order(value);
185 if (!strcmp(var, "sort-key"))
186 return parse_callchain_sort_key(value);
187 if (!strcmp(var, "threshold")) {
188 callchain_param.min_percent = strtod(value, &endptr);
192 if (!strcmp(var, "print-limit")) {
193 callchain_param.print_limit = strtod(value, &endptr);
202 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
203 enum chain_mode mode)
205 struct rb_node **p = &root->rb_node;
206 struct rb_node *parent = NULL;
207 struct callchain_node *rnode;
208 u64 chain_cumul = callchain_cumul_hits(chain);
214 rnode = rb_entry(parent, struct callchain_node, rb_node);
215 rnode_cumul = callchain_cumul_hits(rnode);
219 if (rnode->hit < chain->hit)
224 case CHAIN_GRAPH_ABS: /* Falldown */
225 case CHAIN_GRAPH_REL:
226 if (rnode_cumul < chain_cumul)
237 rb_link_node(&chain->rb_node, parent, p);
238 rb_insert_color(&chain->rb_node, root);
242 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
246 struct callchain_node *child;
248 n = rb_first(&node->rb_root_in);
250 child = rb_entry(n, struct callchain_node, rb_node_in);
253 __sort_chain_flat(rb_root, child, min_hit);
256 if (node->hit && node->hit >= min_hit)
257 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
261 * Once we get every callchains from the stream, we can now
265 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
266 u64 min_hit, struct callchain_param *param __maybe_unused)
268 __sort_chain_flat(rb_root, &root->node, min_hit);
271 static void __sort_chain_graph_abs(struct callchain_node *node,
275 struct callchain_node *child;
277 node->rb_root = RB_ROOT;
278 n = rb_first(&node->rb_root_in);
281 child = rb_entry(n, struct callchain_node, rb_node_in);
284 __sort_chain_graph_abs(child, min_hit);
285 if (callchain_cumul_hits(child) >= min_hit)
286 rb_insert_callchain(&node->rb_root, child,
292 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
293 u64 min_hit, struct callchain_param *param __maybe_unused)
295 __sort_chain_graph_abs(&chain_root->node, min_hit);
296 rb_root->rb_node = chain_root->node.rb_root.rb_node;
299 static void __sort_chain_graph_rel(struct callchain_node *node,
303 struct callchain_node *child;
306 node->rb_root = RB_ROOT;
307 min_hit = ceil(node->children_hit * min_percent);
309 n = rb_first(&node->rb_root_in);
311 child = rb_entry(n, struct callchain_node, rb_node_in);
314 __sort_chain_graph_rel(child, min_percent);
315 if (callchain_cumul_hits(child) >= min_hit)
316 rb_insert_callchain(&node->rb_root, child,
322 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
323 u64 min_hit __maybe_unused, struct callchain_param *param)
325 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
326 rb_root->rb_node = chain_root->node.rb_root.rb_node;
329 int callchain_register_param(struct callchain_param *param)
331 switch (param->mode) {
332 case CHAIN_GRAPH_ABS:
333 param->sort = sort_chain_graph_abs;
335 case CHAIN_GRAPH_REL:
336 param->sort = sort_chain_graph_rel;
339 param->sort = sort_chain_flat;
349 * Create a child for a parent. If inherit_children, then the new child
350 * will become the new parent of it's parent children
352 static struct callchain_node *
353 create_child(struct callchain_node *parent, bool inherit_children)
355 struct callchain_node *new;
357 new = zalloc(sizeof(*new));
359 perror("not enough memory to create child for code path tree");
362 new->parent = parent;
363 INIT_LIST_HEAD(&new->val);
365 if (inherit_children) {
367 struct callchain_node *child;
369 new->rb_root_in = parent->rb_root_in;
370 parent->rb_root_in = RB_ROOT;
372 n = rb_first(&new->rb_root_in);
374 child = rb_entry(n, struct callchain_node, rb_node_in);
379 /* make it the first child */
380 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
381 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
389 * Fill the node with callchain values
392 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
394 struct callchain_cursor_node *cursor_node;
396 node->val_nr = cursor->nr - cursor->pos;
398 pr_warning("Warning: empty node in callchain tree\n");
400 cursor_node = callchain_cursor_current(cursor);
402 while (cursor_node) {
403 struct callchain_list *call;
405 call = zalloc(sizeof(*call));
407 perror("not enough memory for the code path tree");
410 call->ip = cursor_node->ip;
411 call->ms.sym = cursor_node->sym;
412 call->ms.map = cursor_node->map;
413 list_add_tail(&call->list, &node->val);
415 callchain_cursor_advance(cursor);
416 cursor_node = callchain_cursor_current(cursor);
420 static struct callchain_node *
421 add_child(struct callchain_node *parent,
422 struct callchain_cursor *cursor,
425 struct callchain_node *new;
427 new = create_child(parent, false);
428 fill_node(new, cursor);
430 new->children_hit = 0;
435 static s64 match_chain(struct callchain_cursor_node *node,
436 struct callchain_list *cnode)
438 struct symbol *sym = node->sym;
440 if (cnode->ms.sym && sym &&
441 callchain_param.key == CCKEY_FUNCTION)
442 return cnode->ms.sym->start - sym->start;
444 return cnode->ip - node->ip;
448 * Split the parent in two parts (a new child is created) and
449 * give a part of its callchain to the created child.
450 * Then create another child to host the given callchain of new branch
453 split_add_child(struct callchain_node *parent,
454 struct callchain_cursor *cursor,
455 struct callchain_list *to_split,
456 u64 idx_parents, u64 idx_local, u64 period)
458 struct callchain_node *new;
459 struct list_head *old_tail;
460 unsigned int idx_total = idx_parents + idx_local;
463 new = create_child(parent, true);
465 /* split the callchain and move a part to the new child */
466 old_tail = parent->val.prev;
467 list_del_range(&to_split->list, old_tail);
468 new->val.next = &to_split->list;
469 new->val.prev = old_tail;
470 to_split->list.prev = &new->val;
471 old_tail->next = &new->val;
474 new->hit = parent->hit;
475 new->children_hit = parent->children_hit;
476 parent->children_hit = callchain_cumul_hits(new);
477 new->val_nr = parent->val_nr - idx_local;
478 parent->val_nr = idx_local;
480 /* create a new child for the new branch if any */
481 if (idx_total < cursor->nr) {
482 struct callchain_node *first;
483 struct callchain_list *cnode;
484 struct callchain_cursor_node *node;
485 struct rb_node *p, **pp;
488 parent->children_hit += period;
490 node = callchain_cursor_current(cursor);
491 new = add_child(parent, cursor, period);
494 * This is second child since we moved parent's children
495 * to new (first) child above.
497 p = parent->rb_root_in.rb_node;
498 first = rb_entry(p, struct callchain_node, rb_node_in);
499 cnode = list_first_entry(&first->val, struct callchain_list,
502 if (match_chain(node, cnode) < 0)
507 rb_link_node(&new->rb_node_in, p, pp);
508 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
510 parent->hit = period;
515 append_chain(struct callchain_node *root,
516 struct callchain_cursor *cursor,
520 append_chain_children(struct callchain_node *root,
521 struct callchain_cursor *cursor,
524 struct callchain_node *rnode;
525 struct callchain_cursor_node *node;
526 struct rb_node **p = &root->rb_root_in.rb_node;
527 struct rb_node *parent = NULL;
529 node = callchain_cursor_current(cursor);
533 /* lookup in childrens */
538 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
540 /* If at least first entry matches, rely to children */
541 ret = append_chain(rnode, cursor, period);
543 goto inc_children_hit;
546 p = &parent->rb_left;
548 p = &parent->rb_right;
550 /* nothing in children, add to the current node */
551 rnode = add_child(root, cursor, period);
552 rb_link_node(&rnode->rb_node_in, parent, p);
553 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
556 root->children_hit += period;
560 append_chain(struct callchain_node *root,
561 struct callchain_cursor *cursor,
564 struct callchain_list *cnode;
565 u64 start = cursor->pos;
571 * Lookup in the current node
572 * If we have a symbol, then compare the start to match
573 * anywhere inside a function, unless function
576 list_for_each_entry(cnode, &root->val, list) {
577 struct callchain_cursor_node *node;
579 node = callchain_cursor_current(cursor);
583 cmp = match_chain(node, cnode);
589 callchain_cursor_advance(cursor);
592 /* matches not, relay no the parent */
594 WARN_ONCE(!cmp, "Chain comparison error\n");
598 matches = cursor->pos - start;
600 /* we match only a part of the node. Split it and add the new chain */
601 if (matches < root->val_nr) {
602 split_add_child(root, cursor, cnode, start, matches, period);
606 /* we match 100% of the path, increment the hit */
607 if (matches == root->val_nr && cursor->pos == cursor->nr) {
612 /* We match the node and still have a part remaining */
613 append_chain_children(root, cursor, period);
618 int callchain_append(struct callchain_root *root,
619 struct callchain_cursor *cursor,
625 callchain_cursor_commit(cursor);
627 append_chain_children(&root->node, cursor, period);
629 if (cursor->nr > root->max_depth)
630 root->max_depth = cursor->nr;
636 merge_chain_branch(struct callchain_cursor *cursor,
637 struct callchain_node *dst, struct callchain_node *src)
639 struct callchain_cursor_node **old_last = cursor->last;
640 struct callchain_node *child;
641 struct callchain_list *list, *next_list;
643 int old_pos = cursor->nr;
646 list_for_each_entry_safe(list, next_list, &src->val, list) {
647 callchain_cursor_append(cursor, list->ip,
648 list->ms.map, list->ms.sym);
649 list_del(&list->list);
654 callchain_cursor_commit(cursor);
655 append_chain_children(dst, cursor, src->hit);
658 n = rb_first(&src->rb_root_in);
660 child = container_of(n, struct callchain_node, rb_node_in);
662 rb_erase(&child->rb_node_in, &src->rb_root_in);
664 err = merge_chain_branch(cursor, dst, child);
671 cursor->nr = old_pos;
672 cursor->last = old_last;
677 int callchain_merge(struct callchain_cursor *cursor,
678 struct callchain_root *dst, struct callchain_root *src)
680 return merge_chain_branch(cursor, &dst->node, &src->node);
683 int callchain_cursor_append(struct callchain_cursor *cursor,
684 u64 ip, struct map *map, struct symbol *sym)
686 struct callchain_cursor_node *node = *cursor->last;
689 node = calloc(1, sizeof(*node));
693 *cursor->last = node;
702 cursor->last = &node->next;
707 int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
708 struct perf_evsel *evsel, struct addr_location *al,
711 if (sample->callchain == NULL)
714 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
716 return thread__resolve_callchain(al->thread, evsel, sample,
717 parent, al, max_stack);
722 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
724 if (!symbol_conf.use_callchain || sample->callchain == NULL)
726 return callchain_append(he->callchain, &callchain_cursor, sample->period);
729 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
730 bool hide_unresolved)
735 al->addr = node->map->map_ip(node->map, node->ip);
739 if (al->sym == NULL) {
746 if (al->map->groups == &al->machine->kmaps) {
747 if (machine__is_host(al->machine)) {
748 al->cpumode = PERF_RECORD_MISC_KERNEL;
751 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
755 if (machine__is_host(al->machine)) {
756 al->cpumode = PERF_RECORD_MISC_USER;
758 } else if (perf_guest) {
759 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
762 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
771 char *callchain_list__sym_name(struct callchain_list *cl,
772 char *bf, size_t bfsize, bool show_dso)
777 if (callchain_param.key == CCKEY_ADDRESS &&
778 cl->ms.map && !cl->srcline)
779 cl->srcline = get_srcline(cl->ms.map->dso,
780 map__rip_2objdump(cl->ms.map,
784 printed = scnprintf(bf, bfsize, "%s %s",
785 cl->ms.sym->name, cl->srcline);
787 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
789 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
792 scnprintf(bf + printed, bfsize - printed, " %s",
794 cl->ms.map->dso->short_name :
800 static void free_callchain_node(struct callchain_node *node)
802 struct callchain_list *list, *tmp;
803 struct callchain_node *child;
806 list_for_each_entry_safe(list, tmp, &node->val, list) {
807 list_del(&list->list);
811 n = rb_first(&node->rb_root_in);
813 child = container_of(n, struct callchain_node, rb_node_in);
815 rb_erase(&child->rb_node_in, &node->rb_root_in);
817 free_callchain_node(child);
822 void free_callchain(struct callchain_root *root)
824 if (!symbol_conf.use_callchain)
827 free_callchain_node(&root->node);