callchain.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. int
  24. parse_callchain_report_opt(const char *arg)
  25. {
  26. char *tok, *tok2;
  27. char *endptr;
  28. symbol_conf.use_callchain = true;
  29. if (!arg)
  30. return 0;
  31. tok = strtok((char *)arg, ",");
  32. if (!tok)
  33. return -1;
  34. /* get the output mode */
  35. if (!strncmp(tok, "graph", strlen(arg))) {
  36. callchain_param.mode = CHAIN_GRAPH_ABS;
  37. } else if (!strncmp(tok, "flat", strlen(arg))) {
  38. callchain_param.mode = CHAIN_FLAT;
  39. } else if (!strncmp(tok, "fractal", strlen(arg))) {
  40. callchain_param.mode = CHAIN_GRAPH_REL;
  41. } else if (!strncmp(tok, "none", strlen(arg))) {
  42. callchain_param.mode = CHAIN_NONE;
  43. symbol_conf.use_callchain = false;
  44. return 0;
  45. } else {
  46. return -1;
  47. }
  48. /* get the min percentage */
  49. tok = strtok(NULL, ",");
  50. if (!tok)
  51. goto setup;
  52. callchain_param.min_percent = strtod(tok, &endptr);
  53. if (tok == endptr)
  54. return -1;
  55. /* get the print limit */
  56. tok2 = strtok(NULL, ",");
  57. if (!tok2)
  58. goto setup;
  59. if (tok2[0] != 'c') {
  60. callchain_param.print_limit = strtoul(tok2, &endptr, 0);
  61. tok2 = strtok(NULL, ",");
  62. if (!tok2)
  63. goto setup;
  64. }
  65. /* get the call chain order */
  66. if (!strncmp(tok2, "caller", strlen("caller")))
  67. callchain_param.order = ORDER_CALLER;
  68. else if (!strncmp(tok2, "callee", strlen("callee")))
  69. callchain_param.order = ORDER_CALLEE;
  70. else
  71. return -1;
  72. /* Get the sort key */
  73. tok2 = strtok(NULL, ",");
  74. if (!tok2)
  75. goto setup;
  76. if (!strncmp(tok2, "function", strlen("function")))
  77. callchain_param.key = CCKEY_FUNCTION;
  78. else if (!strncmp(tok2, "address", strlen("address")))
  79. callchain_param.key = CCKEY_ADDRESS;
  80. else
  81. return -1;
  82. setup:
  83. if (callchain_register_param(&callchain_param) < 0) {
  84. pr_err("Can't register callchain params\n");
  85. return -1;
  86. }
  87. return 0;
  88. }
  89. static void
  90. rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
  91. enum chain_mode mode)
  92. {
  93. struct rb_node **p = &root->rb_node;
  94. struct rb_node *parent = NULL;
  95. struct callchain_node *rnode;
  96. u64 chain_cumul = callchain_cumul_hits(chain);
  97. while (*p) {
  98. u64 rnode_cumul;
  99. parent = *p;
  100. rnode = rb_entry(parent, struct callchain_node, rb_node);
  101. rnode_cumul = callchain_cumul_hits(rnode);
  102. switch (mode) {
  103. case CHAIN_FLAT:
  104. if (rnode->hit < chain->hit)
  105. p = &(*p)->rb_left;
  106. else
  107. p = &(*p)->rb_right;
  108. break;
  109. case CHAIN_GRAPH_ABS: /* Falldown */
  110. case CHAIN_GRAPH_REL:
  111. if (rnode_cumul < chain_cumul)
  112. p = &(*p)->rb_left;
  113. else
  114. p = &(*p)->rb_right;
  115. break;
  116. case CHAIN_NONE:
  117. default:
  118. break;
  119. }
  120. }
  121. rb_link_node(&chain->rb_node, parent, p);
  122. rb_insert_color(&chain->rb_node, root);
  123. }
  124. static void
  125. __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
  126. u64 min_hit)
  127. {
  128. struct rb_node *n;
  129. struct callchain_node *child;
  130. n = rb_first(&node->rb_root_in);
  131. while (n) {
  132. child = rb_entry(n, struct callchain_node, rb_node_in);
  133. n = rb_next(n);
  134. __sort_chain_flat(rb_root, child, min_hit);
  135. }
  136. if (node->hit && node->hit >= min_hit)
  137. rb_insert_callchain(rb_root, node, CHAIN_FLAT);
  138. }
  139. /*
  140. * Once we get every callchains from the stream, we can now
  141. * sort them by hit
  142. */
  143. static void
  144. sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
  145. u64 min_hit, struct callchain_param *param __maybe_unused)
  146. {
  147. __sort_chain_flat(rb_root, &root->node, min_hit);
  148. }
  149. static void __sort_chain_graph_abs(struct callchain_node *node,
  150. u64 min_hit)
  151. {
  152. struct rb_node *n;
  153. struct callchain_node *child;
  154. node->rb_root = RB_ROOT;
  155. n = rb_first(&node->rb_root_in);
  156. while (n) {
  157. child = rb_entry(n, struct callchain_node, rb_node_in);
  158. n = rb_next(n);
  159. __sort_chain_graph_abs(child, min_hit);
  160. if (callchain_cumul_hits(child) >= min_hit)
  161. rb_insert_callchain(&node->rb_root, child,
  162. CHAIN_GRAPH_ABS);
  163. }
  164. }
  165. static void
  166. sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
  167. u64 min_hit, struct callchain_param *param __maybe_unused)
  168. {
  169. __sort_chain_graph_abs(&chain_root->node, min_hit);
  170. rb_root->rb_node = chain_root->node.rb_root.rb_node;
  171. }
  172. static void __sort_chain_graph_rel(struct callchain_node *node,
  173. double min_percent)
  174. {
  175. struct rb_node *n;
  176. struct callchain_node *child;
  177. u64 min_hit;
  178. node->rb_root = RB_ROOT;
  179. min_hit = ceil(node->children_hit * min_percent);
  180. n = rb_first(&node->rb_root_in);
  181. while (n) {
  182. child = rb_entry(n, struct callchain_node, rb_node_in);
  183. n = rb_next(n);
  184. __sort_chain_graph_rel(child, min_percent);
  185. if (callchain_cumul_hits(child) >= min_hit)
  186. rb_insert_callchain(&node->rb_root, child,
  187. CHAIN_GRAPH_REL);
  188. }
  189. }
  190. static void
  191. sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
  192. u64 min_hit __maybe_unused, struct callchain_param *param)
  193. {
  194. __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
  195. rb_root->rb_node = chain_root->node.rb_root.rb_node;
  196. }
  197. int callchain_register_param(struct callchain_param *param)
  198. {
  199. switch (param->mode) {
  200. case CHAIN_GRAPH_ABS:
  201. param->sort = sort_chain_graph_abs;
  202. break;
  203. case CHAIN_GRAPH_REL:
  204. param->sort = sort_chain_graph_rel;
  205. break;
  206. case CHAIN_FLAT:
  207. param->sort = sort_chain_flat;
  208. break;
  209. case CHAIN_NONE:
  210. default:
  211. return -1;
  212. }
  213. return 0;
  214. }
  215. /*
  216. * Create a child for a parent. If inherit_children, then the new child
  217. * will become the new parent of it's parent children
  218. */
  219. static struct callchain_node *
  220. create_child(struct callchain_node *parent, bool inherit_children)
  221. {
  222. struct callchain_node *new;
  223. new = zalloc(sizeof(*new));
  224. if (!new) {
  225. perror("not enough memory to create child for code path tree");
  226. return NULL;
  227. }
  228. new->parent = parent;
  229. INIT_LIST_HEAD(&new->val);
  230. if (inherit_children) {
  231. struct rb_node *n;
  232. struct callchain_node *child;
  233. new->rb_root_in = parent->rb_root_in;
  234. parent->rb_root_in = RB_ROOT;
  235. n = rb_first(&new->rb_root_in);
  236. while (n) {
  237. child = rb_entry(n, struct callchain_node, rb_node_in);
  238. child->parent = new;
  239. n = rb_next(n);
  240. }
  241. /* make it the first child */
  242. rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
  243. rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
  244. }
  245. return new;
  246. }
  247. /*
  248. * Fill the node with callchain values
  249. */
  250. static void
  251. fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
  252. {
  253. struct callchain_cursor_node *cursor_node;
  254. node->val_nr = cursor->nr - cursor->pos;
  255. if (!node->val_nr)
  256. pr_warning("Warning: empty node in callchain tree\n");
  257. cursor_node = callchain_cursor_current(cursor);
  258. while (cursor_node) {
  259. struct callchain_list *call;
  260. call = zalloc(sizeof(*call));
  261. if (!call) {
  262. perror("not enough memory for the code path tree");
  263. return;
  264. }
  265. call->ip = cursor_node->ip;
  266. call->ms.sym = cursor_node->sym;
  267. call->ms.map = cursor_node->map;
  268. list_add_tail(&call->list, &node->val);
  269. callchain_cursor_advance(cursor);
  270. cursor_node = callchain_cursor_current(cursor);
  271. }
  272. }
  273. static struct callchain_node *
  274. add_child(struct callchain_node *parent,
  275. struct callchain_cursor *cursor,
  276. u64 period)
  277. {
  278. struct callchain_node *new;
  279. new = create_child(parent, false);
  280. fill_node(new, cursor);
  281. new->children_hit = 0;
  282. new->hit = period;
  283. return new;
  284. }
  285. static s64 match_chain(struct callchain_cursor_node *node,
  286. struct callchain_list *cnode)
  287. {
  288. struct symbol *sym = node->sym;
  289. if (cnode->ms.sym && sym &&
  290. callchain_param.key == CCKEY_FUNCTION)
  291. return cnode->ms.sym->start - sym->start;
  292. else
  293. return cnode->ip - node->ip;
  294. }
  295. /*
  296. * Split the parent in two parts (a new child is created) and
  297. * give a part of its callchain to the created child.
  298. * Then create another child to host the given callchain of new branch
  299. */
  300. static void
  301. split_add_child(struct callchain_node *parent,
  302. struct callchain_cursor *cursor,
  303. struct callchain_list *to_split,
  304. u64 idx_parents, u64 idx_local, u64 period)
  305. {
  306. struct callchain_node *new;
  307. struct list_head *old_tail;
  308. unsigned int idx_total = idx_parents + idx_local;
  309. /* split */
  310. new = create_child(parent, true);
  311. /* split the callchain and move a part to the new child */
  312. old_tail = parent->val.prev;
  313. list_del_range(&to_split->list, old_tail);
  314. new->val.next = &to_split->list;
  315. new->val.prev = old_tail;
  316. to_split->list.prev = &new->val;
  317. old_tail->next = &new->val;
  318. /* split the hits */
  319. new->hit = parent->hit;
  320. new->children_hit = parent->children_hit;
  321. parent->children_hit = callchain_cumul_hits(new);
  322. new->val_nr = parent->val_nr - idx_local;
  323. parent->val_nr = idx_local;
  324. /* create a new child for the new branch if any */
  325. if (idx_total < cursor->nr) {
  326. struct callchain_node *first;
  327. struct callchain_list *cnode;
  328. struct callchain_cursor_node *node;
  329. struct rb_node *p, **pp;
  330. parent->hit = 0;
  331. parent->children_hit += period;
  332. node = callchain_cursor_current(cursor);
  333. new = add_child(parent, cursor, period);
  334. /*
  335. * This is second child since we moved parent's children
  336. * to new (first) child above.
  337. */
  338. p = parent->rb_root_in.rb_node;
  339. first = rb_entry(p, struct callchain_node, rb_node_in);
  340. cnode = list_first_entry(&first->val, struct callchain_list,
  341. list);
  342. if (match_chain(node, cnode) < 0)
  343. pp = &p->rb_left;
  344. else
  345. pp = &p->rb_right;
  346. rb_link_node(&new->rb_node_in, p, pp);
  347. rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
  348. } else {
  349. parent->hit = period;
  350. }
  351. }
  352. static int
  353. append_chain(struct callchain_node *root,
  354. struct callchain_cursor *cursor,
  355. u64 period);
  356. static void
  357. append_chain_children(struct callchain_node *root,
  358. struct callchain_cursor *cursor,
  359. u64 period)
  360. {
  361. struct callchain_node *rnode;
  362. struct callchain_cursor_node *node;
  363. struct rb_node **p = &root->rb_root_in.rb_node;
  364. struct rb_node *parent = NULL;
  365. node = callchain_cursor_current(cursor);
  366. if (!node)
  367. return;
  368. /* lookup in childrens */
  369. while (*p) {
  370. s64 ret;
  371. parent = *p;
  372. rnode = rb_entry(parent, struct callchain_node, rb_node_in);
  373. /* If at least first entry matches, rely to children */
  374. ret = append_chain(rnode, cursor, period);
  375. if (ret == 0)
  376. goto inc_children_hit;
  377. if (ret < 0)
  378. p = &parent->rb_left;
  379. else
  380. p = &parent->rb_right;
  381. }
  382. /* nothing in children, add to the current node */
  383. rnode = add_child(root, cursor, period);
  384. rb_link_node(&rnode->rb_node_in, parent, p);
  385. rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
  386. inc_children_hit:
  387. root->children_hit += period;
  388. }
  389. static int
  390. append_chain(struct callchain_node *root,
  391. struct callchain_cursor *cursor,
  392. u64 period)
  393. {
  394. struct callchain_list *cnode;
  395. u64 start = cursor->pos;
  396. bool found = false;
  397. u64 matches;
  398. int cmp = 0;
  399. /*
  400. * Lookup in the current node
  401. * If we have a symbol, then compare the start to match
  402. * anywhere inside a function, unless function
  403. * mode is disabled.
  404. */
  405. list_for_each_entry(cnode, &root->val, list) {
  406. struct callchain_cursor_node *node;
  407. node = callchain_cursor_current(cursor);
  408. if (!node)
  409. break;
  410. cmp = match_chain(node, cnode);
  411. if (cmp)
  412. break;
  413. found = true;
  414. callchain_cursor_advance(cursor);
  415. }
  416. /* matches not, relay no the parent */
  417. if (!found) {
  418. WARN_ONCE(!cmp, "Chain comparison error\n");
  419. return cmp;
  420. }
  421. matches = cursor->pos - start;
  422. /* we match only a part of the node. Split it and add the new chain */
  423. if (matches < root->val_nr) {
  424. split_add_child(root, cursor, cnode, start, matches, period);
  425. return 0;
  426. }
  427. /* we match 100% of the path, increment the hit */
  428. if (matches == root->val_nr && cursor->pos == cursor->nr) {
  429. root->hit += period;
  430. return 0;
  431. }
  432. /* We match the node and still have a part remaining */
  433. append_chain_children(root, cursor, period);
  434. return 0;
  435. }
  436. int callchain_append(struct callchain_root *root,
  437. struct callchain_cursor *cursor,
  438. u64 period)
  439. {
  440. if (!cursor->nr)
  441. return 0;
  442. callchain_cursor_commit(cursor);
  443. append_chain_children(&root->node, cursor, period);
  444. if (cursor->nr > root->max_depth)
  445. root->max_depth = cursor->nr;
  446. return 0;
  447. }
  448. static int
  449. merge_chain_branch(struct callchain_cursor *cursor,
  450. struct callchain_node *dst, struct callchain_node *src)
  451. {
  452. struct callchain_cursor_node **old_last = cursor->last;
  453. struct callchain_node *child;
  454. struct callchain_list *list, *next_list;
  455. struct rb_node *n;
  456. int old_pos = cursor->nr;
  457. int err = 0;
  458. list_for_each_entry_safe(list, next_list, &src->val, list) {
  459. callchain_cursor_append(cursor, list->ip,
  460. list->ms.map, list->ms.sym);
  461. list_del(&list->list);
  462. free(list);
  463. }
  464. if (src->hit) {
  465. callchain_cursor_commit(cursor);
  466. append_chain_children(dst, cursor, src->hit);
  467. }
  468. n = rb_first(&src->rb_root_in);
  469. while (n) {
  470. child = container_of(n, struct callchain_node, rb_node_in);
  471. n = rb_next(n);
  472. rb_erase(&child->rb_node_in, &src->rb_root_in);
  473. err = merge_chain_branch(cursor, dst, child);
  474. if (err)
  475. break;
  476. free(child);
  477. }
  478. cursor->nr = old_pos;
  479. cursor->last = old_last;
  480. return err;
  481. }
  482. int callchain_merge(struct callchain_cursor *cursor,
  483. struct callchain_root *dst, struct callchain_root *src)
  484. {
  485. return merge_chain_branch(cursor, &dst->node, &src->node);
  486. }
  487. int callchain_cursor_append(struct callchain_cursor *cursor,
  488. u64 ip, struct map *map, struct symbol *sym)
  489. {
  490. struct callchain_cursor_node *node = *cursor->last;
  491. if (!node) {
  492. node = calloc(1, sizeof(*node));
  493. if (!node)
  494. return -ENOMEM;
  495. *cursor->last = node;
  496. }
  497. node->ip = ip;
  498. node->map = map;
  499. node->sym = sym;
  500. cursor->nr++;
  501. cursor->last = &node->next;
  502. return 0;
  503. }
  504. int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
  505. struct perf_evsel *evsel, struct addr_location *al,
  506. int max_stack)
  507. {
  508. if (sample->callchain == NULL)
  509. return 0;
  510. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
  511. sort__has_parent) {
  512. return machine__resolve_callchain(al->machine, evsel, al->thread,
  513. sample, parent, al, max_stack);
  514. }
  515. return 0;
  516. }
  517. int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
  518. {
  519. if (!symbol_conf.use_callchain)
  520. return 0;
  521. return callchain_append(he->callchain, &callchain_cursor, sample->period);
  522. }
  523. int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
  524. bool hide_unresolved)
  525. {
  526. al->map = node->map;
  527. al->sym = node->sym;
  528. if (node->map)
  529. al->addr = node->map->map_ip(node->map, node->ip);
  530. else
  531. al->addr = node->ip;
  532. if (al->sym == NULL) {
  533. if (hide_unresolved)
  534. return 0;
  535. if (al->map == NULL)
  536. goto out;
  537. }
  538. if (al->map->groups == &al->machine->kmaps) {
  539. if (machine__is_host(al->machine)) {
  540. al->cpumode = PERF_RECORD_MISC_KERNEL;
  541. al->level = 'k';
  542. } else {
  543. al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
  544. al->level = 'g';
  545. }
  546. } else {
  547. if (machine__is_host(al->machine)) {
  548. al->cpumode = PERF_RECORD_MISC_USER;
  549. al->level = '.';
  550. } else if (perf_guest) {
  551. al->cpumode = PERF_RECORD_MISC_GUEST_USER;
  552. al->level = 'u';
  553. } else {
  554. al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
  555. al->level = 'H';
  556. }
  557. }
  558. out:
  559. return 1;
  560. }