In this article, you can learn more about real-time solutions on Linux. Most of the focus beyond the introduction is on the soft real-time PREEMPT_RT patch.
There are multiple options for using Linux in a real-time, deterministic, low-latency application.
There are advantages & disadvantages to all of these approaches. Toradex does not provide official support for those methods except in experimental state for some cases, but you can make them work:
Paid support for these alternative solutions may be available from companies in the Toradex partner network. Additionally, information about the real-time capabilities of Linux itself, including the PREEMPT_RT patch set, is provided below.
The BSP Layers and Reference Images for Yocto Project and TorizonCore distributions with the PREEMPT_RT patch, as well as their pre-built images available on our Toradex Easy Installer online feeds or for offline installation, haven't been fully validated by Toradex as of October, 2021. We have only run smoke tests and cyclictest
on TorizonCore to make sure that the system boots properly and the PREEMPT_RT patches are working under stressful conditions. You can find the tests, including the stress conditions, on our GitHub repository rt-validation.
Any tuning, additional kernel configuration, and validation of the RT performance for your own use case must be done at your discretion. The contents provided in this article are guidelines to help you and it doesn't mean that any specific use case or features are supported or validated by Toradex at the moment. Toradex only releases PREEMPT_RT BSPs and images in an experimental state. We don't guarantee any functionality and bug fixes.
If you have questions or want to share your use case with us, so we can have a more accurate understanding of customers' needs, please contact us.
Get an overview of different options and get see some test results.
Linux has been built as a general purpose operating system and therefore is, by design, not well optimized for real-time operations. However, since desktop applications such as audio or video playback require a certain real-time capability, the standard kernel today provides rather impressive (soft) real-time capabilities. The main knob to improve the kernels latency is the Preemption Model (available in the submenu Kernel Features of the kernel configuration). A modern vanilla Linux kernel offers three preemption options:
The PREEMPT_RT kernel (see below) adds additional options, notably:
You can use /proc/config.gz to access the kernel configuration of the running kernel:
root@vybridvf61-v12:~# zcat /proc/config.gz | grep PREEMPT # CONFIG_PREEMPT_NONE is not set CONFIG_PREEMPT_VOLUNTARY=y # CONFIG_PREEMPT is not set
To provide real-time capabilities to a process or thread the priority and scheduler options need to be set. From within an application the standard Linux system call sched_setscheduler can be used. For a code example refer to the Hello World example on the Real-Time Linux Wiki at kernel.org. The utility chrt (part of util-linux) can be used to configure a process' priority and scheduler after start directly from the shell.
The command ps can be used to verify a process'/thread's current priority and scheduler settings:
root@vybridvf61-v12:~# ps -eLfc | grep cyclictest root 3825 3816 3825 2 TS 19 21:47 pts/0 00:00:00 cyclictest -n -p 60 root 3825 3816 3826 2 FF 100 21:47 pts/0 00:00:00 cyclictest -n -p 60 root 3828 439 3828 1 TS 19 21:47 ttyLP0 00:00:00 grep cyclictest
One of the contributors to latency in the Voluntary Preemption and Preemptible Kernel models are console messages written to a serial console. Serial console messages may be minimized by using the kernel argument quiet. To suppress messages after boot, use the following line to only enable emergency messages:
echo 1 > /proc/sys/kernel/printk
The configuration file /etc/sysctl.conf provides a means of configuring the log level applied automatically after boot:
kernel.printk = 1 4 1 7
Disabling memory overcommiting to avoid latency generated by the Out-of-Memory Killer is a worthwhile option too:
echo 2 > /proc/sys/vm/overcommit_memory
Using the configuration file /etc/sysctl.conf:
vm.overcommit_memory = 2
See also: http://linuxrealtime.org/index.php/Basic_Linux_from_a_Real-Time_Perspective#Memory_Overcommit
General information regarding PREEMPT_RT may be found at the Real-Time Linux Wiki at kernel.org. The PREEMPT_RT kernel patches can be downloaded directly from kernel.org.
Note: The mainline PREEMPT_RT patches are specifically written for the vanilla mainline Linux kernel. The Toradex BSPs may provide drivers which are not part of the vanilla Linux kernel and therefore do not get patched by the PREEMPT_RT patch set.
To apply these patches to the kernel, follow the directions to Build U-Boot and Linux Kernel from Source Code. After cloning the appropriate kernel branch, the real-time patches may be applied. Be sure to use the patches for the nearest kernel version.
Note: In most cases the patches will fail to apply cleanly. As such, it may be necessary to fix patch errors by manually resolving discrepancies between the patches and the source code.
After applying the patches and executing "make {defconfig target}", the kernel config must be modified to enable CONFIG_PREEMPT_RT. "make menuconfig" or "make nconfig" can be used to make these edits to the kernel configuration. Also ensure that the high resolution timer is enabled. Also note that some configurations and drivers may adversely affect real-time performance. For more general information about patch application and kernel configuration, see the "How-To" at the Real-Time Linux Wiki at linuxfoundation.org.
Toradex provides a real-time kernel recipe for the i.MX6 & i.MX7 for use in an OpenEmbedded-Core (Yocto Project) build. The linux-toradex-rt recipe may be found in the meta-toradex-nxp layer.
To select the real-time kernel as the preferred kernel in a build, set the preferred provider variable for virtual/kernel in an appropriate configuration file (e.g. conf/local.conf
):
PREFERRED_PROVIDER_virtual/kernel = "linux-toradex-rt"
Starting with the 3.0 BSP Toradex has own distro in meta-toradex-distro layer. To use real-time kernel, set the distro for i.MX8 in configuration file (e.g. conf/local.conf
):
DISTRO_mx8 = "tdx-xwayland-rt"
Note: It may be advantageous to disable display/graphics related drivers - namely FB, DRM & Vivante drivers. Power management or CPU governing may also present issues.
To test the real-time capabilities on Linux, the open-source tool cyclictest is widely adopted. You are encouraged to read the tool documentation and understand how to make measurements for your own use case.
The GitHub repository rt-validation provides instructions on how to run cyclictest
together with stress tests on TorizonCore. You may use it as a starting point.
At the moment, we don't provide cyclictest
results, since those values are only meaningful for a specific use case. In addition, the PREEMPT_RT patch support is experimental, and omitting those values raises awareness that customers who decide to use it must also take ownership in ensuring the proper system configuration, reliability, and latency results for their use case.