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_setspeed parameter). |
按照原文给出的解释,我大概把这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端的开发中,我们如何使用呢?
贴贴代码吧,不做过多的解释了,相信大家多能读懂!
- <span style="font-family:Microsoft YaHei;">/**
- * @author matrixxu
- *
- */
- public class CPUFreqSetting {
- /**
- * 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 (?????)
- */
- private final String TAG = "SetCPU";
- private final String cpuFreqPath = "/sys/devices/system/cpu/cpu0/cpufreq";
- private final static String PERFORMANCE_GOVERNOR = "performance";
- private final static String POWER_SAVE_GOVERNOR = "performance";
- private final static String ONDEMAND_GOVERNOR = "performance";
- private final static String CONSERVATIVE_GOVERNOR = "performance";
- private final static String USERSAPCE_GOVERNOR = "performance";
- // public void powerSaveGovernor() {
- // List<String> governors = readCpuGovernors();
- // if (governors.contains(object)) {
- //
- // }
- // }
- /**
- * 获得当前CPU调控模式
- */
- public void getCpuCurGovernor() {
- try {
- DataInputStream is = null;
- Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_governor");
- is = new DataInputStream(process.getInputStream());
- String line = is.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 重写CPU调控模式
- * @param governor
- * @return
- */
- private boolean writeCpuGovernor(String governor) {
- DataOutputStream os = null;
- byte[] buffer = new byte[256];
- String command = "echo " + governor + " > " + cpuFreqPath + "/scaling_governor";
- Log.i(TAG, "command: " + command);
- try {
- Process process = Runtime.getRuntime().exec("su");
- os = new DataOutputStream(process.getOutputStream());
- os.writeBytes(command + "\n");
- os.writeBytes("exit\n");
- os.flush();
- process.waitFor();
- Log.i(TAG, "exit value = " + process.exitValue());
- } catch (IOException e) {
- Log.i(TAG, "writeCpuGovernor: write CPU Governor(" + governor + ") failed!");
- return false;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return true;
- }
- /**
- * 获得CPU所有调控模式
- * @return
- */
- private List<String> readCpuGovernors() {
- List<String> governors = new ArrayList<String>();
- DataInputStream is = null;
- try {
- Process process = Runtime.getRuntime().exec("cat " + cpuFreqPath + "/scaling_available_governors");
- is = new DataInputStream(process.getInputStream());
- String line = is.readLine();
- String[] strs = line.split(" ");
- for (int i = 0; i < strs.length; i++)
- governors.add(strs[i]);
- } catch (IOException e) {
- Log.i(TAG, "readCpuGovernors: read CPU Governors failed!");
- }
- return governors;
- }
- }</span>
完!
android省电开发之cpu降频的更多相关文章
- Android Studio快速开发之道
概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. P ...
- Android插件化开发之OpenAtlas生成插件信息列表
上一篇文章.[Android插件化开发之Atlas初体验]( http://blog.csdn.net/sbsujjbcy/article/details/47446733),简单的介绍了使用Atla ...
- cpu降频问题
cpu做为能耗很高的硬件,最近几年厂商在节能方面做了很多处理,在服务器运行时,基于负载情况可调节成节能模式,节省电能,副作用是cpu的频率会降低,导致应用程序性能降低. 有第三方统计,服务器规模达到万 ...
- Android控件开发之Gallery3D效果
package xiaosi.GalleryFlow; import android.app.Activity; import android.os.Bundle; public class Gall ...
- Android控件开发之Chronometer(转)
(转自:http://blog.csdn.net/sun6255028/article/details/6688349) Chronometr是一个简单的定时器,你可以给它一个开始时间,并以此定时,或 ...
- Android 4.0开发之GridLayOut布局实践
在上一篇教程中http://blog.csdn.net/dawanganban/article/details/9952379,我们初步学习了解了GridLayout的布局基本知识,通过学习知道,Gr ...
- CentOS7 CPU 降频问题
CentOS7 系统默认的 CPUPOWER 策略是 powersave 节能模式,Google 了非常多的资料,一直没有找到解决办法,现在分享一下. 执行: tuned-adm profile th ...
- Android即时通讯开发之XMPP (一)初识XMPP协议和asmack
在讲XMPP和asmck之前 ,我还是先分享一些资源文档,如果你有耐心,可以直接忽略我下面所写的.下面有关XMPP的介绍大部分是摘抄网上的文档,后面我会写一些基于XMPP协议和asmck开源库的聊天室 ...
- 【Android】安卓开发之activity如何传值到fragment,activity与fragment传值
作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 大家知道,我们利用activity使 ...
随机推荐
- Python自动化运维之31、Tornado框架
Tornado 官网:http://www.tornadoweb.org/en/stable/ Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本. ...
- Git配置安装使用教程操作github上传克隆数据
Git是何方神圣? Git是用C语言开发的分布版本控制系统.版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历史记录状态).另一个状态可以是不同的文件,也可以是不同的文件内容 ...
- sql语句中出现笛卡尔乘积
没有join条件导致笛卡尔乘积 学过线性代数的人都知道,笛卡尔乘积通俗的说,就是两个集合中的每一个成员,都与对方集合中的任意一个成员有关联.可以想象,在SQL查询中,如果对两张表join查询而没有jo ...
- PLSQL Developer如何设置自动打开上次编辑的文件
作为开发人员经常把sql语句保存到文件中以方便下次继续使用,问题是plsqlDev重启后每次都需要手工打开这个文件,好不方便: 以下设置是plsqlDev启动后自动打开上次编辑的文件. 选择配置> ...
- ExtJS 4 Grids 详解
Grid Panel是ExtJS最常用的组件之一,它的功能非常丰富,提供了非常便捷的方法执行排序,分组,编辑数据. Basic Grid Panel 基本表格面板 让我们创建一个简单的表格,这有创建和 ...
- How to solve "The specified service has been marked for deletion" error
There may be several causes which lead to the service being stuck in “marked for deletion”. Microsof ...
- 如何从BBC网站学习英语
- LINUX怎么修改IP地址
1,先搜索了一下,得到以下解释 IP IP地址 Netmark 子网掩码 Gateway 默认网关 HostName 主机名称 DomainName 域名 DNS DNS的IP 2,需要修改的文件常有 ...
- id有空格获取不到元素
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...