如何获取Android设备的内核数 [英] How to get the number of cores of an android device

查看:162
本文介绍了如何获取Android设备的内核数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找确定(或计数)Android设备的嵌入式处理器中的内核数.

I was looking to determine(or count) the number of cores in the embedded processor of android device.

我尝试使用/proc/cpuinfo,但是它没有返回设备的内核数!

I tried using /proc/cpuinfo, but it is not returning the number of cores of device !

我在Internet上的任何地方都找不到该代码.这里有人知道如何确定吗,请回答.预先感谢

I don't found the code anywhere on the Internet. Does anyone here know how can I determine this, then please answer. Thanks in advance

更新:

此问题的答案

The answers on this question How can you detect a dual-core cpu on an Android device from code? doesn't run well in some devices. I tested them is dual core & quad core processors, they returned 2 & 4 respectively, fine !

但是,在像三星Note 3那样的 Octa Core 处理器上,它返回了 4 . (也许在注释3中,有两组分别运行的四核处理器)

But, On Octa Core processor like in Samsung Note 3 it returned 4. (Perhaps in note 3 there are 2 sets of quad core processors running individually )

我一直想解决这个问题.

I was looking to solve this problem.

更新

应用程序 CPU-Z 在我的设备Samsung note 3中返回了正确的内核数 这里似乎存在一个可能的解决方案...

The app CPU-Z is returning the correct core count in my device Samsung note 3 Here it seems that there exists a possible solution...

推荐答案

您可以将以上答案与此结合使用.这样,它将在运行API 17+的设备上更快地执行,因为此方法比过滤文件快得多.

You may use a combination of above answer with this one. This way it will perform faster on devices running API 17+ as this method is much faster than filtering out files.

private int getNumberOfCores() {
    if(Build.VERSION.SDK_INT >= 17) {
        return Runtime.getRuntime().availableProcessors()
    }
    else {
       // Use saurabh64's answer
       return getNumCoresOldPhones();
    }
}

/**
 * Gets the number of cores available in this device, across all processors.
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
 * @return The number of cores, or 1 if failed to get result
 */
private int getNumCoresOldPhones() {
    //Private Class to display only CPU devices in the directory listing
    class CpuFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            //Check if filename is "cpu", followed by a single digit number
            if(Pattern.matches("cpu[0-9]+", pathname.getName())) {
                return true;
            }
            return false;
        }      
    }

    try {
        //Get directory containing CPU info
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about
        File[] files = dir.listFiles(new CpuFilter());
        //Return the number of cores (virtual CPU devices)
        return files.length;
    } catch(Exception e) {
        //Default to return 1 core
        return 1;
    }
}

public int availableProcessors()

public int availableProcessors ()

在API级别1中添加了返回可用处理器核心的数量 到VM,至少为1.传统上,这会返回该数字 当前处于在线状态,但是许多移动设备都可以使用未使用的设备 核心可离线以节省电量,因此发布的版本要比Android 4.2(Jelly Bean)返回可以使用的最大内核数 如果没有功率或热量限制.

Added in API level 1 Returns the number of processor cores available to the VM, at least 1. Traditionally this returned the number currently online, but many mobile devices are able to take unused cores offline to save power, so releases newer than Android 4.2 (Jelly Bean) return the maximum number of cores that could be made available if there were no power or heat constraints.

还提供了有关位于以下位置的文件中的内核数的信息 /sys/devices/system/cpu/present它以以下格式报告可用CPU的数量:

Also there is information about number of cores inside a file located in /sys/devices/system/cpu/present It reports the number of available CPUs in the following format:

  • 0->单CPU/核心
  • 0-1->两个CPU/核
  • 0-3->四个CPU/核
  • 0-7->八个CPU/核

还请检查注释3上/sys/devices/system/cpu/possible的内容

Also please check what is inside /sys/devices/system/cpu/possible on a note 3

两个文件都设置了r--r--r--444文件权限,因此您应该能够在没有root设备的情况下读取它们.

Both files have r--r--r-- or 444 file permissions set on them, so you should be able to read them without a rooted device in code.

发布代码以帮助您

 private void printNumberOfCores() {
        printFile("/sys/devices/system/cpu/present");
        printFile("/sys/devices/system/cpu/possible");

    }

    private void printFile(String path) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path);
            if (inputStream != null) {

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line;

                do {
                    line = bufferedReader.readLine();
                    Log.d(path, line);
                } while (line != null);
            }
        } catch (Exception e) {

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

结果

D//sys/devices/system/cpu/present﹕ 0-3
D//sys/devices/system/cpu/possible﹕ 0-3

该测试是在运行有Android v5.1.1的BlissPop ROM的OnePlus One上运行的,它会按需打印.请尝试使用三星

The test was run on OnePlus One running BlissPop ROM with Android v5.1.1 and it prints as it should. Please try on your Samsung

这篇关于如何获取Android设备的内核数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆