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 -g 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;
156 parse_callchain_report_opt(const char *arg)
160 bool minpcnt_set = false;
162 symbol_conf.use_callchain = true;
167 while ((tok = strtok((char *)arg, ",")) != NULL) {
168 if (!strncmp(tok, "none", strlen(tok))) {
169 callchain_param.mode = CHAIN_NONE;
170 symbol_conf.use_callchain = false;
174 if (!parse_callchain_mode(tok) ||
175 !parse_callchain_order(tok) ||
176 !parse_callchain_sort_key(tok)) {
177 /* parsing ok - move on to the next */
178 } else if (!minpcnt_set) {
179 /* try to get the min percent */
180 callchain_param.min_percent = strtod(tok, &endptr);
185 /* try print limit at last */
186 callchain_param.print_limit = strtoul(tok, &endptr, 0);
194 if (callchain_register_param(&callchain_param) < 0) {
195 pr_err("Can't register callchain params\n");
201 int perf_callchain_config(const char *var, const char *value)
205 if (prefixcmp(var, "call-graph."))
207 var += sizeof("call-graph.") - 1;
209 if (!strcmp(var, "record-mode"))
210 return parse_callchain_record_opt(value);
211 #ifdef HAVE_DWARF_UNWIND_SUPPORT
212 if (!strcmp(var, "dump-size")) {
213 unsigned long size = 0;
216 ret = get_stack_size(value, &size);
217 callchain_param.dump_size = size;
222 if (!strcmp(var, "print-type"))
223 return parse_callchain_mode(value);
224 if (!strcmp(var, "order"))
225 return parse_callchain_order(value);
226 if (!strcmp(var, "sort-key"))
227 return parse_callchain_sort_key(value);
228 if (!strcmp(var, "threshold")) {
229 callchain_param.min_percent = strtod(value, &endptr);
233 if (!strcmp(var, "print-limit")) {
234 callchain_param.print_limit = strtod(value, &endptr);
243 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
244 enum chain_mode mode)
246 struct rb_node **p = &root->rb_node;
247 struct rb_node *parent = NULL;
248 struct callchain_node *rnode;
249 u64 chain_cumul = callchain_cumul_hits(chain);
255 rnode = rb_entry(parent, struct callchain_node, rb_node);
256 rnode_cumul = callchain_cumul_hits(rnode);
260 if (rnode->hit < chain->hit)
265 case CHAIN_GRAPH_ABS: /* Falldown */
266 case CHAIN_GRAPH_REL:
267 if (rnode_cumul < chain_cumul)
278 rb_link_node(&chain->rb_node, parent, p);
279 rb_insert_color(&chain->rb_node, root);
283 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
287 struct callchain_node *child;
289 n = rb_first(&node->rb_root_in);
291 child = rb_entry(n, struct callchain_node, rb_node_in);
294 __sort_chain_flat(rb_root, child, min_hit);
297 if (node->hit && node->hit >= min_hit)
298 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
302 * Once we get every callchains from the stream, we can now
306 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
307 u64 min_hit, struct callchain_param *param __maybe_unused)
309 __sort_chain_flat(rb_root, &root->node, min_hit);
312 static void __sort_chain_graph_abs(struct callchain_node *node,
316 struct callchain_node *child;
318 node->rb_root = RB_ROOT;
319 n = rb_first(&node->rb_root_in);
322 child = rb_entry(n, struct callchain_node, rb_node_in);
325 __sort_chain_graph_abs(child, min_hit);
326 if (callchain_cumul_hits(child) >= min_hit)
327 rb_insert_callchain(&node->rb_root, child,
333 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
334 u64 min_hit, struct callchain_param *param __maybe_unused)
336 __sort_chain_graph_abs(&chain_root->node, min_hit);
337 rb_root->rb_node = chain_root->node.rb_root.rb_node;
340 static void __sort_chain_graph_rel(struct callchain_node *node,
344 struct callchain_node *child;
347 node->rb_root = RB_ROOT;
348 min_hit = ceil(node->children_hit * min_percent);
350 n = rb_first(&node->rb_root_in);
352 child = rb_entry(n, struct callchain_node, rb_node_in);
355 __sort_chain_graph_rel(child, min_percent);
356 if (callchain_cumul_hits(child) >= min_hit)
357 rb_insert_callchain(&node->rb_root, child,
363 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
364 u64 min_hit __maybe_unused, struct callchain_param *param)
366 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
367 rb_root->rb_node = chain_root->node.rb_root.rb_node;
370 int callchain_register_param(struct callchain_param *param)
372 switch (param->mode) {
373 case CHAIN_GRAPH_ABS:
374 param->sort = sort_chain_graph_abs;
376 case CHAIN_GRAPH_REL:
377 param->sort = sort_chain_graph_rel;
380 param->sort = sort_chain_flat;
390 * Create a child for a parent. If inherit_children, then the new child
391 * will become the new parent of it's parent children
393 static struct callchain_node *
394 create_child(struct callchain_node *parent, bool inherit_children)
396 struct callchain_node *new;
398 new = zalloc(sizeof(*new));
400 perror("not enough memory to create child for code path tree");
403 new->parent = parent;
404 INIT_LIST_HEAD(&new->val);
406 if (inherit_children) {
408 struct callchain_node *child;
410 new->rb_root_in = parent->rb_root_in;
411 parent->rb_root_in = RB_ROOT;
413 n = rb_first(&new->rb_root_in);
415 child = rb_entry(n, struct callchain_node, rb_node_in);
420 /* make it the first child */
421 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
422 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
430 * Fill the node with callchain values
433 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
435 struct callchain_cursor_node *cursor_node;
437 node->val_nr = cursor->nr - cursor->pos;
439 pr_warning("Warning: empty node in callchain tree\n");
441 cursor_node = callchain_cursor_current(cursor);
443 while (cursor_node) {
444 struct callchain_list *call;
446 call = zalloc(sizeof(*call));
448 perror("not enough memory for the code path tree");
451 call->ip = cursor_node->ip;
452 call->ms.sym = cursor_node->sym;
453 call->ms.map = cursor_node->map;
454 list_add_tail(&call->list, &node->val);
456 callchain_cursor_advance(cursor);
457 cursor_node = callchain_cursor_current(cursor);
461 static struct callchain_node *
462 add_child(struct callchain_node *parent,
463 struct callchain_cursor *cursor,
466 struct callchain_node *new;
468 new = create_child(parent, false);
469 fill_node(new, cursor);
471 new->children_hit = 0;
476 static s64 match_chain(struct callchain_cursor_node *node,
477 struct callchain_list *cnode)
479 struct symbol *sym = node->sym;
481 if (cnode->ms.sym && sym &&
482 callchain_param.key == CCKEY_FUNCTION)
483 return cnode->ms.sym->start - sym->start;
485 return cnode->ip - node->ip;
489 * Split the parent in two parts (a new child is created) and
490 * give a part of its callchain to the created child.
491 * Then create another child to host the given callchain of new branch
494 split_add_child(struct callchain_node *parent,
495 struct callchain_cursor *cursor,
496 struct callchain_list *to_split,
497 u64 idx_parents, u64 idx_local, u64 period)
499 struct callchain_node *new;
500 struct list_head *old_tail;
501 unsigned int idx_total = idx_parents + idx_local;
504 new = create_child(parent, true);
506 /* split the callchain and move a part to the new child */
507 old_tail = parent->val.prev;
508 list_del_range(&to_split->list, old_tail);
509 new->val.next = &to_split->list;
510 new->val.prev = old_tail;
511 to_split->list.prev = &new->val;
512 old_tail->next = &new->val;
515 new->hit = parent->hit;
516 new->children_hit = parent->children_hit;
517 parent->children_hit = callchain_cumul_hits(new);
518 new->val_nr = parent->val_nr - idx_local;
519 parent->val_nr = idx_local;
521 /* create a new child for the new branch if any */
522 if (idx_total < cursor->nr) {
523 struct callchain_node *first;
524 struct callchain_list *cnode;
525 struct callchain_cursor_node *node;
526 struct rb_node *p, **pp;
529 parent->children_hit += period;
531 node = callchain_cursor_current(cursor);
532 new = add_child(parent, cursor, period);
535 * This is second child since we moved parent's children
536 * to new (first) child above.
538 p = parent->rb_root_in.rb_node;
539 first = rb_entry(p, struct callchain_node, rb_node_in);
540 cnode = list_first_entry(&first->val, struct callchain_list,
543 if (match_chain(node, cnode) < 0)
548 rb_link_node(&new->rb_node_in, p, pp);
549 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
551 parent->hit = period;
556 append_chain(struct callchain_node *root,
557 struct callchain_cursor *cursor,
561 append_chain_children(struct callchain_node *root,
562 struct callchain_cursor *cursor,
565 struct callchain_node *rnode;
566 struct callchain_cursor_node *node;
567 struct rb_node **p = &root->rb_root_in.rb_node;
568 struct rb_node *parent = NULL;
570 node = callchain_cursor_current(cursor);
574 /* lookup in childrens */
579 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
581 /* If at least first entry matches, rely to children */
582 ret = append_chain(rnode, cursor, period);
584 goto inc_children_hit;
587 p = &parent->rb_left;
589 p = &parent->rb_right;
591 /* nothing in children, add to the current node */
592 rnode = add_child(root, cursor, period);
593 rb_link_node(&rnode->rb_node_in, parent, p);
594 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
597 root->children_hit += period;
601 append_chain(struct callchain_node *root,
602 struct callchain_cursor *cursor,
605 struct callchain_list *cnode;
606 u64 start = cursor->pos;
612 * Lookup in the current node
613 * If we have a symbol, then compare the start to match
614 * anywhere inside a function, unless function
617 list_for_each_entry(cnode, &root->val, list) {
618 struct callchain_cursor_node *node;
620 node = callchain_cursor_current(cursor);
624 cmp = match_chain(node, cnode);
630 callchain_cursor_advance(cursor);
633 /* matches not, relay no the parent */
635 WARN_ONCE(!cmp, "Chain comparison error\n");
639 matches = cursor->pos - start;
641 /* we match only a part of the node. Split it and add the new chain */
642 if (matches < root->val_nr) {
643 split_add_child(root, cursor, cnode, start, matches, period);
647 /* we match 100% of the path, increment the hit */
648 if (matches == root->val_nr && cursor->pos == cursor->nr) {
653 /* We match the node and still have a part remaining */
654 append_chain_children(root, cursor, period);
659 int callchain_append(struct callchain_root *root,
660 struct callchain_cursor *cursor,
666 callchain_cursor_commit(cursor);
668 append_chain_children(&root->node, cursor, period);
670 if (cursor->nr > root->max_depth)
671 root->max_depth = cursor->nr;
677 merge_chain_branch(struct callchain_cursor *cursor,
678 struct callchain_node *dst, struct callchain_node *src)
680 struct callchain_cursor_node **old_last = cursor->last;
681 struct callchain_node *child;
682 struct callchain_list *list, *next_list;
684 int old_pos = cursor->nr;
687 list_for_each_entry_safe(list, next_list, &src->val, list) {
688 callchain_cursor_append(cursor, list->ip,
689 list->ms.map, list->ms.sym);
690 list_del(&list->list);
695 callchain_cursor_commit(cursor);
696 append_chain_children(dst, cursor, src->hit);
699 n = rb_first(&src->rb_root_in);
701 child = container_of(n, struct callchain_node, rb_node_in);
703 rb_erase(&child->rb_node_in, &src->rb_root_in);
705 err = merge_chain_branch(cursor, dst, child);
712 cursor->nr = old_pos;
713 cursor->last = old_last;
718 int callchain_merge(struct callchain_cursor *cursor,
719 struct callchain_root *dst, struct callchain_root *src)
721 return merge_chain_branch(cursor, &dst->node, &src->node);
724 int callchain_cursor_append(struct callchain_cursor *cursor,
725 u64 ip, struct map *map, struct symbol *sym)
727 struct callchain_cursor_node *node = *cursor->last;
730 node = calloc(1, sizeof(*node));
734 *cursor->last = node;
743 cursor->last = &node->next;
748 int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
749 struct perf_evsel *evsel, struct addr_location *al,
752 if (sample->callchain == NULL)
755 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
757 return machine__resolve_callchain(al->machine, evsel, al->thread,
758 sample, parent, al, max_stack);
763 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
765 if (!symbol_conf.use_callchain || sample->callchain == NULL)
767 return callchain_append(he->callchain, &callchain_cursor, sample->period);
770 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
771 bool hide_unresolved)
776 al->addr = node->map->map_ip(node->map, node->ip);
780 if (al->sym == NULL) {
787 if (al->map->groups == &al->machine->kmaps) {
788 if (machine__is_host(al->machine)) {
789 al->cpumode = PERF_RECORD_MISC_KERNEL;
792 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
796 if (machine__is_host(al->machine)) {
797 al->cpumode = PERF_RECORD_MISC_USER;
799 } else if (perf_guest) {
800 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
803 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;