Android access to device CPU cores, clock frequencies, and memory size _android

Source: Internet
Author: User
Tags sqlite database

The examples in this article describe how Android obtains device CPU cores, clock frequencies, and memory size. Share to everyone for your reference, specific as follows:

For project needs, analyze the Facebook Open source project-Device year Class.

The main function of the Device year Class is to rank devices based on CPU cores, clock frequencies, and memory size. The code is simple and contains only two classes:

DeviceInfo-> Get device parameters,
Yearclass-> is graded according to the parameters.

The following table is the rating standard offered by Facebook, where the year column represents the hierarchical result.

Year
cores Clock RAM
2008 1 528mhz 192mb
2009 n/a 600mhz 290MB
2010 n/a 1.0ghz 512mb
2011 1.2ghz 1gb
2012 4 1.5ghz 1.5GB
2013 n/a 2.0ghz 2gb
2014 n/a >2ghz >2gb< /td>

About the output year calculation method can refer to the source code, this article only some of the more commonly used functions extracted out to do a brief introduction.

Get CPU kernel Count

As we all know, the devices in Linux are in the form of files, the CPU is no exception, so the number of files on the CPU is equivalent to the number of cores.

The Android CPU device file is located in the/sys/devices/system/cpu/directory, and the file name is in the format cpu\d+.

ROOT@GENERIC_X86_64:/SYS/DEVICES/SYSTEM/CPU # ls
cpu0
cpufreq
cpuidle
kernel_max
Modalias
offline
online
possible
power
present
uevent

Count the number of files to obtain the CPU core.

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 a app in the background run without impacting//the foreground application.
 But for our purposes, it makes them single core.
 return 1;
 int cores;
 try {cores = new File ("/sys/devices/system/cpu/"). Listfiles (cpu_filter). length;
 catch (SecurityException e) {cores = Deviceinfo_unknown;
 catch (NullPointerException e) {cores = Deviceinfo_unknown;
return cores; private static final FileFilter Cpu_filter = new FileFilter () {@Override public boolean accept (File pathname) {Stri
 ng path = Pathname.getname ();
 The regex is slow, so checking char by Char. if (Path.startswith ("CPU")) {for (int i = 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 system files-/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq or/proc/cpuinfo.

There is no cpuinfo_max_freq file in my Android emulator, so I can only read/proc/cpuinfo.

/proc/cpuinfo contains a lot of CPU data.

processor:0
Vendor_id:genuineintel
CPU Family:6
Model:70
Model Name:intel (R) Core (TM) I7-4770HQ CPU @ 2.20GHz
Stepping:1
CPU mhz:0.000
Cache size:1024 KB
Fdiv_bug:no
Hlt_bug:no
F00f_bug:no
Coma_bug:no
Fpu:yes
Fpu_exception:yes
CPUID Level:4
Wp:yes

The code is as follows:

public static int getcpumaxfreqkhz () {int maxfreq = Deviceinfo_unknown; try {for (int i = 0; i < getnumberofcpucores (); i++) {String filename = "/sys/devices/system/cpu/cpu + i +"/C
  Pufreq/cpuinfo_max_freq ";
  File Cpuinfomaxfreqfile = new file (filename);
  if (cpuinfomaxfreqfile.exists ()) {byte[] buffer = new byte[128];
  FileInputStream stream = new FileInputStream (cpuinfomaxfreqfile);
   try {stream.read (buffer);
   int endindex = 0;
   Trim the ' the ' the ' the ' of the ' the ' byte buffer. while (Buffer[endindex] >= ' 0 ' && Buffer[endindex] <= ' 9 ' && endindex < buffer.length) Endind
   ex++;
   String str = new string (buffer, 0, endindex);
   Integer freqbound = Integer.parseint (str);
  if (Freqbound > Maxfreq) maxfreq = Freqbound;
  The catch (NumberFormatException e) {//fall through and use/proc/cpuinfo.
  finally {stream.close (); }} if (Maxfreq = deviceinfo_unknown) {FileInputStream stream = new FileInputStream ("/proc/cpuinfo");
  try {int freqbound = Parsefileforvalue ("CPU MHz", stream); Freqbound *= 1000;
  MHz-> kHz if (Freqbound > Maxfreq) maxfreq = Freqbound;
  finally {stream.close ();
 The catch (IOException e) {maxfreq = Deviceinfo_unknown;//fall through and return UNKNOWN.
return maxfreq;

 }

Get memory size

If the SDK version is greater than or equal to Jelly_bean, you can obtain the inner from size by Activitymanager.

Activitymanager.memoryinfo meminfo = new Activitymanager.memoryinfo ();
Activitymanager am = (activitymanager) c.getsystemservice (context.activity_service);
Am.getmemoryinfo (Meminfo);

If the version is below Jelly_bean, only the system files can be read.

FileInputStream stream = 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 = new Activitymanager.memoryinfo ();
 Activitymanager am = (activitymanager) c.getsystemservice (Context.activity_service);
 Am.getmemoryinfo (Meminfo);
 if (meminfo!= null) {return meminfo.totalmem;
 else {return deviceinfo_unknown;
 } else {long totalmem = Deviceinfo_unknown;
  try {fileinputstream stream = new FileInputStream ("/proc/meminfo");
  try {totalmem = Parsefileforvalue ("Memtotal", stream);
  Totalmem *= 1024;
  finally {stream.close ();
 The catch (IOException e) {} return totalmem; }
}

More interested readers of Android-related content can view the site's topics: "The Overview of Android View View Tips", "Android operation XML Data Skills Summary", "Android programming activity Operation Skills Summary", " Android Resource Operation tips Summary, "Android file Operation Tips", "Android operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operating skills summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.