前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux电源管理-wakeup count

Linux电源管理-wakeup count

作者头像
DragonKingZhu
发布2020-03-24 17:12:17
2.4K0
发布2020-03-24 17:12:17
举报
文章被收录于专栏:Linux内核深入分析

前言

在wakeup events framework小节中提到,wakeup events framwork可以解决system suspend和wakeup events之间的同步问题。而整篇下来没有看到是如何解决同步问题的。所有本小节继续分析wakeup events framework中的重要知识点-wakeup count。

"wakeup count"是不是很熟悉? 是的,在wakeup_source结构体中就存在"wakeup_count"成员,此成员的意思是:终止suspend的次数。而本小节的wakeup count并非此意,只是名字相同罢了。:(

实现原理

1. 在进行suspend之前,需要先获取系统中总的wakeup event数量。

2. 将获得的值保存到全局变量saved_count中。

3. 此后可能系统已经进入了suspend的流程中。这时候如果系统发生了wakeup events,就会增加wakeup event的数量。

4. 在suspend执行的过程中,会调用pm_wakeup_pending接口检测系统有没有发生wakeup event。(通过比较当前的wakeup events和之前保存的值saved_count是否相同)

5. 如果不同,则终止系统suspend。否则继续执行suspend流程。

那wakeup event framework是如何保存当前系统中所有的wakeup event? 以及如何如何判断当前是否有wake events正在处理?

通常思路: 用一个变量记录当前系统发生的所有wakeup event,用另一个变量记录当前是否有wake events在处理。

那Linux内核到底是如何记录这两个变量呢?

linux中使用一个原子变量,高16位记录系统所有的wakeup event总数,低16位记录是否有wakeup events在处理中。

代码语言:javascript
复制
/*
 * Combined counters of registered wakeup events and wakeup events in progress.
 * They need to be modified together atomically, so it's better to use one
 * atomic variable to hold them both.
 */
static atomic_t combined_event_count = ATOMIC_INIT(0);

#define IN_PROGRESS_BITS	(sizeof(int) * 4)
#define MAX_IN_PROGRESS		((1 << IN_PROGRESS_BITS) - 1)

static void split_counters(unsigned int *cnt, unsigned int *inpr)
{
	unsigned int comb = atomic_read(&combined_event_count);

	*cnt = (comb >> IN_PROGRESS_BITS);
	*inpr = comb & MAX_IN_PROGRESS;
}

"registered wakeup events"代表系统自启动以来所有的wakeup event的总数,在combined_event_count的高16位。

"wakeup event in progress"代表系统是否有wake events正在处理,在combined_event_count的低16位。

当系统有wakeup events上报时,调用wakeup events framework的接口active该wakeup source,然后"wakeup event in progress"加1。

代码语言:javascript
复制
static void wakeup_source_activate(struct wakeup_source *ws)
{
	unsigned int cec;

	/*
	 * active wakeup source should bring the system
	 * out of PM_SUSPEND_FREEZE state
	 */
	freeze_wake();

	ws->active = true;
	ws->active_count++;
	ws->last_time = ktime_get();
	if (ws->autosleep_enabled)
		ws->start_prevent_time = ws->last_time;

	/* Increment the counter of events in progress. */
	cec = atomic_inc_return(&combined_event_count);

	trace_wakeup_source_activate(ws->name, cec);
}

那什么时候"registered wakeup events"加1呢? 答案是在"wakeup event in progress"减1的时候,"registered wakeup events"加1。因为这时候一个wakeup event刚处理完毕,就代表系统已经发生过一个wakeup event。

代码语言:javascript
复制
	/*
	 * Increment the counter of registered wakeup events and decrement the
	 * couter of wakeup events in progress simultaneously.
	 */
	cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);

当然了此处的注释写的也很清楚。

那接着再用linux中的实现方法回答上述的问题: wakeup event framework是如何保存当前系统中所有的wakeup event? 以及如何如何判断当前是否有wake events正在处理?

1. 获取combined_event_count的高16位就可以知道有多少wakeup event。

2. 判断combined_event_count的低16位是否为零,就知道有没有wakeup event在处理。

实现流程

既然明白了上述的原理,就按照此原理一步一步分析代码的处理流程即可。

a. 在进行suspend操作之前,需要获取wakeup event的数量。之前说过wakeup event的数量存在combined_event_count的高16位,而获取该值可以通过split_counters接口,所以可以直接搜索该接口即可。

代码语言:javascript
复制
bool pm_get_wakeup_count(unsigned int *count, bool block)
{
	unsigned int cnt, inpr;

	if (block) {
		DEFINE_WAIT(wait);

		for (;;) {
			prepare_to_wait(&wakeup_count_wait_queue, &wait,
					TASK_INTERRUPTIBLE);
			split_counters(&cnt, &inpr);
			if (inpr == 0 || signal_pending(current))
				break;

			schedule();
		}
		finish_wait(&wakeup_count_wait_queue, &wait);
	}

	split_counters(&cnt, &inpr);
	*count = cnt;
	return !inpr;
}

1. 如果block为false, 直接通过split_counters就可以获取wakeup event的总数,存在count返回。同时返回"wakeup event in progress"的状态,如果返回false,说明有wakeup events在处理,则不允许suspend,否则可以。

2. 如果block为ture,就会定义一个wait队列,等待"wakeup in event progress"为0,然后在返回count。

b. 获得到当前的"wakeup event"总数后,就需要将此值存到全局变量saved_count中。

代码语言:javascript
复制
bool pm_save_wakeup_count(unsigned int count)
{
	unsigned int cnt, inpr;
	unsigned long flags;

	events_check_enabled = false;
	spin_lock_irqsave(&events_lock, flags);
	split_counters(&cnt, &inpr);
	if (cnt == count && inpr == 0) {
		saved_count = count;
		events_check_enabled = true;
	}
	spin_unlock_irqrestore(&events_lock, flags);
	return events_check_enabled;
}

1. 首先获取"wakeup event in progress"和"register wakeup event"的值。

2. 然后比较传入的count是否等于"register wakeup event" 同时"wakeup event in progress"需要等于0。如果都不满足,说明在存储之前发生了wakeup event。

3. 置位events_check_enabled的值为true。如果此值为false,wakeup event 检测机制就会不起作用的。

c. 假设在suspend的过程中,发生了wakeup event事件。同时上报到wakeup event framework。

d. 在susupend的流程中,就会调用pm_wakeup_pending接口检测是否有wakeup event发生。比如如下代码:

代码语言:javascript
复制
	error = syscore_suspend();
	if (!error) {
		*wakeup = pm_wakeup_pending();
		if (!(suspend_test(TEST_CORE) || *wakeup)) {
			trace_suspend_resume(TPS("machine_suspend"),
				state, true);
			error = suspend_ops->enter(state);
			trace_suspend_resume(TPS("machine_suspend"),
				state, false);
			events_check_enabled = false;
		} else if (*wakeup) {
			pm_get_active_wakeup_sources(suspend_abort,
				MAX_SUSPEND_ABORT_LEN);
			log_suspend_abort_reason(suspend_abort);
			error = -EBUSY;
		}
		syscore_resume();
	}

在suspend的最后阶段会再次调用pending接口检测是否有wakeup event发生的。

代码语言:javascript
复制
bool pm_wakeup_pending(void)
{
	unsigned long flags;
	bool ret = false;

	spin_lock_irqsave(&events_lock, flags);
	if (events_check_enabled) {
		unsigned int cnt, inpr;

		split_counters(&cnt, &inpr);
		ret = (cnt != saved_count || inpr > 0);
		events_check_enabled = !ret;
	}
	spin_unlock_irqrestore(&events_lock, flags);

	if (ret) {
		pr_info("PM: Wakeup pending, aborting suspend\n");
		pm_print_active_wakeup_sources();
	}

	return ret || pm_abort_suspend;
}

1. 判断events_check_enabled是否为true,如果为false,就不会abort suspend流程。

2. 如果events_check_enabled为true,获取"registered wakeup event"和"wakeup event in progress"的值。判断registered wakeup event的值是否和saved_count的值不等,且wakeup event in progress大于0,说明有新的wakeup event发生,不能suspend,清除event_check_enabled标志。

3. 否则registered wakeup event等于saved_count且wakeup event in progress等于0,说明没有新的wakeup event发生,继续睡眠。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 实现原理
  • 实现流程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档