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 中文乱码解决
# -*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf8') physicsPath = u"D: ...
- HDU 1069 Monkey and Banana(LIS最长上升子序列)
B - LIS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...
- SharePoint 2013 更新多个用户字段(Person or Group)
有时我们需要更新一个用户到Person or Group类型的字段, 当然这个属性允许设置多个用户, 要如何才能添加新的用户到该字段,同时还不影响原始存在的值. 这里我们需要了解 SPFieldUse ...
- Python 更改cmd中的字色
没有gui的python程序是在cmd窗口中运行的,黑色背景,灰色的字,确实很复古,不符合现代人的使用习惯-同事在用我写的小工具时,清一色的字色,看起来会没有重点性,因此我就想通过更改cmd中的字色来 ...
- cadence遇到的问题(持续更新)
1.画了DB9的封装,共十一个焊盘,其中两个是机械焊盘,在绘制PCB板时,想要将其接地,但无法连接,如图所示 因为是机械焊盘,所以无法用更改logic的方法进行网络更改,现在只发现一个办法,就是更改封 ...
- Oralce 字符串截取
update E_SYS_STRATEGY set sconfigvalue=(select Substr((select sconfigvalue from E_SYS_STRATEGY where ...
- VS2012中使用Boost库的方法(超级简单)
很不错的博客一定得看 http://my.csdn.net/caimouse 1.下载boost库 从http://www.boost.org上下载到目前最新的boost库,快速传送门:boost_1 ...
- 智能硬件开发如何选择低功耗MCU
本文将市场上典型的低功耗MCU系列进行了比较,分析得出基于ARM. Cortex M0+内核的MCU系列最适合穿戴式医疗设备的开发.设备开发者当密切关注其发展动向,结合现有的市场需求.产品体系的构建和 ...
- JPA概要
1 JPA概述 JPA(Java Persistence API,Java持久化API),定义了对象-关系映射(ORM)以及实体对象持久化的标准接口. JPA是JSR-220(EJB3.0)规范的一部 ...
- 【HDOJ】4932 Miaomiao's Geometry
递归检测.因为dis数组开的不够大,各种wa.写了个数据发生器,果断发现错误,改完就过了. #include <cstdio> #include <cstring> #incl ...