hist.c 19 KB

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