hist.c 62 KB

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