]>
Commit | Line | Data |
---|---|---|
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 | 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 DZ |
111 | |
112 | symbol_conf.use_callchain = true; | |
113 | ||
114 | if (!arg) | |
115 | return 0; | |
116 | ||
e8232f1a NK |
117 | while ((tok = strtok((char *)arg, ",")) != NULL) { |
118 | if (!strncmp(tok, "none", strlen(tok))) { | |
119 | callchain_param.mode = CHAIN_NONE; | |
120 | symbol_conf.use_callchain = false; | |
121 | return 0; | |
122 | } | |
123 | ||
2b9240ca NK |
124 | if (!parse_callchain_mode(tok) || |
125 | !parse_callchain_order(tok) || | |
f2af0086 NK |
126 | !parse_callchain_sort_key(tok) || |
127 | !parse_callchain_value(tok)) { | |
2b9240ca | 128 | /* parsing ok - move on to the next */ |
a2c10d39 NK |
129 | try_stack_size = false; |
130 | goto next; | |
131 | } else if (allow_record_opt && !record_opt_set) { | |
132 | if (parse_callchain_record(tok, &callchain_param)) | |
133 | goto try_numbers; | |
134 | ||
135 | /* assume that number followed by 'dwarf' is stack size */ | |
136 | if (callchain_param.record_mode == CALLCHAIN_DWARF) | |
137 | try_stack_size = true; | |
138 | ||
139 | record_opt_set = true; | |
140 | goto next; | |
141 | } | |
142 | ||
143 | try_numbers: | |
144 | if (try_stack_size) { | |
145 | unsigned long size = 0; | |
146 | ||
147 | if (get_stack_size(tok, &size) < 0) | |
148 | return -1; | |
149 | callchain_param.dump_size = size; | |
150 | try_stack_size = false; | |
2b9240ca NK |
151 | } else if (!minpcnt_set) { |
152 | /* try to get the min percent */ | |
e8232f1a NK |
153 | callchain_param.min_percent = strtod(tok, &endptr); |
154 | if (tok == endptr) | |
155 | return -1; | |
156 | minpcnt_set = true; | |
157 | } else { | |
158 | /* try print limit at last */ | |
159 | callchain_param.print_limit = strtoul(tok, &endptr, 0); | |
160 | if (tok == endptr) | |
161 | return -1; | |
162 | } | |
a2c10d39 | 163 | next: |
e8232f1a | 164 | arg = NULL; |
cff6bb46 DZ |
165 | } |
166 | ||
cff6bb46 DZ |
167 | if (callchain_register_param(&callchain_param) < 0) { |
168 | pr_err("Can't register callchain params\n"); | |
169 | return -1; | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | ||
a2c10d39 NK |
174 | int parse_callchain_report_opt(const char *arg) |
175 | { | |
176 | return __parse_callchain_report_opt(arg, false); | |
177 | } | |
178 | ||
179 | int parse_callchain_top_opt(const char *arg) | |
180 | { | |
181 | return __parse_callchain_report_opt(arg, true); | |
182 | } | |
183 | ||
2b9240ca NK |
184 | int perf_callchain_config(const char *var, const char *value) |
185 | { | |
186 | char *endptr; | |
187 | ||
188 | if (prefixcmp(var, "call-graph.")) | |
189 | return 0; | |
190 | var += sizeof("call-graph.") - 1; | |
191 | ||
192 | if (!strcmp(var, "record-mode")) | |
c3a6a8c4 | 193 | return parse_callchain_record_opt(value, &callchain_param); |
2b9240ca NK |
194 | #ifdef HAVE_DWARF_UNWIND_SUPPORT |
195 | if (!strcmp(var, "dump-size")) { | |
196 | unsigned long size = 0; | |
197 | int ret; | |
198 | ||
199 | ret = get_stack_size(value, &size); | |
200 | callchain_param.dump_size = size; | |
201 | ||
202 | return ret; | |
203 | } | |
204 | #endif | |
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; | |
8cb76d99 | 441 | list_add_tail(&call->list, &node->val); |
1b3a0e95 FW |
442 | |
443 | callchain_cursor_advance(cursor); | |
444 | cursor_node = callchain_cursor_current(cursor); | |
8cb76d99 | 445 | } |
8451cbb9 | 446 | return 0; |
8cb76d99 FW |
447 | } |
448 | ||
e369517c | 449 | static struct callchain_node * |
1b3a0e95 FW |
450 | add_child(struct callchain_node *parent, |
451 | struct callchain_cursor *cursor, | |
452 | u64 period) | |
8cb76d99 FW |
453 | { |
454 | struct callchain_node *new; | |
455 | ||
deac911c | 456 | new = create_child(parent, false); |
7565bd39 NK |
457 | if (new == NULL) |
458 | return NULL; | |
459 | ||
8451cbb9 NK |
460 | if (fill_node(new, cursor) < 0) { |
461 | struct callchain_list *call, *tmp; | |
462 | ||
463 | list_for_each_entry_safe(call, tmp, &new->val, list) { | |
464 | list_del(&call->list); | |
465 | free(call); | |
466 | } | |
467 | free(new); | |
468 | return NULL; | |
469 | } | |
8cb76d99 | 470 | |
1953287b | 471 | new->children_hit = 0; |
108553e1 | 472 | new->hit = period; |
5e47f8ff NK |
473 | new->children_count = 0; |
474 | new->count = 1; | |
e369517c NK |
475 | return new; |
476 | } | |
477 | ||
2d713b80 NK |
478 | enum match_result { |
479 | MATCH_ERROR = -1, | |
480 | MATCH_EQ, | |
481 | MATCH_LT, | |
482 | MATCH_GT, | |
483 | }; | |
484 | ||
485 | static enum match_result match_chain(struct callchain_cursor_node *node, | |
486 | struct callchain_list *cnode) | |
e369517c NK |
487 | { |
488 | struct symbol *sym = node->sym; | |
2d713b80 | 489 | u64 left, right; |
e369517c NK |
490 | |
491 | if (cnode->ms.sym && sym && | |
2d713b80 NK |
492 | callchain_param.key == CCKEY_FUNCTION) { |
493 | left = cnode->ms.sym->start; | |
494 | right = sym->start; | |
495 | } else { | |
496 | left = cnode->ip; | |
497 | right = node->ip; | |
498 | } | |
499 | ||
500 | if (left == right) | |
501 | return MATCH_EQ; | |
502 | ||
503 | return left > right ? MATCH_GT : MATCH_LT; | |
8cb76d99 FW |
504 | } |
505 | ||
deac911c FW |
506 | /* |
507 | * Split the parent in two parts (a new child is created) and | |
508 | * give a part of its callchain to the created child. | |
509 | * Then create another child to host the given callchain of new branch | |
510 | */ | |
f2bb4c5a | 511 | static int |
1b3a0e95 FW |
512 | split_add_child(struct callchain_node *parent, |
513 | struct callchain_cursor *cursor, | |
514 | struct callchain_list *to_split, | |
515 | u64 idx_parents, u64 idx_local, u64 period) | |
8cb76d99 FW |
516 | { |
517 | struct callchain_node *new; | |
deac911c | 518 | struct list_head *old_tail; |
f37a291c | 519 | unsigned int idx_total = idx_parents + idx_local; |
8cb76d99 FW |
520 | |
521 | /* split */ | |
deac911c | 522 | new = create_child(parent, true); |
f2bb4c5a NK |
523 | if (new == NULL) |
524 | return -1; | |
deac911c FW |
525 | |
526 | /* split the callchain and move a part to the new child */ | |
527 | old_tail = parent->val.prev; | |
528 | list_del_range(&to_split->list, old_tail); | |
529 | new->val.next = &to_split->list; | |
530 | new->val.prev = old_tail; | |
531 | to_split->list.prev = &new->val; | |
532 | old_tail->next = &new->val; | |
8cb76d99 | 533 | |
deac911c FW |
534 | /* split the hits */ |
535 | new->hit = parent->hit; | |
1953287b | 536 | new->children_hit = parent->children_hit; |
f08c3154 | 537 | parent->children_hit = callchain_cumul_hits(new); |
deac911c FW |
538 | new->val_nr = parent->val_nr - idx_local; |
539 | parent->val_nr = idx_local; | |
5e47f8ff NK |
540 | new->count = parent->count; |
541 | new->children_count = parent->children_count; | |
542 | parent->children_count = callchain_cumul_counts(new); | |
deac911c FW |
543 | |
544 | /* create a new child for the new branch if any */ | |
1b3a0e95 | 545 | if (idx_total < cursor->nr) { |
e369517c NK |
546 | struct callchain_node *first; |
547 | struct callchain_list *cnode; | |
548 | struct callchain_cursor_node *node; | |
549 | struct rb_node *p, **pp; | |
550 | ||
deac911c | 551 | parent->hit = 0; |
108553e1 | 552 | parent->children_hit += period; |
5e47f8ff NK |
553 | parent->count = 0; |
554 | parent->children_count += 1; | |
e369517c NK |
555 | |
556 | node = callchain_cursor_current(cursor); | |
557 | new = add_child(parent, cursor, period); | |
7565bd39 | 558 | if (new == NULL) |
f2bb4c5a | 559 | return -1; |
e369517c NK |
560 | |
561 | /* | |
562 | * This is second child since we moved parent's children | |
563 | * to new (first) child above. | |
564 | */ | |
565 | p = parent->rb_root_in.rb_node; | |
566 | first = rb_entry(p, struct callchain_node, rb_node_in); | |
567 | cnode = list_first_entry(&first->val, struct callchain_list, | |
568 | list); | |
569 | ||
2d713b80 | 570 | if (match_chain(node, cnode) == MATCH_LT) |
e369517c NK |
571 | pp = &p->rb_left; |
572 | else | |
573 | pp = &p->rb_right; | |
574 | ||
575 | rb_link_node(&new->rb_node_in, p, pp); | |
576 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); | |
deac911c | 577 | } else { |
108553e1 | 578 | parent->hit = period; |
5e47f8ff | 579 | parent->count = 1; |
deac911c | 580 | } |
f2bb4c5a | 581 | return 0; |
8cb76d99 FW |
582 | } |
583 | ||
2d713b80 | 584 | static enum match_result |
1b3a0e95 FW |
585 | append_chain(struct callchain_node *root, |
586 | struct callchain_cursor *cursor, | |
587 | u64 period); | |
8cb76d99 | 588 | |
deac911c | 589 | static void |
1b3a0e95 FW |
590 | append_chain_children(struct callchain_node *root, |
591 | struct callchain_cursor *cursor, | |
592 | u64 period) | |
8cb76d99 FW |
593 | { |
594 | struct callchain_node *rnode; | |
e369517c NK |
595 | struct callchain_cursor_node *node; |
596 | struct rb_node **p = &root->rb_root_in.rb_node; | |
597 | struct rb_node *parent = NULL; | |
598 | ||
599 | node = callchain_cursor_current(cursor); | |
600 | if (!node) | |
601 | return; | |
8cb76d99 FW |
602 | |
603 | /* lookup in childrens */ | |
e369517c | 604 | while (*p) { |
2d713b80 | 605 | enum match_result ret; |
f37a291c | 606 | |
e369517c NK |
607 | parent = *p; |
608 | rnode = rb_entry(parent, struct callchain_node, rb_node_in); | |
e369517c | 609 | |
b965bb41 FW |
610 | /* If at least first entry matches, rely to children */ |
611 | ret = append_chain(rnode, cursor, period); | |
2d713b80 | 612 | if (ret == MATCH_EQ) |
1953287b | 613 | goto inc_children_hit; |
e369517c | 614 | |
2d713b80 | 615 | if (ret == MATCH_LT) |
e369517c NK |
616 | p = &parent->rb_left; |
617 | else | |
618 | p = &parent->rb_right; | |
8cb76d99 | 619 | } |
deac911c | 620 | /* nothing in children, add to the current node */ |
e369517c | 621 | rnode = add_child(root, cursor, period); |
7565bd39 NK |
622 | if (rnode == NULL) |
623 | return; | |
624 | ||
e369517c NK |
625 | rb_link_node(&rnode->rb_node_in, parent, p); |
626 | rb_insert_color(&rnode->rb_node_in, &root->rb_root_in); | |
e05b876c | 627 | |
1953287b | 628 | inc_children_hit: |
108553e1 | 629 | root->children_hit += period; |
5e47f8ff | 630 | root->children_count++; |
8cb76d99 FW |
631 | } |
632 | ||
2d713b80 | 633 | static enum match_result |
1b3a0e95 FW |
634 | append_chain(struct callchain_node *root, |
635 | struct callchain_cursor *cursor, | |
636 | u64 period) | |
8cb76d99 FW |
637 | { |
638 | struct callchain_list *cnode; | |
1b3a0e95 | 639 | u64 start = cursor->pos; |
8cb76d99 | 640 | bool found = false; |
1b3a0e95 | 641 | u64 matches; |
2d713b80 | 642 | enum match_result cmp = MATCH_ERROR; |
8cb76d99 | 643 | |
deac911c FW |
644 | /* |
645 | * Lookup in the current node | |
646 | * If we have a symbol, then compare the start to match | |
99571ab3 AK |
647 | * anywhere inside a function, unless function |
648 | * mode is disabled. | |
deac911c | 649 | */ |
8cb76d99 | 650 | list_for_each_entry(cnode, &root->val, list) { |
1b3a0e95 | 651 | struct callchain_cursor_node *node; |
301fde27 | 652 | |
1b3a0e95 FW |
653 | node = callchain_cursor_current(cursor); |
654 | if (!node) | |
deac911c | 655 | break; |
301fde27 | 656 | |
b965bb41 | 657 | cmp = match_chain(node, cnode); |
2d713b80 | 658 | if (cmp != MATCH_EQ) |
8cb76d99 | 659 | break; |
301fde27 | 660 | |
e369517c | 661 | found = true; |
1b3a0e95 FW |
662 | |
663 | callchain_cursor_advance(cursor); | |
8cb76d99 FW |
664 | } |
665 | ||
e369517c | 666 | /* matches not, relay no the parent */ |
1b3a0e95 | 667 | if (!found) { |
2d713b80 | 668 | WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n"); |
b965bb41 | 669 | return cmp; |
1b3a0e95 FW |
670 | } |
671 | ||
672 | matches = cursor->pos - start; | |
8cb76d99 FW |
673 | |
674 | /* we match only a part of the node. Split it and add the new chain */ | |
1b3a0e95 | 675 | if (matches < root->val_nr) { |
f2bb4c5a NK |
676 | if (split_add_child(root, cursor, cnode, start, matches, |
677 | period) < 0) | |
678 | return MATCH_ERROR; | |
679 | ||
2d713b80 | 680 | return MATCH_EQ; |
8cb76d99 FW |
681 | } |
682 | ||
683 | /* we match 100% of the path, increment the hit */ | |
1b3a0e95 | 684 | if (matches == root->val_nr && cursor->pos == cursor->nr) { |
108553e1 | 685 | root->hit += period; |
5e47f8ff | 686 | root->count++; |
2d713b80 | 687 | return MATCH_EQ; |
8cb76d99 FW |
688 | } |
689 | ||
deac911c | 690 | /* We match the node and still have a part remaining */ |
1b3a0e95 | 691 | append_chain_children(root, cursor, period); |
deac911c | 692 | |
2d713b80 | 693 | return MATCH_EQ; |
8cb76d99 FW |
694 | } |
695 | ||
1b3a0e95 FW |
696 | int callchain_append(struct callchain_root *root, |
697 | struct callchain_cursor *cursor, | |
698 | u64 period) | |
8cb76d99 | 699 | { |
1b3a0e95 | 700 | if (!cursor->nr) |
301fde27 FW |
701 | return 0; |
702 | ||
1b3a0e95 | 703 | callchain_cursor_commit(cursor); |
301fde27 | 704 | |
1b3a0e95 | 705 | append_chain_children(&root->node, cursor, period); |
d2009c51 | 706 | |
1b3a0e95 FW |
707 | if (cursor->nr > root->max_depth) |
708 | root->max_depth = cursor->nr; | |
301fde27 FW |
709 | |
710 | return 0; | |
8cb76d99 | 711 | } |
612d4fd7 FW |
712 | |
713 | static int | |
1b3a0e95 FW |
714 | merge_chain_branch(struct callchain_cursor *cursor, |
715 | struct callchain_node *dst, struct callchain_node *src) | |
612d4fd7 | 716 | { |
1b3a0e95 | 717 | struct callchain_cursor_node **old_last = cursor->last; |
e369517c | 718 | struct callchain_node *child; |
612d4fd7 | 719 | struct callchain_list *list, *next_list; |
e369517c | 720 | struct rb_node *n; |
1b3a0e95 | 721 | int old_pos = cursor->nr; |
612d4fd7 FW |
722 | int err = 0; |
723 | ||
724 | list_for_each_entry_safe(list, next_list, &src->val, list) { | |
1b3a0e95 FW |
725 | callchain_cursor_append(cursor, list->ip, |
726 | list->ms.map, list->ms.sym); | |
612d4fd7 FW |
727 | list_del(&list->list); |
728 | free(list); | |
729 | } | |
730 | ||
1b3a0e95 FW |
731 | if (src->hit) { |
732 | callchain_cursor_commit(cursor); | |
733 | append_chain_children(dst, cursor, src->hit); | |
734 | } | |
612d4fd7 | 735 | |
e369517c NK |
736 | n = rb_first(&src->rb_root_in); |
737 | while (n) { | |
738 | child = container_of(n, struct callchain_node, rb_node_in); | |
739 | n = rb_next(n); | |
740 | rb_erase(&child->rb_node_in, &src->rb_root_in); | |
741 | ||
1b3a0e95 | 742 | err = merge_chain_branch(cursor, dst, child); |
612d4fd7 FW |
743 | if (err) |
744 | break; | |
745 | ||
612d4fd7 FW |
746 | free(child); |
747 | } | |
748 | ||
1b3a0e95 FW |
749 | cursor->nr = old_pos; |
750 | cursor->last = old_last; | |
612d4fd7 FW |
751 | |
752 | return err; | |
753 | } | |
754 | ||
1b3a0e95 FW |
755 | int callchain_merge(struct callchain_cursor *cursor, |
756 | struct callchain_root *dst, struct callchain_root *src) | |
757 | { | |
758 | return merge_chain_branch(cursor, &dst->node, &src->node); | |
759 | } | |
760 | ||
761 | int callchain_cursor_append(struct callchain_cursor *cursor, | |
762 | u64 ip, struct map *map, struct symbol *sym) | |
612d4fd7 | 763 | { |
1b3a0e95 | 764 | struct callchain_cursor_node *node = *cursor->last; |
612d4fd7 | 765 | |
1b3a0e95 | 766 | if (!node) { |
91b98804 | 767 | node = calloc(1, sizeof(*node)); |
1b3a0e95 FW |
768 | if (!node) |
769 | return -ENOMEM; | |
612d4fd7 | 770 | |
1b3a0e95 FW |
771 | *cursor->last = node; |
772 | } | |
612d4fd7 | 773 | |
1b3a0e95 FW |
774 | node->ip = ip; |
775 | node->map = map; | |
776 | node->sym = sym; | |
612d4fd7 | 777 | |
1b3a0e95 | 778 | cursor->nr++; |
612d4fd7 | 779 | |
1b3a0e95 FW |
780 | cursor->last = &node->next; |
781 | ||
782 | return 0; | |
612d4fd7 | 783 | } |
2dc9fb1a NK |
784 | |
785 | int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent, | |
786 | struct perf_evsel *evsel, struct addr_location *al, | |
787 | int max_stack) | |
788 | { | |
789 | if (sample->callchain == NULL) | |
790 | return 0; | |
791 | ||
7a13aa28 NK |
792 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain || |
793 | sort__has_parent) { | |
cc8b7c2b ACM |
794 | return thread__resolve_callchain(al->thread, evsel, sample, |
795 | parent, al, max_stack); | |
2dc9fb1a NK |
796 | } |
797 | return 0; | |
798 | } | |
799 | ||
800 | int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample) | |
801 | { | |
4d40b051 | 802 | if (!symbol_conf.use_callchain || sample->callchain == NULL) |
2dc9fb1a NK |
803 | return 0; |
804 | return callchain_append(he->callchain, &callchain_cursor, sample->period); | |
805 | } | |
c7405d85 NK |
806 | |
807 | int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node, | |
808 | bool hide_unresolved) | |
809 | { | |
810 | al->map = node->map; | |
811 | al->sym = node->sym; | |
812 | if (node->map) | |
813 | al->addr = node->map->map_ip(node->map, node->ip); | |
814 | else | |
815 | al->addr = node->ip; | |
816 | ||
817 | if (al->sym == NULL) { | |
818 | if (hide_unresolved) | |
819 | return 0; | |
820 | if (al->map == NULL) | |
821 | goto out; | |
822 | } | |
823 | ||
824 | if (al->map->groups == &al->machine->kmaps) { | |
825 | if (machine__is_host(al->machine)) { | |
826 | al->cpumode = PERF_RECORD_MISC_KERNEL; | |
827 | al->level = 'k'; | |
828 | } else { | |
829 | al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL; | |
830 | al->level = 'g'; | |
831 | } | |
832 | } else { | |
833 | if (machine__is_host(al->machine)) { | |
834 | al->cpumode = PERF_RECORD_MISC_USER; | |
835 | al->level = '.'; | |
836 | } else if (perf_guest) { | |
837 | al->cpumode = PERF_RECORD_MISC_GUEST_USER; | |
838 | al->level = 'u'; | |
839 | } else { | |
840 | al->cpumode = PERF_RECORD_MISC_HYPERVISOR; | |
841 | al->level = 'H'; | |
842 | } | |
843 | } | |
844 | ||
845 | out: | |
846 | return 1; | |
847 | } | |
2989ccaa AK |
848 | |
849 | char *callchain_list__sym_name(struct callchain_list *cl, | |
850 | char *bf, size_t bfsize, bool show_dso) | |
851 | { | |
852 | int printed; | |
853 | ||
854 | if (cl->ms.sym) { | |
23f0981b AK |
855 | if (callchain_param.key == CCKEY_ADDRESS && |
856 | cl->ms.map && !cl->srcline) | |
857 | cl->srcline = get_srcline(cl->ms.map->dso, | |
858 | map__rip_2objdump(cl->ms.map, | |
85c116a6 AK |
859 | cl->ip), |
860 | cl->ms.sym, false); | |
23f0981b AK |
861 | if (cl->srcline) |
862 | printed = scnprintf(bf, bfsize, "%s %s", | |
863 | cl->ms.sym->name, cl->srcline); | |
864 | else | |
865 | printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name); | |
2989ccaa AK |
866 | } else |
867 | printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip); | |
868 | ||
869 | if (show_dso) | |
870 | scnprintf(bf + printed, bfsize - printed, " %s", | |
871 | cl->ms.map ? | |
872 | cl->ms.map->dso->short_name : | |
873 | "unknown"); | |
874 | ||
875 | return bf; | |
876 | } | |
d114960c | 877 | |
5ab250ca NK |
878 | char *callchain_node__scnprintf_value(struct callchain_node *node, |
879 | char *bf, size_t bfsize, u64 total) | |
880 | { | |
881 | double percent = 0.0; | |
882 | u64 period = callchain_cumul_hits(node); | |
f2af0086 | 883 | unsigned count = callchain_cumul_counts(node); |
5ab250ca | 884 | |
f2af0086 | 885 | if (callchain_param.mode == CHAIN_FOLDED) { |
5ab250ca | 886 | period = node->hit; |
f2af0086 NK |
887 | count = node->count; |
888 | } | |
5ab250ca | 889 | |
f2af0086 NK |
890 | switch (callchain_param.value) { |
891 | case CCVAL_PERIOD: | |
892 | scnprintf(bf, bfsize, "%"PRIu64, period); | |
893 | break; | |
894 | case CCVAL_COUNT: | |
895 | scnprintf(bf, bfsize, "%u", count); | |
896 | break; | |
897 | case CCVAL_PERCENT: | |
898 | default: | |
899 | if (total) | |
900 | percent = period * 100.0 / total; | |
901 | scnprintf(bf, bfsize, "%.2f%%", percent); | |
902 | break; | |
903 | } | |
5ab250ca NK |
904 | return bf; |
905 | } | |
906 | ||
907 | int callchain_node__fprintf_value(struct callchain_node *node, | |
908 | FILE *fp, u64 total) | |
909 | { | |
910 | double percent = 0.0; | |
911 | u64 period = callchain_cumul_hits(node); | |
f2af0086 | 912 | unsigned count = callchain_cumul_counts(node); |
5ab250ca | 913 | |
f2af0086 | 914 | if (callchain_param.mode == CHAIN_FOLDED) { |
5ab250ca | 915 | period = node->hit; |
f2af0086 NK |
916 | count = node->count; |
917 | } | |
5ab250ca | 918 | |
f2af0086 NK |
919 | switch (callchain_param.value) { |
920 | case CCVAL_PERIOD: | |
921 | return fprintf(fp, "%"PRIu64, period); | |
922 | case CCVAL_COUNT: | |
923 | return fprintf(fp, "%u", count); | |
924 | case CCVAL_PERCENT: | |
925 | default: | |
926 | if (total) | |
927 | percent = period * 100.0 / total; | |
928 | return percent_color_fprintf(fp, "%.2f%%", percent); | |
929 | } | |
930 | return 0; | |
5ab250ca NK |
931 | } |
932 | ||
d114960c NK |
933 | static void free_callchain_node(struct callchain_node *node) |
934 | { | |
935 | struct callchain_list *list, *tmp; | |
936 | struct callchain_node *child; | |
937 | struct rb_node *n; | |
938 | ||
4b3a3212 NK |
939 | list_for_each_entry_safe(list, tmp, &node->parent_val, list) { |
940 | list_del(&list->list); | |
941 | free(list); | |
942 | } | |
943 | ||
d114960c NK |
944 | list_for_each_entry_safe(list, tmp, &node->val, list) { |
945 | list_del(&list->list); | |
946 | free(list); | |
947 | } | |
948 | ||
949 | n = rb_first(&node->rb_root_in); | |
950 | while (n) { | |
951 | child = container_of(n, struct callchain_node, rb_node_in); | |
952 | n = rb_next(n); | |
953 | rb_erase(&child->rb_node_in, &node->rb_root_in); | |
954 | ||
955 | free_callchain_node(child); | |
956 | free(child); | |
957 | } | |
958 | } | |
959 | ||
960 | void free_callchain(struct callchain_root *root) | |
961 | { | |
962 | if (!symbol_conf.use_callchain) | |
963 | return; | |
964 | ||
965 | free_callchain_node(&root->node); | |
966 | } | |
4b3a3212 | 967 | |
42b276a2 NK |
968 | static u64 decay_callchain_node(struct callchain_node *node) |
969 | { | |
970 | struct callchain_node *child; | |
971 | struct rb_node *n; | |
972 | u64 child_hits = 0; | |
973 | ||
974 | n = rb_first(&node->rb_root_in); | |
975 | while (n) { | |
976 | child = container_of(n, struct callchain_node, rb_node_in); | |
977 | ||
978 | child_hits += decay_callchain_node(child); | |
979 | n = rb_next(n); | |
980 | } | |
981 | ||
982 | node->hit = (node->hit * 7) / 8; | |
983 | node->children_hit = child_hits; | |
984 | ||
985 | return node->hit; | |
986 | } | |
987 | ||
988 | void decay_callchain(struct callchain_root *root) | |
989 | { | |
990 | if (!symbol_conf.use_callchain) | |
991 | return; | |
992 | ||
993 | decay_callchain_node(&root->node); | |
994 | } | |
995 | ||
4b3a3212 NK |
996 | int callchain_node__make_parent_list(struct callchain_node *node) |
997 | { | |
998 | struct callchain_node *parent = node->parent; | |
999 | struct callchain_list *chain, *new; | |
1000 | LIST_HEAD(head); | |
1001 | ||
1002 | while (parent) { | |
1003 | list_for_each_entry_reverse(chain, &parent->val, list) { | |
1004 | new = malloc(sizeof(*new)); | |
1005 | if (new == NULL) | |
1006 | goto out; | |
1007 | *new = *chain; | |
1008 | new->has_children = false; | |
1009 | list_add_tail(&new->list, &head); | |
1010 | } | |
1011 | parent = parent->parent; | |
1012 | } | |
1013 | ||
1014 | list_for_each_entry_safe_reverse(chain, new, &head, list) | |
1015 | list_move_tail(&chain->list, &node->parent_val); | |
1016 | ||
1017 | if (!list_empty(&node->parent_val)) { | |
1018 | chain = list_first_entry(&node->parent_val, struct callchain_list, list); | |
1019 | chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node); | |
1020 | ||
1021 | chain = list_first_entry(&node->val, struct callchain_list, list); | |
1022 | chain->has_children = false; | |
1023 | } | |
1024 | return 0; | |
1025 | ||
1026 | out: | |
1027 | list_for_each_entry_safe(chain, new, &head, list) { | |
1028 | list_del(&chain->list); | |
1029 | free(chain); | |
1030 | } | |
1031 | return -ENOMEM; | |
1032 | } |