3568watchdog看门狗使用

1.驱动文件:

  kernel/drivers/watchdog/dw_wdt.c

 

2.dts节点配置:

  默认是打开的

wdt: watchdog@fe600000 {
        compatible = "snps,dw-wdt";
        reg = <0x0 0xfe600000 0x0 0x100>;
        clocks = <&cru TCLK_WDT_NS>, <&cru PCLK_WDT_NS>;
        clock-names = "tclk", "pclk";
        interrupts = <GIC_SPI 149 IRQ_TYPE_LEVEL_HIGH>;
        status = "okay";
    };

 

3.指令测试:

  输入如下命令后系统会打开看门狗,不再喂狗,watchdog会自动重启

echo A > /dev/watchdog , 这里写入的是除大写V以外的任意字符。

 

4.Android层启动watchdog进程:

  Android层的watchdog进程默认也没有启动,按如下修改可以启动watchdogd进程,当系统卡住时会停止喂狗来触发系统重启

复制代码
/device/rockchip/common

diff --git a/init.rockchip.rc b/init.rockchip.rc
index 4f80547..4a54f78 100755
--- a/init.rockchip.rc
+++ b/init.rockchip.rc
@@ -29,9 +29,9 @@ service charger /system/bin/charger
     file /proc/last_kmsg r
 
 # Set watchdog timer to 30 seconds and pet it every 10 seconds to get a 20 second margin
-service watchdogd /sbin/watchdogd 10 20
+service watchdogd /system/bin/watchdogd 10 20 //这里10 20表示喂狗的超时时间是10s+20s=30s
     class core
-    disabled
+#    disabled
     seclabel u:r:watchdogd:s0
复制代码

  其中watchdogd 是应用层的代码system\core\watchdogd\watchdogd.cpp编译出来的二进制文件,代码如下:

复制代码
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <fcntl.h>
#include <linux/watchdog.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <android-base/logging.h>

#define DEV_NAME "/dev/watchdog"

int main(int argc, char** argv) {
    android::base::InitLogging(argv, &android::base::KernelLogger);

    int interval = 10;
    if (argc >= 2) interval = atoi(argv[1]);

    int margin = 10;
    if (argc >= 3) margin = atoi(argv[2]);

    LOG(INFO) << "watchdogd started (interval " << interval << ", margin " << margin << ")!";

    int fd = open(DEV_NAME, O_RDWR | O_CLOEXEC);
    if (fd == -1) {
        PLOG(ERROR) << "Failed to open " << DEV_NAME;
        return 1;
    }

    int timeout = interval + margin;
    int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
    if (ret) {
        PLOG(ERROR) << "Failed to set timeout to " << timeout;
        ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
        if (ret) {
            PLOG(ERROR) << "Failed to get timeout";
        } else {
            if (timeout > margin) {
                interval = timeout - margin;
            } else {
                interval = 1;
            }
            LOG(WARNING) << "Adjusted interval to timeout returned by driver: "
                         << "timeout " << timeout << ", interval " << interval << ", margin "
                         << margin;
        }
    }

    while (true) {
        write(fd, "", 1);
        sleep(interval);
    }
}
复制代码

这里主要是实现了对/dev/watchdog节点的写操作

 

5.测试watchdog:

 5.1 关掉系统panic重启

echo 0 > /sys/module/kernel/parameters/panic

 

 5.2 人为制作一个panic使系统卡住

echo c > proc/sysrq-trigger

  

   5.3 等待30s看系统是否重启,如果重启则表示watchdog正常工作了

 

参考:https://blog.csdn.net/weixin_43245753/article/details/124269920

https://blog.csdn.net/yangguoyu8023/article/details/121777657

posted @   M-kobe  阅读(2)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示