简介
休眠/唤醒在嵌入式Linux中是非常重要的部分,嵌入式设备尽可能的进入休眠状 态来延长电池的续航时间.这篇文章就详细介绍一下Linux中休眠/唤醒是如何工作 的, 还有Android中如何把这部分和Linux的机制联系起来的.
作者: zhangjiejing <kzjeef#gmail.com> Date: 2010-04-07, http://www.thinksrc.com
版本信息
- Linux Kernel: v2.6.28
- Android: v2.0
对于休眠(suspend)的简单介绍
在Linux中,休眠主要分三个主要的步骤:
- 冻结用户态进程和内核态任务
- 调用注册的设备的suspend的回调函数
- 顺序是按照注册顺序
- 休眠核心设备和使CPU进入休眠态冻结进程是内核把进程列表中所有的进程的状态都设置为停止,并且保存下所有进程的上下文. 当这些进程被解冻的时候,他们是不知道自己被冻结过的,只是简单的继续执行.如何让Linux进入休眠呢?用户可以通过读写sys文件/sys /power/state 是实现控制系统进入休眠. 比如
# echo standby > /sys/power/state<br style="word-wrap:break-word; margin:0px; padding:0px" />
命令系统进入休眠. 也可以使用
# cat /sys/power/state<br style="word-wrap:break-word; margin:0px; padding:0px" />
来得到内核支持哪几种休眠方式.
Linux Suspend 的流程
相关的文件:
你可以通过访问 Linux内核网站 来得到源代码,下面是文件的路径:
- linux_soruce/kernel/power/main.c
- linux_source/kernel/arch/xxx/mach-xxx/pm.c
- linux_source/driver/base/power/main.c
接下来让我们详细的看一下Linux是怎么休眠/唤醒的. Let 's going to see how these happens.
用户对于/sys/power/state 的读写会调用到 main.c中的state_store(), 用户可以写入 const char * const pm_state[] 中定义的字符串, 比如"mem", "standby".
然后state_store()会调用enter_state(), 它首先会检查一些状态参数,然后同步文件系统. 下面是代码:
/**<br style="word-wrap:break-word; margin:0px; padding:0px" /> * enter_state - Do common work of entering low-power state.<br style="word-wrap:break-word; margin:0px; padding:0px" /> * @state: pm_state structure for state we're entering.<br style="word-wrap:break-word; margin:0px; padding:0px" /> *<br style="word-wrap:break-word; margin:0px; padding:0px" /> * Make sure we're the only ones trying to enter a sleep state. Fail<br style="word-wrap:break-word; margin:0px; padding:0px" /> * if someone has beat us to it, since we don't want anything weird to<br style="word-wrap:break-word; margin:0px; padding:0px" /> * happen when we wake up.<br style="word-wrap:break-word; margin:0px; padding:0px" /> * Then, do the setup for suspend, enter the state, and cleaup (after<br style="word-wrap:break-word; margin:0px; padding:0px" /> * we've woken up).<br style="word-wrap:break-word; margin:0px; padding:0px" /> */<br style="word-wrap:break-word; margin:0px; padding:0px" />static int enter_state(suspend_state_t state)<br style="word-wrap:break-word; margin:0px; padding:0px" />{<br style="word-wrap:break-word; margin:0px; padding:0px" /> int error;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (!valid_state(state))<br style="word-wrap:break-word; margin:0px; padding:0px" /> return -ENODEV;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (!mutex_trylock(&pm_mutex))<br style="word-wrap:break-word; margin:0px; padding:0px" /> return -EBUSY;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> printk(KERN_INFO "PM: Syncing filesystems ... ");<br style="word-wrap:break-word; margin:0px; padding:0px" /> sys_sync();<br style="word-wrap:break-word; margin:0px; padding:0px" /> printk("done.\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);<br style="word-wrap:break-word; margin:0px; padding:0px" /> error = suspend_prepare();<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (error)<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Unlock;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_test(TEST_FREEZER))<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Finish;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_debug("PM: Entering %s sleep\n", pm_states[state]);<br style="word-wrap:break-word; margin:0px; padding:0px" /> error = suspend_devices_and_enter(state);<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> Finish:<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_debug("PM: Finishing wakeup.\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_finish();<br style="word-wrap:break-word; margin:0px; padding:0px" /> Unlock:<br style="word-wrap:break-word; margin:0px; padding:0px" /> mutex_unlock(&pm_mutex);<br style="word-wrap:break-word; margin:0px; padding:0px" /> return error;<br style="word-wrap:break-word; margin:0px; padding:0px" />}<br style="word-wrap:break-word; margin:0px; padding:0px" />
准备, 冻结进程
当进入到suspend_prepare()中以后, 它会给suspend分配一个虚拟终端来输出信 息, 然后广播一个系统要进入suspend的Notify, 关闭掉用户态的helper进程, 然 后一次调用suspend_freeze_processes()冻结所有的进程, 这里会保存所有进程 当前的状态, 也许有一些进程会拒绝进入冻结状态, 当有这样的进程存在的时候, 会导致冻结失败,此函数就会放弃冻结进程,并且解冻刚才冻结的所有进程.
/**<br style="word-wrap:break-word; margin:0px; padding:0px" /> * suspend_prepare - Do prep work before entering low-power state.<br style="word-wrap:break-word; margin:0px; padding:0px" /> *<br style="word-wrap:break-word; margin:0px; padding:0px" /> * This is common code that is called for each state that we're entering.<br style="word-wrap:break-word; margin:0px; padding:0px" /> * Run suspend notifiers, allocate a console and stop all processes.<br style="word-wrap:break-word; margin:0px; padding:0px" /> */<br style="word-wrap:break-word; margin:0px; padding:0px" />static int suspend_prepare(void)<br style="word-wrap:break-word; margin:0px; padding:0px" />{<br style="word-wrap:break-word; margin:0px; padding:0px" /> int error;<br style="word-wrap:break-word; margin:0px; padding:0px" /> unsigned int free_pages;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (!suspend_ops || !suspend_ops->enter)<br style="word-wrap:break-word; margin:0px; padding:0px" /> return -EPERM;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> pm_prepare_console();<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (error)<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Finish;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> error = usermodehelper_disable();<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (error)<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Finish;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_freeze_processes()) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> error = -EAGAIN;<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Thaw;<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> free_pages = global_page_state(NR_FREE_PAGES);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (free_pages < FREE_PAGE_NUMBER) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_debug("PM: free some memory\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> shrink_all_memory(FREE_PAGE_NUMBER - free_pages);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (nr_free_pages() < FREE_PAGE_NUMBER) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> error = -ENOMEM;<br style="word-wrap:break-word; margin:0px; padding:0px" /> printk(KERN_ERR "PM: No enough memory\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (!error)<br style="word-wrap:break-word; margin:0px; padding:0px" /> return 0;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> Thaw:<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_thaw_processes();<br style="word-wrap:break-word; margin:0px; padding:0px" /> usermodehelper_enable();<br style="word-wrap:break-word; margin:0px; padding:0px" /> Finish:<br style="word-wrap:break-word; margin:0px; padding:0px" /> pm_notifier_call_chain(PM_POST_SUSPEND);<br style="word-wrap:break-word; margin:0px; padding:0px" /> pm_restore_console();<br style="word-wrap:break-word; margin:0px; padding:0px" /> return error;<br style="word-wrap:break-word; margin:0px; padding:0px" />}<br style="word-wrap:break-word; margin:0px; padding:0px" />
让外设进入休眠
现在, 所有的进程(也包括workqueue/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进入 省电状态. 这时候,就已经休眠了.代码的执行也就停在这里了.
/**<br style="word-wrap:break-word; margin:0px; padding:0px" /> * suspend_devices_and_enter - suspend devices and enter the desired system<br style="word-wrap:break-word; margin:0px; padding:0px" /> * sleep state.<br style="word-wrap:break-word; margin:0px; padding:0px" /> * @state: state to enter<br style="word-wrap:break-word; margin:0px; padding:0px" /> */<br style="word-wrap:break-word; margin:0px; padding:0px" />int suspend_devices_and_enter(suspend_state_t state)<br style="word-wrap:break-word; margin:0px; padding:0px" />{<br style="word-wrap:break-word; margin:0px; padding:0px" /> int error, ftrace_save;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (!suspend_ops)<br style="word-wrap:break-word; margin:0px; padding:0px" /> return -ENOSYS;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_ops->begin) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> error = suspend_ops->begin(state);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (error)<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Close;<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_console();<br style="word-wrap:break-word; margin:0px; padding:0px" /> ftrace_save = __ftrace_enabled_save();<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_test_start();<br style="word-wrap:break-word; margin:0px; padding:0px" /> error = device_suspend(PMSG_SUSPEND);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (error) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> printk(KERN_ERR "PM: Some devices failed to suspend\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Recover_platform;<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_test_finish("suspend devices");<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_test(TEST_DEVICES))<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Recover_platform;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_ops->prepare) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> error = suspend_ops->prepare();<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (error)<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Resume_devices;<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_test(TEST_PLATFORM))<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Finish;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> error = disable_nonboot_cpus();<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (!error && !suspend_test(TEST_CPUS))<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_enter(state);<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> enable_nonboot_cpus();<br style="word-wrap:break-word; margin:0px; padding:0px" /> Finish:<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_ops->finish)<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_ops->finish();<br style="word-wrap:break-word; margin:0px; padding:0px" /> Resume_devices:<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_test_start();<br style="word-wrap:break-word; margin:0px; padding:0px" /> device_resume(PMSG_RESUME);<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_test_finish("resume devices");<br style="word-wrap:break-word; margin:0px; padding:0px" /> __ftrace_enabled_restore(ftrace_save);<br style="word-wrap:break-word; margin:0px; padding:0px" /> resume_console();<br style="word-wrap:break-word; margin:0px; padding:0px" /> Close:<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_ops->end)<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_ops->end();<br style="word-wrap:break-word; margin:0px; padding:0px" /> return error;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> Recover_platform:<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (suspend_ops->recover)<br style="word-wrap:break-word; margin:0px; padding:0px" /> suspend_ops->recover();<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto Resume_devices;<br style="word-wrap:break-word; margin:0px; padding:0px" />}<br style="word-wrap:break-word; margin:0px; padding:0px" />
Resume
如果在休眠中系统被中断或者其他事件唤醒, 接下来的代码就会开始执行, 这个 唤醒的顺序是和休眠的循序相反的,所以系统设备和总线会首先唤醒,使能系统中 断, 使能休眠时候停止掉的非启动CPU, 以及调用suspend_ops->finish(), 而且 在suspend_devices_and_enter()函数中也会继续唤醒每个设备,使能虚拟终端, 最后调用 suspend_ops->end().
在返回到enter_state()函数中的, 当 suspend_devices_and_enter() 返回以后, 外设已经唤醒了, 但是进程和任务都还是冻结状态, 这里会调用suspend_finish()来解冻这些进程和任务, 而且发出Notify来表示系统已经从suspend状态退出, 唤醒终端.
到这里, 所有的休眠和唤醒就已经完毕了, 系统继续运行了.
Android 休眠(suspend)
在一个打过android补丁的内核中, state_store()函数会走另外一条路,会进 入到request_suspend_state()中, 这个文件在earlysuspend.c中. 这些功能都 是android系统加的, 后面会对earlysuspend和late resume 进行介绍.
涉及到的文件:
- linux_source/kernel/power/main.c
- linux_source/kernel/power/earlysuspend.c
- linux_source/kernel/power/wakelock.c
特性介绍
Android Suspend
当用户写入mem 或者 standby到 /sys/power/state中的时候, state_store()会被调用, 然后Android会在这里调用 request_suspend_state() 而标准的Linux会在这里进入enter_state()这个函数. 如果请求的是休眠, 那么early_suspend这个workqueue就会被调用,并且进入early_suspend状态.
void request_suspend_state(suspend_state_t new_state)<br style="word-wrap:break-word; margin:0px; padding:0px" />{<br style="word-wrap:break-word; margin:0px; padding:0px" /> unsigned long irqflags;<br style="word-wrap:break-word; margin:0px; padding:0px" /> int old_sleep;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_lock_irqsave(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" /> old_sleep = state & SUSPEND_REQUESTED;<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_USER_STATE) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> struct timespec ts;<br style="word-wrap:break-word; margin:0px; padding:0px" /> struct rtc_time tm;<br style="word-wrap:break-word; margin:0px; padding:0px" /> getnstimeofday(&ts);<br style="word-wrap:break-word; margin:0px; padding:0px" /> rtc_time_to_tm(ts.tv_sec, &tm);<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("request_suspend_state: %s (%d->%d) at %lld "<br style="word-wrap:break-word; margin:0px; padding:0px" /> "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",<br style="word-wrap:break-word; margin:0px; padding:0px" /> new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",<br style="word-wrap:break-word; margin:0px; padding:0px" /> requested_suspend_state, new_state,<br style="word-wrap:break-word; margin:0px; padding:0px" /> ktime_to_ns(ktime_get()),<br style="word-wrap:break-word; margin:0px; padding:0px" /> tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,<br style="word-wrap:break-word; margin:0px; padding:0px" /> tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (!old_sleep && new_state != PM_SUSPEND_ON) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> state |= SUSPEND_REQUESTED;<br style="word-wrap:break-word; margin:0px; padding:0px" /> queue_work(suspend_work_queue, &early_suspend_work);<br style="word-wrap:break-word; margin:0px; padding:0px" /> } else if (old_sleep && new_state == PM_SUSPEND_ON) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> state &= ~SUSPEND_REQUESTED;<br style="word-wrap:break-word; margin:0px; padding:0px" /> wake_lock(&main_wake_lock);<br style="word-wrap:break-word; margin:0px; padding:0px" /> queue_work(suspend_work_queue, &late_resume_work);<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> requested_suspend_state = new_state;<br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_unlock_irqrestore(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" />}<br style="word-wrap:break-word; margin:0px; padding:0px" />
Early Suspend
在early_suspend()函数中, 首先会检查现在请求的状态还是否是suspend, 来 防止suspend的请求会在这个时候取消掉(因为这个时候用户进程还在运行),如 果需要退出, 就简单的退出了. 如果没有, 这个函数就会把early suspend中 注册的一系列的回调都调用一次, 然后同步文件系统, 然后放弃掉 main_wake_lock, 这个wake lock是一个没有超时的锁,如果这个锁不释放, 那 么系统就无法进入休眠.
static void early_suspend(struct work_struct *work)<br style="word-wrap:break-word; margin:0px; padding:0px" />{<br style="word-wrap:break-word; margin:0px; padding:0px" /> struct early_suspend *pos;<br style="word-wrap:break-word; margin:0px; padding:0px" /> unsigned long irqflags;<br style="word-wrap:break-word; margin:0px; padding:0px" /> int abort = 0;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> mutex_lock(&early_suspend_lock);<br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_lock_irqsave(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (state == SUSPEND_REQUESTED)<br style="word-wrap:break-word; margin:0px; padding:0px" /> state |= SUSPENDED;<br style="word-wrap:break-word; margin:0px; padding:0px" /> else<br style="word-wrap:break-word; margin:0px; padding:0px" /> abort = 1;<br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_unlock_irqrestore(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (abort) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("early_suspend: abort, state %d\n", state);<br style="word-wrap:break-word; margin:0px; padding:0px" /> mutex_unlock(&early_suspend_lock);<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto abort;<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("early_suspend: call handlers\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> list_for_each_entry(pos, &early_suspend_handlers, link) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (pos->suspend != NULL)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pos->suspend(pos);<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> mutex_unlock(&early_suspend_lock);<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("early_suspend: sync\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> sys_sync();<br style="word-wrap:break-word; margin:0px; padding:0px" />abort:<br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_lock_irqsave(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (state == SUSPEND_REQUESTED_AND_SUSPENDED)<br style="word-wrap:break-word; margin:0px; padding:0px" /> wake_unlock(&main_wake_lock);<br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_unlock_irqrestore(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" />}<br style="word-wrap:break-word; margin:0px; padding:0px" />
Late Resume
当所有的唤醒已经结束以后, 用户进程都已经开始运行了, 唤醒通常会是以下的几种原因:
- 来电
如果是来电, 那么Modem会通过发送命令给rild来让rild通知WindowManager有 来电响应,这样就会远程调用PowerManagerService来写"on" 到 /sys/power/state 来执行late resume的设备, 比如点亮屏幕等.
- 用户按键用户按键事件会送到WindowManager中, WindowManager会处理这些 按键事件,按键分为几种情况, 如果案件不是唤醒键(能够唤醒系统的按键) 那么WindowManager会主动放弃wakeLock来使系统进入再次休眠, 如果按键 是唤醒键,那么WindowManger就会调用PowerManagerService中的接口来执行 Late Resume.
- Late Resume 会依次唤醒前面调用了Early Suspend的设备.
static void late_resume(struct work_struct *work)<br style="word-wrap:break-word; margin:0px; padding:0px" />{<br style="word-wrap:break-word; margin:0px; padding:0px" /> struct early_suspend *pos;<br style="word-wrap:break-word; margin:0px; padding:0px" /> unsigned long irqflags;<br style="word-wrap:break-word; margin:0px; padding:0px" /> int abort = 0;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> mutex_lock(&early_suspend_lock);<br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_lock_irqsave(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (state == SUSPENDED)<br style="word-wrap:break-word; margin:0px; padding:0px" /> state &= ~SUSPENDED;<br style="word-wrap:break-word; margin:0px; padding:0px" /> else<br style="word-wrap:break-word; margin:0px; padding:0px" /> abort = 1;<br style="word-wrap:break-word; margin:0px; padding:0px" /> spin_unlock_irqrestore(&state_lock, irqflags);<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (abort) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("late_resume: abort, state %d\n", state);<br style="word-wrap:break-word; margin:0px; padding:0px" /> goto abort;<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("late_resume: call handlers\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> list_for_each_entry_reverse(pos, &early_suspend_handlers, link)<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (pos->resume != NULL)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pos->resume(pos);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("late_resume: done\n");<br style="word-wrap:break-word; margin:0px; padding:0px" />abort:<br style="word-wrap:break-word; margin:0px; padding:0px" /> mutex_unlock(&early_suspend_lock);<br style="word-wrap:break-word; margin:0px; padding:0px" />}<br style="word-wrap:break-word; margin:0px; padding:0px" />
Wake Lock
我们接下来看一看wake lock的机制是怎么运行和起作用的, 主要关注 wakelock.c文件就可以了.
wake lock 有加锁和解锁两种状态, 加锁的方式有两种, 一种是永久的锁住, 这样的锁除非显示的放开, 是不会解锁的, 所以这种锁的使用是非常小心的. 第二种是超时锁, 这种锁会锁定系统唤醒一段时间, 如果这个时间过去了, 这个锁会自动解除.
锁有两种类型:
- WAKE_LOCK_SUSPEND 这种锁会防止系统进入睡眠
- WAKE_LOCK_IDLE 这种锁不会影响系统的休眠, 作用我不是很清楚.
在wake lock中, 会有3个地方让系统直接开始suspend(), 分别是:
- 在wake_unlock()中, 如果发现解锁以后没有任何其他的wake lock了, 就开始休眠
- 在定时器都到时间以后, 定时器的回调函数会查看是否有其他的wake lock, 如果没有, 就在这里让系统进入睡眠.
- 在wake_lock() 中, 对一个wake lock加锁以后, 会再次检查一下有没有锁, 我想这里的检查是没有必要的, 更好的方法是使加锁的这个操作原子化, 而 不是繁冗的检查. 而且这样的检查也有可能漏掉.
Suspend
当wake_lock 运行 suspend()以后, 在wakelock.c的suspend()函数会被调用,这 个函数首先sync文件系统,然后调用pm_suspend(request_suspend_state),接 下来pm_suspend()就会调用enter_state()来进入Linux的休眠流程..
static void suspend(struct work_struct *work)<br style="word-wrap:break-word; margin:0px; padding:0px" />{<br style="word-wrap:break-word; margin:0px; padding:0px" /> int ret;<br style="word-wrap:break-word; margin:0px; padding:0px" /> int entry_event_num;<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> if (has_wake_lock(WAKE_LOCK_SUSPEND)) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("suspend: abort suspend\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> return;<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" /><br style="word-wrap:break-word; margin:0px; padding:0px" /> entry_event_num = current_event_num;<br style="word-wrap:break-word; margin:0px; padding:0px" /> sys_sync();<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (debug_mask & DEBUG_SUSPEND)<br style="word-wrap:break-word; margin:0px; padding:0px" /> pr_info("suspend: enter suspend\n");<br style="word-wrap:break-word; margin:0px; padding:0px" /> ret = pm_suspend(requested_suspend_state);<br style="word-wrap:break-word; margin:0px; padding:0px" /> if (current_event_num == entry_event_num) {<br style="word-wrap:break-word; margin:0px; padding:0px" /> wake_lock_timeout(&unknown_wakeup, HZ / 2);<br style="word-wrap:break-word; margin:0px; padding:0px" /> }<br style="word-wrap:break-word; margin:0px; padding:0px" />}<br style="word-wrap:break-word; margin:0px; padding:0px" />
Android于标准Linux休眠的区别
pm_suspend() 虽然会调用enter_state()来进入标准的Linux休眠流程,但是还 是有一些区别:
- 当进入冻结进程的时候, android首先会检查有没有wake lock,如果没有, 才会停止这些进程, 因为在开始suspend和冻结进程期间有可能有人申请了 wake lock,如果是这样, 冻结进程会被中断.
- 在suspend_late()中, 会最后检查一次有没有wake lock, 这有可能是某种快速申请wake lock,并且快速释放这个锁的进程导致的,如果有这种情况, 这里会返回错误, 整个suspend就会全部放弃.如果pm_suspend()成功了,LOG的输出可以通过在kernel cmd里面增加 "no_console_suspend" 来看到suspend和resume过程中的log输出。
http://blog.chinaunix.net/uid-27875-id-2136126.html
http://www.byywee.com/page/M0/S633/633654.html
http://blog.csdn.net/h490516509/article/details/7719432
http://www.cnblogs.com/sdphome/archive/2011/11/07/2240371.html
http://blog.csdn.net/angle_birds/article/details/8461633
http://blog.sina.com.cn/s/blog_a6559d920101auj8.html#wakelock
kernel/kernel/power/main.c
kernel/drivers/base/power/main.c
kernel/arch/arm/mach-sc8810/pm.c
kernel/kernel/power/earlysuspend.c
kernel/kernel/power/suspend.c
kernel/kernel/power/power.h
kernel/include/linux/suspend.h