hist.c 14 KB

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