callchain.c 10 KB

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