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