hist.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. #include "annotate.h"
  2. #include "util.h"
  3. #include "build-id.h"
  4. #include "hist.h"
  5. #include "session.h"
  6. #include "sort.h"
  7. #include <math.h>
  8. static bool hists__filter_entry_by_dso(struct hists *hists,
  9. struct hist_entry *he);
  10. static bool hists__filter_entry_by_thread(struct hists *hists,
  11. struct hist_entry *he);
  12. static bool hists__filter_entry_by_symbol(struct hists *hists,
  13. struct hist_entry *he);
  14. enum hist_filter {
  15. HIST_FILTER__DSO,
  16. HIST_FILTER__THREAD,
  17. HIST_FILTER__PARENT,
  18. HIST_FILTER__SYMBOL,
  19. };
  20. struct callchain_param callchain_param = {
  21. .mode = CHAIN_GRAPH_REL,
  22. .min_percent = 0.5,
  23. .order = ORDER_CALLEE
  24. };
  25. u16 hists__col_len(struct hists *hists, enum hist_column col)
  26. {
  27. return hists->col_len[col];
  28. }
  29. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
  30. {
  31. hists->col_len[col] = len;
  32. }
  33. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
  34. {
  35. if (len > hists__col_len(hists, col)) {
  36. hists__set_col_len(hists, col, len);
  37. return true;
  38. }
  39. return false;
  40. }
  41. static void hists__reset_col_len(struct hists *hists)
  42. {
  43. enum hist_column col;
  44. for (col = 0; col < HISTC_NR_COLS; ++col)
  45. hists__set_col_len(hists, col, 0);
  46. }
  47. static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
  48. {
  49. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  50. if (hists__col_len(hists, dso) < unresolved_col_width &&
  51. !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  52. !symbol_conf.dso_list)
  53. hists__set_col_len(hists, dso, unresolved_col_width);
  54. }
  55. static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
  56. {
  57. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  58. u16 len;
  59. if (h->ms.sym)
  60. hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
  61. else
  62. hists__set_unres_dso_col_len(hists, HISTC_DSO);
  63. len = thread__comm_len(h->thread);
  64. if (hists__new_col_len(hists, HISTC_COMM, len))
  65. hists__set_col_len(hists, HISTC_THREAD, len + 6);
  66. if (h->ms.map) {
  67. len = dso__name_len(h->ms.map->dso);
  68. hists__new_col_len(hists, HISTC_DSO, len);
  69. }
  70. if (h->branch_info) {
  71. int symlen;
  72. /*
  73. * +4 accounts for '[x] ' priv level info
  74. * +2 account of 0x prefix on raw addresses
  75. */
  76. if (h->branch_info->from.sym) {
  77. symlen = (int)h->branch_info->from.sym->namelen + 4;
  78. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  79. symlen = dso__name_len(h->branch_info->from.map->dso);
  80. hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
  81. } else {
  82. symlen = unresolved_col_width + 4 + 2;
  83. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  84. hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
  85. }
  86. if (h->branch_info->to.sym) {
  87. symlen = (int)h->branch_info->to.sym->namelen + 4;
  88. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  89. symlen = dso__name_len(h->branch_info->to.map->dso);
  90. hists__new_col_len(hists, HISTC_DSO_TO, symlen);
  91. } else {
  92. symlen = unresolved_col_width + 4 + 2;
  93. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  94. hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
  95. }
  96. }
  97. }
  98. static void hist_entry__add_cpumode_period(struct hist_entry *he,
  99. unsigned int cpumode, u64 period)
  100. {
  101. switch (cpumode) {
  102. case PERF_RECORD_MISC_KERNEL:
  103. he->period_sys += period;
  104. break;
  105. case PERF_RECORD_MISC_USER:
  106. he->period_us += period;
  107. break;
  108. case PERF_RECORD_MISC_GUEST_KERNEL:
  109. he->period_guest_sys += period;
  110. break;
  111. case PERF_RECORD_MISC_GUEST_USER:
  112. he->period_guest_us += period;
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. static void hist_entry__decay(struct hist_entry *he)
  119. {
  120. he->period = (he->period * 7) / 8;
  121. he->nr_events = (he->nr_events * 7) / 8;
  122. }
  123. static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
  124. {
  125. u64 prev_period = he->period;
  126. if (prev_period == 0)
  127. return true;
  128. hist_entry__decay(he);
  129. if (!he->filtered)
  130. hists->stats.total_period -= prev_period - he->period;
  131. return he->period == 0;
  132. }
  133. static void __hists__decay_entries(struct hists *hists, bool zap_user,
  134. bool zap_kernel, bool threaded)
  135. {
  136. struct rb_node *next = rb_first(&hists->entries);
  137. struct hist_entry *n;
  138. while (next) {
  139. n = rb_entry(next, struct hist_entry, rb_node);
  140. next = rb_next(&n->rb_node);
  141. /*
  142. * We may be annotating this, for instance, so keep it here in
  143. * case some it gets new samples, we'll eventually free it when
  144. * the user stops browsing and it agains gets fully decayed.
  145. */
  146. if (((zap_user && n->level == '.') ||
  147. (zap_kernel && n->level != '.') ||
  148. hists__decay_entry(hists, n)) &&
  149. !n->used) {
  150. rb_erase(&n->rb_node, &hists->entries);
  151. if (sort__need_collapse || threaded)
  152. rb_erase(&n->rb_node_in, &hists->entries_collapsed);
  153. hist_entry__free(n);
  154. --hists->nr_entries;
  155. }
  156. }
  157. }
  158. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  159. {
  160. return __hists__decay_entries(hists, zap_user, zap_kernel, false);
  161. }
  162. void hists__decay_entries_threaded(struct hists *hists,
  163. bool zap_user, bool zap_kernel)
  164. {
  165. return __hists__decay_entries(hists, zap_user, zap_kernel, true);
  166. }
  167. /*
  168. * histogram, sorted on item, collects periods
  169. */
  170. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  171. {
  172. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  173. struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
  174. if (he != NULL) {
  175. *he = *template;
  176. he->nr_events = 1;
  177. if (he->ms.map)
  178. he->ms.map->referenced = true;
  179. if (symbol_conf.use_callchain)
  180. callchain_init(he->callchain);
  181. }
  182. return he;
  183. }
  184. static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  185. {
  186. if (!h->filtered) {
  187. hists__calc_col_len(hists, h);
  188. ++hists->nr_entries;
  189. hists->stats.total_period += h->period;
  190. }
  191. }
  192. static u8 symbol__parent_filter(const struct symbol *parent)
  193. {
  194. if (symbol_conf.exclude_other && parent == NULL)
  195. return 1 << HIST_FILTER__PARENT;
  196. return 0;
  197. }
  198. static struct hist_entry *add_hist_entry(struct hists *hists,
  199. struct hist_entry *entry,
  200. struct addr_location *al,
  201. u64 period)
  202. {
  203. struct rb_node **p;
  204. struct rb_node *parent = NULL;
  205. struct hist_entry *he;
  206. int cmp;
  207. pthread_mutex_lock(&hists->lock);
  208. p = &hists->entries_in->rb_node;
  209. while (*p != NULL) {
  210. parent = *p;
  211. he = rb_entry(parent, struct hist_entry, rb_node_in);
  212. cmp = hist_entry__cmp(entry, he);
  213. if (!cmp) {
  214. he->period += period;
  215. ++he->nr_events;
  216. /* If the map of an existing hist_entry has
  217. * become out-of-date due to an exec() or
  218. * similar, update it. Otherwise we will
  219. * mis-adjust symbol addresses when computing
  220. * the history counter to increment.
  221. */
  222. if (he->ms.map != entry->ms.map) {
  223. he->ms.map = entry->ms.map;
  224. if (he->ms.map)
  225. he->ms.map->referenced = true;
  226. }
  227. goto out;
  228. }
  229. if (cmp < 0)
  230. p = &(*p)->rb_left;
  231. else
  232. p = &(*p)->rb_right;
  233. }
  234. he = hist_entry__new(entry);
  235. if (!he)
  236. goto out_unlock;
  237. rb_link_node(&he->rb_node_in, parent, p);
  238. rb_insert_color(&he->rb_node_in, hists->entries_in);
  239. out:
  240. hist_entry__add_cpumode_period(he, al->cpumode, period);
  241. out_unlock:
  242. pthread_mutex_unlock(&hists->lock);
  243. return he;
  244. }
  245. struct hist_entry *__hists__add_branch_entry(struct hists *self,
  246. struct addr_location *al,
  247. struct symbol *sym_parent,
  248. struct branch_info *bi,
  249. u64 period)
  250. {
  251. struct hist_entry entry = {
  252. .thread = al->thread,
  253. .ms = {
  254. .map = bi->to.map,
  255. .sym = bi->to.sym,
  256. },
  257. .cpu = al->cpu,
  258. .ip = bi->to.addr,
  259. .level = al->level,
  260. .period = period,
  261. .parent = sym_parent,
  262. .filtered = symbol__parent_filter(sym_parent),
  263. .branch_info = bi,
  264. };
  265. return add_hist_entry(self, &entry, al, period);
  266. }
  267. struct hist_entry *__hists__add_entry(struct hists *self,
  268. struct addr_location *al,
  269. struct symbol *sym_parent, u64 period)
  270. {
  271. struct hist_entry entry = {
  272. .thread = al->thread,
  273. .ms = {
  274. .map = al->map,
  275. .sym = al->sym,
  276. },
  277. .cpu = al->cpu,
  278. .ip = al->addr,
  279. .level = al->level,
  280. .period = period,
  281. .parent = sym_parent,
  282. .filtered = symbol__parent_filter(sym_parent),
  283. };
  284. return add_hist_entry(self, &entry, al, period);
  285. }
  286. int64_t
  287. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  288. {
  289. struct sort_entry *se;
  290. int64_t cmp = 0;
  291. list_for_each_entry(se, &hist_entry__sort_list, list) {
  292. cmp = se->se_cmp(left, right);
  293. if (cmp)
  294. break;
  295. }
  296. return cmp;
  297. }
  298. int64_t
  299. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  300. {
  301. struct sort_entry *se;
  302. int64_t cmp = 0;
  303. list_for_each_entry(se, &hist_entry__sort_list, list) {
  304. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  305. f = se->se_collapse ?: se->se_cmp;
  306. cmp = f(left, right);
  307. if (cmp)
  308. break;
  309. }
  310. return cmp;
  311. }
  312. void hist_entry__free(struct hist_entry *he)
  313. {
  314. free(he);
  315. }
  316. /*
  317. * collapse the histogram
  318. */
  319. static bool hists__collapse_insert_entry(struct hists *hists __used,
  320. struct rb_root *root,
  321. struct hist_entry *he)
  322. {
  323. struct rb_node **p = &root->rb_node;
  324. struct rb_node *parent = NULL;
  325. struct hist_entry *iter;
  326. int64_t cmp;
  327. while (*p != NULL) {
  328. parent = *p;
  329. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  330. cmp = hist_entry__collapse(iter, he);
  331. if (!cmp) {
  332. iter->period += he->period;
  333. iter->nr_events += he->nr_events;
  334. if (symbol_conf.use_callchain) {
  335. callchain_cursor_reset(&callchain_cursor);
  336. callchain_merge(&callchain_cursor,
  337. iter->callchain,
  338. he->callchain);
  339. }
  340. hist_entry__free(he);
  341. return false;
  342. }
  343. if (cmp < 0)
  344. p = &(*p)->rb_left;
  345. else
  346. p = &(*p)->rb_right;
  347. }
  348. rb_link_node(&he->rb_node_in, parent, p);
  349. rb_insert_color(&he->rb_node_in, root);
  350. return true;
  351. }
  352. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  353. {
  354. struct rb_root *root;
  355. pthread_mutex_lock(&hists->lock);
  356. root = hists->entries_in;
  357. if (++hists->entries_in > &hists->entries_in_array[1])
  358. hists->entries_in = &hists->entries_in_array[0];
  359. pthread_mutex_unlock(&hists->lock);
  360. return root;
  361. }
  362. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  363. {
  364. hists__filter_entry_by_dso(hists, he);
  365. hists__filter_entry_by_thread(hists, he);
  366. hists__filter_entry_by_symbol(hists, he);
  367. }
  368. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  369. {
  370. struct rb_root *root;
  371. struct rb_node *next;
  372. struct hist_entry *n;
  373. if (!sort__need_collapse && !threaded)
  374. return;
  375. root = hists__get_rotate_entries_in(hists);
  376. next = rb_first(root);
  377. while (next) {
  378. n = rb_entry(next, struct hist_entry, rb_node_in);
  379. next = rb_next(&n->rb_node_in);
  380. rb_erase(&n->rb_node_in, root);
  381. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  382. /*
  383. * If it wasn't combined with one of the entries already
  384. * collapsed, we need to apply the filters that may have
  385. * been set by, say, the hist_browser.
  386. */
  387. hists__apply_filters(hists, n);
  388. }
  389. }
  390. }
  391. void hists__collapse_resort(struct hists *hists)
  392. {
  393. return __hists__collapse_resort(hists, false);
  394. }
  395. void hists__collapse_resort_threaded(struct hists *hists)
  396. {
  397. return __hists__collapse_resort(hists, true);
  398. }
  399. /*
  400. * reverse the map, sort on period.
  401. */
  402. static void __hists__insert_output_entry(struct rb_root *entries,
  403. struct hist_entry *he,
  404. u64 min_callchain_hits)
  405. {
  406. struct rb_node **p = &entries->rb_node;
  407. struct rb_node *parent = NULL;
  408. struct hist_entry *iter;
  409. if (symbol_conf.use_callchain)
  410. callchain_param.sort(&he->sorted_chain, he->callchain,
  411. min_callchain_hits, &callchain_param);
  412. while (*p != NULL) {
  413. parent = *p;
  414. iter = rb_entry(parent, struct hist_entry, rb_node);
  415. if (he->period > iter->period)
  416. p = &(*p)->rb_left;
  417. else
  418. p = &(*p)->rb_right;
  419. }
  420. rb_link_node(&he->rb_node, parent, p);
  421. rb_insert_color(&he->rb_node, entries);
  422. }
  423. static void __hists__output_resort(struct hists *hists, bool threaded)
  424. {
  425. struct rb_root *root;
  426. struct rb_node *next;
  427. struct hist_entry *n;
  428. u64 min_callchain_hits;
  429. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  430. if (sort__need_collapse || threaded)
  431. root = &hists->entries_collapsed;
  432. else
  433. root = hists->entries_in;
  434. next = rb_first(root);
  435. hists->entries = RB_ROOT;
  436. hists->nr_entries = 0;
  437. hists->stats.total_period = 0;
  438. hists__reset_col_len(hists);
  439. while (next) {
  440. n = rb_entry(next, struct hist_entry, rb_node_in);
  441. next = rb_next(&n->rb_node_in);
  442. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  443. hists__inc_nr_entries(hists, n);
  444. }
  445. }
  446. void hists__output_resort(struct hists *hists)
  447. {
  448. return __hists__output_resort(hists, false);
  449. }
  450. void hists__output_resort_threaded(struct hists *hists)
  451. {
  452. return __hists__output_resort(hists, true);
  453. }
  454. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  455. {
  456. int i;
  457. int ret = fprintf(fp, " ");
  458. for (i = 0; i < left_margin; i++)
  459. ret += fprintf(fp, " ");
  460. return ret;
  461. }
  462. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  463. int left_margin)
  464. {
  465. int i;
  466. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  467. for (i = 0; i < depth; i++)
  468. if (depth_mask & (1 << i))
  469. ret += fprintf(fp, "| ");
  470. else
  471. ret += fprintf(fp, " ");
  472. ret += fprintf(fp, "\n");
  473. return ret;
  474. }
  475. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  476. int depth, int depth_mask, int period,
  477. u64 total_samples, u64 hits,
  478. int left_margin)
  479. {
  480. int i;
  481. size_t ret = 0;
  482. ret += callchain__fprintf_left_margin(fp, left_margin);
  483. for (i = 0; i < depth; i++) {
  484. if (depth_mask & (1 << i))
  485. ret += fprintf(fp, "|");
  486. else
  487. ret += fprintf(fp, " ");
  488. if (!period && i == depth - 1) {
  489. double percent;
  490. percent = hits * 100.0 / total_samples;
  491. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  492. } else
  493. ret += fprintf(fp, "%s", " ");
  494. }
  495. if (chain->ms.sym)
  496. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  497. else
  498. ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
  499. return ret;
  500. }
  501. static struct symbol *rem_sq_bracket;
  502. static struct callchain_list rem_hits;
  503. static void init_rem_hits(void)
  504. {
  505. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  506. if (!rem_sq_bracket) {
  507. fprintf(stderr, "Not enough memory to display remaining hits\n");
  508. return;
  509. }
  510. strcpy(rem_sq_bracket->name, "[...]");
  511. rem_hits.ms.sym = rem_sq_bracket;
  512. }
  513. static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  514. u64 total_samples, int depth,
  515. int depth_mask, int left_margin)
  516. {
  517. struct rb_node *node, *next;
  518. struct callchain_node *child;
  519. struct callchain_list *chain;
  520. int new_depth_mask = depth_mask;
  521. u64 remaining;
  522. size_t ret = 0;
  523. int i;
  524. uint entries_printed = 0;
  525. remaining = total_samples;
  526. node = rb_first(root);
  527. while (node) {
  528. u64 new_total;
  529. u64 cumul;
  530. child = rb_entry(node, struct callchain_node, rb_node);
  531. cumul = callchain_cumul_hits(child);
  532. remaining -= cumul;
  533. /*
  534. * The depth mask manages the output of pipes that show
  535. * the depth. We don't want to keep the pipes of the current
  536. * level for the last child of this depth.
  537. * Except if we have remaining filtered hits. They will
  538. * supersede the last child
  539. */
  540. next = rb_next(node);
  541. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  542. new_depth_mask &= ~(1 << (depth - 1));
  543. /*
  544. * But we keep the older depth mask for the line separator
  545. * to keep the level link until we reach the last child
  546. */
  547. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  548. left_margin);
  549. i = 0;
  550. list_for_each_entry(chain, &child->val, list) {
  551. ret += ipchain__fprintf_graph(fp, chain, depth,
  552. new_depth_mask, i++,
  553. total_samples,
  554. cumul,
  555. left_margin);
  556. }
  557. if (callchain_param.mode == CHAIN_GRAPH_REL)
  558. new_total = child->children_hit;
  559. else
  560. new_total = total_samples;
  561. ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
  562. depth + 1,
  563. new_depth_mask | (1 << depth),
  564. left_margin);
  565. node = next;
  566. if (++entries_printed == callchain_param.print_limit)
  567. break;
  568. }
  569. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  570. remaining && remaining != total_samples) {
  571. if (!rem_sq_bracket)
  572. return ret;
  573. new_depth_mask &= ~(1 << (depth - 1));
  574. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  575. new_depth_mask, 0, total_samples,
  576. remaining, left_margin);
  577. }
  578. return ret;
  579. }
  580. static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  581. u64 total_samples, int left_margin)
  582. {
  583. struct callchain_node *cnode;
  584. struct callchain_list *chain;
  585. u32 entries_printed = 0;
  586. bool printed = false;
  587. struct rb_node *node;
  588. int i = 0;
  589. int ret = 0;
  590. /*
  591. * If have one single callchain root, don't bother printing
  592. * its percentage (100 % in fractal mode and the same percentage
  593. * than the hist in graph mode). This also avoid one level of column.
  594. */
  595. node = rb_first(root);
  596. if (node && !rb_next(node)) {
  597. cnode = rb_entry(node, struct callchain_node, rb_node);
  598. list_for_each_entry(chain, &cnode->val, list) {
  599. /*
  600. * If we sort by symbol, the first entry is the same than
  601. * the symbol. No need to print it otherwise it appears as
  602. * displayed twice.
  603. */
  604. if (!i++ && sort__first_dimension == SORT_SYM)
  605. continue;
  606. if (!printed) {
  607. ret += callchain__fprintf_left_margin(fp, left_margin);
  608. ret += fprintf(fp, "|\n");
  609. ret += callchain__fprintf_left_margin(fp, left_margin);
  610. ret += fprintf(fp, "---");
  611. left_margin += 3;
  612. printed = true;
  613. } else
  614. ret += callchain__fprintf_left_margin(fp, left_margin);
  615. if (chain->ms.sym)
  616. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  617. else
  618. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  619. if (++entries_printed == callchain_param.print_limit)
  620. break;
  621. }
  622. root = &cnode->rb_root;
  623. }
  624. ret += __callchain__fprintf_graph(fp, root, total_samples,
  625. 1, 1, left_margin);
  626. ret += fprintf(fp, "\n");
  627. return ret;
  628. }
  629. static size_t __callchain__fprintf_flat(FILE *fp,
  630. struct callchain_node *self,
  631. u64 total_samples)
  632. {
  633. struct callchain_list *chain;
  634. size_t ret = 0;
  635. if (!self)
  636. return 0;
  637. ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
  638. list_for_each_entry(chain, &self->val, list) {
  639. if (chain->ip >= PERF_CONTEXT_MAX)
  640. continue;
  641. if (chain->ms.sym)
  642. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  643. else
  644. ret += fprintf(fp, " %p\n",
  645. (void *)(long)chain->ip);
  646. }
  647. return ret;
  648. }
  649. static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
  650. u64 total_samples)
  651. {
  652. size_t ret = 0;
  653. u32 entries_printed = 0;
  654. struct rb_node *rb_node;
  655. struct callchain_node *chain;
  656. rb_node = rb_first(self);
  657. while (rb_node) {
  658. double percent;
  659. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  660. percent = chain->hit * 100.0 / total_samples;
  661. ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
  662. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  663. ret += fprintf(fp, "\n");
  664. if (++entries_printed == callchain_param.print_limit)
  665. break;
  666. rb_node = rb_next(rb_node);
  667. }
  668. return ret;
  669. }
  670. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  671. u64 total_samples, int left_margin,
  672. FILE *fp)
  673. {
  674. switch (callchain_param.mode) {
  675. case CHAIN_GRAPH_REL:
  676. return callchain__fprintf_graph(fp, &he->sorted_chain, he->period,
  677. left_margin);
  678. break;
  679. case CHAIN_GRAPH_ABS:
  680. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  681. left_margin);
  682. break;
  683. case CHAIN_FLAT:
  684. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  685. break;
  686. case CHAIN_NONE:
  687. break;
  688. default:
  689. pr_err("Bad callchain mode\n");
  690. }
  691. return 0;
  692. }
  693. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  694. {
  695. struct rb_node *next = rb_first(&hists->entries);
  696. struct hist_entry *n;
  697. int row = 0;
  698. hists__reset_col_len(hists);
  699. while (next && row++ < max_rows) {
  700. n = rb_entry(next, struct hist_entry, rb_node);
  701. if (!n->filtered)
  702. hists__calc_col_len(hists, n);
  703. next = rb_next(&n->rb_node);
  704. }
  705. }
  706. static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
  707. size_t size, struct hists *pair_hists,
  708. bool show_displacement, long displacement,
  709. bool color, u64 total_period)
  710. {
  711. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  712. u64 nr_events;
  713. const char *sep = symbol_conf.field_sep;
  714. int ret;
  715. if (symbol_conf.exclude_other && !he->parent)
  716. return 0;
  717. if (pair_hists) {
  718. period = he->pair ? he->pair->period : 0;
  719. nr_events = he->pair ? he->pair->nr_events : 0;
  720. total = pair_hists->stats.total_period;
  721. period_sys = he->pair ? he->pair->period_sys : 0;
  722. period_us = he->pair ? he->pair->period_us : 0;
  723. period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
  724. period_guest_us = he->pair ? he->pair->period_guest_us : 0;
  725. } else {
  726. period = he->period;
  727. nr_events = he->nr_events;
  728. total = total_period;
  729. period_sys = he->period_sys;
  730. period_us = he->period_us;
  731. period_guest_sys = he->period_guest_sys;
  732. period_guest_us = he->period_guest_us;
  733. }
  734. if (total) {
  735. if (color)
  736. ret = percent_color_snprintf(s, size,
  737. sep ? "%.2f" : " %6.2f%%",
  738. (period * 100.0) / total);
  739. else
  740. ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  741. (period * 100.0) / total);
  742. if (symbol_conf.show_cpu_utilization) {
  743. ret += percent_color_snprintf(s + ret, size - ret,
  744. sep ? "%.2f" : " %6.2f%%",
  745. (period_sys * 100.0) / total);
  746. ret += percent_color_snprintf(s + ret, size - ret,
  747. sep ? "%.2f" : " %6.2f%%",
  748. (period_us * 100.0) / total);
  749. if (perf_guest) {
  750. ret += percent_color_snprintf(s + ret,
  751. size - ret,
  752. sep ? "%.2f" : " %6.2f%%",
  753. (period_guest_sys * 100.0) /
  754. total);
  755. ret += percent_color_snprintf(s + ret,
  756. size - ret,
  757. sep ? "%.2f" : " %6.2f%%",
  758. (period_guest_us * 100.0) /
  759. total);
  760. }
  761. }
  762. } else
  763. ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  764. if (symbol_conf.show_nr_samples) {
  765. if (sep)
  766. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  767. else
  768. ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  769. }
  770. if (symbol_conf.show_total_period) {
  771. if (sep)
  772. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  773. else
  774. ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
  775. }
  776. if (pair_hists) {
  777. char bf[32];
  778. double old_percent = 0, new_percent = 0, diff;
  779. if (total > 0)
  780. old_percent = (period * 100.0) / total;
  781. if (total_period > 0)
  782. new_percent = (he->period * 100.0) / total_period;
  783. diff = new_percent - old_percent;
  784. if (fabs(diff) >= 0.01)
  785. scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  786. else
  787. scnprintf(bf, sizeof(bf), " ");
  788. if (sep)
  789. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  790. else
  791. ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
  792. if (show_displacement) {
  793. if (displacement)
  794. scnprintf(bf, sizeof(bf), "%+4ld", displacement);
  795. else
  796. scnprintf(bf, sizeof(bf), " ");
  797. if (sep)
  798. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  799. else
  800. ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
  801. }
  802. }
  803. return ret;
  804. }
  805. int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
  806. struct hists *hists)
  807. {
  808. const char *sep = symbol_conf.field_sep;
  809. struct sort_entry *se;
  810. int ret = 0;
  811. list_for_each_entry(se, &hist_entry__sort_list, list) {
  812. if (se->elide)
  813. continue;
  814. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  815. ret += se->se_snprintf(he, s + ret, size - ret,
  816. hists__col_len(hists, se->se_width_idx));
  817. }
  818. return ret;
  819. }
  820. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  821. struct hists *hists, struct hists *pair_hists,
  822. bool show_displacement, long displacement,
  823. u64 total_period, FILE *fp)
  824. {
  825. char bf[512];
  826. int ret;
  827. if (size == 0 || size > sizeof(bf))
  828. size = sizeof(bf);
  829. ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
  830. show_displacement, displacement,
  831. true, total_period);
  832. hist_entry__snprintf(he, bf + ret, size - ret, hists);
  833. return fprintf(fp, "%s\n", bf);
  834. }
  835. static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
  836. struct hists *hists,
  837. u64 total_period, FILE *fp)
  838. {
  839. int left_margin = 0;
  840. if (sort__first_dimension == SORT_COMM) {
  841. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  842. typeof(*se), list);
  843. left_margin = hists__col_len(hists, se->se_width_idx);
  844. left_margin -= thread__comm_len(he->thread);
  845. }
  846. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  847. }
  848. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  849. bool show_displacement, bool show_header, int max_rows,
  850. int max_cols, FILE *fp)
  851. {
  852. struct sort_entry *se;
  853. struct rb_node *nd;
  854. size_t ret = 0;
  855. u64 total_period;
  856. unsigned long position = 1;
  857. long displacement = 0;
  858. unsigned int width;
  859. const char *sep = symbol_conf.field_sep;
  860. const char *col_width = symbol_conf.col_width_list_str;
  861. int nr_rows = 0;
  862. init_rem_hits();
  863. if (!show_header)
  864. goto print_entries;
  865. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  866. if (symbol_conf.show_cpu_utilization) {
  867. if (sep) {
  868. ret += fprintf(fp, "%csys", *sep);
  869. ret += fprintf(fp, "%cus", *sep);
  870. if (perf_guest) {
  871. ret += fprintf(fp, "%cguest sys", *sep);
  872. ret += fprintf(fp, "%cguest us", *sep);
  873. }
  874. } else {
  875. ret += fprintf(fp, " sys ");
  876. ret += fprintf(fp, " us ");
  877. if (perf_guest) {
  878. ret += fprintf(fp, " guest sys ");
  879. ret += fprintf(fp, " guest us ");
  880. }
  881. }
  882. }
  883. if (symbol_conf.show_nr_samples) {
  884. if (sep)
  885. fprintf(fp, "%cSamples", *sep);
  886. else
  887. fputs(" Samples ", fp);
  888. }
  889. if (symbol_conf.show_total_period) {
  890. if (sep)
  891. ret += fprintf(fp, "%cPeriod", *sep);
  892. else
  893. ret += fprintf(fp, " Period ");
  894. }
  895. if (pair) {
  896. if (sep)
  897. ret += fprintf(fp, "%cDelta", *sep);
  898. else
  899. ret += fprintf(fp, " Delta ");
  900. if (show_displacement) {
  901. if (sep)
  902. ret += fprintf(fp, "%cDisplacement", *sep);
  903. else
  904. ret += fprintf(fp, " Displ");
  905. }
  906. }
  907. list_for_each_entry(se, &hist_entry__sort_list, list) {
  908. if (se->elide)
  909. continue;
  910. if (sep) {
  911. fprintf(fp, "%c%s", *sep, se->se_header);
  912. continue;
  913. }
  914. width = strlen(se->se_header);
  915. if (symbol_conf.col_width_list_str) {
  916. if (col_width) {
  917. hists__set_col_len(hists, se->se_width_idx,
  918. atoi(col_width));
  919. col_width = strchr(col_width, ',');
  920. if (col_width)
  921. ++col_width;
  922. }
  923. }
  924. if (!hists__new_col_len(hists, se->se_width_idx, width))
  925. width = hists__col_len(hists, se->se_width_idx);
  926. fprintf(fp, " %*s", width, se->se_header);
  927. }
  928. fprintf(fp, "\n");
  929. if (max_rows && ++nr_rows >= max_rows)
  930. goto out;
  931. if (sep)
  932. goto print_entries;
  933. fprintf(fp, "# ........");
  934. if (symbol_conf.show_cpu_utilization)
  935. fprintf(fp, " ....... .......");
  936. if (symbol_conf.show_nr_samples)
  937. fprintf(fp, " ..........");
  938. if (symbol_conf.show_total_period)
  939. fprintf(fp, " ............");
  940. if (pair) {
  941. fprintf(fp, " ..........");
  942. if (show_displacement)
  943. fprintf(fp, " .....");
  944. }
  945. list_for_each_entry(se, &hist_entry__sort_list, list) {
  946. unsigned int i;
  947. if (se->elide)
  948. continue;
  949. fprintf(fp, " ");
  950. width = hists__col_len(hists, se->se_width_idx);
  951. if (width == 0)
  952. width = strlen(se->se_header);
  953. for (i = 0; i < width; i++)
  954. fprintf(fp, ".");
  955. }
  956. fprintf(fp, "\n");
  957. if (max_rows && ++nr_rows >= max_rows)
  958. goto out;
  959. fprintf(fp, "#\n");
  960. if (max_rows && ++nr_rows >= max_rows)
  961. goto out;
  962. print_entries:
  963. total_period = hists->stats.total_period;
  964. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  965. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  966. if (h->filtered)
  967. continue;
  968. if (show_displacement) {
  969. if (h->pair != NULL)
  970. displacement = ((long)h->pair->position -
  971. (long)position);
  972. else
  973. displacement = 0;
  974. ++position;
  975. }
  976. ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
  977. displacement, total_period, fp);
  978. if (symbol_conf.use_callchain)
  979. ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
  980. if (max_rows && ++nr_rows >= max_rows)
  981. goto out;
  982. if (h->ms.map == NULL && verbose > 1) {
  983. __map_groups__fprintf_maps(&h->thread->mg,
  984. MAP__FUNCTION, verbose, fp);
  985. fprintf(fp, "%.10s end\n", graph_dotted_line);
  986. }
  987. }
  988. out:
  989. free(rem_sq_bracket);
  990. return ret;
  991. }
  992. /*
  993. * See hists__fprintf to match the column widths
  994. */
  995. unsigned int hists__sort_list_width(struct hists *hists)
  996. {
  997. struct sort_entry *se;
  998. int ret = 9; /* total % */
  999. if (symbol_conf.show_cpu_utilization) {
  1000. ret += 7; /* count_sys % */
  1001. ret += 6; /* count_us % */
  1002. if (perf_guest) {
  1003. ret += 13; /* count_guest_sys % */
  1004. ret += 12; /* count_guest_us % */
  1005. }
  1006. }
  1007. if (symbol_conf.show_nr_samples)
  1008. ret += 11;
  1009. if (symbol_conf.show_total_period)
  1010. ret += 13;
  1011. list_for_each_entry(se, &hist_entry__sort_list, list)
  1012. if (!se->elide)
  1013. ret += 2 + hists__col_len(hists, se->se_width_idx);
  1014. if (verbose) /* Addr + origin */
  1015. ret += 3 + BITS_PER_LONG / 4;
  1016. return ret;
  1017. }
  1018. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  1019. enum hist_filter filter)
  1020. {
  1021. h->filtered &= ~(1 << filter);
  1022. if (h->filtered)
  1023. return;
  1024. ++hists->nr_entries;
  1025. if (h->ms.unfolded)
  1026. hists->nr_entries += h->nr_rows;
  1027. h->row_offset = 0;
  1028. hists->stats.total_period += h->period;
  1029. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  1030. hists__calc_col_len(hists, h);
  1031. }
  1032. static bool hists__filter_entry_by_dso(struct hists *hists,
  1033. struct hist_entry *he)
  1034. {
  1035. if (hists->dso_filter != NULL &&
  1036. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  1037. he->filtered |= (1 << HIST_FILTER__DSO);
  1038. return true;
  1039. }
  1040. return false;
  1041. }
  1042. void hists__filter_by_dso(struct hists *hists)
  1043. {
  1044. struct rb_node *nd;
  1045. hists->nr_entries = hists->stats.total_period = 0;
  1046. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1047. hists__reset_col_len(hists);
  1048. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1049. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1050. if (symbol_conf.exclude_other && !h->parent)
  1051. continue;
  1052. if (hists__filter_entry_by_dso(hists, h))
  1053. continue;
  1054. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  1055. }
  1056. }
  1057. static bool hists__filter_entry_by_thread(struct hists *hists,
  1058. struct hist_entry *he)
  1059. {
  1060. if (hists->thread_filter != NULL &&
  1061. he->thread != hists->thread_filter) {
  1062. he->filtered |= (1 << HIST_FILTER__THREAD);
  1063. return true;
  1064. }
  1065. return false;
  1066. }
  1067. void hists__filter_by_thread(struct hists *hists)
  1068. {
  1069. struct rb_node *nd;
  1070. hists->nr_entries = hists->stats.total_period = 0;
  1071. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1072. hists__reset_col_len(hists);
  1073. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1074. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1075. if (hists__filter_entry_by_thread(hists, h))
  1076. continue;
  1077. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  1078. }
  1079. }
  1080. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1081. struct hist_entry *he)
  1082. {
  1083. if (hists->symbol_filter_str != NULL &&
  1084. (!he->ms.sym || strstr(he->ms.sym->name,
  1085. hists->symbol_filter_str) == NULL)) {
  1086. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1087. return true;
  1088. }
  1089. return false;
  1090. }
  1091. void hists__filter_by_symbol(struct hists *hists)
  1092. {
  1093. struct rb_node *nd;
  1094. hists->nr_entries = hists->stats.total_period = 0;
  1095. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1096. hists__reset_col_len(hists);
  1097. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1098. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1099. if (hists__filter_entry_by_symbol(hists, h))
  1100. continue;
  1101. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  1102. }
  1103. }
  1104. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  1105. {
  1106. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  1107. }
  1108. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  1109. {
  1110. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  1111. }
  1112. void hists__inc_nr_events(struct hists *hists, u32 type)
  1113. {
  1114. ++hists->stats.nr_events[0];
  1115. ++hists->stats.nr_events[type];
  1116. }
  1117. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  1118. {
  1119. int i;
  1120. size_t ret = 0;
  1121. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  1122. const char *name;
  1123. if (hists->stats.nr_events[i] == 0)
  1124. continue;
  1125. name = perf_event__name(i);
  1126. if (!strcmp(name, "UNKNOWN"))
  1127. continue;
  1128. ret += fprintf(fp, "%16s events: %10d\n", name,
  1129. hists->stats.nr_events[i]);
  1130. }
  1131. return ret;
  1132. }