Understanding Context Switches in Xenomai-Linux POSIX skin

I’m basically trying to figure out if my program is handled by the Linux kernel (secondary mode) or the Xenomai kernel (primary mode) and also figureo ut when the switch happens.
I’m running a RT (Real Time) 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 my 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;
}
`

Im also monitoring
/proc/xenomai/stat

I see that CSW and MSW for PID 3323 changes continuously.

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

The output of the program is as follows

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 16which 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?