callchain.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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, const 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, u64 period)
  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 = period;
  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. u64 period)
  214. {
  215. struct callchain_node *new;
  216. struct list_head *old_tail;
  217. unsigned int idx_total = idx_parents + idx_local;
  218. /* split */
  219. new = create_child(parent, true);
  220. /* split the callchain and move a part to the new child */
  221. old_tail = parent->val.prev;
  222. list_del_range(&to_split->list, old_tail);
  223. new->val.next = &to_split->list;
  224. new->val.prev = old_tail;
  225. to_split->list.prev = &new->val;
  226. old_tail->next = &new->val;
  227. /* split the hits */
  228. new->hit = parent->hit;
  229. new->children_hit = parent->children_hit;
  230. parent->children_hit = cumul_hits(new);
  231. new->val_nr = parent->val_nr - idx_local;
  232. parent->val_nr = idx_local;
  233. /* create a new child for the new branch if any */
  234. if (idx_total < chain->nr) {
  235. parent->hit = 0;
  236. add_child(parent, chain, idx_total, period);
  237. parent->children_hit += period;
  238. } else {
  239. parent->hit = period;
  240. }
  241. }
  242. static int
  243. __append_chain(struct callchain_node *root, struct resolved_chain *chain,
  244. unsigned int start, u64 period);
  245. static void
  246. __append_chain_children(struct callchain_node *root,
  247. struct resolved_chain *chain,
  248. unsigned int start, u64 period)
  249. {
  250. struct callchain_node *rnode;
  251. /* lookup in childrens */
  252. chain_for_each_child(rnode, root) {
  253. unsigned int ret = __append_chain(rnode, chain, start, period);
  254. if (!ret)
  255. goto inc_children_hit;
  256. }
  257. /* nothing in children, add to the current node */
  258. add_child(root, chain, start, period);
  259. inc_children_hit:
  260. root->children_hit += period;
  261. }
  262. static int
  263. __append_chain(struct callchain_node *root, struct resolved_chain *chain,
  264. unsigned int start, u64 period)
  265. {
  266. struct callchain_list *cnode;
  267. unsigned int i = start;
  268. bool found = false;
  269. /*
  270. * Lookup in the current node
  271. * If we have a symbol, then compare the start to match
  272. * anywhere inside a function.
  273. */
  274. list_for_each_entry(cnode, &root->val, list) {
  275. struct symbol *sym;
  276. if (i == chain->nr)
  277. break;
  278. sym = chain->ips[i].ms.sym;
  279. if (cnode->ms.sym && sym) {
  280. if (cnode->ms.sym->start != sym->start)
  281. break;
  282. } else if (cnode->ip != chain->ips[i].ip)
  283. break;
  284. if (!found)
  285. found = true;
  286. i++;
  287. }
  288. /* matches not, relay on the parent */
  289. if (!found)
  290. return -1;
  291. /* we match only a part of the node. Split it and add the new chain */
  292. if (i - start < root->val_nr) {
  293. split_add_child(root, chain, cnode, start, i - start, period);
  294. return 0;
  295. }
  296. /* we match 100% of the path, increment the hit */
  297. if (i - start == root->val_nr && i == chain->nr) {
  298. root->hit += period;
  299. return 0;
  300. }
  301. /* We match the node and still have a part remaining */
  302. __append_chain_children(root, chain, i, period);
  303. return 0;
  304. }
  305. static void filter_context(struct ip_callchain *old, struct resolved_chain *new,
  306. struct map_symbol *syms)
  307. {
  308. int i, j = 0;
  309. for (i = 0; i < (int)old->nr; i++) {
  310. if (old->ips[i] >= PERF_CONTEXT_MAX)
  311. continue;
  312. new->ips[j].ip = old->ips[i];
  313. new->ips[j].ms = syms[i];
  314. j++;
  315. }
  316. new->nr = j;
  317. }
  318. int append_chain(struct callchain_node *root, struct ip_callchain *chain,
  319. struct map_symbol *syms, u64 period)
  320. {
  321. struct resolved_chain *filtered;
  322. if (!chain->nr)
  323. return 0;
  324. filtered = zalloc(sizeof(*filtered) +
  325. chain->nr * sizeof(struct resolved_ip));
  326. if (!filtered)
  327. return -ENOMEM;
  328. filter_context(chain, filtered, syms);
  329. if (!filtered->nr)
  330. goto end;
  331. __append_chain_children(root, filtered, 0, period);
  332. end:
  333. free(filtered);
  334. return 0;
  335. }