hist.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. enum hist_filter {
  9. HIST_FILTER__DSO,
  10. HIST_FILTER__THREAD,
  11. HIST_FILTER__PARENT,
  12. };
  13. struct callchain_param callchain_param = {
  14. .mode = CHAIN_GRAPH_REL,
  15. .min_percent = 0.5,
  16. .order = ORDER_CALLEE
  17. };
  18. u16 hists__col_len(struct hists *hists, enum hist_column col)
  19. {
  20. return hists->col_len[col];
  21. }
  22. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
  23. {
  24. hists->col_len[col] = len;
  25. }
  26. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
  27. {
  28. if (len > hists__col_len(hists, col)) {
  29. hists__set_col_len(hists, col, len);
  30. return true;
  31. }
  32. return false;
  33. }
  34. static void hists__reset_col_len(struct hists *hists)
  35. {
  36. enum hist_column col;
  37. for (col = 0; col < HISTC_NR_COLS; ++col)
  38. hists__set_col_len(hists, col, 0);
  39. }
  40. static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
  41. {
  42. u16 len;
  43. if (h->ms.sym)
  44. hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen);
  45. else {
  46. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  47. if (hists__col_len(hists, HISTC_DSO) < unresolved_col_width &&
  48. !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  49. !symbol_conf.dso_list)
  50. hists__set_col_len(hists, HISTC_DSO,
  51. unresolved_col_width);
  52. }
  53. len = thread__comm_len(h->thread);
  54. if (hists__new_col_len(hists, HISTC_COMM, len))
  55. hists__set_col_len(hists, HISTC_THREAD, len + 6);
  56. if (h->ms.map) {
  57. len = dso__name_len(h->ms.map->dso);
  58. hists__new_col_len(hists, HISTC_DSO, len);
  59. }
  60. }
  61. static void hist_entry__add_cpumode_period(struct hist_entry *self,
  62. unsigned int cpumode, u64 period)
  63. {
  64. switch (cpumode) {
  65. case PERF_RECORD_MISC_KERNEL:
  66. self->period_sys += period;
  67. break;
  68. case PERF_RECORD_MISC_USER:
  69. self->period_us += period;
  70. break;
  71. case PERF_RECORD_MISC_GUEST_KERNEL:
  72. self->period_guest_sys += period;
  73. break;
  74. case PERF_RECORD_MISC_GUEST_USER:
  75. self->period_guest_us += period;
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. /*
  82. * histogram, sorted on item, collects periods
  83. */
  84. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  85. {
  86. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  87. struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
  88. if (self != NULL) {
  89. *self = *template;
  90. self->nr_events = 1;
  91. if (self->ms.map)
  92. self->ms.map->referenced = true;
  93. if (symbol_conf.use_callchain)
  94. callchain_init(self->callchain);
  95. }
  96. return self;
  97. }
  98. static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  99. {
  100. if (!h->filtered) {
  101. hists__calc_col_len(hists, h);
  102. ++hists->nr_entries;
  103. }
  104. }
  105. static u8 symbol__parent_filter(const struct symbol *parent)
  106. {
  107. if (symbol_conf.exclude_other && parent == NULL)
  108. return 1 << HIST_FILTER__PARENT;
  109. return 0;
  110. }
  111. struct hist_entry *__hists__add_entry(struct hists *hists,
  112. struct addr_location *al,
  113. struct symbol *sym_parent, u64 period)
  114. {
  115. struct rb_node **p = &hists->entries.rb_node;
  116. struct rb_node *parent = NULL;
  117. struct hist_entry *he;
  118. struct hist_entry entry = {
  119. .thread = al->thread,
  120. .ms = {
  121. .map = al->map,
  122. .sym = al->sym,
  123. },
  124. .cpu = al->cpu,
  125. .ip = al->addr,
  126. .level = al->level,
  127. .period = period,
  128. .parent = sym_parent,
  129. .filtered = symbol__parent_filter(sym_parent),
  130. };
  131. int cmp;
  132. while (*p != NULL) {
  133. parent = *p;
  134. he = rb_entry(parent, struct hist_entry, rb_node);
  135. cmp = hist_entry__cmp(&entry, he);
  136. if (!cmp) {
  137. he->period += period;
  138. ++he->nr_events;
  139. goto out;
  140. }
  141. if (cmp < 0)
  142. p = &(*p)->rb_left;
  143. else
  144. p = &(*p)->rb_right;
  145. }
  146. he = hist_entry__new(&entry);
  147. if (!he)
  148. return NULL;
  149. rb_link_node(&he->rb_node, parent, p);
  150. rb_insert_color(&he->rb_node, &hists->entries);
  151. hists__inc_nr_entries(hists, he);
  152. out:
  153. hist_entry__add_cpumode_period(he, al->cpumode, period);
  154. return he;
  155. }
  156. int64_t
  157. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  158. {
  159. struct sort_entry *se;
  160. int64_t cmp = 0;
  161. list_for_each_entry(se, &hist_entry__sort_list, list) {
  162. cmp = se->se_cmp(left, right);
  163. if (cmp)
  164. break;
  165. }
  166. return cmp;
  167. }
  168. int64_t
  169. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  170. {
  171. struct sort_entry *se;
  172. int64_t cmp = 0;
  173. list_for_each_entry(se, &hist_entry__sort_list, list) {
  174. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  175. f = se->se_collapse ?: se->se_cmp;
  176. cmp = f(left, right);
  177. if (cmp)
  178. break;
  179. }
  180. return cmp;
  181. }
  182. void hist_entry__free(struct hist_entry *he)
  183. {
  184. free(he);
  185. }
  186. /*
  187. * collapse the histogram
  188. */
  189. static bool hists__collapse_insert_entry(struct hists *hists,
  190. struct rb_root *root,
  191. struct hist_entry *he)
  192. {
  193. struct rb_node **p = &root->rb_node;
  194. struct rb_node *parent = NULL;
  195. struct hist_entry *iter;
  196. int64_t cmp;
  197. while (*p != NULL) {
  198. parent = *p;
  199. iter = rb_entry(parent, struct hist_entry, rb_node);
  200. cmp = hist_entry__collapse(iter, he);
  201. if (!cmp) {
  202. iter->period += he->period;
  203. if (symbol_conf.use_callchain) {
  204. callchain_cursor_reset(&hists->callchain_cursor);
  205. callchain_merge(&hists->callchain_cursor, iter->callchain,
  206. he->callchain);
  207. }
  208. hist_entry__free(he);
  209. return false;
  210. }
  211. if (cmp < 0)
  212. p = &(*p)->rb_left;
  213. else
  214. p = &(*p)->rb_right;
  215. }
  216. rb_link_node(&he->rb_node, parent, p);
  217. rb_insert_color(&he->rb_node, root);
  218. return true;
  219. }
  220. void hists__collapse_resort(struct hists *hists)
  221. {
  222. struct rb_root tmp;
  223. struct rb_node *next;
  224. struct hist_entry *n;
  225. if (!sort__need_collapse)
  226. return;
  227. tmp = RB_ROOT;
  228. next = rb_first(&hists->entries);
  229. hists->nr_entries = 0;
  230. hists__reset_col_len(hists);
  231. while (next) {
  232. n = rb_entry(next, struct hist_entry, rb_node);
  233. next = rb_next(&n->rb_node);
  234. rb_erase(&n->rb_node, &hists->entries);
  235. if (hists__collapse_insert_entry(hists, &tmp, n))
  236. hists__inc_nr_entries(hists, n);
  237. }
  238. hists->entries = tmp;
  239. }
  240. /*
  241. * reverse the map, sort on period.
  242. */
  243. static void __hists__insert_output_entry(struct rb_root *entries,
  244. struct hist_entry *he,
  245. u64 min_callchain_hits)
  246. {
  247. struct rb_node **p = &entries->rb_node;
  248. struct rb_node *parent = NULL;
  249. struct hist_entry *iter;
  250. if (symbol_conf.use_callchain)
  251. callchain_param.sort(&he->sorted_chain, he->callchain,
  252. min_callchain_hits, &callchain_param);
  253. while (*p != NULL) {
  254. parent = *p;
  255. iter = rb_entry(parent, struct hist_entry, rb_node);
  256. if (he->period > iter->period)
  257. p = &(*p)->rb_left;
  258. else
  259. p = &(*p)->rb_right;
  260. }
  261. rb_link_node(&he->rb_node, parent, p);
  262. rb_insert_color(&he->rb_node, entries);
  263. }
  264. void hists__output_resort(struct hists *hists)
  265. {
  266. struct rb_root tmp;
  267. struct rb_node *next;
  268. struct hist_entry *n;
  269. u64 min_callchain_hits;
  270. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  271. tmp = RB_ROOT;
  272. next = rb_first(&hists->entries);
  273. hists->nr_entries = 0;
  274. hists__reset_col_len(hists);
  275. while (next) {
  276. n = rb_entry(next, struct hist_entry, rb_node);
  277. next = rb_next(&n->rb_node);
  278. rb_erase(&n->rb_node, &hists->entries);
  279. __hists__insert_output_entry(&tmp, n, min_callchain_hits);
  280. hists__inc_nr_entries(hists, n);
  281. }
  282. hists->entries = tmp;
  283. }
  284. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  285. {
  286. int i;
  287. int ret = fprintf(fp, " ");
  288. for (i = 0; i < left_margin; i++)
  289. ret += fprintf(fp, " ");
  290. return ret;
  291. }
  292. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  293. int left_margin)
  294. {
  295. int i;
  296. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  297. for (i = 0; i < depth; i++)
  298. if (depth_mask & (1 << i))
  299. ret += fprintf(fp, "| ");
  300. else
  301. ret += fprintf(fp, " ");
  302. ret += fprintf(fp, "\n");
  303. return ret;
  304. }
  305. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  306. int depth, int depth_mask, int period,
  307. u64 total_samples, u64 hits,
  308. int left_margin)
  309. {
  310. int i;
  311. size_t ret = 0;
  312. ret += callchain__fprintf_left_margin(fp, left_margin);
  313. for (i = 0; i < depth; i++) {
  314. if (depth_mask & (1 << i))
  315. ret += fprintf(fp, "|");
  316. else
  317. ret += fprintf(fp, " ");
  318. if (!period && i == depth - 1) {
  319. double percent;
  320. percent = hits * 100.0 / total_samples;
  321. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  322. } else
  323. ret += fprintf(fp, "%s", " ");
  324. }
  325. if (chain->ms.sym)
  326. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  327. else
  328. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  329. return ret;
  330. }
  331. static struct symbol *rem_sq_bracket;
  332. static struct callchain_list rem_hits;
  333. static void init_rem_hits(void)
  334. {
  335. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  336. if (!rem_sq_bracket) {
  337. fprintf(stderr, "Not enough memory to display remaining hits\n");
  338. return;
  339. }
  340. strcpy(rem_sq_bracket->name, "[...]");
  341. rem_hits.ms.sym = rem_sq_bracket;
  342. }
  343. static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  344. u64 total_samples, int depth,
  345. int depth_mask, int left_margin)
  346. {
  347. struct rb_node *node, *next;
  348. struct callchain_node *child;
  349. struct callchain_list *chain;
  350. int new_depth_mask = depth_mask;
  351. u64 new_total;
  352. u64 remaining;
  353. size_t ret = 0;
  354. int i;
  355. uint entries_printed = 0;
  356. if (callchain_param.mode == CHAIN_GRAPH_REL)
  357. new_total = self->children_hit;
  358. else
  359. new_total = total_samples;
  360. remaining = new_total;
  361. node = rb_first(&self->rb_root);
  362. while (node) {
  363. u64 cumul;
  364. child = rb_entry(node, struct callchain_node, rb_node);
  365. cumul = callchain_cumul_hits(child);
  366. remaining -= cumul;
  367. /*
  368. * The depth mask manages the output of pipes that show
  369. * the depth. We don't want to keep the pipes of the current
  370. * level for the last child of this depth.
  371. * Except if we have remaining filtered hits. They will
  372. * supersede the last child
  373. */
  374. next = rb_next(node);
  375. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  376. new_depth_mask &= ~(1 << (depth - 1));
  377. /*
  378. * But we keep the older depth mask for the line separator
  379. * to keep the level link until we reach the last child
  380. */
  381. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  382. left_margin);
  383. i = 0;
  384. list_for_each_entry(chain, &child->val, list) {
  385. ret += ipchain__fprintf_graph(fp, chain, depth,
  386. new_depth_mask, i++,
  387. new_total,
  388. cumul,
  389. left_margin);
  390. }
  391. ret += __callchain__fprintf_graph(fp, child, new_total,
  392. depth + 1,
  393. new_depth_mask | (1 << depth),
  394. left_margin);
  395. node = next;
  396. if (++entries_printed == callchain_param.print_limit)
  397. break;
  398. }
  399. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  400. remaining && remaining != new_total) {
  401. if (!rem_sq_bracket)
  402. return ret;
  403. new_depth_mask &= ~(1 << (depth - 1));
  404. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  405. new_depth_mask, 0, new_total,
  406. remaining, left_margin);
  407. }
  408. return ret;
  409. }
  410. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  411. u64 total_samples, int left_margin)
  412. {
  413. struct callchain_list *chain;
  414. bool printed = false;
  415. int i = 0;
  416. int ret = 0;
  417. u32 entries_printed = 0;
  418. list_for_each_entry(chain, &self->val, list) {
  419. if (!i++ && sort__first_dimension == SORT_SYM)
  420. continue;
  421. if (!printed) {
  422. ret += callchain__fprintf_left_margin(fp, left_margin);
  423. ret += fprintf(fp, "|\n");
  424. ret += callchain__fprintf_left_margin(fp, left_margin);
  425. ret += fprintf(fp, "---");
  426. left_margin += 3;
  427. printed = true;
  428. } else
  429. ret += callchain__fprintf_left_margin(fp, left_margin);
  430. if (chain->ms.sym)
  431. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  432. else
  433. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  434. if (++entries_printed == callchain_param.print_limit)
  435. break;
  436. }
  437. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  438. return ret;
  439. }
  440. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  441. u64 total_samples)
  442. {
  443. struct callchain_list *chain;
  444. size_t ret = 0;
  445. if (!self)
  446. return 0;
  447. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  448. list_for_each_entry(chain, &self->val, list) {
  449. if (chain->ip >= PERF_CONTEXT_MAX)
  450. continue;
  451. if (chain->ms.sym)
  452. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  453. else
  454. ret += fprintf(fp, " %p\n",
  455. (void *)(long)chain->ip);
  456. }
  457. return ret;
  458. }
  459. static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  460. u64 total_samples, int left_margin)
  461. {
  462. struct rb_node *rb_node;
  463. struct callchain_node *chain;
  464. size_t ret = 0;
  465. u32 entries_printed = 0;
  466. rb_node = rb_first(&self->sorted_chain);
  467. while (rb_node) {
  468. double percent;
  469. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  470. percent = chain->hit * 100.0 / total_samples;
  471. switch (callchain_param.mode) {
  472. case CHAIN_FLAT:
  473. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  474. percent);
  475. ret += callchain__fprintf_flat(fp, chain, total_samples);
  476. break;
  477. case CHAIN_GRAPH_ABS: /* Falldown */
  478. case CHAIN_GRAPH_REL:
  479. ret += callchain__fprintf_graph(fp, chain, total_samples,
  480. left_margin);
  481. case CHAIN_NONE:
  482. default:
  483. break;
  484. }
  485. ret += fprintf(fp, "\n");
  486. if (++entries_printed == callchain_param.print_limit)
  487. break;
  488. rb_node = rb_next(rb_node);
  489. }
  490. return ret;
  491. }
  492. int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
  493. struct hists *hists, struct hists *pair_hists,
  494. bool show_displacement, long displacement,
  495. bool color, u64 session_total)
  496. {
  497. struct sort_entry *se;
  498. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  499. u64 nr_events;
  500. const char *sep = symbol_conf.field_sep;
  501. int ret;
  502. if (symbol_conf.exclude_other && !self->parent)
  503. return 0;
  504. if (pair_hists) {
  505. period = self->pair ? self->pair->period : 0;
  506. nr_events = self->pair ? self->pair->nr_events : 0;
  507. total = pair_hists->stats.total_period;
  508. period_sys = self->pair ? self->pair->period_sys : 0;
  509. period_us = self->pair ? self->pair->period_us : 0;
  510. period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
  511. period_guest_us = self->pair ? self->pair->period_guest_us : 0;
  512. } else {
  513. period = self->period;
  514. nr_events = self->nr_events;
  515. total = session_total;
  516. period_sys = self->period_sys;
  517. period_us = self->period_us;
  518. period_guest_sys = self->period_guest_sys;
  519. period_guest_us = self->period_guest_us;
  520. }
  521. if (total) {
  522. if (color)
  523. ret = percent_color_snprintf(s, size,
  524. sep ? "%.2f" : " %6.2f%%",
  525. (period * 100.0) / total);
  526. else
  527. ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  528. (period * 100.0) / total);
  529. if (symbol_conf.show_cpu_utilization) {
  530. ret += percent_color_snprintf(s + ret, size - ret,
  531. sep ? "%.2f" : " %6.2f%%",
  532. (period_sys * 100.0) / total);
  533. ret += percent_color_snprintf(s + ret, size - ret,
  534. sep ? "%.2f" : " %6.2f%%",
  535. (period_us * 100.0) / total);
  536. if (perf_guest) {
  537. ret += percent_color_snprintf(s + ret,
  538. size - ret,
  539. sep ? "%.2f" : " %6.2f%%",
  540. (period_guest_sys * 100.0) /
  541. total);
  542. ret += percent_color_snprintf(s + ret,
  543. size - ret,
  544. sep ? "%.2f" : " %6.2f%%",
  545. (period_guest_us * 100.0) /
  546. total);
  547. }
  548. }
  549. } else
  550. ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  551. if (symbol_conf.show_nr_samples) {
  552. if (sep)
  553. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  554. else
  555. ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  556. }
  557. if (symbol_conf.show_total_period) {
  558. if (sep)
  559. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  560. else
  561. ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
  562. }
  563. if (pair_hists) {
  564. char bf[32];
  565. double old_percent = 0, new_percent = 0, diff;
  566. if (total > 0)
  567. old_percent = (period * 100.0) / total;
  568. if (session_total > 0)
  569. new_percent = (self->period * 100.0) / session_total;
  570. diff = new_percent - old_percent;
  571. if (fabs(diff) >= 0.01)
  572. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  573. else
  574. snprintf(bf, sizeof(bf), " ");
  575. if (sep)
  576. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  577. else
  578. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  579. if (show_displacement) {
  580. if (displacement)
  581. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  582. else
  583. snprintf(bf, sizeof(bf), " ");
  584. if (sep)
  585. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  586. else
  587. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  588. }
  589. }
  590. list_for_each_entry(se, &hist_entry__sort_list, list) {
  591. if (se->elide)
  592. continue;
  593. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  594. ret += se->se_snprintf(self, s + ret, size - ret,
  595. hists__col_len(hists, se->se_width_idx));
  596. }
  597. return ret;
  598. }
  599. int hist_entry__fprintf(struct hist_entry *he, size_t size, struct hists *hists,
  600. struct hists *pair_hists, bool show_displacement,
  601. long displacement, FILE *fp, u64 session_total)
  602. {
  603. char bf[512];
  604. if (size == 0 || size > sizeof(bf))
  605. size = sizeof(bf);
  606. hist_entry__snprintf(he, bf, size, hists, pair_hists,
  607. show_displacement, displacement,
  608. true, session_total);
  609. return fprintf(fp, "%s\n", bf);
  610. }
  611. static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
  612. struct hists *hists, FILE *fp,
  613. u64 session_total)
  614. {
  615. int left_margin = 0;
  616. if (sort__first_dimension == SORT_COMM) {
  617. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  618. typeof(*se), list);
  619. left_margin = hists__col_len(hists, se->se_width_idx);
  620. left_margin -= thread__comm_len(self->thread);
  621. }
  622. return hist_entry_callchain__fprintf(fp, self, session_total,
  623. left_margin);
  624. }
  625. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  626. bool show_displacement, bool show_header, int max_rows,
  627. int max_cols, FILE *fp)
  628. {
  629. struct sort_entry *se;
  630. struct rb_node *nd;
  631. size_t ret = 0;
  632. unsigned long position = 1;
  633. long displacement = 0;
  634. unsigned int width;
  635. const char *sep = symbol_conf.field_sep;
  636. const char *col_width = symbol_conf.col_width_list_str;
  637. int nr_rows = 0;
  638. init_rem_hits();
  639. if (!show_header)
  640. goto print_entries;
  641. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  642. if (symbol_conf.show_nr_samples) {
  643. if (sep)
  644. fprintf(fp, "%cSamples", *sep);
  645. else
  646. fputs(" Samples ", fp);
  647. }
  648. if (symbol_conf.show_total_period) {
  649. if (sep)
  650. ret += fprintf(fp, "%cPeriod", *sep);
  651. else
  652. ret += fprintf(fp, " Period ");
  653. }
  654. if (symbol_conf.show_cpu_utilization) {
  655. if (sep) {
  656. ret += fprintf(fp, "%csys", *sep);
  657. ret += fprintf(fp, "%cus", *sep);
  658. if (perf_guest) {
  659. ret += fprintf(fp, "%cguest sys", *sep);
  660. ret += fprintf(fp, "%cguest us", *sep);
  661. }
  662. } else {
  663. ret += fprintf(fp, " sys ");
  664. ret += fprintf(fp, " us ");
  665. if (perf_guest) {
  666. ret += fprintf(fp, " guest sys ");
  667. ret += fprintf(fp, " guest us ");
  668. }
  669. }
  670. }
  671. if (pair) {
  672. if (sep)
  673. ret += fprintf(fp, "%cDelta", *sep);
  674. else
  675. ret += fprintf(fp, " Delta ");
  676. if (show_displacement) {
  677. if (sep)
  678. ret += fprintf(fp, "%cDisplacement", *sep);
  679. else
  680. ret += fprintf(fp, " Displ");
  681. }
  682. }
  683. list_for_each_entry(se, &hist_entry__sort_list, list) {
  684. if (se->elide)
  685. continue;
  686. if (sep) {
  687. fprintf(fp, "%c%s", *sep, se->se_header);
  688. continue;
  689. }
  690. width = strlen(se->se_header);
  691. if (symbol_conf.col_width_list_str) {
  692. if (col_width) {
  693. hists__set_col_len(hists, se->se_width_idx,
  694. atoi(col_width));
  695. col_width = strchr(col_width, ',');
  696. if (col_width)
  697. ++col_width;
  698. }
  699. }
  700. if (!hists__new_col_len(hists, se->se_width_idx, width))
  701. width = hists__col_len(hists, se->se_width_idx);
  702. fprintf(fp, " %*s", width, se->se_header);
  703. }
  704. fprintf(fp, "\n");
  705. if (max_rows && ++nr_rows >= max_rows)
  706. goto out;
  707. if (sep)
  708. goto print_entries;
  709. fprintf(fp, "# ........");
  710. if (symbol_conf.show_nr_samples)
  711. fprintf(fp, " ..........");
  712. if (symbol_conf.show_total_period)
  713. fprintf(fp, " ............");
  714. if (pair) {
  715. fprintf(fp, " ..........");
  716. if (show_displacement)
  717. fprintf(fp, " .....");
  718. }
  719. list_for_each_entry(se, &hist_entry__sort_list, list) {
  720. unsigned int i;
  721. if (se->elide)
  722. continue;
  723. fprintf(fp, " ");
  724. width = hists__col_len(hists, se->se_width_idx);
  725. if (width == 0)
  726. width = strlen(se->se_header);
  727. for (i = 0; i < width; i++)
  728. fprintf(fp, ".");
  729. }
  730. fprintf(fp, "\n");
  731. if (max_rows && ++nr_rows >= max_rows)
  732. goto out;
  733. fprintf(fp, "#\n");
  734. if (max_rows && ++nr_rows >= max_rows)
  735. goto out;
  736. print_entries:
  737. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  738. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  739. if (h->filtered)
  740. continue;
  741. if (show_displacement) {
  742. if (h->pair != NULL)
  743. displacement = ((long)h->pair->position -
  744. (long)position);
  745. else
  746. displacement = 0;
  747. ++position;
  748. }
  749. ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
  750. displacement, fp, hists->stats.total_period);
  751. if (symbol_conf.use_callchain)
  752. ret += hist_entry__fprintf_callchain(h, hists, fp,
  753. hists->stats.total_period);
  754. if (max_rows && ++nr_rows >= max_rows)
  755. goto out;
  756. if (h->ms.map == NULL && verbose > 1) {
  757. __map_groups__fprintf_maps(&h->thread->mg,
  758. MAP__FUNCTION, verbose, fp);
  759. fprintf(fp, "%.10s end\n", graph_dotted_line);
  760. }
  761. }
  762. out:
  763. free(rem_sq_bracket);
  764. return ret;
  765. }
  766. /*
  767. * See hists__fprintf to match the column widths
  768. */
  769. unsigned int hists__sort_list_width(struct hists *hists)
  770. {
  771. struct sort_entry *se;
  772. int ret = 9; /* total % */
  773. if (symbol_conf.show_cpu_utilization) {
  774. ret += 7; /* count_sys % */
  775. ret += 6; /* count_us % */
  776. if (perf_guest) {
  777. ret += 13; /* count_guest_sys % */
  778. ret += 12; /* count_guest_us % */
  779. }
  780. }
  781. if (symbol_conf.show_nr_samples)
  782. ret += 11;
  783. if (symbol_conf.show_total_period)
  784. ret += 13;
  785. list_for_each_entry(se, &hist_entry__sort_list, list)
  786. if (!se->elide)
  787. ret += 2 + hists__col_len(hists, se->se_width_idx);
  788. if (verbose) /* Addr + origin */
  789. ret += 3 + BITS_PER_LONG / 4;
  790. return ret;
  791. }
  792. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  793. enum hist_filter filter)
  794. {
  795. h->filtered &= ~(1 << filter);
  796. if (h->filtered)
  797. return;
  798. ++hists->nr_entries;
  799. if (h->ms.unfolded)
  800. hists->nr_entries += h->nr_rows;
  801. h->row_offset = 0;
  802. hists->stats.total_period += h->period;
  803. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  804. hists__calc_col_len(hists, h);
  805. }
  806. void hists__filter_by_dso(struct hists *hists, const struct dso *dso)
  807. {
  808. struct rb_node *nd;
  809. hists->nr_entries = hists->stats.total_period = 0;
  810. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  811. hists__reset_col_len(hists);
  812. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  813. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  814. if (symbol_conf.exclude_other && !h->parent)
  815. continue;
  816. if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
  817. h->filtered |= (1 << HIST_FILTER__DSO);
  818. continue;
  819. }
  820. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  821. }
  822. }
  823. void hists__filter_by_thread(struct hists *hists, const struct thread *thread)
  824. {
  825. struct rb_node *nd;
  826. hists->nr_entries = hists->stats.total_period = 0;
  827. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  828. hists__reset_col_len(hists);
  829. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  830. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  831. if (thread != NULL && h->thread != thread) {
  832. h->filtered |= (1 << HIST_FILTER__THREAD);
  833. continue;
  834. }
  835. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  836. }
  837. }
  838. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  839. {
  840. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  841. }
  842. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  843. {
  844. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  845. }
  846. void hists__inc_nr_events(struct hists *hists, u32 type)
  847. {
  848. ++hists->stats.nr_events[0];
  849. ++hists->stats.nr_events[type];
  850. }
  851. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  852. {
  853. int i;
  854. size_t ret = 0;
  855. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  856. const char *name;
  857. if (hists->stats.nr_events[i] == 0)
  858. continue;
  859. name = perf_event__name(i);
  860. if (!strcmp(name, "UNKNOWN"))
  861. continue;
  862. ret += fprintf(fp, "%16s events: %10d\n", name,
  863. hists->stats.nr_events[i]);
  864. }
  865. return ret;
  866. }