gpt4 book ai didi

/proc/stat 中报告的 Android cpu 内核

转载 作者:行者123 更新时间:2023-11-29 14:38:22 26 4
gpt4 key购买 nike

我正在开发一个显示每个内核的 CPU 负载和内存消耗的 Android 应用程序。对于 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
内部 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
进程 16919
procs_running 2
procs_blocked 0
软中断 1688530 407 706934 422 1558 407 407 92978 324500 1267 559650

5 秒后变成:

中央处理器 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
内部 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
进程 16946
procs_running 2
procs_blocked 0
软中断 1690205 407 707396 422 1558 407 407 93311 324790 1267 560240

这是否意味着某些情况下 cpu 内核正在休眠?

最佳答案

我知道这是旧的,但似乎没有答案,我也一直在寻找解决方案。

我的想法是,如果我正在记录每个名为“cpu”的核心,其后跟一个数字。 cpu 从 0 到 3。如果我按顺序阅读核心内容。然后,如果/proc/stat 的 .readline() 返回一个不包含 cpu 的字符串,则该核心一定没有工作,并且处于离线状态。因此,理论上,它的使用率为零。所以,返回 0。

* 带代码的完整答案,见下文 *

这是一些代码,以防我说的没有意义,我的是基于此: Get Memory Usage in Android

下面是我如何找到一个新的计算方法,可以更准确地表示核心读数:How to get total cpu usage in Linux (c++)

首先,这是我的一些 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);
}

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 文档:“当处于 Activity 状态时,CPU 可以联机或脱机,更改时钟速度和相关电压(可能还会影响内存总线速度和其他系统核心电源状态),并且可以进入低功耗空闲状态内核空闲循环中的状态。不仅要针对功率配置文件测量这些不同的 CPU 功率状态,在测量其他参数时可能需要避免功率消耗变化。”

链接:https://source.android.com/devices/tech/power.html#

关于/proc/stat 中报告的 Android cpu 内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22405403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com