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