1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android系统的获取 CPU 核数

Android系统的获取 CPU 核数

时间:2019-11-20 10:55:40

相关推荐

Android系统的获取 CPU 核数

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

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

Z91:/sys/devices/system/cpu # ls -allls -alltotal 0drwxr-xr-x 13 root root 0 -01-09 11:17 .drwxr-xr-x 6 root root 0 -01-09 11:17 ..drwxr-xr-x 5 root root 0 -01-09 11:17 cpu0drwxr-xr-x 5 root root 0 -01-10 08:33 cpu1drwxr-xr-x 5 root root 0 -01-10 08:33 cpu2drwxr-xr-x 5 root root 0 -01-10 08:33 cpu3drwxr-xr-x 3 root root 0 -01-09 11:17 cpufreqdrwxr-xr-x 2 root root 0 -01-09 11:17 cpuidledrwxr-xr-x 2 root root 0 -01-09 11:17 cputopodrwxr-xr-x 2 root root 0 -01-09 11:17 eas-r--r--r-- 1 root root 4096 -01-09 11:17 isolated-r--r--r-- 1 root root 4096 -01-09 11:17 kernel_max-r--r--r-- 1 root root 4096 -01-09 11:17 modalias-r--r--r-- 1 root root 4096 -01-09 11:17 offline-r--r--r-- 1 root root 4096 -01-09 11:17 online-r--r--r-- 1 root root 4096 -01-09 11:17 possibledrwxr-xr-x 2 root root 0 -01-09 11:17 power-r--r--r-- 1 root root 4096 -01-09 11:17 presentdrwxr-xr-x 2 root root 0 -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"* <p>* Source: /questions/7962155/** @return The number of cores, or 1 if failed to get result*/public static int getNumCpuCores() {try {// Get directory containing CPU infojava.io.File dir = new java.io.File("/sys/devices/system/cpu/");// Filter to only list the devices we care aboutjava.io.File[] files = dir.listFiles(new FileFilter() {@Overridepublic boolean accept(File file) {// Check if filename is "cpu", followed by a single digit numberif (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 coreLog.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 -01-09 11:17 .drwxr-xr-x 13 root root 0 -01-09 11:17 ..lrwxrwxrwx 1 root root 0 -01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x 5 root root 0 -01-09 11:17 cpuidlelrwxrwxrwx 1 root root 0 -01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@0-rw-r--r-- 1 root root 4096 -01-09 11:17 onlinedrwxr-xr-x 2 root root 0 -01-09 11:17 powerlrwxrwxrwx 1 root root 0 -01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x 2 root root 0 -01-09 11:17 topology-rw-r--r-- 1 root root 4096 -01-09 11:17 ueventcpu1========================================================Z91:/sys/devices/system/cpu/cpu1 # ls -allls -alltotal 0drwxr-xr-x 5 root root 0 -01-10 08:33 .drwxr-xr-x 13 root root 0 -01-09 11:17 ..lrwxrwxrwx 1 root root 0 -01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x 5 root root 0 -01-09 11:17 cpuidlelrwxrwxrwx 1 root root 0 -01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@001-rw-r--r-- 1 root root 4096 -01-09 11:17 onlinedrwxr-xr-x 2 root root 0 -01-09 11:17 powerlrwxrwxrwx 1 root root 0 -01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x 2 root root 0 -01-10 08:33 topology-rw-r--r-- 1 root root 4096 -01-09 11:17 ueventcpu2========================================================Z91:/sys/devices/system/cpu/cpu2 # ls -allls -alltotal 0drwxr-xr-x 5 root root 0 -01-10 08:33 .drwxr-xr-x 13 root root 0 -01-09 11:17 ..lrwxrwxrwx 1 root root 0 -01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x 5 root root 0 -01-09 11:17 cpuidlelrwxrwxrwx 1 root root 0 -01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@002-rw-r--r-- 1 root root 4096 -01-09 11:17 onlinedrwxr-xr-x 2 root root 0 -01-09 11:17 powerlrwxrwxrwx 1 root root 0 -01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x 2 root root 0 -01-10 08:33 topology-rw-r--r-- 1 root root 4096 -01-09 11:17 ueventcpu3========================================================Z91:/sys/devices/system/cpu/cpu3 # ls -allls -alltotal 0drwxr-xr-x 5 root root 0 -01-10 08:33 .drwxr-xr-x 13 root root 0 -01-09 11:17 ..lrwxrwxrwx 1 root root 0 -01-09 11:17 cpufreq -> ../cpufreq/policy0drwxr-xr-x 5 root root 0 -01-09 11:17 cpuidlelrwxrwxrwx 1 root root 0 -01-09 11:17 of_node -> ../../../../firmware/devicetree/base/cpus/cpu@003-rw-r--r-- 1 root root 4096 -01-09 11:17 onlinedrwxr-xr-x 2 root root 0 -01-09 11:17 powerlrwxrwxrwx 1 root root 0 -01-09 11:17 subsystem -> ../../../../bus/cpudrwxr-xr-x 2 root root 0 -01-10 08:33 topology-rw-r--r-- 1 root root 4096 -01-09 11:17 uevent

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。