callchain.c 13 KB

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