hist.c 23 KB

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