hist.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. #include "annotate.h"
  2. #include "util.h"
  3. #include "build-id.h"
  4. #include "hist.h"
  5. #include "session.h"
  6. #include "sort.h"
  7. #include <math.h>
  8. static bool hists__filter_entry_by_dso(struct hists *hists,
  9. struct hist_entry *he);
  10. static bool hists__filter_entry_by_thread(struct hists *hists,
  11. struct hist_entry *he);
  12. static bool hists__filter_entry_by_symbol(struct hists *hists,
  13. struct hist_entry *he);
  14. enum hist_filter {
  15. HIST_FILTER__DSO,
  16. HIST_FILTER__THREAD,
  17. HIST_FILTER__PARENT,
  18. HIST_FILTER__SYMBOL,
  19. };
  20. struct callchain_param callchain_param = {
  21. .mode = CHAIN_GRAPH_REL,
  22. .min_percent = 0.5,
  23. .order = ORDER_CALLEE
  24. };
  25. u16 hists__col_len(struct hists *hists, enum hist_column col)
  26. {
  27. return hists->col_len[col];
  28. }
  29. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
  30. {
  31. hists->col_len[col] = len;
  32. }
  33. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
  34. {
  35. if (len > hists__col_len(hists, col)) {
  36. hists__set_col_len(hists, col, len);
  37. return true;
  38. }
  39. return false;
  40. }
  41. void hists__reset_col_len(struct hists *hists)
  42. {
  43. enum hist_column col;
  44. for (col = 0; col < HISTC_NR_COLS; ++col)
  45. hists__set_col_len(hists, col, 0);
  46. }
  47. static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
  48. {
  49. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  50. if (hists__col_len(hists, dso) < unresolved_col_width &&
  51. !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  52. !symbol_conf.dso_list)
  53. hists__set_col_len(hists, dso, unresolved_col_width);
  54. }
  55. void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
  56. {
  57. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  58. u16 len;
  59. if (h->ms.sym)
  60. hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
  61. else
  62. hists__set_unres_dso_col_len(hists, HISTC_DSO);
  63. len = thread__comm_len(h->thread);
  64. if (hists__new_col_len(hists, HISTC_COMM, len))
  65. hists__set_col_len(hists, HISTC_THREAD, len + 6);
  66. if (h->ms.map) {
  67. len = dso__name_len(h->ms.map->dso);
  68. hists__new_col_len(hists, HISTC_DSO, len);
  69. }
  70. if (h->parent)
  71. hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
  72. if (h->branch_info) {
  73. int symlen;
  74. /*
  75. * +4 accounts for '[x] ' priv level info
  76. * +2 account of 0x prefix on raw addresses
  77. */
  78. if (h->branch_info->from.sym) {
  79. symlen = (int)h->branch_info->from.sym->namelen + 4;
  80. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  81. symlen = dso__name_len(h->branch_info->from.map->dso);
  82. hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
  83. } else {
  84. symlen = unresolved_col_width + 4 + 2;
  85. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  86. hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
  87. }
  88. if (h->branch_info->to.sym) {
  89. symlen = (int)h->branch_info->to.sym->namelen + 4;
  90. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  91. symlen = dso__name_len(h->branch_info->to.map->dso);
  92. hists__new_col_len(hists, HISTC_DSO_TO, symlen);
  93. } else {
  94. symlen = unresolved_col_width + 4 + 2;
  95. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  96. hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
  97. }
  98. }
  99. }
  100. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  101. {
  102. struct rb_node *next = rb_first(&hists->entries);
  103. struct hist_entry *n;
  104. int row = 0;
  105. hists__reset_col_len(hists);
  106. while (next && row++ < max_rows) {
  107. n = rb_entry(next, struct hist_entry, rb_node);
  108. if (!n->filtered)
  109. hists__calc_col_len(hists, n);
  110. next = rb_next(&n->rb_node);
  111. }
  112. }
  113. static void hist_entry__add_cpumode_period(struct hist_entry *he,
  114. unsigned int cpumode, u64 period)
  115. {
  116. switch (cpumode) {
  117. case PERF_RECORD_MISC_KERNEL:
  118. he->stat.period_sys += period;
  119. break;
  120. case PERF_RECORD_MISC_USER:
  121. he->stat.period_us += period;
  122. break;
  123. case PERF_RECORD_MISC_GUEST_KERNEL:
  124. he->stat.period_guest_sys += period;
  125. break;
  126. case PERF_RECORD_MISC_GUEST_USER:
  127. he->stat.period_guest_us += period;
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. static void he_stat__add_period(struct he_stat *he_stat, u64 period)
  134. {
  135. he_stat->period += period;
  136. he_stat->nr_events += 1;
  137. }
  138. static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
  139. {
  140. dest->period += src->period;
  141. dest->period_sys += src->period_sys;
  142. dest->period_us += src->period_us;
  143. dest->period_guest_sys += src->period_guest_sys;
  144. dest->period_guest_us += src->period_guest_us;
  145. dest->nr_events += src->nr_events;
  146. }
  147. static void hist_entry__decay(struct hist_entry *he)
  148. {
  149. he->stat.period = (he->stat.period * 7) / 8;
  150. he->stat.nr_events = (he->stat.nr_events * 7) / 8;
  151. }
  152. static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
  153. {
  154. u64 prev_period = he->stat.period;
  155. if (prev_period == 0)
  156. return true;
  157. hist_entry__decay(he);
  158. if (!he->filtered)
  159. hists->stats.total_period -= prev_period - he->stat.period;
  160. return he->stat.period == 0;
  161. }
  162. static void __hists__decay_entries(struct hists *hists, bool zap_user,
  163. bool zap_kernel, bool threaded)
  164. {
  165. struct rb_node *next = rb_first(&hists->entries);
  166. struct hist_entry *n;
  167. while (next) {
  168. n = rb_entry(next, struct hist_entry, rb_node);
  169. next = rb_next(&n->rb_node);
  170. /*
  171. * We may be annotating this, for instance, so keep it here in
  172. * case some it gets new samples, we'll eventually free it when
  173. * the user stops browsing and it agains gets fully decayed.
  174. */
  175. if (((zap_user && n->level == '.') ||
  176. (zap_kernel && n->level != '.') ||
  177. hists__decay_entry(hists, n)) &&
  178. !n->used) {
  179. rb_erase(&n->rb_node, &hists->entries);
  180. if (sort__need_collapse || threaded)
  181. rb_erase(&n->rb_node_in, &hists->entries_collapsed);
  182. hist_entry__free(n);
  183. --hists->nr_entries;
  184. }
  185. }
  186. }
  187. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  188. {
  189. return __hists__decay_entries(hists, zap_user, zap_kernel, false);
  190. }
  191. void hists__decay_entries_threaded(struct hists *hists,
  192. bool zap_user, bool zap_kernel)
  193. {
  194. return __hists__decay_entries(hists, zap_user, zap_kernel, true);
  195. }
  196. /*
  197. * histogram, sorted on item, collects periods
  198. */
  199. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  200. {
  201. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  202. struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
  203. if (he != NULL) {
  204. *he = *template;
  205. if (he->ms.map)
  206. he->ms.map->referenced = true;
  207. if (symbol_conf.use_callchain)
  208. callchain_init(he->callchain);
  209. INIT_LIST_HEAD(&he->pairs.node);
  210. }
  211. return he;
  212. }
  213. void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  214. {
  215. if (!h->filtered) {
  216. hists__calc_col_len(hists, h);
  217. ++hists->nr_entries;
  218. hists->stats.total_period += h->stat.period;
  219. }
  220. }
  221. static u8 symbol__parent_filter(const struct symbol *parent)
  222. {
  223. if (symbol_conf.exclude_other && parent == NULL)
  224. return 1 << HIST_FILTER__PARENT;
  225. return 0;
  226. }
  227. static struct hist_entry *add_hist_entry(struct hists *hists,
  228. struct hist_entry *entry,
  229. struct addr_location *al,
  230. u64 period)
  231. {
  232. struct rb_node **p;
  233. struct rb_node *parent = NULL;
  234. struct hist_entry *he;
  235. int cmp;
  236. pthread_mutex_lock(&hists->lock);
  237. p = &hists->entries_in->rb_node;
  238. while (*p != NULL) {
  239. parent = *p;
  240. he = rb_entry(parent, struct hist_entry, rb_node_in);
  241. /*
  242. * Make sure that it receives arguments in a same order as
  243. * hist_entry__collapse() so that we can use an appropriate
  244. * function when searching an entry regardless which sort
  245. * keys were used.
  246. */
  247. cmp = hist_entry__cmp(he, entry);
  248. if (!cmp) {
  249. he_stat__add_period(&he->stat, period);
  250. /* If the map of an existing hist_entry has
  251. * become out-of-date due to an exec() or
  252. * similar, update it. Otherwise we will
  253. * mis-adjust symbol addresses when computing
  254. * the history counter to increment.
  255. */
  256. if (he->ms.map != entry->ms.map) {
  257. he->ms.map = entry->ms.map;
  258. if (he->ms.map)
  259. he->ms.map->referenced = true;
  260. }
  261. goto out;
  262. }
  263. if (cmp < 0)
  264. p = &(*p)->rb_left;
  265. else
  266. p = &(*p)->rb_right;
  267. }
  268. he = hist_entry__new(entry);
  269. if (!he)
  270. goto out_unlock;
  271. rb_link_node(&he->rb_node_in, parent, p);
  272. rb_insert_color(&he->rb_node_in, hists->entries_in);
  273. out:
  274. hist_entry__add_cpumode_period(he, al->cpumode, period);
  275. out_unlock:
  276. pthread_mutex_unlock(&hists->lock);
  277. return he;
  278. }
  279. struct hist_entry *__hists__add_branch_entry(struct hists *self,
  280. struct addr_location *al,
  281. struct symbol *sym_parent,
  282. struct branch_info *bi,
  283. u64 period)
  284. {
  285. struct hist_entry entry = {
  286. .thread = al->thread,
  287. .ms = {
  288. .map = bi->to.map,
  289. .sym = bi->to.sym,
  290. },
  291. .cpu = al->cpu,
  292. .ip = bi->to.addr,
  293. .level = al->level,
  294. .stat = {
  295. .period = period,
  296. .nr_events = 1,
  297. },
  298. .parent = sym_parent,
  299. .filtered = symbol__parent_filter(sym_parent),
  300. .branch_info = bi,
  301. .hists = self,
  302. };
  303. return add_hist_entry(self, &entry, al, period);
  304. }
  305. struct hist_entry *__hists__add_entry(struct hists *self,
  306. struct addr_location *al,
  307. struct symbol *sym_parent, u64 period)
  308. {
  309. struct hist_entry entry = {
  310. .thread = al->thread,
  311. .ms = {
  312. .map = al->map,
  313. .sym = al->sym,
  314. },
  315. .cpu = al->cpu,
  316. .ip = al->addr,
  317. .level = al->level,
  318. .stat = {
  319. .period = period,
  320. .nr_events = 1,
  321. },
  322. .parent = sym_parent,
  323. .filtered = symbol__parent_filter(sym_parent),
  324. .hists = self,
  325. };
  326. return add_hist_entry(self, &entry, al, period);
  327. }
  328. int64_t
  329. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  330. {
  331. struct sort_entry *se;
  332. int64_t cmp = 0;
  333. list_for_each_entry(se, &hist_entry__sort_list, list) {
  334. cmp = se->se_cmp(left, right);
  335. if (cmp)
  336. break;
  337. }
  338. return cmp;
  339. }
  340. int64_t
  341. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  342. {
  343. struct sort_entry *se;
  344. int64_t cmp = 0;
  345. list_for_each_entry(se, &hist_entry__sort_list, list) {
  346. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  347. f = se->se_collapse ?: se->se_cmp;
  348. cmp = f(left, right);
  349. if (cmp)
  350. break;
  351. }
  352. return cmp;
  353. }
  354. void hist_entry__free(struct hist_entry *he)
  355. {
  356. free(he->branch_info);
  357. free(he);
  358. }
  359. /*
  360. * collapse the histogram
  361. */
  362. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  363. struct rb_root *root,
  364. struct hist_entry *he)
  365. {
  366. struct rb_node **p = &root->rb_node;
  367. struct rb_node *parent = NULL;
  368. struct hist_entry *iter;
  369. int64_t cmp;
  370. while (*p != NULL) {
  371. parent = *p;
  372. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  373. cmp = hist_entry__collapse(iter, he);
  374. if (!cmp) {
  375. he_stat__add_stat(&iter->stat, &he->stat);
  376. if (symbol_conf.use_callchain) {
  377. callchain_cursor_reset(&callchain_cursor);
  378. callchain_merge(&callchain_cursor,
  379. iter->callchain,
  380. he->callchain);
  381. }
  382. hist_entry__free(he);
  383. return false;
  384. }
  385. if (cmp < 0)
  386. p = &(*p)->rb_left;
  387. else
  388. p = &(*p)->rb_right;
  389. }
  390. rb_link_node(&he->rb_node_in, parent, p);
  391. rb_insert_color(&he->rb_node_in, root);
  392. return true;
  393. }
  394. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  395. {
  396. struct rb_root *root;
  397. pthread_mutex_lock(&hists->lock);
  398. root = hists->entries_in;
  399. if (++hists->entries_in > &hists->entries_in_array[1])
  400. hists->entries_in = &hists->entries_in_array[0];
  401. pthread_mutex_unlock(&hists->lock);
  402. return root;
  403. }
  404. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  405. {
  406. hists__filter_entry_by_dso(hists, he);
  407. hists__filter_entry_by_thread(hists, he);
  408. hists__filter_entry_by_symbol(hists, he);
  409. }
  410. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  411. {
  412. struct rb_root *root;
  413. struct rb_node *next;
  414. struct hist_entry *n;
  415. if (!sort__need_collapse && !threaded)
  416. return;
  417. root = hists__get_rotate_entries_in(hists);
  418. next = rb_first(root);
  419. while (next) {
  420. n = rb_entry(next, struct hist_entry, rb_node_in);
  421. next = rb_next(&n->rb_node_in);
  422. rb_erase(&n->rb_node_in, root);
  423. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  424. /*
  425. * If it wasn't combined with one of the entries already
  426. * collapsed, we need to apply the filters that may have
  427. * been set by, say, the hist_browser.
  428. */
  429. hists__apply_filters(hists, n);
  430. }
  431. }
  432. }
  433. void hists__collapse_resort(struct hists *hists)
  434. {
  435. return __hists__collapse_resort(hists, false);
  436. }
  437. void hists__collapse_resort_threaded(struct hists *hists)
  438. {
  439. return __hists__collapse_resort(hists, true);
  440. }
  441. /*
  442. * reverse the map, sort on period.
  443. */
  444. static void __hists__insert_output_entry(struct rb_root *entries,
  445. struct hist_entry *he,
  446. u64 min_callchain_hits)
  447. {
  448. struct rb_node **p = &entries->rb_node;
  449. struct rb_node *parent = NULL;
  450. struct hist_entry *iter;
  451. if (symbol_conf.use_callchain)
  452. callchain_param.sort(&he->sorted_chain, he->callchain,
  453. min_callchain_hits, &callchain_param);
  454. while (*p != NULL) {
  455. parent = *p;
  456. iter = rb_entry(parent, struct hist_entry, rb_node);
  457. if (he->stat.period > iter->stat.period)
  458. p = &(*p)->rb_left;
  459. else
  460. p = &(*p)->rb_right;
  461. }
  462. rb_link_node(&he->rb_node, parent, p);
  463. rb_insert_color(&he->rb_node, entries);
  464. }
  465. static void __hists__output_resort(struct hists *hists, bool threaded)
  466. {
  467. struct rb_root *root;
  468. struct rb_node *next;
  469. struct hist_entry *n;
  470. u64 min_callchain_hits;
  471. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  472. if (sort__need_collapse || threaded)
  473. root = &hists->entries_collapsed;
  474. else
  475. root = hists->entries_in;
  476. next = rb_first(root);
  477. hists->entries = RB_ROOT;
  478. hists->nr_entries = 0;
  479. hists->stats.total_period = 0;
  480. hists__reset_col_len(hists);
  481. while (next) {
  482. n = rb_entry(next, struct hist_entry, rb_node_in);
  483. next = rb_next(&n->rb_node_in);
  484. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  485. hists__inc_nr_entries(hists, n);
  486. }
  487. }
  488. void hists__output_resort(struct hists *hists)
  489. {
  490. return __hists__output_resort(hists, false);
  491. }
  492. void hists__output_resort_threaded(struct hists *hists)
  493. {
  494. return __hists__output_resort(hists, true);
  495. }
  496. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  497. enum hist_filter filter)
  498. {
  499. h->filtered &= ~(1 << filter);
  500. if (h->filtered)
  501. return;
  502. ++hists->nr_entries;
  503. if (h->ms.unfolded)
  504. hists->nr_entries += h->nr_rows;
  505. h->row_offset = 0;
  506. hists->stats.total_period += h->stat.period;
  507. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
  508. hists__calc_col_len(hists, h);
  509. }
  510. static bool hists__filter_entry_by_dso(struct hists *hists,
  511. struct hist_entry *he)
  512. {
  513. if (hists->dso_filter != NULL &&
  514. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  515. he->filtered |= (1 << HIST_FILTER__DSO);
  516. return true;
  517. }
  518. return false;
  519. }
  520. void hists__filter_by_dso(struct hists *hists)
  521. {
  522. struct rb_node *nd;
  523. hists->nr_entries = hists->stats.total_period = 0;
  524. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  525. hists__reset_col_len(hists);
  526. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  527. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  528. if (symbol_conf.exclude_other && !h->parent)
  529. continue;
  530. if (hists__filter_entry_by_dso(hists, h))
  531. continue;
  532. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  533. }
  534. }
  535. static bool hists__filter_entry_by_thread(struct hists *hists,
  536. struct hist_entry *he)
  537. {
  538. if (hists->thread_filter != NULL &&
  539. he->thread != hists->thread_filter) {
  540. he->filtered |= (1 << HIST_FILTER__THREAD);
  541. return true;
  542. }
  543. return false;
  544. }
  545. void hists__filter_by_thread(struct hists *hists)
  546. {
  547. struct rb_node *nd;
  548. hists->nr_entries = hists->stats.total_period = 0;
  549. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  550. hists__reset_col_len(hists);
  551. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  552. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  553. if (hists__filter_entry_by_thread(hists, h))
  554. continue;
  555. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  556. }
  557. }
  558. static bool hists__filter_entry_by_symbol(struct hists *hists,
  559. struct hist_entry *he)
  560. {
  561. if (hists->symbol_filter_str != NULL &&
  562. (!he->ms.sym || strstr(he->ms.sym->name,
  563. hists->symbol_filter_str) == NULL)) {
  564. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  565. return true;
  566. }
  567. return false;
  568. }
  569. void hists__filter_by_symbol(struct hists *hists)
  570. {
  571. struct rb_node *nd;
  572. hists->nr_entries = hists->stats.total_period = 0;
  573. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  574. hists__reset_col_len(hists);
  575. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  576. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  577. if (hists__filter_entry_by_symbol(hists, h))
  578. continue;
  579. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  580. }
  581. }
  582. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  583. {
  584. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  585. }
  586. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  587. {
  588. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  589. }
  590. void events_stats__inc(struct events_stats *stats, u32 type)
  591. {
  592. ++stats->nr_events[0];
  593. ++stats->nr_events[type];
  594. }
  595. void hists__inc_nr_events(struct hists *hists, u32 type)
  596. {
  597. events_stats__inc(&hists->stats, type);
  598. }
  599. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  600. struct hist_entry *pair)
  601. {
  602. struct rb_root *root;
  603. struct rb_node **p;
  604. struct rb_node *parent = NULL;
  605. struct hist_entry *he;
  606. int cmp;
  607. if (sort__need_collapse)
  608. root = &hists->entries_collapsed;
  609. else
  610. root = hists->entries_in;
  611. p = &root->rb_node;
  612. while (*p != NULL) {
  613. parent = *p;
  614. he = rb_entry(parent, struct hist_entry, rb_node_in);
  615. cmp = hist_entry__collapse(he, pair);
  616. if (!cmp)
  617. goto out;
  618. if (cmp < 0)
  619. p = &(*p)->rb_left;
  620. else
  621. p = &(*p)->rb_right;
  622. }
  623. he = hist_entry__new(pair);
  624. if (he) {
  625. memset(&he->stat, 0, sizeof(he->stat));
  626. he->hists = hists;
  627. rb_link_node(&he->rb_node_in, parent, p);
  628. rb_insert_color(&he->rb_node_in, root);
  629. hists__inc_nr_entries(hists, he);
  630. }
  631. out:
  632. return he;
  633. }
  634. static struct hist_entry *hists__find_entry(struct hists *hists,
  635. struct hist_entry *he)
  636. {
  637. struct rb_node *n;
  638. if (sort__need_collapse)
  639. n = hists->entries_collapsed.rb_node;
  640. else
  641. n = hists->entries_in->rb_node;
  642. while (n) {
  643. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  644. int64_t cmp = hist_entry__collapse(iter, he);
  645. if (cmp < 0)
  646. n = n->rb_left;
  647. else if (cmp > 0)
  648. n = n->rb_right;
  649. else
  650. return iter;
  651. }
  652. return NULL;
  653. }
  654. /*
  655. * Look for pairs to link to the leader buckets (hist_entries):
  656. */
  657. void hists__match(struct hists *leader, struct hists *other)
  658. {
  659. struct rb_root *root;
  660. struct rb_node *nd;
  661. struct hist_entry *pos, *pair;
  662. if (sort__need_collapse)
  663. root = &leader->entries_collapsed;
  664. else
  665. root = leader->entries_in;
  666. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  667. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  668. pair = hists__find_entry(other, pos);
  669. if (pair)
  670. hist_entry__add_pair(pair, pos);
  671. }
  672. }
  673. /*
  674. * Look for entries in the other hists that are not present in the leader, if
  675. * we find them, just add a dummy entry on the leader hists, with period=0,
  676. * nr_events=0, to serve as the list header.
  677. */
  678. int hists__link(struct hists *leader, struct hists *other)
  679. {
  680. struct rb_root *root;
  681. struct rb_node *nd;
  682. struct hist_entry *pos, *pair;
  683. if (sort__need_collapse)
  684. root = &other->entries_collapsed;
  685. else
  686. root = other->entries_in;
  687. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  688. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  689. if (!hist_entry__has_pairs(pos)) {
  690. pair = hists__add_dummy_entry(leader, pos);
  691. if (pair == NULL)
  692. return -1;
  693. hist_entry__add_pair(pos, pair);
  694. }
  695. }
  696. return 0;
  697. }