diff mbox series

[v3,1/2] cyclictest: Move main pid setaffinity handling into a function

Message ID 7204424803922d15bb11bba65bb5672cd76aa20c.1616702706.git.schwenderjonathan@gmail.com
State New
Headers show
Series rt-tests: cyclictest: Add option to specify main pid affinity | expand

Commit Message

Jonathan Schwender March 25, 2021, 8:18 p.m. UTC
Move error handling for setting the affinity of the main pid
into a separate function.
This prevents duplicating the code in the next commit,
where the main thread pid can be restricted to one of
two bitmasks depending on the passed parameters.

After feedback from John Kacur, the function is now
located in src/lib/rt-numa.c.
This allows other tests to reuse this, if they also
prefer to warn if numa_sched_setaffinity fails.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
---
 src/cyclictest/cyclictest.c |  8 +-------
 src/include/rt-numa.h       |  2 ++
 src/lib/rt-numa.c           | 11 +++++++++++
 3 files changed, 14 insertions(+), 7 deletions(-)

Comments

John Kacur May 7, 2021, 5:07 p.m. UTC | #1
On Thu, 25 Mar 2021, Jonathan Schwender wrote:

> Move error handling for setting the affinity of the main pid

> into a separate function.

> This prevents duplicating the code in the next commit,

> where the main thread pid can be restricted to one of

> two bitmasks depending on the passed parameters.

> 

> After feedback from John Kacur, the function is now

> located in src/lib/rt-numa.c.

> This allows other tests to reuse this, if they also

> prefer to warn if numa_sched_setaffinity fails.

> 

> Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>

> ---

>  src/cyclictest/cyclictest.c |  8 +-------

>  src/include/rt-numa.h       |  2 ++

>  src/lib/rt-numa.c           | 11 +++++++++++

>  3 files changed, 14 insertions(+), 7 deletions(-)

> 

> diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c

> index dca9610..460f6ae 100644

> --- a/src/cyclictest/cyclictest.c

> +++ b/src/cyclictest/cyclictest.c

> @@ -1791,13 +1791,7 @@ int main(int argc, char **argv)

>  

>  	/* Restrict the main pid to the affinity specified by the user */

>  	if (affinity_mask != NULL) {

> -		int res;

> -

> -		errno = 0;

> -		res = numa_sched_setaffinity(getpid(), affinity_mask);

> -		if (res != 0)

> -			warn("Couldn't setaffinity in main thread: %s\n", strerror(errno));

> -

> +		try_numa_sched_setaffinity(getpid(), affinity_mask);

>  		if (verbose)

>  			printf("Using %u cpus.\n",

>  				numa_bitmask_weight(affinity_mask));

> diff --git a/src/include/rt-numa.h b/src/include/rt-numa.h

> index ca86a45..e9fa312 100644

> --- a/src/include/rt-numa.h

> +++ b/src/include/rt-numa.h

> @@ -18,4 +18,6 @@ int cpu_for_thread_ua(int thread_num, int max_cpus);

>  

>  int parse_cpumask(char *str, int max_cpus, struct bitmask **cpumask);

>  

> +void try_numa_sched_setaffinity(__pid_t pid, struct bitmask *cpumask);

> +

>  #endif

> diff --git a/src/lib/rt-numa.c b/src/lib/rt-numa.c

> index babcc63..9a81cfb 100644

> --- a/src/lib/rt-numa.c

> +++ b/src/lib/rt-numa.c

> @@ -138,3 +138,14 @@ int parse_cpumask(char *str, int max_cpus, struct bitmask **cpumask)

>  

>  	return 0;

>  }

> +

> +void try_numa_sched_setaffinity(pid_t pid, struct bitmask *cpumask)

> +{

> +	int res;

> +

> +	errno = 0;

> +	res = numa_sched_setaffinity(pid, cpumask);

> +	if (res != 0)

> +		warn("Couldn't setaffinity for thread %d: %s\n", pid,

> +		     strerror(errno));

> +}

> -- 

> 2.30.2

> 

> 


Yuck. My bad, this is some pretty inelegant error code wrapped around a 
numa call, I don't think it really warrents trying to make an API out of 
it. That said, I might need to do this anyway, when I put back code that 
can run when numa is not available on a target machine.

For now, can you just leave this inline and respin?

Thanks

John Kacur
diff mbox series

Patch

diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index dca9610..460f6ae 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -1791,13 +1791,7 @@  int main(int argc, char **argv)
 
 	/* Restrict the main pid to the affinity specified by the user */
 	if (affinity_mask != NULL) {
-		int res;
-
-		errno = 0;
-		res = numa_sched_setaffinity(getpid(), affinity_mask);
-		if (res != 0)
-			warn("Couldn't setaffinity in main thread: %s\n", strerror(errno));
-
+		try_numa_sched_setaffinity(getpid(), affinity_mask);
 		if (verbose)
 			printf("Using %u cpus.\n",
 				numa_bitmask_weight(affinity_mask));
diff --git a/src/include/rt-numa.h b/src/include/rt-numa.h
index ca86a45..e9fa312 100644
--- a/src/include/rt-numa.h
+++ b/src/include/rt-numa.h
@@ -18,4 +18,6 @@  int cpu_for_thread_ua(int thread_num, int max_cpus);
 
 int parse_cpumask(char *str, int max_cpus, struct bitmask **cpumask);
 
+void try_numa_sched_setaffinity(__pid_t pid, struct bitmask *cpumask);
+
 #endif
diff --git a/src/lib/rt-numa.c b/src/lib/rt-numa.c
index babcc63..9a81cfb 100644
--- a/src/lib/rt-numa.c
+++ b/src/lib/rt-numa.c
@@ -138,3 +138,14 @@  int parse_cpumask(char *str, int max_cpus, struct bitmask **cpumask)
 
 	return 0;
 }
+
+void try_numa_sched_setaffinity(pid_t pid, struct bitmask *cpumask)
+{
+	int res;
+
+	errno = 0;
+	res = numa_sched_setaffinity(pid, cpumask);
+	if (res != 0)
+		warn("Couldn't setaffinity for thread %d: %s\n", pid,
+		     strerror(errno));
+}