hist.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. if (perf_hpp__should_skip(fmt))
  374. continue;
  375. cmp = fmt->cmp(left, right);
  376. if (cmp)
  377. break;
  378. }
  379. return cmp;
  380. }
  381. int64_t
  382. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  383. {
  384. struct perf_hpp_fmt *fmt;
  385. int64_t cmp = 0;
  386. perf_hpp__for_each_sort_list(fmt) {
  387. if (perf_hpp__should_skip(fmt))
  388. continue;
  389. cmp = fmt->collapse(left, right);
  390. if (cmp)
  391. break;
  392. }
  393. return cmp;
  394. }
  395. void hist_entry__free(struct hist_entry *he)
  396. {
  397. zfree(&he->branch_info);
  398. zfree(&he->mem_info);
  399. free_srcline(he->srcline);
  400. free(he);
  401. }
  402. /*
  403. * collapse the histogram
  404. */
  405. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  406. struct rb_root *root,
  407. struct hist_entry *he)
  408. {
  409. struct rb_node **p = &root->rb_node;
  410. struct rb_node *parent = NULL;
  411. struct hist_entry *iter;
  412. int64_t cmp;
  413. while (*p != NULL) {
  414. parent = *p;
  415. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  416. cmp = hist_entry__collapse(iter, he);
  417. if (!cmp) {
  418. he_stat__add_stat(&iter->stat, &he->stat);
  419. if (symbol_conf.use_callchain) {
  420. callchain_cursor_reset(&callchain_cursor);
  421. callchain_merge(&callchain_cursor,
  422. iter->callchain,
  423. he->callchain);
  424. }
  425. hist_entry__free(he);
  426. return false;
  427. }
  428. if (cmp < 0)
  429. p = &(*p)->rb_left;
  430. else
  431. p = &(*p)->rb_right;
  432. }
  433. rb_link_node(&he->rb_node_in, parent, p);
  434. rb_insert_color(&he->rb_node_in, root);
  435. return true;
  436. }
  437. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  438. {
  439. struct rb_root *root;
  440. pthread_mutex_lock(&hists->lock);
  441. root = hists->entries_in;
  442. if (++hists->entries_in > &hists->entries_in_array[1])
  443. hists->entries_in = &hists->entries_in_array[0];
  444. pthread_mutex_unlock(&hists->lock);
  445. return root;
  446. }
  447. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  448. {
  449. hists__filter_entry_by_dso(hists, he);
  450. hists__filter_entry_by_thread(hists, he);
  451. hists__filter_entry_by_symbol(hists, he);
  452. }
  453. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  454. {
  455. struct rb_root *root;
  456. struct rb_node *next;
  457. struct hist_entry *n;
  458. if (!sort__need_collapse)
  459. return;
  460. root = hists__get_rotate_entries_in(hists);
  461. next = rb_first(root);
  462. while (next) {
  463. if (session_done())
  464. break;
  465. n = rb_entry(next, struct hist_entry, rb_node_in);
  466. next = rb_next(&n->rb_node_in);
  467. rb_erase(&n->rb_node_in, root);
  468. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  469. /*
  470. * If it wasn't combined with one of the entries already
  471. * collapsed, we need to apply the filters that may have
  472. * been set by, say, the hist_browser.
  473. */
  474. hists__apply_filters(hists, n);
  475. }
  476. if (prog)
  477. ui_progress__update(prog, 1);
  478. }
  479. }
  480. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  481. {
  482. struct perf_hpp_fmt *fmt;
  483. int64_t cmp = 0;
  484. perf_hpp__for_each_sort_list(fmt) {
  485. if (perf_hpp__should_skip(fmt))
  486. continue;
  487. cmp = fmt->sort(a, b);
  488. if (cmp)
  489. break;
  490. }
  491. return cmp;
  492. }
  493. static void hists__reset_filter_stats(struct hists *hists)
  494. {
  495. hists->nr_non_filtered_entries = 0;
  496. hists->stats.total_non_filtered_period = 0;
  497. }
  498. void hists__reset_stats(struct hists *hists)
  499. {
  500. hists->nr_entries = 0;
  501. hists->stats.total_period = 0;
  502. hists__reset_filter_stats(hists);
  503. }
  504. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  505. {
  506. hists->nr_non_filtered_entries++;
  507. hists->stats.total_non_filtered_period += h->stat.period;
  508. }
  509. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  510. {
  511. if (!h->filtered)
  512. hists__inc_filter_stats(hists, h);
  513. hists->nr_entries++;
  514. hists->stats.total_period += h->stat.period;
  515. }
  516. static void __hists__insert_output_entry(struct rb_root *entries,
  517. struct hist_entry *he,
  518. u64 min_callchain_hits)
  519. {
  520. struct rb_node **p = &entries->rb_node;
  521. struct rb_node *parent = NULL;
  522. struct hist_entry *iter;
  523. if (symbol_conf.use_callchain)
  524. callchain_param.sort(&he->sorted_chain, he->callchain,
  525. min_callchain_hits, &callchain_param);
  526. while (*p != NULL) {
  527. parent = *p;
  528. iter = rb_entry(parent, struct hist_entry, rb_node);
  529. if (hist_entry__sort(he, iter) > 0)
  530. p = &(*p)->rb_left;
  531. else
  532. p = &(*p)->rb_right;
  533. }
  534. rb_link_node(&he->rb_node, parent, p);
  535. rb_insert_color(&he->rb_node, entries);
  536. }
  537. void hists__output_resort(struct hists *hists)
  538. {
  539. struct rb_root *root;
  540. struct rb_node *next;
  541. struct hist_entry *n;
  542. u64 min_callchain_hits;
  543. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  544. if (sort__need_collapse)
  545. root = &hists->entries_collapsed;
  546. else
  547. root = hists->entries_in;
  548. next = rb_first(root);
  549. hists->entries = RB_ROOT;
  550. hists__reset_stats(hists);
  551. hists__reset_col_len(hists);
  552. while (next) {
  553. n = rb_entry(next, struct hist_entry, rb_node_in);
  554. next = rb_next(&n->rb_node_in);
  555. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  556. hists__inc_stats(hists, n);
  557. if (!n->filtered)
  558. hists__calc_col_len(hists, n);
  559. }
  560. }
  561. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  562. enum hist_filter filter)
  563. {
  564. h->filtered &= ~(1 << filter);
  565. if (h->filtered)
  566. return;
  567. /* force fold unfiltered entry for simplicity */
  568. h->ms.unfolded = false;
  569. h->row_offset = 0;
  570. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  571. hists__inc_filter_stats(hists, h);
  572. hists__calc_col_len(hists, h);
  573. }
  574. static bool hists__filter_entry_by_dso(struct hists *hists,
  575. struct hist_entry *he)
  576. {
  577. if (hists->dso_filter != NULL &&
  578. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  579. he->filtered |= (1 << HIST_FILTER__DSO);
  580. return true;
  581. }
  582. return false;
  583. }
  584. void hists__filter_by_dso(struct hists *hists)
  585. {
  586. struct rb_node *nd;
  587. hists->stats.nr_non_filtered_samples = 0;
  588. hists__reset_filter_stats(hists);
  589. hists__reset_col_len(hists);
  590. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  591. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  592. if (symbol_conf.exclude_other && !h->parent)
  593. continue;
  594. if (hists__filter_entry_by_dso(hists, h))
  595. continue;
  596. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  597. }
  598. }
  599. static bool hists__filter_entry_by_thread(struct hists *hists,
  600. struct hist_entry *he)
  601. {
  602. if (hists->thread_filter != NULL &&
  603. he->thread != hists->thread_filter) {
  604. he->filtered |= (1 << HIST_FILTER__THREAD);
  605. return true;
  606. }
  607. return false;
  608. }
  609. void hists__filter_by_thread(struct hists *hists)
  610. {
  611. struct rb_node *nd;
  612. hists->stats.nr_non_filtered_samples = 0;
  613. hists__reset_filter_stats(hists);
  614. hists__reset_col_len(hists);
  615. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  616. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  617. if (hists__filter_entry_by_thread(hists, h))
  618. continue;
  619. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  620. }
  621. }
  622. static bool hists__filter_entry_by_symbol(struct hists *hists,
  623. struct hist_entry *he)
  624. {
  625. if (hists->symbol_filter_str != NULL &&
  626. (!he->ms.sym || strstr(he->ms.sym->name,
  627. hists->symbol_filter_str) == NULL)) {
  628. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  629. return true;
  630. }
  631. return false;
  632. }
  633. void hists__filter_by_symbol(struct hists *hists)
  634. {
  635. struct rb_node *nd;
  636. hists->stats.nr_non_filtered_samples = 0;
  637. hists__reset_filter_stats(hists);
  638. hists__reset_col_len(hists);
  639. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  640. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  641. if (hists__filter_entry_by_symbol(hists, h))
  642. continue;
  643. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  644. }
  645. }
  646. void events_stats__inc(struct events_stats *stats, u32 type)
  647. {
  648. ++stats->nr_events[0];
  649. ++stats->nr_events[type];
  650. }
  651. void hists__inc_nr_events(struct hists *hists, u32 type)
  652. {
  653. events_stats__inc(&hists->stats, type);
  654. }
  655. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  656. struct hist_entry *pair)
  657. {
  658. struct rb_root *root;
  659. struct rb_node **p;
  660. struct rb_node *parent = NULL;
  661. struct hist_entry *he;
  662. int64_t cmp;
  663. if (sort__need_collapse)
  664. root = &hists->entries_collapsed;
  665. else
  666. root = hists->entries_in;
  667. p = &root->rb_node;
  668. while (*p != NULL) {
  669. parent = *p;
  670. he = rb_entry(parent, struct hist_entry, rb_node_in);
  671. cmp = hist_entry__collapse(he, pair);
  672. if (!cmp)
  673. goto out;
  674. if (cmp < 0)
  675. p = &(*p)->rb_left;
  676. else
  677. p = &(*p)->rb_right;
  678. }
  679. he = hist_entry__new(pair);
  680. if (he) {
  681. memset(&he->stat, 0, sizeof(he->stat));
  682. he->hists = hists;
  683. rb_link_node(&he->rb_node_in, parent, p);
  684. rb_insert_color(&he->rb_node_in, root);
  685. hists__inc_stats(hists, he);
  686. he->dummy = true;
  687. }
  688. out:
  689. return he;
  690. }
  691. static struct hist_entry *hists__find_entry(struct hists *hists,
  692. struct hist_entry *he)
  693. {
  694. struct rb_node *n;
  695. if (sort__need_collapse)
  696. n = hists->entries_collapsed.rb_node;
  697. else
  698. n = hists->entries_in->rb_node;
  699. while (n) {
  700. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  701. int64_t cmp = hist_entry__collapse(iter, he);
  702. if (cmp < 0)
  703. n = n->rb_left;
  704. else if (cmp > 0)
  705. n = n->rb_right;
  706. else
  707. return iter;
  708. }
  709. return NULL;
  710. }
  711. /*
  712. * Look for pairs to link to the leader buckets (hist_entries):
  713. */
  714. void hists__match(struct hists *leader, struct hists *other)
  715. {
  716. struct rb_root *root;
  717. struct rb_node *nd;
  718. struct hist_entry *pos, *pair;
  719. if (sort__need_collapse)
  720. root = &leader->entries_collapsed;
  721. else
  722. root = leader->entries_in;
  723. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  724. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  725. pair = hists__find_entry(other, pos);
  726. if (pair)
  727. hist_entry__add_pair(pair, pos);
  728. }
  729. }
  730. /*
  731. * Look for entries in the other hists that are not present in the leader, if
  732. * we find them, just add a dummy entry on the leader hists, with period=0,
  733. * nr_events=0, to serve as the list header.
  734. */
  735. int hists__link(struct hists *leader, struct hists *other)
  736. {
  737. struct rb_root *root;
  738. struct rb_node *nd;
  739. struct hist_entry *pos, *pair;
  740. if (sort__need_collapse)
  741. root = &other->entries_collapsed;
  742. else
  743. root = other->entries_in;
  744. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  745. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  746. if (!hist_entry__has_pairs(pos)) {
  747. pair = hists__add_dummy_entry(leader, pos);
  748. if (pair == NULL)
  749. return -1;
  750. hist_entry__add_pair(pos, pair);
  751. }
  752. }
  753. return 0;
  754. }
  755. u64 hists__total_period(struct hists *hists)
  756. {
  757. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  758. hists->stats.total_period;
  759. }
  760. int parse_filter_percentage(const struct option *opt __maybe_unused,
  761. const char *arg, int unset __maybe_unused)
  762. {
  763. if (!strcmp(arg, "relative"))
  764. symbol_conf.filter_relative = true;
  765. else if (!strcmp(arg, "absolute"))
  766. symbol_conf.filter_relative = false;
  767. else
  768. return -1;
  769. return 0;
  770. }
  771. int perf_hist_config(const char *var, const char *value)
  772. {
  773. if (!strcmp(var, "hist.percentage"))
  774. return parse_filter_percentage(NULL, value, 0);
  775. return 0;
  776. }