hist.c 59 KB

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