Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of cores of an android device

Tags:

android

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

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

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

UPDATE:

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 !

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.

UPDATE

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

like image 805
Vivek Warde Avatar asked May 08 '15 08:05

Vivek Warde


1 Answers

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 ()

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.

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 -> single CPU/core
  • 0-1 -> two CPUs/cores
  • 0-3 -> four CPUs/cores
  • 0-7 -> eight CPUs/cores

etc.

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

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.

EDIT: Posting code to help you out

 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) {                 }             }         }     } 

With the result

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

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

like image 141
Bojan Kseneman Avatar answered Sep 22 '22 19:09

Bojan Kseneman