hist.c 23 KB

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