callchain.c 12 KB

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