Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you detect a dual-core cpu on an Android device from code?

Tags:

I've run into a problem that appears to affect only dual-core Android devices running Android 2.3 (Gingerbread or greater. I'd like to give a dialog regarding this issue, but only to my users that fit that criterion. I know how to check OS level but haven't found anything that can definitively tell me the device is using multi-core.

Any ideas?

like image 370
newbyca Avatar asked Nov 01 '11 03:11

newbyca


People also ask

How do I know if I have a dual core CPU?

Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.


1 Answers

Unfortunately for most Android devices, the availableProcessors() method doesn't work correctly. Even /proc/stat doesn't always show the correct number of CPUs.

The only reliable method I've found to determine the number of CPUs is to enumerate the list of virtual CPUs at /sys/devices/system/cpu/ as described in this forum post. The code:

/**  * 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 one or more digits             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;     } } 

This Java code should work in any Android application, even without root.

like image 94
David Avatar answered Sep 19 '22 12:09

David