hist.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. }
  159. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  160. {
  161. struct rb_node *next = rb_first(&hists->entries);
  162. struct hist_entry *n;
  163. int row = 0;
  164. hists__reset_col_len(hists);
  165. while (next && row++ < max_rows) {
  166. n = rb_entry(next, struct hist_entry, rb_node);
  167. if (!n->filtered)
  168. hists__calc_col_len(hists, n);
  169. next = rb_next(&n->rb_node);
  170. }
  171. }
  172. static void he_stat__add_cpumode_period(struct he_stat *he_stat,
  173. unsigned int cpumode, u64 period)
  174. {
  175. switch (cpumode) {
  176. case PERF_RECORD_MISC_KERNEL:
  177. he_stat->period_sys += period;
  178. break;
  179. case PERF_RECORD_MISC_USER:
  180. he_stat->period_us += period;
  181. break;
  182. case PERF_RECORD_MISC_GUEST_KERNEL:
  183. he_stat->period_guest_sys += period;
  184. break;
  185. case PERF_RECORD_MISC_GUEST_USER:
  186. he_stat->period_guest_us += period;
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. static void he_stat__add_period(struct he_stat *he_stat, u64 period,
  193. u64 weight)
  194. {
  195. he_stat->period += period;
  196. he_stat->weight += weight;
  197. he_stat->nr_events += 1;
  198. }
  199. static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
  200. {
  201. dest->period += src->period;
  202. dest->period_sys += src->period_sys;
  203. dest->period_us += src->period_us;
  204. dest->period_guest_sys += src->period_guest_sys;
  205. dest->period_guest_us += src->period_guest_us;
  206. dest->nr_events += src->nr_events;
  207. dest->weight += src->weight;
  208. }
  209. static void he_stat__decay(struct he_stat *he_stat)
  210. {
  211. he_stat->period = (he_stat->period * 7) / 8;
  212. he_stat->nr_events = (he_stat->nr_events * 7) / 8;
  213. /* XXX need decay for weight too? */
  214. }
  215. static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
  216. {
  217. u64 prev_period = he->stat.period;
  218. u64 diff;
  219. if (prev_period == 0)
  220. return true;
  221. he_stat__decay(&he->stat);
  222. if (symbol_conf.cumulate_callchain)
  223. he_stat__decay(he->stat_acc);
  224. decay_callchain(he->callchain);
  225. diff = prev_period - he->stat.period;
  226. hists->stats.total_period -= diff;
  227. if (!he->filtered)
  228. hists->stats.total_non_filtered_period -= diff;
  229. return he->stat.period == 0;
  230. }
  231. static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
  232. {
  233. rb_erase(&he->rb_node, &hists->entries);
  234. if (sort__need_collapse)
  235. rb_erase(&he->rb_node_in, &hists->entries_collapsed);
  236. else
  237. rb_erase(&he->rb_node_in, hists->entries_in);
  238. --hists->nr_entries;
  239. if (!he->filtered)
  240. --hists->nr_non_filtered_entries;
  241. hist_entry__delete(he);
  242. }
  243. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  244. {
  245. struct rb_node *next = rb_first(&hists->entries);
  246. struct hist_entry *n;
  247. while (next) {
  248. n = rb_entry(next, struct hist_entry, rb_node);
  249. next = rb_next(&n->rb_node);
  250. if (((zap_user && n->level == '.') ||
  251. (zap_kernel && n->level != '.') ||
  252. hists__decay_entry(hists, n))) {
  253. hists__delete_entry(hists, n);
  254. }
  255. }
  256. }
  257. void hists__delete_entries(struct hists *hists)
  258. {
  259. struct rb_node *next = rb_first(&hists->entries);
  260. struct hist_entry *n;
  261. while (next) {
  262. n = rb_entry(next, struct hist_entry, rb_node);
  263. next = rb_next(&n->rb_node);
  264. hists__delete_entry(hists, n);
  265. }
  266. }
  267. /*
  268. * histogram, sorted on item, collects periods
  269. */
  270. static struct hist_entry *hist_entry__new(struct hist_entry *template,
  271. bool sample_self)
  272. {
  273. size_t callchain_size = 0;
  274. struct hist_entry *he;
  275. if (symbol_conf.use_callchain)
  276. callchain_size = sizeof(struct callchain_root);
  277. he = zalloc(sizeof(*he) + callchain_size);
  278. if (he != NULL) {
  279. *he = *template;
  280. if (symbol_conf.cumulate_callchain) {
  281. he->stat_acc = malloc(sizeof(he->stat));
  282. if (he->stat_acc == NULL) {
  283. free(he);
  284. return NULL;
  285. }
  286. memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
  287. if (!sample_self)
  288. memset(&he->stat, 0, sizeof(he->stat));
  289. }
  290. map__get(he->ms.map);
  291. if (he->branch_info) {
  292. /*
  293. * This branch info is (a part of) allocated from
  294. * sample__resolve_bstack() and will be freed after
  295. * adding new entries. So we need to save a copy.
  296. */
  297. he->branch_info = malloc(sizeof(*he->branch_info));
  298. if (he->branch_info == NULL) {
  299. map__zput(he->ms.map);
  300. free(he->stat_acc);
  301. free(he);
  302. return NULL;
  303. }
  304. memcpy(he->branch_info, template->branch_info,
  305. sizeof(*he->branch_info));
  306. map__get(he->branch_info->from.map);
  307. map__get(he->branch_info->to.map);
  308. }
  309. if (he->mem_info) {
  310. map__get(he->mem_info->iaddr.map);
  311. map__get(he->mem_info->daddr.map);
  312. }
  313. if (symbol_conf.use_callchain)
  314. callchain_init(he->callchain);
  315. if (he->raw_data) {
  316. he->raw_data = memdup(he->raw_data, he->raw_size);
  317. if (he->raw_data == NULL) {
  318. map__put(he->ms.map);
  319. if (he->branch_info) {
  320. map__put(he->branch_info->from.map);
  321. map__put(he->branch_info->to.map);
  322. free(he->branch_info);
  323. }
  324. if (he->mem_info) {
  325. map__put(he->mem_info->iaddr.map);
  326. map__put(he->mem_info->daddr.map);
  327. }
  328. free(he->stat_acc);
  329. free(he);
  330. return NULL;
  331. }
  332. }
  333. INIT_LIST_HEAD(&he->pairs.node);
  334. thread__get(he->thread);
  335. }
  336. return he;
  337. }
  338. static u8 symbol__parent_filter(const struct symbol *parent)
  339. {
  340. if (symbol_conf.exclude_other && parent == NULL)
  341. return 1 << HIST_FILTER__PARENT;
  342. return 0;
  343. }
  344. static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
  345. {
  346. if (!symbol_conf.use_callchain)
  347. return;
  348. he->hists->callchain_period += period;
  349. if (!he->filtered)
  350. he->hists->callchain_non_filtered_period += period;
  351. }
  352. static struct hist_entry *hists__findnew_entry(struct hists *hists,
  353. struct hist_entry *entry,
  354. struct addr_location *al,
  355. bool sample_self)
  356. {
  357. struct rb_node **p;
  358. struct rb_node *parent = NULL;
  359. struct hist_entry *he;
  360. int64_t cmp;
  361. u64 period = entry->stat.period;
  362. u64 weight = entry->stat.weight;
  363. p = &hists->entries_in->rb_node;
  364. while (*p != NULL) {
  365. parent = *p;
  366. he = rb_entry(parent, struct hist_entry, rb_node_in);
  367. /*
  368. * Make sure that it receives arguments in a same order as
  369. * hist_entry__collapse() so that we can use an appropriate
  370. * function when searching an entry regardless which sort
  371. * keys were used.
  372. */
  373. cmp = hist_entry__cmp(he, entry);
  374. if (!cmp) {
  375. if (sample_self) {
  376. he_stat__add_period(&he->stat, period, weight);
  377. hist_entry__add_callchain_period(he, period);
  378. }
  379. if (symbol_conf.cumulate_callchain)
  380. he_stat__add_period(he->stat_acc, period, weight);
  381. /*
  382. * This mem info was allocated from sample__resolve_mem
  383. * and will not be used anymore.
  384. */
  385. zfree(&entry->mem_info);
  386. /* If the map of an existing hist_entry has
  387. * become out-of-date due to an exec() or
  388. * similar, update it. Otherwise we will
  389. * mis-adjust symbol addresses when computing
  390. * the history counter to increment.
  391. */
  392. if (he->ms.map != entry->ms.map) {
  393. map__put(he->ms.map);
  394. he->ms.map = map__get(entry->ms.map);
  395. }
  396. goto out;
  397. }
  398. if (cmp < 0)
  399. p = &(*p)->rb_left;
  400. else
  401. p = &(*p)->rb_right;
  402. }
  403. he = hist_entry__new(entry, sample_self);
  404. if (!he)
  405. return NULL;
  406. if (sample_self)
  407. hist_entry__add_callchain_period(he, period);
  408. hists->nr_entries++;
  409. rb_link_node(&he->rb_node_in, parent, p);
  410. rb_insert_color(&he->rb_node_in, hists->entries_in);
  411. out:
  412. if (sample_self)
  413. he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
  414. if (symbol_conf.cumulate_callchain)
  415. he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
  416. return he;
  417. }
  418. struct hist_entry *__hists__add_entry(struct hists *hists,
  419. struct addr_location *al,
  420. struct symbol *sym_parent,
  421. struct branch_info *bi,
  422. struct mem_info *mi,
  423. struct perf_sample *sample,
  424. bool sample_self)
  425. {
  426. struct hist_entry entry = {
  427. .thread = al->thread,
  428. .comm = thread__comm(al->thread),
  429. .ms = {
  430. .map = al->map,
  431. .sym = al->sym,
  432. },
  433. .socket = al->socket,
  434. .cpu = al->cpu,
  435. .cpumode = al->cpumode,
  436. .ip = al->addr,
  437. .level = al->level,
  438. .stat = {
  439. .nr_events = 1,
  440. .period = sample->period,
  441. .weight = sample->weight,
  442. },
  443. .parent = sym_parent,
  444. .filtered = symbol__parent_filter(sym_parent) | al->filtered,
  445. .hists = hists,
  446. .branch_info = bi,
  447. .mem_info = mi,
  448. .transaction = sample->transaction,
  449. .raw_data = sample->raw_data,
  450. .raw_size = sample->raw_size,
  451. };
  452. return hists__findnew_entry(hists, &entry, al, sample_self);
  453. }
  454. static int
  455. iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  456. struct addr_location *al __maybe_unused)
  457. {
  458. return 0;
  459. }
  460. static int
  461. iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
  462. struct addr_location *al __maybe_unused)
  463. {
  464. return 0;
  465. }
  466. static int
  467. iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  468. {
  469. struct perf_sample *sample = iter->sample;
  470. struct mem_info *mi;
  471. mi = sample__resolve_mem(sample, al);
  472. if (mi == NULL)
  473. return -ENOMEM;
  474. iter->priv = mi;
  475. return 0;
  476. }
  477. static int
  478. iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
  479. {
  480. u64 cost;
  481. struct mem_info *mi = iter->priv;
  482. struct hists *hists = evsel__hists(iter->evsel);
  483. struct perf_sample *sample = iter->sample;
  484. struct hist_entry *he;
  485. if (mi == NULL)
  486. return -EINVAL;
  487. cost = sample->weight;
  488. if (!cost)
  489. cost = 1;
  490. /*
  491. * must pass period=weight in order to get the correct
  492. * sorting from hists__collapse_resort() which is solely
  493. * based on periods. We want sorting be done on nr_events * weight
  494. * and this is indirectly achieved by passing period=weight here
  495. * and the he_stat__add_period() function.
  496. */
  497. sample->period = cost;
  498. he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
  499. sample, true);
  500. if (!he)
  501. return -ENOMEM;
  502. iter->he = he;
  503. return 0;
  504. }
  505. static int
  506. iter_finish_mem_entry(struct hist_entry_iter *iter,
  507. struct addr_location *al __maybe_unused)
  508. {
  509. struct perf_evsel *evsel = iter->evsel;
  510. struct hists *hists = evsel__hists(evsel);
  511. struct hist_entry *he = iter->he;
  512. int err = -EINVAL;
  513. if (he == NULL)
  514. goto out;
  515. hists__inc_nr_samples(hists, he->filtered);
  516. err = hist_entry__append_callchain(he, iter->sample);
  517. out:
  518. /*
  519. * We don't need to free iter->priv (mem_info) here since the mem info
  520. * was either already freed in hists__findnew_entry() or passed to a
  521. * new hist entry by hist_entry__new().
  522. */
  523. iter->priv = NULL;
  524. iter->he = NULL;
  525. return err;
  526. }
  527. static int
  528. iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  529. {
  530. struct branch_info *bi;
  531. struct perf_sample *sample = iter->sample;
  532. bi = sample__resolve_bstack(sample, al);
  533. if (!bi)
  534. return -ENOMEM;
  535. iter->curr = 0;
  536. iter->total = sample->branch_stack->nr;
  537. iter->priv = bi;
  538. return 0;
  539. }
  540. static int
  541. iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
  542. struct addr_location *al __maybe_unused)
  543. {
  544. /* to avoid calling callback function */
  545. iter->he = NULL;
  546. return 0;
  547. }
  548. static int
  549. iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  550. {
  551. struct branch_info *bi = iter->priv;
  552. int i = iter->curr;
  553. if (bi == NULL)
  554. return 0;
  555. if (iter->curr >= iter->total)
  556. return 0;
  557. al->map = bi[i].to.map;
  558. al->sym = bi[i].to.sym;
  559. al->addr = bi[i].to.addr;
  560. return 1;
  561. }
  562. static int
  563. iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
  564. {
  565. struct branch_info *bi;
  566. struct perf_evsel *evsel = iter->evsel;
  567. struct hists *hists = evsel__hists(evsel);
  568. struct perf_sample *sample = iter->sample;
  569. struct hist_entry *he = NULL;
  570. int i = iter->curr;
  571. int err = 0;
  572. bi = iter->priv;
  573. if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
  574. goto out;
  575. /*
  576. * The report shows the percentage of total branches captured
  577. * and not events sampled. Thus we use a pseudo period of 1.
  578. */
  579. sample->period = 1;
  580. sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
  581. he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
  582. sample, true);
  583. if (he == NULL)
  584. return -ENOMEM;
  585. hists__inc_nr_samples(hists, he->filtered);
  586. out:
  587. iter->he = he;
  588. iter->curr++;
  589. return err;
  590. }
  591. static int
  592. iter_finish_branch_entry(struct hist_entry_iter *iter,
  593. struct addr_location *al __maybe_unused)
  594. {
  595. zfree(&iter->priv);
  596. iter->he = NULL;
  597. return iter->curr >= iter->total ? 0 : -1;
  598. }
  599. static int
  600. iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
  601. struct addr_location *al __maybe_unused)
  602. {
  603. return 0;
  604. }
  605. static int
  606. iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
  607. {
  608. struct perf_evsel *evsel = iter->evsel;
  609. struct perf_sample *sample = iter->sample;
  610. struct hist_entry *he;
  611. he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
  612. sample, true);
  613. if (he == NULL)
  614. return -ENOMEM;
  615. iter->he = he;
  616. return 0;
  617. }
  618. static int
  619. iter_finish_normal_entry(struct hist_entry_iter *iter,
  620. struct addr_location *al __maybe_unused)
  621. {
  622. struct hist_entry *he = iter->he;
  623. struct perf_evsel *evsel = iter->evsel;
  624. struct perf_sample *sample = iter->sample;
  625. if (he == NULL)
  626. return 0;
  627. iter->he = NULL;
  628. hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
  629. return hist_entry__append_callchain(he, sample);
  630. }
  631. static int
  632. iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
  633. struct addr_location *al __maybe_unused)
  634. {
  635. struct hist_entry **he_cache;
  636. callchain_cursor_commit(&callchain_cursor);
  637. /*
  638. * This is for detecting cycles or recursions so that they're
  639. * cumulated only one time to prevent entries more than 100%
  640. * overhead.
  641. */
  642. he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
  643. if (he_cache == NULL)
  644. return -ENOMEM;
  645. iter->priv = he_cache;
  646. iter->curr = 0;
  647. return 0;
  648. }
  649. static int
  650. iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
  651. struct addr_location *al)
  652. {
  653. struct perf_evsel *evsel = iter->evsel;
  654. struct hists *hists = evsel__hists(evsel);
  655. struct perf_sample *sample = iter->sample;
  656. struct hist_entry **he_cache = iter->priv;
  657. struct hist_entry *he;
  658. int err = 0;
  659. he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
  660. sample, true);
  661. if (he == NULL)
  662. return -ENOMEM;
  663. iter->he = he;
  664. he_cache[iter->curr++] = he;
  665. hist_entry__append_callchain(he, sample);
  666. /*
  667. * We need to re-initialize the cursor since callchain_append()
  668. * advanced the cursor to the end.
  669. */
  670. callchain_cursor_commit(&callchain_cursor);
  671. hists__inc_nr_samples(hists, he->filtered);
  672. return err;
  673. }
  674. static int
  675. iter_next_cumulative_entry(struct hist_entry_iter *iter,
  676. struct addr_location *al)
  677. {
  678. struct callchain_cursor_node *node;
  679. node = callchain_cursor_current(&callchain_cursor);
  680. if (node == NULL)
  681. return 0;
  682. return fill_callchain_info(al, node, iter->hide_unresolved);
  683. }
  684. static int
  685. iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
  686. struct addr_location *al)
  687. {
  688. struct perf_evsel *evsel = iter->evsel;
  689. struct perf_sample *sample = iter->sample;
  690. struct hist_entry **he_cache = iter->priv;
  691. struct hist_entry *he;
  692. struct hist_entry he_tmp = {
  693. .hists = evsel__hists(evsel),
  694. .cpu = al->cpu,
  695. .thread = al->thread,
  696. .comm = thread__comm(al->thread),
  697. .ip = al->addr,
  698. .ms = {
  699. .map = al->map,
  700. .sym = al->sym,
  701. },
  702. .parent = iter->parent,
  703. .raw_data = sample->raw_data,
  704. .raw_size = sample->raw_size,
  705. };
  706. int i;
  707. struct callchain_cursor cursor;
  708. callchain_cursor_snapshot(&cursor, &callchain_cursor);
  709. callchain_cursor_advance(&callchain_cursor);
  710. /*
  711. * Check if there's duplicate entries in the callchain.
  712. * It's possible that it has cycles or recursive calls.
  713. */
  714. for (i = 0; i < iter->curr; i++) {
  715. if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
  716. /* to avoid calling callback function */
  717. iter->he = NULL;
  718. return 0;
  719. }
  720. }
  721. he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
  722. sample, false);
  723. if (he == NULL)
  724. return -ENOMEM;
  725. iter->he = he;
  726. he_cache[iter->curr++] = he;
  727. if (symbol_conf.use_callchain)
  728. callchain_append(he->callchain, &cursor, sample->period);
  729. return 0;
  730. }
  731. static int
  732. iter_finish_cumulative_entry(struct hist_entry_iter *iter,
  733. struct addr_location *al __maybe_unused)
  734. {
  735. zfree(&iter->priv);
  736. iter->he = NULL;
  737. return 0;
  738. }
  739. const struct hist_iter_ops hist_iter_mem = {
  740. .prepare_entry = iter_prepare_mem_entry,
  741. .add_single_entry = iter_add_single_mem_entry,
  742. .next_entry = iter_next_nop_entry,
  743. .add_next_entry = iter_add_next_nop_entry,
  744. .finish_entry = iter_finish_mem_entry,
  745. };
  746. const struct hist_iter_ops hist_iter_branch = {
  747. .prepare_entry = iter_prepare_branch_entry,
  748. .add_single_entry = iter_add_single_branch_entry,
  749. .next_entry = iter_next_branch_entry,
  750. .add_next_entry = iter_add_next_branch_entry,
  751. .finish_entry = iter_finish_branch_entry,
  752. };
  753. const struct hist_iter_ops hist_iter_normal = {
  754. .prepare_entry = iter_prepare_normal_entry,
  755. .add_single_entry = iter_add_single_normal_entry,
  756. .next_entry = iter_next_nop_entry,
  757. .add_next_entry = iter_add_next_nop_entry,
  758. .finish_entry = iter_finish_normal_entry,
  759. };
  760. const struct hist_iter_ops hist_iter_cumulative = {
  761. .prepare_entry = iter_prepare_cumulative_entry,
  762. .add_single_entry = iter_add_single_cumulative_entry,
  763. .next_entry = iter_next_cumulative_entry,
  764. .add_next_entry = iter_add_next_cumulative_entry,
  765. .finish_entry = iter_finish_cumulative_entry,
  766. };
  767. int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
  768. int max_stack_depth, void *arg)
  769. {
  770. int err, err2;
  771. err = sample__resolve_callchain(iter->sample, &iter->parent,
  772. iter->evsel, al, max_stack_depth);
  773. if (err)
  774. return err;
  775. iter->max_stack = max_stack_depth;
  776. err = iter->ops->prepare_entry(iter, al);
  777. if (err)
  778. goto out;
  779. err = iter->ops->add_single_entry(iter, al);
  780. if (err)
  781. goto out;
  782. if (iter->he && iter->add_entry_cb) {
  783. err = iter->add_entry_cb(iter, al, true, arg);
  784. if (err)
  785. goto out;
  786. }
  787. while (iter->ops->next_entry(iter, al)) {
  788. err = iter->ops->add_next_entry(iter, al);
  789. if (err)
  790. break;
  791. if (iter->he && iter->add_entry_cb) {
  792. err = iter->add_entry_cb(iter, al, false, arg);
  793. if (err)
  794. goto out;
  795. }
  796. }
  797. out:
  798. err2 = iter->ops->finish_entry(iter, al);
  799. if (!err)
  800. err = err2;
  801. return err;
  802. }
  803. int64_t
  804. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  805. {
  806. struct hists *hists = left->hists;
  807. struct perf_hpp_fmt *fmt;
  808. int64_t cmp = 0;
  809. hists__for_each_sort_list(hists, fmt) {
  810. cmp = fmt->cmp(fmt, left, right);
  811. if (cmp)
  812. break;
  813. }
  814. return cmp;
  815. }
  816. int64_t
  817. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  818. {
  819. struct hists *hists = left->hists;
  820. struct perf_hpp_fmt *fmt;
  821. int64_t cmp = 0;
  822. hists__for_each_sort_list(hists, fmt) {
  823. cmp = fmt->collapse(fmt, left, right);
  824. if (cmp)
  825. break;
  826. }
  827. return cmp;
  828. }
  829. void hist_entry__delete(struct hist_entry *he)
  830. {
  831. thread__zput(he->thread);
  832. map__zput(he->ms.map);
  833. if (he->branch_info) {
  834. map__zput(he->branch_info->from.map);
  835. map__zput(he->branch_info->to.map);
  836. zfree(&he->branch_info);
  837. }
  838. if (he->mem_info) {
  839. map__zput(he->mem_info->iaddr.map);
  840. map__zput(he->mem_info->daddr.map);
  841. zfree(&he->mem_info);
  842. }
  843. zfree(&he->stat_acc);
  844. free_srcline(he->srcline);
  845. if (he->srcfile && he->srcfile[0])
  846. free(he->srcfile);
  847. free_callchain(he->callchain);
  848. free(he->trace_output);
  849. free(he->raw_data);
  850. free(he);
  851. }
  852. /*
  853. * If this is not the last column, then we need to pad it according to the
  854. * pre-calculated max lenght for this column, otherwise don't bother adding
  855. * spaces because that would break viewing this with, for instance, 'less',
  856. * that would show tons of trailing spaces when a long C++ demangled method
  857. * names is sampled.
  858. */
  859. int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
  860. struct perf_hpp_fmt *fmt, int printed)
  861. {
  862. if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
  863. const int width = fmt->width(fmt, hpp, hists_to_evsel(he->hists));
  864. if (printed < width) {
  865. advance_hpp(hpp, printed);
  866. printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
  867. }
  868. }
  869. return printed;
  870. }
  871. /*
  872. * collapse the histogram
  873. */
  874. bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
  875. struct rb_root *root, struct hist_entry *he)
  876. {
  877. struct rb_node **p = &root->rb_node;
  878. struct rb_node *parent = NULL;
  879. struct hist_entry *iter;
  880. int64_t cmp;
  881. while (*p != NULL) {
  882. parent = *p;
  883. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  884. cmp = hist_entry__collapse(iter, he);
  885. if (!cmp) {
  886. he_stat__add_stat(&iter->stat, &he->stat);
  887. if (symbol_conf.cumulate_callchain)
  888. he_stat__add_stat(iter->stat_acc, he->stat_acc);
  889. if (symbol_conf.use_callchain) {
  890. callchain_cursor_reset(&callchain_cursor);
  891. callchain_merge(&callchain_cursor,
  892. iter->callchain,
  893. he->callchain);
  894. }
  895. hist_entry__delete(he);
  896. return false;
  897. }
  898. if (cmp < 0)
  899. p = &(*p)->rb_left;
  900. else
  901. p = &(*p)->rb_right;
  902. }
  903. hists->nr_entries++;
  904. rb_link_node(&he->rb_node_in, parent, p);
  905. rb_insert_color(&he->rb_node_in, root);
  906. return true;
  907. }
  908. struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  909. {
  910. struct rb_root *root;
  911. pthread_mutex_lock(&hists->lock);
  912. root = hists->entries_in;
  913. if (++hists->entries_in > &hists->entries_in_array[1])
  914. hists->entries_in = &hists->entries_in_array[0];
  915. pthread_mutex_unlock(&hists->lock);
  916. return root;
  917. }
  918. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  919. {
  920. hists__filter_entry_by_dso(hists, he);
  921. hists__filter_entry_by_thread(hists, he);
  922. hists__filter_entry_by_symbol(hists, he);
  923. hists__filter_entry_by_socket(hists, he);
  924. }
  925. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
  926. {
  927. struct rb_root *root;
  928. struct rb_node *next;
  929. struct hist_entry *n;
  930. if (!sort__need_collapse)
  931. return;
  932. hists->nr_entries = 0;
  933. root = hists__get_rotate_entries_in(hists);
  934. next = rb_first(root);
  935. while (next) {
  936. if (session_done())
  937. break;
  938. n = rb_entry(next, struct hist_entry, rb_node_in);
  939. next = rb_next(&n->rb_node_in);
  940. rb_erase(&n->rb_node_in, root);
  941. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  942. /*
  943. * If it wasn't combined with one of the entries already
  944. * collapsed, we need to apply the filters that may have
  945. * been set by, say, the hist_browser.
  946. */
  947. hists__apply_filters(hists, n);
  948. }
  949. if (prog)
  950. ui_progress__update(prog, 1);
  951. }
  952. }
  953. static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
  954. {
  955. struct hists *hists = a->hists;
  956. struct perf_hpp_fmt *fmt;
  957. int64_t cmp = 0;
  958. hists__for_each_sort_list(hists, fmt) {
  959. if (perf_hpp__should_skip(fmt, a->hists))
  960. continue;
  961. cmp = fmt->sort(fmt, a, b);
  962. if (cmp)
  963. break;
  964. }
  965. return cmp;
  966. }
  967. static void hists__reset_filter_stats(struct hists *hists)
  968. {
  969. hists->nr_non_filtered_entries = 0;
  970. hists->stats.total_non_filtered_period = 0;
  971. }
  972. void hists__reset_stats(struct hists *hists)
  973. {
  974. hists->nr_entries = 0;
  975. hists->stats.total_period = 0;
  976. hists__reset_filter_stats(hists);
  977. }
  978. static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
  979. {
  980. hists->nr_non_filtered_entries++;
  981. hists->stats.total_non_filtered_period += h->stat.period;
  982. }
  983. void hists__inc_stats(struct hists *hists, struct hist_entry *h)
  984. {
  985. if (!h->filtered)
  986. hists__inc_filter_stats(hists, h);
  987. hists->nr_entries++;
  988. hists->stats.total_period += h->stat.period;
  989. }
  990. static void __hists__insert_output_entry(struct rb_root *entries,
  991. struct hist_entry *he,
  992. u64 min_callchain_hits,
  993. bool use_callchain)
  994. {
  995. struct rb_node **p = &entries->rb_node;
  996. struct rb_node *parent = NULL;
  997. struct hist_entry *iter;
  998. if (use_callchain) {
  999. if (callchain_param.mode == CHAIN_GRAPH_REL) {
  1000. u64 total = he->stat.period;
  1001. if (symbol_conf.cumulate_callchain)
  1002. total = he->stat_acc->period;
  1003. min_callchain_hits = total * (callchain_param.min_percent / 100);
  1004. }
  1005. callchain_param.sort(&he->sorted_chain, he->callchain,
  1006. min_callchain_hits, &callchain_param);
  1007. }
  1008. while (*p != NULL) {
  1009. parent = *p;
  1010. iter = rb_entry(parent, struct hist_entry, rb_node);
  1011. if (hist_entry__sort(he, iter) > 0)
  1012. p = &(*p)->rb_left;
  1013. else
  1014. p = &(*p)->rb_right;
  1015. }
  1016. rb_link_node(&he->rb_node, parent, p);
  1017. rb_insert_color(&he->rb_node, entries);
  1018. }
  1019. static void output_resort(struct hists *hists, struct ui_progress *prog,
  1020. bool use_callchain)
  1021. {
  1022. struct rb_root *root;
  1023. struct rb_node *next;
  1024. struct hist_entry *n;
  1025. u64 callchain_total;
  1026. u64 min_callchain_hits;
  1027. callchain_total = hists->callchain_period;
  1028. if (symbol_conf.filter_relative)
  1029. callchain_total = hists->callchain_non_filtered_period;
  1030. min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
  1031. if (sort__need_collapse)
  1032. root = &hists->entries_collapsed;
  1033. else
  1034. root = hists->entries_in;
  1035. next = rb_first(root);
  1036. hists->entries = RB_ROOT;
  1037. hists__reset_stats(hists);
  1038. hists__reset_col_len(hists);
  1039. while (next) {
  1040. n = rb_entry(next, struct hist_entry, rb_node_in);
  1041. next = rb_next(&n->rb_node_in);
  1042. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
  1043. hists__inc_stats(hists, n);
  1044. if (!n->filtered)
  1045. hists__calc_col_len(hists, n);
  1046. if (prog)
  1047. ui_progress__update(prog, 1);
  1048. }
  1049. }
  1050. void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
  1051. {
  1052. bool use_callchain;
  1053. if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
  1054. use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
  1055. else
  1056. use_callchain = symbol_conf.use_callchain;
  1057. output_resort(evsel__hists(evsel), prog, use_callchain);
  1058. }
  1059. void hists__output_resort(struct hists *hists, struct ui_progress *prog)
  1060. {
  1061. output_resort(hists, prog, symbol_conf.use_callchain);
  1062. }
  1063. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  1064. enum hist_filter filter)
  1065. {
  1066. h->filtered &= ~(1 << filter);
  1067. if (h->filtered)
  1068. return;
  1069. /* force fold unfiltered entry for simplicity */
  1070. h->unfolded = false;
  1071. h->row_offset = 0;
  1072. h->nr_rows = 0;
  1073. hists->stats.nr_non_filtered_samples += h->stat.nr_events;
  1074. hists__inc_filter_stats(hists, h);
  1075. hists__calc_col_len(hists, h);
  1076. }
  1077. static bool hists__filter_entry_by_dso(struct hists *hists,
  1078. struct hist_entry *he)
  1079. {
  1080. if (hists->dso_filter != NULL &&
  1081. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  1082. he->filtered |= (1 << HIST_FILTER__DSO);
  1083. return true;
  1084. }
  1085. return false;
  1086. }
  1087. static bool hists__filter_entry_by_thread(struct hists *hists,
  1088. struct hist_entry *he)
  1089. {
  1090. if (hists->thread_filter != NULL &&
  1091. he->thread != hists->thread_filter) {
  1092. he->filtered |= (1 << HIST_FILTER__THREAD);
  1093. return true;
  1094. }
  1095. return false;
  1096. }
  1097. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1098. struct hist_entry *he)
  1099. {
  1100. if (hists->symbol_filter_str != NULL &&
  1101. (!he->ms.sym || strstr(he->ms.sym->name,
  1102. hists->symbol_filter_str) == NULL)) {
  1103. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1104. return true;
  1105. }
  1106. return false;
  1107. }
  1108. static bool hists__filter_entry_by_socket(struct hists *hists,
  1109. struct hist_entry *he)
  1110. {
  1111. if ((hists->socket_filter > -1) &&
  1112. (he->socket != hists->socket_filter)) {
  1113. he->filtered |= (1 << HIST_FILTER__SOCKET);
  1114. return true;
  1115. }
  1116. return false;
  1117. }
  1118. typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
  1119. static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
  1120. {
  1121. struct rb_node *nd;
  1122. hists->stats.nr_non_filtered_samples = 0;
  1123. hists__reset_filter_stats(hists);
  1124. hists__reset_col_len(hists);
  1125. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1126. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1127. if (filter(hists, h))
  1128. continue;
  1129. hists__remove_entry_filter(hists, h, type);
  1130. }
  1131. }
  1132. void hists__filter_by_thread(struct hists *hists)
  1133. {
  1134. hists__filter_by_type(hists, HIST_FILTER__THREAD,
  1135. hists__filter_entry_by_thread);
  1136. }
  1137. void hists__filter_by_dso(struct hists *hists)
  1138. {
  1139. hists__filter_by_type(hists, HIST_FILTER__DSO,
  1140. hists__filter_entry_by_dso);
  1141. }
  1142. void hists__filter_by_symbol(struct hists *hists)
  1143. {
  1144. hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
  1145. hists__filter_entry_by_symbol);
  1146. }
  1147. void hists__filter_by_socket(struct hists *hists)
  1148. {
  1149. hists__filter_by_type(hists, HIST_FILTER__SOCKET,
  1150. hists__filter_entry_by_socket);
  1151. }
  1152. void events_stats__inc(struct events_stats *stats, u32 type)
  1153. {
  1154. ++stats->nr_events[0];
  1155. ++stats->nr_events[type];
  1156. }
  1157. void hists__inc_nr_events(struct hists *hists, u32 type)
  1158. {
  1159. events_stats__inc(&hists->stats, type);
  1160. }
  1161. void hists__inc_nr_samples(struct hists *hists, bool filtered)
  1162. {
  1163. events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
  1164. if (!filtered)
  1165. hists->stats.nr_non_filtered_samples++;
  1166. }
  1167. static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
  1168. struct hist_entry *pair)
  1169. {
  1170. struct rb_root *root;
  1171. struct rb_node **p;
  1172. struct rb_node *parent = NULL;
  1173. struct hist_entry *he;
  1174. int64_t cmp;
  1175. if (sort__need_collapse)
  1176. root = &hists->entries_collapsed;
  1177. else
  1178. root = hists->entries_in;
  1179. p = &root->rb_node;
  1180. while (*p != NULL) {
  1181. parent = *p;
  1182. he = rb_entry(parent, struct hist_entry, rb_node_in);
  1183. cmp = hist_entry__collapse(he, pair);
  1184. if (!cmp)
  1185. goto out;
  1186. if (cmp < 0)
  1187. p = &(*p)->rb_left;
  1188. else
  1189. p = &(*p)->rb_right;
  1190. }
  1191. he = hist_entry__new(pair, true);
  1192. if (he) {
  1193. memset(&he->stat, 0, sizeof(he->stat));
  1194. he->hists = hists;
  1195. rb_link_node(&he->rb_node_in, parent, p);
  1196. rb_insert_color(&he->rb_node_in, root);
  1197. hists__inc_stats(hists, he);
  1198. he->dummy = true;
  1199. }
  1200. out:
  1201. return he;
  1202. }
  1203. static struct hist_entry *hists__find_entry(struct hists *hists,
  1204. struct hist_entry *he)
  1205. {
  1206. struct rb_node *n;
  1207. if (sort__need_collapse)
  1208. n = hists->entries_collapsed.rb_node;
  1209. else
  1210. n = hists->entries_in->rb_node;
  1211. while (n) {
  1212. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
  1213. int64_t cmp = hist_entry__collapse(iter, he);
  1214. if (cmp < 0)
  1215. n = n->rb_left;
  1216. else if (cmp > 0)
  1217. n = n->rb_right;
  1218. else
  1219. return iter;
  1220. }
  1221. return NULL;
  1222. }
  1223. /*
  1224. * Look for pairs to link to the leader buckets (hist_entries):
  1225. */
  1226. void hists__match(struct hists *leader, struct hists *other)
  1227. {
  1228. struct rb_root *root;
  1229. struct rb_node *nd;
  1230. struct hist_entry *pos, *pair;
  1231. if (sort__need_collapse)
  1232. root = &leader->entries_collapsed;
  1233. else
  1234. root = leader->entries_in;
  1235. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1236. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1237. pair = hists__find_entry(other, pos);
  1238. if (pair)
  1239. hist_entry__add_pair(pair, pos);
  1240. }
  1241. }
  1242. /*
  1243. * Look for entries in the other hists that are not present in the leader, if
  1244. * we find them, just add a dummy entry on the leader hists, with period=0,
  1245. * nr_events=0, to serve as the list header.
  1246. */
  1247. int hists__link(struct hists *leader, struct hists *other)
  1248. {
  1249. struct rb_root *root;
  1250. struct rb_node *nd;
  1251. struct hist_entry *pos, *pair;
  1252. if (sort__need_collapse)
  1253. root = &other->entries_collapsed;
  1254. else
  1255. root = other->entries_in;
  1256. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  1257. pos = rb_entry(nd, struct hist_entry, rb_node_in);
  1258. if (!hist_entry__has_pairs(pos)) {
  1259. pair = hists__add_dummy_entry(leader, pos);
  1260. if (pair == NULL)
  1261. return -1;
  1262. hist_entry__add_pair(pos, pair);
  1263. }
  1264. }
  1265. return 0;
  1266. }
  1267. void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
  1268. struct perf_sample *sample, bool nonany_branch_mode)
  1269. {
  1270. struct branch_info *bi;
  1271. /* If we have branch cycles always annotate them. */
  1272. if (bs && bs->nr && bs->entries[0].flags.cycles) {
  1273. int i;
  1274. bi = sample__resolve_bstack(sample, al);
  1275. if (bi) {
  1276. struct addr_map_symbol *prev = NULL;
  1277. /*
  1278. * Ignore errors, still want to process the
  1279. * other entries.
  1280. *
  1281. * For non standard branch modes always
  1282. * force no IPC (prev == NULL)
  1283. *
  1284. * Note that perf stores branches reversed from
  1285. * program order!
  1286. */
  1287. for (i = bs->nr - 1; i >= 0; i--) {
  1288. addr_map_symbol__account_cycles(&bi[i].from,
  1289. nonany_branch_mode ? NULL : prev,
  1290. bi[i].flags.cycles);
  1291. prev = &bi[i].to;
  1292. }
  1293. free(bi);
  1294. }
  1295. }
  1296. }
  1297. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
  1298. {
  1299. struct perf_evsel *pos;
  1300. size_t ret = 0;
  1301. evlist__for_each(evlist, pos) {
  1302. ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
  1303. ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
  1304. }
  1305. return ret;
  1306. }
  1307. u64 hists__total_period(struct hists *hists)
  1308. {
  1309. return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
  1310. hists->stats.total_period;
  1311. }
  1312. int parse_filter_percentage(const struct option *opt __maybe_unused,
  1313. const char *arg, int unset __maybe_unused)
  1314. {
  1315. if (!strcmp(arg, "relative"))
  1316. symbol_conf.filter_relative = true;
  1317. else if (!strcmp(arg, "absolute"))
  1318. symbol_conf.filter_relative = false;
  1319. else
  1320. return -1;
  1321. return 0;
  1322. }
  1323. int perf_hist_config(const char *var, const char *value)
  1324. {
  1325. if (!strcmp(var, "hist.percentage"))
  1326. return parse_filter_percentage(NULL, value, 0);
  1327. return 0;
  1328. }
  1329. int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
  1330. {
  1331. memset(hists, 0, sizeof(*hists));
  1332. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  1333. hists->entries_in = &hists->entries_in_array[0];
  1334. hists->entries_collapsed = RB_ROOT;
  1335. hists->entries = RB_ROOT;
  1336. pthread_mutex_init(&hists->lock, NULL);
  1337. hists->socket_filter = -1;
  1338. hists->hpp_list = hpp_list;
  1339. return 0;
  1340. }
  1341. static void hists__delete_remaining_entries(struct rb_root *root)
  1342. {
  1343. struct rb_node *node;
  1344. struct hist_entry *he;
  1345. while (!RB_EMPTY_ROOT(root)) {
  1346. node = rb_first(root);
  1347. rb_erase(node, root);
  1348. he = rb_entry(node, struct hist_entry, rb_node_in);
  1349. hist_entry__delete(he);
  1350. }
  1351. }
  1352. static void hists__delete_all_entries(struct hists *hists)
  1353. {
  1354. hists__delete_entries(hists);
  1355. hists__delete_remaining_entries(&hists->entries_in_array[0]);
  1356. hists__delete_remaining_entries(&hists->entries_in_array[1]);
  1357. hists__delete_remaining_entries(&hists->entries_collapsed);
  1358. }
  1359. static void hists_evsel__exit(struct perf_evsel *evsel)
  1360. {
  1361. struct hists *hists = evsel__hists(evsel);
  1362. hists__delete_all_entries(hists);
  1363. }
  1364. static int hists_evsel__init(struct perf_evsel *evsel)
  1365. {
  1366. struct hists *hists = evsel__hists(evsel);
  1367. __hists__init(hists, &perf_hpp_list);
  1368. return 0;
  1369. }
  1370. /*
  1371. * XXX We probably need a hists_evsel__exit() to free the hist_entries
  1372. * stored in the rbtree...
  1373. */
  1374. int hists__init(void)
  1375. {
  1376. int err = perf_evsel__object_config(sizeof(struct hists_evsel),
  1377. hists_evsel__init,
  1378. hists_evsel__exit);
  1379. if (err)
  1380. fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
  1381. return err;
  1382. }
  1383. void perf_hpp_list__init(struct perf_hpp_list *list)
  1384. {
  1385. INIT_LIST_HEAD(&list->fields);
  1386. INIT_LIST_HEAD(&list->sorts);
  1387. }