android 休眠與喚醒II

ndroid 休眠(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
特性介紹
Early Suspend
Early suspend 是android 引進的一種機制, 這種機制在上游備受爭議,這裏 不作評論. 這個機制做用在關閉顯示的時候, 在這個時候, 一些和顯示有關的 設備, 好比LCD背光, 好比重力感應器, 觸摸屏, 這些設備都會關掉, 可是系統可能仍是在運行狀態(這時候還有wake lock)進行任務的處理, 例如在掃描 SD卡上的文件等. 在嵌入式設備中, 背光是一個很大的電源消耗,因此 android會加入這樣一種機制. linux

Late Resume
Late Resume 是和suspend 配套的一種機制, 是在內核喚醒完畢開始執行的. 主要就是喚醒在Early Suspend的時候休眠的設備. android

Wake Lock
Wake Lock 在Android的電源管理系統中扮演一個核心的角色. Wake Lock是一種鎖的機制, 只要有人拿着這個鎖, 系統就沒法進入休眠, 能夠被用戶態程序和內核得到. 這個鎖能夠是有超時的或者是沒有超時的, 超時的鎖會在時間過去之後自動解鎖. 若是沒有鎖了或者超時了, 內核就會啓動休眠的那套機制來進入休眠. 函數

Android Suspend
當用戶寫入mem 或者 standby到 /sys/power/state中的時候, state_store()會被調用, 而後Android會在這裏調用 request_suspend_state() 而標準的Linux會在這裏進入enter_state()這個函數. 若是請求的是休眠, 那麼early_suspend這個workqueue就會被調用,而且進入early_suspend狀態. debug

void request_suspend_state(suspend_state_t new_state) rest

{ 接口

       unsigned long irqflags; 進程

       int old_sleep; 事件

       spin_lock_irqsave(&state_lock, irqflags); get

       old_sleep = state & SUSPEND_REQUESTED; cmd

       if (debug_mask & DEBUG_USER_STATE) {

               struct timespec ts;

               struct rtc_time tm;

               getnstimeofday(&ts);

               rtc_time_to_tm(ts.tv_sec, &tm);

               pr_info("request_suspend_state: %s (%d->%d) at %lld " 

                      "(%d-d-d d:d:d. lu UTC)",

                       new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",

                       requested_suspend_state, new_state,

                       ktime_to_ns(ktime_get()),

                       tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,

                       tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);

       }

       if (!old_sleep && new_state != PM_SUSPEND_ON) {

               state |= SUSPEND_REQUESTED;

               queue_work(suspend_work_queue, &early_suspend_work);

       } else if (old_sleep && new_state == PM_SUSPEND_ON) { 

              state &= ~SUSPEND_REQUESTED;

               wake_lock(&main_wake_lock);

               queue_work(suspend_work_queue, &late_resume_work);

       }

       requested_suspend_state = new_state;

       spin_unlock_irqrestore(&state_lock, irqflags);

}

Early Suspend
在early_suspend()函數中, 首先會檢查如今請求的狀態仍是否是suspend, 來防止suspend的請求會在這個時候取消掉(由於這個時候用戶進程還在運行),如 果須要退出, 就簡單的退出了. 若是沒有, 這個函數就會把early suspend中 註冊的一系列的回調都調用一次, 而後同步文件系統, 而後放棄掉 main_wake_lock, 這個wake lock是一個沒有超時的鎖,若是這個鎖不釋放, 那 麼系統就沒法進入休眠.

static void early_suspend(struct work_struct *work)

{

       struct early_suspend *pos;

       unsigned long irqflags;

       int abort = 0;

       mutex_lock(&early_suspend_lock);

       spin_lock_irqsave(&state_lock, irqflags);

       if (state == SUSPEND_REQUESTED)

               state |= SUSPENDED;

       else

               abort = 1;

       spin_unlock_irqrestore(&state_lock, irqflags);

       if (abort) {

               if (debug_mask & DEBUG_SUSPEND)

                       pr_info("early_suspend: abort, state %d", state);

               mutex_unlock(&early_suspend_lock);

               goto abort;

       }

       if (debug_mask & DEBUG_SUSPEND)

               pr_info("early_suspend: call handlers");

       list_for_each_entry(pos, &early_suspend_handlers, link) {

               if (pos->suspend != NULL)

                       pos->suspend(pos);

       }

       mutex_unlock(&early_suspend_lock);

       if (debug_mask & DEBUG_SUSPEND)

               pr_info("early_suspend: sync");

       sys_sync();

abort:

       spin_lock_irqsave(&state_lock, irqflags);

       if (state == SUSPEND_REQUESTED_AND_SUSPENDED)

               wake_unlock(&main_wake_lock);

       spin_unlock_irqrestore(&state_lock, irqflags);

}

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)

{

       struct early_suspend *pos;

       unsigned long irqflags;

       int abort = 0;

       mutex_lock(&early_suspend_lock);

       spin_lock_irqsave(&state_lock, irqflags);

       if (state == SUSPENDED)

               state &= ~SUSPENDED;

       else

               abort = 1;

       spin_unlock_irqrestore(&state_lock, irqflags);

       if (abort) {

               if (debug_mask & DEBUG_SUSPEND)

                       pr_info("late_resume: abort, state %d", state);

               goto abort;

       }

       if (debug_mask & DEBUG_SUSPEND)

               pr_info("late_resume: call handlers");

       list_for_each_entry_reverse(pos, &early_suspend_handlers, link);

               if (pos->resume != NULL)

                       pos->resume(pos);

       if (debug_mask & DEBUG_SUSPEND)

               pr_info("late_resume: done");

abort:

       mutex_unlock(&early_suspend_lock);

}

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)

{

       int ret;

       int entry_event_num;

       if (has_wake_lock(WAKE_LOCK_SUSPEND)) {

               if (debug_mask & DEBUG_SUSPEND) 

                      pr_info("suspend: abort suspend");

               return;

       }

       entry_event_num = current_event_num;

       sys_sync();

       if (debug_mask & DEBUG_SUSPEND)

               pr_info("suspend: enter suspend");

       ret = pm_suspend(requested_suspend_state);

       if (current_event_num == entry_event_num) {

               wake_lock_timeout(&unknown_wakeup, HZ / 2);

       }

}

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輸出。
相關文章
相關標籤/搜索