selftests/powerpc: Allow bind_to_cpu() to automatically pick CPU
authorBenjamin Gray <bgray@linux.ibm.com>
Thu, 6 Apr 2023 04:33:16 +0000 (14:33 +1000)
committerMichael Ellerman <mpe@ellerman.id.au>
Thu, 20 Apr 2023 03:21:45 +0000 (13:21 +1000)
All current users of bind_to_cpu() don't care _which_ CPU they get, just
that they are bound to a single free one. So alter the interface to

1. Accept a BIND_CPU_ANY value that tells it to automatically
   pick a CPU
2. Return the picked CPU

And convert all these users to bind_to_cpu(BIND_CPU_ANY).

Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20230406043320.125138-4-bgray@linux.ibm.com
tools/testing/selftests/powerpc/include/utils.h
tools/testing/selftests/powerpc/pmu/ebb/cpu_event_pinned_vs_ebb_test.c
tools/testing/selftests/powerpc/pmu/ebb/cpu_event_vs_ebb_test.c
tools/testing/selftests/powerpc/pmu/ebb/ebb_vs_cpu_event_test.c
tools/testing/selftests/powerpc/pmu/ebb/multi_ebb_procs_test.c
tools/testing/selftests/powerpc/pmu/lib.c
tools/testing/selftests/powerpc/utils.c

index d3589e1..44bfd48 100644 (file)
@@ -31,6 +31,8 @@ int read_auxv(char *buf, ssize_t buf_size);
 void *find_auxv_entry(int type, char *auxv);
 void *get_auxv_entry(int type);
 
+#define BIND_CPU_ANY   (-1)
+
 int pick_online_cpu(void);
 int bind_to_cpu(int cpu);
 
index 3cd33eb..fab7f34 100644 (file)
@@ -45,9 +45,8 @@ int cpu_event_pinned_vs_ebb(void)
 
        SKIP_IF(!ebb_is_supported());
 
-       cpu = pick_online_cpu();
+       cpu = bind_to_cpu(BIND_CPU_ANY);
        FAIL_IF(cpu < 0);
-       FAIL_IF(bind_to_cpu(cpu));
 
        FAIL_IF(pipe(read_pipe.fds) == -1);
        FAIL_IF(pipe(write_pipe.fds) == -1);
index 8466ef9..7c54c26 100644 (file)
@@ -43,9 +43,8 @@ int cpu_event_vs_ebb(void)
 
        SKIP_IF(!ebb_is_supported());
 
-       cpu = pick_online_cpu();
+       cpu = bind_to_cpu(BIND_CPU_ANY);
        FAIL_IF(cpu < 0);
-       FAIL_IF(bind_to_cpu(cpu));
 
        FAIL_IF(pipe(read_pipe.fds) == -1);
        FAIL_IF(pipe(write_pipe.fds) == -1);
index 4d822cb..d7064b5 100644 (file)
@@ -43,9 +43,8 @@ int ebb_vs_cpu_event(void)
 
        SKIP_IF(!ebb_is_supported());
 
-       cpu = pick_online_cpu();
+       cpu = bind_to_cpu(BIND_CPU_ANY);
        FAIL_IF(cpu < 0);
-       FAIL_IF(bind_to_cpu(cpu));
 
        FAIL_IF(pipe(read_pipe.fds) == -1);
        FAIL_IF(pipe(write_pipe.fds) == -1);
index 9b0f70d..4ac22b2 100644 (file)
@@ -75,13 +75,11 @@ static int cycles_child(void)
 int multi_ebb_procs(void)
 {
        pid_t pids[NR_CHILDREN];
-       int cpu, rc, i;
+       int rc, i;
 
        SKIP_IF(!ebb_is_supported());
 
-       cpu = pick_online_cpu();
-       FAIL_IF(cpu < 0);
-       FAIL_IF(bind_to_cpu(cpu));
+       FAIL_IF(bind_to_cpu(BIND_CPU_ANY) < 0);
 
        for (i = 0; i < NR_CHILDREN; i++) {
                pids[i] = fork();
index 144f90a..3213579 100644 (file)
@@ -103,12 +103,10 @@ static int eat_cpu_child(union pipe read_pipe, union pipe write_pipe)
 pid_t eat_cpu(int (test_function)(void))
 {
        union pipe read_pipe, write_pipe;
-       int cpu, rc;
+       int rc;
        pid_t pid;
 
-       cpu = pick_online_cpu();
-       FAIL_IF(cpu < 0);
-       FAIL_IF(bind_to_cpu(cpu));
+       FAIL_IF(bind_to_cpu(BIND_CPU_ANY) < 0);
 
        if (pipe(read_pipe.fds) == -1)
                return -1;
index cdb996d..252fb4a 100644 (file)
@@ -455,13 +455,24 @@ done:
 int bind_to_cpu(int cpu)
 {
        cpu_set_t mask;
+       int err;
+
+       if (cpu == BIND_CPU_ANY) {
+               cpu = pick_online_cpu();
+               if (cpu < 0)
+                       return cpu;
+       }
 
        printf("Binding to cpu %d\n", cpu);
 
        CPU_ZERO(&mask);
        CPU_SET(cpu, &mask);
 
-       return sched_setaffinity(0, sizeof(mask), &mask);
+       err = sched_setaffinity(0, sizeof(mask), &mask);
+       if (err)
+               return err;
+
+       return cpu;
 }
 
 bool is_ppc64le(void)