hist.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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. if (((zap_user && n->level == '.') ||
  227. (zap_kernel && n->level != '.') ||
  228. hists__decay_entry(hists, n))) {
  229. hists__delete_entry(hists, n);
  230. }
  231. }
  232. }
  233. void hists__delete_entries(struct hists *hists)
  234. {
  235. struct rb_node *next = rb_first(&hists->entries);
  236. struct hist_entry *n;
  237. while (next) {
  238. n = rb_entry(next, struct hist_entry, rb_node);
  239. next = rb_next(&n->rb_node);
  240. hists__delete_entry(hists, n);
  241. }
  242. }
  243. /*
  244. * histogram, sorted on item, collects periods
  245. */
  246. static struct hist_entry *hist_entry__new(struct hist_entry *template,
  247. bool sample_self)
  248. {
  249. size_t callchain_size = 0;
  250. struct hist_entry *he;
  251. if (symbol_conf.use_callchain)
  252. callchain_size = sizeof(struct callchain_root);
  253. he = zalloc(sizeof(*he) + callchain_size);
  254. if (he != NULL) {
  255. *he = *template;
  256. if (symbol_conf.cumulate_callchain) {
  257. he->stat_acc = malloc(sizeof(he->stat));
  258. if (he->stat_acc == NULL) {
  259. free(he);
  260. return NULL;
  261. }
  262. memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
  263. if (!sample_self)
  264. memset(&he->stat, 0, sizeof(he->stat));
  265. }
  266. if (he->ms.map)
  267. he->ms.map->referenced = true;
  268. if (he->branch_info) {
  269. /*
  270. * This branch info is (a part of) allocated from
  271. * sample__resolve_bstack() and will be freed after
  272. * adding new entries. So we need to save a copy.
  273. */
  274. he->branch_info = malloc(sizeof(*he->branch_info));
  275. if (he->branch_info == NULL) {
  276. free(he->stat_acc);
  277. free(he);
  278. return NULL;
  279. }
  280. memcpy(he->branch_info, template->branch_info,
  281. sizeof(*he->branch_info));
  282. if (he->branch_info->from.map)
  283. he->branch_info->from.map->referenced = true;
  284. if (he->branch_info->to.map)
  285. he->branch_info->to.map->referenced = true;
  286. }
  287. if (he->mem_info) {
  288. if (he->mem_info->iaddr.map)
  289. he->mem_info->iaddr.map->referenced = true;
  290. if (he->mem_info->daddr.map)
  291. he->mem_info->daddr.map->referenced = true;
  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 *add_hist_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. he->ms.map = entry->ms.map;
  346. if (he->ms.map)
  347. he->ms.map->referenced = true;
  348. }
  349. goto out;
  350. }
  351. if (cmp < 0)
  352. p = &(*p)->rb_left;
  353. else
  354. p = &(*p)->rb_right;
  355. }
  356. he = hist_entry__new(entry, sample_self);
  357. if (!he)
  358. return NULL;
  359. hists->nr_entries++;
  360. rb_link_node(&he->rb_node_in, parent, p);
  361. rb_insert_color(&he->rb_node_in, hists->entries_in);
  362. out:
  363. if (sample_self)
  364. he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
  365. if (symbol_conf.cumulate_callchain)
  366. he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
  367. return he;
  368. }
  369. struct hist_entry *__hists__add_entry(struct hists *hists,
  370. struct addr_location *al,
  371. struct symbol *sym_parent,
  372. struct branch_info *bi,
  373. struct mem_info *mi,
  374. u64 period, u64 weight, u64 transaction,
  375. bool sample_self)
  376. {
  377. struct hist_entry entry = {
  378. .thread = al->thread,
  379. .comm = thread__comm(al->thread),
  380. .ms = {
  381. .map = al->map,
  382. .sym = al->sym,
  383. },
  384. .cpu = al->cpu,
  385. .cpumode = al->cpumode,
  386. .ip = al->addr,
  387. .level = al->level,
  388. .stat = {
  389. .nr_events = 1,
  390. .period = period,
  391. .weight = weight,
  392. },
  393. .parent = sym_parent,
  394. .filtered = symbol__parent_filter(sym_parent) | al->filtered,
  395. .hists = hists,
  396. .branch_info = bi,
  397. .mem_info = mi,
  398. .transaction = transaction,
  399. };
  400. return add_hist_entry(hists, &entry, al, sample_self);
  401. }
  402. static int
  403. iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  404. struct addr_location *al __maybe_unused)
  405. {
  406. return 0;
  407. }
  408. static int
  409. iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  410. struct addr_location *al __maybe_unused)
  411. {
  412. return 0;
  413. }
  414. static int
  415. iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  416. {
  417. struct perf_sample *sample = iter->sample;
  418. struct mem_info *mi;
  419. mi = sample__resolve_mem(sample, al);
  420. if (mi == NULL)
  421. return -ENOMEM;
  422. iter->priv = mi;
  423. return 0;
  424. }
  425. static int
  426. iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  427. {
  428. u64 cost;
  429. struct mem_info *mi = iter->priv;
  430. struct hists *hists = evsel__hists(iter->evsel);
  431. struct hist_entry *he;
  432. if (mi == NULL)
  433. return -EINVAL;
  434. cost = iter->sample->weight;
  435. if (!cost)
  436. cost = 1;
  437. /*
  438. * must pass period=weight in order to get the correct
  439. * sorting from hists__collapse_resort() which is solely
  440. * based on periods. We want sorting be done on nr_events * weight
  441. * and this is indirectly achieved by passing period=weight here
  442. * and the he_stat__add_period() function.
  443. */
  444. he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
  445. cost, cost, 0, true);
  446. if (!he)
  447. return -ENOMEM;
  448. iter->he = he;
  449. return 0;
  450. }
  451. static int
  452. iter_finish_mem_entry(struct hist_entry_iter *iter,
  453. struct addr_location *al __maybe_unused)
  454. {
  455. struct perf_evsel *evsel = iter->evsel;
  456. struct hists *hists = evsel__hists(evsel);
  457. struct hist_entry *he = iter->he;
  458. int err = -EINVAL;
  459. if (he == NULL)
  460. goto out;
  461. hists__inc_nr_samples(hists, he->filtered);
  462. err = hist_entry__append_callchain(he, iter->sample);
  463. out:
  464. /*
  465. * We don't need to free iter->priv (mem_info) here since
  466. * the mem info was either already freed in add_hist_entry() or
  467. * passed to a new hist entry by hist_entry__new().
  468. */
  469. iter->priv = NULL;
  470. iter->he = NULL;
  471. return err;
  472. }
  473. static int
  474. iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  475. {
  476. struct branch_info *bi;
  477. struct perf_sample *sample = iter->sample;
  478. bi = sample__resolve_bstack(sample, al);
  479. if (!bi)
  480. return -ENOMEM;
  481. iter->curr = 0;
  482. iter->total = sample->branch_stack->nr;
  483. iter->priv = bi;
  484. return 0;
  485. }
  486. static int
  487. iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
  488. struct addr_location *al __maybe_unused)
  489. {
  490. /* to avoid calling callback function */
  491. iter->he = NULL;
  492. return 0;
  493. }
  494. static int
  495. iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  496. {
  497. struct branch_info *bi = iter->priv;
  498. int i = iter->curr;
  499. if (bi == NULL)
  500. return 0;
  501. if (iter->curr >= iter->total)
  502. return 0;
  503. al->map = bi[i].to.map;
  504. al->sym = bi[i].to.sym;
  505. al->addr = bi[i].to.addr;
  506. return 1;
  507. }
  508. static int
  509. iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  510. {
  511. struct branch_info *bi;
  512. struct perf_evsel *evsel = iter->evsel;
  513. struct hists *hists = evsel__hists(evsel);
  514. struct hist_entry *he = NULL;
  515. int i = iter->curr;
  516. int err = 0;
  517. bi = iter->priv;
  518. if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
  519. goto out;
  520. /*
  521. * The report shows the percentage of total branches captured
  522. * and not events sampled. Thus we use a pseudo period of 1.
  523. */
  524. he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
  525. 1, 1, 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. .cpu = al->cpu,
  639. .thread = al->thread,
  640. .comm = thread__comm(al->thread),
  641. .ip = al->addr,
  642. .ms = {
  643. .map = al->map,
  644. .sym = al->sym,
  645. },
  646. .parent = iter->parent,
  647. };
  648. int i;
  649. struct callchain_cursor cursor;
  650. callchain_cursor_snapshot(&cursor, &callchain_cursor);
  651. callchain_cursor_advance(&callchain_cursor);
  652. /*
  653. * Check if there's duplicate entries in the callchain.
  654. * It's possible that it has cycles or recursive calls.
  655. */
  656. for (i = 0; i < iter->curr; i++) {
  657. if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
  658. /* to avoid calling callback function */
  659. iter->he = NULL;
  660. return 0;
  661. }
  662. }
  663. he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
  664. sample->period, sample->weight,
  665. sample->transaction, false);
  666. if (he == NULL)
  667. return -ENOMEM;
  668. iter->he = he;
  669. he_cache[iter->curr++] = he;
  670. if (symbol_conf.use_callchain)
  671. callchain_append(he->callchain, &cursor, sample->period);
  672. return 0;
  673. }
  674. static int
  675. iter_finish_cumulative_entry(struct hist_entry_iter *iter,
  676. struct addr_location *al __maybe_unused)
  677. {
  678. zfree(&iter->priv);
  679. iter->he = NULL;
  680. return 0;
  681. }
  682. const struct hist_iter_ops hist_iter_mem = {
  683. .prepare_entry = iter_prepare_mem_entry,
  684. .add_single_entry = iter_add_single_mem_entry,
  685. .next_entry = iter_next_nop_entry,
  686. .add_next_entry = iter_add_next_nop_entry,
  687. .finish_entry = iter_finish_mem_entry,
  688. };
  689. const struct hist_iter_ops hist_iter_branch = {
  690. .prepare_entry = iter_prepare_branch_entry,
  691. .add_single_entry = iter_add_single_branch_entry,
  692. .next_entry = iter_next_branch_entry,
  693. .add_next_entry = iter_add_next_branch_entry,
  694. .finish_entry = iter_finish_branch_entry,
  695. };
  696. const struct hist_iter_ops hist_iter_normal = {
  697. .prepare_entry = iter_prepare_normal_entry,
  698. .add_single_entry = iter_add_single_normal_entry,
  699. .next_entry = iter_next_nop_entry,
  700. .add_next_entry = iter_add_next_nop_entry,
  701. .finish_entry = iter_finish_normal_entry,
  702. };
  703. const struct hist_iter_ops hist_iter_cumulative = {
  704. .prepare_entry = iter_prepare_cumulative_entry,
  705. .add_single_entry = iter_add_single_cumulative_entry,
  706. .next_entry = iter_next_cumulative_entry,
  707. .add_next_entry = iter_add_next_cumulative_entry,
  708. .finish_entry = iter_finish_cumulative_entry,
  709. };
  710. int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
  711. struct perf_evsel *evsel, struct perf_sample *sample,
  712. int max_stack_depth, void *arg)
  713. {
  714. int err, err2;
  715. err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
  716. max_stack_depth);
  717. if (err)
  718. return err;
  719. iter->evsel = evsel;
  720. iter->sample = sample;
  721. err = iter->ops->prepare_entry(iter, al);
  722. if (err)
  723. goto out;
  724. err = iter->ops->add_single_entry(iter, al);
  725. if (err)
  726. goto out;
  727. if (iter->he && iter->add_entry_cb) {
  728. err = iter->add_entry_cb(iter, al, true, arg);
  729. if (err)
  730. goto out;
  731. }
  732. while (iter->ops->next_entry(iter, al)) {
  733. err = iter->ops->add_next_entry(iter, al);
  734. if (err)
  735. break;
  736. if (iter->he && iter->add_entry_cb) {
  737. err = iter->add_entry_cb(iter, al, false, arg);
  738. if (err)
  739. goto out;
  740. }
  741. }
  742. out:
  743. err2 = iter->ops->finish_entry(iter, al);
  744. if (!err)
  745. err = err2;
  746. return err;
  747. }
  748. int64_t
  749. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  750. {
  751. struct perf_hpp_fmt *fmt;
  752. int64_t cmp = 0;
  753. perf_hpp__for_each_sort_list(fmt) {
  754. if (perf_hpp__should_skip(fmt))
  755. continue;
  756. cmp = fmt->cmp(fmt, left, right);
  757. if (cmp)
  758. break;
  759. }
  760. return cmp;
  761. }
  762. int64_t
  763. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  764. {
  765. struct perf_hpp_fmt *fmt;
  766. int64_t cmp = 0;
  767. perf_hpp__for_each_sort_list(fmt) {
  768. if (perf_hpp__should_skip(fmt))
  769. continue;
  770. cmp = fmt->collapse(fmt, left, right);
  771. if (cmp)
  772. break;
  773. }
  774. return cmp;
  775. }
  776. void hist_entry__delete(struct hist_entry *he)
  777. {
  778. thread__zput(he->thread);
  779. zfree(&he->branch_info);
  780. zfree(&he->mem_info);
  781. zfree(&he->stat_acc);
  782. free_srcline(he->srcline);
  783. free_callchain(he->callchain);
  784. free(he);
  785. }
  786. /*
  787. * collapse the histogram
  788. */
  789. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  790. struct rb_root *root,
  791. struct hist_entry *he)
  792. {
  793. struct rb_node **p = &root->rb_node;
  794. struct rb_node *parent = NULL;
  795. struct hist_entry *iter;
  796. int64_t cmp;
  797. while (*p != NULL) {
  798. parent = *p;
  799. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  800. cmp = hist_entry__collapse(iter, he);
  801. if (!cmp) {
  802. he_stat__add_stat(&iter->stat, &he->stat);
  803. if (symbol_conf.cumulate_callchain)
  804. he_stat__add_stat(iter->stat_acc, he->stat_acc);
  805. if (symbol_conf.use_callchain) {
  806. callchain_cursor_reset(&callchain_cursor);
  807. callchain_merge(&callchain_cursor,
  808. iter->callchain,
  809. he->callchain);
  810. }
  811. hist_entry__delete(he);
  812. return false;
  813. }
  814. if (cmp < 0)
  815. p = &(*p)->rb_left;
  816. else
  817. p = &(*p)->rb_right;
  818. }
  819. hists->nr_entries++;
  820. rb_link_node(&he->rb_node_in, parent, p);
  821. rb_insert_color(&he->rb_node_in, root);
  822. return true;
  823. }
  824. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  825. {
  826. struct rb_root *root;
  827. pthread_mutex_lock(&hists->lock);
  828. root = hists->entries_in;
  829. if (++hists->entries_in > &hists->entries_in_array[1])
  830. hists->entries_in = &hists->entries_in_array[0];
  831. pthread_mutex_unlock(&hists->lock);
  832. return root;
  833. }
  834. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  835. {
  836. hists__filter_entry_by_dso(hists, he);
  837. hists__filter_entry_by_thread(hists, he);
  838. hists__filter_entry_by_symbol(hists, he);
  839. }
  840. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  841. {
  842. struct rb_root *root;
  843. struct rb_node *next;
  844. struct hist_entry *n;
  845. if (!sort__need_collapse)
  846. return;
  847. hists->nr_entries = 0;
  848. root = hists__get_rotate_entries_in(hists);
  849. next = rb_first(root);
  850. while (next) {
  851. if (session_done())
  852. break;
  853. n = rb_entry(next, struct hist_entry, rb_node_in);
  854. next = rb_next(&n->rb_node_in);
  855. rb_erase(&n->rb_node_in, root);
  856. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  857. /*
  858. * If it wasn't combined with one of the entries already
  859. * collapsed, we need to apply the filters that may have
  860. * been set by, say, the hist_browser.
  861. */
  862. hists__apply_filters(hists, n);
  863. }
  864. if (prog)
  865. ui_progress__update(prog, 1);
  866. }
  867. }
  868. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  869. {
  870. struct perf_hpp_fmt *fmt;
  871. int64_t cmp = 0;
  872. perf_hpp__for_each_sort_list(fmt) {
  873. if (perf_hpp__should_skip(fmt))
  874. continue;
  875. cmp = fmt->sort(fmt, a, b);
  876. if (cmp)
  877. break;
  878. }
  879. return cmp;
  880. }
  881. static void hists__reset_filter_stats(struct hists *hists)
  882. {
  883. hists->nr_non_filtered_entries = 0;
  884. hists->stats.total_non_filtered_period = 0;
  885. }
  886. void hists__reset_stats(struct hists *hists)
  887. {
  888. hists->nr_entries = 0;
  889. hists->stats.total_period = 0;
  890. hists__reset_filter_stats(hists);
  891. }
  892. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  893. {
  894. hists->nr_non_filtered_entries++;
  895. hists->stats.total_non_filtered_period += h->stat.period;
  896. }
  897. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  898. {
  899. if (!h->filtered)
  900. hists__inc_filter_stats(hists, h);
  901. hists->nr_entries++;
  902. hists->stats.total_period += h->stat.period;
  903. }
  904. static void __hists__insert_output_entry(struct rb_root *entries,
  905. struct hist_entry *he,
  906. u64 min_callchain_hits)
  907. {
  908. struct rb_node **p = &entries->rb_node;
  909. struct rb_node *parent = NULL;
  910. struct hist_entry *iter;
  911. if (symbol_conf.use_callchain)
  912. callchain_param.sort(&he->sorted_chain, he->callchain,
  913. min_callchain_hits, &callchain_param);
  914. while (*p != NULL) {
  915. parent = *p;
  916. iter = rb_entry(parent, struct hist_entry, rb_node);
  917. if (hist_entry__sort(he, iter) > 0)
  918. p = &(*p)->rb_left;
  919. else
  920. p = &(*p)->rb_right;
  921. }
  922. rb_link_node(&he->rb_node, parent, p);
  923. rb_insert_color(&he->rb_node, entries);
  924. }
  925. void hists__output_resort(struct hists *hists, struct ui_progress *prog)
  926. {
  927. struct rb_root *root;
  928. struct rb_node *next;
  929. struct hist_entry *n;
  930. u64 min_callchain_hits;
  931. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  932. if (sort__need_collapse)
  933. root = &hists->entries_collapsed;
  934. else
  935. root = hists->entries_in;
  936. next = rb_first(root);
  937. hists->entries = RB_ROOT;
  938. hists__reset_stats(hists);
  939. hists__reset_col_len(hists);
  940. while (next) {
  941. n = rb_entry(next, struct hist_entry, rb_node_in);
  942. next = rb_next(&n->rb_node_in);
  943. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  944. hists__inc_stats(hists, n);
  945. if (!n->filtered)
  946. hists__calc_col_len(hists, n);
  947. if (prog)
  948. ui_progress__update(prog, 1);
  949. }
  950. }
  951. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  952. enum hist_filter filter)
  953. {
  954. h->filtered &= ~(1 << filter);
  955. if (h->filtered)
  956. return;
  957. /* force fold unfiltered entry for simplicity */
  958. h->ms.unfolded = false;
  959. h->row_offset = 0;
  960. h->nr_rows = 0;
  961. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  962. hists__inc_filter_stats(hists, h);
  963. hists__calc_col_len(hists, h);
  964. }
  965. static bool hists__filter_entry_by_dso(struct hists *hists,
  966. struct hist_entry *he)
  967. {
  968. if (hists->dso_filter != NULL &&
  969. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  970. he->filtered |= (1 << HIST_FILTER__DSO);
  971. return true;
  972. }
  973. return false;
  974. }
  975. void hists__filter_by_dso(struct hists *hists)
  976. {
  977. struct rb_node *nd;
  978. hists->stats.nr_non_filtered_samples = 0;
  979. hists__reset_filter_stats(hists);
  980. hists__reset_col_len(hists);
  981. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  982. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  983. if (symbol_conf.exclude_other && !h->parent)
  984. continue;
  985. if (hists__filter_entry_by_dso(hists, h))
  986. continue;
  987. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  988. }
  989. }
  990. static bool hists__filter_entry_by_thread(struct hists *hists,
  991. struct hist_entry *he)
  992. {
  993. if (hists->thread_filter != NULL &&
  994. he->thread != hists->thread_filter) {
  995. he->filtered |= (1 << HIST_FILTER__THREAD);
  996. return true;
  997. }
  998. return false;
  999. }
  1000. void hists__filter_by_thread(struct hists *hists)
  1001. {
  1002. struct rb_node *nd;
  1003. hists->stats.nr_non_filtered_samples = 0;
  1004. hists__reset_filter_stats(hists);
  1005. hists__reset_col_len(hists);
  1006. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1007. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1008. if (hists__filter_entry_by_thread(hists, h))
  1009. continue;
  1010. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  1011. }
  1012. }
  1013. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1014. struct hist_entry *he)
  1015. {
  1016. if (hists->symbol_filter_str != NULL &&
  1017. (!he->ms.sym || strstr(he->ms.sym->name,
  1018. hists->symbol_filter_str) == NULL)) {
  1019. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1020. return true;
  1021. }
  1022. return false;
  1023. }
  1024. void hists__filter_by_symbol(struct hists *hists)
  1025. {
  1026. struct rb_node *nd;
  1027. hists->stats.nr_non_filtered_samples = 0;
  1028. hists__reset_filter_stats(hists);
  1029. hists__reset_col_len(hists);
  1030. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1031. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1032. if (hists__filter_entry_by_symbol(hists, h))
  1033. continue;
  1034. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  1035. }
  1036. }
  1037. void events_stats__inc(struct events_stats *stats, u32 type)
  1038. {
  1039. ++stats->nr_events[0];
  1040. ++stats->nr_events[type];
  1041. }
  1042. void hists__inc_nr_events(struct hists *hists, u32 type)
  1043. {
  1044. events_stats__inc(&hists->stats, type);
  1045. }
  1046. void hists__inc_nr_samples(struct hists *hists, bool filtered)
  1047. {
  1048. events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
  1049. if (!filtered)
  1050. hists->stats.nr_non_filtered_samples++;
  1051. }
  1052. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  1053. struct hist_entry *pair)
  1054. {
  1055. struct rb_root *root;
  1056. struct rb_node **p;
  1057. struct rb_node *parent = NULL;
  1058. struct hist_entry *he;
  1059. int64_t cmp;
  1060. if (sort__need_collapse)
  1061. root = &hists->entries_collapsed;
  1062. else
  1063. root = hists->entries_in;
  1064. p = &root->rb_node;
  1065. while (*p != NULL) {
  1066. parent = *p;
  1067. he = rb_entry(parent, struct hist_entry, rb_node_in);
  1068. cmp = hist_entry__collapse(he, pair);
  1069. if (!cmp)
  1070. goto out;
  1071. if (cmp < 0)
  1072. p = &(*p)->rb_left;
  1073. else
  1074. p = &(*p)->rb_right;
  1075. }
  1076. he = hist_entry__new(pair, true);
  1077. if (he) {
  1078. memset(&he->stat, 0, sizeof(he->stat));
  1079. he->hists = hists;
  1080. rb_link_node(&he->rb_node_in, parent, p);
  1081. rb_insert_color(&he->rb_node_in, root);
  1082. hists__inc_stats(hists, he);
  1083. he->dummy = true;
  1084. }
  1085. out:
  1086. return he;
  1087. }
  1088. static struct hist_entry *hists__find_entry(struct hists *hists,
  1089. struct hist_entry *he)
  1090. {
  1091. struct rb_node *n;
  1092. if (sort__need_collapse)
  1093. n = hists->entries_collapsed.rb_node;
  1094. else
  1095. n = hists->entries_in->rb_node;
  1096. while (n) {
  1097. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  1098. int64_t cmp = hist_entry__collapse(iter, he);
  1099. if (cmp < 0)
  1100. n = n->rb_left;
  1101. else if (cmp > 0)
  1102. n = n->rb_right;
  1103. else
  1104. return iter;
  1105. }
  1106. return NULL;
  1107. }
  1108. /*
  1109. * Look for pairs to link to the leader buckets (hist_entries):
  1110. */
  1111. void hists__match(struct hists *leader, struct hists *other)
  1112. {
  1113. struct rb_root *root;
  1114. struct rb_node *nd;
  1115. struct hist_entry *pos, *pair;
  1116. if (sort__need_collapse)
  1117. root = &leader->entries_collapsed;
  1118. else
  1119. root = leader->entries_in;
  1120. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1121. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1122. pair = hists__find_entry(other, pos);
  1123. if (pair)
  1124. hist_entry__add_pair(pair, pos);
  1125. }
  1126. }
  1127. /*
  1128. * Look for entries in the other hists that are not present in the leader, if
  1129. * we find them, just add a dummy entry on the leader hists, with period=0,
  1130. * nr_events=0, to serve as the list header.
  1131. */
  1132. int hists__link(struct hists *leader, struct hists *other)
  1133. {
  1134. struct rb_root *root;
  1135. struct rb_node *nd;
  1136. struct hist_entry *pos, *pair;
  1137. if (sort__need_collapse)
  1138. root = &other->entries_collapsed;
  1139. else
  1140. root = other->entries_in;
  1141. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1142. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1143. if (!hist_entry__has_pairs(pos)) {
  1144. pair = hists__add_dummy_entry(leader, pos);
  1145. if (pair == NULL)
  1146. return -1;
  1147. hist_entry__add_pair(pos, pair);
  1148. }
  1149. }
  1150. return 0;
  1151. }
  1152. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
  1153. {
  1154. struct perf_evsel *pos;
  1155. size_t ret = 0;
  1156. evlist__for_each(evlist, pos) {
  1157. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1158. ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
  1159. }
  1160. return ret;
  1161. }
  1162. u64 hists__total_period(struct hists *hists)
  1163. {
  1164. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  1165. hists->stats.total_period;
  1166. }
  1167. int parse_filter_percentage(const struct option *opt __maybe_unused,
  1168. const char *arg, int unset __maybe_unused)
  1169. {
  1170. if (!strcmp(arg, "relative"))
  1171. symbol_conf.filter_relative = true;
  1172. else if (!strcmp(arg, "absolute"))
  1173. symbol_conf.filter_relative = false;
  1174. else
  1175. return -1;
  1176. return 0;
  1177. }
  1178. int perf_hist_config(const char *var, const char *value)
  1179. {
  1180. if (!strcmp(var, "hist.percentage"))
  1181. return parse_filter_percentage(NULL, value, 0);
  1182. return 0;
  1183. }
  1184. static int hists_evsel__init(struct perf_evsel *evsel)
  1185. {
  1186. struct hists *hists = evsel__hists(evsel);
  1187. memset(hists, 0, sizeof(*hists));
  1188. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  1189. hists->entries_in = &hists->entries_in_array[0];
  1190. hists->entries_collapsed = RB_ROOT;
  1191. hists->entries = RB_ROOT;
  1192. pthread_mutex_init(&hists->lock, NULL);
  1193. return 0;
  1194. }
  1195. /*
  1196. * XXX We probably need a hists_evsel__exit() to free the hist_entries
  1197. * stored in the rbtree...
  1198. */
  1199. int hists__init(void)
  1200. {
  1201. int err = perf_evsel__object_config(sizeof(struct hists_evsel),
  1202. hists_evsel__init, NULL);
  1203. if (err)
  1204. fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
  1205. return err;
  1206. }