hist.c 34 KB

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