hel*_*eye 1 android cpu-usage cpu-cores
我正在开发一个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内核正在休眠?
小智 5
我知道这很老,但是似乎没有答案,我也在寻找解决方案。
我想的是,如果我记录每个跟在数字后面的名为“ cpu”的核心。和cpus从0-3开始。如果我按顺序阅读内核。然后,如果/ proc / stat的.readline()返回一个不包含cpu的字符串,则该内核一定不能工作,并且处于脱机状态。因此,从理论上讲,这将是零使用率。因此,返回0。
*带代码的完整答案,请参见下文*
这是一些代码,以防万一我说的没有意义,我的基于此: 在Android中获取内存使用情况
这就是我如何找到一个新的计算方法,从而使我可以更准确地表示核心读数:如何获得Linux(c ++)中的总CPU使用率
首先,这是我的一些CPU函数,这些函数在这些循环和填充后显示给用户。我将其发布,以便您更好地了解i以及我的代码的含义
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);
}
Run Code Online (Sandbox Code Playgroud)
getNumCores可以在这里找到,我不会在此发布,因为我觉得这样做的人应该得到它的称赞:如何从代码中检测Android设备上的双核CPU?
最后,这是我的代码,希望它有意义,并且我提供了很多评论。
//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)
///sf/ask/211201371/#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
///sf/ask/211201371/#3017438
return (float)(work2 - work1) / ((total2 - total1));
}
else
{
reader.close();
return 0;
}
}
else
{
reader.close();
return 0;
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
最后,我的readCore函数将返回0.0-1.0的值,您需要乘以100才能得到一个百分比。
编辑 按照以下Android文档的注释的要求:“在处于活动状态时,CPU可以联机或脱机,更改时钟速度和相关电压(可能还会影响内存总线速度和其他系统核心电源状态),并且可以输入较低的电源在内核空闲循环中时,它们处于空闲状态。不仅为功率配置文件测量了这些不同的CPU功率状态,而且在测量其他参数时可能有必要避免功耗变化。”
链接:https://source.android.com/devices/tech/power.html#
| 归档时间: |
|
| 查看次数: |
8240 次 |
| 最近记录: |