我想确定(或计算)Android设备的嵌入式处理器中的核心数量.
我尝试使用/proc/cpuinfo,但它没有返回设备的核心数量!
我没有在互联网上的任何地方找到代码.这里有谁知道我怎么能确定这个,那么请回答.提前致谢
更新:
关于这个问题的答案如何从代码中检测Android设备上的双核cpu?某些设备运行不佳.我测试的是双核和四核处理器,它们分别返回2和4,很好!
但是,在像三星Note 3这样的Octa Core处理器上,它返回了4.(也许在注3中有2套四核处理器单独运行)
我本来想解决这个问题.
UPDATE
应用程序CPU-Z在我的设备Samsung note 3中返回正确的核心数
这似乎存在一种可能的解决方案......
Boj*_*man 45
您可以将上述答案与此答案结合使用.这样,它将在运行API 17+的设备上执行得更快,因为此方法比过滤文件快得多.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
public int availableProcessors()
在API级别1中添加返回VM可用的处理器核心数量,至少为1.传统上,这返回了当前在线的数量,但许多移动设备能够使未使用的核心脱机以节省电量,因此发布的版本比Android 4.2更新( Jelly Bean)返回在没有电源或热量限制的情况下可用的最大核心数.
还有关于文件内核心数量的信息.
/sys/devices/system/cpu/present它以下列格式报告可用CPU的数量:
等等
另外请检查/sys/devices/system/cpu/possible注释3中的内容
这两个文件都设置了文件r--r--r--或444文件权限,因此您应该能够在代码中没有root设备的情况下读取它们.
编辑:发布代码来帮助您
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) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果
D//sys/devices/system/cpu/present? 0-3
D//sys/devices/system/cpu/possible? 0-3
Run Code Online (Sandbox Code Playgroud)
测试是在运行带有Android v5.1.1的BlissPop ROM的OnePlus One上运行的,它应该打印出来.请试试你的三星
首先,似乎这是不可能的,这里有一些信息:
我们将使用三星Exynos 5 Octa CPU作为此线程的示例,但通常任何ARM处理器都将使用相同的工作.
三星Exynos 5 Octa CPU有8个CPU内核.但实际上它有两个进程,每个进程有4个核心.这叫做big.LITTLE.您有1个快速处理器和1个节能处理器.
在Exynos 5 CPU中,它内置了两个不同的CPU,一个Cortex a15和一个Cortex a7 ......
在big.LITTLE中,两个CPU都无法同时运行,在任何给定时间只能有一个CPU处于活动状态......
但是还有一个名为"big.LITTLE mp"的东西,其中两个CPU可以同时处于活动状态,但这里是捕获(再次!)在任何给定时间,只有四个核可以对软件有效.这是一个更好地解释这一点的图像:

现在您可以看到,这里功能更强大的处理器使用单个CPU内核,然后从更节能的a7 CPU集群中激活其他四个内核.
您可以在此处阅读有关big.LITTLE架构的更多信息:http: //www.arm.com/products/processors/technologies/biglittleprocessing.php
您可以在此处阅读有关big.LITTLE mp架构的更多信息:http: //www.arm.com/products/processors/technologies/biglittleprocessing.php
您现在想要了解的是,是否可以知道CPU架构是Big.LITTLE还是BIG.LITTLE mp.然后看看你是否可以直接找出每个CPU的CPU数量,但我找不到任何相关的代码.ARMs网站上有很多文档,但我不太确定是否可以获取这些信息.无论哪种方式,只能使用4个CPU内核,因此您拥有的代码在技术上是正确的,因为这是可用内核的数量.
我希望这有帮助,如果您有任何疑问或想要清理任何事情,请告诉我.那里有很多信息
试试这个:
Runtime.getRuntime().availableProcessors();
Run Code Online (Sandbox Code Playgroud)
根据我的经验,这将返回此特定虚拟机可用的CPU数量.这可能不是你想要的,但是,出于一些目的,这是非常方便的.您可以非常轻松地测试它:杀死所有应用程序,运行上面的代码.打开10个非常密集的应用程序,然后再次运行测试.当然,我认为这只适用于多CPU设备.
用这个来得到没有。请仔细阅读此链接,并了解作者试图区分虚拟设备和真实设备的内容。
该代码来自本网站
/**
* 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 getNumCores() {
//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());
Log.d(TAG, "CPU Count: "+files.length);
//Return the number of cores (virtual CPU devices)
return files.length;
} catch(Exception e) {
//Print exception
Log.d(TAG, "CPU Count: Failed.");
e.printStackTrace();
//Default to return 1 core
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23828 次 |
| 最近记录: |