Skip to contentSkip to conversation

Preemptive scheduling

Xenomai-help Jack Whorn 4 participants 12 messages Mar 28, 2007 – Apr 11, 2007

lore
Share & tools
lore

Jack Whorn

author
#0 lore raw

Hi,

Im new to Xenomai and Im experiencing some issues.

Why does a Xenomai thread (native skin) with an infinite loop hang the system, though there are higher-priority RT-threads present, and rt_task_set_mode periodically clears T_LOCK and T_SHIELD and sets T_RRB?

Isnt the scheduler preemptive, first considering the threads priority and subsequently the threads position in its prioritys ready queue?

I have attached some sample code.

Thanks for any hint,

Jack

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <sys/types.h>

#include <native/task.h>
#include <native/queue.h>
#include <native/intr.h>
#include <native/timer.h>

#define STACK_SIZE 8192
#define PRIO 10

static RT_TASK timer_task_desc;

// ..s.ms.µs.ns
static RTIME task_period_ns = 0100000000llu;

static unsigned long overruns = 0;

static void clean_exit(int dummy) {

  rt_task_delete(&timer_task_desc);

}

static void thread(void *cookie) {

int ret;
int i;
int c;

  ret = rt_task_set_mode(T_LOCK, T_RRB, &c);
  if (ret)
    return;

  ret = rt_task_set_periodic(NULL, TM_NOW, rt_timer_ns2ticks(task_period_ns));
  if (ret)
    return;

  while (1) {
    ret = rt_task_set_mode(T_LOCK | T_SHIELD, T_RRB | T_PRIMARY, NULL);
    if (ret)
      return;

    ret = rt_task_wait_period(&overruns);
    if (ret == -ETIMEDOUT) {
    } else if (ret) {
      clean_exit(0);
    }

    while (1);	/* infinite loop */

}
kill(0, SIGTERM);
}

int main(int argc, char** argv) {

int err;

  signal(SIGTERM, clean_exit);
  signal(SIGINT, clean_exit);

  mlockall(MCL_CURRENT | MCL_FUTURE);

  err = rt_task_spawn(&timer_task_desc, "thread", STACK_SIZE, PRIO, 0, &thread, NULL);
  if (err)
    return 0;

  pause();

return EXIT_SUCCESS;

}

Signature · 2 lines
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
#1 lore raw
Quoted 66 lines · Jack Whorn

Hi,

Im new to Xenomai and Im experiencing some issues.

Why does a Xenomai thread (native skin) with an infinite loop hang the system, though there are higher-priority RT-threads present, and rt_task_set_mode periodically clears T_LOCK and T_SHIELD and sets T_RRB?

Isnt the scheduler preemptive, first considering the threads priority and subsequently the threads position in its prioritys ready queue?

I have attached some sample code.

Thanks for any hint,

Jack

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <sys/types.h>

#include <native/task.h>
#include <native/queue.h>
#include <native/intr.h>
#include <native/timer.h>

#define STACK_SIZE 8192
#define PRIO 10

static RT_TASK timer_task_desc;

// ..s.ms.µs.ns
static RTIME task_period_ns = 0100000000llu;

static unsigned long overruns = 0;

static void clean_exit(int dummy) {

  rt_task_delete(&timer_task_desc);

}

static void thread(void *cookie) {

int ret;
int i;
int c;

  ret = rt_task_set_mode(T_LOCK, T_RRB, &c);
  if (ret)
    return;

  ret = rt_task_set_periodic(NULL, TM_NOW, rt_timer_ns2ticks(task_period_ns));
  if (ret)
    return;

  while (1) {
Quoted 11 lines
    ret = rt_task_set_mode(T_LOCK | T_SHIELD, T_RRB | T_PRIMARY, NULL);
    if (ret)
      return;

    ret = rt_task_wait_period(&overruns);
    if (ret == -ETIMEDOUT) {
    } else if (ret) {
      clean_exit(0);
    }

    while (1);	/* infinite loop */

Once your program enters this infinte loop, it never gets out, so :
- your assumption that "t_task_set_mode periodically clears T_LOCK and T_SHIELD and sets T_RRB" is false;
- even if higher priority task runs, Linux, which is Xenomai idle task never runs, so the system locks up.

In short, maybe you have observed some scheduling problems, but your example does not demonstrate them. Such misuse of Xenomai should be caught by the watchdog, if you enable it.

Signature · 1 line
					    Gilles Chanteperdrix.
author
#2 lore raw

Hi Gilles,

Quoted 3 lines

Once your program enters this infinte loop, it never gets out, so :
- your assumption that "t_task_set_mode periodically clears T_LOCK and T_SHIELD and sets T_RRB" is false;

Yeah, I just realized that it does this only once. According to http://snail.fsffrance.org/www.xenomai.org/documentation/branches/v2.0.x/html/api/group__task.html#ga44But I think that the arguments used in the rt_task_set_mode() -call are correct. Isn't that sufficient for the Linux OS to catch e.g. keyboard interrupts?

Quoted 2 lines

- even if higher priority task runs, Linux, which is Xenomai idle task never runs, so the system locks up.

Gotcha, that sounds sensible to me.

Actually, the higher-priority task uses some linux-system calls to store data to hard disk. This does no longer take place as soon as the infinite loop is entered. I expected priority coupling to solve that. Where is my error in reasoning?

Quoted 3 lines

In short, maybe you have observed some scheduling problems, but your example does not demonstrate them. Such misuse of Xenomai should be caught by the watchdog, if you enable it.

I might try that watchdog, thanks for this hint!

Regards,

Jack

Signature · 2 lines
"Feel free" - 5 GB Mailbox, 50 FreeSMS/Monat ...
Jetzt GMX ProMail testen: http://www.gmx.net/de/go/promail
#3 lore raw
Quoted 11 lines · Jack Whorn

Hi Gilles,

Quoted 3 lines

Once your program enters this infinte loop, it never gets out, so :
- your assumption that "t_task_set_mode periodically clears T_LOCK and T_SHIELD and sets T_RRB" is false;

Yeah, I just realized that it does this only once. According to http://snail.fsffrance.org/www.xenomai.org/documentation/branches/v2.0.x/html/api/group__task.html#ga44But I think that the arguments used in the rt_task_set_mode() -call are correct. Isn't that sufficient for the Linux OS to catch e.g. keyboard interrupts?

Quoted 7 lines
Quoted 2 lines

- even if higher priority task runs, Linux, which is Xenomai idle task never runs, so the system locks up.

Gotcha, that sounds sensible to me.

Actually, the higher-priority task uses some linux-system calls to store data to hard disk. This does no longer take place as soon as the infinite loop is entered. I expected priority coupling to solve that. Where is my error in reasoning?

Maybe storing data to the disk requires a kernel thread to flush them. If this thread never has a chance to run, then your data are written in the cache but never flushed to the disk. I may be wrong, but I am almost sure that if you let Linux run, everything will go as expected.

If I am wrong, please, send us a self-contained example program which demonstrates the behaviour you observed.

Signature · 1 line
                                                 Gilles Chanteperdrix
author
#4 lore raw

Hi Gilles, Hi folks,

The data is stored by a thread that periodically issues a single call to
rt_task_set_mode(0, T_PRIMARY, NULL) followed by several file operation calls like fprintf() and finally a call to fflush(NULL) and sync().

The application is not a kernel module, so - as far as I see it - it is a user mode thread, and it actually has the highest priority in my system.

Does this help understanding the phenomenon?

Regards,

Jack

Quoted 37 lines
Quoted 9 lines · Jack Whorn

Hi Gilles,

Quoted 3 lines

Once your program enters this infinte loop, it never gets out, so :
- your assumption that "t_task_set_mode periodically clears T_LOCK and T_SHIELD and sets T_RRB" is false;

Yeah, I just realized that it does this only once. According to

http://snail.fsffrance.org/www.xenomai.org/documentation/branches/v2.0.x/html/api/group__task.html#ga44But
I think that the arguments used in the rt_task_set_mode() -call are correct. Isn't that sufficient for the Linux OS to catch e.g. keyboard interrupts?

Quoted 9 lines
Quoted 4 lines

- even if higher priority task runs, Linux, which is Xenomai idle task never runs, so the system locks up.

Gotcha, that sounds sensible to me.

Actually, the higher-priority task uses some linux-system calls to store

data to hard disk. This does no longer take place as soon as the infinite loop is entered. I expected priority coupling to solve that. Where is my error in reasoning?

Maybe storing data to the disk requires a kernel thread to flush them. If this thread never has a chance to run, then your data are written in the cache but never flushed to the disk. I may be wrong, but I am almost sure that if you let Linux run, everything will go as expected.

If I am wrong, please, send us a self-contained example program which demonstrates the behaviour you observed.

Signature · 1 line
                                                 Gilles Chanteperdrix
Signature · 2 lines
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
#5 lore raw
Quoted 9 lines · Jack Whorn

Hi Gilles, Hi folks,

The data is stored by a thread that periodically issues a single call to
rt_task_set_mode(0, T_PRIMARY, NULL) followed by several file operation calls like fprintf() and finally a call to fflush(NULL) and sync().

The application is not a kernel module, so - as far as I see it - it is a user mode thread, and it actually has the highest priority in my system.

Does this help understanding the phenomenon?

This whole thing reminds me of an issue involving T_LOCK that existed for any version earlier than 2.3.1. The way the scheduler lock (i.e. T_LOCK) was managed at that time would cause unexpectable results (aka "utter mess") to happen whenever a scheduler lock holder thread switches to secondary mode, and thinking about it, this would surely cause a lockup due to a scheduling wreckage. This has been fixed on January 30, by allowing the currently running thread which holds the scheduler lock to sleep.

You may want to try 2.3.1 to check this.

Signature · 1 line
Philippe.
author
#6 lore raw

Hi Philippe,

thank you for this suggestion!

You are right, I`m still using version 2.3. This is due to some adjustments I have made in the Xenomai Sources which prevent me from simply overwriting the old files.

I have just locked at the source-code, the only occasion I use T_LOCK is in the rt_task_set_mode() call, used as the first argument (clear mask!). So there should not be a T_LOCK at all.

Nevertheless, if anybody could provide me with the appropriate positions in Xenomai`s source code, I might apply this bugfix manually and find out whether the problem is solved this way.

Thank you very much,

Jack

-------- Original-Nachricht --------
Datum: Tue, 03 Apr 2007 22:24:35 +0200
Von: Philippe Gerum <[email protected]>
An: Jack Whorn <[email protected]>
CC: Gilles Chanteperdrix <[email protected]>, [email protected]
Betreff: Re: [Xenomai-help] Preemptive scheduling

Quoted 27 lines · Jack Whorn
Quoted 4 lines

Hi Gilles, Hi folks,

The data is stored by a thread that periodically issues a single call to rt_task_set_mode(0, T_PRIMARY, NULL) followed by several file operation

calls like fprintf() and finally a call to fflush(NULL) and sync().

Quoted 2 lines

The application is not a kernel module, so - as far as I see it - it is

a user mode thread, and it actually has the highest priority in my system.

Quoted 3 lines

Does this help understanding the phenomenon?

This whole thing reminds me of an issue involving T_LOCK that existed for any version earlier than 2.3.1. The way the scheduler lock (i.e. T_LOCK) was managed at that time would cause unexpectable results (aka "utter mess") to happen whenever a scheduler lock holder thread switches to secondary mode, and thinking about it, this would surely cause a lockup due to a scheduling wreckage. This has been fixed on January 30, by allowing the currently running thread which holds the scheduler lock to sleep.

You may want to try 2.3.1 to check this.

Signature · 1 line
Philippe.
Signature · 2 lines
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
#7 lore raw
Quoted 4 lines · Jack Whorn

Hi Philippe,

thank you for this suggestion!

Quoted 9 lines

You are right, I`m still using version 2.3. This is due to some adjustments I have made in the Xenomai
Sources which prevent me from simply overwriting the old files.

I have just locked at the source-code, the only occasion I use T_LOCK is in the rt_task_set_mode() call,
used as the first argument (clear mask!). So there should not be a T_LOCK at all.

Nevertheless, if anybody could provide me with the appropriate positions in Xenomai`s source code,
I might apply this bugfix manually and find out whether the problem is solved this way.

Changeset r2092 introduced the sleeping scheduler lock for v2.3.x.

Quoted 40 lines

Thank you very much,

Jack

-------- Original-Nachricht --------
Datum: Tue, 03 Apr 2007 22:24:35 +0200
Von: Philippe Gerum <[email protected]>
An: Jack Whorn <[email protected]>
CC: Gilles Chanteperdrix <[email protected]>, [email protected]
Betreff: Re: [Xenomai-help] Preemptive scheduling

Quoted 27 lines · Jack Whorn
Quoted 4 lines

Hi Gilles, Hi folks,

The data is stored by a thread that periodically issues a single call to rt_task_set_mode(0, T_PRIMARY, NULL) followed by several file operation

calls like fprintf() and finally a call to fflush(NULL) and sync().

Quoted 2 lines

The application is not a kernel module, so - as far as I see it - it is

a user mode thread, and it actually has the highest priority in my system.

Quoted 3 lines

Does this help understanding the phenomenon?

This whole thing reminds me of an issue involving T_LOCK that existed for any version earlier than 2.3.1. The way the scheduler lock (i.e. T_LOCK) was managed at that time would cause unexpectable results (aka "utter mess") to happen whenever a scheduler lock holder thread switches to secondary mode, and thinking about it, this would surely cause a lockup due to a scheduling wreckage. This has been fixed on January 30, by allowing the currently running thread which holds the scheduler lock to sleep.

You may want to try 2.3.1 to check this.

Signature · 1 line
Philippe.
Signature · 1 line
Philippe.
author
#8 lore raw
Quoted 2 lines

Changeset r2092 introduced the sleeping scheduler lock for v2.3.x.

Hi Philippe,

I appreciate your help very much! I found and applied the diffs of changeset r2092. It seems to work much better now: The system is able to access the hard disk, as I see the LED blinking periodically. That`s great!

Still, I cant use the keyboard to stop the application by pressing Strg-C. Also, the text cursor doesnt blink, so the Linux system seems to be overloaded (probably, it`s no longer scheduled).

So, the Linux-Kernel now seems to be able to run on behalf of a high-priority task. Though, it is no longer scheduled as an idle task, which is ok as there is the higher priority-task with the infinite loop. This way, everything acts as expected.

Nevertheless, it would be interesting to run a linux shell concurrently. Can I adjust linux priority during runtime (or: where do I have to adjust it in the sources)?

And btw, where can I look up the keyboard interrupts priority (is it kernels priority)?

Thanks a lot,

Jack

Signature · 2 lines
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
#9 lore raw
Quoted 8 lines · Jack Whorn
Quoted 1 line

Changeset r2092 introduced the sleeping scheduler lock for v2.3.x.

Hi Philippe,

I appreciate your help very much! I found and applied the diffs of changeset r2092.
It seems to work much better now: The system is able to access the hard disk, as I see the LED blinking periodically. That`s great!

Quoted 8 lines

Still, I cant use the keyboard to stop the application by pressing Strg-C. Also, the text cursor doesnt blink, so the Linux system seems to be overloaded
(probably, it`s no longer scheduled).

So, the Linux-Kernel now seems to be able to run on behalf of a high-priority task.
Though, it is no longer scheduled as an idle task, which is ok as there is the highe
r priority-task with the infinite loop. This way, everything acts as expected.

What you see is your real-time task called "thread" running, nothing else.

Quoted 5 lines

Nevertheless, it would be interesting to run a linux shell concurrently. Can I adjust
linux priority during runtime (or: where do I have to adjust it in the sources)?

And btw, where can I look up the keyboard interrupts priority (is it kernels priority)?

You may want to read these articles, explaining the basic concept of primary and secondary domains as seen by the native API and all other interfaces, and how this is implementing by the Adeos layer:
http://www.xenomai.org/documentation/branches/v2.3.x/pdf/Native-API-Tour-rev-C.pdf
http://www.xenomai.org/documentation/branches/v2.3.x/pdf/Life-with-Adeos-rev-B.pdf

In short, the infinite loop inside your application runs in primary mode, which starves the regular Linux kernel from CPU and interrupts, so that primary mode remains highly predictable time-wise. For that reason, hitting ^C has no effect when running this loop (no kbd IRQ and no input core polling for kbd event => no driver interaction => no termio handling => no SIGINT ever emitted therefore received by your process). All the CPU is eaten by your thread, and only real-time interrupts are allowed to preempt it from time to time.
(Activating CONFIG_XENO_OPT_WATCHDOG would pull the break and stop this loop after 4s btw).

You cannot adjust the kernel priority in the Xenomai scale yourself, this would introduce unpredictable latencies. Xenomai does a similar adjustment automatically when a real-time switches to secondary mode (an explanation can be found in the cited docs), but this could be applied to any random Linux task without wrecking the real-time behaviour, so this has not been made possible.

To sum up, if you want regular I/O such as keyboard input to be processed by Linux, you need to leave a bit of CPU time to the regular kernel. This is why the infinite loop seems to lock up the box.

Rule of thumb: this is not a pure RTOS, but a combo merging a GPOS and a RTOS sub-system to get the best of both worlds, so you need to make sure that both can co-exist together - which does not prevent to one a higher priority over the other - and leaving CPU time to the regular kernel to process regular I/O through normal drivers is part of the requirements. It is possible to develop real-time I/O drivers (RTDM is there for this task), in this case, most of their code is controlled in primary mode by Xenomai, and not by the vanilla Linux kernel.

Quoted 4 lines

Thanks a lot,

Jack

Signature · 1 line
Philippe.
author
#10 lore raw

Hi Philippe,

Thanks a lot for this information!

This watchdog is great. I just tried and it successfully stopped my application. Nice to see!

At this point, I have a short question:

As far as I have seen, Xenomai`s watchdog kills the currently running task when wd_count (which is increased every second) has reached 4. But when is wd_count reset? I have only seen wd_count being reset to zero whenever Linux is scheduled. I don`t understand how this can be sufficient (which obviously is!), as I had expected it being reset whenever *any* task is rescheduled. How can the system know that it is the currently running task that has prevented Linux from being scheduled?

Thanks for giving me a hint on this,

Jack

-------- Original-Nachricht --------
Datum: Thu, 05 Apr 2007 20:38:57 +0200
Von: Philippe Gerum <[email protected]>
An: Jack Whorn <[email protected]>
CC: [email protected]
Betreff: Re: [Xenomai-help] Preemptive scheduling

Quoted 81 lines · Jack Whorn
Quoted 5 lines
Quoted 1 line

Changeset r2092 introduced the sleeping scheduler lock for v2.3.x.

Hi Philippe,

I appreciate your help very much! I found and applied the diffs of

changeset r2092.

Quoted 1 line

It seems to work much better now: The system is able to access the hard

disk, as I

Quoted 3 lines

see the LED blinking periodically. That`s great!

Still, I can`t use the keyboard to stop the application by pressing

Strg-C.

Quoted 1 line

Also, the text cursor doesn`t blink, so the Linux system seems to be

overloaded

Quoted 3 lines

(probably, it`s no longer scheduled).

So, the Linux-Kernel now seems to be able to run on behalf of a

high-priority task.

Quoted 1 line

Though, it is no longer scheduled as an idle task, which is ok as there

is the highe

Quoted 1 line

r priority-task with the infinite loop. This way, everything acts as

expected.

Quoted 1 line

What you see is your real-time task called "thread" running, nothing else.

Quoted 1 line

Nevertheless, it would be interesting to run a linux shell concurrently.

Can I adjust

Quoted 1 line

linux priority during runtime (or: where do I have to adjust it in the

sources)?

Quoted 2 lines

And btw, where can I look up the keyboard interrupt`s priority (is it

kernel`s priority)?

Quoted 1 line

You may want to read these articles, explaining the basic concept of primary and secondary domains as seen by the native API and all other interfaces, and how this is implementing by the Adeos layer:
http://www.xenomai.org/documentation/branches/v2.3.x/pdf/Native-API-Tour-rev-C.pdf
http://www.xenomai.org/documentation/branches/v2.3.x/pdf/Life-with-Adeos-rev-B.pdf

In short, the infinite loop inside your application runs in primary mode, which starves the regular Linux kernel from CPU and interrupts, so that primary mode remains highly predictable time-wise. For that reason, hitting ^C has no effect when running this loop (no kbd IRQ and no input core polling for kbd event => no driver interaction => no termio handling => no SIGINT ever emitted therefore received by your process). All the CPU is eaten by your thread, and only real-time interrupts are allowed to preempt it from time to time.
(Activating CONFIG_XENO_OPT_WATCHDOG would pull the break and stop this loop after 4s btw).

You cannot adjust the kernel priority in the Xenomai scale yourself, this would introduce unpredictable latencies. Xenomai does a similar adjustment automatically when a real-time switches to secondary mode (an explanation can be found in the cited docs), but this could be applied to any random Linux task without wrecking the real-time behaviour, so this has not been made possible.

To sum up, if you want regular I/O such as keyboard input to be processed by Linux, you need to leave a bit of CPU time to the regular kernel. This is why the infinite loop seems to lock up the box.

Rule of thumb: this is not a pure RTOS, but a combo merging a GPOS and a RTOS sub-system to get the best of both worlds, so you need to make sure that both can co-exist together - which does not prevent to one a higher priority over the other - and leaving CPU time to the regular kernel to process regular I/O through normal drivers is part of the requirements. It is possible to develop real-time I/O drivers (RTDM is there for this task), in this case, most of their code is controlled in primary mode by Xenomai, and not by the vanilla Linux kernel.

Quoted 4 lines

Thanks a lot,

Jack

Signature · 1 line
Philippe.
Signature · 2 lines
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
#11 lore raw
Quoted 3 lines · Jack Whorn

[...]
As far as I have seen, Xenomai`s watchdog kills the currently running task when >wd_count (which is increased every second) has reached 4. But when is wd_count >reset? I have only seen wd_count being reset to zero whenever Linux is scheduled. I >don`t understand how this can be sufficient (which obviously is!), as I had expected it >being reset whenever *any* task is rescheduled. How can the system know that it is >the currently running task that has prevented Linux from being scheduled?

The comment in xnpod_watchdog_handler() is a bit misleading.

The idea is to avoid starvation of the linux kernel for more than 4 seconds, right. But this starvation can be well done either by one rt task or a few which are running one by one. The watchdog will kill the one that happens to run at 4-th second.

e.g. 2 tasks : task1 takes 3.98 s. and then task2 - just 0.05 second. The watchdog handler will shoot task2, although the real problem is task1.

Nevertheless, for the majority of cases, this problem happens when some rt task is misbehaving (say, an endless loop under some circumstances) and - the one that gets killed - is the one that monopolise the CPU indeed.

Signature · 2 lines
Best regards,
Dmitry Adamushko