1

I'm testing POSIX skin in Xenomai. I'm trying to read and write from some GPIOs on a Raspberry Pi, and when I execute the program, there is an increasing number of context switching (in /proc/xenomai/stat/).

The main of the program maps the GPIOs to memory and starts the pthreads. The pthread that makes trouble is this:

void *productive_thread(void *arg)
{
    struct timespec delay, sleep;
    unsigned long over;

    delay.tv_sec = 0;
    delay.tv_nsec = 10000; // 10 usec

    sleep.tv_sec = 0;
    sleep.tv_nsec = *(long *)arg;

    while(1)
    {
            // This are the read and write macros (gpio is the address of the GPIO mapping):
            // #define GPIO_SET *(gpio+7)
            // #define GPIO_CLR *(gpio+10)
            // #define GPIO_READ(g) (*(gpio + 13)&(1<<(g)))>>4
        while(GPIO_READ(4) != 1);
        GPIO_SET = 1 << 17;
        clock_nanosleep(CLOCK_REALTIME, 0, &delay, NULL);
        GPIO_CLR = 1 << 17;
        clock_nanosleep(CLOCK_REALTIME, 0, &sleep, NULL);
    }
    return NULL;
}

The number of context switching increases by every loop. I suspect the problem is clock_nanosleep, because all other operations are arithmetic, but clock_nanosleep is defined in Xenomai documentation. Can it be improved somehow (using POSIX skin)?

markmb
  • 852
  • 4
  • 12
  • 32
  • `clock_nanosleep` is an interruptible sleep, which explains the reason for the context switch increases. If you don't want the context switch, then you can use a busy loop using `rt_timer_spin`, but that's CPU intensive :( – Anya Shenanigans Nov 06 '13 at 17:30

1 Answers1

3

When a process sleeps it performs a voluntary context switch. This is fine, unless you are actually seeing your deadlines missed.

caf
  • 233,326
  • 40
  • 323
  • 462