博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android系统的获取 CPU 核数
阅读量:4146 次
发布时间:2019-05-25

本文共 5871 字,大约阅读时间需要 19 分钟。

一 观察/sys/devices/system/cpu 目录结构

手机系统/sys/devices/system/cpu

Z91:/sys/devices/system/cpu # ls -allls -alltotal 0drwxr-xr-x 13 root root    0 2018-01-09 11:17 .drwxr-xr-x  6 root root    0 2018-01-09 11:17 ..drwxr-xr-x  5 root root    0 2018-01-09 11:17 cpu0drwxr-xr-x  5 root root    0 2018-01-10 08:33 cpu1drwxr-xr-x  5 root root    0 2018-01-10 08:33 cpu2drwxr-xr-x  5 root root    0 2018-01-10 08:33 cpu3drwxr-xr-x  3 root root    0 2018-01-09 11:17 cpufreqdrwxr-xr-x  2 root root    0 2018-01-09 11:17 cpuidledrwxr-xr-x  2 root root    0 2018-01-09 11:17 cputopodrwxr-xr-x  2 root root    0 2018-01-09 11:17 eas-r--r--r--  1 root root 4096 2018-01-09 11:17 isolated-r--r--r--  1 root root 4096 2018-01-09 11:17 kernel_max-r--r--r--  1 root root 4096 2018-01-09 11:17 modalias-r--r--r--  1 root root 4096 2018-01-09 11:17 offline-r--r--r--  1 root root 4096 2018-01-09 11:17 online-r--r--r--  1 root root 4096 2018-01-09 11:17 possibledrwxr-xr-x  2 root root    0 2018-01-09 11:17 power-r--r--r--  1 root root 4096 2018-01-09 11:17 presentdrwxr-xr-x  2 root root    0 2018-01-09 11:17 rq-stats

cpu0~cpu4 表示有4个CPU

二 获取 CPU 核数

根据上述,进行目录的正则匹配 Pattern.matches(“cpu[0-9]+”, file.getName()) 可算出 CPU 核数大小

import android.util.Log;import java.io.BufferedReader;import java.io.File;import java.io.FileFilter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.regex.Pattern;    /**     * It's also good way to get cpu core number     */    public static int getCPUCoreNum() {        return Runtime.getRuntime().availableProcessors();    }    /**     * Gets the number of cores available in this device, across all processors.     * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"     * 

* Source: http://stackoverflow.com/questions/7962155/ * * @return The number of cores, or 1 if failed to get result */ public static int getNumCpuCores() { try { // Get directory containing CPU info java.io.File dir = new java.io.File("/sys/devices/system/cpu/"); // Filter to only list the devices we care about java.io.File[] files = dir.listFiles(new FileFilter() { @Override public boolean accept(File file) { // Check if filename is "cpu", followed by a single digit number if (java.util.regex.Pattern.matches("cpu[0-9]+", file.getName())) { return true; } return false; } }); // Return the number of cores (virtual CPU devices) return files.length; } catch (Exception e) { // Default to return 1 core Log.e(TAG, "Failed to count number of cores, defaulting to 1", e); return 1; } }

其他 cpu0~cpu4 的目录结构

我们可以再其中获取对应的 CPU 频率信息,因为里面的文件都是可读的

cpu0========================================================Z91:/sys/devices/system/cpu/cpu0 # ls -alls -altotal 0drwxr-xr-x  5 root root    0 2018-01-09 11:17 .drwxr-xr-x 13 root root    0 2018-01-09 11:17 ..lrwxrwxrwx  1 root root    0 2018-01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x  5 root root    0 2018-01-09 11:17 cpuidlelrwxrwxrwx  1 root root    0 2018-01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@0-rw-r--r--  1 root root 4096 2018-01-09 11:17 onlinedrwxr-xr-x  2 root root    0 2018-01-09 11:17 powerlrwxrwxrwx  1 root root    0 2018-01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x  2 root root    0 2018-01-09 11:17 topology-rw-r--r--  1 root root 4096 2018-01-09 11:17 ueventcpu1========================================================Z91:/sys/devices/system/cpu/cpu1 # ls -allls -alltotal 0drwxr-xr-x  5 root root    0 2018-01-10 08:33 .drwxr-xr-x 13 root root    0 2018-01-09 11:17 ..lrwxrwxrwx  1 root root    0 2018-01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x  5 root root    0 2018-01-09 11:17 cpuidlelrwxrwxrwx  1 root root    0 2018-01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@001-rw-r--r--  1 root root 4096 2018-01-09 11:17 onlinedrwxr-xr-x  2 root root    0 2018-01-09 11:17 powerlrwxrwxrwx  1 root root    0 2018-01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x  2 root root    0 2018-01-10 08:33 topology-rw-r--r--  1 root root 4096 2018-01-09 11:17 ueventcpu2========================================================Z91:/sys/devices/system/cpu/cpu2 # ls -allls -alltotal 0drwxr-xr-x  5 root root    0 2018-01-10 08:33 .drwxr-xr-x 13 root root    0 2018-01-09 11:17 ..lrwxrwxrwx  1 root root    0 2018-01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x  5 root root    0 2018-01-09 11:17 cpuidlelrwxrwxrwx  1 root root    0 2018-01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@002-rw-r--r--  1 root root 4096 2018-01-09 11:17 onlinedrwxr-xr-x  2 root root    0 2018-01-09 11:17 powerlrwxrwxrwx  1 root root    0 2018-01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x  2 root root    0 2018-01-10 08:33 topology-rw-r--r--  1 root root 4096 2018-01-09 11:17 ueventcpu3========================================================Z91:/sys/devices/system/cpu/cpu3 # ls -allls -alltotal 0drwxr-xr-x  5 root root    0 2018-01-10 08:33 .drwxr-xr-x 13 root root    0 2018-01-09 11:17 ..lrwxrwxrwx  1 root root    0 2018-01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x  5 root root    0 2018-01-09 11:17 cpuidlelrwxrwxrwx  1 root root    0 2018-01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@003-rw-r--r--  1 root root 4096 2018-01-09 11:17 onlinedrwxr-xr-x  2 root root    0 2018-01-09 11:17 powerlrwxrwxrwx  1 root root    0 2018-01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x  2 root root    0 2018-01-10 08:33 topology-rw-r--r--  1 root root 4096 2018-01-09 11:17 uevent

转载地址:http://kocti.baihongyu.com/

你可能感兴趣的文章
js after append
查看>>
sweetalert
查看>>
React.js 之筛选篇
查看>>
html5新标签dataList
查看>>
window.open()参数传递及获取
查看>>
nodejs静态服务器anywhere
查看>>
sessionStorage和localStorage
查看>>
解决smartGit过期问题
查看>>
angularjs毫秒转成化日期格式(date:'yyyy-MM-dd HH:mm:ss')
查看>>
angularjs如何打开modal
查看>>
引入css的方式及link和@import的区别
查看>>
js打开本地某个APP
查看>>
Tomcat启动失败,提示Server Tomcat v7.0 Server at localhost failed to start
查看>>
常用Java8代码汇编(二)
查看>>
Java8 stream流之分组 groupingBy 的使用
查看>>
Java8常用的时间 工具类 DateUtils
查看>>
源码分析-kube-proxy(proxyMode: iptables)
查看>>
网络协议——最大传输单元 MTU
查看>>
golang 学习笔记——channel
查看>>
网络协议——VXLAN 报文格式
查看>>