Android cpu data sample

班建义
2023-12-01
public class CPUManager extends Manager {
    public CPUManager(Config config) {
        super(config);
    }

    /**
     * 获取CPU型号。
     *
     * @return
     */
    public String getCpuName() {
        String str1 = "/proc/cpuinfo";
        String str2 = "";

        try {
            FileReader fr = new FileReader(str1);
            BufferedReader bufferedReader = new BufferedReader(fr);
            while ((str2 = bufferedReader.readLine()) != null) {
                if (str2.contains("Hardware")) {
                    return str2.split(":")[1];
                }
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取CPU核心数。
     */
    public int getCpuCoreNum() {
        return Runtime.getRuntime().availableProcessors();
    }

    // 获取CPU最大频率。
    public long getMaxCpuFreq() {
        long result = 0L;
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"));
            if ((line = br.readLine()) != null) {
                result = Long.parseLong(line);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    // 获取CPU最小频率。
    public long getMinCpuFreq() {
        long result = 0L;
        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"));
            if ((line = br.readLine()) != null) {
                result = Long.parseLong(line);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    // 实时获取CPU当前频率。
    public static String getCurCpuFreq() {
        String result = "";
        try {
            FileReader fr = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            result = text.trim();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 是否是64位CPU架构。
     *
     * @return
     */
    public boolean isCpu64() {
        boolean result = false;
        String mProcessor = null;
        try {
            mProcessor = getFieldFromCpuinfo("Processor");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (mProcessor != null) {
            if (mProcessor.contains("aarch64")) {
                result = true;
            }
        }

        return result;
    }


    /*  cat /proc/cpuinfo 命令的输出:

        processor       : 0
        Processor       : AArch64 Processor rev 4 (aarch64)
        model name      : AArch64 Processor rev 4 (aarch64)
        BogoMIPS        : 26.00
        Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32
        CPU implementer : 0x41
        CPU architecture: 8
        CPU variant     : 0x0
        CPU part        : 0xd03
        CPU revision    : 4
    */
    public String getFieldFromCpuinfo(String field) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("/proc/cpuinfo"));
        Pattern p = Pattern.compile(field + "\\s*:\\s*(.*)");

        try {
            String line;
            while ((line = br.readLine()) != null) {
                Matcher m = p.matcher(line);
                if (m.matches()) {
                    return m.group(1);
                }
            }
        } finally {
            br.close();
        }

        return null;
    }


    // 获取应用占用的CPU时间。
    public long getAppCpuTime() {
        String[] cpuInfos = null;
        try {
            int pid = android.os.Process.myPid();
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/" + pid + "/stat")), 1000);
            String load = reader.readLine();
            reader.close();
            cpuInfos = load.split(" ");
        } catch (Exception e) {
            e.printStackTrace();
        }

        long appCpuTime = Long.parseLong(cpuInfos[13]) + Long.parseLong(cpuInfos[14]) + Long.parseLong(cpuInfos[15]) + Long.parseLong(cpuInfos[16]);

        return appCpuTime;
    }
}

 

 类似资料:

相关阅读

相关文章

相关问答