hist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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,
  182. struct callchain_node *self,
  183. u64 total_samples)
  184. {
  185. struct callchain_list *chain;
  186. size_t ret = 0;
  187. if (!self)
  188. return 0;
  189. ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
  190. list_for_each_entry(chain, &self->val, list) {
  191. if (chain->ip >= PERF_CONTEXT_MAX)
  192. continue;
  193. if (chain->ms.sym)
  194. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  195. else
  196. ret += fprintf(fp, " %p\n",
  197. (void *)(long)chain->ip);
  198. }
  199. return ret;
  200. }
  201. static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
  202. u64 total_samples)
  203. {
  204. size_t ret = 0;
  205. u32 entries_printed = 0;
  206. struct rb_node *rb_node;
  207. struct callchain_node *chain;
  208. rb_node = rb_first(self);
  209. while (rb_node) {
  210. double percent;
  211. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  212. percent = chain->hit * 100.0 / total_samples;
  213. ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
  214. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  215. ret += fprintf(fp, "\n");
  216. if (++entries_printed == callchain_param.print_limit)
  217. break;
  218. rb_node = rb_next(rb_node);
  219. }
  220. return ret;
  221. }
  222. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  223. u64 total_samples, int left_margin,
  224. FILE *fp)
  225. {
  226. switch (callchain_param.mode) {
  227. case CHAIN_GRAPH_REL:
  228. return callchain__fprintf_graph(fp, &he->sorted_chain, he->stat.period,
  229. left_margin);
  230. break;
  231. case CHAIN_GRAPH_ABS:
  232. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  233. left_margin);
  234. break;
  235. case CHAIN_FLAT:
  236. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  237. break;
  238. case CHAIN_NONE:
  239. break;
  240. default:
  241. pr_err("Bad callchain mode\n");
  242. }
  243. return 0;
  244. }
  245. static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
  246. struct hists *hists,
  247. FILE *fp)
  248. {
  249. int left_margin = 0;
  250. u64 total_period = hists->stats.total_period;
  251. if (sort__first_dimension == SORT_COMM) {
  252. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  253. typeof(*se), list);
  254. left_margin = hists__col_len(hists, se->se_width_idx);
  255. left_margin -= thread__comm_len(he->thread);
  256. }
  257. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  258. }
  259. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  260. {
  261. hpp->buf += inc;
  262. hpp->size -= inc;
  263. }
  264. static int hist_entry__period_snprintf(struct perf_hpp *hpp,
  265. struct hist_entry *he)
  266. {
  267. const char *sep = symbol_conf.field_sep;
  268. struct perf_hpp_fmt *fmt;
  269. char *start = hpp->buf;
  270. int ret;
  271. bool first = true;
  272. if (symbol_conf.exclude_other && !he->parent)
  273. return 0;
  274. perf_hpp__for_each_format(fmt) {
  275. /*
  276. * If there's no field_sep, we still need
  277. * to display initial ' '.
  278. */
  279. if (!sep || !first) {
  280. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  281. advance_hpp(hpp, ret);
  282. } else
  283. first = false;
  284. if (perf_hpp__use_color() && fmt->color)
  285. ret = fmt->color(fmt, hpp, he);
  286. else
  287. ret = fmt->entry(fmt, hpp, he);
  288. advance_hpp(hpp, ret);
  289. }
  290. return hpp->buf - start;
  291. }
  292. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  293. struct hists *hists,
  294. char *bf, size_t bfsz, FILE *fp)
  295. {
  296. int ret;
  297. struct perf_hpp hpp = {
  298. .buf = bf,
  299. .size = size,
  300. };
  301. if (size == 0 || size > bfsz)
  302. size = hpp.size = bfsz;
  303. ret = hist_entry__period_snprintf(&hpp, he);
  304. hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
  305. ret = fprintf(fp, "%s\n", bf);
  306. if (symbol_conf.use_callchain)
  307. ret += hist_entry__callchain_fprintf(he, hists, fp);
  308. return ret;
  309. }
  310. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  311. int max_cols, float min_pcnt, FILE *fp)
  312. {
  313. struct perf_hpp_fmt *fmt;
  314. struct sort_entry *se;
  315. struct rb_node *nd;
  316. size_t ret = 0;
  317. unsigned int width;
  318. const char *sep = symbol_conf.field_sep;
  319. const char *col_width = symbol_conf.col_width_list_str;
  320. int nr_rows = 0;
  321. char bf[96];
  322. struct perf_hpp dummy_hpp = {
  323. .buf = bf,
  324. .size = sizeof(bf),
  325. .ptr = hists_to_evsel(hists),
  326. };
  327. bool first = true;
  328. size_t linesz;
  329. char *line = NULL;
  330. init_rem_hits();
  331. if (!show_header)
  332. goto print_entries;
  333. fprintf(fp, "# ");
  334. perf_hpp__for_each_format(fmt) {
  335. if (!first)
  336. fprintf(fp, "%s", sep ?: " ");
  337. else
  338. first = false;
  339. fmt->header(fmt, &dummy_hpp);
  340. fprintf(fp, "%s", bf);
  341. }
  342. list_for_each_entry(se, &hist_entry__sort_list, list) {
  343. if (se->elide)
  344. continue;
  345. if (sep) {
  346. fprintf(fp, "%c%s", *sep, se->se_header);
  347. continue;
  348. }
  349. width = strlen(se->se_header);
  350. if (symbol_conf.col_width_list_str) {
  351. if (col_width) {
  352. hists__set_col_len(hists, se->se_width_idx,
  353. atoi(col_width));
  354. col_width = strchr(col_width, ',');
  355. if (col_width)
  356. ++col_width;
  357. }
  358. }
  359. if (!hists__new_col_len(hists, se->se_width_idx, width))
  360. width = hists__col_len(hists, se->se_width_idx);
  361. fprintf(fp, " %*s", width, se->se_header);
  362. }
  363. fprintf(fp, "\n");
  364. if (max_rows && ++nr_rows >= max_rows)
  365. goto out;
  366. if (sep)
  367. goto print_entries;
  368. first = true;
  369. fprintf(fp, "# ");
  370. perf_hpp__for_each_format(fmt) {
  371. unsigned int i;
  372. if (!first)
  373. fprintf(fp, "%s", sep ?: " ");
  374. else
  375. first = false;
  376. width = fmt->width(fmt, &dummy_hpp);
  377. for (i = 0; i < width; i++)
  378. fprintf(fp, ".");
  379. }
  380. list_for_each_entry(se, &hist_entry__sort_list, list) {
  381. unsigned int i;
  382. if (se->elide)
  383. continue;
  384. fprintf(fp, " ");
  385. width = hists__col_len(hists, se->se_width_idx);
  386. if (width == 0)
  387. width = strlen(se->se_header);
  388. for (i = 0; i < width; i++)
  389. fprintf(fp, ".");
  390. }
  391. fprintf(fp, "\n");
  392. if (max_rows && ++nr_rows >= max_rows)
  393. goto out;
  394. fprintf(fp, "#\n");
  395. if (max_rows && ++nr_rows >= max_rows)
  396. goto out;
  397. print_entries:
  398. linesz = hists__sort_list_width(hists) + 3 + 1;
  399. linesz += perf_hpp__color_overhead();
  400. line = malloc(linesz);
  401. if (line == NULL) {
  402. ret = -1;
  403. goto out;
  404. }
  405. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  406. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  407. float percent = h->stat.period * 100.0 /
  408. hists->stats.total_period;
  409. if (h->filtered)
  410. continue;
  411. if (percent < min_pcnt)
  412. continue;
  413. ret += hist_entry__fprintf(h, max_cols, hists, line, linesz, fp);
  414. if (max_rows && ++nr_rows >= max_rows)
  415. break;
  416. if (h->ms.map == NULL && verbose > 1) {
  417. __map_groups__fprintf_maps(&h->thread->mg,
  418. MAP__FUNCTION, verbose, fp);
  419. fprintf(fp, "%.10s end\n", graph_dotted_line);
  420. }
  421. }
  422. free(line);
  423. out:
  424. free(rem_sq_bracket);
  425. return ret;
  426. }
  427. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
  428. {
  429. int i;
  430. size_t ret = 0;
  431. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  432. const char *name;
  433. if (stats->nr_events[i] == 0)
  434. continue;
  435. name = perf_event__name(i);
  436. if (!strcmp(name, "UNKNOWN"))
  437. continue;
  438. ret += fprintf(fp, "%16s events: %10d\n", name,
  439. stats->nr_events[i]);
  440. }
  441. return ret;
  442. }