2013-12-08 106 views
0

我們都知道Android操作系統支持多核心架構。但是,如何測試這些線程實際上是在多個內核上進行處理的?它是一個項目分配。請建議如何繼續。如何查看Android操作系統真的使用多核心

public int numCores() throws IOException { 
    Pattern processorLinePattern = Pattern.compile("^processor\\s+: \\d+$", Pattern.MULTILINE); 
    String cpuinfo = Files.toString(new File("/proc/cpuinfo"), Charsets.US_ASCII); 
    Matcher matcher = processorLinePattern.matcher(cpuinfo); 
    int count = 0; 
    while (matcher.find()) { 
     count++; 
    } 
    return count; 
} 

通常你應該喜歡做與像番石榴或Apache下議院助手庫I/O,有很多優勢的情況下獲得:

回答

1
try this.Might help you 

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()); 
     //Return the number of cores (virtual CPU devices) 
     return files.length; 
    } catch(Exception e) { 
     //Default to return 1 core 
     return 1; 
    } 
} 
0

使用Guava's Files helper讀取文件試試parsing /proc/cpuinfo,例如對。如果你需要避免依賴,自己實現類似的東西(這個示例實現有幾個漏洞,但並不太可怕):

private String readFile(File file, Charset charset) throws IOException { 
    StringBuilder result = new StringBuilder(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); 
    try { 
     String line; 
     while ((line = reader.readLine()) != null) { 
      result.append(line).append("\n"); 
     } 
     return result.toString(); 
    } finally { 
     reader.close(); 
    } 
} 

public int numCores() throws IOException { 
    // ... 
    String cpuinfo = readFile(new File("/proc/cpuinfo"), Charset.forName("US-ASCII")); 
    // ... 
}