hist.c 13 KB

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