[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aRIbBBBl8mxd2rtC@arm.com>
Date: Mon, 10 Nov 2025 18:04:04 +0100
From: Beata Michalska <beata.michalska@....com>
To: Bowen Yu <yubowen8@...wei.com>
Cc: linux-arm-kernel@...ts.infradead.org, linux-kernel@...r.kernel.org,
catalin.marinas@....com, will@...nel.org, ptsm@...ux.microsoft.com,
linuxarm@...wei.com, jonathan.cameron@...wei.com,
zhanjie9@...ilicon.com, prime.zeng@...ilicon.com,
wanghuiqiang@...wei.com, xuwei5@...wei.com, zhenglifeng1@...wei.com,
zhangpengjie2@...wei.com
Subject: Re: [PATCH 1/3] arm64: topology: Improve AMU-based frequency
calculation
On Tue, Nov 04, 2025 at 03:55:42PM +0800, Bowen Yu wrote:
> The current approach of reverse-calculating CPU frequency from capacity
> values introduces quantization errors due to intermediate scaling of
> arch_scale_freq_capacity, which results in the calculated frequency having
> only 1/1024 resolution.
>
> This patch:
> 1. Directly computes frequency using AMU counters in amu_scale_freq_tick():
> freq = (core_cycles_delta * timer_freq) / (const_cycles_delta * 1000)
> - core_cycles_delta: Measured CPU cycles
> - timer_freq: Architectural timer frequency
> - const_cycles_delta: Reference cycles from fixed-frequency timer
> 2. Returns pre-computed avgfreq in arch_freq_get_on_cpu()
>
> examples:
> Before change
> [root@...alhost ~]# cat /sys/devices/system/cpu/cpufreq/policy*/cpuinfo_avg_freq
> 2297851
> 2297851
> 2295312
> 2297851
> 2297851
> 2295312
> 2297851
> 2295312
> 2297851
> 2297851
> 2297851
> 2295312
> 2295312
> 2297851
> 2297851
> 2297851
> 2297851
> 2300390
> 2297851
> 2297851
> 2297851
>
> After change
> [root@...alhost ~]# cat /sys/devices/system/cpu/cpufreq/policy*/cpuinfo_avg_freq
> 2299177
> 2298117
> 2299188
> 2297330
> 2296530
> 2298817
> 2298434
> 2298986
> 2298596
> 2299395
> 2299560
> 2298446
> 2299108
> 2299294
> 2298707
> 2298453
> 2298632
> 2299218
> 2297962
Based on your numbers the shift is on average ~0.055–0.057%.
I'm not entirely convinced it is worth it, especially that this is an average
frequency. What is the use case here if < 0,2% makes a difference ?
---
BR
Beata
>
> Signed-off-by: Bowen Yu <yubowen8@...wei.com>
> ---
> arch/arm64/kernel/topology.c | 20 +++++++++++---------
> 1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c
> index 5d07ee85bdae..c0dbc27289ea 100644
> --- a/arch/arm64/kernel/topology.c
> +++ b/arch/arm64/kernel/topology.c
> @@ -20,6 +20,7 @@
> #include <linux/percpu.h>
> #include <linux/sched/isolation.h>
> #include <linux/xarray.h>
> +#include <linux/units.h>
>
> #include <asm/cpu.h>
> #include <asm/cputype.h>
> @@ -144,6 +145,8 @@ int __init parse_acpi_topology(void)
> */
> static DEFINE_PER_CPU_READ_MOSTLY(unsigned long, arch_max_freq_scale) = 1UL << (2 * SCHED_CAPACITY_SHIFT);
> static cpumask_var_t amu_fie_cpus;
> +static DEFINE_PER_CPU(unsigned long, core_delta);
> +static DEFINE_PER_CPU(unsigned long, const_delta);
>
> struct amu_cntr_sample {
> u64 arch_const_cycles_prev;
> @@ -246,6 +249,7 @@ static void amu_scale_freq_tick(void)
> * arch_max_freq_scale and the use of SCHED_CAPACITY_SHIFT.
> */
> scale = core_cnt - prev_core_cnt;
> + this_cpu_write(core_delta, scale);
> scale *= this_cpu_read(arch_max_freq_scale);
> scale = div64_u64(scale >> SCHED_CAPACITY_SHIFT,
> const_cnt - prev_const_cnt);
> @@ -253,6 +257,7 @@ static void amu_scale_freq_tick(void)
> scale = min_t(unsigned long, scale, SCHED_CAPACITY_SCALE);
> this_cpu_write(arch_freq_scale, (unsigned long)scale);
>
> + this_cpu_write(const_delta, const_cnt - prev_const_cnt);
> amu_sample->last_scale_update = jiffies;
> }
>
> @@ -288,7 +293,7 @@ int arch_freq_get_on_cpu(int cpu)
> unsigned int start_cpu = cpu;
> unsigned long last_update;
> unsigned int freq = 0;
> - u64 scale;
> + u64 delta_core_kHz;
>
> if (!amu_fie_cpu_supported(cpu) || !arch_scale_freq_ref(cpu))
> return -EOPNOTSUPP;
> @@ -340,14 +345,11 @@ int arch_freq_get_on_cpu(int cpu)
> break;
> }
> }
> - /*
> - * Reversed computation to the one used to determine
> - * the arch_freq_scale value
> - * (see amu_scale_freq_tick for details)
> - */
> - scale = arch_scale_freq_capacity(cpu);
> - freq = scale * arch_scale_freq_ref(cpu);
> - freq >>= SCHED_CAPACITY_SHIFT;
> +
> + if (check_mul_overflow(per_cpu(core_delta, cpu), arch_timer_get_cntfrq(), &delta_core_kHz))
> + return -EINVAL;
> +
> + freq = div_u64(delta_core_kHz, per_cpu(const_delta, cpu) * HZ_PER_KHZ);
> return freq;
> }
>
> --
> 2.33.0
>
Powered by blists - more mailing lists