hist.c 53 KB

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