In today’s rapidly advancing digital age, the demand for real-time systems is becoming increasingly prominent. Whether for robotic control, audio development, or CNC applications, real-time performance is key to successfully implementing these functions. As a highly flexible and customizable operating system, Linux’s real-time capabilities are continuously improving. This article will delve into the implementation of Linux real-time performance, particularly how to set it up using PREEMPT_RT and Docker on Ubuntu.
Basic Concepts: What is a Real-Time System?
A real-time system is one that must complete tasks within strict time constraints. Unlike ordinary operating systems, real-time systems need to ensure that tasks are executed within a predetermined time to avoid delays in critical events. Typical real-time applications include industrial automation, robotics, and multimedia processing.
In Linux, there are several methods to achieve real-time functionality, among which PREEMPT_RT is a widely used and supported solution. This patch significantly reduces system scheduling latency, making Linux more suitable for handling real-time tasks.
Real-Time Kernel Setup: Installation and Configuration
To set up a real-time system on Ubuntu, the first step is to install a real-time kernel. As of February 2023, Ubuntu 22.04 offers a real-time kernel that can be installed with a simple command. This is distributed through Ubuntu Pro, and users can register for free for up to five personal devices.
The process of installing the real-time kernel includes the following steps:
- 1. Log in to Ubuntu Pro, register, and obtain installation permissions for the real-time kernel.
- 2. Install the real-time kernel via terminal commands; specific steps can be referenced in doc/PreemptRt.md.
Once the kernel is installed, users need to ensure that Docker containers can run with real-time priority. This requires adding the necessary permission configurations when starting Docker.
Docker Container Configuration: Real-Time Priority
Docker shares the host kernel, so as long as the host supports real-time functionality, Docker containers can effectively run real-time code. When starting a Docker container, the following parameters need to be set:
cap_add:
-SYS_NICE
ulimits:
rtprio:99
rttime:-1 # Indicates "unlimited"
memlock:8428281856
network_mode: host
With this configuration, processes within the container can set real-time priorities through system calls, effectively handling real-time tasks.
Optimizing Real-Time Systems: Reducing Latency
To achieve optimal real-time performance, in addition to the basic kernel and container installation, users also need to perform a series of optimizations. For example, the cyclictest tool can be used to evaluate the system’s real-time performance.
The command to run cyclictest is as follows:
$ docker-compose -f docker/docker-compose.yml up
Then run the following command in the <span>benchmark/</span> folder to generate a latency histogram:
$ ./mklatencyplot.bash
This chart will show the difference between the thread scheduling wake-up time and the actual wake-up time, further analyzing the system’s real-time performance under different loads.
Compatibility of Nvidia Drivers with PREEMPT_RT
In certain real-time applications, users may need to use Nvidia graphics cards simultaneously. To ensure that the Nvidia driver runs on PREEMPT_RT, specific settings for the driver are required. Detailed implementation specifics can be referenced in the documentation, but it is important to note that some drivers may have compatibility issues with the real-time kernel, so debugging preparations should be made.
Conclusion
The real-time capabilities of Linux provide a solid foundation for the development of various real-time applications. By utilizing the PREEMPT_RT patch and Docker technology, users can easily create a reliable, low-latency real-time system. Whether for robotic control, audio programming, or CNC applications, mastering these skills will enable developers to thrive in the world of real-time systems.
Project Address: https://github.com/2b-t/linux-realtime