To achieve minimal latency and predictable performance essential for real-time applications, it is crucial to use a real-time optimized kernel. The PREEMPT_RT patch transforms the standard Linux kernel into a real-time kernel by enabling full preemption and reducing interrupt handling delays.
Ubuntu provides options to install low-latency or real-time kernels, typically available through Ubuntu Pro subscriptions. Alternatively, users can compile their own kernel with the PREEMPT_RT patch enabled.
sudo apt-get update
sudo apt-get install linux-image-rt-amd64
sudo reboot
After installation, verify the active kernel version to ensure the real-time kernel is in use.
Setting the CPU governor to 'performance' ensures that the CPU operates at its maximum frequency, reducing latency and enhancing responsiveness. This can be done using the cpufrequtils
package.
sudo apt-get install cpufrequtils
sudo cpufreq-set -g performance
Alternatively, you can set the governor by writing directly to the scaling governor file:
echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
Power-saving features like C-states and dynamic frequency scaling can introduce latency. Disabling these features can lead to more consistent performance.
Add the following parameters to the GRUB configuration to disable C-states:
sudo nano /etc/default/grub
Modify the GRUB_CMDLINE_LINUX_DEFAULT
line:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash intel_idle.max_cstate=0 processor.max_cstate=0 idle=poll"
sudo update-grub
sudo reboot
Disabling Turbo Boost can also be beneficial if consistent CPU performance is critical. This can typically be done through the BIOS/UEFI settings.
Isolating specific CPU cores for real-time tasks prevents other system processes from interfering, thereby reducing latency. This is achieved by adding the isolcpus
parameter to the GRUB configuration.
sudo nano /etc/default/grub
Add isolcpus=0-3
(adjust based on your CPU cores) to the GRUB_CMDLINE_LINUX_DEFAULT
line:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash isolcpus=0-3"
sudo update-grub
sudo reboot
Using sysctl
to modify kernel parameters can help in reducing latency and improving performance.
sudo sysctl -w vm.swappiness=10
sudo sysctl -w kernel.sched_rt_runtime_us=950000
These settings reduce the tendency to swap memory to disk and increase the runtime allocated to real-time tasks.
Optimizing interrupt request handling ensures that hardware interrupts do not interfere with real-time processes. Tools like irqbalance
can be configured or disabled to manually assign IRQs to specific CPU cores.
sudo apt-get install irqbalance
sudo irqbalance --oneshot
Alternatively, manually bind IRQs to less critical cores to minimize latency on real-time cores.
Reducing the number of active services frees up system resources for real-time applications. Review and disable non-essential services using systemd.
systemctl list-units --type=service
sudo systemctl disable <service-name>
Allocate specific CPU cores exclusively for real-time tasks to prevent contention. This can be done using the isolcpus
parameter as previously mentioned, and by setting CPU affinity for real-time processes.
Assigning real-time scheduling policies like SCHED_FIFO
or SCHED_RR
ensures that critical tasks receive appropriate CPU time.
sudo apt-get install real-time-scheduler
chrt -f 99 <command>
Hyper-threading can introduce unpredictability in CPU performance due to shared resources between threads. Disabling it can lead to more consistent real-time performance.
Disable Hyper-Threading through BIOS/UEFI settings. The exact steps vary depending on the motherboard manufacturer.
Using a lightweight desktop environment such as LXDE or XFCE reduces system resource consumption, leaving more CPU and memory available for real-time applications.
sudo apt-get install xubuntu-desktop
Choose the lightweight environment during login or set it as the default.
Tools like cyclictest
and RTLA
help in measuring system latency and identifying performance issues.
sudo apt-get install rt-tests
sudo cyclictest -l100000 -m -S -p 98
Utilize perf
, ftrace
, and latencytop
to profile and analyze system performance, helping to pinpoint and mitigate bottlenecks.
sudo apt-get install perf-tools-unstable
sudo perf record -a
sudo perf report
Ensure that your motherboard’s BIOS/UEFI firmware is up-to-date to benefit from performance enhancements and compatibility improvements.
Disable features that might interfere with real-time performance, such as Intel Turbo Boost, through the BIOS/UEFI settings.
Depending on your application, employing real-time middleware like Xenomai or other specialized frameworks can provide additional guarantees and APIs for handling real-time tasks efficiently.
Real-time performance optimization is an ongoing process. Regularly test system changes under realistic conditions, monitor performance, and make iterative adjustments to achieve the desired results.
Maximizing real-time performance on an Ubuntu system with an Intel CPU involves a multifaceted approach. By implementing a real-time kernel, optimizing CPU and power settings, fine-tuning system parameters, and carefully managing CPU affinity and priority, you can significantly enhance system responsiveness and predictability. Additionally, minimizing system overhead through lightweight desktop environments, disabling hyper-threading, and leveraging specialized middleware further contribute to achieving optimal real-time performance. Continuous monitoring and iterative testing ensure that the system remains stable and performs efficiently under real-world conditions.