callchain.c 9.0 KB

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