티스토리 뷰

안드로이드 디바이스의 CPU 코어 개수를 가져오는 코드이다. 

이 코드의 출처는 : http://stackoverflow.com/questions/7962155/how-can-you-detect-a-dual-core-cpu-on-an-android-device-from-code 

 

원리를 설명하자면 우선 /sys/devices/system/cpu/ 디렉토리를 찾고 이 내부에 cpu[0-9] 의 이름 규칙을 갖는 디렉토리를 찾는다.

 

만약 쿼드 코어라면

 

/sys/devices/system/cpu/cpu0

/sys/devices/system/cpu/cpu1

/sys/devices/system/cpu/cpu2, 

/sys/devices/system/cpu/cpu3

 

형태로 디렉토리가 존재한다.

그럼 그 디렉토리 개수를 확인 함으로써 코어 수도 알 수 있는 것이다.

위의 사항과 다른경우 1을 반환시켜 준다. 

테스트 결과 갤럭시 시리즈, 넥서스 시리즈, 옵티머스 시리즈, 등등에서는 모두 동작 하였다.

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 
* 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());
 
       //Return the number of cores (virtual CPU devices)
 
       return files.length;
 
   } catch(Exception e) {
 
       return 1;
 
   }
 
}


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함