hist.c 62 KB

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