最近机器运行一段时间后会出现卡顿,然后就是无法远程连接(ssh/telnet)均不可,接着就会出现无法ping通的问题;经过一番调查发现驱动程序里有一个线程死循环了,虽然使用了udelay()函数做延迟,但是我们知道这个函数不会让出CPU,而是让CPU计时,跟程序死循环的效果的是一样的,导致CPU占用率达到100%,这个时候一旦有另外一个内核进程不小心被用户手动开启,比如执行dmesg -n 8(开启内核log)就会让CPU彻底被内核侵占;众所周知内核进程是不可抢占的,悲催的跟内核使用一个CPU核的用户程序就崩了(比如sshd、telnetd),后来把udelay换成了usleep_rangeCPU占用率瞬间降低到1%。

问题是解决了,但是网络无法连接的时候,用户是崩溃的,代表着你对机器失去了控制,你总不能告诉用户:你用串口线去连接吧!

这个时候机器里需要一只看门狗了,帮你看家护院,一旦有问题立马重启机器。关于linux看门狗下面连接说的很清楚了,以及看门狗程序的下载使用,参考https://www.ibm.com/developerworks/cn/linux/l-cn-watchdog/

我这里要说是补充看门狗程序,增加CPU检测功能,因为ARM linux里没有检测cpu softlockup的程序,这里我们手动写一个加到上面连接的程序里。

思路是这样的:

1.启用和系统CPU内核一样数量的线程,比如我的是四核,就启动四个线程;

2.每个线程和每个CPU绑定,使其应用指定的CPU;

3.每个CPU定时对一个变量做加运算;

4.在主程序里检查变量的数值,一旦发现和时间不相符,则表示CPU这段时间有核没有执行这个计数线程,则停止喂狗,重启系统。

下面是程序

#define __USE_GNU
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h> #include "extern.h"
#include "watch_err.h"#ifdef HAVE_CONFIG_H
#include "config.h"
#endif#if USE_SYSLOG
#include <syslog.h>
#include <syslog.h>
#endif#include <fcntl.h>static int g_cpu_0_count = 0;
static int g_cpu_1_count = 0;
static int g_cpu_2_count = 0;
static int g_cpu_3_count = 0;//设置线程绑定指定CPU
static void cpu_bind_thread(int cpu_num)
{cpu_set_t mask;cpu_set_t get;CPU_ZERO(&mask);CPU_SET(cpu_num, &mask);/* 设置cpu 亲和性(affinity) */if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {syslog(LOG_INFO, "[watchdog]set thread affinity failed\n");}   usleep(1000);/* 查看cpu 亲和性(affinity) */CPU_ZERO(&get);if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {syslog(LOG_INFO, "[watchdog]get thread affinity failed\n");}   /* 查看当前线程所运行的所有cpu */if (CPU_ISSET(cpu_num, &get)) {syslog(LOG_INFO, "[watchdog]get thread id %d\n", cpu_num);}
}//看门狗
int check_cpu_lockup(int tint)
{//如果四个CPU有一个处于异常状态则退出循环停止喂狗if (   (g_cpu_0_count < tint * 3)|| (g_cpu_1_count < tint * 3)|| (g_cpu_2_count < tint * 3)|| (g_cpu_3_count < tint * 3)){syslog(LOG_INFO,"[watchdog]cpu is lockup\n");return (CPULOCKUP);}//CPU计数复原g_cpu_0_count = 0;g_cpu_1_count = 0;g_cpu_2_count = 0;g_cpu_3_count = 0;return (ENOERR);
}
//CPU 0 检测
void *cpu_check_thread_0()
{cpu_bind_thread(0);while(1){g_cpu_0_count++;usleep(100 * 1000);}
}//CPU 1 检测
void *cpu_check_thread_1()
{cpu_bind_thread(1);while(1){g_cpu_1_count++;usleep(100 * 1000);}
}//CPU 2 检测
void *cpu_check_thread_2()
{cpu_bind_thread(2);while(1){g_cpu_2_count++;usleep(100 * 1000);}
}//CPU 3 检测
void *cpu_check_thread_3()
{cpu_bind_thread(3);while(1){g_cpu_3_count++;usleep(100 * 1000);}
}int cpu_lockup_check_init()
{ int ret;pthread_t cpu_check_id_0;pthread_t cpu_check_id_1;pthread_t cpu_check_id_2;pthread_t cpu_check_id_3;ret = pthread_create(&cpu_check_id_0, NULL, (void *)cpu_check_thread_0, NULL);if (ret){syslog(LOG_ERR,"[watchdog]Create cpu_check_thread 0 failed!\n");}ret = pthread_create(&cpu_check_id_1, NULL, (void *)cpu_check_thread_1, NULL);if (ret){syslog(LOG_ERR,"[watchdog]Create cpu_check_thread 1 failed!\n");}ret = pthread_create(&cpu_check_id_2, NULL, (void *)cpu_check_thread_2, NULL);if (ret){syslog(LOG_ERR,"[watchdog]Create cpu_check_thread 2 failed!\n");}ret = pthread_create(&cpu_check_id_3, NULL, (void *)cpu_check_thread_3, NULL);if (ret){syslog(LOG_ERR,"[watchdog]Create cpu_check_thread 3 failed!\n");}return 0;
}

这是watcdog里的改动

 /* init the cpu lockup check progrome */cpu_lockup_check_init();usleep(tint * 500000);/* check teh cpu lockup */do_check(check_cpu_lockup(tint), rbinary, NULL);

下载地址:https://download.csdn.net/download/u010659887/11829702

linux watchdog应用相关推荐

  1. linux watchdog demo hacking

    /*********************************************************************** linux watchdog demo hacking ...

  2. Linux Watchdog Test Program

    /************************************************************************ Linux Watchdog Test Progra ...

  3. Linux Watchdog/看门狗

    Watchdog timer(看门狗定时器)是一种电子计时器,其用于检测和恢复计算机故障.在正常操作期间,计算机定期重置看门狗定时器以防止它"超时".如果由于硬件故障或程序错误,计 ...

  4. Linux Watchdog 机制

    ​前言 Watchdog 是 Linux 系统一个很重要的机制,其目的是监测系统运行的情况,一旦出现锁死,死机的情况,能及时重启机器(取决于设置策略),并收集crash dump. watchdog, ...

  5. arm linux下看门狗应用,arm linux watchdog 看门狗

    目前手上有个项目需要设计看门狗,是arm+CPLD 方式.由于对看门狗要求很高,打算做一个双看门狗,arm CPLD互相为 对方的看门狗.理论上CPLD是不需要看门狗的,还是这么去设计了.接下来对看门 ...

  6. Linux驱动学习之:WDT(watchdog)驱动

    第一部分: WDT驱动原理 WDT在内核中通常都实现为misc驱动. WDT介绍 一个Watchdog Timer(WDT)是一个在软件出错的时候可以复位计算机系统的硬件电路. 通常一个用户空间守护进 ...

  7. 2021-12-10 Linux内核中watchdog,用户层喂狗程序分析

    一.我这里是MTK平台,喂狗的代码在\system\core\watchdogd\,实际测试,如果write(fd, "", 1);注释掉,开机后过段时间会reboot. 1.\s ...

  8. Qt 控制watchdog app hacking

    /*************************************************************************** Qt 控制watchdog app hacki ...

  9. Linux Kernel Development——列出系统中所有的进程

    1. 在内核模块中列出所有的进程: 从init_task开始遍历内核链表,输出所有进程 #include <linux/module.h> #include <linux/list. ...

最新文章

  1. qstring 属于元数据类型吗_2020年退休养老金只有1800元,属于什么水平?还要继续工作吗?...
  2. 极大似然估计_计量经济学 | 极大似然估计
  3. 秒拨动态ip切换技术python_Python爬虫如何通过更换IP避开网站的反爬虫机制(一)...
  4. 新手必看的编程介绍,帮你推荐学习方案!
  5. plsql job执行多个存储过程_spring-boot-micro-job一款分布式任务调度执行框架
  6. 选择器Selector
  7. c语言程序整数四则运算,c语言中三个整数随机的四则运算
  8. c#怎么拟合函数得到参数_吴恩达老师课程笔记系列第32节 -正则化之代价函数(2)...
  9. 计算机网络之网络层:11、移动IP
  10. Outlook中的Notes的链接打不开,提示错误信息“File does not exit”
  11. BNU 背包密码(编码与解密)
  12. 如何入门 Python 爬虫?50集免费全套教程视频让你轻松掌握
  13. 桃子CCD视觉高速喷射点胶机,用它你就会爱上它
  14. Stanford CoreNLP 纯Python版本的深度学习NLP工具包 stanza 使用笔记
  15. Zemax 快捷键及使用技巧(持续更新中)
  16. C# 读写Excel
  17. 第7关:求解一元二次方程
  18. 数据结构之树与二叉树
  19. 完全解读:用最小二乘法求模型参数
  20. php继电器信号,中间继电器是将 信号变成 信号的继电器,主要起中间转换作用,其输入为线圈的 和 ,输出信号是触点的断开和闭合。它可将输出信号同时传给几个控制元件或回路。...

热门文章

  1. Android 省电模式
  2. 华为服务器最多磁盘,华为存储服务器使用20TB SMR硬盘 最大19.2PB
  3. 程序员的收入现状,工作1-5年。上海
  4. 当网络安全不断“下线”,谁能成为最好的“守门员”?
  5. shader 反射 水面_unity水面波浪光照反射折射物理渲染着色器Lux Water 1.01
  6. 未捕获和意外的异常处理
  7. mysql重启时报错 /etc/my.cnf is ignored
  8. 圆曾经的小车梦,造一台智能小车(一)
  9. 电信免费电话卡究竟是个什么鬼,是真的吗
  10. UIPageControl修改圆点图片及间距