Linux Kernel and Android休眠與喚醒

來源:互聯網
上載者:User
標籤:Linux and Kernel Android

版本資訊

    Linux Kernel: v2.6.28 
    Android: v2.0

對於休眠(suspend)的簡單介紹

在Linux中,休眠主要分三個主要的步驟:版本資訊

  • Linux Kernel: v2.6.28
  • Android: v2.0
對於休眠(suspend)的簡單介紹

在Linux中,休眠主要分三個主要的步驟:

  1. 凍結使用者態進程和核心態任務
  2. 調用註冊的裝置的suspend的回呼函數
    • 順序是按照註冊順序
  3. 休眠核心裝置和使CPU進入休眠態凍結進程是核心把進程列表中所有的進 程的狀態都設定為停止,並且儲存下所有進程的上下文. 當這些進程被解凍的時候,他們是不知道自己被凍結過的,只是簡單的繼續執行.如何讓Linux進入休眠呢?使用者可以通過讀寫sys檔案/sys /power/state 是實現控制系統進入休眠. 比如
    # echo standby > /sys/power/state

    命令系統進入休眠. 也可以使用

    # cat /sys/power/state

    來得到核心支援哪幾種休眠方式.

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(), 它首先會檢查一些狀態參數,然後同步檔案系統. 下面是代碼:

/**
* enter_state - Do common work of entering low-power state.
* @state: pm_state structure for state we're entering.
*
* Make sure we're the only ones trying to enter a sleep state. Fail
* if someone has beat us to it, since we don't want anything weird to
* happen when we wake up.
* Then, do the setup for suspend, enter the state, and cleaup (after
* we've woken up).
*/
static int enter_state(suspend_state_t state)
{
int error;

if (!valid_state(state))
return -ENODEV;

if (!mutex_trylock(&pm_mutex))
return -EBUSY;

printk(KERN_INFO "PM: Syncing filesystems ... ");
sys_sync();
printk("done.
");

pr_debug("PM: Preparing system for %s sleep
", pm_states[state]);
error = suspend_prepare();
if (error)
goto Unlock;

if (suspend_test(TEST_FREEZER))
goto Finish;

pr_debug("PM: Entering %s sleep
", pm_states[state]);
error = suspend_devices_and_enter(state);

Finish:
pr_debug("PM: Finishing wakeup.
");
suspend_finish();
Unlock:
mutex_unlock(&pm_mutex);
return error;
}
準備, 凍結進程

當進入到suspend_prepare()中以後, 它會給suspend分配一個虛擬終端來輸出信 息, 然後廣播一個系統要進入suspend的Notify, 關閉掉使用者態的helper進程, 然後一次調用suspend_freeze_processes()凍結所有的進程, 這裡會儲存所有進程 當前的狀態, 也許有一些進程會拒絕進入凍結狀態, 當有這樣的進程存在的時候, 會導致凍結失敗,此函數就會放棄凍結進程,並且解凍剛才凍結的所有進程.

/**
* suspend_prepare - Do prep work before entering low-power state.
*
* This is common code that is called for each state that we're entering.
* Run suspend notifiers, allocate a console and stop all processes.
*/
static int suspend_prepare(void)
{
int error;
unsigned int free_pages;

if (!suspend_ops || !suspend_ops->enter)
return -EPERM;

pm_prepare_console();

error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
if (error)
goto Finish;

error = usermodehelper_disable();
if (error)
goto Finish;

if (suspend_freeze_processes()) {
error = -EAGAIN;
goto Thaw;
}

free_pages = global_page_state(NR_FREE_PAGES);
if (free_pages < FREE_PAGE_NUMBER) {
pr_debug("PM: free some memory
");
shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
if (nr_free_pages() < FREE_PAGE_NUMBER) {
error = -ENOMEM;
printk(KERN_ERR "PM: No enough memory
");
}
}
if (!error)
return 0;

Thaw:
suspend_thaw_processes();
usermodehelper_enable();
Finish:
pm_notifier_call_chain(PM_POST_SUSPEND);
pm_restore_console();
return error;
}
讓外設進入休眠

現在, 所有的進程(也包括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進入 省電狀態. 這時候,就已經休眠了.代碼的執行也就停在這裡了.

/**
* suspend_devices_and_enter - suspend devices and enter the desired system
* sleep state.
* @state: state to enter
*/
int suspend_devices_and_enter(suspend_state_t state)
{
int error, ftrace_save;

if (!suspend_ops)
return -ENOSYS;

if (suspend_ops->begin) {
error = suspend_ops->begin(state);
if (error)
goto Close;
}
suspend_console();
ftrace_save = __ftrace_enabled_save();
suspend_test_start();
error = device_suspend(PMSG_SUSPEND);
if (error) {
printk(KERN_ERR "PM: Some devices failed to suspend
");
goto Recover_platform;
}
suspend_test_finish("suspend devices");
if (suspend_test(TEST_DEVICES))
goto Recover_platform;

if (suspend_ops->prepare) {
error = suspend_ops->prepare();
if (error)
goto Resume_devices;
}

if (suspend_test(TEST_PLATFORM))
goto Finish;

error = disable_nonboot_cpus();
if (!error && !suspend_test(TEST_CPUS))
suspend_enter(state);

enable_nonboot_cpus();
Finish:
if (suspend_ops->finish)
suspend_ops->finish();
Resume_devices:
suspend_test_start();
device_resume(PMSG_RESUME);
suspend_test_finish("resume devices");
__ftrace_enabled_restore(ftrace_save);
resume_console();
Close:
if (suspend_ops->end)
suspend_ops->end();
return error;

Recover_platform:
if (suspend_ops->recover)
suspend_ops->recover();
goto Resume_devices;
}
Resume

如果在休眠中系統被中斷或者其他事件喚醒, 接下來的代碼就會開始執行, 這個喚醒的順序是和休眠的循序相反的,所以系統裝置和匯流排會首先喚醒,使能系統中 斷, 使能休眠時候停止掉的非啟動CPU, 以及調用suspend_ops->finish(), 而且在suspend_devices_and_enter()函數中也會繼續喚醒每個裝置,使能虛擬終端, 最後調用 suspend_ops->end().

在返回到enter_state()函數中的, 當 suspend_devices_and_enter() 返回以後, 外設已經喚醒了, 但是進程和任務都還是凍結狀態, 這裡會調用suspend_finish()來解凍這些進程和任務, 而且發出Notify來表示系統已經從suspend狀態退出, 喚醒終端.

到這裡, 所有的休眠和喚醒就已經完畢了, 系統繼續運行了.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.