From: Beata Michalska <beata.michalska@arm.com>
To: Bowen Yu <yubowen8@huawei.com>
Cc: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, catalin.marinas@arm.com,
will@kernel.org, ptsm@linux.microsoft.com, linuxarm@huawei.com,
jonathan.cameron@huawei.com, zhanjie9@hisilicon.com,
prime.zeng@hisilicon.com, wanghuiqiang@huawei.com,
xuwei5@huawei.com, zhenglifeng1@huawei.com,
zhangpengjie2@huawei.com
Subject: Re: [PATCH 1/3] arm64: topology: Improve AMU-based frequency calculation
Date: Mon, 10 Nov 2025 18:04:04 +0100 [thread overview]
Message-ID: <aRIbBBBl8mxd2rtC@arm.com> (raw)
In-Reply-To: <20251104075544.3243606-2-yubowen8@huawei.com>
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@localhost ~]# 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@localhost ~]# 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@huawei.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
>
next prev parent reply other threads:[~2025-11-10 17:04 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-04 7:55 [PATCH 0/3] arm64: topology: Improve cpuinfo_avg_freq for ARM64 Bowen Yu
2025-11-04 7:55 ` [PATCH 1/3] arm64: topology: Improve AMU-based frequency calculation Bowen Yu
2025-11-06 4:12 ` Jie Zhan
2025-11-10 17:04 ` Beata Michalska [this message]
2025-11-04 7:55 ` [PATCH 2/3] arm64: topology: Use current freq in governor for idle cpus in cpuinfo_avg_freq Bowen Yu
2025-11-10 17:11 ` Beata Michalska
2025-11-16 7:46 ` yubowen (H)
2025-11-04 7:55 ` [PATCH 3/3] arm64: topology: Remove redundant housekeeping_cpu() checks in arch_freq_get_on_cpu Bowen Yu
2025-11-10 17:15 ` Beata Michalska
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aRIbBBBl8mxd2rtC@arm.com \
--to=beata.michalska@arm.com \
--cc=catalin.marinas@arm.com \
--cc=jonathan.cameron@huawei.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxarm@huawei.com \
--cc=prime.zeng@hisilicon.com \
--cc=ptsm@linux.microsoft.com \
--cc=wanghuiqiang@huawei.com \
--cc=will@kernel.org \
--cc=xuwei5@huawei.com \
--cc=yubowen8@huawei.com \
--cc=zhangpengjie2@huawei.com \
--cc=zhanjie9@hisilicon.com \
--cc=zhenglifeng1@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).