hist.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #define hpp__call_print_fn(hpp, fn, fmt, ...) \
  9. ({ \
  10. int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
  11. advance_hpp(hpp, __ret); \
  12. __ret; \
  13. })
  14. int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
  15. hpp_field_fn get_field, hpp_callback_fn callback,
  16. const char *fmt, hpp_snprint_fn print_fn, bool fmt_percent)
  17. {
  18. int ret = 0;
  19. struct hists *hists = he->hists;
  20. struct perf_evsel *evsel = hists_to_evsel(hists);
  21. char *buf = hpp->buf;
  22. size_t size = hpp->size;
  23. if (callback) {
  24. ret = callback(hpp, true);
  25. advance_hpp(hpp, ret);
  26. }
  27. if (fmt_percent) {
  28. double percent = 0.0;
  29. if (hists->stats.total_period)
  30. percent = 100.0 * get_field(he) /
  31. hists->stats.total_period;
  32. ret += hpp__call_print_fn(hpp, print_fn, fmt, percent);
  33. } else
  34. ret += hpp__call_print_fn(hpp, print_fn, fmt, get_field(he));
  35. if (perf_evsel__is_group_event(evsel)) {
  36. int prev_idx, idx_delta;
  37. struct hist_entry *pair;
  38. int nr_members = evsel->nr_members;
  39. prev_idx = perf_evsel__group_idx(evsel);
  40. list_for_each_entry(pair, &he->pairs.head, pairs.node) {
  41. u64 period = get_field(pair);
  42. u64 total = pair->hists->stats.total_period;
  43. if (!total)
  44. continue;
  45. evsel = hists_to_evsel(pair->hists);
  46. idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
  47. while (idx_delta--) {
  48. /*
  49. * zero-fill group members in the middle which
  50. * have no sample
  51. */
  52. if (fmt_percent) {
  53. ret += hpp__call_print_fn(hpp, print_fn,
  54. fmt, 0.0);
  55. } else {
  56. ret += hpp__call_print_fn(hpp, print_fn,
  57. fmt, 0ULL);
  58. }
  59. }
  60. if (fmt_percent) {
  61. ret += hpp__call_print_fn(hpp, print_fn, fmt,
  62. 100.0 * period / total);
  63. } else {
  64. ret += hpp__call_print_fn(hpp, print_fn, fmt,
  65. period);
  66. }
  67. prev_idx = perf_evsel__group_idx(evsel);
  68. }
  69. idx_delta = nr_members - prev_idx - 1;
  70. while (idx_delta--) {
  71. /*
  72. * zero-fill group members at last which have no sample
  73. */
  74. if (fmt_percent) {
  75. ret += hpp__call_print_fn(hpp, print_fn,
  76. fmt, 0.0);
  77. } else {
  78. ret += hpp__call_print_fn(hpp, print_fn,
  79. fmt, 0ULL);
  80. }
  81. }
  82. }
  83. if (callback) {
  84. int __ret = callback(hpp, false);
  85. advance_hpp(hpp, __ret);
  86. ret += __ret;
  87. }
  88. /*
  89. * Restore original buf and size as it's where caller expects
  90. * the result will be saved.
  91. */
  92. hpp->buf = buf;
  93. hpp->size = size;
  94. return ret;
  95. }
  96. #define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
  97. static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  98. struct perf_hpp *hpp, \
  99. struct perf_evsel *evsel) \
  100. { \
  101. int len = _min_width; \
  102. \
  103. if (symbol_conf.event_group) \
  104. len = max(len, evsel->nr_members * _unit_width); \
  105. \
  106. return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
  107. }
  108. #define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
  109. static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  110. struct perf_hpp *hpp __maybe_unused, \
  111. struct perf_evsel *evsel) \
  112. { \
  113. int len = _min_width; \
  114. \
  115. if (symbol_conf.event_group) \
  116. len = max(len, evsel->nr_members * _unit_width); \
  117. \
  118. return len; \
  119. }
  120. static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
  121. {
  122. va_list args;
  123. ssize_t ssize = hpp->size;
  124. double percent;
  125. int ret;
  126. va_start(args, fmt);
  127. percent = va_arg(args, double);
  128. ret = value_color_snprintf(hpp->buf, hpp->size, fmt, percent);
  129. va_end(args);
  130. return (ret >= ssize) ? (ssize - 1) : ret;
  131. }
  132. static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
  133. {
  134. va_list args;
  135. ssize_t ssize = hpp->size;
  136. int ret;
  137. va_start(args, fmt);
  138. ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
  139. va_end(args);
  140. return (ret >= ssize) ? (ssize - 1) : ret;
  141. }
  142. #define __HPP_COLOR_PERCENT_FN(_type, _field) \
  143. static u64 he_get_##_field(struct hist_entry *he) \
  144. { \
  145. return he->stat._field; \
  146. } \
  147. \
  148. static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  149. struct perf_hpp *hpp, struct hist_entry *he) \
  150. { \
  151. return __hpp__fmt(hpp, he, he_get_##_field, NULL, " %6.2f%%", \
  152. hpp_color_scnprintf, true); \
  153. }
  154. #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
  155. static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
  156. struct perf_hpp *hpp, struct hist_entry *he) \
  157. { \
  158. const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
  159. return __hpp__fmt(hpp, he, he_get_##_field, NULL, fmt, \
  160. hpp_entry_scnprintf, true); \
  161. }
  162. #define __HPP_ENTRY_RAW_FN(_type, _field) \
  163. static u64 he_get_raw_##_field(struct hist_entry *he) \
  164. { \
  165. return he->stat._field; \
  166. } \
  167. \
  168. static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
  169. struct perf_hpp *hpp, struct hist_entry *he) \
  170. { \
  171. const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
  172. return __hpp__fmt(hpp, he, he_get_raw_##_field, NULL, fmt, \
  173. hpp_entry_scnprintf, false); \
  174. }
  175. #define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
  176. __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
  177. __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
  178. __HPP_COLOR_PERCENT_FN(_type, _field) \
  179. __HPP_ENTRY_PERCENT_FN(_type, _field)
  180. #define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
  181. __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
  182. __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
  183. __HPP_ENTRY_RAW_FN(_type, _field)
  184. HPP_PERCENT_FNS(overhead, "Overhead", period, 8, 8)
  185. HPP_PERCENT_FNS(overhead_sys, "sys", period_sys, 8, 8)
  186. HPP_PERCENT_FNS(overhead_us, "usr", period_us, 8, 8)
  187. HPP_PERCENT_FNS(overhead_guest_sys, "guest sys", period_guest_sys, 9, 8)
  188. HPP_PERCENT_FNS(overhead_guest_us, "guest usr", period_guest_us, 9, 8)
  189. HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
  190. HPP_RAW_FNS(period, "Period", period, 12, 12)
  191. #define HPP__COLOR_PRINT_FNS(_name) \
  192. { \
  193. .header = hpp__header_ ## _name, \
  194. .width = hpp__width_ ## _name, \
  195. .color = hpp__color_ ## _name, \
  196. .entry = hpp__entry_ ## _name \
  197. }
  198. #define HPP__PRINT_FNS(_name) \
  199. { \
  200. .header = hpp__header_ ## _name, \
  201. .width = hpp__width_ ## _name, \
  202. .entry = hpp__entry_ ## _name \
  203. }
  204. struct perf_hpp_fmt perf_hpp__format[] = {
  205. HPP__COLOR_PRINT_FNS(overhead),
  206. HPP__COLOR_PRINT_FNS(overhead_sys),
  207. HPP__COLOR_PRINT_FNS(overhead_us),
  208. HPP__COLOR_PRINT_FNS(overhead_guest_sys),
  209. HPP__COLOR_PRINT_FNS(overhead_guest_us),
  210. HPP__PRINT_FNS(samples),
  211. HPP__PRINT_FNS(period)
  212. };
  213. LIST_HEAD(perf_hpp__list);
  214. #undef HPP__COLOR_PRINT_FNS
  215. #undef HPP__PRINT_FNS
  216. #undef HPP_PERCENT_FNS
  217. #undef HPP_RAW_FNS
  218. #undef __HPP_HEADER_FN
  219. #undef __HPP_WIDTH_FN
  220. #undef __HPP_COLOR_PERCENT_FN
  221. #undef __HPP_ENTRY_PERCENT_FN
  222. #undef __HPP_ENTRY_RAW_FN
  223. void perf_hpp__init(void)
  224. {
  225. perf_hpp__column_enable(PERF_HPP__OVERHEAD);
  226. if (symbol_conf.show_cpu_utilization) {
  227. perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
  228. perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
  229. if (perf_guest) {
  230. perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
  231. perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
  232. }
  233. }
  234. if (symbol_conf.show_nr_samples)
  235. perf_hpp__column_enable(PERF_HPP__SAMPLES);
  236. if (symbol_conf.show_total_period)
  237. perf_hpp__column_enable(PERF_HPP__PERIOD);
  238. }
  239. void perf_hpp__column_register(struct perf_hpp_fmt *format)
  240. {
  241. list_add_tail(&format->list, &perf_hpp__list);
  242. }
  243. void perf_hpp__column_enable(unsigned col)
  244. {
  245. BUG_ON(col >= PERF_HPP__MAX_INDEX);
  246. perf_hpp__column_register(&perf_hpp__format[col]);
  247. }
  248. int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
  249. struct hists *hists)
  250. {
  251. const char *sep = symbol_conf.field_sep;
  252. struct sort_entry *se;
  253. int ret = 0;
  254. list_for_each_entry(se, &hist_entry__sort_list, list) {
  255. if (se->elide)
  256. continue;
  257. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  258. ret += se->se_snprintf(he, s + ret, size - ret,
  259. hists__col_len(hists, se->se_width_idx));
  260. }
  261. return ret;
  262. }
  263. /*
  264. * See hists__fprintf to match the column widths
  265. */
  266. unsigned int hists__sort_list_width(struct hists *hists)
  267. {
  268. struct perf_hpp_fmt *fmt;
  269. struct sort_entry *se;
  270. int i = 0, ret = 0;
  271. struct perf_hpp dummy_hpp;
  272. perf_hpp__for_each_format(fmt) {
  273. if (i)
  274. ret += 2;
  275. ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
  276. }
  277. list_for_each_entry(se, &hist_entry__sort_list, list)
  278. if (!se->elide)
  279. ret += 2 + hists__col_len(hists, se->se_width_idx);
  280. if (verbose) /* Addr + origin */
  281. ret += 3 + BITS_PER_LONG / 4;
  282. return ret;
  283. }