hist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 *self, enum hist_column col)
  19. {
  20. return self->col_len[col];
  21. }
  22. void hists__set_col_len(struct hists *self, enum hist_column col, u16 len)
  23. {
  24. self->col_len[col] = len;
  25. }
  26. bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len)
  27. {
  28. if (len > hists__col_len(self, col)) {
  29. hists__set_col_len(self, col, len);
  30. return true;
  31. }
  32. return false;
  33. }
  34. static void hists__reset_col_len(struct hists *self)
  35. {
  36. enum hist_column col;
  37. for (col = 0; col < HISTC_NR_COLS; ++col)
  38. hists__set_col_len(self, col, 0);
  39. }
  40. static void hists__calc_col_len(struct hists *self, struct hist_entry *h)
  41. {
  42. u16 len;
  43. if (h->ms.sym)
  44. hists__new_col_len(self, HISTC_SYMBOL, h->ms.sym->namelen);
  45. else {
  46. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  47. if (hists__col_len(self, 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(self, HISTC_DSO,
  51. unresolved_col_width);
  52. }
  53. len = thread__comm_len(h->thread);
  54. if (hists__new_col_len(self, HISTC_COMM, len))
  55. hists__set_col_len(self, HISTC_THREAD, len + 6);
  56. if (h->ms.map) {
  57. len = dso__name_len(h->ms.map->dso);
  58. hists__new_col_len(self, 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 *self, struct hist_entry *h)
  99. {
  100. if (!h->filtered) {
  101. hists__calc_col_len(self, h);
  102. ++self->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 *self,
  112. struct addr_location *al,
  113. struct symbol *sym_parent, u64 period)
  114. {
  115. struct rb_node **p = &self->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, &self->entries);
  151. hists__inc_nr_entries(self, 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 *self,
  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(&self->callchain_cursor);
  205. callchain_merge(&self->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 *self)
  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(&self->entries);
  229. self->nr_entries = 0;
  230. hists__reset_col_len(self);
  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, &self->entries);
  235. if (hists__collapse_insert_entry(self, &tmp, n))
  236. hists__inc_nr_entries(self, n);
  237. }
  238. self->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 *self)
  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 = self->stats.total_period * (callchain_param.min_percent / 100);
  271. tmp = RB_ROOT;
  272. next = rb_first(&self->entries);
  273. self->nr_entries = 0;
  274. hists__reset_col_len(self);
  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, &self->entries);
  279. __hists__insert_output_entry(&tmp, n, min_callchain_hits);
  280. hists__inc_nr_entries(self, n);
  281. }
  282. self->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 (pair_hists) {
  558. char bf[32];
  559. double old_percent = 0, new_percent = 0, diff;
  560. if (total > 0)
  561. old_percent = (period * 100.0) / total;
  562. if (session_total > 0)
  563. new_percent = (self->period * 100.0) / session_total;
  564. diff = new_percent - old_percent;
  565. if (fabs(diff) >= 0.01)
  566. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  567. else
  568. snprintf(bf, sizeof(bf), " ");
  569. if (sep)
  570. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  571. else
  572. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  573. if (show_displacement) {
  574. if (displacement)
  575. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  576. else
  577. snprintf(bf, sizeof(bf), " ");
  578. if (sep)
  579. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  580. else
  581. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  582. }
  583. }
  584. list_for_each_entry(se, &hist_entry__sort_list, list) {
  585. if (se->elide)
  586. continue;
  587. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  588. ret += se->se_snprintf(self, s + ret, size - ret,
  589. hists__col_len(hists, se->se_width_idx));
  590. }
  591. return ret;
  592. }
  593. int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
  594. struct hists *pair_hists, bool show_displacement,
  595. long displacement, FILE *fp, u64 session_total)
  596. {
  597. char bf[512];
  598. hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
  599. show_displacement, displacement,
  600. true, session_total);
  601. return fprintf(fp, "%s\n", bf);
  602. }
  603. static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
  604. struct hists *hists, FILE *fp,
  605. u64 session_total)
  606. {
  607. int left_margin = 0;
  608. if (sort__first_dimension == SORT_COMM) {
  609. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  610. typeof(*se), list);
  611. left_margin = hists__col_len(hists, se->se_width_idx);
  612. left_margin -= thread__comm_len(self->thread);
  613. }
  614. return hist_entry_callchain__fprintf(fp, self, session_total,
  615. left_margin);
  616. }
  617. size_t hists__fprintf(struct hists *self, struct hists *pair,
  618. bool show_displacement, FILE *fp)
  619. {
  620. struct sort_entry *se;
  621. struct rb_node *nd;
  622. size_t ret = 0;
  623. unsigned long position = 1;
  624. long displacement = 0;
  625. unsigned int width;
  626. const char *sep = symbol_conf.field_sep;
  627. const char *col_width = symbol_conf.col_width_list_str;
  628. init_rem_hits();
  629. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  630. if (symbol_conf.show_nr_samples) {
  631. if (sep)
  632. fprintf(fp, "%cSamples", *sep);
  633. else
  634. fputs(" Samples ", fp);
  635. }
  636. if (symbol_conf.show_cpu_utilization) {
  637. if (sep) {
  638. ret += fprintf(fp, "%csys", *sep);
  639. ret += fprintf(fp, "%cus", *sep);
  640. if (perf_guest) {
  641. ret += fprintf(fp, "%cguest sys", *sep);
  642. ret += fprintf(fp, "%cguest us", *sep);
  643. }
  644. } else {
  645. ret += fprintf(fp, " sys ");
  646. ret += fprintf(fp, " us ");
  647. if (perf_guest) {
  648. ret += fprintf(fp, " guest sys ");
  649. ret += fprintf(fp, " guest us ");
  650. }
  651. }
  652. }
  653. if (pair) {
  654. if (sep)
  655. ret += fprintf(fp, "%cDelta", *sep);
  656. else
  657. ret += fprintf(fp, " Delta ");
  658. if (show_displacement) {
  659. if (sep)
  660. ret += fprintf(fp, "%cDisplacement", *sep);
  661. else
  662. ret += fprintf(fp, " Displ");
  663. }
  664. }
  665. list_for_each_entry(se, &hist_entry__sort_list, list) {
  666. if (se->elide)
  667. continue;
  668. if (sep) {
  669. fprintf(fp, "%c%s", *sep, se->se_header);
  670. continue;
  671. }
  672. width = strlen(se->se_header);
  673. if (symbol_conf.col_width_list_str) {
  674. if (col_width) {
  675. hists__set_col_len(self, se->se_width_idx,
  676. atoi(col_width));
  677. col_width = strchr(col_width, ',');
  678. if (col_width)
  679. ++col_width;
  680. }
  681. }
  682. if (!hists__new_col_len(self, se->se_width_idx, width))
  683. width = hists__col_len(self, se->se_width_idx);
  684. fprintf(fp, " %*s", width, se->se_header);
  685. }
  686. fprintf(fp, "\n");
  687. if (sep)
  688. goto print_entries;
  689. fprintf(fp, "# ........");
  690. if (symbol_conf.show_nr_samples)
  691. fprintf(fp, " ..........");
  692. if (pair) {
  693. fprintf(fp, " ..........");
  694. if (show_displacement)
  695. fprintf(fp, " .....");
  696. }
  697. list_for_each_entry(se, &hist_entry__sort_list, list) {
  698. unsigned int i;
  699. if (se->elide)
  700. continue;
  701. fprintf(fp, " ");
  702. width = hists__col_len(self, se->se_width_idx);
  703. if (width == 0)
  704. width = strlen(se->se_header);
  705. for (i = 0; i < width; i++)
  706. fprintf(fp, ".");
  707. }
  708. fprintf(fp, "\n#\n");
  709. print_entries:
  710. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  711. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  712. if (h->filtered)
  713. continue;
  714. if (show_displacement) {
  715. if (h->pair != NULL)
  716. displacement = ((long)h->pair->position -
  717. (long)position);
  718. else
  719. displacement = 0;
  720. ++position;
  721. }
  722. ret += hist_entry__fprintf(h, self, pair, show_displacement,
  723. displacement, fp, self->stats.total_period);
  724. if (symbol_conf.use_callchain)
  725. ret += hist_entry__fprintf_callchain(h, self, fp,
  726. self->stats.total_period);
  727. if (h->ms.map == NULL && verbose > 1) {
  728. __map_groups__fprintf_maps(&h->thread->mg,
  729. MAP__FUNCTION, verbose, fp);
  730. fprintf(fp, "%.10s end\n", graph_dotted_line);
  731. }
  732. }
  733. free(rem_sq_bracket);
  734. return ret;
  735. }
  736. /*
  737. * See hists__fprintf to match the column widths
  738. */
  739. unsigned int hists__sort_list_width(struct hists *self)
  740. {
  741. struct sort_entry *se;
  742. int ret = 9; /* total % */
  743. if (symbol_conf.show_cpu_utilization) {
  744. ret += 7; /* count_sys % */
  745. ret += 6; /* count_us % */
  746. if (perf_guest) {
  747. ret += 13; /* count_guest_sys % */
  748. ret += 12; /* count_guest_us % */
  749. }
  750. }
  751. if (symbol_conf.show_nr_samples)
  752. ret += 11;
  753. list_for_each_entry(se, &hist_entry__sort_list, list)
  754. if (!se->elide)
  755. ret += 2 + hists__col_len(self, se->se_width_idx);
  756. if (verbose) /* Addr + origin */
  757. ret += 3 + BITS_PER_LONG / 4;
  758. return ret;
  759. }
  760. static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
  761. enum hist_filter filter)
  762. {
  763. h->filtered &= ~(1 << filter);
  764. if (h->filtered)
  765. return;
  766. ++self->nr_entries;
  767. if (h->ms.unfolded)
  768. self->nr_entries += h->nr_rows;
  769. h->row_offset = 0;
  770. self->stats.total_period += h->period;
  771. self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  772. hists__calc_col_len(self, h);
  773. }
  774. void hists__filter_by_dso(struct hists *self, const struct dso *dso)
  775. {
  776. struct rb_node *nd;
  777. self->nr_entries = self->stats.total_period = 0;
  778. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  779. hists__reset_col_len(self);
  780. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  781. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  782. if (symbol_conf.exclude_other && !h->parent)
  783. continue;
  784. if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
  785. h->filtered |= (1 << HIST_FILTER__DSO);
  786. continue;
  787. }
  788. hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
  789. }
  790. }
  791. void hists__filter_by_thread(struct hists *self, const struct thread *thread)
  792. {
  793. struct rb_node *nd;
  794. self->nr_entries = self->stats.total_period = 0;
  795. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  796. hists__reset_col_len(self);
  797. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  798. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  799. if (thread != NULL && h->thread != thread) {
  800. h->filtered |= (1 << HIST_FILTER__THREAD);
  801. continue;
  802. }
  803. hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
  804. }
  805. }
  806. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  807. {
  808. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  809. }
  810. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  811. {
  812. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  813. }
  814. void hists__inc_nr_events(struct hists *self, u32 type)
  815. {
  816. ++self->stats.nr_events[0];
  817. ++self->stats.nr_events[type];
  818. }
  819. size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
  820. {
  821. int i;
  822. size_t ret = 0;
  823. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  824. const char *name;
  825. if (self->stats.nr_events[i] == 0)
  826. continue;
  827. name = perf_event__name(i);
  828. if (!strcmp(name, "UNKNOWN"))
  829. continue;
  830. ret += fprintf(fp, "%16s events: %10d\n", name,
  831. self->stats.nr_events[i]);
  832. }
  833. return ret;
  834. }