Linux与Android休眠对比(1)
Linux休眠(suspend)的简单介绍
在Linux中(PC和嵌入式系统),休眠主要分三个主要步骤:
1. 冻结用户态进出和内核态任务;
2. 调用注册的设备和suspend的回调函数,调用顺序按照注册顺序;
3. 休眠核心设备和使CPU进入休眠态冻结进程是内核把进程列表中所有的进程的状态都设置为停止,并且保存所有进程的上下文。当这些进程被解冻时,它们是不知道自己被冻结过的,只是简单的继续执行。如何让Linux进入休眠呢?用户可以通过读写sys文件/sys/power/state使系统进入休眠,比如:
Echo standby > /sys/power/state
命令是系统进入休眠,也可以使用:
Cat /sys/power/state
来查看内核支持哪几种休眠方式。
Linux 休眠与唤醒
相关的文件的路径:
linux/kernel/power/main.c
linux/kernel/arch/xxx/mach-xxx/pm.c
linux/driver/base/power/main.c
接下来让我们详细的看一下Linux是怎么休眠/唤醒的。kernel/power/main.c文件是整个框架的入口。用户可以通过读写sys文件/sys/power/state实现控制系统进入低功耗状态.用户对于/sys/power/state的读写会调用到main.c中的state_store(),用户可以写入constchar * const pm_states[] 中定义的字符串,比如“on”,“mem”,“standby”,“disk”.
然后state_store()会调用enter_state(),它首先会检查一些状态参数,然后同步文件系统. 下面是代码:
/**
261 * enter_state - Do common work of entering low-power state.
262 * @state: pm_state structure for state we're entering.
263 *
264 * Make sure we're the only ones trying to enter a sleep state. Fail
265 * if someone has beat us to it, since we don't want anything weird to
266 * happen when we wake up.
267 * Then, do the setup for suspend, enter the state, and cleaup (after
268 * we've woken up).
269 */
270int enter_state(suspend_state_t state)
271{
272 int error;
273
274 if (!valid_state(state))
275 return -ENODEV;
276
277 if (!mutex_trylock(&pm_mutex))
278 return -EBUSY;
279
280 printk(KERN_INFO "PM: Syncing filesystems ... ");
281 sys_sync();
282 printk("done.\n");
283
284 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
285 error = suspend_prepare();
286 if (error)
287 goto Unlock;
288
289 if (suspend_test(TEST_FREEZER))
290 goto Finish;
291
292 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
293 pm_restrict_gfp_mask();
294 error = suspend_devices_and_enter(state);
295 pm_restore_gfp_mask();
296
297 Finish:
298 pr_debug("PM: Finishing wakeup.\n");
299 suspend_finish();
300 Unlock:
301 mutex_unlock(&pm_mutex);
302 return error;
303}
准备, 冻结进程
当进入到suspend_prepare()中以后,它会给suspend分配一个虚拟终端来输出信息pm_prepare_console(),然后广播一个系统要进入suspend的Notify--pm_notifier_call_chain(),关闭掉用户态的helper进程usermodehelper_disable(), 然后一次调用suspend_freeze_processes()冻结所有的进程,这里会保存所有进程当前的状态,也许有一些进程会拒绝进入冻结状态, 当有这样的进程存在的时候,会导致冻结失败,此函数就会放弃冻结进程,并且解冻刚才冻结的所有进程.suspend_thaw_processes()和usermodehelper_enable()。
84/**
85 * suspend_prepare - Do prep work before entering low-power state.
86 *
87 * This is common code that is called for each state that we're entering.
88 * Run suspend notifiers, allocate a console and stop all processes.
89 */
90static int suspend_prepare(void)
91{
92 int error;
93
94 if (!suspend_ops || !suspend_ops->enter)
95 return -EPERM;
96
97 pm_prepare_console();
98
99 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
100 if (error)
101 goto Finish;
102
103 error = suspend_freeze_processes();
104 if (!error)
105 return 0;
106
107 suspend_stats.failed_freeze++;
108 dpm_save_failed_step(SUSPEND_FREEZE);
109 Finish:
110 pm_notifier_call_chain(PM_POST_SUSPEND);
111 pm_restore_console();
112 return error;
113}
让外设进入休眠
现在, 所有的进程(也包括workqueue[queue_work()]/kthread) 都已经停止了, 内核态任务有可能在停止的时候握有一些信号量, 所以如果这时候在外设里面去解锁这个信号量有可能会发生死锁, 所以在外设的suspend()函数里面作lock/unlock锁要非常小心,这里建议设计的时候就不要在suspend()里面等待锁. 而且因为suspend的时候,有一些Log是无法输出的,所以一旦出现问题,非常难调试.
然后kernel在这里会尝试释放一些内存,最后会调用suspend_devices_and_enter()来把所有的外设休眠, 在这个函数中, 如果平台注册了suspend_pos(通常是在板级定义中定义和注册), 这里就会调用suspend_ops->begin(), 然后driver/base/power/main.c 中的device_suspend()->dpm_suspend() 会被调用,他们会依次调用驱动的suspend() 回调来休眠掉所有的设备.
当所有的设备休眠以后, suspend_ops->prepare()会被调用, 这个函数通常会作一些准备工作来让板机进入休眠. 接下来Linux,在多核的CPU中的非启动CPU会被关掉, 通过注释看到是避免这些其他的CPU造成race condion,接下来的以后只有一个CPU在运行了.
suspend_ops 是板级的电源管理操作, 通常注册在文件arch/xxx/mach-xxx/pm.c 中.
接下来,suspend_enter()会被调用, 这个函数会关闭arch irq, 调用device_power_down(), 它会调用suspend_late()函数, 这个函数是系统真正进入休眠最后调用的函数, 通常会在这个函数中作最后的检查. 如果检查没问题, 接下来休眠所有的系统设备和总线, 并且调用suspend_pos->enter() 来使CPU进入 省电状态. 这时候,就已经休眠了.代码的执行也就停在这里了.
195/**
196 * suspend_devices_and_enter - suspend devices and enter the desired system
197 * sleep state.
198 * @state: state to enter
199 */
200int suspend_devices_and_enter(suspend_state_t state)
201{
202 int error;
203 bool wakeup = false;
204
205 if (!suspend_ops)
206 return -ENOSYS;
207
208 trace_machine_suspend(state);
209 if (suspend_ops->begin) {
210 error = suspend_ops->begin(state);
211 if (error)
212 goto Close;
213 }
214 suspend_console();
215 suspend_test_start();
216 error = dpm_suspend_start(PMSG_SUSPEND);
217 if (error) {
218 printk(KERN_ERR "PM: Some devices failed to suspend\n");
219 goto Recover_platform;
220 }
221 suspend_test_finish("suspend devices");
222 if (suspend_test(TEST_DEVICES))
223 goto Recover_platform;
224
225 do {
226 error = suspend_enter(state, &wakeup);
227 } while (!error && !wakeup
228 && suspend_ops->suspend_again && suspend_ops->suspend_again());
229
230 Resume_devices:
231 suspend_test_start();
232 dpm_resume_end(PMSG_RESUME);
233 suspend_test_finish("resume devices");
234 resume_console();
235 Close:
236 if (suspend_ops->end)
237 suspend_ops->end();
238 trace_machine_suspend(PWR_EVENT_EXIT);
239 return error;
240
241 Recover_platform:
242 if (suspend_ops->recover)
243 suspend_ops->recover();
244 goto Resume_devices;
245}
Resume唤醒
如果在休眠中系统被中断或者其他事件唤醒, 接下来的代码就会开始执行, 这个唤醒的顺序是和休眠的循序相反的,所以系统设备和总线会首先唤醒,使能系统中断, 使能休眠时候停止掉的非启动CPU, 以及调用suspend_ops->finish(), 而且在suspend_devices_and_enter()函数中也会继续唤醒每个设备,使能虚拟终端, 最后调用suspend_ops->end().
在返回到enter_state()函数中的, 当suspend_devices_and_enter() 返回以后, 外设已经唤醒了, 但是进程和任务都还是冻结状态, 这里会调用suspend_finish()来解冻这些进程和任务, 而且发出Notify来表示系统已经从suspend状态退出, 唤醒终端.
到这里, 所有的休眠和唤醒就已经完毕了, 系统继续运行了.
源地址:http://hi.baidu.com/ch_ff/blog/item/005b61fb603c278359ee90aa.html