Skip to contentSkip to conversation

Add env vars for session and cpu-affinity

PATCH In review Simon Graber 2 participants 10 messages Aug 29, 2024 – Sep 2, 2024

lore
Share & tools
lore
#0 lore raw

cover Add env vars for session and cpu-affinity

Hello,

Revision 3 of this patch adds documentation for the new environment variables.

Changes since v1:
* Usage of environment variables is documented
* lib/copperplate/init.c: Conditionally query XENO_SESSION_NAME
* Reformat patch 2 to adhere to code style

Simon Graber (4):
lib/copperplate: Get session name from env var
lib/boilerplate: Get cpu-affinity from env var
alchemytests: Set cpu-affinity by env var
Add documentation for env vars

 doc/asciidoc/README.INSTALL.adoc             |  7 ++--
 lib/boilerplate/setup.c                      | 39 +++++++++++++++-----
 lib/copperplate/init.c                       | 21 ++++++++---
 testsuite/smokey/alchemytests/alchemytests.c | 10 ++++-
 4 files changed, 58 insertions(+), 19 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/4 lib/copperplate: Get session name from env var

At present, for two Xenomai processes to share resources, they must be started with identical --session= parameters.
This is cumbersome for extensive projects involving numerous helper processes. This commit allows the specification of the session name via a global environment variable, namely XENO_SESSION_NAME. The --session= parameter overrules the environment variable.

---
 lib/copperplate/init.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
lib/copperplate/init.c +15 -6
index 16fc59f7a..01969cfea 100644
@@ -114,6 +114,7 @@ static int get_session_root(int *regflags_r)
 {
 	char *sessdir, *session;
 	struct passwd *pw;
+	char *sn_env;
 	int ret;
 
 	pw = getpwuid(geteuid());
@@ -121,12 +122,20 @@ static int get_session_root(int *regflags_r)
 		return -errno;
 
 	if (__copperplate_setup_data.session_label == NULL) {
-		ret = asprintf(&session, "anon@%d", __node_id);
-		if (ret < 0)
-			return -ENOMEM;
-		__copperplate_setup_data.session_label = session;
-		*regflags_r |= REGISTRY_ANON;
-	} else if (strchr(__copperplate_setup_data.session_label, '/')) {
+		sn_env = getenv("XENO_SESSION_NAME");
+		if (sn_env) {
+			__copperplate_setup_data.session_label = strdup(sn_env);
+			if (__copperplate_setup_data.session_label == NULL)
+				return -ENOMEM;
+		} else {
+			ret = asprintf(&session, "anon@%d", __node_id);
+			if (ret < 0)
+				return -ENOMEM;
+			__copperplate_setup_data.session_label = session;
+			*regflags_r |= REGISTRY_ANON;
+		}
+	}
+	if (strchr(__copperplate_setup_data.session_label, '/')) {
 		warning("session name may not contain slashes");
 		return -EINVAL;
 	}
Signature · 1 line
2.46.0
#2 lore raw

PATCH 2/4 lib/boilerplate: Get cpu-affinity from env var

In reply to Simon Graber

It might be helpful to specify the cpu-affinity via an env var, XENO_CPU_AFFINITY.
This commit adds a helper function, called in __xenomai_init. The --cpu-affinity= parameter overrules the environment variable.

---
 lib/boilerplate/setup.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
lib/boilerplate/setup.c +17 -0
index 26c8c4cf5..9573a546f 100644
@@ -383,6 +383,18 @@ void xenomai_usage(void)
         fprintf(stderr, "--help				display help\n");
 }
 
+static int set_affinity_from_env(void)
+{
+	char *affinity_env;
+	int ret = 0;
+
+	affinity_env = getenv("XENO_CPU_AFFINITY");
+	if (affinity_env)
+		ret = collect_cpu_affinity(affinity_env);
+
+	return ret;
+}
+
 static int parse_base_options(int *argcp, char **uargv,
 			      const struct option *options,
 			      int base_opt_start)
@@ -560,6 +572,11 @@ static void __xenomai_init(int *argcp, char *const **argvp, const char *me)
 	/* Retrieve the default CPU affinity. */
 	retrieve_default_cpu_affinity();
 
+	/* Environment variables can be overruled by arguments */
+	ret = set_affinity_from_env();
+	if (ret)
+		goto fail;
+
 	/*
 	 * Parse the base options first, to bootstrap the core with
 	 * the right config values.
Signature · 1 line
2.46.0
#3 lore raw

PATCH 3/4 alchemytests: Set cpu-affinity by env var

In reply to Simon Graber

The alchemy test runner can now make use of
the XENO_CPU_AFFINITY environment variable by setting it before dispatching to the inidvidual test programs.

In a later patch, the --vm argument will be passed via an env var too.

---
 testsuite/smokey/alchemytests/alchemytests.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
testsuite/smokey/alchemytests/alchemytests.c +9 -1
index cf9f970f1..edbafe6e7 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <string.h>
+#include <stdlib.h>
 #include <smokey/smokey.h>
 
 static const char * const tests[] = {
@@ -31,12 +32,19 @@ static const char * const tests[] = {
 
 static int run_alchemytests(struct smokey_test *t, int argc, char *const argv[])
 {
-	const char *args = "--vm --cpu-affinity=0";
 	const char *const mod = "xeno_rtipc";
+	const char *args = "--vm";
 	int test_ret = 0;
 	int ret = 0;
 	int tmp;
 
+	ret = setenv("XENO_CPU_AFFINITY", "0", 0);
+	if (ret != 0) {
+		fprintf(stderr, "%s failed to set XENO_CPU_AFFINITY: %m\n",
+			__func__);
+		return ret;
+	}
+
 	/* Try loading the xeno_rtipc module as the pipe test depends on it */
 	tmp = smokey_modprobe(mod, true);
 	if (tmp)
Signature · 1 line
2.46.0
#4 lore raw

Re: [PATCH 3/4] alchemytests: Set cpu-affinity by env var

Quoted 3 lines · Simon Graber

The alchemy test runner can now make use of
the XENO_CPU_AFFINITY environment variable by setting it before dispatching to the inidvidual test programs.

individual

Quoted 3 lines

In a later patch, the --vm argument will be passed via an env var too.

Not really happening yet, is it?

In any case, the advantage of the alternative passing method should be rather mentioned here. Just looking at the code, one may think it only complicates the task.

Quoted 27 lines
---
 testsuite/smokey/alchemytests/alchemytests.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
testsuite/smokey/alchemytests/alchemytests.c +2 -1
index cf9f970f1..edbafe6e7 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <string.h>
+#include <stdlib.h>
 #include <smokey/smokey.h>
 
 static const char * const tests[] = {
@@ -31,12 +32,19 @@ static const char * const tests[] = {
 
 static int run_alchemytests(struct smokey_test *t, int argc, char *const argv[])
 {
-	const char *args = "--vm --cpu-affinity=0";
 	const char *const mod = "xeno_rtipc";
+	const char *args = "--vm";
 	int test_ret = 0;
 	int ret = 0;
 	int tmp;
 

testsuite/smokey/alchemytests/alchemytests.c static const char * const tests[] = { · line 50

Quoted 10 lines
testsuite/smokey/alchemytests/alchemytests.c +7 -0
+	ret = setenv("XENO_CPU_AFFINITY", "0", 0);
+	if (ret != 0) {
+		fprintf(stderr, "%s failed to set XENO_CPU_AFFINITY: %m\n",
+			__func__);
+		return ret;
+	}
+
 	/* Try loading the xeno_rtipc module as the pipe test depends on it */
 	tmp = smokey_modprobe(mod, true);
 	if (tmp)

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
author
#5 lore raw

Re: [PATCH 3/4] alchemytests: Set cpu-affinity by env var

Quoted 3 lines · Jan Kiszka

In any case, the advantage of the alternative passing method should be rather mentioned here. Just looking at the code, one may think it only complicates the task.

The tasks launched by run_alchemytests strcmp() against argv[1].

Checking for an environment variable would be more readable and less error-prone (ordering of args, -- or single -).

Once the --vm arg has a corresponding env var, the tasks can rely entirely on env vars.

#6 lore raw

Re: [PATCH 3/4] alchemytests: Set cpu-affinity by env var

Quoted 11 lines · Simon
Quoted 3 lines · Jan Kiszka

In any case, the advantage of the alternative passing method should be rather mentioned here. Just looking at the code, one may think it only complicates the task.

The tasks launched by run_alchemytests strcmp() against argv[1].

Checking for an environment variable would be more readable and less error-prone (ordering of args, -- or single -).

Once the --vm arg has a corresponding env var, the tasks can rely entirely on env vars.

So, this particular change is more to demonstrate and implicitly test the new feature, not really a value of its own yet. Maybe something like this?

"The alchemy test runner can now make use of the XENO_CPU_AFFINITY environment variable by setting it before dispatching to the individual test programs.

While not changing anything user-visibly, this stresses the new interface in CI and allows in a later commit to pass the --vm argument via an env var too, removing command line injection from this test case."

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
#7 lore raw

PATCH 4/4 Add documentation for env vars

In reply to Simon Graber

Usage message in lib/boilerplate/setup.c now prints XENO_CPU_AFFINITY and XENO_SESSION_NAME.

The usage of the session arg/env var are also
described in README.INSTALL.adoc.

---
 doc/asciidoc/README.INSTALL.adoc |  7 ++++---
 lib/boilerplate/setup.c          | 22 +++++++++++++---------
 2 files changed, 17 insertions(+), 12 deletions(-)
doc/asciidoc/README.INSTALL.adoc +4 -3
index d2b7e6f78..52bef566d 100644
@@ -428,9 +428,10 @@ Generic configuration options (both cores)
 When this option is enabled, the system creates a file hierachy at
 `<user>/<session>/<pid>` under the registry root path, where you
 can access the internal state of the active real-time objects. The
-session label is obtained from the --session runtime switch. If no
-session name is specified, `anon@<pid>` will be used. E.g. looking at
-the properties of a VxWorks task could be done as follows:
+session label is obtained from the `--session` runtime switch, which
+takes precedence over the environment variable `XENO_SESSION_NAME`.
+If neither of these are specified, `anon@<pid>` will be used. E.g.
+looking at the properties of a VxWorks task could be done as follows:
 
 If not specified in the configuration switch, the registry root path
 will be +/var/run/xenomai+.
lib/boilerplate/setup.c +13 -9
index 9573a546f..7bb24f6f4 100644
@@ -370,17 +370,21 @@ void xenomai_usage(void)
 		}
 	}
 
-        fprintf(stderr, "--cpu-affinity=<cpu[,cpu]...>	set CPU affinity of threads\n");
-        fprintf(stderr, "--[no-]sanity			disable/enable sanity checks\n");
-        fprintf(stderr, "--verbose[=level] 		set verbosity to desired level [=1]\n");
-        fprintf(stderr, "--silent, --quiet 		same as --verbose=0\n");
-        fprintf(stderr, "--trace[=level] 		set tracing to desired level [=1]\n");
-        fprintf(stderr, "--version			get version information\n");
-        fprintf(stderr, "--dump-config			dump configuration settings\n");
+	fprintf(stderr, "--session=<label>[/<group>]	enable shared session\n");
+	fprintf(stderr, "--cpu-affinity=<cpu[,cpu]...>	set CPU affinity of threads\n");
+	fprintf(stderr, "--[no-]sanity			disable/enable sanity checks\n");
+	fprintf(stderr, "--verbose[=level]		set verbosity to desired level [=1]\n");
+	fprintf(stderr, "--silent, --quiet		same as --verbose=0\n");
+	fprintf(stderr, "--trace[=level]		set tracing to desired level [=1]\n");
+	fprintf(stderr, "--version			get version information\n");
+	fprintf(stderr, "--dump-config			dump configuration settings\n");
 #ifdef CONFIG_XENO_MERCURY
-        fprintf(stderr, "--no-mlock			do not lock memory at init\n");
+	fprintf(stderr, "--no-mlock			do not lock memory at init\n");
 #endif
-        fprintf(stderr, "--help				display help\n");
+	fprintf(stderr, "--help				display help\n");
+	fprintf(stderr, "\nEnvironment variables (arguments take priority):\n"
+			"XENO_SESSION_NAME=<label>[/<group>]	enable shared session\n"
+			"XENO_CPU_AFFINITY=<cpu[,cpu]...>	set CPU affinity of threads\n");
 }
 
 static int set_affinity_from_env(void)
Signature · 1 line
2.46.0
#8 lore raw
In reply to Simon Graber
Quoted 5 lines · Simon Graber

Hello,

Revision 3 of this patch adds documentation for the new environment variables.

Please also put the version into the subject:

[PATCH v3 n/m]

Makes it easier to tell the threads apart.

Jan

Quoted 17 lines

Changes since v1:
* Usage of environment variables is documented
* lib/copperplate/init.c: Conditionally query XENO_SESSION_NAME
* Reformat patch 2 to adhere to code style

Simon Graber (4):
lib/copperplate: Get session name from env var
lib/boilerplate: Get cpu-affinity from env var
alchemytests: Set cpu-affinity by env var
Add documentation for env vars

 doc/asciidoc/README.INSTALL.adoc             |  7 ++--
 lib/boilerplate/setup.c                      | 39 +++++++++++++++-----
 lib/copperplate/init.c                       | 21 ++++++++---
 testsuite/smokey/alchemytests/alchemytests.c | 10 ++++-
 4 files changed, 58 insertions(+), 19 deletions(-)
Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
#9 lore raw
In reply to Simon Graber
Quoted 16 lines · Simon Graber

Hello,

Revision 3 of this patch adds documentation for the new environment variables.

Changes since v1:
* Usage of environment variables is documented
* lib/copperplate/init.c: Conditionally query XENO_SESSION_NAME
* Reformat patch 2 to adhere to code style

Simon Graber (4):
lib/copperplate: Get session name from env var
lib/boilerplate: Get cpu-affinity from env var
alchemytests: Set cpu-affinity by env var
Add documentation for env vars

Quoted 6 lines
 doc/asciidoc/README.INSTALL.adoc             |  7 ++--
 lib/boilerplate/setup.c                      | 39 +++++++++++++++-----
 lib/copperplate/init.c                       | 21 ++++++++---
 testsuite/smokey/alchemytests/alchemytests.c | 10 ++++-
 4 files changed, 58 insertions(+), 19 deletions(-)

Looks good in general. Maybe we can sort out my comment on patch 3 without version 4.

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center