本文共 5871 字,大约阅读时间需要 19 分钟。
手机系统/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
根据上述,进行目录的正则匹配 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; } }
我们可以再其中获取对应的 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/