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;
71 static int parse_callchain_order(const char *value)
73 if (!strncmp(value, "caller", strlen(value))) {
74 callchain_param.order = ORDER_CALLER;
75 callchain_param.order_set = true;
78 if (!strncmp(value, "callee", strlen(value))) {
79 callchain_param.order = ORDER_CALLEE;
80 callchain_param.order_set = true;
86 static int parse_callchain_sort_key(const char *value)
88 if (!strncmp(value, "function", strlen(value))) {
89 callchain_param.key = CCKEY_FUNCTION;
92 if (!strncmp(value, "address", strlen(value))) {
93 callchain_param.key = CCKEY_ADDRESS;
96 if (!strncmp(value, "srcline", strlen(value))) {
97 callchain_param.key = CCKEY_SRCLINE;
100 if (!strncmp(value, "branch", strlen(value))) {
101 callchain_param.branch_callstack = 1;
107 static int parse_callchain_value(const char *value)
109 if (!strncmp(value, "percent", strlen(value))) {
110 callchain_param.value = CCVAL_PERCENT;
113 if (!strncmp(value, "period", strlen(value))) {
114 callchain_param.value = CCVAL_PERIOD;
117 if (!strncmp(value, "count", strlen(value))) {
118 callchain_param.value = CCVAL_COUNT;
124 static int get_stack_size(const char *str, unsigned long *_size)
128 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
130 size = strtoul(str, &endptr, 0);
136 size = round_up(size, sizeof(u64));
137 if (!size || size > max_size)
145 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
151 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
154 char *endptr, *saveptr = NULL;
155 bool minpcnt_set = false;
156 bool record_opt_set = false;
157 bool try_stack_size = false;
159 callchain_param.enabled = true;
160 symbol_conf.use_callchain = true;
165 while ((tok = strtok_r((char *)arg, ",", &saveptr)) != NULL) {
166 if (!strncmp(tok, "none", strlen(tok))) {
167 callchain_param.mode = CHAIN_NONE;
168 callchain_param.enabled = false;
169 symbol_conf.use_callchain = false;
173 if (!parse_callchain_mode(tok) ||
174 !parse_callchain_order(tok) ||
175 !parse_callchain_sort_key(tok) ||
176 !parse_callchain_value(tok)) {
177 /* parsing ok - move on to the next */
178 try_stack_size = false;
180 } else if (allow_record_opt && !record_opt_set) {
181 if (parse_callchain_record(tok, &callchain_param))
184 /* assume that number followed by 'dwarf' is stack size */
185 if (callchain_param.record_mode == CALLCHAIN_DWARF)
186 try_stack_size = true;
188 record_opt_set = true;
193 if (try_stack_size) {
194 unsigned long size = 0;
196 if (get_stack_size(tok, &size) < 0)
198 callchain_param.dump_size = size;
199 try_stack_size = false;
200 } else if (!minpcnt_set) {
201 /* try to get the min percent */
202 callchain_param.min_percent = strtod(tok, &endptr);
207 /* try print limit at last */
208 callchain_param.print_limit = strtoul(tok, &endptr, 0);
216 if (callchain_register_param(&callchain_param) < 0) {
217 pr_err("Can't register callchain params\n");
223 int parse_callchain_report_opt(const char *arg)
225 return __parse_callchain_report_opt(arg, false);
228 int parse_callchain_top_opt(const char *arg)
230 return __parse_callchain_report_opt(arg, true);
233 int parse_callchain_record(const char *arg, struct callchain_param *param)
235 char *tok, *name, *saveptr = NULL;
239 /* We need buffer that we know we can write to. */
240 buf = malloc(strlen(arg) + 1);
246 tok = strtok_r((char *)buf, ",", &saveptr);
247 name = tok ? : (char *)buf;
250 /* Framepointer style */
251 if (!strncmp(name, "fp", sizeof("fp"))) {
252 if (!strtok_r(NULL, ",", &saveptr)) {
253 param->record_mode = CALLCHAIN_FP;
256 pr_err("callchain: No more arguments "
257 "needed for --call-graph fp\n");
261 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
262 const unsigned long default_stack_dump_size = 8192;
265 param->record_mode = CALLCHAIN_DWARF;
266 param->dump_size = default_stack_dump_size;
268 tok = strtok_r(NULL, ",", &saveptr);
270 unsigned long size = 0;
272 ret = get_stack_size(tok, &size);
273 param->dump_size = size;
275 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
276 if (!strtok_r(NULL, ",", &saveptr)) {
277 param->record_mode = CALLCHAIN_LBR;
280 pr_err("callchain: No more arguments "
281 "needed for --call-graph lbr\n");
284 pr_err("callchain: Unknown --call-graph option "
295 int perf_callchain_config(const char *var, const char *value)
299 if (!strstarts(var, "call-graph."))
301 var += sizeof("call-graph.") - 1;
303 if (!strcmp(var, "record-mode"))
304 return parse_callchain_record_opt(value, &callchain_param);
305 if (!strcmp(var, "dump-size")) {
306 unsigned long size = 0;
309 ret = get_stack_size(value, &size);
310 callchain_param.dump_size = size;
314 if (!strcmp(var, "print-type")){
316 ret = parse_callchain_mode(value);
318 pr_err("Invalid callchain mode: %s\n", value);
321 if (!strcmp(var, "order")){
323 ret = parse_callchain_order(value);
325 pr_err("Invalid callchain order: %s\n", value);
328 if (!strcmp(var, "sort-key")){
330 ret = parse_callchain_sort_key(value);
332 pr_err("Invalid callchain sort key: %s\n", value);
335 if (!strcmp(var, "threshold")) {
336 callchain_param.min_percent = strtod(value, &endptr);
337 if (value == endptr) {
338 pr_err("Invalid callchain threshold: %s\n", value);
342 if (!strcmp(var, "print-limit")) {
343 callchain_param.print_limit = strtod(value, &endptr);
344 if (value == endptr) {
345 pr_err("Invalid callchain print limit: %s\n", value);
354 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
355 enum chain_mode mode)
357 struct rb_node **p = &root->rb_node;
358 struct rb_node *parent = NULL;
359 struct callchain_node *rnode;
360 u64 chain_cumul = callchain_cumul_hits(chain);
366 rnode = rb_entry(parent, struct callchain_node, rb_node);
367 rnode_cumul = callchain_cumul_hits(rnode);
372 if (rnode->hit < chain->hit)
377 case CHAIN_GRAPH_ABS: /* Falldown */
378 case CHAIN_GRAPH_REL:
379 if (rnode_cumul < chain_cumul)
390 rb_link_node(&chain->rb_node, parent, p);
391 rb_insert_color(&chain->rb_node, root);
395 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
399 struct callchain_node *child;
401 n = rb_first(&node->rb_root_in);
403 child = rb_entry(n, struct callchain_node, rb_node_in);
406 __sort_chain_flat(rb_root, child, min_hit);
409 if (node->hit && node->hit >= min_hit)
410 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
414 * Once we get every callchains from the stream, we can now
418 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
419 u64 min_hit, struct callchain_param *param __maybe_unused)
422 __sort_chain_flat(rb_root, &root->node, min_hit);
425 static void __sort_chain_graph_abs(struct callchain_node *node,
429 struct callchain_node *child;
431 node->rb_root = RB_ROOT;
432 n = rb_first(&node->rb_root_in);
435 child = rb_entry(n, struct callchain_node, rb_node_in);
438 __sort_chain_graph_abs(child, min_hit);
439 if (callchain_cumul_hits(child) >= min_hit)
440 rb_insert_callchain(&node->rb_root, child,
446 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
447 u64 min_hit, struct callchain_param *param __maybe_unused)
449 __sort_chain_graph_abs(&chain_root->node, min_hit);
450 rb_root->rb_node = chain_root->node.rb_root.rb_node;
453 static void __sort_chain_graph_rel(struct callchain_node *node,
457 struct callchain_node *child;
460 node->rb_root = RB_ROOT;
461 min_hit = ceil(node->children_hit * min_percent);
463 n = rb_first(&node->rb_root_in);
465 child = rb_entry(n, struct callchain_node, rb_node_in);
468 __sort_chain_graph_rel(child, min_percent);
469 if (callchain_cumul_hits(child) >= min_hit)
470 rb_insert_callchain(&node->rb_root, child,
476 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
477 u64 min_hit __maybe_unused, struct callchain_param *param)
479 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
480 rb_root->rb_node = chain_root->node.rb_root.rb_node;
483 int callchain_register_param(struct callchain_param *param)
485 switch (param->mode) {
486 case CHAIN_GRAPH_ABS:
487 param->sort = sort_chain_graph_abs;
489 case CHAIN_GRAPH_REL:
490 param->sort = sort_chain_graph_rel;
494 param->sort = sort_chain_flat;
504 * Create a child for a parent. If inherit_children, then the new child
505 * will become the new parent of it's parent children
507 static struct callchain_node *
508 create_child(struct callchain_node *parent, bool inherit_children)
510 struct callchain_node *new;
512 new = zalloc(sizeof(*new));
514 perror("not enough memory to create child for code path tree");
517 new->parent = parent;
518 INIT_LIST_HEAD(&new->val);
519 INIT_LIST_HEAD(&new->parent_val);
521 if (inherit_children) {
523 struct callchain_node *child;
525 new->rb_root_in = parent->rb_root_in;
526 parent->rb_root_in = RB_ROOT;
528 n = rb_first(&new->rb_root_in);
530 child = rb_entry(n, struct callchain_node, rb_node_in);
535 /* make it the first child */
536 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
537 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
545 * Fill the node with callchain values
548 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
550 struct callchain_cursor_node *cursor_node;
552 node->val_nr = cursor->nr - cursor->pos;
554 pr_warning("Warning: empty node in callchain tree\n");
556 cursor_node = callchain_cursor_current(cursor);
558 while (cursor_node) {
559 struct callchain_list *call;
561 call = zalloc(sizeof(*call));
563 perror("not enough memory for the code path tree");
566 call->ip = cursor_node->ip;
567 call->ms.sym = cursor_node->sym;
568 call->ms.map = map__get(cursor_node->map);
570 if (cursor_node->branch) {
571 call->branch_count = 1;
573 if (cursor_node->branch_from) {
575 * branch_from is set with value somewhere else
576 * to imply it's "to" of a branch.
578 call->brtype_stat.branch_to = true;
580 if (cursor_node->branch_flags.predicted)
581 call->predicted_count = 1;
583 if (cursor_node->branch_flags.abort)
584 call->abort_count = 1;
586 branch_type_count(&call->brtype_stat,
587 &cursor_node->branch_flags,
588 cursor_node->branch_from,
592 * It's "from" of a branch
594 call->brtype_stat.branch_to = false;
596 cursor_node->branch_flags.cycles;
597 call->iter_count = cursor_node->nr_loop_iter;
598 call->iter_cycles = cursor_node->iter_cycles;
602 list_add_tail(&call->list, &node->val);
604 callchain_cursor_advance(cursor);
605 cursor_node = callchain_cursor_current(cursor);
610 static struct callchain_node *
611 add_child(struct callchain_node *parent,
612 struct callchain_cursor *cursor,
615 struct callchain_node *new;
617 new = create_child(parent, false);
621 if (fill_node(new, cursor) < 0) {
622 struct callchain_list *call, *tmp;
624 list_for_each_entry_safe(call, tmp, &new->val, list) {
625 list_del(&call->list);
626 map__zput(call->ms.map);
633 new->children_hit = 0;
635 new->children_count = 0;
647 static enum match_result match_chain_srcline(struct callchain_cursor_node *node,
648 struct callchain_list *cnode)
652 enum match_result ret = MATCH_EQ;
656 left = get_srcline(cnode->ms.map->dso,
657 map__rip_2objdump(cnode->ms.map, cnode->ip),
658 cnode->ms.sym, true, false);
660 right = get_srcline(node->map->dso,
661 map__rip_2objdump(node->map, node->ip),
662 node->sym, true, false);
665 cmp = strcmp(left, right);
666 else if (!left && right)
668 else if (left && !right)
670 else if (cnode->ip == node->ip)
673 cmp = (cnode->ip < node->ip) ? -1 : 1;
676 ret = cmp < 0 ? MATCH_LT : MATCH_GT;
683 static enum match_result match_chain(struct callchain_cursor_node *node,
684 struct callchain_list *cnode)
686 struct symbol *sym = node->sym;
688 struct dso *left_dso = NULL;
689 struct dso *right_dso = NULL;
691 if (callchain_param.key == CCKEY_SRCLINE) {
692 enum match_result match = match_chain_srcline(node, cnode);
694 if (match != MATCH_ERROR)
698 if (cnode->ms.sym && sym && callchain_param.key == CCKEY_FUNCTION) {
699 left = cnode->ms.sym->start;
701 left_dso = cnode->ms.map->dso;
702 right_dso = node->map->dso;
708 if (left == right && left_dso == right_dso) {
710 cnode->branch_count++;
712 if (node->branch_from) {
714 * It's "to" of a branch
716 cnode->brtype_stat.branch_to = true;
718 if (node->branch_flags.predicted)
719 cnode->predicted_count++;
721 if (node->branch_flags.abort)
722 cnode->abort_count++;
724 branch_type_count(&cnode->brtype_stat,
730 * It's "from" of a branch
732 cnode->brtype_stat.branch_to = false;
733 cnode->cycles_count +=
734 node->branch_flags.cycles;
735 cnode->iter_count += node->nr_loop_iter;
736 cnode->iter_cycles += node->iter_cycles;
743 return left > right ? MATCH_GT : MATCH_LT;
747 * Split the parent in two parts (a new child is created) and
748 * give a part of its callchain to the created child.
749 * Then create another child to host the given callchain of new branch
752 split_add_child(struct callchain_node *parent,
753 struct callchain_cursor *cursor,
754 struct callchain_list *to_split,
755 u64 idx_parents, u64 idx_local, u64 period)
757 struct callchain_node *new;
758 struct list_head *old_tail;
759 unsigned int idx_total = idx_parents + idx_local;
762 new = create_child(parent, true);
766 /* split the callchain and move a part to the new child */
767 old_tail = parent->val.prev;
768 list_del_range(&to_split->list, old_tail);
769 new->val.next = &to_split->list;
770 new->val.prev = old_tail;
771 to_split->list.prev = &new->val;
772 old_tail->next = &new->val;
775 new->hit = parent->hit;
776 new->children_hit = parent->children_hit;
777 parent->children_hit = callchain_cumul_hits(new);
778 new->val_nr = parent->val_nr - idx_local;
779 parent->val_nr = idx_local;
780 new->count = parent->count;
781 new->children_count = parent->children_count;
782 parent->children_count = callchain_cumul_counts(new);
784 /* create a new child for the new branch if any */
785 if (idx_total < cursor->nr) {
786 struct callchain_node *first;
787 struct callchain_list *cnode;
788 struct callchain_cursor_node *node;
789 struct rb_node *p, **pp;
792 parent->children_hit += period;
794 parent->children_count += 1;
796 node = callchain_cursor_current(cursor);
797 new = add_child(parent, cursor, period);
802 * This is second child since we moved parent's children
803 * to new (first) child above.
805 p = parent->rb_root_in.rb_node;
806 first = rb_entry(p, struct callchain_node, rb_node_in);
807 cnode = list_first_entry(&first->val, struct callchain_list,
810 if (match_chain(node, cnode) == MATCH_LT)
815 rb_link_node(&new->rb_node_in, p, pp);
816 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
818 parent->hit = period;
824 static enum match_result
825 append_chain(struct callchain_node *root,
826 struct callchain_cursor *cursor,
830 append_chain_children(struct callchain_node *root,
831 struct callchain_cursor *cursor,
834 struct callchain_node *rnode;
835 struct callchain_cursor_node *node;
836 struct rb_node **p = &root->rb_root_in.rb_node;
837 struct rb_node *parent = NULL;
839 node = callchain_cursor_current(cursor);
843 /* lookup in childrens */
845 enum match_result ret;
848 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
850 /* If at least first entry matches, rely to children */
851 ret = append_chain(rnode, cursor, period);
853 goto inc_children_hit;
854 if (ret == MATCH_ERROR)
858 p = &parent->rb_left;
860 p = &parent->rb_right;
862 /* nothing in children, add to the current node */
863 rnode = add_child(root, cursor, period);
867 rb_link_node(&rnode->rb_node_in, parent, p);
868 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
871 root->children_hit += period;
872 root->children_count++;
876 static enum match_result
877 append_chain(struct callchain_node *root,
878 struct callchain_cursor *cursor,
881 struct callchain_list *cnode;
882 u64 start = cursor->pos;
885 enum match_result cmp = MATCH_ERROR;
888 * Lookup in the current node
889 * If we have a symbol, then compare the start to match
890 * anywhere inside a function, unless function
893 list_for_each_entry(cnode, &root->val, list) {
894 struct callchain_cursor_node *node;
896 node = callchain_cursor_current(cursor);
900 cmp = match_chain(node, cnode);
906 callchain_cursor_advance(cursor);
909 /* matches not, relay no the parent */
911 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
915 matches = cursor->pos - start;
917 /* we match only a part of the node. Split it and add the new chain */
918 if (matches < root->val_nr) {
919 if (split_add_child(root, cursor, cnode, start, matches,
926 /* we match 100% of the path, increment the hit */
927 if (matches == root->val_nr && cursor->pos == cursor->nr) {
933 /* We match the node and still have a part remaining */
934 if (append_chain_children(root, cursor, period) < 0)
940 int callchain_append(struct callchain_root *root,
941 struct callchain_cursor *cursor,
947 callchain_cursor_commit(cursor);
949 if (append_chain_children(&root->node, cursor, period) < 0)
952 if (cursor->nr > root->max_depth)
953 root->max_depth = cursor->nr;
959 merge_chain_branch(struct callchain_cursor *cursor,
960 struct callchain_node *dst, struct callchain_node *src)
962 struct callchain_cursor_node **old_last = cursor->last;
963 struct callchain_node *child;
964 struct callchain_list *list, *next_list;
966 int old_pos = cursor->nr;
969 list_for_each_entry_safe(list, next_list, &src->val, list) {
970 callchain_cursor_append(cursor, list->ip,
971 list->ms.map, list->ms.sym,
972 false, NULL, 0, 0, 0);
973 list_del(&list->list);
974 map__zput(list->ms.map);
979 callchain_cursor_commit(cursor);
980 if (append_chain_children(dst, cursor, src->hit) < 0)
984 n = rb_first(&src->rb_root_in);
986 child = container_of(n, struct callchain_node, rb_node_in);
988 rb_erase(&child->rb_node_in, &src->rb_root_in);
990 err = merge_chain_branch(cursor, dst, child);
997 cursor->nr = old_pos;
998 cursor->last = old_last;
1003 int callchain_merge(struct callchain_cursor *cursor,
1004 struct callchain_root *dst, struct callchain_root *src)
1006 return merge_chain_branch(cursor, &dst->node, &src->node);
1009 int callchain_cursor_append(struct callchain_cursor *cursor,
1010 u64 ip, struct map *map, struct symbol *sym,
1011 bool branch, struct branch_flags *flags,
1012 int nr_loop_iter, u64 iter_cycles, u64 branch_from)
1014 struct callchain_cursor_node *node = *cursor->last;
1017 node = calloc(1, sizeof(*node));
1021 *cursor->last = node;
1025 map__zput(node->map);
1026 node->map = map__get(map);
1028 node->branch = branch;
1029 node->nr_loop_iter = nr_loop_iter;
1030 node->iter_cycles = iter_cycles;
1033 memcpy(&node->branch_flags, flags,
1034 sizeof(struct branch_flags));
1036 node->branch_from = branch_from;
1039 cursor->last = &node->next;
1044 int sample__resolve_callchain(struct perf_sample *sample,
1045 struct callchain_cursor *cursor, struct symbol **parent,
1046 struct perf_evsel *evsel, struct addr_location *al,
1049 if (sample->callchain == NULL && !symbol_conf.show_branchflag_count)
1052 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
1053 perf_hpp_list.parent || symbol_conf.show_branchflag_count) {
1054 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
1055 parent, al, max_stack);
1060 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
1062 if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
1063 !symbol_conf.show_branchflag_count)
1065 return callchain_append(he->callchain, &callchain_cursor, sample->period);
1068 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
1069 bool hide_unresolved)
1071 al->map = node->map;
1072 al->sym = node->sym;
1074 al->addr = node->map->map_ip(node->map, node->ip);
1076 al->addr = node->ip;
1078 if (al->sym == NULL) {
1079 if (hide_unresolved)
1081 if (al->map == NULL)
1085 if (al->map->groups == &al->machine->kmaps) {
1086 if (machine__is_host(al->machine)) {
1087 al->cpumode = PERF_RECORD_MISC_KERNEL;
1090 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
1094 if (machine__is_host(al->machine)) {
1095 al->cpumode = PERF_RECORD_MISC_USER;
1097 } else if (perf_guest) {
1098 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
1101 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
1110 char *callchain_list__sym_name(struct callchain_list *cl,
1111 char *bf, size_t bfsize, bool show_dso)
1113 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1114 bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
1118 if (show_srcline && cl->ms.map && !cl->srcline)
1119 cl->srcline = get_srcline(cl->ms.map->dso,
1120 map__rip_2objdump(cl->ms.map,
1122 cl->ms.sym, false, show_addr);
1124 printed = scnprintf(bf, bfsize, "%s %s",
1125 cl->ms.sym->name, cl->srcline);
1127 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
1129 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
1132 scnprintf(bf + printed, bfsize - printed, " %s",
1134 cl->ms.map->dso->short_name :
1140 char *callchain_node__scnprintf_value(struct callchain_node *node,
1141 char *bf, size_t bfsize, u64 total)
1143 double percent = 0.0;
1144 u64 period = callchain_cumul_hits(node);
1145 unsigned count = callchain_cumul_counts(node);
1147 if (callchain_param.mode == CHAIN_FOLDED) {
1149 count = node->count;
1152 switch (callchain_param.value) {
1154 scnprintf(bf, bfsize, "%"PRIu64, period);
1157 scnprintf(bf, bfsize, "%u", count);
1162 percent = period * 100.0 / total;
1163 scnprintf(bf, bfsize, "%.2f%%", percent);
1169 int callchain_node__fprintf_value(struct callchain_node *node,
1170 FILE *fp, 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 return fprintf(fp, "%"PRIu64, period);
1185 return fprintf(fp, "%u", count);
1189 percent = period * 100.0 / total;
1190 return percent_color_fprintf(fp, "%.2f%%", percent);
1195 static void callchain_counts_value(struct callchain_node *node,
1196 u64 *branch_count, u64 *predicted_count,
1197 u64 *abort_count, u64 *cycles_count)
1199 struct callchain_list *clist;
1201 list_for_each_entry(clist, &node->val, list) {
1203 *branch_count += clist->branch_count;
1205 if (predicted_count)
1206 *predicted_count += clist->predicted_count;
1209 *abort_count += clist->abort_count;
1212 *cycles_count += clist->cycles_count;
1216 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1218 u64 *predicted_count,
1222 struct callchain_node *child;
1225 n = rb_first(&node->rb_root_in);
1227 child = rb_entry(n, struct callchain_node, rb_node_in);
1230 callchain_node_branch_counts_cumul(child, branch_count,
1235 callchain_counts_value(child, branch_count,
1236 predicted_count, abort_count,
1243 int callchain_branch_counts(struct callchain_root *root,
1244 u64 *branch_count, u64 *predicted_count,
1245 u64 *abort_count, u64 *cycles_count)
1250 if (predicted_count)
1251 *predicted_count = 0;
1259 return callchain_node_branch_counts_cumul(&root->node,
1266 static int count_pri64_printf(int idx, const char *str, u64 value, char *bf, int bfsize)
1270 printed = scnprintf(bf, bfsize, "%s%s:%" PRId64 "", (idx) ? " " : " (", str, value);
1275 static int count_float_printf(int idx, const char *str, float value,
1276 char *bf, int bfsize, float threshold)
1280 if (threshold != 0.0 && value < threshold)
1283 printed = scnprintf(bf, bfsize, "%s%s:%.1f%%", (idx) ? " " : " (", str, value);
1288 static int branch_to_str(char *bf, int bfsize,
1289 u64 branch_count, u64 predicted_count,
1291 struct branch_type_stat *brtype_stat)
1295 printed = branch_type_str(brtype_stat, bf, bfsize);
1299 if (predicted_count < branch_count) {
1300 printed += count_float_printf(i++, "predicted",
1301 predicted_count * 100.0 / branch_count,
1302 bf + printed, bfsize - printed, 0.0);
1306 printed += count_float_printf(i++, "abort",
1307 abort_count * 100.0 / branch_count,
1308 bf + printed, bfsize - printed, 0.1);
1312 printed += scnprintf(bf + printed, bfsize - printed, ")");
1317 static int branch_from_str(char *bf, int bfsize,
1319 u64 cycles_count, u64 iter_count,
1322 int printed = 0, i = 0;
1325 cycles = cycles_count / branch_count;
1327 printed += count_pri64_printf(i++, "cycles",
1329 bf + printed, bfsize - printed);
1333 printed += count_pri64_printf(i++, "iter",
1335 bf + printed, bfsize - printed);
1337 printed += count_pri64_printf(i++, "avg_cycles",
1338 iter_cycles / iter_count,
1339 bf + printed, bfsize - printed);
1343 printed += scnprintf(bf + printed, bfsize - printed, ")");
1348 static int counts_str_build(char *bf, int bfsize,
1349 u64 branch_count, u64 predicted_count,
1350 u64 abort_count, u64 cycles_count,
1351 u64 iter_count, u64 iter_cycles,
1352 struct branch_type_stat *brtype_stat)
1356 if (branch_count == 0)
1357 return scnprintf(bf, bfsize, " (calltrace)");
1359 if (brtype_stat->branch_to) {
1360 printed = branch_to_str(bf, bfsize, branch_count,
1361 predicted_count, abort_count, brtype_stat);
1363 printed = branch_from_str(bf, bfsize, branch_count,
1364 cycles_count, iter_count, iter_cycles);
1373 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1374 u64 branch_count, u64 predicted_count,
1375 u64 abort_count, u64 cycles_count,
1376 u64 iter_count, u64 iter_cycles,
1377 struct branch_type_stat *brtype_stat)
1381 counts_str_build(str, sizeof(str), branch_count,
1382 predicted_count, abort_count, cycles_count,
1383 iter_count, iter_cycles, brtype_stat);
1386 return fprintf(fp, "%s", str);
1388 return scnprintf(bf, bfsize, "%s", str);
1391 int callchain_list_counts__printf_value(struct callchain_list *clist,
1392 FILE *fp, char *bf, int bfsize)
1394 u64 branch_count, predicted_count;
1395 u64 abort_count, cycles_count;
1396 u64 iter_count, iter_cycles;
1398 branch_count = clist->branch_count;
1399 predicted_count = clist->predicted_count;
1400 abort_count = clist->abort_count;
1401 cycles_count = clist->cycles_count;
1402 iter_count = clist->iter_count;
1403 iter_cycles = clist->iter_cycles;
1405 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1406 predicted_count, abort_count,
1407 cycles_count, iter_count, iter_cycles,
1408 &clist->brtype_stat);
1411 static void free_callchain_node(struct callchain_node *node)
1413 struct callchain_list *list, *tmp;
1414 struct callchain_node *child;
1417 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1418 list_del(&list->list);
1419 map__zput(list->ms.map);
1423 list_for_each_entry_safe(list, tmp, &node->val, list) {
1424 list_del(&list->list);
1425 map__zput(list->ms.map);
1429 n = rb_first(&node->rb_root_in);
1431 child = container_of(n, struct callchain_node, rb_node_in);
1433 rb_erase(&child->rb_node_in, &node->rb_root_in);
1435 free_callchain_node(child);
1440 void free_callchain(struct callchain_root *root)
1442 if (!symbol_conf.use_callchain)
1445 free_callchain_node(&root->node);
1448 static u64 decay_callchain_node(struct callchain_node *node)
1450 struct callchain_node *child;
1454 n = rb_first(&node->rb_root_in);
1456 child = container_of(n, struct callchain_node, rb_node_in);
1458 child_hits += decay_callchain_node(child);
1462 node->hit = (node->hit * 7) / 8;
1463 node->children_hit = child_hits;
1468 void decay_callchain(struct callchain_root *root)
1470 if (!symbol_conf.use_callchain)
1473 decay_callchain_node(&root->node);
1476 int callchain_node__make_parent_list(struct callchain_node *node)
1478 struct callchain_node *parent = node->parent;
1479 struct callchain_list *chain, *new;
1483 list_for_each_entry_reverse(chain, &parent->val, list) {
1484 new = malloc(sizeof(*new));
1488 new->has_children = false;
1489 map__get(new->ms.map);
1490 list_add_tail(&new->list, &head);
1492 parent = parent->parent;
1495 list_for_each_entry_safe_reverse(chain, new, &head, list)
1496 list_move_tail(&chain->list, &node->parent_val);
1498 if (!list_empty(&node->parent_val)) {
1499 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1500 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1502 chain = list_first_entry(&node->val, struct callchain_list, list);
1503 chain->has_children = false;
1508 list_for_each_entry_safe(chain, new, &head, list) {
1509 list_del(&chain->list);
1510 map__zput(chain->ms.map);
1516 int callchain_cursor__copy(struct callchain_cursor *dst,
1517 struct callchain_cursor *src)
1521 callchain_cursor_reset(dst);
1522 callchain_cursor_commit(src);
1525 struct callchain_cursor_node *node;
1527 node = callchain_cursor_current(src);
1531 rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
1532 node->branch, &node->branch_flags,
1539 callchain_cursor_advance(src);