/tools/perf/ui/hist.c

https://github.com/andygross/omap_dmm_tiler · C · 527 lines · 407 code · 116 blank · 4 comment · 30 complexity · 1f1342a055578eb374e25786e0e22f54 MD5 · raw file

  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. const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
  210. char buf[32] = " ";
  211. double diff;
  212. if (he->diff.computed)
  213. diff = he->diff.period_ratio_delta;
  214. else
  215. diff = perf_diff__compute_delta(he);
  216. if (fabs(diff) >= 0.01)
  217. scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
  218. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  219. }
  220. static int hpp__header_ratio(struct perf_hpp *hpp)
  221. {
  222. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  223. return scnprintf(hpp->buf, hpp->size, fmt, "Ratio");
  224. }
  225. static int hpp__width_ratio(struct perf_hpp *hpp __maybe_unused)
  226. {
  227. return 14;
  228. }
  229. static int hpp__entry_ratio(struct perf_hpp *hpp, struct hist_entry *he)
  230. {
  231. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  232. char buf[32] = " ";
  233. double ratio;
  234. if (he->diff.computed)
  235. ratio = he->diff.period_ratio;
  236. else
  237. ratio = perf_diff__compute_ratio(he);
  238. if (ratio > 0.0)
  239. scnprintf(buf, sizeof(buf), "%+14.6F", ratio);
  240. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  241. }
  242. static int hpp__header_wdiff(struct perf_hpp *hpp)
  243. {
  244. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  245. return scnprintf(hpp->buf, hpp->size, fmt, "Weighted diff");
  246. }
  247. static int hpp__width_wdiff(struct perf_hpp *hpp __maybe_unused)
  248. {
  249. return 14;
  250. }
  251. static int hpp__entry_wdiff(struct perf_hpp *hpp, struct hist_entry *he)
  252. {
  253. const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
  254. char buf[32] = " ";
  255. s64 wdiff;
  256. if (he->diff.computed)
  257. wdiff = he->diff.wdiff;
  258. else
  259. wdiff = perf_diff__compute_wdiff(he);
  260. if (wdiff != 0)
  261. scnprintf(buf, sizeof(buf), "%14ld", wdiff);
  262. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  263. }
  264. static int hpp__header_displ(struct perf_hpp *hpp)
  265. {
  266. return scnprintf(hpp->buf, hpp->size, "Displ.");
  267. }
  268. static int hpp__width_displ(struct perf_hpp *hpp __maybe_unused)
  269. {
  270. return 6;
  271. }
  272. static int hpp__entry_displ(struct perf_hpp *hpp,
  273. struct hist_entry *he)
  274. {
  275. struct hist_entry *pair = hist_entry__next_pair(he);
  276. long displacement = pair ? pair->position - he->position : 0;
  277. const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
  278. char buf[32] = " ";
  279. if (displacement)
  280. scnprintf(buf, sizeof(buf), "%+4ld", displacement);
  281. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  282. }
  283. static int hpp__header_formula(struct perf_hpp *hpp)
  284. {
  285. const char *fmt = symbol_conf.field_sep ? "%s" : "%70s";
  286. return scnprintf(hpp->buf, hpp->size, fmt, "Formula");
  287. }
  288. static int hpp__width_formula(struct perf_hpp *hpp __maybe_unused)
  289. {
  290. return 70;
  291. }
  292. static int hpp__entry_formula(struct perf_hpp *hpp, struct hist_entry *he)
  293. {
  294. const char *fmt = symbol_conf.field_sep ? "%s" : "%-70s";
  295. char buf[96] = " ";
  296. perf_diff__formula(buf, sizeof(buf), he);
  297. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  298. }
  299. #define HPP__COLOR_PRINT_FNS(_name) \
  300. .header = hpp__header_ ## _name, \
  301. .width = hpp__width_ ## _name, \
  302. .color = hpp__color_ ## _name, \
  303. .entry = hpp__entry_ ## _name
  304. #define HPP__PRINT_FNS(_name) \
  305. .header = hpp__header_ ## _name, \
  306. .width = hpp__width_ ## _name, \
  307. .entry = hpp__entry_ ## _name
  308. struct perf_hpp_fmt perf_hpp__format[] = {
  309. { .cond = false, HPP__COLOR_PRINT_FNS(baseline) },
  310. { .cond = true, HPP__COLOR_PRINT_FNS(overhead) },
  311. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_sys) },
  312. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_us) },
  313. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_sys) },
  314. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_us) },
  315. { .cond = false, HPP__PRINT_FNS(samples) },
  316. { .cond = false, HPP__PRINT_FNS(period) },
  317. { .cond = false, HPP__PRINT_FNS(period_baseline) },
  318. { .cond = false, HPP__PRINT_FNS(delta) },
  319. { .cond = false, HPP__PRINT_FNS(ratio) },
  320. { .cond = false, HPP__PRINT_FNS(wdiff) },
  321. { .cond = false, HPP__PRINT_FNS(displ) },
  322. { .cond = false, HPP__PRINT_FNS(formula) }
  323. };
  324. #undef HPP__COLOR_PRINT_FNS
  325. #undef HPP__PRINT_FNS
  326. void perf_hpp__init(void)
  327. {
  328. if (symbol_conf.show_cpu_utilization) {
  329. perf_hpp__format[PERF_HPP__OVERHEAD_SYS].cond = true;
  330. perf_hpp__format[PERF_HPP__OVERHEAD_US].cond = true;
  331. if (perf_guest) {
  332. perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].cond = true;
  333. perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].cond = true;
  334. }
  335. }
  336. if (symbol_conf.show_nr_samples)
  337. perf_hpp__format[PERF_HPP__SAMPLES].cond = true;
  338. if (symbol_conf.show_total_period)
  339. perf_hpp__format[PERF_HPP__PERIOD].cond = true;
  340. }
  341. void perf_hpp__column_enable(unsigned col, bool enable)
  342. {
  343. BUG_ON(col >= PERF_HPP__MAX_INDEX);
  344. perf_hpp__format[col].cond = enable;
  345. }
  346. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  347. {
  348. hpp->buf += inc;
  349. hpp->size -= inc;
  350. }
  351. int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
  352. bool color)
  353. {
  354. const char *sep = symbol_conf.field_sep;
  355. char *start = hpp->buf;
  356. int i, ret;
  357. bool first = true;
  358. if (symbol_conf.exclude_other && !he->parent)
  359. return 0;
  360. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  361. if (!perf_hpp__format[i].cond)
  362. continue;
  363. if (!sep || !first) {
  364. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  365. advance_hpp(hpp, ret);
  366. first = false;
  367. }
  368. if (color && perf_hpp__format[i].color)
  369. ret = perf_hpp__format[i].color(hpp, he);
  370. else
  371. ret = perf_hpp__format[i].entry(hpp, he);
  372. advance_hpp(hpp, ret);
  373. }
  374. return hpp->buf - start;
  375. }
  376. int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
  377. struct hists *hists)
  378. {
  379. const char *sep = symbol_conf.field_sep;
  380. struct sort_entry *se;
  381. int ret = 0;
  382. list_for_each_entry(se, &hist_entry__sort_list, list) {
  383. if (se->elide)
  384. continue;
  385. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  386. ret += se->se_snprintf(he, s + ret, size - ret,
  387. hists__col_len(hists, se->se_width_idx));
  388. }
  389. return ret;
  390. }
  391. /*
  392. * See hists__fprintf to match the column widths
  393. */
  394. unsigned int hists__sort_list_width(struct hists *hists)
  395. {
  396. struct sort_entry *se;
  397. int i, ret = 0;
  398. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  399. if (!perf_hpp__format[i].cond)
  400. continue;
  401. if (i)
  402. ret += 2;
  403. ret += perf_hpp__format[i].width(NULL);
  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. }