[PATCH 3/3] tools/power turbostat: allow turbostat to work when aperf is not available

David Arcari posted 3 patches 1 week, 6 days ago
[PATCH 3/3] tools/power turbostat: allow turbostat to work when aperf is not available
Posted by David Arcari 1 week, 6 days ago
Currently when aperf is not available the function has_amperf() still
returns true.  The end result is that the program gets an error in
delta_thread() which causes turbostat to restart.  We can avoid this
by not setting msr_counter_arch_infos[MSR_ARCH_INFO_APERF_INDEX].present
when aperf is not available allowing turbostat to execute normally.

Signed-off-by: David Arcari <darcari@redhat.com>
Cc: Len Brown <lenb@kernel.org>
Cc: linux-kernel@vger.kernel.org
---
 tools/power/x86/turbostat/turbostat.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 5567b9ecd516..b3f1e4ae5813 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -8592,6 +8592,10 @@ void msr_perf_init_(void)
 				continue;
 
 			if (cai->needed) {
+				/* check to see if APERF is available */
+				if (cidx == MSR_ARCH_INFO_APERF_INDEX && !has_aperf)
+					continue;
+
 				/* Use perf API for this counter */
 				if (add_msr_perf_counter(cpu, cci, cai) != -1) {
 					cci->source[cai->rci_index] = COUNTER_SOURCE_PERF;
-- 
2.51.0
Re: [PATCH 3/3] tools/power turbostat: allow turbostat to work when aperf is not available
Posted by Len Brown 6 days, 7 hours ago
It would be helpful if you could describe exactly what environment you
are running in.

are there any MSRs?
Is APERF available via perf, but not via MSR?
etc.

On Tue, Nov 18, 2025 at 10:58 AM David Arcari <darcari@redhat.com> wrote:
>
> Currently when aperf is not available the function has_amperf() still
> returns true.  The end result is that the program gets an error in
> delta_thread() which causes turbostat to restart.  We can avoid this
> by not setting msr_counter_arch_infos[MSR_ARCH_INFO_APERF_INDEX].present
> when aperf is not available allowing turbostat to execute normally.
>
> Signed-off-by: David Arcari <darcari@redhat.com>
> Cc: Len Brown <lenb@kernel.org>
> Cc: linux-kernel@vger.kernel.org
> ---
>  tools/power/x86/turbostat/turbostat.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
> index 5567b9ecd516..b3f1e4ae5813 100644
> --- a/tools/power/x86/turbostat/turbostat.c
> +++ b/tools/power/x86/turbostat/turbostat.c
> @@ -8592,6 +8592,10 @@ void msr_perf_init_(void)
>                                 continue;
>
>                         if (cai->needed) {
> +                               /* check to see if APERF is available */
> +                               if (cidx == MSR_ARCH_INFO_APERF_INDEX && !has_aperf)
> +                                       continue;
> +
>                                 /* Use perf API for this counter */
>                                 if (add_msr_perf_counter(cpu, cci, cai) != -1) {
>                                         cci->source[cai->rci_index] = COUNTER_SOURCE_PERF;
> --
> 2.51.0
>
>


-- 
Len Brown, Intel Open Source Technology Center
Re: [PATCH 3/3] tools/power turbostat: allow turbostat to work when aperf is not available
Posted by David Arcari 6 days, 4 hours ago

On 11/25/25 2:14 PM, Len Brown wrote:
> It would be helpful if you could describe exactly what environment you
> are running in.

It' a VMWARE instance.

CPUID(0): AuthenticAMD 0xd CPUID levels
CPUID(1): family:model:stepping 0x17:0:0 (23:0:0) microcode 0x0
CPUID(0x80000000): max_extended_levels: 0x8000001f
CPUID(1): SSE3 - - - - TSC MSR - - -
CPUID(6): No-APERF, No-TURBO, No-DTS, No-PTM, No-HWP, No-HWPnotify, 
No-HWPwindow, No-HWPepp, No-HWPpkg, No-EPB
CPUID(7): No-SGX No-Hybrid


> 
> are there any MSRs?

I'm not certain, is there something in particular you are looking for?

> Is APERF available via perf, but not via MSR?
> etc.

I don't believe that APERF was available via perf. I'll go back and 
verify when I have a chance.

-DA

> 
> On Tue, Nov 18, 2025 at 10:58 AM David Arcari <darcari@redhat.com> wrote:
>>
>> Currently when aperf is not available the function has_amperf() still
>> returns true.  The end result is that the program gets an error in
>> delta_thread() which causes turbostat to restart.  We can avoid this
>> by not setting msr_counter_arch_infos[MSR_ARCH_INFO_APERF_INDEX].present
>> when aperf is not available allowing turbostat to execute normally.
>>
>> Signed-off-by: David Arcari <darcari@redhat.com>
>> Cc: Len Brown <lenb@kernel.org>
>> Cc: linux-kernel@vger.kernel.org
>> ---
>>   tools/power/x86/turbostat/turbostat.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
>> index 5567b9ecd516..b3f1e4ae5813 100644
>> --- a/tools/power/x86/turbostat/turbostat.c
>> +++ b/tools/power/x86/turbostat/turbostat.c
>> @@ -8592,6 +8592,10 @@ void msr_perf_init_(void)
>>                                  continue;
>>
>>                          if (cai->needed) {
>> +                               /* check to see if APERF is available */
>> +                               if (cidx == MSR_ARCH_INFO_APERF_INDEX && !has_aperf)
>> +                                       continue;
>> +
>>                                  /* Use perf API for this counter */
>>                                  if (add_msr_perf_counter(cpu, cci, cai) != -1) {
>>                                          cci->source[cai->rci_index] = COUNTER_SOURCE_PERF;
>> --
>> 2.51.0
>>
>>
> 
> 


Re: [PATCH 3/3] tools/power turbostat: allow turbostat to work when aperf is not available
Posted by David Arcari 11 hours ago

On 11/25/25 4:55 PM, David Arcari wrote:
> 
> 
> On 11/25/25 2:14 PM, Len Brown wrote:
>> It would be helpful if you could describe exactly what environment you
>> are running in.
> 
> It' a VMWARE instance.
> 
> CPUID(0): AuthenticAMD 0xd CPUID levels
> CPUID(1): family:model:stepping 0x17:0:0 (23:0:0) microcode 0x0
> CPUID(0x80000000): max_extended_levels: 0x8000001f
> CPUID(1): SSE3 - - - - TSC MSR - - -
> CPUID(6): No-APERF, No-TURBO, No-DTS, No-PTM, No-HWP, No-HWPnotify, No- 
> HWPwindow, No-HWPepp, No-HWPpkg, No-EPB
> CPUID(7): No-SGX No-Hybrid
> 
> 
>>
>> are there any MSRs?
> 
> I'm not certain, is there something in particular you are looking for?
> 

rdmsr returns zero for MSR_IA32_APERF and MSR_IA32_MPERF.

>> Is APERF available via perf, but not via MSR?
>> etc.

No.  add_msr_perf_counter() returns -1 as the call to open_perf_counter 
returns -1.

> 
> I don't believe that APERF was available via perf. I'll go back and 
> verify when I have a chance.
> 
> -DA
> 
Is there anything else you need?

Thanks,
-DA
Re: [PATCH 3/3] tools/power turbostat: allow turbostat to work when aperf is not available
Posted by Len Brown 6 hours ago
> ...add_msr_perf_counter() returns -1

good.  and then add_msr_counter() succeeds because...

> rdmsr returns zero for MSR_IA32_APERF and MSR_IA32_MPERF.

Your patch is a good suggestion -- though it checks for APERF only and
not for MPERF.

We already ran CPUID and cleared has_aperf, so I'm thinking we should
be heading this off earlier.  Let me send you a test patch later today.

thanks,
Len Brown, Intel Open Source Technology Center
Re: [PATCH 3/3] tools/power turbostat: allow turbostat to work when aperf is not available
Posted by Len Brown 6 hours ago
Something like this?

diff --git a/tools/power/x86/turbostat/turbostat.c
b/tools/power/x86/turbostat/turbostat.c
index 5bc47ad5da09..4a847e7e9c65 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -8530,7 +8530,7 @@ void rapl_perf_init(void)
 /* Assumes msr_counter_info is populated */
 static int has_amperf_access(void)
 {
-       return msr_counter_arch_infos[MSR_ARCH_INFO_APERF_INDEX].present &&
+       return has_aperf &&
msr_counter_arch_infos[MSR_ARCH_INFO_APERF_INDEX].present &&
            msr_counter_arch_infos[MSR_ARCH_INFO_MPERF_INDEX].present;
 }


On Mon, Dec 1, 2025 at 2:53 PM Len Brown <lenb@kernel.org> wrote:
>
> > ...add_msr_perf_counter() returns -1
>
> good.  and then add_msr_counter() succeeds because...
>
> > rdmsr returns zero for MSR_IA32_APERF and MSR_IA32_MPERF.
>
> Your patch is a good suggestion -- though it checks for APERF only and
> not for MPERF.
>
> We already ran CPUID and cleared has_aperf, so I'm thinking we should
> be heading this off earlier.  Let me send you a test patch later today.
>
> thanks,
> Len Brown, Intel Open Source Technology Center



-- 
Len Brown, Intel Open Source Technology Center