open_posix_testsuite: add set_affinity_single()
PATCH 1/1 open_posix_testsuite: add set_affinity_single()
Couple testcases rely on set_affinity(), to limit usage of CPUs to a single one. However they hardcode CPU number to "0", which is causing problems on systems where CPU0 is not available (for example it's offline, etc.)
This patch adds set_affinity_single() (which sets affinity to first single present CPU) and replaces all calls to set_affinity(0).
--- .../interfaces/pthread_attr_setschedpolicy/2-1.c | 4 +- .../conformance/interfaces/pthread_create/1-6.c | 2 +- .../conformance/interfaces/sched_setparam/2-1.c | 2 +- .../conformance/interfaces/sched_setparam/2-2.c | 2 +- .../conformance/interfaces/sched_setparam/9-1.c | 2 +- .../conformance/interfaces/sched_yield/1-1.c | 4 +- testcases/open_posix_testsuite/include/affinity.h | 54 ++++++++++++++++++++++ 7 files changed, 62 insertions(+), 8 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c +2 -2
@@ -150,9 +150,9 @@ int main(void) status =PTS_UNRESOLVED ; - rc =set_affinity (0 ); + rc =set_affinity_single ();if (rc) -FAIL_AND_EXIT ("set_affinity" , errno); +FAIL_AND_EXIT ("set_affinity_single" , errno); sp.sched_priority =PRIO_MAIN ; rc =pthread_setschedparam (pthread_self (),SCHED_FIFO , &sp);
testcases/open_posix_testsuite/conformance/interfaces/pthread_create/1-6.c +1 -1
@@ -238,7 +238,7 @@ int main(void)int ret;unsigned int i; - ret =set_affinity (0 ); + ret =set_affinity_single ();if (ret) { n_threads =get_ncpu ();if (n_threads ==-1 ) {
testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-1.c +1 -1
@@ -93,7 +93,7 @@ int main(void)There is no guarantee that the LOOP of the child can be certainly big enough on any device at any time. */ -int rc =set_affinity (0 ); +int rc =set_affinity_single ();if (rc) { nb_child =get_ncpu ();if (nb_child ==-1 ) {
testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-2.c +1 -1
@@ -94,7 +94,7 @@ int main(void)There is no guarantee that the LOOP of the child can be certainly big enough on any device at any time. */ -int rc =set_affinity (0 ); +int rc =set_affinity_single ();if (rc) { nb_child =get_ncpu ();if (nb_child ==-1 ) {
testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c +1 -1
@@ -109,7 +109,7 @@ int main(void)int *child_pid, oldcount, newcount, shm_id, i;struct sched_param param;key_t key; -int rc =set_affinity (0 ); +int rc =set_affinity_single ();if (rc) { nb_cpu =get_ncpu ();if (nb_cpu ==-1 ) {
testcases/open_posix_testsuite/conformance/interfaces/sched_yield/1-1.c +2 -2
@@ -85,9 +85,9 @@ int main(void) }/* Must only use a single CPU */ - rc =set_affinity (0 ); + rc =set_affinity_single ();if (rc) { -ERR_LOG ("set_affinity" , rc); +ERR_LOG ("set_affinity_single" , rc);return status; }
testcases/open_posix_testsuite/include/affinity.h +54 -0
66 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 1/1 open_posix_testsuite: add set_affinity_single()
----- Original Message -----
Quoted 145 lines
From: "Jan Stancek" <[email protected]>
To: [email protected]
Sent: Thursday, 12 November, 2015 1:45:47 PM
Subject: [LTP] [PATCH]open_posix_testsuite: addset_affinity_single()Couple testcases rely on
set_affinity(), to limit usage of CPUs to a single one. However they hardcode CPU number to "0", which is causing problems on systems where CPU0 is not available (for example it's offline, etc.)This patch adds
set_affinity_single()(which sets affinity to first single present CPU) and replaces all calls toset_affinity(0).Signed-off-by Jan Stancek <[email protected]>--- .../interfaces/pthread_attr_setschedpolicy/2-1.c | 4 +- .../conformance/interfaces/pthread_create/1-6.c | 2 +- .../conformance/interfaces/sched_setparam/2-1.c | 2 +- .../conformance/interfaces/sched_setparam/2-2.c | 2 +- .../conformance/interfaces/sched_setparam/9-1.c | 2 +- .../conformance/interfaces/sched_yield/1-1.c | 4 +- testcases/open_posix_testsuite/include/affinity.h | 54 ++++++++++++++++++++++ 7 files changed, 62 insertions(+), 8 deletions(-)diff --git a/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c index
426988b44eb6..3c4cec77277f100644
---
a/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c
+++
b/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.ctestcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c +2
-2@@ -150,9 +150,9 @@ int main(void) status =PTS_UNRESOLVED ; - rc =set_affinity (0 ); + rc =set_affinity_single ();if (rc) -FAIL_AND_EXIT ("set_affinity" , errno); +FAIL_AND_EXIT ("set_affinity_single" , errno); sp.sched_priority =PRIO_MAIN ; rc =pthread_setschedparam (pthread_self (),SCHED_FIFO , &sp);diff --git a/testcases/open_posix_testsuite/conformance/interfaces/pthread_create/1-6.c b/testcases/open_posix_testsuite/conformance/interfaces/pthread_create/1-6.c index
11654dfc00bf..03c266a4adc9100644
---
a/testcases/open_posix_testsuite/conformance/interfaces/pthread_create/1-6.c
+++
b/testcases/open_posix_testsuite/conformance/interfaces/pthread_create/1-6.ctestcases/open_posix_testsuite/conformance/interfaces/pthread_create/1-6.c +1
-1@@ -238,7 +238,7 @@ int main(void)int ret;unsigned int i; - ret =set_affinity (0 ); + ret =set_affinity_single ();if (ret) { n_threads =get_ncpu ();if (n_threads ==-1 ) {diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-1.c index
10de0afe5e90..86cf3a726893100644
---
a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-1.c
+++
b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-1.ctestcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-1.c +1
-1@@ -93,7 +93,7 @@ int main(void)There is no guarantee that the LOOP of the child can be certainly big enough on any device at any time. */ -int rc =set_affinity (0 ); +int rc =set_affinity_single ();if (rc) { nb_child =get_ncpu ();if (nb_child ==-1 ) {diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-2.c b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-2.c index
4b14691d9200..746f9a107217100644
---
a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-2.c
+++
b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-2.ctestcases/open_posix_testsuite/conformance/interfaces/sched_setparam/2-1.c +1
-1@@ -94,7 +94,7 @@ int main(void)There is no guarantee that the LOOP of the child can be certainly big enough on any device at any time. */ -int rc =set_affinity (0 ); +int rc =set_affinity_single ();if (rc) { nb_child =get_ncpu ();if (nb_child ==-1 ) {diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c index
c3e4f1c113c3..32f3e5be4501100644
---
a/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c
+++
b/testcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.ctestcases/open_posix_testsuite/conformance/interfaces/sched_setparam/9-1.c +1
-1@@ -109,7 +109,7 @@ int main(void)int *child_pid, oldcount, newcount, shm_id, i;struct sched_param param;key_t key; -int rc =set_affinity (0 ); +int rc =set_affinity_single ();if (rc) { nb_cpu =get_ncpu ();if (nb_cpu ==-1 ) {diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sched_yield/1-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sched_yield/1-1.c index
621b9deaaf3d..602733e608ae100644testcases/open_posix_testsuite/conformance/interfaces/sched_yield/1-1.c +2
-2@@ -85,9 +85,9 @@ int main(void) }/* Must only use a single CPU */ - rc =set_affinity (0 ); + rc =set_affinity_single ();if (rc) { -ERR_LOG ("set_affinity" , rc); +ERR_LOG ("set_affinity_single" , rc);return status; }testcases/open_posix_testsuite/include/affinity.h +1
-0@@ -27,6 +27,7 @@# define _GNU_SOURCE# define AFFINITY_NEEDS_GNU_SOURCE 1# include <sched.h> +# include <stdio.h> static int set_affinity (int cpu) {
testcases/open_posix_testsuite/include/affinity.h static int set_affinity(int cpu) · line 47
Quoted 10 lines
testcases/open_posix_testsuite/include/affinity.h +7
-0@@ -37,6 +38,59 @@ static int set_affinity(int cpu)return (sched_setaffinity (0 ,sizeof (cpu_set_t), &mask)); } + +static int get_present_cpu_from_sysfs (void ) +{ +FILE *f; +int cpu =-1 ; + + f =fopen ("/sys/devices/system/cpu/present" ,"r" );
This should have been "online", not present. I'll fix that in v2.
Quoted 56 lines
testcases/open_posix_testsuite/include/affinity.h +46
-1+if (!f) +return -1 ; +fscanf (f,"%d" , &cpu); +fclose (f); + +return cpu; +} + +static int get_present_cpu_from_cpuinfo (void ) +{ +FILE *f; +int cpu =-1 ; +char line[4096 ]; + + f =fopen ("/proc/cpuinfo" ,"r" ); +if (!f) +return -1 ; + +while (!feof (f)) { +if (!fgets (line,sizeof (line), f)) +return -1 ; +/* +* cpuinfo output is not consistent across all archictures, +* it can be "processor : N", but for example on s390 +* it's: "processor N: ...", so ignore any non-number +* after "processor" +*/ +if (sscanf (line,"processor%*[^0123456789]%d" , &cpu) ==1 ) +break ; + } +fclose (f); + +return cpu; +} + +static int set_affinity_single (void ) +{ +int cpu; + + cpu =get_present_cpu_from_sysfs (); +if (cpu <0 ) + cpu =get_present_cpu_from_cpuinfo (); + +return set_affinity (cpu); +} +#else static int set_affinity (int cpu) { --1.8.3.1
--
Mailing list info: http://lists.linux.it/listinfo/ltp
PATCH 1/1 open_posix_testsuite: add set_affinity_single()
In reply to Jan StancekHi!
Quoted 38 lines
testcases/open_posix_testsuite/include/affinity.h +38
-0+static int get_present_cpu_from_sysfs (void ) +{ +FILE *f; +int cpu =-1 ; + + f =fopen ("/sys/devices/system/cpu/present" ,"r" ); +if (!f) +return -1 ; +fscanf (f,"%d" , &cpu); +fclose (f); + +return cpu; +} + +static int get_present_cpu_from_cpuinfo (void ) +{ +FILE *f; +int cpu =-1 ; +char line[4096 ]; + + f =fopen ("/proc/cpuinfo" ,"r" ); +if (!f) +return -1 ; + +while (!feof (f)) { +if (!fgets (line,sizeof (line), f)) +return -1 ; +/* +* cpuinfo output is not consistent across all archictures, +* it can be "processor : N", but for example on s390 +* it's: "processor N: ...", so ignore any non-number +* after "processor" +*/ +if (sscanf (line,"processor%*[^0123456789]%d" , &cpu) ==1 ) +break ; + } +fclose (f); +
testcases/open_posix_testsuite/include/affinity.h static int set_affinity(int cpu) · line 89
Quoted 10 lines
testcases/open_posix_testsuite/include/affinity.h +10
-0+return cpu; +} + +static int set_affinity_single (void ) +{ +int cpu; + + cpu =get_present_cpu_from_sysfs (); +if (cpu <0 ) + cpu =get_present_cpu_from_cpuinfo ();
We should probably fallback here to cpu = 0 in case that we have failed to parse the cpuinfo. Not that it's likely to happen.
So I would do here:
cpu = get_present_cpu_from_sysfs(); if (cpu >= 0) goto set_affinity; cpu = get_present_cpu_from_cpuinfo(); if (cpu >= 0) goto set_affinity; fprintf(stderr, "WARNING: Failed to detect present cpu using cpu=0\n"); cpu = 0;
set_affinity:
return set_affinity(cpu);
testcases/open_posix_testsuite/include/affinity.h static int set_affinity(int cpu) · line 95
Quoted 5 lines
testcases/open_posix_testsuite/include/affinity.h +3
-0+ return set_affinity(cpu); +} +#else static int set_affinity (int cpu)
^
You should also rename this stub for non-linux OSes.
Otherwise it looks good, acked.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 open_posix_testsuite: add set_affinity_single()
----- Original Message -----
Quoted 87 lines
From: "Cyril Hrubis" <[email protected]>
To: "Jan Stancek" <[email protected]>
Cc: [email protected]
Sent: Thursday, 12 November, 2015 2:09:52 PM
Subject: Re: [LTP] [PATCH]open_posix_testsuite: addset_affinity_single()Hi!
Quoted 38 lines
testcases/open_posix_testsuite/include/affinity.h +38
-0+static int get_present_cpu_from_sysfs (void ) +{ +FILE *f; +int cpu =-1 ; + + f =fopen ("/sys/devices/system/cpu/present" ,"r" ); +if (!f) +return -1 ; +fscanf (f,"%d" , &cpu); +fclose (f); + +return cpu; +} + +static int get_present_cpu_from_cpuinfo (void ) +{ +FILE *f; +int cpu =-1 ; +char line[4096 ]; + + f =fopen ("/proc/cpuinfo" ,"r" ); +if (!f) +return -1 ; + +while (!feof (f)) { +if (!fgets (line,sizeof (line), f)) +return -1 ; +/* +* cpuinfo output is not consistent across all archictures, +* it can be "processor : N", but for example on s390 +* it's: "processor N: ...", so ignore any non-number +* after "processor" +*/ +if (sscanf (line,"processor%*[^0123456789]%d" , &cpu) ==1 ) +break ; + } +fclose (f); +testcases/open_posix_testsuite/include/affinity.h static int set_affinity(int cpu) · line 89
Quoted 10 lines
testcases/open_posix_testsuite/include/affinity.h +10
-0+return cpu; +} + +static int set_affinity_single (void ) +{ +int cpu; + + cpu =get_present_cpu_from_sysfs (); +if (cpu <0 ) + cpu =get_present_cpu_from_cpuinfo ();We should probably fallback here to cpu = 0 in case that we have failed to parse the cpuinfo. Not that it's likely to happen.
So I would do here:
cpu = get_present_cpu_from_sysfs(); if (cpu >= 0) goto set_affinity; cpu = get_present_cpu_from_cpuinfo(); if (cpu >= 0) goto set_affinity; fprintf(stderr, "WARNING: Failed to detect present cpu using cpu=0\n"); cpu = 0;
set_affinity:return set_affinity(cpu);testcases/open_posix_testsuite/include/affinity.h static int set_affinity(int cpu) · line 95
Quoted 5 lines
testcases/open_posix_testsuite/include/affinity.h +3
-0+ return set_affinity(cpu); +} +#else static int set_affinity (int cpu)^
You should also rename this stub for non-linux OSes.
Quoted 1 line
Otherwise it looks good, acked.
Pushed with changes you suggested.
Regards,
Jan
Quoted 5 lines
--
Cyril Hrubis
[email protected]