callchain.c 8.7 KB

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