[PATCH v1] perf tool_pmu: Fix aggregation on duration_time

Ian Rogers posted 1 patch 9 months, 3 weeks ago
tools/perf/util/tool_pmu.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
[PATCH v1] perf tool_pmu: Fix aggregation on duration_time
Posted by Ian Rogers 9 months, 3 weeks ago
evsel__count_has_error fails counters when the enabled or running time
are 0. The duration_time event reads 0 when the cpu_map_idx != 0 to
avoid aggregating time over CPUs. Change the enable and running time
to always have a ratio of 100% so that evsel__count_has_error won't
fail.

Before:
```
$ sudo /tmp/perf/perf stat --per-core -a -M UNCORE_FREQ sleep 1

 Performance counter stats for 'system wide':

S0-D0-C0              1      2,615,819,485      UNC_CLOCK.SOCKET                 #     2.61 UNCORE_FREQ
S0-D0-C0              2      <not counted>      duration_time

       1.002111784 seconds time elapsed
```

After:
```
$ perf stat --per-core -a -M UNCORE_FREQ sleep 1

 Performance counter stats for 'system wide':

S0-D0-C0              1        758,160,296      UNC_CLOCK.SOCKET                 #     0.76 UNCORE_FREQ
S0-D0-C0              2      1,003,438,246      duration_time

       1.002486017 seconds time elapsed
```

Note: the metric reads the value a different way and isn't impacted.

Reported-by: Stephane Eranian <eranian@google.com>
Fixes: 240505b2d0ad ("perf tool_pmu: Factor tool events into their own PMU")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/tool_pmu.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c
index 97b327d1ce4a..727a10e3f990 100644
--- a/tools/perf/util/tool_pmu.c
+++ b/tools/perf/util/tool_pmu.c
@@ -486,8 +486,14 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
 		delta_start *= 1000000000 / ticks_per_sec;
 	}
 	count->val    = delta_start;
-	count->ena    = count->run = delta_start;
 	count->lost   = 0;
+	/*
+	 * The values of enabled and running must make a ratio of 100%. The
+	 * exact values don't matter as long as they are non-zero to avoid
+	 * issues with evsel__count_has_error.
+	 */
+	count->ena++;
+	count->run++;
 	return 0;
 }
 
-- 
2.49.0.805.g082f7c87e0-goog
Re: [PATCH v1] perf tool_pmu: Fix aggregation on duration_time
Posted by James Clark 9 months, 3 weeks ago

On 23/04/2025 6:03 am, Ian Rogers wrote:
> evsel__count_has_error fails counters when the enabled or running time
> are 0. The duration_time event reads 0 when the cpu_map_idx != 0 to
> avoid aggregating time over CPUs. Change the enable and running time
> to always have a ratio of 100% so that evsel__count_has_error won't
> fail.
> 
> Before:
> ```
> $ sudo /tmp/perf/perf stat --per-core -a -M UNCORE_FREQ sleep 1
> 
>   Performance counter stats for 'system wide':
> 
> S0-D0-C0              1      2,615,819,485      UNC_CLOCK.SOCKET                 #     2.61 UNCORE_FREQ
> S0-D0-C0              2      <not counted>      duration_time
> 
>         1.002111784 seconds time elapsed
> ```
> 
> After:
> ```
> $ perf stat --per-core -a -M UNCORE_FREQ sleep 1
> 
>   Performance counter stats for 'system wide':
> 
> S0-D0-C0              1        758,160,296      UNC_CLOCK.SOCKET                 #     0.76 UNCORE_FREQ
> S0-D0-C0              2      1,003,438,246      duration_time
> 
>         1.002486017 seconds time elapsed
> ```
> 
> Note: the metric reads the value a different way and isn't impacted.
> 
> Reported-by: Stephane Eranian <eranian@google.com>
> Fixes: 240505b2d0ad ("perf tool_pmu: Factor tool events into their own PMU")
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>   tools/perf/util/tool_pmu.c | 8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c
> index 97b327d1ce4a..727a10e3f990 100644
> --- a/tools/perf/util/tool_pmu.c
> +++ b/tools/perf/util/tool_pmu.c
> @@ -486,8 +486,14 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
>   		delta_start *= 1000000000 / ticks_per_sec;
>   	}
>   	count->val    = delta_start;
> -	count->ena    = count->run = delta_start;
>   	count->lost   = 0;
> +	/*
> +	 * The values of enabled and running must make a ratio of 100%. The
> +	 * exact values don't matter as long as they are non-zero to avoid
> +	 * issues with evsel__count_has_error.
> +	 */
> +	count->ena++;
> +	count->run++;
>   	return 0;
>   }
>   

Reviewed-by: James Clark <james.clark@linaro.org>
Re: [PATCH v1] perf tool_pmu: Fix aggregation on duration_time
Posted by Arnaldo Carvalho de Melo 9 months, 2 weeks ago
On Wed, Apr 23, 2025 at 09:58:38AM +0100, James Clark wrote:
> > +++ b/tools/perf/util/tool_pmu.c
> > @@ -486,8 +486,14 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
> >   		delta_start *= 1000000000 / ticks_per_sec;
> >   	}
> >   	count->val    = delta_start;
> > -	count->ena    = count->run = delta_start;
> >   	count->lost   = 0;
> > +	/*
> > +	 * The values of enabled and running must make a ratio of 100%. The
> > +	 * exact values don't matter as long as they are non-zero to avoid
> > +	 * issues with evsel__count_has_error.
> > +	 */
> > +	count->ena++;
> > +	count->run++;
> >   	return 0;
> >   }
 
> Reviewed-by: James Clark <james.clark@linaro.org>

Thanks, applied to perf-tools-next,

- Arnaldo