hist.c 54 KB

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