callchain.c 11 KB

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