linux-rt-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] rt-tests: cyclictest: Add option to specify main pid affinity
@ 2021-02-23 13:54 Jonathan Schwender
  2021-02-23 13:54 ` [PATCH v2 1/2] cyclictest: Move main pid setaffinity handling into a function Jonathan Schwender
  2021-02-23 13:54 ` [PATCH v2 2/2] cyclictest: Add --mainaffinity=[CPUSET] option Jonathan Schwender
  0 siblings, 2 replies; 3+ messages in thread
From: Jonathan Schwender @ 2021-02-23 13:54 UTC (permalink / raw)
  To: jkacur, williams; +Cc: linux-rt-users

Hi John,

Changes in v2:
I moved the wrapper around numa_sched_setaffinity
to lib/rt-numa.c and added the pid as a parameter.
I called it try_numa_sched_setaffinity since it's basically
just a wrapper, that warns if an error occurs.
I'm not sure if this is what you had in mind, when you
suggested moving that section to rt-numa.c
The code-style should also be consistent now.


This patch adds the option --mainaffinity to specify the affinity of
the main pid. 
This is mainly useful if you want to bind the main thread to a 
different (e.g. housekeeping ) CPU than the measurement threads.

Regards

Jonathan


Jonathan Schwender (2):
  cyclictest: Move main pid setaffinity handling into a function
  cyclictest: Add --mainaffinity=[CPUSET] option.

 src/cyclictest/cyclictest.c | 28 +++++++++++++++++++---------
 src/include/rt-numa.h       |  2 ++
 src/lib/rt-numa.c           | 11 +++++++++++
 3 files changed, 32 insertions(+), 9 deletions(-)

-- 
2.29.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] cyclictest: Move main pid setaffinity handling into a function
  2021-02-23 13:54 [PATCH v2 0/2] rt-tests: cyclictest: Add option to specify main pid affinity Jonathan Schwender
@ 2021-02-23 13:54 ` Jonathan Schwender
  2021-02-23 13:54 ` [PATCH v2 2/2] cyclictest: Add --mainaffinity=[CPUSET] option Jonathan Schwender
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Schwender @ 2021-02-23 13:54 UTC (permalink / raw)
  To: jkacur, williams; +Cc: linux-rt-users

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 c3d45f3..9d8eca8 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -1778,13 +1778,7 @@ int main(int argc, char **argv)
 
 	/* Restrict the main pid to the affinity specified by the user */
 	if (affinity_mask) {
-		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 dbeaef6..33c80a7 100644
--- a/src/lib/rt-numa.c
+++ b/src/lib/rt-numa.c
@@ -133,3 +133,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.29.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] cyclictest: Add --mainaffinity=[CPUSET] option.
  2021-02-23 13:54 [PATCH v2 0/2] rt-tests: cyclictest: Add option to specify main pid affinity Jonathan Schwender
  2021-02-23 13:54 ` [PATCH v2 1/2] cyclictest: Move main pid setaffinity handling into a function Jonathan Schwender
@ 2021-02-23 13:54 ` Jonathan Schwender
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Schwender @ 2021-02-23 13:54 UTC (permalink / raw)
  To: jkacur, williams; +Cc: linux-rt-users

This allows the user to specify a separate cpuset for the main pid,
e.g. on a housekeeping CPU.
If --mainaffinity is not specified, but --affinity is, then the
current behaviour is preserved and the main pid is bound
to the cpuset specified by --affinity

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
---
 src/cyclictest/cyclictest.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index 9d8eca8..3ce934d 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -836,6 +836,8 @@ static void display_help(int error)
 	       "	 --laptop	   Save battery when running cyclictest\n"
 	       "			   This will give you poorer realtime results\n"
 	       "			   but will not drain your battery so quickly\n"
+	       "         --mainaffinity=[CPUSET]  Run the main thread on CPU #N. This only affects\n"
+	       "                           the main thread and not the measurement threads\n"
 	       "-m       --mlockall        lock current and future memory allocations\n"
 	       "-M       --refresh_on_max  delay updating the screen until a new max\n"
 	       "			   latency is hit. Useful for low bandwidth.\n"
@@ -891,6 +893,7 @@ static int quiet;
 static int interval = DEFAULT_INTERVAL;
 static int distance = -1;
 static struct bitmask *affinity_mask = NULL;
+static struct bitmask *main_affinity_mask = NULL;
 static int smp = 0;
 
 static int clocksources[] = {
@@ -943,7 +946,7 @@ enum option_values {
 	OPT_AFFINITY=1, OPT_BREAKTRACE, OPT_CLOCK,
 	OPT_DISTANCE, OPT_DURATION, OPT_LATENCY,
 	OPT_FIFO, OPT_HISTOGRAM, OPT_HISTOFALL, OPT_HISTFILE,
-	OPT_INTERVAL, OPT_LOOPS, OPT_MLOCKALL, OPT_REFRESH,
+	OPT_INTERVAL, OPT_LOOPS, OPT_MAINAFFINITY, OPT_MLOCKALL, OPT_REFRESH,
 	OPT_NANOSLEEP, OPT_NSECS, OPT_OSCOPE, OPT_PRIORITY,
 	OPT_QUIET, OPT_PRIOSPREAD, OPT_RELATIVE, OPT_RESOLUTION,
 	OPT_SYSTEM, OPT_SMP, OPT_THREADS, OPT_TRIGGER,
@@ -980,6 +983,7 @@ static void process_options(int argc, char *argv[], int max_cpus)
 			{"interval",         required_argument, NULL, OPT_INTERVAL },
 			{"laptop",	     no_argument,	NULL, OPT_LAPTOP },
 			{"loops",            required_argument, NULL, OPT_LOOPS },
+			{"mainaffinity",     required_argument, NULL, OPT_MAINAFFINITY},
 			{"mlockall",         no_argument,       NULL, OPT_MLOCKALL },
 			{"refresh_on_max",   no_argument,       NULL, OPT_REFRESH },
 			{"nsecs",            no_argument,       NULL, OPT_NSECS },
@@ -1071,6 +1075,16 @@ static void process_options(int argc, char *argv[], int max_cpus)
 		case 'l':
 		case OPT_LOOPS:
 			max_cycles = atoi(optarg); break;
+		case OPT_MAINAFFINITY:
+			if (optarg) {
+				parse_cpumask(optarg, max_cpus, &main_affinity_mask);
+			} else if (optind < argc &&
+			           (atoi(argv[optind]) ||
+			            argv[optind][0] == '0' ||
+			            argv[optind][0] == '!')) {
+				parse_cpumask(argv[optind], max_cpus, &main_affinity_mask);
+			}
+			break;
 		case 'm':
 		case OPT_MLOCKALL:
 			lockall = 1; break;
@@ -1777,7 +1791,9 @@ int main(int argc, char **argv)
 	}
 
 	/* Restrict the main pid to the affinity specified by the user */
-	if (affinity_mask) {
+	if (main_affinity_mask) {
+		try_numa_sched_setaffinity(getpid(), main_affinity_mask);
+	} else if (affinity_mask) {
 		try_numa_sched_setaffinity(getpid(), affinity_mask);
 		if (verbose)
 			printf("Using %u cpus.\n",
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-02-23 13:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-23 13:54 [PATCH v2 0/2] rt-tests: cyclictest: Add option to specify main pid affinity Jonathan Schwender
2021-02-23 13:54 ` [PATCH v2 1/2] cyclictest: Move main pid setaffinity handling into a function Jonathan Schwender
2021-02-23 13:54 ` [PATCH v2 2/2] cyclictest: Add --mainaffinity=[CPUSET] option Jonathan Schwender

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).