hist.c 36 KB

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