callchain.c 11 KB

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