callchain.c 8.9 KB

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