hist.c 59 KB

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