Skip to contentSkip to conversation

Limiting Xenomai tasks to one certain core in a dual core system

Xenomai-help M. Koehrer 3 participants 6 messages Nov 22, 2006 – Nov 25, 2006

lore
Share & tools
lore

M. Koehrer

author
#0 lore raw

Hi all,

I want to run Xenomai and rtnet (using RTDM) on a dual core CPU.
Using the kernel parameter isolcpus I can force the standard Linux tasks to run on one of the cores.
The other core should be used exclusively by all real time tasks (including rtnet). I can - of course - pass a parameter on each rt_task_create() call. However how can I force rtnet (using RTDM) to run on this CPU core?
Is there a possibility to mask the CPUs to be used globally?
This could also avoid to pass the CPU number with each rt_task_create() call.

Thanks for any feedback on this.

Regards

Mathias

Signature · 9 lines
Mathias Koehrer
[email protected]


Viel oder wenig? Schnell oder langsam? Unbegrenzt surfen + telefonieren
ohne Zeit- und Volumenbegrenzung? DAS TOP ANGEBOT JETZT bei Arcor: günstig
und schnell mit DSL - das All-Inclusive-Paket für clevere Doppel-Sparer,
nur  44,85 €  inkl. DSL- und ISDN-Grundgebühr!
http://www.arcor.de/rd/emf-dsl-2
#1 lore raw
Quoted 8 lines · M. Koehrer

Hi all,

I want to run Xenomai and rtnet (using RTDM) on a dual core CPU.
Using the kernel parameter isolcpus I can force the standard Linux tasks to run on one of the cores.
The other core should be used exclusively by all real time tasks (including rtnet). I can - of course - pass a parameter on each rt_task_create() call. However how can I force rtnet (using RTDM) to run on this CPU core?

You hit a weak point, see the Task Market in the wiki: "CPU affinity for RTDM threads." We both need an API for RTDM and its application on RTnet. Not complex work, just work...

Quoted 2 lines

Is there a possibility to mask the CPUs to be used globally?
This could also avoid to pass the CPU number with each rt_task_create() call.

Well, that would actually be an additional way. Yeah, maybe some global CPU mask that controls the affinity on future thread creation. We could export it via /proc so that you can set it right before starting a specific group of applications and drivers.

On the long term, a control interface to the Xenomai scheduler would be good so that things like policy, priority, time-slice, or CPU affinity could be manipulated with some command line tool or via /proc after thread creation.

Further feedback welcome!

Jan

#2 lore raw
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · M. Koehrer

Is there a possibility to mask the CPUs to be used globally?
This could also avoid to pass the CPU number with each rt_task_create() call.

Well, that would actually be an additional way. Yeah, maybe some global CPU mask that controls the affinity on future thread creation. We could export it via /proc so that you can set it right before starting a specific group of applications and drivers.

Some food for thoughts:

Here is an experimental implementation of such a global mask. Seems to work, at least I was able to prevent any task creation on a UP box by setting the mask to 0.

Jan

---
 include/nucleus/pod.h |    2 +
 ksrc/nucleus/module.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ksrc/nucleus/pod.c    |    3 ++
 3 files changed, 62 insertions(+)
include/nucleus/pod.h +2 -0
Index: xenomai/include/nucleus/pod.h
===================================================================
@@ -253,6 +253,8 @@ extern u_long nktickdef;
 
 extern char *nkmsgbuf;
 
+extern xnarch_cpumask_t nkaffinity;
+
 #define xnprintf(fmt,args...)  xnarch_printf(fmt , ##args)
 #define xnloginfo(fmt,args...) xnarch_loginfo(fmt , ##args)
 #define xnlogwarn(fmt,args...) xnarch_logwarn(fmt , ##args)
ksrc/nucleus/module.c +57 -0

75 lines not on this page · raw message

ksrc/nucleus/pod.c +3 -0
Index: xenomai/ksrc/nucleus/pod.c
===================================================================
@@ -70,6 +70,8 @@ MODULE_PARM_DESC(tick_arg, "Fixed clock 
 
 char *nkmsgbuf = NULL;
 
+xnarch_cpumask_t nkaffinity = XNPOD_ALL_CPUS;
+
 const char *xnpod_fatal_helper(const char *format, ...)
 {
 	const unsigned nr_cpus = xnarch_num_online_cpus();
@@ -915,6 +917,7 @@ int xnpod_start_thread(xnthread_t *threa
 	xnlock_get_irqsave(&nklock, s);
 
 	thread->affinity = xnarch_cpu_online_map;
+	xnarch_cpus_and(thread->affinity, nkaffinity, thread->affinity);
 	xnarch_cpus_and(thread->affinity, affinity, thread->affinity);
 
 	if (xnarch_cpus_empty(thread->affinity)) {
#3 lore raw
Quoted 9 lines · Jan Kiszka
Quoted 7 lines · Jan Kiszka
Quoted 2 lines · M. Koehrer

Is there a possibility to mask the CPUs to be used globally?
This could also avoid to pass the CPU number with each rt_task_create() call.

Well, that would actually be an additional way. Yeah, maybe some global CPU mask that controls the affinity on future thread creation. We could export it via /proc so that you can set it right before starting a specific group of applications and drivers.

Quoted 5 lines

Some food for thoughts:

Here is an experimental implementation of such a global mask. Seems to work, at least I was able to prevent any task creation on a UP box by setting the mask to 0.

Even more brain exercises:

This is version 2 of my patch, consequently applying the global mask idea also on IRQ affinity. I think this simple mechanism can solve quite a few basic SMP setup issue by forcing all user threads, driver threads, and IRQs that are created/attached during a certain period to a specific CPU set.

That's the theory. In practice, things look not that clear ATM. I noticed some oddity on a 4-way qemu box running the latency test and hacked the attached enhancement for /proc/xenomai/sched. This is what I get on that box:

Quoted 10 lines

[email protected] :/root# cat /proc/xenomai/sched

CPU  AFFINITY PID    PRI      PERIOD   TIMEOUT    STAT       NAME
  0  00000001 0       -1      0        0          R          ROOT/0
  1  00000002 0       -1      0        0          R          ROOT/1
  2  00000004 0       -1      0        0          R          ROOT/2
  3  00000008 0       -1      0        0          R          ROOT/3
  0  0000000c 917      0      0        0          W          display-916
  2  0000000c 918     99      5000000  1          D          sampling-916

[email protected] :/root# cat /proc/xenomai/affinity 0x0000000c

Philippe, anyone else: This scheduling of display-916 is not like as it should be, is it?

Jan

PS: /proc output of IRQ affinities would be nice as well, but we are lacking the require abstraction by ipipe ATM, something to read out the currently set affinity.

---
 include/nucleus/pod.h |    2 +
 ksrc/nucleus/intr.c   |    3 ++
 ksrc/nucleus/module.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ksrc/nucleus/pod.c    |    3 ++
 4 files changed, 65 insertions(+)
include/nucleus/pod.h +2 -0
Index: xenomai/include/nucleus/pod.h
===================================================================
@@ -253,6 +253,8 @@ extern u_long nktickdef;
 
 extern char *nkmsgbuf;
 
+extern xnarch_cpumask_t nkaffinity;
+
 #define xnprintf(fmt,args...)  xnarch_printf(fmt , ##args)
 #define xnloginfo(fmt,args...) xnarch_loginfo(fmt , ##args)
 #define xnlogwarn(fmt,args...) xnarch_logwarn(fmt , ##args)
ksrc/nucleus/intr.c +3 -0
Index: xenomai/ksrc/nucleus/intr.c
===================================================================
@@ -626,6 +626,9 @@ int xnintr_attach(xnintr_t *intr, void *
 
 	xnlock_get_irqsave(&intrlock, s);
 
+#ifdef CONFIG_SMP
+	xnarch_set_irq_affinity(intr->irq, nkaffinity);
+#endif /* CONFIG_SMP */
 	err = xnintr_irq_attach(intr);
 
 	xnlock_put_irqrestore(&intrlock, s);
ksrc/nucleus/module.c +57 -0

75 lines not on this page · raw message

ksrc/nucleus/pod.c +3 -0
Index: xenomai/ksrc/nucleus/pod.c
===================================================================
@@ -70,6 +70,8 @@ MODULE_PARM_DESC(tick_arg, "Fixed clock 
 
 char *nkmsgbuf = NULL;
 
+xnarch_cpumask_t nkaffinity = XNPOD_ALL_CPUS;
+
 const char *xnpod_fatal_helper(const char *format, ...)
 {
 	const unsigned nr_cpus = xnarch_num_online_cpus();
@@ -915,6 +917,7 @@ int xnpod_start_thread(xnthread_t *threa
 	xnlock_get_irqsave(&nklock, s);
 
 	thread->affinity = xnarch_cpu_online_map;
+	xnarch_cpus_and(thread->affinity, nkaffinity, thread->affinity);
 	xnarch_cpus_and(thread->affinity, affinity, thread->affinity);
 
 	if (xnarch_cpus_empty(thread->affinity)) {
---
 ksrc/nucleus/module.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
ksrc/nucleus/module.c +7 -3
Index: xenomai/ksrc/nucleus/module.c
===================================================================
@@ -103,6 +103,7 @@ struct sched_seq_iterator {
 		xnticks_t period;
 		xnticks_t timeout;
 		xnflags_t status;
+		xnarch_cpumask_t affinity;
 	} sched_info[1];
 };
 
@@ -145,8 +146,9 @@ static int sched_seq_show(struct seq_fil
 	char sbuf[64], pbuf[16];
 
 	if (v == SEQ_START_TOKEN)
-		seq_printf(seq, "%-3s  %-6s %-8s %-8s %-10s %-10s %s\n",
-			   "CPU", "PID", "PRI", "PERIOD", "TIMEOUT", "STAT", "NAME");
+		seq_printf(seq, "%-3s  %-8s %-6s %-8s %-8s %-10s %-10s %s\n",
+			   "CPU", "AFFINITY", "PID", "PRI", "PERIOD", "TIMEOUT",
+			   "STAT", "NAME");
 	else {
 		struct sched_seq_info *p = (struct sched_seq_info *)v;
 
@@ -156,8 +158,9 @@ static int sched_seq_show(struct seq_fil
 		else
 			snprintf(pbuf, sizeof(pbuf), "%3d", p->cprio);
 
-		seq_printf(seq, "%3u  %-6d %-8s %-8Lu %-10Lu %-10s %s\n",
+		seq_printf(seq, "%3u  %08x %-6d %-8s %-8Lu %-10Lu %-10s %s\n",
 			   p->cpu,
+			   p->affinity,
 			   p->pid,
 			   pbuf,
 			   p->period,
@@ -237,6 +240,7 @@ static int sched_seq_open(struct inode *
 		iter->sched_info[n].period = xnthread_get_period(thread);
 		iter->sched_info[n].timeout = xnthread_get_timeout(thread, iter->start_time);
 		iter->sched_info[n].status = thread->status;
+		iter->sched_info[n].affinity = thread->affinity;
 
 		holder = nextq(&nkpod->threadq, holder);
 
#4 lore raw
Quoted 6 lines · Jan Kiszka

This is version 2 of my patch, consequently applying the global mask idea also on IRQ affinity. I think this simple mechanism can solve quite a few basic SMP setup issue by forcing all user threads, driver threads, and IRQs that are created/attached during a certain period to a specific CPU set.

Quoted 16 lines

That's the theory. In practice, things look not that clear ATM. I noticed some oddity on a 4-way qemu box running the latency test and hacked the attached enhancement for /proc/xenomai/sched. This is what I get on that box:

Quoted 10 lines

[email protected] :/root# cat /proc/xenomai/sched

CPU  AFFINITY PID    PRI      PERIOD   TIMEOUT    STAT       NAME
  0  00000001 0       -1      0        0          R          ROOT/0
  1  00000002 0       -1      0        0          R          ROOT/1
  2  00000004 0       -1      0        0          R          ROOT/2
  3  00000008 0       -1      0        0          R          ROOT/3
  0  0000000c 917      0      0        0          W          display-916
  2  0000000c 918     99      5000000  1          D          sampling-916

[email protected] :/root# cat /proc/xenomai/affinity 0x0000000c

[I should really stop believing heavy issues can be solved with light-weight patches...]

OK, after reading a bit more in what I was hacking on, I'm starting to understand the CPU selection mechanisms of shadow threads. Here comes version 3 of the patch. Now it actually forces all threads to the desired CPUs.

The reason why I failed with previous approaches could even be a SMP bug of the original code. The Linux affinity of a shadow thread was only set if no u_completion has been passed to xnshadow_map - which affected native threads e.g. So I reordered the code, and it works.

I also changed the policy of /proc/xenoami/affinity. It will now only override the mapping if the user didn't provide an explicit mask. I think this provides more flexibility.

Comment?

Jan

---
 include/nucleus/pod.h |    2 +
 ksrc/nucleus/intr.c   |    3 ++
 ksrc/nucleus/module.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ksrc/nucleus/pod.c    |    4 ++-
 ksrc/nucleus/shadow.c |   15 +++++--------
 5 files changed, 71 insertions(+), 10 deletions(-)
include/nucleus/pod.h +2 -0
Index: xenomai/include/nucleus/pod.h
===================================================================
@@ -253,6 +253,8 @@ extern u_long nktickdef;
 
 extern char *nkmsgbuf;
 
+extern xnarch_cpumask_t nkaffinity;
+
 #define xnprintf(fmt,args...)  xnarch_printf(fmt , ##args)
 #define xnloginfo(fmt,args...) xnarch_loginfo(fmt , ##args)
 #define xnlogwarn(fmt,args...) xnarch_logwarn(fmt , ##args)
ksrc/nucleus/intr.c +3 -0
Index: xenomai/ksrc/nucleus/intr.c
===================================================================
@@ -626,6 +626,9 @@ int xnintr_attach(xnintr_t *intr, void *
 
 	xnlock_get_irqsave(&intrlock, s);
 
+#ifdef CONFIG_SMP
+	xnarch_set_irq_affinity(intr->irq, nkaffinity);
+#endif /* CONFIG_SMP */
 	err = xnintr_irq_attach(intr);
 
 	xnlock_put_irqrestore(&intrlock, s);
ksrc/nucleus/module.c +57 -0

75 lines not on this page · raw message

ksrc/nucleus/pod.c +3 -1
Index: xenomai/ksrc/nucleus/pod.c
===================================================================
@@ -70,6 +70,8 @@ MODULE_PARM_DESC(tick_arg, "Fixed clock 
 
 char *nkmsgbuf = NULL;
 
+xnarch_cpumask_t nkaffinity = XNPOD_ALL_CPUS;
+
 const char *xnpod_fatal_helper(const char *format, ...)
 {
 	const unsigned nr_cpus = xnarch_num_online_cpus();
@@ -910,7 +912,7 @@ int xnpod_start_thread(xnthread_t *threa
 		return -EBUSY;
 
 	if (xnarch_cpus_empty(affinity))
-		affinity = XNARCH_CPU_MASK_ALL;
+		affinity = nkaffinity;
 
 	xnlock_get_irqsave(&nklock, s);
 
ksrc/nucleus/shadow.c +6 -9
Index: xenomai/ksrc/nucleus/shadow.c
===================================================================
@@ -853,20 +853,17 @@ int xnshadow_map(xnthread_t *thread, xnc
 	xnshadow_thrptd(current) = thread;
 	xnpod_suspend_thread(thread, XNRELAX, XN_INFINITE, NULL);
 
+	affinity = thread->affinity;
+	if (xnarch_cpus_empty(affinity))
+		affinity = nkaffinity;
+	set_cpus_allowed(current, affinity);
+
 	if (u_completion) {
 		xnshadow_signal_completion(u_completion, 0);
 		return 0;
 	}
 
-	/* Nobody waits for us, so we may start the shadow immediately
-	   after having forced the CPU affinity to the current
-	   processor. Note that we don't use smp_processor_id() to prevent
-	   kernel debug stuff to yell at us for calling it in a preemptible
-	   section of code. */
-
-	affinity = xnarch_cpumask_of_cpu(rthal_processor_id());
-	set_cpus_allowed(current, affinity);
-
+	/* Nobody waits for us, so we may start the shadow immediately. */
 	mode = thread->rrperiod != XN_INFINITE ? XNRRB : 0;
 	xnpod_start_thread(thread, mode, 0, affinity, NULL, NULL);
 
#5 lore raw

On Sat, 2006-11-25 at 16:12 +0100, Jan Kiszka wrote:

[...]

Quoted 10 lines

OK, after reading a bit more in what I was hacking on, I'm starting to understand the CPU selection mechanisms of shadow threads. Here comes version 3 of the patch. Now it actually forces all threads to the desired CPUs.

The reason why I failed with previous approaches could even be a SMP bug of the original code. The Linux affinity of a shadow thread was only set if no u_completion has been passed to xnshadow_map - which affected native threads e.g. So I reordered the code, and it works.

The root issue is why LO_START_REQ which normally sets the affinity at the end of the create -> start protocol did not get a chance to run. Likely that the newly mapped thread was still in a runnable state when tested from xnshadow_start(), which might be due to a kernel preemption artefact.

Quoted 5 lines

I also changed the policy of /proc/xenoami/affinity. It will now only override the mapping if the user didn't provide an explicit mask. I think this provides more flexibility.

Comment?

[...]

ksrc/nucleus/pod.c int xnpod_start_thread(xnthread_t *threa · line 915

Quoted 7 lines
ksrc/nucleus/pod.c +1 -1
 	const unsigned nr_cpus = xnarch_num_online_cpus();
@@ -910,7 +912,7 @@ int xnpod_start_thread(xnthread_t *threa
 		return -EBUSY;
 
 	if (xnarch_cpus_empty(affinity))
-		affinity = XNARCH_CPU_MASK_ALL;
+		affinity = nkaffinity;

This is fairly significant API change that should be loudly documented, especially because its behaviour now depends on a new variable.

Quoted 8 lines
ksrc/nucleus/pod.c +0 -0
 	xnlock_get_irqsave(&nklock, s);
 

Index: xenomai/ksrc/nucleus/shadow.c
===================================================================

ksrc/nucleus/shadow.c int xnshadow_map(xnthread_t *thread, xnc · line 858

Quoted 6 lines
ksrc/nucleus/shadow.c +3 -0
@@ -853,20 +853,17 @@ int xnshadow_map(xnthread_t *thread, xnc
 	xnshadow_thrptd(current) = thread;
 	xnpod_suspend_thread(thread, XNRELAX, XN_INFINITE, NULL);
 
+	affinity = thread->affinity;
+	if (xnarch_cpus_empty(affinity))
+		affinity = nkaffinity;

This won't work as expected. The thread->affinity info should not be used in the xnshadow_map() context, because it has not been set yet (xnshadow_map() should be seen as the equivalent to xnpod_init_thread() for shadow threads). This information is set by the nucleus by a call to xnpod_start_thread(), so one step later in the task spawn protocol. Luckily, it seems to be zeroed in your test, but it has not been initialized yet.

ksrc/nucleus/shadow.c int xnshadow_map(xnthread_t *thread, xnc · line 860

Quoted 2 lines
ksrc/nucleus/shadow.c +2 -0
+	set_cpus_allowed(current, affinity);
+

This should be ok to pin the newly mapped task to a CPU early on, even in the auto-start configuration, but that case, we should kill the now redundant set_cpus_allowed() from LO_START_REQ. Actually, we would be better of killing LO_START_REQ entirely, since it would not make sense anymore, compared to a regular WAKEUP request.

Signature · 1 line
Philippe.