Android系统的获取 CPU 核数

您所在的位置:网站首页 怎么看linux是几核的 Android系统的获取 CPU 核数

Android系统的获取 CPU 核数

2023-12-10 22:24| 来源: 网络整理| 查看: 265

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

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

Z91:/sys/devices/system/cpu # ls -all ls -all total 0 drwxr-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 cpu0 drwxr-xr-x 5 root root 0 2018-01-10 08:33 cpu1 drwxr-xr-x 5 root root 0 2018-01-10 08:33 cpu2 drwxr-xr-x 5 root root 0 2018-01-10 08:33 cpu3 drwxr-xr-x 3 root root 0 2018-01-09 11:17 cpufreq drwxr-xr-x 2 root root 0 2018-01-09 11:17 cpuidle drwxr-xr-x 2 root root 0 2018-01-09 11:17 cputopo drwxr-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 possible drwxr-xr-x 2 root root 0 2018-01-09 11:17 power -r--r--r-- 1 root root 4096 2018-01-09 11:17 present drwxr-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 -al ls -al total 0 drwxr-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/policy0 drwxr-xr-x 5 root root 0 2018-01-09 11:17 cpuidle lrwxrwxrwx 1 root root 0 2018-01-09 11:17 of_node -> ../../../../firmware/de vicetree/base/cpus/cpu@0 -rw-r--r-- 1 root root 4096 2018-01-09 11:17 online drwxr-xr-x 2 root root 0 2018-01-09 11:17 power lrwxrwxrwx 1 root root 0 2018-01-09 11:17 subsystem -> ../../../../bus/cpu drwxr-xr-x 2 root root 0 2018-01-09 11:17 topology -rw-r--r-- 1 root root 4096 2018-01-09 11:17 uevent cpu1 ======================================================== Z91:/sys/devices/system/cpu/cpu1 # ls -all ls -all total 0 drwxr-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/policy0 drwxr-xr-x 5 root root 0 2018-01-09 11:17 cpuidle lrwxrwxrwx 1 root root 0 2018-01-09 11:17 of_node -> ../../../../firmware/de vicetree/base/cpus/cpu@001 -rw-r--r-- 1 root root 4096 2018-01-09 11:17 online drwxr-xr-x 2 root root 0 2018-01-09 11:17 power lrwxrwxrwx 1 root root 0 2018-01-09 11:17 subsystem -> ../../../../bus/cpu drwxr-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 cpu2 ======================================================== Z91:/sys/devices/system/cpu/cpu2 # ls -all ls -all total 0 drwxr-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/policy0 drwxr-xr-x 5 root root 0 2018-01-09 11:17 cpuidle lrwxrwxrwx 1 root root 0 2018-01-09 11:17 of_node -> ../../../../firmware/de vicetree/base/cpus/cpu@002 -rw-r--r-- 1 root root 4096 2018-01-09 11:17 online drwxr-xr-x 2 root root 0 2018-01-09 11:17 power lrwxrwxrwx 1 root root 0 2018-01-09 11:17 subsystem -> ../../../../bus/cpu drwxr-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 cpu3 ======================================================== Z91:/sys/devices/system/cpu/cpu3 # ls -all ls -all total 0 drwxr-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/policy0 drwxr-xr-x 5 root root 0 2018-01-09 11:17 cpuidle lrwxrwxrwx 1 root root 0 2018-01-09 11:17 of_node -> ../../../../firmware/de vicetree/base/cpus/cpu@003 -rw-r--r-- 1 root root 4096 2018-01-09 11:17 online drwxr-xr-x 2 root root 0 2018-01-09 11:17 power lrwxrwxrwx 1 root root 0 2018-01-09 11:17 subsystem -> ../../../../bus/cpu drwxr-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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3