1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
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.
26 #include "callchain.h"
29 #define CALLCHAIN_PARAM_DEFAULT \
30 .mode = CHAIN_GRAPH_ABS, \
32 .order = ORDER_CALLEE, \
33 .key = CCKEY_FUNCTION, \
34 .value = CCVAL_PERCENT, \
36 struct callchain_param callchain_param = {
37 CALLCHAIN_PARAM_DEFAULT
41 * Are there any events usind DWARF callchains?
45 * -e cycles/call-graph=dwarf/
47 bool dwarf_callchain_users;
49 struct callchain_param callchain_param_default = {
50 CALLCHAIN_PARAM_DEFAULT
53 __thread struct callchain_cursor callchain_cursor;
55 int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
57 return parse_callchain_record(arg, param);
60 static int parse_callchain_mode(const char *value)
62 if (!strncmp(value, "graph", strlen(value))) {
63 callchain_param.mode = CHAIN_GRAPH_ABS;
66 if (!strncmp(value, "flat", strlen(value))) {
67 callchain_param.mode = CHAIN_FLAT;
70 if (!strncmp(value, "fractal", strlen(value))) {
71 callchain_param.mode = CHAIN_GRAPH_REL;
74 if (!strncmp(value, "folded", strlen(value))) {
75 callchain_param.mode = CHAIN_FOLDED;
81 static int parse_callchain_order(const char *value)
83 if (!strncmp(value, "caller", strlen(value))) {
84 callchain_param.order = ORDER_CALLER;
85 callchain_param.order_set = true;
88 if (!strncmp(value, "callee", strlen(value))) {
89 callchain_param.order = ORDER_CALLEE;
90 callchain_param.order_set = true;
96 static int parse_callchain_sort_key(const char *value)
98 if (!strncmp(value, "function", strlen(value))) {
99 callchain_param.key = CCKEY_FUNCTION;
102 if (!strncmp(value, "address", strlen(value))) {
103 callchain_param.key = CCKEY_ADDRESS;
106 if (!strncmp(value, "srcline", strlen(value))) {
107 callchain_param.key = CCKEY_SRCLINE;
110 if (!strncmp(value, "branch", strlen(value))) {
111 callchain_param.branch_callstack = 1;
117 static int parse_callchain_value(const char *value)
119 if (!strncmp(value, "percent", strlen(value))) {
120 callchain_param.value = CCVAL_PERCENT;
123 if (!strncmp(value, "period", strlen(value))) {
124 callchain_param.value = CCVAL_PERIOD;
127 if (!strncmp(value, "count", strlen(value))) {
128 callchain_param.value = CCVAL_COUNT;
134 static int get_stack_size(const char *str, unsigned long *_size)
138 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
140 size = strtoul(str, &endptr, 0);
146 size = round_up(size, sizeof(u64));
147 if (!size || size > max_size)
155 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
161 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
164 char *endptr, *saveptr = NULL;
165 bool minpcnt_set = false;
166 bool record_opt_set = false;
167 bool try_stack_size = false;
169 callchain_param.enabled = true;
170 symbol_conf.use_callchain = true;
175 while ((tok = strtok_r((char *)arg, ",", &saveptr)) != NULL) {
176 if (!strncmp(tok, "none", strlen(tok))) {
177 callchain_param.mode = CHAIN_NONE;
178 callchain_param.enabled = false;
179 symbol_conf.use_callchain = false;
183 if (!parse_callchain_mode(tok) ||
184 !parse_callchain_order(tok) ||
185 !parse_callchain_sort_key(tok) ||
186 !parse_callchain_value(tok)) {
187 /* parsing ok - move on to the next */
188 try_stack_size = false;
190 } else if (allow_record_opt && !record_opt_set) {
191 if (parse_callchain_record(tok, &callchain_param))
194 /* assume that number followed by 'dwarf' is stack size */
195 if (callchain_param.record_mode == CALLCHAIN_DWARF)
196 try_stack_size = true;
198 record_opt_set = true;
203 if (try_stack_size) {
204 unsigned long size = 0;
206 if (get_stack_size(tok, &size) < 0)
208 callchain_param.dump_size = size;
209 try_stack_size = false;
210 } else if (!minpcnt_set) {
211 /* try to get the min percent */
212 callchain_param.min_percent = strtod(tok, &endptr);
217 /* try print limit at last */
218 callchain_param.print_limit = strtoul(tok, &endptr, 0);
226 if (callchain_register_param(&callchain_param) < 0) {
227 pr_err("Can't register callchain params\n");
233 int parse_callchain_report_opt(const char *arg)
235 return __parse_callchain_report_opt(arg, false);
238 int parse_callchain_top_opt(const char *arg)
240 return __parse_callchain_report_opt(arg, true);
243 int parse_callchain_record(const char *arg, struct callchain_param *param)
245 char *tok, *name, *saveptr = NULL;
249 /* We need buffer that we know we can write to. */
250 buf = malloc(strlen(arg) + 1);
256 tok = strtok_r((char *)buf, ",", &saveptr);
257 name = tok ? : (char *)buf;
260 /* Framepointer style */
261 if (!strncmp(name, "fp", sizeof("fp"))) {
262 if (!strtok_r(NULL, ",", &saveptr)) {
263 param->record_mode = CALLCHAIN_FP;
266 pr_err("callchain: No more arguments "
267 "needed for --call-graph fp\n");
271 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
272 const unsigned long default_stack_dump_size = 8192;
275 param->record_mode = CALLCHAIN_DWARF;
276 param->dump_size = default_stack_dump_size;
277 dwarf_callchain_users = true;
279 tok = strtok_r(NULL, ",", &saveptr);
281 unsigned long size = 0;
283 ret = get_stack_size(tok, &size);
284 param->dump_size = size;
286 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
287 if (!strtok_r(NULL, ",", &saveptr)) {
288 param->record_mode = CALLCHAIN_LBR;
291 pr_err("callchain: No more arguments "
292 "needed for --call-graph lbr\n");
295 pr_err("callchain: Unknown --call-graph option "
306 int perf_callchain_config(const char *var, const char *value)
310 if (!strstarts(var, "call-graph."))
312 var += sizeof("call-graph.") - 1;
314 if (!strcmp(var, "record-mode"))
315 return parse_callchain_record_opt(value, &callchain_param);
316 if (!strcmp(var, "dump-size")) {
317 unsigned long size = 0;
320 ret = get_stack_size(value, &size);
321 callchain_param.dump_size = size;
325 if (!strcmp(var, "print-type")){
327 ret = parse_callchain_mode(value);
329 pr_err("Invalid callchain mode: %s\n", value);
332 if (!strcmp(var, "order")){
334 ret = parse_callchain_order(value);
336 pr_err("Invalid callchain order: %s\n", value);
339 if (!strcmp(var, "sort-key")){
341 ret = parse_callchain_sort_key(value);
343 pr_err("Invalid callchain sort key: %s\n", value);
346 if (!strcmp(var, "threshold")) {
347 callchain_param.min_percent = strtod(value, &endptr);
348 if (value == endptr) {
349 pr_err("Invalid callchain threshold: %s\n", value);
353 if (!strcmp(var, "print-limit")) {
354 callchain_param.print_limit = strtod(value, &endptr);
355 if (value == endptr) {
356 pr_err("Invalid callchain print limit: %s\n", value);
365 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
366 enum chain_mode mode)
368 struct rb_node **p = &root->rb_node;
369 struct rb_node *parent = NULL;
370 struct callchain_node *rnode;
371 u64 chain_cumul = callchain_cumul_hits(chain);
377 rnode = rb_entry(parent, struct callchain_node, rb_node);
378 rnode_cumul = callchain_cumul_hits(rnode);
383 if (rnode->hit < chain->hit)
388 case CHAIN_GRAPH_ABS: /* Falldown */
389 case CHAIN_GRAPH_REL:
390 if (rnode_cumul < chain_cumul)
401 rb_link_node(&chain->rb_node, parent, p);
402 rb_insert_color(&chain->rb_node, root);
406 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
410 struct callchain_node *child;
412 n = rb_first(&node->rb_root_in);
414 child = rb_entry(n, struct callchain_node, rb_node_in);
417 __sort_chain_flat(rb_root, child, min_hit);
420 if (node->hit && node->hit >= min_hit)
421 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
425 * Once we get every callchains from the stream, we can now
429 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
430 u64 min_hit, struct callchain_param *param __maybe_unused)
433 __sort_chain_flat(rb_root, &root->node, min_hit);
436 static void __sort_chain_graph_abs(struct callchain_node *node,
440 struct callchain_node *child;
442 node->rb_root = RB_ROOT;
443 n = rb_first(&node->rb_root_in);
446 child = rb_entry(n, struct callchain_node, rb_node_in);
449 __sort_chain_graph_abs(child, min_hit);
450 if (callchain_cumul_hits(child) >= min_hit)
451 rb_insert_callchain(&node->rb_root, child,
457 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
458 u64 min_hit, struct callchain_param *param __maybe_unused)
460 __sort_chain_graph_abs(&chain_root->node, min_hit);
461 rb_root->rb_node = chain_root->node.rb_root.rb_node;
464 static void __sort_chain_graph_rel(struct callchain_node *node,
468 struct callchain_node *child;
471 node->rb_root = RB_ROOT;
472 min_hit = ceil(node->children_hit * min_percent);
474 n = rb_first(&node->rb_root_in);
476 child = rb_entry(n, struct callchain_node, rb_node_in);
479 __sort_chain_graph_rel(child, min_percent);
480 if (callchain_cumul_hits(child) >= min_hit)
481 rb_insert_callchain(&node->rb_root, child,
487 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
488 u64 min_hit __maybe_unused, struct callchain_param *param)
490 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
491 rb_root->rb_node = chain_root->node.rb_root.rb_node;
494 int callchain_register_param(struct callchain_param *param)
496 switch (param->mode) {
497 case CHAIN_GRAPH_ABS:
498 param->sort = sort_chain_graph_abs;
500 case CHAIN_GRAPH_REL:
501 param->sort = sort_chain_graph_rel;
505 param->sort = sort_chain_flat;
515 * Create a child for a parent. If inherit_children, then the new child
516 * will become the new parent of it's parent children
518 static struct callchain_node *
519 create_child(struct callchain_node *parent, bool inherit_children)
521 struct callchain_node *new;
523 new = zalloc(sizeof(*new));
525 perror("not enough memory to create child for code path tree");
528 new->parent = parent;
529 INIT_LIST_HEAD(&new->val);
530 INIT_LIST_HEAD(&new->parent_val);
532 if (inherit_children) {
534 struct callchain_node *child;
536 new->rb_root_in = parent->rb_root_in;
537 parent->rb_root_in = RB_ROOT;
539 n = rb_first(&new->rb_root_in);
541 child = rb_entry(n, struct callchain_node, rb_node_in);
546 /* make it the first child */
547 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
548 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
556 * Fill the node with callchain values
559 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
561 struct callchain_cursor_node *cursor_node;
563 node->val_nr = cursor->nr - cursor->pos;
565 pr_warning("Warning: empty node in callchain tree\n");
567 cursor_node = callchain_cursor_current(cursor);
569 while (cursor_node) {
570 struct callchain_list *call;
572 call = zalloc(sizeof(*call));
574 perror("not enough memory for the code path tree");
577 call->ip = cursor_node->ip;
578 call->ms.sym = cursor_node->sym;
579 call->ms.map = map__get(cursor_node->map);
580 call->srcline = cursor_node->srcline;
582 if (cursor_node->branch) {
583 call->branch_count = 1;
585 if (cursor_node->branch_from) {
587 * branch_from is set with value somewhere else
588 * to imply it's "to" of a branch.
590 call->brtype_stat.branch_to = true;
592 if (cursor_node->branch_flags.predicted)
593 call->predicted_count = 1;
595 if (cursor_node->branch_flags.abort)
596 call->abort_count = 1;
598 branch_type_count(&call->brtype_stat,
599 &cursor_node->branch_flags,
600 cursor_node->branch_from,
604 * It's "from" of a branch
606 call->brtype_stat.branch_to = false;
608 cursor_node->branch_flags.cycles;
609 call->iter_count = cursor_node->nr_loop_iter;
610 call->iter_cycles = cursor_node->iter_cycles;
614 list_add_tail(&call->list, &node->val);
616 callchain_cursor_advance(cursor);
617 cursor_node = callchain_cursor_current(cursor);
622 static struct callchain_node *
623 add_child(struct callchain_node *parent,
624 struct callchain_cursor *cursor,
627 struct callchain_node *new;
629 new = create_child(parent, false);
633 if (fill_node(new, cursor) < 0) {
634 struct callchain_list *call, *tmp;
636 list_for_each_entry_safe(call, tmp, &new->val, list) {
637 list_del(&call->list);
638 map__zput(call->ms.map);
645 new->children_hit = 0;
647 new->children_count = 0;
659 static enum match_result match_chain_strings(const char *left,
662 enum match_result ret = MATCH_EQ;
666 cmp = strcmp(left, right);
667 else if (!left && right)
669 else if (left && !right)
675 ret = cmp < 0 ? MATCH_LT : MATCH_GT;
681 * We need to always use relative addresses because we're aggregating
682 * callchains from multiple threads, i.e. different address spaces, so
683 * comparing absolute addresses make no sense as a symbol in a DSO may end up
684 * in a different address when used in a different binary or even the same
685 * binary but with some sort of address randomization technique, thus we need
686 * to compare just relative addresses. -acme
688 static enum match_result match_chain_dso_addresses(struct map *left_map, u64 left_ip,
689 struct map *right_map, u64 right_ip)
691 struct dso *left_dso = left_map ? left_map->dso : NULL;
692 struct dso *right_dso = right_map ? right_map->dso : NULL;
694 if (left_dso != right_dso)
695 return left_dso < right_dso ? MATCH_LT : MATCH_GT;
697 if (left_ip != right_ip)
698 return left_ip < right_ip ? MATCH_LT : MATCH_GT;
703 static enum match_result match_chain(struct callchain_cursor_node *node,
704 struct callchain_list *cnode)
706 enum match_result match = MATCH_ERROR;
708 switch (callchain_param.key) {
710 match = match_chain_strings(cnode->srcline, node->srcline);
711 if (match != MATCH_ERROR)
713 /* otherwise fall-back to symbol-based comparison below */
716 if (node->sym && cnode->ms.sym) {
718 * Compare inlined frames based on their symbol name
719 * because different inlined frames will have the same
720 * symbol start. Otherwise do a faster comparison based
721 * on the symbol start address.
723 if (cnode->ms.sym->inlined || node->sym->inlined) {
724 match = match_chain_strings(cnode->ms.sym->name,
726 if (match != MATCH_ERROR)
729 match = match_chain_dso_addresses(cnode->ms.map, cnode->ms.sym->start,
730 node->map, node->sym->start);
734 /* otherwise fall-back to IP-based comparison below */
738 match = match_chain_dso_addresses(cnode->ms.map, cnode->ip, node->map, node->ip);
742 if (match == MATCH_EQ && node->branch) {
743 cnode->branch_count++;
745 if (node->branch_from) {
747 * It's "to" of a branch
749 cnode->brtype_stat.branch_to = true;
751 if (node->branch_flags.predicted)
752 cnode->predicted_count++;
754 if (node->branch_flags.abort)
755 cnode->abort_count++;
757 branch_type_count(&cnode->brtype_stat,
763 * It's "from" of a branch
765 cnode->brtype_stat.branch_to = false;
766 cnode->cycles_count += node->branch_flags.cycles;
767 cnode->iter_count += node->nr_loop_iter;
768 cnode->iter_cycles += node->iter_cycles;
777 * Split the parent in two parts (a new child is created) and
778 * give a part of its callchain to the created child.
779 * Then create another child to host the given callchain of new branch
782 split_add_child(struct callchain_node *parent,
783 struct callchain_cursor *cursor,
784 struct callchain_list *to_split,
785 u64 idx_parents, u64 idx_local, u64 period)
787 struct callchain_node *new;
788 struct list_head *old_tail;
789 unsigned int idx_total = idx_parents + idx_local;
792 new = create_child(parent, true);
796 /* split the callchain and move a part to the new child */
797 old_tail = parent->val.prev;
798 list_del_range(&to_split->list, old_tail);
799 new->val.next = &to_split->list;
800 new->val.prev = old_tail;
801 to_split->list.prev = &new->val;
802 old_tail->next = &new->val;
805 new->hit = parent->hit;
806 new->children_hit = parent->children_hit;
807 parent->children_hit = callchain_cumul_hits(new);
808 new->val_nr = parent->val_nr - idx_local;
809 parent->val_nr = idx_local;
810 new->count = parent->count;
811 new->children_count = parent->children_count;
812 parent->children_count = callchain_cumul_counts(new);
814 /* create a new child for the new branch if any */
815 if (idx_total < cursor->nr) {
816 struct callchain_node *first;
817 struct callchain_list *cnode;
818 struct callchain_cursor_node *node;
819 struct rb_node *p, **pp;
822 parent->children_hit += period;
824 parent->children_count += 1;
826 node = callchain_cursor_current(cursor);
827 new = add_child(parent, cursor, period);
832 * This is second child since we moved parent's children
833 * to new (first) child above.
835 p = parent->rb_root_in.rb_node;
836 first = rb_entry(p, struct callchain_node, rb_node_in);
837 cnode = list_first_entry(&first->val, struct callchain_list,
840 if (match_chain(node, cnode) == MATCH_LT)
845 rb_link_node(&new->rb_node_in, p, pp);
846 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
848 parent->hit = period;
854 static enum match_result
855 append_chain(struct callchain_node *root,
856 struct callchain_cursor *cursor,
860 append_chain_children(struct callchain_node *root,
861 struct callchain_cursor *cursor,
864 struct callchain_node *rnode;
865 struct callchain_cursor_node *node;
866 struct rb_node **p = &root->rb_root_in.rb_node;
867 struct rb_node *parent = NULL;
869 node = callchain_cursor_current(cursor);
873 /* lookup in childrens */
875 enum match_result ret;
878 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
880 /* If at least first entry matches, rely to children */
881 ret = append_chain(rnode, cursor, period);
883 goto inc_children_hit;
884 if (ret == MATCH_ERROR)
888 p = &parent->rb_left;
890 p = &parent->rb_right;
892 /* nothing in children, add to the current node */
893 rnode = add_child(root, cursor, period);
897 rb_link_node(&rnode->rb_node_in, parent, p);
898 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
901 root->children_hit += period;
902 root->children_count++;
906 static enum match_result
907 append_chain(struct callchain_node *root,
908 struct callchain_cursor *cursor,
911 struct callchain_list *cnode;
912 u64 start = cursor->pos;
915 enum match_result cmp = MATCH_ERROR;
918 * Lookup in the current node
919 * If we have a symbol, then compare the start to match
920 * anywhere inside a function, unless function
923 list_for_each_entry(cnode, &root->val, list) {
924 struct callchain_cursor_node *node;
926 node = callchain_cursor_current(cursor);
930 cmp = match_chain(node, cnode);
936 callchain_cursor_advance(cursor);
939 /* matches not, relay no the parent */
941 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
945 matches = cursor->pos - start;
947 /* we match only a part of the node. Split it and add the new chain */
948 if (matches < root->val_nr) {
949 if (split_add_child(root, cursor, cnode, start, matches,
956 /* we match 100% of the path, increment the hit */
957 if (matches == root->val_nr && cursor->pos == cursor->nr) {
963 /* We match the node and still have a part remaining */
964 if (append_chain_children(root, cursor, period) < 0)
970 int callchain_append(struct callchain_root *root,
971 struct callchain_cursor *cursor,
977 callchain_cursor_commit(cursor);
979 if (append_chain_children(&root->node, cursor, period) < 0)
982 if (cursor->nr > root->max_depth)
983 root->max_depth = cursor->nr;
989 merge_chain_branch(struct callchain_cursor *cursor,
990 struct callchain_node *dst, struct callchain_node *src)
992 struct callchain_cursor_node **old_last = cursor->last;
993 struct callchain_node *child;
994 struct callchain_list *list, *next_list;
996 int old_pos = cursor->nr;
999 list_for_each_entry_safe(list, next_list, &src->val, list) {
1000 callchain_cursor_append(cursor, list->ip,
1001 list->ms.map, list->ms.sym,
1002 false, NULL, 0, 0, 0, list->srcline);
1003 list_del(&list->list);
1004 map__zput(list->ms.map);
1009 callchain_cursor_commit(cursor);
1010 if (append_chain_children(dst, cursor, src->hit) < 0)
1014 n = rb_first(&src->rb_root_in);
1016 child = container_of(n, struct callchain_node, rb_node_in);
1018 rb_erase(&child->rb_node_in, &src->rb_root_in);
1020 err = merge_chain_branch(cursor, dst, child);
1027 cursor->nr = old_pos;
1028 cursor->last = old_last;
1033 int callchain_merge(struct callchain_cursor *cursor,
1034 struct callchain_root *dst, struct callchain_root *src)
1036 return merge_chain_branch(cursor, &dst->node, &src->node);
1039 int callchain_cursor_append(struct callchain_cursor *cursor,
1040 u64 ip, struct map *map, struct symbol *sym,
1041 bool branch, struct branch_flags *flags,
1042 int nr_loop_iter, u64 iter_cycles, u64 branch_from,
1043 const char *srcline)
1045 struct callchain_cursor_node *node = *cursor->last;
1048 node = calloc(1, sizeof(*node));
1052 *cursor->last = node;
1056 map__zput(node->map);
1057 node->map = map__get(map);
1059 node->branch = branch;
1060 node->nr_loop_iter = nr_loop_iter;
1061 node->iter_cycles = iter_cycles;
1062 node->srcline = srcline;
1065 memcpy(&node->branch_flags, flags,
1066 sizeof(struct branch_flags));
1068 node->branch_from = branch_from;
1071 cursor->last = &node->next;
1076 int sample__resolve_callchain(struct perf_sample *sample,
1077 struct callchain_cursor *cursor, struct symbol **parent,
1078 struct perf_evsel *evsel, struct addr_location *al,
1081 if (sample->callchain == NULL && !symbol_conf.show_branchflag_count)
1084 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
1085 perf_hpp_list.parent || symbol_conf.show_branchflag_count) {
1086 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
1087 parent, al, max_stack);
1092 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
1094 if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
1095 !symbol_conf.show_branchflag_count)
1097 return callchain_append(he->callchain, &callchain_cursor, sample->period);
1100 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
1101 bool hide_unresolved)
1103 al->map = node->map;
1104 al->sym = node->sym;
1105 al->srcline = node->srcline;
1106 al->addr = node->ip;
1108 if (al->sym == NULL) {
1109 if (hide_unresolved)
1111 if (al->map == NULL)
1115 if (al->map->groups == &al->machine->kmaps) {
1116 if (machine__is_host(al->machine)) {
1117 al->cpumode = PERF_RECORD_MISC_KERNEL;
1120 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
1124 if (machine__is_host(al->machine)) {
1125 al->cpumode = PERF_RECORD_MISC_USER;
1127 } else if (perf_guest) {
1128 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
1131 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
1140 char *callchain_list__sym_name(struct callchain_list *cl,
1141 char *bf, size_t bfsize, bool show_dso)
1143 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1144 bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
1148 const char *inlined = cl->ms.sym->inlined ? " (inlined)" : "";
1150 if (show_srcline && cl->srcline)
1151 printed = scnprintf(bf, bfsize, "%s %s%s",
1152 cl->ms.sym->name, cl->srcline,
1155 printed = scnprintf(bf, bfsize, "%s%s",
1156 cl->ms.sym->name, inlined);
1158 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
1161 scnprintf(bf + printed, bfsize - printed, " %s",
1163 cl->ms.map->dso->short_name :
1169 char *callchain_node__scnprintf_value(struct callchain_node *node,
1170 char *bf, size_t bfsize, u64 total)
1172 double percent = 0.0;
1173 u64 period = callchain_cumul_hits(node);
1174 unsigned count = callchain_cumul_counts(node);
1176 if (callchain_param.mode == CHAIN_FOLDED) {
1178 count = node->count;
1181 switch (callchain_param.value) {
1183 scnprintf(bf, bfsize, "%"PRIu64, period);
1186 scnprintf(bf, bfsize, "%u", count);
1191 percent = period * 100.0 / total;
1192 scnprintf(bf, bfsize, "%.2f%%", percent);
1198 int callchain_node__fprintf_value(struct callchain_node *node,
1199 FILE *fp, u64 total)
1201 double percent = 0.0;
1202 u64 period = callchain_cumul_hits(node);
1203 unsigned count = callchain_cumul_counts(node);
1205 if (callchain_param.mode == CHAIN_FOLDED) {
1207 count = node->count;
1210 switch (callchain_param.value) {
1212 return fprintf(fp, "%"PRIu64, period);
1214 return fprintf(fp, "%u", count);
1218 percent = period * 100.0 / total;
1219 return percent_color_fprintf(fp, "%.2f%%", percent);
1224 static void callchain_counts_value(struct callchain_node *node,
1225 u64 *branch_count, u64 *predicted_count,
1226 u64 *abort_count, u64 *cycles_count)
1228 struct callchain_list *clist;
1230 list_for_each_entry(clist, &node->val, list) {
1232 *branch_count += clist->branch_count;
1234 if (predicted_count)
1235 *predicted_count += clist->predicted_count;
1238 *abort_count += clist->abort_count;
1241 *cycles_count += clist->cycles_count;
1245 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1247 u64 *predicted_count,
1251 struct callchain_node *child;
1254 n = rb_first(&node->rb_root_in);
1256 child = rb_entry(n, struct callchain_node, rb_node_in);
1259 callchain_node_branch_counts_cumul(child, branch_count,
1264 callchain_counts_value(child, branch_count,
1265 predicted_count, abort_count,
1272 int callchain_branch_counts(struct callchain_root *root,
1273 u64 *branch_count, u64 *predicted_count,
1274 u64 *abort_count, u64 *cycles_count)
1279 if (predicted_count)
1280 *predicted_count = 0;
1288 return callchain_node_branch_counts_cumul(&root->node,
1295 static int count_pri64_printf(int idx, const char *str, u64 value, char *bf, int bfsize)
1299 printed = scnprintf(bf, bfsize, "%s%s:%" PRId64 "", (idx) ? " " : " (", str, value);
1304 static int count_float_printf(int idx, const char *str, float value,
1305 char *bf, int bfsize, float threshold)
1309 if (threshold != 0.0 && value < threshold)
1312 printed = scnprintf(bf, bfsize, "%s%s:%.1f%%", (idx) ? " " : " (", str, value);
1317 static int branch_to_str(char *bf, int bfsize,
1318 u64 branch_count, u64 predicted_count,
1320 struct branch_type_stat *brtype_stat)
1324 printed = branch_type_str(brtype_stat, bf, bfsize);
1328 if (predicted_count < branch_count) {
1329 printed += count_float_printf(i++, "predicted",
1330 predicted_count * 100.0 / branch_count,
1331 bf + printed, bfsize - printed, 0.0);
1335 printed += count_float_printf(i++, "abort",
1336 abort_count * 100.0 / branch_count,
1337 bf + printed, bfsize - printed, 0.1);
1341 printed += scnprintf(bf + printed, bfsize - printed, ")");
1346 static int branch_from_str(char *bf, int bfsize,
1348 u64 cycles_count, u64 iter_count,
1349 u64 iter_cycles, u64 from_count)
1351 int printed = 0, i = 0;
1354 cycles = cycles_count / branch_count;
1356 printed += count_pri64_printf(i++, "cycles",
1358 bf + printed, bfsize - printed);
1361 if (iter_count && from_count) {
1362 v = iter_count / from_count;
1364 printed += count_pri64_printf(i++, "iter",
1365 v, bf + printed, bfsize - printed);
1367 printed += count_pri64_printf(i++, "avg_cycles",
1368 iter_cycles / iter_count,
1369 bf + printed, bfsize - printed);
1374 printed += scnprintf(bf + printed, bfsize - printed, ")");
1379 static int counts_str_build(char *bf, int bfsize,
1380 u64 branch_count, u64 predicted_count,
1381 u64 abort_count, u64 cycles_count,
1382 u64 iter_count, u64 iter_cycles,
1384 struct branch_type_stat *brtype_stat)
1388 if (branch_count == 0)
1389 return scnprintf(bf, bfsize, " (calltrace)");
1391 if (brtype_stat->branch_to) {
1392 printed = branch_to_str(bf, bfsize, branch_count,
1393 predicted_count, abort_count, brtype_stat);
1395 printed = branch_from_str(bf, bfsize, branch_count,
1396 cycles_count, iter_count, iter_cycles,
1406 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1407 u64 branch_count, u64 predicted_count,
1408 u64 abort_count, u64 cycles_count,
1409 u64 iter_count, u64 iter_cycles,
1411 struct branch_type_stat *brtype_stat)
1415 counts_str_build(str, sizeof(str), branch_count,
1416 predicted_count, abort_count, cycles_count,
1417 iter_count, iter_cycles, from_count, brtype_stat);
1420 return fprintf(fp, "%s", str);
1422 return scnprintf(bf, bfsize, "%s", str);
1425 int callchain_list_counts__printf_value(struct callchain_list *clist,
1426 FILE *fp, char *bf, int bfsize)
1428 u64 branch_count, predicted_count;
1429 u64 abort_count, cycles_count;
1430 u64 iter_count, iter_cycles;
1433 branch_count = clist->branch_count;
1434 predicted_count = clist->predicted_count;
1435 abort_count = clist->abort_count;
1436 cycles_count = clist->cycles_count;
1437 iter_count = clist->iter_count;
1438 iter_cycles = clist->iter_cycles;
1439 from_count = clist->from_count;
1441 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1442 predicted_count, abort_count,
1443 cycles_count, iter_count, iter_cycles,
1444 from_count, &clist->brtype_stat);
1447 static void free_callchain_node(struct callchain_node *node)
1449 struct callchain_list *list, *tmp;
1450 struct callchain_node *child;
1453 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1454 list_del(&list->list);
1455 map__zput(list->ms.map);
1459 list_for_each_entry_safe(list, tmp, &node->val, list) {
1460 list_del(&list->list);
1461 map__zput(list->ms.map);
1465 n = rb_first(&node->rb_root_in);
1467 child = container_of(n, struct callchain_node, rb_node_in);
1469 rb_erase(&child->rb_node_in, &node->rb_root_in);
1471 free_callchain_node(child);
1476 void free_callchain(struct callchain_root *root)
1478 if (!symbol_conf.use_callchain)
1481 free_callchain_node(&root->node);
1484 static u64 decay_callchain_node(struct callchain_node *node)
1486 struct callchain_node *child;
1490 n = rb_first(&node->rb_root_in);
1492 child = container_of(n, struct callchain_node, rb_node_in);
1494 child_hits += decay_callchain_node(child);
1498 node->hit = (node->hit * 7) / 8;
1499 node->children_hit = child_hits;
1504 void decay_callchain(struct callchain_root *root)
1506 if (!symbol_conf.use_callchain)
1509 decay_callchain_node(&root->node);
1512 int callchain_node__make_parent_list(struct callchain_node *node)
1514 struct callchain_node *parent = node->parent;
1515 struct callchain_list *chain, *new;
1519 list_for_each_entry_reverse(chain, &parent->val, list) {
1520 new = malloc(sizeof(*new));
1524 new->has_children = false;
1525 map__get(new->ms.map);
1526 list_add_tail(&new->list, &head);
1528 parent = parent->parent;
1531 list_for_each_entry_safe_reverse(chain, new, &head, list)
1532 list_move_tail(&chain->list, &node->parent_val);
1534 if (!list_empty(&node->parent_val)) {
1535 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1536 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1538 chain = list_first_entry(&node->val, struct callchain_list, list);
1539 chain->has_children = false;
1544 list_for_each_entry_safe(chain, new, &head, list) {
1545 list_del(&chain->list);
1546 map__zput(chain->ms.map);
1552 int callchain_cursor__copy(struct callchain_cursor *dst,
1553 struct callchain_cursor *src)
1557 callchain_cursor_reset(dst);
1558 callchain_cursor_commit(src);
1561 struct callchain_cursor_node *node;
1563 node = callchain_cursor_current(src);
1567 rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
1568 node->branch, &node->branch_flags,
1571 node->branch_from, node->srcline);
1575 callchain_cursor_advance(src);