hist.c 23 KB

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