Merge tag 'arm64-perf' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux-2.6-block.git] / tools / perf / util / callchain.c
CommitLineData
8cb76d99 1/*
1b3a0e95 2 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
8cb76d99
FW
3 *
4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree.
6 *
deac911c
FW
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.
9 *
8cb76d99
FW
10 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdbool.h>
15#include <errno.h>
c0a8865e 16#include <math.h>
8cb76d99 17
b965bb41
FW
18#include "asm/bug.h"
19
99571ab3 20#include "hist.h"
b36f19d5 21#include "util.h"
2dc9fb1a
NK
22#include "sort.h"
23#include "machine.h"
8cb76d99
FW
24#include "callchain.h"
25
47260645
NK
26__thread struct callchain_cursor callchain_cursor;
27
c3a6a8c4 28int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
f7f084f4 29{
076a30c4 30 return parse_callchain_record(arg, param);
f7f084f4
NK
31}
32
2b9240ca
NK
33static int parse_callchain_mode(const char *value)
34{
35 if (!strncmp(value, "graph", strlen(value))) {
36 callchain_param.mode = CHAIN_GRAPH_ABS;
37 return 0;
38 }
39 if (!strncmp(value, "flat", strlen(value))) {
40 callchain_param.mode = CHAIN_FLAT;
41 return 0;
42 }
43 if (!strncmp(value, "fractal", strlen(value))) {
44 callchain_param.mode = CHAIN_GRAPH_REL;
45 return 0;
46 }
26e77924
NK
47 if (!strncmp(value, "folded", strlen(value))) {
48 callchain_param.mode = CHAIN_FOLDED;
49 return 0;
50 }
2b9240ca
NK
51 return -1;
52}
53
54static int parse_callchain_order(const char *value)
55{
56 if (!strncmp(value, "caller", strlen(value))) {
57 callchain_param.order = ORDER_CALLER;
792aeafa 58 callchain_param.order_set = true;
2b9240ca
NK
59 return 0;
60 }
61 if (!strncmp(value, "callee", strlen(value))) {
62 callchain_param.order = ORDER_CALLEE;
792aeafa 63 callchain_param.order_set = true;
2b9240ca
NK
64 return 0;
65 }
66 return -1;
67}
68
69static int parse_callchain_sort_key(const char *value)
70{
71 if (!strncmp(value, "function", strlen(value))) {
72 callchain_param.key = CCKEY_FUNCTION;
73 return 0;
74 }
75 if (!strncmp(value, "address", strlen(value))) {
76 callchain_param.key = CCKEY_ADDRESS;
77 return 0;
78 }
8b7bad58
AK
79 if (!strncmp(value, "branch", strlen(value))) {
80 callchain_param.branch_callstack = 1;
81 return 0;
82 }
2b9240ca
NK
83 return -1;
84}
85
f2af0086
NK
86static int parse_callchain_value(const char *value)
87{
88 if (!strncmp(value, "percent", strlen(value))) {
89 callchain_param.value = CCVAL_PERCENT;
90 return 0;
91 }
92 if (!strncmp(value, "period", strlen(value))) {
93 callchain_param.value = CCVAL_PERIOD;
94 return 0;
95 }
96 if (!strncmp(value, "count", strlen(value))) {
97 callchain_param.value = CCVAL_COUNT;
98 return 0;
99 }
100 return -1;
101}
102
a2c10d39
NK
103static int
104__parse_callchain_report_opt(const char *arg, bool allow_record_opt)
cff6bb46 105{
e8232f1a 106 char *tok;
cff6bb46 107 char *endptr;
e8232f1a 108 bool minpcnt_set = false;
a2c10d39
NK
109 bool record_opt_set = false;
110 bool try_stack_size = false;
cff6bb46 111
30234f09 112 callchain_param.enabled = true;
cff6bb46
DZ
113 symbol_conf.use_callchain = true;
114
115 if (!arg)
116 return 0;
117
e8232f1a
NK
118 while ((tok = strtok((char *)arg, ",")) != NULL) {
119 if (!strncmp(tok, "none", strlen(tok))) {
120 callchain_param.mode = CHAIN_NONE;
30234f09 121 callchain_param.enabled = false;
e8232f1a
NK
122 symbol_conf.use_callchain = false;
123 return 0;
124 }
125
2b9240ca
NK
126 if (!parse_callchain_mode(tok) ||
127 !parse_callchain_order(tok) ||
f2af0086
NK
128 !parse_callchain_sort_key(tok) ||
129 !parse_callchain_value(tok)) {
2b9240ca 130 /* parsing ok - move on to the next */
a2c10d39
NK
131 try_stack_size = false;
132 goto next;
133 } else if (allow_record_opt && !record_opt_set) {
134 if (parse_callchain_record(tok, &callchain_param))
135 goto try_numbers;
136
137 /* assume that number followed by 'dwarf' is stack size */
138 if (callchain_param.record_mode == CALLCHAIN_DWARF)
139 try_stack_size = true;
140
141 record_opt_set = true;
142 goto next;
143 }
144
145try_numbers:
146 if (try_stack_size) {
147 unsigned long size = 0;
148
149 if (get_stack_size(tok, &size) < 0)
150 return -1;
151 callchain_param.dump_size = size;
152 try_stack_size = false;
2b9240ca
NK
153 } else if (!minpcnt_set) {
154 /* try to get the min percent */
e8232f1a
NK
155 callchain_param.min_percent = strtod(tok, &endptr);
156 if (tok == endptr)
157 return -1;
158 minpcnt_set = true;
159 } else {
160 /* try print limit at last */
161 callchain_param.print_limit = strtoul(tok, &endptr, 0);
162 if (tok == endptr)
163 return -1;
164 }
a2c10d39 165next:
e8232f1a 166 arg = NULL;
cff6bb46
DZ
167 }
168
cff6bb46
DZ
169 if (callchain_register_param(&callchain_param) < 0) {
170 pr_err("Can't register callchain params\n");
171 return -1;
172 }
173 return 0;
174}
175
a2c10d39
NK
176int parse_callchain_report_opt(const char *arg)
177{
178 return __parse_callchain_report_opt(arg, false);
179}
180
181int parse_callchain_top_opt(const char *arg)
182{
183 return __parse_callchain_report_opt(arg, true);
184}
185
2b9240ca
NK
186int perf_callchain_config(const char *var, const char *value)
187{
188 char *endptr;
189
190 if (prefixcmp(var, "call-graph."))
191 return 0;
192 var += sizeof("call-graph.") - 1;
193
194 if (!strcmp(var, "record-mode"))
c3a6a8c4 195 return parse_callchain_record_opt(value, &callchain_param);
2b9240ca
NK
196#ifdef HAVE_DWARF_UNWIND_SUPPORT
197 if (!strcmp(var, "dump-size")) {
198 unsigned long size = 0;
199 int ret;
200
201 ret = get_stack_size(value, &size);
202 callchain_param.dump_size = size;
203
204 return ret;
205 }
206#endif
207 if (!strcmp(var, "print-type"))
208 return parse_callchain_mode(value);
209 if (!strcmp(var, "order"))
210 return parse_callchain_order(value);
211 if (!strcmp(var, "sort-key"))
212 return parse_callchain_sort_key(value);
213 if (!strcmp(var, "threshold")) {
214 callchain_param.min_percent = strtod(value, &endptr);
215 if (value == endptr)
216 return -1;
217 }
218 if (!strcmp(var, "print-limit")) {
219 callchain_param.print_limit = strtod(value, &endptr);
220 if (value == endptr)
221 return -1;
222 }
223
224 return 0;
225}
226
deac911c 227static void
4eb3e478
FW
228rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
229 enum chain_mode mode)
8cb76d99
FW
230{
231 struct rb_node **p = &root->rb_node;
232 struct rb_node *parent = NULL;
233 struct callchain_node *rnode;
f08c3154 234 u64 chain_cumul = callchain_cumul_hits(chain);
8cb76d99
FW
235
236 while (*p) {
1953287b
FW
237 u64 rnode_cumul;
238
8cb76d99
FW
239 parent = *p;
240 rnode = rb_entry(parent, struct callchain_node, rb_node);
f08c3154 241 rnode_cumul = callchain_cumul_hits(rnode);
8cb76d99 242
4eb3e478 243 switch (mode) {
805d127d 244 case CHAIN_FLAT:
26e77924 245 case CHAIN_FOLDED:
4eb3e478
FW
246 if (rnode->hit < chain->hit)
247 p = &(*p)->rb_left;
248 else
249 p = &(*p)->rb_right;
250 break;
805d127d
FW
251 case CHAIN_GRAPH_ABS: /* Falldown */
252 case CHAIN_GRAPH_REL:
1953287b 253 if (rnode_cumul < chain_cumul)
4eb3e478
FW
254 p = &(*p)->rb_left;
255 else
256 p = &(*p)->rb_right;
257 break;
83a0944f 258 case CHAIN_NONE:
4eb3e478
FW
259 default:
260 break;
261 }
8cb76d99
FW
262 }
263
264 rb_link_node(&chain->rb_node, parent, p);
265 rb_insert_color(&chain->rb_node, root);
266}
267
805d127d
FW
268static void
269__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
270 u64 min_hit)
271{
e369517c 272 struct rb_node *n;
805d127d
FW
273 struct callchain_node *child;
274
e369517c
NK
275 n = rb_first(&node->rb_root_in);
276 while (n) {
277 child = rb_entry(n, struct callchain_node, rb_node_in);
278 n = rb_next(n);
279
805d127d 280 __sort_chain_flat(rb_root, child, min_hit);
e369517c 281 }
805d127d
FW
282
283 if (node->hit && node->hit >= min_hit)
284 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
285}
286
8cb76d99
FW
287/*
288 * Once we get every callchains from the stream, we can now
289 * sort them by hit
290 */
805d127d 291static void
d2009c51 292sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
1d037ca1 293 u64 min_hit, struct callchain_param *param __maybe_unused)
805d127d 294{
0356218a 295 *rb_root = RB_ROOT;
d2009c51 296 __sort_chain_flat(rb_root, &root->node, min_hit);
805d127d
FW
297}
298
299static void __sort_chain_graph_abs(struct callchain_node *node,
300 u64 min_hit)
8cb76d99 301{
e369517c 302 struct rb_node *n;
8cb76d99
FW
303 struct callchain_node *child;
304
805d127d 305 node->rb_root = RB_ROOT;
e369517c
NK
306 n = rb_first(&node->rb_root_in);
307
308 while (n) {
309 child = rb_entry(n, struct callchain_node, rb_node_in);
310 n = rb_next(n);
8cb76d99 311
805d127d 312 __sort_chain_graph_abs(child, min_hit);
f08c3154 313 if (callchain_cumul_hits(child) >= min_hit)
805d127d
FW
314 rb_insert_callchain(&node->rb_root, child,
315 CHAIN_GRAPH_ABS);
316 }
317}
318
319static void
d2009c51 320sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
1d037ca1 321 u64 min_hit, struct callchain_param *param __maybe_unused)
805d127d 322{
d2009c51
FW
323 __sort_chain_graph_abs(&chain_root->node, min_hit);
324 rb_root->rb_node = chain_root->node.rb_root.rb_node;
4eb3e478
FW
325}
326
805d127d
FW
327static void __sort_chain_graph_rel(struct callchain_node *node,
328 double min_percent)
4eb3e478 329{
e369517c 330 struct rb_node *n;
4eb3e478 331 struct callchain_node *child;
805d127d 332 u64 min_hit;
4eb3e478
FW
333
334 node->rb_root = RB_ROOT;
c0a8865e 335 min_hit = ceil(node->children_hit * min_percent);
4eb3e478 336
e369517c
NK
337 n = rb_first(&node->rb_root_in);
338 while (n) {
339 child = rb_entry(n, struct callchain_node, rb_node_in);
340 n = rb_next(n);
341
805d127d 342 __sort_chain_graph_rel(child, min_percent);
f08c3154 343 if (callchain_cumul_hits(child) >= min_hit)
805d127d
FW
344 rb_insert_callchain(&node->rb_root, child,
345 CHAIN_GRAPH_REL);
4eb3e478
FW
346 }
347}
348
805d127d 349static void
d2009c51 350sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
1d037ca1 351 u64 min_hit __maybe_unused, struct callchain_param *param)
4eb3e478 352{
d2009c51
FW
353 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
354 rb_root->rb_node = chain_root->node.rb_root.rb_node;
8cb76d99
FW
355}
356
16537f13 357int callchain_register_param(struct callchain_param *param)
805d127d
FW
358{
359 switch (param->mode) {
360 case CHAIN_GRAPH_ABS:
361 param->sort = sort_chain_graph_abs;
362 break;
363 case CHAIN_GRAPH_REL:
364 param->sort = sort_chain_graph_rel;
365 break;
366 case CHAIN_FLAT:
26e77924 367 case CHAIN_FOLDED:
805d127d
FW
368 param->sort = sort_chain_flat;
369 break;
83a0944f 370 case CHAIN_NONE:
805d127d
FW
371 default:
372 return -1;
373 }
374 return 0;
375}
376
deac911c
FW
377/*
378 * Create a child for a parent. If inherit_children, then the new child
379 * will become the new parent of it's parent children
380 */
381static struct callchain_node *
382create_child(struct callchain_node *parent, bool inherit_children)
8cb76d99
FW
383{
384 struct callchain_node *new;
385
cdd5b75b 386 new = zalloc(sizeof(*new));
8cb76d99
FW
387 if (!new) {
388 perror("not enough memory to create child for code path tree");
389 return NULL;
390 }
391 new->parent = parent;
8cb76d99 392 INIT_LIST_HEAD(&new->val);
4b3a3212 393 INIT_LIST_HEAD(&new->parent_val);
deac911c
FW
394
395 if (inherit_children) {
e369517c
NK
396 struct rb_node *n;
397 struct callchain_node *child;
398
399 new->rb_root_in = parent->rb_root_in;
400 parent->rb_root_in = RB_ROOT;
deac911c 401
e369517c
NK
402 n = rb_first(&new->rb_root_in);
403 while (n) {
404 child = rb_entry(n, struct callchain_node, rb_node_in);
405 child->parent = new;
406 n = rb_next(n);
407 }
deac911c 408
e369517c
NK
409 /* make it the first child */
410 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
411 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
deac911c 412 }
8cb76d99
FW
413
414 return new;
415}
416
301fde27 417
deac911c
FW
418/*
419 * Fill the node with callchain values
420 */
8451cbb9 421static int
1b3a0e95 422fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
8cb76d99 423{
1b3a0e95
FW
424 struct callchain_cursor_node *cursor_node;
425
426 node->val_nr = cursor->nr - cursor->pos;
427 if (!node->val_nr)
428 pr_warning("Warning: empty node in callchain tree\n");
8cb76d99 429
1b3a0e95
FW
430 cursor_node = callchain_cursor_current(cursor);
431
432 while (cursor_node) {
8cb76d99
FW
433 struct callchain_list *call;
434
cdd5b75b 435 call = zalloc(sizeof(*call));
8cb76d99
FW
436 if (!call) {
437 perror("not enough memory for the code path tree");
8451cbb9 438 return -1;
8cb76d99 439 }
1b3a0e95
FW
440 call->ip = cursor_node->ip;
441 call->ms.sym = cursor_node->sym;
442 call->ms.map = cursor_node->map;
8cb76d99 443 list_add_tail(&call->list, &node->val);
1b3a0e95
FW
444
445 callchain_cursor_advance(cursor);
446 cursor_node = callchain_cursor_current(cursor);
8cb76d99 447 }
8451cbb9 448 return 0;
8cb76d99
FW
449}
450
e369517c 451static struct callchain_node *
1b3a0e95
FW
452add_child(struct callchain_node *parent,
453 struct callchain_cursor *cursor,
454 u64 period)
8cb76d99
FW
455{
456 struct callchain_node *new;
457
deac911c 458 new = create_child(parent, false);
7565bd39
NK
459 if (new == NULL)
460 return NULL;
461
8451cbb9
NK
462 if (fill_node(new, cursor) < 0) {
463 struct callchain_list *call, *tmp;
464
465 list_for_each_entry_safe(call, tmp, &new->val, list) {
466 list_del(&call->list);
467 free(call);
468 }
469 free(new);
470 return NULL;
471 }
8cb76d99 472
1953287b 473 new->children_hit = 0;
108553e1 474 new->hit = period;
5e47f8ff
NK
475 new->children_count = 0;
476 new->count = 1;
e369517c
NK
477 return new;
478}
479
2d713b80
NK
480enum match_result {
481 MATCH_ERROR = -1,
482 MATCH_EQ,
483 MATCH_LT,
484 MATCH_GT,
485};
486
487static enum match_result match_chain(struct callchain_cursor_node *node,
488 struct callchain_list *cnode)
e369517c
NK
489{
490 struct symbol *sym = node->sym;
2d713b80 491 u64 left, right;
e369517c
NK
492
493 if (cnode->ms.sym && sym &&
2d713b80
NK
494 callchain_param.key == CCKEY_FUNCTION) {
495 left = cnode->ms.sym->start;
496 right = sym->start;
497 } else {
498 left = cnode->ip;
499 right = node->ip;
500 }
501
502 if (left == right)
503 return MATCH_EQ;
504
505 return left > right ? MATCH_GT : MATCH_LT;
8cb76d99
FW
506}
507
deac911c
FW
508/*
509 * Split the parent in two parts (a new child is created) and
510 * give a part of its callchain to the created child.
511 * Then create another child to host the given callchain of new branch
512 */
f2bb4c5a 513static int
1b3a0e95
FW
514split_add_child(struct callchain_node *parent,
515 struct callchain_cursor *cursor,
516 struct callchain_list *to_split,
517 u64 idx_parents, u64 idx_local, u64 period)
8cb76d99
FW
518{
519 struct callchain_node *new;
deac911c 520 struct list_head *old_tail;
f37a291c 521 unsigned int idx_total = idx_parents + idx_local;
8cb76d99
FW
522
523 /* split */
deac911c 524 new = create_child(parent, true);
f2bb4c5a
NK
525 if (new == NULL)
526 return -1;
deac911c
FW
527
528 /* split the callchain and move a part to the new child */
529 old_tail = parent->val.prev;
530 list_del_range(&to_split->list, old_tail);
531 new->val.next = &to_split->list;
532 new->val.prev = old_tail;
533 to_split->list.prev = &new->val;
534 old_tail->next = &new->val;
8cb76d99 535
deac911c
FW
536 /* split the hits */
537 new->hit = parent->hit;
1953287b 538 new->children_hit = parent->children_hit;
f08c3154 539 parent->children_hit = callchain_cumul_hits(new);
deac911c
FW
540 new->val_nr = parent->val_nr - idx_local;
541 parent->val_nr = idx_local;
5e47f8ff
NK
542 new->count = parent->count;
543 new->children_count = parent->children_count;
544 parent->children_count = callchain_cumul_counts(new);
deac911c
FW
545
546 /* create a new child for the new branch if any */
1b3a0e95 547 if (idx_total < cursor->nr) {
e369517c
NK
548 struct callchain_node *first;
549 struct callchain_list *cnode;
550 struct callchain_cursor_node *node;
551 struct rb_node *p, **pp;
552
deac911c 553 parent->hit = 0;
108553e1 554 parent->children_hit += period;
5e47f8ff
NK
555 parent->count = 0;
556 parent->children_count += 1;
e369517c
NK
557
558 node = callchain_cursor_current(cursor);
559 new = add_child(parent, cursor, period);
7565bd39 560 if (new == NULL)
f2bb4c5a 561 return -1;
e369517c
NK
562
563 /*
564 * This is second child since we moved parent's children
565 * to new (first) child above.
566 */
567 p = parent->rb_root_in.rb_node;
568 first = rb_entry(p, struct callchain_node, rb_node_in);
569 cnode = list_first_entry(&first->val, struct callchain_list,
570 list);
571
2d713b80 572 if (match_chain(node, cnode) == MATCH_LT)
e369517c
NK
573 pp = &p->rb_left;
574 else
575 pp = &p->rb_right;
576
577 rb_link_node(&new->rb_node_in, p, pp);
578 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
deac911c 579 } else {
108553e1 580 parent->hit = period;
5e47f8ff 581 parent->count = 1;
deac911c 582 }
f2bb4c5a 583 return 0;
8cb76d99
FW
584}
585
2d713b80 586static enum match_result
1b3a0e95
FW
587append_chain(struct callchain_node *root,
588 struct callchain_cursor *cursor,
589 u64 period);
8cb76d99 590
dca0d122 591static int
1b3a0e95
FW
592append_chain_children(struct callchain_node *root,
593 struct callchain_cursor *cursor,
594 u64 period)
8cb76d99
FW
595{
596 struct callchain_node *rnode;
e369517c
NK
597 struct callchain_cursor_node *node;
598 struct rb_node **p = &root->rb_root_in.rb_node;
599 struct rb_node *parent = NULL;
600
601 node = callchain_cursor_current(cursor);
602 if (!node)
dca0d122 603 return -1;
8cb76d99
FW
604
605 /* lookup in childrens */
e369517c 606 while (*p) {
2d713b80 607 enum match_result ret;
f37a291c 608
e369517c
NK
609 parent = *p;
610 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
e369517c 611
b965bb41
FW
612 /* If at least first entry matches, rely to children */
613 ret = append_chain(rnode, cursor, period);
2d713b80 614 if (ret == MATCH_EQ)
1953287b 615 goto inc_children_hit;
dca0d122
NK
616 if (ret == MATCH_ERROR)
617 return -1;
e369517c 618
2d713b80 619 if (ret == MATCH_LT)
e369517c
NK
620 p = &parent->rb_left;
621 else
622 p = &parent->rb_right;
8cb76d99 623 }
deac911c 624 /* nothing in children, add to the current node */
e369517c 625 rnode = add_child(root, cursor, period);
7565bd39 626 if (rnode == NULL)
dca0d122 627 return -1;
7565bd39 628
e369517c
NK
629 rb_link_node(&rnode->rb_node_in, parent, p);
630 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
e05b876c 631
1953287b 632inc_children_hit:
108553e1 633 root->children_hit += period;
5e47f8ff 634 root->children_count++;
dca0d122 635 return 0;
8cb76d99
FW
636}
637
2d713b80 638static enum match_result
1b3a0e95
FW
639append_chain(struct callchain_node *root,
640 struct callchain_cursor *cursor,
641 u64 period)
8cb76d99
FW
642{
643 struct callchain_list *cnode;
1b3a0e95 644 u64 start = cursor->pos;
8cb76d99 645 bool found = false;
1b3a0e95 646 u64 matches;
2d713b80 647 enum match_result cmp = MATCH_ERROR;
8cb76d99 648
deac911c
FW
649 /*
650 * Lookup in the current node
651 * If we have a symbol, then compare the start to match
99571ab3
AK
652 * anywhere inside a function, unless function
653 * mode is disabled.
deac911c 654 */
8cb76d99 655 list_for_each_entry(cnode, &root->val, list) {
1b3a0e95 656 struct callchain_cursor_node *node;
301fde27 657
1b3a0e95
FW
658 node = callchain_cursor_current(cursor);
659 if (!node)
deac911c 660 break;
301fde27 661
b965bb41 662 cmp = match_chain(node, cnode);
2d713b80 663 if (cmp != MATCH_EQ)
8cb76d99 664 break;
301fde27 665
e369517c 666 found = true;
1b3a0e95
FW
667
668 callchain_cursor_advance(cursor);
8cb76d99
FW
669 }
670
e369517c 671 /* matches not, relay no the parent */
1b3a0e95 672 if (!found) {
2d713b80 673 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
b965bb41 674 return cmp;
1b3a0e95
FW
675 }
676
677 matches = cursor->pos - start;
8cb76d99
FW
678
679 /* we match only a part of the node. Split it and add the new chain */
1b3a0e95 680 if (matches < root->val_nr) {
f2bb4c5a
NK
681 if (split_add_child(root, cursor, cnode, start, matches,
682 period) < 0)
683 return MATCH_ERROR;
684
2d713b80 685 return MATCH_EQ;
8cb76d99
FW
686 }
687
688 /* we match 100% of the path, increment the hit */
1b3a0e95 689 if (matches == root->val_nr && cursor->pos == cursor->nr) {
108553e1 690 root->hit += period;
5e47f8ff 691 root->count++;
2d713b80 692 return MATCH_EQ;
8cb76d99
FW
693 }
694
deac911c 695 /* We match the node and still have a part remaining */
dca0d122
NK
696 if (append_chain_children(root, cursor, period) < 0)
697 return MATCH_ERROR;
deac911c 698
2d713b80 699 return MATCH_EQ;
8cb76d99
FW
700}
701
1b3a0e95
FW
702int callchain_append(struct callchain_root *root,
703 struct callchain_cursor *cursor,
704 u64 period)
8cb76d99 705{
1b3a0e95 706 if (!cursor->nr)
301fde27
FW
707 return 0;
708
1b3a0e95 709 callchain_cursor_commit(cursor);
301fde27 710
dca0d122
NK
711 if (append_chain_children(&root->node, cursor, period) < 0)
712 return -1;
d2009c51 713
1b3a0e95
FW
714 if (cursor->nr > root->max_depth)
715 root->max_depth = cursor->nr;
301fde27
FW
716
717 return 0;
8cb76d99 718}
612d4fd7
FW
719
720static int
1b3a0e95
FW
721merge_chain_branch(struct callchain_cursor *cursor,
722 struct callchain_node *dst, struct callchain_node *src)
612d4fd7 723{
1b3a0e95 724 struct callchain_cursor_node **old_last = cursor->last;
e369517c 725 struct callchain_node *child;
612d4fd7 726 struct callchain_list *list, *next_list;
e369517c 727 struct rb_node *n;
1b3a0e95 728 int old_pos = cursor->nr;
612d4fd7
FW
729 int err = 0;
730
731 list_for_each_entry_safe(list, next_list, &src->val, list) {
1b3a0e95
FW
732 callchain_cursor_append(cursor, list->ip,
733 list->ms.map, list->ms.sym);
612d4fd7
FW
734 list_del(&list->list);
735 free(list);
736 }
737
1b3a0e95
FW
738 if (src->hit) {
739 callchain_cursor_commit(cursor);
dca0d122
NK
740 if (append_chain_children(dst, cursor, src->hit) < 0)
741 return -1;
1b3a0e95 742 }
612d4fd7 743
e369517c
NK
744 n = rb_first(&src->rb_root_in);
745 while (n) {
746 child = container_of(n, struct callchain_node, rb_node_in);
747 n = rb_next(n);
748 rb_erase(&child->rb_node_in, &src->rb_root_in);
749
1b3a0e95 750 err = merge_chain_branch(cursor, dst, child);
612d4fd7
FW
751 if (err)
752 break;
753
612d4fd7
FW
754 free(child);
755 }
756
1b3a0e95
FW
757 cursor->nr = old_pos;
758 cursor->last = old_last;
612d4fd7
FW
759
760 return err;
761}
762
1b3a0e95
FW
763int callchain_merge(struct callchain_cursor *cursor,
764 struct callchain_root *dst, struct callchain_root *src)
765{
766 return merge_chain_branch(cursor, &dst->node, &src->node);
767}
768
769int callchain_cursor_append(struct callchain_cursor *cursor,
770 u64 ip, struct map *map, struct symbol *sym)
612d4fd7 771{
1b3a0e95 772 struct callchain_cursor_node *node = *cursor->last;
612d4fd7 773
1b3a0e95 774 if (!node) {
91b98804 775 node = calloc(1, sizeof(*node));
1b3a0e95
FW
776 if (!node)
777 return -ENOMEM;
612d4fd7 778
1b3a0e95
FW
779 *cursor->last = node;
780 }
612d4fd7 781
1b3a0e95
FW
782 node->ip = ip;
783 node->map = map;
784 node->sym = sym;
612d4fd7 785
1b3a0e95 786 cursor->nr++;
612d4fd7 787
1b3a0e95
FW
788 cursor->last = &node->next;
789
790 return 0;
612d4fd7 791}
2dc9fb1a 792
91d7b2de
ACM
793int sample__resolve_callchain(struct perf_sample *sample,
794 struct callchain_cursor *cursor, struct symbol **parent,
2dc9fb1a
NK
795 struct perf_evsel *evsel, struct addr_location *al,
796 int max_stack)
797{
798 if (sample->callchain == NULL)
799 return 0;
800
7a13aa28 801 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
de7e6a7c 802 perf_hpp_list.parent) {
91d7b2de 803 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
cc8b7c2b 804 parent, al, max_stack);
2dc9fb1a
NK
805 }
806 return 0;
807}
808
809int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
810{
4d40b051 811 if (!symbol_conf.use_callchain || sample->callchain == NULL)
2dc9fb1a
NK
812 return 0;
813 return callchain_append(he->callchain, &callchain_cursor, sample->period);
814}
c7405d85
NK
815
816int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
817 bool hide_unresolved)
818{
819 al->map = node->map;
820 al->sym = node->sym;
821 if (node->map)
822 al->addr = node->map->map_ip(node->map, node->ip);
823 else
824 al->addr = node->ip;
825
826 if (al->sym == NULL) {
827 if (hide_unresolved)
828 return 0;
829 if (al->map == NULL)
830 goto out;
831 }
832
833 if (al->map->groups == &al->machine->kmaps) {
834 if (machine__is_host(al->machine)) {
835 al->cpumode = PERF_RECORD_MISC_KERNEL;
836 al->level = 'k';
837 } else {
838 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
839 al->level = 'g';
840 }
841 } else {
842 if (machine__is_host(al->machine)) {
843 al->cpumode = PERF_RECORD_MISC_USER;
844 al->level = '.';
845 } else if (perf_guest) {
846 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
847 al->level = 'u';
848 } else {
849 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
850 al->level = 'H';
851 }
852 }
853
854out:
855 return 1;
856}
2989ccaa
AK
857
858char *callchain_list__sym_name(struct callchain_list *cl,
859 char *bf, size_t bfsize, bool show_dso)
860{
861 int printed;
862
863 if (cl->ms.sym) {
23f0981b
AK
864 if (callchain_param.key == CCKEY_ADDRESS &&
865 cl->ms.map && !cl->srcline)
866 cl->srcline = get_srcline(cl->ms.map->dso,
867 map__rip_2objdump(cl->ms.map,
85c116a6
AK
868 cl->ip),
869 cl->ms.sym, false);
23f0981b
AK
870 if (cl->srcline)
871 printed = scnprintf(bf, bfsize, "%s %s",
872 cl->ms.sym->name, cl->srcline);
873 else
874 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
2989ccaa
AK
875 } else
876 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
877
878 if (show_dso)
879 scnprintf(bf + printed, bfsize - printed, " %s",
880 cl->ms.map ?
881 cl->ms.map->dso->short_name :
882 "unknown");
883
884 return bf;
885}
d114960c 886
5ab250ca
NK
887char *callchain_node__scnprintf_value(struct callchain_node *node,
888 char *bf, size_t bfsize, u64 total)
889{
890 double percent = 0.0;
891 u64 period = callchain_cumul_hits(node);
f2af0086 892 unsigned count = callchain_cumul_counts(node);
5ab250ca 893
f2af0086 894 if (callchain_param.mode == CHAIN_FOLDED) {
5ab250ca 895 period = node->hit;
f2af0086
NK
896 count = node->count;
897 }
5ab250ca 898
f2af0086
NK
899 switch (callchain_param.value) {
900 case CCVAL_PERIOD:
901 scnprintf(bf, bfsize, "%"PRIu64, period);
902 break;
903 case CCVAL_COUNT:
904 scnprintf(bf, bfsize, "%u", count);
905 break;
906 case CCVAL_PERCENT:
907 default:
908 if (total)
909 percent = period * 100.0 / total;
910 scnprintf(bf, bfsize, "%.2f%%", percent);
911 break;
912 }
5ab250ca
NK
913 return bf;
914}
915
916int callchain_node__fprintf_value(struct callchain_node *node,
917 FILE *fp, u64 total)
918{
919 double percent = 0.0;
920 u64 period = callchain_cumul_hits(node);
f2af0086 921 unsigned count = callchain_cumul_counts(node);
5ab250ca 922
f2af0086 923 if (callchain_param.mode == CHAIN_FOLDED) {
5ab250ca 924 period = node->hit;
f2af0086
NK
925 count = node->count;
926 }
5ab250ca 927
f2af0086
NK
928 switch (callchain_param.value) {
929 case CCVAL_PERIOD:
930 return fprintf(fp, "%"PRIu64, period);
931 case CCVAL_COUNT:
932 return fprintf(fp, "%u", count);
933 case CCVAL_PERCENT:
934 default:
935 if (total)
936 percent = period * 100.0 / total;
937 return percent_color_fprintf(fp, "%.2f%%", percent);
938 }
939 return 0;
5ab250ca
NK
940}
941
d114960c
NK
942static void free_callchain_node(struct callchain_node *node)
943{
944 struct callchain_list *list, *tmp;
945 struct callchain_node *child;
946 struct rb_node *n;
947
4b3a3212
NK
948 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
949 list_del(&list->list);
950 free(list);
951 }
952
d114960c
NK
953 list_for_each_entry_safe(list, tmp, &node->val, list) {
954 list_del(&list->list);
955 free(list);
956 }
957
958 n = rb_first(&node->rb_root_in);
959 while (n) {
960 child = container_of(n, struct callchain_node, rb_node_in);
961 n = rb_next(n);
962 rb_erase(&child->rb_node_in, &node->rb_root_in);
963
964 free_callchain_node(child);
965 free(child);
966 }
967}
968
969void free_callchain(struct callchain_root *root)
970{
971 if (!symbol_conf.use_callchain)
972 return;
973
974 free_callchain_node(&root->node);
975}
4b3a3212 976
42b276a2
NK
977static u64 decay_callchain_node(struct callchain_node *node)
978{
979 struct callchain_node *child;
980 struct rb_node *n;
981 u64 child_hits = 0;
982
983 n = rb_first(&node->rb_root_in);
984 while (n) {
985 child = container_of(n, struct callchain_node, rb_node_in);
986
987 child_hits += decay_callchain_node(child);
988 n = rb_next(n);
989 }
990
991 node->hit = (node->hit * 7) / 8;
992 node->children_hit = child_hits;
993
994 return node->hit;
995}
996
997void decay_callchain(struct callchain_root *root)
998{
999 if (!symbol_conf.use_callchain)
1000 return;
1001
1002 decay_callchain_node(&root->node);
1003}
1004
4b3a3212
NK
1005int callchain_node__make_parent_list(struct callchain_node *node)
1006{
1007 struct callchain_node *parent = node->parent;
1008 struct callchain_list *chain, *new;
1009 LIST_HEAD(head);
1010
1011 while (parent) {
1012 list_for_each_entry_reverse(chain, &parent->val, list) {
1013 new = malloc(sizeof(*new));
1014 if (new == NULL)
1015 goto out;
1016 *new = *chain;
1017 new->has_children = false;
1018 list_add_tail(&new->list, &head);
1019 }
1020 parent = parent->parent;
1021 }
1022
1023 list_for_each_entry_safe_reverse(chain, new, &head, list)
1024 list_move_tail(&chain->list, &node->parent_val);
1025
1026 if (!list_empty(&node->parent_val)) {
1027 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1028 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1029
1030 chain = list_first_entry(&node->val, struct callchain_list, list);
1031 chain->has_children = false;
1032 }
1033 return 0;
1034
1035out:
1036 list_for_each_entry_safe(chain, new, &head, list) {
1037 list_del(&chain->list);
1038 free(chain);
1039 }
1040 return -ENOMEM;
1041}