hist.c 11 KB

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