hist.c 24 KB

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