Skip to contentSkip to conversation

perf: Optimise topology iteration

PATCH Superseded Lin Ming 2 participants 5 messages Feb 20, 2011 – Feb 21, 2011

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lin Ming

author
#0 lore raw

PATCH 5/6 perf: Optimise topology iteration

Currently we iterate the full machine looking for a matching core_id/nb for the percore and the amd northbridge stuff , using a smaller topology mask makes sense.

---
 arch/x86/kernel/cpu/perf_event_amd.c   |    3 ++-
 arch/x86/kernel/cpu/perf_event_intel.c |    2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event_amd.c +2 -1
index 461f62b..7217d84 100644
@@ -320,6 +320,7 @@ static void amd_pmu_cpu_starting(int cpu)
 {
 	struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
 	struct amd_nb *nb;
+	const struct cpumask *mask = cpu_coregroup_mask(cpu);
 	int i, nb_id;
 
 	if (boot_cpu_data.x86_max_cores < 2)
@@ -328,7 +329,7 @@ static void amd_pmu_cpu_starting(int cpu)
 	nb_id = amd_get_nb_id(cpu);
 	WARN_ON_ONCE(nb_id == BAD_APICID);
 
-	for_each_online_cpu(i) {
+	for_each_cpu(i, mask) {
 		nb = per_cpu(cpu_hw_events, i).amd_nb;
 		if (WARN_ON_ONCE(!nb))
 			continue;
arch/x86/kernel/cpu/perf_event_intel.c +1 -1
index 059c0ab..5540d35 100644
@@ -1123,7 +1123,7 @@ static void intel_pmu_cpu_starting(int cpu)
 	if (!ht_capable())
 		return;
 
-	for_each_online_cpu(i) {
+	for_each_cpu(i, topology_thread_cpumask(cpu)) {
 		struct intel_percore *pc = per_cpu(cpu_hw_events, i).per_core;
 
 		if (pc && pc->core_id == core_id) {
Signature · 1 line
1.7.3
#1 lore raw
Quoted 3 lines · Lin Ming

Currently we iterate the full machine looking for a matching core_id/nb for the percore and the amd northbridge stuff , using a smaller topology mask makes sense.

This is still wrong for CPU hotplug. The CPU "owning" the per core does not necessarily need to be online anymore.
Please drop this patch.

-Andi

author
#2 lore raw
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Lin Ming

Currently we iterate the full machine looking for a matching core_id/nb for the percore and the amd northbridge stuff , using a smaller topology mask makes sense.

This is still wrong for CPU hotplug. The CPU "owning" the per core does not necessarily need to be online anymore.

This is remain issue for hotplug case, no matter we use for_each_online_cpu or topology_thread_cpumask.

Quoted 1 line

Please drop this patch.

Re-look at the code, I think for_each_online_cpu is wrong for percore, we should use topology_thread_cpumask instead.

for_each_online_cpu(i) {
	struct intel_percore *pc = per_cpu(cpu_hw_events, i).per_core;

	if (pc && pc->core_id == core_id) {       
		kfree(cpuc->per_core);
		cpuc->per_core = pc;              
		break;
	}
}

Assume 2 sockets,

//socket 0
cpu 0: core_id 0
cpu 1: core_id 0

//socket 1
cpu 2: core_id 0
cpu 3: core_id 0

If for_each_online_cpu is used, apparently 4 logical cpus will share the same percore. This is wrong.

If topology_thread_cpumask is used, then cpu0 and cpu1 share one percore and cpu2 and cpu3 share another percore. This is what we want.

Lin Ming

Quoted 2 lines

-Andi

#3 lore raw
Quoted 11 lines · Lin Ming
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Lin Ming

Currently we iterate the full machine looking for a matching core_id/nb for the percore and the amd northbridge stuff , using a smaller topology mask makes sense.

This is still wrong for CPU hotplug. The CPU "owning" the per core does not necessarily need to be online anymore.

This is remain issue for hotplug case, no matter we use for_each_online_cpu or topology_thread_cpumask.

The original code I submitted used for_each_possible_cpu which is correct.

Quoted 5 lines
Quoted 2 lines

Please drop this patch.

Re-look at the code, I think for_each_online_cpu is wrong for percore, we should use topology_thread_cpumask instead.

No, that's also cleared on unplug. You really need the possible map and nothing else.

-Andi

author
#4 lore raw
Quoted 17 lines · Andi Kleen
Quoted 11 lines · Lin Ming
Quoted 7 lines · Andi Kleen
Quoted 3 lines · Lin Ming

Currently we iterate the full machine looking for a matching core_id/nb for the percore and the amd northbridge stuff , using a smaller topology mask makes sense.

This is still wrong for CPU hotplug. The CPU "owning" the per core does not necessarily need to be online anymore.

This is remain issue for hotplug case, no matter we use for_each_online_cpu or topology_thread_cpumask.

The original code I submitted used for_each_possible_cpu which is correct.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Please drop this patch.

Re-look at the code, I think for_each_online_cpu is wrong for percore, we should use topology_thread_cpumask instead.

No, that's also cleared on unplug. You really need the possible map and nothing else.

That's wrong for kernel initialization, not related to hotplug.

I wrote a simple debug patch,

arch/x86/kernel/cpu/perf_event_intel.c +4 -1
index f152930..913a8a5 100644
@@ -1123,7 +1123,7 @@ static void intel_pmu_cpu_starting(int cpu)
 	if (!ht_capable())
 		return;
 
-	for_each_cpu(i, topology_thread_cpumask(cpu)) {
+	for_each_possible_cpu(i) {
 		struct intel_percore *pc = per_cpu(cpu_hw_events, i).per_core;
 
 		if (pc && pc->core_id == core_id) {
@@ -1135,6 +1135,9 @@ static void intel_pmu_cpu_starting(int cpu)
 
 	cpuc->per_core->core_id = core_id;
 	cpuc->per_core->refcnt++;
+
+	printk("DEBUG: cpu%d, per_core %p, core_id: %d, ref_count: %d\n",
+		cpu, cpuc->per_core, cpuc->per_core->core_id, cpuc->per_core->refcnt);
 }
 
 static void intel_pmu_cpu_dying(int cpu)

The output as below,

DEBUG: cpu0, per_core ffff8801bec32600, core_id: 0, ref_count: 1
DEBUG: cpu1, per_core ffff8801bec32600, core_id: 0, ref_count: 2
DEBUG: cpu2, per_core ffff8801bec32a20, core_id: 1, ref_count: 1
DEBUG: cpu3, per_core ffff8801bec32a20, core_id: 1, ref_count: 2
DEBUG: cpu4, per_core ffff8801bec32de0, core_id: 2, ref_count: 1
DEBUG: cpu5, per_core ffff8801bec32de0, core_id: 2, ref_count: 2
DEBUG: cpu6, per_core ffff8801becfc120, core_id: 3, ref_count: 1
DEBUG: cpu7, per_core ffff8801becfc120, core_id: 3, ref_count: 2
DEBUG: cpu8, per_core ffff8801bec32600, core_id: 0, ref_count: 3
DEBUG: cpu9, per_core ffff8801bec32600, core_id: 0, ref_count: 4
DEBUG: cpu10, per_core ffff8801bec32a20, core_id: 1, ref_count: 3
DEBUG: cpu11, per_core ffff8801bec32a20, core_id: 1, ref_count: 4
DEBUG: cpu12, per_core ffff8801bec32de0, core_id: 2, ref_count: 3
DEBUG: cpu13, per_core ffff8801bec32de0, core_id: 2, ref_count: 4
DEBUG: cpu14, per_core ffff8801becfc120, core_id: 3, ref_count: 3
DEBUG: cpu15, per_core ffff8801becfc120, core_id: 3, ref_count: 4

As you can see, cpu0, cpu1, cpu8 and cpu9 share the same per_core(ffff8801bec32600). This is wrong.

cpu0 and cpu8 should share one pef_core, cpu1 and cpu9 share another per_core.

Quoted 2 lines

-Andi