callchain.c 8.0 KB

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