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