hist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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_formula(struct perf_hpp *hpp)
  275. {
  276. const char *fmt = symbol_conf.field_sep ? "%s" : "%70s";
  277. return scnprintf(hpp->buf, hpp->size, fmt, "Formula");
  278. }
  279. static int hpp__width_formula(struct perf_hpp *hpp __maybe_unused)
  280. {
  281. return 70;
  282. }
  283. static int hpp__entry_formula(struct perf_hpp *hpp, struct hist_entry *he)
  284. {
  285. struct hist_entry *pair = hist_entry__next_pair(he);
  286. const char *fmt = symbol_conf.field_sep ? "%s" : "%-70s";
  287. char buf[96] = " ";
  288. if (pair)
  289. perf_diff__formula(he, pair, buf, sizeof(buf));
  290. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  291. }
  292. #define HPP__COLOR_PRINT_FNS(_name) \
  293. { \
  294. .header = hpp__header_ ## _name, \
  295. .width = hpp__width_ ## _name, \
  296. .color = hpp__color_ ## _name, \
  297. .entry = hpp__entry_ ## _name \
  298. }
  299. #define HPP__PRINT_FNS(_name) \
  300. { \
  301. .header = hpp__header_ ## _name, \
  302. .width = hpp__width_ ## _name, \
  303. .entry = hpp__entry_ ## _name \
  304. }
  305. struct perf_hpp_fmt perf_hpp__format[] = {
  306. HPP__COLOR_PRINT_FNS(baseline),
  307. HPP__COLOR_PRINT_FNS(overhead),
  308. HPP__COLOR_PRINT_FNS(overhead_sys),
  309. HPP__COLOR_PRINT_FNS(overhead_us),
  310. HPP__COLOR_PRINT_FNS(overhead_guest_sys),
  311. HPP__COLOR_PRINT_FNS(overhead_guest_us),
  312. HPP__PRINT_FNS(samples),
  313. HPP__PRINT_FNS(period),
  314. HPP__PRINT_FNS(period_baseline),
  315. HPP__PRINT_FNS(delta),
  316. HPP__PRINT_FNS(ratio),
  317. HPP__PRINT_FNS(wdiff),
  318. HPP__PRINT_FNS(formula)
  319. };
  320. LIST_HEAD(perf_hpp__list);
  321. #undef HPP__COLOR_PRINT_FNS
  322. #undef HPP__PRINT_FNS
  323. void perf_hpp__init(void)
  324. {
  325. if (symbol_conf.show_cpu_utilization) {
  326. perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
  327. perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
  328. if (perf_guest) {
  329. perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
  330. perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
  331. }
  332. }
  333. if (symbol_conf.show_nr_samples)
  334. perf_hpp__column_enable(PERF_HPP__SAMPLES);
  335. if (symbol_conf.show_total_period)
  336. perf_hpp__column_enable(PERF_HPP__PERIOD);
  337. }
  338. void perf_hpp__column_register(struct perf_hpp_fmt *format)
  339. {
  340. list_add_tail(&format->list, &perf_hpp__list);
  341. }
  342. void perf_hpp__column_enable(unsigned col)
  343. {
  344. BUG_ON(col >= PERF_HPP__MAX_INDEX);
  345. perf_hpp__column_register(&perf_hpp__format[col]);
  346. }
  347. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  348. {
  349. hpp->buf += inc;
  350. hpp->size -= inc;
  351. }
  352. int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
  353. bool color)
  354. {
  355. const char *sep = symbol_conf.field_sep;
  356. struct perf_hpp_fmt *fmt;
  357. char *start = hpp->buf;
  358. int ret;
  359. bool first = true;
  360. if (symbol_conf.exclude_other && !he->parent)
  361. return 0;
  362. perf_hpp__for_each_format(fmt) {
  363. /*
  364. * If there's no field_sep, we still need
  365. * to display initial ' '.
  366. */
  367. if (!sep || !first) {
  368. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  369. advance_hpp(hpp, ret);
  370. } else
  371. first = false;
  372. if (color && fmt->color)
  373. ret = fmt->color(hpp, he);
  374. else
  375. ret = fmt->entry(hpp, he);
  376. advance_hpp(hpp, ret);
  377. }
  378. return hpp->buf - start;
  379. }
  380. int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
  381. struct hists *hists)
  382. {
  383. const char *sep = symbol_conf.field_sep;
  384. struct sort_entry *se;
  385. int ret = 0;
  386. list_for_each_entry(se, &hist_entry__sort_list, list) {
  387. if (se->elide)
  388. continue;
  389. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  390. ret += se->se_snprintf(he, s + ret, size - ret,
  391. hists__col_len(hists, se->se_width_idx));
  392. }
  393. return ret;
  394. }
  395. /*
  396. * See hists__fprintf to match the column widths
  397. */
  398. unsigned int hists__sort_list_width(struct hists *hists)
  399. {
  400. struct perf_hpp_fmt *fmt;
  401. struct sort_entry *se;
  402. int i = 0, ret = 0;
  403. perf_hpp__for_each_format(fmt) {
  404. if (i)
  405. ret += 2;
  406. ret += fmt->width(NULL);
  407. }
  408. list_for_each_entry(se, &hist_entry__sort_list, list)
  409. if (!se->elide)
  410. ret += 2 + hists__col_len(hists, se->se_width_idx);
  411. if (verbose) /* Addr + origin */
  412. ret += 3 + BITS_PER_LONG / 4;
  413. return ret;
  414. }