hist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include <stdio.h>
  2. #include "../../util/util.h"
  3. #include "../../util/hist.h"
  4. #include "../../util/sort.h"
  5. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  6. {
  7. int i;
  8. int ret = fprintf(fp, " ");
  9. for (i = 0; i < left_margin; i++)
  10. ret += fprintf(fp, " ");
  11. return ret;
  12. }
  13. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  14. int left_margin)
  15. {
  16. int i;
  17. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  18. for (i = 0; i < depth; i++)
  19. if (depth_mask & (1 << i))
  20. ret += fprintf(fp, "| ");
  21. else
  22. ret += fprintf(fp, " ");
  23. ret += fprintf(fp, "\n");
  24. return ret;
  25. }
  26. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  27. int depth, int depth_mask, int period,
  28. u64 total_samples, u64 hits,
  29. int left_margin)
  30. {
  31. int i;
  32. size_t ret = 0;
  33. ret += callchain__fprintf_left_margin(fp, left_margin);
  34. for (i = 0; i < depth; i++) {
  35. if (depth_mask & (1 << i))
  36. ret += fprintf(fp, "|");
  37. else
  38. ret += fprintf(fp, " ");
  39. if (!period && i == depth - 1) {
  40. double percent;
  41. percent = hits * 100.0 / total_samples;
  42. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  43. } else
  44. ret += fprintf(fp, "%s", " ");
  45. }
  46. if (chain->ms.sym)
  47. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  48. else
  49. ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
  50. return ret;
  51. }
  52. static struct symbol *rem_sq_bracket;
  53. static struct callchain_list rem_hits;
  54. static void init_rem_hits(void)
  55. {
  56. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  57. if (!rem_sq_bracket) {
  58. fprintf(stderr, "Not enough memory to display remaining hits\n");
  59. return;
  60. }
  61. strcpy(rem_sq_bracket->name, "[...]");
  62. rem_hits.ms.sym = rem_sq_bracket;
  63. }
  64. static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  65. u64 total_samples, int depth,
  66. int depth_mask, int left_margin)
  67. {
  68. struct rb_node *node, *next;
  69. struct callchain_node *child;
  70. struct callchain_list *chain;
  71. int new_depth_mask = depth_mask;
  72. u64 remaining;
  73. size_t ret = 0;
  74. int i;
  75. uint entries_printed = 0;
  76. remaining = total_samples;
  77. node = rb_first(root);
  78. while (node) {
  79. u64 new_total;
  80. u64 cumul;
  81. child = rb_entry(node, struct callchain_node, rb_node);
  82. cumul = callchain_cumul_hits(child);
  83. remaining -= cumul;
  84. /*
  85. * The depth mask manages the output of pipes that show
  86. * the depth. We don't want to keep the pipes of the current
  87. * level for the last child of this depth.
  88. * Except if we have remaining filtered hits. They will
  89. * supersede the last child
  90. */
  91. next = rb_next(node);
  92. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  93. new_depth_mask &= ~(1 << (depth - 1));
  94. /*
  95. * But we keep the older depth mask for the line separator
  96. * to keep the level link until we reach the last child
  97. */
  98. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  99. left_margin);
  100. i = 0;
  101. list_for_each_entry(chain, &child->val, list) {
  102. ret += ipchain__fprintf_graph(fp, chain, depth,
  103. new_depth_mask, i++,
  104. total_samples,
  105. cumul,
  106. left_margin);
  107. }
  108. if (callchain_param.mode == CHAIN_GRAPH_REL)
  109. new_total = child->children_hit;
  110. else
  111. new_total = total_samples;
  112. ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
  113. depth + 1,
  114. new_depth_mask | (1 << depth),
  115. left_margin);
  116. node = next;
  117. if (++entries_printed == callchain_param.print_limit)
  118. break;
  119. }
  120. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  121. remaining && remaining != total_samples) {
  122. if (!rem_sq_bracket)
  123. return ret;
  124. new_depth_mask &= ~(1 << (depth - 1));
  125. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  126. new_depth_mask, 0, total_samples,
  127. remaining, left_margin);
  128. }
  129. return ret;
  130. }
  131. static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  132. u64 total_samples, int left_margin)
  133. {
  134. struct callchain_node *cnode;
  135. struct callchain_list *chain;
  136. u32 entries_printed = 0;
  137. bool printed = false;
  138. struct rb_node *node;
  139. int i = 0;
  140. int ret = 0;
  141. /*
  142. * If have one single callchain root, don't bother printing
  143. * its percentage (100 % in fractal mode and the same percentage
  144. * than the hist in graph mode). This also avoid one level of column.
  145. */
  146. node = rb_first(root);
  147. if (node && !rb_next(node)) {
  148. cnode = rb_entry(node, struct callchain_node, rb_node);
  149. list_for_each_entry(chain, &cnode->val, list) {
  150. /*
  151. * If we sort by symbol, the first entry is the same than
  152. * the symbol. No need to print it otherwise it appears as
  153. * displayed twice.
  154. */
  155. if (!i++ && sort__first_dimension == SORT_SYM)
  156. continue;
  157. if (!printed) {
  158. ret += callchain__fprintf_left_margin(fp, left_margin);
  159. ret += fprintf(fp, "|\n");
  160. ret += callchain__fprintf_left_margin(fp, left_margin);
  161. ret += fprintf(fp, "---");
  162. left_margin += 3;
  163. printed = true;
  164. } else
  165. ret += callchain__fprintf_left_margin(fp, left_margin);
  166. if (chain->ms.sym)
  167. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  168. else
  169. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  170. if (++entries_printed == callchain_param.print_limit)
  171. break;
  172. }
  173. root = &cnode->rb_root;
  174. }
  175. ret += __callchain__fprintf_graph(fp, root, total_samples,
  176. 1, 1, left_margin);
  177. ret += fprintf(fp, "\n");
  178. return ret;
  179. }
  180. static size_t __callchain__fprintf_flat(FILE *fp,
  181. struct callchain_node *self,
  182. u64 total_samples)
  183. {
  184. struct callchain_list *chain;
  185. size_t ret = 0;
  186. if (!self)
  187. return 0;
  188. ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
  189. list_for_each_entry(chain, &self->val, list) {
  190. if (chain->ip >= PERF_CONTEXT_MAX)
  191. continue;
  192. if (chain->ms.sym)
  193. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  194. else
  195. ret += fprintf(fp, " %p\n",
  196. (void *)(long)chain->ip);
  197. }
  198. return ret;
  199. }
  200. static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
  201. u64 total_samples)
  202. {
  203. size_t ret = 0;
  204. u32 entries_printed = 0;
  205. struct rb_node *rb_node;
  206. struct callchain_node *chain;
  207. rb_node = rb_first(self);
  208. while (rb_node) {
  209. double percent;
  210. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  211. percent = chain->hit * 100.0 / total_samples;
  212. ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
  213. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  214. ret += fprintf(fp, "\n");
  215. if (++entries_printed == callchain_param.print_limit)
  216. break;
  217. rb_node = rb_next(rb_node);
  218. }
  219. return ret;
  220. }
  221. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  222. u64 total_samples, int left_margin,
  223. FILE *fp)
  224. {
  225. switch (callchain_param.mode) {
  226. case CHAIN_GRAPH_REL:
  227. return callchain__fprintf_graph(fp, &he->sorted_chain, he->stat.period,
  228. left_margin);
  229. break;
  230. case CHAIN_GRAPH_ABS:
  231. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  232. left_margin);
  233. break;
  234. case CHAIN_FLAT:
  235. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  236. break;
  237. case CHAIN_NONE:
  238. break;
  239. default:
  240. pr_err("Bad callchain mode\n");
  241. }
  242. return 0;
  243. }
  244. static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
  245. struct hists *hists,
  246. FILE *fp)
  247. {
  248. int left_margin = 0;
  249. u64 total_period = hists->stats.total_period;
  250. if (sort__first_dimension == SORT_COMM) {
  251. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  252. typeof(*se), list);
  253. left_margin = hists__col_len(hists, se->se_width_idx);
  254. left_margin -= thread__comm_len(he->thread);
  255. }
  256. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  257. }
  258. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  259. struct hists *hists, FILE *fp)
  260. {
  261. char bf[512];
  262. int ret;
  263. struct perf_hpp hpp = {
  264. .buf = bf,
  265. .size = size,
  266. };
  267. bool color = !symbol_conf.field_sep;
  268. if (size == 0 || size > sizeof(bf))
  269. size = hpp.size = sizeof(bf);
  270. ret = hist_entry__period_snprintf(&hpp, he, color);
  271. hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
  272. ret = fprintf(fp, "%s\n", bf);
  273. if (symbol_conf.use_callchain)
  274. ret += hist_entry__callchain_fprintf(he, hists, fp);
  275. return ret;
  276. }
  277. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  278. int max_cols, FILE *fp)
  279. {
  280. struct perf_hpp_fmt *fmt;
  281. struct sort_entry *se;
  282. struct rb_node *nd;
  283. size_t ret = 0;
  284. unsigned int width;
  285. const char *sep = symbol_conf.field_sep;
  286. const char *col_width = symbol_conf.col_width_list_str;
  287. int nr_rows = 0;
  288. char bf[96];
  289. struct perf_hpp dummy_hpp = {
  290. .buf = bf,
  291. .size = sizeof(bf),
  292. };
  293. bool first = true;
  294. init_rem_hits();
  295. if (!show_header)
  296. goto print_entries;
  297. fprintf(fp, "# ");
  298. perf_hpp__for_each_format(fmt) {
  299. if (!first)
  300. fprintf(fp, "%s", sep ?: " ");
  301. else
  302. first = false;
  303. fmt->header(&dummy_hpp);
  304. fprintf(fp, "%s", bf);
  305. }
  306. list_for_each_entry(se, &hist_entry__sort_list, list) {
  307. if (se->elide)
  308. continue;
  309. if (sep) {
  310. fprintf(fp, "%c%s", *sep, se->se_header);
  311. continue;
  312. }
  313. width = strlen(se->se_header);
  314. if (symbol_conf.col_width_list_str) {
  315. if (col_width) {
  316. hists__set_col_len(hists, se->se_width_idx,
  317. atoi(col_width));
  318. col_width = strchr(col_width, ',');
  319. if (col_width)
  320. ++col_width;
  321. }
  322. }
  323. if (!hists__new_col_len(hists, se->se_width_idx, width))
  324. width = hists__col_len(hists, se->se_width_idx);
  325. fprintf(fp, " %*s", width, se->se_header);
  326. }
  327. fprintf(fp, "\n");
  328. if (max_rows && ++nr_rows >= max_rows)
  329. goto out;
  330. if (sep)
  331. goto print_entries;
  332. first = true;
  333. fprintf(fp, "# ");
  334. perf_hpp__for_each_format(fmt) {
  335. unsigned int i;
  336. if (!first)
  337. fprintf(fp, "%s", sep ?: " ");
  338. else
  339. first = false;
  340. width = fmt->width(&dummy_hpp);
  341. for (i = 0; i < width; i++)
  342. fprintf(fp, ".");
  343. }
  344. list_for_each_entry(se, &hist_entry__sort_list, list) {
  345. unsigned int i;
  346. if (se->elide)
  347. continue;
  348. fprintf(fp, " ");
  349. width = hists__col_len(hists, se->se_width_idx);
  350. if (width == 0)
  351. width = strlen(se->se_header);
  352. for (i = 0; i < width; i++)
  353. fprintf(fp, ".");
  354. }
  355. fprintf(fp, "\n");
  356. if (max_rows && ++nr_rows >= max_rows)
  357. goto out;
  358. fprintf(fp, "#\n");
  359. if (max_rows && ++nr_rows >= max_rows)
  360. goto out;
  361. print_entries:
  362. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  363. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  364. if (h->filtered)
  365. continue;
  366. ret += hist_entry__fprintf(h, max_cols, hists, fp);
  367. if (max_rows && ++nr_rows >= max_rows)
  368. goto out;
  369. if (h->ms.map == NULL && verbose > 1) {
  370. __map_groups__fprintf_maps(&h->thread->mg,
  371. MAP__FUNCTION, verbose, fp);
  372. fprintf(fp, "%.10s end\n", graph_dotted_line);
  373. }
  374. }
  375. out:
  376. free(rem_sq_bracket);
  377. return ret;
  378. }
  379. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
  380. {
  381. int i;
  382. size_t ret = 0;
  383. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  384. const char *name;
  385. if (stats->nr_events[i] == 0)
  386. continue;
  387. name = perf_event__name(i);
  388. if (!strcmp(name, "UNKNOWN"))
  389. continue;
  390. ret += fprintf(fp, "%16s events: %10d\n", name,
  391. stats->nr_events[i]);
  392. }
  393. return ret;
  394. }