softlockup检测(watchdog)原理(用于检测系统调度是否正常)

简介:

softlockup(watchdog)用于检测系统调度是否正常,即软锁的情况,当发生softlockup时,内核不能调度,但还能响应中断,对用户的表现可能为:能ping通,但无法登陆系统,无法进行正常操作。
其基本原理为:为每个CPU启动一个内核线程(watchdog/x),此线程为优先级最高的实时线程,在该线程得到调度时,会更新相应的计数(时间戳),同时会启动定时器,当定时器到期时检查相应的时间戳,如果超过指定时间,都没有更新,则说明这段时间内都没有发生调度(因为此线程优先级最高),则打印相应告警或根据配置可以进入panic流程。
基本代码分析(2.6.32)
rest_init->kernel_init->lockup_detector_init->cpu_callback->watchdog_prepare_cpu(初始化watchdog定时器):

点击(此处)折叠或打开

  1. static int watchdog_prepare_cpu(int cpu)

  2. {

  3.     struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);


  4.     WARN_ON(per_cpu(softlockup_watchdog, cpu));

  5.     hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);//初始化高精度定时器

  6.     hrtimer->function = watchdog_timer_fn;//设置定时器处理函数


  7.     return 0;

  8. }

看门狗定时器处理函数:

点击(此处)折叠或打开

  1. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)

  2. {

  3. //获取计数watchdog_touch_ts,该计数在watchdog内核线程被调度时更新

  4.     unsigned long touch_ts = __get_cpu_var(watchdog_touch_ts);

  5.     struct pt_regs *regs = get_irq_regs();

  6.     int duration;


  7.     /* kick the hardlockup detector */

  8. //增加中断计数,证明没有发生硬锁(关中断死锁)

  9.     watchdog_interrupt_count();


  10.     /* kick the softlockup detector */

  11. //唤醒wathdog内核线程

  12.     wake_up_process(__get_cpu_var(softlockup_watchdog));


  13.     /* .. and repeat */

  14. //重启定时器

  15.     hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));

  16.     if (touch_ts == 0) {

  17.         if (unlikely(__get_cpu_var(softlockup_touch_sync))) {

  18.             /*

  19.              * If the time stamp was touched atomically

  20.              * make sure the scheduler tick is up to date.

  21.              */

  22.             __get_cpu_var(softlockup_touch_sync) = false;

  23.             sched_clock_tick();

  24.         }

  25.         __touch_watchdog();

  26.         return HRTIMER_RESTART;

  27.     }


  28.     /* check for a softlockup

  29.      * This is done by making sure a high priority task is

  30.      * being scheduled. The task touches the watchdog to

  31.      * indicate it is getting cpu time. If it hasn'then

  32.      * this is a good indication some task is hogging the cpu

  33.      */

  34. //判断是否发生了软锁,原理是判断touch_ts(时间戳)是否超过一定时间没有更新

  35.     duration = is_softlockup(touch_ts);

  36.     if (unlikely(duration)) {

  37.         /* only warn once */

  38.         if (__get_cpu_var(soft_watchdog_warn) == true)

  39.             return HRTIMER_RESTART;

  40. //发生了软锁后,进行一些列的信息记录和告警。

  41.         printk(KERN_EMERG "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",

  42.             smp_processor_id(), duration,

  43.             current->comm, task_pid_nr(current));

  44.         print_modules();

  45.         print_irqtrace_events(current);

  46.         if (regs)

  47.             show_regs(regs);

  48.         else

  49.             dump_stack();

  50. //如果配置了softlockup_panic(proc中配置),则panic

  51.         if (softlockup_panic)

  52.             panic("softlockup: hung tasks");

  53.         __get_cpu_var(soft_watchdog_warn) = true;

  54.     } else

  55.         __get_cpu_var(soft_watchdog_warn) = false;


  56.     return HRTIMER_RESTART;

  57. }


启动看门狗,即创建watchdog内核线程。

点击(此处)折叠或打开

  1. static int watchdog_enable(int cpu)

  2. {

  3.     struct task_struct *= per_cpu(softlockup_watchdog, cpu);

  4.     int err = 0;


  5.     /* enable the perf event */

  6.     err = watchdog_nmi_enable(cpu);


  7.     /* Regardless of err above, fall through and start softlockup */


  8.     /* create the watchdog thread */

  9.     if (!p) {

  10. //创建watchdog内核线程

  11.         p = kthread_create(watchdog, (void *)(unsigned long)cpu, "watchdog/%d", cpu);

  12.         if (IS_ERR(p)) {

  13.             printk(KERN_ERR "softlockup watchdog for %i failed\n", cpu);

  14.             if (!err)

  15.                 /* if hardlockup hasn't already set this */

  16.                 err = PTR_ERR(p);

  17.             goto out;

  18.         }

  19.         kthread_bind(p, cpu);

  20.         per_cpu(watchdog_touch_ts, cpu) = 0;

  21.         per_cpu(softlockup_watchdog, cpu) = p;

  22.         wake_up_process(p);

  23.     }


  24. out:

  25.     return err;

  26. }


watchdog内核线程执行主函数,主要是要更新计数(时间戳)

点击(此处)折叠或打开

  1. static int watchdog(void *unused)

  2. {

  3. //设置为最高优先级

  4.     struct sched_param param = { .sched_priority = MAX_RT_PRIO-};

  5.     struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);

  6. //设置为实时线程

  7.     sched_setscheduler(current, SCHED_FIFO, &param);


  8.     /* initialize timestamp */

  9. //初始化计数(时间戳)

  10.     __touch_watchdog();


  11.     /* kick off the timer for the hardlockup detector */

  12.     /* done here because hrtimer_start can only pin to smp_processor_id() */

  13. //启动定时器,用于检测是否发生软锁

  14.     hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),

  15.          HRTIMER_MODE_REL_PINNED);

  16. //睡眠

  17.     set_current_state(TASK_INTERRUPTIBLE);

  18.     /*

  19.      * Run briefly once per second to reset the softlockup timestamp.

  20.      * If this gets delayed for more than 60 seconds then the

  21.      * debug-printout triggers in watchdog_timer_fn().

  22.      */

  23.     while (!kthread_should_stop()) {

  24. //更新计数

  25.         __touch_watchdog();

  26.         schedule();


  27.         if (kthread_should_stop())

  28.             break;


  29.         set_current_state(TASK_INTERRUPTIBLE);

  30.     }

  31.     __set_current_state(TASK_RUNNING);


  32.     return 0;

  33. }


判断是否发生软锁:is_softlockup

点击(此处)折叠或打开

  1. static int is_softlockup(unsigned long touch_ts)

  2. {

  3.     unsigned long now = get_timestamp(smp_processor_id());


  4.     /* Warn about unreasonable delays: */

  5. //检测计数多久没有更新了,如果超过了60s,则表示发生了软锁

  6.     if (time_after(now, touch_ts + softlockup_thresh))

  7.         return now - touch_ts;


  8.     return 0;

  9. }


本文转自 guowang327 51CTO博客,原文链接:http://blog.51cto.com/guowang327/1962741,如需转载请自行联系原作者
相关文章
|
2月前
|
编解码 监控 计算机视觉
YOLOv8改进 | 检测头篇 | 利用DynamicHead增加辅助检测头针对性检测(四头版本)
YOLOv8改进 | 检测头篇 | 利用DynamicHead增加辅助检测头针对性检测(四头版本)
238 0
|
7月前
8.9 RDTSC时钟检测反调试
RDTSC时钟检测同样可实现反调试检测,使用时钟检测方法是利用`rdtsc`汇编指令,它返回至系统重新启动以来的时钟数,并且将其作为一个64位的值存入`EDX:EAX`寄存器中,通过运行两次`rdstc`指令,然后计算出他们之间的差值,即可判定对方是否在调试我们的程序。
37 1
|
传感器
VM系列振弦采集模块信号检测与采样
VMXXX 内部有振弦传感器的信号检测、 有效性检测机制, 仅信号幅值位于预设的合理区间时,才会进行数据采样, 当完成足够数量的样本采样后立即进行信号质量分析计算,得到频率、频模值及多个信号质量表征值更新于对应的只读寄存器内,读取这些寄存器值,即可得到当前测量结果数据和信号质量。
VM系列振弦采集模块信号检测与采样
|
传感器 存储
VM系列振弦采集模块的信号检测与分析计算
振弦传感器钢弦起振后,信号强度在短时间内迅速达到最大,然后在钢弦张力及空气阻力作用下逐渐恢复静止。我们可将整个振动过程分为起振、调整、稳定、消失几个阶段,上述几个阶段中,起振和调整阶段的振动又叫做强迫振动,稳定与消失阶段合称为自主振动。
259 66
VM系列振弦采集模块的信号检测与分析计算
|
传感器 移动开发
使用标准信号检测 VM振弦采集模块测量精度(三)
VM 振弦采集模块自 SF3.51 版本开始,新增加了频率和温度的多项式修正功能。测量、计算完成后的频率值和温度值,经过一个 2 次多项式进行修正,最终更新到频率和温度寄存器。
使用标准信号检测 VM振弦采集模块测量精度(三)
|
传感器 数据处理
使用标准信号检测 VM振弦采集模块测量精度(二)
振弦传感器采集读数模块:指针对振弦传感器的特性而设计的传感器激励、读数模块。具有集成度高、功能模块化、数字接口的一系列特性,能完成振弦 传感器的激励、信号检测、数据处理、质量评估等专用针对性功能,进行传感器频 率和温度物理量模数转换,进而通过数字接口实现数据交互。振弦传感器读数模块 是振弦传感器与数字化、信息化之间的核心转换单元。 
使用标准信号检测 VM振弦采集模块测量精度(二)
|
传感器 芯片
使用标准信号检测 VM振弦采集模块测量精度(一)
真值是检测任何设备测量精度的基础条件,真值不能用信号发生器号称的误差来衡量、不能用电阻标称的阻值来衡量。获取真值最可靠的办法是使用比要检测精度更高一个数量级的仪表去测量。例如:电阻的值必须要用 6 位半或者更高精度的仪表测量后才能确定真实的电阻到底是多少。信号发生器也必须用一个更高精度的频率测量设备检测后才可以使用。如果“真值” 是不可靠的,那么对任何设备的精度检测工作,都会是徒劳的。
使用标准信号检测 VM振弦采集模块测量精度(一)
|
弹性计算 Kubernetes 监控
CloudIaC 漂移检测功能详解
云霁CloudIaC 是一款开源的基于terraform 的低代码平台管理库,本篇文章主要介绍其中关于配置漂移的处理方式。
1770 1
|
云安全 安全 Unix
安全检测 | 学习笔记
快速学习安全检测,重点介绍了如何在 Linux 下进行安全防护,并从用户系统安全、SSH 安全、恶意文件安全和云安全四个角度诠释如何提升系统的安全性。
安全检测 | 学习笔记
|
负载均衡 搜索推荐 Java
定时检测服务状态脚本实现
1、定时监测服务状态需求
200 0