4.5.18. WatchDog 测试

4.5.18.1. 测试原理

X5 集成硬件看门狗(watchdog@34250000,驱动 dw_wdt)。WDT 启动内部计数器持续递减,必须超时收到 kick(喂狗)计数器重载,否则触发 SoC 系统复位

典型行为:

  • 内核喂(默认):内核 watchdogd 线程周期性 kick,/sys/class/watchdog/watchdog0/stateinactive,无需用户程序参与

  • 用户接管:进程 open /dev/watchdog0写入数据后,内核停止喂,须进程持续喂狗

  • 关闭看门狗nowayout=0通过 Magic Close 关闭;关闭重启才能再次启用

  内核 / 用户态                    DesignWare WDT     超时未 kick
 ┌─────────────┐   周期性 kick    ┌──────────────┐                   ┌─────────┐
 │  喂狗源      │ ──────────────> │  计数器递减   │   ──────────────> │ SoC 复位│
 └─────────────┘                  └──────────────┘                  └─────────┘

4.5.18.2. 准备工作

脚本使用说明

watchdog_test.sh 支持通过 -h 参数查看帮助信息

# ./watchdog_test.sh --help

usage: ./watchdog_test.sh <command>

commands:
  info      show hardware watchdog information
  timeout   show configured watchdog timeout (seconds)
  feeder    show whether kernel or userspace is feeding
  timeleft  show remaining timeout (seconds)
  feed      feed watchdog N times: feed <count> [-i interval_sec]
  disable   stop watchdog (requires nowayout=0)

examples:
  ./watchdog_test.sh info
  ./watchdog_test.sh feeder
  ./watchdog_test.sh feed 1
  ./watchdog_test.sh feed 6 -i 2

参数解析如下:

  • info: 查看硬件看门狗基本信息

  • timeout 查看当前配置超时时间 (秒)

  • feeder 判断当前内核还是用户态在喂狗

  • timeleft 查看当前剩余超时时间 (秒)

  • feed 喂狗 <次数> [-i 间隔秒数] ; 默认间隔 1 秒

  • disable 关闭看门狗,关闭重启才能再次启用

  • -h / --help 显示帮助

4.5.18.3. 测试方法

查看默认超时时间

# ./watchdog_test.sh timeout

Watchdog timeout: 10 sec

查看喂狗来源

# ./watchdog_test.sh  feeder

========================================
Watchdog Feeding Source
========================================
Sysfs state : inactive
Sampling timeleft (1s x 7, kernel feeds about every 5s):
  [1] timeleft=9s
  [2] timeleft=8s
  [3] timeleft=7s
  [4] timeleft=6s
  [5] timeleft=10s
  [6] timeleft=9s
  [7] timeleft=8s
Timeleft seq : 9 8 7 6 10 9 8
Feed source : kernel  # 当前是内核态喂狗
Reason      : state=inactive, timeleft increased during sampling
              (kernel watchdog worker kicked the hardware WDT)
========================================
root@buildroot:/app/platform_samples/chip_base_test/14_watchdog_test

查看剩余超时时间

# ./watchdog_test.sh timeleft

Watchdog timeleft: 9 sec (timeout=10 sec)

喂狗测试

  • 喂狗一次

# ./watchdog_test.sh  feed 1

Feeding watchdog on /dev/watchdog0: 1 time(s), interval=1s
  [1/1] keepalive ok, timeleft=10s
[  647.681517] watchdog: watchdog0: watchdog did not stop!
Feed done (1 times)
Note: userspace took over (state may become active);
      kernel auto-feed pauses until reboot or disable
  • 喂狗多次,且每次间隔 5s

# ./watchdog_test.sh feed 10  -i 5

Feeding watchdog on /dev/watchdog0: 10 time(s), interval=5s
  [1/10] keepalive ok, timeleft=10s
  [2/10] keepalive ok, timeleft=10s
  [3/10] keepalive ok, timeleft=10s
  [4/10] keepalive ok, timeleft=10s
  [5/10] keepalive ok, timeleft=10s
  [6/10] keepalive ok, timeleft=10s
  [7/10] keepalive ok, timeleft=10s
  [8/10] keepalive ok, timeleft=10s
  [9/10] keepalive ok, timeleft=10s
  [10/10] keepalive ok, timeleft=10s
[  329.019018] watchdog: watchdog0: watchdog did not stop!
Feed done (10 times)
Note: userspace took over (state may become active);
      kernel auto-feed pauses until reboot or disable

注意 :用户一旦喂狗,内核喂狗暂停,若喂 N 次退出,之后不再喂,超时系统复位。

关闭看门狗

# ./watchdog_test.sh disable

Stopping watchdog via magic close on /dev/watchdog0 ...
State after disable: inactive
Timeleft after disable: 0 sec
Watchdog disable command sent (magic close 'V')
Note: reboot or re-probe driver to re-enable if needed

注意 :关闭重启才能再次启用。

4.5.18.4. 测试指标

  • 看门狗为 Synopsys DesignWare 硬件 WDT,驱动 dw_wdt,设备节点 /dev/watchdog0 可用

  • /sys/class/watchdog/watchdog0 目录timelefttimeoutidentitystate属性存在