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:44]
COMFILE Technology [Test Program]
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 ''​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?​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++>
Line 77: Line 81:
     auto t1 = steady_clock::​now();​     auto t1 = steady_clock::​now();​
     auto t2 = t1 + microseconds(us);​     auto t2 = t1 + microseconds(us);​
 +    this_thread::​sleep_for(10us);​
     while (steady_clock::​now() < t2);     while (steady_clock::​now() < t2);
 } }
Line 95: Line 100:
     {     {
         gpiod_line_set_value(line16,​ true);         gpiod_line_set_value(line16,​ true);
-        delay(100);+        delay(50);
         gpiod_line_set_value(line16,​ false);         gpiod_line_set_value(line16,​ false);
-        delay(100);+        delay(50);
     }     }
  
Line 148: 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.1683859471.txt.gz · Last modified: 2023/05/12 11:44 by COMFILE Technology