> ## 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.

# 参考文件 — RTC Testbench

## **参考文件**

RTC Testbench 附带的三个文件的完整源码。这些文件由 `meta-rtc` 层安装到 `/usr/share/rtc-testbench/tests/common/` 和 `/etc/`。

***

## `postboot.sh`

每次启动后、运行 `ptp.sh` 之前运行一次。应用所有无法在 initscripts 阶段设置的 RT 调优。

**安装路径**：`/usr/share/rtc-testbench/tests/common/postboot.sh`

```bash theme={null}
#!/bin/sh
#
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear
#
# Pre-test setup for OPC/UA validation on the Qualcomm internal EMAC (end0).
#

set -e

# Disable tracing.
echo 0 >/sys/kernel/debug/tracing/tracing_on

# Disable timer migration.
echo 0 >/proc/sys/kernel/timer_migration

# Affine kernel workqueues to housekeeping CPUs (0-2).
for wq in /sys/devices/virtual/workqueue/*; do
  if [ -w "$wq/cpumask" ]; then
    echo 07 >"$wq/cpumask"
  fi
done

# Disable cpu-idle on all states, all CPUs.
for cpu in /sys/devices/system/cpu/cpu[0-7]*; do
  for state in 0 1 2; do
    file="$cpu/cpuidle/state${state}/disable"
    if [ -w "$file" ]; then
      echo 1 >"$file"
    fi
  done
done

# CPU frequency governor: performance.
echo performance >/sys/devices/system/cpu/cpufreq/policy0/scaling_governor
echo performance >/sys/devices/system/cpu/cpufreq/policy4/scaling_governor

# Disable RT scheduling runtime cap.
echo -1 >/proc/sys/kernel/sched_rt_runtime_us

# Restrict IRQ delivery to housekeeping CPUs (0-2). CPUs 3-7 are RT-isolated.
ALLOW_CPUS="0,1,2"

cpu_list_to_mask() {
  cpus="$1"
  mask=0
  for cpu in $(echo "$cpus" | 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
  irq_num=$(basename "$irq")
  smp_file="$irq/smp_affinity"

  if [ -w "$smp_file" ]; then
    echo "$MASK" >"$smp_file" 2>/dev/null || echo "Masking failed for IRQ $irq_num"
  else
    echo "Skipping IRQ $irq_num (permission denied or not movable)"
  fi
done

# Remove WiFi kernel modules.
# cfg80211_wiphy_work was observed bursting on housekeeping CPUs and
# interfering with NAPI scheduling.
rmmod ath11k_pci ath11k mac80211 libarc4 cfg80211 2>/dev/null || true

# Kill unnecessary background services that add PM/I/O noise.
pkill -x redis-server 2>/dev/null || true
pkill -x containerd 2>/dev/null || true

# Disable runtime PM on the Ethernet MAC platform device.
for dev in /sys/bus/platform/devices/*ethernet*; do
  if [ -w "$dev/power/control" ]; then
    echo on >"$dev/power/control"
  fi
done

# Disable runtime PM on the UFS controller platform device.
for dev in /sys/bus/platform/devices/*ufshc*; do
  if [ -w "$dev/power/control" ]; then
    echo on >"$dev/power/control"
  fi
done

# Disable UFS clock gating and clock scaling.
for dev in /sys/devices/platform/soc@0/*ufshc*; do
  [ -w "$dev/clkgate_enable" ] && echo 0 >"$dev/clkgate_enable"
  [ -w "$dev/clkscale_enable" ] && echo 0 >"$dev/clkscale_enable"
done

# Disable runtime PM on all SCSI devices and hosts.
for dev in /sys/class/scsi_device/*/device/power/control; do
  echo on >"$dev"
done
for host in /sys/class/scsi_host/*/device/power/control; do
  echo on >"$host"
done

# Affine end0 (internal EMAC) hardware IRQs to CPU 6 (mask 0x40).
END0_CPU_MASK=40

for irq in $(grep end0 /proc/interrupts | awk -F: '{ print $1; }'); do
  irq=$(echo "$irq" | tr -d ' ')
  if [ -w "/proc/irq/${irq}/smp_affinity" ]; then
    echo "Affining end0 IRQ ${irq} to CPU 6"
    echo "${END0_CPU_MASK}" >"/proc/irq/${irq}/smp_affinity"
  fi
done

exit 0
```

***

## `ptp.sh`

启动 `ptp4l` 和 `phc2sys` 以进行 802.1AS gPTP 时钟同步。在两台设备上同时运行。

**安装路径**：`/usr/share/rtc-testbench/tests/common/ptp.sh`

```bash theme={null}
#!/bin/bash
#
# Copyright (C) 2021,2025 Linutronix GmbH
# Author Kurt Kanzenbach <kurt@linutronix.de>
# SPDX-License-Identifier: BSD-2-Clause
#
# Start ptp and synchronize system to network time.
#

set -e

cd "$(dirname "$0")"

# Interface (defaults to enP1p1s0; override by passing end0 as argument)
INTERFACE=$1
[ -z $INTERFACE ] && INTERFACE="enP1p1s0"

# Kill already running daemons
pkill ptp4l || true
pkill phc2sys || true

# Set NTP time into PHC
phc_ctl ${INTERFACE} set

# Stop time sync daemons
systemctl stop systemd-timesyncd || true
systemctl stop ntpd || true
systemctl stop chrony || true

# Start ptp4l — 802.1AS-2011 endstation profile, pinned to CPU 1, SCHED_FIFO 98
taskset -c 1 chrt -f 98 ptp4l -2 -H -m -i ${INTERFACE} \
  --socket_priority=4 --tx_timestamp_timeout=40 \
  -f /etc/gPTP.cfg --use_syslog 0 &>/var/log/ptp4l_run.log &

# Wait for ptp4l to initialise
sleep 10

# Configure UTC-TAI offset
pmc -u -b 0 -t 1 "SET GRANDMASTER_SETTINGS_NP clockClass 248 \
        clockAccuracy 0xfe offsetScaledLogVariance 0xffff \
        currentUtcOffset 37 leap61 0 leap59 0 currentUtcOffsetValid 1 \
        ptpTimescale 1 timeTraceable 1 frequencyTraceable 0 \
        timeSource 0xa0"

# Start phc2sys — synchronize system clock to PHC, pinned to CPU 2, SCHED_FIFO 98
taskset -c 2 chrt -f 98 phc2sys -s ${INTERFACE} \
  --step_threshold=1 --transportSpecific=1 -w -m \
  --use_syslog 0 &>/var/log/phc2sys_run.log &

exit 0
```

> [!NOTE]
>
> `ptp.sh` 默认使用 `enP1p1s0`。在 Qualcomm EMAC 上运行 RTC Testbench 时，请将 `end0` 作为参数传入，或确保接口在您的环境中已正确映射。

***

## `gPTP_qcom_emac.cfg`

通过 `-f /etc/gPTP.cfg` 传递给 `ptp4l` 的 802.1AS-2011 gPTP 配置文件。

**安装路径**：`/etc/gPTP.cfg`

```ini theme={null}
#
# 802.1AS example configuration containing those attributes which
# differ from the defaults.  See the file, default.cfg, for the
# complete list of available options.
#
[global]
gmCapable               1
priority1               248
priority2               248
logAnnounceInterval     0
logSyncInterval         -3
syncReceiptTimeout      3
neighborPropDelayThresh 100000
min_neighbor_prop_delay -20000000
assume_two_step         1
path_trace_enabled      1
follow_up_info          1
transportSpecific       0x1
ptp_dst_mac             01:80:C2:00:00:0E
network_transport       L2
delay_mechanism         P2P
```

| 参数 | 值 | 说明 |
| - | - | - |
| `gmCapable` | 1 | 设备可充当主时钟（grandmaster） |
| `logSyncInterval` | -3 | 每 125 ms 同步一次（2⁻³ s） |
| `delay_mechanism` | P2P | 点对点延迟测量 |
| `network_transport` | L2 | 原始以太网（无 UDP） |
| `transportSpecific` | 0x1 | 802.1AS 配置文件标识符 |

***

> [card] [构建与设置](https://dragonwingdocs-staging.qualcomm.com/zh/Suites/Industrial-Edge-Suite/rtc-testbench-setup.md)
>
> 如何使用这些脚本

> [card] [测试执行](https://dragonwingdocs-staging.qualcomm.com/zh/Suites/Industrial-Edge-Suite/rtc-testbench-execution.md)
>
> 运行测试并解读结果
