Last active
October 27, 2016 01:51
-
-
Save giuliomoro/457c8cde1eda6190a28566b3288d38c0 to your computer and use it in GitHub Desktop.
xenomai shared memory causes mode switches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/mman.h> | |
#include <sys/stat.h> /* For mode constants */ | |
#include <fcntl.h> /* For O_* constants */ | |
#include <stdio.h> | |
#include <error.h> | |
#include <errno.h> | |
#include <native/task.h> | |
#define SHM_NAME "/myshared" | |
int main () | |
{ | |
rt_print_auto_init(1); // enable rt_printf output | |
// turn ourselves into a xenomai task, in primary mode | |
rt_task_shadow(NULL, "main", 95, T_FPU); | |
int fd; | |
int count = 0; | |
// this will switch us to secondary mode | |
//chose either line according to your kernel config | |
fd = shm_open(SHM_NAME, O_RDWR | O_CREAT | O_DIRECT, 0); // CONFIG_XENO_OPT_POSIX_SHM=y | |
//fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, 0); // CONFIG_XENO_OPT_POSIX_SHM=n | |
if(fd < 0){ | |
printf("%s\n", strerror(errno)); | |
return 1; | |
} | |
rt_printf("Opened\n"); | |
size_t len = 40; | |
ftruncate(fd, len); | |
rt_printf("size: %d\n", len); | |
void* ignored; | |
//this causes a switch to secondary mode | |
void* addr = mmap(ignored, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
if(addr == MAP_FAILED){ | |
rt_printf("Error: %s %p\n", strerror(errno)); | |
return 1; | |
} | |
rt_printf("mmapped\n"); | |
float* arr = (float*)addr; | |
// can now force to go back to primary mode | |
rt_task_shadow(NULL, "main", 95, T_FPU); | |
unsigned int n = 0; | |
unsigned int j = 0; | |
rt_printf("accessing mapped memory\n"); | |
for(j = 0; j < 10; ++j){ | |
for(n = 0; n < len/sizeof(float); ++n){ | |
// from here we should be able to access memory without mode switches | |
// so if this line causes mode switches, you would | |
// see them incrementing | |
arr[n] = (float)n; | |
rt_task_sleep(0); // sleep, switches to primary mode | |
rt_printf("%f\n", arr[n]); | |
rt_task_sleep(0); // sleep, switches to primary mode | |
} | |
rt_task_sleep(900000000); // sleep, switches to primary mode | |
} | |
rt_task_sleep(5000000000); //sleep 5 seconds so you can have a final look at /proc/xenomai/stat | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment