Android系统的获取 CPU 核数

一 观察/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"
     * <p>
     * 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值