Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AbeiOS opened this issue Jan 21, 2022 · 0 comments
Open

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

AbeiOS opened this issue Jan 21, 2022 · 0 comments
Assignees

Comments

@AbeiOS
Copy link

AbeiOS commented Jan 21, 2022

建议提issues之前可以参考一下DoKit社区答疑流程:#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;
    }

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants