hist.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 *hists__findnew_entry(struct hists *hists,
  307. struct hist_entry *entry,
  308. struct addr_location *al,
  309. bool sample_self)
  310. {
  311. struct rb_node **p;
  312. struct rb_node *parent = NULL;
  313. struct hist_entry *he;
  314. int64_t cmp;
  315. u64 period = entry->stat.period;
  316. u64 weight = entry->stat.weight;
  317. p = &hists->entries_in->rb_node;
  318. while (*p != NULL) {
  319. parent = *p;
  320. he = rb_entry(parent, struct hist_entry, rb_node_in);
  321. /*
  322. * Make sure that it receives arguments in a same order as
  323. * hist_entry__collapse() so that we can use an appropriate
  324. * function when searching an entry regardless which sort
  325. * keys were used.
  326. */
  327. cmp = hist_entry__cmp(he, entry);
  328. if (!cmp) {
  329. if (sample_self)
  330. he_stat__add_period(&he->stat, period, weight);
  331. if (symbol_conf.cumulate_callchain)
  332. he_stat__add_period(he->stat_acc, period, weight);
  333. /*
  334. * This mem info was allocated from sample__resolve_mem
  335. * and will not be used anymore.
  336. */
  337. zfree(&entry->mem_info);
  338. /* If the map of an existing hist_entry has
  339. * become out-of-date due to an exec() or
  340. * similar, update it. Otherwise we will
  341. * mis-adjust symbol addresses when computing
  342. * the history counter to increment.
  343. */
  344. if (he->ms.map != entry->ms.map) {
  345. 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 hists__findnew_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 the mem info
  466. * was either already freed in hists__findnew_entry() or passed to a
  467. * 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. int max_stack_depth, void *arg)
  712. {
  713. int err, err2;
  714. err = sample__resolve_callchain(iter->sample, &iter->parent,
  715. iter->evsel, al, max_stack_depth);
  716. if (err)
  717. return err;
  718. err = iter->ops->prepare_entry(iter, al);
  719. if (err)
  720. goto out;
  721. err = iter->ops->add_single_entry(iter, al);
  722. if (err)
  723. goto out;
  724. if (iter->he && iter->add_entry_cb) {
  725. err = iter->add_entry_cb(iter, al, true, arg);
  726. if (err)
  727. goto out;
  728. }
  729. while (iter->ops->next_entry(iter, al)) {
  730. err = iter->ops->add_next_entry(iter, al);
  731. if (err)
  732. break;
  733. if (iter->he && iter->add_entry_cb) {
  734. err = iter->add_entry_cb(iter, al, false, arg);
  735. if (err)
  736. goto out;
  737. }
  738. }
  739. out:
  740. err2 = iter->ops->finish_entry(iter, al);
  741. if (!err)
  742. err = err2;
  743. return err;
  744. }
  745. int64_t
  746. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  747. {
  748. struct perf_hpp_fmt *fmt;
  749. int64_t cmp = 0;
  750. perf_hpp__for_each_sort_list(fmt) {
  751. if (perf_hpp__should_skip(fmt))
  752. continue;
  753. cmp = fmt->cmp(fmt, left, right);
  754. if (cmp)
  755. break;
  756. }
  757. return cmp;
  758. }
  759. int64_t
  760. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  761. {
  762. struct perf_hpp_fmt *fmt;
  763. int64_t cmp = 0;
  764. perf_hpp__for_each_sort_list(fmt) {
  765. if (perf_hpp__should_skip(fmt))
  766. continue;
  767. cmp = fmt->collapse(fmt, left, right);
  768. if (cmp)
  769. break;
  770. }
  771. return cmp;
  772. }
  773. void hist_entry__delete(struct hist_entry *he)
  774. {
  775. thread__zput(he->thread);
  776. zfree(&he->branch_info);
  777. zfree(&he->mem_info);
  778. zfree(&he->stat_acc);
  779. free_srcline(he->srcline);
  780. free_callchain(he->callchain);
  781. free(he);
  782. }
  783. /*
  784. * collapse the histogram
  785. */
  786. static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  787. struct rb_root *root,
  788. struct hist_entry *he)
  789. {
  790. struct rb_node **p = &root->rb_node;
  791. struct rb_node *parent = NULL;
  792. struct hist_entry *iter;
  793. int64_t cmp;
  794. while (*p != NULL) {
  795. parent = *p;
  796. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  797. cmp = hist_entry__collapse(iter, he);
  798. if (!cmp) {
  799. he_stat__add_stat(&iter->stat, &he->stat);
  800. if (symbol_conf.cumulate_callchain)
  801. he_stat__add_stat(iter->stat_acc, he->stat_acc);
  802. if (symbol_conf.use_callchain) {
  803. callchain_cursor_reset(&callchain_cursor);
  804. callchain_merge(&callchain_cursor,
  805. iter->callchain,
  806. he->callchain);
  807. }
  808. hist_entry__delete(he);
  809. return false;
  810. }
  811. if (cmp < 0)
  812. p = &(*p)->rb_left;
  813. else
  814. p = &(*p)->rb_right;
  815. }
  816. hists->nr_entries++;
  817. rb_link_node(&he->rb_node_in, parent, p);
  818. rb_insert_color(&he->rb_node_in, root);
  819. return true;
  820. }
  821. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  822. {
  823. struct rb_root *root;
  824. pthread_mutex_lock(&hists->lock);
  825. root = hists->entries_in;
  826. if (++hists->entries_in > &hists->entries_in_array[1])
  827. hists->entries_in = &hists->entries_in_array[0];
  828. pthread_mutex_unlock(&hists->lock);
  829. return root;
  830. }
  831. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  832. {
  833. hists__filter_entry_by_dso(hists, he);
  834. hists__filter_entry_by_thread(hists, he);
  835. hists__filter_entry_by_symbol(hists, he);
  836. }
  837. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  838. {
  839. struct rb_root *root;
  840. struct rb_node *next;
  841. struct hist_entry *n;
  842. if (!sort__need_collapse)
  843. return;
  844. hists->nr_entries = 0;
  845. root = hists__get_rotate_entries_in(hists);
  846. next = rb_first(root);
  847. while (next) {
  848. if (session_done())
  849. break;
  850. n = rb_entry(next, struct hist_entry, rb_node_in);
  851. next = rb_next(&n->rb_node_in);
  852. rb_erase(&n->rb_node_in, root);
  853. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  854. /*
  855. * If it wasn't combined with one of the entries already
  856. * collapsed, we need to apply the filters that may have
  857. * been set by, say, the hist_browser.
  858. */
  859. hists__apply_filters(hists, n);
  860. }
  861. if (prog)
  862. ui_progress__update(prog, 1);
  863. }
  864. }
  865. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  866. {
  867. struct perf_hpp_fmt *fmt;
  868. int64_t cmp = 0;
  869. perf_hpp__for_each_sort_list(fmt) {
  870. if (perf_hpp__should_skip(fmt))
  871. continue;
  872. cmp = fmt->sort(fmt, a, b);
  873. if (cmp)
  874. break;
  875. }
  876. return cmp;
  877. }
  878. static void hists__reset_filter_stats(struct hists *hists)
  879. {
  880. hists->nr_non_filtered_entries = 0;
  881. hists->stats.total_non_filtered_period = 0;
  882. }
  883. void hists__reset_stats(struct hists *hists)
  884. {
  885. hists->nr_entries = 0;
  886. hists->stats.total_period = 0;
  887. hists__reset_filter_stats(hists);
  888. }
  889. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  890. {
  891. hists->nr_non_filtered_entries++;
  892. hists->stats.total_non_filtered_period += h->stat.period;
  893. }
  894. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  895. {
  896. if (!h->filtered)
  897. hists__inc_filter_stats(hists, h);
  898. hists->nr_entries++;
  899. hists->stats.total_period += h->stat.period;
  900. }
  901. static void __hists__insert_output_entry(struct rb_root *entries,
  902. struct hist_entry *he,
  903. u64 min_callchain_hits)
  904. {
  905. struct rb_node **p = &entries->rb_node;
  906. struct rb_node *parent = NULL;
  907. struct hist_entry *iter;
  908. if (symbol_conf.use_callchain)
  909. callchain_param.sort(&he->sorted_chain, he->callchain,
  910. min_callchain_hits, &callchain_param);
  911. while (*p != NULL) {
  912. parent = *p;
  913. iter = rb_entry(parent, struct hist_entry, rb_node);
  914. if (hist_entry__sort(he, iter) > 0)
  915. p = &(*p)->rb_left;
  916. else
  917. p = &(*p)->rb_right;
  918. }
  919. rb_link_node(&he->rb_node, parent, p);
  920. rb_insert_color(&he->rb_node, entries);
  921. }
  922. void hists__output_resort(struct hists *hists, struct ui_progress *prog)
  923. {
  924. struct rb_root *root;
  925. struct rb_node *next;
  926. struct hist_entry *n;
  927. u64 min_callchain_hits;
  928. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  929. if (sort__need_collapse)
  930. root = &hists->entries_collapsed;
  931. else
  932. root = hists->entries_in;
  933. next = rb_first(root);
  934. hists->entries = RB_ROOT;
  935. hists__reset_stats(hists);
  936. hists__reset_col_len(hists);
  937. while (next) {
  938. n = rb_entry(next, struct hist_entry, rb_node_in);
  939. next = rb_next(&n->rb_node_in);
  940. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  941. hists__inc_stats(hists, n);
  942. if (!n->filtered)
  943. hists__calc_col_len(hists, n);
  944. if (prog)
  945. ui_progress__update(prog, 1);
  946. }
  947. }
  948. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  949. enum hist_filter filter)
  950. {
  951. h->filtered &= ~(1 << filter);
  952. if (h->filtered)
  953. return;
  954. /* force fold unfiltered entry for simplicity */
  955. h->unfolded = false;
  956. h->row_offset = 0;
  957. h->nr_rows = 0;
  958. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  959. hists__inc_filter_stats(hists, h);
  960. hists__calc_col_len(hists, h);
  961. }
  962. static bool hists__filter_entry_by_dso(struct hists *hists,
  963. struct hist_entry *he)
  964. {
  965. if (hists->dso_filter != NULL &&
  966. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  967. he->filtered |= (1 << HIST_FILTER__DSO);
  968. return true;
  969. }
  970. return false;
  971. }
  972. void hists__filter_by_dso(struct hists *hists)
  973. {
  974. struct rb_node *nd;
  975. hists->stats.nr_non_filtered_samples = 0;
  976. hists__reset_filter_stats(hists);
  977. hists__reset_col_len(hists);
  978. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  979. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  980. if (symbol_conf.exclude_other && !h->parent)
  981. continue;
  982. if (hists__filter_entry_by_dso(hists, h))
  983. continue;
  984. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  985. }
  986. }
  987. static bool hists__filter_entry_by_thread(struct hists *hists,
  988. struct hist_entry *he)
  989. {
  990. if (hists->thread_filter != NULL &&
  991. he->thread != hists->thread_filter) {
  992. he->filtered |= (1 << HIST_FILTER__THREAD);
  993. return true;
  994. }
  995. return false;
  996. }
  997. void hists__filter_by_thread(struct hists *hists)
  998. {
  999. struct rb_node *nd;
  1000. hists->stats.nr_non_filtered_samples = 0;
  1001. hists__reset_filter_stats(hists);
  1002. hists__reset_col_len(hists);
  1003. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1004. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1005. if (hists__filter_entry_by_thread(hists, h))
  1006. continue;
  1007. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  1008. }
  1009. }
  1010. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1011. struct hist_entry *he)
  1012. {
  1013. if (hists->symbol_filter_str != NULL &&
  1014. (!he->ms.sym || strstr(he->ms.sym->name,
  1015. hists->symbol_filter_str) == NULL)) {
  1016. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1017. return true;
  1018. }
  1019. return false;
  1020. }
  1021. void hists__filter_by_symbol(struct hists *hists)
  1022. {
  1023. struct rb_node *nd;
  1024. hists->stats.nr_non_filtered_samples = 0;
  1025. hists__reset_filter_stats(hists);
  1026. hists__reset_col_len(hists);
  1027. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1028. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1029. if (hists__filter_entry_by_symbol(hists, h))
  1030. continue;
  1031. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  1032. }
  1033. }
  1034. void events_stats__inc(struct events_stats *stats, u32 type)
  1035. {
  1036. ++stats->nr_events[0];
  1037. ++stats->nr_events[type];
  1038. }
  1039. void hists__inc_nr_events(struct hists *hists, u32 type)
  1040. {
  1041. events_stats__inc(&hists->stats, type);
  1042. }
  1043. void hists__inc_nr_samples(struct hists *hists, bool filtered)
  1044. {
  1045. events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
  1046. if (!filtered)
  1047. hists->stats.nr_non_filtered_samples++;
  1048. }
  1049. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  1050. struct hist_entry *pair)
  1051. {
  1052. struct rb_root *root;
  1053. struct rb_node **p;
  1054. struct rb_node *parent = NULL;
  1055. struct hist_entry *he;
  1056. int64_t cmp;
  1057. if (sort__need_collapse)
  1058. root = &hists->entries_collapsed;
  1059. else
  1060. root = hists->entries_in;
  1061. p = &root->rb_node;
  1062. while (*p != NULL) {
  1063. parent = *p;
  1064. he = rb_entry(parent, struct hist_entry, rb_node_in);
  1065. cmp = hist_entry__collapse(he, pair);
  1066. if (!cmp)
  1067. goto out;
  1068. if (cmp < 0)
  1069. p = &(*p)->rb_left;
  1070. else
  1071. p = &(*p)->rb_right;
  1072. }
  1073. he = hist_entry__new(pair, true);
  1074. if (he) {
  1075. memset(&he->stat, 0, sizeof(he->stat));
  1076. he->hists = hists;
  1077. rb_link_node(&he->rb_node_in, parent, p);
  1078. rb_insert_color(&he->rb_node_in, root);
  1079. hists__inc_stats(hists, he);
  1080. he->dummy = true;
  1081. }
  1082. out:
  1083. return he;
  1084. }
  1085. static struct hist_entry *hists__find_entry(struct hists *hists,
  1086. struct hist_entry *he)
  1087. {
  1088. struct rb_node *n;
  1089. if (sort__need_collapse)
  1090. n = hists->entries_collapsed.rb_node;
  1091. else
  1092. n = hists->entries_in->rb_node;
  1093. while (n) {
  1094. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  1095. int64_t cmp = hist_entry__collapse(iter, he);
  1096. if (cmp < 0)
  1097. n = n->rb_left;
  1098. else if (cmp > 0)
  1099. n = n->rb_right;
  1100. else
  1101. return iter;
  1102. }
  1103. return NULL;
  1104. }
  1105. /*
  1106. * Look for pairs to link to the leader buckets (hist_entries):
  1107. */
  1108. void hists__match(struct hists *leader, struct hists *other)
  1109. {
  1110. struct rb_root *root;
  1111. struct rb_node *nd;
  1112. struct hist_entry *pos, *pair;
  1113. if (sort__need_collapse)
  1114. root = &leader->entries_collapsed;
  1115. else
  1116. root = leader->entries_in;
  1117. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1118. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1119. pair = hists__find_entry(other, pos);
  1120. if (pair)
  1121. hist_entry__add_pair(pair, pos);
  1122. }
  1123. }
  1124. /*
  1125. * Look for entries in the other hists that are not present in the leader, if
  1126. * we find them, just add a dummy entry on the leader hists, with period=0,
  1127. * nr_events=0, to serve as the list header.
  1128. */
  1129. int hists__link(struct hists *leader, struct hists *other)
  1130. {
  1131. struct rb_root *root;
  1132. struct rb_node *nd;
  1133. struct hist_entry *pos, *pair;
  1134. if (sort__need_collapse)
  1135. root = &other->entries_collapsed;
  1136. else
  1137. root = other->entries_in;
  1138. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1139. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1140. if (!hist_entry__has_pairs(pos)) {
  1141. pair = hists__add_dummy_entry(leader, pos);
  1142. if (pair == NULL)
  1143. return -1;
  1144. hist_entry__add_pair(pos, pair);
  1145. }
  1146. }
  1147. return 0;
  1148. }
  1149. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
  1150. {
  1151. struct perf_evsel *pos;
  1152. size_t ret = 0;
  1153. evlist__for_each(evlist, pos) {
  1154. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1155. ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
  1156. }
  1157. return ret;
  1158. }
  1159. u64 hists__total_period(struct hists *hists)
  1160. {
  1161. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  1162. hists->stats.total_period;
  1163. }
  1164. int parse_filter_percentage(const struct option *opt __maybe_unused,
  1165. const char *arg, int unset __maybe_unused)
  1166. {
  1167. if (!strcmp(arg, "relative"))
  1168. symbol_conf.filter_relative = true;
  1169. else if (!strcmp(arg, "absolute"))
  1170. symbol_conf.filter_relative = false;
  1171. else
  1172. return -1;
  1173. return 0;
  1174. }
  1175. int perf_hist_config(const char *var, const char *value)
  1176. {
  1177. if (!strcmp(var, "hist.percentage"))
  1178. return parse_filter_percentage(NULL, value, 0);
  1179. return 0;
  1180. }
  1181. static int hists_evsel__init(struct perf_evsel *evsel)
  1182. {
  1183. struct hists *hists = evsel__hists(evsel);
  1184. memset(hists, 0, sizeof(*hists));
  1185. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  1186. hists->entries_in = &hists->entries_in_array[0];
  1187. hists->entries_collapsed = RB_ROOT;
  1188. hists->entries = RB_ROOT;
  1189. pthread_mutex_init(&hists->lock, NULL);
  1190. return 0;
  1191. }
  1192. /*
  1193. * XXX We probably need a hists_evsel__exit() to free the hist_entries
  1194. * stored in the rbtree...
  1195. */
  1196. int hists__init(void)
  1197. {
  1198. int err = perf_evsel__object_config(sizeof(struct hists_evsel),
  1199. hists_evsel__init, NULL);
  1200. if (err)
  1201. fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
  1202. return err;
  1203. }