hist.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  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. default:
  1008. return;
  1009. }
  1010. /* if it's filtered by own fmt, it has to have filter bits */
  1011. perf_hpp_list__for_each_format(he->hpp_list, fmt) {
  1012. if (check(fmt)) {
  1013. type_match = true;
  1014. break;
  1015. }
  1016. }
  1017. if (type_match) {
  1018. /*
  1019. * If the filter is for current level entry, propagate
  1020. * filter marker to parents. The marker bit was
  1021. * already set by default so it only needs to clear
  1022. * non-filtered entries.
  1023. */
  1024. if (!(he->filtered & (1 << type))) {
  1025. while (parent) {
  1026. parent->filtered &= ~(1 << type);
  1027. parent = parent->parent_he;
  1028. }
  1029. }
  1030. } else {
  1031. /*
  1032. * If current entry doesn't have matching formats, set
  1033. * filter marker for upper level entries. it will be
  1034. * cleared if its lower level entries is not filtered.
  1035. *
  1036. * For lower-level entries, it inherits parent's
  1037. * filter bit so that lower level entries of a
  1038. * non-filtered entry won't set the filter marker.
  1039. */
  1040. if (parent == NULL)
  1041. he->filtered |= (1 << type);
  1042. else
  1043. he->filtered |= (parent->filtered & (1 << type));
  1044. }
  1045. }
  1046. static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
  1047. {
  1048. hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
  1049. check_thread_entry);
  1050. hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
  1051. perf_hpp__is_dso_entry);
  1052. hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
  1053. perf_hpp__is_sym_entry);
  1054. hists__apply_filters(he->hists, he);
  1055. }
  1056. static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
  1057. struct rb_root *root,
  1058. struct hist_entry *he,
  1059. struct hist_entry *parent_he,
  1060. struct perf_hpp_list *hpp_list)
  1061. {
  1062. struct rb_node **p = &root->rb_node;
  1063. struct rb_node *parent = NULL;
  1064. struct hist_entry *iter, *new;
  1065. struct perf_hpp_fmt *fmt;
  1066. int64_t cmp;
  1067. while (*p != NULL) {
  1068. parent = *p;
  1069. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  1070. cmp = 0;
  1071. perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
  1072. cmp = fmt->collapse(fmt, iter, he);
  1073. if (cmp)
  1074. break;
  1075. }
  1076. if (!cmp) {
  1077. he_stat__add_stat(&iter->stat, &he->stat);
  1078. return iter;
  1079. }
  1080. if (cmp < 0)
  1081. p = &parent->rb_left;
  1082. else
  1083. p = &parent->rb_right;
  1084. }
  1085. new = hist_entry__new(he, true);
  1086. if (new == NULL)
  1087. return NULL;
  1088. hists->nr_entries++;
  1089. /* save related format list for output */
  1090. new->hpp_list = hpp_list;
  1091. new->parent_he = parent_he;
  1092. hist_entry__apply_hierarchy_filters(new);
  1093. /* some fields are now passed to 'new' */
  1094. perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
  1095. if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
  1096. he->trace_output = NULL;
  1097. else
  1098. new->trace_output = NULL;
  1099. if (perf_hpp__is_srcline_entry(fmt))
  1100. he->srcline = NULL;
  1101. else
  1102. new->srcline = NULL;
  1103. if (perf_hpp__is_srcfile_entry(fmt))
  1104. he->srcfile = NULL;
  1105. else
  1106. new->srcfile = NULL;
  1107. }
  1108. rb_link_node(&new->rb_node_in, parent, p);
  1109. rb_insert_color(&new->rb_node_in, root);
  1110. return new;
  1111. }
  1112. static int hists__hierarchy_insert_entry(struct hists *hists,
  1113. struct rb_root *root,
  1114. struct hist_entry *he)
  1115. {
  1116. struct perf_hpp_list_node *node;
  1117. struct hist_entry *new_he = NULL;
  1118. struct hist_entry *parent = NULL;
  1119. int depth = 0;
  1120. int ret = 0;
  1121. list_for_each_entry(node, &hists->hpp_formats, list) {
  1122. /* skip period (overhead) and elided columns */
  1123. if (node->level == 0 || node->skip)
  1124. continue;
  1125. /* insert copy of 'he' for each fmt into the hierarchy */
  1126. new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
  1127. if (new_he == NULL) {
  1128. ret = -1;
  1129. break;
  1130. }
  1131. root = &new_he->hroot_in;
  1132. new_he->depth = depth++;
  1133. parent = new_he;
  1134. }
  1135. if (new_he) {
  1136. new_he->leaf = true;
  1137. if (symbol_conf.use_callchain) {
  1138. callchain_cursor_reset(&callchain_cursor);
  1139. if (callchain_merge(&callchain_cursor,
  1140. new_he->callchain,
  1141. he->callchain) < 0)
  1142. ret = -1;
  1143. }
  1144. }
  1145. /* 'he' is no longer used */
  1146. hist_entry__delete(he);
  1147. /* return 0 (or -1) since it already applied filters */
  1148. return ret;
  1149. }
  1150. static int hists__collapse_insert_entry(struct hists *hists,
  1151. struct rb_root *root,
  1152. struct hist_entry *he)
  1153. {
  1154. struct rb_node **p = &root->rb_node;
  1155. struct rb_node *parent = NULL;
  1156. struct hist_entry *iter;
  1157. int64_t cmp;
  1158. if (symbol_conf.report_hierarchy)
  1159. return hists__hierarchy_insert_entry(hists, root, he);
  1160. while (*p != NULL) {
  1161. parent = *p;
  1162. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  1163. cmp = hist_entry__collapse(iter, he);
  1164. if (!cmp) {
  1165. int ret = 0;
  1166. he_stat__add_stat(&iter->stat, &he->stat);
  1167. if (symbol_conf.cumulate_callchain)
  1168. he_stat__add_stat(iter->stat_acc, he->stat_acc);
  1169. if (symbol_conf.use_callchain) {
  1170. callchain_cursor_reset(&callchain_cursor);
  1171. if (callchain_merge(&callchain_cursor,
  1172. iter->callchain,
  1173. he->callchain) < 0)
  1174. ret = -1;
  1175. }
  1176. hist_entry__delete(he);
  1177. return ret;
  1178. }
  1179. if (cmp < 0)
  1180. p = &(*p)->rb_left;
  1181. else
  1182. p = &(*p)->rb_right;
  1183. }
  1184. hists->nr_entries++;
  1185. rb_link_node(&he->rb_node_in, parent, p);
  1186. rb_insert_color(&he->rb_node_in, root);
  1187. return 1;
  1188. }
  1189. struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  1190. {
  1191. struct rb_root *root;
  1192. pthread_mutex_lock(&hists->lock);
  1193. root = hists->entries_in;
  1194. if (++hists->entries_in > &hists->entries_in_array[1])
  1195. hists->entries_in = &hists->entries_in_array[0];
  1196. pthread_mutex_unlock(&hists->lock);
  1197. return root;
  1198. }
  1199. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  1200. {
  1201. hists__filter_entry_by_dso(hists, he);
  1202. hists__filter_entry_by_thread(hists, he);
  1203. hists__filter_entry_by_symbol(hists, he);
  1204. hists__filter_entry_by_socket(hists, he);
  1205. }
  1206. int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  1207. {
  1208. struct rb_root *root;
  1209. struct rb_node *next;
  1210. struct hist_entry *n;
  1211. int ret;
  1212. if (!hists__has(hists, need_collapse))
  1213. return 0;
  1214. hists->nr_entries = 0;
  1215. root = hists__get_rotate_entries_in(hists);
  1216. next = rb_first(root);
  1217. while (next) {
  1218. if (session_done())
  1219. break;
  1220. n = rb_entry(next, struct hist_entry, rb_node_in);
  1221. next = rb_next(&n->rb_node_in);
  1222. rb_erase(&n->rb_node_in, root);
  1223. ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
  1224. if (ret < 0)
  1225. return -1;
  1226. if (ret) {
  1227. /*
  1228. * If it wasn't combined with one of the entries already
  1229. * collapsed, we need to apply the filters that may have
  1230. * been set by, say, the hist_browser.
  1231. */
  1232. hists__apply_filters(hists, n);
  1233. }
  1234. if (prog)
  1235. ui_progress__update(prog, 1);
  1236. }
  1237. return 0;
  1238. }
  1239. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  1240. {
  1241. struct hists *hists = a->hists;
  1242. struct perf_hpp_fmt *fmt;
  1243. int64_t cmp = 0;
  1244. hists__for_each_sort_list(hists, fmt) {
  1245. if (perf_hpp__should_skip(fmt, a->hists))
  1246. continue;
  1247. cmp = fmt->sort(fmt, a, b);
  1248. if (cmp)
  1249. break;
  1250. }
  1251. return cmp;
  1252. }
  1253. static void hists__reset_filter_stats(struct hists *hists)
  1254. {
  1255. hists->nr_non_filtered_entries = 0;
  1256. hists->stats.total_non_filtered_period = 0;
  1257. }
  1258. void hists__reset_stats(struct hists *hists)
  1259. {
  1260. hists->nr_entries = 0;
  1261. hists->stats.total_period = 0;
  1262. hists__reset_filter_stats(hists);
  1263. }
  1264. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  1265. {
  1266. hists->nr_non_filtered_entries++;
  1267. hists->stats.total_non_filtered_period += h->stat.period;
  1268. }
  1269. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  1270. {
  1271. if (!h->filtered)
  1272. hists__inc_filter_stats(hists, h);
  1273. hists->nr_entries++;
  1274. hists->stats.total_period += h->stat.period;
  1275. }
  1276. static void hierarchy_recalc_total_periods(struct hists *hists)
  1277. {
  1278. struct rb_node *node;
  1279. struct hist_entry *he;
  1280. node = rb_first(&hists->entries);
  1281. hists->stats.total_period = 0;
  1282. hists->stats.total_non_filtered_period = 0;
  1283. /*
  1284. * recalculate total period using top-level entries only
  1285. * since lower level entries only see non-filtered entries
  1286. * but upper level entries have sum of both entries.
  1287. */
  1288. while (node) {
  1289. he = rb_entry(node, struct hist_entry, rb_node);
  1290. node = rb_next(node);
  1291. hists->stats.total_period += he->stat.period;
  1292. if (!he->filtered)
  1293. hists->stats.total_non_filtered_period += he->stat.period;
  1294. }
  1295. }
  1296. static void hierarchy_insert_output_entry(struct rb_root *root,
  1297. struct hist_entry *he)
  1298. {
  1299. struct rb_node **p = &root->rb_node;
  1300. struct rb_node *parent = NULL;
  1301. struct hist_entry *iter;
  1302. struct perf_hpp_fmt *fmt;
  1303. while (*p != NULL) {
  1304. parent = *p;
  1305. iter = rb_entry(parent, struct hist_entry, rb_node);
  1306. if (hist_entry__sort(he, iter) > 0)
  1307. p = &parent->rb_left;
  1308. else
  1309. p = &parent->rb_right;
  1310. }
  1311. rb_link_node(&he->rb_node, parent, p);
  1312. rb_insert_color(&he->rb_node, root);
  1313. /* update column width of dynamic entry */
  1314. perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
  1315. if (perf_hpp__is_dynamic_entry(fmt))
  1316. fmt->sort(fmt, he, NULL);
  1317. }
  1318. }
  1319. static void hists__hierarchy_output_resort(struct hists *hists,
  1320. struct ui_progress *prog,
  1321. struct rb_root *root_in,
  1322. struct rb_root *root_out,
  1323. u64 min_callchain_hits,
  1324. bool use_callchain)
  1325. {
  1326. struct rb_node *node;
  1327. struct hist_entry *he;
  1328. *root_out = RB_ROOT;
  1329. node = rb_first(root_in);
  1330. while (node) {
  1331. he = rb_entry(node, struct hist_entry, rb_node_in);
  1332. node = rb_next(node);
  1333. hierarchy_insert_output_entry(root_out, he);
  1334. if (prog)
  1335. ui_progress__update(prog, 1);
  1336. hists->nr_entries++;
  1337. if (!he->filtered) {
  1338. hists->nr_non_filtered_entries++;
  1339. hists__calc_col_len(hists, he);
  1340. }
  1341. if (!he->leaf) {
  1342. hists__hierarchy_output_resort(hists, prog,
  1343. &he->hroot_in,
  1344. &he->hroot_out,
  1345. min_callchain_hits,
  1346. use_callchain);
  1347. continue;
  1348. }
  1349. if (!use_callchain)
  1350. continue;
  1351. if (callchain_param.mode == CHAIN_GRAPH_REL) {
  1352. u64 total = he->stat.period;
  1353. if (symbol_conf.cumulate_callchain)
  1354. total = he->stat_acc->period;
  1355. min_callchain_hits = total * (callchain_param.min_percent / 100);
  1356. }
  1357. callchain_param.sort(&he->sorted_chain, he->callchain,
  1358. min_callchain_hits, &callchain_param);
  1359. }
  1360. }
  1361. static void __hists__insert_output_entry(struct rb_root *entries,
  1362. struct hist_entry *he,
  1363. u64 min_callchain_hits,
  1364. bool use_callchain)
  1365. {
  1366. struct rb_node **p = &entries->rb_node;
  1367. struct rb_node *parent = NULL;
  1368. struct hist_entry *iter;
  1369. struct perf_hpp_fmt *fmt;
  1370. if (use_callchain) {
  1371. if (callchain_param.mode == CHAIN_GRAPH_REL) {
  1372. u64 total = he->stat.period;
  1373. if (symbol_conf.cumulate_callchain)
  1374. total = he->stat_acc->period;
  1375. min_callchain_hits = total * (callchain_param.min_percent / 100);
  1376. }
  1377. callchain_param.sort(&he->sorted_chain, he->callchain,
  1378. min_callchain_hits, &callchain_param);
  1379. }
  1380. while (*p != NULL) {
  1381. parent = *p;
  1382. iter = rb_entry(parent, struct hist_entry, rb_node);
  1383. if (hist_entry__sort(he, iter) > 0)
  1384. p = &(*p)->rb_left;
  1385. else
  1386. p = &(*p)->rb_right;
  1387. }
  1388. rb_link_node(&he->rb_node, parent, p);
  1389. rb_insert_color(&he->rb_node, entries);
  1390. perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
  1391. if (perf_hpp__is_dynamic_entry(fmt) &&
  1392. perf_hpp__defined_dynamic_entry(fmt, he->hists))
  1393. fmt->sort(fmt, he, NULL); /* update column width */
  1394. }
  1395. }
  1396. static void output_resort(struct hists *hists, struct ui_progress *prog,
  1397. bool use_callchain, hists__resort_cb_t cb)
  1398. {
  1399. struct rb_root *root;
  1400. struct rb_node *next;
  1401. struct hist_entry *n;
  1402. u64 callchain_total;
  1403. u64 min_callchain_hits;
  1404. callchain_total = hists->callchain_period;
  1405. if (symbol_conf.filter_relative)
  1406. callchain_total = hists->callchain_non_filtered_period;
  1407. min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
  1408. hists__reset_stats(hists);
  1409. hists__reset_col_len(hists);
  1410. if (symbol_conf.report_hierarchy) {
  1411. hists__hierarchy_output_resort(hists, prog,
  1412. &hists->entries_collapsed,
  1413. &hists->entries,
  1414. min_callchain_hits,
  1415. use_callchain);
  1416. hierarchy_recalc_total_periods(hists);
  1417. return;
  1418. }
  1419. if (hists__has(hists, need_collapse))
  1420. root = &hists->entries_collapsed;
  1421. else
  1422. root = hists->entries_in;
  1423. next = rb_first(root);
  1424. hists->entries = RB_ROOT;
  1425. while (next) {
  1426. n = rb_entry(next, struct hist_entry, rb_node_in);
  1427. next = rb_next(&n->rb_node_in);
  1428. if (cb && cb(n))
  1429. continue;
  1430. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
  1431. hists__inc_stats(hists, n);
  1432. if (!n->filtered)
  1433. hists__calc_col_len(hists, n);
  1434. if (prog)
  1435. ui_progress__update(prog, 1);
  1436. }
  1437. }
  1438. void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
  1439. {
  1440. bool use_callchain;
  1441. if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
  1442. use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
  1443. else
  1444. use_callchain = symbol_conf.use_callchain;
  1445. output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
  1446. }
  1447. void hists__output_resort(struct hists *hists, struct ui_progress *prog)
  1448. {
  1449. output_resort(hists, prog, symbol_conf.use_callchain, NULL);
  1450. }
  1451. void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
  1452. hists__resort_cb_t cb)
  1453. {
  1454. output_resort(hists, prog, symbol_conf.use_callchain, cb);
  1455. }
  1456. static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
  1457. {
  1458. if (he->leaf || hmd == HMD_FORCE_SIBLING)
  1459. return false;
  1460. if (he->unfolded || hmd == HMD_FORCE_CHILD)
  1461. return true;
  1462. return false;
  1463. }
  1464. struct rb_node *rb_hierarchy_last(struct rb_node *node)
  1465. {
  1466. struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
  1467. while (can_goto_child(he, HMD_NORMAL)) {
  1468. node = rb_last(&he->hroot_out);
  1469. he = rb_entry(node, struct hist_entry, rb_node);
  1470. }
  1471. return node;
  1472. }
  1473. struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
  1474. {
  1475. struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
  1476. if (can_goto_child(he, hmd))
  1477. node = rb_first(&he->hroot_out);
  1478. else
  1479. node = rb_next(node);
  1480. while (node == NULL) {
  1481. he = he->parent_he;
  1482. if (he == NULL)
  1483. break;
  1484. node = rb_next(&he->rb_node);
  1485. }
  1486. return node;
  1487. }
  1488. struct rb_node *rb_hierarchy_prev(struct rb_node *node)
  1489. {
  1490. struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
  1491. node = rb_prev(node);
  1492. if (node)
  1493. return rb_hierarchy_last(node);
  1494. he = he->parent_he;
  1495. if (he == NULL)
  1496. return NULL;
  1497. return &he->rb_node;
  1498. }
  1499. bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
  1500. {
  1501. struct rb_node *node;
  1502. struct hist_entry *child;
  1503. float percent;
  1504. if (he->leaf)
  1505. return false;
  1506. node = rb_first(&he->hroot_out);
  1507. child = rb_entry(node, struct hist_entry, rb_node);
  1508. while (node && child->filtered) {
  1509. node = rb_next(node);
  1510. child = rb_entry(node, struct hist_entry, rb_node);
  1511. }
  1512. if (node)
  1513. percent = hist_entry__get_percent_limit(child);
  1514. else
  1515. percent = 0;
  1516. return node && percent >= limit;
  1517. }
  1518. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  1519. enum hist_filter filter)
  1520. {
  1521. h->filtered &= ~(1 << filter);
  1522. if (symbol_conf.report_hierarchy) {
  1523. struct hist_entry *parent = h->parent_he;
  1524. while (parent) {
  1525. he_stat__add_stat(&parent->stat, &h->stat);
  1526. parent->filtered &= ~(1 << filter);
  1527. if (parent->filtered)
  1528. goto next;
  1529. /* force fold unfiltered entry for simplicity */
  1530. parent->unfolded = false;
  1531. parent->has_no_entry = false;
  1532. parent->row_offset = 0;
  1533. parent->nr_rows = 0;
  1534. next:
  1535. parent = parent->parent_he;
  1536. }
  1537. }
  1538. if (h->filtered)
  1539. return;
  1540. /* force fold unfiltered entry for simplicity */
  1541. h->unfolded = false;
  1542. h->has_no_entry = false;
  1543. h->row_offset = 0;
  1544. h->nr_rows = 0;
  1545. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  1546. hists__inc_filter_stats(hists, h);
  1547. hists__calc_col_len(hists, h);
  1548. }
  1549. static bool hists__filter_entry_by_dso(struct hists *hists,
  1550. struct hist_entry *he)
  1551. {
  1552. if (hists->dso_filter != NULL &&
  1553. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  1554. he->filtered |= (1 << HIST_FILTER__DSO);
  1555. return true;
  1556. }
  1557. return false;
  1558. }
  1559. static bool hists__filter_entry_by_thread(struct hists *hists,
  1560. struct hist_entry *he)
  1561. {
  1562. if (hists->thread_filter != NULL &&
  1563. he->thread != hists->thread_filter) {
  1564. he->filtered |= (1 << HIST_FILTER__THREAD);
  1565. return true;
  1566. }
  1567. return false;
  1568. }
  1569. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1570. struct hist_entry *he)
  1571. {
  1572. if (hists->symbol_filter_str != NULL &&
  1573. (!he->ms.sym || strstr(he->ms.sym->name,
  1574. hists->symbol_filter_str) == NULL)) {
  1575. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1576. return true;
  1577. }
  1578. return false;
  1579. }
  1580. static bool hists__filter_entry_by_socket(struct hists *hists,
  1581. struct hist_entry *he)
  1582. {
  1583. if ((hists->socket_filter > -1) &&
  1584. (he->socket != hists->socket_filter)) {
  1585. he->filtered |= (1 << HIST_FILTER__SOCKET);
  1586. return true;
  1587. }
  1588. return false;
  1589. }
  1590. typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
  1591. static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
  1592. {
  1593. struct rb_node *nd;
  1594. hists->stats.nr_non_filtered_samples = 0;
  1595. hists__reset_filter_stats(hists);
  1596. hists__reset_col_len(hists);
  1597. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1598. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1599. if (filter(hists, h))
  1600. continue;
  1601. hists__remove_entry_filter(hists, h, type);
  1602. }
  1603. }
  1604. static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
  1605. {
  1606. struct rb_node **p = &root->rb_node;
  1607. struct rb_node *parent = NULL;
  1608. struct hist_entry *iter;
  1609. struct rb_root new_root = RB_ROOT;
  1610. struct rb_node *nd;
  1611. while (*p != NULL) {
  1612. parent = *p;
  1613. iter = rb_entry(parent, struct hist_entry, rb_node);
  1614. if (hist_entry__sort(he, iter) > 0)
  1615. p = &(*p)->rb_left;
  1616. else
  1617. p = &(*p)->rb_right;
  1618. }
  1619. rb_link_node(&he->rb_node, parent, p);
  1620. rb_insert_color(&he->rb_node, root);
  1621. if (he->leaf || he->filtered)
  1622. return;
  1623. nd = rb_first(&he->hroot_out);
  1624. while (nd) {
  1625. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1626. nd = rb_next(nd);
  1627. rb_erase(&h->rb_node, &he->hroot_out);
  1628. resort_filtered_entry(&new_root, h);
  1629. }
  1630. he->hroot_out = new_root;
  1631. }
  1632. static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
  1633. {
  1634. struct rb_node *nd;
  1635. struct rb_root new_root = RB_ROOT;
  1636. hists->stats.nr_non_filtered_samples = 0;
  1637. hists__reset_filter_stats(hists);
  1638. hists__reset_col_len(hists);
  1639. nd = rb_first(&hists->entries);
  1640. while (nd) {
  1641. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1642. int ret;
  1643. ret = hist_entry__filter(h, type, arg);
  1644. /*
  1645. * case 1. non-matching type
  1646. * zero out the period, set filter marker and move to child
  1647. */
  1648. if (ret < 0) {
  1649. memset(&h->stat, 0, sizeof(h->stat));
  1650. h->filtered |= (1 << type);
  1651. nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
  1652. }
  1653. /*
  1654. * case 2. matched type (filter out)
  1655. * set filter marker and move to next
  1656. */
  1657. else if (ret == 1) {
  1658. h->filtered |= (1 << type);
  1659. nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
  1660. }
  1661. /*
  1662. * case 3. ok (not filtered)
  1663. * add period to hists and parents, erase the filter marker
  1664. * and move to next sibling
  1665. */
  1666. else {
  1667. hists__remove_entry_filter(hists, h, type);
  1668. nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
  1669. }
  1670. }
  1671. hierarchy_recalc_total_periods(hists);
  1672. /*
  1673. * resort output after applying a new filter since filter in a lower
  1674. * hierarchy can change periods in a upper hierarchy.
  1675. */
  1676. nd = rb_first(&hists->entries);
  1677. while (nd) {
  1678. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1679. nd = rb_next(nd);
  1680. rb_erase(&h->rb_node, &hists->entries);
  1681. resort_filtered_entry(&new_root, h);
  1682. }
  1683. hists->entries = new_root;
  1684. }
  1685. void hists__filter_by_thread(struct hists *hists)
  1686. {
  1687. if (symbol_conf.report_hierarchy)
  1688. hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
  1689. hists->thread_filter);
  1690. else
  1691. hists__filter_by_type(hists, HIST_FILTER__THREAD,
  1692. hists__filter_entry_by_thread);
  1693. }
  1694. void hists__filter_by_dso(struct hists *hists)
  1695. {
  1696. if (symbol_conf.report_hierarchy)
  1697. hists__filter_hierarchy(hists, HIST_FILTER__DSO,
  1698. hists->dso_filter);
  1699. else
  1700. hists__filter_by_type(hists, HIST_FILTER__DSO,
  1701. hists__filter_entry_by_dso);
  1702. }
  1703. void hists__filter_by_symbol(struct hists *hists)
  1704. {
  1705. if (symbol_conf.report_hierarchy)
  1706. hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
  1707. hists->symbol_filter_str);
  1708. else
  1709. hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
  1710. hists__filter_entry_by_symbol);
  1711. }
  1712. void hists__filter_by_socket(struct hists *hists)
  1713. {
  1714. if (symbol_conf.report_hierarchy)
  1715. hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
  1716. &hists->socket_filter);
  1717. else
  1718. hists__filter_by_type(hists, HIST_FILTER__SOCKET,
  1719. hists__filter_entry_by_socket);
  1720. }
  1721. void events_stats__inc(struct events_stats *stats, u32 type)
  1722. {
  1723. ++stats->nr_events[0];
  1724. ++stats->nr_events[type];
  1725. }
  1726. void hists__inc_nr_events(struct hists *hists, u32 type)
  1727. {
  1728. events_stats__inc(&hists->stats, type);
  1729. }
  1730. void hists__inc_nr_samples(struct hists *hists, bool filtered)
  1731. {
  1732. events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
  1733. if (!filtered)
  1734. hists->stats.nr_non_filtered_samples++;
  1735. }
  1736. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  1737. struct hist_entry *pair)
  1738. {
  1739. struct rb_root *root;
  1740. struct rb_node **p;
  1741. struct rb_node *parent = NULL;
  1742. struct hist_entry *he;
  1743. int64_t cmp;
  1744. if (hists__has(hists, need_collapse))
  1745. root = &hists->entries_collapsed;
  1746. else
  1747. root = hists->entries_in;
  1748. p = &root->rb_node;
  1749. while (*p != NULL) {
  1750. parent = *p;
  1751. he = rb_entry(parent, struct hist_entry, rb_node_in);
  1752. cmp = hist_entry__collapse(he, pair);
  1753. if (!cmp)
  1754. goto out;
  1755. if (cmp < 0)
  1756. p = &(*p)->rb_left;
  1757. else
  1758. p = &(*p)->rb_right;
  1759. }
  1760. he = hist_entry__new(pair, true);
  1761. if (he) {
  1762. memset(&he->stat, 0, sizeof(he->stat));
  1763. he->hists = hists;
  1764. if (symbol_conf.cumulate_callchain)
  1765. memset(he->stat_acc, 0, sizeof(he->stat));
  1766. rb_link_node(&he->rb_node_in, parent, p);
  1767. rb_insert_color(&he->rb_node_in, root);
  1768. hists__inc_stats(hists, he);
  1769. he->dummy = true;
  1770. }
  1771. out:
  1772. return he;
  1773. }
  1774. static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
  1775. struct rb_root *root,
  1776. struct hist_entry *pair)
  1777. {
  1778. struct rb_node **p;
  1779. struct rb_node *parent = NULL;
  1780. struct hist_entry *he;
  1781. struct perf_hpp_fmt *fmt;
  1782. p = &root->rb_node;
  1783. while (*p != NULL) {
  1784. int64_t cmp = 0;
  1785. parent = *p;
  1786. he = rb_entry(parent, struct hist_entry, rb_node_in);
  1787. perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
  1788. cmp = fmt->collapse(fmt, he, pair);
  1789. if (cmp)
  1790. break;
  1791. }
  1792. if (!cmp)
  1793. goto out;
  1794. if (cmp < 0)
  1795. p = &parent->rb_left;
  1796. else
  1797. p = &parent->rb_right;
  1798. }
  1799. he = hist_entry__new(pair, true);
  1800. if (he) {
  1801. rb_link_node(&he->rb_node_in, parent, p);
  1802. rb_insert_color(&he->rb_node_in, root);
  1803. he->dummy = true;
  1804. he->hists = hists;
  1805. memset(&he->stat, 0, sizeof(he->stat));
  1806. hists__inc_stats(hists, he);
  1807. }
  1808. out:
  1809. return he;
  1810. }
  1811. static struct hist_entry *hists__find_entry(struct hists *hists,
  1812. struct hist_entry *he)
  1813. {
  1814. struct rb_node *n;
  1815. if (hists__has(hists, need_collapse))
  1816. n = hists->entries_collapsed.rb_node;
  1817. else
  1818. n = hists->entries_in->rb_node;
  1819. while (n) {
  1820. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  1821. int64_t cmp = hist_entry__collapse(iter, he);
  1822. if (cmp < 0)
  1823. n = n->rb_left;
  1824. else if (cmp > 0)
  1825. n = n->rb_right;
  1826. else
  1827. return iter;
  1828. }
  1829. return NULL;
  1830. }
  1831. static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
  1832. struct hist_entry *he)
  1833. {
  1834. struct rb_node *n = root->rb_node;
  1835. while (n) {
  1836. struct hist_entry *iter;
  1837. struct perf_hpp_fmt *fmt;
  1838. int64_t cmp = 0;
  1839. iter = rb_entry(n, struct hist_entry, rb_node_in);
  1840. perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
  1841. cmp = fmt->collapse(fmt, iter, he);
  1842. if (cmp)
  1843. break;
  1844. }
  1845. if (cmp < 0)
  1846. n = n->rb_left;
  1847. else if (cmp > 0)
  1848. n = n->rb_right;
  1849. else
  1850. return iter;
  1851. }
  1852. return NULL;
  1853. }
  1854. static void hists__match_hierarchy(struct rb_root *leader_root,
  1855. struct rb_root *other_root)
  1856. {
  1857. struct rb_node *nd;
  1858. struct hist_entry *pos, *pair;
  1859. for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
  1860. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1861. pair = hists__find_hierarchy_entry(other_root, pos);
  1862. if (pair) {
  1863. hist_entry__add_pair(pair, pos);
  1864. hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
  1865. }
  1866. }
  1867. }
  1868. /*
  1869. * Look for pairs to link to the leader buckets (hist_entries):
  1870. */
  1871. void hists__match(struct hists *leader, struct hists *other)
  1872. {
  1873. struct rb_root *root;
  1874. struct rb_node *nd;
  1875. struct hist_entry *pos, *pair;
  1876. if (symbol_conf.report_hierarchy) {
  1877. /* hierarchy report always collapses entries */
  1878. return hists__match_hierarchy(&leader->entries_collapsed,
  1879. &other->entries_collapsed);
  1880. }
  1881. if (hists__has(leader, need_collapse))
  1882. root = &leader->entries_collapsed;
  1883. else
  1884. root = leader->entries_in;
  1885. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1886. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1887. pair = hists__find_entry(other, pos);
  1888. if (pair)
  1889. hist_entry__add_pair(pair, pos);
  1890. }
  1891. }
  1892. static int hists__link_hierarchy(struct hists *leader_hists,
  1893. struct hist_entry *parent,
  1894. struct rb_root *leader_root,
  1895. struct rb_root *other_root)
  1896. {
  1897. struct rb_node *nd;
  1898. struct hist_entry *pos, *leader;
  1899. for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
  1900. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1901. if (hist_entry__has_pairs(pos)) {
  1902. bool found = false;
  1903. list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
  1904. if (leader->hists == leader_hists) {
  1905. found = true;
  1906. break;
  1907. }
  1908. }
  1909. if (!found)
  1910. return -1;
  1911. } else {
  1912. leader = add_dummy_hierarchy_entry(leader_hists,
  1913. leader_root, pos);
  1914. if (leader == NULL)
  1915. return -1;
  1916. /* do not point parent in the pos */
  1917. leader->parent_he = parent;
  1918. hist_entry__add_pair(pos, leader);
  1919. }
  1920. if (!pos->leaf) {
  1921. if (hists__link_hierarchy(leader_hists, leader,
  1922. &leader->hroot_in,
  1923. &pos->hroot_in) < 0)
  1924. return -1;
  1925. }
  1926. }
  1927. return 0;
  1928. }
  1929. /*
  1930. * Look for entries in the other hists that are not present in the leader, if
  1931. * we find them, just add a dummy entry on the leader hists, with period=0,
  1932. * nr_events=0, to serve as the list header.
  1933. */
  1934. int hists__link(struct hists *leader, struct hists *other)
  1935. {
  1936. struct rb_root *root;
  1937. struct rb_node *nd;
  1938. struct hist_entry *pos, *pair;
  1939. if (symbol_conf.report_hierarchy) {
  1940. /* hierarchy report always collapses entries */
  1941. return hists__link_hierarchy(leader, NULL,
  1942. &leader->entries_collapsed,
  1943. &other->entries_collapsed);
  1944. }
  1945. if (hists__has(other, need_collapse))
  1946. root = &other->entries_collapsed;
  1947. else
  1948. root = other->entries_in;
  1949. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1950. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1951. if (!hist_entry__has_pairs(pos)) {
  1952. pair = hists__add_dummy_entry(leader, pos);
  1953. if (pair == NULL)
  1954. return -1;
  1955. hist_entry__add_pair(pos, pair);
  1956. }
  1957. }
  1958. return 0;
  1959. }
  1960. void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
  1961. struct perf_sample *sample, bool nonany_branch_mode)
  1962. {
  1963. struct branch_info *bi;
  1964. /* If we have branch cycles always annotate them. */
  1965. if (bs && bs->nr && bs->entries[0].flags.cycles) {
  1966. int i;
  1967. bi = sample__resolve_bstack(sample, al);
  1968. if (bi) {
  1969. struct addr_map_symbol *prev = NULL;
  1970. /*
  1971. * Ignore errors, still want to process the
  1972. * other entries.
  1973. *
  1974. * For non standard branch modes always
  1975. * force no IPC (prev == NULL)
  1976. *
  1977. * Note that perf stores branches reversed from
  1978. * program order!
  1979. */
  1980. for (i = bs->nr - 1; i >= 0; i--) {
  1981. addr_map_symbol__account_cycles(&bi[i].from,
  1982. nonany_branch_mode ? NULL : prev,
  1983. bi[i].flags.cycles);
  1984. prev = &bi[i].to;
  1985. }
  1986. free(bi);
  1987. }
  1988. }
  1989. }
  1990. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
  1991. {
  1992. struct perf_evsel *pos;
  1993. size_t ret = 0;
  1994. evlist__for_each_entry(evlist, pos) {
  1995. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1996. ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
  1997. }
  1998. return ret;
  1999. }
  2000. u64 hists__total_period(struct hists *hists)
  2001. {
  2002. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  2003. hists->stats.total_period;
  2004. }
  2005. int parse_filter_percentage(const struct option *opt __maybe_unused,
  2006. const char *arg, int unset __maybe_unused)
  2007. {
  2008. if (!strcmp(arg, "relative"))
  2009. symbol_conf.filter_relative = true;
  2010. else if (!strcmp(arg, "absolute"))
  2011. symbol_conf.filter_relative = false;
  2012. else
  2013. return -1;
  2014. return 0;
  2015. }
  2016. int perf_hist_config(const char *var, const char *value)
  2017. {
  2018. if (!strcmp(var, "hist.percentage"))
  2019. return parse_filter_percentage(NULL, value, 0);
  2020. return 0;
  2021. }
  2022. int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
  2023. {
  2024. memset(hists, 0, sizeof(*hists));
  2025. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  2026. hists->entries_in = &hists->entries_in_array[0];
  2027. hists->entries_collapsed = RB_ROOT;
  2028. hists->entries = RB_ROOT;
  2029. pthread_mutex_init(&hists->lock, NULL);
  2030. hists->socket_filter = -1;
  2031. hists->hpp_list = hpp_list;
  2032. INIT_LIST_HEAD(&hists->hpp_formats);
  2033. return 0;
  2034. }
  2035. static void hists__delete_remaining_entries(struct rb_root *root)
  2036. {
  2037. struct rb_node *node;
  2038. struct hist_entry *he;
  2039. while (!RB_EMPTY_ROOT(root)) {
  2040. node = rb_first(root);
  2041. rb_erase(node, root);
  2042. he = rb_entry(node, struct hist_entry, rb_node_in);
  2043. hist_entry__delete(he);
  2044. }
  2045. }
  2046. static void hists__delete_all_entries(struct hists *hists)
  2047. {
  2048. hists__delete_entries(hists);
  2049. hists__delete_remaining_entries(&hists->entries_in_array[0]);
  2050. hists__delete_remaining_entries(&hists->entries_in_array[1]);
  2051. hists__delete_remaining_entries(&hists->entries_collapsed);
  2052. }
  2053. static void hists_evsel__exit(struct perf_evsel *evsel)
  2054. {
  2055. struct hists *hists = evsel__hists(evsel);
  2056. struct perf_hpp_fmt *fmt, *pos;
  2057. struct perf_hpp_list_node *node, *tmp;
  2058. hists__delete_all_entries(hists);
  2059. list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
  2060. perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
  2061. list_del(&fmt->list);
  2062. free(fmt);
  2063. }
  2064. list_del(&node->list);
  2065. free(node);
  2066. }
  2067. }
  2068. static int hists_evsel__init(struct perf_evsel *evsel)
  2069. {
  2070. struct hists *hists = evsel__hists(evsel);
  2071. __hists__init(hists, &perf_hpp_list);
  2072. return 0;
  2073. }
  2074. /*
  2075. * XXX We probably need a hists_evsel__exit() to free the hist_entries
  2076. * stored in the rbtree...
  2077. */
  2078. int hists__init(void)
  2079. {
  2080. int err = perf_evsel__object_config(sizeof(struct hists_evsel),
  2081. hists_evsel__init,
  2082. hists_evsel__exit);
  2083. if (err)
  2084. fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
  2085. return err;
  2086. }
  2087. void perf_hpp_list__init(struct perf_hpp_list *list)
  2088. {
  2089. INIT_LIST_HEAD(&list->fields);
  2090. INIT_LIST_HEAD(&list->sorts);
  2091. }