hist.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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)
  192. {
  193. struct hist_entry *pair = he->pair;
  194. long displacement = pair ? pair->position - he->position : 0;
  195. const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
  196. char buf[32] = " ";
  197. if (displacement)
  198. scnprintf(buf, sizeof(buf), "%+4ld", displacement);
  199. return scnprintf(hpp->buf, hpp->size, fmt, buf);
  200. }
  201. #define HPP__COLOR_PRINT_FNS(_name) \
  202. .header = hpp__header_ ## _name, \
  203. .width = hpp__width_ ## _name, \
  204. .color = hpp__color_ ## _name, \
  205. .entry = hpp__entry_ ## _name
  206. #define HPP__PRINT_FNS(_name) \
  207. .header = hpp__header_ ## _name, \
  208. .width = hpp__width_ ## _name, \
  209. .entry = hpp__entry_ ## _name
  210. struct perf_hpp_fmt perf_hpp__format[] = {
  211. { .cond = true, HPP__COLOR_PRINT_FNS(overhead) },
  212. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_sys) },
  213. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_us) },
  214. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_sys) },
  215. { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_us) },
  216. { .cond = false, HPP__PRINT_FNS(samples) },
  217. { .cond = false, HPP__PRINT_FNS(period) },
  218. { .cond = false, HPP__PRINT_FNS(delta) },
  219. { .cond = false, HPP__PRINT_FNS(displ) }
  220. };
  221. #undef HPP__COLOR_PRINT_FNS
  222. #undef HPP__PRINT_FNS
  223. void perf_hpp__init(bool need_pair, bool show_displacement)
  224. {
  225. if (symbol_conf.show_cpu_utilization) {
  226. perf_hpp__format[PERF_HPP__OVERHEAD_SYS].cond = true;
  227. perf_hpp__format[PERF_HPP__OVERHEAD_US].cond = true;
  228. if (perf_guest) {
  229. perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].cond = true;
  230. perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].cond = true;
  231. }
  232. }
  233. if (symbol_conf.show_nr_samples)
  234. perf_hpp__format[PERF_HPP__SAMPLES].cond = true;
  235. if (symbol_conf.show_total_period)
  236. perf_hpp__format[PERF_HPP__PERIOD].cond = true;
  237. if (need_pair) {
  238. perf_hpp__format[PERF_HPP__DELTA].cond = true;
  239. if (show_displacement)
  240. perf_hpp__format[PERF_HPP__DISPL].cond = true;
  241. }
  242. }
  243. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  244. {
  245. hpp->buf += inc;
  246. hpp->size -= inc;
  247. }
  248. int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
  249. bool color)
  250. {
  251. const char *sep = symbol_conf.field_sep;
  252. char *start = hpp->buf;
  253. int i, ret;
  254. if (symbol_conf.exclude_other && !he->parent)
  255. return 0;
  256. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  257. if (!perf_hpp__format[i].cond)
  258. continue;
  259. if (!sep || i > 0) {
  260. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  261. advance_hpp(hpp, ret);
  262. }
  263. if (color && perf_hpp__format[i].color)
  264. ret = perf_hpp__format[i].color(hpp, he);
  265. else
  266. ret = perf_hpp__format[i].entry(hpp, he);
  267. advance_hpp(hpp, ret);
  268. }
  269. return hpp->buf - start;
  270. }
  271. int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
  272. struct hists *hists)
  273. {
  274. const char *sep = symbol_conf.field_sep;
  275. struct sort_entry *se;
  276. int ret = 0;
  277. list_for_each_entry(se, &hist_entry__sort_list, list) {
  278. if (se->elide)
  279. continue;
  280. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  281. ret += se->se_snprintf(he, s + ret, size - ret,
  282. hists__col_len(hists, se->se_width_idx));
  283. }
  284. return ret;
  285. }
  286. /*
  287. * See hists__fprintf to match the column widths
  288. */
  289. unsigned int hists__sort_list_width(struct hists *hists)
  290. {
  291. struct sort_entry *se;
  292. int i, ret = 0;
  293. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  294. if (!perf_hpp__format[i].cond)
  295. continue;
  296. if (i)
  297. ret += 2;
  298. ret += perf_hpp__format[i].width(NULL);
  299. }
  300. list_for_each_entry(se, &hist_entry__sort_list, list)
  301. if (!se->elide)
  302. ret += 2 + hists__col_len(hists, se->se_width_idx);
  303. if (verbose) /* Addr + origin */
  304. ret += 3 + BITS_PER_LONG / 4;
  305. return ret;
  306. }