hist.c 23 KB

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