callchain.c 8.1 KB

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