hist.c 13 KB

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