Linux电源管理(7)_Wakeup events framework
1. 前言
本文繼續(xù)“Linux電源管理(6)_Generic PM之Suspend功能”中有關(guān)suspend同步以及PM wakeup的話題。這個(gè)話題,是近幾年Linux kernel最具爭(zhēng)議的話題之一,在國(guó)外Linux開發(fā)論壇,經(jīng)常可以看到圍繞該話題的辯論。辯論的時(shí)間跨度和空間跨度可以持續(xù)很長(zhǎng),且無(wú)法達(dá)成一致。
wakeup events framework是這個(gè)話題的一個(gè)臨時(shí)性的解決方案,包括wake lock、wakeup count、autosleep等機(jī)制。它們就是本文的話題。
2. wakeup events framework要解決的問題
我們知道,系統(tǒng)處于suspend狀態(tài),可通過wakeup events喚醒。具體的wakeup events可以是按鍵按下,可以是充電器插入,等等。但是,如果在suspend的過程中,產(chǎn)生了wakeup events,怎么辦?答案很肯定,"wakeup"系統(tǒng)。由于此時(shí)系統(tǒng)沒有真正suspend,所以這的"wakeup"是個(gè)假動(dòng)作,實(shí)際上只是終止suspend。
但由于系統(tǒng)在suspend的過程中,會(huì)進(jìn)行process freeze、 device suspend等操作,而這些操作可能導(dǎo)致內(nèi)核或用戶空間程序不能及時(shí)獲取wakeup events,從而使系統(tǒng)不能正確wakeup,這就是wakeup events framework要解決的問題:system suspend和system wakeup events之間的同步問題。
3. wakeup events framework的功能總結(jié)
仔細(xì)推敲一下,上面所講的同步問題包括兩種情況:
情況1:內(nèi)核空間的同步
wakeup events產(chǎn)生后,通常是以中斷的形式通知device driver。driver會(huì)處理events,處理的過程中,系統(tǒng)不能suspend。
注1:同步問題只存在于中斷開啟的情況,因?yàn)槿糁袛嚓P(guān)閉,就不會(huì)產(chǎn)生wakeup events,也就不存在同步的概念。
情況2:用戶空間的同步
一般情況下,driver對(duì)wakeup events處理后,會(huì)交給用戶空間程序繼續(xù)處理,處理的過程,也不允許suspend。這又可以分為兩種情況:
1)進(jìn)行后續(xù)處理的用戶進(jìn)程,根本沒有機(jī)會(huì)被調(diào)度,即該wakeup events無(wú)法上報(bào)到用戶空間。
2)進(jìn)行后續(xù)處理的用戶進(jìn)程被調(diào)度,處理的過程中(以及處理結(jié)束后,決定終止suspend操作),系統(tǒng)不能suspend。
因此,wakeup events framework就包括3大功能:
解決內(nèi)核空間同步問題(framework的核心功能);
解決用戶空間同步問題的情景1(wakeup count功能);
解決用戶空間同步問題的情景2(wake lock功能) 。
注2:
用戶空間同步的兩種情況,咋一看,非常合乎情理,kernel你得好好處理!但事實(shí)上,該同步問題牽涉到了另外一個(gè)比較有爭(zhēng)議的話題:日常的電源管理機(jī)制。是否要基于suspend實(shí)現(xiàn)?系統(tǒng)何時(shí)進(jìn)入低功耗狀態(tài),應(yīng)該由誰(shuí)決定?kernel還是用戶空間程序?
這最終會(huì)決定是否存在用空間同步問題。但是,在當(dāng)前這個(gè)時(shí)間點(diǎn),對(duì)這個(gè)話題,Linux kernel developers和Android developers持相反的觀點(diǎn)。這也造成了wakeup events framework在實(shí)現(xiàn)上的撕裂。Kernel的本意是不愿處理用戶空間同步問題的,但為了兼容Android平臺(tái),不得不增加相關(guān)的功能(Wakeup count和Wake lock)。
蝸蝸會(huì)在下一篇文章和大家探討該話題,本文就先f(wàn)ocus在wakeup events framework上。
4. wakeup events framework architecture
下面圖片描述了wakeup events framework的architecture:
圖片中紅色邊框的block是wakeup events相關(guān)的block:
抽象wakeup source和wakeup event的概念;
向各個(gè)device driver提供wakeup source的注冊(cè)、使能等接口;
向各個(gè)device driver提供wakeup event的上報(bào)、停止等接口;
向上層的PM core(包括wakeup count、auto sleep、suspend、hibernate等模塊)提供wakeup event的查詢接口,以判斷是否可以suspend、是否需要終止正在進(jìn)行的suspend。
2)wakeup events framework sysfs,將設(shè)備的wakeup信息,以sysfs的形式提供到用戶空間,供用戶空間程序查詢、配置。在drivers/base/power/sysfs.c中實(shí)現(xiàn)。
3)wake lock/unlock,為了兼容Android舊的wakeup lock機(jī)制而留下的一個(gè)后門,擴(kuò)展wakeup events framework的功能,允許用戶空間程序報(bào)告/停止wakeup events。換句話說(shuō),該后門允許用戶空間的任一程序決定系統(tǒng)是否可以休眠。
4)wakeup count,基于wakeup events framework,解決用戶空間同步的問題。
5)auto sleep,允許系統(tǒng)在沒有活動(dòng)時(shí)(即一段時(shí)間內(nèi),沒有產(chǎn)生wakeup event),自動(dòng)休眠。
注3:在Linux kernel看來(lái),power是系統(tǒng)的核心資源,不應(yīng)開放給用戶程序隨意訪問(wake lock機(jī)制違背了這個(gè)原則)。而在運(yùn)行時(shí)的電源管理過程中,系統(tǒng)何時(shí)進(jìn)入低功耗狀態(tài),也不是用戶空間程序能決定的(auto sleep中槍了)。因此,kernel對(duì)上述功能的支持,非常的不樂意,我們可以從kernel/power/main.c中sysfs attribute文件窺見一斑(只要定義了PM_SLEEP,就一定支持wakeup count功能,但autosleep和wake lock功能,由另外的宏定義控制):
1: static struct attribute * g[] = {2: &state_attr.attr,3: #ifdef CONFIG_PM_TRACE4: &pm_trace_attr.attr,5: &pm_trace_dev_match_attr.attr,6: #endif7: #ifdef CONFIG_PM_SLEEP8: &pm_async_attr.attr,9: &wakeup_count_attr.attr,10: #ifdef CONFIG_PM_AUTOSLEEP11: &autosleep_attr.attr,12: #endif13: #ifdef CONFIG_PM_WAKELOCKS14: &wake_lock_attr.attr,15: &wake_unlock_attr.attr,16: #endif17: #ifdef CONFIG_PM_DEBUG18: &pm_test_attr.attr,19: #endif20: #ifdef CONFIG_PM_SLEEP_DEBUG21: &pm_print_times_attr.attr,22: #endif23: #endif24: #ifdef CONFIG_FREEZER25: &pm_freeze_timeout_attr.attr,26: #endif27: NULL,28: };5. 代碼分析
5.1 wakeup source和wakeup event
在kernel中,可以喚醒系統(tǒng)的只有設(shè)備(struct device),但并不是每個(gè)設(shè)備都具備喚醒功能,那些具有喚醒功能的設(shè)備稱作wakeup source。是時(shí)候回到這篇文章中了(Linux設(shè)備模型(5)_device和device driver),在那里,介紹struct device結(jié)構(gòu)時(shí),涉及到一個(gè)struct dev_pm_info類型的power變量,蝸蝸說(shuō)留待后面的專題講解。我們?cè)倩貞浺幌聅truct device結(jié)構(gòu):
1: struct device {2: ...3: struct dev_pm_info power;4: ...5: };該結(jié)構(gòu)中有一個(gè)power變量,保存了和wakeup event相關(guān)的信息,讓我們接著看一下struct dev_pm_info數(shù)據(jù)結(jié)構(gòu)(只保留和本文有關(guān)的內(nèi)容):
1: struct dev_pm_info {2: ...3: unsigned int can_wakeup:1;4: ...5: #ifdef CONFIG_PM_SLEEP6: ...7: struct wakeup_source *wakeup;8: ...9: #else10: unsigned int should_wakeup:1;11: #endif12: };can_wakeup,標(biāo)識(shí)本設(shè)備是否具有喚醒能力。只有具備喚醒能力的設(shè)備,才會(huì)在sysfs中有一個(gè)power目錄,用于提供所有的wakeup信息,這些信息是以struct wakeup_source的形式組織起來(lái)的。也就是上面wakeup指針。具體有哪些信息呢?讓我們看看struct wakeup_source的定義。
1: /* include\linux\pm_wakeup.h */2: struct wakeup_source {3: const char *name;4: struct list_head entry;5: spinlock_t lock;6: struct timer_list timer;7: unsigned long timer_expires;8: ktime_t total_time;9: ktime_t max_time;10: ktime_t last_time;11: ktime_t start_prevent_time;12: ktime_t prevent_sleep_time;13: unsigned long event_count;14: unsigned long active_count;15: unsigned long relax_count;16: unsigned long expire_count;17: unsigned long wakeup_count;18: bool active:1;19: bool autosleep_enabled:1;20: };因此,一個(gè)wakeup source代表了一個(gè)具有喚醒能力的設(shè)備,也稱該設(shè)備為一個(gè)wakeup source。該結(jié)構(gòu)中各個(gè)字段的意義如下:
name,該wakeup source的名稱,一般為對(duì)應(yīng)的device name(有個(gè)例外,就是wakelock);
entery,用于將所有的wakeup source掛在一個(gè)鏈表中;
timer、timer_expires,一個(gè)wakeup source產(chǎn)生了wakeup event,稱作wakeup source activate,wakeup event處理完畢后(不再需要系統(tǒng)為此保持active),稱作deactivate。activate和deactivate的操作可以由driver親自設(shè)置,也可以在activate時(shí),指定一個(gè)timeout時(shí)間,時(shí)間到達(dá)后,由wakeup events framework自動(dòng)將其設(shè)置為deactivate狀態(tài)。這里的timer以及expires時(shí)間,就是用來(lái)實(shí)現(xiàn)該功能;
total_time,該wakeup source處于activate狀態(tài)的總時(shí)間(可以指示該wakeup source對(duì)應(yīng)的設(shè)備的繁忙程度、耗電等級(jí));
max_time,該wakeup source持續(xù)處于activate狀態(tài)的最大時(shí)間(越長(zhǎng)越不合理);
last_time,該wakeup source上次active的開始時(shí)間;
start_prevent_time,該wakeup source開始阻止系統(tǒng)自動(dòng)睡眠(auto sleep)的時(shí)間點(diǎn);
prevent_sleep_time,該wakeup source阻止系統(tǒng)自動(dòng)睡眠的總時(shí)間;
event_count,wakeup source上報(bào)的event個(gè)數(shù);
active_count,wakeup source activate的次數(shù);
relax_count, wakeup source deactivate的次數(shù);
expire_count,wakeup source timeout到達(dá)的次數(shù);
wakeup_count,wakeup source終止suspend過程的次數(shù);
active,wakeup source的activate狀態(tài);
autosleep_enabled,記錄系統(tǒng)auto sleep的使能狀態(tài)(每個(gè)wakeup source都重復(fù)記錄這樣一個(gè)狀態(tài),這種設(shè)計(jì)真實(shí)不敢恭維!)
wakeup source代表一個(gè)具有喚醒能力的設(shè)備,該設(shè)備產(chǎn)生的可以喚醒系統(tǒng)的事件,就稱作wakeup event。當(dāng)wakeup source產(chǎn)生wakeup event時(shí),需要將wakeup source切換為activate狀態(tài);當(dāng)wakeup event處理完畢后,要切換為deactivate狀態(tài)。因此,我們?cè)賮?lái)理解一下幾個(gè)wakeup source比較混淆的變量:event_count, active_count和wakeup_count:
event_count,wakeup source產(chǎn)生的wakeup event的個(gè)數(shù);
active_count,產(chǎn)生wakeup event時(shí),wakeup source需要切換到activate狀態(tài)。但并不是每次都要切換,因此有可能已經(jīng)處于activate狀態(tài)了。因此active_count可能小于event_count,換句話說(shuō),很有可能在前一個(gè)wakeup event沒被處理完時(shí),又產(chǎn)生了一個(gè)。這從一定程度上反映了wakeup source所代表的設(shè)備的繁忙程度;
wakeup_count,wakeup source在suspend過程中產(chǎn)生wakeup event的話,就會(huì)終止suspend過程,該變量記錄了wakeup source終止suspend過程的次數(shù)(如果發(fā)現(xiàn)系統(tǒng)總是suspend失敗,檢查一下各個(gè)wakeup source的該變量,就可以知道問題出在誰(shuí)身上了)。
5.2 幾個(gè)counters
在drivers\base\power\wakeup.c中,有幾個(gè)比較重要的計(jì)數(shù)器,是wakeup events framework的實(shí)現(xiàn)基礎(chǔ),包括:
1)registered wakeup events和saved_count
記錄了系統(tǒng)運(yùn)行以來(lái)產(chǎn)生的所有wakeup event的個(gè)數(shù),在wakeup source上報(bào)event時(shí)加1。
這個(gè)counter對(duì)解決用戶空間同步問題很有幫助,因?yàn)橐话闱闆r下(無(wú)論是用戶程序主動(dòng)suspend,還是auto sleep),由專門的進(jìn)程(或線程)觸發(fā)suspend。當(dāng)這個(gè)進(jìn)程判斷系統(tǒng)滿足suspend條件,決定suspend時(shí),會(huì)記錄一個(gè)counter值(saved_count)。在后面suspend的過程中,如果系統(tǒng)發(fā)現(xiàn)counter有變,則說(shuō)明系統(tǒng)產(chǎn)生了新的wakeup event,這樣就可以終止suspend。
該功能即是wakeup count功能,會(huì)在后面更詳細(xì)的說(shuō)明。
2)wakeup events in progress
記錄正在處理的event個(gè)數(shù)。
當(dāng)wakeup source產(chǎn)生wakeup event時(shí),會(huì)通過wakeup events framework提供的接口將wakeup source設(shè)置為activate狀態(tài)。當(dāng)該event處理結(jié)束后,設(shè)置為deactivate狀態(tài)。activate到deactivate的區(qū)間,表示該event正在被處理。
當(dāng)系統(tǒng)中有任何正在被處理的wakeup event時(shí),則不允許suspend。如果suspend正在進(jìn)行,則要終止。
思考一個(gè)問題:registered wakeup events在什么時(shí)候增加?答案是在wakeup events in progress減小時(shí),因?yàn)橐呀?jīng)完整的處理完一個(gè)event了,可以記錄在案了。
1: /*2: * Combined counters of registered wakeup events and wakeup events in progress.3: * They need to be modified together atomically, so it's better to use one4: * atomic variable to hold them both.5: */6: static atomic_t combined_event_count = ATOMIC_INIT(0);7: 8: #define IN_PROGRESS_BITS (sizeof(int) * 4)9: #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)10: 11: static void split_counters(unsigned int *cnt, unsigned int *inpr)12: {13: unsigned int comb = atomic_read(&combined_event_count);14: 15: *cnt = (comb >> IN_PROGRESS_BITS);16: *inpr = comb & MAX_IN_PROGRESS;17: }定義和讀取。
1: cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);wakeup events in progress減1,registered wakeup events加1,這個(gè)語(yǔ)句簡(jiǎn)直是美輪美奐,讀者可以仔細(xì)品味一下,絕對(duì)比看xxx片還過癮,哈哈。
1: cec = atomic_inc_return(&combined_event_count);wakeup events in progress加1。
5.3 wakeup events framework的核心功能
wakeup events framework的核心功能體現(xiàn)在它向底層的設(shè)備驅(qū)動(dòng)所提供的用于上報(bào)wakeup event的接口,這些接口根據(jù)操作對(duì)象可分為兩類,具體如下。
類型一(操作對(duì)象為wakeup source,編寫設(shè)備驅(qū)動(dòng)時(shí),一般不會(huì)直接使用):
1: /* include/linux/pm_wakeup.h */2: extern void __pm_stay_awake(struct wakeup_source *ws);3: extern void __pm_relax(struct wakeup_source *ws);4: extern void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec);__pm_stay_awake,通知PM core,ws產(chǎn)生了wakeup event,且正在處理,因此不允許系統(tǒng)suspend(stay awake);
__pm_relax,通知PM core,ws沒有正在處理的wakeup event,允許系統(tǒng)suspend(relax);
__pm_wakeup_event,為上邊兩個(gè)接口的功能組合,通知PM core,ws產(chǎn)生了wakeup event,會(huì)在msec毫秒內(nèi)處理結(jié)束(wakeup events framework自動(dòng)relax)。
注4:__pm_stay_awake和__pm_relax應(yīng)成對(duì)調(diào)用。
注5:上面3個(gè)接口,均可以在中斷上下文調(diào)用。
類型二(操作對(duì)象為device,為設(shè)備驅(qū)動(dòng)的常用接口):
1: /* include/linux/pm_wakeup.h */2: extern int device_wakeup_enable(struct device *dev);3: extern int device_wakeup_disable(struct device *dev);4: extern void device_set_wakeup_capable(struct device *dev, bool capable);5: extern int device_init_wakeup(struct device *dev, bool val);6: extern int device_set_wakeup_enable(struct device *dev, bool enable);7: extern void pm_stay_awake(struct device *dev);8: extern void pm_relax(struct device *dev);9: extern void pm_wakeup_event(struct device *dev, unsigned int msec);device_set_wakeup_capable,設(shè)置dev的can_wakeup標(biāo)志(enable或disable,可參考5.1小節(jié)),并增加或移除該設(shè)備在sysfs相關(guān)的power文件;
device_wakeup_enable/device_wakeup_disable/device_set_wakeup_enable,對(duì)于can_wakeup的設(shè)備,使能或者禁止wakeup功能。主要是對(duì)struct wakeup_source結(jié)構(gòu)的相關(guān)操作;
device_init_wakeup,設(shè)置dev的can_wakeup標(biāo)志,若是enable,同時(shí)調(diào)用device_wakeup_enable使能wakeup功能;
pm_stay_awake、pm_relax、pm_wakeup_event,直接調(diào)用上面的wakeup source操作接口,操作device的struct wakeup_source變量,處理wakeup events。
5.3.1 device_set_wakeup_capable
該接口位于在drivers/base/power/wakeup.c中,代碼如下:
1: void device_set_wakeup_capable(struct device *dev, bool capable)2: {3: if (!!dev->power.can_wakeup == !!capable)4: return;5: 6: if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {7: if (capable) {8: if (wakeup_sysfs_add(dev))9: return;10: } else {11: wakeup_sysfs_remove(dev);12: }13: }14: dev->power.can_wakeup = capable;15: }該接口的實(shí)現(xiàn)很簡(jiǎn)單,主要包括sysfs的add/remove和can_wakeup標(biāo)志的設(shè)置兩部分。如果設(shè)置can_wakeup標(biāo)志,則調(diào)用wakeup_sysfs_add,向該設(shè)備的sysfs目錄下添加power文件夾,并注冊(cè)相應(yīng)的attribute文件。如果清除can_wakeup標(biāo)志,執(zhí)行sysfs的移除操作。
wakeup_sysfs_add/wakeup_sysfs_remove位于drivers/base/power/sysfs.c中,對(duì)wakeup events framework來(lái)說(shuō),主要包括如下的attribute文件:
1: static struct attribute *wakeup_attrs[] = {2: #ifdef CONFIG_PM_SLEEP3: &dev_attr_wakeup.attr,4: &dev_attr_wakeup_count.attr,5: &dev_attr_wakeup_active_count.attr,6: &dev_attr_wakeup_abort_count.attr,7: &dev_attr_wakeup_expire_count.attr,8: &dev_attr_wakeup_active.attr,9: &dev_attr_wakeup_total_time_ms.attr,10: &dev_attr_wakeup_max_time_ms.attr,11: &dev_attr_wakeup_last_time_ms.attr,12: #ifdef CONFIG_PM_AUTOSLEEP13: &dev_attr_wakeup_prevent_sleep_time_ms.attr,14: #endif15: #endif16: NULL,17: };18: static struct attribute_group pm_wakeup_attr_group = {19: .name = power_group_name,20: .attrs = wakeup_attrs,21: }; 1: static struct attribute *wakeup_attrs[] = {2: #ifdef CONFIG_PM_SLEEP3: &dev_attr_wakeup.attr,4: &dev_attr_wakeup_count.attr,5: &dev_attr_wakeup_active_count.attr,6: &dev_attr_wakeup_abort_count.attr,7: &dev_attr_wakeup_expire_count.attr,8: &dev_attr_wakeup_active.attr,9: &dev_attr_wakeup_total_time_ms.attr,10: &dev_attr_wakeup_max_time_ms.attr,11: &dev_attr_wakeup_last_time_ms.attr,12: #ifdef CONFIG_PM_AUTOSLEEP13: &dev_attr_wakeup_prevent_sleep_time_ms.attr,14: #endif15: #endif16: NULL,17: };18: static struct attribute_group pm_wakeup_attr_group = {19: .name = power_group_name,20: .attrs = wakeup_attrs,21: };1)wakeup
讀,獲得設(shè)備wakeup功能的使能狀態(tài),返回"enabled"或"disabled"字符串。
寫,更改設(shè)備wakeup功能的使能狀態(tài),根據(jù)寫入的字符串("enabled"或"disabled"),調(diào)用device_set_wakeup_enable接口完成實(shí)際的狀態(tài)切換。
設(shè)備wakeup功能是否使能,取決于設(shè)備的can_wakeup標(biāo)志,以及設(shè)備是否注冊(cè)有相應(yīng)的struct wakeup_source指針。即can wakeup和may wakeup,如下:
1: /*2: * Changes to device_may_wakeup take effect on the next pm state change.3: */4: 5: static inline bool device_can_wakeup(struct device *dev)6: {7: return dev->power.can_wakeup;8: }9: 10: static inline bool device_may_wakeup(struct device *dev)11: {12: return dev->power.can_wakeup && !!dev->power.wakeup;13: }2)wakeup_count
只讀,獲取dev->power.wakeup->event_count值。有關(guān)event_count的意義,請(qǐng)參考5.1小節(jié),下同。順便抱怨一下,這個(gè)attribute文件的命名簡(jiǎn)直糟糕透頂了!直接用event_count就是了,用什么wakeup_count,會(huì)和wakeup source中的同名字段搞混淆的!
3)wakeup_active_count,只讀,獲取dev->power.wakeup->active_count值。
4)wakeup_abort_count,只讀,獲取dev->power.wakeup->wakeup_count值。
5)wakeup_expire_count,只讀,獲dev->power.wakeup->expire_count取值。
6)wakeup_active,只讀,獲取dev->power.wakeup->active值。
7)wakeup_total_time_ms,只讀,獲取dev->power.wakeup->total_time值,單位為ms。
8)wakeup_max_time_ms,只讀,獲dev->power.wakeup->max_time取值,單位為ms。
9)wakeup_last_time_ms,只讀,獲dev->power.wakeup->last_time取值,單位為ms。
10)wakeup_prevent_sleep_time_ms,只讀,獲取dev->power.wakeup->prevent_sleep_time值,單位為ms。
注6:閱讀上述代碼時(shí),我們可以看到很多類似“!!dev->power.can_wakeup == !!capable”的、帶有兩個(gè)“!”操作符的語(yǔ)句,是為了保證最后的操作對(duì)象非0即1。這從側(cè)面反映了內(nèi)核開發(fā)者的嚴(yán)謹(jǐn)程度,值得我們學(xué)習(xí)。
5.3.2 device_wakeup_enable/device_wakeup_disable/device_set_wakeup_enable
以device_wakeup_enable為例(其它類似,就不浪費(fèi)屏幕了):
1: int device_wakeup_enable(struct device *dev)2: {3: struct wakeup_source *ws;4: int ret;5: 6: if (!dev || !dev->power.can_wakeup)7: return -EINVAL;8: 9: ws = wakeup_source_register(dev_name(dev));10: if (!ws)11: return -ENOMEM;12: 13: ret = device_wakeup_attach(dev, ws);14: if (ret)15: wakeup_source_unregister(ws);16: 17: return ret;18: }也很簡(jiǎn)單:
a)若設(shè)備指針為空,或者設(shè)備不具備wakeup能力,免談,報(bào)錯(cuò)退出。
b)調(diào)用wakeup_source_register接口,以設(shè)備名為參數(shù),創(chuàng)建并注冊(cè)一個(gè)wakeup source。
c)調(diào)用device_wakeup_attach接口,將新建的wakeup source保存在dev->power.wakeup指針中。
wakeup_source_register接口的實(shí)現(xiàn)也比較簡(jiǎn)單,會(huì)先后調(diào)用wakeup_source_create、wakeup_source_prepare、wakeup_source_add等接口,所做的工作包括分配struct wakeup_source變量所需的內(nèi)存空間、初始化內(nèi)部變量、將新建的wakeup source添加到名稱為wakeup_sources的全局鏈表中、等等。
device_wakeup_attach接口更為直觀,不過有一點(diǎn)我們要關(guān)注,如果設(shè)備的dev->power.wakeup非空,也就是說(shuō)之前已經(jīng)有一個(gè)wakeup source了,是不允許再enable了的,會(huì)報(bào)錯(cuò)返回。
5.3.3 pm_stay_awake
當(dāng)設(shè)備有wakeup event正在處理時(shí),需要調(diào)用該接口通知PM core,該接口的實(shí)現(xiàn)如下:
1: void pm_stay_awake(struct device *dev)2: {3: unsigned long flags;4: 5: if (!dev)6: return;7: 8: spin_lock_irqsave(&dev->power.lock, flags);9: __pm_stay_awake(dev->power.wakeup);10: spin_unlock_irqrestore(&dev->power.lock, flags);11: }呵呵,直接調(diào)用__pm_stay_awake,這也是本文的index里沒有該接口的原因。接著看代碼。
1: void __pm_stay_awake(struct wakeup_source *ws)2: {3: unsigned long flags;4: 5: if (!ws)6: return;7: 8: spin_lock_irqsave(&ws->lock, flags);9: 10: wakeup_source_report_event(ws);11: del_timer(&ws->timer);12: ws->timer_expires = 0;13: 14: spin_unlock_irqrestore(&ws->lock, flags);15: }由于pm_stay_awake報(bào)告的event需要經(jīng)過pm_relax主動(dòng)停止,因此就不再需要timer,所以__pm_stay_awake實(shí)現(xiàn)是直接調(diào)用wakeup_source_report_event,然后停止timer。接著看代碼:
1: static void wakeup_source_report_event(struct wakeup_source *ws)2: {3: ws->event_count++;4: /* This is racy, but the counter is approximate anyway. */5: if (events_check_enabled)6: ws->wakeup_count++;7: 8: if (!ws->active)9: wakeup_source_activate(ws);10: }a)增加wakeup source的event_count,表示該source又產(chǎn)生了一個(gè)event。
b)根據(jù)events_check_enabled變量的狀態(tài),決定是否增加wakeup_count。這和wakeup count的功能有關(guān),到時(shí)再詳細(xì)描述。
c)如果wakeup source沒有active,則調(diào)用wakeup_source_activate,activate之。這也是5.1小節(jié)所描述的,event_count和active_count的區(qū)別所在。wakeup_source_activate的代碼如下。
1: static void wakeup_source_activate(struct wakeup_source *ws)2: {3: unsigned int cec;4: 5: /*6: * active wakeup source should bring the system7: * out of PM_SUSPEND_FREEZE state8: */9: freeze_wake();10: 11: ws->active = true;12: ws->active_count++;13: ws->last_time = ktime_get();14: if (ws->autosleep_enabled)15: ws->start_prevent_time = ws->last_time;16: 17: /* Increment the counter of events in progress. */18: cec = atomic_inc_return(&combined_event_count);19: 20: trace_wakeup_source_activate(ws->name, cec);21: }a)調(diào)用freeze_wake,將系統(tǒng)從suspend to freeze狀態(tài)下喚醒。有關(guān)freeze功能,請(qǐng)參考相關(guān)的文章。
b)設(shè)置active標(biāo)志,增加active_count,更新last_time。
c)如果使能了autosleep,更新start_prevent_time,因?yàn)榇丝淘搘akeup source會(huì)開始阻止系統(tǒng)auto sleep。具體可參考auto sleep的功能描述。
d)增加“wakeup events in progress”計(jì)數(shù)(5.2小節(jié)有描述)。該操作是wakeup events framework的靈魂,增加該計(jì)數(shù),意味著系統(tǒng)正在處理的wakeup event數(shù)目不為零,則系統(tǒng)不能suspend。
到此,pm_stay_awake執(zhí)行結(jié)束,意味著系統(tǒng)至少正在處理一個(gè)wakeup event,因此不能suspend。那處理完成后呢?driver要調(diào)用pm_relax通知PM core。
5.3.4 pm_relax
pm_relax和pm_stay_awake成對(duì)出現(xiàn),用于在event處理結(jié)束后通知PM core,其實(shí)現(xiàn)如下:
1: /**2: * pm_relax - Notify the PM core that processing of a wakeup event has ended.3: * @dev: Device that signaled the event.4: *5: * Execute __pm_relax() for the @dev's wakeup source object.6: */7: void pm_relax(struct device *dev)8: {9: unsigned long flags;10: 11: if (!dev)12: return;13: 14: spin_lock_irqsave(&dev->power.lock, flags);15: __pm_relax(dev->power.wakeup);16: spin_unlock_irqrestore(&dev->power.lock, flags);17: }直接調(diào)用__pm_relax,如下:
1: void __pm_relax(struct wakeup_source *ws)2: {3: unsigned long flags;4: 5: if (!ws)6: return;7: 8: spin_lock_irqsave(&ws->lock, flags);9: if (ws->active)10: wakeup_source_deactivate(ws);11: spin_unlock_irqrestore(&ws->lock, flags);12: }如果該wakeup source處于active狀態(tài),調(diào)用wakeup_source_deactivate接口,deactivate之。deactivate接口和activate接口一樣,是wakeup events framework的核心邏輯,如下:
1: static void wakeup_source_deactivate(struct wakeup_source *ws)2: {3: unsigned int cnt, inpr, cec;4: ktime_t duration;5: ktime_t now;6: 7: ws->relax_count++;8: /*9: * __pm_relax() may be called directly or from a timer function.10: * If it is called directly right after the timer function has been11: * started, but before the timer function calls __pm_relax(), it is12: * possible that __pm_stay_awake() will be called in the meantime and13: * will set ws->active. Then, ws->active may be cleared immediately14: * by the __pm_relax() called from the timer function, but in such a15: * case ws->relax_count will be different from ws->active_count.16: */17: if (ws->relax_count != ws->active_count) {18: ws->relax_count--;19: return;20: }21: 22: ws->active = false;23: 24: now = ktime_get();25: duration = ktime_sub(now, ws->last_time);26: ws->total_time = ktime_add(ws->total_time, duration);27: if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))28: ws->max_time = duration;29: 30: ws->last_time = now;31: del_timer(&ws->timer);32: ws->timer_expires = 0;33: 34: if (ws->autosleep_enabled)35: update_prevent_sleep_time(ws, now);36: 37: /*38: * Increment the counter of registered wakeup events and decrement the39: * couter of wakeup events in progress simultaneously.40: */41: cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count);42: trace_wakeup_source_deactivate(ws->name, cec);43: 44: 45: split_counters(&cnt, &inpr);46: if (!inpr && waitqueue_active(&wakeup_count_wait_queue))47: wake_up(&wakeup_count_wait_queue);48: }a)relax_count加1(如果relax_count和active_count不等,則說(shuō)明有重復(fù)調(diào)用,要退出)。
b)清除active標(biāo)記。
c)更新total_time、max_time、last_time等變量。
d)如果使能auto sleep,更新相關(guān)的變量(后面再詳細(xì)描述)。
e)再欣賞一下藝術(shù),wakeup events in progress減1,registered wakeup events加1。
f)wakeup count相關(guān)的處理,后面再詳細(xì)說(shuō)明。
5.3.5 pm_wakeup_event
pm_wakeup_event是pm_stay_awake和pm_relax的組合版,在上報(bào)event時(shí),指定一個(gè)timeout時(shí)間,timeout后,自動(dòng)relax,一般用于不知道何時(shí)能處理完成的場(chǎng)景。該接口比較簡(jiǎn)單,就不一一描述了。
5.3.6 pm_wakeup_pending
drivers產(chǎn)生的wakeup events,最終要上報(bào)到PM core,PM core會(huì)根據(jù)這些events,決定是否要終止suspend過程。這表現(xiàn)在suspend過程中頻繁調(diào)用pm_wakeup_pending接口上(可參考“Linux電源管理(6)_Generic PM之Suspend功能”)。該接口的實(shí)現(xiàn)如下:
1: /**2: * pm_wakeup_pending - Check if power transition in progress should be aborted.3: *4: * Compare the current number of registered wakeup events with its preserved5: * value from the past and return true if new wakeup events have been registered6: * since the old value was stored. Also return true if the current number of7: * wakeup events being processed is different from zero.8: */9: bool pm_wakeup_pending(void)10: {11: unsigned long flags;12: bool ret = false;13: 14: spin_lock_irqsave(&events_lock, flags);15: if (events_check_enabled) {16: unsigned int cnt, inpr;17: 18: split_counters(&cnt, &inpr);19: ret = (cnt != saved_count || inpr > 0);20: events_check_enabled = !ret;21: }22: spin_unlock_irqrestore(&events_lock, flags);23: 24: if (ret)25: print_active_wakeup_sources();26: 27: return ret;28: }該接口的邏輯比較直觀,先拋開wakeup count的邏輯不談(后面會(huì)重點(diǎn)說(shuō)明),只要正在處理的events不為0,就返回true,調(diào)用者就會(huì)終止suspend。
5.4 wakeup count、wake lock和auto sleep
這篇文章寫的有點(diǎn)長(zhǎng)了,不能繼續(xù)了,這幾個(gè)功能,會(huì)接下來(lái)的文章中繼續(xù)分析。
總結(jié)
以上是生活随笔為你收集整理的Linux电源管理(7)_Wakeup events framework的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java程序获取时区不对,linux时区
- 下一篇: Linux中的sda, sdb, sdc