Add is_cpu_eligible_to_run() utility function, which checks whether the current process, or one of its threads, is eligible to run on a particular CPU. This information is obtained using sched_getaffinity. Signed-off-by: Ricardo Koller <ricarkol@xxxxxxxxxx> --- .../testing/selftests/kvm/include/test_util.h | 2 ++ tools/testing/selftests/kvm/lib/test_util.c | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index 99e0dcdc923f..a7653f369b6c 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -143,4 +143,6 @@ static inline void *align_ptr_up(void *x, size_t size) return (void *)align_up((unsigned long)x, size); } +bool is_cpu_eligible_to_run(int pcpu); + #endif /* SELFTEST_KVM_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index 6d23878bbfe1..7813a68333c0 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -4,6 +4,7 @@ * * Copyright (C) 2020, Google LLC. */ +#define _GNU_SOURCE #include <assert.h> #include <ctype.h> @@ -13,7 +14,9 @@ #include <sys/stat.h> #include <sys/syscall.h> #include <linux/mman.h> -#include "linux/kernel.h" +#include <linux/kernel.h> +#include <sched.h> +#include <sys/sysinfo.h> #include "test_util.h" @@ -334,3 +337,18 @@ long get_run_delay(void) return val[1]; } + +bool is_cpu_eligible_to_run(int pcpu) +{ + cpu_set_t cpuset; + long i, nprocs; + + nprocs = get_nprocs_conf(); + sched_getaffinity(0, sizeof(cpu_set_t), &cpuset); + for (i = 0; i < nprocs; i++) { + if (i == pcpu) + return CPU_ISSET(i, &cpuset); + } + + return false; +} -- 2.35.1.1094.g7c7d902a7c-goog