Understanding Context Switches in Xenomai-Linux POSIX skin

2

I'm running a RT program on BeagleBone Black with Xenomai and trying to figure how to monitor/understand context switches (I know the concept of context switches) so that I can determine when my program (in C using POSIX skin) switches from primary and secondary mode.

Here's the program main_posix.c

#ifndef __XENO_SIM__
#ifndef __KERNEL__
#include <stdio.h>
#define xnarch_printf printf
#endif

#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include <pthread.h>
#include <mqueue.h>
#else /* __XENO_SIM */
#include <posix/posix.h>
#endif /* __XENO_SIM */

void warn_upon_switch(){

    printf("Switched to Secondary Mode \n");


}


void *threadFunc(void *arg)
{
    char *str;
    int i = 0;
    struct timespec delay, sleep;
    unsigned long over;
    int ret;

    str=(char*)arg;

    printf("In thread \n");

    sleep.tv_sec = 1;
    sleep.tv_nsec = 0;

    #ifdef __XENO__

        ret = pthread_set_mode_np(0,  0x00040000);

        printf("Warn Bit Ret %d\n", ret);

    #endif /* __XENO__ */

    // run this for some arbitrary time
    while(i < 110000000 )
    {
        clock_nanosleep(CLOCK_REALTIME, 0, &sleep, NULL);
        printf("threadFunc says: %s\n",str);
        ++i;
    } 

    return NULL;
}

int main(void)
{


    signal(SIGXCPU, warn_upon_switch);
    signal(SIGKILL, warn_upon_switch);



    pthread_t pth;  

    double i = 0;

    int ret;

    pthread_attr_t tattr;   

    struct sched_param sparam;
    sparam.sched_priority = 99;


    ret = pthread_attr_init(&tattr);
    printf("Init Return Val %d\n", ret);

    ret = pthread_setschedparam(pth,SCHED_FIFO, &sparam);
    printf("SetSchedParam Ret Value %d\n", ret);

    pthread_create(&pth,&tattr,threadFunc,"foo");

    printf("main waiting for thread to terminate...\n");
    pthread_join(pth,NULL);

    return 0;
}

I'm also monitoring /proc/xenomai/stat continuously through watch

enter image description here

I see that CSWand MSW for PID 3323 changes continuously.

Here's the output of ps -e -o class,rtprio,pri,nice,cmd | grep ./main_posix

enter image description here

The output is as follows

enter image description here

My questions are as follows

  1. How do I know if my program is running in primary or secondary mode?
  2. I get the return value of ret = pthread_setschedparam(pth,SCHED_FIFO, &sparam); as 16 which is EBUSY. Any idea why?
  3. Tried catching the switch signal using signal(SIGXCPU, warn_upon_switch);. The function never gets called.
  4. If the program can be seen in Linux (meaning it gets a PID through the Linux kernel), does it mean its running in secondary mode?
  5. In proc/xenomai/stat, I see two processes for the same program. Is it the main and the thread?

Here are some resources I used

  1. Periodic thread fails real-time in Xenomai
  2. Xenomai clock_nanosleep in POSIX skin jumps to Linux Kernel
  3. http://xenomai.org/2014/08/porting-a-linux-application-to-xenomai-dual-kernel/#Using_the_PTHREAD_WARNSW_bit
  4. http://www.xenomai.org/documentation/xenomai-2.6/html/api/sigxcpu_8c-example.html
c
linux
posix
beagleboneblack
xenomai
asked on Stack Overflow Feb 13, 2017 by am3 • edited May 23, 2017 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0