]>
Commit | Line | Data |
---|---|---|
8cb76d99 | 1 | /* |
1b3a0e95 | 2 | * Copyright (C) 2009-2011, Frederic Weisbecker <[email protected]> |
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 | 28 | int 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 |
33 | static 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 | ||
54 | static 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 | ||
69 | static 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 |
86 | static 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 |
103 | static 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 | ||
145 | try_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 | 165 | next: |
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 |
176 | int parse_callchain_report_opt(const char *arg) |
177 | { | |
178 | return __parse_callchain_report_opt(arg, false); | |
179 | } | |
180 | ||
181 | int parse_callchain_top_opt(const char *arg) | |
182 | { | |
183 | return __parse_callchain_report_opt(arg, true); | |
184 | } | |
185 | ||
2b9240ca NK |
186 | int 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 | if (!strcmp(var, "dump-size")) { |
197 | unsigned long size = 0; | |
198 | int ret; | |
199 | ||
200 | ret = get_stack_size(value, &size); | |
201 | callchain_param.dump_size = size; | |
202 | ||
203 | return ret; | |
204 | } | |
2b9240ca NK |
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); | |
213 | if (value == endptr) | |
214 | return -1; | |
215 | } | |
216 | if (!strcmp(var, "print-limit")) { | |
217 | callchain_param.print_limit = strtod(value, &endptr); | |
218 | if (value == endptr) | |
219 | return -1; | |
220 | } | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
deac911c | 225 | static void |
4eb3e478 FW |
226 | rb_insert_callchain(struct rb_root *root, struct callchain_node *chain, |
227 | enum chain_mode mode) | |
8cb76d99 FW |
228 | { |
229 | struct rb_node **p = &root->rb_node; | |
230 | struct rb_node *parent = NULL; | |
231 | struct callchain_node *rnode; | |
f08c3154 | 232 | u64 chain_cumul = callchain_cumul_hits(chain); |
8cb76d99 FW |
233 | |
234 | while (*p) { | |
1953287b FW |
235 | u64 rnode_cumul; |
236 | ||
8cb76d99 FW |
237 | parent = *p; |
238 | rnode = rb_entry(parent, struct callchain_node, rb_node); | |
f08c3154 | 239 | rnode_cumul = callchain_cumul_hits(rnode); |
8cb76d99 | 240 | |
4eb3e478 | 241 | switch (mode) { |
805d127d | 242 | case CHAIN_FLAT: |
26e77924 | 243 | case CHAIN_FOLDED: |
4eb3e478 FW |
244 | if (rnode->hit < chain->hit) |
245 | p = &(*p)->rb_left; | |
246 | else | |
247 | p = &(*p)->rb_right; | |
248 | break; | |
805d127d FW |
249 | case CHAIN_GRAPH_ABS: /* Falldown */ |
250 | case CHAIN_GRAPH_REL: | |
1953287b | 251 | if (rnode_cumul < chain_cumul) |
4eb3e478 FW |
252 | p = &(*p)->rb_left; |
253 | else | |
254 | p = &(*p)->rb_right; | |
255 | break; | |
83a0944f | 256 | case CHAIN_NONE: |
4eb3e478 FW |
257 | default: |
258 | break; | |
259 | } | |
8cb76d99 FW |
260 | } |
261 | ||
262 | rb_link_node(&chain->rb_node, parent, p); | |
263 | rb_insert_color(&chain->rb_node, root); | |
264 | } | |
265 | ||
805d127d FW |
266 | static void |
267 | __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node, | |
268 | u64 min_hit) | |
269 | { | |
e369517c | 270 | struct rb_node *n; |
805d127d FW |
271 | struct callchain_node *child; |
272 | ||
e369517c NK |
273 | n = rb_first(&node->rb_root_in); |
274 | while (n) { | |
275 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
276 | n = rb_next(n); | |
277 | ||
805d127d | 278 | __sort_chain_flat(rb_root, child, min_hit); |
e369517c | 279 | } |
805d127d FW |
280 | |
281 | if (node->hit && node->hit >= min_hit) | |
282 | rb_insert_callchain(rb_root, node, CHAIN_FLAT); | |
283 | } | |
284 | ||
8cb76d99 FW |
285 | /* |
286 | * Once we get every callchains from the stream, we can now | |
287 | * sort them by hit | |
288 | */ | |
805d127d | 289 | static void |
d2009c51 | 290 | sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root, |
1d037ca1 | 291 | u64 min_hit, struct callchain_param *param __maybe_unused) |
805d127d | 292 | { |
0356218a | 293 | *rb_root = RB_ROOT; |
d2009c51 | 294 | __sort_chain_flat(rb_root, &root->node, min_hit); |
805d127d FW |
295 | } |
296 | ||
297 | static void __sort_chain_graph_abs(struct callchain_node *node, | |
298 | u64 min_hit) | |
8cb76d99 | 299 | { |
e369517c | 300 | struct rb_node *n; |
8cb76d99 FW |
301 | struct callchain_node *child; |
302 | ||
805d127d | 303 | node->rb_root = RB_ROOT; |
e369517c NK |
304 | n = rb_first(&node->rb_root_in); |
305 | ||
306 | while (n) { | |
307 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
308 | n = rb_next(n); | |
8cb76d99 | 309 | |
805d127d | 310 | __sort_chain_graph_abs(child, min_hit); |
f08c3154 | 311 | if (callchain_cumul_hits(child) >= min_hit) |
805d127d FW |
312 | rb_insert_callchain(&node->rb_root, child, |
313 | CHAIN_GRAPH_ABS); | |
314 | } | |
315 | } | |
316 | ||
317 | static void | |
d2009c51 | 318 | sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root, |
1d037ca1 | 319 | u64 min_hit, struct callchain_param *param __maybe_unused) |
805d127d | 320 | { |
d2009c51 FW |
321 | __sort_chain_graph_abs(&chain_root->node, min_hit); |
322 | rb_root->rb_node = chain_root->node.rb_root.rb_node; | |
4eb3e478 FW |
323 | } |
324 | ||
805d127d FW |
325 | static void __sort_chain_graph_rel(struct callchain_node *node, |
326 | double min_percent) | |
4eb3e478 | 327 | { |
e369517c | 328 | struct rb_node *n; |
4eb3e478 | 329 | struct callchain_node *child; |
805d127d | 330 | u64 min_hit; |
4eb3e478 FW |
331 | |
332 | node->rb_root = RB_ROOT; | |
c0a8865e | 333 | min_hit = ceil(node->children_hit * min_percent); |
4eb3e478 | 334 | |
e369517c NK |
335 | n = rb_first(&node->rb_root_in); |
336 | while (n) { | |
337 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
338 | n = rb_next(n); | |
339 | ||
805d127d | 340 | __sort_chain_graph_rel(child, min_percent); |
f08c3154 | 341 | if (callchain_cumul_hits(child) >= min_hit) |
805d127d FW |
342 | rb_insert_callchain(&node->rb_root, child, |
343 | CHAIN_GRAPH_REL); | |
4eb3e478 FW |
344 | } |
345 | } | |
346 | ||
805d127d | 347 | static void |
d2009c51 | 348 | sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root, |
1d037ca1 | 349 | u64 min_hit __maybe_unused, struct callchain_param *param) |
4eb3e478 | 350 | { |
d2009c51 FW |
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; | |
8cb76d99 FW |
353 | } |
354 | ||
16537f13 | 355 | int callchain_register_param(struct callchain_param *param) |
805d127d FW |
356 | { |
357 | switch (param->mode) { | |
358 | case CHAIN_GRAPH_ABS: | |
359 | param->sort = sort_chain_graph_abs; | |
360 | break; | |
361 | case CHAIN_GRAPH_REL: | |
362 | param->sort = sort_chain_graph_rel; | |
363 | break; | |
364 | case CHAIN_FLAT: | |
26e77924 | 365 | case CHAIN_FOLDED: |
805d127d FW |
366 | param->sort = sort_chain_flat; |
367 | break; | |
83a0944f | 368 | case CHAIN_NONE: |
805d127d FW |
369 | default: |
370 | return -1; | |
371 | } | |
372 | return 0; | |
373 | } | |
374 | ||
deac911c FW |
375 | /* |
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 | |
378 | */ | |
379 | static struct callchain_node * | |
380 | create_child(struct callchain_node *parent, bool inherit_children) | |
8cb76d99 FW |
381 | { |
382 | struct callchain_node *new; | |
383 | ||
cdd5b75b | 384 | new = zalloc(sizeof(*new)); |
8cb76d99 FW |
385 | if (!new) { |
386 | perror("not enough memory to create child for code path tree"); | |
387 | return NULL; | |
388 | } | |
389 | new->parent = parent; | |
8cb76d99 | 390 | INIT_LIST_HEAD(&new->val); |
4b3a3212 | 391 | INIT_LIST_HEAD(&new->parent_val); |
deac911c FW |
392 | |
393 | if (inherit_children) { | |
e369517c NK |
394 | struct rb_node *n; |
395 | struct callchain_node *child; | |
396 | ||
397 | new->rb_root_in = parent->rb_root_in; | |
398 | parent->rb_root_in = RB_ROOT; | |
deac911c | 399 | |
e369517c NK |
400 | n = rb_first(&new->rb_root_in); |
401 | while (n) { | |
402 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
403 | child->parent = new; | |
404 | n = rb_next(n); | |
405 | } | |
deac911c | 406 | |
e369517c NK |
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); | |
deac911c | 410 | } |
8cb76d99 FW |
411 | |
412 | return new; | |
413 | } | |
414 | ||
301fde27 | 415 | |
deac911c FW |
416 | /* |
417 | * Fill the node with callchain values | |
418 | */ | |
8451cbb9 | 419 | static int |
1b3a0e95 | 420 | fill_node(struct callchain_node *node, struct callchain_cursor *cursor) |
8cb76d99 | 421 | { |
1b3a0e95 FW |
422 | struct callchain_cursor_node *cursor_node; |
423 | ||
424 | node->val_nr = cursor->nr - cursor->pos; | |
425 | if (!node->val_nr) | |
426 | pr_warning("Warning: empty node in callchain tree\n"); | |
8cb76d99 | 427 | |
1b3a0e95 FW |
428 | cursor_node = callchain_cursor_current(cursor); |
429 | ||
430 | while (cursor_node) { | |
8cb76d99 FW |
431 | struct callchain_list *call; |
432 | ||
cdd5b75b | 433 | call = zalloc(sizeof(*call)); |
8cb76d99 FW |
434 | if (!call) { |
435 | perror("not enough memory for the code path tree"); | |
8451cbb9 | 436 | return -1; |
8cb76d99 | 437 | } |
1b3a0e95 FW |
438 | call->ip = cursor_node->ip; |
439 | call->ms.sym = cursor_node->sym; | |
440 | call->ms.map = cursor_node->map; | |
3dd029ef JY |
441 | |
442 | if (cursor_node->branch) { | |
443 | call->branch_count = 1; | |
444 | ||
445 | if (cursor_node->branch_flags.predicted) | |
446 | call->predicted_count = 1; | |
447 | ||
448 | if (cursor_node->branch_flags.abort) | |
449 | call->abort_count = 1; | |
450 | ||
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; | |
454 | } | |
455 | ||
8cb76d99 | 456 | list_add_tail(&call->list, &node->val); |
1b3a0e95 FW |
457 | |
458 | callchain_cursor_advance(cursor); | |
459 | cursor_node = callchain_cursor_current(cursor); | |
8cb76d99 | 460 | } |
8451cbb9 | 461 | return 0; |
8cb76d99 FW |
462 | } |
463 | ||
e369517c | 464 | static struct callchain_node * |
1b3a0e95 FW |
465 | add_child(struct callchain_node *parent, |
466 | struct callchain_cursor *cursor, | |
467 | u64 period) | |
8cb76d99 FW |
468 | { |
469 | struct callchain_node *new; | |
470 | ||
deac911c | 471 | new = create_child(parent, false); |
7565bd39 NK |
472 | if (new == NULL) |
473 | return NULL; | |
474 | ||
8451cbb9 NK |
475 | if (fill_node(new, cursor) < 0) { |
476 | struct callchain_list *call, *tmp; | |
477 | ||
478 | list_for_each_entry_safe(call, tmp, &new->val, list) { | |
479 | list_del(&call->list); | |
480 | free(call); | |
481 | } | |
482 | free(new); | |
483 | return NULL; | |
484 | } | |
8cb76d99 | 485 | |
1953287b | 486 | new->children_hit = 0; |
108553e1 | 487 | new->hit = period; |
5e47f8ff NK |
488 | new->children_count = 0; |
489 | new->count = 1; | |
e369517c NK |
490 | return new; |
491 | } | |
492 | ||
2d713b80 NK |
493 | enum match_result { |
494 | MATCH_ERROR = -1, | |
495 | MATCH_EQ, | |
496 | MATCH_LT, | |
497 | MATCH_GT, | |
498 | }; | |
499 | ||
500 | static enum match_result match_chain(struct callchain_cursor_node *node, | |
501 | struct callchain_list *cnode) | |
e369517c NK |
502 | { |
503 | struct symbol *sym = node->sym; | |
2d713b80 | 504 | u64 left, right; |
e369517c NK |
505 | |
506 | if (cnode->ms.sym && sym && | |
2d713b80 NK |
507 | callchain_param.key == CCKEY_FUNCTION) { |
508 | left = cnode->ms.sym->start; | |
509 | right = sym->start; | |
510 | } else { | |
511 | left = cnode->ip; | |
512 | right = node->ip; | |
513 | } | |
514 | ||
3dd029ef JY |
515 | if (left == right) { |
516 | if (node->branch) { | |
517 | cnode->branch_count++; | |
518 | ||
519 | if (node->branch_flags.predicted) | |
520 | cnode->predicted_count++; | |
521 | ||
522 | if (node->branch_flags.abort) | |
523 | cnode->abort_count++; | |
524 | ||
525 | cnode->cycles_count += node->branch_flags.cycles; | |
526 | cnode->iter_count += node->nr_loop_iter; | |
527 | cnode->samples_count += node->samples; | |
528 | } | |
529 | ||
2d713b80 | 530 | return MATCH_EQ; |
3dd029ef | 531 | } |
2d713b80 NK |
532 | |
533 | return left > right ? MATCH_GT : MATCH_LT; | |
8cb76d99 FW |
534 | } |
535 | ||
deac911c FW |
536 | /* |
537 | * Split the parent in two parts (a new child is created) and | |
538 | * give a part of its callchain to the created child. | |
539 | * Then create another child to host the given callchain of new branch | |
540 | */ | |
f2bb4c5a | 541 | static int |
1b3a0e95 FW |
542 | split_add_child(struct callchain_node *parent, |
543 | struct callchain_cursor *cursor, | |
544 | struct callchain_list *to_split, | |
545 | u64 idx_parents, u64 idx_local, u64 period) | |
8cb76d99 FW |
546 | { |
547 | struct callchain_node *new; | |
deac911c | 548 | struct list_head *old_tail; |
f37a291c | 549 | unsigned int idx_total = idx_parents + idx_local; |
8cb76d99 FW |
550 | |
551 | /* split */ | |
deac911c | 552 | new = create_child(parent, true); |
f2bb4c5a NK |
553 | if (new == NULL) |
554 | return -1; | |
deac911c FW |
555 | |
556 | /* split the callchain and move a part to the new child */ | |
557 | old_tail = parent->val.prev; | |
558 | list_del_range(&to_split->list, old_tail); | |
559 | new->val.next = &to_split->list; | |
560 | new->val.prev = old_tail; | |
561 | to_split->list.prev = &new->val; | |
562 | old_tail->next = &new->val; | |
8cb76d99 | 563 | |
deac911c FW |
564 | /* split the hits */ |
565 | new->hit = parent->hit; | |
1953287b | 566 | new->children_hit = parent->children_hit; |
f08c3154 | 567 | parent->children_hit = callchain_cumul_hits(new); |
deac911c FW |
568 | new->val_nr = parent->val_nr - idx_local; |
569 | parent->val_nr = idx_local; | |
5e47f8ff NK |
570 | new->count = parent->count; |
571 | new->children_count = parent->children_count; | |
572 | parent->children_count = callchain_cumul_counts(new); | |
deac911c FW |
573 | |
574 | /* create a new child for the new branch if any */ | |
1b3a0e95 | 575 | if (idx_total < cursor->nr) { |
e369517c NK |
576 | struct callchain_node *first; |
577 | struct callchain_list *cnode; | |
578 | struct callchain_cursor_node *node; | |
579 | struct rb_node *p, **pp; | |
580 | ||
deac911c | 581 | parent->hit = 0; |
108553e1 | 582 | parent->children_hit += period; |
5e47f8ff NK |
583 | parent->count = 0; |
584 | parent->children_count += 1; | |
e369517c NK |
585 | |
586 | node = callchain_cursor_current(cursor); | |
587 | new = add_child(parent, cursor, period); | |
7565bd39 | 588 | if (new == NULL) |
f2bb4c5a | 589 | return -1; |
e369517c NK |
590 | |
591 | /* | |
592 | * This is second child since we moved parent's children | |
593 | * to new (first) child above. | |
594 | */ | |
595 | p = parent->rb_root_in.rb_node; | |
596 | first = rb_entry(p, struct callchain_node, rb_node_in); | |
597 | cnode = list_first_entry(&first->val, struct callchain_list, | |
598 | list); | |
599 | ||
2d713b80 | 600 | if (match_chain(node, cnode) == MATCH_LT) |
e369517c NK |
601 | pp = &p->rb_left; |
602 | else | |
603 | pp = &p->rb_right; | |
604 | ||
605 | rb_link_node(&new->rb_node_in, p, pp); | |
606 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); | |
deac911c | 607 | } else { |
108553e1 | 608 | parent->hit = period; |
5e47f8ff | 609 | parent->count = 1; |
deac911c | 610 | } |
f2bb4c5a | 611 | return 0; |
8cb76d99 FW |
612 | } |
613 | ||
2d713b80 | 614 | static enum match_result |
1b3a0e95 FW |
615 | append_chain(struct callchain_node *root, |
616 | struct callchain_cursor *cursor, | |
617 | u64 period); | |
8cb76d99 | 618 | |
dca0d122 | 619 | static int |
1b3a0e95 FW |
620 | append_chain_children(struct callchain_node *root, |
621 | struct callchain_cursor *cursor, | |
622 | u64 period) | |
8cb76d99 FW |
623 | { |
624 | struct callchain_node *rnode; | |
e369517c NK |
625 | struct callchain_cursor_node *node; |
626 | struct rb_node **p = &root->rb_root_in.rb_node; | |
627 | struct rb_node *parent = NULL; | |
628 | ||
629 | node = callchain_cursor_current(cursor); | |
630 | if (!node) | |
dca0d122 | 631 | return -1; |
8cb76d99 FW |
632 | |
633 | /* lookup in childrens */ | |
e369517c | 634 | while (*p) { |
2d713b80 | 635 | enum match_result ret; |
f37a291c | 636 | |
e369517c NK |
637 | parent = *p; |
638 | rnode = rb_entry(parent, struct callchain_node, rb_node_in); | |
e369517c | 639 | |
b965bb41 FW |
640 | /* If at least first entry matches, rely to children */ |
641 | ret = append_chain(rnode, cursor, period); | |
2d713b80 | 642 | if (ret == MATCH_EQ) |
1953287b | 643 | goto inc_children_hit; |
dca0d122 NK |
644 | if (ret == MATCH_ERROR) |
645 | return -1; | |
e369517c | 646 | |
2d713b80 | 647 | if (ret == MATCH_LT) |
e369517c NK |
648 | p = &parent->rb_left; |
649 | else | |
650 | p = &parent->rb_right; | |
8cb76d99 | 651 | } |
deac911c | 652 | /* nothing in children, add to the current node */ |
e369517c | 653 | rnode = add_child(root, cursor, period); |
7565bd39 | 654 | if (rnode == NULL) |
dca0d122 | 655 | return -1; |
7565bd39 | 656 | |
e369517c NK |
657 | rb_link_node(&rnode->rb_node_in, parent, p); |
658 | rb_insert_color(&rnode->rb_node_in, &root->rb_root_in); | |
e05b876c | 659 | |
1953287b | 660 | inc_children_hit: |
108553e1 | 661 | root->children_hit += period; |
5e47f8ff | 662 | root->children_count++; |
dca0d122 | 663 | return 0; |
8cb76d99 FW |
664 | } |
665 | ||
2d713b80 | 666 | static enum match_result |
1b3a0e95 FW |
667 | append_chain(struct callchain_node *root, |
668 | struct callchain_cursor *cursor, | |
669 | u64 period) | |
8cb76d99 FW |
670 | { |
671 | struct callchain_list *cnode; | |
1b3a0e95 | 672 | u64 start = cursor->pos; |
8cb76d99 | 673 | bool found = false; |
1b3a0e95 | 674 | u64 matches; |
2d713b80 | 675 | enum match_result cmp = MATCH_ERROR; |
8cb76d99 | 676 | |
deac911c FW |
677 | /* |
678 | * Lookup in the current node | |
679 | * If we have a symbol, then compare the start to match | |
99571ab3 AK |
680 | * anywhere inside a function, unless function |
681 | * mode is disabled. | |
deac911c | 682 | */ |
8cb76d99 | 683 | list_for_each_entry(cnode, &root->val, list) { |
1b3a0e95 | 684 | struct callchain_cursor_node *node; |
301fde27 | 685 | |
1b3a0e95 FW |
686 | node = callchain_cursor_current(cursor); |
687 | if (!node) | |
deac911c | 688 | break; |
301fde27 | 689 | |
b965bb41 | 690 | cmp = match_chain(node, cnode); |
2d713b80 | 691 | if (cmp != MATCH_EQ) |
8cb76d99 | 692 | break; |
301fde27 | 693 | |
e369517c | 694 | found = true; |
1b3a0e95 FW |
695 | |
696 | callchain_cursor_advance(cursor); | |
8cb76d99 FW |
697 | } |
698 | ||
e369517c | 699 | /* matches not, relay no the parent */ |
1b3a0e95 | 700 | if (!found) { |
2d713b80 | 701 | WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n"); |
b965bb41 | 702 | return cmp; |
1b3a0e95 FW |
703 | } |
704 | ||
705 | matches = cursor->pos - start; | |
8cb76d99 FW |
706 | |
707 | /* we match only a part of the node. Split it and add the new chain */ | |
1b3a0e95 | 708 | if (matches < root->val_nr) { |
f2bb4c5a NK |
709 | if (split_add_child(root, cursor, cnode, start, matches, |
710 | period) < 0) | |
711 | return MATCH_ERROR; | |
712 | ||
2d713b80 | 713 | return MATCH_EQ; |
8cb76d99 FW |
714 | } |
715 | ||
716 | /* we match 100% of the path, increment the hit */ | |
1b3a0e95 | 717 | if (matches == root->val_nr && cursor->pos == cursor->nr) { |
108553e1 | 718 | root->hit += period; |
5e47f8ff | 719 | root->count++; |
2d713b80 | 720 | return MATCH_EQ; |
8cb76d99 FW |
721 | } |
722 | ||
deac911c | 723 | /* We match the node and still have a part remaining */ |
dca0d122 NK |
724 | if (append_chain_children(root, cursor, period) < 0) |
725 | return MATCH_ERROR; | |
deac911c | 726 | |
2d713b80 | 727 | return MATCH_EQ; |
8cb76d99 FW |
728 | } |
729 | ||
1b3a0e95 FW |
730 | int callchain_append(struct callchain_root *root, |
731 | struct callchain_cursor *cursor, | |
732 | u64 period) | |
8cb76d99 | 733 | { |
1b3a0e95 | 734 | if (!cursor->nr) |
301fde27 FW |
735 | return 0; |
736 | ||
1b3a0e95 | 737 | callchain_cursor_commit(cursor); |
301fde27 | 738 | |
dca0d122 NK |
739 | if (append_chain_children(&root->node, cursor, period) < 0) |
740 | return -1; | |
d2009c51 | 741 | |
1b3a0e95 FW |
742 | if (cursor->nr > root->max_depth) |
743 | root->max_depth = cursor->nr; | |
301fde27 FW |
744 | |
745 | return 0; | |
8cb76d99 | 746 | } |
612d4fd7 FW |
747 | |
748 | static int | |
1b3a0e95 FW |
749 | merge_chain_branch(struct callchain_cursor *cursor, |
750 | struct callchain_node *dst, struct callchain_node *src) | |
612d4fd7 | 751 | { |
1b3a0e95 | 752 | struct callchain_cursor_node **old_last = cursor->last; |
e369517c | 753 | struct callchain_node *child; |
612d4fd7 | 754 | struct callchain_list *list, *next_list; |
e369517c | 755 | struct rb_node *n; |
1b3a0e95 | 756 | int old_pos = cursor->nr; |
612d4fd7 FW |
757 | int err = 0; |
758 | ||
759 | list_for_each_entry_safe(list, next_list, &src->val, list) { | |
1b3a0e95 | 760 | callchain_cursor_append(cursor, list->ip, |
410024db JY |
761 | list->ms.map, list->ms.sym, |
762 | false, NULL, 0, 0); | |
612d4fd7 FW |
763 | list_del(&list->list); |
764 | free(list); | |
765 | } | |
766 | ||
1b3a0e95 FW |
767 | if (src->hit) { |
768 | callchain_cursor_commit(cursor); | |
dca0d122 NK |
769 | if (append_chain_children(dst, cursor, src->hit) < 0) |
770 | return -1; | |
1b3a0e95 | 771 | } |
612d4fd7 | 772 | |
e369517c NK |
773 | n = rb_first(&src->rb_root_in); |
774 | while (n) { | |
775 | child = container_of(n, struct callchain_node, rb_node_in); | |
776 | n = rb_next(n); | |
777 | rb_erase(&child->rb_node_in, &src->rb_root_in); | |
778 | ||
1b3a0e95 | 779 | err = merge_chain_branch(cursor, dst, child); |
612d4fd7 FW |
780 | if (err) |
781 | break; | |
782 | ||
612d4fd7 FW |
783 | free(child); |
784 | } | |
785 | ||
1b3a0e95 FW |
786 | cursor->nr = old_pos; |
787 | cursor->last = old_last; | |
612d4fd7 FW |
788 | |
789 | return err; | |
790 | } | |
791 | ||
1b3a0e95 FW |
792 | int callchain_merge(struct callchain_cursor *cursor, |
793 | struct callchain_root *dst, struct callchain_root *src) | |
794 | { | |
795 | return merge_chain_branch(cursor, &dst->node, &src->node); | |
796 | } | |
797 | ||
798 | int callchain_cursor_append(struct callchain_cursor *cursor, | |
410024db JY |
799 | u64 ip, struct map *map, struct symbol *sym, |
800 | bool branch, struct branch_flags *flags, | |
801 | int nr_loop_iter, int samples) | |
612d4fd7 | 802 | { |
1b3a0e95 | 803 | struct callchain_cursor_node *node = *cursor->last; |
612d4fd7 | 804 | |
1b3a0e95 | 805 | if (!node) { |
91b98804 | 806 | node = calloc(1, sizeof(*node)); |
1b3a0e95 FW |
807 | if (!node) |
808 | return -ENOMEM; | |
612d4fd7 | 809 | |
1b3a0e95 FW |
810 | *cursor->last = node; |
811 | } | |
612d4fd7 | 812 | |
1b3a0e95 FW |
813 | node->ip = ip; |
814 | node->map = map; | |
815 | node->sym = sym; | |
410024db JY |
816 | node->branch = branch; |
817 | node->nr_loop_iter = nr_loop_iter; | |
818 | node->samples = samples; | |
819 | ||
820 | if (flags) | |
821 | memcpy(&node->branch_flags, flags, | |
822 | sizeof(struct branch_flags)); | |
612d4fd7 | 823 | |
1b3a0e95 | 824 | cursor->nr++; |
612d4fd7 | 825 | |
1b3a0e95 FW |
826 | cursor->last = &node->next; |
827 | ||
828 | return 0; | |
612d4fd7 | 829 | } |
2dc9fb1a | 830 | |
91d7b2de ACM |
831 | int sample__resolve_callchain(struct perf_sample *sample, |
832 | struct callchain_cursor *cursor, struct symbol **parent, | |
2dc9fb1a NK |
833 | struct perf_evsel *evsel, struct addr_location *al, |
834 | int max_stack) | |
835 | { | |
836 | if (sample->callchain == NULL) | |
837 | return 0; | |
838 | ||
7a13aa28 | 839 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain || |
de7e6a7c | 840 | perf_hpp_list.parent) { |
91d7b2de | 841 | return thread__resolve_callchain(al->thread, cursor, evsel, sample, |
cc8b7c2b | 842 | parent, al, max_stack); |
2dc9fb1a NK |
843 | } |
844 | return 0; | |
845 | } | |
846 | ||
847 | int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample) | |
848 | { | |
4d40b051 | 849 | if (!symbol_conf.use_callchain || sample->callchain == NULL) |
2dc9fb1a NK |
850 | return 0; |
851 | return callchain_append(he->callchain, &callchain_cursor, sample->period); | |
852 | } | |
c7405d85 NK |
853 | |
854 | int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node, | |
855 | bool hide_unresolved) | |
856 | { | |
857 | al->map = node->map; | |
858 | al->sym = node->sym; | |
859 | if (node->map) | |
860 | al->addr = node->map->map_ip(node->map, node->ip); | |
861 | else | |
862 | al->addr = node->ip; | |
863 | ||
864 | if (al->sym == NULL) { | |
865 | if (hide_unresolved) | |
866 | return 0; | |
867 | if (al->map == NULL) | |
868 | goto out; | |
869 | } | |
870 | ||
871 | if (al->map->groups == &al->machine->kmaps) { | |
872 | if (machine__is_host(al->machine)) { | |
873 | al->cpumode = PERF_RECORD_MISC_KERNEL; | |
874 | al->level = 'k'; | |
875 | } else { | |
876 | al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL; | |
877 | al->level = 'g'; | |
878 | } | |
879 | } else { | |
880 | if (machine__is_host(al->machine)) { | |
881 | al->cpumode = PERF_RECORD_MISC_USER; | |
882 | al->level = '.'; | |
883 | } else if (perf_guest) { | |
884 | al->cpumode = PERF_RECORD_MISC_GUEST_USER; | |
885 | al->level = 'u'; | |
886 | } else { | |
887 | al->cpumode = PERF_RECORD_MISC_HYPERVISOR; | |
888 | al->level = 'H'; | |
889 | } | |
890 | } | |
891 | ||
892 | out: | |
893 | return 1; | |
894 | } | |
2989ccaa AK |
895 | |
896 | char *callchain_list__sym_name(struct callchain_list *cl, | |
897 | char *bf, size_t bfsize, bool show_dso) | |
898 | { | |
899 | int printed; | |
900 | ||
901 | if (cl->ms.sym) { | |
23f0981b AK |
902 | if (callchain_param.key == CCKEY_ADDRESS && |
903 | cl->ms.map && !cl->srcline) | |
904 | cl->srcline = get_srcline(cl->ms.map->dso, | |
905 | map__rip_2objdump(cl->ms.map, | |
85c116a6 AK |
906 | cl->ip), |
907 | cl->ms.sym, false); | |
23f0981b AK |
908 | if (cl->srcline) |
909 | printed = scnprintf(bf, bfsize, "%s %s", | |
910 | cl->ms.sym->name, cl->srcline); | |
911 | else | |
912 | printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name); | |
2989ccaa AK |
913 | } else |
914 | printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip); | |
915 | ||
916 | if (show_dso) | |
917 | scnprintf(bf + printed, bfsize - printed, " %s", | |
918 | cl->ms.map ? | |
919 | cl->ms.map->dso->short_name : | |
920 | "unknown"); | |
921 | ||
922 | return bf; | |
923 | } | |
d114960c | 924 | |
5ab250ca NK |
925 | char *callchain_node__scnprintf_value(struct callchain_node *node, |
926 | char *bf, size_t bfsize, u64 total) | |
927 | { | |
928 | double percent = 0.0; | |
929 | u64 period = callchain_cumul_hits(node); | |
f2af0086 | 930 | unsigned count = callchain_cumul_counts(node); |
5ab250ca | 931 | |
f2af0086 | 932 | if (callchain_param.mode == CHAIN_FOLDED) { |
5ab250ca | 933 | period = node->hit; |
f2af0086 NK |
934 | count = node->count; |
935 | } | |
5ab250ca | 936 | |
f2af0086 NK |
937 | switch (callchain_param.value) { |
938 | case CCVAL_PERIOD: | |
939 | scnprintf(bf, bfsize, "%"PRIu64, period); | |
940 | break; | |
941 | case CCVAL_COUNT: | |
942 | scnprintf(bf, bfsize, "%u", count); | |
943 | break; | |
944 | case CCVAL_PERCENT: | |
945 | default: | |
946 | if (total) | |
947 | percent = period * 100.0 / total; | |
948 | scnprintf(bf, bfsize, "%.2f%%", percent); | |
949 | break; | |
950 | } | |
5ab250ca NK |
951 | return bf; |
952 | } | |
953 | ||
954 | int callchain_node__fprintf_value(struct callchain_node *node, | |
955 | FILE *fp, u64 total) | |
956 | { | |
957 | double percent = 0.0; | |
958 | u64 period = callchain_cumul_hits(node); | |
f2af0086 | 959 | unsigned count = callchain_cumul_counts(node); |
5ab250ca | 960 | |
f2af0086 | 961 | if (callchain_param.mode == CHAIN_FOLDED) { |
5ab250ca | 962 | period = node->hit; |
f2af0086 NK |
963 | count = node->count; |
964 | } | |
5ab250ca | 965 | |
f2af0086 NK |
966 | switch (callchain_param.value) { |
967 | case CCVAL_PERIOD: | |
968 | return fprintf(fp, "%"PRIu64, period); | |
969 | case CCVAL_COUNT: | |
970 | return fprintf(fp, "%u", count); | |
971 | case CCVAL_PERCENT: | |
972 | default: | |
973 | if (total) | |
974 | percent = period * 100.0 / total; | |
975 | return percent_color_fprintf(fp, "%.2f%%", percent); | |
976 | } | |
977 | return 0; | |
5ab250ca NK |
978 | } |
979 | ||
3dd029ef JY |
980 | static void callchain_counts_value(struct callchain_node *node, |
981 | u64 *branch_count, u64 *predicted_count, | |
982 | u64 *abort_count, u64 *cycles_count) | |
983 | { | |
984 | struct callchain_list *clist; | |
985 | ||
986 | list_for_each_entry(clist, &node->val, list) { | |
987 | if (branch_count) | |
988 | *branch_count += clist->branch_count; | |
989 | ||
990 | if (predicted_count) | |
991 | *predicted_count += clist->predicted_count; | |
992 | ||
993 | if (abort_count) | |
994 | *abort_count += clist->abort_count; | |
995 | ||
996 | if (cycles_count) | |
997 | *cycles_count += clist->cycles_count; | |
998 | } | |
999 | } | |
1000 | ||
1001 | static int callchain_node_branch_counts_cumul(struct callchain_node *node, | |
1002 | u64 *branch_count, | |
1003 | u64 *predicted_count, | |
1004 | u64 *abort_count, | |
1005 | u64 *cycles_count) | |
1006 | { | |
1007 | struct callchain_node *child; | |
1008 | struct rb_node *n; | |
1009 | ||
1010 | n = rb_first(&node->rb_root_in); | |
1011 | while (n) { | |
1012 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
1013 | n = rb_next(n); | |
1014 | ||
1015 | callchain_node_branch_counts_cumul(child, branch_count, | |
1016 | predicted_count, | |
1017 | abort_count, | |
1018 | cycles_count); | |
1019 | ||
1020 | callchain_counts_value(child, branch_count, | |
1021 | predicted_count, abort_count, | |
1022 | cycles_count); | |
1023 | } | |
1024 | ||
1025 | return 0; | |
1026 | } | |
1027 | ||
1028 | int callchain_branch_counts(struct callchain_root *root, | |
1029 | u64 *branch_count, u64 *predicted_count, | |
1030 | u64 *abort_count, u64 *cycles_count) | |
1031 | { | |
1032 | if (branch_count) | |
1033 | *branch_count = 0; | |
1034 | ||
1035 | if (predicted_count) | |
1036 | *predicted_count = 0; | |
1037 | ||
1038 | if (abort_count) | |
1039 | *abort_count = 0; | |
1040 | ||
1041 | if (cycles_count) | |
1042 | *cycles_count = 0; | |
1043 | ||
1044 | return callchain_node_branch_counts_cumul(&root->node, | |
1045 | branch_count, | |
1046 | predicted_count, | |
1047 | abort_count, | |
1048 | cycles_count); | |
1049 | } | |
1050 | ||
1051 | static int callchain_counts_printf(FILE *fp, char *bf, int bfsize, | |
1052 | u64 branch_count, u64 predicted_count, | |
1053 | u64 abort_count, u64 cycles_count, | |
1054 | u64 iter_count, u64 samples_count) | |
1055 | { | |
1056 | double predicted_percent = 0.0; | |
1057 | const char *null_str = ""; | |
1058 | char iter_str[32]; | |
1059 | char *str; | |
1060 | u64 cycles = 0; | |
1061 | ||
1062 | if (branch_count == 0) { | |
1063 | if (fp) | |
1064 | return fprintf(fp, " (calltrace)"); | |
1065 | ||
1066 | return scnprintf(bf, bfsize, " (calltrace)"); | |
1067 | } | |
1068 | ||
1069 | if (iter_count && samples_count) { | |
1070 | scnprintf(iter_str, sizeof(iter_str), | |
1071 | ", iterations:%" PRId64 "", | |
1072 | iter_count / samples_count); | |
1073 | str = iter_str; | |
1074 | } else | |
1075 | str = (char *)null_str; | |
1076 | ||
1077 | predicted_percent = predicted_count * 100.0 / branch_count; | |
1078 | cycles = cycles_count / branch_count; | |
1079 | ||
1080 | if ((predicted_percent >= 100.0) && (abort_count == 0)) { | |
1081 | if (fp) | |
1082 | return fprintf(fp, " (cycles:%" PRId64 "%s)", | |
1083 | cycles, str); | |
1084 | ||
1085 | return scnprintf(bf, bfsize, " (cycles:%" PRId64 "%s)", | |
1086 | cycles, str); | |
1087 | } | |
1088 | ||
1089 | if ((predicted_percent < 100.0) && (abort_count == 0)) { | |
1090 | if (fp) | |
1091 | return fprintf(fp, | |
1092 | " (predicted:%.1f%%, cycles:%" PRId64 "%s)", | |
1093 | predicted_percent, cycles, str); | |
1094 | ||
1095 | return scnprintf(bf, bfsize, | |
1096 | " (predicted:%.1f%%, cycles:%" PRId64 "%s)", | |
1097 | predicted_percent, cycles, str); | |
1098 | } | |
1099 | ||
1100 | if (fp) | |
1101 | return fprintf(fp, | |
1102 | " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)", | |
1103 | predicted_percent, abort_count, cycles, str); | |
1104 | ||
1105 | return scnprintf(bf, bfsize, | |
1106 | " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)", | |
1107 | predicted_percent, abort_count, cycles, str); | |
1108 | } | |
1109 | ||
1110 | int callchain_list_counts__printf_value(struct callchain_node *node, | |
1111 | struct callchain_list *clist, | |
1112 | FILE *fp, char *bf, int bfsize) | |
1113 | { | |
1114 | u64 branch_count, predicted_count; | |
1115 | u64 abort_count, cycles_count; | |
1116 | u64 iter_count = 0, samples_count = 0; | |
1117 | ||
1118 | branch_count = clist->branch_count; | |
1119 | predicted_count = clist->predicted_count; | |
1120 | abort_count = clist->abort_count; | |
1121 | cycles_count = clist->cycles_count; | |
1122 | ||
1123 | if (node) { | |
1124 | struct callchain_list *call; | |
1125 | ||
1126 | list_for_each_entry(call, &node->val, list) { | |
1127 | iter_count += call->iter_count; | |
1128 | samples_count += call->samples_count; | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | return callchain_counts_printf(fp, bf, bfsize, branch_count, | |
1133 | predicted_count, abort_count, | |
1134 | cycles_count, iter_count, samples_count); | |
1135 | } | |
1136 | ||
d114960c NK |
1137 | static void free_callchain_node(struct callchain_node *node) |
1138 | { | |
1139 | struct callchain_list *list, *tmp; | |
1140 | struct callchain_node *child; | |
1141 | struct rb_node *n; | |
1142 | ||
4b3a3212 NK |
1143 | list_for_each_entry_safe(list, tmp, &node->parent_val, list) { |
1144 | list_del(&list->list); | |
1145 | free(list); | |
1146 | } | |
1147 | ||
d114960c NK |
1148 | list_for_each_entry_safe(list, tmp, &node->val, list) { |
1149 | list_del(&list->list); | |
1150 | free(list); | |
1151 | } | |
1152 | ||
1153 | n = rb_first(&node->rb_root_in); | |
1154 | while (n) { | |
1155 | child = container_of(n, struct callchain_node, rb_node_in); | |
1156 | n = rb_next(n); | |
1157 | rb_erase(&child->rb_node_in, &node->rb_root_in); | |
1158 | ||
1159 | free_callchain_node(child); | |
1160 | free(child); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | void free_callchain(struct callchain_root *root) | |
1165 | { | |
1166 | if (!symbol_conf.use_callchain) | |
1167 | return; | |
1168 | ||
1169 | free_callchain_node(&root->node); | |
1170 | } | |
4b3a3212 | 1171 | |
42b276a2 NK |
1172 | static u64 decay_callchain_node(struct callchain_node *node) |
1173 | { | |
1174 | struct callchain_node *child; | |
1175 | struct rb_node *n; | |
1176 | u64 child_hits = 0; | |
1177 | ||
1178 | n = rb_first(&node->rb_root_in); | |
1179 | while (n) { | |
1180 | child = container_of(n, struct callchain_node, rb_node_in); | |
1181 | ||
1182 | child_hits += decay_callchain_node(child); | |
1183 | n = rb_next(n); | |
1184 | } | |
1185 | ||
1186 | node->hit = (node->hit * 7) / 8; | |
1187 | node->children_hit = child_hits; | |
1188 | ||
1189 | return node->hit; | |
1190 | } | |
1191 | ||
1192 | void decay_callchain(struct callchain_root *root) | |
1193 | { | |
1194 | if (!symbol_conf.use_callchain) | |
1195 | return; | |
1196 | ||
1197 | decay_callchain_node(&root->node); | |
1198 | } | |
1199 | ||
4b3a3212 NK |
1200 | int callchain_node__make_parent_list(struct callchain_node *node) |
1201 | { | |
1202 | struct callchain_node *parent = node->parent; | |
1203 | struct callchain_list *chain, *new; | |
1204 | LIST_HEAD(head); | |
1205 | ||
1206 | while (parent) { | |
1207 | list_for_each_entry_reverse(chain, &parent->val, list) { | |
1208 | new = malloc(sizeof(*new)); | |
1209 | if (new == NULL) | |
1210 | goto out; | |
1211 | *new = *chain; | |
1212 | new->has_children = false; | |
1213 | list_add_tail(&new->list, &head); | |
1214 | } | |
1215 | parent = parent->parent; | |
1216 | } | |
1217 | ||
1218 | list_for_each_entry_safe_reverse(chain, new, &head, list) | |
1219 | list_move_tail(&chain->list, &node->parent_val); | |
1220 | ||
1221 | if (!list_empty(&node->parent_val)) { | |
1222 | chain = list_first_entry(&node->parent_val, struct callchain_list, list); | |
1223 | chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node); | |
1224 | ||
1225 | chain = list_first_entry(&node->val, struct callchain_list, list); | |
1226 | chain->has_children = false; | |
1227 | } | |
1228 | return 0; | |
1229 | ||
1230 | out: | |
1231 | list_for_each_entry_safe(chain, new, &head, list) { | |
1232 | list_del(&chain->list); | |
1233 | free(chain); | |
1234 | } | |
1235 | return -ENOMEM; | |
1236 | } | |
571f1eb9 NK |
1237 | |
1238 | int callchain_cursor__copy(struct callchain_cursor *dst, | |
1239 | struct callchain_cursor *src) | |
1240 | { | |
1241 | int rc = 0; | |
1242 | ||
1243 | callchain_cursor_reset(dst); | |
1244 | callchain_cursor_commit(src); | |
1245 | ||
1246 | while (true) { | |
1247 | struct callchain_cursor_node *node; | |
1248 | ||
1249 | node = callchain_cursor_current(src); | |
1250 | if (node == NULL) | |
1251 | break; | |
1252 | ||
1253 | rc = callchain_cursor_append(dst, node->ip, node->map, node->sym, | |
1254 | node->branch, &node->branch_flags, | |
1255 | node->nr_loop_iter, node->samples); | |
1256 | if (rc) | |
1257 | break; | |
1258 | ||
1259 | callchain_cursor_advance(src); | |
1260 | } | |
1261 | ||
1262 | return rc; | |
1263 | } |