> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs-staging.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 了解实时 Linux 内核

## **实时（RT）内核**

实时系统是一种确定性系统，要求在设定的时间内对事件作出响应。

满足以下条件的系统被归类为兼容 RT：

* 不存在无界延迟。
* 能够精确计算最大响应时间。
* 满足任务调度的既定标准（延迟和截止时间）。

Linux 可以配置为实时操作系统（RTOS），其中实时任务具有明确定义的周期性执行周期（周期时间），并在规定的范围（抖动）内满足执行标准。

要安装补丁，请参阅 [PREEMPT\_RT 补丁版本](https://wiki.linuxfoundation.org/realtime/start)。

> [!NOTE]
>
> 实时支持适用于内核空间进程，不适用于用户空间。

> [!NOTE]
>
> 本节不适用于 QCS5430。

![图：构建顺序](https://mintcdn.com/qualcomm-staging/3pCxfvUGXlOT6fV1/Suites/Industrial-Edge-Suite/media/k2l-performance/RT-kernel-build-sequence.svg?fit=max\&auto=format\&n=3pCxfvUGXlOT6fV1\&q=85\&s=b45be73d67206c7462b94dea92986fba)

图：构建顺序

### **设置工作区**

Qualcomm Linux 内核支持 LTS RT 内核（6.18.x），该内核通过 `meta-qcom` 层中 `recipes-kernel/linux/linux-qcom-rt_6.18.bb` 文件内的 Yocto 配方进行维护。

有关克隆工作区以及获取所有 Qualcomm Linux 元层以使用 Qualcomm RT Linux 的更多信息，请参阅 [Sync](https://docs.qualcomm.com/doc/80-80023-254/topic/build_from_source_github_intro.html#sync)。

### **启用 RT 内核**

Qualcomm Linux `meta-qcom` 层支持 `linux-qcom-rt_6.18.bb` 配方，该配方默认为受支持的机器获取并构建 Qualcomm Linux 内核。

`meta-qcom` 层在现有层的基础上应用更改。在内核构建期间，`meta-qcom` 层会根据内核版本使用 `rt.config` 启用 `PREEMPT_RT`，并允许实时配置。

> [!NOTE]
>
> * 对于 QLI.2.0，请使用 `linux-qcom-rt_6.18.bb`。
> * 对于 `qcom-next`，请使用 `linux-qcom-next-rt_git.bb`。

有关受支持机器的更多信息，请参阅 [Build critical release tags](https://docs.qualcomm.com/doc/80-80023-300/topic/build_critical_release_tags.html)。

### **自定义 RT 内核**

* 如果您对 RT 内核有任何更改，请按如下方式在配方中维护这些更改：
  * 将补丁文件维护在 `recipes-kernel/linux/linux-qcom-6.18/<your_patch_file>.patch` 下
  * 在 `recipes-kernel/linux/linux-qcom-rt_6.18.bb` 文件中将补丁文件追加到 `SRC_URI`。

```text theme={null}
SRC_URI += " \
    file://<your_patch_file>.patch \
    "
```

* 要在 RT 内核上应用任何外部配置：
  * 将配置文件维护在 `recipes-kernel/linux/linux-qcom-6.18/configs/<your_config>.cfg` 配方中。
  * 在 `recipes-kernel/linux/linux-qcom-rt_6.18.bb` 文件中将配置文件追加到 `SRC_URI`。

```text theme={null}
SRC_URI += " \
    file://configs/qcom_rt.cfg \
"
```

* 要修改内核命令行，请将命令行参数添加到 `meta-qcom/ci/base.yml` 文件中的 `KERNEL_CMDLINE_EXTRA` 变量。

```text theme={null}
KERNEL_CMDLINE_EXTRA:append = " qcom_scm.download_mode=1 <new_parameter>"
```

### **为 RT 内核配置内核设置**

RT 内核中使用可选和必需的内核配置。

要在 RT 内核中启用完全抢占，请使用 `CONFIG_PREEMPT_RT`。

`CONFIG_PREEMPT_RT` 标志默认已启用，它是 `linux-qcom-rt_6.18.bb` 配方所用 `rt.config` 的一部分。

以下示例展示了内核配置：

```text theme={null}
zcat proc/config.gz | grep CONFIG_PREEMPT
# CONFIG_PREEMPT_NONE is not set
# CONFIG_PREEMPT_VOLUNTARY is not set
# CONFIG_PREEMPT is not set
CONFIG_PREEMPT_RT=y
```

在为 RT 任务设置亲和性时，请设置以下内核配置选项：

* `CONFIG_NO_HZ_COMMON` - 启用后，会为无时钟节拍（tickless）运行配置内核基础设施。
* `CONFIG_NO_HZ_FULL` - 启用后，会将内核配置为避免向只有单个可运行任务的 CPU 发送调度时钟中断。
* `CONFIG_CPUSETS` - 使用 `CONFIG_CPUSETS` 配置选项启用 `cpuset`，将 CPU 分组形成一个集合。

![图：RT 内核验证](https://mintcdn.com/qualcomm-staging/3pCxfvUGXlOT6fV1/Suites/Industrial-Edge-Suite/media/k2l-performance/compile-RT-kernel.svg?fit=max\&auto=format\&n=3pCxfvUGXlOT6fV1\&q=85\&s=a65ec124e6e67a99e1f54d1076eac7c1)

图：RT 内核验证

### **构建 RT 内核**

要构建 RT 内核，请运行以下命令：

1. 要确保您处于 KAS shell 中，请运行以下命令：

```text theme={null}
kas shell meta-qcom/ci/qcs6490-rb3gen2-core-kit.yml:meta-qcom/ci/linux-qcom-rt-6.18.yml:meta-qcom/ci/qcom-distro-kvm.yml:meta-qcom/ci/lock.yml
```

> [!NOTE]
>
> 对于 QLI 2.0，`qcom-distro-kvm.yml` 配置仅支持 KVM。RT 内核（`PREEMPT_RT`）与虚拟机监控程序无关。出于以下原因，KVM 被选为 QLI 2.0 的默认虚拟机监控程序：
>
> * **上游社区支持** — KVM 及其驱动程序是主线 Linux 内核的一部分，有利于更广泛的生态系统贡献和长期支持。
> * **标准 VirtIO 支持** — KVM 支持所有上游 VirtIO 设备，并与 QEMU 等标准 VMM 兼容。
> * **开放开发** — 拥有活跃的上游社区，开发者对其更为熟悉。

kas 构建命令会自动将 `xbl_config_kvm.elf` 包含在生成的 `qcomflash` 包中，无需手动配置 UEFI BDS。

> [!NOTE]
>
> 将 kas 锁文件从 meta-qcom-releases 复制到 meta-qcom，请参阅 [Build a BSP image](https://docs.qualcomm.com/doc/80-80023-254/topic/build_from_source_github_intro.html#build-a-bsp-image)。

2. 要编译 BitBake Qualcomm Linux 多媒体镜像，请运行以下命令：

```text theme={null}
bitbake qcom-multimedia-image
```

### **调优 RT 内核**

调优 RT 内核，以使设备中的 RT 任务获得确定性延迟。

将运行 RT 任务的 CPU 核心设置为以最高工作频率运行，同时防止对 CPU 频率进行热缓解。例如，在空闲睡眠场景中，由于 CPU 唤醒时间延迟，RT 任务会面临调度延迟。

> [!NOTE]
>
> 启动 RT 测试（cyclictest）时，[URM](./features-impacting-performance#understand-the-userspace-resource-manager) 会自动应用这些优化。
>
> > 正常运行情况下**无需**手动配置。
> > 以下步骤仅供参考、调试或自定义设置使用。
>
> 要在运行测试之前为 QCS6490、Qualcomm Dragonwing<sup>™</sup> IQ-9075 和 Qualcomm Dragonwing<sup>™</sup> IQ-615 Development Kit 配置系统，请执行以下操作：
>
> 1. 禁用定时器迁移：
>    ```text theme={null}
>    echo 0 > /proc/sys/kernel/timer_migration
>    ```
> 2. 将 `/sys/devices/virtual/workqueue/*` 中的所有内核工作队列绑定到内务处理（housekeeping）CPU：
>    ```text theme={null}
>    for wq in /sys/devices/virtual/workqueue/*; do
>       [ -w "$wq/cpumask" ] && echo 7F > "$wq/cpumask"
>    done
>    # 7F = CPUs 0-6 (binary 0111 1111)
>    ```
> 3. 将 CPU 频率调节器设置为 performance：
>    ```text theme={null}
>    for policy in /sys/devices/system/cpu/cpufreq/policy*; do
>       [ -w "$policy/scaling_governor" ] && echo performance > "$policy/scaling_governor"
>    done
>    ```
> 4. 禁用 RT 统计/限流：
>    ```text theme={null}
>    echo -1 > /proc/sys/kernel/sched_rt_runtime_us
>    ```
> 5. 将 IRQ 亲和性设置为内务处理 CPU：
>    ```text theme={null}
>    ALLOW_CPUS="0,1,2,3,4,5,6"
>    cpu_list_to_mask() {
>       MASK=0
>       for cpu in $(echo $1 | tr ',' ' '); do
>          MASK=$((MASK | (1 << cpu)))
>       done
>       printf "%x\n" "$MASK"
>    }
>    MASK=$(cpu_list_to_mask "$ALLOW_CPUS")
>    echo "Setting IRQ affinity to CPUs: $ALLOW_CPUS (mask=0x$MASK)"
>    for irq in /proc/irq/[0-9]*; do
>       smp_file="$irq/smp_affinity"
>       [ -w "$smp_file" ] && echo "$MASK" > "$smp_file" 2>/dev/null
>    done
>    ```
>
> 要在运行测试之前为 Dragonwing<sup>™</sup> IQ-8275 Development Kit 配置系统，请执行以下操作：
>
> 1. 禁用定时器迁移：
>    ```text theme={null}
>    echo 0 > /proc/sys/kernel/timer_migration
>    ```
> 2. 将 `/sys/devices/virtual/workqueue/*` 中的所有内核工作队列绑定到内务处理 CPU：
>    ```text theme={null}
>    for wq in /sys/devices/virtual/workqueue/*; do
>       [ -w "$wq/cpumask" ] && echo F7 > "$wq/cpumask"
>    done
>    # F7 = CPUs 0–2,4-7 (binary 1111 0111)
>    ```
> 3. 将 CPU 频率调节器设置为 performance：
>    ```text theme={null}
>    for policy in /sys/devices/system/cpu/cpufreq/policy*; do
>       [ -w "$policy/scaling_governor" ] && echo performance > "$policy/scaling_governor"
>    done
>    ```
> 4. 禁用 RT 统计/限流：
>    ```text theme={null}
>    echo -1 > /proc/sys/kernel/sched_rt_runtime_us
>    ```
> 5. 将 IRQ 亲和性设置为内务处理 CPU：
>    ```text theme={null}
>    ALLOW_CPUS="0,1,2,4,5,6,7"
>    cpu_list_to_mask() {
>       MASK=0
>       for cpu in $(echo $1 | tr ',' ' '); do
>          MASK=$((MASK | (1 << cpu)))
>       done
>       printf "%x\n" "$MASK"
>    }
>    MASK=$(cpu_list_to_mask "$ALLOW_CPUS")
>    echo "Setting IRQ affinity to CPUs: $ALLOW_CPUS (mask=0x$MASK)"
>    for irq in /proc/irq/[0-9]*; do
>       smp_file="$irq/smp_affinity"
>       [ -w "$smp_file" ] && echo "$MASK" > "$smp_file" 2>/dev/null
>    done
>    ```
>
> 以下示例展示了如何在 `meta-qcom/conf/machine/<machine-name.conf>` 中添加内核命令行参数以禁用 RCU 回调（**rcu\_nocbs**）：
>
> * CPU 核心 7
> * IRQ 绑定到核心 0-6
> * RCU 无回调核心 7
>
> ```text theme={null}
> QCOM_RT_CPU        = "7"
> QCOM_IRQAFF        = "0-6"
> QCOM_RCU_NOCBS     = "7"
> QCOM_RCU_EXPEDITED = "1"
> QCOM_CPUIDLE_OFF   = "1"
> ```

### **测试 RT 内核**

Linux 基金会 RT 测试套件中提供了一套测试。RT Linux 内核测试可获取以下信息：

* RT Linux 内核的实时性能
* RT Linux 内核延迟和关键性能指标（KPI）

> [!NOTE]
>
> RT Linux 内核测试会运行超过 24 小时，测试期间请勿重启系统。

Cyclictest 工具用于对 RT Linux 内核系统进行基准测试，用于评估实时系统的相对性能。Qualcomm Linux 构建中包含 cyclictest 工具。有关更多信息，请参阅 [Cyclictest](https://wiki.linuxfoundation.org/realtime/documentation/howto/tools/cyclictest/start)。

本指南介绍以下 cyclictest：

* 无负载 Cyclictest：执行此测试时不添加系统负载。
* 带 stress-ng（next-generation）的 Cyclictest：执行此测试时添加特定百分比的负载，以测量最坏情况下的系统延迟。

1. 在 QCS6490、Qualcomm Dragonwing<sup>™</sup> IQ-9075 和 Qualcomm Dragonwing<sup>™</sup> IQ-615 开发套件上运行以下 cyclic 测试：
   ```text theme={null}
   cyclictest -a 7 -t 1 -m -l 100000000 -i 1000 -p 99 -h 100
   # -a 7 → pin threads to CPU 7 (RT cores)
   # -t 1 → 1 threads
   # -m → lock memory (avoid page faults)
   # -l 100000000 → long run
   # -i 1000 → 1 ms interval
   # -p 99 → RT priority
   # -h 100 → histogram up to 100 µs
   ```
2. 在 Dragonwing<sup>™</sup> IQ-8275 Development Kit 上运行以下 cyclic 测试：
   ```text theme={null}
   cyclictest -a 3 -t 1 -m -l 100000000 -i 1000 -p 99 -h 100
   ```

要访问 RT 测试套件源代码，请参阅 [rt-tests/rt-tests.git](https://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git/)。有关更多信息，请参阅 [RT-Tests](https://wiki.linuxfoundation.org/realtime/documentation/howto/tools/rt-tests)。

3. 记录延迟。

要在 QCS6490、Qualcomm Dragonwing IQ-9075 和 Qualcomm Dragonwing<sup>™</sup> IQ-615 上运行带 stress-ng 的 cyclic 测试，请执行以下操作：

1. 设置目标 CPU 负载。定义所需的 CPU 负载百分比。如果未指定，默认负载为 60%：
   ```text theme={null}
   LOAD=60
   ```
2. 在后台于选定的 CPU 核心上运行 stress-ng。使用 stress-ng 在核心 0、1、2、4、5、6 和 7 上施加 CPU 负载。每个实例运行一天，并绑定到特定的 CPU 核心：
   ```text theme={null}
   for cpu in 0 1 2 3 4 5 6; do
       taskset -c $cpu stress-ng --cpu 1 --cpu-load "$LOAD" \
       --temp-path . -t 1d &
   done
   ```
3. 运行 cyclic 延迟测试。以高优先级执行 cyclictest 以测量调度延迟。测试运行约 27.78 小时：
   ```text theme={null}
   cyclictest -a 7 -t 1 -m -l 100000000 -i 1000 -p 99 -h 100 --mainaffinity 6
   ```
4. 记录最坏情况延迟。

要在 Dragonwing IQ-8275 上运行带 stress-ng 的 cyclic 测试，请执行以下操作：

1. 设置目标 CPU 负载：
   ```text theme={null}
   LOAD=60
   ```
2. 在选定的 CPU 核心上运行 stress-ng：
   ```text theme={null}
   for cpu in 0 1 2 4 5 6 7; do
        taskset -c $cpu stress-ng --cpu 1 --cpu-load "$LOAD" \
        --temp-path . -t 1d &
   done
   ```
3. 运行 cyclic 延迟测试：
   ```text theme={null}
   cyclictest -a 3 -t 1 -m -l 100000000 -i 1000 -p 99 -h 100 --mainaffinity 2
   ```
4. 记录最坏情况延迟。

### **RT 测试结果**

| **设备** | **发行版和镜像** | **Cyclictest 用例** | **结果** | **备注** |
| - | - | - | - | - |
| QCS6490 RT 核心：最佳核心 - 7 启动流程 - KVM | `performance_linux-qcom-rt-6.18_qcom-distro-kvm and qcom-multimedia-image` | 无负载 | `T: 0 ( 1687) P:99 I:1000 C:100000000 Min: 1 Act: 2 Avg: 1 Max: 5` | 已完成；延迟符合要求 \< 20 µs |
| QCS6490 RT 核心：最佳核心 - 7 启动流程 - KVM | `performance_linux-qcom-rt-6.18_qcom-distro-kvm and qcom-multimedia-image` | Stress-NG | `T: 0 ( 1853) P:99 I:1000 C:100000000 Min: 1 Act: 2 Avg: 1 Max: 10` | 已完成；延迟符合要求 \< 20 µs |
| Dragonwing IQ-615 RT 核心：最佳核心 - 7 启动流程 - KVM | `performance_linux-qcom-rt-6.18_qcom-distro-kvm and qcom-multimedia-image` | 无负载 | `T: 0 (23693) P:99 I:1000 C: 100000000 Min: 1 Act: 2 Avg: 2 Max: 9` | 已完成；延迟符合要求 \< 20 µs |
| Dragonwing IQ-615 RT 核心：最佳核心 - 7 启动流程 - KVM | `performance_linux-qcom-rt-6.18_qcom-distro-kvm and qcom-multimedia-image` | Stress-NG | `T: 0 (1590198) P:99 I:1000 C:100000000 Min: 1 Act: 2 Avg: 2 Max: 7` | 已完成；延迟符合要求 \< 20 µs |

## **各平台 RT 基准测试**

> [card] [RT 基准测试 IQX](https://dragonwingdocs-staging.qualcomm.com/zh/Suites/Industrial-Edge-Suite/rt-benchmarks-iqx.md)
>
> IQ-X 系列高性能平台的基准测试结果

> [card] [RT 基准测试 IQ9](https://dragonwingdocs-staging.qualcomm.com/zh/Suites/Industrial-Edge-Suite/rt-benchmarks-iq9.md)
>
> Dragonwing IQ-9075 平台的基准测试结果

> [card] [RT 基准测试 IQ8](https://dragonwingdocs-staging.qualcomm.com/zh/Suites/Industrial-Edge-Suite/rt-benchmarks-iq8.md)
>
> Dragonwing IQ-8275 平台的基准测试结果

> [card] [RT 基准测试 IQ6](https://dragonwingdocs-staging.qualcomm.com/zh/Suites/Industrial-Edge-Suite/rt-benchmarks-iq6.md)
>
> Dragonwing IQ-615 平台的基准测试结果
