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;
54 static int parse_callchain_order(const char *value)
56 if (!strncmp(value, "caller", strlen(value))) {
57 callchain_param.order = ORDER_CALLER;
58 callchain_param.order_set = true;
61 if (!strncmp(value, "callee", strlen(value))) {
62 callchain_param.order = ORDER_CALLEE;
63 callchain_param.order_set = true;
69 static int parse_callchain_sort_key(const char *value)
71 if (!strncmp(value, "function", strlen(value))) {
72 callchain_param.key = CCKEY_FUNCTION;
75 if (!strncmp(value, "address", strlen(value))) {
76 callchain_param.key = CCKEY_ADDRESS;
79 if (!strncmp(value, "branch", strlen(value))) {
80 callchain_param.branch_callstack = 1;
86 static int parse_callchain_value(const char *value)
88 if (!strncmp(value, "percent", strlen(value))) {
89 callchain_param.value = CCVAL_PERCENT;
92 if (!strncmp(value, "period", strlen(value))) {
93 callchain_param.value = CCVAL_PERIOD;
96 if (!strncmp(value, "count", strlen(value))) {
97 callchain_param.value = CCVAL_COUNT;
104 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
108 bool minpcnt_set = false;
109 bool record_opt_set = false;
110 bool try_stack_size = false;
112 callchain_param.enabled = true;
113 symbol_conf.use_callchain = true;
118 while ((tok = strtok((char *)arg, ",")) != NULL) {
119 if (!strncmp(tok, "none", strlen(tok))) {
120 callchain_param.mode = CHAIN_NONE;
121 callchain_param.enabled = false;
122 symbol_conf.use_callchain = false;
126 if (!parse_callchain_mode(tok) ||
127 !parse_callchain_order(tok) ||
128 !parse_callchain_sort_key(tok) ||
129 !parse_callchain_value(tok)) {
130 /* parsing ok - move on to the next */
131 try_stack_size = false;
133 } else if (allow_record_opt && !record_opt_set) {
134 if (parse_callchain_record(tok, &callchain_param))
137 /* assume that number followed by 'dwarf' is stack size */
138 if (callchain_param.record_mode == CALLCHAIN_DWARF)
139 try_stack_size = true;
141 record_opt_set = true;
146 if (try_stack_size) {
147 unsigned long size = 0;
149 if (get_stack_size(tok, &size) < 0)
151 callchain_param.dump_size = size;
152 try_stack_size = false;
153 } else if (!minpcnt_set) {
154 /* try to get the min percent */
155 callchain_param.min_percent = strtod(tok, &endptr);
160 /* try print limit at last */
161 callchain_param.print_limit = strtoul(tok, &endptr, 0);
169 if (callchain_register_param(&callchain_param) < 0) {
170 pr_err("Can't register callchain params\n");
176 int parse_callchain_report_opt(const char *arg)
178 return __parse_callchain_report_opt(arg, false);
181 int parse_callchain_top_opt(const char *arg)
183 return __parse_callchain_report_opt(arg, true);
186 int perf_callchain_config(const char *var, const char *value)
190 if (prefixcmp(var, "call-graph."))
192 var += sizeof("call-graph.") - 1;
194 if (!strcmp(var, "record-mode"))
195 return parse_callchain_record_opt(value, &callchain_param);
196 if (!strcmp(var, "dump-size")) {
197 unsigned long size = 0;
200 ret = get_stack_size(value, &size);
201 callchain_param.dump_size = size;
205 if (!strcmp(var, "print-type"))
206 return parse_callchain_mode(value);
207 if (!strcmp(var, "order"))
208 return parse_callchain_order(value);
209 if (!strcmp(var, "sort-key"))
210 return parse_callchain_sort_key(value);
211 if (!strcmp(var, "threshold")) {
212 callchain_param.min_percent = strtod(value, &endptr);
216 if (!strcmp(var, "print-limit")) {
217 callchain_param.print_limit = strtod(value, &endptr);
226 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
227 enum chain_mode mode)
229 struct rb_node **p = &root->rb_node;
230 struct rb_node *parent = NULL;
231 struct callchain_node *rnode;
232 u64 chain_cumul = callchain_cumul_hits(chain);
238 rnode = rb_entry(parent, struct callchain_node, rb_node);
239 rnode_cumul = callchain_cumul_hits(rnode);
244 if (rnode->hit < chain->hit)
249 case CHAIN_GRAPH_ABS: /* Falldown */
250 case CHAIN_GRAPH_REL:
251 if (rnode_cumul < chain_cumul)
262 rb_link_node(&chain->rb_node, parent, p);
263 rb_insert_color(&chain->rb_node, root);
267 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
271 struct callchain_node *child;
273 n = rb_first(&node->rb_root_in);
275 child = rb_entry(n, struct callchain_node, rb_node_in);
278 __sort_chain_flat(rb_root, child, min_hit);
281 if (node->hit && node->hit >= min_hit)
282 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
286 * Once we get every callchains from the stream, we can now
290 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
291 u64 min_hit, struct callchain_param *param __maybe_unused)
294 __sort_chain_flat(rb_root, &root->node, min_hit);
297 static void __sort_chain_graph_abs(struct callchain_node *node,
301 struct callchain_node *child;
303 node->rb_root = RB_ROOT;
304 n = rb_first(&node->rb_root_in);
307 child = rb_entry(n, struct callchain_node, rb_node_in);
310 __sort_chain_graph_abs(child, min_hit);
311 if (callchain_cumul_hits(child) >= min_hit)
312 rb_insert_callchain(&node->rb_root, child,
318 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
319 u64 min_hit, struct callchain_param *param __maybe_unused)
321 __sort_chain_graph_abs(&chain_root->node, min_hit);
322 rb_root->rb_node = chain_root->node.rb_root.rb_node;
325 static void __sort_chain_graph_rel(struct callchain_node *node,
329 struct callchain_node *child;
332 node->rb_root = RB_ROOT;
333 min_hit = ceil(node->children_hit * min_percent);
335 n = rb_first(&node->rb_root_in);
337 child = rb_entry(n, struct callchain_node, rb_node_in);
340 __sort_chain_graph_rel(child, min_percent);
341 if (callchain_cumul_hits(child) >= min_hit)
342 rb_insert_callchain(&node->rb_root, child,
348 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
349 u64 min_hit __maybe_unused, struct callchain_param *param)
351 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
352 rb_root->rb_node = chain_root->node.rb_root.rb_node;
355 int callchain_register_param(struct callchain_param *param)
357 switch (param->mode) {
358 case CHAIN_GRAPH_ABS:
359 param->sort = sort_chain_graph_abs;
361 case CHAIN_GRAPH_REL:
362 param->sort = sort_chain_graph_rel;
366 param->sort = sort_chain_flat;
376 * Create a child for a parent. If inherit_children, then the new child
377 * will become the new parent of it's parent children
379 static struct callchain_node *
380 create_child(struct callchain_node *parent, bool inherit_children)
382 struct callchain_node *new;
384 new = zalloc(sizeof(*new));
386 perror("not enough memory to create child for code path tree");
389 new->parent = parent;
390 INIT_LIST_HEAD(&new->val);
391 INIT_LIST_HEAD(&new->parent_val);
393 if (inherit_children) {
395 struct callchain_node *child;
397 new->rb_root_in = parent->rb_root_in;
398 parent->rb_root_in = RB_ROOT;
400 n = rb_first(&new->rb_root_in);
402 child = rb_entry(n, struct callchain_node, rb_node_in);
407 /* make it the first child */
408 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
409 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
417 * Fill the node with callchain values
420 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
422 struct callchain_cursor_node *cursor_node;
424 node->val_nr = cursor->nr - cursor->pos;
426 pr_warning("Warning: empty node in callchain tree\n");
428 cursor_node = callchain_cursor_current(cursor);
430 while (cursor_node) {
431 struct callchain_list *call;
433 call = zalloc(sizeof(*call));
435 perror("not enough memory for the code path tree");
438 call->ip = cursor_node->ip;
439 call->ms.sym = cursor_node->sym;
440 call->ms.map = map__get(cursor_node->map);
442 if (cursor_node->branch) {
443 call->branch_count = 1;
445 if (cursor_node->branch_flags.predicted)
446 call->predicted_count = 1;
448 if (cursor_node->branch_flags.abort)
449 call->abort_count = 1;
451 call->cycles_count = cursor_node->branch_flags.cycles;
452 call->iter_count = cursor_node->nr_loop_iter;
453 call->samples_count = cursor_node->samples;
456 list_add_tail(&call->list, &node->val);
458 callchain_cursor_advance(cursor);
459 cursor_node = callchain_cursor_current(cursor);
464 static struct callchain_node *
465 add_child(struct callchain_node *parent,
466 struct callchain_cursor *cursor,
469 struct callchain_node *new;
471 new = create_child(parent, false);
475 if (fill_node(new, cursor) < 0) {
476 struct callchain_list *call, *tmp;
478 list_for_each_entry_safe(call, tmp, &new->val, list) {
479 list_del(&call->list);
480 map__zput(call->ms.map);
487 new->children_hit = 0;
489 new->children_count = 0;
501 static enum match_result match_chain(struct callchain_cursor_node *node,
502 struct callchain_list *cnode)
504 struct symbol *sym = node->sym;
507 if (cnode->ms.sym && sym &&
508 callchain_param.key == CCKEY_FUNCTION) {
509 left = cnode->ms.sym->start;
518 cnode->branch_count++;
520 if (node->branch_flags.predicted)
521 cnode->predicted_count++;
523 if (node->branch_flags.abort)
524 cnode->abort_count++;
526 cnode->cycles_count += node->branch_flags.cycles;
527 cnode->iter_count += node->nr_loop_iter;
528 cnode->samples_count += node->samples;
534 return left > right ? MATCH_GT : MATCH_LT;
538 * Split the parent in two parts (a new child is created) and
539 * give a part of its callchain to the created child.
540 * Then create another child to host the given callchain of new branch
543 split_add_child(struct callchain_node *parent,
544 struct callchain_cursor *cursor,
545 struct callchain_list *to_split,
546 u64 idx_parents, u64 idx_local, u64 period)
548 struct callchain_node *new;
549 struct list_head *old_tail;
550 unsigned int idx_total = idx_parents + idx_local;
553 new = create_child(parent, true);
557 /* split the callchain and move a part to the new child */
558 old_tail = parent->val.prev;
559 list_del_range(&to_split->list, old_tail);
560 new->val.next = &to_split->list;
561 new->val.prev = old_tail;
562 to_split->list.prev = &new->val;
563 old_tail->next = &new->val;
566 new->hit = parent->hit;
567 new->children_hit = parent->children_hit;
568 parent->children_hit = callchain_cumul_hits(new);
569 new->val_nr = parent->val_nr - idx_local;
570 parent->val_nr = idx_local;
571 new->count = parent->count;
572 new->children_count = parent->children_count;
573 parent->children_count = callchain_cumul_counts(new);
575 /* create a new child for the new branch if any */
576 if (idx_total < cursor->nr) {
577 struct callchain_node *first;
578 struct callchain_list *cnode;
579 struct callchain_cursor_node *node;
580 struct rb_node *p, **pp;
583 parent->children_hit += period;
585 parent->children_count += 1;
587 node = callchain_cursor_current(cursor);
588 new = add_child(parent, cursor, period);
593 * This is second child since we moved parent's children
594 * to new (first) child above.
596 p = parent->rb_root_in.rb_node;
597 first = rb_entry(p, struct callchain_node, rb_node_in);
598 cnode = list_first_entry(&first->val, struct callchain_list,
601 if (match_chain(node, cnode) == MATCH_LT)
606 rb_link_node(&new->rb_node_in, p, pp);
607 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
609 parent->hit = period;
615 static enum match_result
616 append_chain(struct callchain_node *root,
617 struct callchain_cursor *cursor,
621 append_chain_children(struct callchain_node *root,
622 struct callchain_cursor *cursor,
625 struct callchain_node *rnode;
626 struct callchain_cursor_node *node;
627 struct rb_node **p = &root->rb_root_in.rb_node;
628 struct rb_node *parent = NULL;
630 node = callchain_cursor_current(cursor);
634 /* lookup in childrens */
636 enum match_result ret;
639 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
641 /* If at least first entry matches, rely to children */
642 ret = append_chain(rnode, cursor, period);
644 goto inc_children_hit;
645 if (ret == MATCH_ERROR)
649 p = &parent->rb_left;
651 p = &parent->rb_right;
653 /* nothing in children, add to the current node */
654 rnode = add_child(root, cursor, period);
658 rb_link_node(&rnode->rb_node_in, parent, p);
659 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
662 root->children_hit += period;
663 root->children_count++;
667 static enum match_result
668 append_chain(struct callchain_node *root,
669 struct callchain_cursor *cursor,
672 struct callchain_list *cnode;
673 u64 start = cursor->pos;
676 enum match_result cmp = MATCH_ERROR;
679 * Lookup in the current node
680 * If we have a symbol, then compare the start to match
681 * anywhere inside a function, unless function
684 list_for_each_entry(cnode, &root->val, list) {
685 struct callchain_cursor_node *node;
687 node = callchain_cursor_current(cursor);
691 cmp = match_chain(node, cnode);
697 callchain_cursor_advance(cursor);
700 /* matches not, relay no the parent */
702 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
706 matches = cursor->pos - start;
708 /* we match only a part of the node. Split it and add the new chain */
709 if (matches < root->val_nr) {
710 if (split_add_child(root, cursor, cnode, start, matches,
717 /* we match 100% of the path, increment the hit */
718 if (matches == root->val_nr && cursor->pos == cursor->nr) {
724 /* We match the node and still have a part remaining */
725 if (append_chain_children(root, cursor, period) < 0)
731 int callchain_append(struct callchain_root *root,
732 struct callchain_cursor *cursor,
738 callchain_cursor_commit(cursor);
740 if (append_chain_children(&root->node, cursor, period) < 0)
743 if (cursor->nr > root->max_depth)
744 root->max_depth = cursor->nr;
750 merge_chain_branch(struct callchain_cursor *cursor,
751 struct callchain_node *dst, struct callchain_node *src)
753 struct callchain_cursor_node **old_last = cursor->last;
754 struct callchain_node *child;
755 struct callchain_list *list, *next_list;
757 int old_pos = cursor->nr;
760 list_for_each_entry_safe(list, next_list, &src->val, list) {
761 callchain_cursor_append(cursor, list->ip,
762 list->ms.map, list->ms.sym,
764 list_del(&list->list);
765 map__zput(list->ms.map);
770 callchain_cursor_commit(cursor);
771 if (append_chain_children(dst, cursor, src->hit) < 0)
775 n = rb_first(&src->rb_root_in);
777 child = container_of(n, struct callchain_node, rb_node_in);
779 rb_erase(&child->rb_node_in, &src->rb_root_in);
781 err = merge_chain_branch(cursor, dst, child);
788 cursor->nr = old_pos;
789 cursor->last = old_last;
794 int callchain_merge(struct callchain_cursor *cursor,
795 struct callchain_root *dst, struct callchain_root *src)
797 return merge_chain_branch(cursor, &dst->node, &src->node);
800 int callchain_cursor_append(struct callchain_cursor *cursor,
801 u64 ip, struct map *map, struct symbol *sym,
802 bool branch, struct branch_flags *flags,
803 int nr_loop_iter, int samples)
805 struct callchain_cursor_node *node = *cursor->last;
808 node = calloc(1, sizeof(*node));
812 *cursor->last = node;
816 map__zput(node->map);
817 node->map = map__get(map);
819 node->branch = branch;
820 node->nr_loop_iter = nr_loop_iter;
821 node->samples = samples;
824 memcpy(&node->branch_flags, flags,
825 sizeof(struct branch_flags));
829 cursor->last = &node->next;
834 int sample__resolve_callchain(struct perf_sample *sample,
835 struct callchain_cursor *cursor, struct symbol **parent,
836 struct perf_evsel *evsel, struct addr_location *al,
839 if (sample->callchain == NULL)
842 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
843 perf_hpp_list.parent) {
844 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
845 parent, al, max_stack);
850 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
852 if (!symbol_conf.use_callchain || sample->callchain == NULL)
854 return callchain_append(he->callchain, &callchain_cursor, sample->period);
857 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
858 bool hide_unresolved)
863 al->addr = node->map->map_ip(node->map, node->ip);
867 if (al->sym == NULL) {
874 if (al->map->groups == &al->machine->kmaps) {
875 if (machine__is_host(al->machine)) {
876 al->cpumode = PERF_RECORD_MISC_KERNEL;
879 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
883 if (machine__is_host(al->machine)) {
884 al->cpumode = PERF_RECORD_MISC_USER;
886 } else if (perf_guest) {
887 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
890 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
899 char *callchain_list__sym_name(struct callchain_list *cl,
900 char *bf, size_t bfsize, bool show_dso)
905 if (callchain_param.key == CCKEY_ADDRESS &&
906 cl->ms.map && !cl->srcline)
907 cl->srcline = get_srcline(cl->ms.map->dso,
908 map__rip_2objdump(cl->ms.map,
912 printed = scnprintf(bf, bfsize, "%s %s",
913 cl->ms.sym->name, cl->srcline);
915 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
917 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
920 scnprintf(bf + printed, bfsize - printed, " %s",
922 cl->ms.map->dso->short_name :
928 char *callchain_node__scnprintf_value(struct callchain_node *node,
929 char *bf, size_t bfsize, u64 total)
931 double percent = 0.0;
932 u64 period = callchain_cumul_hits(node);
933 unsigned count = callchain_cumul_counts(node);
935 if (callchain_param.mode == CHAIN_FOLDED) {
940 switch (callchain_param.value) {
942 scnprintf(bf, bfsize, "%"PRIu64, period);
945 scnprintf(bf, bfsize, "%u", count);
950 percent = period * 100.0 / total;
951 scnprintf(bf, bfsize, "%.2f%%", percent);
957 int callchain_node__fprintf_value(struct callchain_node *node,
960 double percent = 0.0;
961 u64 period = callchain_cumul_hits(node);
962 unsigned count = callchain_cumul_counts(node);
964 if (callchain_param.mode == CHAIN_FOLDED) {
969 switch (callchain_param.value) {
971 return fprintf(fp, "%"PRIu64, period);
973 return fprintf(fp, "%u", count);
977 percent = period * 100.0 / total;
978 return percent_color_fprintf(fp, "%.2f%%", percent);
983 static void callchain_counts_value(struct callchain_node *node,
984 u64 *branch_count, u64 *predicted_count,
985 u64 *abort_count, u64 *cycles_count)
987 struct callchain_list *clist;
989 list_for_each_entry(clist, &node->val, list) {
991 *branch_count += clist->branch_count;
994 *predicted_count += clist->predicted_count;
997 *abort_count += clist->abort_count;
1000 *cycles_count += clist->cycles_count;
1004 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1006 u64 *predicted_count,
1010 struct callchain_node *child;
1013 n = rb_first(&node->rb_root_in);
1015 child = rb_entry(n, struct callchain_node, rb_node_in);
1018 callchain_node_branch_counts_cumul(child, branch_count,
1023 callchain_counts_value(child, branch_count,
1024 predicted_count, abort_count,
1031 int callchain_branch_counts(struct callchain_root *root,
1032 u64 *branch_count, u64 *predicted_count,
1033 u64 *abort_count, u64 *cycles_count)
1038 if (predicted_count)
1039 *predicted_count = 0;
1047 return callchain_node_branch_counts_cumul(&root->node,
1054 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1055 u64 branch_count, u64 predicted_count,
1056 u64 abort_count, u64 cycles_count,
1057 u64 iter_count, u64 samples_count)
1059 double predicted_percent = 0.0;
1060 const char *null_str = "";
1065 if (branch_count == 0) {
1067 return fprintf(fp, " (calltrace)");
1069 return scnprintf(bf, bfsize, " (calltrace)");
1072 if (iter_count && samples_count) {
1073 scnprintf(iter_str, sizeof(iter_str),
1074 ", iterations:%" PRId64 "",
1075 iter_count / samples_count);
1078 str = (char *)null_str;
1080 predicted_percent = predicted_count * 100.0 / branch_count;
1081 cycles = cycles_count / branch_count;
1083 if ((predicted_percent >= 100.0) && (abort_count == 0)) {
1085 return fprintf(fp, " (cycles:%" PRId64 "%s)",
1088 return scnprintf(bf, bfsize, " (cycles:%" PRId64 "%s)",
1092 if ((predicted_percent < 100.0) && (abort_count == 0)) {
1095 " (predicted:%.1f%%, cycles:%" PRId64 "%s)",
1096 predicted_percent, cycles, str);
1098 return scnprintf(bf, bfsize,
1099 " (predicted:%.1f%%, cycles:%" PRId64 "%s)",
1100 predicted_percent, cycles, str);
1105 " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
1106 predicted_percent, abort_count, cycles, str);
1108 return scnprintf(bf, bfsize,
1109 " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
1110 predicted_percent, abort_count, cycles, str);
1113 int callchain_list_counts__printf_value(struct callchain_node *node,
1114 struct callchain_list *clist,
1115 FILE *fp, char *bf, int bfsize)
1117 u64 branch_count, predicted_count;
1118 u64 abort_count, cycles_count;
1119 u64 iter_count = 0, samples_count = 0;
1121 branch_count = clist->branch_count;
1122 predicted_count = clist->predicted_count;
1123 abort_count = clist->abort_count;
1124 cycles_count = clist->cycles_count;
1127 struct callchain_list *call;
1129 list_for_each_entry(call, &node->val, list) {
1130 iter_count += call->iter_count;
1131 samples_count += call->samples_count;
1135 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1136 predicted_count, abort_count,
1137 cycles_count, iter_count, samples_count);
1140 static void free_callchain_node(struct callchain_node *node)
1142 struct callchain_list *list, *tmp;
1143 struct callchain_node *child;
1146 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1147 list_del(&list->list);
1148 map__zput(list->ms.map);
1152 list_for_each_entry_safe(list, tmp, &node->val, list) {
1153 list_del(&list->list);
1154 map__zput(list->ms.map);
1158 n = rb_first(&node->rb_root_in);
1160 child = container_of(n, struct callchain_node, rb_node_in);
1162 rb_erase(&child->rb_node_in, &node->rb_root_in);
1164 free_callchain_node(child);
1169 void free_callchain(struct callchain_root *root)
1171 if (!symbol_conf.use_callchain)
1174 free_callchain_node(&root->node);
1177 static u64 decay_callchain_node(struct callchain_node *node)
1179 struct callchain_node *child;
1183 n = rb_first(&node->rb_root_in);
1185 child = container_of(n, struct callchain_node, rb_node_in);
1187 child_hits += decay_callchain_node(child);
1191 node->hit = (node->hit * 7) / 8;
1192 node->children_hit = child_hits;
1197 void decay_callchain(struct callchain_root *root)
1199 if (!symbol_conf.use_callchain)
1202 decay_callchain_node(&root->node);
1205 int callchain_node__make_parent_list(struct callchain_node *node)
1207 struct callchain_node *parent = node->parent;
1208 struct callchain_list *chain, *new;
1212 list_for_each_entry_reverse(chain, &parent->val, list) {
1213 new = malloc(sizeof(*new));
1217 new->has_children = false;
1218 map__get(new->ms.map);
1219 list_add_tail(&new->list, &head);
1221 parent = parent->parent;
1224 list_for_each_entry_safe_reverse(chain, new, &head, list)
1225 list_move_tail(&chain->list, &node->parent_val);
1227 if (!list_empty(&node->parent_val)) {
1228 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1229 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1231 chain = list_first_entry(&node->val, struct callchain_list, list);
1232 chain->has_children = false;
1237 list_for_each_entry_safe(chain, new, &head, list) {
1238 list_del(&chain->list);
1239 map__zput(chain->ms.map);
1245 int callchain_cursor__copy(struct callchain_cursor *dst,
1246 struct callchain_cursor *src)
1250 callchain_cursor_reset(dst);
1251 callchain_cursor_commit(src);
1254 struct callchain_cursor_node *node;
1256 node = callchain_cursor_current(src);
1260 rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
1261 node->branch, &node->branch_flags,
1262 node->nr_loop_iter, node->samples);
1266 callchain_cursor_advance(src);