1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
4 *
5 * Handle the callchains from the stream in an ad-hoc radix tree and then
6 * sort them in an rbtree.
7 *
8 * Using a radix for code path provides a fast retrieval and factorizes
9 * memory use. Also that lets us use the paths in a hierarchical graph view.
10 *
11 */
12
13 #include <inttypes.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <stdbool.h>
17 #include <errno.h>
18 #include <math.h>
19
20 #include "asm/bug.h"
21
22 #include "hist.h"
23 #include "util.h"
24 #include "sort.h"
25 #include "machine.h"
26 #include "callchain.h"
27 #include "branch.h"
28
29 #define CALLCHAIN_PARAM_DEFAULT \
30 .mode = CHAIN_GRAPH_ABS, \
31 .min_percent = 0.5, \
32 .order = ORDER_CALLEE, \
33 .key = CCKEY_FUNCTION, \
34 .value = CCVAL_PERCENT, \
35
36 struct callchain_param callchain_param = {
37 CALLCHAIN_PARAM_DEFAULT
38 };
39
40 /*
41 * Are there any events usind DWARF callchains?
42 *
43 * I.e.
44 *
45 * -e cycles/call-graph=dwarf/
46 */
47 bool dwarf_callchain_users;
48
49 struct callchain_param callchain_param_default = {
50 CALLCHAIN_PARAM_DEFAULT
51 };
52
53 __thread struct callchain_cursor callchain_cursor;
54
parse_callchain_record_opt(const char * arg,struct callchain_param * param)55 int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
56 {
57 return parse_callchain_record(arg, param);
58 }
59
parse_callchain_mode(const char * value)60 static int parse_callchain_mode(const char *value)
61 {
62 if (!strncmp(value, "graph", strlen(value))) {
63 callchain_param.mode = CHAIN_GRAPH_ABS;
64 return 0;
65 }
66 if (!strncmp(value, "flat", strlen(value))) {
67 callchain_param.mode = CHAIN_FLAT;
68 return 0;
69 }
70 if (!strncmp(value, "fractal", strlen(value))) {
71 callchain_param.mode = CHAIN_GRAPH_REL;
72 return 0;
73 }
74 if (!strncmp(value, "folded", strlen(value))) {
75 callchain_param.mode = CHAIN_FOLDED;
76 return 0;
77 }
78 return -1;
79 }
80
parse_callchain_order(const char * value)81 static int parse_callchain_order(const char *value)
82 {
83 if (!strncmp(value, "caller", strlen(value))) {
84 callchain_param.order = ORDER_CALLER;
85 callchain_param.order_set = true;
86 return 0;
87 }
88 if (!strncmp(value, "callee", strlen(value))) {
89 callchain_param.order = ORDER_CALLEE;
90 callchain_param.order_set = true;
91 return 0;
92 }
93 return -1;
94 }
95
parse_callchain_sort_key(const char * value)96 static int parse_callchain_sort_key(const char *value)
97 {
98 if (!strncmp(value, "function", strlen(value))) {
99 callchain_param.key = CCKEY_FUNCTION;
100 return 0;
101 }
102 if (!strncmp(value, "address", strlen(value))) {
103 callchain_param.key = CCKEY_ADDRESS;
104 return 0;
105 }
106 if (!strncmp(value, "srcline", strlen(value))) {
107 callchain_param.key = CCKEY_SRCLINE;
108 return 0;
109 }
110 if (!strncmp(value, "branch", strlen(value))) {
111 callchain_param.branch_callstack = 1;
112 return 0;
113 }
114 return -1;
115 }
116
parse_callchain_value(const char * value)117 static int parse_callchain_value(const char *value)
118 {
119 if (!strncmp(value, "percent", strlen(value))) {
120 callchain_param.value = CCVAL_PERCENT;
121 return 0;
122 }
123 if (!strncmp(value, "period", strlen(value))) {
124 callchain_param.value = CCVAL_PERIOD;
125 return 0;
126 }
127 if (!strncmp(value, "count", strlen(value))) {
128 callchain_param.value = CCVAL_COUNT;
129 return 0;
130 }
131 return -1;
132 }
133
get_stack_size(const char * str,unsigned long * _size)134 static int get_stack_size(const char *str, unsigned long *_size)
135 {
136 char *endptr;
137 unsigned long size;
138 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
139
140 size = strtoul(str, &endptr, 0);
141
142 do {
143 if (*endptr)
144 break;
145
146 size = round_up(size, sizeof(u64));
147 if (!size || size > max_size)
148 break;
149
150 *_size = size;
151 return 0;
152
153 } while (0);
154
155 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
156 max_size, str);
157 return -1;
158 }
159
160 static int
__parse_callchain_report_opt(const char * arg,bool allow_record_opt)161 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
162 {
163 char *tok;
164 char *endptr, *saveptr = NULL;
165 bool minpcnt_set = false;
166 bool record_opt_set = false;
167 bool try_stack_size = false;
168
169 callchain_param.enabled = true;
170 symbol_conf.use_callchain = true;
171
172 if (!arg)
173 return 0;
174
175 while ((tok = strtok_r((char *)arg, ",", &saveptr)) != NULL) {
176 if (!strncmp(tok, "none", strlen(tok))) {
177 callchain_param.mode = CHAIN_NONE;
178 callchain_param.enabled = false;
179 symbol_conf.use_callchain = false;
180 return 0;
181 }
182
183 if (!parse_callchain_mode(tok) ||
184 !parse_callchain_order(tok) ||
185 !parse_callchain_sort_key(tok) ||
186 !parse_callchain_value(tok)) {
187 /* parsing ok - move on to the next */
188 try_stack_size = false;
189 goto next;
190 } else if (allow_record_opt && !record_opt_set) {
191 if (parse_callchain_record(tok, &callchain_param))
192 goto try_numbers;
193
194 /* assume that number followed by 'dwarf' is stack size */
195 if (callchain_param.record_mode == CALLCHAIN_DWARF)
196 try_stack_size = true;
197
198 record_opt_set = true;
199 goto next;
200 }
201
202 try_numbers:
203 if (try_stack_size) {
204 unsigned long size = 0;
205
206 if (get_stack_size(tok, &size) < 0)
207 return -1;
208 callchain_param.dump_size = size;
209 try_stack_size = false;
210 } else if (!minpcnt_set) {
211 /* try to get the min percent */
212 callchain_param.min_percent = strtod(tok, &endptr);
213 if (tok == endptr)
214 return -1;
215 minpcnt_set = true;
216 } else {
217 /* try print limit at last */
218 callchain_param.print_limit = strtoul(tok, &endptr, 0);
219 if (tok == endptr)
220 return -1;
221 }
222 next:
223 arg = NULL;
224 }
225
226 if (callchain_register_param(&callchain_param) < 0) {
227 pr_err("Can't register callchain params\n");
228 return -1;
229 }
230 return 0;
231 }
232
parse_callchain_report_opt(const char * arg)233 int parse_callchain_report_opt(const char *arg)
234 {
235 return __parse_callchain_report_opt(arg, false);
236 }
237
parse_callchain_top_opt(const char * arg)238 int parse_callchain_top_opt(const char *arg)
239 {
240 return __parse_callchain_report_opt(arg, true);
241 }
242
parse_callchain_record(const char * arg,struct callchain_param * param)243 int parse_callchain_record(const char *arg, struct callchain_param *param)
244 {
245 char *tok, *name, *saveptr = NULL;
246 char *buf;
247 int ret = -1;
248
249 /* We need buffer that we know we can write to. */
250 buf = malloc(strlen(arg) + 1);
251 if (!buf)
252 return -ENOMEM;
253
254 strcpy(buf, arg);
255
256 tok = strtok_r((char *)buf, ",", &saveptr);
257 name = tok ? : (char *)buf;
258
259 do {
260 /* Framepointer style */
261 if (!strncmp(name, "fp", sizeof("fp"))) {
262 if (!strtok_r(NULL, ",", &saveptr)) {
263 param->record_mode = CALLCHAIN_FP;
264 ret = 0;
265 } else
266 pr_err("callchain: No more arguments "
267 "needed for --call-graph fp\n");
268 break;
269
270 /* Dwarf style */
271 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
272 const unsigned long default_stack_dump_size = 8192;
273
274 ret = 0;
275 param->record_mode = CALLCHAIN_DWARF;
276 param->dump_size = default_stack_dump_size;
277 dwarf_callchain_users = true;
278
279 tok = strtok_r(NULL, ",", &saveptr);
280 if (tok) {
281 unsigned long size = 0;
282
283 ret = get_stack_size(tok, &size);
284 param->dump_size = size;
285 }
286 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
287 if (!strtok_r(NULL, ",", &saveptr)) {
288 param->record_mode = CALLCHAIN_LBR;
289 ret = 0;
290 } else
291 pr_err("callchain: No more arguments "
292 "needed for --call-graph lbr\n");
293 break;
294 } else {
295 pr_err("callchain: Unknown --call-graph option "
296 "value: %s\n", arg);
297 break;
298 }
299
300 } while (0);
301
302 free(buf);
303 return ret;
304 }
305
perf_callchain_config(const char * var,const char * value)306 int perf_callchain_config(const char *var, const char *value)
307 {
308 char *endptr;
309
310 if (!strstarts(var, "call-graph."))
311 return 0;
312 var += sizeof("call-graph.") - 1;
313
314 if (!strcmp(var, "record-mode"))
315 return parse_callchain_record_opt(value, &callchain_param);
316 if (!strcmp(var, "dump-size")) {
317 unsigned long size = 0;
318 int ret;
319
320 ret = get_stack_size(value, &size);
321 callchain_param.dump_size = size;
322
323 return ret;
324 }
325 if (!strcmp(var, "print-type")){
326 int ret;
327 ret = parse_callchain_mode(value);
328 if (ret == -1)
329 pr_err("Invalid callchain mode: %s\n", value);
330 return ret;
331 }
332 if (!strcmp(var, "order")){
333 int ret;
334 ret = parse_callchain_order(value);
335 if (ret == -1)
336 pr_err("Invalid callchain order: %s\n", value);
337 return ret;
338 }
339 if (!strcmp(var, "sort-key")){
340 int ret;
341 ret = parse_callchain_sort_key(value);
342 if (ret == -1)
343 pr_err("Invalid callchain sort key: %s\n", value);
344 return ret;
345 }
346 if (!strcmp(var, "threshold")) {
347 callchain_param.min_percent = strtod(value, &endptr);
348 if (value == endptr) {
349 pr_err("Invalid callchain threshold: %s\n", value);
350 return -1;
351 }
352 }
353 if (!strcmp(var, "print-limit")) {
354 callchain_param.print_limit = strtod(value, &endptr);
355 if (value == endptr) {
356 pr_err("Invalid callchain print limit: %s\n", value);
357 return -1;
358 }
359 }
360
361 return 0;
362 }
363
364 static void
rb_insert_callchain(struct rb_root * root,struct callchain_node * chain,enum chain_mode mode)365 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
366 enum chain_mode mode)
367 {
368 struct rb_node **p = &root->rb_node;
369 struct rb_node *parent = NULL;
370 struct callchain_node *rnode;
371 u64 chain_cumul = callchain_cumul_hits(chain);
372
373 while (*p) {
374 u64 rnode_cumul;
375
376 parent = *p;
377 rnode = rb_entry(parent, struct callchain_node, rb_node);
378 rnode_cumul = callchain_cumul_hits(rnode);
379
380 switch (mode) {
381 case CHAIN_FLAT:
382 case CHAIN_FOLDED:
383 if (rnode->hit < chain->hit)
384 p = &(*p)->rb_left;
385 else
386 p = &(*p)->rb_right;
387 break;
388 case CHAIN_GRAPH_ABS: /* Falldown */
389 case CHAIN_GRAPH_REL:
390 if (rnode_cumul < chain_cumul)
391 p = &(*p)->rb_left;
392 else
393 p = &(*p)->rb_right;
394 break;
395 case CHAIN_NONE:
396 default:
397 break;
398 }
399 }
400
401 rb_link_node(&chain->rb_node, parent, p);
402 rb_insert_color(&chain->rb_node, root);
403 }
404
405 static void
__sort_chain_flat(struct rb_root * rb_root,struct callchain_node * node,u64 min_hit)406 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
407 u64 min_hit)
408 {
409 struct rb_node *n;
410 struct callchain_node *child;
411
412 n = rb_first(&node->rb_root_in);
413 while (n) {
414 child = rb_entry(n, struct callchain_node, rb_node_in);
415 n = rb_next(n);
416
417 __sort_chain_flat(rb_root, child, min_hit);
418 }
419
420 if (node->hit && node->hit >= min_hit)
421 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
422 }
423
424 /*
425 * Once we get every callchains from the stream, we can now
426 * sort them by hit
427 */
428 static void
sort_chain_flat(struct rb_root * rb_root,struct callchain_root * root,u64 min_hit,struct callchain_param * param __maybe_unused)429 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
430 u64 min_hit, struct callchain_param *param __maybe_unused)
431 {
432 *rb_root = RB_ROOT;
433 __sort_chain_flat(rb_root, &root->node, min_hit);
434 }
435
__sort_chain_graph_abs(struct callchain_node * node,u64 min_hit)436 static void __sort_chain_graph_abs(struct callchain_node *node,
437 u64 min_hit)
438 {
439 struct rb_node *n;
440 struct callchain_node *child;
441
442 node->rb_root = RB_ROOT;
443 n = rb_first(&node->rb_root_in);
444
445 while (n) {
446 child = rb_entry(n, struct callchain_node, rb_node_in);
447 n = rb_next(n);
448
449 __sort_chain_graph_abs(child, min_hit);
450 if (callchain_cumul_hits(child) >= min_hit)
451 rb_insert_callchain(&node->rb_root, child,
452 CHAIN_GRAPH_ABS);
453 }
454 }
455
456 static void
sort_chain_graph_abs(struct rb_root * rb_root,struct callchain_root * chain_root,u64 min_hit,struct callchain_param * param __maybe_unused)457 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
458 u64 min_hit, struct callchain_param *param __maybe_unused)
459 {
460 __sort_chain_graph_abs(&chain_root->node, min_hit);
461 rb_root->rb_node = chain_root->node.rb_root.rb_node;
462 }
463
__sort_chain_graph_rel(struct callchain_node * node,double min_percent)464 static void __sort_chain_graph_rel(struct callchain_node *node,
465 double min_percent)
466 {
467 struct rb_node *n;
468 struct callchain_node *child;
469 u64 min_hit;
470
471 node->rb_root = RB_ROOT;
472 min_hit = ceil(node->children_hit * min_percent);
473
474 n = rb_first(&node->rb_root_in);
475 while (n) {
476 child = rb_entry(n, struct callchain_node, rb_node_in);
477 n = rb_next(n);
478
479 __sort_chain_graph_rel(child, min_percent);
480 if (callchain_cumul_hits(child) >= min_hit)
481 rb_insert_callchain(&node->rb_root, child,
482 CHAIN_GRAPH_REL);
483 }
484 }
485
486 static void
sort_chain_graph_rel(struct rb_root * rb_root,struct callchain_root * chain_root,u64 min_hit __maybe_unused,struct callchain_param * param)487 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
488 u64 min_hit __maybe_unused, struct callchain_param *param)
489 {
490 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
491 rb_root->rb_node = chain_root->node.rb_root.rb_node;
492 }
493
callchain_register_param(struct callchain_param * param)494 int callchain_register_param(struct callchain_param *param)
495 {
496 switch (param->mode) {
497 case CHAIN_GRAPH_ABS:
498 param->sort = sort_chain_graph_abs;
499 break;
500 case CHAIN_GRAPH_REL:
501 param->sort = sort_chain_graph_rel;
502 break;
503 case CHAIN_FLAT:
504 case CHAIN_FOLDED:
505 param->sort = sort_chain_flat;
506 break;
507 case CHAIN_NONE:
508 default:
509 return -1;
510 }
511 return 0;
512 }
513
514 /*
515 * Create a child for a parent. If inherit_children, then the new child
516 * will become the new parent of it's parent children
517 */
518 static struct callchain_node *
create_child(struct callchain_node * parent,bool inherit_children)519 create_child(struct callchain_node *parent, bool inherit_children)
520 {
521 struct callchain_node *new;
522
523 new = zalloc(sizeof(*new));
524 if (!new) {
525 perror("not enough memory to create child for code path tree");
526 return NULL;
527 }
528 new->parent = parent;
529 INIT_LIST_HEAD(&new->val);
530 INIT_LIST_HEAD(&new->parent_val);
531
532 if (inherit_children) {
533 struct rb_node *n;
534 struct callchain_node *child;
535
536 new->rb_root_in = parent->rb_root_in;
537 parent->rb_root_in = RB_ROOT;
538
539 n = rb_first(&new->rb_root_in);
540 while (n) {
541 child = rb_entry(n, struct callchain_node, rb_node_in);
542 child->parent = new;
543 n = rb_next(n);
544 }
545
546 /* make it the first child */
547 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
548 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
549 }
550
551 return new;
552 }
553
554
555 /*
556 * Fill the node with callchain values
557 */
558 static int
fill_node(struct callchain_node * node,struct callchain_cursor * cursor)559 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
560 {
561 struct callchain_cursor_node *cursor_node;
562
563 node->val_nr = cursor->nr - cursor->pos;
564 if (!node->val_nr)
565 pr_warning("Warning: empty node in callchain tree\n");
566
567 cursor_node = callchain_cursor_current(cursor);
568
569 while (cursor_node) {
570 struct callchain_list *call;
571
572 call = zalloc(sizeof(*call));
573 if (!call) {
574 perror("not enough memory for the code path tree");
575 return -1;
576 }
577 call->ip = cursor_node->ip;
578 call->ms.sym = cursor_node->sym;
579 call->ms.map = map__get(cursor_node->map);
580
581 if (cursor_node->branch) {
582 call->branch_count = 1;
583
584 if (cursor_node->branch_from) {
585 /*
586 * branch_from is set with value somewhere else
587 * to imply it's "to" of a branch.
588 */
589 call->brtype_stat.branch_to = true;
590
591 if (cursor_node->branch_flags.predicted)
592 call->predicted_count = 1;
593
594 if (cursor_node->branch_flags.abort)
595 call->abort_count = 1;
596
597 branch_type_count(&call->brtype_stat,
598 &cursor_node->branch_flags,
599 cursor_node->branch_from,
600 cursor_node->ip);
601 } else {
602 /*
603 * It's "from" of a branch
604 */
605 call->brtype_stat.branch_to = false;
606 call->cycles_count =
607 cursor_node->branch_flags.cycles;
608 call->iter_count = cursor_node->nr_loop_iter;
609 call->iter_cycles = cursor_node->iter_cycles;
610 }
611 }
612
613 list_add_tail(&call->list, &node->val);
614
615 callchain_cursor_advance(cursor);
616 cursor_node = callchain_cursor_current(cursor);
617 }
618 return 0;
619 }
620
621 static struct callchain_node *
add_child(struct callchain_node * parent,struct callchain_cursor * cursor,u64 period)622 add_child(struct callchain_node *parent,
623 struct callchain_cursor *cursor,
624 u64 period)
625 {
626 struct callchain_node *new;
627
628 new = create_child(parent, false);
629 if (new == NULL)
630 return NULL;
631
632 if (fill_node(new, cursor) < 0) {
633 struct callchain_list *call, *tmp;
634
635 list_for_each_entry_safe(call, tmp, &new->val, list) {
636 list_del(&call->list);
637 map__zput(call->ms.map);
638 free(call);
639 }
640 free(new);
641 return NULL;
642 }
643
644 new->children_hit = 0;
645 new->hit = period;
646 new->children_count = 0;
647 new->count = 1;
648 return new;
649 }
650
651 enum match_result {
652 MATCH_ERROR = -1,
653 MATCH_EQ,
654 MATCH_LT,
655 MATCH_GT,
656 };
657
match_chain_srcline(struct callchain_cursor_node * node,struct callchain_list * cnode)658 static enum match_result match_chain_srcline(struct callchain_cursor_node *node,
659 struct callchain_list *cnode)
660 {
661 char *left = NULL;
662 char *right = NULL;
663 enum match_result ret = MATCH_EQ;
664 int cmp;
665
666 if (cnode->ms.map)
667 left = get_srcline(cnode->ms.map->dso,
668 map__rip_2objdump(cnode->ms.map, cnode->ip),
669 cnode->ms.sym, true, false);
670 if (node->map)
671 right = get_srcline(node->map->dso,
672 map__rip_2objdump(node->map, node->ip),
673 node->sym, true, false);
674
675 if (left && right)
676 cmp = strcmp(left, right);
677 else if (!left && right)
678 cmp = 1;
679 else if (left && !right)
680 cmp = -1;
681 else if (cnode->ip == node->ip)
682 cmp = 0;
683 else
684 cmp = (cnode->ip < node->ip) ? -1 : 1;
685
686 if (cmp != 0)
687 ret = cmp < 0 ? MATCH_LT : MATCH_GT;
688
689 free_srcline(left);
690 free_srcline(right);
691 return ret;
692 }
693
match_chain(struct callchain_cursor_node * node,struct callchain_list * cnode)694 static enum match_result match_chain(struct callchain_cursor_node *node,
695 struct callchain_list *cnode)
696 {
697 struct symbol *sym = node->sym;
698 u64 left, right;
699 struct dso *left_dso = NULL;
700 struct dso *right_dso = NULL;
701
702 if (callchain_param.key == CCKEY_SRCLINE) {
703 enum match_result match = match_chain_srcline(node, cnode);
704
705 if (match != MATCH_ERROR)
706 return match;
707 }
708
709 if (cnode->ms.sym && sym && callchain_param.key == CCKEY_FUNCTION) {
710 left = cnode->ms.sym->start;
711 right = sym->start;
712 left_dso = cnode->ms.map->dso;
713 right_dso = node->map->dso;
714 } else {
715 left = cnode->ip;
716 right = node->ip;
717 }
718
719 if (left == right && left_dso == right_dso) {
720 if (node->branch) {
721 cnode->branch_count++;
722
723 if (node->branch_from) {
724 /*
725 * It's "to" of a branch
726 */
727 cnode->brtype_stat.branch_to = true;
728
729 if (node->branch_flags.predicted)
730 cnode->predicted_count++;
731
732 if (node->branch_flags.abort)
733 cnode->abort_count++;
734
735 branch_type_count(&cnode->brtype_stat,
736 &node->branch_flags,
737 node->branch_from,
738 node->ip);
739 } else {
740 /*
741 * It's "from" of a branch
742 */
743 cnode->brtype_stat.branch_to = false;
744 cnode->cycles_count +=
745 node->branch_flags.cycles;
746 cnode->iter_count += node->nr_loop_iter;
747 cnode->iter_cycles += node->iter_cycles;
748 }
749 }
750
751 return MATCH_EQ;
752 }
753
754 return left > right ? MATCH_GT : MATCH_LT;
755 }
756
757 /*
758 * Split the parent in two parts (a new child is created) and
759 * give a part of its callchain to the created child.
760 * Then create another child to host the given callchain of new branch
761 */
762 static int
split_add_child(struct callchain_node * parent,struct callchain_cursor * cursor,struct callchain_list * to_split,u64 idx_parents,u64 idx_local,u64 period)763 split_add_child(struct callchain_node *parent,
764 struct callchain_cursor *cursor,
765 struct callchain_list *to_split,
766 u64 idx_parents, u64 idx_local, u64 period)
767 {
768 struct callchain_node *new;
769 struct list_head *old_tail;
770 unsigned int idx_total = idx_parents + idx_local;
771
772 /* split */
773 new = create_child(parent, true);
774 if (new == NULL)
775 return -1;
776
777 /* split the callchain and move a part to the new child */
778 old_tail = parent->val.prev;
779 list_del_range(&to_split->list, old_tail);
780 new->val.next = &to_split->list;
781 new->val.prev = old_tail;
782 to_split->list.prev = &new->val;
783 old_tail->next = &new->val;
784
785 /* split the hits */
786 new->hit = parent->hit;
787 new->children_hit = parent->children_hit;
788 parent->children_hit = callchain_cumul_hits(new);
789 new->val_nr = parent->val_nr - idx_local;
790 parent->val_nr = idx_local;
791 new->count = parent->count;
792 new->children_count = parent->children_count;
793 parent->children_count = callchain_cumul_counts(new);
794
795 /* create a new child for the new branch if any */
796 if (idx_total < cursor->nr) {
797 struct callchain_node *first;
798 struct callchain_list *cnode;
799 struct callchain_cursor_node *node;
800 struct rb_node *p, **pp;
801
802 parent->hit = 0;
803 parent->children_hit += period;
804 parent->count = 0;
805 parent->children_count += 1;
806
807 node = callchain_cursor_current(cursor);
808 new = add_child(parent, cursor, period);
809 if (new == NULL)
810 return -1;
811
812 /*
813 * This is second child since we moved parent's children
814 * to new (first) child above.
815 */
816 p = parent->rb_root_in.rb_node;
817 first = rb_entry(p, struct callchain_node, rb_node_in);
818 cnode = list_first_entry(&first->val, struct callchain_list,
819 list);
820
821 if (match_chain(node, cnode) == MATCH_LT)
822 pp = &p->rb_left;
823 else
824 pp = &p->rb_right;
825
826 rb_link_node(&new->rb_node_in, p, pp);
827 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
828 } else {
829 parent->hit = period;
830 parent->count = 1;
831 }
832 return 0;
833 }
834
835 static enum match_result
836 append_chain(struct callchain_node *root,
837 struct callchain_cursor *cursor,
838 u64 period);
839
840 static int
append_chain_children(struct callchain_node * root,struct callchain_cursor * cursor,u64 period)841 append_chain_children(struct callchain_node *root,
842 struct callchain_cursor *cursor,
843 u64 period)
844 {
845 struct callchain_node *rnode;
846 struct callchain_cursor_node *node;
847 struct rb_node **p = &root->rb_root_in.rb_node;
848 struct rb_node *parent = NULL;
849
850 node = callchain_cursor_current(cursor);
851 if (!node)
852 return -1;
853
854 /* lookup in childrens */
855 while (*p) {
856 enum match_result ret;
857
858 parent = *p;
859 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
860
861 /* If at least first entry matches, rely to children */
862 ret = append_chain(rnode, cursor, period);
863 if (ret == MATCH_EQ)
864 goto inc_children_hit;
865 if (ret == MATCH_ERROR)
866 return -1;
867
868 if (ret == MATCH_LT)
869 p = &parent->rb_left;
870 else
871 p = &parent->rb_right;
872 }
873 /* nothing in children, add to the current node */
874 rnode = add_child(root, cursor, period);
875 if (rnode == NULL)
876 return -1;
877
878 rb_link_node(&rnode->rb_node_in, parent, p);
879 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
880
881 inc_children_hit:
882 root->children_hit += period;
883 root->children_count++;
884 return 0;
885 }
886
887 static enum match_result
append_chain(struct callchain_node * root,struct callchain_cursor * cursor,u64 period)888 append_chain(struct callchain_node *root,
889 struct callchain_cursor *cursor,
890 u64 period)
891 {
892 struct callchain_list *cnode;
893 u64 start = cursor->pos;
894 bool found = false;
895 u64 matches;
896 enum match_result cmp = MATCH_ERROR;
897
898 /*
899 * Lookup in the current node
900 * If we have a symbol, then compare the start to match
901 * anywhere inside a function, unless function
902 * mode is disabled.
903 */
904 list_for_each_entry(cnode, &root->val, list) {
905 struct callchain_cursor_node *node;
906
907 node = callchain_cursor_current(cursor);
908 if (!node)
909 break;
910
911 cmp = match_chain(node, cnode);
912 if (cmp != MATCH_EQ)
913 break;
914
915 found = true;
916
917 callchain_cursor_advance(cursor);
918 }
919
920 /* matches not, relay no the parent */
921 if (!found) {
922 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
923 return cmp;
924 }
925
926 matches = cursor->pos - start;
927
928 /* we match only a part of the node. Split it and add the new chain */
929 if (matches < root->val_nr) {
930 if (split_add_child(root, cursor, cnode, start, matches,
931 period) < 0)
932 return MATCH_ERROR;
933
934 return MATCH_EQ;
935 }
936
937 /* we match 100% of the path, increment the hit */
938 if (matches == root->val_nr && cursor->pos == cursor->nr) {
939 root->hit += period;
940 root->count++;
941 return MATCH_EQ;
942 }
943
944 /* We match the node and still have a part remaining */
945 if (append_chain_children(root, cursor, period) < 0)
946 return MATCH_ERROR;
947
948 return MATCH_EQ;
949 }
950
callchain_append(struct callchain_root * root,struct callchain_cursor * cursor,u64 period)951 int callchain_append(struct callchain_root *root,
952 struct callchain_cursor *cursor,
953 u64 period)
954 {
955 if (!cursor->nr)
956 return 0;
957
958 callchain_cursor_commit(cursor);
959
960 if (append_chain_children(&root->node, cursor, period) < 0)
961 return -1;
962
963 if (cursor->nr > root->max_depth)
964 root->max_depth = cursor->nr;
965
966 return 0;
967 }
968
969 static int
merge_chain_branch(struct callchain_cursor * cursor,struct callchain_node * dst,struct callchain_node * src)970 merge_chain_branch(struct callchain_cursor *cursor,
971 struct callchain_node *dst, struct callchain_node *src)
972 {
973 struct callchain_cursor_node **old_last = cursor->last;
974 struct callchain_node *child;
975 struct callchain_list *list, *next_list;
976 struct rb_node *n;
977 int old_pos = cursor->nr;
978 int err = 0;
979
980 list_for_each_entry_safe(list, next_list, &src->val, list) {
981 callchain_cursor_append(cursor, list->ip,
982 list->ms.map, list->ms.sym,
983 false, NULL, 0, 0, 0);
984 list_del(&list->list);
985 map__zput(list->ms.map);
986 free(list);
987 }
988
989 if (src->hit) {
990 callchain_cursor_commit(cursor);
991 if (append_chain_children(dst, cursor, src->hit) < 0)
992 return -1;
993 }
994
995 n = rb_first(&src->rb_root_in);
996 while (n) {
997 child = container_of(n, struct callchain_node, rb_node_in);
998 n = rb_next(n);
999 rb_erase(&child->rb_node_in, &src->rb_root_in);
1000
1001 err = merge_chain_branch(cursor, dst, child);
1002 if (err)
1003 break;
1004
1005 free(child);
1006 }
1007
1008 cursor->nr = old_pos;
1009 cursor->last = old_last;
1010
1011 return err;
1012 }
1013
callchain_merge(struct callchain_cursor * cursor,struct callchain_root * dst,struct callchain_root * src)1014 int callchain_merge(struct callchain_cursor *cursor,
1015 struct callchain_root *dst, struct callchain_root *src)
1016 {
1017 return merge_chain_branch(cursor, &dst->node, &src->node);
1018 }
1019
callchain_cursor_append(struct callchain_cursor * cursor,u64 ip,struct map * map,struct symbol * sym,bool branch,struct branch_flags * flags,int nr_loop_iter,u64 iter_cycles,u64 branch_from)1020 int callchain_cursor_append(struct callchain_cursor *cursor,
1021 u64 ip, struct map *map, struct symbol *sym,
1022 bool branch, struct branch_flags *flags,
1023 int nr_loop_iter, u64 iter_cycles, u64 branch_from)
1024 {
1025 struct callchain_cursor_node *node = *cursor->last;
1026
1027 if (!node) {
1028 node = calloc(1, sizeof(*node));
1029 if (!node)
1030 return -ENOMEM;
1031
1032 *cursor->last = node;
1033 }
1034
1035 node->ip = ip;
1036 map__zput(node->map);
1037 node->map = map__get(map);
1038 node->sym = sym;
1039 node->branch = branch;
1040 node->nr_loop_iter = nr_loop_iter;
1041 node->iter_cycles = iter_cycles;
1042
1043 if (flags)
1044 memcpy(&node->branch_flags, flags,
1045 sizeof(struct branch_flags));
1046
1047 node->branch_from = branch_from;
1048 cursor->nr++;
1049
1050 cursor->last = &node->next;
1051
1052 return 0;
1053 }
1054
sample__resolve_callchain(struct perf_sample * sample,struct callchain_cursor * cursor,struct symbol ** parent,struct perf_evsel * evsel,struct addr_location * al,int max_stack)1055 int sample__resolve_callchain(struct perf_sample *sample,
1056 struct callchain_cursor *cursor, struct symbol **parent,
1057 struct perf_evsel *evsel, struct addr_location *al,
1058 int max_stack)
1059 {
1060 if (sample->callchain == NULL && !symbol_conf.show_branchflag_count)
1061 return 0;
1062
1063 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
1064 perf_hpp_list.parent || symbol_conf.show_branchflag_count) {
1065 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
1066 parent, al, max_stack);
1067 }
1068 return 0;
1069 }
1070
hist_entry__append_callchain(struct hist_entry * he,struct perf_sample * sample)1071 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
1072 {
1073 if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
1074 !symbol_conf.show_branchflag_count)
1075 return 0;
1076 return callchain_append(he->callchain, &callchain_cursor, sample->period);
1077 }
1078
fill_callchain_info(struct addr_location * al,struct callchain_cursor_node * node,bool hide_unresolved)1079 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
1080 bool hide_unresolved)
1081 {
1082 al->map = node->map;
1083 al->sym = node->sym;
1084 if (node->map)
1085 al->addr = node->map->map_ip(node->map, node->ip);
1086 else
1087 al->addr = node->ip;
1088
1089 if (al->sym == NULL) {
1090 if (hide_unresolved)
1091 return 0;
1092 if (al->map == NULL)
1093 goto out;
1094 }
1095
1096 if (al->map->groups == &al->machine->kmaps) {
1097 if (machine__is_host(al->machine)) {
1098 al->cpumode = PERF_RECORD_MISC_KERNEL;
1099 al->level = 'k';
1100 } else {
1101 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
1102 al->level = 'g';
1103 }
1104 } else {
1105 if (machine__is_host(al->machine)) {
1106 al->cpumode = PERF_RECORD_MISC_USER;
1107 al->level = '.';
1108 } else if (perf_guest) {
1109 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
1110 al->level = 'u';
1111 } else {
1112 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
1113 al->level = 'H';
1114 }
1115 }
1116
1117 out:
1118 return 1;
1119 }
1120
callchain_list__sym_name(struct callchain_list * cl,char * bf,size_t bfsize,bool show_dso)1121 char *callchain_list__sym_name(struct callchain_list *cl,
1122 char *bf, size_t bfsize, bool show_dso)
1123 {
1124 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1125 bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
1126 int printed;
1127
1128 if (cl->ms.sym) {
1129 if (show_srcline && cl->ms.map && !cl->srcline)
1130 cl->srcline = get_srcline(cl->ms.map->dso,
1131 map__rip_2objdump(cl->ms.map,
1132 cl->ip),
1133 cl->ms.sym, false, show_addr);
1134 if (cl->srcline)
1135 printed = scnprintf(bf, bfsize, "%s %s",
1136 cl->ms.sym->name, cl->srcline);
1137 else
1138 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
1139 } else
1140 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
1141
1142 if (show_dso)
1143 scnprintf(bf + printed, bfsize - printed, " %s",
1144 cl->ms.map ?
1145 cl->ms.map->dso->short_name :
1146 "unknown");
1147
1148 return bf;
1149 }
1150
callchain_node__scnprintf_value(struct callchain_node * node,char * bf,size_t bfsize,u64 total)1151 char *callchain_node__scnprintf_value(struct callchain_node *node,
1152 char *bf, size_t bfsize, u64 total)
1153 {
1154 double percent = 0.0;
1155 u64 period = callchain_cumul_hits(node);
1156 unsigned count = callchain_cumul_counts(node);
1157
1158 if (callchain_param.mode == CHAIN_FOLDED) {
1159 period = node->hit;
1160 count = node->count;
1161 }
1162
1163 switch (callchain_param.value) {
1164 case CCVAL_PERIOD:
1165 scnprintf(bf, bfsize, "%"PRIu64, period);
1166 break;
1167 case CCVAL_COUNT:
1168 scnprintf(bf, bfsize, "%u", count);
1169 break;
1170 case CCVAL_PERCENT:
1171 default:
1172 if (total)
1173 percent = period * 100.0 / total;
1174 scnprintf(bf, bfsize, "%.2f%%", percent);
1175 break;
1176 }
1177 return bf;
1178 }
1179
callchain_node__fprintf_value(struct callchain_node * node,FILE * fp,u64 total)1180 int callchain_node__fprintf_value(struct callchain_node *node,
1181 FILE *fp, u64 total)
1182 {
1183 double percent = 0.0;
1184 u64 period = callchain_cumul_hits(node);
1185 unsigned count = callchain_cumul_counts(node);
1186
1187 if (callchain_param.mode == CHAIN_FOLDED) {
1188 period = node->hit;
1189 count = node->count;
1190 }
1191
1192 switch (callchain_param.value) {
1193 case CCVAL_PERIOD:
1194 return fprintf(fp, "%"PRIu64, period);
1195 case CCVAL_COUNT:
1196 return fprintf(fp, "%u", count);
1197 case CCVAL_PERCENT:
1198 default:
1199 if (total)
1200 percent = period * 100.0 / total;
1201 return percent_color_fprintf(fp, "%.2f%%", percent);
1202 }
1203 return 0;
1204 }
1205
callchain_counts_value(struct callchain_node * node,u64 * branch_count,u64 * predicted_count,u64 * abort_count,u64 * cycles_count)1206 static void callchain_counts_value(struct callchain_node *node,
1207 u64 *branch_count, u64 *predicted_count,
1208 u64 *abort_count, u64 *cycles_count)
1209 {
1210 struct callchain_list *clist;
1211
1212 list_for_each_entry(clist, &node->val, list) {
1213 if (branch_count)
1214 *branch_count += clist->branch_count;
1215
1216 if (predicted_count)
1217 *predicted_count += clist->predicted_count;
1218
1219 if (abort_count)
1220 *abort_count += clist->abort_count;
1221
1222 if (cycles_count)
1223 *cycles_count += clist->cycles_count;
1224 }
1225 }
1226
callchain_node_branch_counts_cumul(struct callchain_node * node,u64 * branch_count,u64 * predicted_count,u64 * abort_count,u64 * cycles_count)1227 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1228 u64 *branch_count,
1229 u64 *predicted_count,
1230 u64 *abort_count,
1231 u64 *cycles_count)
1232 {
1233 struct callchain_node *child;
1234 struct rb_node *n;
1235
1236 n = rb_first(&node->rb_root_in);
1237 while (n) {
1238 child = rb_entry(n, struct callchain_node, rb_node_in);
1239 n = rb_next(n);
1240
1241 callchain_node_branch_counts_cumul(child, branch_count,
1242 predicted_count,
1243 abort_count,
1244 cycles_count);
1245
1246 callchain_counts_value(child, branch_count,
1247 predicted_count, abort_count,
1248 cycles_count);
1249 }
1250
1251 return 0;
1252 }
1253
callchain_branch_counts(struct callchain_root * root,u64 * branch_count,u64 * predicted_count,u64 * abort_count,u64 * cycles_count)1254 int callchain_branch_counts(struct callchain_root *root,
1255 u64 *branch_count, u64 *predicted_count,
1256 u64 *abort_count, u64 *cycles_count)
1257 {
1258 if (branch_count)
1259 *branch_count = 0;
1260
1261 if (predicted_count)
1262 *predicted_count = 0;
1263
1264 if (abort_count)
1265 *abort_count = 0;
1266
1267 if (cycles_count)
1268 *cycles_count = 0;
1269
1270 return callchain_node_branch_counts_cumul(&root->node,
1271 branch_count,
1272 predicted_count,
1273 abort_count,
1274 cycles_count);
1275 }
1276
count_pri64_printf(int idx,const char * str,u64 value,char * bf,int bfsize)1277 static int count_pri64_printf(int idx, const char *str, u64 value, char *bf, int bfsize)
1278 {
1279 int printed;
1280
1281 printed = scnprintf(bf, bfsize, "%s%s:%" PRId64 "", (idx) ? " " : " (", str, value);
1282
1283 return printed;
1284 }
1285
count_float_printf(int idx,const char * str,float value,char * bf,int bfsize,float threshold)1286 static int count_float_printf(int idx, const char *str, float value,
1287 char *bf, int bfsize, float threshold)
1288 {
1289 int printed;
1290
1291 if (threshold != 0.0 && value < threshold)
1292 return 0;
1293
1294 printed = scnprintf(bf, bfsize, "%s%s:%.1f%%", (idx) ? " " : " (", str, value);
1295
1296 return printed;
1297 }
1298
branch_to_str(char * bf,int bfsize,u64 branch_count,u64 predicted_count,u64 abort_count,struct branch_type_stat * brtype_stat)1299 static int branch_to_str(char *bf, int bfsize,
1300 u64 branch_count, u64 predicted_count,
1301 u64 abort_count,
1302 struct branch_type_stat *brtype_stat)
1303 {
1304 int printed, i = 0;
1305
1306 printed = branch_type_str(brtype_stat, bf, bfsize);
1307 if (printed)
1308 i++;
1309
1310 if (predicted_count < branch_count) {
1311 printed += count_float_printf(i++, "predicted",
1312 predicted_count * 100.0 / branch_count,
1313 bf + printed, bfsize - printed, 0.0);
1314 }
1315
1316 if (abort_count) {
1317 printed += count_float_printf(i++, "abort",
1318 abort_count * 100.0 / branch_count,
1319 bf + printed, bfsize - printed, 0.1);
1320 }
1321
1322 if (i)
1323 printed += scnprintf(bf + printed, bfsize - printed, ")");
1324
1325 return printed;
1326 }
1327
branch_from_str(char * bf,int bfsize,u64 branch_count,u64 cycles_count,u64 iter_count,u64 iter_cycles)1328 static int branch_from_str(char *bf, int bfsize,
1329 u64 branch_count,
1330 u64 cycles_count, u64 iter_count,
1331 u64 iter_cycles)
1332 {
1333 int printed = 0, i = 0;
1334 u64 cycles;
1335
1336 cycles = cycles_count / branch_count;
1337 if (cycles) {
1338 printed += count_pri64_printf(i++, "cycles",
1339 cycles,
1340 bf + printed, bfsize - printed);
1341 }
1342
1343 if (iter_count) {
1344 printed += count_pri64_printf(i++, "iter",
1345 iter_count,
1346 bf + printed, bfsize - printed);
1347
1348 printed += count_pri64_printf(i++, "avg_cycles",
1349 iter_cycles / iter_count,
1350 bf + printed, bfsize - printed);
1351 }
1352
1353 if (i)
1354 printed += scnprintf(bf + printed, bfsize - printed, ")");
1355
1356 return printed;
1357 }
1358
counts_str_build(char * bf,int bfsize,u64 branch_count,u64 predicted_count,u64 abort_count,u64 cycles_count,u64 iter_count,u64 iter_cycles,struct branch_type_stat * brtype_stat)1359 static int counts_str_build(char *bf, int bfsize,
1360 u64 branch_count, u64 predicted_count,
1361 u64 abort_count, u64 cycles_count,
1362 u64 iter_count, u64 iter_cycles,
1363 struct branch_type_stat *brtype_stat)
1364 {
1365 int printed;
1366
1367 if (branch_count == 0)
1368 return scnprintf(bf, bfsize, " (calltrace)");
1369
1370 if (brtype_stat->branch_to) {
1371 printed = branch_to_str(bf, bfsize, branch_count,
1372 predicted_count, abort_count, brtype_stat);
1373 } else {
1374 printed = branch_from_str(bf, bfsize, branch_count,
1375 cycles_count, iter_count, iter_cycles);
1376 }
1377
1378 if (!printed)
1379 bf[0] = 0;
1380
1381 return printed;
1382 }
1383
callchain_counts_printf(FILE * fp,char * bf,int bfsize,u64 branch_count,u64 predicted_count,u64 abort_count,u64 cycles_count,u64 iter_count,u64 iter_cycles,struct branch_type_stat * brtype_stat)1384 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1385 u64 branch_count, u64 predicted_count,
1386 u64 abort_count, u64 cycles_count,
1387 u64 iter_count, u64 iter_cycles,
1388 struct branch_type_stat *brtype_stat)
1389 {
1390 char str[256];
1391
1392 counts_str_build(str, sizeof(str), branch_count,
1393 predicted_count, abort_count, cycles_count,
1394 iter_count, iter_cycles, brtype_stat);
1395
1396 if (fp)
1397 return fprintf(fp, "%s", str);
1398
1399 return scnprintf(bf, bfsize, "%s", str);
1400 }
1401
callchain_list_counts__printf_value(struct callchain_list * clist,FILE * fp,char * bf,int bfsize)1402 int callchain_list_counts__printf_value(struct callchain_list *clist,
1403 FILE *fp, char *bf, int bfsize)
1404 {
1405 u64 branch_count, predicted_count;
1406 u64 abort_count, cycles_count;
1407 u64 iter_count, iter_cycles;
1408
1409 branch_count = clist->branch_count;
1410 predicted_count = clist->predicted_count;
1411 abort_count = clist->abort_count;
1412 cycles_count = clist->cycles_count;
1413 iter_count = clist->iter_count;
1414 iter_cycles = clist->iter_cycles;
1415
1416 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1417 predicted_count, abort_count,
1418 cycles_count, iter_count, iter_cycles,
1419 &clist->brtype_stat);
1420 }
1421
free_callchain_node(struct callchain_node * node)1422 static void free_callchain_node(struct callchain_node *node)
1423 {
1424 struct callchain_list *list, *tmp;
1425 struct callchain_node *child;
1426 struct rb_node *n;
1427
1428 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1429 list_del(&list->list);
1430 map__zput(list->ms.map);
1431 free(list);
1432 }
1433
1434 list_for_each_entry_safe(list, tmp, &node->val, list) {
1435 list_del(&list->list);
1436 map__zput(list->ms.map);
1437 free(list);
1438 }
1439
1440 n = rb_first(&node->rb_root_in);
1441 while (n) {
1442 child = container_of(n, struct callchain_node, rb_node_in);
1443 n = rb_next(n);
1444 rb_erase(&child->rb_node_in, &node->rb_root_in);
1445
1446 free_callchain_node(child);
1447 free(child);
1448 }
1449 }
1450
free_callchain(struct callchain_root * root)1451 void free_callchain(struct callchain_root *root)
1452 {
1453 if (!symbol_conf.use_callchain)
1454 return;
1455
1456 free_callchain_node(&root->node);
1457 }
1458
decay_callchain_node(struct callchain_node * node)1459 static u64 decay_callchain_node(struct callchain_node *node)
1460 {
1461 struct callchain_node *child;
1462 struct rb_node *n;
1463 u64 child_hits = 0;
1464
1465 n = rb_first(&node->rb_root_in);
1466 while (n) {
1467 child = container_of(n, struct callchain_node, rb_node_in);
1468
1469 child_hits += decay_callchain_node(child);
1470 n = rb_next(n);
1471 }
1472
1473 node->hit = (node->hit * 7) / 8;
1474 node->children_hit = child_hits;
1475
1476 return node->hit;
1477 }
1478
decay_callchain(struct callchain_root * root)1479 void decay_callchain(struct callchain_root *root)
1480 {
1481 if (!symbol_conf.use_callchain)
1482 return;
1483
1484 decay_callchain_node(&root->node);
1485 }
1486
callchain_node__make_parent_list(struct callchain_node * node)1487 int callchain_node__make_parent_list(struct callchain_node *node)
1488 {
1489 struct callchain_node *parent = node->parent;
1490 struct callchain_list *chain, *new;
1491 LIST_HEAD(head);
1492
1493 while (parent) {
1494 list_for_each_entry_reverse(chain, &parent->val, list) {
1495 new = malloc(sizeof(*new));
1496 if (new == NULL)
1497 goto out;
1498 *new = *chain;
1499 new->has_children = false;
1500 map__get(new->ms.map);
1501 list_add_tail(&new->list, &head);
1502 }
1503 parent = parent->parent;
1504 }
1505
1506 list_for_each_entry_safe_reverse(chain, new, &head, list)
1507 list_move_tail(&chain->list, &node->parent_val);
1508
1509 if (!list_empty(&node->parent_val)) {
1510 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1511 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1512
1513 chain = list_first_entry(&node->val, struct callchain_list, list);
1514 chain->has_children = false;
1515 }
1516 return 0;
1517
1518 out:
1519 list_for_each_entry_safe(chain, new, &head, list) {
1520 list_del(&chain->list);
1521 map__zput(chain->ms.map);
1522 free(chain);
1523 }
1524 return -ENOMEM;
1525 }
1526
callchain_cursor__copy(struct callchain_cursor * dst,struct callchain_cursor * src)1527 int callchain_cursor__copy(struct callchain_cursor *dst,
1528 struct callchain_cursor *src)
1529 {
1530 int rc = 0;
1531
1532 callchain_cursor_reset(dst);
1533 callchain_cursor_commit(src);
1534
1535 while (true) {
1536 struct callchain_cursor_node *node;
1537
1538 node = callchain_cursor_current(src);
1539 if (node == NULL)
1540 break;
1541
1542 rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
1543 node->branch, &node->branch_flags,
1544 node->nr_loop_iter,
1545 node->iter_cycles,
1546 node->branch_from);
1547 if (rc)
1548 break;
1549
1550 callchain_cursor_advance(src);
1551 }
1552
1553 return rc;
1554 }
1555