By Toradex胡珊逢

 

Real-time Linux 是指在普通 Linux 内核打上 PREEMPT_RT补丁后使内核满足实时要求。下面我们将使用 Apalis iMX8QM 介绍如何开启 Linux 5.4 的实时功能。

 

首先需要下载适用于 Apalis iMX8QM 模块的最新 Linux 内核源码。

 

git clone -b toradex_5.4-2.1.x-imx     git://git.toradex.cn/linux-toradex.git

 

 

在源码根目录下打开 Makefile 文件查看内核版本,这里我们看到是5.4.47

 

$ head Makefile

# SPDX-License-Identifier: GPL-2.0

VERSION = 5

PATCHLEVEL = 4

SUBLEVEL = 47

EXTRAVERSION =

NAME = Kleptomaniac Octopus

 

 

下载对应版本的 PREEMPT_RT补丁,解压后放到源码根目录下。

https://cdn.kernel.org/pub/linux/kernel/projects/rt/5.4/older/patch-5.4.47-rt28.patch.gz

 

执行下面命令为 5.4.47 内核打上补丁。

 

$ patch -p1     < patch-5.4.47-rt28.patch

 

 

根据这里的说明完成内核以及驱动模块编译。

 

$ make     defconfig

$ make     menuconfig

 

 

开启 > General setup > [*] Configure standard kernel features

 > General setup > reemption Model 中选择 Fully Preemptible Kernel (Real-Time)

image001.png

为了获得更好的实时性能,我们将关闭显示功能以及部分音频功能。在 > Device Drivers > Graphics support 中取消相关显示接口。

image002.png

 

 > Device Drivers > Sound card support > Advanced Linux Sound Architecture > ALSA for SoC audio support > SoC Audio for Freescale CPUs 中取消 AUDIO TO HDMI TX module support  XCVR 支持。

image003.png

 

保存配置后退出,开始编译内核。

 

$ make     Image.gz 2>&1 | tee build.log

$ make -j3     modules

$ mkdir     modules

$ export     INSTALL_MOD_PATH=modules

$ make     modules_install

 

 

 arch/arm64/boot  modules/lib/modules 目录分别是内核文件 Image.gz 和驱动模块。将其复制到 Apalis iMX8QM 模块的 /media/mmcblk0p1  /lib/modules/。然后执行重启命令。

 

root@apalis-imx8:~# sync

root@apalis-imx8:~# reboot

 

 

启动后可以查看目前运行的内核配置情况。

 

root@apalis-imx8:~#     zcat /proc/config.gz |grep PREEMPT                                                                                                                        CONFIG_HAVE_PREEMPT_LAZY=y                                                                                                                                                    CONFIG_PREEMPT_LAZY=y                                                                                                                                                         #     CONFIG_PREEMPT_NONE is not set                                                                                                                                              # CONFIG_PREEMPT_VOLUNTARY is not set                                                                                                                                         # CONFIG_PREEMPT is not set                                                                                                                                                   CONFIG_PREEMPT_RT=y                                                                                                                                                           CONFIG_PREEMPT_COUNT=y                                                                                                                                                    CONFIG_PREEMPTION=y                                                                                                                                                           CONFIG_PREEMPT_RCU=y                                                                                                                                                          CONFIG_PREEMPT_NOTIFIERS=y

 

 

 Apalis iMX8QM 上的两个 A72  四个 A53 核心的时钟都设置到最高。

 

root@apalis-imx8:~# echo performance >     /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor

root@apalis-imx8:~# echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

root@apalis-imx8:~# cpufreq-info |grep current

      current policy: frequency should be within 600 MHz and 1.20 GHz.

      current CPU frequency is 1.20 GHz (asserted by call to hardware).

      current policy: frequency should be within 600 MHz and 1.20 GHz.

      current CPU frequency is 1.20 GHz (asserted by call to hardware).

      current policy: frequency should be within 600 MHz and 1.20 GHz.

      current CPU frequency is 1.20 GHz (asserted by call to hardware).

  current     policy: frequency should be within 600 MHz and 1.20 GHz.

      current CPU frequency is 1.20 GHz (asserted by call to hardware).

      current policy: frequency should be within 600 MHz and 1.60 GHz.

      current CPU frequency is 1.60 GHz (asserted by call to hardware).

      current policy: frequency should be within 600 MHz and 1.60 GHz.

      current CPU frequency is 1.60 GHz (asserted by call to hardware).

 

 

执行 cyclictest 工具查看内核实时性能。

 

root@apalis-imx8:~# cyclictest -n m -S -p 99     --policy=fifo -i 431 -H 300

# /dev/cpu_dma_latency set to 0us

policy: fifo: loadavg: 0.42 0.31 0.13 2/399     834         

 

T: 0 (      829) P:99 I:431 C:  59963     Min:      6 Act:    8 Avg:    9 Max:      24

T: 1 (      830) P:99 I:431 C:  59962     Min:      5 Act:    5 Avg:    6 Max:      26

T: 2 (      831) P:99 I:431 C:  59962     Min:      5 Act:    7 Avg:    6 Max:      20

T: 3 (      832) P:99 I:431 C:  59962     Min:      5 Act:    6 Avg:    6 Max:      21

T: 4 (      833) P:99 I:431 C:  59962     Min:      4 Act:    5 Avg:    6 Max:      20

T: 3 (      832) P:99 I:431 C:  60517     Min:      5 Act:    7 Avg:    6 Max:      21

T: 4 (      833) P:99 I:431 C:  60517     Min:      4 Act:    7 Avg:    6 Max:      17

T: 5 (      834) P:99 I:431 C:  60517     Min:      3 Act:    5 Avg:    4 Max:      12

 

下面链接是我们提供的包含实时内核的 BSP,可用于评估和测试。

ftp://ftp.toradex.cn/Documents/ApplicationNote/resources/RT-Linux/Apalis-iMX8_Reference-Minimal-Image-rt-Tezi_5.0.0-devel-20201028085956+build.0.zip

 

 

总结

使用 Toradex Linux BSP 可以很方便集成 PREEMPT-RT 补丁,实现 real-time Linux。为了获得更好的实时性能,除了调整 Linux 内核配置,还需要关注文件系统,通常一个精简的文件系统,由于加载的应用软件或者驱动较少,可以避免占用 CPU 资源。在类似 Apalis iMX8 这种异构多核架构的处理器上会有额外的 Cortex-M4 核心,其可以运行 FreeRTOS提供硬实时能力。具体使用方法可以参考文末链接。

 

 

 

 

 

 

 

 

参考:

https://developer.toradex.cn/knowledge-base/real-time-linux

https://blog.savoirfairelinux.com/en-ca/2020/linux-for-real-time-systems-evaluation-and-security-considerations/

https://www.toradex.cn/blog/nxp-imx8-m4he-xinfreertos-rpmsg-ying-yong-shi-li