This shows you the differences between two versions of the page.
| — | comfilepi:improving_real-time_performance:index [2026/02/20 15:37] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Improving Real-Time Performance on the ComfilePi ====== | ||
| + | The following changes can be made to the ComfilePi to improve the the real-time performance of a program running on it. | ||
| + | |||
| + | * Set the CPU governor to '' | ||
| + | * Isolate a real-time program to one of the 4 CPU cores | ||
| + | * Schedule the program as real-time with high priority | ||
| + | * Disable realtime throttling. | ||
| + | * [[comfilepi: | ||
| + | |||
| + | ===== Setting the CPU Governor to '' | ||
| + | |||
| + | Run the following commands in a terminal to set the CPU governor to '' | ||
| + | |||
| + | < | ||
| + | sudo apt install cpufrequtils | ||
| + | sudo cpufreq-set -g performance | ||
| + | </ | ||
| + | |||
| + | Operating system utilities should report the frequency at 1.2GHz. | ||
| + | < | ||
| + | pi@raspberrypi: | ||
| + | 1500000 | ||
| + | 1500000 | ||
| + | 1500000 | ||
| + | 1500000 | ||
| + | pi@raspberrypi: | ||
| + | frequency(48)=1500398464 | ||
| + | </ | ||
| + | ===== Isolate a Program to One of the 4 CPU Cores ===== | ||
| + | |||
| + | First, add '' | ||
| + | |||
| + | Then use '' | ||
| + | < | ||
| + | sudo taskset -cp 3 `pidof test` | ||
| + | </ | ||
| + | |||
| + | Core isolation can also be achieved using // | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ===== Schedule the Program as Real-Time with High Priority ===== | ||
| + | |||
| + | This will configure the process with FIFO scheduling, and increase its priority to the maximum. | ||
| + | |||
| + | < | ||
| + | sudo chrt -f -p 99 `pidof test` | ||
| + | </ | ||
| + | |||
| + | '' | ||
| + | {{ : | ||
| + | |||
| + | |||
| + | ===== Disable Realtime Throttling (Not Recommended) ===== | ||
| + | |||
| + | When using FIFO scheduling, it may be necessary to disable [[https:// | ||
| + | |||
| + | < | ||
| + | sudo sh -c "echo -1 > / | ||
| + | </ | ||
| + | |||
| + | However, this is a blunt instrument because it disables realtime throttling on all cores. | ||
| + | |||
| + | ===== Demonstration, | ||
| + | |||
| + | ==== Test Program ==== | ||
| + | |||
| + | In this test, a program is created to output a 100µs alternating pulse (500kHz) on one of the ComfilePI' | ||
| + | |||
| + | <code C++> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | using namespace std:: | ||
| + | |||
| + | void delay(uint32_t us) | ||
| + | { | ||
| + | auto t1 = steady_clock:: | ||
| + | auto t2 = t1 + microseconds(us); | ||
| + | this_thread:: | ||
| + | while (steady_clock:: | ||
| + | } | ||
| + | |||
| + | int main(int argc, char **argv) | ||
| + | { | ||
| + | const char *chipname = " | ||
| + | struct gpiod_chip *chip; | ||
| + | struct gpiod_line *line16; | ||
| + | |||
| + | // Open GPIO16 | ||
| + | chip = gpiod_chip_open_by_name(chipname); | ||
| + | line16 = gpiod_chip_get_line(chip, | ||
| + | gpiod_line_request_output(line16, | ||
| + | |||
| + | // Toggle GPIO every 100us | ||
| + | while (true) | ||
| + | { | ||
| + | gpiod_line_set_value(line16, | ||
| + | delay(50); | ||
| + | gpiod_line_set_value(line16, | ||
| + | delay(50); | ||
| + | } | ||
| + | |||
| + | // Release lines and chip | ||
| + | gpiod_line_release(line16); | ||
| + | gpiod_chip_close(chip); | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Compile with... | ||
| + | < | ||
| + | # Install the gpiod library if necessary | ||
| + | # sudo apt install libgpiod-dev | ||
| + | |||
| + | # Compile the program | ||
| + | g++ -O3 test.cpp -lgpiod -o test | ||
| + | </ | ||
| + | |||
| + | An oscilloscope shows the pulse as physically generated by the ComfilePi. | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | While the real-time program is executing, a separate process is run to simulate browsing the web using the Chromium browser. | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | |||
| + | while true | ||
| + | do | ||
| + | chromium-browser https:// | ||
| + | sleep 15s | ||
| + | killall chromium-browser | ||
| + | sleep 5s | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | Due to the CPU isolation configuration explained above, Linux will allocate the Chromium browser only to the first 3 cores. | ||
| + | ==== Measurement and Results ==== | ||
| + | |||
| + | An external data capture instrument is used to measure the consistency of the pulse. | ||
| + | |||
| + | The test is conducted under 3 configurations: | ||
| + | - An ordinary kernel with no CPU isolation or real-time scheduling | ||
| + | - An ordinary kernel with CPU isolation and real-time scheduling | ||
| + | - A real-time kernel with CPU isolation and real-time scheduling | ||
| + | |||
| + | |||
| + | {{ : | ||
| + | |||
| + | As can be seen in the chart above, the ordinary kernel, without any CPU isolation or real-time scheduling, operates with excessive jitter and would likely not be suitable for any real-time application. | ||
| + | |||
| + | Although the real-time kernel can reduce jitter, it trades off overall performance throughput. | ||
| + | |||
| + | ==== Additional Information ==== | ||
| + | * [[https:// | ||
| + | |||
| + | [[comfilepi: | ||