hist.c 23 KB

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