Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 1 | /* |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 2 | * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com> |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 3 | * |
| 4 | * Handle the callchains from the stream in an ad-hoc radix tree and then |
| 5 | * sort them in an rbtree. |
| 6 | * |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 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 | * |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <errno.h> |
Frederic Weisbecker | c0a8865 | 2009-08-09 04:19:15 +0200 | [diff] [blame] | 16 | #include <math.h> |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 17 | |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 18 | #include "asm/bug.h" |
| 19 | |
Andi Kleen | 99571ab | 2013-07-18 15:33:57 -0700 | [diff] [blame] | 20 | #include "hist.h" |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 21 | #include "util.h" |
Namhyung Kim | 2dc9fb1 | 2014-01-14 14:25:35 +0900 | [diff] [blame] | 22 | #include "sort.h" |
| 23 | #include "machine.h" |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 24 | #include "callchain.h" |
| 25 | |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 26 | __thread struct callchain_cursor callchain_cursor; |
| 27 | |
Kan Liang | c3a6a8c | 2015-08-04 04:30:20 -0400 | [diff] [blame] | 28 | int parse_callchain_record_opt(const char *arg, struct callchain_param *param) |
Namhyung Kim | f7f084f | 2014-09-23 10:01:42 +0900 | [diff] [blame] | 29 | { |
Kan Liang | 076a30c | 2015-08-06 15:44:52 -0400 | [diff] [blame] | 30 | return parse_callchain_record(arg, param); |
Namhyung Kim | f7f084f | 2014-09-23 10:01:42 +0900 | [diff] [blame] | 31 | } |
| 32 | |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 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 | } |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | static int parse_callchain_order(const char *value) |
| 51 | { |
| 52 | if (!strncmp(value, "caller", strlen(value))) { |
| 53 | callchain_param.order = ORDER_CALLER; |
Namhyung Kim | 792aeaf | 2015-10-22 16:45:46 +0900 | [diff] [blame] | 54 | callchain_param.order_set = true; |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | if (!strncmp(value, "callee", strlen(value))) { |
| 58 | callchain_param.order = ORDER_CALLEE; |
Namhyung Kim | 792aeaf | 2015-10-22 16:45:46 +0900 | [diff] [blame] | 59 | callchain_param.order_set = true; |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | static int parse_callchain_sort_key(const char *value) |
| 66 | { |
| 67 | if (!strncmp(value, "function", strlen(value))) { |
| 68 | callchain_param.key = CCKEY_FUNCTION; |
| 69 | return 0; |
| 70 | } |
| 71 | if (!strncmp(value, "address", strlen(value))) { |
| 72 | callchain_param.key = CCKEY_ADDRESS; |
| 73 | return 0; |
| 74 | } |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 75 | if (!strncmp(value, "branch", strlen(value))) { |
| 76 | callchain_param.branch_callstack = 1; |
| 77 | return 0; |
| 78 | } |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 79 | return -1; |
| 80 | } |
| 81 | |
Namhyung Kim | a2c10d3 | 2015-10-22 15:28:49 +0900 | [diff] [blame] | 82 | static int |
| 83 | __parse_callchain_report_opt(const char *arg, bool allow_record_opt) |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 84 | { |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame] | 85 | char *tok; |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 86 | char *endptr; |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame] | 87 | bool minpcnt_set = false; |
Namhyung Kim | a2c10d3 | 2015-10-22 15:28:49 +0900 | [diff] [blame] | 88 | bool record_opt_set = false; |
| 89 | bool try_stack_size = false; |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 90 | |
| 91 | symbol_conf.use_callchain = true; |
| 92 | |
| 93 | if (!arg) |
| 94 | return 0; |
| 95 | |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame] | 96 | while ((tok = strtok((char *)arg, ",")) != NULL) { |
| 97 | if (!strncmp(tok, "none", strlen(tok))) { |
| 98 | callchain_param.mode = CHAIN_NONE; |
| 99 | symbol_conf.use_callchain = false; |
| 100 | return 0; |
| 101 | } |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 102 | |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 103 | if (!parse_callchain_mode(tok) || |
| 104 | !parse_callchain_order(tok) || |
| 105 | !parse_callchain_sort_key(tok)) { |
| 106 | /* parsing ok - move on to the next */ |
Namhyung Kim | a2c10d3 | 2015-10-22 15:28:49 +0900 | [diff] [blame] | 107 | try_stack_size = false; |
| 108 | goto next; |
| 109 | } else if (allow_record_opt && !record_opt_set) { |
| 110 | if (parse_callchain_record(tok, &callchain_param)) |
| 111 | goto try_numbers; |
| 112 | |
| 113 | /* assume that number followed by 'dwarf' is stack size */ |
| 114 | if (callchain_param.record_mode == CALLCHAIN_DWARF) |
| 115 | try_stack_size = true; |
| 116 | |
| 117 | record_opt_set = true; |
| 118 | goto next; |
| 119 | } |
| 120 | |
| 121 | try_numbers: |
| 122 | if (try_stack_size) { |
| 123 | unsigned long size = 0; |
| 124 | |
| 125 | if (get_stack_size(tok, &size) < 0) |
| 126 | return -1; |
| 127 | callchain_param.dump_size = size; |
| 128 | try_stack_size = false; |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 129 | } else if (!minpcnt_set) { |
| 130 | /* try to get the min percent */ |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame] | 131 | callchain_param.min_percent = strtod(tok, &endptr); |
| 132 | if (tok == endptr) |
| 133 | return -1; |
| 134 | minpcnt_set = true; |
| 135 | } else { |
| 136 | /* try print limit at last */ |
| 137 | callchain_param.print_limit = strtoul(tok, &endptr, 0); |
| 138 | if (tok == endptr) |
| 139 | return -1; |
| 140 | } |
Namhyung Kim | a2c10d3 | 2015-10-22 15:28:49 +0900 | [diff] [blame] | 141 | next: |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame] | 142 | arg = NULL; |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 145 | if (callchain_register_param(&callchain_param) < 0) { |
| 146 | pr_err("Can't register callchain params\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
Namhyung Kim | a2c10d3 | 2015-10-22 15:28:49 +0900 | [diff] [blame] | 152 | int parse_callchain_report_opt(const char *arg) |
| 153 | { |
| 154 | return __parse_callchain_report_opt(arg, false); |
| 155 | } |
| 156 | |
| 157 | int parse_callchain_top_opt(const char *arg) |
| 158 | { |
| 159 | return __parse_callchain_report_opt(arg, true); |
| 160 | } |
| 161 | |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 162 | int perf_callchain_config(const char *var, const char *value) |
| 163 | { |
| 164 | char *endptr; |
| 165 | |
| 166 | if (prefixcmp(var, "call-graph.")) |
| 167 | return 0; |
| 168 | var += sizeof("call-graph.") - 1; |
| 169 | |
| 170 | if (!strcmp(var, "record-mode")) |
Kan Liang | c3a6a8c | 2015-08-04 04:30:20 -0400 | [diff] [blame] | 171 | return parse_callchain_record_opt(value, &callchain_param); |
Namhyung Kim | 2b9240c | 2014-09-23 10:01:43 +0900 | [diff] [blame] | 172 | #ifdef HAVE_DWARF_UNWIND_SUPPORT |
| 173 | if (!strcmp(var, "dump-size")) { |
| 174 | unsigned long size = 0; |
| 175 | int ret; |
| 176 | |
| 177 | ret = get_stack_size(value, &size); |
| 178 | callchain_param.dump_size = size; |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | #endif |
| 183 | if (!strcmp(var, "print-type")) |
| 184 | return parse_callchain_mode(value); |
| 185 | if (!strcmp(var, "order")) |
| 186 | return parse_callchain_order(value); |
| 187 | if (!strcmp(var, "sort-key")) |
| 188 | return parse_callchain_sort_key(value); |
| 189 | if (!strcmp(var, "threshold")) { |
| 190 | callchain_param.min_percent = strtod(value, &endptr); |
| 191 | if (value == endptr) |
| 192 | return -1; |
| 193 | } |
| 194 | if (!strcmp(var, "print-limit")) { |
| 195 | callchain_param.print_limit = strtod(value, &endptr); |
| 196 | if (value == endptr) |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 203 | static void |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 204 | rb_insert_callchain(struct rb_root *root, struct callchain_node *chain, |
| 205 | enum chain_mode mode) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 206 | { |
| 207 | struct rb_node **p = &root->rb_node; |
| 208 | struct rb_node *parent = NULL; |
| 209 | struct callchain_node *rnode; |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 210 | u64 chain_cumul = callchain_cumul_hits(chain); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 211 | |
| 212 | while (*p) { |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 213 | u64 rnode_cumul; |
| 214 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 215 | parent = *p; |
| 216 | rnode = rb_entry(parent, struct callchain_node, rb_node); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 217 | rnode_cumul = callchain_cumul_hits(rnode); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 218 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 219 | switch (mode) { |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 220 | case CHAIN_FLAT: |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 221 | if (rnode->hit < chain->hit) |
| 222 | p = &(*p)->rb_left; |
| 223 | else |
| 224 | p = &(*p)->rb_right; |
| 225 | break; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 226 | case CHAIN_GRAPH_ABS: /* Falldown */ |
| 227 | case CHAIN_GRAPH_REL: |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 228 | if (rnode_cumul < chain_cumul) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 229 | p = &(*p)->rb_left; |
| 230 | else |
| 231 | p = &(*p)->rb_right; |
| 232 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 233 | case CHAIN_NONE: |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 234 | default: |
| 235 | break; |
| 236 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | rb_link_node(&chain->rb_node, parent, p); |
| 240 | rb_insert_color(&chain->rb_node, root); |
| 241 | } |
| 242 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 243 | static void |
| 244 | __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node, |
| 245 | u64 min_hit) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 246 | { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 247 | struct rb_node *n; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 248 | struct callchain_node *child; |
| 249 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 250 | n = rb_first(&node->rb_root_in); |
| 251 | while (n) { |
| 252 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 253 | n = rb_next(n); |
| 254 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 255 | __sort_chain_flat(rb_root, child, min_hit); |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 256 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 257 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 258 | if (node->hit && node->hit >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 259 | rb_insert_callchain(rb_root, node, CHAIN_FLAT); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 262 | /* |
| 263 | * Once we get every callchains from the stream, we can now |
| 264 | * sort them by hit |
| 265 | */ |
| 266 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 267 | sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 268 | u64 min_hit, struct callchain_param *param __maybe_unused) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 269 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 270 | __sort_chain_flat(rb_root, &root->node, min_hit); |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void __sort_chain_graph_abs(struct callchain_node *node, |
| 274 | u64 min_hit) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 275 | { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 276 | struct rb_node *n; |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 277 | struct callchain_node *child; |
| 278 | |
| 279 | node->rb_root = RB_ROOT; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 280 | n = rb_first(&node->rb_root_in); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 281 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 282 | while (n) { |
| 283 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 284 | n = rb_next(n); |
| 285 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 286 | __sort_chain_graph_abs(child, min_hit); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 287 | if (callchain_cumul_hits(child) >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 288 | rb_insert_callchain(&node->rb_root, child, |
| 289 | CHAIN_GRAPH_ABS); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 293 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 294 | sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 295 | u64 min_hit, struct callchain_param *param __maybe_unused) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 296 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 297 | __sort_chain_graph_abs(&chain_root->node, min_hit); |
| 298 | rb_root->rb_node = chain_root->node.rb_root.rb_node; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 301 | static void __sort_chain_graph_rel(struct callchain_node *node, |
| 302 | double min_percent) |
| 303 | { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 304 | struct rb_node *n; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 305 | struct callchain_node *child; |
| 306 | u64 min_hit; |
| 307 | |
| 308 | node->rb_root = RB_ROOT; |
Frederic Weisbecker | c0a8865 | 2009-08-09 04:19:15 +0200 | [diff] [blame] | 309 | min_hit = ceil(node->children_hit * min_percent); |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 310 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 311 | n = rb_first(&node->rb_root_in); |
| 312 | while (n) { |
| 313 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 314 | n = rb_next(n); |
| 315 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 316 | __sort_chain_graph_rel(child, min_percent); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 317 | if (callchain_cumul_hits(child) >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 318 | rb_insert_callchain(&node->rb_root, child, |
| 319 | CHAIN_GRAPH_REL); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 324 | sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 325 | u64 min_hit __maybe_unused, struct callchain_param *param) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 326 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 327 | __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0); |
| 328 | rb_root->rb_node = chain_root->node.rb_root.rb_node; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Frederic Weisbecker | 16537f1 | 2011-01-14 04:52:00 +0100 | [diff] [blame] | 331 | int callchain_register_param(struct callchain_param *param) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 332 | { |
| 333 | switch (param->mode) { |
| 334 | case CHAIN_GRAPH_ABS: |
| 335 | param->sort = sort_chain_graph_abs; |
| 336 | break; |
| 337 | case CHAIN_GRAPH_REL: |
| 338 | param->sort = sort_chain_graph_rel; |
| 339 | break; |
| 340 | case CHAIN_FLAT: |
| 341 | param->sort = sort_chain_flat; |
| 342 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 343 | case CHAIN_NONE: |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 344 | default: |
| 345 | return -1; |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 350 | /* |
| 351 | * Create a child for a parent. If inherit_children, then the new child |
| 352 | * will become the new parent of it's parent children |
| 353 | */ |
| 354 | static struct callchain_node * |
| 355 | create_child(struct callchain_node *parent, bool inherit_children) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 356 | { |
| 357 | struct callchain_node *new; |
| 358 | |
Arnaldo Carvalho de Melo | cdd5b75b | 2010-05-10 10:56:50 -0300 | [diff] [blame] | 359 | new = zalloc(sizeof(*new)); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 360 | if (!new) { |
| 361 | perror("not enough memory to create child for code path tree"); |
| 362 | return NULL; |
| 363 | } |
| 364 | new->parent = parent; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 365 | INIT_LIST_HEAD(&new->val); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 366 | |
| 367 | if (inherit_children) { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 368 | struct rb_node *n; |
| 369 | struct callchain_node *child; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 370 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 371 | new->rb_root_in = parent->rb_root_in; |
| 372 | parent->rb_root_in = RB_ROOT; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 373 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 374 | n = rb_first(&new->rb_root_in); |
| 375 | while (n) { |
| 376 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 377 | child->parent = new; |
| 378 | n = rb_next(n); |
| 379 | } |
| 380 | |
| 381 | /* make it the first child */ |
| 382 | rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node); |
| 383 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 384 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 385 | |
| 386 | return new; |
| 387 | } |
| 388 | |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 389 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 390 | /* |
| 391 | * Fill the node with callchain values |
| 392 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 393 | static void |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 394 | fill_node(struct callchain_node *node, struct callchain_cursor *cursor) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 395 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 396 | struct callchain_cursor_node *cursor_node; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 397 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 398 | node->val_nr = cursor->nr - cursor->pos; |
| 399 | if (!node->val_nr) |
| 400 | pr_warning("Warning: empty node in callchain tree\n"); |
| 401 | |
| 402 | cursor_node = callchain_cursor_current(cursor); |
| 403 | |
| 404 | while (cursor_node) { |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 405 | struct callchain_list *call; |
| 406 | |
Arnaldo Carvalho de Melo | cdd5b75b | 2010-05-10 10:56:50 -0300 | [diff] [blame] | 407 | call = zalloc(sizeof(*call)); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 408 | if (!call) { |
| 409 | perror("not enough memory for the code path tree"); |
| 410 | return; |
| 411 | } |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 412 | call->ip = cursor_node->ip; |
| 413 | call->ms.sym = cursor_node->sym; |
| 414 | call->ms.map = cursor_node->map; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 415 | list_add_tail(&call->list, &node->val); |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 416 | |
| 417 | callchain_cursor_advance(cursor); |
| 418 | cursor_node = callchain_cursor_current(cursor); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 419 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 422 | static struct callchain_node * |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 423 | add_child(struct callchain_node *parent, |
| 424 | struct callchain_cursor *cursor, |
| 425 | u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 426 | { |
| 427 | struct callchain_node *new; |
| 428 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 429 | new = create_child(parent, false); |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 430 | fill_node(new, cursor); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 431 | |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 432 | new->children_hit = 0; |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 433 | new->hit = period; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 434 | return new; |
| 435 | } |
| 436 | |
| 437 | static s64 match_chain(struct callchain_cursor_node *node, |
| 438 | struct callchain_list *cnode) |
| 439 | { |
| 440 | struct symbol *sym = node->sym; |
| 441 | |
| 442 | if (cnode->ms.sym && sym && |
| 443 | callchain_param.key == CCKEY_FUNCTION) |
| 444 | return cnode->ms.sym->start - sym->start; |
| 445 | else |
| 446 | return cnode->ip - node->ip; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 449 | /* |
| 450 | * Split the parent in two parts (a new child is created) and |
| 451 | * give a part of its callchain to the created child. |
| 452 | * Then create another child to host the given callchain of new branch |
| 453 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 454 | static void |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 455 | split_add_child(struct callchain_node *parent, |
| 456 | struct callchain_cursor *cursor, |
| 457 | struct callchain_list *to_split, |
| 458 | u64 idx_parents, u64 idx_local, u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 459 | { |
| 460 | struct callchain_node *new; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 461 | struct list_head *old_tail; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 462 | unsigned int idx_total = idx_parents + idx_local; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 463 | |
| 464 | /* split */ |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 465 | new = create_child(parent, true); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 466 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 467 | /* split the callchain and move a part to the new child */ |
| 468 | old_tail = parent->val.prev; |
| 469 | list_del_range(&to_split->list, old_tail); |
| 470 | new->val.next = &to_split->list; |
| 471 | new->val.prev = old_tail; |
| 472 | to_split->list.prev = &new->val; |
| 473 | old_tail->next = &new->val; |
| 474 | |
| 475 | /* split the hits */ |
| 476 | new->hit = parent->hit; |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 477 | new->children_hit = parent->children_hit; |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 478 | parent->children_hit = callchain_cumul_hits(new); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 479 | new->val_nr = parent->val_nr - idx_local; |
| 480 | parent->val_nr = idx_local; |
| 481 | |
| 482 | /* create a new child for the new branch if any */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 483 | if (idx_total < cursor->nr) { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 484 | struct callchain_node *first; |
| 485 | struct callchain_list *cnode; |
| 486 | struct callchain_cursor_node *node; |
| 487 | struct rb_node *p, **pp; |
| 488 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 489 | parent->hit = 0; |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 490 | parent->children_hit += period; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 491 | |
| 492 | node = callchain_cursor_current(cursor); |
| 493 | new = add_child(parent, cursor, period); |
| 494 | |
| 495 | /* |
| 496 | * This is second child since we moved parent's children |
| 497 | * to new (first) child above. |
| 498 | */ |
| 499 | p = parent->rb_root_in.rb_node; |
| 500 | first = rb_entry(p, struct callchain_node, rb_node_in); |
| 501 | cnode = list_first_entry(&first->val, struct callchain_list, |
| 502 | list); |
| 503 | |
| 504 | if (match_chain(node, cnode) < 0) |
| 505 | pp = &p->rb_left; |
| 506 | else |
| 507 | pp = &p->rb_right; |
| 508 | |
| 509 | rb_link_node(&new->rb_node_in, p, pp); |
| 510 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 511 | } else { |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 512 | parent->hit = period; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 513 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static int |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 517 | append_chain(struct callchain_node *root, |
| 518 | struct callchain_cursor *cursor, |
| 519 | u64 period); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 520 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 521 | static void |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 522 | append_chain_children(struct callchain_node *root, |
| 523 | struct callchain_cursor *cursor, |
| 524 | u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 525 | { |
| 526 | struct callchain_node *rnode; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 527 | struct callchain_cursor_node *node; |
| 528 | struct rb_node **p = &root->rb_root_in.rb_node; |
| 529 | struct rb_node *parent = NULL; |
| 530 | |
| 531 | node = callchain_cursor_current(cursor); |
| 532 | if (!node) |
| 533 | return; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 534 | |
| 535 | /* lookup in childrens */ |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 536 | while (*p) { |
| 537 | s64 ret; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 538 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 539 | parent = *p; |
| 540 | rnode = rb_entry(parent, struct callchain_node, rb_node_in); |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 541 | |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 542 | /* If at least first entry matches, rely to children */ |
| 543 | ret = append_chain(rnode, cursor, period); |
| 544 | if (ret == 0) |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 545 | goto inc_children_hit; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 546 | |
| 547 | if (ret < 0) |
| 548 | p = &parent->rb_left; |
| 549 | else |
| 550 | p = &parent->rb_right; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 551 | } |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 552 | /* nothing in children, add to the current node */ |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 553 | rnode = add_child(root, cursor, period); |
| 554 | rb_link_node(&rnode->rb_node_in, parent, p); |
| 555 | rb_insert_color(&rnode->rb_node_in, &root->rb_root_in); |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame] | 556 | |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 557 | inc_children_hit: |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 558 | root->children_hit += period; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static int |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 562 | append_chain(struct callchain_node *root, |
| 563 | struct callchain_cursor *cursor, |
| 564 | u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 565 | { |
| 566 | struct callchain_list *cnode; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 567 | u64 start = cursor->pos; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 568 | bool found = false; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 569 | u64 matches; |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 570 | int cmp = 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 571 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 572 | /* |
| 573 | * Lookup in the current node |
| 574 | * If we have a symbol, then compare the start to match |
Andi Kleen | 99571ab | 2013-07-18 15:33:57 -0700 | [diff] [blame] | 575 | * anywhere inside a function, unless function |
| 576 | * mode is disabled. |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 577 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 578 | list_for_each_entry(cnode, &root->val, list) { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 579 | struct callchain_cursor_node *node; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 580 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 581 | node = callchain_cursor_current(cursor); |
| 582 | if (!node) |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 583 | break; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 584 | |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 585 | cmp = match_chain(node, cnode); |
| 586 | if (cmp) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 587 | break; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 588 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 589 | found = true; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 590 | |
| 591 | callchain_cursor_advance(cursor); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 594 | /* matches not, relay no the parent */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 595 | if (!found) { |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 596 | WARN_ONCE(!cmp, "Chain comparison error\n"); |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 597 | return cmp; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | matches = cursor->pos - start; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 601 | |
| 602 | /* we match only a part of the node. Split it and add the new chain */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 603 | if (matches < root->val_nr) { |
| 604 | split_add_child(root, cursor, cnode, start, matches, period); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* we match 100% of the path, increment the hit */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 609 | if (matches == root->val_nr && cursor->pos == cursor->nr) { |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 610 | root->hit += period; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 614 | /* We match the node and still have a part remaining */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 615 | append_chain_children(root, cursor, period); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 616 | |
| 617 | return 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 620 | int callchain_append(struct callchain_root *root, |
| 621 | struct callchain_cursor *cursor, |
| 622 | u64 period) |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 623 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 624 | if (!cursor->nr) |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 625 | return 0; |
| 626 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 627 | callchain_cursor_commit(cursor); |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 628 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 629 | append_chain_children(&root->node, cursor, period); |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 630 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 631 | if (cursor->nr > root->max_depth) |
| 632 | root->max_depth = cursor->nr; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 633 | |
| 634 | return 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 635 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 636 | |
| 637 | static int |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 638 | merge_chain_branch(struct callchain_cursor *cursor, |
| 639 | struct callchain_node *dst, struct callchain_node *src) |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 640 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 641 | struct callchain_cursor_node **old_last = cursor->last; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 642 | struct callchain_node *child; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 643 | struct callchain_list *list, *next_list; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 644 | struct rb_node *n; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 645 | int old_pos = cursor->nr; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 646 | int err = 0; |
| 647 | |
| 648 | list_for_each_entry_safe(list, next_list, &src->val, list) { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 649 | callchain_cursor_append(cursor, list->ip, |
| 650 | list->ms.map, list->ms.sym); |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 651 | list_del(&list->list); |
| 652 | free(list); |
| 653 | } |
| 654 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 655 | if (src->hit) { |
| 656 | callchain_cursor_commit(cursor); |
| 657 | append_chain_children(dst, cursor, src->hit); |
| 658 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 659 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 660 | n = rb_first(&src->rb_root_in); |
| 661 | while (n) { |
| 662 | child = container_of(n, struct callchain_node, rb_node_in); |
| 663 | n = rb_next(n); |
| 664 | rb_erase(&child->rb_node_in, &src->rb_root_in); |
| 665 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 666 | err = merge_chain_branch(cursor, dst, child); |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 667 | if (err) |
| 668 | break; |
| 669 | |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 670 | free(child); |
| 671 | } |
| 672 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 673 | cursor->nr = old_pos; |
| 674 | cursor->last = old_last; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 675 | |
| 676 | return err; |
| 677 | } |
| 678 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 679 | int callchain_merge(struct callchain_cursor *cursor, |
| 680 | struct callchain_root *dst, struct callchain_root *src) |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 681 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 682 | return merge_chain_branch(cursor, &dst->node, &src->node); |
| 683 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 684 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 685 | int callchain_cursor_append(struct callchain_cursor *cursor, |
| 686 | u64 ip, struct map *map, struct symbol *sym) |
| 687 | { |
| 688 | struct callchain_cursor_node *node = *cursor->last; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 689 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 690 | if (!node) { |
Paul Gortmaker | 91b9880 | 2013-01-30 20:05:49 -0500 | [diff] [blame] | 691 | node = calloc(1, sizeof(*node)); |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 692 | if (!node) |
| 693 | return -ENOMEM; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 694 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 695 | *cursor->last = node; |
| 696 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 697 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 698 | node->ip = ip; |
| 699 | node->map = map; |
| 700 | node->sym = sym; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 701 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 702 | cursor->nr++; |
| 703 | |
| 704 | cursor->last = &node->next; |
| 705 | |
| 706 | return 0; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 707 | } |
Namhyung Kim | 2dc9fb1 | 2014-01-14 14:25:35 +0900 | [diff] [blame] | 708 | |
| 709 | int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent, |
| 710 | struct perf_evsel *evsel, struct addr_location *al, |
| 711 | int max_stack) |
| 712 | { |
| 713 | if (sample->callchain == NULL) |
| 714 | return 0; |
| 715 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 716 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain || |
| 717 | sort__has_parent) { |
Arnaldo Carvalho de Melo | cc8b7c2 | 2014-10-23 15:26:17 -0300 | [diff] [blame] | 718 | return thread__resolve_callchain(al->thread, evsel, sample, |
| 719 | parent, al, max_stack); |
Namhyung Kim | 2dc9fb1 | 2014-01-14 14:25:35 +0900 | [diff] [blame] | 720 | } |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample) |
| 725 | { |
Adrian Hunter | 4d40b05 | 2014-07-14 13:02:35 +0300 | [diff] [blame] | 726 | if (!symbol_conf.use_callchain || sample->callchain == NULL) |
Namhyung Kim | 2dc9fb1 | 2014-01-14 14:25:35 +0900 | [diff] [blame] | 727 | return 0; |
| 728 | return callchain_append(he->callchain, &callchain_cursor, sample->period); |
| 729 | } |
Namhyung Kim | c7405d8 | 2013-10-31 13:58:30 +0900 | [diff] [blame] | 730 | |
| 731 | int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node, |
| 732 | bool hide_unresolved) |
| 733 | { |
| 734 | al->map = node->map; |
| 735 | al->sym = node->sym; |
| 736 | if (node->map) |
| 737 | al->addr = node->map->map_ip(node->map, node->ip); |
| 738 | else |
| 739 | al->addr = node->ip; |
| 740 | |
| 741 | if (al->sym == NULL) { |
| 742 | if (hide_unresolved) |
| 743 | return 0; |
| 744 | if (al->map == NULL) |
| 745 | goto out; |
| 746 | } |
| 747 | |
| 748 | if (al->map->groups == &al->machine->kmaps) { |
| 749 | if (machine__is_host(al->machine)) { |
| 750 | al->cpumode = PERF_RECORD_MISC_KERNEL; |
| 751 | al->level = 'k'; |
| 752 | } else { |
| 753 | al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL; |
| 754 | al->level = 'g'; |
| 755 | } |
| 756 | } else { |
| 757 | if (machine__is_host(al->machine)) { |
| 758 | al->cpumode = PERF_RECORD_MISC_USER; |
| 759 | al->level = '.'; |
| 760 | } else if (perf_guest) { |
| 761 | al->cpumode = PERF_RECORD_MISC_GUEST_USER; |
| 762 | al->level = 'u'; |
| 763 | } else { |
| 764 | al->cpumode = PERF_RECORD_MISC_HYPERVISOR; |
| 765 | al->level = 'H'; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | out: |
| 770 | return 1; |
| 771 | } |
Andi Kleen | 2989cca | 2014-11-12 18:05:23 -0800 | [diff] [blame] | 772 | |
| 773 | char *callchain_list__sym_name(struct callchain_list *cl, |
| 774 | char *bf, size_t bfsize, bool show_dso) |
| 775 | { |
| 776 | int printed; |
| 777 | |
| 778 | if (cl->ms.sym) { |
Andi Kleen | 23f0981 | 2014-11-12 18:05:24 -0800 | [diff] [blame] | 779 | if (callchain_param.key == CCKEY_ADDRESS && |
| 780 | cl->ms.map && !cl->srcline) |
| 781 | cl->srcline = get_srcline(cl->ms.map->dso, |
| 782 | map__rip_2objdump(cl->ms.map, |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 783 | cl->ip), |
| 784 | cl->ms.sym, false); |
Andi Kleen | 23f0981 | 2014-11-12 18:05:24 -0800 | [diff] [blame] | 785 | if (cl->srcline) |
| 786 | printed = scnprintf(bf, bfsize, "%s %s", |
| 787 | cl->ms.sym->name, cl->srcline); |
| 788 | else |
| 789 | printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name); |
Andi Kleen | 2989cca | 2014-11-12 18:05:23 -0800 | [diff] [blame] | 790 | } else |
| 791 | printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip); |
| 792 | |
| 793 | if (show_dso) |
| 794 | scnprintf(bf + printed, bfsize - printed, " %s", |
| 795 | cl->ms.map ? |
| 796 | cl->ms.map->dso->short_name : |
| 797 | "unknown"); |
| 798 | |
| 799 | return bf; |
| 800 | } |
Namhyung Kim | d114960 | 2014-12-30 14:38:13 +0900 | [diff] [blame] | 801 | |
| 802 | static void free_callchain_node(struct callchain_node *node) |
| 803 | { |
| 804 | struct callchain_list *list, *tmp; |
| 805 | struct callchain_node *child; |
| 806 | struct rb_node *n; |
| 807 | |
| 808 | list_for_each_entry_safe(list, tmp, &node->val, list) { |
| 809 | list_del(&list->list); |
| 810 | free(list); |
| 811 | } |
| 812 | |
| 813 | n = rb_first(&node->rb_root_in); |
| 814 | while (n) { |
| 815 | child = container_of(n, struct callchain_node, rb_node_in); |
| 816 | n = rb_next(n); |
| 817 | rb_erase(&child->rb_node_in, &node->rb_root_in); |
| 818 | |
| 819 | free_callchain_node(child); |
| 820 | free(child); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | void free_callchain(struct callchain_root *root) |
| 825 | { |
| 826 | if (!symbol_conf.use_callchain) |
| 827 | return; |
| 828 | |
| 829 | free_callchain_node(&root->node); |
| 830 | } |