hist.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  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 + 8);
  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. if (h->branch_info->srcline_from)
  104. hists__new_col_len(hists, HISTC_SRCLINE_FROM,
  105. strlen(h->branch_info->srcline_from));
  106. if (h->branch_info->srcline_to)
  107. hists__new_col_len(hists, HISTC_SRCLINE_TO,
  108. strlen(h->branch_info->srcline_to));
  109. }
  110. if (h->mem_info) {
  111. if (h->mem_info->daddr.sym) {
  112. symlen = (int)h->mem_info->daddr.sym->namelen + 4
  113. + unresolved_col_width + 2;
  114. hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
  115. symlen);
  116. hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
  117. symlen + 1);
  118. } else {
  119. symlen = unresolved_col_width + 4 + 2;
  120. hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
  121. symlen);
  122. hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
  123. symlen);
  124. }
  125. if (h->mem_info->iaddr.sym) {
  126. symlen = (int)h->mem_info->iaddr.sym->namelen + 4
  127. + unresolved_col_width + 2;
  128. hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
  129. symlen);
  130. } else {
  131. symlen = unresolved_col_width + 4 + 2;
  132. hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
  133. symlen);
  134. }
  135. if (h->mem_info->daddr.map) {
  136. symlen = dso__name_len(h->mem_info->daddr.map->dso);
  137. hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
  138. symlen);
  139. } else {
  140. symlen = unresolved_col_width + 4 + 2;
  141. hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
  142. }
  143. } else {
  144. symlen = unresolved_col_width + 4 + 2;
  145. hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
  146. hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
  147. hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
  148. }
  149. hists__new_col_len(hists, HISTC_CPU, 3);
  150. hists__new_col_len(hists, HISTC_SOCKET, 6);
  151. hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
  152. hists__new_col_len(hists, HISTC_MEM_TLB, 22);
  153. hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
  154. hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
  155. hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
  156. hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
  157. if (h->srcline)
  158. hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
  159. if (h->srcfile)
  160. hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
  161. if (h->transaction)
  162. hists__new_col_len(hists, HISTC_TRANSACTION,
  163. hist_entry__transaction_len());
  164. if (h->trace_output)
  165. hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
  166. }
  167. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  168. {
  169. struct rb_node *next = rb_first(&hists->entries);
  170. struct hist_entry *n;
  171. int row = 0;
  172. hists__reset_col_len(hists);
  173. while (next && row++ < max_rows) {
  174. n = rb_entry(next, struct hist_entry, rb_node);
  175. if (!n->filtered)
  176. hists__calc_col_len(hists, n);
  177. next = rb_next(&n->rb_node);
  178. }
  179. }
  180. static void he_stat__add_cpumode_period(struct he_stat *he_stat,
  181. unsigned int cpumode, u64 period)
  182. {
  183. switch (cpumode) {
  184. case PERF_RECORD_MISC_KERNEL:
  185. he_stat->period_sys += period;
  186. break;
  187. case PERF_RECORD_MISC_USER:
  188. he_stat->period_us += period;
  189. break;
  190. case PERF_RECORD_MISC_GUEST_KERNEL:
  191. he_stat->period_guest_sys += period;
  192. break;
  193. case PERF_RECORD_MISC_GUEST_USER:
  194. he_stat->period_guest_us += period;
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. static void he_stat__add_period(struct he_stat *he_stat, u64 period,
  201. u64 weight)
  202. {
  203. he_stat->period += period;
  204. he_stat->weight += weight;
  205. he_stat->nr_events += 1;
  206. }
  207. static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
  208. {
  209. dest->period += src->period;
  210. dest->period_sys += src->period_sys;
  211. dest->period_us += src->period_us;
  212. dest->period_guest_sys += src->period_guest_sys;
  213. dest->period_guest_us += src->period_guest_us;
  214. dest->nr_events += src->nr_events;
  215. dest->weight += src->weight;
  216. }
  217. static void he_stat__decay(struct he_stat *he_stat)
  218. {
  219. he_stat->period = (he_stat->period * 7) / 8;
  220. he_stat->nr_events = (he_stat->nr_events * 7) / 8;
  221. /* XXX need decay for weight too? */
  222. }
  223. static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
  224. static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
  225. {
  226. u64 prev_period = he->stat.period;
  227. u64 diff;
  228. if (prev_period == 0)
  229. return true;
  230. he_stat__decay(&he->stat);
  231. if (symbol_conf.cumulate_callchain)
  232. he_stat__decay(he->stat_acc);
  233. decay_callchain(he->callchain);
  234. diff = prev_period - he->stat.period;
  235. if (!he->depth) {
  236. hists->stats.total_period -= diff;
  237. if (!he->filtered)
  238. hists->stats.total_non_filtered_period -= diff;
  239. }
  240. if (!he->leaf) {
  241. struct hist_entry *child;
  242. struct rb_node *node = rb_first(&he->hroot_out);
  243. while (node) {
  244. child = rb_entry(node, struct hist_entry, rb_node);
  245. node = rb_next(node);
  246. if (hists__decay_entry(hists, child))
  247. hists__delete_entry(hists, child);
  248. }
  249. }
  250. return he->stat.period == 0;
  251. }
  252. static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
  253. {
  254. struct rb_root *root_in;
  255. struct rb_root *root_out;
  256. if (he->parent_he) {
  257. root_in = &he->parent_he->hroot_in;
  258. root_out = &he->parent_he->hroot_out;
  259. } else {
  260. if (hists__has(hists, need_collapse))
  261. root_in = &hists->entries_collapsed;
  262. else
  263. root_in = hists->entries_in;
  264. root_out = &hists->entries;
  265. }
  266. rb_erase(&he->rb_node_in, root_in);
  267. rb_erase(&he->rb_node, root_out);
  268. --hists->nr_entries;
  269. if (!he->filtered)
  270. --hists->nr_non_filtered_entries;
  271. hist_entry__delete(he);
  272. }
  273. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  274. {
  275. struct rb_node *next = rb_first(&hists->entries);
  276. struct hist_entry *n;
  277. while (next) {
  278. n = rb_entry(next, struct hist_entry, rb_node);
  279. next = rb_next(&n->rb_node);
  280. if (((zap_user && n->level == '.') ||
  281. (zap_kernel && n->level != '.') ||
  282. hists__decay_entry(hists, n))) {
  283. hists__delete_entry(hists, n);
  284. }
  285. }
  286. }
  287. void hists__delete_entries(struct hists *hists)
  288. {
  289. struct rb_node *next = rb_first(&hists->entries);
  290. struct hist_entry *n;
  291. while (next) {
  292. n = rb_entry(next, struct hist_entry, rb_node);
  293. next = rb_next(&n->rb_node);
  294. hists__delete_entry(hists, n);
  295. }
  296. }
  297. /*
  298. * histogram, sorted on item, collects periods
  299. */
  300. static int hist_entry__init(struct hist_entry *he,
  301. struct hist_entry *template,
  302. bool sample_self)
  303. {
  304. *he = *template;
  305. if (symbol_conf.cumulate_callchain) {
  306. he->stat_acc = malloc(sizeof(he->stat));
  307. if (he->stat_acc == NULL)
  308. return -ENOMEM;
  309. memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
  310. if (!sample_self)
  311. memset(&he->stat, 0, sizeof(he->stat));
  312. }
  313. map__get(he->ms.map);
  314. if (he->branch_info) {
  315. /*
  316. * This branch info is (a part of) allocated from
  317. * sample__resolve_bstack() and will be freed after
  318. * adding new entries. So we need to save a copy.
  319. */
  320. he->branch_info = malloc(sizeof(*he->branch_info));
  321. if (he->branch_info == NULL) {
  322. map__zput(he->ms.map);
  323. free(he->stat_acc);
  324. return -ENOMEM;
  325. }
  326. memcpy(he->branch_info, template->branch_info,
  327. sizeof(*he->branch_info));
  328. map__get(he->branch_info->from.map);
  329. map__get(he->branch_info->to.map);
  330. }
  331. if (he->mem_info) {
  332. map__get(he->mem_info->iaddr.map);
  333. map__get(he->mem_info->daddr.map);
  334. }
  335. if (symbol_conf.use_callchain)
  336. callchain_init(he->callchain);
  337. if (he->raw_data) {
  338. he->raw_data = memdup(he->raw_data, he->raw_size);
  339. if (he->raw_data == NULL) {
  340. map__put(he->ms.map);
  341. if (he->branch_info) {
  342. map__put(he->branch_info->from.map);
  343. map__put(he->branch_info->to.map);
  344. free(he->branch_info);
  345. }
  346. if (he->mem_info) {
  347. map__put(he->mem_info->iaddr.map);
  348. map__put(he->mem_info->daddr.map);
  349. }
  350. free(he->stat_acc);
  351. return -ENOMEM;
  352. }
  353. }
  354. INIT_LIST_HEAD(&he->pairs.node);
  355. thread__get(he->thread);
  356. if (!symbol_conf.report_hierarchy)
  357. he->leaf = true;
  358. return 0;
  359. }
  360. static void *hist_entry__zalloc(size_t size)
  361. {
  362. return zalloc(size + sizeof(struct hist_entry));
  363. }
  364. static void hist_entry__free(void *ptr)
  365. {
  366. free(ptr);
  367. }
  368. static struct hist_entry_ops default_ops = {
  369. .new = hist_entry__zalloc,
  370. .free = hist_entry__free,
  371. };
  372. static struct hist_entry *hist_entry__new(struct hist_entry *template,
  373. bool sample_self)
  374. {
  375. struct hist_entry_ops *ops = template->ops;
  376. size_t callchain_size = 0;
  377. struct hist_entry *he;
  378. int err = 0;
  379. if (!ops)
  380. ops = template->ops = &default_ops;
  381. if (symbol_conf.use_callchain)
  382. callchain_size = sizeof(struct callchain_root);
  383. he = ops->new(callchain_size);
  384. if (he) {
  385. err = hist_entry__init(he, template, sample_self);
  386. if (err) {
  387. ops->free(he);
  388. he = NULL;
  389. }
  390. }
  391. return he;
  392. }
  393. static u8 symbol__parent_filter(const struct symbol *parent)
  394. {
  395. if (symbol_conf.exclude_other && parent == NULL)
  396. return 1 << HIST_FILTER__PARENT;
  397. return 0;
  398. }
  399. static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
  400. {
  401. if (!symbol_conf.use_callchain)
  402. return;
  403. he->hists->callchain_period += period;
  404. if (!he->filtered)
  405. he->hists->callchain_non_filtered_period += period;
  406. }
  407. static struct hist_entry *hists__findnew_entry(struct hists *hists,
  408. struct hist_entry *entry,
  409. struct addr_location *al,
  410. bool sample_self)
  411. {
  412. struct rb_node **p;
  413. struct rb_node *parent = NULL;
  414. struct hist_entry *he;
  415. int64_t cmp;
  416. u64 period = entry->stat.period;
  417. u64 weight = entry->stat.weight;
  418. p = &hists->entries_in->rb_node;
  419. while (*p != NULL) {
  420. parent = *p;
  421. he = rb_entry(parent, struct hist_entry, rb_node_in);
  422. /*
  423. * Make sure that it receives arguments in a same order as
  424. * hist_entry__collapse() so that we can use an appropriate
  425. * function when searching an entry regardless which sort
  426. * keys were used.
  427. */
  428. cmp = hist_entry__cmp(he, entry);
  429. if (!cmp) {
  430. if (sample_self) {
  431. he_stat__add_period(&he->stat, period, weight);
  432. hist_entry__add_callchain_period(he, period);
  433. }
  434. if (symbol_conf.cumulate_callchain)
  435. he_stat__add_period(he->stat_acc, period, weight);
  436. /*
  437. * This mem info was allocated from sample__resolve_mem
  438. * and will not be used anymore.
  439. */
  440. zfree(&entry->mem_info);
  441. /* If the map of an existing hist_entry has
  442. * become out-of-date due to an exec() or
  443. * similar, update it. Otherwise we will
  444. * mis-adjust symbol addresses when computing
  445. * the history counter to increment.
  446. */
  447. if (he->ms.map != entry->ms.map) {
  448. map__put(he->ms.map);
  449. he->ms.map = map__get(entry->ms.map);
  450. }
  451. goto out;
  452. }
  453. if (cmp < 0)
  454. p = &(*p)->rb_left;
  455. else
  456. p = &(*p)->rb_right;
  457. }
  458. he = hist_entry__new(entry, sample_self);
  459. if (!he)
  460. return NULL;
  461. if (sample_self)
  462. hist_entry__add_callchain_period(he, period);
  463. hists->nr_entries++;
  464. rb_link_node(&he->rb_node_in, parent, p);
  465. rb_insert_color(&he->rb_node_in, hists->entries_in);
  466. out:
  467. if (sample_self)
  468. he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
  469. if (symbol_conf.cumulate_callchain)
  470. he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
  471. return he;
  472. }
  473. static struct hist_entry*
  474. __hists__add_entry(struct hists *hists,
  475. struct addr_location *al,
  476. struct symbol *sym_parent,
  477. struct branch_info *bi,
  478. struct mem_info *mi,
  479. struct perf_sample *sample,
  480. bool sample_self,
  481. struct hist_entry_ops *ops)
  482. {
  483. struct hist_entry entry = {
  484. .thread = al->thread,
  485. .comm = thread__comm(al->thread),
  486. .ms = {
  487. .map = al->map,
  488. .sym = al->sym,
  489. },
  490. .socket = al->socket,
  491. .cpu = al->cpu,
  492. .cpumode = al->cpumode,
  493. .ip = al->addr,
  494. .level = al->level,
  495. .stat = {
  496. .nr_events = 1,
  497. .period = sample->period,
  498. .weight = sample->weight,
  499. },
  500. .parent = sym_parent,
  501. .filtered = symbol__parent_filter(sym_parent) | al->filtered,
  502. .hists = hists,
  503. .branch_info = bi,
  504. .mem_info = mi,
  505. .transaction = sample->transaction,
  506. .raw_data = sample->raw_data,
  507. .raw_size = sample->raw_size,
  508. .ops = ops,
  509. };
  510. return hists__findnew_entry(hists, &entry, al, sample_self);
  511. }
  512. struct hist_entry *hists__add_entry(struct hists *hists,
  513. struct addr_location *al,
  514. struct symbol *sym_parent,
  515. struct branch_info *bi,
  516. struct mem_info *mi,
  517. struct perf_sample *sample,
  518. bool sample_self)
  519. {
  520. return __hists__add_entry(hists, al, sym_parent, bi, mi,
  521. sample, sample_self, NULL);
  522. }
  523. struct hist_entry *hists__add_entry_ops(struct hists *hists,
  524. struct hist_entry_ops *ops,
  525. struct addr_location *al,
  526. struct symbol *sym_parent,
  527. struct branch_info *bi,
  528. struct mem_info *mi,
  529. struct perf_sample *sample,
  530. bool sample_self)
  531. {
  532. return __hists__add_entry(hists, al, sym_parent, bi, mi,
  533. sample, sample_self, ops);
  534. }
  535. static int
  536. iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  537. struct addr_location *al __maybe_unused)
  538. {
  539. return 0;
  540. }
  541. static int
  542. iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  543. struct addr_location *al __maybe_unused)
  544. {
  545. return 0;
  546. }
  547. static int
  548. iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  549. {
  550. struct perf_sample *sample = iter->sample;
  551. struct mem_info *mi;
  552. mi = sample__resolve_mem(sample, al);
  553. if (mi == NULL)
  554. return -ENOMEM;
  555. iter->priv = mi;
  556. return 0;
  557. }
  558. static int
  559. iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  560. {
  561. u64 cost;
  562. struct mem_info *mi = iter->priv;
  563. struct hists *hists = evsel__hists(iter->evsel);
  564. struct perf_sample *sample = iter->sample;
  565. struct hist_entry *he;
  566. if (mi == NULL)
  567. return -EINVAL;
  568. cost = sample->weight;
  569. if (!cost)
  570. cost = 1;
  571. /*
  572. * must pass period=weight in order to get the correct
  573. * sorting from hists__collapse_resort() which is solely
  574. * based on periods. We want sorting be done on nr_events * weight
  575. * and this is indirectly achieved by passing period=weight here
  576. * and the he_stat__add_period() function.
  577. */
  578. sample->period = cost;
  579. he = hists__add_entry(hists, al, iter->parent, NULL, mi,
  580. sample, true);
  581. if (!he)
  582. return -ENOMEM;
  583. iter->he = he;
  584. return 0;
  585. }
  586. static int
  587. iter_finish_mem_entry(struct hist_entry_iter *iter,
  588. struct addr_location *al __maybe_unused)
  589. {
  590. struct perf_evsel *evsel = iter->evsel;
  591. struct hists *hists = evsel__hists(evsel);
  592. struct hist_entry *he = iter->he;
  593. int err = -EINVAL;
  594. if (he == NULL)
  595. goto out;
  596. hists__inc_nr_samples(hists, he->filtered);
  597. err = hist_entry__append_callchain(he, iter->sample);
  598. out:
  599. /*
  600. * We don't need to free iter->priv (mem_info) here since the mem info
  601. * was either already freed in hists__findnew_entry() or passed to a
  602. * new hist entry by hist_entry__new().
  603. */
  604. iter->priv = NULL;
  605. iter->he = NULL;
  606. return err;
  607. }
  608. static int
  609. iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  610. {
  611. struct branch_info *bi;
  612. struct perf_sample *sample = iter->sample;
  613. bi = sample__resolve_bstack(sample, al);
  614. if (!bi)
  615. return -ENOMEM;
  616. iter->curr = 0;
  617. iter->total = sample->branch_stack->nr;
  618. iter->priv = bi;
  619. return 0;
  620. }
  621. static int
  622. iter_add_single_branch_entry(struct hist_entry_iter *iter,
  623. struct addr_location *al __maybe_unused)
  624. {
  625. /* to avoid calling callback function */
  626. iter->he = NULL;
  627. return 0;
  628. }
  629. static int
  630. iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  631. {
  632. struct branch_info *bi = iter->priv;
  633. int i = iter->curr;
  634. if (bi == NULL)
  635. return 0;
  636. if (iter->curr >= iter->total)
  637. return 0;
  638. al->map = bi[i].to.map;
  639. al->sym = bi[i].to.sym;
  640. al->addr = bi[i].to.addr;
  641. return 1;
  642. }
  643. static int
  644. iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  645. {
  646. struct branch_info *bi;
  647. struct perf_evsel *evsel = iter->evsel;
  648. struct hists *hists = evsel__hists(evsel);
  649. struct perf_sample *sample = iter->sample;
  650. struct hist_entry *he = NULL;
  651. int i = iter->curr;
  652. int err = 0;
  653. bi = iter->priv;
  654. if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
  655. goto out;
  656. /*
  657. * The report shows the percentage of total branches captured
  658. * and not events sampled. Thus we use a pseudo period of 1.
  659. */
  660. sample->period = 1;
  661. sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
  662. he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
  663. sample, true);
  664. if (he == NULL)
  665. return -ENOMEM;
  666. hists__inc_nr_samples(hists, he->filtered);
  667. out:
  668. iter->he = he;
  669. iter->curr++;
  670. return err;
  671. }
  672. static int
  673. iter_finish_branch_entry(struct hist_entry_iter *iter,
  674. struct addr_location *al __maybe_unused)
  675. {
  676. zfree(&iter->priv);
  677. iter->he = NULL;
  678. return iter->curr >= iter->total ? 0 : -1;
  679. }
  680. static int
  681. iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
  682. struct addr_location *al __maybe_unused)
  683. {
  684. return 0;
  685. }
  686. static int
  687. iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
  688. {
  689. struct perf_evsel *evsel = iter->evsel;
  690. struct perf_sample *sample = iter->sample;
  691. struct hist_entry *he;
  692. he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
  693. sample, true);
  694. if (he == NULL)
  695. return -ENOMEM;
  696. iter->he = he;
  697. return 0;
  698. }
  699. static int
  700. iter_finish_normal_entry(struct hist_entry_iter *iter,
  701. struct addr_location *al __maybe_unused)
  702. {
  703. struct hist_entry *he = iter->he;
  704. struct perf_evsel *evsel = iter->evsel;
  705. struct perf_sample *sample = iter->sample;
  706. if (he == NULL)
  707. return 0;
  708. iter->he = NULL;
  709. hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
  710. return hist_entry__append_callchain(he, sample);
  711. }
  712. static int
  713. iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
  714. struct addr_location *al __maybe_unused)
  715. {
  716. struct hist_entry **he_cache;
  717. callchain_cursor_commit(&callchain_cursor);
  718. /*
  719. * This is for detecting cycles or recursions so that they're
  720. * cumulated only one time to prevent entries more than 100%
  721. * overhead.
  722. */
  723. he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
  724. if (he_cache == NULL)
  725. return -ENOMEM;
  726. iter->priv = he_cache;
  727. iter->curr = 0;
  728. return 0;
  729. }
  730. static int
  731. iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
  732. struct addr_location *al)
  733. {
  734. struct perf_evsel *evsel = iter->evsel;
  735. struct hists *hists = evsel__hists(evsel);
  736. struct perf_sample *sample = iter->sample;
  737. struct hist_entry **he_cache = iter->priv;
  738. struct hist_entry *he;
  739. int err = 0;
  740. he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
  741. sample, true);
  742. if (he == NULL)
  743. return -ENOMEM;
  744. iter->he = he;
  745. he_cache[iter->curr++] = he;
  746. hist_entry__append_callchain(he, sample);
  747. /*
  748. * We need to re-initialize the cursor since callchain_append()
  749. * advanced the cursor to the end.
  750. */
  751. callchain_cursor_commit(&callchain_cursor);
  752. hists__inc_nr_samples(hists, he->filtered);
  753. return err;
  754. }
  755. static int
  756. iter_next_cumulative_entry(struct hist_entry_iter *iter,
  757. struct addr_location *al)
  758. {
  759. struct callchain_cursor_node *node;
  760. node = callchain_cursor_current(&callchain_cursor);
  761. if (node == NULL)
  762. return 0;
  763. return fill_callchain_info(al, node, iter->hide_unresolved);
  764. }
  765. static int
  766. iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
  767. struct addr_location *al)
  768. {
  769. struct perf_evsel *evsel = iter->evsel;
  770. struct perf_sample *sample = iter->sample;
  771. struct hist_entry **he_cache = iter->priv;
  772. struct hist_entry *he;
  773. struct hist_entry he_tmp = {
  774. .hists = evsel__hists(evsel),
  775. .cpu = al->cpu,
  776. .thread = al->thread,
  777. .comm = thread__comm(al->thread),
  778. .ip = al->addr,
  779. .ms = {
  780. .map = al->map,
  781. .sym = al->sym,
  782. },
  783. .parent = iter->parent,
  784. .raw_data = sample->raw_data,
  785. .raw_size = sample->raw_size,
  786. };
  787. int i;
  788. struct callchain_cursor cursor;
  789. callchain_cursor_snapshot(&cursor, &callchain_cursor);
  790. callchain_cursor_advance(&callchain_cursor);
  791. /*
  792. * Check if there's duplicate entries in the callchain.
  793. * It's possible that it has cycles or recursive calls.
  794. */
  795. for (i = 0; i < iter->curr; i++) {
  796. if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
  797. /* to avoid calling callback function */
  798. iter->he = NULL;
  799. return 0;
  800. }
  801. }
  802. he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
  803. sample, false);
  804. if (he == NULL)
  805. return -ENOMEM;
  806. iter->he = he;
  807. he_cache[iter->curr++] = he;
  808. if (symbol_conf.use_callchain)
  809. callchain_append(he->callchain, &cursor, sample->period);
  810. return 0;
  811. }
  812. static int
  813. iter_finish_cumulative_entry(struct hist_entry_iter *iter,
  814. struct addr_location *al __maybe_unused)
  815. {
  816. zfree(&iter->priv);
  817. iter->he = NULL;
  818. return 0;
  819. }
  820. const struct hist_iter_ops hist_iter_mem = {
  821. .prepare_entry = iter_prepare_mem_entry,
  822. .add_single_entry = iter_add_single_mem_entry,
  823. .next_entry = iter_next_nop_entry,
  824. .add_next_entry = iter_add_next_nop_entry,
  825. .finish_entry = iter_finish_mem_entry,
  826. };
  827. const struct hist_iter_ops hist_iter_branch = {
  828. .prepare_entry = iter_prepare_branch_entry,
  829. .add_single_entry = iter_add_single_branch_entry,
  830. .next_entry = iter_next_branch_entry,
  831. .add_next_entry = iter_add_next_branch_entry,
  832. .finish_entry = iter_finish_branch_entry,
  833. };
  834. const struct hist_iter_ops hist_iter_normal = {
  835. .prepare_entry = iter_prepare_normal_entry,
  836. .add_single_entry = iter_add_single_normal_entry,
  837. .next_entry = iter_next_nop_entry,
  838. .add_next_entry = iter_add_next_nop_entry,
  839. .finish_entry = iter_finish_normal_entry,
  840. };
  841. const struct hist_iter_ops hist_iter_cumulative = {
  842. .prepare_entry = iter_prepare_cumulative_entry,
  843. .add_single_entry = iter_add_single_cumulative_entry,
  844. .next_entry = iter_next_cumulative_entry,
  845. .add_next_entry = iter_add_next_cumulative_entry,
  846. .finish_entry = iter_finish_cumulative_entry,
  847. };
  848. int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
  849. int max_stack_depth, void *arg)
  850. {
  851. int err, err2;
  852. err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
  853. iter->evsel, al, max_stack_depth);
  854. if (err)
  855. return err;
  856. iter->max_stack = max_stack_depth;
  857. err = iter->ops->prepare_entry(iter, al);
  858. if (err)
  859. goto out;
  860. err = iter->ops->add_single_entry(iter, al);
  861. if (err)
  862. goto out;
  863. if (iter->he && iter->add_entry_cb) {
  864. err = iter->add_entry_cb(iter, al, true, arg);
  865. if (err)
  866. goto out;
  867. }
  868. while (iter->ops->next_entry(iter, al)) {
  869. err = iter->ops->add_next_entry(iter, al);
  870. if (err)
  871. break;
  872. if (iter->he && iter->add_entry_cb) {
  873. err = iter->add_entry_cb(iter, al, false, arg);
  874. if (err)
  875. goto out;
  876. }
  877. }
  878. out:
  879. err2 = iter->ops->finish_entry(iter, al);
  880. if (!err)
  881. err = err2;
  882. return err;
  883. }
  884. int64_t
  885. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  886. {
  887. struct hists *hists = left->hists;
  888. struct perf_hpp_fmt *fmt;
  889. int64_t cmp = 0;
  890. hists__for_each_sort_list(hists, fmt) {
  891. if (perf_hpp__is_dynamic_entry(fmt) &&
  892. !perf_hpp__defined_dynamic_entry(fmt, hists))
  893. continue;
  894. cmp = fmt->cmp(fmt, left, right);
  895. if (cmp)
  896. break;
  897. }
  898. return cmp;
  899. }
  900. int64_t
  901. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  902. {
  903. struct hists *hists = left->hists;
  904. struct perf_hpp_fmt *fmt;
  905. int64_t cmp = 0;
  906. hists__for_each_sort_list(hists, fmt) {
  907. if (perf_hpp__is_dynamic_entry(fmt) &&
  908. !perf_hpp__defined_dynamic_entry(fmt, hists))
  909. continue;
  910. cmp = fmt->collapse(fmt, left, right);
  911. if (cmp)
  912. break;
  913. }
  914. return cmp;
  915. }
  916. void hist_entry__delete(struct hist_entry *he)
  917. {
  918. struct hist_entry_ops *ops = he->ops;
  919. thread__zput(he->thread);
  920. map__zput(he->ms.map);
  921. if (he->branch_info) {
  922. map__zput(he->branch_info->from.map);
  923. map__zput(he->branch_info->to.map);
  924. free_srcline(he->branch_info->srcline_from);
  925. free_srcline(he->branch_info->srcline_to);
  926. zfree(&he->branch_info);
  927. }
  928. if (he->mem_info) {
  929. map__zput(he->mem_info->iaddr.map);
  930. map__zput(he->mem_info->daddr.map);
  931. zfree(&he->mem_info);
  932. }
  933. zfree(&he->stat_acc);
  934. free_srcline(he->srcline);
  935. if (he->srcfile && he->srcfile[0])
  936. free(he->srcfile);
  937. free_callchain(he->callchain);
  938. free(he->trace_output);
  939. free(he->raw_data);
  940. ops->free(he);
  941. }
  942. /*
  943. * If this is not the last column, then we need to pad it according to the
  944. * pre-calculated max lenght for this column, otherwise don't bother adding
  945. * spaces because that would break viewing this with, for instance, 'less',
  946. * that would show tons of trailing spaces when a long C++ demangled method
  947. * names is sampled.
  948. */
  949. int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
  950. struct perf_hpp_fmt *fmt, int printed)
  951. {
  952. if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
  953. const int width = fmt->width(fmt, hpp, he->hists);
  954. if (printed < width) {
  955. advance_hpp(hpp, printed);
  956. printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
  957. }
  958. }
  959. return printed;
  960. }
  961. /*
  962. * collapse the histogram
  963. */
  964. static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
  965. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
  966. enum hist_filter type);
  967. typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
  968. static bool check_thread_entry(struct perf_hpp_fmt *fmt)
  969. {
  970. return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
  971. }
  972. static void hist_entry__check_and_remove_filter(struct hist_entry *he,
  973. enum hist_filter type,
  974. fmt_chk_fn check)
  975. {
  976. struct perf_hpp_fmt *fmt;
  977. bool type_match = false;
  978. struct hist_entry *parent = he->parent_he;
  979. switch (type) {
  980. case HIST_FILTER__THREAD:
  981. if (symbol_conf.comm_list == NULL &&
  982. symbol_conf.pid_list == NULL &&
  983. symbol_conf.tid_list == NULL)
  984. return;
  985. break;
  986. case HIST_FILTER__DSO:
  987. if (symbol_conf.dso_list == NULL)
  988. return;
  989. break;
  990. case HIST_FILTER__SYMBOL:
  991. if (symbol_conf.sym_list == NULL)
  992. return;
  993. break;
  994. case HIST_FILTER__PARENT:
  995. case HIST_FILTER__GUEST:
  996. case HIST_FILTER__HOST:
  997. case HIST_FILTER__SOCKET:
  998. default:
  999. return;
  1000. }
  1001. /* if it's filtered by own fmt, it has to have filter bits */
  1002. perf_hpp_list__for_each_format(he->hpp_list, fmt) {
  1003. if (check(fmt)) {
  1004. type_match = true;
  1005. break;
  1006. }
  1007. }
  1008. if (type_match) {
  1009. /*
  1010. * If the filter is for current level entry, propagate
  1011. * filter marker to parents. The marker bit was
  1012. * already set by default so it only needs to clear
  1013. * non-filtered entries.
  1014. */
  1015. if (!(he->filtered & (1 << type))) {
  1016. while (parent) {
  1017. parent->filtered &= ~(1 << type);
  1018. parent = parent->parent_he;
  1019. }
  1020. }
  1021. } else {
  1022. /*
  1023. * If current entry doesn't have matching formats, set
  1024. * filter marker for upper level entries. it will be
  1025. * cleared if its lower level entries is not filtered.
  1026. *
  1027. * For lower-level entries, it inherits parent's
  1028. * filter bit so that lower level entries of a
  1029. * non-filtered entry won't set the filter marker.
  1030. */
  1031. if (parent == NULL)
  1032. he->filtered |= (1 << type);
  1033. else
  1034. he->filtered |= (parent->filtered & (1 << type));
  1035. }
  1036. }
  1037. static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
  1038. {
  1039. hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
  1040. check_thread_entry);
  1041. hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
  1042. perf_hpp__is_dso_entry);
  1043. hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
  1044. perf_hpp__is_sym_entry);
  1045. hists__apply_filters(he->hists, he);
  1046. }
  1047. static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
  1048. struct rb_root *root,
  1049. struct hist_entry *he,
  1050. struct hist_entry *parent_he,
  1051. struct perf_hpp_list *hpp_list)
  1052. {
  1053. struct rb_node **p = &root->rb_node;
  1054. struct rb_node *parent = NULL;
  1055. struct hist_entry *iter, *new;
  1056. struct perf_hpp_fmt *fmt;
  1057. int64_t cmp;
  1058. while (*p != NULL) {
  1059. parent = *p;
  1060. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  1061. cmp = 0;
  1062. perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
  1063. cmp = fmt->collapse(fmt, iter, he);
  1064. if (cmp)
  1065. break;
  1066. }
  1067. if (!cmp) {
  1068. he_stat__add_stat(&iter->stat, &he->stat);
  1069. return iter;
  1070. }
  1071. if (cmp < 0)
  1072. p = &parent->rb_left;
  1073. else
  1074. p = &parent->rb_right;
  1075. }
  1076. new = hist_entry__new(he, true);
  1077. if (new == NULL)
  1078. return NULL;
  1079. hists->nr_entries++;
  1080. /* save related format list for output */
  1081. new->hpp_list = hpp_list;
  1082. new->parent_he = parent_he;
  1083. hist_entry__apply_hierarchy_filters(new);
  1084. /* some fields are now passed to 'new' */
  1085. perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
  1086. if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
  1087. he->trace_output = NULL;
  1088. else
  1089. new->trace_output = NULL;
  1090. if (perf_hpp__is_srcline_entry(fmt))
  1091. he->srcline = NULL;
  1092. else
  1093. new->srcline = NULL;
  1094. if (perf_hpp__is_srcfile_entry(fmt))
  1095. he->srcfile = NULL;
  1096. else
  1097. new->srcfile = NULL;
  1098. }
  1099. rb_link_node(&new->rb_node_in, parent, p);
  1100. rb_insert_color(&new->rb_node_in, root);
  1101. return new;
  1102. }
  1103. static int hists__hierarchy_insert_entry(struct hists *hists,
  1104. struct rb_root *root,
  1105. struct hist_entry *he)
  1106. {
  1107. struct perf_hpp_list_node *node;
  1108. struct hist_entry *new_he = NULL;
  1109. struct hist_entry *parent = NULL;
  1110. int depth = 0;
  1111. int ret = 0;
  1112. list_for_each_entry(node, &hists->hpp_formats, list) {
  1113. /* skip period (overhead) and elided columns */
  1114. if (node->level == 0 || node->skip)
  1115. continue;
  1116. /* insert copy of 'he' for each fmt into the hierarchy */
  1117. new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
  1118. if (new_he == NULL) {
  1119. ret = -1;
  1120. break;
  1121. }
  1122. root = &new_he->hroot_in;
  1123. new_he->depth = depth++;
  1124. parent = new_he;
  1125. }
  1126. if (new_he) {
  1127. new_he->leaf = true;
  1128. if (symbol_conf.use_callchain) {
  1129. callchain_cursor_reset(&callchain_cursor);
  1130. if (callchain_merge(&callchain_cursor,
  1131. new_he->callchain,
  1132. he->callchain) < 0)
  1133. ret = -1;
  1134. }
  1135. }
  1136. /* 'he' is no longer used */
  1137. hist_entry__delete(he);
  1138. /* return 0 (or -1) since it already applied filters */
  1139. return ret;
  1140. }
  1141. static int hists__collapse_insert_entry(struct hists *hists,
  1142. struct rb_root *root,
  1143. struct hist_entry *he)
  1144. {
  1145. struct rb_node **p = &root->rb_node;
  1146. struct rb_node *parent = NULL;
  1147. struct hist_entry *iter;
  1148. int64_t cmp;
  1149. if (symbol_conf.report_hierarchy)
  1150. return hists__hierarchy_insert_entry(hists, root, he);
  1151. while (*p != NULL) {
  1152. parent = *p;
  1153. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  1154. cmp = hist_entry__collapse(iter, he);
  1155. if (!cmp) {
  1156. int ret = 0;
  1157. he_stat__add_stat(&iter->stat, &he->stat);
  1158. if (symbol_conf.cumulate_callchain)
  1159. he_stat__add_stat(iter->stat_acc, he->stat_acc);
  1160. if (symbol_conf.use_callchain) {
  1161. callchain_cursor_reset(&callchain_cursor);
  1162. if (callchain_merge(&callchain_cursor,
  1163. iter->callchain,
  1164. he->callchain) < 0)
  1165. ret = -1;
  1166. }
  1167. hist_entry__delete(he);
  1168. return ret;
  1169. }
  1170. if (cmp < 0)
  1171. p = &(*p)->rb_left;
  1172. else
  1173. p = &(*p)->rb_right;
  1174. }
  1175. hists->nr_entries++;
  1176. rb_link_node(&he->rb_node_in, parent, p);
  1177. rb_insert_color(&he->rb_node_in, root);
  1178. return 1;
  1179. }
  1180. struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  1181. {
  1182. struct rb_root *root;
  1183. pthread_mutex_lock(&hists->lock);
  1184. root = hists->entries_in;
  1185. if (++hists->entries_in > &hists->entries_in_array[1])
  1186. hists->entries_in = &hists->entries_in_array[0];
  1187. pthread_mutex_unlock(&hists->lock);
  1188. return root;
  1189. }
  1190. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  1191. {
  1192. hists__filter_entry_by_dso(hists, he);
  1193. hists__filter_entry_by_thread(hists, he);
  1194. hists__filter_entry_by_symbol(hists, he);
  1195. hists__filter_entry_by_socket(hists, he);
  1196. }
  1197. int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  1198. {
  1199. struct rb_root *root;
  1200. struct rb_node *next;
  1201. struct hist_entry *n;
  1202. int ret;
  1203. if (!hists__has(hists, need_collapse))
  1204. return 0;
  1205. hists->nr_entries = 0;
  1206. root = hists__get_rotate_entries_in(hists);
  1207. next = rb_first(root);
  1208. while (next) {
  1209. if (session_done())
  1210. break;
  1211. n = rb_entry(next, struct hist_entry, rb_node_in);
  1212. next = rb_next(&n->rb_node_in);
  1213. rb_erase(&n->rb_node_in, root);
  1214. ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
  1215. if (ret < 0)
  1216. return -1;
  1217. if (ret) {
  1218. /*
  1219. * If it wasn't combined with one of the entries already
  1220. * collapsed, we need to apply the filters that may have
  1221. * been set by, say, the hist_browser.
  1222. */
  1223. hists__apply_filters(hists, n);
  1224. }
  1225. if (prog)
  1226. ui_progress__update(prog, 1);
  1227. }
  1228. return 0;
  1229. }
  1230. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  1231. {
  1232. struct hists *hists = a->hists;
  1233. struct perf_hpp_fmt *fmt;
  1234. int64_t cmp = 0;
  1235. hists__for_each_sort_list(hists, fmt) {
  1236. if (perf_hpp__should_skip(fmt, a->hists))
  1237. continue;
  1238. cmp = fmt->sort(fmt, a, b);
  1239. if (cmp)
  1240. break;
  1241. }
  1242. return cmp;
  1243. }
  1244. static void hists__reset_filter_stats(struct hists *hists)
  1245. {
  1246. hists->nr_non_filtered_entries = 0;
  1247. hists->stats.total_non_filtered_period = 0;
  1248. }
  1249. void hists__reset_stats(struct hists *hists)
  1250. {
  1251. hists->nr_entries = 0;
  1252. hists->stats.total_period = 0;
  1253. hists__reset_filter_stats(hists);
  1254. }
  1255. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  1256. {
  1257. hists->nr_non_filtered_entries++;
  1258. hists->stats.total_non_filtered_period += h->stat.period;
  1259. }
  1260. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  1261. {
  1262. if (!h->filtered)
  1263. hists__inc_filter_stats(hists, h);
  1264. hists->nr_entries++;
  1265. hists->stats.total_period += h->stat.period;
  1266. }
  1267. static void hierarchy_recalc_total_periods(struct hists *hists)
  1268. {
  1269. struct rb_node *node;
  1270. struct hist_entry *he;
  1271. node = rb_first(&hists->entries);
  1272. hists->stats.total_period = 0;
  1273. hists->stats.total_non_filtered_period = 0;
  1274. /*
  1275. * recalculate total period using top-level entries only
  1276. * since lower level entries only see non-filtered entries
  1277. * but upper level entries have sum of both entries.
  1278. */
  1279. while (node) {
  1280. he = rb_entry(node, struct hist_entry, rb_node);
  1281. node = rb_next(node);
  1282. hists->stats.total_period += he->stat.period;
  1283. if (!he->filtered)
  1284. hists->stats.total_non_filtered_period += he->stat.period;
  1285. }
  1286. }
  1287. static void hierarchy_insert_output_entry(struct rb_root *root,
  1288. struct hist_entry *he)
  1289. {
  1290. struct rb_node **p = &root->rb_node;
  1291. struct rb_node *parent = NULL;
  1292. struct hist_entry *iter;
  1293. struct perf_hpp_fmt *fmt;
  1294. while (*p != NULL) {
  1295. parent = *p;
  1296. iter = rb_entry(parent, struct hist_entry, rb_node);
  1297. if (hist_entry__sort(he, iter) > 0)
  1298. p = &parent->rb_left;
  1299. else
  1300. p = &parent->rb_right;
  1301. }
  1302. rb_link_node(&he->rb_node, parent, p);
  1303. rb_insert_color(&he->rb_node, root);
  1304. /* update column width of dynamic entry */
  1305. perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
  1306. if (perf_hpp__is_dynamic_entry(fmt))
  1307. fmt->sort(fmt, he, NULL);
  1308. }
  1309. }
  1310. static void hists__hierarchy_output_resort(struct hists *hists,
  1311. struct ui_progress *prog,
  1312. struct rb_root *root_in,
  1313. struct rb_root *root_out,
  1314. u64 min_callchain_hits,
  1315. bool use_callchain)
  1316. {
  1317. struct rb_node *node;
  1318. struct hist_entry *he;
  1319. *root_out = RB_ROOT;
  1320. node = rb_first(root_in);
  1321. while (node) {
  1322. he = rb_entry(node, struct hist_entry, rb_node_in);
  1323. node = rb_next(node);
  1324. hierarchy_insert_output_entry(root_out, he);
  1325. if (prog)
  1326. ui_progress__update(prog, 1);
  1327. if (!he->leaf) {
  1328. hists__hierarchy_output_resort(hists, prog,
  1329. &he->hroot_in,
  1330. &he->hroot_out,
  1331. min_callchain_hits,
  1332. use_callchain);
  1333. hists->nr_entries++;
  1334. if (!he->filtered) {
  1335. hists->nr_non_filtered_entries++;
  1336. hists__calc_col_len(hists, he);
  1337. }
  1338. continue;
  1339. }
  1340. if (!use_callchain)
  1341. continue;
  1342. if (callchain_param.mode == CHAIN_GRAPH_REL) {
  1343. u64 total = he->stat.period;
  1344. if (symbol_conf.cumulate_callchain)
  1345. total = he->stat_acc->period;
  1346. min_callchain_hits = total * (callchain_param.min_percent / 100);
  1347. }
  1348. callchain_param.sort(&he->sorted_chain, he->callchain,
  1349. min_callchain_hits, &callchain_param);
  1350. }
  1351. }
  1352. static void __hists__insert_output_entry(struct rb_root *entries,
  1353. struct hist_entry *he,
  1354. u64 min_callchain_hits,
  1355. bool use_callchain)
  1356. {
  1357. struct rb_node **p = &entries->rb_node;
  1358. struct rb_node *parent = NULL;
  1359. struct hist_entry *iter;
  1360. struct perf_hpp_fmt *fmt;
  1361. if (use_callchain) {
  1362. if (callchain_param.mode == CHAIN_GRAPH_REL) {
  1363. u64 total = he->stat.period;
  1364. if (symbol_conf.cumulate_callchain)
  1365. total = he->stat_acc->period;
  1366. min_callchain_hits = total * (callchain_param.min_percent / 100);
  1367. }
  1368. callchain_param.sort(&he->sorted_chain, he->callchain,
  1369. min_callchain_hits, &callchain_param);
  1370. }
  1371. while (*p != NULL) {
  1372. parent = *p;
  1373. iter = rb_entry(parent, struct hist_entry, rb_node);
  1374. if (hist_entry__sort(he, iter) > 0)
  1375. p = &(*p)->rb_left;
  1376. else
  1377. p = &(*p)->rb_right;
  1378. }
  1379. rb_link_node(&he->rb_node, parent, p);
  1380. rb_insert_color(&he->rb_node, entries);
  1381. perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
  1382. if (perf_hpp__is_dynamic_entry(fmt) &&
  1383. perf_hpp__defined_dynamic_entry(fmt, he->hists))
  1384. fmt->sort(fmt, he, NULL); /* update column width */
  1385. }
  1386. }
  1387. static void output_resort(struct hists *hists, struct ui_progress *prog,
  1388. bool use_callchain, hists__resort_cb_t cb)
  1389. {
  1390. struct rb_root *root;
  1391. struct rb_node *next;
  1392. struct hist_entry *n;
  1393. u64 callchain_total;
  1394. u64 min_callchain_hits;
  1395. callchain_total = hists->callchain_period;
  1396. if (symbol_conf.filter_relative)
  1397. callchain_total = hists->callchain_non_filtered_period;
  1398. min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
  1399. hists__reset_stats(hists);
  1400. hists__reset_col_len(hists);
  1401. if (symbol_conf.report_hierarchy) {
  1402. hists__hierarchy_output_resort(hists, prog,
  1403. &hists->entries_collapsed,
  1404. &hists->entries,
  1405. min_callchain_hits,
  1406. use_callchain);
  1407. hierarchy_recalc_total_periods(hists);
  1408. return;
  1409. }
  1410. if (hists__has(hists, need_collapse))
  1411. root = &hists->entries_collapsed;
  1412. else
  1413. root = hists->entries_in;
  1414. next = rb_first(root);
  1415. hists->entries = RB_ROOT;
  1416. while (next) {
  1417. n = rb_entry(next, struct hist_entry, rb_node_in);
  1418. next = rb_next(&n->rb_node_in);
  1419. if (cb && cb(n))
  1420. continue;
  1421. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
  1422. hists__inc_stats(hists, n);
  1423. if (!n->filtered)
  1424. hists__calc_col_len(hists, n);
  1425. if (prog)
  1426. ui_progress__update(prog, 1);
  1427. }
  1428. }
  1429. void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
  1430. {
  1431. bool use_callchain;
  1432. if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
  1433. use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
  1434. else
  1435. use_callchain = symbol_conf.use_callchain;
  1436. output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
  1437. }
  1438. void hists__output_resort(struct hists *hists, struct ui_progress *prog)
  1439. {
  1440. output_resort(hists, prog, symbol_conf.use_callchain, NULL);
  1441. }
  1442. void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
  1443. hists__resort_cb_t cb)
  1444. {
  1445. output_resort(hists, prog, symbol_conf.use_callchain, cb);
  1446. }
  1447. static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
  1448. {
  1449. if (he->leaf || hmd == HMD_FORCE_SIBLING)
  1450. return false;
  1451. if (he->unfolded || hmd == HMD_FORCE_CHILD)
  1452. return true;
  1453. return false;
  1454. }
  1455. struct rb_node *rb_hierarchy_last(struct rb_node *node)
  1456. {
  1457. struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
  1458. while (can_goto_child(he, HMD_NORMAL)) {
  1459. node = rb_last(&he->hroot_out);
  1460. he = rb_entry(node, struct hist_entry, rb_node);
  1461. }
  1462. return node;
  1463. }
  1464. struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
  1465. {
  1466. struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
  1467. if (can_goto_child(he, hmd))
  1468. node = rb_first(&he->hroot_out);
  1469. else
  1470. node = rb_next(node);
  1471. while (node == NULL) {
  1472. he = he->parent_he;
  1473. if (he == NULL)
  1474. break;
  1475. node = rb_next(&he->rb_node);
  1476. }
  1477. return node;
  1478. }
  1479. struct rb_node *rb_hierarchy_prev(struct rb_node *node)
  1480. {
  1481. struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
  1482. node = rb_prev(node);
  1483. if (node)
  1484. return rb_hierarchy_last(node);
  1485. he = he->parent_he;
  1486. if (he == NULL)
  1487. return NULL;
  1488. return &he->rb_node;
  1489. }
  1490. bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
  1491. {
  1492. struct rb_node *node;
  1493. struct hist_entry *child;
  1494. float percent;
  1495. if (he->leaf)
  1496. return false;
  1497. node = rb_first(&he->hroot_out);
  1498. child = rb_entry(node, struct hist_entry, rb_node);
  1499. while (node && child->filtered) {
  1500. node = rb_next(node);
  1501. child = rb_entry(node, struct hist_entry, rb_node);
  1502. }
  1503. if (node)
  1504. percent = hist_entry__get_percent_limit(child);
  1505. else
  1506. percent = 0;
  1507. return node && percent >= limit;
  1508. }
  1509. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  1510. enum hist_filter filter)
  1511. {
  1512. h->filtered &= ~(1 << filter);
  1513. if (symbol_conf.report_hierarchy) {
  1514. struct hist_entry *parent = h->parent_he;
  1515. while (parent) {
  1516. he_stat__add_stat(&parent->stat, &h->stat);
  1517. parent->filtered &= ~(1 << filter);
  1518. if (parent->filtered)
  1519. goto next;
  1520. /* force fold unfiltered entry for simplicity */
  1521. parent->unfolded = false;
  1522. parent->has_no_entry = false;
  1523. parent->row_offset = 0;
  1524. parent->nr_rows = 0;
  1525. next:
  1526. parent = parent->parent_he;
  1527. }
  1528. }
  1529. if (h->filtered)
  1530. return;
  1531. /* force fold unfiltered entry for simplicity */
  1532. h->unfolded = false;
  1533. h->has_no_entry = false;
  1534. h->row_offset = 0;
  1535. h->nr_rows = 0;
  1536. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  1537. hists__inc_filter_stats(hists, h);
  1538. hists__calc_col_len(hists, h);
  1539. }
  1540. static bool hists__filter_entry_by_dso(struct hists *hists,
  1541. struct hist_entry *he)
  1542. {
  1543. if (hists->dso_filter != NULL &&
  1544. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  1545. he->filtered |= (1 << HIST_FILTER__DSO);
  1546. return true;
  1547. }
  1548. return false;
  1549. }
  1550. static bool hists__filter_entry_by_thread(struct hists *hists,
  1551. struct hist_entry *he)
  1552. {
  1553. if (hists->thread_filter != NULL &&
  1554. he->thread != hists->thread_filter) {
  1555. he->filtered |= (1 << HIST_FILTER__THREAD);
  1556. return true;
  1557. }
  1558. return false;
  1559. }
  1560. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1561. struct hist_entry *he)
  1562. {
  1563. if (hists->symbol_filter_str != NULL &&
  1564. (!he->ms.sym || strstr(he->ms.sym->name,
  1565. hists->symbol_filter_str) == NULL)) {
  1566. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1567. return true;
  1568. }
  1569. return false;
  1570. }
  1571. static bool hists__filter_entry_by_socket(struct hists *hists,
  1572. struct hist_entry *he)
  1573. {
  1574. if ((hists->socket_filter > -1) &&
  1575. (he->socket != hists->socket_filter)) {
  1576. he->filtered |= (1 << HIST_FILTER__SOCKET);
  1577. return true;
  1578. }
  1579. return false;
  1580. }
  1581. typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
  1582. static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
  1583. {
  1584. struct rb_node *nd;
  1585. hists->stats.nr_non_filtered_samples = 0;
  1586. hists__reset_filter_stats(hists);
  1587. hists__reset_col_len(hists);
  1588. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1589. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1590. if (filter(hists, h))
  1591. continue;
  1592. hists__remove_entry_filter(hists, h, type);
  1593. }
  1594. }
  1595. static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
  1596. {
  1597. struct rb_node **p = &root->rb_node;
  1598. struct rb_node *parent = NULL;
  1599. struct hist_entry *iter;
  1600. struct rb_root new_root = RB_ROOT;
  1601. struct rb_node *nd;
  1602. while (*p != NULL) {
  1603. parent = *p;
  1604. iter = rb_entry(parent, struct hist_entry, rb_node);
  1605. if (hist_entry__sort(he, iter) > 0)
  1606. p = &(*p)->rb_left;
  1607. else
  1608. p = &(*p)->rb_right;
  1609. }
  1610. rb_link_node(&he->rb_node, parent, p);
  1611. rb_insert_color(&he->rb_node, root);
  1612. if (he->leaf || he->filtered)
  1613. return;
  1614. nd = rb_first(&he->hroot_out);
  1615. while (nd) {
  1616. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1617. nd = rb_next(nd);
  1618. rb_erase(&h->rb_node, &he->hroot_out);
  1619. resort_filtered_entry(&new_root, h);
  1620. }
  1621. he->hroot_out = new_root;
  1622. }
  1623. static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
  1624. {
  1625. struct rb_node *nd;
  1626. struct rb_root new_root = RB_ROOT;
  1627. hists->stats.nr_non_filtered_samples = 0;
  1628. hists__reset_filter_stats(hists);
  1629. hists__reset_col_len(hists);
  1630. nd = rb_first(&hists->entries);
  1631. while (nd) {
  1632. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1633. int ret;
  1634. ret = hist_entry__filter(h, type, arg);
  1635. /*
  1636. * case 1. non-matching type
  1637. * zero out the period, set filter marker and move to child
  1638. */
  1639. if (ret < 0) {
  1640. memset(&h->stat, 0, sizeof(h->stat));
  1641. h->filtered |= (1 << type);
  1642. nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
  1643. }
  1644. /*
  1645. * case 2. matched type (filter out)
  1646. * set filter marker and move to next
  1647. */
  1648. else if (ret == 1) {
  1649. h->filtered |= (1 << type);
  1650. nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
  1651. }
  1652. /*
  1653. * case 3. ok (not filtered)
  1654. * add period to hists and parents, erase the filter marker
  1655. * and move to next sibling
  1656. */
  1657. else {
  1658. hists__remove_entry_filter(hists, h, type);
  1659. nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
  1660. }
  1661. }
  1662. hierarchy_recalc_total_periods(hists);
  1663. /*
  1664. * resort output after applying a new filter since filter in a lower
  1665. * hierarchy can change periods in a upper hierarchy.
  1666. */
  1667. nd = rb_first(&hists->entries);
  1668. while (nd) {
  1669. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1670. nd = rb_next(nd);
  1671. rb_erase(&h->rb_node, &hists->entries);
  1672. resort_filtered_entry(&new_root, h);
  1673. }
  1674. hists->entries = new_root;
  1675. }
  1676. void hists__filter_by_thread(struct hists *hists)
  1677. {
  1678. if (symbol_conf.report_hierarchy)
  1679. hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
  1680. hists->thread_filter);
  1681. else
  1682. hists__filter_by_type(hists, HIST_FILTER__THREAD,
  1683. hists__filter_entry_by_thread);
  1684. }
  1685. void hists__filter_by_dso(struct hists *hists)
  1686. {
  1687. if (symbol_conf.report_hierarchy)
  1688. hists__filter_hierarchy(hists, HIST_FILTER__DSO,
  1689. hists->dso_filter);
  1690. else
  1691. hists__filter_by_type(hists, HIST_FILTER__DSO,
  1692. hists__filter_entry_by_dso);
  1693. }
  1694. void hists__filter_by_symbol(struct hists *hists)
  1695. {
  1696. if (symbol_conf.report_hierarchy)
  1697. hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
  1698. hists->symbol_filter_str);
  1699. else
  1700. hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
  1701. hists__filter_entry_by_symbol);
  1702. }
  1703. void hists__filter_by_socket(struct hists *hists)
  1704. {
  1705. if (symbol_conf.report_hierarchy)
  1706. hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
  1707. &hists->socket_filter);
  1708. else
  1709. hists__filter_by_type(hists, HIST_FILTER__SOCKET,
  1710. hists__filter_entry_by_socket);
  1711. }
  1712. void events_stats__inc(struct events_stats *stats, u32 type)
  1713. {
  1714. ++stats->nr_events[0];
  1715. ++stats->nr_events[type];
  1716. }
  1717. void hists__inc_nr_events(struct hists *hists, u32 type)
  1718. {
  1719. events_stats__inc(&hists->stats, type);
  1720. }
  1721. void hists__inc_nr_samples(struct hists *hists, bool filtered)
  1722. {
  1723. events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
  1724. if (!filtered)
  1725. hists->stats.nr_non_filtered_samples++;
  1726. }
  1727. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  1728. struct hist_entry *pair)
  1729. {
  1730. struct rb_root *root;
  1731. struct rb_node **p;
  1732. struct rb_node *parent = NULL;
  1733. struct hist_entry *he;
  1734. int64_t cmp;
  1735. if (hists__has(hists, need_collapse))
  1736. root = &hists->entries_collapsed;
  1737. else
  1738. root = hists->entries_in;
  1739. p = &root->rb_node;
  1740. while (*p != NULL) {
  1741. parent = *p;
  1742. he = rb_entry(parent, struct hist_entry, rb_node_in);
  1743. cmp = hist_entry__collapse(he, pair);
  1744. if (!cmp)
  1745. goto out;
  1746. if (cmp < 0)
  1747. p = &(*p)->rb_left;
  1748. else
  1749. p = &(*p)->rb_right;
  1750. }
  1751. he = hist_entry__new(pair, true);
  1752. if (he) {
  1753. memset(&he->stat, 0, sizeof(he->stat));
  1754. he->hists = hists;
  1755. if (symbol_conf.cumulate_callchain)
  1756. memset(he->stat_acc, 0, sizeof(he->stat));
  1757. rb_link_node(&he->rb_node_in, parent, p);
  1758. rb_insert_color(&he->rb_node_in, root);
  1759. hists__inc_stats(hists, he);
  1760. he->dummy = true;
  1761. }
  1762. out:
  1763. return he;
  1764. }
  1765. static struct hist_entry *hists__find_entry(struct hists *hists,
  1766. struct hist_entry *he)
  1767. {
  1768. struct rb_node *n;
  1769. if (hists__has(hists, need_collapse))
  1770. n = hists->entries_collapsed.rb_node;
  1771. else
  1772. n = hists->entries_in->rb_node;
  1773. while (n) {
  1774. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  1775. int64_t cmp = hist_entry__collapse(iter, he);
  1776. if (cmp < 0)
  1777. n = n->rb_left;
  1778. else if (cmp > 0)
  1779. n = n->rb_right;
  1780. else
  1781. return iter;
  1782. }
  1783. return NULL;
  1784. }
  1785. /*
  1786. * Look for pairs to link to the leader buckets (hist_entries):
  1787. */
  1788. void hists__match(struct hists *leader, struct hists *other)
  1789. {
  1790. struct rb_root *root;
  1791. struct rb_node *nd;
  1792. struct hist_entry *pos, *pair;
  1793. if (hists__has(leader, need_collapse))
  1794. root = &leader->entries_collapsed;
  1795. else
  1796. root = leader->entries_in;
  1797. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1798. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1799. pair = hists__find_entry(other, pos);
  1800. if (pair)
  1801. hist_entry__add_pair(pair, pos);
  1802. }
  1803. }
  1804. /*
  1805. * Look for entries in the other hists that are not present in the leader, if
  1806. * we find them, just add a dummy entry on the leader hists, with period=0,
  1807. * nr_events=0, to serve as the list header.
  1808. */
  1809. int hists__link(struct hists *leader, struct hists *other)
  1810. {
  1811. struct rb_root *root;
  1812. struct rb_node *nd;
  1813. struct hist_entry *pos, *pair;
  1814. if (hists__has(other, need_collapse))
  1815. root = &other->entries_collapsed;
  1816. else
  1817. root = other->entries_in;
  1818. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1819. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1820. if (!hist_entry__has_pairs(pos)) {
  1821. pair = hists__add_dummy_entry(leader, pos);
  1822. if (pair == NULL)
  1823. return -1;
  1824. hist_entry__add_pair(pos, pair);
  1825. }
  1826. }
  1827. return 0;
  1828. }
  1829. void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
  1830. struct perf_sample *sample, bool nonany_branch_mode)
  1831. {
  1832. struct branch_info *bi;
  1833. /* If we have branch cycles always annotate them. */
  1834. if (bs && bs->nr && bs->entries[0].flags.cycles) {
  1835. int i;
  1836. bi = sample__resolve_bstack(sample, al);
  1837. if (bi) {
  1838. struct addr_map_symbol *prev = NULL;
  1839. /*
  1840. * Ignore errors, still want to process the
  1841. * other entries.
  1842. *
  1843. * For non standard branch modes always
  1844. * force no IPC (prev == NULL)
  1845. *
  1846. * Note that perf stores branches reversed from
  1847. * program order!
  1848. */
  1849. for (i = bs->nr - 1; i >= 0; i--) {
  1850. addr_map_symbol__account_cycles(&bi[i].from,
  1851. nonany_branch_mode ? NULL : prev,
  1852. bi[i].flags.cycles);
  1853. prev = &bi[i].to;
  1854. }
  1855. free(bi);
  1856. }
  1857. }
  1858. }
  1859. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
  1860. {
  1861. struct perf_evsel *pos;
  1862. size_t ret = 0;
  1863. evlist__for_each_entry(evlist, pos) {
  1864. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1865. ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
  1866. }
  1867. return ret;
  1868. }
  1869. u64 hists__total_period(struct hists *hists)
  1870. {
  1871. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  1872. hists->stats.total_period;
  1873. }
  1874. int parse_filter_percentage(const struct option *opt __maybe_unused,
  1875. const char *arg, int unset __maybe_unused)
  1876. {
  1877. if (!strcmp(arg, "relative"))
  1878. symbol_conf.filter_relative = true;
  1879. else if (!strcmp(arg, "absolute"))
  1880. symbol_conf.filter_relative = false;
  1881. else
  1882. return -1;
  1883. return 0;
  1884. }
  1885. int perf_hist_config(const char *var, const char *value)
  1886. {
  1887. if (!strcmp(var, "hist.percentage"))
  1888. return parse_filter_percentage(NULL, value, 0);
  1889. return 0;
  1890. }
  1891. int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
  1892. {
  1893. memset(hists, 0, sizeof(*hists));
  1894. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  1895. hists->entries_in = &hists->entries_in_array[0];
  1896. hists->entries_collapsed = RB_ROOT;
  1897. hists->entries = RB_ROOT;
  1898. pthread_mutex_init(&hists->lock, NULL);
  1899. hists->socket_filter = -1;
  1900. hists->hpp_list = hpp_list;
  1901. INIT_LIST_HEAD(&hists->hpp_formats);
  1902. return 0;
  1903. }
  1904. static void hists__delete_remaining_entries(struct rb_root *root)
  1905. {
  1906. struct rb_node *node;
  1907. struct hist_entry *he;
  1908. while (!RB_EMPTY_ROOT(root)) {
  1909. node = rb_first(root);
  1910. rb_erase(node, root);
  1911. he = rb_entry(node, struct hist_entry, rb_node_in);
  1912. hist_entry__delete(he);
  1913. }
  1914. }
  1915. static void hists__delete_all_entries(struct hists *hists)
  1916. {
  1917. hists__delete_entries(hists);
  1918. hists__delete_remaining_entries(&hists->entries_in_array[0]);
  1919. hists__delete_remaining_entries(&hists->entries_in_array[1]);
  1920. hists__delete_remaining_entries(&hists->entries_collapsed);
  1921. }
  1922. static void hists_evsel__exit(struct perf_evsel *evsel)
  1923. {
  1924. struct hists *hists = evsel__hists(evsel);
  1925. struct perf_hpp_fmt *fmt, *pos;
  1926. struct perf_hpp_list_node *node, *tmp;
  1927. hists__delete_all_entries(hists);
  1928. list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
  1929. perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
  1930. list_del(&fmt->list);
  1931. free(fmt);
  1932. }
  1933. list_del(&node->list);
  1934. free(node);
  1935. }
  1936. }
  1937. static int hists_evsel__init(struct perf_evsel *evsel)
  1938. {
  1939. struct hists *hists = evsel__hists(evsel);
  1940. __hists__init(hists, &perf_hpp_list);
  1941. return 0;
  1942. }
  1943. /*
  1944. * XXX We probably need a hists_evsel__exit() to free the hist_entries
  1945. * stored in the rbtree...
  1946. */
  1947. int hists__init(void)
  1948. {
  1949. int err = perf_evsel__object_config(sizeof(struct hists_evsel),
  1950. hists_evsel__init,
  1951. hists_evsel__exit);
  1952. if (err)
  1953. fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
  1954. return err;
  1955. }
  1956. void perf_hpp_list__init(struct perf_hpp_list *list)
  1957. {
  1958. INIT_LIST_HEAD(&list->fields);
  1959. INIT_LIST_HEAD(&list->sorts);
  1960. }