hist.c 62 KB

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