hist.c 13 KB

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