callchain.c 10 KB

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