hist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. char bf[1024];
  35. ret += callchain__fprintf_left_margin(fp, left_margin);
  36. for (i = 0; i < depth; i++) {
  37. if (depth_mask & (1 << i))
  38. ret += fprintf(fp, "|");
  39. else
  40. ret += fprintf(fp, " ");
  41. if (!period && i == depth - 1) {
  42. double percent;
  43. percent = hits * 100.0 / total_samples;
  44. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  45. } else
  46. ret += fprintf(fp, "%s", " ");
  47. }
  48. fputs(callchain_list__sym_name(chain, bf, sizeof(bf), false), fp);
  49. fputc('\n', fp);
  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. char bf[1024];
  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++ && field_order == NULL &&
  157. sort_order && !prefixcmp(sort_order, "sym"))
  158. continue;
  159. if (!printed) {
  160. ret += callchain__fprintf_left_margin(fp, left_margin);
  161. ret += fprintf(fp, "|\n");
  162. ret += callchain__fprintf_left_margin(fp, left_margin);
  163. ret += fprintf(fp, "---");
  164. left_margin += 3;
  165. printed = true;
  166. } else
  167. ret += callchain__fprintf_left_margin(fp, left_margin);
  168. ret += fprintf(fp, "%s\n", callchain_list__sym_name(chain, bf, sizeof(bf),
  169. false));
  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, struct callchain_node *node,
  181. u64 total_samples)
  182. {
  183. struct callchain_list *chain;
  184. size_t ret = 0;
  185. char bf[1024];
  186. if (!node)
  187. return 0;
  188. ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
  189. list_for_each_entry(chain, &node->val, list) {
  190. if (chain->ip >= PERF_CONTEXT_MAX)
  191. continue;
  192. ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
  193. bf, sizeof(bf), false));
  194. }
  195. return ret;
  196. }
  197. static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
  198. u64 total_samples)
  199. {
  200. size_t ret = 0;
  201. u32 entries_printed = 0;
  202. struct callchain_node *chain;
  203. struct rb_node *rb_node = rb_first(tree);
  204. while (rb_node) {
  205. double percent;
  206. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  207. percent = chain->hit * 100.0 / total_samples;
  208. ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
  209. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  210. ret += fprintf(fp, "\n");
  211. if (++entries_printed == callchain_param.print_limit)
  212. break;
  213. rb_node = rb_next(rb_node);
  214. }
  215. return ret;
  216. }
  217. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  218. u64 total_samples, int left_margin,
  219. FILE *fp)
  220. {
  221. switch (callchain_param.mode) {
  222. case CHAIN_GRAPH_REL:
  223. return callchain__fprintf_graph(fp, &he->sorted_chain,
  224. symbol_conf.cumulate_callchain ?
  225. he->stat_acc->period : he->stat.period,
  226. left_margin);
  227. break;
  228. case CHAIN_GRAPH_ABS:
  229. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  230. left_margin);
  231. break;
  232. case CHAIN_FLAT:
  233. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  234. break;
  235. case CHAIN_NONE:
  236. break;
  237. default:
  238. pr_err("Bad callchain mode\n");
  239. }
  240. return 0;
  241. }
  242. static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
  243. struct hists *hists,
  244. FILE *fp)
  245. {
  246. int left_margin = 0;
  247. u64 total_period = hists->stats.total_period;
  248. if (field_order == NULL && (sort_order == NULL ||
  249. !prefixcmp(sort_order, "comm"))) {
  250. struct perf_hpp_fmt *fmt;
  251. perf_hpp__for_each_format(fmt) {
  252. if (!perf_hpp__is_sort_entry(fmt))
  253. continue;
  254. /* must be 'comm' sort entry */
  255. left_margin = fmt->width(fmt, NULL, hists_to_evsel(hists));
  256. left_margin -= thread__comm_len(he->thread);
  257. break;
  258. }
  259. }
  260. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  261. }
  262. static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
  263. {
  264. const char *sep = symbol_conf.field_sep;
  265. struct perf_hpp_fmt *fmt;
  266. char *start = hpp->buf;
  267. int ret;
  268. bool first = true;
  269. if (symbol_conf.exclude_other && !he->parent)
  270. return 0;
  271. perf_hpp__for_each_format(fmt) {
  272. if (perf_hpp__should_skip(fmt))
  273. continue;
  274. /*
  275. * If there's no field_sep, we still need
  276. * to display initial ' '.
  277. */
  278. if (!sep || !first) {
  279. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  280. advance_hpp(hpp, ret);
  281. } else
  282. first = false;
  283. if (perf_hpp__use_color() && fmt->color)
  284. ret = fmt->color(fmt, hpp, he);
  285. else
  286. ret = fmt->entry(fmt, hpp, he);
  287. advance_hpp(hpp, ret);
  288. }
  289. return hpp->buf - start;
  290. }
  291. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  292. struct hists *hists,
  293. char *bf, size_t bfsz, FILE *fp)
  294. {
  295. int ret;
  296. struct perf_hpp hpp = {
  297. .buf = bf,
  298. .size = size,
  299. };
  300. if (size == 0 || size > bfsz)
  301. size = hpp.size = bfsz;
  302. hist_entry__snprintf(he, &hpp);
  303. ret = fprintf(fp, "%s\n", bf);
  304. if (symbol_conf.use_callchain)
  305. ret += hist_entry__callchain_fprintf(he, hists, fp);
  306. return ret;
  307. }
  308. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  309. int max_cols, float min_pcnt, FILE *fp)
  310. {
  311. struct perf_hpp_fmt *fmt;
  312. struct rb_node *nd;
  313. size_t ret = 0;
  314. unsigned int width;
  315. const char *sep = symbol_conf.field_sep;
  316. int nr_rows = 0;
  317. char bf[96];
  318. struct perf_hpp dummy_hpp = {
  319. .buf = bf,
  320. .size = sizeof(bf),
  321. };
  322. bool first = true;
  323. size_t linesz;
  324. char *line = NULL;
  325. init_rem_hits();
  326. perf_hpp__for_each_format(fmt)
  327. perf_hpp__reset_width(fmt, hists);
  328. if (symbol_conf.col_width_list_str)
  329. perf_hpp__set_user_width(symbol_conf.col_width_list_str);
  330. if (!show_header)
  331. goto print_entries;
  332. fprintf(fp, "# ");
  333. perf_hpp__for_each_format(fmt) {
  334. if (perf_hpp__should_skip(fmt))
  335. continue;
  336. if (!first)
  337. fprintf(fp, "%s", sep ?: " ");
  338. else
  339. first = false;
  340. fmt->header(fmt, &dummy_hpp, hists_to_evsel(hists));
  341. fprintf(fp, "%s", bf);
  342. }
  343. fprintf(fp, "\n");
  344. if (max_rows && ++nr_rows >= max_rows)
  345. goto out;
  346. if (sep)
  347. goto print_entries;
  348. first = true;
  349. fprintf(fp, "# ");
  350. perf_hpp__for_each_format(fmt) {
  351. unsigned int i;
  352. if (perf_hpp__should_skip(fmt))
  353. continue;
  354. if (!first)
  355. fprintf(fp, "%s", sep ?: " ");
  356. else
  357. first = false;
  358. width = fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
  359. for (i = 0; i < width; i++)
  360. fprintf(fp, ".");
  361. }
  362. fprintf(fp, "\n");
  363. if (max_rows && ++nr_rows >= max_rows)
  364. goto out;
  365. fprintf(fp, "#\n");
  366. if (max_rows && ++nr_rows >= max_rows)
  367. goto out;
  368. print_entries:
  369. linesz = hists__sort_list_width(hists) + 3 + 1;
  370. linesz += perf_hpp__color_overhead();
  371. line = malloc(linesz);
  372. if (line == NULL) {
  373. ret = -1;
  374. goto out;
  375. }
  376. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  377. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  378. float percent;
  379. if (h->filtered)
  380. continue;
  381. percent = hist_entry__get_percent_limit(h);
  382. if (percent < min_pcnt)
  383. continue;
  384. ret += hist_entry__fprintf(h, max_cols, hists, line, linesz, fp);
  385. if (max_rows && ++nr_rows >= max_rows)
  386. break;
  387. if (h->ms.map == NULL && verbose > 1) {
  388. __map_groups__fprintf_maps(h->thread->mg,
  389. MAP__FUNCTION, fp);
  390. fprintf(fp, "%.10s end\n", graph_dotted_line);
  391. }
  392. }
  393. free(line);
  394. out:
  395. zfree(&rem_sq_bracket);
  396. return ret;
  397. }
  398. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
  399. {
  400. int i;
  401. size_t ret = 0;
  402. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  403. const char *name;
  404. if (stats->nr_events[i] == 0)
  405. continue;
  406. name = perf_event__name(i);
  407. if (!strcmp(name, "UNKNOWN"))
  408. continue;
  409. ret += fprintf(fp, "%16s events: %10d\n", name,
  410. stats->nr_events[i]);
  411. }
  412. return ret;
  413. }