hist.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. if (he->branch_info->from.map)
  251. he->branch_info->from.map->referenced = true;
  252. if (he->branch_info->to.map)
  253. he->branch_info->to.map->referenced = true;
  254. }
  255. if (he->mem_info) {
  256. if (he->mem_info->iaddr.map)
  257. he->mem_info->iaddr.map->referenced = true;
  258. if (he->mem_info->daddr.map)
  259. he->mem_info->daddr.map->referenced = true;
  260. }
  261. if (symbol_conf.use_callchain)
  262. callchain_init(he->callchain);
  263. INIT_LIST_HEAD(&he->pairs.node);
  264. }
  265. return he;
  266. }
  267. void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  268. {
  269. if (!h->filtered) {
  270. hists__calc_col_len(hists, h);
  271. ++hists->nr_entries;
  272. hists->stats.total_period += h->stat.period;
  273. }
  274. }
  275. static u8 symbol__parent_filter(const struct symbol *parent)
  276. {
  277. if (symbol_conf.exclude_other && parent == NULL)
  278. return 1 << HIST_FILTER__PARENT;
  279. return 0;
  280. }
  281. static struct hist_entry *add_hist_entry(struct hists *hists,
  282. struct hist_entry *entry,
  283. struct addr_location *al,
  284. u64 period,
  285. u64 weight)
  286. {
  287. struct rb_node **p;
  288. struct rb_node *parent = NULL;
  289. struct hist_entry *he;
  290. int cmp;
  291. pthread_mutex_lock(&hists->lock);
  292. p = &hists->entries_in->rb_node;
  293. while (*p != NULL) {
  294. parent = *p;
  295. he = rb_entry(parent, struct hist_entry, rb_node_in);
  296. /*
  297. * Make sure that it receives arguments in a same order as
  298. * hist_entry__collapse() so that we can use an appropriate
  299. * function when searching an entry regardless which sort
  300. * keys were used.
  301. */
  302. cmp = hist_entry__cmp(he, entry);
  303. if (!cmp) {
  304. he_stat__add_period(&he->stat, period, weight);
  305. /* If the map of an existing hist_entry has
  306. * become out-of-date due to an exec() or
  307. * similar, update it. Otherwise we will
  308. * mis-adjust symbol addresses when computing
  309. * the history counter to increment.
  310. */
  311. if (he->ms.map != entry->ms.map) {
  312. he->ms.map = entry->ms.map;
  313. if (he->ms.map)
  314. he->ms.map->referenced = true;
  315. }
  316. goto out;
  317. }
  318. if (cmp < 0)
  319. p = &(*p)->rb_left;
  320. else
  321. p = &(*p)->rb_right;
  322. }
  323. he = hist_entry__new(entry);
  324. if (!he)
  325. goto out_unlock;
  326. rb_link_node(&he->rb_node_in, parent, p);
  327. rb_insert_color(&he->rb_node_in, hists->entries_in);
  328. out:
  329. hist_entry__add_cpumode_period(he, al->cpumode, period);
  330. out_unlock:
  331. pthread_mutex_unlock(&hists->lock);
  332. return he;
  333. }
  334. struct hist_entry *__hists__add_mem_entry(struct hists *self,
  335. struct addr_location *al,
  336. struct symbol *sym_parent,
  337. struct mem_info *mi,
  338. u64 period,
  339. u64 weight)
  340. {
  341. struct hist_entry entry = {
  342. .thread = al->thread,
  343. .ms = {
  344. .map = al->map,
  345. .sym = al->sym,
  346. },
  347. .stat = {
  348. .period = period,
  349. .weight = weight,
  350. .nr_events = 1,
  351. },
  352. .cpu = al->cpu,
  353. .ip = al->addr,
  354. .level = al->level,
  355. .parent = sym_parent,
  356. .filtered = symbol__parent_filter(sym_parent),
  357. .hists = self,
  358. .mem_info = mi,
  359. .branch_info = NULL,
  360. };
  361. return add_hist_entry(self, &entry, al, period, weight);
  362. }
  363. struct hist_entry *__hists__add_branch_entry(struct hists *self,
  364. struct addr_location *al,
  365. struct symbol *sym_parent,
  366. struct branch_info *bi,
  367. u64 period,
  368. u64 weight)
  369. {
  370. struct hist_entry entry = {
  371. .thread = al->thread,
  372. .ms = {
  373. .map = bi->to.map,
  374. .sym = bi->to.sym,
  375. },
  376. .cpu = al->cpu,
  377. .ip = bi->to.addr,
  378. .level = al->level,
  379. .stat = {
  380. .period = period,
  381. .nr_events = 1,
  382. .weight = weight,
  383. },
  384. .parent = sym_parent,
  385. .filtered = symbol__parent_filter(sym_parent),
  386. .branch_info = bi,
  387. .hists = self,
  388. .mem_info = NULL,
  389. };
  390. return add_hist_entry(self, &entry, al, period, weight);
  391. }
  392. struct hist_entry *__hists__add_entry(struct hists *self,
  393. struct addr_location *al,
  394. struct symbol *sym_parent, u64 period,
  395. u64 weight)
  396. {
  397. struct hist_entry entry = {
  398. .thread = al->thread,
  399. .ms = {
  400. .map = al->map,
  401. .sym = al->sym,
  402. },
  403. .cpu = al->cpu,
  404. .ip = al->addr,
  405. .level = al->level,
  406. .stat = {
  407. .period = period,
  408. .nr_events = 1,
  409. .weight = weight,
  410. },
  411. .parent = sym_parent,
  412. .filtered = symbol__parent_filter(sym_parent),
  413. .hists = self,
  414. .branch_info = NULL,
  415. .mem_info = NULL,
  416. };
  417. return add_hist_entry(self, &entry, al, period, weight);
  418. }
  419. int64_t
  420. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  421. {
  422. struct sort_entry *se;
  423. int64_t cmp = 0;
  424. list_for_each_entry(se, &hist_entry__sort_list, list) {
  425. cmp = se->se_cmp(left, right);
  426. if (cmp)
  427. break;
  428. }
  429. return cmp;
  430. }
  431. int64_t
  432. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  433. {
  434. struct sort_entry *se;
  435. int64_t cmp = 0;
  436. list_for_each_entry(se, &hist_entry__sort_list, list) {
  437. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  438. f = se->se_collapse ?: se->se_cmp;
  439. cmp = f(left, right);
  440. if (cmp)
  441. break;
  442. }
  443. return cmp;
  444. }
  445. void hist_entry__free(struct hist_entry *he)
  446. {
  447. free(he->branch_info);
  448. free(he);
  449. }
  450. /*
  451. * collapse the histogram
  452. */
  453. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  454. struct rb_root *root,
  455. struct hist_entry *he)
  456. {
  457. struct rb_node **p = &root->rb_node;
  458. struct rb_node *parent = NULL;
  459. struct hist_entry *iter;
  460. int64_t cmp;
  461. while (*p != NULL) {
  462. parent = *p;
  463. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  464. cmp = hist_entry__collapse(iter, he);
  465. if (!cmp) {
  466. he_stat__add_stat(&iter->stat, &he->stat);
  467. if (symbol_conf.use_callchain) {
  468. callchain_cursor_reset(&callchain_cursor);
  469. callchain_merge(&callchain_cursor,
  470. iter->callchain,
  471. he->callchain);
  472. }
  473. hist_entry__free(he);
  474. return false;
  475. }
  476. if (cmp < 0)
  477. p = &(*p)->rb_left;
  478. else
  479. p = &(*p)->rb_right;
  480. }
  481. rb_link_node(&he->rb_node_in, parent, p);
  482. rb_insert_color(&he->rb_node_in, root);
  483. return true;
  484. }
  485. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  486. {
  487. struct rb_root *root;
  488. pthread_mutex_lock(&hists->lock);
  489. root = hists->entries_in;
  490. if (++hists->entries_in > &hists->entries_in_array[1])
  491. hists->entries_in = &hists->entries_in_array[0];
  492. pthread_mutex_unlock(&hists->lock);
  493. return root;
  494. }
  495. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  496. {
  497. hists__filter_entry_by_dso(hists, he);
  498. hists__filter_entry_by_thread(hists, he);
  499. hists__filter_entry_by_symbol(hists, he);
  500. }
  501. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  502. {
  503. struct rb_root *root;
  504. struct rb_node *next;
  505. struct hist_entry *n;
  506. if (!sort__need_collapse && !threaded)
  507. return;
  508. root = hists__get_rotate_entries_in(hists);
  509. next = rb_first(root);
  510. while (next) {
  511. n = rb_entry(next, struct hist_entry, rb_node_in);
  512. next = rb_next(&n->rb_node_in);
  513. rb_erase(&n->rb_node_in, root);
  514. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  515. /*
  516. * If it wasn't combined with one of the entries already
  517. * collapsed, we need to apply the filters that may have
  518. * been set by, say, the hist_browser.
  519. */
  520. hists__apply_filters(hists, n);
  521. }
  522. }
  523. }
  524. void hists__collapse_resort(struct hists *hists)
  525. {
  526. return __hists__collapse_resort(hists, false);
  527. }
  528. void hists__collapse_resort_threaded(struct hists *hists)
  529. {
  530. return __hists__collapse_resort(hists, true);
  531. }
  532. /*
  533. * reverse the map, sort on period.
  534. */
  535. static int period_cmp(u64 period_a, u64 period_b)
  536. {
  537. if (period_a > period_b)
  538. return 1;
  539. if (period_a < period_b)
  540. return -1;
  541. return 0;
  542. }
  543. static int hist_entry__sort_on_period(struct hist_entry *a,
  544. struct hist_entry *b)
  545. {
  546. int ret;
  547. int i, nr_members;
  548. struct perf_evsel *evsel;
  549. struct hist_entry *pair;
  550. u64 *periods_a, *periods_b;
  551. ret = period_cmp(a->stat.period, b->stat.period);
  552. if (ret || !symbol_conf.event_group)
  553. return ret;
  554. evsel = hists_to_evsel(a->hists);
  555. nr_members = evsel->nr_members;
  556. if (nr_members <= 1)
  557. return ret;
  558. periods_a = zalloc(sizeof(periods_a) * nr_members);
  559. periods_b = zalloc(sizeof(periods_b) * nr_members);
  560. if (!periods_a || !periods_b)
  561. goto out;
  562. list_for_each_entry(pair, &a->pairs.head, pairs.node) {
  563. evsel = hists_to_evsel(pair->hists);
  564. periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
  565. }
  566. list_for_each_entry(pair, &b->pairs.head, pairs.node) {
  567. evsel = hists_to_evsel(pair->hists);
  568. periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
  569. }
  570. for (i = 1; i < nr_members; i++) {
  571. ret = period_cmp(periods_a[i], periods_b[i]);
  572. if (ret)
  573. break;
  574. }
  575. out:
  576. free(periods_a);
  577. free(periods_b);
  578. return ret;
  579. }
  580. static void __hists__insert_output_entry(struct rb_root *entries,
  581. struct hist_entry *he,
  582. u64 min_callchain_hits)
  583. {
  584. struct rb_node **p = &entries->rb_node;
  585. struct rb_node *parent = NULL;
  586. struct hist_entry *iter;
  587. if (symbol_conf.use_callchain)
  588. callchain_param.sort(&he->sorted_chain, he->callchain,
  589. min_callchain_hits, &callchain_param);
  590. while (*p != NULL) {
  591. parent = *p;
  592. iter = rb_entry(parent, struct hist_entry, rb_node);
  593. if (hist_entry__sort_on_period(he, iter) > 0)
  594. p = &(*p)->rb_left;
  595. else
  596. p = &(*p)->rb_right;
  597. }
  598. rb_link_node(&he->rb_node, parent, p);
  599. rb_insert_color(&he->rb_node, entries);
  600. }
  601. static void __hists__output_resort(struct hists *hists, bool threaded)
  602. {
  603. struct rb_root *root;
  604. struct rb_node *next;
  605. struct hist_entry *n;
  606. u64 min_callchain_hits;
  607. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  608. if (sort__need_collapse || threaded)
  609. root = &hists->entries_collapsed;
  610. else
  611. root = hists->entries_in;
  612. next = rb_first(root);
  613. hists->entries = RB_ROOT;
  614. hists->nr_entries = 0;
  615. hists->stats.total_period = 0;
  616. hists__reset_col_len(hists);
  617. while (next) {
  618. n = rb_entry(next, struct hist_entry, rb_node_in);
  619. next = rb_next(&n->rb_node_in);
  620. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  621. hists__inc_nr_entries(hists, n);
  622. }
  623. }
  624. void hists__output_resort(struct hists *hists)
  625. {
  626. return __hists__output_resort(hists, false);
  627. }
  628. void hists__output_resort_threaded(struct hists *hists)
  629. {
  630. return __hists__output_resort(hists, true);
  631. }
  632. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  633. enum hist_filter filter)
  634. {
  635. h->filtered &= ~(1 << filter);
  636. if (h->filtered)
  637. return;
  638. ++hists->nr_entries;
  639. if (h->ms.unfolded)
  640. hists->nr_entries += h->nr_rows;
  641. h->row_offset = 0;
  642. hists->stats.total_period += h->stat.period;
  643. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
  644. hists__calc_col_len(hists, h);
  645. }
  646. static bool hists__filter_entry_by_dso(struct hists *hists,
  647. struct hist_entry *he)
  648. {
  649. if (hists->dso_filter != NULL &&
  650. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  651. he->filtered |= (1 << HIST_FILTER__DSO);
  652. return true;
  653. }
  654. return false;
  655. }
  656. void hists__filter_by_dso(struct hists *hists)
  657. {
  658. struct rb_node *nd;
  659. hists->nr_entries = hists->stats.total_period = 0;
  660. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  661. hists__reset_col_len(hists);
  662. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  663. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  664. if (symbol_conf.exclude_other && !h->parent)
  665. continue;
  666. if (hists__filter_entry_by_dso(hists, h))
  667. continue;
  668. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  669. }
  670. }
  671. static bool hists__filter_entry_by_thread(struct hists *hists,
  672. struct hist_entry *he)
  673. {
  674. if (hists->thread_filter != NULL &&
  675. he->thread != hists->thread_filter) {
  676. he->filtered |= (1 << HIST_FILTER__THREAD);
  677. return true;
  678. }
  679. return false;
  680. }
  681. void hists__filter_by_thread(struct hists *hists)
  682. {
  683. struct rb_node *nd;
  684. hists->nr_entries = hists->stats.total_period = 0;
  685. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  686. hists__reset_col_len(hists);
  687. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  688. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  689. if (hists__filter_entry_by_thread(hists, h))
  690. continue;
  691. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  692. }
  693. }
  694. static bool hists__filter_entry_by_symbol(struct hists *hists,
  695. struct hist_entry *he)
  696. {
  697. if (hists->symbol_filter_str != NULL &&
  698. (!he->ms.sym || strstr(he->ms.sym->name,
  699. hists->symbol_filter_str) == NULL)) {
  700. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  701. return true;
  702. }
  703. return false;
  704. }
  705. void hists__filter_by_symbol(struct hists *hists)
  706. {
  707. struct rb_node *nd;
  708. hists->nr_entries = hists->stats.total_period = 0;
  709. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  710. hists__reset_col_len(hists);
  711. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  712. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  713. if (hists__filter_entry_by_symbol(hists, h))
  714. continue;
  715. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  716. }
  717. }
  718. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  719. {
  720. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  721. }
  722. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  723. {
  724. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  725. }
  726. void events_stats__inc(struct events_stats *stats, u32 type)
  727. {
  728. ++stats->nr_events[0];
  729. ++stats->nr_events[type];
  730. }
  731. void hists__inc_nr_events(struct hists *hists, u32 type)
  732. {
  733. events_stats__inc(&hists->stats, type);
  734. }
  735. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  736. struct hist_entry *pair)
  737. {
  738. struct rb_root *root;
  739. struct rb_node **p;
  740. struct rb_node *parent = NULL;
  741. struct hist_entry *he;
  742. int cmp;
  743. if (sort__need_collapse)
  744. root = &hists->entries_collapsed;
  745. else
  746. root = hists->entries_in;
  747. p = &root->rb_node;
  748. while (*p != NULL) {
  749. parent = *p;
  750. he = rb_entry(parent, struct hist_entry, rb_node_in);
  751. cmp = hist_entry__collapse(he, pair);
  752. if (!cmp)
  753. goto out;
  754. if (cmp < 0)
  755. p = &(*p)->rb_left;
  756. else
  757. p = &(*p)->rb_right;
  758. }
  759. he = hist_entry__new(pair);
  760. if (he) {
  761. memset(&he->stat, 0, sizeof(he->stat));
  762. he->hists = hists;
  763. rb_link_node(&he->rb_node_in, parent, p);
  764. rb_insert_color(&he->rb_node_in, root);
  765. hists__inc_nr_entries(hists, he);
  766. }
  767. out:
  768. return he;
  769. }
  770. static struct hist_entry *hists__find_entry(struct hists *hists,
  771. struct hist_entry *he)
  772. {
  773. struct rb_node *n;
  774. if (sort__need_collapse)
  775. n = hists->entries_collapsed.rb_node;
  776. else
  777. n = hists->entries_in->rb_node;
  778. while (n) {
  779. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  780. int64_t cmp = hist_entry__collapse(iter, he);
  781. if (cmp < 0)
  782. n = n->rb_left;
  783. else if (cmp > 0)
  784. n = n->rb_right;
  785. else
  786. return iter;
  787. }
  788. return NULL;
  789. }
  790. /*
  791. * Look for pairs to link to the leader buckets (hist_entries):
  792. */
  793. void hists__match(struct hists *leader, struct hists *other)
  794. {
  795. struct rb_root *root;
  796. struct rb_node *nd;
  797. struct hist_entry *pos, *pair;
  798. if (sort__need_collapse)
  799. root = &leader->entries_collapsed;
  800. else
  801. root = leader->entries_in;
  802. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  803. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  804. pair = hists__find_entry(other, pos);
  805. if (pair)
  806. hist_entry__add_pair(pair, pos);
  807. }
  808. }
  809. /*
  810. * Look for entries in the other hists that are not present in the leader, if
  811. * we find them, just add a dummy entry on the leader hists, with period=0,
  812. * nr_events=0, to serve as the list header.
  813. */
  814. int hists__link(struct hists *leader, struct hists *other)
  815. {
  816. struct rb_root *root;
  817. struct rb_node *nd;
  818. struct hist_entry *pos, *pair;
  819. if (sort__need_collapse)
  820. root = &other->entries_collapsed;
  821. else
  822. root = other->entries_in;
  823. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  824. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  825. if (!hist_entry__has_pairs(pos)) {
  826. pair = hists__add_dummy_entry(leader, pos);
  827. if (pair == NULL)
  828. return -1;
  829. hist_entry__add_pair(pos, pair);
  830. }
  831. }
  832. return 0;
  833. }