hist.c 38 KB

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