hist.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. const char *fmt = hpp->ptr ? "Baseline" : "Overhead";
  9. return scnprintf(hpp->buf, hpp->size, fmt);
  10. }
  11. static int hpp__width_overhead(struct perf_hpp *hpp __maybe_unused)
  12. {
  13. return 8;
  14. }
  15. static int hpp__color_overhead(struct perf_hpp *hpp, struct hist_entry *he)
  16. {
  17. double percent = 100.0 * he->period / hpp->total_period;
  18. if (hpp->ptr) {
  19. struct hists *old_hists = hpp->ptr;
  20. u64 total_period = old_hists->stats.total_period;
  21. u64 base_period = he->pair ? he->pair->period : 0;
  22. if (total_period)
  23. percent = 100.0 * base_period / total_period;
  24. else
  25. percent = 0.0;
  26. }
  27. return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
  28. }
  29. static int hpp__entry_overhead(struct perf_hpp *hpp, struct hist_entry *he)
  30. {
  31. double percent = 100.0 * he->period / hpp->total_period;
  32. const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
  33. if (hpp->ptr) {
  34. struct hists *old_hists = hpp->ptr;
  35. u64 total_period = old_hists->stats.total_period;
  36. u64 base_period = he->pair ? he->pair->period : 0;
  37. if (total_period)
  38. percent = 100.0 * base_period / total_period;
  39. else
  40. percent = 0.0;
  41. }
  42. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  43. }
  44. static int hpp__header_overhead_sys(struct perf_hpp *hpp)
  45. {
  46. const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
  47. return scnprintf(hpp->buf, hpp->size, fmt, "sys");
  48. }
  49. static int hpp__width_overhead_sys(struct perf_hpp *hpp __maybe_unused)
  50. {
  51. return 7;
  52. }
  53. static int hpp__color_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
  54. {
  55. double percent = 100.0 * he->period_sys / hpp->total_period;
  56. return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
  57. }
  58. static int hpp__entry_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
  59. {
  60. double percent = 100.0 * he->period_sys / hpp->total_period;
  61. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  62. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  63. }
  64. static int hpp__header_overhead_us(struct perf_hpp *hpp)
  65. {
  66. const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
  67. return scnprintf(hpp->buf, hpp->size, fmt, "user");
  68. }
  69. static int hpp__width_overhead_us(struct perf_hpp *hpp __maybe_unused)
  70. {
  71. return 7;
  72. }
  73. static int hpp__color_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
  74. {
  75. double percent = 100.0 * he->period_us / hpp->total_period;
  76. return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
  77. }
  78. static int hpp__entry_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
  79. {
  80. double percent = 100.0 * he->period_us / hpp->total_period;
  81. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  82. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  83. }
  84. static int hpp__header_overhead_guest_sys(struct perf_hpp *hpp)
  85. {
  86. return scnprintf(hpp->buf, hpp->size, "guest sys");
  87. }
  88. static int hpp__width_overhead_guest_sys(struct perf_hpp *hpp __maybe_unused)
  89. {
  90. return 9;
  91. }
  92. static int hpp__color_overhead_guest_sys(struct perf_hpp *hpp,
  93. struct hist_entry *he)
  94. {
  95. double percent = 100.0 * he->period_guest_sys / hpp->total_period;
  96. return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
  97. }
  98. static int hpp__entry_overhead_guest_sys(struct perf_hpp *hpp,
  99. struct hist_entry *he)
  100. {
  101. double percent = 100.0 * he->period_guest_sys / hpp->total_period;
  102. const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
  103. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  104. }
  105. static int hpp__header_overhead_guest_us(struct perf_hpp *hpp)
  106. {
  107. return scnprintf(hpp->buf, hpp->size, "guest usr");
  108. }
  109. static int hpp__width_overhead_guest_us(struct perf_hpp *hpp __maybe_unused)
  110. {
  111. return 9;
  112. }
  113. static int hpp__color_overhead_guest_us(struct perf_hpp *hpp,
  114. struct hist_entry *he)
  115. {
  116. double percent = 100.0 * he->period_guest_us / hpp->total_period;
  117. return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
  118. }
  119. static int hpp__entry_overhead_guest_us(struct perf_hpp *hpp,
  120. struct hist_entry *he)
  121. {
  122. double percent = 100.0 * he->period_guest_us / hpp->total_period;
  123. const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
  124. return scnprintf(hpp->buf, hpp->size, fmt, percent);
  125. }
  126. static int hpp__header_samples(struct perf_hpp *hpp)
  127. {
  128. const char *fmt = symbol_conf.field_sep ? "%s" : "%11s";
  129. return scnprintf(hpp->buf, hpp->size, fmt, "Samples");
  130. }
  131. static int hpp__width_samples(struct perf_hpp *hpp __maybe_unused)
  132. {
  133. return 11;
  134. }
  135. static int hpp__entry_samples(struct perf_hpp *hpp, struct hist_entry *he)
  136. {
  137. const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%11" PRIu64;
  138. return scnprintf(hpp->buf, hpp->size, fmt, he->nr_events);
  139. }
  140. static int hpp__header_period(struct perf_hpp *hpp)
  141. {
  142. const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
  143. return scnprintf(hpp->buf, hpp->size, fmt, "Period");
  144. }
  145. static int hpp__width_period(struct perf_hpp *hpp __maybe_unused)
  146. {
  147. return 12;
  148. }
  149. static int hpp__entry_period(struct perf_hpp *hpp, struct hist_entry *he)
  150. {
  151. const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
  152. return scnprintf(hpp->buf, hpp->size, fmt, he->period);
  153. }
  154. static int hpp__header_delta(struct perf_hpp *hpp)
  155. {
  156. const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
  157. return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
  158. }
  159. static int hpp__width_delta(struct perf_hpp *hpp __maybe_unused)
  160. {
  161. return 7;
  162. }
  163. static int hpp__entry_delta(struct perf_hpp *hpp, struct hist_entry *he)
  164. {
  165. struct hists *pair_hists = hpp->ptr;
  166. u64 old_total, new_total;
  167. double old_percent = 0, new_percent = 0;
  168. double diff;
  169. const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
  170. char buf[32] = " ";
  171. old_total = pair_hists->stats.total_period;
  172. if (old_total > 0 && he->pair)
  173. old_percent = 100.0 * he->pair->period / old_total;
  174. new_total = hpp->total_period;
  175. if (new_total > 0)
  176. new_percent = 100.0 * he->period / new_total;
  177. diff = new_percent - old_percent;
  178. if (fabs(diff) >= 0.01)
  179. scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
  180. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  181. }
  182. static int hpp__header_displ(struct perf_hpp *hpp)
  183. {
  184. return scnprintf(hpp->buf, hpp->size, "Displ.");
  185. }
  186. static int hpp__width_displ(struct perf_hpp *hpp __maybe_unused)
  187. {
  188. return 6;
  189. }
  190. static int hpp__entry_displ(struct perf_hpp *hpp,
  191. struct hist_entry *he __maybe_unused)
  192. {
  193. const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
  194. char buf[32] = " ";
  195. if (hpp->displacement)
  196. scnprintf(buf, sizeof(buf), "%+4ld", hpp->displacement);
  197. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  198. }
  199. #define HPP__COLOR_PRINT_FNS(_name) \
  200. .header = hpp__header_ ## _name, \
  201. .width = hpp__width_ ## _name, \
  202. .color = hpp__color_ ## _name, \
  203. .entry = hpp__entry_ ## _name
  204. #define HPP__PRINT_FNS(_name) \
  205. .header = hpp__header_ ## _name, \
  206. .width = hpp__width_ ## _name, \
  207. .entry = hpp__entry_ ## _name
  208. struct perf_hpp_fmt perf_hpp__format[] = {
  209. { .cond = true, HPP__COLOR_PRINT_FNS(overhead) },
  210. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_sys) },
  211. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_us) },
  212. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_sys) },
  213. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_us) },
  214. { .cond = false, HPP__PRINT_FNS(samples) },
  215. { .cond = false, HPP__PRINT_FNS(period) },
  216. { .cond = false, HPP__PRINT_FNS(delta) },
  217. { .cond = false, HPP__PRINT_FNS(displ) }
  218. };
  219. #undef HPP__COLOR_PRINT_FNS
  220. #undef HPP__PRINT_FNS
  221. void perf_hpp__init(bool need_pair, bool show_displacement)
  222. {
  223. if (symbol_conf.show_cpu_utilization) {
  224. perf_hpp__format[PERF_HPP__OVERHEAD_SYS].cond = true;
  225. perf_hpp__format[PERF_HPP__OVERHEAD_US].cond = true;
  226. if (perf_guest) {
  227. perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].cond = true;
  228. perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].cond = true;
  229. }
  230. }
  231. if (symbol_conf.show_nr_samples)
  232. perf_hpp__format[PERF_HPP__SAMPLES].cond = true;
  233. if (symbol_conf.show_total_period)
  234. perf_hpp__format[PERF_HPP__PERIOD].cond = true;
  235. if (need_pair) {
  236. perf_hpp__format[PERF_HPP__DELTA].cond = true;
  237. if (show_displacement)
  238. perf_hpp__format[PERF_HPP__DISPL].cond = true;
  239. }
  240. }
  241. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  242. {
  243. hpp->buf += inc;
  244. hpp->size -= inc;
  245. }
  246. int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
  247. bool color)
  248. {
  249. const char *sep = symbol_conf.field_sep;
  250. char *start = hpp->buf;
  251. int i, ret;
  252. if (symbol_conf.exclude_other && !he->parent)
  253. return 0;
  254. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  255. if (!perf_hpp__format[i].cond)
  256. continue;
  257. if (!sep || i > 0) {
  258. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  259. advance_hpp(hpp, ret);
  260. }
  261. if (color && perf_hpp__format[i].color)
  262. ret = perf_hpp__format[i].color(hpp, he);
  263. else
  264. ret = perf_hpp__format[i].entry(hpp, he);
  265. advance_hpp(hpp, ret);
  266. }
  267. return hpp->buf - start;
  268. }
  269. int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
  270. struct hists *hists)
  271. {
  272. const char *sep = symbol_conf.field_sep;
  273. struct sort_entry *se;
  274. int ret = 0;
  275. list_for_each_entry(se, &hist_entry__sort_list, list) {
  276. if (se->elide)
  277. continue;
  278. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  279. ret += se->se_snprintf(he, s + ret, size - ret,
  280. hists__col_len(hists, se->se_width_idx));
  281. }
  282. return ret;
  283. }
  284. /*
  285. * See hists__fprintf to match the column widths
  286. */
  287. unsigned int hists__sort_list_width(struct hists *hists)
  288. {
  289. struct sort_entry *se;
  290. int i, ret = 0;
  291. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  292. if (!perf_hpp__format[i].cond)
  293. continue;
  294. if (i)
  295. ret += 2;
  296. ret += perf_hpp__format[i].width(NULL);
  297. }
  298. list_for_each_entry(se, &hist_entry__sort_list, list)
  299. if (!se->elide)
  300. ret += 2 + hists__col_len(hists, se->se_width_idx);
  301. if (verbose) /* Addr + origin */
  302. ret += 3 + BITS_PER_LONG / 4;
  303. return ret;
  304. }