android省电开发之cpu降频
眾所周知,在android系統的耗電量排行里,cpu的耗電占了比較大的一部分比例,也就是說,cpu的使用率和使用頻率將直接或間接的影響電量的分配和使用,但很遺憾,android-sdk中沒有為android的開發者提供類似cpu管理的功能,但是當下很多省電類應用或專業的cpu管理軟件都提供了cpu的降頻甚至是超頻的功能,那么這樣的功能是如何實現的,本文將詳細說明在android環境下調整cpu頻率的一些方法,僅供參考。
按照我寫文章的風格,在闡述過程和結論之前,都不得不對關鍵性的概念做一下解釋,本文也不例外。
1.CPU的工作頻率
單位赫茲或者兆赫茲,具體含義不解釋,說實話也不太清楚,不過可以確認一點的是,CPU的工作頻率越高,耗電量越大,反之亦然。我們做這個模塊省電的終極目標就是降低cpu的工作頻率。
2.CPU的調控模式
英文詞為:Governor,解釋為調速器,控制器。大家都指導android的framework是基于linux平臺的,那么cpu的管理體系這塊也跟linux基本上一樣,其中包括cat命令,和一些文件的讀寫配置都是基本上差不多的。Linux在管理CPU方面,提供了如下集中調控模式,分別為:
| Governor | Select this governor if | How it works |
|---|---|---|
| performance | Performance is the only consideration. | Sets the CPU to run at the highest frequency (scaling_max_freq) |
| powersave | Efficiency is the only consideration | Sets the CPU to run at the lowest frequency (scaling_min_freq) |
| ondemand | Adaptation to the current load is the main consideration. | Checks the load regularly. When the load rises aboveup_threshold, sets the CPU to run at the highest frequency (scaling_max_freq). When the load falls below the same threshold, sets the CPU to run at the next lowest frequency. Causes less latency than the conservative governor. |
| conservative | Close adaptation to the current load is the consideration. | Checks the load regularly. When the load rises aboveup_threshold, sets the CPU to run at the next highest frequency. When the load falls belowdown_threshold, sets the CPU to run at the next lowest frequency. Has more latency than the ondemand governor. |
| userspace | Control by other user space program is preferred. | Sets the CPU frequency to the value specified by the user space program (through the use of thescaling_setspeedparameter). |
這個表格的出自:http://publib.boulder.ibm.com/infocenter/lnxinfo/v3r0m0/index.jsp?topic=%2Fliaai%2Fcpufreq%2FTheCPUFreqGovernors.htm
按照原文給出的解釋,我大概把這5種調控模式理解為一下幾種觀點,如有不足,還請指正!
1.performance,這個不多說,就是將cpu的工作頻率調整到最大模式,讓cpu充分的工作。
2.powersave,將cpu的工作頻率調整到節能模式,也就是這個模式下的cpu平率是最低的。
3.ondemand,定期檢查負載。當負荷超越了閾值,設置的CPU運行以最高的頻率。當負載低于相同的閾值,設置的CPU運行在下一個的最低頻率。導致更少的延遲比。這個理解起來可能比較困難,我用白話大概解釋一下,ondemand從字面翻譯是“根據需求,按照需要”,cpu在工作的時候頻率會在一個最大值和最小值之間波動,當負載提高時,該調控期會自動提高cpu的頻率,反之亦然。“Causes less latency than the conservative governor.”這句話的意思是,該模式跟conservative相比,會導致更少的延遲。ok,那讓我們再看看conservative是如何解釋的。
4.conservative,改詞用來形容保守的,守舊的。該模式與ondemand的最大區別在于,conservative模式不會立刻在負載增加的情況下將cpu頻率調整到最大,他會調整到比目前頻率稍微大的頻段去工作,保守,實在是保守!所以換來的結果是,在某種極端情況下,該模式的延遲會大于ondemand。
5.usersapce,該模式將cpu的掌控權交給了用戶態,也就是交給了應用程序,應用程序可以通過配置文件的方式修改cpu的頻率信息,上面3種模式好比linux已經給你定義好的4種模式,userspace好比上面4種模式無法滿足我的需求,我需要自定義!(由于第四種方式需要進行大量的文件操作和配置,本文不闡述了,與省電的目標相差比較遠)
ok!我們了解了這5種省電模式,那么如何查看你的android手機當前運行在哪種模式下呢?當前的cpu運行的工作頻率是多少呢?最大最小頻率是多少?我如何修改模式呢?
其實很簡單,android的cat命令都為我們解決了這些問題,首先要強調的是,必須要獲得系統的root權限,才能進行以下操作。
第一步:adb shell 進入root 命令行模式
第二步 cd /sys/devices/system/cpu/cpu0/cpufreq 進入這個目錄下面
第三步 ls
第四步 你能看見很多的文件
第五步 參考下面的命令,打一遍就清楚了,不過為了給傻瓜提供更好的服務,我還是盡可能的將命令的使用和作用寫的詳細。
/**
* cpu cat命令大全
* cat [%cpuFreqPath%]/cpuinfo_cur_freq (當前cpu頻率)
* cat [%cpuFreqPath%]/cpuinfo_max_freq (最大cpu頻率)
* cat [%cpuFreqPath%]/cpuinfo_min_freq (最小cpu頻率)
* cat [%cpuFreqPath%]/related_cpus (cpu數量標號,從0開始,如果是雙核,結果為0,1)
* cat [%cpuFreqPath%]/scaling_available_frequencies (cpu所有可用頻率)
* cat [%cpuFreqPath%]/scaling_available_governors (cpu所有可用調控模式)
* cat [%cpuFreqPath%]/scaling_available_governors (cpu所有可用調控模式)
* cat [%cpuFreqPath%]/scaling_cur_freq (?????)
* cat [%cpuFreqPath%]/scaling_driver(調控驅動)
* cat [%cpuFreqPath%]/scaling_governor(當前使用哪種調控模式)
* cat [%cpuFreqPath%]/scaling_max_freq(?????)
* cat [%cpuFreqPath%]/scaling_min_freq(?????)
* cat [%cpuFreqPath%]/scaling_setspeed(?????)
* cat [%cpuFreqPath%]/cpuinfo_transition_latency(變頻延遲)
*/
熟悉了這些語法和密令之后,我們就可以很輕松的明白,如何省電了,無非就是將cpu降頻嘛,把當前的調控模式調整為powersave就可以了嘛,不錯,但是還不完全正確。
首先我先講一下如何通過命令行改寫當前的調控模式,很簡單,一句命令:
echo "你想使用的調控模式" /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
但是因為手機出場適配等問題,有些機器上是沒有powersave這個模式的,對了,你一定要確認當前手機是否有powersave這個模式,具體怎么看就在我上面給的命令大全里面,自己找!在沒有powersave模式的情況下,我們只好通過更狠的方法修改cpu的頻率,那就是直接改寫cpu的頻率,命令:
echo 2331000 > cpu0/cpufreq/scaling_min_freq 設置最小的工作頻率,同時也可以設置最大的工作頻率。
ok!設置cpu的頻率的兩種方式我基本上說清楚了,本質沒什么差別。那么在實際java端的開發中,我們如何使用呢?
貼貼代碼吧,不做過多的解釋了,相信大家多能讀懂!
[java]view plaincopy
<spanstyle="font-family:MicrosoftYaHei;">/**
*@authormatrixxu
*
*/
publicclassCPUFreqSetting{
/**
*cpucat命令大全
*cat[%cpuFreqPath%]/cpuinfo_cur_freq(當前cpu頻率)
*cat[%cpuFreqPath%]/cpuinfo_max_freq(最大cpu頻率)
*cat[%cpuFreqPath%]/cpuinfo_min_freq(最小cpu頻率)
*cat[%cpuFreqPath%]/related_cpus(cpu數量標號,從0開始,如果是雙核,結果為0,1)
*cat[%cpuFreqPath%]/scaling_available_frequencies(cpu所有可用頻率)
*cat[%cpuFreqPath%]/scaling_available_governors(cpu所有可用調控模式)
*cat[%cpuFreqPath%]/scaling_available_governors(cpu所有可用調控模式)
*cat[%cpuFreqPath%]/scaling_cur_freq(?????)
*cat[%cpuFreqPath%]/scaling_driver(?????)
*cat[%cpuFreqPath%]/scaling_governor(?????)
*cat[%cpuFreqPath%]/scaling_max_freq(?????)
*cat[%cpuFreqPath%]/scaling_min_freq(?????)
*cat[%cpuFreqPath%]/scaling_setspeed(?????)
*cat[%cpuFreqPath%]/cpuinfo_transition_latency(?????)
*/
privatefinalStringTAG="SetCPU";
privatefinalStringcpuFreqPath="/sys/devices/system/cpu/cpu0/cpufreq";
privatefinalstaticStringPERFORMANCE_GOVERNOR="performance";
privatefinalstaticStringPOWER_SAVE_GOVERNOR="performance";
privatefinalstaticStringONDEMAND_GOVERNOR="performance";
privatefinalstaticStringCONSERVATIVE_GOVERNOR="performance";
privatefinalstaticStringUSERSAPCE_GOVERNOR="performance";
//publicvoidpowerSaveGovernor(){
//List<String>governors=readCpuGovernors();
//if(governors.contains(object)){
//
//}
//}
/**
*獲得當前CPU調控模式
*/
publicvoidgetCpuCurGovernor(){
try{
DataInputStreamis=null;
Processprocess=Runtime.getRuntime().exec("cat"+cpuFreqPath+"/scaling_governor");
is=newDataInputStream(process.getInputStream());
Stringline=is.readLine();
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*重寫CPU調控模式
*@paramgovernor
*@return
*/
privatebooleanwriteCpuGovernor(Stringgovernor){
DataOutputStreamos=null;
byte[]buffer=newbyte[256];
Stringcommand="echo"+governor+">"+cpuFreqPath+"/scaling_governor";
Log.i(TAG,"command:"+command);
try{
Processprocess=Runtime.getRuntime().exec("su");
os=newDataOutputStream(process.getOutputStream());
os.writeBytes(command+"
");
os.writeBytes("exit
");
os.flush();
process.waitFor();
Log.i(TAG,"exitvalue="+process.exitValue());
}catch(IOExceptione){
Log.i(TAG,"writeCpuGovernor:writeCPUGovernor("+governor+")failed!");
returnfalse;
}catch(InterruptedExceptione){
e.printStackTrace();
}
returntrue;
}
/**
*獲得CPU所有調控模式
*@return
*/
privateList<String>readCpuGovernors(){
List<String>governors=newArrayList<String>();
DataInputStreamis=null;
try{
Processprocess=Runtime.getRuntime().exec("cat"+cpuFreqPath+"/scaling_available_governors");
is=newDataInputStream(process.getInputStream());
Stringline=is.readLine();
String[]strs=line.split("");
for(inti=0;i<strs.length;i++)
governors.add(strs[i]);
}catch(IOExceptione){
Log.i(TAG,"readCpuGovernors:readCPUGovernorsfailed!");
}
returngovernors;
}
}</span>
完!
總結
以上是生活随笔為你收集整理的android省电开发之cpu降频的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分形之科赫(Koch)雪花
- 下一篇: 向量组与向量空间