Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com> |
| 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> |
| 16 | |
| 17 | #include "callchain.h" |
| 18 | |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 19 | #define chain_for_each_child(child, parent) \ |
| 20 | list_for_each_entry(child, &parent->children, brothers) |
| 21 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 22 | static void |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 23 | rb_insert_callchain(struct rb_root *root, struct callchain_node *chain, |
| 24 | enum chain_mode mode) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 25 | { |
| 26 | struct rb_node **p = &root->rb_node; |
| 27 | struct rb_node *parent = NULL; |
| 28 | struct callchain_node *rnode; |
| 29 | |
| 30 | while (*p) { |
| 31 | parent = *p; |
| 32 | rnode = rb_entry(parent, struct callchain_node, rb_node); |
| 33 | |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 34 | switch (mode) { |
| 35 | case FLAT: |
| 36 | if (rnode->hit < chain->hit) |
| 37 | p = &(*p)->rb_left; |
| 38 | else |
| 39 | p = &(*p)->rb_right; |
| 40 | break; |
| 41 | case GRAPH: |
| 42 | if (rnode->cumul_hit < chain->cumul_hit) |
| 43 | p = &(*p)->rb_left; |
| 44 | else |
| 45 | p = &(*p)->rb_right; |
| 46 | break; |
| 47 | default: |
| 48 | break; |
| 49 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | rb_link_node(&chain->rb_node, parent, p); |
| 53 | rb_insert_color(&chain->rb_node, root); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Once we get every callchains from the stream, we can now |
| 58 | * sort them by hit |
| 59 | */ |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 60 | void sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node, |
| 61 | u64 min_hit) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 62 | { |
| 63 | struct callchain_node *child; |
| 64 | |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 65 | chain_for_each_child(child, node) |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 66 | sort_chain_flat(rb_root, child, min_hit); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 67 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 68 | if (node->hit && node->hit >= min_hit) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 69 | rb_insert_callchain(rb_root, node, FLAT); |
| 70 | } |
| 71 | |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 72 | static void __sort_chain_graph(struct callchain_node *node, u64 min_hit) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 73 | { |
| 74 | struct callchain_node *child; |
| 75 | |
| 76 | node->rb_root = RB_ROOT; |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 77 | |
| 78 | chain_for_each_child(child, node) { |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 79 | __sort_chain_graph(child, min_hit); |
| 80 | if (child->cumul_hit >= min_hit) |
| 81 | rb_insert_callchain(&node->rb_root, child, GRAPH); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | void |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 86 | sort_chain_graph(struct rb_root *rb_root, struct callchain_node *chain_root, |
| 87 | u64 min_hit) |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 88 | { |
Frederic Weisbecker | c20ab37 | 2009-07-02 20:14:33 +0200 | [diff] [blame] | 89 | __sort_chain_graph(chain_root, min_hit); |
Frederic Weisbecker | 4eb3e47 | 2009-07-02 17:58:21 +0200 | [diff] [blame] | 90 | rb_root->rb_node = chain_root->rb_root.rb_node; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Create a child for a parent. If inherit_children, then the new child |
| 95 | * will become the new parent of it's parent children |
| 96 | */ |
| 97 | static struct callchain_node * |
| 98 | create_child(struct callchain_node *parent, bool inherit_children) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 99 | { |
| 100 | struct callchain_node *new; |
| 101 | |
| 102 | new = malloc(sizeof(*new)); |
| 103 | if (!new) { |
| 104 | perror("not enough memory to create child for code path tree"); |
| 105 | return NULL; |
| 106 | } |
| 107 | new->parent = parent; |
| 108 | INIT_LIST_HEAD(&new->children); |
| 109 | INIT_LIST_HEAD(&new->val); |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 110 | |
| 111 | if (inherit_children) { |
| 112 | struct callchain_node *next; |
| 113 | |
| 114 | list_splice(&parent->children, &new->children); |
| 115 | INIT_LIST_HEAD(&parent->children); |
| 116 | |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 117 | chain_for_each_child(next, new) |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 118 | next->parent = new; |
| 119 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 120 | list_add_tail(&new->brothers, &parent->children); |
| 121 | |
| 122 | return new; |
| 123 | } |
| 124 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Fill the node with callchain values |
| 127 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 128 | static void |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 129 | fill_node(struct callchain_node *node, struct ip_callchain *chain, |
| 130 | int start, struct symbol **syms) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 131 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 132 | unsigned int i; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 133 | |
| 134 | for (i = start; i < chain->nr; i++) { |
| 135 | struct callchain_list *call; |
| 136 | |
Frederic Weisbecker | 9198aa7 | 2009-07-01 05:35:13 +0200 | [diff] [blame] | 137 | call = malloc(sizeof(*call)); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 138 | if (!call) { |
| 139 | perror("not enough memory for the code path tree"); |
| 140 | return; |
| 141 | } |
| 142 | call->ip = chain->ips[i]; |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 143 | call->sym = syms[i]; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 144 | list_add_tail(&call->list, &node->val); |
| 145 | } |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 146 | node->val_nr = chain->nr - start; |
| 147 | if (!node->val_nr) |
| 148 | printf("Warning: empty node in callchain tree\n"); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 151 | static void |
| 152 | add_child(struct callchain_node *parent, struct ip_callchain *chain, |
| 153 | int start, struct symbol **syms) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 154 | { |
| 155 | struct callchain_node *new; |
| 156 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 157 | new = create_child(parent, false); |
| 158 | fill_node(new, chain, start, syms); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 159 | |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame^] | 160 | new->cumul_hit = new->hit = 1; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Split the parent in two parts (a new child is created) and |
| 165 | * give a part of its callchain to the created child. |
| 166 | * Then create another child to host the given callchain of new branch |
| 167 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 168 | static void |
| 169 | split_add_child(struct callchain_node *parent, struct ip_callchain *chain, |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 170 | struct callchain_list *to_split, int idx_parents, int idx_local, |
| 171 | struct symbol **syms) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 172 | { |
| 173 | struct callchain_node *new; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 174 | struct list_head *old_tail; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 175 | unsigned int idx_total = idx_parents + idx_local; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 176 | |
| 177 | /* split */ |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 178 | new = create_child(parent, true); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 179 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 180 | /* split the callchain and move a part to the new child */ |
| 181 | old_tail = parent->val.prev; |
| 182 | list_del_range(&to_split->list, old_tail); |
| 183 | new->val.next = &to_split->list; |
| 184 | new->val.prev = old_tail; |
| 185 | to_split->list.prev = &new->val; |
| 186 | old_tail->next = &new->val; |
| 187 | |
| 188 | /* split the hits */ |
| 189 | new->hit = parent->hit; |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame^] | 190 | new->cumul_hit = parent->cumul_hit; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 191 | new->val_nr = parent->val_nr - idx_local; |
| 192 | parent->val_nr = idx_local; |
| 193 | |
| 194 | /* create a new child for the new branch if any */ |
| 195 | if (idx_total < chain->nr) { |
| 196 | parent->hit = 0; |
| 197 | add_child(parent, chain, idx_total, syms); |
| 198 | } else { |
| 199 | parent->hit = 1; |
| 200 | } |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static int |
| 204 | __append_chain(struct callchain_node *root, struct ip_callchain *chain, |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 205 | unsigned int start, struct symbol **syms); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 206 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 207 | static void |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 208 | __append_chain_children(struct callchain_node *root, struct ip_callchain *chain, |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 209 | struct symbol **syms, unsigned int start) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 210 | { |
| 211 | struct callchain_node *rnode; |
| 212 | |
| 213 | /* lookup in childrens */ |
Frederic Weisbecker | 14f4654 | 2009-07-02 17:58:19 +0200 | [diff] [blame] | 214 | chain_for_each_child(rnode, root) { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 215 | unsigned int ret = __append_chain(rnode, chain, start, syms); |
| 216 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 217 | if (!ret) |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame^] | 218 | goto cumul; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 219 | } |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 220 | /* nothing in children, add to the current node */ |
| 221 | add_child(root, chain, start, syms); |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame^] | 222 | |
| 223 | cumul: |
| 224 | root->cumul_hit++; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int |
| 228 | __append_chain(struct callchain_node *root, struct ip_callchain *chain, |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 229 | unsigned int start, struct symbol **syms) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 230 | { |
| 231 | struct callchain_list *cnode; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 232 | unsigned int i = start; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 233 | bool found = false; |
| 234 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 235 | /* |
| 236 | * Lookup in the current node |
| 237 | * If we have a symbol, then compare the start to match |
| 238 | * anywhere inside a function. |
| 239 | */ |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 240 | list_for_each_entry(cnode, &root->val, list) { |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 241 | if (i == chain->nr) |
| 242 | break; |
| 243 | if (cnode->sym && syms[i]) { |
| 244 | if (cnode->sym->start != syms[i]->start) |
| 245 | break; |
| 246 | } else if (cnode->ip != chain->ips[i]) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 247 | break; |
| 248 | if (!found) |
| 249 | found = true; |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 250 | i++; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* matches not, relay on the parent */ |
| 254 | if (!found) |
| 255 | return -1; |
| 256 | |
| 257 | /* 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] | 258 | if (i - start < root->val_nr) { |
| 259 | split_add_child(root, chain, cnode, start, i - start, syms); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /* we match 100% of the path, increment the hit */ |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 264 | if (i - start == root->val_nr && i == chain->nr) { |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 265 | root->hit++; |
Frederic Weisbecker | e05b876 | 2009-07-05 07:39:20 +0200 | [diff] [blame^] | 266 | root->cumul_hit++; |
| 267 | |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 271 | /* We match the node and still have a part remaining */ |
| 272 | __append_chain_children(root, chain, syms, i); |
| 273 | |
| 274 | return 0; |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Frederic Weisbecker | 4424961 | 2009-07-01 05:35:14 +0200 | [diff] [blame] | 277 | void append_chain(struct callchain_node *root, struct ip_callchain *chain, |
| 278 | struct symbol **syms) |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 279 | { |
Frederic Weisbecker | deac911 | 2009-07-01 05:35:15 +0200 | [diff] [blame] | 280 | __append_chain_children(root, chain, syms, 0); |
Frederic Weisbecker | 8cb76d9 | 2009-06-26 16:28:00 +0200 | [diff] [blame] | 281 | } |