26

Is there any Java API for that? How can I read this information.

michael
  • 3,250
  • 10
  • 43
  • 58

3 Answers3

50

To have frequency on Android, just read these special files in /sys directory:

#cat "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
#cat "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"
#cat "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"

You will have current, min and max Frequency allowed.

Ellis
  • 1,521
  • 1
  • 14
  • 16
  • 3
    Awesome answer Ellis, you really helped me a lot. One thing that I found in investigating this was that scaling_cur_freq is not necessarily the current CPU frequency, but rather what the kernel thinks the frequency is. To get the real frequency, you need root access to read cpuinfo_cur_freq. Also, gaining root access allows you to set the cpu speed, which is quite useful for profiling under best/worst case conditions. – JonnyBoy May 25 '12 at 19:01
  • Hummm It seems to me that even with root access you can't edit the cpuinfo_* files.. – JohnTortugo Jan 30 '14 at 18:49
  • How can we get CPU load or utilization? – Ashkan Jun 28 '15 at 19:54
  • How can these files be read from within an app? – Cornelius Roemer Dec 13 '19 at 18:49
  • I notice subdirectory `policyX` under `/sys/devices/system/cpu/cpufreq/`, what is that? – user1651758 Dec 17 '21 at 14:47
8

not MHz, but at least something. bogoMIPS value can be useful for you.

private String ReadCPUinfo()
 {
  ProcessBuilder cmd;
  String result="";

  try{
   String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
   cmd = new ProcessBuilder(args);

   Process process = cmd.start();
   InputStream in = process.getInputStream();
   byte[] re = new byte[1024];
   while(in.read(re) != -1){
    System.out.println(new String(re));
    result = result + new String(re);
   }
   in.close();
  } catch(IOException ex){
   ex.printStackTrace();
  }
  return result;
 }
zed_0xff
  • 32,417
  • 7
  • 53
  • 72
  • Thanks, but is there any way to convert it to MHz or read it somewhere? – michael Jun 11 '10 at 16:18
  • 11
    Any reason you're not just opening /proc/cpuinfo as a file and reading it directly? – fadden Jun 11 '10 at 23:38
  • michael, read a wiki page about bogoMIPS, there's a formula. fadden, I'm not sure that file could be simply read from java, just found this code laying around. – zed_0xff Jun 12 '10 at 13:22
4

If you are interested in how long your system spent in what state, check out the file

/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state

I'm not sure, whether root access is necessary for that.

Michael Kopp
  • 1,571
  • 12
  • 17