hist.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. u64 diff;
  199. if (prev_period == 0)
  200. return true;
  201. he_stat__decay(&he->stat);
  202. diff = prev_period - he->stat.period;
  203. hists->stats.total_period -= diff;
  204. if (!he->filtered)
  205. hists->stats.total_non_filtered_period -= diff;
  206. return he->stat.period == 0;
  207. }
  208. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  209. {
  210. struct rb_node *next = rb_first(&hists->entries);
  211. struct hist_entry *n;
  212. while (next) {
  213. n = rb_entry(next, struct hist_entry, rb_node);
  214. next = rb_next(&n->rb_node);
  215. /*
  216. * We may be annotating this, for instance, so keep it here in
  217. * case some it gets new samples, we'll eventually free it when
  218. * the user stops browsing and it agains gets fully decayed.
  219. */
  220. if (((zap_user && n->level == '.') ||
  221. (zap_kernel && n->level != '.') ||
  222. hists__decay_entry(hists, n)) &&
  223. !n->used) {
  224. rb_erase(&n->rb_node, &hists->entries);
  225. if (sort__need_collapse)
  226. rb_erase(&n->rb_node_in, &hists->entries_collapsed);
  227. --hists->nr_entries;
  228. if (!n->filtered)
  229. --hists->nr_non_filtered_entries;
  230. hist_entry__free(n);
  231. }
  232. }
  233. }
  234. /*
  235. * histogram, sorted on item, collects periods
  236. */
  237. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  238. {
  239. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  240. struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
  241. if (he != NULL) {
  242. *he = *template;
  243. if (he->ms.map)
  244. he->ms.map->referenced = true;
  245. if (he->branch_info) {
  246. /*
  247. * This branch info is (a part of) allocated from
  248. * sample__resolve_bstack() and will be freed after
  249. * adding new entries. So we need to save a copy.
  250. */
  251. he->branch_info = malloc(sizeof(*he->branch_info));
  252. if (he->branch_info == NULL) {
  253. free(he);
  254. return NULL;
  255. }
  256. memcpy(he->branch_info, template->branch_info,
  257. sizeof(*he->branch_info));
  258. if (he->branch_info->from.map)
  259. he->branch_info->from.map->referenced = true;
  260. if (he->branch_info->to.map)
  261. he->branch_info->to.map->referenced = true;
  262. }
  263. if (he->mem_info) {
  264. if (he->mem_info->iaddr.map)
  265. he->mem_info->iaddr.map->referenced = true;
  266. if (he->mem_info->daddr.map)
  267. he->mem_info->daddr.map->referenced = true;
  268. }
  269. if (symbol_conf.use_callchain)
  270. callchain_init(he->callchain);
  271. INIT_LIST_HEAD(&he->pairs.node);
  272. }
  273. return he;
  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. {
  285. struct rb_node **p;
  286. struct rb_node *parent = NULL;
  287. struct hist_entry *he;
  288. int64_t cmp;
  289. u64 period = entry->stat.period;
  290. u64 weight = entry->stat.weight;
  291. p = &hists->entries_in->rb_node;
  292. while (*p != NULL) {
  293. parent = *p;
  294. he = rb_entry(parent, struct hist_entry, rb_node_in);
  295. /*
  296. * Make sure that it receives arguments in a same order as
  297. * hist_entry__collapse() so that we can use an appropriate
  298. * function when searching an entry regardless which sort
  299. * keys were used.
  300. */
  301. cmp = hist_entry__cmp(he, entry);
  302. if (!cmp) {
  303. he_stat__add_period(&he->stat, period, weight);
  304. /*
  305. * This mem info was allocated from sample__resolve_mem
  306. * and will not be used anymore.
  307. */
  308. zfree(&entry->mem_info);
  309. /* If the map of an existing hist_entry has
  310. * become out-of-date due to an exec() or
  311. * similar, update it. Otherwise we will
  312. * mis-adjust symbol addresses when computing
  313. * the history counter to increment.
  314. */
  315. if (he->ms.map != entry->ms.map) {
  316. he->ms.map = entry->ms.map;
  317. if (he->ms.map)
  318. he->ms.map->referenced = true;
  319. }
  320. goto out;
  321. }
  322. if (cmp < 0)
  323. p = &(*p)->rb_left;
  324. else
  325. p = &(*p)->rb_right;
  326. }
  327. he = hist_entry__new(entry);
  328. if (!he)
  329. return NULL;
  330. rb_link_node(&he->rb_node_in, parent, p);
  331. rb_insert_color(&he->rb_node_in, hists->entries_in);
  332. out:
  333. he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
  334. return he;
  335. }
  336. struct hist_entry *__hists__add_entry(struct hists *hists,
  337. struct addr_location *al,
  338. struct symbol *sym_parent,
  339. struct branch_info *bi,
  340. struct mem_info *mi,
  341. u64 period, u64 weight, u64 transaction)
  342. {
  343. struct hist_entry entry = {
  344. .thread = al->thread,
  345. .comm = thread__comm(al->thread),
  346. .ms = {
  347. .map = al->map,
  348. .sym = al->sym,
  349. },
  350. .cpu = al->cpu,
  351. .ip = al->addr,
  352. .level = al->level,
  353. .stat = {
  354. .nr_events = 1,
  355. .period = period,
  356. .weight = weight,
  357. },
  358. .parent = sym_parent,
  359. .filtered = symbol__parent_filter(sym_parent) | al->filtered,
  360. .hists = hists,
  361. .branch_info = bi,
  362. .mem_info = mi,
  363. .transaction = transaction,
  364. };
  365. return add_hist_entry(hists, &entry, al);
  366. }
  367. int64_t
  368. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  369. {
  370. struct perf_hpp_fmt *fmt;
  371. int64_t cmp = 0;
  372. perf_hpp__for_each_sort_list(fmt) {
  373. cmp = fmt->cmp(left, right);
  374. if (cmp)
  375. break;
  376. }
  377. return cmp;
  378. }
  379. int64_t
  380. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  381. {
  382. struct perf_hpp_fmt *fmt;
  383. int64_t cmp = 0;
  384. perf_hpp__for_each_sort_list(fmt) {
  385. cmp = fmt->collapse(left, right);
  386. if (cmp)
  387. break;
  388. }
  389. return cmp;
  390. }
  391. void hist_entry__free(struct hist_entry *he)
  392. {
  393. zfree(&he->branch_info);
  394. zfree(&he->mem_info);
  395. free_srcline(he->srcline);
  396. free(he);
  397. }
  398. /*
  399. * collapse the histogram
  400. */
  401. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  402. struct rb_root *root,
  403. struct hist_entry *he)
  404. {
  405. struct rb_node **p = &root->rb_node;
  406. struct rb_node *parent = NULL;
  407. struct hist_entry *iter;
  408. int64_t cmp;
  409. while (*p != NULL) {
  410. parent = *p;
  411. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  412. cmp = hist_entry__collapse(iter, he);
  413. if (!cmp) {
  414. he_stat__add_stat(&iter->stat, &he->stat);
  415. if (symbol_conf.use_callchain) {
  416. callchain_cursor_reset(&callchain_cursor);
  417. callchain_merge(&callchain_cursor,
  418. iter->callchain,
  419. he->callchain);
  420. }
  421. hist_entry__free(he);
  422. return false;
  423. }
  424. if (cmp < 0)
  425. p = &(*p)->rb_left;
  426. else
  427. p = &(*p)->rb_right;
  428. }
  429. rb_link_node(&he->rb_node_in, parent, p);
  430. rb_insert_color(&he->rb_node_in, root);
  431. return true;
  432. }
  433. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  434. {
  435. struct rb_root *root;
  436. pthread_mutex_lock(&hists->lock);
  437. root = hists->entries_in;
  438. if (++hists->entries_in > &hists->entries_in_array[1])
  439. hists->entries_in = &hists->entries_in_array[0];
  440. pthread_mutex_unlock(&hists->lock);
  441. return root;
  442. }
  443. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  444. {
  445. hists__filter_entry_by_dso(hists, he);
  446. hists__filter_entry_by_thread(hists, he);
  447. hists__filter_entry_by_symbol(hists, he);
  448. }
  449. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  450. {
  451. struct rb_root *root;
  452. struct rb_node *next;
  453. struct hist_entry *n;
  454. if (!sort__need_collapse)
  455. return;
  456. root = hists__get_rotate_entries_in(hists);
  457. next = rb_first(root);
  458. while (next) {
  459. if (session_done())
  460. break;
  461. n = rb_entry(next, struct hist_entry, rb_node_in);
  462. next = rb_next(&n->rb_node_in);
  463. rb_erase(&n->rb_node_in, root);
  464. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  465. /*
  466. * If it wasn't combined with one of the entries already
  467. * collapsed, we need to apply the filters that may have
  468. * been set by, say, the hist_browser.
  469. */
  470. hists__apply_filters(hists, n);
  471. }
  472. if (prog)
  473. ui_progress__update(prog, 1);
  474. }
  475. }
  476. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  477. {
  478. struct perf_hpp_fmt *fmt;
  479. int64_t cmp = 0;
  480. perf_hpp__for_each_format(fmt) {
  481. cmp = fmt->sort(a, b);
  482. if (cmp)
  483. break;
  484. }
  485. return cmp;
  486. }
  487. static void hists__reset_filter_stats(struct hists *hists)
  488. {
  489. hists->nr_non_filtered_entries = 0;
  490. hists->stats.total_non_filtered_period = 0;
  491. }
  492. void hists__reset_stats(struct hists *hists)
  493. {
  494. hists->nr_entries = 0;
  495. hists->stats.total_period = 0;
  496. hists__reset_filter_stats(hists);
  497. }
  498. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  499. {
  500. hists->nr_non_filtered_entries++;
  501. hists->stats.total_non_filtered_period += h->stat.period;
  502. }
  503. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  504. {
  505. if (!h->filtered)
  506. hists__inc_filter_stats(hists, h);
  507. hists->nr_entries++;
  508. hists->stats.total_period += h->stat.period;
  509. }
  510. static void __hists__insert_output_entry(struct rb_root *entries,
  511. struct hist_entry *he,
  512. u64 min_callchain_hits)
  513. {
  514. struct rb_node **p = &entries->rb_node;
  515. struct rb_node *parent = NULL;
  516. struct hist_entry *iter;
  517. if (symbol_conf.use_callchain)
  518. callchain_param.sort(&he->sorted_chain, he->callchain,
  519. min_callchain_hits, &callchain_param);
  520. while (*p != NULL) {
  521. parent = *p;
  522. iter = rb_entry(parent, struct hist_entry, rb_node);
  523. if (hist_entry__sort(he, iter) > 0)
  524. p = &(*p)->rb_left;
  525. else
  526. p = &(*p)->rb_right;
  527. }
  528. rb_link_node(&he->rb_node, parent, p);
  529. rb_insert_color(&he->rb_node, entries);
  530. }
  531. void hists__output_resort(struct hists *hists)
  532. {
  533. struct rb_root *root;
  534. struct rb_node *next;
  535. struct hist_entry *n;
  536. u64 min_callchain_hits;
  537. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  538. if (sort__need_collapse)
  539. root = &hists->entries_collapsed;
  540. else
  541. root = hists->entries_in;
  542. next = rb_first(root);
  543. hists->entries = RB_ROOT;
  544. hists__reset_stats(hists);
  545. hists__reset_col_len(hists);
  546. while (next) {
  547. n = rb_entry(next, struct hist_entry, rb_node_in);
  548. next = rb_next(&n->rb_node_in);
  549. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  550. hists__inc_stats(hists, n);
  551. if (!n->filtered)
  552. hists__calc_col_len(hists, n);
  553. }
  554. }
  555. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  556. enum hist_filter filter)
  557. {
  558. h->filtered &= ~(1 << filter);
  559. if (h->filtered)
  560. return;
  561. /* force fold unfiltered entry for simplicity */
  562. h->ms.unfolded = false;
  563. h->row_offset = 0;
  564. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  565. hists__inc_filter_stats(hists, h);
  566. hists__calc_col_len(hists, h);
  567. }
  568. static bool hists__filter_entry_by_dso(struct hists *hists,
  569. struct hist_entry *he)
  570. {
  571. if (hists->dso_filter != NULL &&
  572. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  573. he->filtered |= (1 << HIST_FILTER__DSO);
  574. return true;
  575. }
  576. return false;
  577. }
  578. void hists__filter_by_dso(struct hists *hists)
  579. {
  580. struct rb_node *nd;
  581. hists->stats.nr_non_filtered_samples = 0;
  582. hists__reset_filter_stats(hists);
  583. hists__reset_col_len(hists);
  584. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  585. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  586. if (symbol_conf.exclude_other && !h->parent)
  587. continue;
  588. if (hists__filter_entry_by_dso(hists, h))
  589. continue;
  590. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  591. }
  592. }
  593. static bool hists__filter_entry_by_thread(struct hists *hists,
  594. struct hist_entry *he)
  595. {
  596. if (hists->thread_filter != NULL &&
  597. he->thread != hists->thread_filter) {
  598. he->filtered |= (1 << HIST_FILTER__THREAD);
  599. return true;
  600. }
  601. return false;
  602. }
  603. void hists__filter_by_thread(struct hists *hists)
  604. {
  605. struct rb_node *nd;
  606. hists->stats.nr_non_filtered_samples = 0;
  607. hists__reset_filter_stats(hists);
  608. hists__reset_col_len(hists);
  609. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  610. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  611. if (hists__filter_entry_by_thread(hists, h))
  612. continue;
  613. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  614. }
  615. }
  616. static bool hists__filter_entry_by_symbol(struct hists *hists,
  617. struct hist_entry *he)
  618. {
  619. if (hists->symbol_filter_str != NULL &&
  620. (!he->ms.sym || strstr(he->ms.sym->name,
  621. hists->symbol_filter_str) == NULL)) {
  622. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  623. return true;
  624. }
  625. return false;
  626. }
  627. void hists__filter_by_symbol(struct hists *hists)
  628. {
  629. struct rb_node *nd;
  630. hists->stats.nr_non_filtered_samples = 0;
  631. hists__reset_filter_stats(hists);
  632. hists__reset_col_len(hists);
  633. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  634. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  635. if (hists__filter_entry_by_symbol(hists, h))
  636. continue;
  637. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  638. }
  639. }
  640. void events_stats__inc(struct events_stats *stats, u32 type)
  641. {
  642. ++stats->nr_events[0];
  643. ++stats->nr_events[type];
  644. }
  645. void hists__inc_nr_events(struct hists *hists, u32 type)
  646. {
  647. events_stats__inc(&hists->stats, type);
  648. }
  649. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  650. struct hist_entry *pair)
  651. {
  652. struct rb_root *root;
  653. struct rb_node **p;
  654. struct rb_node *parent = NULL;
  655. struct hist_entry *he;
  656. int64_t cmp;
  657. if (sort__need_collapse)
  658. root = &hists->entries_collapsed;
  659. else
  660. root = hists->entries_in;
  661. p = &root->rb_node;
  662. while (*p != NULL) {
  663. parent = *p;
  664. he = rb_entry(parent, struct hist_entry, rb_node_in);
  665. cmp = hist_entry__collapse(he, pair);
  666. if (!cmp)
  667. goto out;
  668. if (cmp < 0)
  669. p = &(*p)->rb_left;
  670. else
  671. p = &(*p)->rb_right;
  672. }
  673. he = hist_entry__new(pair);
  674. if (he) {
  675. memset(&he->stat, 0, sizeof(he->stat));
  676. he->hists = hists;
  677. rb_link_node(&he->rb_node_in, parent, p);
  678. rb_insert_color(&he->rb_node_in, root);
  679. hists__inc_stats(hists, he);
  680. he->dummy = true;
  681. }
  682. out:
  683. return he;
  684. }
  685. static struct hist_entry *hists__find_entry(struct hists *hists,
  686. struct hist_entry *he)
  687. {
  688. struct rb_node *n;
  689. if (sort__need_collapse)
  690. n = hists->entries_collapsed.rb_node;
  691. else
  692. n = hists->entries_in->rb_node;
  693. while (n) {
  694. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  695. int64_t cmp = hist_entry__collapse(iter, he);
  696. if (cmp < 0)
  697. n = n->rb_left;
  698. else if (cmp > 0)
  699. n = n->rb_right;
  700. else
  701. return iter;
  702. }
  703. return NULL;
  704. }
  705. /*
  706. * Look for pairs to link to the leader buckets (hist_entries):
  707. */
  708. void hists__match(struct hists *leader, struct hists *other)
  709. {
  710. struct rb_root *root;
  711. struct rb_node *nd;
  712. struct hist_entry *pos, *pair;
  713. if (sort__need_collapse)
  714. root = &leader->entries_collapsed;
  715. else
  716. root = leader->entries_in;
  717. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  718. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  719. pair = hists__find_entry(other, pos);
  720. if (pair)
  721. hist_entry__add_pair(pair, pos);
  722. }
  723. }
  724. /*
  725. * Look for entries in the other hists that are not present in the leader, if
  726. * we find them, just add a dummy entry on the leader hists, with period=0,
  727. * nr_events=0, to serve as the list header.
  728. */
  729. int hists__link(struct hists *leader, struct hists *other)
  730. {
  731. struct rb_root *root;
  732. struct rb_node *nd;
  733. struct hist_entry *pos, *pair;
  734. if (sort__need_collapse)
  735. root = &other->entries_collapsed;
  736. else
  737. root = other->entries_in;
  738. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  739. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  740. if (!hist_entry__has_pairs(pos)) {
  741. pair = hists__add_dummy_entry(leader, pos);
  742. if (pair == NULL)
  743. return -1;
  744. hist_entry__add_pair(pos, pair);
  745. }
  746. }
  747. return 0;
  748. }
  749. u64 hists__total_period(struct hists *hists)
  750. {
  751. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  752. hists->stats.total_period;
  753. }
  754. int parse_filter_percentage(const struct option *opt __maybe_unused,
  755. const char *arg, int unset __maybe_unused)
  756. {
  757. if (!strcmp(arg, "relative"))
  758. symbol_conf.filter_relative = true;
  759. else if (!strcmp(arg, "absolute"))
  760. symbol_conf.filter_relative = false;
  761. else
  762. return -1;
  763. return 0;
  764. }
  765. int perf_hist_config(const char *var, const char *value)
  766. {
  767. if (!strcmp(var, "hist.percentage"))
  768. return parse_filter_percentage(NULL, value, 0);
  769. return 0;
  770. }