hist.c 59 KB

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