hist.c 23 KB

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