perf/x86/amd/power: Add AMD accumulated power reporting mechanism
Subsystems (2)
PATCH v3 1/1 perf/x86/amd/power: Add AMD accumulated power reporting mechanism
Introduce 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]
---
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].
Changes from v1 -> v2:
- Add a patch to fix the build issue which is reported by kbuild test robot.
Changes from v2 -> v3:
- Use raw_spinlock_t instead of spinlock_t, because it need meet the -rt mode use case.
- Use topology_sibling_cpumask to make the cpumask operation easier.
Thanks,
Rui
---
arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/cpu/perf_event_amd_power.c | 505 +++++++++++++++++++++++++++++ 2 files changed, 506 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 +505 -0
505 lines not on this page · raw message
Signature · 1 line
1.9.1
Peter Zijlstra
maintainerarch/x86/kernel/cpu/perf_event_amd_power.c line 50
Quoted 3 lines · Huang Rui
arch/x86/kernel/cpu/perf_event_amd_power.c new file +3
-0+struct power_pmu { +raw_spinlock_t lock; +struct list_head active_list;
Maybe a dumb question, but what is that list for?
You only ever add/del events to/from it, you never iterate it. So why keep it?
If you drop the list, the lock can go too I think, making all this stuff simpler.
Quoted 7 lines · Peter Zijlstra
arch/x86/kernel/cpu/perf_event_amd_power.c line 50
Quoted 3 lines · Huang Rui
arch/x86/kernel/cpu/perf_event_amd_power.c new file +3
-0+struct power_pmu { +raw_spinlock_t lock; +struct list_head active_list;Maybe a dumb question, but what is that list for?
Quoted 5 lines
You only ever add/del events to/from it, you never iterate it. So why keep it?
If you drop the list, the lock can go too I think, making all this stuff simpler.
Yes, the active_list seems a bit superfluous if not be iterated. I will remove it.
Any other comments? :-)
Thanks,
Rui
Quoted 15 lines · Huang Rui
arch/x86/kernel/cpu/perf_event_amd_power.c new file +13
-0+/* +* Acc power status counters +*/ +#define AMD_POWER_PKG_ID 0 +#define AMD_POWER_EVENTSEL_PKG 1 +/* +* the ratio of compute unit power accumulator sample period to the +* PTSC period +*/ +/* +* Accumulated power is to measure the sum of each compute unit's +* power consumption. So it picks only one core from each compute unit +* to get the power with MSR_F15H_CU_PWR_ACCUMULATOR. The cpu_mask
arch/x86/kernel/cpu/perf_event_amd_power.c line 97
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +9
-0+* represents CPU bit map of all cores which are picked to measure the +* power for the compute units that they belong to. +*/ +static cpumask_t cpu_mask; +/* +* calculate the power comsumption for each compute unit over +* a time period, the unit of final value (delta) is +* micro-Watts. Then add it into event count. +*/
Please capitalize sentences consistently - half of the comments you added start lower-case.
arch/x86/kernel/cpu/perf_event_amd_power.c line 114
Quoted 2 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +2
-0+static void __pmu_event_start (struct power_pmu *pmu, +struct perf_event *event)
So this looks better either on a single line, or as:
static void__pmu_event_start(struct power_pmu *pmu, struct perf_event *event)
Breaking the argument list combines the worst of the two worlds.
arch/x86/kernel/cpu/perf_event_amd_power.c line 157
Quoted 4 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +4
-0+if ((mode &PERF_EF_UPDATE ) && !(hwc->state &PERF_HES_UPTODATE )) { +/* +* Drain the remaining delta count out of a event +* that we are disabling:
s/an event
(Please re-read all the comments - I saw a few other typos and spelling mistakes as well.)
arch/x86/kernel/cpu/perf_event_amd_power.c line 216
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10
-0+if (cfg ==AMD_POWER_EVENTSEL_PKG ) + bit =AMD_POWER_PKG_ID ; +else +return -EINVAL ; + + event->hw.event_base =MSR_F15H_CU_PWR_ACCUMULATOR ; + event->hw.config = cfg; + event->hw.idx = bit; + +return ret;
so this control flow looks pretty weird. Why not:
arch/x86/kernel/cpu/perf_event_amd_power.c line 216
Quoted 8 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +8
-0+if (cfg !=AMD_POWER_EVENTSEL_PKG ) +return -EINVAL ; + + event->hw.event_base =MSR_F15H_CU_PWR_ACCUMULATOR ; + event->hw.config = cfg; + event->hw.idx =AMD_POWER_PKG_ID ; + +return ret;
?
arch/x86/kernel/cpu/perf_event_amd_power.c line 245
Quoted 7 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +7
-0+static struct attribute_group pmu_attr_group = { + .attrs = pmu_attrs, +}; + + +/* +* at current, it only supports to report the power of each
s/currently it only supports power reporting of each
Quoted 15 lines
+ * processor/package
+ */arch/x86/kernel/cpu/perf_event_amd_power.c new file +13
-0+EVENT_ATTR_STR (power-pkg, power_pkg,"event=0x01" ); + +EVENT_ATTR_STR (power-pkg.unit, power_pkg_unit,"mWatts" ); + +/* convert the count from micro-Watts to milli-Watts */ +EVENT_ATTR_STR (power-pkg.scale, power_pkg_scale,"1.000000e-3" ); + + +static struct attribute *events_attr[] = { +EVENT_PTR (power_pkg), +EVENT_PTR (power_pkg_unit), +EVENT_PTR (power_pkg_scale), +NULL ,
arch/x86/kernel/cpu/perf_event_amd_power.c line 278
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +9
-0+}; + +static struct attribute_group pmu_events_group = { + .name ="events" , + .attrs = events_attr, +static struct attribute_group pmu_format_group = { + .name ="format" , + .attrs = formats_attr, +};
Please initialize structures in a vertically aligned manner, like you did it later on:
arch/x86/kernel/cpu/perf_event_amd_power.c line 296
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10
-0+static struct pmu pmu_class = { + .attr_groups = attr_groups, + .task_ctx_nr = perf_invalid_context,/* system-wide only */ + .event_init = pmu_event_init, + .add = pmu_event_add,/* must have */ + .del = pmu_event_del,/* must have */ + .start = pmu_event_start, + .stop = pmu_event_stop, + .read = pmu_event_read, +};
Btw., I don't think the 'must have' comment adds any important information needed to understand this new PMU driver.
arch/x86/kernel/cpu/perf_event_amd_power.c line 347
Quoted 13 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +13
-0+static int power_cpu_init (int cpu) +{ +struct power_pmu *pmu =per_cpu (amd_power_pmu, cpu); + +if (pmu) +return 0 ; + +if (!cpumask_and (pmu->mask,topology_sibling_cpumask (cpu), + &cpu_mask)) +cpumask_set_cpu (cpu, &cpu_mask); + +return 0 ; +}
Hm, has this function ever been runtime tested? This function either does nothing (contrary to the clear intention of twiddling the cpu_mask), or crashes on a NULL pointer.
( Also, the code has an annoying line-break. Don't pacify checkpatch by making the
code harder to read. )
Thanks,
Ingo
Quoted 23 lines · Ingo Molnar
Quoted 19 lines · Huang Rui
arch/x86/kernel/cpu/perf_event_amd_power.c new file +17
-0+/* +* Acc power status counters +*/ +#define AMD_POWER_PKG_ID 0 +#define AMD_POWER_EVENTSEL_PKG 1 +/* +* the ratio of compute unit power accumulator sample period to the +* PTSC period +*/ +/* +* Accumulated power is to measure the sum of each compute unit's +* power consumption. So it picks only one core from each compute unit +* to get the power with MSR_F15H_CU_PWR_ACCUMULATOR. The cpu_mask +* represents CPU bit map of all cores which are picked to measure the +* power for the compute units that they belong to. +*/ +static cpumask_t cpu_mask;
Quoted 9 lines
arch/x86/kernel/cpu/perf_event_amd_power.c line 97
Quoted 5 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +5
-0+/* +* calculate the power comsumption for each compute unit over +* a time period, the unit of final value (delta) is +* micro-Watts. Then add it into event count. +*/ Please capitalize sentences consistently - half of the comments you added start lower-case.
Some of lower-case starting cases are not complete sentences such as:
/*
* the ratio of compute unit power accumulator sample period to the
* PTSC period
*/
/* maximum accumulated power of a compute unit */
So can I check again and capitalize comment if it is complete sentence?
Quoted 14 lines
arch/x86/kernel/cpu/perf_event_amd_power.c line 216
Quoted 11 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10
-0+if (cfg ==AMD_POWER_EVENTSEL_PKG ) + bit =AMD_POWER_PKG_ID ; +else +return -EINVAL ; + + event->hw.event_base =MSR_F15H_CU_PWR_ACCUMULATOR ; + event->hw.config = cfg; + event->hw.idx = bit; + +return ret;so this control flow looks pretty weird. Why not:
Quoted 11 lines
arch/x86/kernel/cpu/perf_event_amd_power.c line 216
Quoted 8 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +8
-0+if (cfg !=AMD_POWER_EVENTSEL_PKG ) +return -EINVAL ; + + event->hw.event_base =MSR_F15H_CU_PWR_ACCUMULATOR ; + event->hw.config = cfg; + event->hw.idx =AMD_POWER_PKG_ID ; + +return ret;?
Looks better.
Quoted 18 lines
arch/x86/kernel/cpu/perf_event_amd_power.c line 347
Quoted 13 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +13
-0+static int power_cpu_init (int cpu) +{ +struct power_pmu *pmu =per_cpu (amd_power_pmu, cpu); + +if (pmu) +return 0 ; + +if (!cpumask_and (pmu->mask,topology_sibling_cpumask (cpu), + &cpu_mask)) +cpumask_set_cpu (cpu, &cpu_mask); + +return 0 ; +}Hm, has this function ever been runtime tested? This function either does nothing (contrary to the clear intention of twiddling the
cpu_mask), or crashes on a NULL pointer.
Quoted 3 lines
( Also, the code has an annoying line-break. Don't pacify checkpatch by making the
code harder to read. )
OK, that should be "if (!pmu)", thanks to check so carefully. I tested to make the core offline and check the event context migration with power_cpu_exit, but might miss this scenario. I will do more testing on runtime case. Will fix it on next version.
Thanks,
Rui