hist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #include <math.h>
  2. #include "../util/hist.h"
  3. #include "../util/util.h"
  4. #include "../util/sort.h"
  5. /* hist period print (hpp) functions */
  6. static int hpp__header_overhead(struct perf_hpp *hpp)
  7. {
  8. return scnprintf(hpp->buf, hpp->size, "Overhead");
  9. }
  10. static int hpp__width_overhead(struct perf_hpp *hpp __maybe_unused)
  11. {
  12. return 8;
  13. }
  14. static int hpp__color_overhead(struct perf_hpp *hpp, struct hist_entry *he)
  15. {
  16. struct hists *hists = he->hists;
  17. double percent = 100.0 * he->stat.period / hists->stats.total_period;
  18. return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
  19. }
  20. static int hpp__entry_overhead(struct perf_hpp *hpp, struct hist_entry *he)
  21. {
  22. struct hists *hists = he->hists;
  23. double percent = 100.0 * he->stat.period / hists->stats.total_period;
  24. const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
  25. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  26. }
  27. static int hpp__header_overhead_sys(struct perf_hpp *hpp)
  28. {
  29. const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
  30. return scnprintf(hpp->buf, hpp->size, fmt, "sys");
  31. }
  32. static int hpp__width_overhead_sys(struct perf_hpp *hpp __maybe_unused)
  33. {
  34. return 7;
  35. }
  36. static int hpp__color_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
  37. {
  38. struct hists *hists = he->hists;
  39. double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
  40. return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
  41. }
  42. static int hpp__entry_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
  43. {
  44. struct hists *hists = he->hists;
  45. double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
  46. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  47. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  48. }
  49. static int hpp__header_overhead_us(struct perf_hpp *hpp)
  50. {
  51. const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
  52. return scnprintf(hpp->buf, hpp->size, fmt, "user");
  53. }
  54. static int hpp__width_overhead_us(struct perf_hpp *hpp __maybe_unused)
  55. {
  56. return 7;
  57. }
  58. static int hpp__color_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
  59. {
  60. struct hists *hists = he->hists;
  61. double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
  62. return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
  63. }
  64. static int hpp__entry_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
  65. {
  66. struct hists *hists = he->hists;
  67. double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
  68. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  69. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  70. }
  71. static int hpp__header_overhead_guest_sys(struct perf_hpp *hpp)
  72. {
  73. return scnprintf(hpp->buf, hpp->size, "guest sys");
  74. }
  75. static int hpp__width_overhead_guest_sys(struct perf_hpp *hpp __maybe_unused)
  76. {
  77. return 9;
  78. }
  79. static int hpp__color_overhead_guest_sys(struct perf_hpp *hpp,
  80. struct hist_entry *he)
  81. {
  82. struct hists *hists = he->hists;
  83. double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
  84. return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
  85. }
  86. static int hpp__entry_overhead_guest_sys(struct perf_hpp *hpp,
  87. struct hist_entry *he)
  88. {
  89. struct hists *hists = he->hists;
  90. double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
  91. const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
  92. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  93. }
  94. static int hpp__header_overhead_guest_us(struct perf_hpp *hpp)
  95. {
  96. return scnprintf(hpp->buf, hpp->size, "guest usr");
  97. }
  98. static int hpp__width_overhead_guest_us(struct perf_hpp *hpp __maybe_unused)
  99. {
  100. return 9;
  101. }
  102. static int hpp__color_overhead_guest_us(struct perf_hpp *hpp,
  103. struct hist_entry *he)
  104. {
  105. struct hists *hists = he->hists;
  106. double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
  107. return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
  108. }
  109. static int hpp__entry_overhead_guest_us(struct perf_hpp *hpp,
  110. struct hist_entry *he)
  111. {
  112. struct hists *hists = he->hists;
  113. double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
  114. const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
  115. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  116. }
  117. static int hpp__header_baseline(struct perf_hpp *hpp)
  118. {
  119. return scnprintf(hpp->buf, hpp->size, "Baseline");
  120. }
  121. static int hpp__width_baseline(struct perf_hpp *hpp __maybe_unused)
  122. {
  123. return 8;
  124. }
  125. static double baseline_percent(struct hist_entry *he)
  126. {
  127. struct hist_entry *pair = hist_entry__next_pair(he);
  128. struct hists *pair_hists = pair ? pair->hists : NULL;
  129. double percent = 0.0;
  130. if (pair) {
  131. u64 total_period = pair_hists->stats.total_period;
  132. u64 base_period = pair->stat.period;
  133. percent = 100.0 * base_period / total_period;
  134. }
  135. return percent;
  136. }
  137. static int hpp__color_baseline(struct perf_hpp *hpp, struct hist_entry *he)
  138. {
  139. double percent = baseline_percent(he);
  140. if (hist_entry__has_pairs(he))
  141. return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
  142. else
  143. return scnprintf(hpp->buf, hpp->size, " ");
  144. }
  145. static int hpp__entry_baseline(struct perf_hpp *hpp, struct hist_entry *he)
  146. {
  147. double percent = baseline_percent(he);
  148. const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
  149. if (hist_entry__has_pairs(he) || symbol_conf.field_sep)
  150. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  151. else
  152. return scnprintf(hpp->buf, hpp->size, " ");
  153. }
  154. static int hpp__header_samples(struct perf_hpp *hpp)
  155. {
  156. const char *fmt = symbol_conf.field_sep ? "%s" : "%11s";
  157. return scnprintf(hpp->buf, hpp->size, fmt, "Samples");
  158. }
  159. static int hpp__width_samples(struct perf_hpp *hpp __maybe_unused)
  160. {
  161. return 11;
  162. }
  163. static int hpp__entry_samples(struct perf_hpp *hpp, struct hist_entry *he)
  164. {
  165. const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%11" PRIu64;
  166. return scnprintf(hpp->buf, hpp->size, fmt, he->stat.nr_events);
  167. }
  168. static int hpp__header_period(struct perf_hpp *hpp)
  169. {
  170. const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
  171. return scnprintf(hpp->buf, hpp->size, fmt, "Period");
  172. }
  173. static int hpp__width_period(struct perf_hpp *hpp __maybe_unused)
  174. {
  175. return 12;
  176. }
  177. static int hpp__entry_period(struct perf_hpp *hpp, struct hist_entry *he)
  178. {
  179. const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
  180. return scnprintf(hpp->buf, hpp->size, fmt, he->stat.period);
  181. }
  182. static int hpp__header_period_baseline(struct perf_hpp *hpp)
  183. {
  184. const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
  185. return scnprintf(hpp->buf, hpp->size, fmt, "Period Base");
  186. }
  187. static int hpp__width_period_baseline(struct perf_hpp *hpp __maybe_unused)
  188. {
  189. return 12;
  190. }
  191. static int hpp__entry_period_baseline(struct perf_hpp *hpp, struct hist_entry *he)
  192. {
  193. struct hist_entry *pair = hist_entry__next_pair(he);
  194. u64 period = pair ? pair->stat.period : 0;
  195. const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
  196. return scnprintf(hpp->buf, hpp->size, fmt, period);
  197. }
  198. static int hpp__header_delta(struct perf_hpp *hpp)
  199. {
  200. const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
  201. return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
  202. }
  203. static int hpp__width_delta(struct perf_hpp *hpp __maybe_unused)
  204. {
  205. return 7;
  206. }
  207. static int hpp__entry_delta(struct perf_hpp *hpp, struct hist_entry *he)
  208. {
  209. struct hist_entry *pair = hist_entry__next_pair(he);
  210. const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
  211. char buf[32] = " ";
  212. double diff = 0.0;
  213. if (pair) {
  214. if (he->diff.computed)
  215. diff = he->diff.period_ratio_delta;
  216. else
  217. diff = perf_diff__compute_delta(he, pair);
  218. } else
  219. diff = perf_diff__period_percent(he, he->stat.period);
  220. if (fabs(diff) >= 0.01)
  221. scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
  222. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  223. }
  224. static int hpp__header_ratio(struct perf_hpp *hpp)
  225. {
  226. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  227. return scnprintf(hpp->buf, hpp->size, fmt, "Ratio");
  228. }
  229. static int hpp__width_ratio(struct perf_hpp *hpp __maybe_unused)
  230. {
  231. return 14;
  232. }
  233. static int hpp__entry_ratio(struct perf_hpp *hpp, struct hist_entry *he)
  234. {
  235. struct hist_entry *pair = hist_entry__next_pair(he);
  236. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  237. char buf[32] = " ";
  238. double ratio = 0.0;
  239. if (pair) {
  240. if (he->diff.computed)
  241. ratio = he->diff.period_ratio;
  242. else
  243. ratio = perf_diff__compute_ratio(he, pair);
  244. }
  245. if (ratio > 0.0)
  246. scnprintf(buf, sizeof(buf), "%+14.6F", ratio);
  247. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  248. }
  249. static int hpp__header_wdiff(struct perf_hpp *hpp)
  250. {
  251. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  252. return scnprintf(hpp->buf, hpp->size, fmt, "Weighted diff");
  253. }
  254. static int hpp__width_wdiff(struct perf_hpp *hpp __maybe_unused)
  255. {
  256. return 14;
  257. }
  258. static int hpp__entry_wdiff(struct perf_hpp *hpp, struct hist_entry *he)
  259. {
  260. struct hist_entry *pair = hist_entry__next_pair(he);
  261. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  262. char buf[32] = " ";
  263. s64 wdiff = 0;
  264. if (pair) {
  265. if (he->diff.computed)
  266. wdiff = he->diff.wdiff;
  267. else
  268. wdiff = perf_diff__compute_wdiff(he, pair);
  269. }
  270. if (wdiff != 0)
  271. scnprintf(buf, sizeof(buf), "%14ld", wdiff);
  272. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  273. }
  274. static int hpp__header_displ(struct perf_hpp *hpp)
  275. {
  276. return scnprintf(hpp->buf, hpp->size, "Displ.");
  277. }
  278. static int hpp__width_displ(struct perf_hpp *hpp __maybe_unused)
  279. {
  280. return 6;
  281. }
  282. static int hpp__entry_displ(struct perf_hpp *hpp,
  283. struct hist_entry *he)
  284. {
  285. struct hist_entry *pair = hist_entry__next_pair(he);
  286. long displacement = pair ? pair->position - he->position : 0;
  287. const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
  288. char buf[32] = " ";
  289. if (displacement)
  290. scnprintf(buf, sizeof(buf), "%+4ld", displacement);
  291. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  292. }
  293. static int hpp__header_formula(struct perf_hpp *hpp)
  294. {
  295. const char *fmt = symbol_conf.field_sep ? "%s" : "%70s";
  296. return scnprintf(hpp->buf, hpp->size, fmt, "Formula");
  297. }
  298. static int hpp__width_formula(struct perf_hpp *hpp __maybe_unused)
  299. {
  300. return 70;
  301. }
  302. static int hpp__entry_formula(struct perf_hpp *hpp, struct hist_entry *he)
  303. {
  304. struct hist_entry *pair = hist_entry__next_pair(he);
  305. const char *fmt = symbol_conf.field_sep ? "%s" : "%-70s";
  306. char buf[96] = " ";
  307. if (pair)
  308. perf_diff__formula(he, pair, buf, sizeof(buf));
  309. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  310. }
  311. #define HPP__COLOR_PRINT_FNS(_name) \
  312. { \
  313. .header = hpp__header_ ## _name, \
  314. .width = hpp__width_ ## _name, \
  315. .color = hpp__color_ ## _name, \
  316. .entry = hpp__entry_ ## _name \
  317. }
  318. #define HPP__PRINT_FNS(_name) \
  319. { \
  320. .header = hpp__header_ ## _name, \
  321. .width = hpp__width_ ## _name, \
  322. .entry = hpp__entry_ ## _name \
  323. }
  324. struct perf_hpp_fmt perf_hpp__format[] = {
  325. HPP__COLOR_PRINT_FNS(baseline),
  326. HPP__COLOR_PRINT_FNS(overhead),
  327. HPP__COLOR_PRINT_FNS(overhead_sys),
  328. HPP__COLOR_PRINT_FNS(overhead_us),
  329. HPP__COLOR_PRINT_FNS(overhead_guest_sys),
  330. HPP__COLOR_PRINT_FNS(overhead_guest_us),
  331. HPP__PRINT_FNS(samples),
  332. HPP__PRINT_FNS(period),
  333. HPP__PRINT_FNS(period_baseline),
  334. HPP__PRINT_FNS(delta),
  335. HPP__PRINT_FNS(ratio),
  336. HPP__PRINT_FNS(wdiff),
  337. HPP__PRINT_FNS(displ),
  338. HPP__PRINT_FNS(formula)
  339. };
  340. LIST_HEAD(perf_hpp__list);
  341. #undef HPP__COLOR_PRINT_FNS
  342. #undef HPP__PRINT_FNS
  343. void perf_hpp__init(void)
  344. {
  345. if (symbol_conf.show_cpu_utilization) {
  346. perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
  347. perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
  348. if (perf_guest) {
  349. perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
  350. perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
  351. }
  352. }
  353. if (symbol_conf.show_nr_samples)
  354. perf_hpp__column_enable(PERF_HPP__SAMPLES);
  355. if (symbol_conf.show_total_period)
  356. perf_hpp__column_enable(PERF_HPP__PERIOD);
  357. }
  358. void perf_hpp__column_register(struct perf_hpp_fmt *format)
  359. {
  360. list_add_tail(&format->list, &perf_hpp__list);
  361. }
  362. void perf_hpp__column_enable(unsigned col)
  363. {
  364. BUG_ON(col >= PERF_HPP__MAX_INDEX);
  365. perf_hpp__column_register(&perf_hpp__format[col]);
  366. }
  367. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  368. {
  369. hpp->buf += inc;
  370. hpp->size -= inc;
  371. }
  372. int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
  373. bool color)
  374. {
  375. const char *sep = symbol_conf.field_sep;
  376. struct perf_hpp_fmt *fmt;
  377. char *start = hpp->buf;
  378. int ret;
  379. bool first = true;
  380. if (symbol_conf.exclude_other && !he->parent)
  381. return 0;
  382. perf_hpp__for_each_format(fmt) {
  383. /*
  384. * If there's no field_sep, we still need
  385. * to display initial ' '.
  386. */
  387. if (!sep || !first) {
  388. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  389. advance_hpp(hpp, ret);
  390. } else
  391. first = false;
  392. if (color && fmt->color)
  393. ret = fmt->color(hpp, he);
  394. else
  395. ret = fmt->entry(hpp, he);
  396. advance_hpp(hpp, ret);
  397. }
  398. return hpp->buf - start;
  399. }
  400. int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
  401. struct hists *hists)
  402. {
  403. const char *sep = symbol_conf.field_sep;
  404. struct sort_entry *se;
  405. int ret = 0;
  406. list_for_each_entry(se, &hist_entry__sort_list, list) {
  407. if (se->elide)
  408. continue;
  409. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  410. ret += se->se_snprintf(he, s + ret, size - ret,
  411. hists__col_len(hists, se->se_width_idx));
  412. }
  413. return ret;
  414. }
  415. /*
  416. * See hists__fprintf to match the column widths
  417. */
  418. unsigned int hists__sort_list_width(struct hists *hists)
  419. {
  420. struct perf_hpp_fmt *fmt;
  421. struct sort_entry *se;
  422. int i = 0, ret = 0;
  423. perf_hpp__for_each_format(fmt) {
  424. if (i)
  425. ret += 2;
  426. ret += fmt->width(NULL);
  427. }
  428. list_for_each_entry(se, &hist_entry__sort_list, list)
  429. if (!se->elide)
  430. ret += 2 + hists__col_len(hists, se->se_width_idx);
  431. if (verbose) /* Addr + origin */
  432. ret += 3 + BITS_PER_LONG / 4;
  433. return ret;
  434. }