hist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #include <stdio.h>
  2. #include "../../util/util.h"
  3. #include "../../util/hist.h"
  4. #include "../../util/sort.h"
  5. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  6. {
  7. int i;
  8. int ret = fprintf(fp, " ");
  9. for (i = 0; i < left_margin; i++)
  10. ret += fprintf(fp, " ");
  11. return ret;
  12. }
  13. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  14. int left_margin)
  15. {
  16. int i;
  17. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  18. for (i = 0; i < depth; i++)
  19. if (depth_mask & (1 << i))
  20. ret += fprintf(fp, "| ");
  21. else
  22. ret += fprintf(fp, " ");
  23. ret += fprintf(fp, "\n");
  24. return ret;
  25. }
  26. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  27. int depth, int depth_mask, int period,
  28. u64 total_samples, u64 hits,
  29. int left_margin)
  30. {
  31. int i;
  32. size_t ret = 0;
  33. ret += callchain__fprintf_left_margin(fp, left_margin);
  34. for (i = 0; i < depth; i++) {
  35. if (depth_mask & (1 << i))
  36. ret += fprintf(fp, "|");
  37. else
  38. ret += fprintf(fp, " ");
  39. if (!period && i == depth - 1) {
  40. double percent;
  41. percent = hits * 100.0 / total_samples;
  42. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  43. } else
  44. ret += fprintf(fp, "%s", " ");
  45. }
  46. if (chain->ms.sym)
  47. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  48. else
  49. ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
  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. /*
  142. * If have one single callchain root, don't bother printing
  143. * its percentage (100 % in fractal mode and the same percentage
  144. * than the hist in graph mode). This also avoid one level of column.
  145. */
  146. node = rb_first(root);
  147. if (node && !rb_next(node)) {
  148. cnode = rb_entry(node, struct callchain_node, rb_node);
  149. list_for_each_entry(chain, &cnode->val, list) {
  150. /*
  151. * If we sort by symbol, the first entry is the same than
  152. * the symbol. No need to print it otherwise it appears as
  153. * displayed twice.
  154. */
  155. if (!i++ && sort__first_dimension == SORT_SYM)
  156. continue;
  157. if (!printed) {
  158. ret += callchain__fprintf_left_margin(fp, left_margin);
  159. ret += fprintf(fp, "|\n");
  160. ret += callchain__fprintf_left_margin(fp, left_margin);
  161. ret += fprintf(fp, "---");
  162. left_margin += 3;
  163. printed = true;
  164. } else
  165. ret += callchain__fprintf_left_margin(fp, left_margin);
  166. if (chain->ms.sym)
  167. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  168. else
  169. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  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,
  181. struct callchain_node *self,
  182. u64 total_samples)
  183. {
  184. struct callchain_list *chain;
  185. size_t ret = 0;
  186. if (!self)
  187. return 0;
  188. ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
  189. list_for_each_entry(chain, &self->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 *self,
  201. u64 total_samples)
  202. {
  203. size_t ret = 0;
  204. u32 entries_printed = 0;
  205. struct rb_node *rb_node;
  206. struct callchain_node *chain;
  207. rb_node = rb_first(self);
  208. while (rb_node) {
  209. double percent;
  210. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  211. percent = chain->hit * 100.0 / total_samples;
  212. ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
  213. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  214. ret += fprintf(fp, "\n");
  215. if (++entries_printed == callchain_param.print_limit)
  216. break;
  217. rb_node = rb_next(rb_node);
  218. }
  219. return ret;
  220. }
  221. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  222. u64 total_samples, int left_margin,
  223. FILE *fp)
  224. {
  225. switch (callchain_param.mode) {
  226. case CHAIN_GRAPH_REL:
  227. return callchain__fprintf_graph(fp, &he->sorted_chain, he->period,
  228. left_margin);
  229. break;
  230. case CHAIN_GRAPH_ABS:
  231. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  232. left_margin);
  233. break;
  234. case CHAIN_FLAT:
  235. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  236. break;
  237. case CHAIN_NONE:
  238. break;
  239. default:
  240. pr_err("Bad callchain mode\n");
  241. }
  242. return 0;
  243. }
  244. static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
  245. struct hists *hists,
  246. u64 total_period, FILE *fp)
  247. {
  248. int left_margin = 0;
  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 int hist_entry__fprintf(struct hist_entry *he, size_t size,
  258. struct hists *hists, struct hists *pair_hists,
  259. long displacement, u64 total_period, FILE *fp)
  260. {
  261. char bf[512];
  262. int ret;
  263. struct perf_hpp hpp = {
  264. .buf = bf,
  265. .size = size,
  266. .total_period = total_period,
  267. .displacement = displacement,
  268. .ptr = pair_hists,
  269. };
  270. bool color = !symbol_conf.field_sep;
  271. if (size == 0 || size > sizeof(bf))
  272. size = hpp.size = sizeof(bf);
  273. ret = hist_entry__period_snprintf(&hpp, he, color);
  274. hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
  275. ret = fprintf(fp, "%s\n", bf);
  276. if (symbol_conf.use_callchain)
  277. ret += hist_entry__callchain_fprintf(he, hists,
  278. total_period, fp);
  279. return ret;
  280. }
  281. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  282. bool show_displacement, bool show_header, int max_rows,
  283. int max_cols, FILE *fp)
  284. {
  285. struct sort_entry *se;
  286. struct rb_node *nd;
  287. size_t ret = 0;
  288. u64 total_period;
  289. unsigned long position = 1;
  290. long displacement = 0;
  291. unsigned int width;
  292. const char *sep = symbol_conf.field_sep;
  293. const char *col_width = symbol_conf.col_width_list_str;
  294. int idx, nr_rows = 0;
  295. char bf[64];
  296. struct perf_hpp dummy_hpp = {
  297. .buf = bf,
  298. .size = sizeof(bf),
  299. .ptr = pair,
  300. };
  301. init_rem_hits();
  302. if (!show_header)
  303. goto print_entries;
  304. fprintf(fp, "# ");
  305. for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
  306. if (!perf_hpp__format[idx].cond)
  307. continue;
  308. if (idx)
  309. fprintf(fp, "%s", sep ?: " ");
  310. perf_hpp__format[idx].header(&dummy_hpp);
  311. fprintf(fp, "%s", bf);
  312. }
  313. list_for_each_entry(se, &hist_entry__sort_list, list) {
  314. if (se->elide)
  315. continue;
  316. if (sep) {
  317. fprintf(fp, "%c%s", *sep, se->se_header);
  318. continue;
  319. }
  320. width = strlen(se->se_header);
  321. if (symbol_conf.col_width_list_str) {
  322. if (col_width) {
  323. hists__set_col_len(hists, se->se_width_idx,
  324. atoi(col_width));
  325. col_width = strchr(col_width, ',');
  326. if (col_width)
  327. ++col_width;
  328. }
  329. }
  330. if (!hists__new_col_len(hists, se->se_width_idx, width))
  331. width = hists__col_len(hists, se->se_width_idx);
  332. fprintf(fp, " %*s", width, se->se_header);
  333. }
  334. fprintf(fp, "\n");
  335. if (max_rows && ++nr_rows >= max_rows)
  336. goto out;
  337. if (sep)
  338. goto print_entries;
  339. fprintf(fp, "# ");
  340. for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
  341. unsigned int i;
  342. if (!perf_hpp__format[idx].cond)
  343. continue;
  344. if (idx)
  345. fprintf(fp, "%s", sep ?: " ");
  346. width = perf_hpp__format[idx].width(&dummy_hpp);
  347. for (i = 0; i < width; i++)
  348. fprintf(fp, ".");
  349. }
  350. list_for_each_entry(se, &hist_entry__sort_list, list) {
  351. unsigned int i;
  352. if (se->elide)
  353. continue;
  354. fprintf(fp, " ");
  355. width = hists__col_len(hists, se->se_width_idx);
  356. if (width == 0)
  357. width = strlen(se->se_header);
  358. for (i = 0; i < width; i++)
  359. fprintf(fp, ".");
  360. }
  361. fprintf(fp, "\n");
  362. if (max_rows && ++nr_rows >= max_rows)
  363. goto out;
  364. fprintf(fp, "#\n");
  365. if (max_rows && ++nr_rows >= max_rows)
  366. goto out;
  367. print_entries:
  368. total_period = hists->stats.total_period;
  369. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  370. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  371. if (h->filtered)
  372. continue;
  373. if (show_displacement) {
  374. if (h->pair != NULL)
  375. displacement = ((long)h->pair->position -
  376. (long)position);
  377. else
  378. displacement = 0;
  379. ++position;
  380. }
  381. ret += hist_entry__fprintf(h, max_cols, hists, pair, displacement,
  382. total_period, fp);
  383. if (max_rows && ++nr_rows >= max_rows)
  384. goto out;
  385. if (h->ms.map == NULL && verbose > 1) {
  386. __map_groups__fprintf_maps(&h->thread->mg,
  387. MAP__FUNCTION, verbose, fp);
  388. fprintf(fp, "%.10s end\n", graph_dotted_line);
  389. }
  390. }
  391. out:
  392. free(rem_sq_bracket);
  393. return ret;
  394. }
  395. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  396. {
  397. int i;
  398. size_t ret = 0;
  399. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  400. const char *name;
  401. if (hists->stats.nr_events[i] == 0)
  402. continue;
  403. name = perf_event__name(i);
  404. if (!strcmp(name, "UNKNOWN"))
  405. continue;
  406. ret += fprintf(fp, "%16s events: %10d\n", name,
  407. hists->stats.nr_events[i]);
  408. }
  409. return ret;
  410. }