hist.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. static void __hists__decay_entries(struct hists *hists, bool zap_user,
  209. bool zap_kernel, bool threaded)
  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 || threaded)
  227. rb_erase(&n->rb_node_in, &hists->entries_collapsed);
  228. hist_entry__free(n);
  229. --hists->nr_entries;
  230. }
  231. }
  232. }
  233. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  234. {
  235. return __hists__decay_entries(hists, zap_user, zap_kernel, false);
  236. }
  237. void hists__decay_entries_threaded(struct hists *hists,
  238. bool zap_user, bool zap_kernel)
  239. {
  240. return __hists__decay_entries(hists, zap_user, zap_kernel, true);
  241. }
  242. /*
  243. * histogram, sorted on item, collects periods
  244. */
  245. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  246. {
  247. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  248. struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
  249. if (he != NULL) {
  250. *he = *template;
  251. if (he->ms.map)
  252. he->ms.map->referenced = true;
  253. if (he->branch_info) {
  254. /*
  255. * This branch info is (a part of) allocated from
  256. * machine__resolve_bstack() and will be freed after
  257. * adding new entries. So we need to save a copy.
  258. */
  259. he->branch_info = malloc(sizeof(*he->branch_info));
  260. if (he->branch_info == NULL) {
  261. free(he);
  262. return NULL;
  263. }
  264. memcpy(he->branch_info, template->branch_info,
  265. sizeof(*he->branch_info));
  266. if (he->branch_info->from.map)
  267. he->branch_info->from.map->referenced = true;
  268. if (he->branch_info->to.map)
  269. he->branch_info->to.map->referenced = true;
  270. }
  271. if (he->mem_info) {
  272. if (he->mem_info->iaddr.map)
  273. he->mem_info->iaddr.map->referenced = true;
  274. if (he->mem_info->daddr.map)
  275. he->mem_info->daddr.map->referenced = true;
  276. }
  277. if (symbol_conf.use_callchain)
  278. callchain_init(he->callchain);
  279. INIT_LIST_HEAD(&he->pairs.node);
  280. }
  281. return he;
  282. }
  283. void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  284. {
  285. if (!h->filtered) {
  286. hists__calc_col_len(hists, h);
  287. ++hists->nr_entries;
  288. hists->stats.total_period += h->stat.period;
  289. }
  290. }
  291. static u8 symbol__parent_filter(const struct symbol *parent)
  292. {
  293. if (symbol_conf.exclude_other && parent == NULL)
  294. return 1 << HIST_FILTER__PARENT;
  295. return 0;
  296. }
  297. static struct hist_entry *add_hist_entry(struct hists *hists,
  298. struct hist_entry *entry,
  299. struct addr_location *al,
  300. u64 period,
  301. u64 weight)
  302. {
  303. struct rb_node **p;
  304. struct rb_node *parent = NULL;
  305. struct hist_entry *he;
  306. int cmp;
  307. pthread_mutex_lock(&hists->lock);
  308. p = &hists->entries_in->rb_node;
  309. while (*p != NULL) {
  310. parent = *p;
  311. he = rb_entry(parent, struct hist_entry, rb_node_in);
  312. /*
  313. * Make sure that it receives arguments in a same order as
  314. * hist_entry__collapse() so that we can use an appropriate
  315. * function when searching an entry regardless which sort
  316. * keys were used.
  317. */
  318. cmp = hist_entry__cmp(he, entry);
  319. if (!cmp) {
  320. he_stat__add_period(&he->stat, period, weight);
  321. /*
  322. * This mem info was allocated from machine__resolve_mem
  323. * and will not be used anymore.
  324. */
  325. free(entry->mem_info);
  326. /* If the map of an existing hist_entry has
  327. * become out-of-date due to an exec() or
  328. * similar, update it. Otherwise we will
  329. * mis-adjust symbol addresses when computing
  330. * the history counter to increment.
  331. */
  332. if (he->ms.map != entry->ms.map) {
  333. he->ms.map = entry->ms.map;
  334. if (he->ms.map)
  335. he->ms.map->referenced = true;
  336. }
  337. goto out;
  338. }
  339. if (cmp < 0)
  340. p = &(*p)->rb_left;
  341. else
  342. p = &(*p)->rb_right;
  343. }
  344. he = hist_entry__new(entry);
  345. if (!he)
  346. goto out_unlock;
  347. rb_link_node(&he->rb_node_in, parent, p);
  348. rb_insert_color(&he->rb_node_in, hists->entries_in);
  349. out:
  350. hist_entry__add_cpumode_period(he, al->cpumode, period);
  351. out_unlock:
  352. pthread_mutex_unlock(&hists->lock);
  353. return he;
  354. }
  355. struct hist_entry *__hists__add_mem_entry(struct hists *self,
  356. struct addr_location *al,
  357. struct symbol *sym_parent,
  358. struct mem_info *mi,
  359. u64 period,
  360. u64 weight)
  361. {
  362. struct hist_entry entry = {
  363. .thread = al->thread,
  364. .ms = {
  365. .map = al->map,
  366. .sym = al->sym,
  367. },
  368. .stat = {
  369. .period = period,
  370. .weight = weight,
  371. .nr_events = 1,
  372. },
  373. .cpu = al->cpu,
  374. .ip = al->addr,
  375. .level = al->level,
  376. .parent = sym_parent,
  377. .filtered = symbol__parent_filter(sym_parent),
  378. .hists = self,
  379. .mem_info = mi,
  380. .branch_info = NULL,
  381. };
  382. return add_hist_entry(self, &entry, al, period, weight);
  383. }
  384. struct hist_entry *__hists__add_branch_entry(struct hists *self,
  385. struct addr_location *al,
  386. struct symbol *sym_parent,
  387. struct branch_info *bi,
  388. u64 period,
  389. u64 weight)
  390. {
  391. struct hist_entry entry = {
  392. .thread = al->thread,
  393. .ms = {
  394. .map = bi->to.map,
  395. .sym = bi->to.sym,
  396. },
  397. .cpu = al->cpu,
  398. .ip = bi->to.addr,
  399. .level = al->level,
  400. .stat = {
  401. .period = period,
  402. .nr_events = 1,
  403. .weight = weight,
  404. },
  405. .parent = sym_parent,
  406. .filtered = symbol__parent_filter(sym_parent),
  407. .branch_info = bi,
  408. .hists = self,
  409. .mem_info = NULL,
  410. };
  411. return add_hist_entry(self, &entry, al, period, weight);
  412. }
  413. struct hist_entry *__hists__add_entry(struct hists *self,
  414. struct addr_location *al,
  415. struct symbol *sym_parent, u64 period,
  416. u64 weight)
  417. {
  418. struct hist_entry entry = {
  419. .thread = al->thread,
  420. .ms = {
  421. .map = al->map,
  422. .sym = al->sym,
  423. },
  424. .cpu = al->cpu,
  425. .ip = al->addr,
  426. .level = al->level,
  427. .stat = {
  428. .period = period,
  429. .nr_events = 1,
  430. .weight = weight,
  431. },
  432. .parent = sym_parent,
  433. .filtered = symbol__parent_filter(sym_parent),
  434. .hists = self,
  435. .branch_info = NULL,
  436. .mem_info = NULL,
  437. };
  438. return add_hist_entry(self, &entry, al, period, weight);
  439. }
  440. int64_t
  441. hist_entry__cmp(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. cmp = se->se_cmp(left, right);
  447. if (cmp)
  448. break;
  449. }
  450. return cmp;
  451. }
  452. int64_t
  453. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  454. {
  455. struct sort_entry *se;
  456. int64_t cmp = 0;
  457. list_for_each_entry(se, &hist_entry__sort_list, list) {
  458. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  459. f = se->se_collapse ?: se->se_cmp;
  460. cmp = f(left, right);
  461. if (cmp)
  462. break;
  463. }
  464. return cmp;
  465. }
  466. void hist_entry__free(struct hist_entry *he)
  467. {
  468. free(he->branch_info);
  469. free(he->mem_info);
  470. free(he);
  471. }
  472. /*
  473. * collapse the histogram
  474. */
  475. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  476. struct rb_root *root,
  477. struct hist_entry *he)
  478. {
  479. struct rb_node **p = &root->rb_node;
  480. struct rb_node *parent = NULL;
  481. struct hist_entry *iter;
  482. int64_t cmp;
  483. while (*p != NULL) {
  484. parent = *p;
  485. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  486. cmp = hist_entry__collapse(iter, he);
  487. if (!cmp) {
  488. he_stat__add_stat(&iter->stat, &he->stat);
  489. if (symbol_conf.use_callchain) {
  490. callchain_cursor_reset(&callchain_cursor);
  491. callchain_merge(&callchain_cursor,
  492. iter->callchain,
  493. he->callchain);
  494. }
  495. hist_entry__free(he);
  496. return false;
  497. }
  498. if (cmp < 0)
  499. p = &(*p)->rb_left;
  500. else
  501. p = &(*p)->rb_right;
  502. }
  503. rb_link_node(&he->rb_node_in, parent, p);
  504. rb_insert_color(&he->rb_node_in, root);
  505. return true;
  506. }
  507. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  508. {
  509. struct rb_root *root;
  510. pthread_mutex_lock(&hists->lock);
  511. root = hists->entries_in;
  512. if (++hists->entries_in > &hists->entries_in_array[1])
  513. hists->entries_in = &hists->entries_in_array[0];
  514. pthread_mutex_unlock(&hists->lock);
  515. return root;
  516. }
  517. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  518. {
  519. hists__filter_entry_by_dso(hists, he);
  520. hists__filter_entry_by_thread(hists, he);
  521. hists__filter_entry_by_symbol(hists, he);
  522. }
  523. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  524. {
  525. struct rb_root *root;
  526. struct rb_node *next;
  527. struct hist_entry *n;
  528. if (!sort__need_collapse && !threaded)
  529. return;
  530. root = hists__get_rotate_entries_in(hists);
  531. next = rb_first(root);
  532. while (next) {
  533. n = rb_entry(next, struct hist_entry, rb_node_in);
  534. next = rb_next(&n->rb_node_in);
  535. rb_erase(&n->rb_node_in, root);
  536. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  537. /*
  538. * If it wasn't combined with one of the entries already
  539. * collapsed, we need to apply the filters that may have
  540. * been set by, say, the hist_browser.
  541. */
  542. hists__apply_filters(hists, n);
  543. }
  544. }
  545. }
  546. void hists__collapse_resort(struct hists *hists)
  547. {
  548. return __hists__collapse_resort(hists, false);
  549. }
  550. void hists__collapse_resort_threaded(struct hists *hists)
  551. {
  552. return __hists__collapse_resort(hists, true);
  553. }
  554. /*
  555. * reverse the map, sort on period.
  556. */
  557. static int period_cmp(u64 period_a, u64 period_b)
  558. {
  559. if (period_a > period_b)
  560. return 1;
  561. if (period_a < period_b)
  562. return -1;
  563. return 0;
  564. }
  565. static int hist_entry__sort_on_period(struct hist_entry *a,
  566. struct hist_entry *b)
  567. {
  568. int ret;
  569. int i, nr_members;
  570. struct perf_evsel *evsel;
  571. struct hist_entry *pair;
  572. u64 *periods_a, *periods_b;
  573. ret = period_cmp(a->stat.period, b->stat.period);
  574. if (ret || !symbol_conf.event_group)
  575. return ret;
  576. evsel = hists_to_evsel(a->hists);
  577. nr_members = evsel->nr_members;
  578. if (nr_members <= 1)
  579. return ret;
  580. periods_a = zalloc(sizeof(periods_a) * nr_members);
  581. periods_b = zalloc(sizeof(periods_b) * nr_members);
  582. if (!periods_a || !periods_b)
  583. goto out;
  584. list_for_each_entry(pair, &a->pairs.head, pairs.node) {
  585. evsel = hists_to_evsel(pair->hists);
  586. periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
  587. }
  588. list_for_each_entry(pair, &b->pairs.head, pairs.node) {
  589. evsel = hists_to_evsel(pair->hists);
  590. periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
  591. }
  592. for (i = 1; i < nr_members; i++) {
  593. ret = period_cmp(periods_a[i], periods_b[i]);
  594. if (ret)
  595. break;
  596. }
  597. out:
  598. free(periods_a);
  599. free(periods_b);
  600. return ret;
  601. }
  602. static void __hists__insert_output_entry(struct rb_root *entries,
  603. struct hist_entry *he,
  604. u64 min_callchain_hits)
  605. {
  606. struct rb_node **p = &entries->rb_node;
  607. struct rb_node *parent = NULL;
  608. struct hist_entry *iter;
  609. if (symbol_conf.use_callchain)
  610. callchain_param.sort(&he->sorted_chain, he->callchain,
  611. min_callchain_hits, &callchain_param);
  612. while (*p != NULL) {
  613. parent = *p;
  614. iter = rb_entry(parent, struct hist_entry, rb_node);
  615. if (hist_entry__sort_on_period(he, iter) > 0)
  616. p = &(*p)->rb_left;
  617. else
  618. p = &(*p)->rb_right;
  619. }
  620. rb_link_node(&he->rb_node, parent, p);
  621. rb_insert_color(&he->rb_node, entries);
  622. }
  623. static void __hists__output_resort(struct hists *hists, bool threaded)
  624. {
  625. struct rb_root *root;
  626. struct rb_node *next;
  627. struct hist_entry *n;
  628. u64 min_callchain_hits;
  629. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  630. if (sort__need_collapse || threaded)
  631. root = &hists->entries_collapsed;
  632. else
  633. root = hists->entries_in;
  634. next = rb_first(root);
  635. hists->entries = RB_ROOT;
  636. hists->nr_entries = 0;
  637. hists->stats.total_period = 0;
  638. hists__reset_col_len(hists);
  639. while (next) {
  640. n = rb_entry(next, struct hist_entry, rb_node_in);
  641. next = rb_next(&n->rb_node_in);
  642. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  643. hists__inc_nr_entries(hists, n);
  644. }
  645. }
  646. void hists__output_resort(struct hists *hists)
  647. {
  648. return __hists__output_resort(hists, false);
  649. }
  650. void hists__output_resort_threaded(struct hists *hists)
  651. {
  652. return __hists__output_resort(hists, true);
  653. }
  654. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  655. enum hist_filter filter)
  656. {
  657. h->filtered &= ~(1 << filter);
  658. if (h->filtered)
  659. return;
  660. ++hists->nr_entries;
  661. if (h->ms.unfolded)
  662. hists->nr_entries += h->nr_rows;
  663. h->row_offset = 0;
  664. hists->stats.total_period += h->stat.period;
  665. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
  666. hists__calc_col_len(hists, h);
  667. }
  668. static bool hists__filter_entry_by_dso(struct hists *hists,
  669. struct hist_entry *he)
  670. {
  671. if (hists->dso_filter != NULL &&
  672. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  673. he->filtered |= (1 << HIST_FILTER__DSO);
  674. return true;
  675. }
  676. return false;
  677. }
  678. void hists__filter_by_dso(struct hists *hists)
  679. {
  680. struct rb_node *nd;
  681. hists->nr_entries = hists->stats.total_period = 0;
  682. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  683. hists__reset_col_len(hists);
  684. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  685. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  686. if (symbol_conf.exclude_other && !h->parent)
  687. continue;
  688. if (hists__filter_entry_by_dso(hists, h))
  689. continue;
  690. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  691. }
  692. }
  693. static bool hists__filter_entry_by_thread(struct hists *hists,
  694. struct hist_entry *he)
  695. {
  696. if (hists->thread_filter != NULL &&
  697. he->thread != hists->thread_filter) {
  698. he->filtered |= (1 << HIST_FILTER__THREAD);
  699. return true;
  700. }
  701. return false;
  702. }
  703. void hists__filter_by_thread(struct hists *hists)
  704. {
  705. struct rb_node *nd;
  706. hists->nr_entries = hists->stats.total_period = 0;
  707. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  708. hists__reset_col_len(hists);
  709. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  710. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  711. if (hists__filter_entry_by_thread(hists, h))
  712. continue;
  713. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  714. }
  715. }
  716. static bool hists__filter_entry_by_symbol(struct hists *hists,
  717. struct hist_entry *he)
  718. {
  719. if (hists->symbol_filter_str != NULL &&
  720. (!he->ms.sym || strstr(he->ms.sym->name,
  721. hists->symbol_filter_str) == NULL)) {
  722. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  723. return true;
  724. }
  725. return false;
  726. }
  727. void hists__filter_by_symbol(struct hists *hists)
  728. {
  729. struct rb_node *nd;
  730. hists->nr_entries = hists->stats.total_period = 0;
  731. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  732. hists__reset_col_len(hists);
  733. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  734. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  735. if (hists__filter_entry_by_symbol(hists, h))
  736. continue;
  737. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  738. }
  739. }
  740. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  741. {
  742. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  743. }
  744. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  745. {
  746. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  747. }
  748. void events_stats__inc(struct events_stats *stats, u32 type)
  749. {
  750. ++stats->nr_events[0];
  751. ++stats->nr_events[type];
  752. }
  753. void hists__inc_nr_events(struct hists *hists, u32 type)
  754. {
  755. events_stats__inc(&hists->stats, type);
  756. }
  757. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  758. struct hist_entry *pair)
  759. {
  760. struct rb_root *root;
  761. struct rb_node **p;
  762. struct rb_node *parent = NULL;
  763. struct hist_entry *he;
  764. int cmp;
  765. if (sort__need_collapse)
  766. root = &hists->entries_collapsed;
  767. else
  768. root = hists->entries_in;
  769. p = &root->rb_node;
  770. while (*p != NULL) {
  771. parent = *p;
  772. he = rb_entry(parent, struct hist_entry, rb_node_in);
  773. cmp = hist_entry__collapse(he, pair);
  774. if (!cmp)
  775. goto out;
  776. if (cmp < 0)
  777. p = &(*p)->rb_left;
  778. else
  779. p = &(*p)->rb_right;
  780. }
  781. he = hist_entry__new(pair);
  782. if (he) {
  783. memset(&he->stat, 0, sizeof(he->stat));
  784. he->hists = hists;
  785. rb_link_node(&he->rb_node_in, parent, p);
  786. rb_insert_color(&he->rb_node_in, root);
  787. hists__inc_nr_entries(hists, he);
  788. }
  789. out:
  790. return he;
  791. }
  792. static struct hist_entry *hists__find_entry(struct hists *hists,
  793. struct hist_entry *he)
  794. {
  795. struct rb_node *n;
  796. if (sort__need_collapse)
  797. n = hists->entries_collapsed.rb_node;
  798. else
  799. n = hists->entries_in->rb_node;
  800. while (n) {
  801. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  802. int64_t cmp = hist_entry__collapse(iter, he);
  803. if (cmp < 0)
  804. n = n->rb_left;
  805. else if (cmp > 0)
  806. n = n->rb_right;
  807. else
  808. return iter;
  809. }
  810. return NULL;
  811. }
  812. /*
  813. * Look for pairs to link to the leader buckets (hist_entries):
  814. */
  815. void hists__match(struct hists *leader, struct hists *other)
  816. {
  817. struct rb_root *root;
  818. struct rb_node *nd;
  819. struct hist_entry *pos, *pair;
  820. if (sort__need_collapse)
  821. root = &leader->entries_collapsed;
  822. else
  823. root = leader->entries_in;
  824. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  825. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  826. pair = hists__find_entry(other, pos);
  827. if (pair)
  828. hist_entry__add_pair(pair, pos);
  829. }
  830. }
  831. /*
  832. * Look for entries in the other hists that are not present in the leader, if
  833. * we find them, just add a dummy entry on the leader hists, with period=0,
  834. * nr_events=0, to serve as the list header.
  835. */
  836. int hists__link(struct hists *leader, struct hists *other)
  837. {
  838. struct rb_root *root;
  839. struct rb_node *nd;
  840. struct hist_entry *pos, *pair;
  841. if (sort__need_collapse)
  842. root = &other->entries_collapsed;
  843. else
  844. root = other->entries_in;
  845. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  846. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  847. if (!hist_entry__has_pairs(pos)) {
  848. pair = hists__add_dummy_entry(leader, pos);
  849. if (pair == NULL)
  850. return -1;
  851. hist_entry__add_pair(pos, pair);
  852. }
  853. }
  854. return 0;
  855. }