Search by Tags

Real-Time Linux

 

Article updated at 08 Oct 2021
Compare with Revision




Introduction

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.

Real-time Linux Solutions

There are multiple options for using Linux in a real-time, deterministic, low-latency application.

  • One option is to use Linux in-conjunction with a hypervisor or co-kernel. In these cases, the hypervisor or co-kernel has priority over the Linux kernel and is where the real-time tasks are performed.
  • Another similar option is to use a heterogeneous asymmetric multi-core system where Linux and a different deterministic kernel run independently on separate processor cores.
  • Finally, the popular strategy of improving the real-time capability of Linux by making it more preemptive may be used. This is most commonly achieved by applying a patch set known as PREEMPT_RT to the kernel.

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:

  • Heterogeneous asymmetric multi-core architecture allowing an RTOS such as FreeRTOS to execute in parallel with Linux on separate processor cores.
  • The PREEMPT_RT patch, you can build our kernels with PREEMPT_RT from our BSP Layers for Yocto Project.

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.

Toradex BSPs and Pre-built Images With the PREEMPTRT Patch

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.

Real-time Linux Webinar

Get an overview of different options and get see some test results.

  • Real-Time Patch, Real-Time Linux
  • Xenomai
  • Heterogeneous multicore processing with i.MX 7

Linux Real-time Capabilities

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:

  • CONFIG_PREEMPT_NONE: No Forced Preemption (Server)
  • CONFIG_PREEMPT_VOLUNTARY: Voluntary Kernel Preemption (Desktop)
  • CONFIG_PREEMPT: Preemptible Kernel (Low-Latency Desktop)

The PREEMPT_RT kernel (see below) adds additional options, notably:

  • PREEMPT_RT_FULL: Fully Preemptible Kernel (RT)

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

Scheduler and Priorities

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

Real-time Optimization

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

PREEMPTRT Patch Set

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.

OE-Core (Yocto Project) Kernel Recipe

i.MX6 & i.MX7 based modules

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"

i.MX8 based modules

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.

Linux Real-Time Tests

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.