时间子系统16_soft lockup机制

简介:
//	1.debug选项LOCKUP_DETECTOR,开启/关闭kernel中的soft lockup和hard lockup探测
//	2.实现:kernel/watchdog.c

//	3.实现原理:
//		1.涉及到了3部分内容:kernel线程,时钟中断,NMI中断
//			优先级:kernel线程 < 时钟中断 < NMI中断。
//		2.利用它们之间优先级的区别,调试系统运行中的两种问题:
//			抢占被长时间关闭而导致进程无法调度(soft lockup)
//			中断被长时间关闭而导致更严重的问题(hard lockup)

//	参考:http://blog.csdn.net/panzhenjie/article/details/10074551
//	内核版本 3.8.6
//	smp per-cpu watchdog核心线程
1.1 static struct smp_hotplug_thread watchdog_threads = {
	.store			= &softlockup_watchdog,
	.thread_should_run	= watchdog_should_run,
	.thread_fn		= watchdog,
	.thread_comm		= "watchdog/%u",
	.setup			= watchdog_enable,
	.park			= watchdog_disable,
	.unpark			= watchdog_enable,
};

//	lockup detector模块初始化
//	函数任务:
//		1.计算hrtimer运行的频率
//		2.注册watchdog核心线程
//	注:
1.2 void __init lockup_detector_init(void)
{
	//计算hrtimer运行的频率
	set_sample_period();
	//注册watchdog核心线程
	if (smpboot_register_percpu_thread(&watchdog_threads)) {
		pr_err("Failed to create watchdog threads, disabled\n");
		watchdog_disabled = -ENODEV;
	}
}
//	设置watchdog timer运行频率
//	调用路径:	lockup_detector_init->get_softlockup_thresh
//	注:
//		1.sample_period,即watchdog timer运行的频率
//		2.watchdog timer在一次soft lockup时间阈值内运行5次
1.3 static void set_sample_period(void)
{
	sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
}

//	认定发生了soft lockup的时间阈值
//	注:如果watchdog kthread在watchdog_thresh * 2 时间内未被调度,
//		则认为发生了soft lockup.
1.4 static int get_softlockup_thresh(void)
{
	return watchdog_thresh * 2;
}

//	启动指定cpu上lockup检测
//	函数任务:
//		1.初始化watchdog timer
//		2.初始化hard lockup的nmi中断事件
//		3.启动watchdog timer
//		4.设置watchdog kthread调度策略FIFO
//		5.更新watchdog时间戳

//	注:设置watchdog kthread为FIFO的调度策略保证了watchdog timer
//		唤醒kthread之后,它可以因高优先级切换到cpu上执行。
2.1 static void watchdog_enable(unsigned int cpu)
{
	struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
	//lockup检测使用的hrtimer
	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	hrtimer->function = watchdog_timer_fn;

	//第一次启动watchdog,暂停current
	if (!watchdog_enabled) {
		kthread_park(current);
		return;
	}	
	//hard lockup检测机制
	watchdog_nmi_enable(cpu);
	//hrtimer sample时间之后运行
	hrtimer_start(hrtimer, ns_to_ktime(sample_period),
		      HRTIMER_MODE_REL_PINNED);
	//watchdog进程FIFO策略
	watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
	//执行一次更新
	__touch_watchdog();
}

//	关闭指定cpu上的lockup检测
//	函数任务:
//		1.恢复watchdog正常优先级
//		2.取消hrtimer
//		3.关闭hard lockup检测机制的nmi中断
2.2 static void watchdog_disable(unsigned int cpu)
{
	struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
	//恢复watchdog正常优先级
	watchdog_set_prio(SCHED_NORMAL, 0);
	//取消hrtimer
	hrtimer_cancel(hrtimer);
	//关闭hard lockup检测机制的nmi中断
	watchdog_nmi_disable(cpu);
}

//	watchdog核心线程可运行的条件
//	函数任务:
//		1.保证watchdog kthread 运行频率 <= watchdog timer 运行频率
//	注:
//		soft_lockup_hrtimer_cnt代表watchdog核心线程运行的次数
//		hrtimer_interrupts代表watchdog timer运行的次数
2.3 static int watchdog_should_run(unsigned int cpu)
{
	return __this_cpu_read(hrtimer_interrupts) !=
		__this_cpu_read(soft_lockup_hrtimer_cnt);
}

//	watchdog核心线程函数
//	函数任务:
//		1.更新soft_lockup_hrtimer_cnt=hrtimer_interrupts
//		2.更新watchdog运行时间戳
2.4 static void watchdog(unsigned int cpu)
{
	__this_cpu_write(soft_lockup_hrtimer_cnt,
			 __this_cpu_read(hrtimer_interrupts));
	__touch_watchdog();
}

//	更新watchdog运行时间戳
2.5 static void __touch_watchdog(void)
{
	int this_cpu = smp_processor_id();

	__this_cpu_write(watchdog_touch_ts, get_timestamp(this_cpu));
}
//	定时器函数
//	函数主要任务:
//		1.获取watchdog上次运行的时间戳
//		2.递增watchdog timer运行次数
//		3.检查watchdog时间戳,是否发生了soft lockup
//			3.1 如果发生了,dump堆栈,打印信息
//		4.重调度timer
//	注:
//		在watchdog timer运行时唤醒watchdog kthread,保证kthread与timer相同的运行频率
3.1 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
{
	//watchdog上次运行的时间戳
	unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
	struct pt_regs *regs = get_irq_regs();
	int duration;
	//在唤醒watchdog kthread之前递增hrtimer_interrupts,保证kthread更新其时间戳
	watchdog_interrupt_count();
	//唤醒watchdog kthread,保证kthread与timer相同的运行频率
	wake_up_process(__this_cpu_read(softlockup_watchdog));
	//再次调度hrtimer下一个周期运行
	hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));

	...

	//检测是否发生soft lockup
	duration = is_softlockup(touch_ts);
	if (unlikely(duration)) {
		printk(KERN_EMERG "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
			smp_processor_id(), duration,
			current->comm, task_pid_nr(current));
		print_modules();
		print_irqtrace_events(current);
		//dump 寄存器和堆栈
		if (regs)
			show_regs(regs);
		else
			dump_stack();

		if (softlockup_panic)
			panic("softlockup: hung tasks");
	} 
	return HRTIMER_RESTART;
}
//	检查抢占被关闭的时间间隔
//		watchdog kthread在watchdog timer的中断上下文中被唤醒,
//		当中断退出时,kthread会抢占cpu上的当前进程。如果
//		抢占被关闭的话,则不会发生抢占,watchdog便无法更新时
//		间戳,当抢占关闭的时间超过阈值时,核心认为发生了soft
//		lock up。
//	注:soft lockup阈值 watchdog_thresh * 2 (20s)
3.2 static int is_softlockup(unsigned long touch_ts)
{
	//当前时间戳
	unsigned long now = get_timestamp(smp_processor_id());
	//watchdog在 watchdog_thresh * 2 时间内未被调度过
	if (time_after(now, touch_ts + get_softlockup_thresh()))
		return now - touch_ts;

	return 0;
}

目录
相关文章
|
Web App开发 缓存 JavaScript
【开发工具】【windows】Visual Studio Code(VS Code)远程Linux服务器环境搭建——SSH篇
【开发工具】【windows】Visual Studio Code(VS Code)远程Linux服务器环境搭建——SSH篇
2109 0
【开发工具】【windows】Visual Studio Code(VS Code)远程Linux服务器环境搭建——SSH篇
|
Java API Android开发
Android10.0(Q) MTK平台增加以太网静态IP功能
Android10.0(Q) MTK平台增加以太网静态IP功能
1480 0
|
存储 SQL 自然语言处理
详解TableStore模糊查询——以订单场景为例
# 背景 订单系统在各行各业中广泛应用,为消费者、商家后台、促销系统等第三方提供用户、产品、订单等多维度的管理和查询服务。为了挖掘出海量订单数据的潜能,丰富高效的查询必不可少。然而很多时候并不能给出完整准确的查询关键字,例如,只知道收货人姓氏,或是产品名称部分关键字,或是根据收货人手机尾号找到订单,我们将这类查询归为“模糊查询”。
6523 0
|
SQL 弹性计算 关系型数据库
PostgreSQL 权限 list 插件 pg_permissions
标签 PostgreSQL , pg_permissions , \dp , 权限 背景 通常列出权限可以使用psql的一些简写: \dp+ \dp+ *.* 列出用户,schema的默认权限。列出表、视图、序列的权限。 这几个简写的介绍如下 \ddp [ pattern ] Lists default access privilege set
1137 0
|
存储 弹性计算 编解码
某接口cpu使用率异常排查记录
1. 问题现象: 某项目跑在 p01~02 2台ECS上,主要用途是聚合服务,给H5页面提供直接访问的接口。最近版本新增加了接口 wechat/mp/qrcode,用途是根据请求参数生成小程序二维码,以swoole+lumen方式对外服务接口上线后,cpu使用率逐步上升,直至100%;同时请求响应时长也逐步攀升 上图是问题期间p01cpu使用率,几个下降过程是因为上线(会重启服务进程)或者手动重启服务进程;后面发现p01 p02cpu基本打满,临时加了 p03 p04 2台ecs以降低问题的影响。
1875 0
|
机器学习/深度学习 人工智能 自然语言处理
AAAI 2020 阿里精选论文抢先看
人工智能领域顶会AAAI 2020刚刚在美国纽约落下帷幕。本届大会,阿里巴巴经济体共60余篇论文被大会收录,同学们以视频和海报的形式远程参与,与全世界同行分享了最新的技术进展。这里小编为你精心挑选了阿里巴巴入选的oral paper Spotlight paper 以及 最佳人工智创新应用奖获奖论文,足不出户,你也能了解阿里巴巴AI技术研究的一线干货。
2803 0
获客成本仅0.0003元 ,众安支付宝小程序高转化率秘籍
在平均获客成本高达100元、低频、低转化率的互金行业,众安保险小程序在支付宝生态把获客成本做到了0.0003元。这是去年双十二期间跟支付宝内公域流量板块“我的快递”合作一周后的成绩单。在这场以赠送快递延误险为吸引点的营销活动中,众安保险小程序收获了62万新增用户,产生保单26万单。
3628 0
|
vr&ar 开发工具 图形学