Skip to contentSkip to conversation

selftests: powerpc: Fix CPU affinity for child process

PATCH Superseded Harish 3 participants 3 messages Jun 9, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Harish

author
#0 lore raw

PATCH v2 1/1 selftests: powerpc: Fix CPU affinity for child process

On systems with large number of cpus, test fails trying to set affinity for child process by calling sched_setaffinity() with smaller size for cpuset. This patch fixes it by making sure that the size of allocated cpu set is dependent on the number of CPUs as reported by get_nprocs().

Fixes 00b7ec5c9cf3 ("selftests/powerpc: Import Anton's context_switch2 benchmark")
---
 .../powerpc/benchmarks/context_switch.c        | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
tools/testing/selftests/powerpc/benchmarks/context_switch.c +12 -6
index a2e8c9da7fa5..de6c49d6f88f 100644
@@ -19,6 +19,7 @@
 #include <limits.h>
 #include <sys/time.h>
 #include <sys/syscall.h>
+#include <sys/sysinfo.h>
 #include <sys/types.h>
 #include <sys/shm.h>
 #include <linux/futex.h>
@@ -104,8 +105,9 @@ static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 
 static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 {
-	int pid;
-	cpu_set_t cpuset;
+	int pid, ncpus;
+	cpu_set_t *cpuset;
+	size_t size;
 
 	pid = fork();
 	if (pid == -1) {
@@ -116,12 +118,16 @@ static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 	if (pid)
 		return;
 
-	CPU_ZERO(&cpuset);
-	CPU_SET(cpu, &cpuset);
+	size = CPU_ALLOC_SIZE(ncpus);
+	ncpus = get_nprocs();
+	cpuset = CPU_ALLOC(ncpus);
+	CPU_ZERO_S(size, cpuset);
+	CPU_SET_S(cpu, size, cpuset);
 
-	if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) {
+	if (sched_setaffinity(0, size, cpuset)) {
 		perror("sched_setaffinity");
-		exit(1);
+		CPU_FREE(cpuset);
+		exit(-1);
 	}
 
 	fn(arg);
Signature · 1 line
2.24.1
#1 lore raw
Quoted 39 lines · Harish

On systems with large number of cpus, test fails trying to set affinity for child process by calling sched_setaffinity() with smaller size for cpuset. This patch fixes it by making sure that the size of allocated cpu set is dependent on the number of CPUs as reported by get_nprocs().

Fixes 00b7ec5c9cf3 ("selftests/powerpc: Import Anton's context_switch2 benchmark")
---
 .../powerpc/benchmarks/context_switch.c        | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
tools/testing/selftests/powerpc/benchmarks/context_switch.c +4 -2
index a2e8c9da7fa5..de6c49d6f88f 100644
@@ -19,6 +19,7 @@
 #include <limits.h>
 #include <sys/time.h>
 #include <sys/syscall.h>
+#include <sys/sysinfo.h>
 #include <sys/types.h>
 #include <sys/shm.h>
 #include <linux/futex.h>
@@ -104,8 +105,9 @@ static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 
 static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 {
-	int pid;
-	cpu_set_t cpuset;
+	int pid, ncpus;
+	cpu_set_t *cpuset;
+	size_t size;
 
 	pid = fork();
 	if (pid == -1) {

tools/testing/selftests/powerpc/benchmarks/context_switch.c static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) · line 122

Quoted 7 lines
tools/testing/selftests/powerpc/benchmarks/context_switch.c +2 -2
@@ -116,12 +118,16 @@ static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 	if (pid)
 		return;
 
-	CPU_ZERO(&cpuset);
-	CPU_SET(cpu, &cpuset);
+	size = CPU_ALLOC_SIZE(ncpus);
+	ncpus = get_nprocs();

above two lines should be interchanged, ncpus not assigned while getting used to get size.

tools/testing/selftests/powerpc/benchmarks/context_switch.c static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) · line 130

Quoted 10 lines
tools/testing/selftests/powerpc/benchmarks/context_switch.c +6 -2
+	cpuset = CPU_ALLOC(ncpus);
+	CPU_ZERO_S(size, cpuset);
+	CPU_SET_S(cpu, size, cpuset);
 
-	if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) {
+	if (sched_setaffinity(0, size, cpuset)) {
 		perror("sched_setaffinity");
-		exit(1);
+		CPU_FREE(cpuset);
+		exit(-1);

do we need to change the return value here? probably other framework might rely on previous value?

Regards,
-Satheesh.

Quoted 7 lines
tools/testing/selftests/powerpc/benchmarks/context_switch.c +0 -0
 	}
 
 	fn(arg);
Signature · 1 line
2.24.1
#2 lore raw
In reply to Harish
Quoted 39 lines · Harish

On systems with large number of cpus, test fails trying to set affinity for child process by calling sched_setaffinity() with smaller size for cpuset. This patch fixes it by making sure that the size of allocated cpu set is dependent on the number of CPUs as reported by get_nprocs().

Fixes 00b7ec5c9cf3 ("selftests/powerpc: Import Anton's context_switch2 benchmark")
---
 .../powerpc/benchmarks/context_switch.c        | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
tools/testing/selftests/powerpc/benchmarks/context_switch.c +4 -2
index a2e8c9da7fa5..de6c49d6f88f 100644
@@ -19,6 +19,7 @@
 #include <limits.h>
 #include <sys/time.h>
 #include <sys/syscall.h>
+#include <sys/sysinfo.h>
 #include <sys/types.h>
 #include <sys/shm.h>
 #include <linux/futex.h>
@@ -104,8 +105,9 @@ static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 
 static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 {
-	int pid;
-	cpu_set_t cpuset;
+	int pid, ncpus;
+	cpu_set_t *cpuset;
+	size_t size;
 
 	pid = fork();
 	if (pid == -1) {

tools/testing/selftests/powerpc/benchmarks/context_switch.c static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) · line 123

Quoted 8 lines
tools/testing/selftests/powerpc/benchmarks/context_switch.c +3 -2
@@ -116,12 +118,16 @@ static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
 	if (pid)
 		return;
 
-	CPU_ZERO(&cpuset);
-	CPU_SET(cpu, &cpuset);
+	size = CPU_ALLOC_SIZE(ncpus);
+	ncpus = get_nprocs();
+	cpuset = CPU_ALLOC(ncpus);

CPU_ALLOC() allocation failure needs to be checked, like malloc() allocations.

tools/testing/selftests/powerpc/benchmarks/context_switch.c static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu) · line 131

Quoted 10 lines
tools/testing/selftests/powerpc/benchmarks/context_switch.c +5 -2
+	CPU_ZERO_S(size, cpuset);
+	CPU_SET_S(cpu, size, cpuset);
 
-	if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) {
+	if (sched_setaffinity(0, size, cpuset)) {
 		perror("sched_setaffinity");
-		exit(1);
+		CPU_FREE(cpuset);
+		exit(-1);
 	}

once the cpu affinity is set, you probably want to free the cpuset mask.

Quoted 3 lines
tools/testing/selftests/powerpc/benchmarks/context_switch.c +0 -0
 	fn(arg);
 
Signature · 1 line
Kamalesh