turbostat fixes for 7.0
Len Brown
maintainercover turbostat fixes for 7.0
Please let me know if you seen any issues with these fixes.
thanks!
Len Brown, Intel Open Source Technology Center
Available here:git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
----------------------------------------------------------------
Artem Bityutskiy (4):
tools/power turbostat: Consistently use print_float_value() tools/power turbostat: Fix incorrect format variable tools/power turbostat: Fix --show/--hide for individual cpuidle counters tools/power turbostat: Fix delimiter bug in print functions
David Arcari (1):
tools/power turbostat: Allow execution to continue after perf_l2_init() failure
Len Brown (1):
tools/power turbostat: Fix swidle header vs data display
Serhii Pievniev (1):
tools/power/turbostat: Fix microcode patch level output for AMD/Hygon
Zhang Rui (2):
tools/power turbostat: Fix illegal memory access when SMT is present and disabled tools/power turbostat: Eliminate unnecessary data structure allocation
tools/power/x86/turbostat/turbostat.c | 100 ++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 46 deletions(-)
Len Brown
maintainerPATCH 1/9 tools/power turbostat: Fix illegal memory access when SMT is present and disabled
From: Zhang Rui <[email protected]>
When SMT is present and disabled, turbostat may under-size the thread_data array. This can corrupt results or cause turbostat to exit with a segmentation fault.
--- tools/power/x86/turbostat/turbostat.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
tools/power/x86/turbostat/turbostat.c +2 -3
@@ -9702,13 +9702,12 @@ void allocate_counters(struct counters *counters) {int i;int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; -int num_threads = topo.threads_per_core * num_cores; - counters->threads =calloc (num_threads,sizeof (struct thread_data )); + counters->threads =calloc (topo.max_cpu_num +1 ,sizeof (struct thread_data ));if (counters->threads ==NULL ) goto error; -for (i =0 ; i < num_threads; i++) +for (i =0 ; i < topo.max_cpu_num +1 ; i++) (counters->threads)[i].cpu_id =-1 ; counters->cores =calloc (num_cores,sizeof (struct core_data ));
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 2/9 tools/power turbostat: Fix swidle header vs data display
From: Len Brown <[email protected]>
I changed my mind about displaying swidle statistics, which are "added counters". Recently I reverted the column headers to 8-columns, but kept print_decimal_value() padding out to 16-columns for all 64-bit counters.
Simplify by keeping print_decimial_value() at %lld -- which will often fit into 8-columns, and live with the fact that it can overflow and shift the other columns, which continue to tab-delimited.
This is a better compromise than inserting a bunch of space characters that most users don't like.
--- tools/power/x86/turbostat/turbostat.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
tools/power/x86/turbostat/turbostat.c +3 -4
@@ -2852,10 +2852,9 @@ static inline int print_hex_value(int width, int *printed, char *delim, unsignedstatic inline int print_decimal_value (int width,int *printed,char *delim,unsigned long long value) { -if (width <=32 ) -return (sprintf (outp,"%s%d" , (*printed++ ? delim :"" ), (unsigned int )value)); -else -return (sprintf (outp,"%s%-8lld" , (*printed++ ? delim :"" ), value)); +UNUSED (width); + +return (sprintf (outp,"%s%lld" , (*printed++ ? delim :"" ), value)); }static inline int print_float_value (int *printed,char *delim,double value)
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 3/9 tools/power turbostat: Eliminate unnecessary data structure allocation
In reply to Len BrownFrom: Zhang Rui <[email protected]>
Linux core_id's are a per-package namespace, not a per-node namespace.
Rename topo.cores_per_node to topo.cores_per_pkg to reflect this.
Eliminate topo.nodes_per_pkg from the sizing for core data structures, since it has no role except to unnecessarily bloat the allocation.
Validated on multiple Intel platforms (ICX/SPR/SRF/EMR/GNR/CWF) with various CPU online/offline configurations and SMT enabled/disabled scenarios.
No functional changes.
--- tools/power/x86/turbostat/turbostat.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/power/x86/turbostat/turbostat.c +4 -4
@@ -2409,7 +2409,7 @@ struct topo_params {int max_l3_id;int max_node_num;int nodes_per_pkg; -int cores_per_node; +int cores_per_pkg;int threads_per_core; } topo;
@@ -9633,9 +9633,9 @@ void topology_probe(bool startup) topo.max_core_id = max_core_id;/* within a package */ topo.max_package_id = max_package_id; - topo.cores_per_node = max_core_id +1 ; + topo.cores_per_pkg = max_core_id +1 ;if (debug >1 ) -fprintf (outf,"max_core_id %d, sizing for %d cores per package\n" , max_core_id, topo.cores_per_node); +fprintf (outf,"max_core_id %d, sizing for %d cores per package\n" , max_core_id, topo.cores_per_pkg);if (!summary_only)BIC_PRESENT (BIC_Core);
@@ -9700,7 +9700,7 @@ void allocate_counters_1(struct counters *counters)void allocate_counters (struct counters *counters) {int i; -int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; +int num_cores = topo.cores_per_pkg * topo.num_packages; counters->threads =calloc (topo.max_cpu_num +1 ,sizeof (struct thread_data ));if (counters->threads ==NULL )
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 4/9 tools/power/turbostat: Fix microcode patch level output for AMD/Hygon
In reply to Len BrownFrom: Serhii Pievniev <[email protected]>
turbostat always used the same logic to read the microcode patch level, which is correct for Intel but not for AMD/Hygon.
While Intel stores the patch level in the upper 32 bits of MSR, AMD stores it in the lower 32 bits, which causes turbostat to report the microcode version as 0x0 on AMD/Hygon.
Fix by shifting right by 32 for non-AMD/Hygon, preserving the existing behavior for Intel and unknown vendors.
--- tools/power/x86/turbostat/turbostat.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
tools/power/x86/turbostat/turbostat.c +6 -3
@@ -9121,10 +9121,13 @@ void process_cpuid() cpuid_has_hv = ecx_flags & (1 <<31 );if (!no_msr) { -if (get_msr (sched_getcpu (),MSR_IA32_UCODE_REV , &ucode_patch)) +if (get_msr (sched_getcpu (),MSR_IA32_UCODE_REV , &ucode_patch)) {warnx ("get_msr(UCODE)" ); -else + }else { ucode_patch_valid = true; +if (!authentic_amd && !hygon_genuine) + ucode_patch >>=32 ; + } }/*
@@ -9138,7 +9141,7 @@ void process_cpuid()if (!quiet) {fprintf (outf,"CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d)" , family, model, stepping, family, model, stepping);if (ucode_patch_valid) -fprintf (outf," microcode 0x%x" , (unsigned int )((ucode_patch >>32 ) &0xFFFFFFFF )); +fprintf (outf," microcode 0x%x" , (unsigned int )ucode_patch);fputc ('\n' , outf);fprintf (outf,"CPUID(0x80000000): max_extended_levels: 0x%x\n" , max_extended_level);
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 5/9 tools/power turbostat: Consistently use print_float_value()
In reply to Len BrownFrom: Artem Bityutskiy <[email protected]>
Fix the PMT thread code to use print_float_value(), to be consistent with the PMT core and package code.
--- tools/power/x86/turbostat/turbostat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/power/x86/turbostat/turbostat.c +2 -2
@@ -3489,12 +3489,12 @@ int format_counters(PER_THREAD_PARAMS)case PMT_TYPE_XTAL_TIME : value_converted =pct (value_raw / crystal_hz, interval_float); - outp +=sprintf (outp,"%s%.2f" , (printed++ ? delim :"" ), value_converted); + outp +=print_float_value (&printed, delim, value_converted);break ;case PMT_TYPE_TCORE_CLOCK : value_converted =pct (value_raw / tcore_clock_freq_hz, interval_float); - outp +=sprintf (outp,"%s%.2f" , (printed++ ? delim :"" ), value_converted); + outp +=print_float_value (&printed, delim, value_converted); } }
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 6/9 tools/power turbostat: Fix incorrect format variable
In reply to Len BrownFrom: Artem Bityutskiy <[email protected]>
In the perf thread, core, and package counter loops, an incorrect 'mp->format' variable is used instead of 'pp->format'.
--- tools/power/x86/turbostat/turbostat.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/power/x86/turbostat/turbostat.c +3 -3
@@ -3468,7 +3468,7 @@ int format_counters(PER_THREAD_PARAMS)for (i =0 , pp = sys.perf_tp; pp; ++i, pp = pp->next) {if (pp->format ==FORMAT_RAW ) outp +=print_hex_value (pp->width, &printed, delim, t->perf_counter[i]); -else if (pp->format ==FORMAT_DELTA || mp->format ==FORMAT_AVERAGE ) +else if (pp->format ==FORMAT_DELTA || pp->format ==FORMAT_AVERAGE ) outp +=print_decimal_value (pp->width, &printed, delim, t->perf_counter[i]);else if (pp->format ==FORMAT_PERCENT ) {if (pp->type ==COUNTER_USEC )
@@ -3538,7 +3538,7 @@ int format_counters(PER_THREAD_PARAMS)for (i =0 , pp = sys.perf_cp; pp; i++, pp = pp->next) {if (pp->format ==FORMAT_RAW ) outp +=print_hex_value (pp->width, &printed, delim, c->perf_counter[i]); -else if (pp->format ==FORMAT_DELTA || mp->format ==FORMAT_AVERAGE ) +else if (pp->format ==FORMAT_DELTA || pp->format ==FORMAT_AVERAGE ) outp +=print_decimal_value (pp->width, &printed, delim, c->perf_counter[i]);else if (pp->format ==FORMAT_PERCENT ) outp +=print_float_value (&printed, delim,pct (c->perf_counter[i], tsc));
@@ -3694,7 +3694,7 @@ int format_counters(PER_THREAD_PARAMS) outp +=print_hex_value (pp->width, &printed, delim, p->perf_counter[i]); else if (pp->type == COUNTER_K2M) outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), (unsigned int)p->perf_counter[i] / 1000); - else if (pp->format == FORMAT_DELTA || mp->format == FORMAT_AVERAGE) + else if (pp->format == FORMAT_DELTA || pp->format == FORMAT_AVERAGE) outp +=print_decimal_value (pp->width, &printed, delim, p->perf_counter[i]);else if (pp->format ==FORMAT_PERCENT ) outp +=print_float_value (&printed, delim,pct (p->perf_counter[i], tsc));
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 7/9 tools/power turbostat: Fix --show/--hide for individual cpuidle counters
In reply to Len BrownFrom: Artem Bityutskiy <[email protected]>
Problem: individual swidle counter names (C1, C1+, C1-, etc.) cannot be selected via --show/--hide due to two bugs in probe_cpuidle_counts():
1. The function returns immediately when BIC_cpuidle is not enabled, without checking deferred_add_index.
2. The deferred name check runs against name_buf before the trailing newline is stripped, so is_deferred_add("C1\n") never matches "C1".
Fix:
1. Relax the early return to pass through when deferred names are queued.
2. Strip the trailing newline from name_buf before performing deferred name checks.
3. Check each suffixed variant (C1+, C1, C1-) individually so that e.g. "--show C1+" enables only the requested metric.
In addition, introduce a helper function to avoid repeating the condition (readability cleanup).
--- tools/power/x86/turbostat/turbostat.c | 35 ++++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-)
tools/power/x86/turbostat/turbostat.c +21 -14
@@ -11285,6 +11285,14 @@ void probe_cpuidle_residency(void) } } +static bool cpuidle_counter_wanted (char *name) +{ +if (is_deferred_skip (name)) +return false; + +return DO_BIC (BIC_cpuidle) ||is_deferred_add (name); +} +void probe_cpuidle_counts (void ) {char path[64 ];
@@ -11294,7 +11302,7 @@ void probe_cpuidle_counts(void)int min_state =1024 , max_state =0 ;char *sp; -if (!DO_BIC (BIC_cpuidle)) +if (!DO_BIC (BIC_cpuidle) && !deferred_add_index)return ;for (state =10 ; state >=0 ; --state) {
@@ -11309,12 +11317,6 @@ void probe_cpuidle_counts(void)remove_underbar (name_buf); -if (!DO_BIC (BIC_cpuidle) && !is_deferred_add (name_buf)) -continue ; - -if (is_deferred_skip (name_buf)) -continue ; -/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ sp =strchr (name_buf,'-' );if (!sp)
@@ -11329,16 +11331,19 @@ void probe_cpuidle_counts(void)* Add 'C1+' for C1, and so on. The 'below' sysfs file always contains 0 for * the last state, so do not add it. */ - *sp ='+' ; *(sp +1 ) ='\0' ; -sprintf (path,"cpuidle/state%d/below" , state); -add_counter (0 , path, name_buf,64 ,SCOPE_CPU ,COUNTER_ITEMS ,FORMAT_DELTA ,SYSFS_PERCPU ,0 ); +if (cpuidle_counter_wanted (name_buf)) { +sprintf (path,"cpuidle/state%d/below" , state); +add_counter (0 , path, name_buf,64 ,SCOPE_CPU ,COUNTER_ITEMS ,FORMAT_DELTA ,SYSFS_PERCPU ,0 ); + } } *sp ='\0' ; -sprintf (path,"cpuidle/state%d/usage" , state); -add_counter (0 , path, name_buf,64 ,SCOPE_CPU ,COUNTER_ITEMS ,FORMAT_DELTA ,SYSFS_PERCPU ,0 ); +if (cpuidle_counter_wanted (name_buf)) { +sprintf (path,"cpuidle/state%d/usage" , state); +add_counter (0 , path, name_buf,64 ,SCOPE_CPU ,COUNTER_ITEMS ,FORMAT_DELTA ,SYSFS_PERCPU ,0 ); + }/* * The 'above' sysfs file always contains 0 for the shallowest state (smallest
@@ -11347,8 +11352,10 @@ void probe_cpuidle_counts(void)if (state != min_state) { *sp ='-' ; *(sp +1 ) ='\0' ; -sprintf (path,"cpuidle/state%d/above" , state); -add_counter (0 , path, name_buf,64 ,SCOPE_CPU ,COUNTER_ITEMS ,FORMAT_DELTA ,SYSFS_PERCPU ,0 ); +if (cpuidle_counter_wanted (name_buf)) { +sprintf (path,"cpuidle/state%d/above" , state); +add_counter (0 , path, name_buf,64 ,SCOPE_CPU ,COUNTER_ITEMS ,FORMAT_DELTA ,SYSFS_PERCPU ,0 ); + } } } }
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 8/9 tools/power turbostat: Fix delimiter bug in print functions
In reply to Len BrownFrom: Artem Bityutskiy <[email protected]>
Commands that add counters, such as 'turbostat --show C1,C1+' display merged columns without a delimiter.
This is caused by the bad syntax: '(*printed++ ? delim : "")', shared by print_name()/print_hex_value()/print_decimal_value()/print_float_value()
Use '((*printed)++ ? delim : "")' to correctly increment the value at *printed.
--- tools/power/x86/turbostat/turbostat.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
tools/power/x86/turbostat/turbostat.c +6 -6
@@ -2837,29 +2837,29 @@ static inline int print_name(int width, int *printed, char *delim, char *name, eUNUSED (type);if (format ==FORMAT_RAW && width >=64 ) -return (sprintf (outp,"%s%-8s" , (*printed++ ? delim :"" ), name)); +return (sprintf (outp,"%s%-8s" , ((*printed)++ ? delim :"" ), name));else -return (sprintf (outp,"%s%s" , (*printed++ ? delim :"" ), name)); +return (sprintf (outp,"%s%s" , ((*printed)++ ? delim :"" ), name)); }static inline int print_hex_value (int width,int *printed,char *delim,unsigned long long value) {if (width <=32 ) -return (sprintf (outp,"%s%08x" , (*printed++ ? delim :"" ), (unsigned int )value)); +return (sprintf (outp,"%s%08x" , ((*printed)++ ? delim :"" ), (unsigned int )value));else -return (sprintf (outp,"%s%016llx" , (*printed++ ? delim :"" ), value)); +return (sprintf (outp,"%s%016llx" , ((*printed)++ ? delim :"" ), value)); }static inline int print_decimal_value (int width,int *printed,char *delim,unsigned long long value) {UNUSED (width); -return (sprintf (outp,"%s%lld" , (*printed++ ? delim :"" ), value)); +return (sprintf (outp,"%s%lld" , ((*printed)++ ? delim :"" ), value)); }static inline int print_float_value (int *printed,char *delim,double value) { -return (sprintf (outp,"%s%0.2f" , (*printed++ ? delim :"" ), value)); +return (sprintf (outp,"%s%0.2f" , ((*printed)++ ? delim :"" ), value)); }void print_header (char *delim)
Signature · 1 line
2.45.2
Len Brown
maintainerPATCH 9/9 tools/power turbostat: Allow execution to continue after perf_l2_init() failure
In reply to Len BrownFrom: David Arcari <[email protected]>
Currently, if perf_l2_init() fails turbostat exits after issuing the following error (which was encountered on AlderLake):
turbostat: perf_l2_init(cpu0, 0x0, 0xff24) REFS: Invalid argument
This occurs because perf_l2_init() calls err(). However, the code has been written in such a manner that it is able to perform cleanup and continue. Therefore, this issue can be addressed by changing the appropriate calls to err() to warnx().
Additionally, correct the PMU type arguments passed to the warning strings in the ecore and lcore blocks so the logs accurately reflect the failing counter type.
--- tools/power/x86/turbostat/turbostat.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
tools/power/x86/turbostat/turbostat.c +8 -8
@@ -9405,13 +9405,13 @@ void perf_l2_init(void)if (!is_hybrid) { fd_l2_percpu[cpu] =open_perf_counter (cpu, perf_pmu_types.uniform, perf_model_support->first.refs,-1 ,PERF_FORMAT_GROUP );if (fd_l2_percpu[cpu] ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.refs); +warnx ("%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.refs);free_fd_l2_percpu ();return ; } retval =open_perf_counter (cpu, perf_pmu_types.uniform, perf_model_support->first.hits, fd_l2_percpu[cpu],PERF_FORMAT_GROUP );if (retval ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.hits); +warnx ("%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.uniform, perf_model_support->first.hits);free_fd_l2_percpu ();return ; }
@@ -9420,39 +9420,39 @@ void perf_l2_init(void)if (perf_pcore_set &&CPU_ISSET_S (cpu, cpu_possible_setsize, perf_pcore_set)) { fd_l2_percpu[cpu] =open_perf_counter (cpu, perf_pmu_types.pcore, perf_model_support->first.refs,-1 ,PERF_FORMAT_GROUP );if (fd_l2_percpu[cpu] ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.refs); +warnx ("%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.refs);free_fd_l2_percpu ();return ; } retval =open_perf_counter (cpu, perf_pmu_types.pcore, perf_model_support->first.hits, fd_l2_percpu[cpu],PERF_FORMAT_GROUP );if (retval ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.hits); +warnx ("%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->first.hits);free_fd_l2_percpu ();return ; } }else if (perf_ecore_set &&CPU_ISSET_S (cpu, cpu_possible_setsize, perf_ecore_set)) { fd_l2_percpu[cpu] =open_perf_counter (cpu, perf_pmu_types.ecore, perf_model_support->second.refs,-1 ,PERF_FORMAT_GROUP );if (fd_l2_percpu[cpu] ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->second.refs); +warnx ("%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.ecore, perf_model_support->second.refs);free_fd_l2_percpu ();return ; } retval =open_perf_counter (cpu, perf_pmu_types.ecore, perf_model_support->second.hits, fd_l2_percpu[cpu],PERF_FORMAT_GROUP );if (retval ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->second.hits); +warnx ("%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.ecore, perf_model_support->second.hits);free_fd_l2_percpu ();return ; } }else if (perf_lcore_set &&CPU_ISSET_S (cpu, cpu_possible_setsize, perf_lcore_set)) { fd_l2_percpu[cpu] =open_perf_counter (cpu, perf_pmu_types.lcore, perf_model_support->third.refs,-1 ,PERF_FORMAT_GROUP );if (fd_l2_percpu[cpu] ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->third.refs); +warnx ("%s(cpu%d, 0x%x, 0x%llx) REFS" , __func__, cpu, perf_pmu_types.lcore, perf_model_support->third.refs);free_fd_l2_percpu ();return ; } retval =open_perf_counter (cpu, perf_pmu_types.lcore, perf_model_support->third.hits, fd_l2_percpu[cpu],PERF_FORMAT_GROUP );if (retval ==-1 ) { -err (-1 ,"%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.pcore, perf_model_support->third.hits); +warnx ("%s(cpu%d, 0x%x, 0x%llx) HITS" , __func__, cpu, perf_pmu_types.lcore, perf_model_support->third.hits);free_fd_l2_percpu ();return ; }
Signature · 1 line
2.45.2