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.
25 #include "callchain.h"
28 #define CALLCHAIN_PARAM_DEFAULT \
29 .mode = CHAIN_GRAPH_ABS, \
31 .order = ORDER_CALLEE, \
32 .key = CCKEY_FUNCTION, \
33 .value = CCVAL_PERCENT, \
35 struct callchain_param callchain_param = {
36 CALLCHAIN_PARAM_DEFAULT
39 struct callchain_param callchain_param_default = {
40 CALLCHAIN_PARAM_DEFAULT
43 __thread struct callchain_cursor callchain_cursor;
45 int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
47 return parse_callchain_record(arg, param);
50 static int parse_callchain_mode(const char *value)
52 if (!strncmp(value, "graph", strlen(value))) {
53 callchain_param.mode = CHAIN_GRAPH_ABS;
56 if (!strncmp(value, "flat", strlen(value))) {
57 callchain_param.mode = CHAIN_FLAT;
60 if (!strncmp(value, "fractal", strlen(value))) {
61 callchain_param.mode = CHAIN_GRAPH_REL;
64 if (!strncmp(value, "folded", strlen(value))) {
65 callchain_param.mode = CHAIN_FOLDED;
69 pr_err("Invalid callchain mode: %s\n", value);
73 static int parse_callchain_order(const char *value)
75 if (!strncmp(value, "caller", strlen(value))) {
76 callchain_param.order = ORDER_CALLER;
77 callchain_param.order_set = true;
80 if (!strncmp(value, "callee", strlen(value))) {
81 callchain_param.order = ORDER_CALLEE;
82 callchain_param.order_set = true;
86 pr_err("Invalid callchain order: %s\n", value);
90 static int parse_callchain_sort_key(const char *value)
92 if (!strncmp(value, "function", strlen(value))) {
93 callchain_param.key = CCKEY_FUNCTION;
96 if (!strncmp(value, "address", strlen(value))) {
97 callchain_param.key = CCKEY_ADDRESS;
100 if (!strncmp(value, "srcline", strlen(value))) {
101 callchain_param.key = CCKEY_SRCLINE;
104 if (!strncmp(value, "branch", strlen(value))) {
105 callchain_param.branch_callstack = 1;
109 pr_err("Invalid callchain sort key: %s\n", value);
113 static int parse_callchain_value(const char *value)
115 if (!strncmp(value, "percent", strlen(value))) {
116 callchain_param.value = CCVAL_PERCENT;
119 if (!strncmp(value, "period", strlen(value))) {
120 callchain_param.value = CCVAL_PERIOD;
123 if (!strncmp(value, "count", strlen(value))) {
124 callchain_param.value = CCVAL_COUNT;
128 pr_err("Invalid callchain config key: %s\n", value);
132 static int get_stack_size(const char *str, unsigned long *_size)
136 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
138 size = strtoul(str, &endptr, 0);
144 size = round_up(size, sizeof(u64));
145 if (!size || size > max_size)
153 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
159 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
162 char *endptr, *saveptr = NULL;
163 bool minpcnt_set = false;
164 bool record_opt_set = false;
165 bool try_stack_size = false;
167 callchain_param.enabled = true;
168 symbol_conf.use_callchain = true;
173 while ((tok = strtok_r((char *)arg, ",", &saveptr)) != NULL) {
174 if (!strncmp(tok, "none", strlen(tok))) {
175 callchain_param.mode = CHAIN_NONE;
176 callchain_param.enabled = false;
177 symbol_conf.use_callchain = false;
181 if (!parse_callchain_mode(tok) ||
182 !parse_callchain_order(tok) ||
183 !parse_callchain_sort_key(tok) ||
184 !parse_callchain_value(tok)) {
185 /* parsing ok - move on to the next */
186 try_stack_size = false;
188 } else if (allow_record_opt && !record_opt_set) {
189 if (parse_callchain_record(tok, &callchain_param))
192 /* assume that number followed by 'dwarf' is stack size */
193 if (callchain_param.record_mode == CALLCHAIN_DWARF)
194 try_stack_size = true;
196 record_opt_set = true;
201 if (try_stack_size) {
202 unsigned long size = 0;
204 if (get_stack_size(tok, &size) < 0)
206 callchain_param.dump_size = size;
207 try_stack_size = false;
208 } else if (!minpcnt_set) {
209 /* try to get the min percent */
210 callchain_param.min_percent = strtod(tok, &endptr);
215 /* try print limit at last */
216 callchain_param.print_limit = strtoul(tok, &endptr, 0);
224 if (callchain_register_param(&callchain_param) < 0) {
225 pr_err("Can't register callchain params\n");
231 int parse_callchain_report_opt(const char *arg)
233 return __parse_callchain_report_opt(arg, false);
236 int parse_callchain_top_opt(const char *arg)
238 return __parse_callchain_report_opt(arg, true);
241 int parse_callchain_record(const char *arg, struct callchain_param *param)
243 char *tok, *name, *saveptr = NULL;
247 /* We need buffer that we know we can write to. */
248 buf = malloc(strlen(arg) + 1);
254 tok = strtok_r((char *)buf, ",", &saveptr);
255 name = tok ? : (char *)buf;
258 /* Framepointer style */
259 if (!strncmp(name, "fp", sizeof("fp"))) {
260 if (!strtok_r(NULL, ",", &saveptr)) {
261 param->record_mode = CALLCHAIN_FP;
264 pr_err("callchain: No more arguments "
265 "needed for --call-graph fp\n");
269 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
270 const unsigned long default_stack_dump_size = 8192;
273 param->record_mode = CALLCHAIN_DWARF;
274 param->dump_size = default_stack_dump_size;
276 tok = strtok_r(NULL, ",", &saveptr);
278 unsigned long size = 0;
280 ret = get_stack_size(tok, &size);
281 param->dump_size = size;
283 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
284 if (!strtok_r(NULL, ",", &saveptr)) {
285 param->record_mode = CALLCHAIN_LBR;
288 pr_err("callchain: No more arguments "
289 "needed for --call-graph lbr\n");
292 pr_err("callchain: Unknown --call-graph option "
303 int perf_callchain_config(const char *var, const char *value)
307 if (!strstarts(var, "call-graph."))
309 var += sizeof("call-graph.") - 1;
311 if (!strcmp(var, "record-mode"))
312 return parse_callchain_record_opt(value, &callchain_param);
313 if (!strcmp(var, "dump-size")) {
314 unsigned long size = 0;
317 ret = get_stack_size(value, &size);
318 callchain_param.dump_size = size;
322 if (!strcmp(var, "print-type"))
323 return parse_callchain_mode(value);
324 if (!strcmp(var, "order"))
325 return parse_callchain_order(value);
326 if (!strcmp(var, "sort-key"))
327 return parse_callchain_sort_key(value);
328 if (!strcmp(var, "threshold")) {
329 callchain_param.min_percent = strtod(value, &endptr);
330 if (value == endptr) {
331 pr_err("Invalid callchain threshold: %s\n", value);
335 if (!strcmp(var, "print-limit")) {
336 callchain_param.print_limit = strtod(value, &endptr);
337 if (value == endptr) {
338 pr_err("Invalid callchain print limit: %s\n", value);
347 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
348 enum chain_mode mode)
350 struct rb_node **p = &root->rb_node;
351 struct rb_node *parent = NULL;
352 struct callchain_node *rnode;
353 u64 chain_cumul = callchain_cumul_hits(chain);
359 rnode = rb_entry(parent, struct callchain_node, rb_node);
360 rnode_cumul = callchain_cumul_hits(rnode);
365 if (rnode->hit < chain->hit)
370 case CHAIN_GRAPH_ABS: /* Falldown */
371 case CHAIN_GRAPH_REL:
372 if (rnode_cumul < chain_cumul)
383 rb_link_node(&chain->rb_node, parent, p);
384 rb_insert_color(&chain->rb_node, root);
388 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
392 struct callchain_node *child;
394 n = rb_first(&node->rb_root_in);
396 child = rb_entry(n, struct callchain_node, rb_node_in);
399 __sort_chain_flat(rb_root, child, min_hit);
402 if (node->hit && node->hit >= min_hit)
403 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
407 * Once we get every callchains from the stream, we can now
411 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
412 u64 min_hit, struct callchain_param *param __maybe_unused)
415 __sort_chain_flat(rb_root, &root->node, min_hit);
418 static void __sort_chain_graph_abs(struct callchain_node *node,
422 struct callchain_node *child;
424 node->rb_root = RB_ROOT;
425 n = rb_first(&node->rb_root_in);
428 child = rb_entry(n, struct callchain_node, rb_node_in);
431 __sort_chain_graph_abs(child, min_hit);
432 if (callchain_cumul_hits(child) >= min_hit)
433 rb_insert_callchain(&node->rb_root, child,
439 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
440 u64 min_hit, struct callchain_param *param __maybe_unused)
442 __sort_chain_graph_abs(&chain_root->node, min_hit);
443 rb_root->rb_node = chain_root->node.rb_root.rb_node;
446 static void __sort_chain_graph_rel(struct callchain_node *node,
450 struct callchain_node *child;
453 node->rb_root = RB_ROOT;
454 min_hit = ceil(node->children_hit * min_percent);
456 n = rb_first(&node->rb_root_in);
458 child = rb_entry(n, struct callchain_node, rb_node_in);
461 __sort_chain_graph_rel(child, min_percent);
462 if (callchain_cumul_hits(child) >= min_hit)
463 rb_insert_callchain(&node->rb_root, child,
469 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
470 u64 min_hit __maybe_unused, struct callchain_param *param)
472 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
473 rb_root->rb_node = chain_root->node.rb_root.rb_node;
476 int callchain_register_param(struct callchain_param *param)
478 switch (param->mode) {
479 case CHAIN_GRAPH_ABS:
480 param->sort = sort_chain_graph_abs;
482 case CHAIN_GRAPH_REL:
483 param->sort = sort_chain_graph_rel;
487 param->sort = sort_chain_flat;
497 * Create a child for a parent. If inherit_children, then the new child
498 * will become the new parent of it's parent children
500 static struct callchain_node *
501 create_child(struct callchain_node *parent, bool inherit_children)
503 struct callchain_node *new;
505 new = zalloc(sizeof(*new));
507 perror("not enough memory to create child for code path tree");
510 new->parent = parent;
511 INIT_LIST_HEAD(&new->val);
512 INIT_LIST_HEAD(&new->parent_val);
514 if (inherit_children) {
516 struct callchain_node *child;
518 new->rb_root_in = parent->rb_root_in;
519 parent->rb_root_in = RB_ROOT;
521 n = rb_first(&new->rb_root_in);
523 child = rb_entry(n, struct callchain_node, rb_node_in);
528 /* make it the first child */
529 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
530 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
538 * Fill the node with callchain values
541 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
543 struct callchain_cursor_node *cursor_node;
545 node->val_nr = cursor->nr - cursor->pos;
547 pr_warning("Warning: empty node in callchain tree\n");
549 cursor_node = callchain_cursor_current(cursor);
551 while (cursor_node) {
552 struct callchain_list *call;
554 call = zalloc(sizeof(*call));
556 perror("not enough memory for the code path tree");
559 call->ip = cursor_node->ip;
560 call->ms.sym = cursor_node->sym;
561 call->ms.map = map__get(cursor_node->map);
563 if (cursor_node->branch) {
564 call->branch_count = 1;
566 if (cursor_node->branch_from) {
568 * branch_from is set with value somewhere else
569 * to imply it's "to" of a branch.
571 call->brtype_stat.branch_to = true;
573 if (cursor_node->branch_flags.predicted)
574 call->predicted_count = 1;
576 if (cursor_node->branch_flags.abort)
577 call->abort_count = 1;
579 branch_type_count(&call->brtype_stat,
580 &cursor_node->branch_flags,
581 cursor_node->branch_from,
585 * It's "from" of a branch
587 call->brtype_stat.branch_to = false;
589 cursor_node->branch_flags.cycles;
590 call->iter_count = cursor_node->nr_loop_iter;
591 call->iter_cycles = cursor_node->iter_cycles;
595 list_add_tail(&call->list, &node->val);
597 callchain_cursor_advance(cursor);
598 cursor_node = callchain_cursor_current(cursor);
603 static struct callchain_node *
604 add_child(struct callchain_node *parent,
605 struct callchain_cursor *cursor,
608 struct callchain_node *new;
610 new = create_child(parent, false);
614 if (fill_node(new, cursor) < 0) {
615 struct callchain_list *call, *tmp;
617 list_for_each_entry_safe(call, tmp, &new->val, list) {
618 list_del(&call->list);
619 map__zput(call->ms.map);
626 new->children_hit = 0;
628 new->children_count = 0;
640 static enum match_result match_chain_srcline(struct callchain_cursor_node *node,
641 struct callchain_list *cnode)
645 enum match_result ret = MATCH_EQ;
649 left = get_srcline(cnode->ms.map->dso,
650 map__rip_2objdump(cnode->ms.map, cnode->ip),
651 cnode->ms.sym, true, false);
653 right = get_srcline(node->map->dso,
654 map__rip_2objdump(node->map, node->ip),
655 node->sym, true, false);
658 cmp = strcmp(left, right);
659 else if (!left && right)
661 else if (left && !right)
663 else if (cnode->ip == node->ip)
666 cmp = (cnode->ip < node->ip) ? -1 : 1;
669 ret = cmp < 0 ? MATCH_LT : MATCH_GT;
676 static enum match_result match_chain(struct callchain_cursor_node *node,
677 struct callchain_list *cnode)
679 struct symbol *sym = node->sym;
682 if (callchain_param.key == CCKEY_SRCLINE) {
683 enum match_result match = match_chain_srcline(node, cnode);
685 if (match != MATCH_ERROR)
689 if (cnode->ms.sym && sym && callchain_param.key == CCKEY_FUNCTION) {
690 left = cnode->ms.sym->start;
699 cnode->branch_count++;
701 if (node->branch_from) {
703 * It's "to" of a branch
705 cnode->brtype_stat.branch_to = true;
707 if (node->branch_flags.predicted)
708 cnode->predicted_count++;
710 if (node->branch_flags.abort)
711 cnode->abort_count++;
713 branch_type_count(&cnode->brtype_stat,
719 * It's "from" of a branch
721 cnode->brtype_stat.branch_to = false;
722 cnode->cycles_count +=
723 node->branch_flags.cycles;
724 cnode->iter_count += node->nr_loop_iter;
725 cnode->iter_cycles += node->iter_cycles;
732 return left > right ? MATCH_GT : MATCH_LT;
736 * Split the parent in two parts (a new child is created) and
737 * give a part of its callchain to the created child.
738 * Then create another child to host the given callchain of new branch
741 split_add_child(struct callchain_node *parent,
742 struct callchain_cursor *cursor,
743 struct callchain_list *to_split,
744 u64 idx_parents, u64 idx_local, u64 period)
746 struct callchain_node *new;
747 struct list_head *old_tail;
748 unsigned int idx_total = idx_parents + idx_local;
751 new = create_child(parent, true);
755 /* split the callchain and move a part to the new child */
756 old_tail = parent->val.prev;
757 list_del_range(&to_split->list, old_tail);
758 new->val.next = &to_split->list;
759 new->val.prev = old_tail;
760 to_split->list.prev = &new->val;
761 old_tail->next = &new->val;
764 new->hit = parent->hit;
765 new->children_hit = parent->children_hit;
766 parent->children_hit = callchain_cumul_hits(new);
767 new->val_nr = parent->val_nr - idx_local;
768 parent->val_nr = idx_local;
769 new->count = parent->count;
770 new->children_count = parent->children_count;
771 parent->children_count = callchain_cumul_counts(new);
773 /* create a new child for the new branch if any */
774 if (idx_total < cursor->nr) {
775 struct callchain_node *first;
776 struct callchain_list *cnode;
777 struct callchain_cursor_node *node;
778 struct rb_node *p, **pp;
781 parent->children_hit += period;
783 parent->children_count += 1;
785 node = callchain_cursor_current(cursor);
786 new = add_child(parent, cursor, period);
791 * This is second child since we moved parent's children
792 * to new (first) child above.
794 p = parent->rb_root_in.rb_node;
795 first = rb_entry(p, struct callchain_node, rb_node_in);
796 cnode = list_first_entry(&first->val, struct callchain_list,
799 if (match_chain(node, cnode) == MATCH_LT)
804 rb_link_node(&new->rb_node_in, p, pp);
805 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
807 parent->hit = period;
813 static enum match_result
814 append_chain(struct callchain_node *root,
815 struct callchain_cursor *cursor,
819 append_chain_children(struct callchain_node *root,
820 struct callchain_cursor *cursor,
823 struct callchain_node *rnode;
824 struct callchain_cursor_node *node;
825 struct rb_node **p = &root->rb_root_in.rb_node;
826 struct rb_node *parent = NULL;
828 node = callchain_cursor_current(cursor);
832 /* lookup in childrens */
834 enum match_result ret;
837 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
839 /* If at least first entry matches, rely to children */
840 ret = append_chain(rnode, cursor, period);
842 goto inc_children_hit;
843 if (ret == MATCH_ERROR)
847 p = &parent->rb_left;
849 p = &parent->rb_right;
851 /* nothing in children, add to the current node */
852 rnode = add_child(root, cursor, period);
856 rb_link_node(&rnode->rb_node_in, parent, p);
857 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
860 root->children_hit += period;
861 root->children_count++;
865 static enum match_result
866 append_chain(struct callchain_node *root,
867 struct callchain_cursor *cursor,
870 struct callchain_list *cnode;
871 u64 start = cursor->pos;
874 enum match_result cmp = MATCH_ERROR;
877 * Lookup in the current node
878 * If we have a symbol, then compare the start to match
879 * anywhere inside a function, unless function
882 list_for_each_entry(cnode, &root->val, list) {
883 struct callchain_cursor_node *node;
885 node = callchain_cursor_current(cursor);
889 cmp = match_chain(node, cnode);
895 callchain_cursor_advance(cursor);
898 /* matches not, relay no the parent */
900 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
904 matches = cursor->pos - start;
906 /* we match only a part of the node. Split it and add the new chain */
907 if (matches < root->val_nr) {
908 if (split_add_child(root, cursor, cnode, start, matches,
915 /* we match 100% of the path, increment the hit */
916 if (matches == root->val_nr && cursor->pos == cursor->nr) {
922 /* We match the node and still have a part remaining */
923 if (append_chain_children(root, cursor, period) < 0)
929 int callchain_append(struct callchain_root *root,
930 struct callchain_cursor *cursor,
936 callchain_cursor_commit(cursor);
938 if (append_chain_children(&root->node, cursor, period) < 0)
941 if (cursor->nr > root->max_depth)
942 root->max_depth = cursor->nr;
948 merge_chain_branch(struct callchain_cursor *cursor,
949 struct callchain_node *dst, struct callchain_node *src)
951 struct callchain_cursor_node **old_last = cursor->last;
952 struct callchain_node *child;
953 struct callchain_list *list, *next_list;
955 int old_pos = cursor->nr;
958 list_for_each_entry_safe(list, next_list, &src->val, list) {
959 callchain_cursor_append(cursor, list->ip,
960 list->ms.map, list->ms.sym,
961 false, NULL, 0, 0, 0);
962 list_del(&list->list);
963 map__zput(list->ms.map);
968 callchain_cursor_commit(cursor);
969 if (append_chain_children(dst, cursor, src->hit) < 0)
973 n = rb_first(&src->rb_root_in);
975 child = container_of(n, struct callchain_node, rb_node_in);
977 rb_erase(&child->rb_node_in, &src->rb_root_in);
979 err = merge_chain_branch(cursor, dst, child);
986 cursor->nr = old_pos;
987 cursor->last = old_last;
992 int callchain_merge(struct callchain_cursor *cursor,
993 struct callchain_root *dst, struct callchain_root *src)
995 return merge_chain_branch(cursor, &dst->node, &src->node);
998 int callchain_cursor_append(struct callchain_cursor *cursor,
999 u64 ip, struct map *map, struct symbol *sym,
1000 bool branch, struct branch_flags *flags,
1001 int nr_loop_iter, u64 iter_cycles, u64 branch_from)
1003 struct callchain_cursor_node *node = *cursor->last;
1006 node = calloc(1, sizeof(*node));
1010 *cursor->last = node;
1014 map__zput(node->map);
1015 node->map = map__get(map);
1017 node->branch = branch;
1018 node->nr_loop_iter = nr_loop_iter;
1019 node->iter_cycles = iter_cycles;
1022 memcpy(&node->branch_flags, flags,
1023 sizeof(struct branch_flags));
1025 node->branch_from = branch_from;
1028 cursor->last = &node->next;
1033 int sample__resolve_callchain(struct perf_sample *sample,
1034 struct callchain_cursor *cursor, struct symbol **parent,
1035 struct perf_evsel *evsel, struct addr_location *al,
1038 if (sample->callchain == NULL && !symbol_conf.show_branchflag_count)
1041 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
1042 perf_hpp_list.parent || symbol_conf.show_branchflag_count) {
1043 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
1044 parent, al, max_stack);
1049 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
1051 if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
1052 !symbol_conf.show_branchflag_count)
1054 return callchain_append(he->callchain, &callchain_cursor, sample->period);
1057 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
1058 bool hide_unresolved)
1060 al->map = node->map;
1061 al->sym = node->sym;
1063 al->addr = node->map->map_ip(node->map, node->ip);
1065 al->addr = node->ip;
1067 if (al->sym == NULL) {
1068 if (hide_unresolved)
1070 if (al->map == NULL)
1074 if (al->map->groups == &al->machine->kmaps) {
1075 if (machine__is_host(al->machine)) {
1076 al->cpumode = PERF_RECORD_MISC_KERNEL;
1079 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
1083 if (machine__is_host(al->machine)) {
1084 al->cpumode = PERF_RECORD_MISC_USER;
1086 } else if (perf_guest) {
1087 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
1090 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
1099 char *callchain_list__sym_name(struct callchain_list *cl,
1100 char *bf, size_t bfsize, bool show_dso)
1102 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1103 bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
1107 if (show_srcline && cl->ms.map && !cl->srcline)
1108 cl->srcline = get_srcline(cl->ms.map->dso,
1109 map__rip_2objdump(cl->ms.map,
1111 cl->ms.sym, false, show_addr);
1113 printed = scnprintf(bf, bfsize, "%s %s",
1114 cl->ms.sym->name, cl->srcline);
1116 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
1118 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
1121 scnprintf(bf + printed, bfsize - printed, " %s",
1123 cl->ms.map->dso->short_name :
1129 char *callchain_node__scnprintf_value(struct callchain_node *node,
1130 char *bf, size_t bfsize, u64 total)
1132 double percent = 0.0;
1133 u64 period = callchain_cumul_hits(node);
1134 unsigned count = callchain_cumul_counts(node);
1136 if (callchain_param.mode == CHAIN_FOLDED) {
1138 count = node->count;
1141 switch (callchain_param.value) {
1143 scnprintf(bf, bfsize, "%"PRIu64, period);
1146 scnprintf(bf, bfsize, "%u", count);
1151 percent = period * 100.0 / total;
1152 scnprintf(bf, bfsize, "%.2f%%", percent);
1158 int callchain_node__fprintf_value(struct callchain_node *node,
1159 FILE *fp, u64 total)
1161 double percent = 0.0;
1162 u64 period = callchain_cumul_hits(node);
1163 unsigned count = callchain_cumul_counts(node);
1165 if (callchain_param.mode == CHAIN_FOLDED) {
1167 count = node->count;
1170 switch (callchain_param.value) {
1172 return fprintf(fp, "%"PRIu64, period);
1174 return fprintf(fp, "%u", count);
1178 percent = period * 100.0 / total;
1179 return percent_color_fprintf(fp, "%.2f%%", percent);
1184 static void callchain_counts_value(struct callchain_node *node,
1185 u64 *branch_count, u64 *predicted_count,
1186 u64 *abort_count, u64 *cycles_count)
1188 struct callchain_list *clist;
1190 list_for_each_entry(clist, &node->val, list) {
1192 *branch_count += clist->branch_count;
1194 if (predicted_count)
1195 *predicted_count += clist->predicted_count;
1198 *abort_count += clist->abort_count;
1201 *cycles_count += clist->cycles_count;
1205 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1207 u64 *predicted_count,
1211 struct callchain_node *child;
1214 n = rb_first(&node->rb_root_in);
1216 child = rb_entry(n, struct callchain_node, rb_node_in);
1219 callchain_node_branch_counts_cumul(child, branch_count,
1224 callchain_counts_value(child, branch_count,
1225 predicted_count, abort_count,
1232 int callchain_branch_counts(struct callchain_root *root,
1233 u64 *branch_count, u64 *predicted_count,
1234 u64 *abort_count, u64 *cycles_count)
1239 if (predicted_count)
1240 *predicted_count = 0;
1248 return callchain_node_branch_counts_cumul(&root->node,
1255 static int count_pri64_printf(int idx, const char *str, u64 value, char *bf, int bfsize)
1259 printed = scnprintf(bf, bfsize, "%s%s:%" PRId64 "", (idx) ? " " : " (", str, value);
1264 static int count_float_printf(int idx, const char *str, float value,
1265 char *bf, int bfsize, float threshold)
1269 if (threshold != 0.0 && value < threshold)
1272 printed = scnprintf(bf, bfsize, "%s%s:%.1f%%", (idx) ? " " : " (", str, value);
1277 static int branch_to_str(char *bf, int bfsize,
1278 u64 branch_count, u64 predicted_count,
1280 struct branch_type_stat *brtype_stat)
1284 printed = branch_type_str(brtype_stat, bf, bfsize);
1288 if (predicted_count < branch_count) {
1289 printed += count_float_printf(i++, "predicted",
1290 predicted_count * 100.0 / branch_count,
1291 bf + printed, bfsize - printed, 0.0);
1295 printed += count_float_printf(i++, "abort",
1296 abort_count * 100.0 / branch_count,
1297 bf + printed, bfsize - printed, 0.1);
1301 printed += scnprintf(bf + printed, bfsize - printed, ")");
1306 static int branch_from_str(char *bf, int bfsize,
1308 u64 cycles_count, u64 iter_count,
1311 int printed = 0, i = 0;
1314 cycles = cycles_count / branch_count;
1316 printed += count_pri64_printf(i++, "cycles",
1318 bf + printed, bfsize - printed);
1322 printed += count_pri64_printf(i++, "iter",
1324 bf + printed, bfsize - printed);
1326 printed += count_pri64_printf(i++, "avg_cycles",
1327 iter_cycles / iter_count,
1328 bf + printed, bfsize - printed);
1332 printed += scnprintf(bf + printed, bfsize - printed, ")");
1337 static int counts_str_build(char *bf, int bfsize,
1338 u64 branch_count, u64 predicted_count,
1339 u64 abort_count, u64 cycles_count,
1340 u64 iter_count, u64 iter_cycles,
1341 struct branch_type_stat *brtype_stat)
1345 if (branch_count == 0)
1346 return scnprintf(bf, bfsize, " (calltrace)");
1348 if (brtype_stat->branch_to) {
1349 printed = branch_to_str(bf, bfsize, branch_count,
1350 predicted_count, abort_count, brtype_stat);
1352 printed = branch_from_str(bf, bfsize, branch_count,
1353 cycles_count, iter_count, iter_cycles);
1362 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1363 u64 branch_count, u64 predicted_count,
1364 u64 abort_count, u64 cycles_count,
1365 u64 iter_count, u64 iter_cycles,
1366 struct branch_type_stat *brtype_stat)
1370 counts_str_build(str, sizeof(str), branch_count,
1371 predicted_count, abort_count, cycles_count,
1372 iter_count, iter_cycles, brtype_stat);
1375 return fprintf(fp, "%s", str);
1377 return scnprintf(bf, bfsize, "%s", str);
1380 int callchain_list_counts__printf_value(struct callchain_list *clist,
1381 FILE *fp, char *bf, int bfsize)
1383 u64 branch_count, predicted_count;
1384 u64 abort_count, cycles_count;
1385 u64 iter_count, iter_cycles;
1387 branch_count = clist->branch_count;
1388 predicted_count = clist->predicted_count;
1389 abort_count = clist->abort_count;
1390 cycles_count = clist->cycles_count;
1391 iter_count = clist->iter_count;
1392 iter_cycles = clist->iter_cycles;
1394 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1395 predicted_count, abort_count,
1396 cycles_count, iter_count, iter_cycles,
1397 &clist->brtype_stat);
1400 static void free_callchain_node(struct callchain_node *node)
1402 struct callchain_list *list, *tmp;
1403 struct callchain_node *child;
1406 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1407 list_del(&list->list);
1408 map__zput(list->ms.map);
1412 list_for_each_entry_safe(list, tmp, &node->val, list) {
1413 list_del(&list->list);
1414 map__zput(list->ms.map);
1418 n = rb_first(&node->rb_root_in);
1420 child = container_of(n, struct callchain_node, rb_node_in);
1422 rb_erase(&child->rb_node_in, &node->rb_root_in);
1424 free_callchain_node(child);
1429 void free_callchain(struct callchain_root *root)
1431 if (!symbol_conf.use_callchain)
1434 free_callchain_node(&root->node);
1437 static u64 decay_callchain_node(struct callchain_node *node)
1439 struct callchain_node *child;
1443 n = rb_first(&node->rb_root_in);
1445 child = container_of(n, struct callchain_node, rb_node_in);
1447 child_hits += decay_callchain_node(child);
1451 node->hit = (node->hit * 7) / 8;
1452 node->children_hit = child_hits;
1457 void decay_callchain(struct callchain_root *root)
1459 if (!symbol_conf.use_callchain)
1462 decay_callchain_node(&root->node);
1465 int callchain_node__make_parent_list(struct callchain_node *node)
1467 struct callchain_node *parent = node->parent;
1468 struct callchain_list *chain, *new;
1472 list_for_each_entry_reverse(chain, &parent->val, list) {
1473 new = malloc(sizeof(*new));
1477 new->has_children = false;
1478 map__get(new->ms.map);
1479 list_add_tail(&new->list, &head);
1481 parent = parent->parent;
1484 list_for_each_entry_safe_reverse(chain, new, &head, list)
1485 list_move_tail(&chain->list, &node->parent_val);
1487 if (!list_empty(&node->parent_val)) {
1488 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1489 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1491 chain = list_first_entry(&node->val, struct callchain_list, list);
1492 chain->has_children = false;
1497 list_for_each_entry_safe(chain, new, &head, list) {
1498 list_del(&chain->list);
1499 map__zput(chain->ms.map);
1505 int callchain_cursor__copy(struct callchain_cursor *dst,
1506 struct callchain_cursor *src)
1510 callchain_cursor_reset(dst);
1511 callchain_cursor_commit(src);
1514 struct callchain_cursor_node *node;
1516 node = callchain_cursor_current(src);
1520 rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
1521 node->branch, &node->branch_flags,
1528 callchain_cursor_advance(src);