didi / didi/DoKit

【DoKit生态场景】-描述出现的问题

Open
#975 0 comments 0 reactions 1 assignee Claimed by @jtsky View on GitHub
Dominant language
Java
Stars
20.4k
Forks
3.2k
Avg merge
1d 12h
Merged PRs (30d)
2

Description

建议提issues之前可以参考一下DoKit社区答疑流程:https://github.com/didi/DoraemonKit/issues/745
假如还是无法解决你的问题,你可以按照以下模板来提交你的issue

请补充如下信息。
> Android 还是 iOS?系统版本是多少?手机品牌是什么?(如有)
Android 11,oppo reno 5
> 期望的表现和实际的表现。(如有)

在 Android 工程中直接使用获取 CPU 的代码后,获取到的 CPU 信息不准确(Android 和 Flutter 的混合工程)

> 问题重现的步骤。(如有)
如下代码在 float rate = Float.parseFloat(cpu) / Runtime.getRuntime().availableProcessors(); 获取到的 rate 值不准确,远低于 Android Studio Profile 中的 CPU 占用率。
~~~
/**
* 8.0以上获取cpu的方式
*
* @return
*/
public static float getCpuDataForO() {
java.lang.Process process = null;
try {
process = Runtime.getRuntime().exec("top -n 1");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
int cpuIndex = -1;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (TextUtils.isEmpty(line)) {
continue;
}
int tempIndex = getCPUIndex(line);
if (tempIndex != -1) {
cpuIndex = tempIndex;
continue;
}
if (line.startsWith(String.valueOf(Process.myPid()))) {
if (cpuIndex == -1) {
continue;
}
String[] param = line.split("\\s+");
if (param.length <= cpuIndex) {
continue;
}
String cpu = param[cpuIndex];
if (cpu.endsWith("%")) {
cpu = cpu.substring(0, cpu.lastIndexOf("%"));
}

float rate = Float.parseFloat(cpu) / Runtime.getRuntime().availableProcessors();
return rate;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
return 0;
}

private static int getCPUIndex(String line) {
if (line.contains("CPU")) {
String[] titles = line.split("\\s+");
for (int i = 0; i < titles.length; i++) {
if (titles[i].contains("CPU")) {
return i;
}
}
}
return -1;
}
~~~

> 其他的错误信息和堆栈信息如果有也可以一并提供出来。(如有)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.