callchain.c 7.8 KB

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