Real-Time Performance

This topic explains real-time performance concepts, measurement methods, and optimization techniques for OpenPLC on VisionFive 2 or VisionFive 2 Lite.

Understanding Real-Time Performance

Real-time performance refers to the ability of the operating system to respond to external events in a timely and deterministic manner. A system with real-time capabilities can guarantee that a certain task is completed within a defined time frame.

Real-time performance for OpenPLC can be measured at two levels: system-level and application-level.
  • System-level testing (Cyclictest): RT-tests is a collection of programs that test the real-time capabilities of Linux. Cyclictest is one of the programs in the RT-tests suite that most commonly used for benchmarking RT systems.
  • Application-level testing: The real-time performance testing of OpenPLC during operation, measured by the delay between input and output signals and the minimum scanning time

Cyclictest

Cyclictest is a benchmark tool that measures the latency of the Linux kernel's real-time scheduling capabilities.

To install cyclictest:
git clone git://git.kernel.org/pub/scm/linux/kernel/git/clrkwllms/rt-tests.git
git checkout -b stable/v1.0
Alternatively,
wget https://mirrors.edge.kernel.org/pub/linux/utils/rt-tests/rt-tests-1.10.tar.gz
make all
sudo make install
To run cyclictest with load:
run ./cyclictest -S -p 95 -d 0 -i 1000 -D 24h -m -n
Note: When running cyclictest, you can use simulated load to create enough load and thus making the statistical delay more meaningful. You can use the following resources to simulate load:

Application-Level Testings

Measure the performance of OpenPLC by the delay between input and output signals and the minimum scanning time.

Table 1. Application-Level Performance Testings
Test Details
Delay between Input and Output Signals
  1. Run the ladder diagram program.
  2. Input PWM signals with frequency of 10Hz and duty ratio of 50% to the input port %IX0.2 through the logic analyzer.
  3. Detect the input signals %IX0.2 and output signals %QX0.0.
  4. Record the time delay between the rising/falling edges of the input and output signals and calculate the mean and standard errors.
Minimum Scanning Time
Scan time is the time a PLC takes to read all inputs, execute the logic program, and write all outputs back. The proposed minimum scan time test is comprised of a single ladder logic line with one input and one output, as shown below.
Figure 1. Example Scan Time Test

By running this program and physically wiring the output %QX0.0 and the input %IX0.2 on the PLC together, it is possible to create an oscillator. This oscillator outputs a square wave with the highest frequency the PLC can generate in software. Since the output toggles at every scan, the scan time is defined by equation t = 1 2 f , where f is the square wave frequency measured by an oscilloscope. In addition to the difference in the ladder diagram, to measure the minimum scan time, the program of the minimum scan time in the scan interval of the Res0 file is set to T#1ms, while in the delay test, it is T#5ms.
Figure 2. Minimum Scan Time

Improve OpenPLC Runtime Performance

Try improving the real-time performance of OpenPLC runtime by the following methods:
  • Isolate CPU core and bind OpenPLC process to the isolated CPU core
  • Run OpenPLC runtime in RT-Linux
Table 2. Performance Improvement Tasks
Task Details
Isolate CPU Core
  1. Run cat /proc/cpuinfo to check the information of CPU.
  2. Add isolcpus parameters in the startup command line file extlinux.conf, and isolate specific CPUs from the general SMP balancing and scheduler algorithms.
    isolcpus=<cpu number>,….,<cpu number>

    For example, isolcpus=3 isolates the CPU core 3.

    For more information, see isolcpus.

Bind OpenPLC to the Isolated CPU Core After isolating a CPU core, bind the OpenPLC process to the core by using either of the following methods:
  • Using taskset command:
    1. Start OpenPLC runtime and then run systemctl status openplc to view the OpenPLC runtime processes.
    2. Run sudo taskset -pc x PID to bind the PID process to the isolated CPU core (x).

      For example, run sudo taskset -pc 3 3140 to bind the main process ./core/openplc to CPU core 3.

  • Modifying the main.cpp file. This is implemented through the following functions:

    After modifying main.cpp, re-compile the file by re-selecting the PLC program or re-saving the hardware layer file and setting kernel isolation.

Run OpenPLC on RT-Linux See Real-Time Linux.