android 的cpu 频率 CPU核心数
来源:互联网 发布:面单打印软件 编辑:程序博客网 时间:2024/04/30 03:29
/**
* Gets the number of cores available in this device, across all processors.
* Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
* @return The number of cores, or 1 if failed to get result
*/
private int getNumCores() {
//Private Class to display only CPU devices in the directory listing
class CpuFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
//Check if filename is "cpu", followed by a single digit number
if(Pattern.matches("cpu[0-9]", pathname.getName())) {
return true;
}
return false;
}
}
try {
//Get directory containing CPU info
File dir = new File("/sys/devices/system/cpu/");
//Filter to only list the devices we care about
File[] files = dir.listFiles(new CpuFilter());
Log.d(TAG, "CPU Count: "+files.length);
//Return the number of cores (virtual CPU devices)
return files.length;
} catch(Exception e) {
//Print exception
Log.d(TAG, "CPU Count: Failed.");
e.printStackTrace();
//Default to return 1 core
return 1;
}
}
读取/proc/stat 所有CPU活动的信息来计算CPU使用率
下面我们就来讲讲如何通过代码来获取CPU频率:
package com.orange.cpu;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
public class CpuManager {
// 获取CPU最大频率(单位KHZ)
// "/system/bin/cat" 命令行
// "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" 存储最大频率的文件的路径
public static String getMaxCpuFreq() {
String result = "";
ProcessBuilder cmd;
try {
String[] args = { "/system/bin/cat",
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[24];
while (in.read(re) != -1) {
result = result + new String(re);
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
result = "N/A";
}
return result.trim();
}
// 获取CPU最小频率(单位KHZ)
public static String getMinCpuFreq() {
String result = "";
ProcessBuilder cmd;
try {
String[] args = { "/system/bin/cat",
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq" };
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[24];
while (in.read(re) != -1) {
result = result + new String(re);
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
result = "N/A";
}
return result.trim();
}
// 实时获取CPU当前频率(单位KHZ)
public static String getCurCpuFreq() {
String result = "N/A";
try {
FileReader fr = new FileReader(
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
result = text.trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
// 获取CPU名字
public static String getCpuName() {
try {
FileReader fr = new FileReader("/proc/cpuinfo");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
String[] array = text.split(":\\s+", 2);
for (int i = 0; i < array.length; i++) {
}
return array[1];
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
- android 的cpu 频率 CPU核心数
- Android下设置CPU核心数和频率
- Android下设置CPU核心数和频率
- Android下设置CPU核心数和频率
- Android下设置CPU核心数和频率
- 获得android手机的CPU核心数
- 获得android手机的CPU核心数
- 获得android手机的CPU核心数
- 获得android手机的CPU核心数
- 获得android手机的CPU核心数
- 获得android手机的CPU核心数
- 在MTK芯片上如何控制CPU的核心数和频率--MTK PerfService
- 获取Android设备的CPU频率
- 获取android手机cpu核心数
- 获取android手机cpu核心数
- CPU的时钟频率
- 关于cpu的频率
- 理解CPU的频率
- 距离杀死我们的那段爱:伤感QQ空间日志
- hdu 1598 枚举+并查集
- IOS设计模式:Delegate模式
- Linux下基于C/C++的Socket编程实例
- 求1000以内所有偶数的和(三)
- android 的cpu 频率 CPU核心数
- sql注入初试:找到可注入的地方,获得数据库信息,修改数据库
- Android 完美解决自定义preference与ActivityGroup UI更新的问题
- 七种qsort排序方法
- 使用Visual Studio连接mysql时出现 mysql_com.h 错误解决方法
- linux驱动开发 方法谈
- 使用bat 进行ftp 批量下载
- 陌小尘最新推荐伤感日志:你爱或不爱,爱就在那里
- [Mac入门]如何在Mac下显示Finder中的所有文件