User Tools

Site Tools

한국어

comfilepi:improving_real-time_performance:index

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
comfilepi:improving_real-time_performance:index [2023/05/12 11:08]
COMFILE Technology [Setting the CPU Governor to ''performance'']
comfilepi:improving_real-time_performance:index [2024/03/13 15:42] (current)
COMFILE Technology [Schedule the Program as Real-Time with High Priority]
Line 30: Line 30:
 ===== Isolate a Program to One of the 4 CPU Cores ===== ===== Isolate a Program to One of the 4 CPU Cores =====
  
-First, add the ''​isolcpus=3'' ​kernel parameter ​to the ///​boot/​cmdline.txt//​ file.  This will prevent Linux from allocating the 4th core to any processes.  Reboot for the change to take effect. ​ See [[https://​www.kernel.org/​doc/​html/​latest/​admin-guide/​kernel-parameters.html?​highlight=isolcpus|the Linux kernel ​reference for isolcpus]] for more information.+First, add ''​isolcpus=nohz,​domain,​managed_irq,​3 irqaffinity=0-2 nohz_full=3 rcu_nocbs=3''​ to the ///​boot/​cmdline.txt//​ file.  This will prevent Linux from allocating the 4th core to most process, interrupt handlers, and kernel threads.  Reboot for the change to take effect. ​ See [[https://​www.kernel.org/​doc/​html/​latest/​admin-guide/​kernel-parameters.html|the Linux kernel ​documentation]] for more information ​on these kernel arguments.
  
 Then use ''​taskset''​ to have Linux run a program, in this case a program called //test//, on the 4th core.  The program will have that core all to itself. ​ See [[https://​www.man7.org/​linux/​man-pages/​man1/​taskset.1.html|the taskset man page]] for more information. Then use ''​taskset''​ to have Linux run a program, in this case a program called //test//, on the 4th core.  The program will have that core all to itself. ​ See [[https://​www.man7.org/​linux/​man-pages/​man1/​taskset.1.html|the taskset man page]] for more information.
Line 36: Line 36:
 sudo taskset -cp 3 `pidof test` sudo taskset -cp 3 `pidof test`
 </​code>​ </​code>​
 +
 +Core isolation can also be achieved using //​cpusets//​. ​ See the following resources for more information:​
 +  * [[https://​docs.kernel.org/​admin-guide/​cgroup-v1/​cpusets.html|Linux kernel documentation on cpusets]]
 +  * [[https://​github.com/​lpechacek/​cpuset/​blob/​master/​doc/​tutorial.txt|cpuset utility package documentation]]
  
 ===== Schedule the Program as Real-Time with High Priority ===== ===== Schedule the Program as Real-Time with High Priority =====
Line 42: Line 46:
  
 <​code>​ <​code>​
-sudo chrt -f -p `pidof test`+sudo chrt -f -p 99 `pidof test`
 </​code>​ </​code>​
  
Line 49: Line 53:
  
  
-===== Disable Realtime Throttling =====+===== Disable Realtime Throttling ​(Not Recommended) ​=====
  
-When using FIFO scheduling, it may help be necessary to disable [[https://​lwn.net/​Articles/​296419/​|realtime throttling]].+When using FIFO scheduling, it may be necessary to disable [[https://​lwn.net/​Articles/​296419/​|realtime throttling]].
  
 <​code>​ <​code>​
Line 57: Line 61:
 </​code>​ </​code>​
  
-However, this is a blunt instrument because it disables realtime throttling on all cores. ​ Instead it may be best to introduce a ''​sleep''​ along with a spin-wait for greater precision. ​ For more information,​ refer to [[https://​stackoverflow.com/​a/​75122036/​539972|this description]].+However, this is a blunt instrument because it disables realtime throttling on all cores. ​ Instead, to implement precise timing, ​it may be best to introduce a ''​sleep''​ along with a spin-wait for greater precision. ​ For more information,​ refer to [[https://​stackoverflow.com/​a/​75122036/​539972|this description]].
  
 ===== Demonstration,​ Measurement,​ and Results ===== ===== Demonstration,​ Measurement,​ and Results =====
Line 63: Line 67:
 ==== Test Program ==== ==== Test Program ====
  
-In this test, a program is created to output a 100µs alternating pulse (500kHz) on one of the ComfilePI'​s GPIO pins.  The program uses the [[http://www.airspayce.com/mikem/bcm2835/|BCM2835]] library.+In this test, a program is created to output a 100µs alternating pulse (500kHz) on one of the ComfilePI'​s GPIO pins.  The program uses the [[https://git.kernel.org/pub/scm/​libs/​libgpiod/​libgpiod.git/|gpiod]] library.
  
 <code C++> <code C++>
 +#include <​gpiod.h>​
 #include <​cstdio>​ #include <​cstdio>​
-#include <bcm2835.h>+#include <chrono>​ 
 +#include <unistd.h>
  
-#define PIN 4 +using namespace std::​chrono;​
-#define DELAY 100+
  
-int main()+void delay(uint32_t us)
 { {
-    ​if (!bcm2835_init()+    ​auto t1 = steady_clock::​now(); 
-    ​+    ​auto t2 = t1 + microseconds(us); 
-        printf("​bcm2835_init() failed\n"​); +    ​this_thread::​sleep_for(10us); 
-        exit(1); +    ​while (steady_clock::​now() < t2); 
-    }+}
  
-    bcm2835_gpio_fsel(PINBCM2835_GPIO_FSEL_OUTP);+int main(int argcchar **argv) 
 +
 +    const char *chipname = "​gpiochip0";​ 
 +    struct gpiod_chip *chip; 
 +    struct gpiod_line *line16;
  
-    ​for(;;)+    ​// Open GPIO16 
 +    chip = gpiod_chip_open_by_name(chipname); 
 +    line16 = gpiod_chip_get_line(chip,​ 16); 
 +    gpiod_line_request_output(line16,​ "​realtime",​ 0); 
 + 
 +    // Toggle GPIO every 100us 
 +    while (true)
     {     {
-        ​bcm2835_gpio_write(PINHIGH); +        ​gpiod_line_set_value(line16true); 
-        ​bcm2835_st_delay(bcm2835_st_read(),​ DELAY); +        ​delay(50); 
-        ​bcm2835_gpio_write(PINLOW); +        ​gpiod_line_set_value(line16false); 
-        ​bcm2835_st_delay(bcm2835_st_read(),​ DELAY);+        ​delay(50);
     }     }
 +
 +    // Release lines and chip
 +    gpiod_line_release(line16);​
 +    gpiod_chip_close(chip);​
 +
 +    return 0;
 } }
 </​code>​ </​code>​
Line 94: Line 115:
 Compile with... Compile with...
 <​code>​ <​code>​
-gcc -O3 test.cpp -lbcm2835 ​-o test+# Install the gpiod library if necessary 
 +# sudo apt install libgpiod-dev 
 + 
 +# Compile the program 
 +g++ -O3 test.cpp -lgpiod ​-o test
 </​code>​ </​code>​
  
Line 128: Line 153:
 {{ :​comfilepi:​improving_real-time_performance:​jitter.png?​900 }} {{ :​comfilepi:​improving_real-time_performance:​jitter.png?​900 }}
  
-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. ​ However, by taking a few measures to optimize a process for real-time performance,​ it is possible to reduce jitter potentially below 50µs which may be acceptable for some real-time use cases.+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. ​ However, by taking a few measures to optimize a process for real-time performance,​ it is possible to reduce jitter potentially below 50µs which may be acceptable for some real-time use cases.  Without the intentional disturbance introduced by running the web browser, the performance is better.
  
 Although the real-time kernel can reduce jitter, it trades off overall performance throughput. ​ Using the real-time kernel may cause the system to boot slower and run some applications with a mild performance penalty, but it will provide more deterministic behavior. Although the real-time kernel can reduce jitter, it trades off overall performance throughput. ​ Using the real-time kernel may cause the system to boot slower and run some applications with a mild performance penalty, but it will provide more deterministic behavior.
comfilepi/improving_real-time_performance/index.1683857307.txt.gz · Last modified: 2023/05/12 11:08 by COMFILE Technology