1 // SPDX-License-Identifier: GPL-2.0
5 * Handle the callchains from the stream in an ad-hoc radix tree and then
6 * sort them in an rbtree.
8 * Using a radix for code path provides a fast retrieval and factorizes
9 * memory use. Also that lets us use the paths in a hierarchical graph view.
19 #include <linux/string.h>
20 #include <linux/zalloc.h>
31 #include "callchain.h"
37 #define CALLCHAIN_PARAM_DEFAULT \
38 .mode = CHAIN_GRAPH_ABS, \
40 .order = ORDER_CALLEE, \
41 .key = CCKEY_FUNCTION, \
42 .value = CCVAL_PERCENT, \
44 struct callchain_param callchain_param = {
45 CALLCHAIN_PARAM_DEFAULT
49 * Are there any events usind DWARF callchains?
53 * -e cycles/call-graph=dwarf/
55 bool dwarf_callchain_users;
57 struct callchain_param callchain_param_default = {
58 CALLCHAIN_PARAM_DEFAULT
61 __thread struct callchain_cursor callchain_cursor;
63 int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
65 return parse_callchain_record(arg, param);
68 static int parse_callchain_mode(const char *value)
70 if (!strncmp(value, "graph", strlen(value))) {
71 callchain_param.mode = CHAIN_GRAPH_ABS;
74 if (!strncmp(value, "flat", strlen(value))) {
75 callchain_param.mode = CHAIN_FLAT;
78 if (!strncmp(value, "fractal", strlen(value))) {
79 callchain_param.mode = CHAIN_GRAPH_REL;
82 if (!strncmp(value, "folded", strlen(value))) {
83 callchain_param.mode = CHAIN_FOLDED;
89 static int parse_callchain_order(const char *value)
91 if (!strncmp(value, "caller", strlen(value))) {
92 callchain_param.order = ORDER_CALLER;
93 callchain_param.order_set = true;
96 if (!strncmp(value, "callee", strlen(value))) {
97 callchain_param.order = ORDER_CALLEE;
98 callchain_param.order_set = true;
104 static int parse_callchain_sort_key(const char *value)
106 if (!strncmp(value, "function", strlen(value))) {
107 callchain_param.key = CCKEY_FUNCTION;
110 if (!strncmp(value, "address", strlen(value))) {
111 callchain_param.key = CCKEY_ADDRESS;
114 if (!strncmp(value, "srcline", strlen(value))) {
115 callchain_param.key = CCKEY_SRCLINE;
118 if (!strncmp(value, "branch", strlen(value))) {
119 callchain_param.branch_callstack = 1;
125 static int parse_callchain_value(const char *value)
127 if (!strncmp(value, "percent", strlen(value))) {
128 callchain_param.value = CCVAL_PERCENT;
131 if (!strncmp(value, "period", strlen(value))) {
132 callchain_param.value = CCVAL_PERIOD;
135 if (!strncmp(value, "count", strlen(value))) {
136 callchain_param.value = CCVAL_COUNT;
142 static int get_stack_size(const char *str, unsigned long *_size)
146 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
148 size = strtoul(str, &endptr, 0);
154 size = round_up(size, sizeof(u64));
155 if (!size || size > max_size)
163 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
169 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
172 char *endptr, *saveptr = NULL;
173 bool minpcnt_set = false;
174 bool record_opt_set = false;
175 bool try_stack_size = false;
177 callchain_param.enabled = true;
178 symbol_conf.use_callchain = true;
183 while ((tok = strtok_r((char *)arg, ",", &saveptr)) != NULL) {
184 if (!strncmp(tok, "none", strlen(tok))) {
185 callchain_param.mode = CHAIN_NONE;
186 callchain_param.enabled = false;
187 symbol_conf.use_callchain = false;
191 if (!parse_callchain_mode(tok) ||
192 !parse_callchain_order(tok) ||
193 !parse_callchain_sort_key(tok) ||
194 !parse_callchain_value(tok)) {
195 /* parsing ok - move on to the next */
196 try_stack_size = false;
198 } else if (allow_record_opt && !record_opt_set) {
199 if (parse_callchain_record(tok, &callchain_param))
202 /* assume that number followed by 'dwarf' is stack size */
203 if (callchain_param.record_mode == CALLCHAIN_DWARF)
204 try_stack_size = true;
206 record_opt_set = true;
211 if (try_stack_size) {
212 unsigned long size = 0;
214 if (get_stack_size(tok, &size) < 0)
216 callchain_param.dump_size = size;
217 try_stack_size = false;
218 } else if (!minpcnt_set) {
219 /* try to get the min percent */
220 callchain_param.min_percent = strtod(tok, &endptr);
225 /* try print limit at last */
226 callchain_param.print_limit = strtoul(tok, &endptr, 0);
234 if (callchain_register_param(&callchain_param) < 0) {
235 pr_err("Can't register callchain params\n");
241 int parse_callchain_report_opt(const char *arg)
243 return __parse_callchain_report_opt(arg, false);
246 int parse_callchain_top_opt(const char *arg)
248 return __parse_callchain_report_opt(arg, true);
251 int parse_callchain_record(const char *arg, struct callchain_param *param)
253 char *tok, *name, *saveptr = NULL;
257 /* We need buffer that we know we can write to. */
258 buf = malloc(strlen(arg) + 1);
264 tok = strtok_r((char *)buf, ",", &saveptr);
265 name = tok ? : (char *)buf;
268 /* Framepointer style */
269 if (!strncmp(name, "fp", sizeof("fp"))) {
271 param->record_mode = CALLCHAIN_FP;
273 tok = strtok_r(NULL, ",", &saveptr);
277 size = strtoul(tok, &name, 0);
278 if (size < (unsigned) sysctl__max_stack())
279 param->max_stack = size;
284 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
285 const unsigned long default_stack_dump_size = 8192;
288 param->record_mode = CALLCHAIN_DWARF;
289 param->dump_size = default_stack_dump_size;
290 dwarf_callchain_users = true;
292 tok = strtok_r(NULL, ",", &saveptr);
294 unsigned long size = 0;
296 ret = get_stack_size(tok, &size);
297 param->dump_size = size;
299 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
300 if (!strtok_r(NULL, ",", &saveptr)) {
301 param->record_mode = CALLCHAIN_LBR;
304 pr_err("callchain: No more arguments "
305 "needed for --call-graph lbr\n");
308 pr_err("callchain: Unknown --call-graph option "
319 int perf_callchain_config(const char *var, const char *value)
323 if (!strstarts(var, "call-graph."))
325 var += sizeof("call-graph.") - 1;
327 if (!strcmp(var, "record-mode"))
328 return parse_callchain_record_opt(value, &callchain_param);
329 if (!strcmp(var, "dump-size")) {
330 unsigned long size = 0;
333 ret = get_stack_size(value, &size);
334 callchain_param.dump_size = size;
338 if (!strcmp(var, "print-type")){
340 ret = parse_callchain_mode(value);
342 pr_err("Invalid callchain mode: %s\n", value);
345 if (!strcmp(var, "order")){
347 ret = parse_callchain_order(value);
349 pr_err("Invalid callchain order: %s\n", value);
352 if (!strcmp(var, "sort-key")){
354 ret = parse_callchain_sort_key(value);
356 pr_err("Invalid callchain sort key: %s\n", value);
359 if (!strcmp(var, "threshold")) {
360 callchain_param.min_percent = strtod(value, &endptr);
361 if (value == endptr) {
362 pr_err("Invalid callchain threshold: %s\n", value);
366 if (!strcmp(var, "print-limit")) {
367 callchain_param.print_limit = strtod(value, &endptr);
368 if (value == endptr) {
369 pr_err("Invalid callchain print limit: %s\n", value);
378 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
379 enum chain_mode mode)
381 struct rb_node **p = &root->rb_node;
382 struct rb_node *parent = NULL;
383 struct callchain_node *rnode;
384 u64 chain_cumul = callchain_cumul_hits(chain);
390 rnode = rb_entry(parent, struct callchain_node, rb_node);
391 rnode_cumul = callchain_cumul_hits(rnode);
396 if (rnode->hit < chain->hit)
401 case CHAIN_GRAPH_ABS: /* Falldown */
402 case CHAIN_GRAPH_REL:
403 if (rnode_cumul < chain_cumul)
414 rb_link_node(&chain->rb_node, parent, p);
415 rb_insert_color(&chain->rb_node, root);
419 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
423 struct callchain_node *child;
425 n = rb_first(&node->rb_root_in);
427 child = rb_entry(n, struct callchain_node, rb_node_in);
430 __sort_chain_flat(rb_root, child, min_hit);
433 if (node->hit && node->hit >= min_hit)
434 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
438 * Once we get every callchains from the stream, we can now
442 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
443 u64 min_hit, struct callchain_param *param __maybe_unused)
446 __sort_chain_flat(rb_root, &root->node, min_hit);
449 static void __sort_chain_graph_abs(struct callchain_node *node,
453 struct callchain_node *child;
455 node->rb_root = RB_ROOT;
456 n = rb_first(&node->rb_root_in);
459 child = rb_entry(n, struct callchain_node, rb_node_in);
462 __sort_chain_graph_abs(child, min_hit);
463 if (callchain_cumul_hits(child) >= min_hit)
464 rb_insert_callchain(&node->rb_root, child,
470 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
471 u64 min_hit, struct callchain_param *param __maybe_unused)
473 __sort_chain_graph_abs(&chain_root->node, min_hit);
474 rb_root->rb_node = chain_root->node.rb_root.rb_node;
477 static void __sort_chain_graph_rel(struct callchain_node *node,
481 struct callchain_node *child;
484 node->rb_root = RB_ROOT;
485 min_hit = ceil(node->children_hit * min_percent);
487 n = rb_first(&node->rb_root_in);
489 child = rb_entry(n, struct callchain_node, rb_node_in);
492 __sort_chain_graph_rel(child, min_percent);
493 if (callchain_cumul_hits(child) >= min_hit)
494 rb_insert_callchain(&node->rb_root, child,
500 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
501 u64 min_hit __maybe_unused, struct callchain_param *param)
503 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
504 rb_root->rb_node = chain_root->node.rb_root.rb_node;
507 int callchain_register_param(struct callchain_param *param)
509 switch (param->mode) {
510 case CHAIN_GRAPH_ABS:
511 param->sort = sort_chain_graph_abs;
513 case CHAIN_GRAPH_REL:
514 param->sort = sort_chain_graph_rel;
518 param->sort = sort_chain_flat;
528 * Create a child for a parent. If inherit_children, then the new child
529 * will become the new parent of it's parent children
531 static struct callchain_node *
532 create_child(struct callchain_node *parent, bool inherit_children)
534 struct callchain_node *new;
536 new = zalloc(sizeof(*new));
538 perror("not enough memory to create child for code path tree");
541 new->parent = parent;
542 INIT_LIST_HEAD(&new->val);
543 INIT_LIST_HEAD(&new->parent_val);
545 if (inherit_children) {
547 struct callchain_node *child;
549 new->rb_root_in = parent->rb_root_in;
550 parent->rb_root_in = RB_ROOT;
552 n = rb_first(&new->rb_root_in);
554 child = rb_entry(n, struct callchain_node, rb_node_in);
559 /* make it the first child */
560 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
561 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
569 * Fill the node with callchain values
572 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
574 struct callchain_cursor_node *cursor_node;
576 node->val_nr = cursor->nr - cursor->pos;
578 pr_warning("Warning: empty node in callchain tree\n");
580 cursor_node = callchain_cursor_current(cursor);
582 while (cursor_node) {
583 struct callchain_list *call;
585 call = zalloc(sizeof(*call));
587 perror("not enough memory for the code path tree");
590 call->ip = cursor_node->ip;
591 call->ms = cursor_node->ms;
592 call->ms.map = map__get(call->ms.map);
593 call->srcline = cursor_node->srcline;
595 if (cursor_node->branch) {
596 call->branch_count = 1;
598 if (cursor_node->branch_from) {
600 * branch_from is set with value somewhere else
601 * to imply it's "to" of a branch.
603 call->brtype_stat.branch_to = true;
605 if (cursor_node->branch_flags.predicted)
606 call->predicted_count = 1;
608 if (cursor_node->branch_flags.abort)
609 call->abort_count = 1;
611 branch_type_count(&call->brtype_stat,
612 &cursor_node->branch_flags,
613 cursor_node->branch_from,
617 * It's "from" of a branch
619 call->brtype_stat.branch_to = false;
621 cursor_node->branch_flags.cycles;
622 call->iter_count = cursor_node->nr_loop_iter;
623 call->iter_cycles = cursor_node->iter_cycles;
627 list_add_tail(&call->list, &node->val);
629 callchain_cursor_advance(cursor);
630 cursor_node = callchain_cursor_current(cursor);
635 static struct callchain_node *
636 add_child(struct callchain_node *parent,
637 struct callchain_cursor *cursor,
640 struct callchain_node *new;
642 new = create_child(parent, false);
646 if (fill_node(new, cursor) < 0) {
647 struct callchain_list *call, *tmp;
649 list_for_each_entry_safe(call, tmp, &new->val, list) {
650 list_del_init(&call->list);
651 map__zput(call->ms.map);
658 new->children_hit = 0;
660 new->children_count = 0;
672 static enum match_result match_chain_strings(const char *left,
675 enum match_result ret = MATCH_EQ;
679 cmp = strcmp(left, right);
680 else if (!left && right)
682 else if (left && !right)
688 ret = cmp < 0 ? MATCH_LT : MATCH_GT;
694 * We need to always use relative addresses because we're aggregating
695 * callchains from multiple threads, i.e. different address spaces, so
696 * comparing absolute addresses make no sense as a symbol in a DSO may end up
697 * in a different address when used in a different binary or even the same
698 * binary but with some sort of address randomization technique, thus we need
699 * to compare just relative addresses. -acme
701 static enum match_result match_chain_dso_addresses(struct map *left_map, u64 left_ip,
702 struct map *right_map, u64 right_ip)
704 struct dso *left_dso = left_map ? map__dso(left_map) : NULL;
705 struct dso *right_dso = right_map ? map__dso(right_map) : NULL;
707 if (left_dso != right_dso)
708 return left_dso < right_dso ? MATCH_LT : MATCH_GT;
710 if (left_ip != right_ip)
711 return left_ip < right_ip ? MATCH_LT : MATCH_GT;
716 static enum match_result match_chain(struct callchain_cursor_node *node,
717 struct callchain_list *cnode)
719 enum match_result match = MATCH_ERROR;
721 switch (callchain_param.key) {
723 match = match_chain_strings(cnode->srcline, node->srcline);
724 if (match != MATCH_ERROR)
726 /* otherwise fall-back to symbol-based comparison below */
729 if (node->ms.sym && cnode->ms.sym) {
731 * Compare inlined frames based on their symbol name
732 * because different inlined frames will have the same
733 * symbol start. Otherwise do a faster comparison based
734 * on the symbol start address.
736 if (cnode->ms.sym->inlined || node->ms.sym->inlined) {
737 match = match_chain_strings(cnode->ms.sym->name,
739 if (match != MATCH_ERROR)
742 match = match_chain_dso_addresses(cnode->ms.map, cnode->ms.sym->start,
743 node->ms.map, node->ms.sym->start);
747 /* otherwise fall-back to IP-based comparison below */
751 match = match_chain_dso_addresses(cnode->ms.map, cnode->ip, node->ms.map, node->ip);
755 if (match == MATCH_EQ && node->branch) {
756 cnode->branch_count++;
758 if (node->branch_from) {
760 * It's "to" of a branch
762 cnode->brtype_stat.branch_to = true;
764 if (node->branch_flags.predicted)
765 cnode->predicted_count++;
767 if (node->branch_flags.abort)
768 cnode->abort_count++;
770 branch_type_count(&cnode->brtype_stat,
776 * It's "from" of a branch
778 cnode->brtype_stat.branch_to = false;
779 cnode->cycles_count += node->branch_flags.cycles;
780 cnode->iter_count += node->nr_loop_iter;
781 cnode->iter_cycles += node->iter_cycles;
790 * Split the parent in two parts (a new child is created) and
791 * give a part of its callchain to the created child.
792 * Then create another child to host the given callchain of new branch
795 split_add_child(struct callchain_node *parent,
796 struct callchain_cursor *cursor,
797 struct callchain_list *to_split,
798 u64 idx_parents, u64 idx_local, u64 period)
800 struct callchain_node *new;
801 struct list_head *old_tail;
802 unsigned int idx_total = idx_parents + idx_local;
805 new = create_child(parent, true);
809 /* split the callchain and move a part to the new child */
810 old_tail = parent->val.prev;
811 list_del_range(&to_split->list, old_tail);
812 new->val.next = &to_split->list;
813 new->val.prev = old_tail;
814 to_split->list.prev = &new->val;
815 old_tail->next = &new->val;
818 new->hit = parent->hit;
819 new->children_hit = parent->children_hit;
820 parent->children_hit = callchain_cumul_hits(new);
821 new->val_nr = parent->val_nr - idx_local;
822 parent->val_nr = idx_local;
823 new->count = parent->count;
824 new->children_count = parent->children_count;
825 parent->children_count = callchain_cumul_counts(new);
827 /* create a new child for the new branch if any */
828 if (idx_total < cursor->nr) {
829 struct callchain_node *first;
830 struct callchain_list *cnode;
831 struct callchain_cursor_node *node;
832 struct rb_node *p, **pp;
835 parent->children_hit += period;
837 parent->children_count += 1;
839 node = callchain_cursor_current(cursor);
840 new = add_child(parent, cursor, period);
845 * This is second child since we moved parent's children
846 * to new (first) child above.
848 p = parent->rb_root_in.rb_node;
849 first = rb_entry(p, struct callchain_node, rb_node_in);
850 cnode = list_first_entry(&first->val, struct callchain_list,
853 if (match_chain(node, cnode) == MATCH_LT)
858 rb_link_node(&new->rb_node_in, p, pp);
859 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
861 parent->hit = period;
867 static enum match_result
868 append_chain(struct callchain_node *root,
869 struct callchain_cursor *cursor,
873 append_chain_children(struct callchain_node *root,
874 struct callchain_cursor *cursor,
877 struct callchain_node *rnode;
878 struct callchain_cursor_node *node;
879 struct rb_node **p = &root->rb_root_in.rb_node;
880 struct rb_node *parent = NULL;
882 node = callchain_cursor_current(cursor);
886 /* lookup in children */
888 enum match_result ret;
891 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
893 /* If at least first entry matches, rely to children */
894 ret = append_chain(rnode, cursor, period);
896 goto inc_children_hit;
897 if (ret == MATCH_ERROR)
901 p = &parent->rb_left;
903 p = &parent->rb_right;
905 /* nothing in children, add to the current node */
906 rnode = add_child(root, cursor, period);
910 rb_link_node(&rnode->rb_node_in, parent, p);
911 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
914 root->children_hit += period;
915 root->children_count++;
919 static enum match_result
920 append_chain(struct callchain_node *root,
921 struct callchain_cursor *cursor,
924 struct callchain_list *cnode;
925 u64 start = cursor->pos;
928 enum match_result cmp = MATCH_ERROR;
931 * Lookup in the current node
932 * If we have a symbol, then compare the start to match
933 * anywhere inside a function, unless function
936 list_for_each_entry(cnode, &root->val, list) {
937 struct callchain_cursor_node *node;
939 node = callchain_cursor_current(cursor);
943 cmp = match_chain(node, cnode);
949 callchain_cursor_advance(cursor);
952 /* matches not, relay no the parent */
954 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
958 matches = cursor->pos - start;
960 /* we match only a part of the node. Split it and add the new chain */
961 if (matches < root->val_nr) {
962 if (split_add_child(root, cursor, cnode, start, matches,
969 /* we match 100% of the path, increment the hit */
970 if (matches == root->val_nr && cursor->pos == cursor->nr) {
976 /* We match the node and still have a part remaining */
977 if (append_chain_children(root, cursor, period) < 0)
983 int callchain_append(struct callchain_root *root,
984 struct callchain_cursor *cursor,
990 callchain_cursor_commit(cursor);
992 if (append_chain_children(&root->node, cursor, period) < 0)
995 if (cursor->nr > root->max_depth)
996 root->max_depth = cursor->nr;
1002 merge_chain_branch(struct callchain_cursor *cursor,
1003 struct callchain_node *dst, struct callchain_node *src)
1005 struct callchain_cursor_node **old_last = cursor->last;
1006 struct callchain_node *child;
1007 struct callchain_list *list, *next_list;
1009 int old_pos = cursor->nr;
1012 list_for_each_entry_safe(list, next_list, &src->val, list) {
1013 callchain_cursor_append(cursor, list->ip, &list->ms,
1014 false, NULL, 0, 0, 0, list->srcline);
1015 list_del_init(&list->list);
1016 map__zput(list->ms.map);
1021 callchain_cursor_commit(cursor);
1022 if (append_chain_children(dst, cursor, src->hit) < 0)
1026 n = rb_first(&src->rb_root_in);
1028 child = container_of(n, struct callchain_node, rb_node_in);
1030 rb_erase(&child->rb_node_in, &src->rb_root_in);
1032 err = merge_chain_branch(cursor, dst, child);
1039 cursor->nr = old_pos;
1040 cursor->last = old_last;
1045 int callchain_merge(struct callchain_cursor *cursor,
1046 struct callchain_root *dst, struct callchain_root *src)
1048 return merge_chain_branch(cursor, &dst->node, &src->node);
1051 int callchain_cursor_append(struct callchain_cursor *cursor,
1052 u64 ip, struct map_symbol *ms,
1053 bool branch, struct branch_flags *flags,
1054 int nr_loop_iter, u64 iter_cycles, u64 branch_from,
1055 const char *srcline)
1057 struct callchain_cursor_node *node = *cursor->last;
1060 node = calloc(1, sizeof(*node));
1064 *cursor->last = node;
1068 map__zput(node->ms.map);
1070 node->ms.map = map__get(node->ms.map);
1071 node->branch = branch;
1072 node->nr_loop_iter = nr_loop_iter;
1073 node->iter_cycles = iter_cycles;
1074 node->srcline = srcline;
1077 memcpy(&node->branch_flags, flags,
1078 sizeof(struct branch_flags));
1080 node->branch_from = branch_from;
1083 cursor->last = &node->next;
1088 int sample__resolve_callchain(struct perf_sample *sample,
1089 struct callchain_cursor *cursor, struct symbol **parent,
1090 struct evsel *evsel, struct addr_location *al,
1093 if (sample->callchain == NULL && !symbol_conf.show_branchflag_count)
1096 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
1097 perf_hpp_list.parent || symbol_conf.show_branchflag_count) {
1098 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
1099 parent, al, max_stack);
1104 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
1106 if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
1107 !symbol_conf.show_branchflag_count)
1109 return callchain_append(he->callchain, &callchain_cursor, sample->period);
1112 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
1113 bool hide_unresolved)
1115 struct machine *machine = maps__machine(node->ms.maps);
1117 al->maps = node->ms.maps;
1119 al->map = map__get(node->ms.map);
1120 al->sym = node->ms.sym;
1121 al->srcline = node->srcline;
1122 al->addr = node->ip;
1124 if (al->sym == NULL) {
1125 if (hide_unresolved)
1127 if (al->map == NULL)
1130 if (al->maps == machine__kernel_maps(machine)) {
1131 if (machine__is_host(machine)) {
1132 al->cpumode = PERF_RECORD_MISC_KERNEL;
1135 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
1139 if (machine__is_host(machine)) {
1140 al->cpumode = PERF_RECORD_MISC_USER;
1142 } else if (perf_guest) {
1143 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
1146 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
1155 char *callchain_list__sym_name(struct callchain_list *cl,
1156 char *bf, size_t bfsize, bool show_dso)
1158 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1159 bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
1163 const char *inlined = cl->ms.sym->inlined ? " (inlined)" : "";
1165 if (show_srcline && cl->srcline)
1166 printed = scnprintf(bf, bfsize, "%s %s%s",
1167 cl->ms.sym->name, cl->srcline,
1170 printed = scnprintf(bf, bfsize, "%s%s",
1171 cl->ms.sym->name, inlined);
1173 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
1176 scnprintf(bf + printed, bfsize - printed, " %s",
1178 map__dso(cl->ms.map)->short_name :
1184 char *callchain_node__scnprintf_value(struct callchain_node *node,
1185 char *bf, size_t bfsize, u64 total)
1187 double percent = 0.0;
1188 u64 period = callchain_cumul_hits(node);
1189 unsigned count = callchain_cumul_counts(node);
1191 if (callchain_param.mode == CHAIN_FOLDED) {
1193 count = node->count;
1196 switch (callchain_param.value) {
1198 scnprintf(bf, bfsize, "%"PRIu64, period);
1201 scnprintf(bf, bfsize, "%u", count);
1206 percent = period * 100.0 / total;
1207 scnprintf(bf, bfsize, "%.2f%%", percent);
1213 int callchain_node__fprintf_value(struct callchain_node *node,
1214 FILE *fp, u64 total)
1216 double percent = 0.0;
1217 u64 period = callchain_cumul_hits(node);
1218 unsigned count = callchain_cumul_counts(node);
1220 if (callchain_param.mode == CHAIN_FOLDED) {
1222 count = node->count;
1225 switch (callchain_param.value) {
1227 return fprintf(fp, "%"PRIu64, period);
1229 return fprintf(fp, "%u", count);
1233 percent = period * 100.0 / total;
1234 return percent_color_fprintf(fp, "%.2f%%", percent);
1239 static void callchain_counts_value(struct callchain_node *node,
1240 u64 *branch_count, u64 *predicted_count,
1241 u64 *abort_count, u64 *cycles_count)
1243 struct callchain_list *clist;
1245 list_for_each_entry(clist, &node->val, list) {
1247 *branch_count += clist->branch_count;
1249 if (predicted_count)
1250 *predicted_count += clist->predicted_count;
1253 *abort_count += clist->abort_count;
1256 *cycles_count += clist->cycles_count;
1260 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1262 u64 *predicted_count,
1266 struct callchain_node *child;
1269 n = rb_first(&node->rb_root_in);
1271 child = rb_entry(n, struct callchain_node, rb_node_in);
1274 callchain_node_branch_counts_cumul(child, branch_count,
1279 callchain_counts_value(child, branch_count,
1280 predicted_count, abort_count,
1287 int callchain_branch_counts(struct callchain_root *root,
1288 u64 *branch_count, u64 *predicted_count,
1289 u64 *abort_count, u64 *cycles_count)
1294 if (predicted_count)
1295 *predicted_count = 0;
1303 return callchain_node_branch_counts_cumul(&root->node,
1310 static int count_pri64_printf(int idx, const char *str, u64 value, char *bf, int bfsize)
1312 return scnprintf(bf, bfsize, "%s%s:%" PRId64 "", (idx) ? " " : " (", str, value);
1315 static int count_float_printf(int idx, const char *str, float value,
1316 char *bf, int bfsize, float threshold)
1318 if (threshold != 0.0 && value < threshold)
1321 return scnprintf(bf, bfsize, "%s%s:%.1f%%", (idx) ? " " : " (", str, value);
1324 static int branch_to_str(char *bf, int bfsize,
1325 u64 branch_count, u64 predicted_count,
1327 struct branch_type_stat *brtype_stat)
1331 printed = branch_type_str(brtype_stat, bf, bfsize);
1335 if (predicted_count < branch_count) {
1336 printed += count_float_printf(i++, "predicted",
1337 predicted_count * 100.0 / branch_count,
1338 bf + printed, bfsize - printed, 0.0);
1342 printed += count_float_printf(i++, "abort",
1343 abort_count * 100.0 / branch_count,
1344 bf + printed, bfsize - printed, 0.1);
1348 printed += scnprintf(bf + printed, bfsize - printed, ")");
1353 static int branch_from_str(char *bf, int bfsize,
1355 u64 cycles_count, u64 iter_count,
1356 u64 iter_cycles, u64 from_count)
1358 int printed = 0, i = 0;
1361 cycles = cycles_count / branch_count;
1363 printed += count_pri64_printf(i++, "cycles",
1365 bf + printed, bfsize - printed);
1368 if (iter_count && from_count) {
1369 v = iter_count / from_count;
1371 printed += count_pri64_printf(i++, "iter",
1372 v, bf + printed, bfsize - printed);
1374 printed += count_pri64_printf(i++, "avg_cycles",
1375 iter_cycles / iter_count,
1376 bf + printed, bfsize - printed);
1381 printed += scnprintf(bf + printed, bfsize - printed, ")");
1386 static int counts_str_build(char *bf, int bfsize,
1387 u64 branch_count, u64 predicted_count,
1388 u64 abort_count, u64 cycles_count,
1389 u64 iter_count, u64 iter_cycles,
1391 struct branch_type_stat *brtype_stat)
1395 if (branch_count == 0)
1396 return scnprintf(bf, bfsize, " (calltrace)");
1398 if (brtype_stat->branch_to) {
1399 printed = branch_to_str(bf, bfsize, branch_count,
1400 predicted_count, abort_count, brtype_stat);
1402 printed = branch_from_str(bf, bfsize, branch_count,
1403 cycles_count, iter_count, iter_cycles,
1413 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1414 u64 branch_count, u64 predicted_count,
1415 u64 abort_count, u64 cycles_count,
1416 u64 iter_count, u64 iter_cycles,
1418 struct branch_type_stat *brtype_stat)
1422 counts_str_build(str, sizeof(str), branch_count,
1423 predicted_count, abort_count, cycles_count,
1424 iter_count, iter_cycles, from_count, brtype_stat);
1427 return fprintf(fp, "%s", str);
1429 return scnprintf(bf, bfsize, "%s", str);
1432 int callchain_list_counts__printf_value(struct callchain_list *clist,
1433 FILE *fp, char *bf, int bfsize)
1435 u64 branch_count, predicted_count;
1436 u64 abort_count, cycles_count;
1437 u64 iter_count, iter_cycles;
1440 branch_count = clist->branch_count;
1441 predicted_count = clist->predicted_count;
1442 abort_count = clist->abort_count;
1443 cycles_count = clist->cycles_count;
1444 iter_count = clist->iter_count;
1445 iter_cycles = clist->iter_cycles;
1446 from_count = clist->from_count;
1448 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1449 predicted_count, abort_count,
1450 cycles_count, iter_count, iter_cycles,
1451 from_count, &clist->brtype_stat);
1454 static void free_callchain_node(struct callchain_node *node)
1456 struct callchain_list *list, *tmp;
1457 struct callchain_node *child;
1460 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1461 list_del_init(&list->list);
1462 map__zput(list->ms.map);
1466 list_for_each_entry_safe(list, tmp, &node->val, list) {
1467 list_del_init(&list->list);
1468 map__zput(list->ms.map);
1472 n = rb_first(&node->rb_root_in);
1474 child = container_of(n, struct callchain_node, rb_node_in);
1476 rb_erase(&child->rb_node_in, &node->rb_root_in);
1478 free_callchain_node(child);
1483 void free_callchain(struct callchain_root *root)
1485 if (!symbol_conf.use_callchain)
1488 free_callchain_node(&root->node);
1491 static u64 decay_callchain_node(struct callchain_node *node)
1493 struct callchain_node *child;
1497 n = rb_first(&node->rb_root_in);
1499 child = container_of(n, struct callchain_node, rb_node_in);
1501 child_hits += decay_callchain_node(child);
1505 node->hit = (node->hit * 7) / 8;
1506 node->children_hit = child_hits;
1511 void decay_callchain(struct callchain_root *root)
1513 if (!symbol_conf.use_callchain)
1516 decay_callchain_node(&root->node);
1519 int callchain_node__make_parent_list(struct callchain_node *node)
1521 struct callchain_node *parent = node->parent;
1522 struct callchain_list *chain, *new;
1526 list_for_each_entry_reverse(chain, &parent->val, list) {
1527 new = malloc(sizeof(*new));
1531 new->has_children = false;
1532 new->ms.map = map__get(new->ms.map);
1533 list_add_tail(&new->list, &head);
1535 parent = parent->parent;
1538 list_for_each_entry_safe_reverse(chain, new, &head, list)
1539 list_move_tail(&chain->list, &node->parent_val);
1541 if (!list_empty(&node->parent_val)) {
1542 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1543 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1545 chain = list_first_entry(&node->val, struct callchain_list, list);
1546 chain->has_children = false;
1551 list_for_each_entry_safe(chain, new, &head, list) {
1552 list_del_init(&chain->list);
1553 map__zput(chain->ms.map);
1559 int callchain_cursor__copy(struct callchain_cursor *dst,
1560 struct callchain_cursor *src)
1564 callchain_cursor_reset(dst);
1565 callchain_cursor_commit(src);
1568 struct callchain_cursor_node *node;
1570 node = callchain_cursor_current(src);
1574 rc = callchain_cursor_append(dst, node->ip, &node->ms,
1575 node->branch, &node->branch_flags,
1578 node->branch_from, node->srcline);
1582 callchain_cursor_advance(src);
1589 * Initialize a cursor before adding entries inside, but keep
1590 * the previously allocated entries as a cache.
1592 void callchain_cursor_reset(struct callchain_cursor *cursor)
1594 struct callchain_cursor_node *node;
1597 cursor->last = &cursor->first;
1599 for (node = cursor->first; node != NULL; node = node->next)
1600 map__zput(node->ms.map);
1603 void callchain_param_setup(u64 sample_type, const char *arch)
1605 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1606 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1607 (sample_type & PERF_SAMPLE_STACK_USER)) {
1608 callchain_param.record_mode = CALLCHAIN_DWARF;
1609 dwarf_callchain_users = true;
1610 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1611 callchain_param.record_mode = CALLCHAIN_LBR;
1613 callchain_param.record_mode = CALLCHAIN_FP;
1617 * It's necessary to use libunwind to reliably determine the caller of
1618 * a leaf function on aarch64, as otherwise we cannot know whether to
1619 * start from the LR or FP.
1621 * Always starting from the LR can result in duplicate or entirely
1622 * erroneous entries. Always skipping the LR and starting from the FP
1623 * can result in missing entries.
1625 if (callchain_param.record_mode == CALLCHAIN_FP && !strcmp(arch, "arm64"))
1626 dwarf_callchain_users = true;
1629 static bool chain_match(struct callchain_list *base_chain,
1630 struct callchain_list *pair_chain)
1632 enum match_result match;
1634 match = match_chain_strings(base_chain->srcline,
1635 pair_chain->srcline);
1636 if (match != MATCH_ERROR)
1637 return match == MATCH_EQ;
1639 match = match_chain_dso_addresses(base_chain->ms.map,
1644 return match == MATCH_EQ;
1647 bool callchain_cnode_matched(struct callchain_node *base_cnode,
1648 struct callchain_node *pair_cnode)
1650 struct callchain_list *base_chain, *pair_chain;
1653 pair_chain = list_first_entry(&pair_cnode->val,
1654 struct callchain_list,
1657 list_for_each_entry(base_chain, &base_cnode->val, list) {
1658 if (&pair_chain->list == &pair_cnode->val)
1661 if (!base_chain->srcline || !pair_chain->srcline) {
1662 pair_chain = list_next_entry(pair_chain, list);
1666 match = chain_match(base_chain, pair_chain);
1670 pair_chain = list_next_entry(pair_chain, list);
1674 * Say chain1 is ABC, chain2 is ABCD, we consider they are
1675 * not fully matched.
1677 if (pair_chain && (&pair_chain->list != &pair_cnode->val))
1683 static u64 count_callchain_hits(struct hist_entry *he)
1685 struct rb_root *root = &he->sorted_chain;
1686 struct rb_node *rb_node = rb_first(root);
1687 struct callchain_node *node;
1691 node = rb_entry(rb_node, struct callchain_node, rb_node);
1692 chain_hits += node->hit;
1693 rb_node = rb_next(rb_node);
1699 u64 callchain_total_hits(struct hists *hists)
1701 struct rb_node *next = rb_first_cached(&hists->entries);
1705 struct hist_entry *he = rb_entry(next, struct hist_entry,
1708 chain_hits += count_callchain_hits(he);
1709 next = rb_next(&he->rb_node);
1715 s64 callchain_avg_cycles(struct callchain_node *cnode)
1717 struct callchain_list *chain;
1720 list_for_each_entry(chain, &cnode->val, list) {
1721 if (chain->srcline && chain->branch_count)
1722 cycles += chain->cycles_count / chain->branch_count;