callchain.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2009, 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 "callchain.h"
  16. #define chain_for_each_child(child, parent) \
  17. list_for_each_entry(child, &parent->children, brothers)
  18. static void
  19. rb_insert_callchain(struct rb_root *root, struct callchain_node *chain)
  20. {
  21. struct rb_node **p = &root->rb_node;
  22. struct rb_node *parent = NULL;
  23. struct callchain_node *rnode;
  24. while (*p) {
  25. parent = *p;
  26. rnode = rb_entry(parent, struct callchain_node, rb_node);
  27. if (rnode->hit < chain->hit)
  28. p = &(*p)->rb_left;
  29. else
  30. p = &(*p)->rb_right;
  31. }
  32. rb_link_node(&chain->rb_node, parent, p);
  33. rb_insert_color(&chain->rb_node, root);
  34. }
  35. /*
  36. * Once we get every callchains from the stream, we can now
  37. * sort them by hit
  38. */
  39. void sort_chain_to_rbtree(struct rb_root *rb_root, struct callchain_node *node)
  40. {
  41. struct callchain_node *child;
  42. chain_for_each_child(child, node)
  43. sort_chain_to_rbtree(rb_root, child);
  44. if (node->hit)
  45. rb_insert_callchain(rb_root, node);
  46. }
  47. /*
  48. * Create a child for a parent. If inherit_children, then the new child
  49. * will become the new parent of it's parent children
  50. */
  51. static struct callchain_node *
  52. create_child(struct callchain_node *parent, bool inherit_children)
  53. {
  54. struct callchain_node *new;
  55. new = malloc(sizeof(*new));
  56. if (!new) {
  57. perror("not enough memory to create child for code path tree");
  58. return NULL;
  59. }
  60. new->parent = parent;
  61. INIT_LIST_HEAD(&new->children);
  62. INIT_LIST_HEAD(&new->val);
  63. if (inherit_children) {
  64. struct callchain_node *next;
  65. list_splice(&parent->children, &new->children);
  66. INIT_LIST_HEAD(&parent->children);
  67. chain_for_each_child(next, new)
  68. next->parent = new;
  69. }
  70. list_add_tail(&new->brothers, &parent->children);
  71. return new;
  72. }
  73. /*
  74. * Fill the node with callchain values
  75. */
  76. static void
  77. fill_node(struct callchain_node *node, struct ip_callchain *chain,
  78. int start, struct symbol **syms)
  79. {
  80. unsigned int i;
  81. for (i = start; i < chain->nr; i++) {
  82. struct callchain_list *call;
  83. call = malloc(sizeof(*call));
  84. if (!call) {
  85. perror("not enough memory for the code path tree");
  86. return;
  87. }
  88. call->ip = chain->ips[i];
  89. call->sym = syms[i];
  90. list_add_tail(&call->list, &node->val);
  91. }
  92. node->val_nr = chain->nr - start;
  93. if (!node->val_nr)
  94. printf("Warning: empty node in callchain tree\n");
  95. }
  96. static void
  97. add_child(struct callchain_node *parent, struct ip_callchain *chain,
  98. int start, struct symbol **syms)
  99. {
  100. struct callchain_node *new;
  101. new = create_child(parent, false);
  102. fill_node(new, chain, start, syms);
  103. new->hit = 1;
  104. }
  105. /*
  106. * Split the parent in two parts (a new child is created) and
  107. * give a part of its callchain to the created child.
  108. * Then create another child to host the given callchain of new branch
  109. */
  110. static void
  111. split_add_child(struct callchain_node *parent, struct ip_callchain *chain,
  112. struct callchain_list *to_split, int idx_parents, int idx_local,
  113. struct symbol **syms)
  114. {
  115. struct callchain_node *new;
  116. struct list_head *old_tail;
  117. unsigned int idx_total = idx_parents + idx_local;
  118. /* split */
  119. new = create_child(parent, true);
  120. /* split the callchain and move a part to the new child */
  121. old_tail = parent->val.prev;
  122. list_del_range(&to_split->list, old_tail);
  123. new->val.next = &to_split->list;
  124. new->val.prev = old_tail;
  125. to_split->list.prev = &new->val;
  126. old_tail->next = &new->val;
  127. /* split the hits */
  128. new->hit = parent->hit;
  129. new->val_nr = parent->val_nr - idx_local;
  130. parent->val_nr = idx_local;
  131. /* create a new child for the new branch if any */
  132. if (idx_total < chain->nr) {
  133. parent->hit = 0;
  134. add_child(parent, chain, idx_total, syms);
  135. } else {
  136. parent->hit = 1;
  137. }
  138. }
  139. static int
  140. __append_chain(struct callchain_node *root, struct ip_callchain *chain,
  141. unsigned int start, struct symbol **syms);
  142. static void
  143. __append_chain_children(struct callchain_node *root, struct ip_callchain *chain,
  144. struct symbol **syms, unsigned int start)
  145. {
  146. struct callchain_node *rnode;
  147. /* lookup in childrens */
  148. chain_for_each_child(rnode, root) {
  149. unsigned int ret = __append_chain(rnode, chain, start, syms);
  150. if (!ret)
  151. return;
  152. }
  153. /* nothing in children, add to the current node */
  154. add_child(root, chain, start, syms);
  155. }
  156. static int
  157. __append_chain(struct callchain_node *root, struct ip_callchain *chain,
  158. unsigned int start, struct symbol **syms)
  159. {
  160. struct callchain_list *cnode;
  161. unsigned int i = start;
  162. bool found = false;
  163. /*
  164. * Lookup in the current node
  165. * If we have a symbol, then compare the start to match
  166. * anywhere inside a function.
  167. */
  168. list_for_each_entry(cnode, &root->val, list) {
  169. if (i == chain->nr)
  170. break;
  171. if (cnode->sym && syms[i]) {
  172. if (cnode->sym->start != syms[i]->start)
  173. break;
  174. } else if (cnode->ip != chain->ips[i])
  175. break;
  176. if (!found)
  177. found = true;
  178. i++;
  179. }
  180. /* matches not, relay on the parent */
  181. if (!found)
  182. return -1;
  183. /* we match only a part of the node. Split it and add the new chain */
  184. if (i - start < root->val_nr) {
  185. split_add_child(root, chain, cnode, start, i - start, syms);
  186. return 0;
  187. }
  188. /* we match 100% of the path, increment the hit */
  189. if (i - start == root->val_nr && i == chain->nr) {
  190. root->hit++;
  191. return 0;
  192. }
  193. /* We match the node and still have a part remaining */
  194. __append_chain_children(root, chain, syms, i);
  195. return 0;
  196. }
  197. void append_chain(struct callchain_node *root, struct ip_callchain *chain,
  198. struct symbol **syms)
  199. {
  200. __append_chain_children(root, chain, syms, 0);
  201. }