perf/x86/power: Introduce AMD accumlated power reporting mechanism
Subsystems (2)
cover perf/x86/power: Introduce AMD accumlated power reporting mechanism
Hi,
This series of patches introduces the perf implementation of accumulated power reporting algorithm. It will calculate the average power consumption for the processor. The CPU feature flag is CPUID.8000_0007H:EDX[12].
Simple example:
root@hr-zp:/home/ray/tip# ./tools/perf/perf stat -a -e 'power/power-pkg/' make -j4 CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CHK include/generated/timeconst.h CHK include/generated/bounds.h CHK include/generated/asm-offsets.h CALL scripts/checksyscalls.sh CHK include/generated/compile.h SKIPPED include/generated/compile.h Building modules, stage 2. Kernel: arch/x86/boot/bzImage is ready (#40) MODPOST 4225 modules
Performance counter stats for 'system wide':
183.44 mWatts power/power-pkg/
341.837270111 seconds time elapsedroot@hr-zp:/home/ray/tip# ./tools/perf/perf stat -a -e 'power/power-pkg/' sleep 10
Performance counter stats for 'system wide':
0.18 mWatts power/power-pkg/
10.012551815 seconds time elapsed
Reference:
http://lkml.kernel.org/r/[email protected]
Thanks,
Rui
Huang Rui (4):
x86/amd: move nodes_per_socket into bsp_init_amd x86/amd: add accessor for number of cores per compute unit x86/cpufeature: add AMD Accumulated Power Mechanism feature flag perf/x86/amd/power: Add AMD accumulated power reporting mechanism
arch/x86/include/asm/cpufeature.h | 2 +- arch/x86/include/asm/processor.h | 1 + arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/cpu/amd.c | 31 +- arch/x86/kernel/cpu/perf_event_amd_power.c | 531 +++++++++++++++++++++++++++++ 5 files changed, 561 insertions(+), 5 deletions(-) create mode 100644 arch/x86/kernel/cpu/perf_event_amd_power.c
Signature · 1 line
1.9.1
PATCH 1/4 x86/amd: move nodes_per_socket into bsp_init_amd
nodes_per_socket is static and it needn't be initialized many times during every cpu core init. So move nodes_per_socket initialization into bsp_init_amd.
--- arch/x86/kernel/cpu/amd.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/amd.c +12 -2
@@ -308,7 +308,6 @@ static void amd_get_topology(struct cpuinfo_x86 *c)u32 eax, ebx, ecx, edx;cpuid (0x8000001e , &eax, &ebx, &ecx, &edx); - nodes_per_socket = ((ecx >>8 ) &7 ) +1 ; node_id = ecx &7 ;/* get compute unit information */
@@ -319,7 +318,6 @@ static void amd_get_topology(struct cpuinfo_x86 *c)u64 value;rdmsrl (MSR_FAM10H_NODE_ID , value); - nodes_per_socket = ((value >>3 ) &7 ) +1 ; node_id = value &7 ; }else return ;
@@ -523,6 +521,18 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)if (cpu_has (c,X86_FEATURE_MWAITX ))use_mwaitx_delay (); + +if (boot_cpu_has (X86_FEATURE_TOPOEXT )) { +u32 ecx; + + ecx =cpuid_ecx (0x8000001e ); + nodes_per_socket = ((ecx >>8 ) &7 ) +1 ; + }else if (boot_cpu_has (X86_FEATURE_NODEID_MSR )) { +u64 value; + +rdmsrl (MSR_FAM10H_NODE_ID , value); + nodes_per_socket = ((value >>3 ) &7 ) +1 ; + } }static void early_init_amd (struct cpuinfo_x86 *c)
Signature · 1 line
1.9.1
PATCH 2/4 x86/amd: add accessor for number of cores per compute unit
In reply to Huang RuiAdd an accessor function amd_get_cores_per_cu() which returns the number of cores per compute unit.
In a subsequent patch, we will use this function in both x86 perf and fam15h driver.
--- arch/x86/include/asm/processor.h | 1 + arch/x86/kernel/cpu/amd.c | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +1 -0
@@ -810,6 +810,7 @@ static inline int mpx_disable_management(void)extern u16 amd_get_nb_id (int cpu);extern u32 amd_get_nodes_per_socket (void ); +extern u32 amd_get_cores_per_cu (void );static inline uint32_t hypervisor_cpuid_base (const char *sig,uint32_t leaves) {
arch/x86/kernel/cpu/amd.c +13 -4
@@ -27,6 +27,9 @@*/ static u32 nodes_per_socket =1 ; +/* cores_per_cu: stores the number of cores per compute unit */ +static u32 cores_per_cu =1 ; +static inline int rdmsrl_amd_safe (unsigned msr,unsigned long long *p) {u32 gprs[8 ] = {0 };
@@ -299,7 +302,6 @@ static int nearby_node(int apicid)#ifdef CONFIG_SMP static void amd_get_topology (struct cpuinfo_x86 *c) { -u32 cores_per_cu =1 ;u8 node_id;int cpu =smp_processor_id ();
@@ -313,7 +315,6 @@ static void amd_get_topology(struct cpuinfo_x86 *c)/* get compute unit information */ smp_num_siblings = ((ebx >>8 ) &3 ) +1 ; c->compute_unit_id = ebx &0xff ; - cores_per_cu += ((ebx >>8 ) &3 ); }else if (cpu_has (c,X86_FEATURE_NODEID_MSR )) {u64 value;
@@ -391,6 +392,13 @@ u32 amd_get_nodes_per_socket(void) }EXPORT_SYMBOL_GPL (amd_get_nodes_per_socket); +/* this function returns the number of cores per compute unit */ +u32 amd_get_cores_per_cu (void ) +{ +return cores_per_cu; +} +EXPORT_SYMBOL_GPL (amd_get_cores_per_cu); +static void srat_detect_node (struct cpuinfo_x86 *c) {#ifdef CONFIG_NUMA
@@ -523,10 +531,11 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)use_mwaitx_delay ();if (boot_cpu_has (X86_FEATURE_TOPOEXT )) { -u32 ecx; +u32 eax, ebx, ecx, edx; - ecx =cpuid_ecx (0x8000001e ); +cpuid (0x8000001e , &eax, &ebx, &ecx, &edx); nodes_per_socket = ((ecx >>8 ) &7 ) +1 ; + cores_per_cu = ((ebx >>8 ) &3 ) +1 ; }else if (boot_cpu_has (X86_FEATURE_NODEID_MSR )) {u64 value;
Signature · 1 line
1.9.1
PATCH 3/4 x86/cpufeature: add AMD Accumulated Power Mechanism feature flag
In reply to Huang RuiAMD CPU family 15h model 0x60 introduces accumulated power mechanism. It is used to report the processor power consumption and indicated by CPUID Fn8000_0007_EDX[12].
--- arch/x86/include/asm/cpufeature.h | 2 +- arch/x86/kernel/cpu/amd.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeature.h +1 -1
@@ -85,7 +85,7 @@#define X86_FEATURE_P4 ( 3*32+ 7)/* "" P4 */ #define X86_FEATURE_CONSTANT_TSC ( 3*32+ 8)/* TSC ticks at a constant rate */ #define X86_FEATURE_UP ( 3*32+ 9)/* smp kernel running on up */ -/* free, was #define X86_FEATURE_FXSAVE_LEAK ( 3*32+10) * "" FXSAVE leaks FOP/FIP/FOP */ +#define X86_FEATURE_ACC_POWER ( 3*32+10)/* AMD Accumulated Power Mechanism */ #define X86_FEATURE_ARCH_PERFMON ( 3*32+11)/* Intel Architectural PerfMon */ #define X86_FEATURE_PEBS ( 3*32+12)/* Precise-Event Based Sampling */ #define X86_FEATURE_BTS ( 3*32+13)/* Branch Trace Store */
arch/x86/kernel/cpu/amd.c +4 -0
@@ -559,6 +559,10 @@ static void early_init_amd(struct cpuinfo_x86 *c)set_sched_clock_stable (); } +/* Bit 12 of 8000_0007 edx is accumulated power mechanism. */ +if (c->x86_power &BIT (12 )) +set_cpu_cap (c,X86_FEATURE_ACC_POWER ); +#ifdef CONFIG_X86_64 set_cpu_cap (c,X86_FEATURE_SYSCALL32 );#else
Signature · 1 line
1.9.1
PATCH 4/4 perf/x86/amd/power: Add AMD accumulated power reporting mechanism
In reply to Huang RuiIntroduce an AMD accumlated power reporting mechanism for Carrizo (Family 15h, Model 60h) processor that should be used to calculate the average power consumed by a processor during a measurement interval. The feature of accumulated power mechanism is indicated by CPUID Fn8000_0007_EDX[12].
---------------------------------------------------------------------
* Tsample: compute unit power accumulator sample period
* Tref: the PTSC counter period
* PTSC: performance timestamp counter
* N: the ratio of compute unit power accumulator sample period to the PTSC period
* Jmax: max compute unit accumulated power which is indicated by MaxCpuSwPwrAcc MSR C001007b
* Jx/Jy: compute unit accumulated power which is indicated by CpuSwPwrAcc MSR C001007a
* Tx/Ty: the value of performance timestamp counter which is indicated by CU_PTSC MSR C0010280
* PwrCPUave: CPU average power
i. Determine the ratio of Tsample to Tref by executing CPUID Fn8000_0007.
N = value of CPUID Fn8000_0007_ECX[CpuPwrSampleTimeRatio[15:0]].
ii. Read the full range of the cumulative energy value from the new MSR MaxCpuSwPwrAcc.
Jmax = value returned.
iii. At time x, SW reads CpuSwPwrAcc MSR and samples the PTSC.
Jx = value read from CpuSwPwrAcc and Tx = value read from
PTSC.
iv. At time y, SW reads CpuSwPwrAcc MSR and samples the PTSC.
Jy = value read from CpuSwPwrAcc and Ty = value read from
PTSC.
v. Calculate the average power consumption for a compute unit over time period (y-x). Unit of result is uWatt.
if (Jy < Jx) // Rollover has occurred
Jdelta = (Jy + Jmax) - Jx
else
Jdelta = Jy - Jx
PwrCPUave = N * Jdelta * 1000 / (Ty - Tx)
----------------------------------------------------------------------
This feature will be implemented both on hwmon and perf that discussed in mail list before. At current design, it provides one event to report per package/processor power consumption by counting each compute unit power value.
Simple example:
root@hr-zp:/home/ray/tip# ./tools/perf/perf stat -a -e 'power/power-pkg/' make -j4 CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CHK include/generated/timeconst.h CHK include/generated/bounds.h CHK include/generated/asm-offsets.h CALL scripts/checksyscalls.sh CHK include/generated/compile.h SKIPPED include/generated/compile.h Building modules, stage 2. Kernel: arch/x86/boot/bzImage is ready (#40) MODPOST 4225 modules
Performance counter stats for 'system wide':
183.44 mWatts power/power-pkg/
341.837270111 seconds time elapsedroot@hr-zp:/home/ray/tip# ./tools/perf/perf stat -a -e 'power/power-pkg/' sleep 10
Performance counter stats for 'system wide':
0.18 mWatts power/power-pkg/
10.012551815 seconds time elapsed
Reference:
http://lkml.kernel.org/r/[email protected]
--- arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/cpu/perf_event_amd_power.c | 531 +++++++++++++++++++++++++++++ 2 files changed, 532 insertions(+) create mode 100644 arch/x86/kernel/cpu/perf_event_amd_power.c
arch/x86/kernel/cpu/Makefile +1 -0
@@ -34,6 +34,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_event.o ifdef CONFIG_PERF_EVENTS obj-$(CONFIG_CPU_SUP_AMD) += perf_event_amd.o perf_event_amd_uncore.o +obj-$(CONFIG_CPU_SUP_AMD) += perf_event_amd_power.o ifdef CONFIG_AMD_IOMMU obj-$(CONFIG_CPU_SUP_AMD) += perf_event_amd_iommu.o endif
arch/x86/kernel/cpu/perf_event_amd_power.c new file +531 -0
531 lines not on this page · raw message
Signature · 1 line
1.9.1
kbuild test robot
build botRe: [PATCH 4/4] perf/x86/amd/power: Add AMD accumulated power reporting mechanism
reproduce:
Hi Huang,
[auto build test ERROR on bp/for-next]
[also build test ERROR on v4.4-rc8 next-20160106]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]
url: https://github.com/0day-ci/linux/commits/Huang-Rui/perf-x86-power-Introduce-AMD-accumlated-power-reporting-mechanism/20160107-103309 base: https://git.kernel.org/pub/scm/linux/kernel/git/bp/bp for-next config: i386-randconfig-s1-201601 (attached as .config) reproduce: # save the attached .config to linux build tree make ARCH=i386
All error/warnings (new ones prefixed by >>):
In file included from include/linux/kobject.h:21:0,
from include/linux/module.h:17,
from arch/x86/kernel/cpu/perf_event_amd_power.c:13:
>> arch/x86/kernel/cpu/perf_event.h:663:31: error: 'events_sysfs_show' undeclared here (not in a function)
.attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
^
include/linux/sysfs.h:103:10: note: in definition of macro '__ATTR'
.show = _show, \
^
>> arch/x86/kernel/cpu/perf_event_amd_power.c:244:1: note: in expansion of macro 'EVENT_ATTR_STR'
EVENT_ATTR_STR(power-pkg, power_pkg, "event=0x01");
^vim +/events_sysfs_show +663 arch/x86/kernel/cpu/perf_event.h
3a54aaa0 Stephane Eranian 2013-01-24 657 .id = PERF_COUNT_HW_##_id, \
3a54aaa0 Stephane Eranian 2013-01-24 658 .event_str = NULL, \
3a54aaa0 Stephane Eranian 2013-01-24 659 };
3a54aaa0 Stephane Eranian 2013-01-24 660
3a54aaa0 Stephane Eranian 2013-01-24 661 #define EVENT_ATTR_STR(_name, v, str) \
3a54aaa0 Stephane Eranian 2013-01-24 662 static struct perf_pmu_events_attr event_attr_##v = { \
3a54aaa0 Stephane Eranian 2013-01-24 @663 .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \
3a54aaa0 Stephane Eranian 2013-01-24 664 .id = 0, \
3a54aaa0 Stephane Eranian 2013-01-24 665 .event_str = str, \
3a54aaa0 Stephane Eranian 2013-01-24 666 };:::::: The code at line 663 was first introduced by commit
:::::: 3a54aaa0a3ddb2cf2ec1b94a94024e9a8a8af962 perf/x86: Improve sysfs event mapping with event string
:::::: TO: Stephane Eranian <[email protected]> :::::: CC: Arnaldo Carvalho de Melo <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 4/4] perf/x86/amd/power: Add AMD accumulated power reporting mechanism
Quoted 25 lines · kbuild test robot
Hi Huang,
[auto build test ERROR on bp/for-next]
[also build test ERROR on v4.4-rc8 next-20160106]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]url: https://github.com/0day-ci/linux/commits/Huang-Rui/perf-x86-power-Introduce-AMD-accumlated-power-reporting-mechanism/20160107-103309 base: https://git.kernel.org/pub/scm/linux/kernel/git/bp/bp for-next config: i386-randconfig-s1-201601 (attached as .config) reproduce: # save the attached .config to linux build tree make ARCH=i386All error/warnings (new ones prefixed by >>):
In file included from
include/linux/kobject.h:21:0,from include/linux/module.h:17, from arch/x86/kernel/cpu/perf_event_amd_power.c:13:Quoted 1 line
Quoted 1 line
arch/x86/kernel/cpu/perf_event.h:663:31: error: 'events_sysfs_show' undeclared here (not in a function).attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \ ^ include/linux/sysfs.h:103:10: note: in definition of macro '__ATTR' .show = _show, \ ^
Quoted 6 lines
Quoted 1 line
Quoted 1 line
arch/x86/kernel/cpu/perf_event_amd_power.c:244:1: note: in expansion of macro 'EVENT_ATTR_STR'EVENT_ATTR_STR(power-pkg, power_pkg, "event=0x01"); ^vim +/events_sysfs_show +663
arch/x86/kernel/cpu/perf_event.h
Thanks to report this issue.
That's because events_sysfs_show is declared under CONFIG_CPU_SUP_INTEL, I will add a patch to move it out.
Thanks,
Rui