hist.c 67 KB

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