hist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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_node *node,
  28. struct callchain_list *chain,
  29. int depth, int depth_mask, int period,
  30. u64 total_samples, 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. ret += fprintf(fp, "--");
  43. ret += callchain_node__fprintf_value(node, fp, total_samples);
  44. ret += fprintf(fp, "--");
  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 = NULL;
  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. int cumul_count = 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. cumul_count += callchain_cumul_counts(child);
  86. /*
  87. * The depth mask manages the output of pipes that show
  88. * the depth. We don't want to keep the pipes of the current
  89. * level for the last child of this depth.
  90. * Except if we have remaining filtered hits. They will
  91. * supersede the last child
  92. */
  93. next = rb_next(node);
  94. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  95. new_depth_mask &= ~(1 << (depth - 1));
  96. /*
  97. * But we keep the older depth mask for the line separator
  98. * to keep the level link until we reach the last child
  99. */
  100. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  101. left_margin);
  102. i = 0;
  103. list_for_each_entry(chain, &child->val, list) {
  104. ret += ipchain__fprintf_graph(fp, child, chain, depth,
  105. new_depth_mask, i++,
  106. total_samples,
  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. struct callchain_node rem_node = {
  124. .hit = remaining,
  125. };
  126. if (!rem_sq_bracket)
  127. return ret;
  128. if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
  129. rem_node.count = child->parent->children_count - cumul_count;
  130. if (rem_node.count <= 0)
  131. return ret;
  132. }
  133. new_depth_mask &= ~(1 << (depth - 1));
  134. ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
  135. new_depth_mask, 0, total_samples,
  136. left_margin);
  137. }
  138. return ret;
  139. }
  140. /*
  141. * If have one single callchain root, don't bother printing
  142. * its percentage (100 % in fractal mode and the same percentage
  143. * than the hist in graph mode). This also avoid one level of column.
  144. *
  145. * However when percent-limit applied, it's possible that single callchain
  146. * node have different (non-100% in fractal mode) percentage.
  147. */
  148. static bool need_percent_display(struct rb_node *node, u64 parent_samples)
  149. {
  150. struct callchain_node *cnode;
  151. if (rb_next(node))
  152. return true;
  153. cnode = rb_entry(node, struct callchain_node, rb_node);
  154. return callchain_cumul_hits(cnode) != parent_samples;
  155. }
  156. static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  157. u64 total_samples, u64 parent_samples,
  158. int left_margin)
  159. {
  160. struct callchain_node *cnode;
  161. struct callchain_list *chain;
  162. u32 entries_printed = 0;
  163. bool printed = false;
  164. struct rb_node *node;
  165. int i = 0;
  166. int ret = 0;
  167. char bf[1024];
  168. node = rb_first(root);
  169. if (node && !need_percent_display(node, parent_samples)) {
  170. cnode = rb_entry(node, struct callchain_node, rb_node);
  171. list_for_each_entry(chain, &cnode->val, list) {
  172. /*
  173. * If we sort by symbol, the first entry is the same than
  174. * the symbol. No need to print it otherwise it appears as
  175. * displayed twice.
  176. */
  177. if (!i++ && field_order == NULL &&
  178. sort_order && !prefixcmp(sort_order, "sym"))
  179. continue;
  180. if (!printed) {
  181. ret += callchain__fprintf_left_margin(fp, left_margin);
  182. ret += fprintf(fp, "|\n");
  183. ret += callchain__fprintf_left_margin(fp, left_margin);
  184. ret += fprintf(fp, "---");
  185. left_margin += 3;
  186. printed = true;
  187. } else
  188. ret += callchain__fprintf_left_margin(fp, left_margin);
  189. ret += fprintf(fp, "%s\n", callchain_list__sym_name(chain, bf, sizeof(bf),
  190. false));
  191. if (++entries_printed == callchain_param.print_limit)
  192. break;
  193. }
  194. root = &cnode->rb_root;
  195. }
  196. if (callchain_param.mode == CHAIN_GRAPH_REL)
  197. total_samples = parent_samples;
  198. ret += __callchain__fprintf_graph(fp, root, total_samples,
  199. 1, 1, left_margin);
  200. ret += fprintf(fp, "\n");
  201. return ret;
  202. }
  203. static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
  204. u64 total_samples)
  205. {
  206. struct callchain_list *chain;
  207. size_t ret = 0;
  208. char bf[1024];
  209. if (!node)
  210. return 0;
  211. ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
  212. list_for_each_entry(chain, &node->val, list) {
  213. if (chain->ip >= PERF_CONTEXT_MAX)
  214. continue;
  215. ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
  216. bf, sizeof(bf), false));
  217. }
  218. return ret;
  219. }
  220. static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
  221. u64 total_samples)
  222. {
  223. size_t ret = 0;
  224. u32 entries_printed = 0;
  225. struct callchain_node *chain;
  226. struct rb_node *rb_node = rb_first(tree);
  227. while (rb_node) {
  228. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  229. ret += fprintf(fp, " ");
  230. ret += callchain_node__fprintf_value(chain, fp, total_samples);
  231. ret += fprintf(fp, "\n");
  232. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  233. ret += fprintf(fp, "\n");
  234. if (++entries_printed == callchain_param.print_limit)
  235. break;
  236. rb_node = rb_next(rb_node);
  237. }
  238. return ret;
  239. }
  240. static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
  241. {
  242. const char *sep = symbol_conf.field_sep ?: ";";
  243. struct callchain_list *chain;
  244. size_t ret = 0;
  245. char bf[1024];
  246. bool first;
  247. if (!node)
  248. return 0;
  249. ret += __callchain__fprintf_folded(fp, node->parent);
  250. first = (ret == 0);
  251. list_for_each_entry(chain, &node->val, list) {
  252. if (chain->ip >= PERF_CONTEXT_MAX)
  253. continue;
  254. ret += fprintf(fp, "%s%s", first ? "" : sep,
  255. callchain_list__sym_name(chain,
  256. bf, sizeof(bf), false));
  257. first = false;
  258. }
  259. return ret;
  260. }
  261. static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
  262. u64 total_samples)
  263. {
  264. size_t ret = 0;
  265. u32 entries_printed = 0;
  266. struct callchain_node *chain;
  267. struct rb_node *rb_node = rb_first(tree);
  268. while (rb_node) {
  269. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  270. ret += callchain_node__fprintf_value(chain, fp, total_samples);
  271. ret += fprintf(fp, " ");
  272. ret += __callchain__fprintf_folded(fp, chain);
  273. ret += fprintf(fp, "\n");
  274. if (++entries_printed == callchain_param.print_limit)
  275. break;
  276. rb_node = rb_next(rb_node);
  277. }
  278. return ret;
  279. }
  280. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  281. u64 total_samples, int left_margin,
  282. FILE *fp)
  283. {
  284. u64 parent_samples = he->stat.period;
  285. if (symbol_conf.cumulate_callchain)
  286. parent_samples = he->stat_acc->period;
  287. switch (callchain_param.mode) {
  288. case CHAIN_GRAPH_REL:
  289. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  290. parent_samples, left_margin);
  291. break;
  292. case CHAIN_GRAPH_ABS:
  293. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  294. parent_samples, left_margin);
  295. break;
  296. case CHAIN_FLAT:
  297. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  298. break;
  299. case CHAIN_FOLDED:
  300. return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
  301. break;
  302. case CHAIN_NONE:
  303. break;
  304. default:
  305. pr_err("Bad callchain mode\n");
  306. }
  307. return 0;
  308. }
  309. static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
  310. {
  311. const char *sep = symbol_conf.field_sep;
  312. struct perf_hpp_fmt *fmt;
  313. char *start = hpp->buf;
  314. int ret;
  315. bool first = true;
  316. if (symbol_conf.exclude_other && !he->parent)
  317. return 0;
  318. perf_hpp__for_each_format(fmt) {
  319. if (perf_hpp__should_skip(fmt, he->hists))
  320. continue;
  321. /*
  322. * If there's no field_sep, we still need
  323. * to display initial ' '.
  324. */
  325. if (!sep || !first) {
  326. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  327. advance_hpp(hpp, ret);
  328. } else
  329. first = false;
  330. if (perf_hpp__use_color() && fmt->color)
  331. ret = fmt->color(fmt, hpp, he);
  332. else
  333. ret = fmt->entry(fmt, hpp, he);
  334. advance_hpp(hpp, ret);
  335. }
  336. return hpp->buf - start;
  337. }
  338. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  339. struct hists *hists,
  340. char *bf, size_t bfsz, FILE *fp)
  341. {
  342. int ret;
  343. struct perf_hpp hpp = {
  344. .buf = bf,
  345. .size = size,
  346. };
  347. u64 total_period = hists->stats.total_period;
  348. if (size == 0 || size > bfsz)
  349. size = hpp.size = bfsz;
  350. hist_entry__snprintf(he, &hpp);
  351. ret = fprintf(fp, "%s\n", bf);
  352. if (symbol_conf.use_callchain)
  353. ret += hist_entry_callchain__fprintf(he, total_period, 0, fp);
  354. return ret;
  355. }
  356. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  357. int max_cols, float min_pcnt, FILE *fp)
  358. {
  359. struct perf_hpp_fmt *fmt;
  360. struct rb_node *nd;
  361. size_t ret = 0;
  362. unsigned int width;
  363. const char *sep = symbol_conf.field_sep;
  364. int nr_rows = 0;
  365. char bf[96];
  366. struct perf_hpp dummy_hpp = {
  367. .buf = bf,
  368. .size = sizeof(bf),
  369. };
  370. bool first = true;
  371. size_t linesz;
  372. char *line = NULL;
  373. init_rem_hits();
  374. perf_hpp__for_each_format(fmt)
  375. perf_hpp__reset_width(fmt, hists);
  376. if (symbol_conf.col_width_list_str)
  377. perf_hpp__set_user_width(symbol_conf.col_width_list_str);
  378. if (!show_header)
  379. goto print_entries;
  380. fprintf(fp, "# ");
  381. perf_hpp__for_each_format(fmt) {
  382. if (perf_hpp__should_skip(fmt, hists))
  383. continue;
  384. if (!first)
  385. fprintf(fp, "%s", sep ?: " ");
  386. else
  387. first = false;
  388. fmt->header(fmt, &dummy_hpp, hists_to_evsel(hists));
  389. fprintf(fp, "%s", bf);
  390. }
  391. fprintf(fp, "\n");
  392. if (max_rows && ++nr_rows >= max_rows)
  393. goto out;
  394. if (sep)
  395. goto print_entries;
  396. first = true;
  397. fprintf(fp, "# ");
  398. perf_hpp__for_each_format(fmt) {
  399. unsigned int i;
  400. if (perf_hpp__should_skip(fmt, hists))
  401. continue;
  402. if (!first)
  403. fprintf(fp, "%s", sep ?: " ");
  404. else
  405. first = false;
  406. width = fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
  407. for (i = 0; i < width; i++)
  408. fprintf(fp, ".");
  409. }
  410. fprintf(fp, "\n");
  411. if (max_rows && ++nr_rows >= max_rows)
  412. goto out;
  413. fprintf(fp, "#\n");
  414. if (max_rows && ++nr_rows >= max_rows)
  415. goto out;
  416. print_entries:
  417. linesz = hists__sort_list_width(hists) + 3 + 1;
  418. linesz += perf_hpp__color_overhead();
  419. line = malloc(linesz);
  420. if (line == NULL) {
  421. ret = -1;
  422. goto out;
  423. }
  424. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  425. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  426. float percent;
  427. if (h->filtered)
  428. continue;
  429. percent = hist_entry__get_percent_limit(h);
  430. if (percent < min_pcnt)
  431. continue;
  432. ret += hist_entry__fprintf(h, max_cols, hists, line, linesz, fp);
  433. if (max_rows && ++nr_rows >= max_rows)
  434. break;
  435. if (h->ms.map == NULL && verbose > 1) {
  436. __map_groups__fprintf_maps(h->thread->mg,
  437. MAP__FUNCTION, fp);
  438. fprintf(fp, "%.10s end\n", graph_dotted_line);
  439. }
  440. }
  441. free(line);
  442. out:
  443. zfree(&rem_sq_bracket);
  444. return ret;
  445. }
  446. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
  447. {
  448. int i;
  449. size_t ret = 0;
  450. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  451. const char *name;
  452. if (stats->nr_events[i] == 0)
  453. continue;
  454. name = perf_event__name(i);
  455. if (!strcmp(name, "UNKNOWN"))
  456. continue;
  457. ret += fprintf(fp, "%16s events: %10d\n", name,
  458. stats->nr_events[i]);
  459. }
  460. return ret;
  461. }