From da44a94383dab6fcf2001e6d447a503259476760 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Mon, 15 Aug 2022 11:11:54 -0400 Subject: [PATCH] fix: ifdef linux specific cpu count compat Expand the '#ifdef __linux__' block in src/compat-cpu.h to all static inline functions related to sysfs since they are only useful on Linux and fail to build on some non-Linux platforms. This issue was reported on QNX. The corresponding unit tests have to be skipped on non-Linux platforms. Thanks to Elad Lahav for reporting this issue. Change-Id: I17c88a9a2fb5b9be6cf5325234a18ff40788cd09 Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- src/compat-smp.h | 2 +- tests/unit/get_cpu_mask_from_sysfs.c | 10 ++++ tests/unit/get_max_cpuid_from_sysfs.c | 10 ++++ tests/unit/test_get_cpu_mask_from_sysfs | 14 +++-- tests/unit/test_get_cpu_mask_from_sysfs_cxx | 14 +++-- tests/unit/test_get_max_cpuid_from_mask.c | 11 ++++ tests/unit/test_get_max_cpuid_from_sysfs | 60 ++++++++++--------- tests/unit/test_get_max_cpuid_from_sysfs_cxx | 60 ++++++++++--------- tests/unit/test_get_possible_cpus_array_len.c | 4 ++ 9 files changed, 118 insertions(+), 67 deletions(-) diff --git a/src/compat-smp.h b/src/compat-smp.h index 7240d2f..31fa979 100644 --- a/src/compat-smp.h +++ b/src/compat-smp.h @@ -36,6 +36,7 @@ static inline int get_num_possible_cpus_sysconf(void) } #endif +#ifdef __linux__ /* * Get the highest CPU id from sysfs. * @@ -235,7 +236,6 @@ error: return -1; } -#ifdef __linux__ /* * On Linux try sysfs first and fallback to sysconf. */ diff --git a/tests/unit/get_cpu_mask_from_sysfs.c b/tests/unit/get_cpu_mask_from_sysfs.c index 91f2ebf..7938f23 100644 --- a/tests/unit/get_cpu_mask_from_sysfs.c +++ b/tests/unit/get_cpu_mask_from_sysfs.c @@ -7,6 +7,8 @@ #include #include +#ifdef __linux__ + #include "compat-smp.h" int main(int argc, char *argv[]) @@ -28,3 +30,11 @@ int main(int argc, char *argv[]) else return EXIT_FAILURE; } + +#else + +int main(void) +{ + return EXIT_SUCCESS; +} +#endif diff --git a/tests/unit/get_max_cpuid_from_sysfs.c b/tests/unit/get_max_cpuid_from_sysfs.c index a948a22..37c4b3d 100644 --- a/tests/unit/get_max_cpuid_from_sysfs.c +++ b/tests/unit/get_max_cpuid_from_sysfs.c @@ -7,6 +7,8 @@ #include #include +#ifdef __linux__ + #include "compat-smp.h" int main(int argc, char *argv[]) @@ -27,3 +29,11 @@ int main(int argc, char *argv[]) else return EXIT_FAILURE; } + +#else + +int main(void) +{ + return EXIT_SUCCESS; +} +#endif diff --git a/tests/unit/test_get_cpu_mask_from_sysfs b/tests/unit/test_get_cpu_mask_from_sysfs index fff561f..2a6acc1 100755 --- a/tests/unit/test_get_cpu_mask_from_sysfs +++ b/tests/unit/test_get_cpu_mask_from_sysfs @@ -43,9 +43,13 @@ test_test_get_cpu_mask_from_sysfs() { ok $? "test_get_cpu_mask_from_sysfs - with '\n' expected: '$cpumask', result: '$result'" } -plan_tests $NUM_TESTS +if [ "$URCU_TESTS_OS_TYPE" == "linux" ]; then + plan_tests $NUM_TESTS -test_test_get_cpu_mask_from_sysfs "" -test_test_get_cpu_mask_from_sysfs "0" -test_test_get_cpu_mask_from_sysfs "0-3" -test_test_get_cpu_mask_from_sysfs "0,3-7,9" + test_test_get_cpu_mask_from_sysfs "" + test_test_get_cpu_mask_from_sysfs "0" + test_test_get_cpu_mask_from_sysfs "0-3" + test_test_get_cpu_mask_from_sysfs "0,3-7,9" +else + plan_skip_all "Linux specific tests." +fi diff --git a/tests/unit/test_get_cpu_mask_from_sysfs_cxx b/tests/unit/test_get_cpu_mask_from_sysfs_cxx index 6b80e53..ffddbf9 100755 --- a/tests/unit/test_get_cpu_mask_from_sysfs_cxx +++ b/tests/unit/test_get_cpu_mask_from_sysfs_cxx @@ -43,9 +43,13 @@ test_test_get_cpu_mask_from_sysfs_cxx() { ok $? "test_get_cpu_mask_from_sysfs_cxx - with '\n' expected: '$cpumask', result: '$result'" } -plan_tests $NUM_TESTS +if [ "$URCU_TESTS_OS_TYPE" == "linux" ]; then + plan_tests $NUM_TESTS -test_test_get_cpu_mask_from_sysfs_cxx "" -test_test_get_cpu_mask_from_sysfs_cxx "0" -test_test_get_cpu_mask_from_sysfs_cxx "0-3" -test_test_get_cpu_mask_from_sysfs_cxx "0,3-7,9" + test_test_get_cpu_mask_from_sysfs_cxx "" + test_test_get_cpu_mask_from_sysfs_cxx "0" + test_test_get_cpu_mask_from_sysfs_cxx "0-3" + test_test_get_cpu_mask_from_sysfs_cxx "0,3-7,9" +else + plan_skip_all "Linux specific tests." +fi diff --git a/tests/unit/test_get_max_cpuid_from_mask.c b/tests/unit/test_get_max_cpuid_from_mask.c index 435bb5c..25a2417 100644 --- a/tests/unit/test_get_max_cpuid_from_mask.c +++ b/tests/unit/test_get_max_cpuid_from_mask.c @@ -10,6 +10,7 @@ #include "compat-smp.h" +#ifdef __linux__ struct parse_test_data { const char *buf; int expected; @@ -75,3 +76,13 @@ int main(void) return exit_status(); } + +#else + +int main(void) +{ + plan_skip_all("Linux specific tests."); + + return exit_status(); +} +#endif diff --git a/tests/unit/test_get_max_cpuid_from_sysfs b/tests/unit/test_get_max_cpuid_from_sysfs index 230a51c..cd646a1 100755 --- a/tests/unit/test_get_max_cpuid_from_sysfs +++ b/tests/unit/test_get_max_cpuid_from_sysfs @@ -44,46 +44,50 @@ test_get_max_cpuid_from_sysfs() { rm -rf "$TESTDIR" } -plan_tests $NUM_TESTS +if [ "$URCU_TESTS_OS_TYPE" == "linux" ]; then + plan_tests $NUM_TESTS -diag "get_max_cpuid_from_sysfs" + diag "get_max_cpuid_from_sysfs" -test_data=(0 "cpu0") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(0 "cpu0") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(1 "cpu0" "cpu1") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(1 "cpu0" "cpu1") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(1 "cpu1" "cpu0") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(1 "cpu1" "cpu0") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(3 "cpu3") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(3 "cpu3") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(99 "cpu99") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(99 "cpu99") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(3 "cpu0" "cpu3") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(3 "cpu0" "cpu3") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(3 "cpufreq" "cpuidle" "cpu0" "cpu1" "cpu2" "cpu3") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(3 "cpufreq" "cpuidle" "cpu0" "cpu1" "cpu2" "cpu3") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(0 "cpu" "cpu0") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(0 "cpu" "cpu0") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(5 "cpu" "cpu5") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(5 "cpu" "cpu5") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(-1 "toto") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(-1 "toto") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(-1 "cpu") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(-1 "cpu") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(-1 "cpua" "cpud") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(-1 "cpua" "cpud") + test_get_max_cpuid_from_sysfs "${test_data[@]}" -test_data=(-1 "cpufreq" "cpuidle") -test_get_max_cpuid_from_sysfs "${test_data[@]}" + test_data=(-1 "cpufreq" "cpuidle") + test_get_max_cpuid_from_sysfs "${test_data[@]}" +else + plan_skip_all "Linux specific tests." +fi diff --git a/tests/unit/test_get_max_cpuid_from_sysfs_cxx b/tests/unit/test_get_max_cpuid_from_sysfs_cxx index 886c065..db99094 100755 --- a/tests/unit/test_get_max_cpuid_from_sysfs_cxx +++ b/tests/unit/test_get_max_cpuid_from_sysfs_cxx @@ -44,46 +44,50 @@ test_get_max_cpuid_from_sysfs_cxx() { rm -rf "$TESTDIR" } -plan_tests $NUM_TESTS +if [ "$URCU_TESTS_OS_TYPE" == "linux" ]; then + plan_tests $NUM_TESTS -diag "get_max_cpuid_from_sysfs_cxx" + diag "get_max_cpuid_from_sysfs_cxx" -test_data=(0 "cpu0") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(0 "cpu0") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(1 "cpu0" "cpu1") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(1 "cpu0" "cpu1") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(1 "cpu1" "cpu0") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(1 "cpu1" "cpu0") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(3 "cpu3") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(3 "cpu3") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(99 "cpu99") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(99 "cpu99") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(3 "cpu0" "cpu3") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(3 "cpu0" "cpu3") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(3 "cpufreq" "cpuidle" "cpu0" "cpu1" "cpu2" "cpu3") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(3 "cpufreq" "cpuidle" "cpu0" "cpu1" "cpu2" "cpu3") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(0 "cpu" "cpu0") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(0 "cpu" "cpu0") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(5 "cpu" "cpu5") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(5 "cpu" "cpu5") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(-1 "toto") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(-1 "toto") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(-1 "cpu") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(-1 "cpu") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(-1 "cpua" "cpud") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(-1 "cpua" "cpud") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" -test_data=(-1 "cpufreq" "cpuidle") -test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + test_data=(-1 "cpufreq" "cpuidle") + test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" +else + plan_skip_all "Linux specific tests." +fi diff --git a/tests/unit/test_get_possible_cpus_array_len.c b/tests/unit/test_get_possible_cpus_array_len.c index ab64d9d..c5ab2b4 100644 --- a/tests/unit/test_get_possible_cpus_array_len.c +++ b/tests/unit/test_get_possible_cpus_array_len.c @@ -19,8 +19,12 @@ int main(void) ret = get_possible_cpus_array_len(); ok(ret > 0, "get_possible_cpus_array_len (%d > 0)", ret); +#ifdef __linux__ ret = get_num_possible_cpus_fallback(); ok(ret > 0, "get_num_possible_cpus_fallback (%d > 0)", ret); +#else + skip(1, "Linux specific test."); +#endif return exit_status(); } -- 2.34.1