Kernel Affinity

In additions, the OpenPLC runtime process can be assigned to the specified CPU core by modify the main.cpp file. It is implemented through the ‘cpuset’, ‘sched_set/getaffinity’ and ‘pthread_setaffinity_np’ functions.

Refer to following links for more details:

For example, modify main.cpp to run the main OpenPLC task on the isolated core. Something like this on the REAL-TIME INITIALIZATION section in main.cpp should do the job:

//Set process affinity to core 3
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(3, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) < 0)
{
printf("WARNING: Error setting affinity of main thread\n");
}
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
{
printf("WARNING: Error setting thread affinity of main thread\n");
}
Figure 1. Example Setting

After modifying main.cpp as shown above, re-compile the modified main.cpp file by re-selecting the PLC program or re-saving the hardware layer file and setting kernel isolation, the OpenPLC main process ./core/openplc will automatically runs on the isolated core after restarting.

It’s recommend to run CPU in highest frequency by run: ‘echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor’ to achieve optimal system performance.