hist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. ret += callchain__fprintf_left_margin(fp, left_margin);
  35. for (i = 0; i < depth; i++) {
  36. if (depth_mask & (1 << i))
  37. ret += fprintf(fp, "|");
  38. else
  39. ret += fprintf(fp, " ");
  40. if (!period && i == depth - 1) {
  41. double percent;
  42. percent = hits * 100.0 / total_samples;
  43. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  44. } else
  45. ret += fprintf(fp, "%s", " ");
  46. }
  47. if (chain->ms.sym)
  48. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  49. else
  50. ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
  51. return ret;
  52. }
  53. static struct symbol *rem_sq_bracket;
  54. static struct callchain_list rem_hits;
  55. static void init_rem_hits(void)
  56. {
  57. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  58. if (!rem_sq_bracket) {
  59. fprintf(stderr, "Not enough memory to display remaining hits\n");
  60. return;
  61. }
  62. strcpy(rem_sq_bracket->name, "[...]");
  63. rem_hits.ms.sym = rem_sq_bracket;
  64. }
  65. static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  66. u64 total_samples, int depth,
  67. int depth_mask, int left_margin)
  68. {
  69. struct rb_node *node, *next;
  70. struct callchain_node *child;
  71. struct callchain_list *chain;
  72. int new_depth_mask = depth_mask;
  73. u64 remaining;
  74. size_t ret = 0;
  75. int i;
  76. uint entries_printed = 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. /*
  86. * The depth mask manages the output of pipes that show
  87. * the depth. We don't want to keep the pipes of the current
  88. * level for the last child of this depth.
  89. * Except if we have remaining filtered hits. They will
  90. * supersede the last child
  91. */
  92. next = rb_next(node);
  93. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  94. new_depth_mask &= ~(1 << (depth - 1));
  95. /*
  96. * But we keep the older depth mask for the line separator
  97. * to keep the level link until we reach the last child
  98. */
  99. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  100. left_margin);
  101. i = 0;
  102. list_for_each_entry(chain, &child->val, list) {
  103. ret += ipchain__fprintf_graph(fp, chain, depth,
  104. new_depth_mask, i++,
  105. total_samples,
  106. cumul,
  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. if (!rem_sq_bracket)
  124. return ret;
  125. new_depth_mask &= ~(1 << (depth - 1));
  126. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  127. new_depth_mask, 0, total_samples,
  128. remaining, left_margin);
  129. }
  130. return ret;
  131. }
  132. static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  133. u64 total_samples, int left_margin)
  134. {
  135. struct callchain_node *cnode;
  136. struct callchain_list *chain;
  137. u32 entries_printed = 0;
  138. bool printed = false;
  139. struct rb_node *node;
  140. int i = 0;
  141. int ret = 0;
  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++ && sort__first_dimension == SORT_SYM)
  157. continue;
  158. if (!printed) {
  159. ret += callchain__fprintf_left_margin(fp, left_margin);
  160. ret += fprintf(fp, "|\n");
  161. ret += callchain__fprintf_left_margin(fp, left_margin);
  162. ret += fprintf(fp, "---");
  163. left_margin += 3;
  164. printed = true;
  165. } else
  166. ret += callchain__fprintf_left_margin(fp, left_margin);
  167. if (chain->ms.sym)
  168. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  169. else
  170. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  171. if (++entries_printed == callchain_param.print_limit)
  172. break;
  173. }
  174. root = &cnode->rb_root;
  175. }
  176. ret += __callchain__fprintf_graph(fp, root, total_samples,
  177. 1, 1, left_margin);
  178. ret += fprintf(fp, "\n");
  179. return ret;
  180. }
  181. static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
  182. u64 total_samples)
  183. {
  184. struct callchain_list *chain;
  185. size_t ret = 0;
  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. 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 *tree,
  201. u64 total_samples)
  202. {
  203. size_t ret = 0;
  204. u32 entries_printed = 0;
  205. struct callchain_node *chain;
  206. struct rb_node *rb_node = rb_first(tree);
  207. while (rb_node) {
  208. double percent;
  209. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  210. percent = chain->hit * 100.0 / total_samples;
  211. ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
  212. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  213. ret += fprintf(fp, "\n");
  214. if (++entries_printed == callchain_param.print_limit)
  215. break;
  216. rb_node = rb_next(rb_node);
  217. }
  218. return ret;
  219. }
  220. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  221. u64 total_samples, int left_margin,
  222. FILE *fp)
  223. {
  224. switch (callchain_param.mode) {
  225. case CHAIN_GRAPH_REL:
  226. return callchain__fprintf_graph(fp, &he->sorted_chain, he->stat.period,
  227. left_margin);
  228. break;
  229. case CHAIN_GRAPH_ABS:
  230. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  231. left_margin);
  232. break;
  233. case CHAIN_FLAT:
  234. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  235. break;
  236. case CHAIN_NONE:
  237. break;
  238. default:
  239. pr_err("Bad callchain mode\n");
  240. }
  241. return 0;
  242. }
  243. static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
  244. struct hists *hists,
  245. FILE *fp)
  246. {
  247. int left_margin = 0;
  248. u64 total_period = hists->stats.total_period;
  249. if (sort__first_dimension == SORT_COMM) {
  250. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  251. typeof(*se), list);
  252. left_margin = hists__col_len(hists, se->se_width_idx);
  253. left_margin -= thread__comm_len(he->thread);
  254. }
  255. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  256. }
  257. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  258. {
  259. hpp->buf += inc;
  260. hpp->size -= inc;
  261. }
  262. static int hist_entry__period_snprintf(struct perf_hpp *hpp,
  263. struct hist_entry *he)
  264. {
  265. const char *sep = symbol_conf.field_sep;
  266. struct perf_hpp_fmt *fmt;
  267. char *start = hpp->buf;
  268. int ret;
  269. bool first = true;
  270. if (symbol_conf.exclude_other && !he->parent)
  271. return 0;
  272. perf_hpp__for_each_format(fmt) {
  273. /*
  274. * If there's no field_sep, we still need
  275. * to display initial ' '.
  276. */
  277. if (!sep || !first) {
  278. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  279. advance_hpp(hpp, ret);
  280. } else
  281. first = false;
  282. if (perf_hpp__use_color() && fmt->color)
  283. ret = fmt->color(fmt, hpp, he);
  284. else
  285. ret = fmt->entry(fmt, hpp, he);
  286. advance_hpp(hpp, ret);
  287. }
  288. return hpp->buf - start;
  289. }
  290. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  291. struct hists *hists,
  292. char *bf, size_t bfsz, FILE *fp)
  293. {
  294. int ret;
  295. struct perf_hpp hpp = {
  296. .buf = bf,
  297. .size = size,
  298. };
  299. if (size == 0 || size > bfsz)
  300. size = hpp.size = bfsz;
  301. ret = hist_entry__period_snprintf(&hpp, he);
  302. hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
  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 sort_entry *se;
  313. struct rb_node *nd;
  314. size_t ret = 0;
  315. unsigned int width;
  316. const char *sep = symbol_conf.field_sep;
  317. const char *col_width = symbol_conf.col_width_list_str;
  318. int nr_rows = 0;
  319. char bf[96];
  320. struct perf_hpp dummy_hpp = {
  321. .buf = bf,
  322. .size = sizeof(bf),
  323. .ptr = hists_to_evsel(hists),
  324. };
  325. bool first = true;
  326. size_t linesz;
  327. char *line = NULL;
  328. init_rem_hits();
  329. if (!show_header)
  330. goto print_entries;
  331. fprintf(fp, "# ");
  332. perf_hpp__for_each_format(fmt) {
  333. if (!first)
  334. fprintf(fp, "%s", sep ?: " ");
  335. else
  336. first = false;
  337. fmt->header(fmt, &dummy_hpp);
  338. fprintf(fp, "%s", bf);
  339. }
  340. list_for_each_entry(se, &hist_entry__sort_list, list) {
  341. if (se->elide)
  342. continue;
  343. if (sep) {
  344. fprintf(fp, "%c%s", *sep, se->se_header);
  345. continue;
  346. }
  347. width = strlen(se->se_header);
  348. if (symbol_conf.col_width_list_str) {
  349. if (col_width) {
  350. hists__set_col_len(hists, se->se_width_idx,
  351. atoi(col_width));
  352. col_width = strchr(col_width, ',');
  353. if (col_width)
  354. ++col_width;
  355. }
  356. }
  357. if (!hists__new_col_len(hists, se->se_width_idx, width))
  358. width = hists__col_len(hists, se->se_width_idx);
  359. fprintf(fp, " %*s", width, se->se_header);
  360. }
  361. fprintf(fp, "\n");
  362. if (max_rows && ++nr_rows >= max_rows)
  363. goto out;
  364. if (sep)
  365. goto print_entries;
  366. first = true;
  367. fprintf(fp, "# ");
  368. perf_hpp__for_each_format(fmt) {
  369. unsigned int i;
  370. if (!first)
  371. fprintf(fp, "%s", sep ?: " ");
  372. else
  373. first = false;
  374. width = fmt->width(fmt, &dummy_hpp);
  375. for (i = 0; i < width; i++)
  376. fprintf(fp, ".");
  377. }
  378. list_for_each_entry(se, &hist_entry__sort_list, list) {
  379. unsigned int i;
  380. if (se->elide)
  381. continue;
  382. fprintf(fp, " ");
  383. width = hists__col_len(hists, se->se_width_idx);
  384. if (width == 0)
  385. width = strlen(se->se_header);
  386. for (i = 0; i < width; i++)
  387. fprintf(fp, ".");
  388. }
  389. fprintf(fp, "\n");
  390. if (max_rows && ++nr_rows >= max_rows)
  391. goto out;
  392. fprintf(fp, "#\n");
  393. if (max_rows && ++nr_rows >= max_rows)
  394. goto out;
  395. print_entries:
  396. linesz = hists__sort_list_width(hists) + 3 + 1;
  397. linesz += perf_hpp__color_overhead();
  398. line = malloc(linesz);
  399. if (line == NULL) {
  400. ret = -1;
  401. goto out;
  402. }
  403. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  404. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  405. float percent = h->stat.period * 100.0 /
  406. hists->stats.total_period;
  407. if (h->filtered)
  408. continue;
  409. if (percent < min_pcnt)
  410. continue;
  411. ret += hist_entry__fprintf(h, max_cols, hists, line, linesz, fp);
  412. if (max_rows && ++nr_rows >= max_rows)
  413. break;
  414. if (h->ms.map == NULL && verbose > 1) {
  415. __map_groups__fprintf_maps(&h->thread->mg,
  416. MAP__FUNCTION, verbose, fp);
  417. fprintf(fp, "%.10s end\n", graph_dotted_line);
  418. }
  419. }
  420. free(line);
  421. out:
  422. free(rem_sq_bracket);
  423. return ret;
  424. }
  425. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
  426. {
  427. int i;
  428. size_t ret = 0;
  429. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  430. const char *name;
  431. if (stats->nr_events[i] == 0)
  432. continue;
  433. name = perf_event__name(i);
  434. if (!strcmp(name, "UNKNOWN"))
  435. continue;
  436. ret += fprintf(fp, "%16s events: %10d\n", name,
  437. stats->nr_events[i]);
  438. }
  439. return ret;
  440. }