* [PATCH] perf metrics: Add literal for system TSC frequency
@ 2022-02-03 1:07 Ian Rogers
2022-02-03 3:59 ` Andi Kleen
0 siblings, 1 reply; 3+ messages in thread
From: Ian Rogers @ 2022-02-03 1:07 UTC (permalink / raw)
To: vineet.singh, perry.taylor, caleb.biggers, asaf.yaffe,
kshipra.bopardikar, Kan Liang, Zhengjun Xing, Peter Zijlstra,
Ingo Molnar, Arnaldo Carvalho de Melo, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Namhyung Kim, Maxime Coquelin,
Alexandre Torgue, Andi Kleen, James Clark, John Garry,
linux-kernel, linux-perf-users
Cc: Stephane Eranian, 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/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
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/expr.c | 42 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
index 675f318ce7c1..7ad01ed57025 100644
--- a/tools/perf/util/expr.c
+++ b/tools/perf/util/expr.c
@@ -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
--
2.35.0.rc2.247.g8bbb082509-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] perf metrics: Add literal for system TSC frequency
2022-02-03 1:07 [PATCH] perf metrics: Add literal for system TSC frequency Ian Rogers
@ 2022-02-03 3:59 ` Andi Kleen
2022-02-03 7:58 ` Ian Rogers
0 siblings, 1 reply; 3+ messages in thread
From: Andi Kleen @ 2022-02-03 3:59 UTC (permalink / raw)
To: Ian Rogers, vineet.singh, perry.taylor, caleb.biggers,
asaf.yaffe, kshipra.bopardikar, Kan Liang, Zhengjun Xing,
Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
Maxime Coquelin, Alexandre Torgue, James Clark, John Garry,
linux-kernel, linux-perf-users
Cc: Stephane Eranian
On 2/2/2022 5:07 PM, Ian Rogers wrote:
> 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.
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] perf metrics: Add literal for system TSC frequency
2022-02-03 3:59 ` Andi Kleen
@ 2022-02-03 7:58 ` Ian Rogers
0 siblings, 0 replies; 3+ messages in thread
From: Ian Rogers @ 2022-02-03 7:58 UTC (permalink / raw)
To: Andi Kleen
Cc: vineet.singh, perry.taylor, caleb.biggers, asaf.yaffe,
kshipra.bopardikar, Kan Liang, Zhengjun Xing, Peter Zijlstra,
Ingo Molnar, Arnaldo Carvalho de Melo, Mark Rutland,
Alexander Shishkin, Jiri Olsa, Namhyung Kim, Maxime Coquelin,
Alexandre Torgue, James Clark, John Garry, linux-kernel,
linux-perf-users, Stephane Eranian
On Wed, Feb 2, 2022 at 7:59 PM Andi Kleen <ak@linux.intel.com> wrote:
>
>
> On 2/2/2022 5:07 PM, Ian Rogers wrote:
> > 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.
>
> 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.
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
> -Andi
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-02-03 7:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03 1:07 [PATCH] perf metrics: Add literal for system TSC frequency Ian Rogers
2022-02-03 3:59 ` Andi Kleen
2022-02-03 7:58 ` Ian Rogers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®