Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf stat: Improve readability of shadow stats

This adds function convert_unit_double() and selects appropriate
unit for shadow stats between K/M/G.

$ sudo perf stat -a -- sleep 1

Before: Unit 'M' is selected even the number is very small.

Performance counter stats for 'system wide':

4,003.06 msec cpu-clock # 3.998 CPUs utilized
16,179 context-switches # 0.004 M/sec
161 cpu-migrations # 0.040 K/sec
4,699 page-faults # 0.001 M/sec
6,135,801,925 cycles # 1.533 GHz (83.21%)
5,783,308,491 stalled-cycles-frontend # 94.26% frontend cycles idle (83.21%)
4,543,694,050 stalled-cycles-backend # 74.05% backend cycles idle (66.49%)
4,720,130,587 instructions # 0.77 insn per cycle
# 1.23 stalled cycles per insn (83.28%)
753,848,078 branches # 188.318 M/sec (83.61%)
37,457,747 branch-misses # 4.97% of all branches (83.48%)

1.001283725 seconds time elapsed

After:

$ sudo perf stat -a -- sleep 2

Performance counter stats for 'system wide':

8,005.52 msec cpu-clock # 3.999 CPUs utilized
10,715 context-switches # 1.338 K/sec
785 cpu-migrations # 98.057 /sec
102 page-faults # 12.741 /sec
1,948,202,279 cycles # 0.243 GHz
2,816,470,932 stalled-cycles-frontend # 144.57% frontend cycles idle
2,661,172,207 stalled-cycles-backend # 136.60% backend cycles idle
464,172,105 instructions # 0.24 insn per cycle
# 6.07 stalled cycles per insn
91,567,662 branches # 11.438 M/sec
7,756,054 branch-misses # 8.47% of all branches

2.002040043 seconds time elapsed

v2:
o do not change 'sec' to 'cpu-sec'.
o use convert_unit_double to implement convert_unit.

Signed-off-by: Changbin Du <changbin.du@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20210315143047.3867-1-changbin.du@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Changbin Du and committed by
Arnaldo Carvalho de Melo
6859bc0e 4a03af3e

+22 -16
+7 -9
tools/perf/util/stat-shadow.c
··· 9 9 #include "expr.h" 10 10 #include "metricgroup.h" 11 11 #include "cgroup.h" 12 + #include "units.h" 12 13 #include <linux/zalloc.h> 13 14 14 15 /* ··· 1271 1270 generic_metric(config, evsel->metric_expr, evsel->metric_events, NULL, 1272 1271 evsel->name, evsel->metric_name, NULL, 1, cpu, out, st); 1273 1272 } else if (runtime_stat_n(st, STAT_NSECS, cpu, &rsd) != 0) { 1274 - char unit = 'M'; 1275 - char unit_buf[10]; 1273 + char unit = ' '; 1274 + char unit_buf[10] = "/sec"; 1276 1275 1277 1276 total = runtime_stat_avg(st, STAT_NSECS, cpu, &rsd); 1278 - 1279 1277 if (total) 1280 - ratio = 1000.0 * avg / total; 1281 - if (ratio < 0.001) { 1282 - ratio *= 1000; 1283 - unit = 'K'; 1284 - } 1285 - snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit); 1278 + ratio = convert_unit_double(1000000000.0 * avg / total, &unit); 1279 + 1280 + if (unit != ' ') 1281 + snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit); 1286 1282 print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio); 1287 1283 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) { 1288 1284 print_smi_cost(config, cpu, out, st, &rsd);
+14 -7
tools/perf/util/units.c
··· 33 33 return (unsigned long) -1; 34 34 } 35 35 36 - unsigned long convert_unit(unsigned long value, char *unit) 36 + double convert_unit_double(double value, char *unit) 37 37 { 38 38 *unit = ' '; 39 39 40 - if (value > 1000) { 41 - value /= 1000; 40 + if (value > 1000.0) { 41 + value /= 1000.0; 42 42 *unit = 'K'; 43 43 } 44 44 45 - if (value > 1000) { 46 - value /= 1000; 45 + if (value > 1000.0) { 46 + value /= 1000.0; 47 47 *unit = 'M'; 48 48 } 49 49 50 - if (value > 1000) { 51 - value /= 1000; 50 + if (value > 1000.0) { 51 + value /= 1000.0; 52 52 *unit = 'G'; 53 53 } 54 54 55 55 return value; 56 + } 57 + 58 + unsigned long convert_unit(unsigned long value, char *unit) 59 + { 60 + double v = convert_unit_double((double)value, unit); 61 + 62 + return (unsigned long)v; 56 63 } 57 64 58 65 int unit_number__scnprintf(char *buf, size_t size, u64 n)
+1
tools/perf/util/units.h
··· 12 12 13 13 unsigned long parse_tag_value(const char *str, struct parse_tag *tags); 14 14 15 + double convert_unit_double(double value, char *unit); 15 16 unsigned long convert_unit(unsigned long value, char *unit); 16 17 int unit_number__scnprintf(char *buf, size_t size, u64 n); 17 18