hist.c 13 KB

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