我正在开发一个Android应用程序,显示每个内核的CPU负载和内存消耗。对于CPU负载,我正在读/ proc / stat和内存 - >的/ proc / meminfo中。 但是我发现/ proc / stat中的CPU核心数在后续读取文件时会发生变化。
cpu 230599 10622 84595 1892023 8236 16 285 0 0 0
cpu0 138005 7992 58080 1738918 6407 16 278 0 0 0
intr 9136791 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9601 0 0 0 0 0 0 .......
ctxt 16904510
btime 1394641996
processes 16919
procs_running 2
procs_blocked 0
softirq 1688530 407 706934 422 1558 407 407 92978 324500 1267 559650
并在5秒后变为:
cpu 230772 10623 84671 1890801 8236 16 286 0 0 0
cpu0 138104 7993 58126 1739267 6407 16 279 0 0 0
cpu1 92668 2630 26545 151534 1829 0 7 0 0 0
intr 9144729 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9601 0 0 0 0 0 0 ........
ctxt 16923744
btime 1394641996
processes 16946
procs_running 2
procs_blocked 0
softirq 1690205 407 707396 422 1558 407 407 93311 324790 1267 560240
这是否意味着cpu核心在某些情况下正在休眠?
答案 0 :(得分:5)
我知道这已经过时了,但似乎没有答案,而且我一直在寻找解决方案。
我想,如果我正在录制一个名为“cpu”的每个核心跟随一个数字。并且cpus从0-3开始。如果我按顺序阅读核心。然后,如果/ proc / stat的.readline()返回一个不包含cpu的字符串,那么该核心必须不能正常工作,并且处于脱机状态。因此,从理论上讲,它的使用率为零。所以,返回0。
*代码完成答案,见下文*
下面是一些代码,如果我说的没有意义,我的基础是: Get Memory Usage in Android
以下是我如何找到一个新计算,让我更准确地表示核心读数:How to get total cpu usage in Linux (c++)
首先,这里是我的一些CPU函数,它在这些循环和东西之后显示一个字符串给用户。我发布这个,这样你就能更好地理解我,以及我的代码意味着什么
float[] coreValues = new float[10];
//get how many cores there are from function
int numCores = getNumCores();
for(byte i = 0; i < numCores; i++)
{
coreValues[i] = readCore(i);
}
getNumCores可以在这里找到,我不会发帖,因为我觉得这个人应该得到它的荣誉:How can you detect a dual-core cpu on an Android device from code?
最后,这是我的代码,我希望它有意义,我提出了很多意见。
//for multi core value
private float readCore(int i)
{
/*
* how to calculate multicore
* this function reads the bytes from a logging file in the android system (/proc/stat for cpu values)
* then puts the line into a string
* then spilts up each individual part into an array
* then(since he know which part represents what) we are able to determine each cpu total and work
* then combine it together to get a single float for overall cpu usage
*/
try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
//skip to the line we need
for(int ii = 0; ii < i + 1; ++ii)
{
reader.readLine();
}
String load = reader.readLine();
//cores will eventually go offline, and if it does, then it is at 0% because it is not being
//used. so we need to do check if the line we got contains cpu, if not, then this core = 0
if(load.contains("cpu"))
{
String[] toks = load.split(" ");
//we are recording the work being used by the user and system(work) and the total info
//of cpu stuff (total)
//https://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c/3017438#3017438
long work1 = Long.parseLong(toks[1])+ Long.parseLong(toks[2]) + Long.parseLong(toks[3]);
long total1 = Long.parseLong(toks[1])+ Long.parseLong(toks[2]) + Long.parseLong(toks[3]) +
Long.parseLong(toks[4]) + Long.parseLong(toks[5])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
try
{
//short sleep time = less accurate. But android devices typically don't have more than
//4 cores, and I'n my app, I run this all in a second. So, I need it a bit shorter
Thread.sleep(200);
}
catch (Exception e) {}
reader.seek(0);
//skip to the line we need
for(int ii = 0; ii < i + 1; ++ii)
{
reader.readLine();
}
load = reader.readLine();
//cores will eventually go offline, and if it does, then it is at 0% because it is not being
//used. so we need to do check if the line we got contains cpu, if not, then this core = 0%
if(load.contains("cpu"))
{
reader.close();
toks = load.split(" ");
long work2 = Long.parseLong(toks[1])+ Long.parseLong(toks[2]) + Long.parseLong(toks[3]);
long total2 = Long.parseLong(toks[1])+ Long.parseLong(toks[2]) + Long.parseLong(toks[3]) +
Long.parseLong(toks[4]) + Long.parseLong(toks[5])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
//here we find the change in user work and total info, and divide by one another to get our total
//seems to be accurate need to test on quad core
//https://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c/3017438#3017438
return (float)(work2 - work1) / ((total2 - total1));
}
else
{
reader.close();
return 0;
}
}
else
{
reader.close();
return 0;
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
return 0;
}
而且,作为最后一点,我的readCore函数将返回0.0 - 1.0的值,您需要乘以100才能获得百分比。
<强> 修改 强> 根据以下评论的要求,Android文档:“在激活时,CPU可以联机或脱机,更改时钟速度和相关电压(可能还会影响内存总线速度和其他系统核心电源状态),并可以进入低功耗空闲状态在内核空闲循环中的状态。不仅为功率分布测量这些不同的CPU功率状态,在测量其他参数时可能需要避免功率消耗差异。“
答案 1 :(得分:1)
即使在问题发生后几个月就接受了答案,这对我来说非常有用。
希望我的建议对将来阅读这个问题的任何人都有用
@ Torch2424的答案非常好,但是没有检查:正如你所说的,有时Android并没有使用所有的CPU;我在4核平板电脑上尝试了你的代码,事实上我发现大多数情况下2个CPU根本没有使用或者经常没有改变,因此/ proc / stat文件的相对行完全相同。这意味着(total2 - total1)
等于0然后你试图除以0,其结果是NaN
这实际上是我的readCore()函数的工作代码:
// for multi core value
private float readCore(int i) {
/*
* how to calculate multicore this function reads the bytes from a
* logging file in the android system (/proc/stat for cpu values) then
* puts the line into a string then spilts up each individual part into
* an array then(since he know which part represents what) we are able
* to determine each cpu total and work then combine it together to get
* a single float for overall cpu usage
*/
try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
// skip to the line we need
for (int ii = 0; ii < i + 1; ++ii) {
String line = reader.readLine();
}
String load = reader.readLine();
// cores will eventually go offline, and if it does, then it is at
// 0% because it is not being
// used. so we need to do check if the line we got contains cpu, if
// not, then this core = 0
if (load.contains("cpu")) {
String[] toks = load.split(" ");
// we are recording the work being used by the user and
// system(work) and the total info
// of cpu stuff (total)
// http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c/3017438#3017438
long work1 = Long.parseLong(toks[1]) + Long.parseLong(toks[2])
+ Long.parseLong(toks[3]);
long total1 = Long.parseLong(toks[1]) + Long.parseLong(toks[2])
+ Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[5]) + Long.parseLong(toks[6])
+ Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
try {
// short sleep time = less accurate. But android devices
// typically don't have more than
// 4 cores, and I'n my app, I run this all in a second. So,
// I need it a bit shorter
Thread.sleep(300);
} catch (Exception e) {
}
reader.seek(0);
// skip to the line we need
for (int ii = 0; ii < i + 1; ++ii) {
reader.readLine();
}
load = reader.readLine();
// cores will eventually go offline, and if it does, then it is
// at 0% because it is not being
// used. so we need to do check if the line we got contains cpu,
// if not, then this core = 0%
if (load.contains("cpu")) {
reader.close();
toks = load.split(" ");
long work2 = Long.parseLong(toks[1]) + Long.parseLong(toks[2])
+ Long.parseLong(toks[3]);
long total2 = Long.parseLong(toks[1]) + Long.parseLong(toks[2])
+ Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[5]) + Long.parseLong(toks[6])
+ Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
// here we find the change in user work and total info, and
// divide by one another to get our total
// seems to be accurate need to test on quad core
// http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c/3017438#3017438
if ((total2 - total1) == 0)
return 0;
else
return (float) (work2 - work1) / ((total2 - total1));
} else {
reader.close();
return 0;
}
} else {
reader.close();
return 0;
}
} catch (IOException ex) {
ex.printStackTrace();
}
return 0;
}
希望它有所帮助!
答案 2 :(得分:1)
@ Torch2424和@Andre的答案还可以,但对于我的用途,他们缺少一些东西:
以下是希望正确修复此问题的代码:class或simple app。
答案 3 :(得分:0)
我一直在寻找如何在日常和夜晚从android中获取CPU
的使用方法,并且有时会遇到这个问题。
好吧,如果我没有错,我认为当你没有获得CPU
核心意味着未显示的核心是离线的(Android会这样做以防止大量电池耗尽)。
如果你想知道android有多少核心找到了另一种方式,那就有太多的东西,然后做你喜欢的任何事情。