Skip to contentSkip to conversation

Scheduling while atomic

Xenomai-core Jeroen Van den Keybus 3 participants 7 messages Jan 18, 2006 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hello,

Apparently, the code I shared with Gilles never made it to this forum. Anyway, the issue I'm having here is really a problem and it might be useful if some of you could try it out or comment on it. I might be making a silly programming error here, but the result is invariably erroneous operation or kernel crashes.

The program creates a file dump.txt and has two independent threads trying to access it and write a one or a zero there. Inside the writing routine, which is accessed by both threads, a check is made to see if the access is really locked. In my setup, I have tons of ALERTS popping up with this program, meaning that something is wrong with my use of mutex. Could anyone please check and see if a) it is correctly written and b) it fails as well on their machine. It would allow me to focus my actions on the Xenomai setup (which I keep frozen this instant, in order to keep a possible bug predictable) or on my own programming.

A second example is also included, which tries to achieve the same goal with a semaphore (initialized to 1). That seems to work, but under heavy load (tmax = 1.0e7), the kernel crashes.

Kernel: 2.6.15 Adeos: 1.1-03 gcc: 4.0.2 Ipipe tracing enabled

TIA

Jeroen.

/* TEST_MUTEX.C */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <math.h>
#include <values.h>

#include <sys/mman.h>

#include <native/task.h>
#include <native/mutex.h>
#include <native/sem.h>

int fd, err;
RT_MUTEX m;
RT_SEM s;
float tmax = 1.0e7;

#define CHECK(arg) check(arg, __LINE__)

int check(int r, int n)

{
    if (r != 0)
        fprintf(stderr, "L%d: %s.\n", n, strerror(-r));
    return(r);
}
void output(char c) {
    static int cnt = 0;
    int n;
    char buf[2];
    RT_MUTEX_INFO mutexinfo;

    buf[0] = c;

    if (cnt == 80) {
        buf[1] = '\n';
        n = 2;
        cnt = 0;
    }
    else {
        n = 1;
        cnt++;
    }

    CHECK(rt_mutex_inquire(&m, &mutexinfo));
    if (mutexinfo.lockcnt <= 0) {
        RT_TASK_INFO taskinfo;
        CHECK(rt_task_inquire(NULL, &taskinfo));
        fprintf(stderr, "ALERT: No lock! (lockcnt=%d) Offending task: %s\n",
                mutexinfo.lockcnt, taskinfo.name);
    }

    if (write(fd, buf, n) != n) {
        fprintf(stderr, "File write error.\n");
        CHECK(rt_sem_v(&s));
    }

}

void task0(void *arg)

{
    CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL));
    while (1) {
        CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX));
        CHECK(rt_mutex_lock(&m, TM_INFINITE));
        output('0');
        CHECK(rt_mutex_unlock(&m));
    }
}

void task1(void *arg)

{
    CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL));
    while (1) {
        CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX));
        CHECK(rt_mutex_lock(&m, TM_INFINITE));
        output('1');
        CHECK(rt_mutex_unlock(&m));
    }
}

void sighandler(int arg)

{
    CHECK(rt_sem_v(&s));
}

int main(int argc, char *argv[])

{
    RT_TASK t, t0, t1;

    if ((fd = open("dump.txt", O_CREAT | O_TRUNC | O_WRONLY)) < 0)
        fprintf(stderr, "File open error.\n");
    else {
        if (argc == 2) {
            tmax = atof(argv[1]);
            if (tmax == 0.0)
                tmax = 1.0e7;
        }
        if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
            printf("mlockall() error.\n");

        CHECK(rt_task_shadow(&t, "main", 1, T_FPU));

        CHECK(rt_timer_start(TM_ONESHOT));

        CHECK(rt_mutex_create(&m, "mutex"));
        CHECK(rt_sem_create(&s, "sem", 0, S_PRIO));

        signal(SIGINT, sighandler);

        CHECK(rt_task_create(&t0, "task0", 0, 30, T_FPU));
        CHECK(rt_task_start(&t0, task0, NULL));
        CHECK(rt_task_create(&t1, "task1", 0, 29, T_FPU));
        CHECK(rt_task_start(&t1, task1, NULL));

        printf("Running for %.2f seconds.\n", (float)MAXLONG/1.0e9);
        CHECK(rt_sem_p(&s, MAXLONG));
        signal(SIGINT, SIG_IGN);

        CHECK(rt_task_delete(&t1));
        CHECK(rt_task_delete(&t1));
        CHECK(rt_task_delete(&t0));

        CHECK(rt_sem_delete(&s));
        CHECK(rt_mutex_delete(&m));

        rt_timer_stop();

        close(fd);
    }
    return 0;
}

/*************************************************************************************/

/* TEST_SEM.C */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <math.h>
#include <values.h>

#include <sys/mman.h>

#include <native/task.h>
#include <native/sem.h>

int fd, err;
RT_SEM s, m;
float tmax = 1.0e9;

#define CHECK(arg) check(arg, __LINE__)

int check(int r, int n)

{
    if (r != 0)
        fprintf(stderr, "L%d: %s.\n", n, strerror(-r));
    return(r);
}
void output(char c) {
    static int cnt = 0;
    int n;
    char buf[2];
    RT_SEM_INFO seminfo;

    buf[0] = c;

    if (cnt == 80) {
        buf[1] = '\n';
        n = 2;
        cnt = 0;
    }
    else {
        n = 1;
        cnt++;
    }

    CHECK(rt_sem_inquire(&m, &seminfo));
    if (seminfo.count != 0) {
        RT_TASK_INFO taskinfo;
        CHECK(rt_task_inquire(NULL, &taskinfo));
        fprintf(stderr, "ALERT: No lock! (count=%ld) Offending task: %s\n",
                seminfo.count, taskinfo.name);
    }

    if (write(fd, buf, n) != n) {
        fprintf(stderr, "File write error.\n");
        CHECK(rt_sem_v(&s));
    }

}

void task0(void *arg)

{
    CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL));
    while (1) {
        CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX));
        CHECK(rt_sem_p(&m, TM_INFINITE));
        output('0');
        CHECK(rt_sem_v(&m));
    }
}

void task1(void *arg)

{
    CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL));
    while (1) {
        CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX));
        CHECK(rt_sem_p(&m, TM_INFINITE));
        output('1');
        CHECK(rt_sem_v(&m));
    }
}

void sighandler(int arg)

{
    CHECK(rt_sem_v(&s));
}

int main(int argc, char *argv[])

{
    RT_TASK t, t0, t1;

    if ((fd = open("dump.txt", O_CREAT | O_TRUNC | O_WRONLY)) < 0)
        fprintf(stderr, "File open error.\n");
    else {
        if (argc == 2) {
            tmax = atof(argv[1]);
            if (tmax == 0.0)
                tmax = 1.0e7;
        }
        if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
            printf("mlockall() error.\n");

        CHECK(rt_task_shadow(&t, "main", 1, T_FPU));

        CHECK(rt_timer_start(TM_ONESHOT));

        CHECK(rt_sem_create(&m, "mutex", 1, S_PRIO));
        CHECK(rt_sem_create(&s, "sem", 0, S_PRIO));

        signal(SIGINT, sighandler);

        CHECK(rt_task_create(&t0, "task0", 0, 30, T_FPU));
        CHECK(rt_task_start(&t0, task0, NULL));
        CHECK(rt_task_create(&t1, "task1", 0, 29, T_FPU));
        CHECK(rt_task_start(&t1, task1, NULL));

        printf("Running for %.2f seconds.\n", (float)MAXLONG/1.0e9);
        CHECK(rt_sem_p(&s, MAXLONG));
        signal(SIGINT, SIG_IGN);

        CHECK(rt_task_delete(&t1));
        CHECK(rt_task_delete(&t1));
        CHECK(rt_task_delete(&t0));

        CHECK(rt_sem_delete(&s));
        CHECK(rt_sem_delete(&m));

        rt_timer_stop();

        close(fd);
    }
    return 0;
}
#1 lore raw
Quoted 4 lines · Jeroen Van den Keybus

Hello,

Apparently, the code I shared with Gilles never made it to this forum. Anyway, the issue I'm having here is really a problem and it might be useful if some of you could try it out or comment on it.

If you have a makefile for them, I'll give it a try. I'm too lazy...err...busy to make one myself(*) ;-)

Best regards,
Hannes.

(*) Jan has the copyright on this line, so I hope it's GPL ;-)

#2 lore raw
Quoted 10 lines · Hannes Mayer
Quoted 6 lines · Jeroen Van den Keybus

Hello,

Apparently, the code I shared with Gilles never made it to this forum. Anyway, the issue I'm having here is really a problem and it might be useful if some of you could try it out or comment on it.

If you have a makefile for them, I'll give it a try. I'm too lazy...err...busy to make one myself(*) ;-)

Try this, it even works without make:

gcc -o <output> <input.c> `<path-to-xeno-config> --xeno-cflags` \
	`<path-to-xeno-config> --xeno-ldlags` -lnative

Do you (or anybody else) have a running 2.0.x installation? If so, please test that setup as well.

Quoted 5 lines

Best regards,
Hannes.

(*) Jan has the copyright on this line, so I hope it's GPL ;-)

No rights reserved. :)

Ok, have to go home now - getting hungry...

Jan

#3 lore raw

Jan Kiszka wrote:
[...]

Quoted 2 lines

Do you (or anybody else) have a running 2.0.x installation? If so, please test that setup as well.

Sure :-)

# uname -r
2.6.13.4-adeos-xenomai
# cat /proc/xenomai/version
2.0
# ./mutex
Running for 2.15 seconds.
ALERT: No lock! (lockcnt=0) Offending task: task0
ALERT: No lock! (lockcnt=0) Offending task: task0
ALERT: No lock! (lockcnt=0) Offending task: task0
ALERT: No lock! (lockcnt=0) Offending task: task0
L121: Connection timed out.
# cat dump.txt
1010010010101010110001000010001[...]
# ./sem
Running for 2.15 seconds.
L119: Connection timed out.
# cat dump.txt
101001muon:/home/xenomai/atomic#

More tests ?

Best regards,
Hannes.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 6 lines · Jeroen Van den Keybus

- Could you replace MAXLONG on line 119 with TM_INFINITE and rerun the sem test (to avoid printf'ing) You can terminate with Ctrl+C.
- Could you try to run with tmax somewhat lower (to increase the load):
e.g. ./sem 5.0e6 or so. Be careful not to starve Linux. I think 1.0e5 will be too low.

Crashes with me happen within the first 10 secs with 1.0e7

On Xeno2.0, kernel 2.6.13.4:

# ./sem 1.0e5
Running for 2.15 seconds.
L119: Operation not permitted.

Kernel.log:

Unable to handle kernel NULL pointer dereference at virtual address 00000114

[freeze]

# ./sem 1.0e4
Running for 2.15 seconds.

Kernel.log:
(sorry no serial console available yet) ...default_idle
...ipipe_supend_domain
...default_idle
...cpu_idle
...start_kernel
...unknown_bootoption

Kernel panic
# ./mutex 1.0e5
ALERT: No lock! (lockcnt=0) Offending task: task0
ALERT: No lock! (lockcnt=0) Offending task: task0
ALERT: No lock! (lockcnt=0) Offending task: task0
[...]
[freeze]

kernel.log:
[off-screen]
...panic
...do_exit
...printk
...die
...__ipipe_sync_stage
...do_page_fault
...__do_softirw
...irq_exit
...do_IRQ
...restore_raw
...__ipipe_handle_exception
...error_code
...__ipipe_sync_stage
...default_idle
...ipipe_supend_domain
...default_idle
...cpu_idle
...start_kernel
...unknown_bootoption

Kernel panic

Best regards,
Hannes.

#6 lore raw
Quoted 4 lines
# ./sem 1.0e5
Running for 2.15 seconds.
L119: Operation not permitted.

Could be a dirty cleanup. Sorry for that.

Quoted 6 lines

...ipipe_supend_domain
...default_idle
...cpu_idle
...start_kernel
...unknown_bootoption

Kernel panic

start_kernel ? Unknown_bootoption ? The plot is thickening.

Quoted 6 lines
# ./mutex 1.0e5
ALERT: No lock! (lockcnt=0) Offending task: task0
ALERT: No lock! (lockcnt=0) Offending task: task0
ALERT: No lock! (lockcnt=0) Offending task: task0
[...]
[freeze]

The problem is: freezing could be due to Linux getting no CPU time anymore. I think the best test now is to run the semaphore test (it should give no ALERTS) at a reasonable (1.0e6 - 1.0e7, which is on average 2kHz - 200Hz) speed for a longer duration. Maybe better 1.0e7, which will yield about 1.5Mb per hour.

Many thanks already for testing and helping us out.

Jeroen.

#7 lore raw
In reply to a message not in the archive

When testing, also check the dmesg logs please. When nucleus debugging is off, you get only a kernel warning 'scheduling while atomic'.

- Could you replace MAXLONG on line 119 with TM_INFINITE and rerun the sem test (to avoid printf'ing) You can terminate with Ctrl+C.

Quoted 11 lines

- Could you try to run with tmax somewhat lower (to increase the load):
e.g. ./sem 5.0e6 or so. Be careful not to starve Linux. I think 1.0e5 will be too low.

Crashes with me happen within the first 10 secs with 1.0e7

Thanks,

Jeroen.