callchain.c 10 KB

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