As a result of the project, we analyzed Facebook's Open source project-Device year Class.
The main function of the device year Class is to rate the device based on the number of CPU cores , clock frequency , and memory size . The code is simple and consists of only two classes:
DeviceInfo, get device parameters,
YearClass, according to the parameters of the rating.
The table below is a ranking standard provided by Facebook, where the Year column represents the grading results.
| Year
cores |
Clock |
RAM |
| 2008 |
1 |
528mhz |
192mb |
| 2009 |
n/a |
600mhz |
290MB |
| 2010 |
n/a |
1.0ghz |
512mb |
| 2011 |
2 |
1.2ghz |
1gb |
| 2012 |
n/a |
2.0ghz |
2gb |
| 2014 |
n/a |
>2ghz |
>2GB |
The calculation method of the output year can refer to the source code, this article only some of the more commonly used functions extracted to do a brief introduction.
Gets the number of CPU cores
As we all know, the devices in Linux are in the form of files, CPU is no exception, so the number of CPU files is equivalent to the number of cores .
The Android CPU device files are located in the /sys/devices/system/cpu/ directory, and the file name is in the format cpu\d+ .
[Email protected]_x86_64:/sys/devices/system/cpu # lscpu0cpufreqcpuidlekernel_ Maxmodaliasofflineonlinepossiblepowerpresentuevent
Count the number of files to get CPU cores.
Public Static int Getnumberofcpucores() {if(Build.VERSION.SDK_INT <= build.version_codes. GINGERBREAD_MR1) {//Gingerbread doesn ' t support giving a single application access to both cores, but a //Handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core //chipset and gingerbread; that can-let's app in the background run without impacting //The foreground application. purposes, it makes them single core. return 1; }intcores;Try{cores =NewFile ("/sys/devices/system/cpu/"). Listfiles (cpu_filter). length; }Catch(SecurityException e) {cores = Deviceinfo_unknown; }Catch(NullPointerException e) {cores = Deviceinfo_unknown; }returnCores;}Private Static FinalFileFilter Cpu_filter =NewFileFilter () {@Override Public Boolean Accept(File pathname) {String path = Pathname.getname ();//regex is slow, so checking char by Char. if(Path.startswith ("CPU")) { for(inti =3; I < path.length (); i++) {if(Path.charat (i) <' 0 '|| Path.charat (i) >' 9 ') {return false; } }return true; }return false; }};
Get clock frequency
Getting the clock frequency requires reading the system files- /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq or /proc/cpuinfo .
There are no files in my Android emulator cpuinfo_max_freq , so I can only read them /proc/cpuinfo .
/proc/cpuinfoContains a lot of CPU data.
Processor : 0vendor_id : Genuineintelcpu family : 6model : 70model name : Intel (R) Core (TM) I7-4770HQ CPU @ 2.20GHzstepping : 1cpu MHz : 0.000cache size : 1024x768 kbfdiv_bug : nohlt_bug : Nof 00f_bug: nocoma_bug : NOFPU : yesfpu_exception : Yescpuid level:4wp : Yes
The code is as follows:
Public Static int getcpumaxfreqkhz() {intMaxFreq = Deviceinfo_unknown;Try{ for(inti =0; I < getnumberofcpucores (); i++) {String filename ="/sys/devices/system/cpu/cpu"+ i +"/cpufreq/cpuinfo_max_freq"; File Cpuinfomaxfreqfile =NewFile (filename);if(Cpuinfomaxfreqfile.exists ()) {byte[] buffer =New byte[ -]; FileInputStream stream =NewFileInputStream (Cpuinfomaxfreqfile);Try{stream.read (buffer);intEndIndex =0;//trim the first number out of the of the byte buffer. while(Buffer[endindex] >=' 0 '&& Buffer[endindex] <=' 9 '&& EndIndex < buffer.length) endindex++; String str =NewString (Buffer,0, EndIndex); Integer freqbound = Integer.parseint (str);if(Freqbound > MaxFreq) maxFreq = Freqbound; }Catch(NumberFormatException e) {//fall through and use/proc/cpuinfo.}finally{Stream.Close (); } } }if(MaxFreq = = Deviceinfo_unknown) {FileInputStream stream =NewFileInputStream ("/proc/cpuinfo");Try{intFreqbound = Parsefileforvalue ("CPU MHz", stream); Freqbound *= +;//mhz-KHz if(Freqbound > MaxFreq) maxFreq = Freqbound; }finally{Stream.Close (); } } }Catch(IOException e) {maxFreq = Deviceinfo_unknown;//fall through and return unknown.}returnMaxFreq;}
Get memory size
If the SDK version is greater than JELLY_BEAN or equal, you can ActivityManager get the inside from the size.
new ActivityManager.MemoryInfo();ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);am.getMemoryInfo(memInfo);
If the version JELLY_BEAN is lower, only the system files can be read.
new FileInputStream("/proc/meminfo");totalMem = parseFileForValue("MemTotal", stream);
The complete code is as follows:
@TargetApi(Build.version_codes. Jelly_bean) Public Static Long gettotalmemory(Context c) {//Meminfo.totalmem not supported in pre-jelly Bean APIs. if(Build.VERSION.SDK_INT >= build.version_codes. Jelly_bean) {Activitymanager.memoryinfo Meminfo =NewActivitymanager.memoryinfo (); Activitymanager am = (activitymanager) c.getsystemservice (Context.activity_service); Am.getmemoryinfo (Meminfo);if(Meminfo! =NULL) {returnMeminfo.totalmem; }Else{returnDeviceinfo_unknown; } }Else{LongTotalmem = Deviceinfo_unknown;Try{FileInputStream stream =NewFileInputStream ("/proc/meminfo");Try{Totalmem = Parsefileforvalue ("Memtotal", stream); Totalmem *=1024x768; }finally{Stream.Close (); } }Catch(IOException e) { }returnTotalmem; }}
Guess you like it.
- Fully Master Android Data Binding
- Android Streaming Layout-FlowLayout
- Picture Slider implemented by Viewpager
How to get the CPU cores, clock frequency, and memory size of an Android device