Skip to contentSkip to conversation

perf/x86/power: Introduce AMD accumlated power reporting mechanism

PATCH Superseded Huang Rui 2 participants 9 messages Mar 3, 2016 – Mar 4, 2016

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Huang Rui

author
#0 lore raw

v6 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].

The V6 is rebased on bp/tip-perf. And need two dependent patches at tip because of modular perf driver:
http://git.kernel.org/tip/675965b00d734c985e4285f5bec7e524d15fc4e1
http://git.kernel.org/tip/3712bba1a260ad851f3aa8ddea9cb7326f6aa0b3

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.

Changes from v3 -> v4:
- Remove active_list, because it is not iterated.
- Capitalize sentences consistently and fix some typos.
- Fix some code style issues.
- Initialize structures in a vertically aligned manner.
- Remove unnecessary comment.
- Fix the runtime bug, and do some testing on CPU-hotplug scenario.

Changes from v4 -> v5:
- Remove "struct pmu" and lock from power_pmu, and rename it to power_pmu_masks
- As Peter's suggestion, add a new struct to hw_perf_event, and track these values from per-event.

Changes from v5 -> v6:
- Remove MAX_CUS check
- Remove DEFINE_PER_CPU(struct power_pmu_masks *, amd_power_pmu)
- Remove power_cpu_prepare power_cpu_free and refine power_cpu_init and power_cpu_exit
- Use smp_num_siblings instead of cores_per_cu variable.
- Make this driver as a module.
- Add a patch to export events_sysfs_show.

Thanks,
Rui

Huang Rui (2):
perf/x86: Export events_sysfs_show() perf/x86/amd/power: Add AMD accumulated power reporting mechanism

 arch/x86/Kconfig                           |   9 +
 arch/x86/kernel/cpu/Makefile               |   1 +
 arch/x86/kernel/cpu/perf_event.c           |   1 +
 arch/x86/kernel/cpu/perf_event_amd_power.c | 360 +++++++++++++++++++++++++++++
 include/linux/perf_event.h                 |   4 +
 5 files changed, 375 insertions(+)
 create mode 100644 arch/x86/kernel/cpu/perf_event_amd_power.c
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v6 1/2 perf/x86: Export events_sysfs_show()

This interface will be used in modular perf drivers.

---
 arch/x86/kernel/cpu/perf_event.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/perf_event.c +1 -0
index 1b443db..47f673c 100644
@@ -1601,6 +1601,7 @@ ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
 
 	return x86_pmu.events_sysfs_show(page, config);
 }
+EXPORT_SYMBOL_GPL(events_sysfs_show);
 
 EVENT_ATTR(cpu-cycles,			CPU_CYCLES		);
 EVENT_ATTR(instructions,		INSTRUCTIONS		);
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH v6 2/2 perf/x86/amd/power: Add AMD accumulated power reporting mechanism

In reply to Huang Rui

Introduce an AMD accumlated power reporting mechanism for the Family 15h, Model 60h processor that can be used to calculate the average power consumed by a processor during a measurement interval. The feature support is indicated by CPUID Fn8000_0007_EDX[12].

This feature will be implemented both in hwmon and perf. The current design provides one event to report per package/processor power consumption by counting each compute unit power value.

Here the gory details of how the computation is done:

---------------------------------------------------------------------
* 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 MSR_C001007b[MaxCpuSwPwrAcc]

* Jx/Jy: compute unit accumulated power which is indicated by MSR_C001007a[CpuSwPwrAcc]

* Tx/Ty: the value of performance timestamp counter which is indicated by CU_PTSC MSR_C0010280[PTSC]
* 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, software reads CpuSwPwrAcc and samples the PTSC.
Jx = value read from CpuSwPwrAcc and Tx = value read from PTSC.

iv. At time y, software reads CpuSwPwrAcc 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)
----------------------------------------------------------------------

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 elapsed
  root@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

---

Hi Thomas,

Thanks to suggest the updates for power_cpu_init(), but there are some minor issues with below codes.

target = cpumask_any_and(&cpu_mask, topology_sibling_cpumask(cpu)); if (target < nr_cpu_ids)

	cpumask_set_cpu(cpu, &cpu_mask);

For example: cores number is 4, and compute unit number is 2 (2 cores per compute units).

When amd_power_pmu completes initialization, the cpumask will be set as 1111. Actually, I just would like to choose one core per compute unit. So the expected value should be 0101.

when core 2 and core 3 are both online (they are in same compute unit) and cpu_mask is 1111,
set core 2 offline, then 1111 -> 1011,
set core 2 online again, then 1011 -> 1111.
Actually, we don't expect two cores both set at cpu_mask in the same compute unit.

So as inspired by you, I do below change for power_cpu_init and tested in my platform, please see my codes. :-)

Thanks,
Rui

---
 arch/x86/Kconfig                           |   9 +
 arch/x86/kernel/cpu/Makefile               |   1 +
 arch/x86/kernel/cpu/perf_event_amd_power.c | 360 +++++++++++++++++++++++++++++
 include/linux/perf_event.h                 |   4 +
 4 files changed, 374 insertions(+)
 create mode 100644 arch/x86/kernel/cpu/perf_event_amd_power.c
arch/x86/Kconfig +9 -0
index 330e738..a0d3fb0 100644
@@ -1202,6 +1202,15 @@ config MICROCODE_OLD_INTERFACE
 	def_bool y
 	depends on MICROCODE
 
+config PERF_EVENTS_AMD_POWER
+	depends on PERF_EVENTS && CPU_SUP_AMD
+	tristate "AMD Processor Power Reporting Mechanism"
+	---help---
+	  Provide power reporting mechanism support for AMD processors.
+	  Currently, it leverages X86_FEATURE_ACC_POWER
+	  (CPUID Fn8000_0007_EDX[12]) interface to calculate the
+	  average power consumption on Family 15h processors.
+
 config X86_MSR
 	tristate "/dev/cpu/*/msr - Model-specific register support"
 	---help---
arch/x86/kernel/cpu/Makefile +1 -0
index faa7b52..95d8419 100644
@@ -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_PERF_EVENTS_AMD_POWER)	+= 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 +360 -0

360 lines not on this page · raw message

include/linux/perf_event.h +4 -0
index f9828a4..01ea21c 100644
@@ -128,6 +128,10 @@ struct hw_perf_event {
 		struct { /* itrace */
 			int			itrace_started;
 		};
+		struct { /* amd_power */
+			u64	pwr_acc;
+			u64	ptsc;
+		};
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
 		struct { /* breakpoint */
 			/*
Signature · 1 line
1.9.1
#3 lore raw

Re: [PATCH v6 2/2] perf/x86/amd/power: Add AMD accumulated power reporting mechanism

arch/x86/kernel/cpu/perf_event_amd_power.c line 36

Quoted 6 lines · Huang Rui
arch/x86/kernel/cpu/perf_event_amd_power.c new file +6 -0
+/*
+ * The ratio of compute unit power accumulator sample period to the
+ * PTSC period.
+ */
+static unsigned int cpu_pwr_sample_ratio;
+static unsigned int cu_num;

Why do you need static storage for that information when the only purpose is to printk it in init?

arch/x86/kernel/cpu/perf_event_amd_power.c line 234

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +3 -0
+static void power_cpu_exit(int cpu)
+{
+	int target = nr_cpumask_bits;

What's that initialization for?

Quoted 23 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +23 -0
+
+	if (!cpumask_test_and_clear_cpu(cpu, &cpu_mask))
+		return;
+
+	/*
+	 * Find a new CPU on the same compute unit, if was set in cpumask
+	 * and still some CPUs on compute unit. Then migrate event and
+	 * context to new CPU.
+	 */
+	target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
+	if (target < nr_cpumask_bits) {
+		cpumask_set_cpu(target, &cpu_mask);
+		perf_pmu_migrate_context(&pmu_class, cpu, target);
+	}
+}
+
+static void power_cpu_init(int cpu)
+{
+	/*
+	 * 1) If any CPU is set at cpu_mask in the same compute unit, do
+	 * nothing.
+	 * 2) If no CPU is set at cpu_mask in the same compute unit,
+	 * set current STARTING CPU.

arch/x86/kernel/cpu/perf_event_amd_power.c line 267

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10 -0
+	 *
+	 * If cpu_mask and topology_sibling_cpumask has intersected
+	 * bits, that means any CPU is set in the same compute unit.
+	 * But cpumask_weight(topology_sibling_cpumask(cpu)) == 1
+	 * means no CPU is set on cpu_mask in the same compute unit
+	 * before init current STARTING CPU.
+	 */
+	if (!cpumask_intersects(&cpu_mask, topology_sibling_cpumask(cpu)) &&
+	    cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
+			cpumask_set_cpu(cpu, &cpu_mask);

I don't think you need that complexity.

	target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
	if (target >= nr_cpumask_bits)
		cpumask_set_cpu(cpu, &cpu_mask);

Simply because if there is a cpu aside of the new one already in the sibling mask, then it is also in cpu_mask. Hmm?

arch/x86/kernel/cpu/perf_event_amd_power.c line 281

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +12 -0
+static int
+power_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
+{
+	unsigned int cpu = (long)hcpu;
+
+	switch (action & ~CPU_TASKS_FROZEN) {
+	case CPU_STARTING:
+		power_cpu_init(cpu);
+		break;
+	case CPU_DOWN_PREPARE:
+		power_cpu_exit(cpu);
+		break;

And of course if CPU_DOWN_PREPARE fails and this is the last cpu in the compute unit, nothing takes over the duty for this compute unit. So you need to handle CPU_DOWN_FAILED ....

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10 -0
+static int __init amd_power_pmu_init(void)
+{
+	int i, ret;
+	u64 tmp;
+
+	if (!x86_match_cpu(cpu_match))
+		return 0;
+
+	if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
+		return -ENODEV;

arch/x86/kernel/cpu/perf_event_amd_power.c line 318

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10 -0
+
+	cu_num = boot_cpu_data.x86_max_cores / smp_num_siblings;
+
+	cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
+
+	if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
+		pr_err("Failed to read max compute unit power accumulator MSR\n");
+		return -ENODEV;
+	}
+	max_cu_acc_power = tmp;

Why do you need an intermediate 'tmp' for this?

arch/x86/kernel/cpu/perf_event_amd_power.c line 324

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +5 -0
+	cpu_notifier_register_begin();
+
+	/* Choose one online core of each compute unit. */
+	for (i = 0; i < boot_cpu_data.x86_max_cores; i += smp_num_siblings) {
+		WARN_ON(cpumask_empty(topology_sibling_cpumask(i)));

Err. What guarantees that in each compute unit is one sibling online? And what value has that WARN_ON? We don't care about the stack trace here, because it's known already.

arch/x86/kernel/cpu/perf_event_amd_power.c line 325

Quoted 1 line
arch/x86/kernel/cpu/perf_event_amd_power.c new file +1 -0
+		cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask);

Of course you just continue in that case and end up with:

       	      cpumask_set_cpu(nr_cpu_ids, &cpu_mask);

i.e. you try to do that on an invalid bit, which will trigger a justified warning in cpumask_set_cpu() if CONFIG_DEBUG_PER_CPU_MAPS is enabled.

Aside of that this only handles a single socket. And why do you do the above if you handle the same thing in the loop below?

arch/x86/kernel/cpu/perf_event_amd_power.c line 335

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10 -0
+	}
+
+	for_each_online_cpu(i)
+		power_cpu_init(i);
+
+	__register_cpu_notifier(&power_cpu_notifier_nb);
+
+	ret = perf_pmu_register(&pmu_class, "power", -1);
+	if (WARN_ON(ret)) {
+		pr_warn("AMD Power PMU registration failed\n");

This still leaks the cpu notifier. .....

arch/x86/kernel/cpu/perf_event_amd_power.c line 339

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +4 -0
+		goto out;
+	}
+
+	pr_info("AMD Power PMU detected, %d compute units\n", cu_num);

Why is the number of compute units interesting at all?

Thanks,

tglx

author
#4 lore raw

Re: [PATCH v6 2/2] perf/x86/amd/power: Add AMD accumulated power reporting mechanism

Quoted 22 lines · Thomas Gleixner
Quoted 8 lines · Huang Rui
arch/x86/kernel/cpu/perf_event_amd_power.c new file +8 -0
+
+static void power_cpu_init(int cpu)
+{
+	/*
+	 * 1) If any CPU is set at cpu_mask in the same compute unit, do
+	 * nothing.
+	 * 2) If no CPU is set at cpu_mask in the same compute unit,
+	 * set current STARTING CPU.

arch/x86/kernel/cpu/perf_event_amd_power.c line 267

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10 -0
+	 *
+	 * If cpu_mask and topology_sibling_cpumask has intersected
+	 * bits, that means any CPU is set in the same compute unit.
+	 * But cpumask_weight(topology_sibling_cpumask(cpu)) == 1
+	 * means no CPU is set on cpu_mask in the same compute unit
+	 * before init current STARTING CPU.
+	 */
+	if (!cpumask_intersects(&cpu_mask, topology_sibling_cpumask(cpu)) &&
+	    cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
+			cpumask_set_cpu(cpu, &cpu_mask);

I don't think you need that complexity.

Quoted 7 lines
	target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
	if (target >= nr_cpumask_bits)
		cpumask_set_cpu(cpu, &cpu_mask);

Simply because if there is a cpu aside of the new one already in the sibling mask, then it is also in cpu_mask. Hmm?

Make sense. Thanks. Will update.

Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +8 -0
+static int
+power_cpu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
+{
+	unsigned int cpu = (long)hcpu;
+
+	switch (action & ~CPU_TASKS_FROZEN) {
+	case CPU_STARTING:
+		power_cpu_init(cpu);
Quoted 9 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 281

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +4 -0
+		break;
+	case CPU_DOWN_PREPARE:
+		power_cpu_exit(cpu);
+		break;

And of course if CPU_DOWN_PREPARE fails and this is the last cpu in the compute unit, nothing takes over the duty for this compute unit. So you need to handle CPU_DOWN_FAILED ....

OK, so I need to do power_cpu_init when notified CPU_DOWN_FAILED, am I right?

Quoted 14 lines
Quoted 14 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +14 -0
+static int __init amd_power_pmu_init(void)
+{
+	int i, ret;
+	u64 tmp;
+
+	if (!x86_match_cpu(cpu_match))
+		return 0;
+
+	if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
+		return -ENODEV;
+
+	cu_num = boot_cpu_data.x86_max_cores / smp_num_siblings;
+
+	cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
Quoted 9 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 318

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +6 -0
+
+	if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
+		pr_err("Failed to read max compute unit power accumulator MSR\n");
+		return -ENODEV;
+	}
+	max_cu_acc_power = tmp;

Why do you need an intermediate 'tmp' for this?

Will use max_cu_acc_power directly.

Quoted 10 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 324

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +5 -0
+	cpu_notifier_register_begin();
+
+	/* Choose one online core of each compute unit. */
+	for (i = 0; i < boot_cpu_data.x86_max_cores; i += smp_num_siblings) {
+		WARN_ON(cpumask_empty(topology_sibling_cpumask(i)));

Err. What guarantees that in each compute unit is one sibling online? And what value has that WARN_ON? We don't care about the stack trace here, because it's known already.

When this driver is not as module before, I think there should be one sibling online at least at initialization phase. But now, you're right, we cannot guarantee it.

Quoted 4 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 325

Quoted 1 line
arch/x86/kernel/cpu/perf_event_amd_power.c new file +1 -0
+		cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask);

Of course you just continue in that case and end up with:

Quoted 8 lines
       	      cpumask_set_cpu(nr_cpu_ids, &cpu_mask);

i.e. you try to do that on an invalid bit, which will trigger a justified warning in cpumask_set_cpu() if CONFIG_DEBUG_PER_CPU_MAPS is enabled.

Aside of that this only handles a single socket. And why do you do the above if you handle the same thing in the loop below?

Because the sibling online shouldn't be empty at initialization phase if the driver is not module before. So...

Thanks to catch it.

How about below update:

for (i = 0; i < boot_cpu_data.x86_max_cores; i += smp_num_siblings) {
        if (!cpumask_empty(topology_sibling_cpumask(i)))
                cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask);
}
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +4 -0
+	}
+
+	for_each_online_cpu(i)
+		power_cpu_init(i);
Quoted 9 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 335

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +6 -0
+
+	__register_cpu_notifier(&power_cpu_notifier_nb);
+
+	ret = perf_pmu_register(&pmu_class, "power", -1);
+	if (WARN_ON(ret)) {
+		pr_warn("AMD Power PMU registration failed\n");

This still leaks the cpu notifier. .....

OK, so I should do __unregister_cpu_notifier(&power_cpu_notifier_nb) here.

Quoted 7 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 339

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +4 -0
+		goto out;
+	}
+
+	pr_info("AMD Power PMU detected, %d compute units\n", cu_num);

Why is the number of compute units interesting at all?

Because the accumulated power bases on compute units. We can see the mask from /sys/devices/power/cpumask and number of compute units to know if all compute units are set at cpumask. So I add a printk here, does it make sense?

Thanks,
Rui

#5 lore raw

Re: [PATCH v6 2/2] perf/x86/amd/power: Add AMD accumulated power reporting mechanism

Quoted 9 lines · Huang Rui
Quoted 5 lines · Thomas Gleixner

On Thu, 3 Mar 2016, Huang Rui wrote:
And of course if CPU_DOWN_PREPARE fails and this is the last cpu in the compute unit, nothing takes over the duty for this compute unit. So you need to handle CPU_DOWN_FAILED ....

OK, so I need to do power_cpu_init when notified CPU_DOWN_FAILED, am I right?

Yes.

Quoted 31 lines
Quoted 10 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 324

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +5 -0
+	cpu_notifier_register_begin();
+
+	/* Choose one online core of each compute unit. */
+	for (i = 0; i < boot_cpu_data.x86_max_cores; i += smp_num_siblings) {
+		WARN_ON(cpumask_empty(topology_sibling_cpumask(i)));

Err. What guarantees that in each compute unit is one sibling online? And what value has that WARN_ON? We don't care about the stack trace here, because it's known already.

When this driver is not as module before, I think there should be one sibling online at least at initialization phase. But now, you're right, we cannot guarantee it.

Quoted 4 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 325

Quoted 1 line
arch/x86/kernel/cpu/perf_event_amd_power.c new file +1 -0
+		cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask);

Of course you just continue in that case and end up with:

Quoted 8 lines
       	      cpumask_set_cpu(nr_cpu_ids, &cpu_mask);

i.e. you try to do that on an invalid bit, which will trigger a justified warning in cpumask_set_cpu() if CONFIG_DEBUG_PER_CPU_MAPS is enabled.

Aside of that this only handles a single socket. And why do you do the above if you handle the same thing in the loop below?

Because the sibling online shouldn't be empty at initialization phase if the driver is not module before. So...

Quoted 8 lines

Thanks to catch it.

How about below update:

for (i = 0; i < boot_cpu_data.x86_max_cores; i += smp_num_siblings) {
        if (!cpumask_empty(topology_sibling_cpumask(i)))
                cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask);
}

Why? You do a full for_each_online_cpu(i) loop after that, which does exactly the same thing, right?

Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +4 -0
+	}
+
+	for_each_online_cpu(i)
+		power_cpu_init(i);
Quoted 9 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 335

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +6 -0
+
+	__register_cpu_notifier(&power_cpu_notifier_nb);
+
+	ret = perf_pmu_register(&pmu_class, "power", -1);
+	if (WARN_ON(ret)) {
+		pr_warn("AMD Power PMU registration failed\n");

This still leaks the cpu notifier. .....

Quoted 2 lines

OK, so I should do __unregister_cpu_notifier(&power_cpu_notifier_nb) here.

You can register the notifier after perf_pmu_register succeeded, right?

Quoted 9 lines
Quoted 4 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 339

Quoted 1 line
arch/x86/kernel/cpu/perf_event_amd_power.c new file +1 -0
+	pr_info("AMD Power PMU detected, %d compute units\n", cu_num);

Why is the number of compute units interesting at all?

Because the accumulated power bases on compute units. We can see the mask from /sys/devices/power/cpumask and number of compute units to know if all compute units are set at cpumask. So I add a printk here, does it make sense?

No, because it's completely non intuitive. How on earth am I supposed to get the connection between /sys/devices/power/cpumask and number of compute units without staring at the code? So this is only interesting for a developer who can deduce that number from /proc/cpuinfo or dmesg as well.

Thanks,

tglx

author
#6 lore raw

Re: [PATCH v6 2/2] perf/x86/amd/power: Add AMD accumulated power reporting mechanism

Quoted 48 lines · Thomas Gleixner
Quoted 9 lines · Huang Rui
Quoted 5 lines · Thomas Gleixner

On Thu, 3 Mar 2016, Huang Rui wrote:
And of course if CPU_DOWN_PREPARE fails and this is the last cpu in the compute unit, nothing takes over the duty for this compute unit. So you need to handle CPU_DOWN_FAILED ....

OK, so I need to do power_cpu_init when notified CPU_DOWN_FAILED, am I right?

Yes.

Quoted 35 lines
Quoted 10 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 324

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +5 -0
+	cpu_notifier_register_begin();
+
+	/* Choose one online core of each compute unit. */
+	for (i = 0; i < boot_cpu_data.x86_max_cores; i += smp_num_siblings) {
+		WARN_ON(cpumask_empty(topology_sibling_cpumask(i)));

Err. What guarantees that in each compute unit is one sibling online? And what value has that WARN_ON? We don't care about the stack trace here, because it's known already.

When this driver is not as module before, I think there should be one sibling online at least at initialization phase. But now, you're right, we cannot guarantee it.

Quoted 4 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 325

Quoted 1 line
arch/x86/kernel/cpu/perf_event_amd_power.c new file +1 -0
+		cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask);

Of course you just continue in that case and end up with:

Quoted 8 lines
       	      cpumask_set_cpu(nr_cpu_ids, &cpu_mask);

i.e. you try to do that on an invalid bit, which will trigger a justified warning in cpumask_set_cpu() if CONFIG_DEBUG_PER_CPU_MAPS is enabled.

Aside of that this only handles a single socket. And why do you do the above if you handle the same thing in the loop below?

Because the sibling online shouldn't be empty at initialization phase if the driver is not module before. So...

Thanks to catch it.

How about below update:

Quoted 8 lines
Quoted 4 lines
for (i = 0; i < boot_cpu_data.x86_max_cores; i += smp_num_siblings) {
        if (!cpumask_empty(topology_sibling_cpumask(i)))
                cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask);
}

Why? You do a full for_each_online_cpu(i) loop after that, which does exactly the same thing, right?

But looks like power_cpu_init cannot handle it if we don't take any action here.

e. g.
cpu_mask: 0000 and online mask: 1111 -> power_cpu_init(0) -> cpu_mask is still: 0000

topology_sibling_cpumask(0): 0011
target: 1 (i. e. we cannot do cpumask_set_cpu(0, &cpu_mask))

Maybe, we need to think out a stronger power_cpu_init if you want to only do init thing at for_each_online_cpu.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 335

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_amd_power.c new file +10 -0
+	}
+
+	for_each_online_cpu(i)
+		power_cpu_init(i);
+
+	__register_cpu_notifier(&power_cpu_notifier_nb);
+
+	ret = perf_pmu_register(&pmu_class, "power", -1);
+	if (WARN_ON(ret)) {
+		pr_warn("AMD Power PMU registration failed\n");

This still leaks the cpu notifier. .....

Quoted 5 lines
Quoted 2 lines

OK, so I should do __unregister_cpu_notifier(&power_cpu_notifier_nb) here.

You can register the notifier after perf_pmu_register succeeded, right?

Yep.

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

arch/x86/kernel/cpu/perf_event_amd_power.c line 339

Quoted 1 line
arch/x86/kernel/cpu/perf_event_amd_power.c new file +1 -0
+	pr_info("AMD Power PMU detected, %d compute units\n", cu_num);

Why is the number of compute units interesting at all?

Because the accumulated power bases on compute units. We can see the mask from /sys/devices/power/cpumask and number of compute units to know if all compute units are set at cpumask. So I add a printk here, does it make sense?

Quoted 5 lines

No, because it's completely non intuitive. How on earth am I supposed to get the connection between /sys/devices/power/cpumask and number of compute units without staring at the code? So this is only interesting for a developer who can deduce that number from /proc/cpuinfo or dmesg as well.

OK, I will remove unnecessary cu_num next version.

Thanks,
Rui

#7 lore raw

Re: [PATCH v6 2/2] perf/x86/amd/power: Add AMD accumulated power reporting mechanism

Quoted 5 lines · Huang Rui
Quoted 3 lines · Thomas Gleixner

Why? You do a full for_each_online_cpu(i) loop after that, which does exactly the same thing, right?

Quoted 8 lines

But looks like power_cpu_init cannot handle it if we don't take any action here.

e. g.
cpu_mask: 0000 and online mask: 1111 -> power_cpu_init(0) -> cpu_mask is still: 0000

topology_sibling_cpumask(0): 0011
target: 1 (i. e. we cannot do cpumask_set_cpu(0, &cpu_mask))

Fair enough, but then you don't need the power_cpu_init() call at all.

But your loop does not cover anything beyond the first socket. So you need a separate init function which does:

for_each_online_cpu(cpu) {

   	target = cpumask_first(topology_sibling_cpumask(cpu));
	if (!cpumask_test_cpu(target, cpumask))
	   	cpumask_set_cpu(target, cpumask);

}

Thanks,

tglx

author
#8 lore raw

Re: [PATCH v6 2/2] perf/x86/amd/power: Add AMD accumulated power reporting mechanism

Quoted 17 lines · Thomas Gleixner
Quoted 5 lines · Huang Rui
Quoted 3 lines · Thomas Gleixner

Why? You do a full for_each_online_cpu(i) loop after that, which does exactly the same thing, right?

Quoted 8 lines

But looks like power_cpu_init cannot handle it if we don't take any action here.

e. g.
cpu_mask: 0000 and online mask: 1111 -> power_cpu_init(0) -> cpu_mask is still: 0000

topology_sibling_cpumask(0): 0011
target: 1 (i. e. we cannot do cpumask_set_cpu(0, &cpu_mask))

Fair enough, but then you don't need the power_cpu_init() call at all.

Quoted 9 lines

But your loop does not cover anything beyond the first socket. So you need a separate init function which does:

   for_each_online_cpu(cpu) {
   	target = cpumask_first(topology_sibling_cpumask(cpu));
	if (!cpumask_test_cpu(target, cpumask))
	   	cpumask_set_cpu(target, cpumask);
   }	      

Looks good. Thanks Thomas. Will update it.

Rui