diff mbox series

[v4,2/4] KVM: selftests: add is_cpu_eligible_to_run() utility function

Message ID 20220404214642.3201659-3-ricarkol@google.com (mailing list archive)
State New, archived
Headers show
Series KVM: arm64: selftests: Add edge cases tests for the arch timer | expand

Commit Message

Ricardo Koller April 4, 2022, 9:46 p.m. UTC
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@google.com>
---
 .../testing/selftests/kvm/include/test_util.h |  2 ++
 tools/testing/selftests/kvm/lib/test_util.c   | 20 ++++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

Comments

Oliver Upton April 5, 2022, 12:13 a.m. UTC | #1
Hi Ricardo,

On Mon, Apr 04, 2022 at 02:46:40PM -0700, Ricardo Koller wrote:
> 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@google.com>
> ---
>  .../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);
> +	}

I don't think you need the loop and can just do CPU_ISSET(pcpu, &cpuset),
right?

--
Thanks,
Oliver
Ricardo Koller April 5, 2022, 1:56 a.m. UTC | #2
On Tue, Apr 05, 2022 at 12:13:33AM +0000, Oliver Upton wrote:
> Hi Ricardo,
> 
> On Mon, Apr 04, 2022 at 02:46:40PM -0700, Ricardo Koller wrote:
> > 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@google.com>
> > ---
> >  .../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);
> > +	}
> 
> I don't think you need the loop and can just do CPU_ISSET(pcpu, &cpuset),
> right?

Oops, definitely not. Thanks for catching this.

> 
> --
> Thanks,
> Oliver
diff mbox series

Patch

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;
+}