hist.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "../../util/util.h"
  4. #include "../../util/hist.h"
  5. #include "../../util/sort.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->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 int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
  246. size_t size, struct hists *pair_hists,
  247. bool show_displacement, long displacement,
  248. bool color, u64 total_period)
  249. {
  250. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  251. u64 nr_events;
  252. const char *sep = symbol_conf.field_sep;
  253. int ret;
  254. if (symbol_conf.exclude_other && !he->parent)
  255. return 0;
  256. if (pair_hists) {
  257. period = he->pair ? he->pair->period : 0;
  258. nr_events = he->pair ? he->pair->nr_events : 0;
  259. total = pair_hists->stats.total_period;
  260. period_sys = he->pair ? he->pair->period_sys : 0;
  261. period_us = he->pair ? he->pair->period_us : 0;
  262. period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
  263. period_guest_us = he->pair ? he->pair->period_guest_us : 0;
  264. } else {
  265. period = he->period;
  266. nr_events = he->nr_events;
  267. total = total_period;
  268. period_sys = he->period_sys;
  269. period_us = he->period_us;
  270. period_guest_sys = he->period_guest_sys;
  271. period_guest_us = he->period_guest_us;
  272. }
  273. if (total) {
  274. if (color)
  275. ret = percent_color_snprintf(s, size,
  276. sep ? "%.2f" : " %6.2f%%",
  277. (period * 100.0) / total);
  278. else
  279. ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  280. (period * 100.0) / total);
  281. if (symbol_conf.show_cpu_utilization) {
  282. ret += percent_color_snprintf(s + ret, size - ret,
  283. sep ? "%.2f" : " %6.2f%%",
  284. (period_sys * 100.0) / total);
  285. ret += percent_color_snprintf(s + ret, size - ret,
  286. sep ? "%.2f" : " %6.2f%%",
  287. (period_us * 100.0) / total);
  288. if (perf_guest) {
  289. ret += percent_color_snprintf(s + ret,
  290. size - ret,
  291. sep ? "%.2f" : " %6.2f%%",
  292. (period_guest_sys * 100.0) /
  293. total);
  294. ret += percent_color_snprintf(s + ret,
  295. size - ret,
  296. sep ? "%.2f" : " %6.2f%%",
  297. (period_guest_us * 100.0) /
  298. total);
  299. }
  300. }
  301. } else
  302. ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  303. if (symbol_conf.show_nr_samples) {
  304. if (sep)
  305. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  306. else
  307. ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  308. }
  309. if (symbol_conf.show_total_period) {
  310. if (sep)
  311. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  312. else
  313. ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
  314. }
  315. if (pair_hists) {
  316. char bf[32];
  317. double old_percent = 0, new_percent = 0, diff;
  318. if (total > 0)
  319. old_percent = (period * 100.0) / total;
  320. if (total_period > 0)
  321. new_percent = (he->period * 100.0) / total_period;
  322. diff = new_percent - old_percent;
  323. if (fabs(diff) >= 0.01)
  324. scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  325. else
  326. scnprintf(bf, sizeof(bf), " ");
  327. if (sep)
  328. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  329. else
  330. ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
  331. if (show_displacement) {
  332. if (displacement)
  333. scnprintf(bf, sizeof(bf), "%+4ld", displacement);
  334. else
  335. scnprintf(bf, sizeof(bf), " ");
  336. if (sep)
  337. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  338. else
  339. ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
  340. }
  341. }
  342. return ret;
  343. }
  344. int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
  345. struct hists *hists)
  346. {
  347. const char *sep = symbol_conf.field_sep;
  348. struct sort_entry *se;
  349. int ret = 0;
  350. list_for_each_entry(se, &hist_entry__sort_list, list) {
  351. if (se->elide)
  352. continue;
  353. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  354. ret += se->se_snprintf(he, s + ret, size - ret,
  355. hists__col_len(hists, se->se_width_idx));
  356. }
  357. return ret;
  358. }
  359. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  360. struct hists *hists, struct hists *pair_hists,
  361. bool show_displacement, long displacement,
  362. u64 total_period, FILE *fp)
  363. {
  364. char bf[512];
  365. int ret;
  366. if (size == 0 || size > sizeof(bf))
  367. size = sizeof(bf);
  368. ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
  369. show_displacement, displacement,
  370. true, total_period);
  371. hist_entry__snprintf(he, bf + ret, size - ret, hists);
  372. return fprintf(fp, "%s\n", bf);
  373. }
  374. static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
  375. struct hists *hists,
  376. u64 total_period, FILE *fp)
  377. {
  378. int left_margin = 0;
  379. if (sort__first_dimension == SORT_COMM) {
  380. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  381. typeof(*se), list);
  382. left_margin = hists__col_len(hists, se->se_width_idx);
  383. left_margin -= thread__comm_len(he->thread);
  384. }
  385. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  386. }
  387. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  388. bool show_displacement, bool show_header, int max_rows,
  389. int max_cols, FILE *fp)
  390. {
  391. struct sort_entry *se;
  392. struct rb_node *nd;
  393. size_t ret = 0;
  394. u64 total_period;
  395. unsigned long position = 1;
  396. long displacement = 0;
  397. unsigned int width;
  398. const char *sep = symbol_conf.field_sep;
  399. const char *col_width = symbol_conf.col_width_list_str;
  400. int nr_rows = 0;
  401. init_rem_hits();
  402. if (!show_header)
  403. goto print_entries;
  404. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  405. if (symbol_conf.show_cpu_utilization) {
  406. if (sep) {
  407. ret += fprintf(fp, "%csys", *sep);
  408. ret += fprintf(fp, "%cus", *sep);
  409. if (perf_guest) {
  410. ret += fprintf(fp, "%cguest sys", *sep);
  411. ret += fprintf(fp, "%cguest us", *sep);
  412. }
  413. } else {
  414. ret += fprintf(fp, " sys ");
  415. ret += fprintf(fp, " us ");
  416. if (perf_guest) {
  417. ret += fprintf(fp, " guest sys ");
  418. ret += fprintf(fp, " guest us ");
  419. }
  420. }
  421. }
  422. if (symbol_conf.show_nr_samples) {
  423. if (sep)
  424. fprintf(fp, "%cSamples", *sep);
  425. else
  426. fputs(" Samples ", fp);
  427. }
  428. if (symbol_conf.show_total_period) {
  429. if (sep)
  430. ret += fprintf(fp, "%cPeriod", *sep);
  431. else
  432. ret += fprintf(fp, " Period ");
  433. }
  434. if (pair) {
  435. if (sep)
  436. ret += fprintf(fp, "%cDelta", *sep);
  437. else
  438. ret += fprintf(fp, " Delta ");
  439. if (show_displacement) {
  440. if (sep)
  441. ret += fprintf(fp, "%cDisplacement", *sep);
  442. else
  443. ret += fprintf(fp, " Displ");
  444. }
  445. }
  446. list_for_each_entry(se, &hist_entry__sort_list, list) {
  447. if (se->elide)
  448. continue;
  449. if (sep) {
  450. fprintf(fp, "%c%s", *sep, se->se_header);
  451. continue;
  452. }
  453. width = strlen(se->se_header);
  454. if (symbol_conf.col_width_list_str) {
  455. if (col_width) {
  456. hists__set_col_len(hists, se->se_width_idx,
  457. atoi(col_width));
  458. col_width = strchr(col_width, ',');
  459. if (col_width)
  460. ++col_width;
  461. }
  462. }
  463. if (!hists__new_col_len(hists, se->se_width_idx, width))
  464. width = hists__col_len(hists, se->se_width_idx);
  465. fprintf(fp, " %*s", width, se->se_header);
  466. }
  467. fprintf(fp, "\n");
  468. if (max_rows && ++nr_rows >= max_rows)
  469. goto out;
  470. if (sep)
  471. goto print_entries;
  472. fprintf(fp, "# ........");
  473. if (symbol_conf.show_cpu_utilization)
  474. fprintf(fp, " ....... .......");
  475. if (symbol_conf.show_nr_samples)
  476. fprintf(fp, " ..........");
  477. if (symbol_conf.show_total_period)
  478. fprintf(fp, " ............");
  479. if (pair) {
  480. fprintf(fp, " ..........");
  481. if (show_displacement)
  482. fprintf(fp, " .....");
  483. }
  484. list_for_each_entry(se, &hist_entry__sort_list, list) {
  485. unsigned int i;
  486. if (se->elide)
  487. continue;
  488. fprintf(fp, " ");
  489. width = hists__col_len(hists, se->se_width_idx);
  490. if (width == 0)
  491. width = strlen(se->se_header);
  492. for (i = 0; i < width; i++)
  493. fprintf(fp, ".");
  494. }
  495. fprintf(fp, "\n");
  496. if (max_rows && ++nr_rows >= max_rows)
  497. goto out;
  498. fprintf(fp, "#\n");
  499. if (max_rows && ++nr_rows >= max_rows)
  500. goto out;
  501. print_entries:
  502. total_period = hists->stats.total_period;
  503. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  504. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  505. if (h->filtered)
  506. continue;
  507. if (show_displacement) {
  508. if (h->pair != NULL)
  509. displacement = ((long)h->pair->position -
  510. (long)position);
  511. else
  512. displacement = 0;
  513. ++position;
  514. }
  515. ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
  516. displacement, total_period, fp);
  517. if (symbol_conf.use_callchain)
  518. ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
  519. if (max_rows && ++nr_rows >= max_rows)
  520. goto out;
  521. if (h->ms.map == NULL && verbose > 1) {
  522. __map_groups__fprintf_maps(&h->thread->mg,
  523. MAP__FUNCTION, verbose, fp);
  524. fprintf(fp, "%.10s end\n", graph_dotted_line);
  525. }
  526. }
  527. out:
  528. free(rem_sq_bracket);
  529. return ret;
  530. }
  531. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  532. {
  533. int i;
  534. size_t ret = 0;
  535. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  536. const char *name;
  537. if (hists->stats.nr_events[i] == 0)
  538. continue;
  539. name = perf_event__name(i);
  540. if (!strcmp(name, "UNKNOWN"))
  541. continue;
  542. ret += fprintf(fp, "%16s events: %10d\n", name,
  543. hists->stats.nr_events[i]);
  544. }
  545. return ret;
  546. }