hist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 sort_entry *se;
  281. struct rb_node *nd;
  282. size_t ret = 0;
  283. unsigned int width;
  284. const char *sep = symbol_conf.field_sep;
  285. const char *col_width = symbol_conf.col_width_list_str;
  286. int idx, nr_rows = 0;
  287. char bf[64];
  288. struct perf_hpp dummy_hpp = {
  289. .buf = bf,
  290. .size = sizeof(bf),
  291. };
  292. bool first = true;
  293. init_rem_hits();
  294. if (!show_header)
  295. goto print_entries;
  296. fprintf(fp, "# ");
  297. for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
  298. if (!perf_hpp__format[idx].cond)
  299. continue;
  300. if (!first)
  301. fprintf(fp, "%s", sep ?: " ");
  302. else
  303. first = false;
  304. perf_hpp__format[idx].header(&dummy_hpp);
  305. fprintf(fp, "%s", bf);
  306. }
  307. list_for_each_entry(se, &hist_entry__sort_list, list) {
  308. if (se->elide)
  309. continue;
  310. if (sep) {
  311. fprintf(fp, "%c%s", *sep, se->se_header);
  312. continue;
  313. }
  314. width = strlen(se->se_header);
  315. if (symbol_conf.col_width_list_str) {
  316. if (col_width) {
  317. hists__set_col_len(hists, se->se_width_idx,
  318. atoi(col_width));
  319. col_width = strchr(col_width, ',');
  320. if (col_width)
  321. ++col_width;
  322. }
  323. }
  324. if (!hists__new_col_len(hists, se->se_width_idx, width))
  325. width = hists__col_len(hists, se->se_width_idx);
  326. fprintf(fp, " %*s", width, se->se_header);
  327. }
  328. fprintf(fp, "\n");
  329. if (max_rows && ++nr_rows >= max_rows)
  330. goto out;
  331. if (sep)
  332. goto print_entries;
  333. first = true;
  334. fprintf(fp, "# ");
  335. for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
  336. unsigned int i;
  337. if (!perf_hpp__format[idx].cond)
  338. continue;
  339. if (!first)
  340. fprintf(fp, "%s", sep ?: " ");
  341. else
  342. first = false;
  343. width = perf_hpp__format[idx].width(&dummy_hpp);
  344. for (i = 0; i < width; i++)
  345. fprintf(fp, ".");
  346. }
  347. list_for_each_entry(se, &hist_entry__sort_list, list) {
  348. unsigned int i;
  349. if (se->elide)
  350. continue;
  351. fprintf(fp, " ");
  352. width = hists__col_len(hists, se->se_width_idx);
  353. if (width == 0)
  354. width = strlen(se->se_header);
  355. for (i = 0; i < width; i++)
  356. fprintf(fp, ".");
  357. }
  358. fprintf(fp, "\n");
  359. if (max_rows && ++nr_rows >= max_rows)
  360. goto out;
  361. fprintf(fp, "#\n");
  362. if (max_rows && ++nr_rows >= max_rows)
  363. goto out;
  364. print_entries:
  365. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  366. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  367. if (h->filtered)
  368. continue;
  369. ret += hist_entry__fprintf(h, max_cols, hists, fp);
  370. if (max_rows && ++nr_rows >= max_rows)
  371. goto out;
  372. if (h->ms.map == NULL && verbose > 1) {
  373. __map_groups__fprintf_maps(&h->thread->mg,
  374. MAP__FUNCTION, verbose, fp);
  375. fprintf(fp, "%.10s end\n", graph_dotted_line);
  376. }
  377. }
  378. out:
  379. free(rem_sq_bracket);
  380. return ret;
  381. }
  382. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  383. {
  384. int i;
  385. size_t ret = 0;
  386. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  387. const char *name;
  388. if (hists->stats.nr_events[i] == 0)
  389. continue;
  390. name = perf_event__name(i);
  391. if (!strcmp(name, "UNKNOWN"))
  392. continue;
  393. ret += fprintf(fp, "%16s events: %10d\n", name,
  394. hists->stats.nr_events[i]);
  395. }
  396. return ret;
  397. }