hist.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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,
  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(&hists->callchain_cursor);
  336. callchain_merge(&hists->callchain_cursor, iter->callchain,
  337. he->callchain);
  338. }
  339. hist_entry__free(he);
  340. return false;
  341. }
  342. if (cmp < 0)
  343. p = &(*p)->rb_left;
  344. else
  345. p = &(*p)->rb_right;
  346. }
  347. rb_link_node(&he->rb_node_in, parent, p);
  348. rb_insert_color(&he->rb_node_in, root);
  349. return true;
  350. }
  351. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  352. {
  353. struct rb_root *root;
  354. pthread_mutex_lock(&hists->lock);
  355. root = hists->entries_in;
  356. if (++hists->entries_in > &hists->entries_in_array[1])
  357. hists->entries_in = &hists->entries_in_array[0];
  358. pthread_mutex_unlock(&hists->lock);
  359. return root;
  360. }
  361. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  362. {
  363. hists__filter_entry_by_dso(hists, he);
  364. hists__filter_entry_by_thread(hists, he);
  365. hists__filter_entry_by_symbol(hists, he);
  366. }
  367. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  368. {
  369. struct rb_root *root;
  370. struct rb_node *next;
  371. struct hist_entry *n;
  372. if (!sort__need_collapse && !threaded)
  373. return;
  374. root = hists__get_rotate_entries_in(hists);
  375. next = rb_first(root);
  376. while (next) {
  377. n = rb_entry(next, struct hist_entry, rb_node_in);
  378. next = rb_next(&n->rb_node_in);
  379. rb_erase(&n->rb_node_in, root);
  380. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  381. /*
  382. * If it wasn't combined with one of the entries already
  383. * collapsed, we need to apply the filters that may have
  384. * been set by, say, the hist_browser.
  385. */
  386. hists__apply_filters(hists, n);
  387. }
  388. }
  389. }
  390. void hists__collapse_resort(struct hists *hists)
  391. {
  392. return __hists__collapse_resort(hists, false);
  393. }
  394. void hists__collapse_resort_threaded(struct hists *hists)
  395. {
  396. return __hists__collapse_resort(hists, true);
  397. }
  398. /*
  399. * reverse the map, sort on period.
  400. */
  401. static void __hists__insert_output_entry(struct rb_root *entries,
  402. struct hist_entry *he,
  403. u64 min_callchain_hits)
  404. {
  405. struct rb_node **p = &entries->rb_node;
  406. struct rb_node *parent = NULL;
  407. struct hist_entry *iter;
  408. if (symbol_conf.use_callchain)
  409. callchain_param.sort(&he->sorted_chain, he->callchain,
  410. min_callchain_hits, &callchain_param);
  411. while (*p != NULL) {
  412. parent = *p;
  413. iter = rb_entry(parent, struct hist_entry, rb_node);
  414. if (he->period > iter->period)
  415. p = &(*p)->rb_left;
  416. else
  417. p = &(*p)->rb_right;
  418. }
  419. rb_link_node(&he->rb_node, parent, p);
  420. rb_insert_color(&he->rb_node, entries);
  421. }
  422. static void __hists__output_resort(struct hists *hists, bool threaded)
  423. {
  424. struct rb_root *root;
  425. struct rb_node *next;
  426. struct hist_entry *n;
  427. u64 min_callchain_hits;
  428. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  429. if (sort__need_collapse || threaded)
  430. root = &hists->entries_collapsed;
  431. else
  432. root = hists->entries_in;
  433. next = rb_first(root);
  434. hists->entries = RB_ROOT;
  435. hists->nr_entries = 0;
  436. hists->stats.total_period = 0;
  437. hists__reset_col_len(hists);
  438. while (next) {
  439. n = rb_entry(next, struct hist_entry, rb_node_in);
  440. next = rb_next(&n->rb_node_in);
  441. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  442. hists__inc_nr_entries(hists, n);
  443. }
  444. }
  445. void hists__output_resort(struct hists *hists)
  446. {
  447. return __hists__output_resort(hists, false);
  448. }
  449. void hists__output_resort_threaded(struct hists *hists)
  450. {
  451. return __hists__output_resort(hists, true);
  452. }
  453. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  454. {
  455. int i;
  456. int ret = fprintf(fp, " ");
  457. for (i = 0; i < left_margin; i++)
  458. ret += fprintf(fp, " ");
  459. return ret;
  460. }
  461. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  462. int left_margin)
  463. {
  464. int i;
  465. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  466. for (i = 0; i < depth; i++)
  467. if (depth_mask & (1 << i))
  468. ret += fprintf(fp, "| ");
  469. else
  470. ret += fprintf(fp, " ");
  471. ret += fprintf(fp, "\n");
  472. return ret;
  473. }
  474. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  475. int depth, int depth_mask, int period,
  476. u64 total_samples, u64 hits,
  477. int left_margin)
  478. {
  479. int i;
  480. size_t ret = 0;
  481. ret += callchain__fprintf_left_margin(fp, left_margin);
  482. for (i = 0; i < depth; i++) {
  483. if (depth_mask & (1 << i))
  484. ret += fprintf(fp, "|");
  485. else
  486. ret += fprintf(fp, " ");
  487. if (!period && i == depth - 1) {
  488. double percent;
  489. percent = hits * 100.0 / total_samples;
  490. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  491. } else
  492. ret += fprintf(fp, "%s", " ");
  493. }
  494. if (chain->ms.sym)
  495. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  496. else
  497. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  498. return ret;
  499. }
  500. static struct symbol *rem_sq_bracket;
  501. static struct callchain_list rem_hits;
  502. static void init_rem_hits(void)
  503. {
  504. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  505. if (!rem_sq_bracket) {
  506. fprintf(stderr, "Not enough memory to display remaining hits\n");
  507. return;
  508. }
  509. strcpy(rem_sq_bracket->name, "[...]");
  510. rem_hits.ms.sym = rem_sq_bracket;
  511. }
  512. static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  513. u64 total_samples, int depth,
  514. int depth_mask, int left_margin)
  515. {
  516. struct rb_node *node, *next;
  517. struct callchain_node *child;
  518. struct callchain_list *chain;
  519. int new_depth_mask = depth_mask;
  520. u64 remaining;
  521. size_t ret = 0;
  522. int i;
  523. uint entries_printed = 0;
  524. remaining = total_samples;
  525. node = rb_first(root);
  526. while (node) {
  527. u64 new_total;
  528. u64 cumul;
  529. child = rb_entry(node, struct callchain_node, rb_node);
  530. cumul = callchain_cumul_hits(child);
  531. remaining -= cumul;
  532. /*
  533. * The depth mask manages the output of pipes that show
  534. * the depth. We don't want to keep the pipes of the current
  535. * level for the last child of this depth.
  536. * Except if we have remaining filtered hits. They will
  537. * supersede the last child
  538. */
  539. next = rb_next(node);
  540. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  541. new_depth_mask &= ~(1 << (depth - 1));
  542. /*
  543. * But we keep the older depth mask for the line separator
  544. * to keep the level link until we reach the last child
  545. */
  546. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  547. left_margin);
  548. i = 0;
  549. list_for_each_entry(chain, &child->val, list) {
  550. ret += ipchain__fprintf_graph(fp, chain, depth,
  551. new_depth_mask, i++,
  552. total_samples,
  553. cumul,
  554. left_margin);
  555. }
  556. if (callchain_param.mode == CHAIN_GRAPH_REL)
  557. new_total = child->children_hit;
  558. else
  559. new_total = total_samples;
  560. ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
  561. depth + 1,
  562. new_depth_mask | (1 << depth),
  563. left_margin);
  564. node = next;
  565. if (++entries_printed == callchain_param.print_limit)
  566. break;
  567. }
  568. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  569. remaining && remaining != total_samples) {
  570. if (!rem_sq_bracket)
  571. return ret;
  572. new_depth_mask &= ~(1 << (depth - 1));
  573. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  574. new_depth_mask, 0, total_samples,
  575. remaining, left_margin);
  576. }
  577. return ret;
  578. }
  579. static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  580. u64 total_samples, int left_margin)
  581. {
  582. struct callchain_node *cnode;
  583. struct callchain_list *chain;
  584. u32 entries_printed = 0;
  585. bool printed = false;
  586. struct rb_node *node;
  587. int i = 0;
  588. int ret;
  589. /*
  590. * If have one single callchain root, don't bother printing
  591. * its percentage (100 % in fractal mode and the same percentage
  592. * than the hist in graph mode). This also avoid one level of column.
  593. */
  594. node = rb_first(root);
  595. if (node && !rb_next(node)) {
  596. cnode = rb_entry(node, struct callchain_node, rb_node);
  597. list_for_each_entry(chain, &cnode->val, list) {
  598. /*
  599. * If we sort by symbol, the first entry is the same than
  600. * the symbol. No need to print it otherwise it appears as
  601. * displayed twice.
  602. */
  603. if (!i++ && sort__first_dimension == SORT_SYM)
  604. continue;
  605. if (!printed) {
  606. ret += callchain__fprintf_left_margin(fp, left_margin);
  607. ret += fprintf(fp, "|\n");
  608. ret += callchain__fprintf_left_margin(fp, left_margin);
  609. ret += fprintf(fp, "---");
  610. left_margin += 3;
  611. printed = true;
  612. } else
  613. ret += callchain__fprintf_left_margin(fp, left_margin);
  614. if (chain->ms.sym)
  615. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  616. else
  617. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  618. if (++entries_printed == callchain_param.print_limit)
  619. break;
  620. }
  621. root = &cnode->rb_root;
  622. }
  623. return __callchain__fprintf_graph(fp, root, total_samples,
  624. 1, 1, left_margin);
  625. }
  626. static size_t __callchain__fprintf_flat(FILE *fp,
  627. struct callchain_node *self,
  628. u64 total_samples)
  629. {
  630. struct callchain_list *chain;
  631. size_t ret = 0;
  632. if (!self)
  633. return 0;
  634. ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
  635. list_for_each_entry(chain, &self->val, list) {
  636. if (chain->ip >= PERF_CONTEXT_MAX)
  637. continue;
  638. if (chain->ms.sym)
  639. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  640. else
  641. ret += fprintf(fp, " %p\n",
  642. (void *)(long)chain->ip);
  643. }
  644. return ret;
  645. }
  646. static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
  647. u64 total_samples)
  648. {
  649. size_t ret = 0;
  650. u32 entries_printed = 0;
  651. struct rb_node *rb_node;
  652. struct callchain_node *chain;
  653. rb_node = rb_first(self);
  654. while (rb_node) {
  655. double percent;
  656. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  657. percent = chain->hit * 100.0 / total_samples;
  658. ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
  659. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  660. ret += fprintf(fp, "\n");
  661. if (++entries_printed == callchain_param.print_limit)
  662. break;
  663. rb_node = rb_next(rb_node);
  664. }
  665. return ret;
  666. }
  667. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  668. u64 total_samples, int left_margin,
  669. FILE *fp)
  670. {
  671. switch (callchain_param.mode) {
  672. case CHAIN_GRAPH_REL:
  673. return callchain__fprintf_graph(fp, &he->sorted_chain, he->period,
  674. left_margin);
  675. break;
  676. case CHAIN_GRAPH_ABS:
  677. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  678. left_margin);
  679. break;
  680. case CHAIN_FLAT:
  681. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  682. break;
  683. case CHAIN_NONE:
  684. break;
  685. default:
  686. pr_err("Bad callchain mode\n");
  687. }
  688. return 0;
  689. }
  690. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  691. {
  692. struct rb_node *next = rb_first(&hists->entries);
  693. struct hist_entry *n;
  694. int row = 0;
  695. hists__reset_col_len(hists);
  696. while (next && row++ < max_rows) {
  697. n = rb_entry(next, struct hist_entry, rb_node);
  698. if (!n->filtered)
  699. hists__calc_col_len(hists, n);
  700. next = rb_next(&n->rb_node);
  701. }
  702. }
  703. static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
  704. size_t size, struct hists *pair_hists,
  705. bool show_displacement, long displacement,
  706. bool color, u64 total_period)
  707. {
  708. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  709. u64 nr_events;
  710. const char *sep = symbol_conf.field_sep;
  711. int ret;
  712. if (symbol_conf.exclude_other && !he->parent)
  713. return 0;
  714. if (pair_hists) {
  715. period = he->pair ? he->pair->period : 0;
  716. nr_events = he->pair ? he->pair->nr_events : 0;
  717. total = pair_hists->stats.total_period;
  718. period_sys = he->pair ? he->pair->period_sys : 0;
  719. period_us = he->pair ? he->pair->period_us : 0;
  720. period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
  721. period_guest_us = he->pair ? he->pair->period_guest_us : 0;
  722. } else {
  723. period = he->period;
  724. nr_events = he->nr_events;
  725. total = total_period;
  726. period_sys = he->period_sys;
  727. period_us = he->period_us;
  728. period_guest_sys = he->period_guest_sys;
  729. period_guest_us = he->period_guest_us;
  730. }
  731. if (total) {
  732. if (color)
  733. ret = percent_color_snprintf(s, size,
  734. sep ? "%.2f" : " %6.2f%%",
  735. (period * 100.0) / total);
  736. else
  737. ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  738. (period * 100.0) / total);
  739. if (symbol_conf.show_cpu_utilization) {
  740. ret += percent_color_snprintf(s + ret, size - ret,
  741. sep ? "%.2f" : " %6.2f%%",
  742. (period_sys * 100.0) / total);
  743. ret += percent_color_snprintf(s + ret, size - ret,
  744. sep ? "%.2f" : " %6.2f%%",
  745. (period_us * 100.0) / total);
  746. if (perf_guest) {
  747. ret += percent_color_snprintf(s + ret,
  748. size - ret,
  749. sep ? "%.2f" : " %6.2f%%",
  750. (period_guest_sys * 100.0) /
  751. total);
  752. ret += percent_color_snprintf(s + ret,
  753. size - ret,
  754. sep ? "%.2f" : " %6.2f%%",
  755. (period_guest_us * 100.0) /
  756. total);
  757. }
  758. }
  759. } else
  760. ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  761. if (symbol_conf.show_nr_samples) {
  762. if (sep)
  763. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  764. else
  765. ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  766. }
  767. if (symbol_conf.show_total_period) {
  768. if (sep)
  769. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  770. else
  771. ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
  772. }
  773. if (pair_hists) {
  774. char bf[32];
  775. double old_percent = 0, new_percent = 0, diff;
  776. if (total > 0)
  777. old_percent = (period * 100.0) / total;
  778. if (total_period > 0)
  779. new_percent = (he->period * 100.0) / total_period;
  780. diff = new_percent - old_percent;
  781. if (fabs(diff) >= 0.01)
  782. scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  783. else
  784. scnprintf(bf, sizeof(bf), " ");
  785. if (sep)
  786. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  787. else
  788. ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
  789. if (show_displacement) {
  790. if (displacement)
  791. scnprintf(bf, sizeof(bf), "%+4ld", displacement);
  792. else
  793. scnprintf(bf, sizeof(bf), " ");
  794. if (sep)
  795. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  796. else
  797. ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
  798. }
  799. }
  800. return ret;
  801. }
  802. int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
  803. struct hists *hists)
  804. {
  805. const char *sep = symbol_conf.field_sep;
  806. struct sort_entry *se;
  807. int ret = 0;
  808. list_for_each_entry(se, &hist_entry__sort_list, list) {
  809. if (se->elide)
  810. continue;
  811. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  812. ret += se->se_snprintf(he, s + ret, size - ret,
  813. hists__col_len(hists, se->se_width_idx));
  814. }
  815. return ret;
  816. }
  817. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  818. struct hists *hists, struct hists *pair_hists,
  819. bool show_displacement, long displacement,
  820. u64 total_period, FILE *fp)
  821. {
  822. char bf[512];
  823. int ret;
  824. if (size == 0 || size > sizeof(bf))
  825. size = sizeof(bf);
  826. ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
  827. show_displacement, displacement,
  828. true, total_period);
  829. hist_entry__snprintf(he, bf + ret, size - ret, hists);
  830. return fprintf(fp, "%s\n", bf);
  831. }
  832. static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
  833. struct hists *hists,
  834. u64 total_period, FILE *fp)
  835. {
  836. int left_margin = 0;
  837. if (sort__first_dimension == SORT_COMM) {
  838. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  839. typeof(*se), list);
  840. left_margin = hists__col_len(hists, se->se_width_idx);
  841. left_margin -= thread__comm_len(he->thread);
  842. }
  843. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  844. }
  845. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  846. bool show_displacement, bool show_header, int max_rows,
  847. int max_cols, FILE *fp)
  848. {
  849. struct sort_entry *se;
  850. struct rb_node *nd;
  851. size_t ret = 0;
  852. u64 total_period;
  853. unsigned long position = 1;
  854. long displacement = 0;
  855. unsigned int width;
  856. const char *sep = symbol_conf.field_sep;
  857. const char *col_width = symbol_conf.col_width_list_str;
  858. int nr_rows = 0;
  859. init_rem_hits();
  860. if (!show_header)
  861. goto print_entries;
  862. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  863. if (symbol_conf.show_cpu_utilization) {
  864. if (sep) {
  865. ret += fprintf(fp, "%csys", *sep);
  866. ret += fprintf(fp, "%cus", *sep);
  867. if (perf_guest) {
  868. ret += fprintf(fp, "%cguest sys", *sep);
  869. ret += fprintf(fp, "%cguest us", *sep);
  870. }
  871. } else {
  872. ret += fprintf(fp, " sys ");
  873. ret += fprintf(fp, " us ");
  874. if (perf_guest) {
  875. ret += fprintf(fp, " guest sys ");
  876. ret += fprintf(fp, " guest us ");
  877. }
  878. }
  879. }
  880. if (symbol_conf.show_nr_samples) {
  881. if (sep)
  882. fprintf(fp, "%cSamples", *sep);
  883. else
  884. fputs(" Samples ", fp);
  885. }
  886. if (symbol_conf.show_total_period) {
  887. if (sep)
  888. ret += fprintf(fp, "%cPeriod", *sep);
  889. else
  890. ret += fprintf(fp, " Period ");
  891. }
  892. if (pair) {
  893. if (sep)
  894. ret += fprintf(fp, "%cDelta", *sep);
  895. else
  896. ret += fprintf(fp, " Delta ");
  897. if (show_displacement) {
  898. if (sep)
  899. ret += fprintf(fp, "%cDisplacement", *sep);
  900. else
  901. ret += fprintf(fp, " Displ");
  902. }
  903. }
  904. list_for_each_entry(se, &hist_entry__sort_list, list) {
  905. if (se->elide)
  906. continue;
  907. if (sep) {
  908. fprintf(fp, "%c%s", *sep, se->se_header);
  909. continue;
  910. }
  911. width = strlen(se->se_header);
  912. if (symbol_conf.col_width_list_str) {
  913. if (col_width) {
  914. hists__set_col_len(hists, se->se_width_idx,
  915. atoi(col_width));
  916. col_width = strchr(col_width, ',');
  917. if (col_width)
  918. ++col_width;
  919. }
  920. }
  921. if (!hists__new_col_len(hists, se->se_width_idx, width))
  922. width = hists__col_len(hists, se->se_width_idx);
  923. fprintf(fp, " %*s", width, se->se_header);
  924. }
  925. fprintf(fp, "\n");
  926. if (max_rows && ++nr_rows >= max_rows)
  927. goto out;
  928. if (sep)
  929. goto print_entries;
  930. fprintf(fp, "# ........");
  931. if (symbol_conf.show_cpu_utilization)
  932. fprintf(fp, " ....... .......");
  933. if (symbol_conf.show_nr_samples)
  934. fprintf(fp, " ..........");
  935. if (symbol_conf.show_total_period)
  936. fprintf(fp, " ............");
  937. if (pair) {
  938. fprintf(fp, " ..........");
  939. if (show_displacement)
  940. fprintf(fp, " .....");
  941. }
  942. list_for_each_entry(se, &hist_entry__sort_list, list) {
  943. unsigned int i;
  944. if (se->elide)
  945. continue;
  946. fprintf(fp, " ");
  947. width = hists__col_len(hists, se->se_width_idx);
  948. if (width == 0)
  949. width = strlen(se->se_header);
  950. for (i = 0; i < width; i++)
  951. fprintf(fp, ".");
  952. }
  953. fprintf(fp, "\n");
  954. if (max_rows && ++nr_rows >= max_rows)
  955. goto out;
  956. fprintf(fp, "#\n");
  957. if (max_rows && ++nr_rows >= max_rows)
  958. goto out;
  959. print_entries:
  960. total_period = hists->stats.total_period;
  961. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  962. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  963. if (h->filtered)
  964. continue;
  965. if (show_displacement) {
  966. if (h->pair != NULL)
  967. displacement = ((long)h->pair->position -
  968. (long)position);
  969. else
  970. displacement = 0;
  971. ++position;
  972. }
  973. ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
  974. displacement, total_period, fp);
  975. if (symbol_conf.use_callchain)
  976. ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
  977. if (max_rows && ++nr_rows >= max_rows)
  978. goto out;
  979. if (h->ms.map == NULL && verbose > 1) {
  980. __map_groups__fprintf_maps(&h->thread->mg,
  981. MAP__FUNCTION, verbose, fp);
  982. fprintf(fp, "%.10s end\n", graph_dotted_line);
  983. }
  984. }
  985. out:
  986. free(rem_sq_bracket);
  987. return ret;
  988. }
  989. /*
  990. * See hists__fprintf to match the column widths
  991. */
  992. unsigned int hists__sort_list_width(struct hists *hists)
  993. {
  994. struct sort_entry *se;
  995. int ret = 9; /* total % */
  996. if (symbol_conf.show_cpu_utilization) {
  997. ret += 7; /* count_sys % */
  998. ret += 6; /* count_us % */
  999. if (perf_guest) {
  1000. ret += 13; /* count_guest_sys % */
  1001. ret += 12; /* count_guest_us % */
  1002. }
  1003. }
  1004. if (symbol_conf.show_nr_samples)
  1005. ret += 11;
  1006. if (symbol_conf.show_total_period)
  1007. ret += 13;
  1008. list_for_each_entry(se, &hist_entry__sort_list, list)
  1009. if (!se->elide)
  1010. ret += 2 + hists__col_len(hists, se->se_width_idx);
  1011. if (verbose) /* Addr + origin */
  1012. ret += 3 + BITS_PER_LONG / 4;
  1013. return ret;
  1014. }
  1015. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  1016. enum hist_filter filter)
  1017. {
  1018. h->filtered &= ~(1 << filter);
  1019. if (h->filtered)
  1020. return;
  1021. ++hists->nr_entries;
  1022. if (h->ms.unfolded)
  1023. hists->nr_entries += h->nr_rows;
  1024. h->row_offset = 0;
  1025. hists->stats.total_period += h->period;
  1026. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  1027. hists__calc_col_len(hists, h);
  1028. }
  1029. static bool hists__filter_entry_by_dso(struct hists *hists,
  1030. struct hist_entry *he)
  1031. {
  1032. if (hists->dso_filter != NULL &&
  1033. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  1034. he->filtered |= (1 << HIST_FILTER__DSO);
  1035. return true;
  1036. }
  1037. return false;
  1038. }
  1039. void hists__filter_by_dso(struct hists *hists)
  1040. {
  1041. struct rb_node *nd;
  1042. hists->nr_entries = hists->stats.total_period = 0;
  1043. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1044. hists__reset_col_len(hists);
  1045. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1046. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1047. if (symbol_conf.exclude_other && !h->parent)
  1048. continue;
  1049. if (hists__filter_entry_by_dso(hists, h))
  1050. continue;
  1051. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  1052. }
  1053. }
  1054. static bool hists__filter_entry_by_thread(struct hists *hists,
  1055. struct hist_entry *he)
  1056. {
  1057. if (hists->thread_filter != NULL &&
  1058. he->thread != hists->thread_filter) {
  1059. he->filtered |= (1 << HIST_FILTER__THREAD);
  1060. return true;
  1061. }
  1062. return false;
  1063. }
  1064. void hists__filter_by_thread(struct hists *hists)
  1065. {
  1066. struct rb_node *nd;
  1067. hists->nr_entries = hists->stats.total_period = 0;
  1068. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1069. hists__reset_col_len(hists);
  1070. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1071. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1072. if (hists__filter_entry_by_thread(hists, h))
  1073. continue;
  1074. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  1075. }
  1076. }
  1077. static bool hists__filter_entry_by_symbol(struct hists *hists,
  1078. struct hist_entry *he)
  1079. {
  1080. if (hists->symbol_filter_str != NULL &&
  1081. (!he->ms.sym || strstr(he->ms.sym->name,
  1082. hists->symbol_filter_str) == NULL)) {
  1083. he->filtered |= (1 << HIST_FILTER__SYMBOL);
  1084. return true;
  1085. }
  1086. return false;
  1087. }
  1088. void hists__filter_by_symbol(struct hists *hists)
  1089. {
  1090. struct rb_node *nd;
  1091. hists->nr_entries = hists->stats.total_period = 0;
  1092. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1093. hists__reset_col_len(hists);
  1094. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1095. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1096. if (hists__filter_entry_by_symbol(hists, h))
  1097. continue;
  1098. hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
  1099. }
  1100. }
  1101. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  1102. {
  1103. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  1104. }
  1105. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  1106. {
  1107. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  1108. }
  1109. void hists__inc_nr_events(struct hists *hists, u32 type)
  1110. {
  1111. ++hists->stats.nr_events[0];
  1112. ++hists->stats.nr_events[type];
  1113. }
  1114. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  1115. {
  1116. int i;
  1117. size_t ret = 0;
  1118. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  1119. const char *name;
  1120. if (hists->stats.nr_events[i] == 0)
  1121. continue;
  1122. name = perf_event__name(i);
  1123. if (!strcmp(name, "UNKNOWN"))
  1124. continue;
  1125. ret += fprintf(fp, "%16s events: %10d\n", name,
  1126. hists->stats.nr_events[i]);
  1127. }
  1128. return ret;
  1129. }