perf metrics: Add literal for system TSC frequency
Ian Rogers
reviewerPATCH 1/1 perf metrics: Add literal for system TSC frequency
Such a literal is useful to calculate things like the average frequency [1]. The TSC frequency isn't exposed by sysfs although some experimental drivers look to add it [2]. This change computes the value using the frequency in /proc/cpuinfo which is accruate at least on Intel processors.
[1] https://github.com/intel/perfmon-metrics/blob/5ad9ef7056f31075e8178b9f1fb732af183b2c8d/SKX/metrics/perf/skx_metric_perf.json#L11
[2] https://github.com/trailofbits/tsc_freq_khz
--- tools/perf/util/expr.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
tools/perf/util/expr.c +42 -0
@@ -402,6 +402,43 @@ double expr_id_data__source_count(const struct expr_id_data *data)return data->val.source_count; } +/* +* Derive the TSC frequency in Hz from the /proc/cpuinfo, for example: +* ... +* model name : Intel(R) Xeon(R) Gold 6154 CPU @ 3.00GHz +* ... +* will return 3000000000. +*/ +static double system_tsc_freq (void ) +{ +double result =NAN ; +FILE *cpuinfo; +char *line =NULL ; +size_t len =0 ; + + cpuinfo =fopen ("/proc/cpuinfo" ,"r" ); +if (!cpuinfo) +return NAN ; + +while (getline (&line, &len, cpuinfo) >0 ) { +if (!strncmp (line,"model name" ,10 )) { +char *pos = line +11 ; + +while (*pos !='@' ) + pos++; +if (sscanf (pos," %lfGHz" , &result) ==1 ) { + result *=1000000000 ; + goto out; + } + } + } + +out: +free (line); +fclose (cpuinfo); +return result; +} +double expr__get_literal (const char *literal) {static struct cpu_topology *topology;
@@ -417,6 +454,11 @@ double expr__get_literal(const char *literal) goto out; } +if (!strcasecmp ("#system_tsc_freq" , literal)) { + result =system_tsc_freq (); + goto out; + } +/* * Assume that topology strings are consistent, such as CPUs "0-1" * wouldn't be listed as "0,1", and so after deduplication the number of
Signature · 1 line
2.35.0.rc2.247.g8bbb082509-goog
Quoted 5 lines · Ian Rogers
Such a literal is useful to calculate things like the average frequency [1]. The TSC frequency isn't exposed by sysfs although some experimental drivers look to add it [2]. This change computes the value using the frequency in
/proc/cpuinfowhich is accruate at least on Intel processors.
FWIW the information in /proc/cpuinfo is going away.
You should really use CPUID for this, which has a new leaf for this. Unfortunately there
were a few systems were it was inaccurate, but I suppose you could still prefer /proc/cpuinfo
Also your method will not work when the metrics are used through perf stat report or perf script
and run on another system. For the perf script case there is support for putting the TSC into
perf.data (that is needed for PT), but the header is not dumped by default unfortunately.
So likely you would need some new mechanism for that.
-Andi
Ian Rogers
reviewerQuoted 20 lines · Andi Kleen
Quoted 5 lines · Ian Rogers
Such a literal is useful to calculate things like the average frequency [1]. The TSC frequency isn't exposed by sysfs although some experimental drivers look to add it [2]. This change computes the value using the frequency in
/proc/cpuinfowhich is accruate at least on Intel processors.FWIW the information in
/proc/cpuinfois going away.You should really use CPUID for this, which has a new leaf for this. Unfortunately there
were a few systems were it was inaccurate, but I suppose you could still prefer
/proc/cpuinfoAlso your method will not work when the metrics are used through perf stat report or perf script
Quoted 7 lines
and run on another system. For the perf script case there is support for putting the TSC into
perf.data (that is needed for PT), but the header is not dumped by default unfortunately.
So likely you would need some new mechanism for that.
Thanks Andi,
yep metrics with perf stat record are broken on a number of fronts and this will be another that will need a fix. It looks like the cpuid approach won't work pre-Skylake so sysfs could act as a fallback, but I don't have an ETA on when that will be available. We could have additional warnings when reading /proc/cpuinfo fails. Wdyt?
Thanks,
Ian
Quoted 2 lines
-Andi