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