hist.c 22 KB

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