hist.c 23 KB

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