Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 1 | /* |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 2 | * Copyright (C) 2009-2010, 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 | |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 18 | #include "util.h" |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 19 | #include "callchain.h" |
| 20 | |
Arnaldo Carvalho de Melo | 41a37e2 | 2010-06-04 08:02:07 -0300 | [diff] [blame] | 21 | bool ip_callchain__valid(struct ip_callchain *chain, const event_t *event) |
Arnaldo Carvalho de Melo | 139633c | 2010-05-09 11:47:13 -0300 | [diff] [blame] | 22 | { |
| 23 | unsigned int chain_size = event->header.size; |
| 24 | chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event; |
| 25 | return chain->nr * sizeof(u64) <= chain_size; |
| 26 | } |
| 27 | |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 28 | #define chain_for_each_child(child, parent) \ |
| 29 | list_for_each_entry(child, &parent->children, brothers) |
| 30 | |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame^] | 31 | #define chain_for_each_child_safe(child, next, parent) \ |
| 32 | list_for_each_entry_safe(child, next, &parent->children, brothers) |
| 33 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 34 | static void |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 35 | rb_insert_callchain(struct rb_root *root, struct callchain_node *chain, |
| 36 | enum chain_mode mode) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 37 | { |
| 38 | struct rb_node **p = &root->rb_node; |
| 39 | struct rb_node *parent = NULL; |
| 40 | struct callchain_node *rnode; |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 41 | u64 chain_cumul = cumul_hits(chain); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 42 | |
| 43 | while (*p) { |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 44 | u64 rnode_cumul; |
| 45 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 46 | parent = *p; |
| 47 | rnode = rb_entry(parent, struct callchain_node, rb_node); |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 48 | rnode_cumul = cumul_hits(rnode); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 49 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 50 | switch (mode) { |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 51 | case CHAIN_FLAT: |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 52 | if (rnode->hit < chain->hit) |
| 53 | p = &(*p)->rb_left; |
| 54 | else |
| 55 | p = &(*p)->rb_right; |
| 56 | break; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 57 | case CHAIN_GRAPH_ABS: /* Falldown */ |
| 58 | case CHAIN_GRAPH_REL: |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 59 | if (rnode_cumul < chain_cumul) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 60 | p = &(*p)->rb_left; |
| 61 | else |
| 62 | p = &(*p)->rb_right; |
| 63 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 64 | case CHAIN_NONE: |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 65 | default: |
| 66 | break; |
| 67 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | rb_link_node(&chain->rb_node, parent, p); |
| 71 | rb_insert_color(&chain->rb_node, root); |
| 72 | } |
| 73 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 74 | static void |
| 75 | __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node, |
| 76 | u64 min_hit) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 77 | { |
| 78 | struct callchain_node *child; |
| 79 | |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 80 | chain_for_each_child(child, node) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 81 | __sort_chain_flat(rb_root, child, min_hit); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 82 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 83 | if (node->hit && node->hit >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 84 | rb_insert_callchain(rb_root, node, CHAIN_FLAT); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 87 | /* |
| 88 | * Once we get every callchains from the stream, we can now |
| 89 | * sort them by hit |
| 90 | */ |
| 91 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 92 | sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root, |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 93 | u64 min_hit, struct callchain_param *param __used) |
| 94 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 95 | __sort_chain_flat(rb_root, &root->node, min_hit); |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void __sort_chain_graph_abs(struct callchain_node *node, |
| 99 | u64 min_hit) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 100 | { |
| 101 | struct callchain_node *child; |
| 102 | |
| 103 | node->rb_root = RB_ROOT; |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 104 | |
| 105 | chain_for_each_child(child, node) { |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 106 | __sort_chain_graph_abs(child, min_hit); |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 107 | if (cumul_hits(child) >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 108 | rb_insert_callchain(&node->rb_root, child, |
| 109 | CHAIN_GRAPH_ABS); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 113 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 114 | sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root, |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 115 | u64 min_hit, struct callchain_param *param __used) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 116 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 117 | __sort_chain_graph_abs(&chain_root->node, min_hit); |
| 118 | rb_root->rb_node = chain_root->node.rb_root.rb_node; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 121 | static void __sort_chain_graph_rel(struct callchain_node *node, |
| 122 | double min_percent) |
| 123 | { |
| 124 | struct callchain_node *child; |
| 125 | u64 min_hit; |
| 126 | |
| 127 | node->rb_root = RB_ROOT; |
Frederic Weisbecker | c0a8865 | 2009-08-09 04:19:15 +0200 | [diff] [blame] | 128 | min_hit = ceil(node->children_hit * min_percent); |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 129 | |
| 130 | chain_for_each_child(child, node) { |
| 131 | __sort_chain_graph_rel(child, min_percent); |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 132 | if (cumul_hits(child) >= min_hit) |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 133 | rb_insert_callchain(&node->rb_root, child, |
| 134 | CHAIN_GRAPH_REL); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 139 | sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root, |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 140 | u64 min_hit __used, struct callchain_param *param) |
| 141 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 142 | __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0); |
| 143 | rb_root->rb_node = chain_root->node.rb_root.rb_node; |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int register_callchain_param(struct callchain_param *param) |
| 147 | { |
| 148 | switch (param->mode) { |
| 149 | case CHAIN_GRAPH_ABS: |
| 150 | param->sort = sort_chain_graph_abs; |
| 151 | break; |
| 152 | case CHAIN_GRAPH_REL: |
| 153 | param->sort = sort_chain_graph_rel; |
| 154 | break; |
| 155 | case CHAIN_FLAT: |
| 156 | param->sort = sort_chain_flat; |
| 157 | break; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 158 | case CHAIN_NONE: |
Frederic Weisbecker | 805d127 | 2009-07-05 07:39:21 +0200 | [diff] [blame] | 159 | default: |
| 160 | return -1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 165 | /* |
| 166 | * Create a child for a parent. If inherit_children, then the new child |
| 167 | * will become the new parent of it's parent children |
| 168 | */ |
| 169 | static struct callchain_node * |
| 170 | create_child(struct callchain_node *parent, bool inherit_children) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 171 | { |
| 172 | struct callchain_node *new; |
| 173 | |
Arnaldo Carvalho de Melo | cdd5b75b | 2010-05-10 10:56:50 -0300 | [diff] [blame] | 174 | new = zalloc(sizeof(*new)); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 175 | if (!new) { |
| 176 | perror("not enough memory to create child for code path tree"); |
| 177 | return NULL; |
| 178 | } |
| 179 | new->parent = parent; |
| 180 | INIT_LIST_HEAD(&new->children); |
| 181 | INIT_LIST_HEAD(&new->val); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 182 | |
| 183 | if (inherit_children) { |
| 184 | struct callchain_node *next; |
| 185 | |
| 186 | list_splice(&parent->children, &new->children); |
| 187 | INIT_LIST_HEAD(&parent->children); |
| 188 | |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 189 | chain_for_each_child(next, new) |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 190 | next->parent = new; |
| 191 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 192 | list_add_tail(&new->brothers, &parent->children); |
| 193 | |
| 194 | return new; |
| 195 | } |
| 196 | |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 197 | |
| 198 | struct resolved_ip { |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 199 | u64 ip; |
| 200 | struct map_symbol ms; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | struct resolved_chain { |
| 204 | u64 nr; |
| 205 | struct resolved_ip ips[0]; |
| 206 | }; |
| 207 | |
| 208 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 209 | /* |
| 210 | * Fill the node with callchain values |
| 211 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 212 | static void |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 213 | fill_node(struct callchain_node *node, struct resolved_chain *chain, int start) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 214 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 215 | unsigned int i; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 216 | |
| 217 | for (i = start; i < chain->nr; i++) { |
| 218 | struct callchain_list *call; |
| 219 | |
Arnaldo Carvalho de Melo | cdd5b75b | 2010-05-10 10:56:50 -0300 | [diff] [blame] | 220 | call = zalloc(sizeof(*call)); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 221 | if (!call) { |
| 222 | perror("not enough memory for the code path tree"); |
| 223 | return; |
| 224 | } |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 225 | call->ip = chain->ips[i].ip; |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 226 | call->ms = chain->ips[i].ms; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 227 | list_add_tail(&call->list, &node->val); |
| 228 | } |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 229 | node->val_nr = chain->nr - start; |
| 230 | if (!node->val_nr) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 231 | pr_warning("Warning: empty node in callchain tree\n"); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 234 | static void |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 235 | add_child(struct callchain_node *parent, struct resolved_chain *chain, |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 236 | int start, u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 237 | { |
| 238 | struct callchain_node *new; |
| 239 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 240 | new = create_child(parent, false); |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 241 | fill_node(new, chain, start); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 242 | |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 243 | new->children_hit = 0; |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 244 | new->hit = period; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 247 | /* |
| 248 | * Split the parent in two parts (a new child is created) and |
| 249 | * give a part of its callchain to the created child. |
| 250 | * Then create another child to host the given callchain of new branch |
| 251 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 252 | static void |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 253 | split_add_child(struct callchain_node *parent, struct resolved_chain *chain, |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 254 | struct callchain_list *to_split, int idx_parents, int idx_local, |
| 255 | u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 256 | { |
| 257 | struct callchain_node *new; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 258 | struct list_head *old_tail; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 259 | unsigned int idx_total = idx_parents + idx_local; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 260 | |
| 261 | /* split */ |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 262 | new = create_child(parent, true); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 263 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 264 | /* split the callchain and move a part to the new child */ |
| 265 | old_tail = parent->val.prev; |
| 266 | list_del_range(&to_split->list, old_tail); |
| 267 | new->val.next = &to_split->list; |
| 268 | new->val.prev = old_tail; |
| 269 | to_split->list.prev = &new->val; |
| 270 | old_tail->next = &new->val; |
| 271 | |
| 272 | /* split the hits */ |
| 273 | new->hit = parent->hit; |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 274 | new->children_hit = parent->children_hit; |
| 275 | parent->children_hit = cumul_hits(new); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 276 | new->val_nr = parent->val_nr - idx_local; |
| 277 | parent->val_nr = idx_local; |
| 278 | |
| 279 | /* create a new child for the new branch if any */ |
| 280 | if (idx_total < chain->nr) { |
| 281 | parent->hit = 0; |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 282 | add_child(parent, chain, idx_total, period); |
| 283 | parent->children_hit += period; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 284 | } else { |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 285 | parent->hit = period; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 286 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static int |
Frederic Weisbecker | 6cb8e56 | 2010-08-22 20:18:01 +0200 | [diff] [blame] | 290 | append_chain(struct callchain_node *root, struct resolved_chain *chain, |
| 291 | unsigned int start, u64 period); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 292 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 293 | static void |
Frederic Weisbecker | 6cb8e56 | 2010-08-22 20:18:01 +0200 | [diff] [blame] | 294 | append_chain_children(struct callchain_node *root, struct resolved_chain *chain, |
| 295 | unsigned int start, u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 296 | { |
| 297 | struct callchain_node *rnode; |
| 298 | |
| 299 | /* lookup in childrens */ |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 300 | chain_for_each_child(rnode, root) { |
Frederic Weisbecker | 6cb8e56 | 2010-08-22 20:18:01 +0200 | [diff] [blame] | 301 | unsigned int ret = append_chain(rnode, chain, start, period); |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 302 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 303 | if (!ret) |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 304 | goto inc_children_hit; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 305 | } |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 306 | /* nothing in children, add to the current node */ |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 307 | add_child(root, chain, start, period); |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame] | 308 | |
Frederic Weisbecker | 1953287 | 2009-08-07 07:11:05 +0200 | [diff] [blame] | 309 | inc_children_hit: |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 310 | root->children_hit += period; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int |
Frederic Weisbecker | 6cb8e56 | 2010-08-22 20:18:01 +0200 | [diff] [blame] | 314 | append_chain(struct callchain_node *root, struct resolved_chain *chain, |
| 315 | unsigned int start, u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 316 | { |
| 317 | struct callchain_list *cnode; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 318 | unsigned int i = start; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 319 | bool found = false; |
| 320 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 321 | /* |
| 322 | * Lookup in the current node |
| 323 | * If we have a symbol, then compare the start to match |
| 324 | * anywhere inside a function. |
| 325 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 326 | list_for_each_entry(cnode, &root->val, list) { |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 327 | struct symbol *sym; |
| 328 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 329 | if (i == chain->nr) |
| 330 | break; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 331 | |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 332 | sym = chain->ips[i].ms.sym; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 333 | |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 334 | if (cnode->ms.sym && sym) { |
| 335 | if (cnode->ms.sym->start != sym->start) |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 336 | break; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 337 | } else if (cnode->ip != chain->ips[i].ip) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 338 | break; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 339 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 340 | if (!found) |
| 341 | found = true; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 342 | i++; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* matches not, relay on the parent */ |
| 346 | if (!found) |
| 347 | return -1; |
| 348 | |
| 349 | /* we match only a part of the node. Split it and add the new chain */ |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 350 | if (i - start < root->val_nr) { |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 351 | split_add_child(root, chain, cnode, start, i - start, period); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /* we match 100% of the path, increment the hit */ |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 356 | if (i - start == root->val_nr && i == chain->nr) { |
Frederic Weisbecker | 108553e | 2010-07-08 03:41:46 +0200 | [diff] [blame] | 357 | root->hit += period; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 361 | /* We match the node and still have a part remaining */ |
Frederic Weisbecker | 6cb8e56 | 2010-08-22 20:18:01 +0200 | [diff] [blame] | 362 | append_chain_children(root, chain, i, period); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 363 | |
| 364 | return 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 367 | static void filter_context(struct ip_callchain *old, struct resolved_chain *new, |
| 368 | struct map_symbol *syms) |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 369 | { |
| 370 | int i, j = 0; |
| 371 | |
| 372 | for (i = 0; i < (int)old->nr; i++) { |
| 373 | if (old->ips[i] >= PERF_CONTEXT_MAX) |
| 374 | continue; |
| 375 | |
| 376 | new->ips[j].ip = old->ips[i]; |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 377 | new->ips[j].ms = syms[i]; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 378 | j++; |
| 379 | } |
| 380 | |
| 381 | new->nr = j; |
| 382 | } |
| 383 | |
| 384 | |
Frederic Weisbecker | 6cb8e56 | 2010-08-22 20:18:01 +0200 | [diff] [blame] | 385 | int callchain_append(struct callchain_root *root, struct ip_callchain *chain, |
| 386 | struct map_symbol *syms, u64 period) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 387 | { |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 388 | struct resolved_chain *filtered; |
| 389 | |
Frederic Weisbecker | b0efe21 | 2009-08-08 02:16:23 +0200 | [diff] [blame] | 390 | if (!chain->nr) |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 391 | return 0; |
| 392 | |
Arnaldo Carvalho de Melo | cdd5b75b | 2010-05-10 10:56:50 -0300 | [diff] [blame] | 393 | filtered = zalloc(sizeof(*filtered) + |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 394 | chain->nr * sizeof(struct resolved_ip)); |
| 395 | if (!filtered) |
| 396 | return -ENOMEM; |
| 397 | |
| 398 | filter_context(chain, filtered, syms); |
| 399 | |
| 400 | if (!filtered->nr) |
| 401 | goto end; |
| 402 | |
Frederic Weisbecker | 6cb8e56 | 2010-08-22 20:18:01 +0200 | [diff] [blame] | 403 | append_chain_children(&root->node, filtered, 0, period); |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 404 | |
| 405 | if (filtered->nr > root->max_depth) |
| 406 | root->max_depth = filtered->nr; |
Frederic Weisbecker | 301fde2 | 2010-03-22 13:09:33 -0300 | [diff] [blame] | 407 | end: |
| 408 | free(filtered); |
| 409 | |
| 410 | return 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 411 | } |
Frederic Weisbecker | 612d4fd | 2010-08-22 21:10:35 +0200 | [diff] [blame^] | 412 | |
| 413 | static int |
| 414 | merge_chain_branch(struct callchain_node *dst, struct callchain_node *src, |
| 415 | struct resolved_chain *chain) |
| 416 | { |
| 417 | struct callchain_node *child, *next_child; |
| 418 | struct callchain_list *list, *next_list; |
| 419 | int old_pos = chain->nr; |
| 420 | int err = 0; |
| 421 | |
| 422 | list_for_each_entry_safe(list, next_list, &src->val, list) { |
| 423 | chain->ips[chain->nr].ip = list->ip; |
| 424 | chain->ips[chain->nr].ms = list->ms; |
| 425 | chain->nr++; |
| 426 | list_del(&list->list); |
| 427 | free(list); |
| 428 | } |
| 429 | |
| 430 | if (src->hit) |
| 431 | append_chain_children(dst, chain, 0, src->hit); |
| 432 | |
| 433 | chain_for_each_child_safe(child, next_child, src) { |
| 434 | err = merge_chain_branch(dst, child, chain); |
| 435 | if (err) |
| 436 | break; |
| 437 | |
| 438 | list_del(&child->brothers); |
| 439 | free(child); |
| 440 | } |
| 441 | |
| 442 | chain->nr = old_pos; |
| 443 | |
| 444 | return err; |
| 445 | } |
| 446 | |
| 447 | int callchain_merge(struct callchain_root *dst, struct callchain_root *src) |
| 448 | { |
| 449 | struct resolved_chain *chain; |
| 450 | int err; |
| 451 | |
| 452 | chain = malloc(sizeof(*chain) + |
| 453 | src->max_depth * sizeof(struct resolved_ip)); |
| 454 | if (!chain) |
| 455 | return -ENOMEM; |
| 456 | |
| 457 | chain->nr = 0; |
| 458 | |
| 459 | err = merge_chain_branch(&dst->node, &src->node, chain); |
| 460 | |
| 461 | free(chain); |
| 462 | |
| 463 | return err; |
| 464 | } |