https://www.cnblogs.com/linhaostudy/p/12113548.html
閱讀目錄
正文
一、休眠概述
休眠,簡而言之就是設(shè)備在不需要工作的時(shí)候把一些部件、外設(shè)關(guān)掉(掉電或讓它進(jìn)入低功耗模式)。
為什么要休眠呢?一言以蔽之:省電。
休眠分主動(dòng)休眠和被動(dòng)休眠。主動(dòng)休眠:比如我電腦不用了,就通過設(shè)置讓系統(tǒng)進(jìn)入休眠模式;被動(dòng)休眠:系統(tǒng)檢測到自己閑的慌,為了節(jié)約故,自己就休眠去了。
廢話不敘。
二、Android休眠
休眠是內(nèi)核的核心工作,而Android是基于Linux內(nèi)核的,所以Android休眠和內(nèi)核有著千絲萬縷的聯(lián)系;由于Android的特殊應(yīng)用場景:移動(dòng)設(shè)備,所以Android休眠和內(nèi)核又有著特別的需求。
1、聯(lián)系:
Android設(shè)備停止使用,系統(tǒng)沒有什么事情可做,進(jìn)入休眠狀態(tài)的功能最終是由內(nèi)核去實(shí)現(xiàn)的;每一類硬件都有自己的驅(qū)動(dòng),具體的驅(qū)動(dòng)決定怎么進(jìn)入休眠以及處于何種層次的休眠。比如:對于platform_device,就按照platform_driver定義的規(guī)則,在suspend調(diào)用的時(shí)候,去做上面提到的事情:
structplatform_driver {int(*probe)(structplatform_device *);int(*remove)(structplatform_device *);void(*shutdown)(structplatform_device *);int(*suspend)(structplatform_device *, pm_message_t state);int(*resume)(structplatform_device *);structdevice_driver driver;conststructplatform_device_id *id_table;};
2、Android的特別需求:
比如對于自己的電腦,不用讓它休眠好了;但是對于我們形影不離的手機(jī),在休眠的時(shí)候還要睜一只眼:來電了要通知你,QQ啊微信啊什么的由信息了也要通知你,所以Android在Linux內(nèi)核休眠機(jī)制之上,提出了“Opportunistic Suspend”。
三、休眠實(shí)踐
絮絮叨叨這么多,下面讓我們切切實(shí)實(shí)體驗(yàn)下休眠。
1、休眠模式
休眠是分好幾種模式的,不同模式實(shí)現(xiàn)方式、耗電量不同,以下來自Documentation/power/states.txt:
The kernel supports four power management states generically, thoughoneisgenericandthe other three are dependentonplatform supportcodetoimplement the low-level detailsforeachstate.This file describeseachstate, what they arecommonly called, what ACPI state they mapto,andwhatstringtowriteto/sys/power/statetoenter that state state:Freeze / Low-Power IdleACPI state:S0String:"freeze"This stateisa generic, pure software, light-weight, low-power state.It allows more energytobe saved relativetoidlebyfreezing userspaceandputting all I/O devicesintolow-power states (possiblylower-power than available at run time), such that the processors canspend more timeintheir idle states.This state can be usedforplatforms without Standby/Suspend-to-RAMsupport,orit can be usedinadditiontoSuspend-to-RAM (memory sleep)toprovide reducedresumelatency.? State:Standby / Power-OnSuspendACPI State:S1String:"standby"This state offers minimal, though real, power savings,whileprovidinga very low-latency transition backtoa working system. No operatingstateislost (the CPU retains power), so the system easily starts upagainwhereit leftoff.? Wetrytoput devicesina low-power state equivalenttoD1, whichalso offers low power savings, but lowresumelatency.Notall devicessupport D1,andthose that don't are left on. State:Suspend-to-RAMACPI State:S3String:"mem"This state offers significant power savingsaseverythinginthesystemisputintoa low-power state, exceptformemory, whichisplacedinself-refresh modetoretain its contents.? Systemanddevice stateissavedandkeptinmemory. All devices aresuspendedandputintoD3.Inmany cases, all peripheral buses losepowerwhenentering STR, so devices must be abletohandle thetransition backtotheOnstate.Forat least ACPI, STR requires some minimal boot-strapping codetoresumethe systemfromSTR. This may betrueonother platforms.? State:Suspend-to-diskACPI State:S4String:"disk"This state offers the greatest power savings,andcan be used eveninthe absenceoflow-level platform supportforpower management. Thisstate operates similarlytoSuspend-to-RAM, but includes a finalstepofwriting memory contentstodisk.Onresume, thisisreadandmemoryisrestoredtoits pre-suspend state.
雖說kernel支持上述四種休眠模式,但具體哪幾種可用取決于你的硬件。那么怎么知道自己的Android設(shè)備支持的休眠模式呢?
答案:通過/sys/文件系統(tǒng)。查詢支持的休眠模式可以cat文件/sys/power/state:
cat/sys/power/state freeze mem
如果我們往/sys/power/state文件echo上面的某一種模式的字符串,系統(tǒng)就會進(jìn)入相應(yīng)的休眠模式:
echo"mem">/sys/power/state
如果你搜索過Android休眠相關(guān)的內(nèi)容,在老版本的Android(4.4版本之前)會見有提到PowerManager的setPowerState()方法,該方法即是通過以上方式使系統(tǒng)進(jìn)入休眠。但自從引入Autosleep后,就不在這么做了,setPowerState()方法也銷聲匿跡。
2、/sys/power/目錄下文件
文件簡介:
/sys/power/state:用來控制系統(tǒng)的Power狀態(tài)。讀取該文件可以獲取系統(tǒng)支持的休眠模式,寫入該文件休眠模式的一種,系統(tǒng)進(jìn)入到指定的休眠模式。如上所示例。
/sys/power/autosleep:從Android wakelocks補(bǔ)丁集中演化而來,用于取代Android
wakelocks中的自動(dòng)休眠功能。向該文件寫入/sys/power/state返回值的某一種,系統(tǒng)會在適當(dāng)?shù)臅r(shí)候進(jìn)入指定的休眠的模式;讀取該文件返回之前寫入的數(shù)值。
/sys/power/wake_lock、/sys/power/wake_unlock:即我們常說的休眠鎖,如果應(yīng)用持有休眠鎖,系統(tǒng)將無法進(jìn)入休眠模式。在Android
wakelocks時(shí)代,寫wake_lock獲取鎖,寫wake_unlock釋放鎖;在AutoSleep時(shí)代,具體參見【Android休眠】之AutoSleep
wakeup_count:用于解決“system suspend和system wakeup events之間的同步問題”。
/sys/power/pm_async:狀態(tài)切換開關(guān),允許/禁止User空間對設(shè)備進(jìn)行異步的suspend和resume操作。
/sys/power/pm_freeze_timeout:系統(tǒng)在執(zhí)行休眠動(dòng)作的時(shí)候要凍結(jié)(freeze)用戶控件的進(jìn)程和內(nèi)核空間的允許凍結(jié)的內(nèi)核線程,執(zhí)行這些操作要耗時(shí)間吧?該文件指定所需時(shí)間的最大值。
四、其他需要明了的問題
1、Android設(shè)備屏幕暗下來的時(shí)候,并不是立即就進(jìn)入了休眠模式;當(dāng)所有喚醒源都處于de-avtive狀態(tài)后,系統(tǒng)才會進(jìn)入休眠。
2、Android設(shè)備連著adb線到其他設(shè)備的情況下,設(shè)備是不會進(jìn)入休眠模式的。
3、有休眠操作就有喚醒,就需要喚醒源。喚醒源有很多種,在內(nèi)核注冊,比如常用的Power按鍵。
4、曾經(jīng)困惑的一個(gè)問題:系統(tǒng)怎么知道自己應(yīng)該進(jìn)入休眠模式了?它的判斷依據(jù)是什么?
在wakelock時(shí)代,系統(tǒng)休眠過程中去檢測休眠鎖;如果系統(tǒng)中沒有其他部件持有休眠鎖,就嘗試進(jìn)入休眠模式,沒有異常事件發(fā)生的話就進(jìn)入休眠模式。
Android從4.4開始使用autosleep機(jī)制,只要不存在任何active的喚醒源(wakeup_source)了,就進(jìn)入休眠模式。
5、系統(tǒng)Power Manager整體流程:
如果您覺得閱讀本文對您有幫助,請點(diǎn)一下“推薦”按鈕,您的“推薦”將是我最大的寫作動(dòng)力!