hist.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. #include "util.h"
  2. #include "build-id.h"
  3. #include "hist.h"
  4. #include "session.h"
  5. #include "sort.h"
  6. #include "evlist.h"
  7. #include "evsel.h"
  8. #include "annotate.h"
  9. #include "ui/progress.h"
  10. #include <math.h>
  11. static bool hists__filter_entry_by_dso(struct hists *hists,
  12. struct hist_entry *he);
  13. static bool hists__filter_entry_by_thread(struct hists *hists,
  14. struct hist_entry *he);
  15. static bool hists__filter_entry_by_symbol(struct hists *hists,
  16. struct hist_entry *he);
  17. u16 hists__col_len(struct hists *hists, enum hist_column col)
  18. {
  19. return hists->col_len[col];
  20. }
  21. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
  22. {
  23. hists->col_len[col] = len;
  24. }
  25. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
  26. {
  27. if (len > hists__col_len(hists, col)) {
  28. hists__set_col_len(hists, col, len);
  29. return true;
  30. }
  31. return false;
  32. }
  33. void hists__reset_col_len(struct hists *hists)
  34. {
  35. enum hist_column col;
  36. for (col = 0; col < HISTC_NR_COLS; ++col)
  37. hists__set_col_len(hists, col, 0);
  38. }
  39. static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
  40. {
  41. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  42. if (hists__col_len(hists, dso) < unresolved_col_width &&
  43. !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  44. !symbol_conf.dso_list)
  45. hists__set_col_len(hists, dso, unresolved_col_width);
  46. }
  47. void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
  48. {
  49. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  50. int symlen;
  51. u16 len;
  52. /*
  53. * +4 accounts for '[x] ' priv level info
  54. * +2 accounts for 0x prefix on raw addresses
  55. * +3 accounts for ' y ' symtab origin info
  56. */
  57. if (h->ms.sym) {
  58. symlen = h->ms.sym->namelen + 4;
  59. if (verbose)
  60. symlen += BITS_PER_LONG / 4 + 2 + 3;
  61. hists__new_col_len(hists, HISTC_SYMBOL, symlen);
  62. } else {
  63. symlen = unresolved_col_width + 4 + 2;
  64. hists__new_col_len(hists, HISTC_SYMBOL, symlen);
  65. hists__set_unres_dso_col_len(hists, HISTC_DSO);
  66. }
  67. len = thread__comm_len(h->thread);
  68. if (hists__new_col_len(hists, HISTC_COMM, len))
  69. hists__set_col_len(hists, HISTC_THREAD, len + 6);
  70. if (h->ms.map) {
  71. len = dso__name_len(h->ms.map->dso);
  72. hists__new_col_len(hists, HISTC_DSO, len);
  73. }
  74. if (h->parent)
  75. hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
  76. if (h->branch_info) {
  77. if (h->branch_info->from.sym) {
  78. symlen = (int)h->branch_info->from.sym->namelen + 4;
  79. if (verbose)
  80. symlen += BITS_PER_LONG / 4 + 2 + 3;
  81. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  82. symlen = dso__name_len(h->branch_info->from.map->dso);
  83. hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
  84. } else {
  85. symlen = unresolved_col_width + 4 + 2;
  86. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  87. hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
  88. }
  89. if (h->branch_info->to.sym) {
  90. symlen = (int)h->branch_info->to.sym->namelen + 4;
  91. if (verbose)
  92. symlen += BITS_PER_LONG / 4 + 2 + 3;
  93. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  94. symlen = dso__name_len(h->branch_info->to.map->dso);
  95. hists__new_col_len(hists, HISTC_DSO_TO, symlen);
  96. } else {
  97. symlen = unresolved_col_width + 4 + 2;
  98. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  99. hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
  100. }
  101. }
  102. if (h->mem_info) {
  103. if (h->mem_info->daddr.sym) {
  104. symlen = (int)h->mem_info->daddr.sym->namelen + 4
  105. + unresolved_col_width + 2;
  106. hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
  107. symlen);
  108. hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
  109. symlen + 1);
  110. } else {
  111. symlen = unresolved_col_width + 4 + 2;
  112. hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
  113. symlen);
  114. }
  115. if (h->mem_info->daddr.map) {
  116. symlen = dso__name_len(h->mem_info->daddr.map->dso);
  117. hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
  118. symlen);
  119. } else {
  120. symlen = unresolved_col_width + 4 + 2;
  121. hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
  122. }
  123. } else {
  124. symlen = unresolved_col_width + 4 + 2;
  125. hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
  126. hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
  127. }
  128. hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
  129. hists__new_col_len(hists, HISTC_MEM_TLB, 22);
  130. hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
  131. hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
  132. hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
  133. hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
  134. if (h->srcline)
  135. hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
  136. if (h->srcfile)
  137. hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
  138. if (h->transaction)
  139. hists__new_col_len(hists, HISTC_TRANSACTION,
  140. hist_entry__transaction_len());
  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 he_stat__add_cpumode_period(struct he_stat *he_stat,
  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 he_stat__decay(struct he_stat *he_stat)
  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. u64 diff;
  202. if (prev_period == 0)
  203. return true;
  204. he_stat__decay(&he->stat);
  205. if (symbol_conf.cumulate_callchain)
  206. he_stat__decay(he->stat_acc);
  207. diff = prev_period - he->stat.period;
  208. hists->stats.total_period -= diff;
  209. if (!he->filtered)
  210. hists->stats.total_non_filtered_period -= diff;
  211. return he->stat.period == 0;
  212. }
  213. static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
  214. {
  215. rb_erase(&he->rb_node, &hists->entries);
  216. if (sort__need_collapse)
  217. rb_erase(&he->rb_node_in, &hists->entries_collapsed);
  218. --hists->nr_entries;
  219. if (!he->filtered)
  220. --hists->nr_non_filtered_entries;
  221. hist_entry__delete(he);
  222. }
  223. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  224. {
  225. struct rb_node *next = rb_first(&hists->entries);
  226. struct hist_entry *n;
  227. while (next) {
  228. n = rb_entry(next, struct hist_entry, rb_node);
  229. next = rb_next(&n->rb_node);
  230. if (((zap_user && n->level == '.') ||
  231. (zap_kernel && n->level != '.') ||
  232. hists__decay_entry(hists, n))) {
  233. hists__delete_entry(hists, n);
  234. }
  235. }
  236. }
  237. void hists__delete_entries(struct hists *hists)
  238. {
  239. struct rb_node *next = rb_first(&hists->entries);
  240. struct hist_entry *n;
  241. while (next) {
  242. n = rb_entry(next, struct hist_entry, rb_node);
  243. next = rb_next(&n->rb_node);
  244. hists__delete_entry(hists, n);
  245. }
  246. }
  247. /*
  248. * histogram, sorted on item, collects periods
  249. */
  250. static struct hist_entry *hist_entry__new(struct hist_entry *template,
  251. bool sample_self)
  252. {
  253. size_t callchain_size = 0;
  254. struct hist_entry *he;
  255. if (symbol_conf.use_callchain)
  256. callchain_size = sizeof(struct callchain_root);
  257. he = zalloc(sizeof(*he) + callchain_size);
  258. if (he != NULL) {
  259. *he = *template;
  260. if (symbol_conf.cumulate_callchain) {
  261. he->stat_acc = malloc(sizeof(he->stat));
  262. if (he->stat_acc == NULL) {
  263. free(he);
  264. return NULL;
  265. }
  266. memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
  267. if (!sample_self)
  268. memset(&he->stat, 0, sizeof(he->stat));
  269. }
  270. map__get(he->ms.map);
  271. if (he->branch_info) {
  272. /*
  273. * This branch info is (a part of) allocated from
  274. * sample__resolve_bstack() and will be freed after
  275. * adding new entries. So we need to save a copy.
  276. */
  277. he->branch_info = malloc(sizeof(*he->branch_info));
  278. if (he->branch_info == NULL) {
  279. map__zput(he->ms.map);
  280. free(he->stat_acc);
  281. free(he);
  282. return NULL;
  283. }
  284. memcpy(he->branch_info, template->branch_info,
  285. sizeof(*he->branch_info));
  286. map__get(he->branch_info->from.map);
  287. map__get(he->branch_info->to.map);
  288. }
  289. if (he->mem_info) {
  290. map__get(he->mem_info->iaddr.map);
  291. map__get(he->mem_info->daddr.map);
  292. }
  293. if (symbol_conf.use_callchain)
  294. callchain_init(he->callchain);
  295. INIT_LIST_HEAD(&he->pairs.node);
  296. thread__get(he->thread);
  297. }
  298. return he;
  299. }
  300. static u8 symbol__parent_filter(const struct symbol *parent)
  301. {
  302. if (symbol_conf.exclude_other && parent == NULL)
  303. return 1 << HIST_FILTER__PARENT;
  304. return 0;
  305. }
  306. static struct hist_entry *hists__findnew_entry(struct hists *hists,
  307. struct hist_entry *entry,
  308. struct addr_location *al,
  309. bool sample_self)
  310. {
  311. struct rb_node **p;
  312. struct rb_node *parent = NULL;
  313. struct hist_entry *he;
  314. int64_t cmp;
  315. u64 period = entry->stat.period;
  316. u64 weight = entry->stat.weight;
  317. p = &hists->entries_in->rb_node;
  318. while (*p != NULL) {
  319. parent = *p;
  320. he = rb_entry(parent, struct hist_entry, rb_node_in);
  321. /*
  322. * Make sure that it receives arguments in a same order as
  323. * hist_entry__collapse() so that we can use an appropriate
  324. * function when searching an entry regardless which sort
  325. * keys were used.
  326. */
  327. cmp = hist_entry__cmp(he, entry);
  328. if (!cmp) {
  329. if (sample_self)
  330. he_stat__add_period(&he->stat, period, weight);
  331. if (symbol_conf.cumulate_callchain)
  332. he_stat__add_period(he->stat_acc, period, weight);
  333. /*
  334. * This mem info was allocated from sample__resolve_mem
  335. * and will not be used anymore.
  336. */
  337. zfree(&entry->mem_info);
  338. /* If the map of an existing hist_entry has
  339. * become out-of-date due to an exec() or
  340. * similar, update it. Otherwise we will
  341. * mis-adjust symbol addresses when computing
  342. * the history counter to increment.
  343. */
  344. if (he->ms.map != entry->ms.map) {
  345. map__put(he->ms.map);
  346. he->ms.map = map__get(entry->ms.map);
  347. }
  348. goto out;
  349. }
  350. if (cmp < 0)
  351. p = &(*p)->rb_left;
  352. else
  353. p = &(*p)->rb_right;
  354. }
  355. he = hist_entry__new(entry, sample_self);
  356. if (!he)
  357. return NULL;
  358. hists->nr_entries++;
  359. rb_link_node(&he->rb_node_in, parent, p);
  360. rb_insert_color(&he->rb_node_in, hists->entries_in);
  361. out:
  362. if (sample_self)
  363. he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
  364. if (symbol_conf.cumulate_callchain)
  365. he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
  366. return he;
  367. }
  368. struct hist_entry *__hists__add_entry(struct hists *hists,
  369. struct addr_location *al,
  370. struct symbol *sym_parent,
  371. struct branch_info *bi,
  372. struct mem_info *mi,
  373. u64 period, u64 weight, u64 transaction,
  374. bool sample_self)
  375. {
  376. struct hist_entry entry = {
  377. .thread = al->thread,
  378. .comm = thread__comm(al->thread),
  379. .ms = {
  380. .map = al->map,
  381. .sym = al->sym,
  382. },
  383. .cpu = al->cpu,
  384. .cpumode = al->cpumode,
  385. .ip = al->addr,
  386. .level = al->level,
  387. .stat = {
  388. .nr_events = 1,
  389. .period = period,
  390. .weight = weight,
  391. },
  392. .parent = sym_parent,
  393. .filtered = symbol__parent_filter(sym_parent) | al->filtered,
  394. .hists = hists,
  395. .branch_info = bi,
  396. .mem_info = mi,
  397. .transaction = transaction,
  398. };
  399. return hists__findnew_entry(hists, &entry, al, sample_self);
  400. }
  401. static int
  402. iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  403. struct addr_location *al __maybe_unused)
  404. {
  405. return 0;
  406. }
  407. static int
  408. iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  409. struct addr_location *al __maybe_unused)
  410. {
  411. return 0;
  412. }
  413. static int
  414. iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  415. {
  416. struct perf_sample *sample = iter->sample;
  417. struct mem_info *mi;
  418. mi = sample__resolve_mem(sample, al);
  419. if (mi == NULL)
  420. return -ENOMEM;
  421. iter->priv = mi;
  422. return 0;
  423. }
  424. static int
  425. iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  426. {
  427. u64 cost;
  428. struct mem_info *mi = iter->priv;
  429. struct hists *hists = evsel__hists(iter->evsel);
  430. struct hist_entry *he;
  431. if (mi == NULL)
  432. return -EINVAL;
  433. cost = iter->sample->weight;
  434. if (!cost)
  435. cost = 1;
  436. /*
  437. * must pass period=weight in order to get the correct
  438. * sorting from hists__collapse_resort() which is solely
  439. * based on periods. We want sorting be done on nr_events * weight
  440. * and this is indirectly achieved by passing period=weight here
  441. * and the he_stat__add_period() function.
  442. */
  443. he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
  444. cost, cost, 0, true);
  445. if (!he)
  446. return -ENOMEM;
  447. iter->he = he;
  448. return 0;
  449. }
  450. static int
  451. iter_finish_mem_entry(struct hist_entry_iter *iter,
  452. struct addr_location *al __maybe_unused)
  453. {
  454. struct perf_evsel *evsel = iter->evsel;
  455. struct hists *hists = evsel__hists(evsel);
  456. struct hist_entry *he = iter->he;
  457. int err = -EINVAL;
  458. if (he == NULL)
  459. goto out;
  460. hists__inc_nr_samples(hists, he->filtered);
  461. err = hist_entry__append_callchain(he, iter->sample);
  462. out:
  463. /*
  464. * We don't need to free iter->priv (mem_info) here since the mem info
  465. * was either already freed in hists__findnew_entry() or passed to a
  466. * new hist entry by hist_entry__new().
  467. */
  468. iter->priv = NULL;
  469. iter->he = NULL;
  470. return err;
  471. }
  472. static int
  473. iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  474. {
  475. struct branch_info *bi;
  476. struct perf_sample *sample = iter->sample;
  477. bi = sample__resolve_bstack(sample, al);
  478. if (!bi)
  479. return -ENOMEM;
  480. iter->curr = 0;
  481. iter->total = sample->branch_stack->nr;
  482. iter->priv = bi;
  483. return 0;
  484. }
  485. static int
  486. iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
  487. struct addr_location *al __maybe_unused)
  488. {
  489. /* to avoid calling callback function */
  490. iter->he = NULL;
  491. return 0;
  492. }
  493. static int
  494. iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  495. {
  496. struct branch_info *bi = iter->priv;
  497. int i = iter->curr;
  498. if (bi == NULL)
  499. return 0;
  500. if (iter->curr >= iter->total)
  501. return 0;
  502. al->map = bi[i].to.map;
  503. al->sym = bi[i].to.sym;
  504. al->addr = bi[i].to.addr;
  505. return 1;
  506. }
  507. static int
  508. iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  509. {
  510. struct branch_info *bi;
  511. struct perf_evsel *evsel = iter->evsel;
  512. struct hists *hists = evsel__hists(evsel);
  513. struct hist_entry *he = NULL;
  514. int i = iter->curr;
  515. int err = 0;
  516. bi = iter->priv;
  517. if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
  518. goto out;
  519. /*
  520. * The report shows the percentage of total branches captured
  521. * and not events sampled. Thus we use a pseudo period of 1.
  522. */
  523. he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
  524. 1, bi->flags.cycles ? bi->flags.cycles : 1,
  525. 0, true);
  526. if (he == NULL)
  527. return -ENOMEM;
  528. hists__inc_nr_samples(hists, he->filtered);
  529. out:
  530. iter->he = he;
  531. iter->curr++;
  532. return err;
  533. }
  534. static int
  535. iter_finish_branch_entry(struct hist_entry_iter *iter,
  536. struct addr_location *al __maybe_unused)
  537. {
  538. zfree(&iter->priv);
  539. iter->he = NULL;
  540. return iter->curr >= iter->total ? 0 : -1;
  541. }
  542. static int
  543. iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
  544. struct addr_location *al __maybe_unused)
  545. {
  546. return 0;
  547. }
  548. static int
  549. iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
  550. {
  551. struct perf_evsel *evsel = iter->evsel;
  552. struct perf_sample *sample = iter->sample;
  553. struct hist_entry *he;
  554. he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
  555. sample->period, sample->weight,
  556. sample->transaction, true);
  557. if (he == NULL)
  558. return -ENOMEM;
  559. iter->he = he;
  560. return 0;
  561. }
  562. static int
  563. iter_finish_normal_entry(struct hist_entry_iter *iter,
  564. struct addr_location *al __maybe_unused)
  565. {
  566. struct hist_entry *he = iter->he;
  567. struct perf_evsel *evsel = iter->evsel;
  568. struct perf_sample *sample = iter->sample;
  569. if (he == NULL)
  570. return 0;
  571. iter->he = NULL;
  572. hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
  573. return hist_entry__append_callchain(he, sample);
  574. }
  575. static int
  576. iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
  577. struct addr_location *al __maybe_unused)
  578. {
  579. struct hist_entry **he_cache;
  580. callchain_cursor_commit(&callchain_cursor);
  581. /*
  582. * This is for detecting cycles or recursions so that they're
  583. * cumulated only one time to prevent entries more than 100%
  584. * overhead.
  585. */
  586. he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
  587. if (he_cache == NULL)
  588. return -ENOMEM;
  589. iter->priv = he_cache;
  590. iter->curr = 0;
  591. return 0;
  592. }
  593. static int
  594. iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
  595. struct addr_location *al)
  596. {
  597. struct perf_evsel *evsel = iter->evsel;
  598. struct hists *hists = evsel__hists(evsel);
  599. struct perf_sample *sample = iter->sample;
  600. struct hist_entry **he_cache = iter->priv;
  601. struct hist_entry *he;
  602. int err = 0;
  603. he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
  604. sample->period, sample->weight,
  605. sample->transaction, true);
  606. if (he == NULL)
  607. return -ENOMEM;
  608. iter->he = he;
  609. he_cache[iter->curr++] = he;
  610. hist_entry__append_callchain(he, sample);
  611. /*
  612. * We need to re-initialize the cursor since callchain_append()
  613. * advanced the cursor to the end.
  614. */
  615. callchain_cursor_commit(&callchain_cursor);
  616. hists__inc_nr_samples(hists, he->filtered);
  617. return err;
  618. }
  619. static int
  620. iter_next_cumulative_entry(struct hist_entry_iter *iter,
  621. struct addr_location *al)
  622. {
  623. struct callchain_cursor_node *node;
  624. node = callchain_cursor_current(&callchain_cursor);
  625. if (node == NULL)
  626. return 0;
  627. return fill_callchain_info(al, node, iter->hide_unresolved);
  628. }
  629. static int
  630. iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
  631. struct addr_location *al)
  632. {
  633. struct perf_evsel *evsel = iter->evsel;
  634. struct perf_sample *sample = iter->sample;
  635. struct hist_entry **he_cache = iter->priv;
  636. struct hist_entry *he;
  637. struct hist_entry he_tmp = {
  638. .hists = evsel__hists(evsel),
  639. .cpu = al->cpu,
  640. .thread = al->thread,
  641. .comm = thread__comm(al->thread),
  642. .ip = al->addr,
  643. .ms = {
  644. .map = al->map,
  645. .sym = al->sym,
  646. },
  647. .parent = iter->parent,
  648. };
  649. int i;
  650. struct callchain_cursor cursor;
  651. callchain_cursor_snapshot(&cursor, &callchain_cursor);
  652. callchain_cursor_advance(&callchain_cursor);
  653. /*
  654. * Check if there's duplicate entries in the callchain.
  655. * It's possible that it has cycles or recursive calls.
  656. */
  657. for (i = 0; i < iter->curr; i++) {
  658. if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
  659. /* to avoid calling callback function */
  660. iter->he = NULL;
  661. return 0;
  662. }
  663. }
  664. he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
  665. sample->period, sample->weight,
  666. sample->transaction, false);
  667. if (he == NULL)
  668. return -ENOMEM;
  669. iter->he = he;
  670. he_cache[iter->curr++] = he;
  671. if (symbol_conf.use_callchain)
  672. callchain_append(he->callchain, &cursor, sample->period);
  673. return 0;
  674. }
  675. static int
  676. iter_finish_cumulative_entry(struct hist_entry_iter *iter,
  677. struct addr_location *al __maybe_unused)
  678. {
  679. zfree(&iter->priv);
  680. iter->he = NULL;
  681. return 0;
  682. }
  683. const struct hist_iter_ops hist_iter_mem = {
  684. .prepare_entry = iter_prepare_mem_entry,
  685. .add_single_entry = iter_add_single_mem_entry,
  686. .next_entry = iter_next_nop_entry,
  687. .add_next_entry = iter_add_next_nop_entry,
  688. .finish_entry = iter_finish_mem_entry,
  689. };
  690. const struct hist_iter_ops hist_iter_branch = {
  691. .prepare_entry = iter_prepare_branch_entry,
  692. .add_single_entry = iter_add_single_branch_entry,
  693. .next_entry = iter_next_branch_entry,
  694. .add_next_entry = iter_add_next_branch_entry,
  695. .finish_entry = iter_finish_branch_entry,
  696. };
  697. const struct hist_iter_ops hist_iter_normal = {
  698. .prepare_entry = iter_prepare_normal_entry,
  699. .add_single_entry = iter_add_single_normal_entry,
  700. .next_entry = iter_next_nop_entry,
  701. .add_next_entry = iter_add_next_nop_entry,
  702. .finish_entry = iter_finish_normal_entry,
  703. };
  704. const struct hist_iter_ops hist_iter_cumulative = {
  705. .prepare_entry = iter_prepare_cumulative_entry,
  706. .add_single_entry = iter_add_single_cumulative_entry,
  707. .next_entry = iter_next_cumulative_entry,
  708. .add_next_entry = iter_add_next_cumulative_entry,
  709. .finish_entry = iter_finish_cumulative_entry,
  710. };
  711. int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
  712. int max_stack_depth, void *arg)
  713. {
  714. int err, err2;
  715. err = sample__resolve_callchain(iter->sample, &iter->parent,
  716. iter->evsel, al, max_stack_depth);
  717. if (err)
  718. return err;
  719. err = iter->ops->prepare_entry(iter, al);
  720. if (err)
  721. goto out;
  722. err = iter->ops->add_single_entry(iter, al);
  723. if (err)
  724. goto out;
  725. if (iter->he && iter->add_entry_cb) {
  726. err = iter->add_entry_cb(iter, al, true, arg);
  727. if (err)
  728. goto out;
  729. }
  730. while (iter->ops->next_entry(iter, al)) {
  731. err = iter->ops->add_next_entry(iter, al);
  732. if (err)
  733. break;
  734. if (iter->he && iter->add_entry_cb) {
  735. err = iter->add_entry_cb(iter, al, false, arg);
  736. if (err)
  737. goto out;
  738. }
  739. }
  740. out:
  741. err2 = iter->ops->finish_entry(iter, al);
  742. if (!err)
  743. err = err2;
  744. return err;
  745. }
  746. int64_t
  747. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  748. {
  749. struct perf_hpp_fmt *fmt;
  750. int64_t cmp = 0;
  751. perf_hpp__for_each_sort_list(fmt) {
  752. if (perf_hpp__should_skip(fmt))
  753. continue;
  754. cmp = fmt->cmp(fmt, left, right);
  755. if (cmp)
  756. break;
  757. }
  758. return cmp;
  759. }
  760. int64_t
  761. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  762. {
  763. struct perf_hpp_fmt *fmt;
  764. int64_t cmp = 0;
  765. perf_hpp__for_each_sort_list(fmt) {
  766. if (perf_hpp__should_skip(fmt))
  767. continue;
  768. cmp = fmt->collapse(fmt, left, right);
  769. if (cmp)
  770. break;
  771. }
  772. return cmp;
  773. }
  774. void hist_entry__delete(struct hist_entry *he)
  775. {
  776. thread__zput(he->thread);
  777. map__zput(he->ms.map);
  778. if (he->branch_info) {
  779. map__zput(he->branch_info->from.map);
  780. map__zput(he->branch_info->to.map);
  781. zfree(&he->branch_info);
  782. }
  783. if (he->mem_info) {
  784. map__zput(he->mem_info->iaddr.map);
  785. map__zput(he->mem_info->daddr.map);
  786. zfree(&he->mem_info);
  787. }
  788. zfree(&he->stat_acc);
  789. free_srcline(he->srcline);
  790. if (he->srcfile && he->srcfile[0])
  791. free(he->srcfile);
  792. free_callchain(he->callchain);
  793. free(he);
  794. }
  795. /*
  796. * collapse the histogram
  797. */
  798. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  799. struct rb_root *root,
  800. struct hist_entry *he)
  801. {
  802. struct rb_node **p = &root->rb_node;
  803. struct rb_node *parent = NULL;
  804. struct hist_entry *iter;
  805. int64_t cmp;
  806. while (*p != NULL) {
  807. parent = *p;
  808. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  809. cmp = hist_entry__collapse(iter, he);
  810. if (!cmp) {
  811. he_stat__add_stat(&iter->stat, &he->stat);
  812. if (symbol_conf.cumulate_callchain)
  813. he_stat__add_stat(iter->stat_acc, he->stat_acc);
  814. if (symbol_conf.use_callchain) {
  815. callchain_cursor_reset(&callchain_cursor);
  816. callchain_merge(&callchain_cursor,
  817. iter->callchain,
  818. he->callchain);
  819. }
  820. hist_entry__delete(he);
  821. return false;
  822. }
  823. if (cmp < 0)
  824. p = &(*p)->rb_left;
  825. else
  826. p = &(*p)->rb_right;
  827. }
  828. hists->nr_entries++;
  829. rb_link_node(&he->rb_node_in, parent, p);
  830. rb_insert_color(&he->rb_node_in, root);
  831. return true;
  832. }
  833. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  834. {
  835. struct rb_root *root;
  836. pthread_mutex_lock(&hists->lock);
  837. root = hists->entries_in;
  838. if (++hists->entries_in > &hists->entries_in_array[1])
  839. hists->entries_in = &hists->entries_in_array[0];
  840. pthread_mutex_unlock(&hists->lock);
  841. return root;
  842. }
  843. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  844. {
  845. hists__filter_entry_by_dso(hists, he);
  846. hists__filter_entry_by_thread(hists, he);
  847. hists__filter_entry_by_symbol(hists, he);
  848. }
  849. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  850. {
  851. struct rb_root *root;
  852. struct rb_node *next;
  853. struct hist_entry *n;
  854. if (!sort__need_collapse)
  855. return;
  856. hists->nr_entries = 0;
  857. root = hists__get_rotate_entries_in(hists);
  858. next = rb_first(root);
  859. while (next) {
  860. if (session_done())
  861. break;
  862. n = rb_entry(next, struct hist_entry, rb_node_in);
  863. next = rb_next(&n->rb_node_in);
  864. rb_erase(&n->rb_node_in, root);
  865. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  866. /*
  867. * If it wasn't combined with one of the entries already
  868. * collapsed, we need to apply the filters that may have
  869. * been set by, say, the hist_browser.
  870. */
  871. hists__apply_filters(hists, n);
  872. }
  873. if (prog)
  874. ui_progress__update(prog, 1);
  875. }
  876. }
  877. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  878. {
  879. struct perf_hpp_fmt *fmt;
  880. int64_t cmp = 0;
  881. perf_hpp__for_each_sort_list(fmt) {
  882. if (perf_hpp__should_skip(fmt))
  883. continue;
  884. cmp = fmt->sort(fmt, a, b);
  885. if (cmp)
  886. break;
  887. }
  888. return cmp;
  889. }
  890. static void hists__reset_filter_stats(struct hists *hists)
  891. {
  892. hists->nr_non_filtered_entries = 0;
  893. hists->stats.total_non_filtered_period = 0;
  894. }
  895. void hists__reset_stats(struct hists *hists)
  896. {
  897. hists->nr_entries = 0;
  898. hists->stats.total_period = 0;
  899. hists__reset_filter_stats(hists);
  900. }
  901. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  902. {
  903. hists->nr_non_filtered_entries++;
  904. hists->stats.total_non_filtered_period += h->stat.period;
  905. }
  906. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  907. {
  908. if (!h->filtered)
  909. hists__inc_filter_stats(hists, h);
  910. hists->nr_entries++;
  911. hists->stats.total_period += h->stat.period;
  912. }
  913. static void __hists__insert_output_entry(struct rb_root *entries,
  914. struct hist_entry *he,
  915. u64 min_callchain_hits,
  916. bool use_callchain)
  917. {
  918. struct rb_node **p = &entries->rb_node;
  919. struct rb_node *parent = NULL;
  920. struct hist_entry *iter;
  921. if (use_callchain)
  922. callchain_param.sort(&he->sorted_chain, he->callchain,
  923. min_callchain_hits, &callchain_param);
  924. while (*p != NULL) {
  925. parent = *p;
  926. iter = rb_entry(parent, struct hist_entry, rb_node);
  927. if (hist_entry__sort(he, iter) > 0)
  928. p = &(*p)->rb_left;
  929. else
  930. p = &(*p)->rb_right;
  931. }
  932. rb_link_node(&he->rb_node, parent, p);
  933. rb_insert_color(&he->rb_node, entries);
  934. }
  935. void hists__output_resort(struct hists *hists, struct ui_progress *prog)
  936. {
  937. struct rb_root *root;
  938. struct rb_node *next;
  939. struct hist_entry *n;
  940. u64 min_callchain_hits;
  941. struct perf_evsel *evsel = hists_to_evsel(hists);
  942. bool use_callchain;
  943. if (evsel && !symbol_conf.show_ref_callgraph)
  944. use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
  945. else
  946. use_callchain = symbol_conf.use_callchain;
  947. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  948. if (sort__need_collapse)
  949. root = &hists->entries_collapsed;
  950. else
  951. root = hists->entries_in;
  952. next = rb_first(root);
  953. hists->entries = RB_ROOT;
  954. hists__reset_stats(hists);
  955. hists__reset_col_len(hists);
  956. while (next) {
  957. n = rb_entry(next, struct hist_entry, rb_node_in);
  958. next = rb_next(&n->rb_node_in);
  959. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
  960. hists__inc_stats(hists, n);
  961. if (!n->filtered)
  962. hists__calc_col_len(hists, n);
  963. if (prog)
  964. ui_progress__update(prog, 1);
  965. }
  966. }
  967. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  968. enum hist_filter filter)
  969. {
  970. h->filtered &= ~(1 << filter);
  971. if (h->filtered)
  972. return;
  973. /* force fold unfiltered entry for simplicity */
  974. h->unfolded = false;
  975. h->row_offset = 0;
  976. h->nr_rows = 0;
  977. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  978. hists__inc_filter_stats(hists, h);
  979. hists__calc_col_len(hists, h);
  980. }
  981. static bool hists__filter_entry_by_dso(struct hists *hists,
  982. struct hist_entry *he)
  983. {
  984. if (hists->dso_filter != NULL &&
  985. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  986. he->filtered |= (1 << HIST_FILTER__DSO);
  987. return true;
  988. }
  989. return false;
  990. }
  991. void hists__filter_by_dso(struct hists *hists)
  992. {
  993. struct rb_node *nd;
  994. hists->stats.nr_non_filtered_samples = 0;
  995. hists__reset_filter_stats(hists);
  996. hists__reset_col_len(hists);
  997. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  998. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  999. if (symbol_conf.exclude_other && !h->parent)
  1000. continue;
  1001. if (hists__filter_entry_by_dso(hists, h))
  1002. continue;
  1003. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  1004. }
  1005. }
  1006. static bool hists__filter_entry_by_thread(struct hists *hists,
  1007. struct hist_entry *he)
  1008. {
  1009. if (hists->thread_filter != NULL &&
  1010. he->thread != hists->thread_filter) {
  1011. he->filtered |= (1 << HIST_FILTER__THREAD);
  1012. return true;
  1013. }
  1014. return false;
  1015. }
  1016. void hists__filter_by_thread(struct hists *hists)
  1017. {
  1018. struct rb_node *nd;
  1019. hists->stats.nr_non_filtered_samples = 0;
  1020. hists__reset_filter_stats(hists);
  1021. hists__reset_col_len(hists);
  1022. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1023. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1024. if (hists__filter_entry_by_thread(hists, h))
  1025. continue;
  1026. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  1027. }
  1028. }
  1029. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1030. struct hist_entry *he)
  1031. {
  1032. if (hists->symbol_filter_str != NULL &&
  1033. (!he->ms.sym || strstr(he->ms.sym->name,
  1034. hists->symbol_filter_str) == NULL)) {
  1035. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1036. return true;
  1037. }
  1038. return false;
  1039. }
  1040. void hists__filter_by_symbol(struct hists *hists)
  1041. {
  1042. struct rb_node *nd;
  1043. hists->stats.nr_non_filtered_samples = 0;
  1044. hists__reset_filter_stats(hists);
  1045. hists__reset_col_len(hists);
  1046. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1047. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1048. if (hists__filter_entry_by_symbol(hists, h))
  1049. continue;
  1050. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  1051. }
  1052. }
  1053. void events_stats__inc(struct events_stats *stats, u32 type)
  1054. {
  1055. ++stats->nr_events[0];
  1056. ++stats->nr_events[type];
  1057. }
  1058. void hists__inc_nr_events(struct hists *hists, u32 type)
  1059. {
  1060. events_stats__inc(&hists->stats, type);
  1061. }
  1062. void hists__inc_nr_samples(struct hists *hists, bool filtered)
  1063. {
  1064. events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
  1065. if (!filtered)
  1066. hists->stats.nr_non_filtered_samples++;
  1067. }
  1068. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  1069. struct hist_entry *pair)
  1070. {
  1071. struct rb_root *root;
  1072. struct rb_node **p;
  1073. struct rb_node *parent = NULL;
  1074. struct hist_entry *he;
  1075. int64_t cmp;
  1076. if (sort__need_collapse)
  1077. root = &hists->entries_collapsed;
  1078. else
  1079. root = hists->entries_in;
  1080. p = &root->rb_node;
  1081. while (*p != NULL) {
  1082. parent = *p;
  1083. he = rb_entry(parent, struct hist_entry, rb_node_in);
  1084. cmp = hist_entry__collapse(he, pair);
  1085. if (!cmp)
  1086. goto out;
  1087. if (cmp < 0)
  1088. p = &(*p)->rb_left;
  1089. else
  1090. p = &(*p)->rb_right;
  1091. }
  1092. he = hist_entry__new(pair, true);
  1093. if (he) {
  1094. memset(&he->stat, 0, sizeof(he->stat));
  1095. he->hists = hists;
  1096. rb_link_node(&he->rb_node_in, parent, p);
  1097. rb_insert_color(&he->rb_node_in, root);
  1098. hists__inc_stats(hists, he);
  1099. he->dummy = true;
  1100. }
  1101. out:
  1102. return he;
  1103. }
  1104. static struct hist_entry *hists__find_entry(struct hists *hists,
  1105. struct hist_entry *he)
  1106. {
  1107. struct rb_node *n;
  1108. if (sort__need_collapse)
  1109. n = hists->entries_collapsed.rb_node;
  1110. else
  1111. n = hists->entries_in->rb_node;
  1112. while (n) {
  1113. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  1114. int64_t cmp = hist_entry__collapse(iter, he);
  1115. if (cmp < 0)
  1116. n = n->rb_left;
  1117. else if (cmp > 0)
  1118. n = n->rb_right;
  1119. else
  1120. return iter;
  1121. }
  1122. return NULL;
  1123. }
  1124. /*
  1125. * Look for pairs to link to the leader buckets (hist_entries):
  1126. */
  1127. void hists__match(struct hists *leader, struct hists *other)
  1128. {
  1129. struct rb_root *root;
  1130. struct rb_node *nd;
  1131. struct hist_entry *pos, *pair;
  1132. if (sort__need_collapse)
  1133. root = &leader->entries_collapsed;
  1134. else
  1135. root = leader->entries_in;
  1136. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1137. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1138. pair = hists__find_entry(other, pos);
  1139. if (pair)
  1140. hist_entry__add_pair(pair, pos);
  1141. }
  1142. }
  1143. /*
  1144. * Look for entries in the other hists that are not present in the leader, if
  1145. * we find them, just add a dummy entry on the leader hists, with period=0,
  1146. * nr_events=0, to serve as the list header.
  1147. */
  1148. int hists__link(struct hists *leader, struct hists *other)
  1149. {
  1150. struct rb_root *root;
  1151. struct rb_node *nd;
  1152. struct hist_entry *pos, *pair;
  1153. if (sort__need_collapse)
  1154. root = &other->entries_collapsed;
  1155. else
  1156. root = other->entries_in;
  1157. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1158. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1159. if (!hist_entry__has_pairs(pos)) {
  1160. pair = hists__add_dummy_entry(leader, pos);
  1161. if (pair == NULL)
  1162. return -1;
  1163. hist_entry__add_pair(pos, pair);
  1164. }
  1165. }
  1166. return 0;
  1167. }
  1168. void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
  1169. struct perf_sample *sample, bool nonany_branch_mode)
  1170. {
  1171. struct branch_info *bi;
  1172. /* If we have branch cycles always annotate them. */
  1173. if (bs && bs->nr && bs->entries[0].flags.cycles) {
  1174. int i;
  1175. bi = sample__resolve_bstack(sample, al);
  1176. if (bi) {
  1177. struct addr_map_symbol *prev = NULL;
  1178. /*
  1179. * Ignore errors, still want to process the
  1180. * other entries.
  1181. *
  1182. * For non standard branch modes always
  1183. * force no IPC (prev == NULL)
  1184. *
  1185. * Note that perf stores branches reversed from
  1186. * program order!
  1187. */
  1188. for (i = bs->nr - 1; i >= 0; i--) {
  1189. addr_map_symbol__account_cycles(&bi[i].from,
  1190. nonany_branch_mode ? NULL : prev,
  1191. bi[i].flags.cycles);
  1192. prev = &bi[i].to;
  1193. }
  1194. free(bi);
  1195. }
  1196. }
  1197. }
  1198. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
  1199. {
  1200. struct perf_evsel *pos;
  1201. size_t ret = 0;
  1202. evlist__for_each(evlist, pos) {
  1203. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1204. ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
  1205. }
  1206. return ret;
  1207. }
  1208. u64 hists__total_period(struct hists *hists)
  1209. {
  1210. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  1211. hists->stats.total_period;
  1212. }
  1213. int parse_filter_percentage(const struct option *opt __maybe_unused,
  1214. const char *arg, int unset __maybe_unused)
  1215. {
  1216. if (!strcmp(arg, "relative"))
  1217. symbol_conf.filter_relative = true;
  1218. else if (!strcmp(arg, "absolute"))
  1219. symbol_conf.filter_relative = false;
  1220. else
  1221. return -1;
  1222. return 0;
  1223. }
  1224. int perf_hist_config(const char *var, const char *value)
  1225. {
  1226. if (!strcmp(var, "hist.percentage"))
  1227. return parse_filter_percentage(NULL, value, 0);
  1228. return 0;
  1229. }
  1230. static int hists_evsel__init(struct perf_evsel *evsel)
  1231. {
  1232. struct hists *hists = evsel__hists(evsel);
  1233. memset(hists, 0, sizeof(*hists));
  1234. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  1235. hists->entries_in = &hists->entries_in_array[0];
  1236. hists->entries_collapsed = RB_ROOT;
  1237. hists->entries = RB_ROOT;
  1238. pthread_mutex_init(&hists->lock, NULL);
  1239. return 0;
  1240. }
  1241. /*
  1242. * XXX We probably need a hists_evsel__exit() to free the hist_entries
  1243. * stored in the rbtree...
  1244. */
  1245. int hists__init(void)
  1246. {
  1247. int err = perf_evsel__object_config(sizeof(struct hists_evsel),
  1248. hists_evsel__init, NULL);
  1249. if (err)
  1250. fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
  1251. return err;
  1252. }