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 | |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 28 | int |
| 29 | parse_callchain_report_opt(const char *arg) |
| 30 | { |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame^] | 31 | char *tok; |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 32 | char *endptr; |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame^] | 33 | bool minpcnt_set = false; |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 34 | |
| 35 | symbol_conf.use_callchain = true; |
| 36 | |
| 37 | if (!arg) |
| 38 | return 0; |
| 39 | |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame^] | 40 | while ((tok = strtok((char *)arg, ",")) != NULL) { |
| 41 | if (!strncmp(tok, "none", strlen(tok))) { |
| 42 | callchain_param.mode = CHAIN_NONE; |
| 43 | symbol_conf.use_callchain = false; |
| 44 | return 0; |
| 45 | } |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 46 | |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame^] | 47 | /* try to get the output mode */ |
| 48 | if (!strncmp(tok, "graph", strlen(tok))) |
| 49 | callchain_param.mode = CHAIN_GRAPH_ABS; |
| 50 | else if (!strncmp(tok, "flat", strlen(tok))) |
| 51 | callchain_param.mode = CHAIN_FLAT; |
| 52 | else if (!strncmp(tok, "fractal", strlen(tok))) |
| 53 | callchain_param.mode = CHAIN_GRAPH_REL; |
| 54 | /* try to get the call chain order */ |
| 55 | else if (!strncmp(tok, "caller", strlen(tok))) |
| 56 | callchain_param.order = ORDER_CALLER; |
| 57 | else if (!strncmp(tok, "callee", strlen(tok))) |
| 58 | callchain_param.order = ORDER_CALLEE; |
| 59 | /* try to get the sort key */ |
| 60 | else if (!strncmp(tok, "function", strlen(tok))) |
| 61 | callchain_param.key = CCKEY_FUNCTION; |
| 62 | else if (!strncmp(tok, "address", strlen(tok))) |
| 63 | callchain_param.key = CCKEY_ADDRESS; |
| 64 | /* try to get the min percent */ |
| 65 | else if (!minpcnt_set) { |
| 66 | callchain_param.min_percent = strtod(tok, &endptr); |
| 67 | if (tok == endptr) |
| 68 | return -1; |
| 69 | minpcnt_set = true; |
| 70 | } else { |
| 71 | /* try print limit at last */ |
| 72 | callchain_param.print_limit = strtoul(tok, &endptr, 0); |
| 73 | if (tok == endptr) |
| 74 | return -1; |
| 75 | } |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 76 | |
Namhyung Kim | e8232f1 | 2014-08-14 15:01:38 +0900 | [diff] [blame^] | 77 | arg = NULL; |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Don Zickus | cff6bb4 | 2014-04-07 14:55:24 -0400 | [diff] [blame] | 80 | if (callchain_register_param(&callchain_param) < 0) { |
| 81 | pr_err("Can't register callchain params\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 87 | static void |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 88 | rb_insert_callchain(struct rb_root *root, struct callchain_node *chain, |
| 89 | enum chain_mode mode) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 90 | { |
| 91 | struct rb_node **p = &root->rb_node; |
| 92 | struct rb_node *parent = NULL; |
| 93 | struct callchain_node *rnode; |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 94 | u64 chain_cumul = callchain_cumul_hits(chain); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 95 | |
| 96 | while (*p) { |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 97 | u64 rnode_cumul; |
| 98 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 99 | parent = *p; |
| 100 | rnode = rb_entry(parent, struct callchain_node, rb_node); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 101 | rnode_cumul = callchain_cumul_hits(rnode); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 102 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 103 | switch (mode) { |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 104 | case CHAIN_FLAT: |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 105 | if (rnode->hit < chain->hit) |
| 106 | p = &(*p)->rb_left; |
| 107 | else |
| 108 | p = &(*p)->rb_right; |
| 109 | break; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 110 | case CHAIN_GRAPH_ABS: /* Falldown */ |
| 111 | case CHAIN_GRAPH_REL: |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 112 | if (rnode_cumul < chain_cumul) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 113 | p = &(*p)->rb_left; |
| 114 | else |
| 115 | p = &(*p)->rb_right; |
| 116 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 117 | case CHAIN_NONE: |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 118 | default: |
| 119 | break; |
| 120 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | rb_link_node(&chain->rb_node, parent, p); |
| 124 | rb_insert_color(&chain->rb_node, root); |
| 125 | } |
| 126 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 127 | static void |
| 128 | __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node, |
| 129 | u64 min_hit) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 130 | { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 131 | struct rb_node *n; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 132 | struct callchain_node *child; |
| 133 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 134 | n = rb_first(&node->rb_root_in); |
| 135 | while (n) { |
| 136 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 137 | n = rb_next(n); |
| 138 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 139 | __sort_chain_flat(rb_root, child, min_hit); |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 140 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 141 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 142 | if (node->hit && node->hit >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 143 | rb_insert_callchain(rb_root, node, CHAIN_FLAT); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 146 | /* |
| 147 | * Once we get every callchains from the stream, we can now |
| 148 | * sort them by hit |
| 149 | */ |
| 150 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 151 | sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 152 | u64 min_hit, struct callchain_param *param __maybe_unused) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 153 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 154 | __sort_chain_flat(rb_root, &root->node, min_hit); |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void __sort_chain_graph_abs(struct callchain_node *node, |
| 158 | u64 min_hit) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 159 | { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 160 | struct rb_node *n; |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 161 | struct callchain_node *child; |
| 162 | |
| 163 | node->rb_root = RB_ROOT; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 164 | n = rb_first(&node->rb_root_in); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 165 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 166 | while (n) { |
| 167 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 168 | n = rb_next(n); |
| 169 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 170 | __sort_chain_graph_abs(child, min_hit); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 171 | if (callchain_cumul_hits(child) >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 172 | rb_insert_callchain(&node->rb_root, child, |
| 173 | CHAIN_GRAPH_ABS); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 177 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 178 | 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] | 179 | u64 min_hit, struct callchain_param *param __maybe_unused) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 180 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 181 | __sort_chain_graph_abs(&chain_root->node, min_hit); |
| 182 | rb_root->rb_node = chain_root->node.rb_root.rb_node; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 185 | static void __sort_chain_graph_rel(struct callchain_node *node, |
| 186 | double min_percent) |
| 187 | { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 188 | struct rb_node *n; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 189 | struct callchain_node *child; |
| 190 | u64 min_hit; |
| 191 | |
| 192 | node->rb_root = RB_ROOT; |
Frederic Weisbecker | c0a8865 | 2009-08-09 04:19:15 +0200 | [diff] [blame] | 193 | min_hit = ceil(node->children_hit * min_percent); |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 194 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 195 | n = rb_first(&node->rb_root_in); |
| 196 | while (n) { |
| 197 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 198 | n = rb_next(n); |
| 199 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 200 | __sort_chain_graph_rel(child, min_percent); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 201 | if (callchain_cumul_hits(child) >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 202 | rb_insert_callchain(&node->rb_root, child, |
| 203 | CHAIN_GRAPH_REL); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 208 | 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] | 209 | u64 min_hit __maybe_unused, struct callchain_param *param) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 210 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 211 | __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0); |
| 212 | rb_root->rb_node = chain_root->node.rb_root.rb_node; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Frederic Weisbecker | 16537f1 | 2011-01-14 04:52:00 +0100 | [diff] [blame] | 215 | int callchain_register_param(struct callchain_param *param) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 216 | { |
| 217 | switch (param->mode) { |
| 218 | case CHAIN_GRAPH_ABS: |
| 219 | param->sort = sort_chain_graph_abs; |
| 220 | break; |
| 221 | case CHAIN_GRAPH_REL: |
| 222 | param->sort = sort_chain_graph_rel; |
| 223 | break; |
| 224 | case CHAIN_FLAT: |
| 225 | param->sort = sort_chain_flat; |
| 226 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 227 | case CHAIN_NONE: |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 228 | default: |
| 229 | return -1; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
| 233 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 234 | /* |
| 235 | * Create a child for a parent. If inherit_children, then the new child |
| 236 | * will become the new parent of it's parent children |
| 237 | */ |
| 238 | static struct callchain_node * |
| 239 | create_child(struct callchain_node *parent, bool inherit_children) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 240 | { |
| 241 | struct callchain_node *new; |
| 242 | |
Arnaldo Carvalho de Melo | cdd5b75b | 2010-05-10 10:56:50 -0300 | [diff] [blame] | 243 | new = zalloc(sizeof(*new)); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 244 | if (!new) { |
| 245 | perror("not enough memory to create child for code path tree"); |
| 246 | return NULL; |
| 247 | } |
| 248 | new->parent = parent; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 249 | INIT_LIST_HEAD(&new->val); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 250 | |
| 251 | if (inherit_children) { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 252 | struct rb_node *n; |
| 253 | struct callchain_node *child; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 254 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 255 | new->rb_root_in = parent->rb_root_in; |
| 256 | parent->rb_root_in = RB_ROOT; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 257 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 258 | n = rb_first(&new->rb_root_in); |
| 259 | while (n) { |
| 260 | child = rb_entry(n, struct callchain_node, rb_node_in); |
| 261 | child->parent = new; |
| 262 | n = rb_next(n); |
| 263 | } |
| 264 | |
| 265 | /* make it the first child */ |
| 266 | rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node); |
| 267 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 268 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 269 | |
| 270 | return new; |
| 271 | } |
| 272 | |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 273 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 274 | /* |
| 275 | * Fill the node with callchain values |
| 276 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 277 | static void |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 278 | fill_node(struct callchain_node *node, struct callchain_cursor *cursor) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 279 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 280 | struct callchain_cursor_node *cursor_node; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 281 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 282 | node->val_nr = cursor->nr - cursor->pos; |
| 283 | if (!node->val_nr) |
| 284 | pr_warning("Warning: empty node in callchain tree\n"); |
| 285 | |
| 286 | cursor_node = callchain_cursor_current(cursor); |
| 287 | |
| 288 | while (cursor_node) { |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 289 | struct callchain_list *call; |
| 290 | |
Arnaldo Carvalho de Melo | cdd5b75b | 2010-05-10 10:56:50 -0300 | [diff] [blame] | 291 | call = zalloc(sizeof(*call)); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 292 | if (!call) { |
| 293 | perror("not enough memory for the code path tree"); |
| 294 | return; |
| 295 | } |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 296 | call->ip = cursor_node->ip; |
| 297 | call->ms.sym = cursor_node->sym; |
| 298 | call->ms.map = cursor_node->map; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 299 | list_add_tail(&call->list, &node->val); |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 300 | |
| 301 | callchain_cursor_advance(cursor); |
| 302 | cursor_node = callchain_cursor_current(cursor); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 303 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 306 | static struct callchain_node * |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 307 | add_child(struct callchain_node *parent, |
| 308 | struct callchain_cursor *cursor, |
| 309 | u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 310 | { |
| 311 | struct callchain_node *new; |
| 312 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 313 | new = create_child(parent, false); |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 314 | fill_node(new, cursor); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 315 | |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 316 | new->children_hit = 0; |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 317 | new->hit = period; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 318 | return new; |
| 319 | } |
| 320 | |
| 321 | static s64 match_chain(struct callchain_cursor_node *node, |
| 322 | struct callchain_list *cnode) |
| 323 | { |
| 324 | struct symbol *sym = node->sym; |
| 325 | |
| 326 | if (cnode->ms.sym && sym && |
| 327 | callchain_param.key == CCKEY_FUNCTION) |
| 328 | return cnode->ms.sym->start - sym->start; |
| 329 | else |
| 330 | return cnode->ip - node->ip; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 333 | /* |
| 334 | * Split the parent in two parts (a new child is created) and |
| 335 | * give a part of its callchain to the created child. |
| 336 | * Then create another child to host the given callchain of new branch |
| 337 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 338 | static void |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 339 | split_add_child(struct callchain_node *parent, |
| 340 | struct callchain_cursor *cursor, |
| 341 | struct callchain_list *to_split, |
| 342 | u64 idx_parents, u64 idx_local, u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 343 | { |
| 344 | struct callchain_node *new; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 345 | struct list_head *old_tail; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 346 | unsigned int idx_total = idx_parents + idx_local; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 347 | |
| 348 | /* split */ |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 349 | new = create_child(parent, true); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 350 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 351 | /* split the callchain and move a part to the new child */ |
| 352 | old_tail = parent->val.prev; |
| 353 | list_del_range(&to_split->list, old_tail); |
| 354 | new->val.next = &to_split->list; |
| 355 | new->val.prev = old_tail; |
| 356 | to_split->list.prev = &new->val; |
| 357 | old_tail->next = &new->val; |
| 358 | |
| 359 | /* split the hits */ |
| 360 | new->hit = parent->hit; |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 361 | new->children_hit = parent->children_hit; |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 362 | parent->children_hit = callchain_cumul_hits(new); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 363 | new->val_nr = parent->val_nr - idx_local; |
| 364 | parent->val_nr = idx_local; |
| 365 | |
| 366 | /* create a new child for the new branch if any */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 367 | if (idx_total < cursor->nr) { |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 368 | struct callchain_node *first; |
| 369 | struct callchain_list *cnode; |
| 370 | struct callchain_cursor_node *node; |
| 371 | struct rb_node *p, **pp; |
| 372 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 373 | parent->hit = 0; |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 374 | parent->children_hit += period; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 375 | |
| 376 | node = callchain_cursor_current(cursor); |
| 377 | new = add_child(parent, cursor, period); |
| 378 | |
| 379 | /* |
| 380 | * This is second child since we moved parent's children |
| 381 | * to new (first) child above. |
| 382 | */ |
| 383 | p = parent->rb_root_in.rb_node; |
| 384 | first = rb_entry(p, struct callchain_node, rb_node_in); |
| 385 | cnode = list_first_entry(&first->val, struct callchain_list, |
| 386 | list); |
| 387 | |
| 388 | if (match_chain(node, cnode) < 0) |
| 389 | pp = &p->rb_left; |
| 390 | else |
| 391 | pp = &p->rb_right; |
| 392 | |
| 393 | rb_link_node(&new->rb_node_in, p, pp); |
| 394 | rb_insert_color(&new->rb_node_in, &parent->rb_root_in); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 395 | } else { |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 396 | parent->hit = period; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 397 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static int |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 401 | append_chain(struct callchain_node *root, |
| 402 | struct callchain_cursor *cursor, |
| 403 | u64 period); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 404 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 405 | static void |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 406 | append_chain_children(struct callchain_node *root, |
| 407 | struct callchain_cursor *cursor, |
| 408 | u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 409 | { |
| 410 | struct callchain_node *rnode; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 411 | struct callchain_cursor_node *node; |
| 412 | struct rb_node **p = &root->rb_root_in.rb_node; |
| 413 | struct rb_node *parent = NULL; |
| 414 | |
| 415 | node = callchain_cursor_current(cursor); |
| 416 | if (!node) |
| 417 | return; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 418 | |
| 419 | /* lookup in childrens */ |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 420 | while (*p) { |
| 421 | s64 ret; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 422 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 423 | parent = *p; |
| 424 | rnode = rb_entry(parent, struct callchain_node, rb_node_in); |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 425 | |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 426 | /* If at least first entry matches, rely to children */ |
| 427 | ret = append_chain(rnode, cursor, period); |
| 428 | if (ret == 0) |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 429 | goto inc_children_hit; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 430 | |
| 431 | if (ret < 0) |
| 432 | p = &parent->rb_left; |
| 433 | else |
| 434 | p = &parent->rb_right; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 435 | } |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 436 | /* nothing in children, add to the current node */ |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 437 | rnode = add_child(root, cursor, period); |
| 438 | rb_link_node(&rnode->rb_node_in, parent, p); |
| 439 | rb_insert_color(&rnode->rb_node_in, &root->rb_root_in); |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame] | 440 | |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 441 | inc_children_hit: |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 442 | root->children_hit += period; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static int |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 446 | append_chain(struct callchain_node *root, |
| 447 | struct callchain_cursor *cursor, |
| 448 | u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 449 | { |
| 450 | struct callchain_list *cnode; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 451 | u64 start = cursor->pos; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 452 | bool found = false; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 453 | u64 matches; |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 454 | int cmp = 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 455 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 456 | /* |
| 457 | * Lookup in the current node |
| 458 | * If we have a symbol, then compare the start to match |
Andi Kleen | 99571ab | 2013-07-18 15:33:57 -0700 | [diff] [blame] | 459 | * anywhere inside a function, unless function |
| 460 | * mode is disabled. |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 461 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 462 | list_for_each_entry(cnode, &root->val, list) { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 463 | struct callchain_cursor_node *node; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 464 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 465 | node = callchain_cursor_current(cursor); |
| 466 | if (!node) |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 467 | break; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 468 | |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 469 | cmp = match_chain(node, cnode); |
| 470 | if (cmp) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 471 | break; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 472 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 473 | found = true; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 474 | |
| 475 | callchain_cursor_advance(cursor); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 478 | /* matches not, relay no the parent */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 479 | if (!found) { |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 480 | WARN_ONCE(!cmp, "Chain comparison error\n"); |
Frederic Weisbecker | b965bb4 | 2014-01-14 16:37:15 +0100 | [diff] [blame] | 481 | return cmp; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | matches = cursor->pos - start; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 485 | |
| 486 | /* 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] | 487 | if (matches < root->val_nr) { |
| 488 | split_add_child(root, cursor, cnode, start, matches, period); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* we match 100% of the path, increment the hit */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 493 | if (matches == root->val_nr && cursor->pos == cursor->nr) { |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 494 | root->hit += period; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 498 | /* We match the node and still have a part remaining */ |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 499 | append_chain_children(root, cursor, period); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 500 | |
| 501 | return 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 504 | int callchain_append(struct callchain_root *root, |
| 505 | struct callchain_cursor *cursor, |
| 506 | u64 period) |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 507 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 508 | if (!cursor->nr) |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 509 | return 0; |
| 510 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 511 | callchain_cursor_commit(cursor); |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 512 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 513 | append_chain_children(&root->node, cursor, period); |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 514 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 515 | if (cursor->nr > root->max_depth) |
| 516 | root->max_depth = cursor->nr; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 517 | |
| 518 | return 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 519 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 520 | |
| 521 | static int |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 522 | merge_chain_branch(struct callchain_cursor *cursor, |
| 523 | struct callchain_node *dst, struct callchain_node *src) |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 524 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 525 | struct callchain_cursor_node **old_last = cursor->last; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 526 | struct callchain_node *child; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 527 | struct callchain_list *list, *next_list; |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 528 | struct rb_node *n; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 529 | int old_pos = cursor->nr; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 530 | int err = 0; |
| 531 | |
| 532 | list_for_each_entry_safe(list, next_list, &src->val, list) { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 533 | callchain_cursor_append(cursor, list->ip, |
| 534 | list->ms.map, list->ms.sym); |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 535 | list_del(&list->list); |
| 536 | free(list); |
| 537 | } |
| 538 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 539 | if (src->hit) { |
| 540 | callchain_cursor_commit(cursor); |
| 541 | append_chain_children(dst, cursor, src->hit); |
| 542 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 543 | |
Namhyung Kim | e369517 | 2013-10-11 14:15:36 +0900 | [diff] [blame] | 544 | n = rb_first(&src->rb_root_in); |
| 545 | while (n) { |
| 546 | child = container_of(n, struct callchain_node, rb_node_in); |
| 547 | n = rb_next(n); |
| 548 | rb_erase(&child->rb_node_in, &src->rb_root_in); |
| 549 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 550 | err = merge_chain_branch(cursor, dst, child); |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 551 | if (err) |
| 552 | break; |
| 553 | |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 554 | free(child); |
| 555 | } |
| 556 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 557 | cursor->nr = old_pos; |
| 558 | cursor->last = old_last; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 559 | |
| 560 | return err; |
| 561 | } |
| 562 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 563 | int callchain_merge(struct callchain_cursor *cursor, |
| 564 | struct callchain_root *dst, struct callchain_root *src) |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 565 | { |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 566 | return merge_chain_branch(cursor, &dst->node, &src->node); |
| 567 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 568 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 569 | int callchain_cursor_append(struct callchain_cursor *cursor, |
| 570 | u64 ip, struct map *map, struct symbol *sym) |
| 571 | { |
| 572 | struct callchain_cursor_node *node = *cursor->last; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 573 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 574 | if (!node) { |
Paul Gortmaker | 91b9880 | 2013-01-30 20:05:49 -0500 | [diff] [blame] | 575 | node = calloc(1, sizeof(*node)); |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 576 | if (!node) |
| 577 | return -ENOMEM; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 578 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 579 | *cursor->last = node; |
| 580 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 581 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 582 | node->ip = ip; |
| 583 | node->map = map; |
| 584 | node->sym = sym; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 585 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 586 | cursor->nr++; |
| 587 | |
| 588 | cursor->last = &node->next; |
| 589 | |
| 590 | return 0; |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame] | 591 | } |
Namhyung Kim | 2dc9fb1 | 2014-01-14 14:25:35 +0900 | [diff] [blame] | 592 | |
| 593 | int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent, |
| 594 | struct perf_evsel *evsel, struct addr_location *al, |
| 595 | int max_stack) |
| 596 | { |
| 597 | if (sample->callchain == NULL) |
| 598 | return 0; |
| 599 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 600 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain || |
| 601 | sort__has_parent) { |
Namhyung Kim | 2dc9fb1 | 2014-01-14 14:25:35 +0900 | [diff] [blame] | 602 | return machine__resolve_callchain(al->machine, evsel, al->thread, |
| 603 | sample, parent, al, max_stack); |
| 604 | } |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample) |
| 609 | { |
Adrian Hunter | 4d40b05 | 2014-07-14 13:02:35 +0300 | [diff] [blame] | 610 | if (!symbol_conf.use_callchain || sample->callchain == NULL) |
Namhyung Kim | 2dc9fb1 | 2014-01-14 14:25:35 +0900 | [diff] [blame] | 611 | return 0; |
| 612 | return callchain_append(he->callchain, &callchain_cursor, sample->period); |
| 613 | } |
Namhyung Kim | c7405d8 | 2013-10-31 13:58:30 +0900 | [diff] [blame] | 614 | |
| 615 | int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node, |
| 616 | bool hide_unresolved) |
| 617 | { |
| 618 | al->map = node->map; |
| 619 | al->sym = node->sym; |
| 620 | if (node->map) |
| 621 | al->addr = node->map->map_ip(node->map, node->ip); |
| 622 | else |
| 623 | al->addr = node->ip; |
| 624 | |
| 625 | if (al->sym == NULL) { |
| 626 | if (hide_unresolved) |
| 627 | return 0; |
| 628 | if (al->map == NULL) |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | if (al->map->groups == &al->machine->kmaps) { |
| 633 | if (machine__is_host(al->machine)) { |
| 634 | al->cpumode = PERF_RECORD_MISC_KERNEL; |
| 635 | al->level = 'k'; |
| 636 | } else { |
| 637 | al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL; |
| 638 | al->level = 'g'; |
| 639 | } |
| 640 | } else { |
| 641 | if (machine__is_host(al->machine)) { |
| 642 | al->cpumode = PERF_RECORD_MISC_USER; |
| 643 | al->level = '.'; |
| 644 | } else if (perf_guest) { |
| 645 | al->cpumode = PERF_RECORD_MISC_GUEST_USER; |
| 646 | al->level = 'u'; |
| 647 | } else { |
| 648 | al->cpumode = PERF_RECORD_MISC_HYPERVISOR; |
| 649 | al->level = 'H'; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | out: |
| 654 | return 1; |
| 655 | } |