我试图获得真正的最大CPU内核速度。
此代码仅适用于我的nexus 5x的四个第一个核心,但它在最后两个核心中失败:
for (int i = 0; i < cpuCoresNumber; i++) {
try {
BufferedReader freqBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("/sys/devices/system/cpu/cpu" + i + "/cpufreq/scaling_max_freq"))));
int freq = Integer.valueOf(freqBufferedReader.readLine()).intValue() / 1000;
freqBufferedReader.close();
coreMaxFreqArray[i] = freq;
} catch (Exception e) {
e.printStackTrace();
}
}
对于前4个我获得实际值,对于最后2个,首先我得到实际值,但有时我得到更低的值,例如600,300,0 ......以及那两个最后核心的实际值是1800mhz。
答案 0 :(得分:1)
尝试其中一些,我执行命令以获得每个核心的最大频率。
private String exCommand(String comando) throws IOException {
try {
Process process = Runtime.getRuntime().exec(comando);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuilder output = new StringBuilder();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
process.waitFor();
return output.toString();
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
并在我的主要方法上编写此代码以获得每个核心速度
for (int i = 0; i <= cores; i++) {
speed = exCommand("cat /sys/devices/system/cpu/cpu"+i+"/cpufreq/cpuinfo_max_freq");
vel = Double.parseDouble(speed);
vel = vel /1000000;
System.out.println(vel);
}
希望有所帮助