linux autosleep,autosleep

來源:互聯網
上載者:User

linux autosleep,autosleep

autosleep.c這個檔案代碼量總共100行出頭,但是其作為Linux低功耗的入口,在linux低功耗流程中卻佔據著舉足輕重的作用。

首先,代碼一,在初始化函數裡,會做兩件事情,一件是建立一個wake source ,供本檔案其他函數為防止進入低功耗時使用;另一件就是建立一個名字為autosleep的工作隊列,用於低功耗work對象附著、調度進入低功耗。

代碼一:

int __init pm_autosleep_init(void){autosleep_ws = wakeup_source_register("autosleep");if (!autosleep_ws)return -ENOMEM;autosleep_wq = alloc_ordered_workqueue("autosleep", 0);if (autosleep_wq)return 0;wakeup_source_unregister(autosleep_ws);return -ENOMEM;}

代碼二,而對於函數pm_autosleep_set_state來講,則是觸發低功耗進入條件的第一個介面,我們可以通過往init.rc裡添加write /sys/power/autosleep mem來觸發(或者控制台輸入echo mem > /sys/power/autosleep),曾經在調試linux低功耗時,系統一直不進入睡眠,後來才打聽到原來是沒有配置此命令。在此函數中,我們可以看到這裡使用了初始化中註冊的wake source來防止進入睡眠(__pm_stay_awake(autosleep_ws));當我們在init.rc裡加入write /sys/power/autosleep mem這條語句後,系統啟動到一定階段,就會調用到該介面,該函數會在if (state > PM_SUSPEND_ON)滿足條件進入到if分支,調用queue_up_suspend_work();來把建立的work對象加入到初始化時建立的工作隊列中

代碼二:

int pm_autosleep_set_state(suspend_state_t state){#ifndef CONFIG_HIBERNATIONif (state >= PM_SUSPEND_MAX)return -EINVAL;#endif__pm_stay_awake(autosleep_ws);mutex_lock(&autosleep_lock);autosleep_state = state;__pm_relax(autosleep_ws);if (state > PM_SUSPEND_ON) {pm_wakep_autosleep_enabled(true);queue_up_suspend_work();} else {pm_wakep_autosleep_enabled(false);}mutex_unlock(&autosleep_lock);return 0;}

代碼三:

static DECLARE_WORK(suspend_work, try_to_suspend);void queue_up_suspend_work(void){if (!work_pending(&suspend_work) && autosleep_state > PM_SUSPEND_ON)queue_work(autosleep_wq, &suspend_work);}


代碼四,而建立的work對象的回呼函數try_to_suspend則是本檔案的另一個重要介面,該函數會檢查當前wake lock鎖的持有狀態,如果還有組件持有反對鎖,則停止睡眠,重新觸發工作隊列;該檔案又引出了其他兩個相關組件,即pm_suspend()介面所在的suspend.c和pm_get_wakeup_count介面所在的檔案Wakeup.c。suspend.c主要就是進入真正的睡眠流程的內容,而wakeup.c則是wake lock鎖的相關內容,這兩部分稍後我們再做介紹。

代碼四:

static void try_to_suspend(struct work_struct *work){unsigned int initial_count, final_count;if (!pm_get_wakeup_count(&initial_count, true))goto out;mutex_lock(&autosleep_lock);if (!pm_save_wakeup_count(initial_count)) {mutex_unlock(&autosleep_lock);goto out;}if (autosleep_state == PM_SUSPEND_ON) {mutex_unlock(&autosleep_lock);return;}if (autosleep_state >= PM_SUSPEND_MAX)hibernate();elsepm_suspend(autosleep_state);mutex_unlock(&autosleep_lock);if (!pm_get_wakeup_count(&final_count, false))goto out;/* * If the wakeup occured for an unknown reason, wait to prevent the * system from trying to suspend and waking up in a tight loop. */if (final_count == initial_count)schedule_timeout_uninterruptible(HZ / 2); out:queue_up_suspend_work();}



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.