2 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree.
7 * Using a radix for code path provides a fast retrieval and factorizes
8 * memory use. Also that lets us use the paths in a hierarchical graph view.
24 #include "callchain.h"
26 __thread struct callchain_cursor callchain_cursor;
28 int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
30 return parse_callchain_record(arg, param);
33 static int parse_callchain_mode(const char *value)
35 if (!strncmp(value, "graph", strlen(value))) {
36 callchain_param.mode = CHAIN_GRAPH_ABS;
39 if (!strncmp(value, "flat", strlen(value))) {
40 callchain_param.mode = CHAIN_FLAT;
43 if (!strncmp(value, "fractal", strlen(value))) {
44 callchain_param.mode = CHAIN_GRAPH_REL;
47 if (!strncmp(value, "folded", strlen(value))) {
48 callchain_param.mode = CHAIN_FOLDED;
52 pr_err("Invalid callchain mode: %s\n", value);
56 static int parse_callchain_order(const char *value)
58 if (!strncmp(value, "caller", strlen(value))) {
59 callchain_param.order = ORDER_CALLER;
60 callchain_param.order_set = true;
63 if (!strncmp(value, "callee", strlen(value))) {
64 callchain_param.order = ORDER_CALLEE;
65 callchain_param.order_set = true;
69 pr_err("Invalid callchain order: %s\n", value);
73 static int parse_callchain_sort_key(const char *value)
75 if (!strncmp(value, "function", strlen(value))) {
76 callchain_param.key = CCKEY_FUNCTION;
79 if (!strncmp(value, "address", strlen(value))) {
80 callchain_param.key = CCKEY_ADDRESS;
83 if (!strncmp(value, "branch", strlen(value))) {
84 callchain_param.branch_callstack = 1;
88 pr_err("Invalid callchain sort key: %s\n", value);
92 static int parse_callchain_value(const char *value)
94 if (!strncmp(value, "percent", strlen(value))) {
95 callchain_param.value = CCVAL_PERCENT;
98 if (!strncmp(value, "period", strlen(value))) {
99 callchain_param.value = CCVAL_PERIOD;
102 if (!strncmp(value, "count", strlen(value))) {
103 callchain_param.value = CCVAL_COUNT;
107 pr_err("Invalid callchain config key: %s\n", value);
112 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
116 bool minpcnt_set = false;
117 bool record_opt_set = false;
118 bool try_stack_size = false;
120 callchain_param.enabled = true;
121 symbol_conf.use_callchain = true;
126 while ((tok = strtok((char *)arg, ",")) != NULL) {
127 if (!strncmp(tok, "none", strlen(tok))) {
128 callchain_param.mode = CHAIN_NONE;
129 callchain_param.enabled = false;
130 symbol_conf.use_callchain = false;
134 if (!parse_callchain_mode(tok) ||
135 !parse_callchain_order(tok) ||
136 !parse_callchain_sort_key(tok) ||
137 !parse_callchain_value(tok)) {
138 /* parsing ok - move on to the next */
139 try_stack_size = false;
141 } else if (allow_record_opt && !record_opt_set) {
142 if (parse_callchain_record(tok, &callchain_param))
145 /* assume that number followed by 'dwarf' is stack size */
146 if (callchain_param.record_mode == CALLCHAIN_DWARF)
147 try_stack_size = true;
149 record_opt_set = true;
154 if (try_stack_size) {
155 unsigned long size = 0;
157 if (get_stack_size(tok, &size) < 0)
159 callchain_param.dump_size = size;
160 try_stack_size = false;
161 } else if (!minpcnt_set) {
162 /* try to get the min percent */
163 callchain_param.min_percent = strtod(tok, &endptr);
168 /* try print limit at last */
169 callchain_param.print_limit = strtoul(tok, &endptr, 0);
177 if (callchain_register_param(&callchain_param) < 0) {
178 pr_err("Can't register callchain params\n");
184 int parse_callchain_report_opt(const char *arg)
186 return __parse_callchain_report_opt(arg, false);
189 int parse_callchain_top_opt(const char *arg)
191 return __parse_callchain_report_opt(arg, true);
194 int perf_callchain_config(const char *var, const char *value)
198 if (prefixcmp(var, "call-graph."))
200 var += sizeof("call-graph.") - 1;
202 if (!strcmp(var, "record-mode"))
203 return parse_callchain_record_opt(value, &callchain_param);
204 if (!strcmp(var, "dump-size")) {
205 unsigned long size = 0;
208 ret = get_stack_size(value, &size);
209 callchain_param.dump_size = size;
213 if (!strcmp(var, "print-type"))
214 return parse_callchain_mode(value);
215 if (!strcmp(var, "order"))
216 return parse_callchain_order(value);
217 if (!strcmp(var, "sort-key"))
218 return parse_callchain_sort_key(value);
219 if (!strcmp(var, "threshold")) {
220 callchain_param.min_percent = strtod(value, &endptr);
221 if (value == endptr) {
222 pr_err("Invalid callchain threshold: %s\n", value);
226 if (!strcmp(var, "print-limit")) {
227 callchain_param.print_limit = strtod(value, &endptr);
228 if (value == endptr) {
229 pr_err("Invalid callchain print limit: %s\n", value);
238 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
239 enum chain_mode mode)
241 struct rb_node **p = &root->rb_node;
242 struct rb_node *parent = NULL;
243 struct callchain_node *rnode;
244 u64 chain_cumul = callchain_cumul_hits(chain);
250 rnode = rb_entry(parent, struct callchain_node, rb_node);
251 rnode_cumul = callchain_cumul_hits(rnode);
256 if (rnode->hit < chain->hit)
261 case CHAIN_GRAPH_ABS: /* Falldown */
262 case CHAIN_GRAPH_REL:
263 if (rnode_cumul < chain_cumul)
274 rb_link_node(&chain->rb_node, parent, p);
275 rb_insert_color(&chain->rb_node, root);
279 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
283 struct callchain_node *child;
285 n = rb_first(&node->rb_root_in);
287 child = rb_entry(n, struct callchain_node, rb_node_in);
290 __sort_chain_flat(rb_root, child, min_hit);
293 if (node->hit && node->hit >= min_hit)
294 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
298 * Once we get every callchains from the stream, we can now
302 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
303 u64 min_hit, struct callchain_param *param __maybe_unused)
306 __sort_chain_flat(rb_root, &root->node, min_hit);
309 static void __sort_chain_graph_abs(struct callchain_node *node,
313 struct callchain_node *child;
315 node->rb_root = RB_ROOT;
316 n = rb_first(&node->rb_root_in);
319 child = rb_entry(n, struct callchain_node, rb_node_in);
322 __sort_chain_graph_abs(child, min_hit);
323 if (callchain_cumul_hits(child) >= min_hit)
324 rb_insert_callchain(&node->rb_root, child,
330 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
331 u64 min_hit, struct callchain_param *param __maybe_unused)
333 __sort_chain_graph_abs(&chain_root->node, min_hit);
334 rb_root->rb_node = chain_root->node.rb_root.rb_node;
337 static void __sort_chain_graph_rel(struct callchain_node *node,
341 struct callchain_node *child;
344 node->rb_root = RB_ROOT;
345 min_hit = ceil(node->children_hit * min_percent);
347 n = rb_first(&node->rb_root_in);
349 child = rb_entry(n, struct callchain_node, rb_node_in);
352 __sort_chain_graph_rel(child, min_percent);
353 if (callchain_cumul_hits(child) >= min_hit)
354 rb_insert_callchain(&node->rb_root, child,
360 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
361 u64 min_hit __maybe_unused, struct callchain_param *param)
363 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
364 rb_root->rb_node = chain_root->node.rb_root.rb_node;
367 int callchain_register_param(struct callchain_param *param)
369 switch (param->mode) {
370 case CHAIN_GRAPH_ABS:
371 param->sort = sort_chain_graph_abs;
373 case CHAIN_GRAPH_REL:
374 param->sort = sort_chain_graph_rel;
378 param->sort = sort_chain_flat;
388 * Create a child for a parent. If inherit_children, then the new child
389 * will become the new parent of it's parent children
391 static struct callchain_node *
392 create_child(struct callchain_node *parent, bool inherit_children)
394 struct callchain_node *new;
396 new = zalloc(sizeof(*new));
398 perror("not enough memory to create child for code path tree");
401 new->parent = parent;
402 INIT_LIST_HEAD(&new->val);
403 INIT_LIST_HEAD(&new->parent_val);
405 if (inherit_children) {
407 struct callchain_node *child;
409 new->rb_root_in = parent->rb_root_in;
410 parent->rb_root_in = RB_ROOT;
412 n = rb_first(&new->rb_root_in);
414 child = rb_entry(n, struct callchain_node, rb_node_in);
419 /* make it the first child */
420 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
421 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
429 * Fill the node with callchain values
432 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
434 struct callchain_cursor_node *cursor_node;
436 node->val_nr = cursor->nr - cursor->pos;
438 pr_warning("Warning: empty node in callchain tree\n");
440 cursor_node = callchain_cursor_current(cursor);
442 while (cursor_node) {
443 struct callchain_list *call;
445 call = zalloc(sizeof(*call));
447 perror("not enough memory for the code path tree");
450 call->ip = cursor_node->ip;
451 call->ms.sym = cursor_node->sym;
452 call->ms.map = map__get(cursor_node->map);
454 if (cursor_node->branch) {
455 call->branch_count = 1;
457 if (cursor_node->branch_flags.predicted)
458 call->predicted_count = 1;
460 if (cursor_node->branch_flags.abort)
461 call->abort_count = 1;
463 call->cycles_count = cursor_node->branch_flags.cycles;
464 call->iter_count = cursor_node->nr_loop_iter;
465 call->samples_count = cursor_node->samples;
468 list_add_tail(&call->list, &node->val);
470 callchain_cursor_advance(cursor);
471 cursor_node = callchain_cursor_current(cursor);
476 static struct callchain_node *
477 add_child(struct callchain_node *parent,
478 struct callchain_cursor *cursor,
481 struct callchain_node *new;
483 new = create_child(parent, false);
487 if (fill_node(new, cursor) < 0) {
488 struct callchain_list *call, *tmp;
490 list_for_each_entry_safe(call, tmp, &new->val, list) {
491 list_del(&call->list);
492 map__zput(call->ms.map);
499 new->children_hit = 0;
501 new->children_count = 0;
513 static enum match_result match_chain(struct callchain_cursor_node *node,
514 struct callchain_list *cnode)
516 struct symbol *sym = node->sym;
519 if (cnode->ms.sym && sym &&
520 callchain_param.key == CCKEY_FUNCTION) {
521 left = cnode->ms.sym->start;
530 cnode->branch_count++;
532 if (node->branch_flags.predicted)
533 cnode->predicted_count++;
535 if (node->branch_flags.abort)
536 cnode->abort_count++;
538 cnode->cycles_count += node->branch_flags.cycles;
539 cnode->iter_count += node->nr_loop_iter;
540 cnode->samples_count += node->samples;
546 return left > right ? MATCH_GT : MATCH_LT;
550 * Split the parent in two parts (a new child is created) and
551 * give a part of its callchain to the created child.
552 * Then create another child to host the given callchain of new branch
555 split_add_child(struct callchain_node *parent,
556 struct callchain_cursor *cursor,
557 struct callchain_list *to_split,
558 u64 idx_parents, u64 idx_local, u64 period)
560 struct callchain_node *new;
561 struct list_head *old_tail;
562 unsigned int idx_total = idx_parents + idx_local;
565 new = create_child(parent, true);
569 /* split the callchain and move a part to the new child */
570 old_tail = parent->val.prev;
571 list_del_range(&to_split->list, old_tail);
572 new->val.next = &to_split->list;
573 new->val.prev = old_tail;
574 to_split->list.prev = &new->val;
575 old_tail->next = &new->val;
578 new->hit = parent->hit;
579 new->children_hit = parent->children_hit;
580 parent->children_hit = callchain_cumul_hits(new);
581 new->val_nr = parent->val_nr - idx_local;
582 parent->val_nr = idx_local;
583 new->count = parent->count;
584 new->children_count = parent->children_count;
585 parent->children_count = callchain_cumul_counts(new);
587 /* create a new child for the new branch if any */
588 if (idx_total < cursor->nr) {
589 struct callchain_node *first;
590 struct callchain_list *cnode;
591 struct callchain_cursor_node *node;
592 struct rb_node *p, **pp;
595 parent->children_hit += period;
597 parent->children_count += 1;
599 node = callchain_cursor_current(cursor);
600 new = add_child(parent, cursor, period);
605 * This is second child since we moved parent's children
606 * to new (first) child above.
608 p = parent->rb_root_in.rb_node;
609 first = rb_entry(p, struct callchain_node, rb_node_in);
610 cnode = list_first_entry(&first->val, struct callchain_list,
613 if (match_chain(node, cnode) == MATCH_LT)
618 rb_link_node(&new->rb_node_in, p, pp);
619 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
621 parent->hit = period;
627 static enum match_result
628 append_chain(struct callchain_node *root,
629 struct callchain_cursor *cursor,
633 append_chain_children(struct callchain_node *root,
634 struct callchain_cursor *cursor,
637 struct callchain_node *rnode;
638 struct callchain_cursor_node *node;
639 struct rb_node **p = &root->rb_root_in.rb_node;
640 struct rb_node *parent = NULL;
642 node = callchain_cursor_current(cursor);
646 /* lookup in childrens */
648 enum match_result ret;
651 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
653 /* If at least first entry matches, rely to children */
654 ret = append_chain(rnode, cursor, period);
656 goto inc_children_hit;
657 if (ret == MATCH_ERROR)
661 p = &parent->rb_left;
663 p = &parent->rb_right;
665 /* nothing in children, add to the current node */
666 rnode = add_child(root, cursor, period);
670 rb_link_node(&rnode->rb_node_in, parent, p);
671 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
674 root->children_hit += period;
675 root->children_count++;
679 static enum match_result
680 append_chain(struct callchain_node *root,
681 struct callchain_cursor *cursor,
684 struct callchain_list *cnode;
685 u64 start = cursor->pos;
688 enum match_result cmp = MATCH_ERROR;
691 * Lookup in the current node
692 * If we have a symbol, then compare the start to match
693 * anywhere inside a function, unless function
696 list_for_each_entry(cnode, &root->val, list) {
697 struct callchain_cursor_node *node;
699 node = callchain_cursor_current(cursor);
703 cmp = match_chain(node, cnode);
709 callchain_cursor_advance(cursor);
712 /* matches not, relay no the parent */
714 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
718 matches = cursor->pos - start;
720 /* we match only a part of the node. Split it and add the new chain */
721 if (matches < root->val_nr) {
722 if (split_add_child(root, cursor, cnode, start, matches,
729 /* we match 100% of the path, increment the hit */
730 if (matches == root->val_nr && cursor->pos == cursor->nr) {
736 /* We match the node and still have a part remaining */
737 if (append_chain_children(root, cursor, period) < 0)
743 int callchain_append(struct callchain_root *root,
744 struct callchain_cursor *cursor,
750 callchain_cursor_commit(cursor);
752 if (append_chain_children(&root->node, cursor, period) < 0)
755 if (cursor->nr > root->max_depth)
756 root->max_depth = cursor->nr;
762 merge_chain_branch(struct callchain_cursor *cursor,
763 struct callchain_node *dst, struct callchain_node *src)
765 struct callchain_cursor_node **old_last = cursor->last;
766 struct callchain_node *child;
767 struct callchain_list *list, *next_list;
769 int old_pos = cursor->nr;
772 list_for_each_entry_safe(list, next_list, &src->val, list) {
773 callchain_cursor_append(cursor, list->ip,
774 list->ms.map, list->ms.sym,
776 list_del(&list->list);
777 map__zput(list->ms.map);
782 callchain_cursor_commit(cursor);
783 if (append_chain_children(dst, cursor, src->hit) < 0)
787 n = rb_first(&src->rb_root_in);
789 child = container_of(n, struct callchain_node, rb_node_in);
791 rb_erase(&child->rb_node_in, &src->rb_root_in);
793 err = merge_chain_branch(cursor, dst, child);
800 cursor->nr = old_pos;
801 cursor->last = old_last;
806 int callchain_merge(struct callchain_cursor *cursor,
807 struct callchain_root *dst, struct callchain_root *src)
809 return merge_chain_branch(cursor, &dst->node, &src->node);
812 int callchain_cursor_append(struct callchain_cursor *cursor,
813 u64 ip, struct map *map, struct symbol *sym,
814 bool branch, struct branch_flags *flags,
815 int nr_loop_iter, int samples)
817 struct callchain_cursor_node *node = *cursor->last;
820 node = calloc(1, sizeof(*node));
824 *cursor->last = node;
828 map__zput(node->map);
829 node->map = map__get(map);
831 node->branch = branch;
832 node->nr_loop_iter = nr_loop_iter;
833 node->samples = samples;
836 memcpy(&node->branch_flags, flags,
837 sizeof(struct branch_flags));
841 cursor->last = &node->next;
846 int sample__resolve_callchain(struct perf_sample *sample,
847 struct callchain_cursor *cursor, struct symbol **parent,
848 struct perf_evsel *evsel, struct addr_location *al,
851 if (sample->callchain == NULL)
854 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
855 perf_hpp_list.parent) {
856 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
857 parent, al, max_stack);
862 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
864 if (!symbol_conf.use_callchain || sample->callchain == NULL)
866 return callchain_append(he->callchain, &callchain_cursor, sample->period);
869 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
870 bool hide_unresolved)
875 al->addr = node->map->map_ip(node->map, node->ip);
879 if (al->sym == NULL) {
886 if (al->map->groups == &al->machine->kmaps) {
887 if (machine__is_host(al->machine)) {
888 al->cpumode = PERF_RECORD_MISC_KERNEL;
891 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
895 if (machine__is_host(al->machine)) {
896 al->cpumode = PERF_RECORD_MISC_USER;
898 } else if (perf_guest) {
899 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
902 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
911 char *callchain_list__sym_name(struct callchain_list *cl,
912 char *bf, size_t bfsize, bool show_dso)
917 if (callchain_param.key == CCKEY_ADDRESS &&
918 cl->ms.map && !cl->srcline)
919 cl->srcline = get_srcline(cl->ms.map->dso,
920 map__rip_2objdump(cl->ms.map,
924 printed = scnprintf(bf, bfsize, "%s %s",
925 cl->ms.sym->name, cl->srcline);
927 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
929 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
932 scnprintf(bf + printed, bfsize - printed, " %s",
934 cl->ms.map->dso->short_name :
940 char *callchain_node__scnprintf_value(struct callchain_node *node,
941 char *bf, size_t bfsize, u64 total)
943 double percent = 0.0;
944 u64 period = callchain_cumul_hits(node);
945 unsigned count = callchain_cumul_counts(node);
947 if (callchain_param.mode == CHAIN_FOLDED) {
952 switch (callchain_param.value) {
954 scnprintf(bf, bfsize, "%"PRIu64, period);
957 scnprintf(bf, bfsize, "%u", count);
962 percent = period * 100.0 / total;
963 scnprintf(bf, bfsize, "%.2f%%", percent);
969 int callchain_node__fprintf_value(struct callchain_node *node,
972 double percent = 0.0;
973 u64 period = callchain_cumul_hits(node);
974 unsigned count = callchain_cumul_counts(node);
976 if (callchain_param.mode == CHAIN_FOLDED) {
981 switch (callchain_param.value) {
983 return fprintf(fp, "%"PRIu64, period);
985 return fprintf(fp, "%u", count);
989 percent = period * 100.0 / total;
990 return percent_color_fprintf(fp, "%.2f%%", percent);
995 static void callchain_counts_value(struct callchain_node *node,
996 u64 *branch_count, u64 *predicted_count,
997 u64 *abort_count, u64 *cycles_count)
999 struct callchain_list *clist;
1001 list_for_each_entry(clist, &node->val, list) {
1003 *branch_count += clist->branch_count;
1005 if (predicted_count)
1006 *predicted_count += clist->predicted_count;
1009 *abort_count += clist->abort_count;
1012 *cycles_count += clist->cycles_count;
1016 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1018 u64 *predicted_count,
1022 struct callchain_node *child;
1025 n = rb_first(&node->rb_root_in);
1027 child = rb_entry(n, struct callchain_node, rb_node_in);
1030 callchain_node_branch_counts_cumul(child, branch_count,
1035 callchain_counts_value(child, branch_count,
1036 predicted_count, abort_count,
1043 int callchain_branch_counts(struct callchain_root *root,
1044 u64 *branch_count, u64 *predicted_count,
1045 u64 *abort_count, u64 *cycles_count)
1050 if (predicted_count)
1051 *predicted_count = 0;
1059 return callchain_node_branch_counts_cumul(&root->node,
1066 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1067 u64 branch_count, u64 predicted_count,
1068 u64 abort_count, u64 cycles_count,
1069 u64 iter_count, u64 samples_count)
1071 double predicted_percent = 0.0;
1072 const char *null_str = "";
1077 if (branch_count == 0) {
1079 return fprintf(fp, " (calltrace)");
1081 return scnprintf(bf, bfsize, " (calltrace)");
1084 if (iter_count && samples_count) {
1085 scnprintf(iter_str, sizeof(iter_str),
1086 ", iterations:%" PRId64 "",
1087 iter_count / samples_count);
1090 str = (char *)null_str;
1092 predicted_percent = predicted_count * 100.0 / branch_count;
1093 cycles = cycles_count / branch_count;
1095 if ((predicted_percent >= 100.0) && (abort_count == 0)) {
1097 return fprintf(fp, " (cycles:%" PRId64 "%s)",
1100 return scnprintf(bf, bfsize, " (cycles:%" PRId64 "%s)",
1104 if ((predicted_percent < 100.0) && (abort_count == 0)) {
1107 " (predicted:%.1f%%, cycles:%" PRId64 "%s)",
1108 predicted_percent, cycles, str);
1110 return scnprintf(bf, bfsize,
1111 " (predicted:%.1f%%, cycles:%" PRId64 "%s)",
1112 predicted_percent, cycles, str);
1117 " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
1118 predicted_percent, abort_count, cycles, str);
1120 return scnprintf(bf, bfsize,
1121 " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
1122 predicted_percent, abort_count, cycles, str);
1125 int callchain_list_counts__printf_value(struct callchain_node *node,
1126 struct callchain_list *clist,
1127 FILE *fp, char *bf, int bfsize)
1129 u64 branch_count, predicted_count;
1130 u64 abort_count, cycles_count;
1131 u64 iter_count = 0, samples_count = 0;
1133 branch_count = clist->branch_count;
1134 predicted_count = clist->predicted_count;
1135 abort_count = clist->abort_count;
1136 cycles_count = clist->cycles_count;
1139 struct callchain_list *call;
1141 list_for_each_entry(call, &node->val, list) {
1142 iter_count += call->iter_count;
1143 samples_count += call->samples_count;
1147 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1148 predicted_count, abort_count,
1149 cycles_count, iter_count, samples_count);
1152 static void free_callchain_node(struct callchain_node *node)
1154 struct callchain_list *list, *tmp;
1155 struct callchain_node *child;
1158 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1159 list_del(&list->list);
1160 map__zput(list->ms.map);
1164 list_for_each_entry_safe(list, tmp, &node->val, list) {
1165 list_del(&list->list);
1166 map__zput(list->ms.map);
1170 n = rb_first(&node->rb_root_in);
1172 child = container_of(n, struct callchain_node, rb_node_in);
1174 rb_erase(&child->rb_node_in, &node->rb_root_in);
1176 free_callchain_node(child);
1181 void free_callchain(struct callchain_root *root)
1183 if (!symbol_conf.use_callchain)
1186 free_callchain_node(&root->node);
1189 static u64 decay_callchain_node(struct callchain_node *node)
1191 struct callchain_node *child;
1195 n = rb_first(&node->rb_root_in);
1197 child = container_of(n, struct callchain_node, rb_node_in);
1199 child_hits += decay_callchain_node(child);
1203 node->hit = (node->hit * 7) / 8;
1204 node->children_hit = child_hits;
1209 void decay_callchain(struct callchain_root *root)
1211 if (!symbol_conf.use_callchain)
1214 decay_callchain_node(&root->node);
1217 int callchain_node__make_parent_list(struct callchain_node *node)
1219 struct callchain_node *parent = node->parent;
1220 struct callchain_list *chain, *new;
1224 list_for_each_entry_reverse(chain, &parent->val, list) {
1225 new = malloc(sizeof(*new));
1229 new->has_children = false;
1230 map__get(new->ms.map);
1231 list_add_tail(&new->list, &head);
1233 parent = parent->parent;
1236 list_for_each_entry_safe_reverse(chain, new, &head, list)
1237 list_move_tail(&chain->list, &node->parent_val);
1239 if (!list_empty(&node->parent_val)) {
1240 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1241 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1243 chain = list_first_entry(&node->val, struct callchain_list, list);
1244 chain->has_children = false;
1249 list_for_each_entry_safe(chain, new, &head, list) {
1250 list_del(&chain->list);
1251 map__zput(chain->ms.map);
1257 int callchain_cursor__copy(struct callchain_cursor *dst,
1258 struct callchain_cursor *src)
1262 callchain_cursor_reset(dst);
1263 callchain_cursor_commit(src);
1266 struct callchain_cursor_node *node;
1268 node = callchain_cursor_current(src);
1272 rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
1273 node->branch, &node->branch_flags,
1274 node->nr_loop_iter, node->samples);
1278 callchain_cursor_advance(src);