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 | { |
d2009c51 | 293 | __sort_chain_flat(rb_root, &root->node, min_hit); |
805d127d FW |
294 | } |
295 | ||
296 | static void __sort_chain_graph_abs(struct callchain_node *node, | |
297 | u64 min_hit) | |
8cb76d99 | 298 | { |
e369517c | 299 | struct rb_node *n; |
8cb76d99 FW |
300 | struct callchain_node *child; |
301 | ||
805d127d | 302 | node->rb_root = RB_ROOT; |
e369517c NK |
303 | n = rb_first(&node->rb_root_in); |
304 | ||
305 | while (n) { | |
306 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
307 | n = rb_next(n); | |
8cb76d99 | 308 | |
805d127d | 309 | __sort_chain_graph_abs(child, min_hit); |
f08c3154 | 310 | if (callchain_cumul_hits(child) >= min_hit) |
805d127d FW |
311 | rb_insert_callchain(&node->rb_root, child, |
312 | CHAIN_GRAPH_ABS); | |
313 | } | |
314 | } | |
315 | ||
316 | static void | |
d2009c51 | 317 | sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root, |
1d037ca1 | 318 | u64 min_hit, struct callchain_param *param __maybe_unused) |
805d127d | 319 | { |
d2009c51 FW |
320 | __sort_chain_graph_abs(&chain_root->node, min_hit); |
321 | rb_root->rb_node = chain_root->node.rb_root.rb_node; | |
4eb3e478 FW |
322 | } |
323 | ||
805d127d FW |
324 | static void __sort_chain_graph_rel(struct callchain_node *node, |
325 | double min_percent) | |
4eb3e478 | 326 | { |
e369517c | 327 | struct rb_node *n; |
4eb3e478 | 328 | struct callchain_node *child; |
805d127d | 329 | u64 min_hit; |
4eb3e478 FW |
330 | |
331 | node->rb_root = RB_ROOT; | |
c0a8865e | 332 | min_hit = ceil(node->children_hit * min_percent); |
4eb3e478 | 333 | |
e369517c NK |
334 | n = rb_first(&node->rb_root_in); |
335 | while (n) { | |
336 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
337 | n = rb_next(n); | |
338 | ||
805d127d | 339 | __sort_chain_graph_rel(child, min_percent); |
f08c3154 | 340 | if (callchain_cumul_hits(child) >= min_hit) |
805d127d FW |
341 | rb_insert_callchain(&node->rb_root, child, |
342 | CHAIN_GRAPH_REL); | |
4eb3e478 FW |
343 | } |
344 | } | |
345 | ||
805d127d | 346 | static void |
d2009c51 | 347 | sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root, |
1d037ca1 | 348 | u64 min_hit __maybe_unused, struct callchain_param *param) |
4eb3e478 | 349 | { |
d2009c51 FW |
350 | __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0); |
351 | rb_root->rb_node = chain_root->node.rb_root.rb_node; | |
8cb76d99 FW |
352 | } |
353 | ||
16537f13 | 354 | int callchain_register_param(struct callchain_param *param) |
805d127d FW |
355 | { |
356 | switch (param->mode) { | |
357 | case CHAIN_GRAPH_ABS: | |
358 | param->sort = sort_chain_graph_abs; | |
359 | break; | |
360 | case CHAIN_GRAPH_REL: | |
361 | param->sort = sort_chain_graph_rel; | |
362 | break; | |
363 | case CHAIN_FLAT: | |
26e77924 | 364 | case CHAIN_FOLDED: |
805d127d FW |
365 | param->sort = sort_chain_flat; |
366 | break; | |
83a0944f | 367 | case CHAIN_NONE: |
805d127d FW |
368 | default: |
369 | return -1; | |
370 | } | |
371 | return 0; | |
372 | } | |
373 | ||
deac911c FW |
374 | /* |
375 | * Create a child for a parent. If inherit_children, then the new child | |
376 | * will become the new parent of it's parent children | |
377 | */ | |
378 | static struct callchain_node * | |
379 | create_child(struct callchain_node *parent, bool inherit_children) | |
8cb76d99 FW |
380 | { |
381 | struct callchain_node *new; | |
382 | ||
cdd5b75b | 383 | new = zalloc(sizeof(*new)); |
8cb76d99 FW |
384 | if (!new) { |
385 | perror("not enough memory to create child for code path tree"); | |
386 | return NULL; | |
387 | } | |
388 | new->parent = parent; | |
8cb76d99 | 389 | INIT_LIST_HEAD(&new->val); |
deac911c FW |
390 | |
391 | if (inherit_children) { | |
e369517c NK |
392 | struct rb_node *n; |
393 | struct callchain_node *child; | |
394 | ||
395 | new->rb_root_in = parent->rb_root_in; | |
396 | parent->rb_root_in = RB_ROOT; | |
deac911c | 397 | |
e369517c NK |
398 | n = rb_first(&new->rb_root_in); |
399 | while (n) { | |
400 | child = rb_entry(n, struct callchain_node, rb_node_in); | |
401 | child->parent = new; | |
402 | n = rb_next(n); | |
403 | } | |
deac911c | 404 | |
e369517c NK |
405 | /* make it the first child */ |
406 | rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node); | |
407 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); | |
deac911c | 408 | } |
8cb76d99 FW |
409 | |
410 | return new; | |
411 | } | |
412 | ||
301fde27 | 413 | |
deac911c FW |
414 | /* |
415 | * Fill the node with callchain values | |
416 | */ | |
8cb76d99 | 417 | static void |
1b3a0e95 | 418 | fill_node(struct callchain_node *node, struct callchain_cursor *cursor) |
8cb76d99 | 419 | { |
1b3a0e95 FW |
420 | struct callchain_cursor_node *cursor_node; |
421 | ||
422 | node->val_nr = cursor->nr - cursor->pos; | |
423 | if (!node->val_nr) | |
424 | pr_warning("Warning: empty node in callchain tree\n"); | |
8cb76d99 | 425 | |
1b3a0e95 FW |
426 | cursor_node = callchain_cursor_current(cursor); |
427 | ||
428 | while (cursor_node) { | |
8cb76d99 FW |
429 | struct callchain_list *call; |
430 | ||
cdd5b75b | 431 | call = zalloc(sizeof(*call)); |
8cb76d99 FW |
432 | if (!call) { |
433 | perror("not enough memory for the code path tree"); | |
434 | return; | |
435 | } | |
1b3a0e95 FW |
436 | call->ip = cursor_node->ip; |
437 | call->ms.sym = cursor_node->sym; | |
438 | call->ms.map = cursor_node->map; | |
8cb76d99 | 439 | list_add_tail(&call->list, &node->val); |
1b3a0e95 FW |
440 | |
441 | callchain_cursor_advance(cursor); | |
442 | cursor_node = callchain_cursor_current(cursor); | |
8cb76d99 | 443 | } |
8cb76d99 FW |
444 | } |
445 | ||
e369517c | 446 | static struct callchain_node * |
1b3a0e95 FW |
447 | add_child(struct callchain_node *parent, |
448 | struct callchain_cursor *cursor, | |
449 | u64 period) | |
8cb76d99 FW |
450 | { |
451 | struct callchain_node *new; | |
452 | ||
deac911c | 453 | new = create_child(parent, false); |
1b3a0e95 | 454 | fill_node(new, cursor); |
8cb76d99 | 455 | |
1953287b | 456 | new->children_hit = 0; |
108553e1 | 457 | new->hit = period; |
5e47f8ff NK |
458 | new->children_count = 0; |
459 | new->count = 1; | |
e369517c NK |
460 | return new; |
461 | } | |
462 | ||
463 | static s64 match_chain(struct callchain_cursor_node *node, | |
464 | struct callchain_list *cnode) | |
465 | { | |
466 | struct symbol *sym = node->sym; | |
467 | ||
468 | if (cnode->ms.sym && sym && | |
469 | callchain_param.key == CCKEY_FUNCTION) | |
470 | return cnode->ms.sym->start - sym->start; | |
471 | else | |
472 | return cnode->ip - node->ip; | |
8cb76d99 FW |
473 | } |
474 | ||
deac911c FW |
475 | /* |
476 | * Split the parent in two parts (a new child is created) and | |
477 | * give a part of its callchain to the created child. | |
478 | * Then create another child to host the given callchain of new branch | |
479 | */ | |
8cb76d99 | 480 | static void |
1b3a0e95 FW |
481 | split_add_child(struct callchain_node *parent, |
482 | struct callchain_cursor *cursor, | |
483 | struct callchain_list *to_split, | |
484 | u64 idx_parents, u64 idx_local, u64 period) | |
8cb76d99 FW |
485 | { |
486 | struct callchain_node *new; | |
deac911c | 487 | struct list_head *old_tail; |
f37a291c | 488 | unsigned int idx_total = idx_parents + idx_local; |
8cb76d99 FW |
489 | |
490 | /* split */ | |
deac911c FW |
491 | new = create_child(parent, true); |
492 | ||
493 | /* split the callchain and move a part to the new child */ | |
494 | old_tail = parent->val.prev; | |
495 | list_del_range(&to_split->list, old_tail); | |
496 | new->val.next = &to_split->list; | |
497 | new->val.prev = old_tail; | |
498 | to_split->list.prev = &new->val; | |
499 | old_tail->next = &new->val; | |
8cb76d99 | 500 | |
deac911c FW |
501 | /* split the hits */ |
502 | new->hit = parent->hit; | |
1953287b | 503 | new->children_hit = parent->children_hit; |
f08c3154 | 504 | parent->children_hit = callchain_cumul_hits(new); |
deac911c FW |
505 | new->val_nr = parent->val_nr - idx_local; |
506 | parent->val_nr = idx_local; | |
5e47f8ff NK |
507 | new->count = parent->count; |
508 | new->children_count = parent->children_count; | |
509 | parent->children_count = callchain_cumul_counts(new); | |
deac911c FW |
510 | |
511 | /* create a new child for the new branch if any */ | |
1b3a0e95 | 512 | if (idx_total < cursor->nr) { |
e369517c NK |
513 | struct callchain_node *first; |
514 | struct callchain_list *cnode; | |
515 | struct callchain_cursor_node *node; | |
516 | struct rb_node *p, **pp; | |
517 | ||
deac911c | 518 | parent->hit = 0; |
108553e1 | 519 | parent->children_hit += period; |
5e47f8ff NK |
520 | parent->count = 0; |
521 | parent->children_count += 1; | |
e369517c NK |
522 | |
523 | node = callchain_cursor_current(cursor); | |
524 | new = add_child(parent, cursor, period); | |
525 | ||
526 | /* | |
527 | * This is second child since we moved parent's children | |
528 | * to new (first) child above. | |
529 | */ | |
530 | p = parent->rb_root_in.rb_node; | |
531 | first = rb_entry(p, struct callchain_node, rb_node_in); | |
532 | cnode = list_first_entry(&first->val, struct callchain_list, | |
533 | list); | |
534 | ||
535 | if (match_chain(node, cnode) < 0) | |
536 | pp = &p->rb_left; | |
537 | else | |
538 | pp = &p->rb_right; | |
539 | ||
540 | rb_link_node(&new->rb_node_in, p, pp); | |
541 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); | |
deac911c | 542 | } else { |
108553e1 | 543 | parent->hit = period; |
5e47f8ff | 544 | parent->count = 1; |
deac911c | 545 | } |
8cb76d99 FW |
546 | } |
547 | ||
548 | static int | |
1b3a0e95 FW |
549 | append_chain(struct callchain_node *root, |
550 | struct callchain_cursor *cursor, | |
551 | u64 period); | |
8cb76d99 | 552 | |
deac911c | 553 | static void |
1b3a0e95 FW |
554 | append_chain_children(struct callchain_node *root, |
555 | struct callchain_cursor *cursor, | |
556 | u64 period) | |
8cb76d99 FW |
557 | { |
558 | struct callchain_node *rnode; | |
e369517c NK |
559 | struct callchain_cursor_node *node; |
560 | struct rb_node **p = &root->rb_root_in.rb_node; | |
561 | struct rb_node *parent = NULL; | |
562 | ||
563 | node = callchain_cursor_current(cursor); | |
564 | if (!node) | |
565 | return; | |
8cb76d99 FW |
566 | |
567 | /* lookup in childrens */ | |
e369517c NK |
568 | while (*p) { |
569 | s64 ret; | |
f37a291c | 570 | |
e369517c NK |
571 | parent = *p; |
572 | rnode = rb_entry(parent, struct callchain_node, rb_node_in); | |
e369517c | 573 | |
b965bb41 FW |
574 | /* If at least first entry matches, rely to children */ |
575 | ret = append_chain(rnode, cursor, period); | |
576 | if (ret == 0) | |
1953287b | 577 | goto inc_children_hit; |
e369517c NK |
578 | |
579 | if (ret < 0) | |
580 | p = &parent->rb_left; | |
581 | else | |
582 | p = &parent->rb_right; | |
8cb76d99 | 583 | } |
deac911c | 584 | /* nothing in children, add to the current node */ |
e369517c NK |
585 | rnode = add_child(root, cursor, period); |
586 | rb_link_node(&rnode->rb_node_in, parent, p); | |
587 | rb_insert_color(&rnode->rb_node_in, &root->rb_root_in); | |
e05b876c | 588 | |
1953287b | 589 | inc_children_hit: |
108553e1 | 590 | root->children_hit += period; |
5e47f8ff | 591 | root->children_count++; |
8cb76d99 FW |
592 | } |
593 | ||
594 | static int | |
1b3a0e95 FW |
595 | append_chain(struct callchain_node *root, |
596 | struct callchain_cursor *cursor, | |
597 | u64 period) | |
8cb76d99 FW |
598 | { |
599 | struct callchain_list *cnode; | |
1b3a0e95 | 600 | u64 start = cursor->pos; |
8cb76d99 | 601 | bool found = false; |
1b3a0e95 | 602 | u64 matches; |
b965bb41 | 603 | int cmp = 0; |
8cb76d99 | 604 | |
deac911c FW |
605 | /* |
606 | * Lookup in the current node | |
607 | * If we have a symbol, then compare the start to match | |
99571ab3 AK |
608 | * anywhere inside a function, unless function |
609 | * mode is disabled. | |
deac911c | 610 | */ |
8cb76d99 | 611 | list_for_each_entry(cnode, &root->val, list) { |
1b3a0e95 | 612 | struct callchain_cursor_node *node; |
301fde27 | 613 | |
1b3a0e95 FW |
614 | node = callchain_cursor_current(cursor); |
615 | if (!node) | |
deac911c | 616 | break; |
301fde27 | 617 | |
b965bb41 FW |
618 | cmp = match_chain(node, cnode); |
619 | if (cmp) | |
8cb76d99 | 620 | break; |
301fde27 | 621 | |
e369517c | 622 | found = true; |
1b3a0e95 FW |
623 | |
624 | callchain_cursor_advance(cursor); | |
8cb76d99 FW |
625 | } |
626 | ||
e369517c | 627 | /* matches not, relay no the parent */ |
1b3a0e95 | 628 | if (!found) { |
b965bb41 | 629 | WARN_ONCE(!cmp, "Chain comparison error\n"); |
b965bb41 | 630 | return cmp; |
1b3a0e95 FW |
631 | } |
632 | ||
633 | matches = cursor->pos - start; | |
8cb76d99 FW |
634 | |
635 | /* we match only a part of the node. Split it and add the new chain */ | |
1b3a0e95 FW |
636 | if (matches < root->val_nr) { |
637 | split_add_child(root, cursor, cnode, start, matches, period); | |
8cb76d99 FW |
638 | return 0; |
639 | } | |
640 | ||
641 | /* we match 100% of the path, increment the hit */ | |
1b3a0e95 | 642 | if (matches == root->val_nr && cursor->pos == cursor->nr) { |
108553e1 | 643 | root->hit += period; |
5e47f8ff | 644 | root->count++; |
8cb76d99 FW |
645 | return 0; |
646 | } | |
647 | ||
deac911c | 648 | /* We match the node and still have a part remaining */ |
1b3a0e95 | 649 | append_chain_children(root, cursor, period); |
deac911c FW |
650 | |
651 | return 0; | |
8cb76d99 FW |
652 | } |
653 | ||
1b3a0e95 FW |
654 | int callchain_append(struct callchain_root *root, |
655 | struct callchain_cursor *cursor, | |
656 | u64 period) | |
8cb76d99 | 657 | { |
1b3a0e95 | 658 | if (!cursor->nr) |
301fde27 FW |
659 | return 0; |
660 | ||
1b3a0e95 | 661 | callchain_cursor_commit(cursor); |
301fde27 | 662 | |
1b3a0e95 | 663 | append_chain_children(&root->node, cursor, period); |
d2009c51 | 664 | |
1b3a0e95 FW |
665 | if (cursor->nr > root->max_depth) |
666 | root->max_depth = cursor->nr; | |
301fde27 FW |
667 | |
668 | return 0; | |
8cb76d99 | 669 | } |
612d4fd7 FW |
670 | |
671 | static int | |
1b3a0e95 FW |
672 | merge_chain_branch(struct callchain_cursor *cursor, |
673 | struct callchain_node *dst, struct callchain_node *src) | |
612d4fd7 | 674 | { |
1b3a0e95 | 675 | struct callchain_cursor_node **old_last = cursor->last; |
e369517c | 676 | struct callchain_node *child; |
612d4fd7 | 677 | struct callchain_list *list, *next_list; |
e369517c | 678 | struct rb_node *n; |
1b3a0e95 | 679 | int old_pos = cursor->nr; |
612d4fd7 FW |
680 | int err = 0; |
681 | ||
682 | list_for_each_entry_safe(list, next_list, &src->val, list) { | |
1b3a0e95 FW |
683 | callchain_cursor_append(cursor, list->ip, |
684 | list->ms.map, list->ms.sym); | |
612d4fd7 FW |
685 | list_del(&list->list); |
686 | free(list); | |
687 | } | |
688 | ||
1b3a0e95 FW |
689 | if (src->hit) { |
690 | callchain_cursor_commit(cursor); | |
691 | append_chain_children(dst, cursor, src->hit); | |
692 | } | |
612d4fd7 | 693 | |
e369517c NK |
694 | n = rb_first(&src->rb_root_in); |
695 | while (n) { | |
696 | child = container_of(n, struct callchain_node, rb_node_in); | |
697 | n = rb_next(n); | |
698 | rb_erase(&child->rb_node_in, &src->rb_root_in); | |
699 | ||
1b3a0e95 | 700 | err = merge_chain_branch(cursor, dst, child); |
612d4fd7 FW |
701 | if (err) |
702 | break; | |
703 | ||
612d4fd7 FW |
704 | free(child); |
705 | } | |
706 | ||
1b3a0e95 FW |
707 | cursor->nr = old_pos; |
708 | cursor->last = old_last; | |
612d4fd7 FW |
709 | |
710 | return err; | |
711 | } | |
712 | ||
1b3a0e95 FW |
713 | int callchain_merge(struct callchain_cursor *cursor, |
714 | struct callchain_root *dst, struct callchain_root *src) | |
715 | { | |
716 | return merge_chain_branch(cursor, &dst->node, &src->node); | |
717 | } | |
718 | ||
719 | int callchain_cursor_append(struct callchain_cursor *cursor, | |
720 | u64 ip, struct map *map, struct symbol *sym) | |
612d4fd7 | 721 | { |
1b3a0e95 | 722 | struct callchain_cursor_node *node = *cursor->last; |
612d4fd7 | 723 | |
1b3a0e95 | 724 | if (!node) { |
91b98804 | 725 | node = calloc(1, sizeof(*node)); |
1b3a0e95 FW |
726 | if (!node) |
727 | return -ENOMEM; | |
612d4fd7 | 728 | |
1b3a0e95 FW |
729 | *cursor->last = node; |
730 | } | |
612d4fd7 | 731 | |
1b3a0e95 FW |
732 | node->ip = ip; |
733 | node->map = map; | |
734 | node->sym = sym; | |
612d4fd7 | 735 | |
1b3a0e95 | 736 | cursor->nr++; |
612d4fd7 | 737 | |
1b3a0e95 FW |
738 | cursor->last = &node->next; |
739 | ||
740 | return 0; | |
612d4fd7 | 741 | } |
2dc9fb1a NK |
742 | |
743 | int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent, | |
744 | struct perf_evsel *evsel, struct addr_location *al, | |
745 | int max_stack) | |
746 | { | |
747 | if (sample->callchain == NULL) | |
748 | return 0; | |
749 | ||
7a13aa28 NK |
750 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain || |
751 | sort__has_parent) { | |
cc8b7c2b ACM |
752 | return thread__resolve_callchain(al->thread, evsel, sample, |
753 | parent, al, max_stack); | |
2dc9fb1a NK |
754 | } |
755 | return 0; | |
756 | } | |
757 | ||
758 | int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample) | |
759 | { | |
4d40b051 | 760 | if (!symbol_conf.use_callchain || sample->callchain == NULL) |
2dc9fb1a NK |
761 | return 0; |
762 | return callchain_append(he->callchain, &callchain_cursor, sample->period); | |
763 | } | |
c7405d85 NK |
764 | |
765 | int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node, | |
766 | bool hide_unresolved) | |
767 | { | |
768 | al->map = node->map; | |
769 | al->sym = node->sym; | |
770 | if (node->map) | |
771 | al->addr = node->map->map_ip(node->map, node->ip); | |
772 | else | |
773 | al->addr = node->ip; | |
774 | ||
775 | if (al->sym == NULL) { | |
776 | if (hide_unresolved) | |
777 | return 0; | |
778 | if (al->map == NULL) | |
779 | goto out; | |
780 | } | |
781 | ||
782 | if (al->map->groups == &al->machine->kmaps) { | |
783 | if (machine__is_host(al->machine)) { | |
784 | al->cpumode = PERF_RECORD_MISC_KERNEL; | |
785 | al->level = 'k'; | |
786 | } else { | |
787 | al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL; | |
788 | al->level = 'g'; | |
789 | } | |
790 | } else { | |
791 | if (machine__is_host(al->machine)) { | |
792 | al->cpumode = PERF_RECORD_MISC_USER; | |
793 | al->level = '.'; | |
794 | } else if (perf_guest) { | |
795 | al->cpumode = PERF_RECORD_MISC_GUEST_USER; | |
796 | al->level = 'u'; | |
797 | } else { | |
798 | al->cpumode = PERF_RECORD_MISC_HYPERVISOR; | |
799 | al->level = 'H'; | |
800 | } | |
801 | } | |
802 | ||
803 | out: | |
804 | return 1; | |
805 | } | |
2989ccaa AK |
806 | |
807 | char *callchain_list__sym_name(struct callchain_list *cl, | |
808 | char *bf, size_t bfsize, bool show_dso) | |
809 | { | |
810 | int printed; | |
811 | ||
812 | if (cl->ms.sym) { | |
23f0981b AK |
813 | if (callchain_param.key == CCKEY_ADDRESS && |
814 | cl->ms.map && !cl->srcline) | |
815 | cl->srcline = get_srcline(cl->ms.map->dso, | |
816 | map__rip_2objdump(cl->ms.map, | |
85c116a6 AK |
817 | cl->ip), |
818 | cl->ms.sym, false); | |
23f0981b AK |
819 | if (cl->srcline) |
820 | printed = scnprintf(bf, bfsize, "%s %s", | |
821 | cl->ms.sym->name, cl->srcline); | |
822 | else | |
823 | printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name); | |
2989ccaa AK |
824 | } else |
825 | printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip); | |
826 | ||
827 | if (show_dso) | |
828 | scnprintf(bf + printed, bfsize - printed, " %s", | |
829 | cl->ms.map ? | |
830 | cl->ms.map->dso->short_name : | |
831 | "unknown"); | |
832 | ||
833 | return bf; | |
834 | } | |
d114960c | 835 | |
5ab250ca NK |
836 | char *callchain_node__scnprintf_value(struct callchain_node *node, |
837 | char *bf, size_t bfsize, u64 total) | |
838 | { | |
839 | double percent = 0.0; | |
840 | u64 period = callchain_cumul_hits(node); | |
f2af0086 | 841 | unsigned count = callchain_cumul_counts(node); |
5ab250ca | 842 | |
f2af0086 | 843 | if (callchain_param.mode == CHAIN_FOLDED) { |
5ab250ca | 844 | period = node->hit; |
f2af0086 NK |
845 | count = node->count; |
846 | } | |
5ab250ca | 847 | |
f2af0086 NK |
848 | switch (callchain_param.value) { |
849 | case CCVAL_PERIOD: | |
850 | scnprintf(bf, bfsize, "%"PRIu64, period); | |
851 | break; | |
852 | case CCVAL_COUNT: | |
853 | scnprintf(bf, bfsize, "%u", count); | |
854 | break; | |
855 | case CCVAL_PERCENT: | |
856 | default: | |
857 | if (total) | |
858 | percent = period * 100.0 / total; | |
859 | scnprintf(bf, bfsize, "%.2f%%", percent); | |
860 | break; | |
861 | } | |
5ab250ca NK |
862 | return bf; |
863 | } | |
864 | ||
865 | int callchain_node__fprintf_value(struct callchain_node *node, | |
866 | FILE *fp, u64 total) | |
867 | { | |
868 | double percent = 0.0; | |
869 | u64 period = callchain_cumul_hits(node); | |
f2af0086 | 870 | unsigned count = callchain_cumul_counts(node); |
5ab250ca | 871 | |
f2af0086 | 872 | if (callchain_param.mode == CHAIN_FOLDED) { |
5ab250ca | 873 | period = node->hit; |
f2af0086 NK |
874 | count = node->count; |
875 | } | |
5ab250ca | 876 | |
f2af0086 NK |
877 | switch (callchain_param.value) { |
878 | case CCVAL_PERIOD: | |
879 | return fprintf(fp, "%"PRIu64, period); | |
880 | case CCVAL_COUNT: | |
881 | return fprintf(fp, "%u", count); | |
882 | case CCVAL_PERCENT: | |
883 | default: | |
884 | if (total) | |
885 | percent = period * 100.0 / total; | |
886 | return percent_color_fprintf(fp, "%.2f%%", percent); | |
887 | } | |
888 | return 0; | |
5ab250ca NK |
889 | } |
890 | ||
d114960c NK |
891 | static void free_callchain_node(struct callchain_node *node) |
892 | { | |
893 | struct callchain_list *list, *tmp; | |
894 | struct callchain_node *child; | |
895 | struct rb_node *n; | |
896 | ||
897 | list_for_each_entry_safe(list, tmp, &node->val, list) { | |
898 | list_del(&list->list); | |
899 | free(list); | |
900 | } | |
901 | ||
902 | n = rb_first(&node->rb_root_in); | |
903 | while (n) { | |
904 | child = container_of(n, struct callchain_node, rb_node_in); | |
905 | n = rb_next(n); | |
906 | rb_erase(&child->rb_node_in, &node->rb_root_in); | |
907 | ||
908 | free_callchain_node(child); | |
909 | free(child); | |
910 | } | |
911 | } | |
912 | ||
913 | void free_callchain(struct callchain_root *root) | |
914 | { | |
915 | if (!symbol_conf.use_callchain) | |
916 | return; | |
917 | ||
918 | free_callchain_node(&root->node); | |
919 | } |