callchain.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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, 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, siblings)
  26. #define chain_for_each_child_safe(child, next, parent) \
  27. list_for_each_entry_safe(child, next, &parent->children, siblings)
  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 = callchain_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 = callchain_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 (callchain_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 (callchain_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 callchain_register_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->siblings, &parent->children);
  165. return new;
  166. }
  167. /*
  168. * Fill the node with callchain values
  169. */
  170. static void
  171. fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
  172. {
  173. struct callchain_cursor_node *cursor_node;
  174. node->val_nr = cursor->nr - cursor->pos;
  175. if (!node->val_nr)
  176. pr_warning("Warning: empty node in callchain tree\n");
  177. cursor_node = callchain_cursor_current(cursor);
  178. while (cursor_node) {
  179. struct callchain_list *call;
  180. call = zalloc(sizeof(*call));
  181. if (!call) {
  182. perror("not enough memory for the code path tree");
  183. return;
  184. }
  185. call->ip = cursor_node->ip;
  186. call->ms.sym = cursor_node->sym;
  187. call->ms.map = cursor_node->map;
  188. list_add_tail(&call->list, &node->val);
  189. callchain_cursor_advance(cursor);
  190. cursor_node = callchain_cursor_current(cursor);
  191. }
  192. }
  193. static void
  194. add_child(struct callchain_node *parent,
  195. struct callchain_cursor *cursor,
  196. u64 period)
  197. {
  198. struct callchain_node *new;
  199. new = create_child(parent, false);
  200. fill_node(new, cursor);
  201. new->children_hit = 0;
  202. new->hit = period;
  203. }
  204. /*
  205. * Split the parent in two parts (a new child is created) and
  206. * give a part of its callchain to the created child.
  207. * Then create another child to host the given callchain of new branch
  208. */
  209. static void
  210. split_add_child(struct callchain_node *parent,
  211. struct callchain_cursor *cursor,
  212. struct callchain_list *to_split,
  213. u64 idx_parents, u64 idx_local, u64 period)
  214. {
  215. struct callchain_node *new;
  216. struct list_head *old_tail;
  217. unsigned int idx_total = idx_parents + idx_local;
  218. /* split */
  219. new = create_child(parent, true);
  220. /* split the callchain and move a part to the new child */
  221. old_tail = parent->val.prev;
  222. list_del_range(&to_split->list, old_tail);
  223. new->val.next = &to_split->list;
  224. new->val.prev = old_tail;
  225. to_split->list.prev = &new->val;
  226. old_tail->next = &new->val;
  227. /* split the hits */
  228. new->hit = parent->hit;
  229. new->children_hit = parent->children_hit;
  230. parent->children_hit = callchain_cumul_hits(new);
  231. new->val_nr = parent->val_nr - idx_local;
  232. parent->val_nr = idx_local;
  233. /* create a new child for the new branch if any */
  234. if (idx_total < cursor->nr) {
  235. parent->hit = 0;
  236. add_child(parent, cursor, period);
  237. parent->children_hit += period;
  238. } else {
  239. parent->hit = period;
  240. }
  241. }
  242. static int
  243. append_chain(struct callchain_node *root,
  244. struct callchain_cursor *cursor,
  245. u64 period);
  246. static void
  247. append_chain_children(struct callchain_node *root,
  248. struct callchain_cursor *cursor,
  249. 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, cursor, period);
  255. if (!ret)
  256. goto inc_children_hit;
  257. }
  258. /* nothing in children, add to the current node */
  259. add_child(root, cursor, period);
  260. inc_children_hit:
  261. root->children_hit += period;
  262. }
  263. static int
  264. append_chain(struct callchain_node *root,
  265. struct callchain_cursor *cursor,
  266. u64 period)
  267. {
  268. struct callchain_cursor_node *curr_snap = cursor->curr;
  269. struct callchain_list *cnode;
  270. u64 start = cursor->pos;
  271. bool found = false;
  272. u64 matches;
  273. /*
  274. * Lookup in the current node
  275. * If we have a symbol, then compare the start to match
  276. * anywhere inside a function.
  277. */
  278. list_for_each_entry(cnode, &root->val, list) {
  279. struct callchain_cursor_node *node;
  280. struct symbol *sym;
  281. node = callchain_cursor_current(cursor);
  282. if (!node)
  283. break;
  284. sym = node->sym;
  285. if (cnode->ms.sym && sym) {
  286. if (cnode->ms.sym->start != sym->start)
  287. break;
  288. } else if (cnode->ip != node->ip)
  289. break;
  290. if (!found)
  291. found = true;
  292. callchain_cursor_advance(cursor);
  293. }
  294. /* matches not, relay on the parent */
  295. if (!found) {
  296. cursor->curr = curr_snap;
  297. cursor->pos = start;
  298. return -1;
  299. }
  300. matches = cursor->pos - start;
  301. /* we match only a part of the node. Split it and add the new chain */
  302. if (matches < root->val_nr) {
  303. split_add_child(root, cursor, cnode, start, matches, period);
  304. return 0;
  305. }
  306. /* we match 100% of the path, increment the hit */
  307. if (matches == root->val_nr && cursor->pos == cursor->nr) {
  308. root->hit += period;
  309. return 0;
  310. }
  311. /* We match the node and still have a part remaining */
  312. append_chain_children(root, cursor, period);
  313. return 0;
  314. }
  315. int callchain_append(struct callchain_root *root,
  316. struct callchain_cursor *cursor,
  317. u64 period)
  318. {
  319. if (!cursor->nr)
  320. return 0;
  321. callchain_cursor_commit(cursor);
  322. append_chain_children(&root->node, cursor, period);
  323. if (cursor->nr > root->max_depth)
  324. root->max_depth = cursor->nr;
  325. return 0;
  326. }
  327. static int
  328. merge_chain_branch(struct callchain_cursor *cursor,
  329. struct callchain_node *dst, struct callchain_node *src)
  330. {
  331. struct callchain_cursor_node **old_last = cursor->last;
  332. struct callchain_node *child, *next_child;
  333. struct callchain_list *list, *next_list;
  334. int old_pos = cursor->nr;
  335. int err = 0;
  336. list_for_each_entry_safe(list, next_list, &src->val, list) {
  337. callchain_cursor_append(cursor, list->ip,
  338. list->ms.map, list->ms.sym);
  339. list_del(&list->list);
  340. free(list);
  341. }
  342. if (src->hit) {
  343. callchain_cursor_commit(cursor);
  344. append_chain_children(dst, cursor, src->hit);
  345. }
  346. chain_for_each_child_safe(child, next_child, src) {
  347. err = merge_chain_branch(cursor, dst, child);
  348. if (err)
  349. break;
  350. list_del(&child->siblings);
  351. free(child);
  352. }
  353. cursor->nr = old_pos;
  354. cursor->last = old_last;
  355. return err;
  356. }
  357. int callchain_merge(struct callchain_cursor *cursor,
  358. struct callchain_root *dst, struct callchain_root *src)
  359. {
  360. return merge_chain_branch(cursor, &dst->node, &src->node);
  361. }
  362. int callchain_cursor_append(struct callchain_cursor *cursor,
  363. u64 ip, struct map *map, struct symbol *sym)
  364. {
  365. struct callchain_cursor_node *node = *cursor->last;
  366. if (!node) {
  367. node = calloc(sizeof(*node), 1);
  368. if (!node)
  369. return -ENOMEM;
  370. *cursor->last = node;
  371. }
  372. node->ip = ip;
  373. node->map = map;
  374. node->sym = sym;
  375. cursor->nr++;
  376. cursor->last = &node->next;
  377. return 0;
  378. }