---
title: "android省电开发之cpu降频"
author: "v5qqbrowser"
date: "2013-01-16T17:00:00Z"
tags: []
slug: "android-cpu"
draft: false
lastmod: "2013-02-01T14:13:38Z"
id: 1782668
url: "https://v5qqbrowser.iteye.com/blog/1782668"
---

众所周知，在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).

这个表格的出自：[http://publib.boulder.ibm.com/infocenter/lnxinfo/v3r0m0/index.jsp?topic=%2Fliaai%2Fcpufreq%2FTheCPUFreqGovernors.htm](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 &gt; cpu0/cpufreq/scaling\_min\_freq 设置最小的工作频率，同时也可以设置最大的工作频率。

ok！设置cpu的频率的两种方式我基本上说清楚了，本质没什么差别。那么在实际java端的开发中，我们如何使用呢？

贴贴代码吧，不做过多的解释了，相信大家多能读懂！

```
/**
 * @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;
	}
}
```

完！