hist.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 (he->branch_info) {
  208. if (he->branch_info->from.map)
  209. he->branch_info->from.map->referenced = true;
  210. if (he->branch_info->to.map)
  211. he->branch_info->to.map->referenced = true;
  212. }
  213. if (symbol_conf.use_callchain)
  214. callchain_init(he->callchain);
  215. INIT_LIST_HEAD(&he->pairs.node);
  216. }
  217. return he;
  218. }
  219. void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  220. {
  221. if (!h->filtered) {
  222. hists__calc_col_len(hists, h);
  223. ++hists->nr_entries;
  224. hists->stats.total_period += h->stat.period;
  225. }
  226. }
  227. static u8 symbol__parent_filter(const struct symbol *parent)
  228. {
  229. if (symbol_conf.exclude_other && parent == NULL)
  230. return 1 << HIST_FILTER__PARENT;
  231. return 0;
  232. }
  233. static struct hist_entry *add_hist_entry(struct hists *hists,
  234. struct hist_entry *entry,
  235. struct addr_location *al,
  236. u64 period)
  237. {
  238. struct rb_node **p;
  239. struct rb_node *parent = NULL;
  240. struct hist_entry *he;
  241. int cmp;
  242. pthread_mutex_lock(&hists->lock);
  243. p = &hists->entries_in->rb_node;
  244. while (*p != NULL) {
  245. parent = *p;
  246. he = rb_entry(parent, struct hist_entry, rb_node_in);
  247. /*
  248. * Make sure that it receives arguments in a same order as
  249. * hist_entry__collapse() so that we can use an appropriate
  250. * function when searching an entry regardless which sort
  251. * keys were used.
  252. */
  253. cmp = hist_entry__cmp(he, entry);
  254. if (!cmp) {
  255. he_stat__add_period(&he->stat, period);
  256. /* If the map of an existing hist_entry has
  257. * become out-of-date due to an exec() or
  258. * similar, update it. Otherwise we will
  259. * mis-adjust symbol addresses when computing
  260. * the history counter to increment.
  261. */
  262. if (he->ms.map != entry->ms.map) {
  263. he->ms.map = entry->ms.map;
  264. if (he->ms.map)
  265. he->ms.map->referenced = true;
  266. }
  267. goto out;
  268. }
  269. if (cmp < 0)
  270. p = &(*p)->rb_left;
  271. else
  272. p = &(*p)->rb_right;
  273. }
  274. he = hist_entry__new(entry);
  275. if (!he)
  276. goto out_unlock;
  277. rb_link_node(&he->rb_node_in, parent, p);
  278. rb_insert_color(&he->rb_node_in, hists->entries_in);
  279. out:
  280. hist_entry__add_cpumode_period(he, al->cpumode, period);
  281. out_unlock:
  282. pthread_mutex_unlock(&hists->lock);
  283. return he;
  284. }
  285. struct hist_entry *__hists__add_branch_entry(struct hists *self,
  286. struct addr_location *al,
  287. struct symbol *sym_parent,
  288. struct branch_info *bi,
  289. u64 period)
  290. {
  291. struct hist_entry entry = {
  292. .thread = al->thread,
  293. .ms = {
  294. .map = bi->to.map,
  295. .sym = bi->to.sym,
  296. },
  297. .cpu = al->cpu,
  298. .ip = bi->to.addr,
  299. .level = al->level,
  300. .stat = {
  301. .period = period,
  302. .nr_events = 1,
  303. },
  304. .parent = sym_parent,
  305. .filtered = symbol__parent_filter(sym_parent),
  306. .branch_info = bi,
  307. .hists = self,
  308. };
  309. return add_hist_entry(self, &entry, al, period);
  310. }
  311. struct hist_entry *__hists__add_entry(struct hists *self,
  312. struct addr_location *al,
  313. struct symbol *sym_parent, u64 period)
  314. {
  315. struct hist_entry entry = {
  316. .thread = al->thread,
  317. .ms = {
  318. .map = al->map,
  319. .sym = al->sym,
  320. },
  321. .cpu = al->cpu,
  322. .ip = al->addr,
  323. .level = al->level,
  324. .stat = {
  325. .period = period,
  326. .nr_events = 1,
  327. },
  328. .parent = sym_parent,
  329. .filtered = symbol__parent_filter(sym_parent),
  330. .hists = self,
  331. };
  332. return add_hist_entry(self, &entry, al, period);
  333. }
  334. int64_t
  335. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  336. {
  337. struct sort_entry *se;
  338. int64_t cmp = 0;
  339. list_for_each_entry(se, &hist_entry__sort_list, list) {
  340. cmp = se->se_cmp(left, right);
  341. if (cmp)
  342. break;
  343. }
  344. return cmp;
  345. }
  346. int64_t
  347. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  348. {
  349. struct sort_entry *se;
  350. int64_t cmp = 0;
  351. list_for_each_entry(se, &hist_entry__sort_list, list) {
  352. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  353. f = se->se_collapse ?: se->se_cmp;
  354. cmp = f(left, right);
  355. if (cmp)
  356. break;
  357. }
  358. return cmp;
  359. }
  360. void hist_entry__free(struct hist_entry *he)
  361. {
  362. free(he->branch_info);
  363. free(he);
  364. }
  365. /*
  366. * collapse the histogram
  367. */
  368. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  369. struct rb_root *root,
  370. struct hist_entry *he)
  371. {
  372. struct rb_node **p = &root->rb_node;
  373. struct rb_node *parent = NULL;
  374. struct hist_entry *iter;
  375. int64_t cmp;
  376. while (*p != NULL) {
  377. parent = *p;
  378. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  379. cmp = hist_entry__collapse(iter, he);
  380. if (!cmp) {
  381. he_stat__add_stat(&iter->stat, &he->stat);
  382. if (symbol_conf.use_callchain) {
  383. callchain_cursor_reset(&callchain_cursor);
  384. callchain_merge(&callchain_cursor,
  385. iter->callchain,
  386. he->callchain);
  387. }
  388. hist_entry__free(he);
  389. return false;
  390. }
  391. if (cmp < 0)
  392. p = &(*p)->rb_left;
  393. else
  394. p = &(*p)->rb_right;
  395. }
  396. rb_link_node(&he->rb_node_in, parent, p);
  397. rb_insert_color(&he->rb_node_in, root);
  398. return true;
  399. }
  400. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  401. {
  402. struct rb_root *root;
  403. pthread_mutex_lock(&hists->lock);
  404. root = hists->entries_in;
  405. if (++hists->entries_in > &hists->entries_in_array[1])
  406. hists->entries_in = &hists->entries_in_array[0];
  407. pthread_mutex_unlock(&hists->lock);
  408. return root;
  409. }
  410. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  411. {
  412. hists__filter_entry_by_dso(hists, he);
  413. hists__filter_entry_by_thread(hists, he);
  414. hists__filter_entry_by_symbol(hists, he);
  415. }
  416. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  417. {
  418. struct rb_root *root;
  419. struct rb_node *next;
  420. struct hist_entry *n;
  421. if (!sort__need_collapse && !threaded)
  422. return;
  423. root = hists__get_rotate_entries_in(hists);
  424. next = rb_first(root);
  425. while (next) {
  426. n = rb_entry(next, struct hist_entry, rb_node_in);
  427. next = rb_next(&n->rb_node_in);
  428. rb_erase(&n->rb_node_in, root);
  429. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  430. /*
  431. * If it wasn't combined with one of the entries already
  432. * collapsed, we need to apply the filters that may have
  433. * been set by, say, the hist_browser.
  434. */
  435. hists__apply_filters(hists, n);
  436. }
  437. }
  438. }
  439. void hists__collapse_resort(struct hists *hists)
  440. {
  441. return __hists__collapse_resort(hists, false);
  442. }
  443. void hists__collapse_resort_threaded(struct hists *hists)
  444. {
  445. return __hists__collapse_resort(hists, true);
  446. }
  447. /*
  448. * reverse the map, sort on period.
  449. */
  450. static void __hists__insert_output_entry(struct rb_root *entries,
  451. struct hist_entry *he,
  452. u64 min_callchain_hits)
  453. {
  454. struct rb_node **p = &entries->rb_node;
  455. struct rb_node *parent = NULL;
  456. struct hist_entry *iter;
  457. if (symbol_conf.use_callchain)
  458. callchain_param.sort(&he->sorted_chain, he->callchain,
  459. min_callchain_hits, &callchain_param);
  460. while (*p != NULL) {
  461. parent = *p;
  462. iter = rb_entry(parent, struct hist_entry, rb_node);
  463. if (he->stat.period > iter->stat.period)
  464. p = &(*p)->rb_left;
  465. else
  466. p = &(*p)->rb_right;
  467. }
  468. rb_link_node(&he->rb_node, parent, p);
  469. rb_insert_color(&he->rb_node, entries);
  470. }
  471. static void __hists__output_resort(struct hists *hists, bool threaded)
  472. {
  473. struct rb_root *root;
  474. struct rb_node *next;
  475. struct hist_entry *n;
  476. u64 min_callchain_hits;
  477. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  478. if (sort__need_collapse || threaded)
  479. root = &hists->entries_collapsed;
  480. else
  481. root = hists->entries_in;
  482. next = rb_first(root);
  483. hists->entries = RB_ROOT;
  484. hists->nr_entries = 0;
  485. hists->stats.total_period = 0;
  486. hists__reset_col_len(hists);
  487. while (next) {
  488. n = rb_entry(next, struct hist_entry, rb_node_in);
  489. next = rb_next(&n->rb_node_in);
  490. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  491. hists__inc_nr_entries(hists, n);
  492. }
  493. }
  494. void hists__output_resort(struct hists *hists)
  495. {
  496. return __hists__output_resort(hists, false);
  497. }
  498. void hists__output_resort_threaded(struct hists *hists)
  499. {
  500. return __hists__output_resort(hists, true);
  501. }
  502. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  503. enum hist_filter filter)
  504. {
  505. h->filtered &= ~(1 << filter);
  506. if (h->filtered)
  507. return;
  508. ++hists->nr_entries;
  509. if (h->ms.unfolded)
  510. hists->nr_entries += h->nr_rows;
  511. h->row_offset = 0;
  512. hists->stats.total_period += h->stat.period;
  513. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
  514. hists__calc_col_len(hists, h);
  515. }
  516. static bool hists__filter_entry_by_dso(struct hists *hists,
  517. struct hist_entry *he)
  518. {
  519. if (hists->dso_filter != NULL &&
  520. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  521. he->filtered |= (1 << HIST_FILTER__DSO);
  522. return true;
  523. }
  524. return false;
  525. }
  526. void hists__filter_by_dso(struct hists *hists)
  527. {
  528. struct rb_node *nd;
  529. hists->nr_entries = hists->stats.total_period = 0;
  530. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  531. hists__reset_col_len(hists);
  532. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  533. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  534. if (symbol_conf.exclude_other && !h->parent)
  535. continue;
  536. if (hists__filter_entry_by_dso(hists, h))
  537. continue;
  538. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  539. }
  540. }
  541. static bool hists__filter_entry_by_thread(struct hists *hists,
  542. struct hist_entry *he)
  543. {
  544. if (hists->thread_filter != NULL &&
  545. he->thread != hists->thread_filter) {
  546. he->filtered |= (1 << HIST_FILTER__THREAD);
  547. return true;
  548. }
  549. return false;
  550. }
  551. void hists__filter_by_thread(struct hists *hists)
  552. {
  553. struct rb_node *nd;
  554. hists->nr_entries = hists->stats.total_period = 0;
  555. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  556. hists__reset_col_len(hists);
  557. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  558. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  559. if (hists__filter_entry_by_thread(hists, h))
  560. continue;
  561. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  562. }
  563. }
  564. static bool hists__filter_entry_by_symbol(struct hists *hists,
  565. struct hist_entry *he)
  566. {
  567. if (hists->symbol_filter_str != NULL &&
  568. (!he->ms.sym || strstr(he->ms.sym->name,
  569. hists->symbol_filter_str) == NULL)) {
  570. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  571. return true;
  572. }
  573. return false;
  574. }
  575. void hists__filter_by_symbol(struct hists *hists)
  576. {
  577. struct rb_node *nd;
  578. hists->nr_entries = hists->stats.total_period = 0;
  579. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  580. hists__reset_col_len(hists);
  581. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  582. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  583. if (hists__filter_entry_by_symbol(hists, h))
  584. continue;
  585. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  586. }
  587. }
  588. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  589. {
  590. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  591. }
  592. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  593. {
  594. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  595. }
  596. void events_stats__inc(struct events_stats *stats, u32 type)
  597. {
  598. ++stats->nr_events[0];
  599. ++stats->nr_events[type];
  600. }
  601. void hists__inc_nr_events(struct hists *hists, u32 type)
  602. {
  603. events_stats__inc(&hists->stats, type);
  604. }
  605. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  606. struct hist_entry *pair)
  607. {
  608. struct rb_root *root;
  609. struct rb_node **p;
  610. struct rb_node *parent = NULL;
  611. struct hist_entry *he;
  612. int cmp;
  613. if (sort__need_collapse)
  614. root = &hists->entries_collapsed;
  615. else
  616. root = hists->entries_in;
  617. p = &root->rb_node;
  618. while (*p != NULL) {
  619. parent = *p;
  620. he = rb_entry(parent, struct hist_entry, rb_node_in);
  621. cmp = hist_entry__collapse(he, pair);
  622. if (!cmp)
  623. goto out;
  624. if (cmp < 0)
  625. p = &(*p)->rb_left;
  626. else
  627. p = &(*p)->rb_right;
  628. }
  629. he = hist_entry__new(pair);
  630. if (he) {
  631. memset(&he->stat, 0, sizeof(he->stat));
  632. he->hists = hists;
  633. rb_link_node(&he->rb_node_in, parent, p);
  634. rb_insert_color(&he->rb_node_in, root);
  635. hists__inc_nr_entries(hists, he);
  636. }
  637. out:
  638. return he;
  639. }
  640. static struct hist_entry *hists__find_entry(struct hists *hists,
  641. struct hist_entry *he)
  642. {
  643. struct rb_node *n;
  644. if (sort__need_collapse)
  645. n = hists->entries_collapsed.rb_node;
  646. else
  647. n = hists->entries_in->rb_node;
  648. while (n) {
  649. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  650. int64_t cmp = hist_entry__collapse(iter, he);
  651. if (cmp < 0)
  652. n = n->rb_left;
  653. else if (cmp > 0)
  654. n = n->rb_right;
  655. else
  656. return iter;
  657. }
  658. return NULL;
  659. }
  660. /*
  661. * Look for pairs to link to the leader buckets (hist_entries):
  662. */
  663. void hists__match(struct hists *leader, struct hists *other)
  664. {
  665. struct rb_root *root;
  666. struct rb_node *nd;
  667. struct hist_entry *pos, *pair;
  668. if (sort__need_collapse)
  669. root = &leader->entries_collapsed;
  670. else
  671. root = leader->entries_in;
  672. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  673. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  674. pair = hists__find_entry(other, pos);
  675. if (pair)
  676. hist_entry__add_pair(pair, pos);
  677. }
  678. }
  679. /*
  680. * Look for entries in the other hists that are not present in the leader, if
  681. * we find them, just add a dummy entry on the leader hists, with period=0,
  682. * nr_events=0, to serve as the list header.
  683. */
  684. int hists__link(struct hists *leader, struct hists *other)
  685. {
  686. struct rb_root *root;
  687. struct rb_node *nd;
  688. struct hist_entry *pos, *pair;
  689. if (sort__need_collapse)
  690. root = &other->entries_collapsed;
  691. else
  692. root = other->entries_in;
  693. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  694. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  695. if (!hist_entry__has_pairs(pos)) {
  696. pair = hists__add_dummy_entry(leader, pos);
  697. if (pair == NULL)
  698. return -1;
  699. hist_entry__add_pair(pos, pair);
  700. }
  701. }
  702. return 0;
  703. }