# How can I determine Max CPU speed at runtime?

I'd like to know the max cpu speed for the processor on an Android system at runtime. I've seen at least a couple different techniques that result in the current cpu clockrate, but not the max.

Root privileges should not be required.

After digging through the source that roxan mentioned a bit, it looks like the following in /proc can tell you what cpu speeds are available and how much time has been spent in each:

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

However, it does give some other states that aren't appropriate. For example, on my Samsung Vibrant, I get:

``````1400000 0
1300000 0
1200000 0
1000000 214141
800000 40092
400000 25273
200000 6159308
100000 389
``````

But the chip's standard clock is 1 GHz. So it looks like it should be fairly trivial to find the highest listed speed that has a non-zero time to it.

For my purposes, this should be very adequate.

Edit: I've observed that sometimes the order in which the frequencies is listed can vary, so you'll want to iterate through the whole list and find the fastest (as opposed to stopping at the first non-zero).

More Edit: Here's a function that works for me:

``````/**
* Get max cpu rate.
*
* This works by examining the list of CPU frequencies in the pseudo file
* "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state" and how much time has been spent
* in each. It finds the highest non-zero time and assumes that is the maximum frequency (note
* that sometimes frequencies higher than that which was designed can be reported.) So it is not
* impossible that this method will return an incorrect CPU frequency.
*
* Also note that (obviously) this will not reflect different CPU cores with different
* maximum speeds.
*
* @return cpu frequency in MHz
*/
public static int getMaxCPUFreqMHz() {

int maxFreq = -1;
try {

RandomAccessFile reader = new RandomAccessFile( "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state", "r" );

boolean done = false;
while ( ! done ) {
if ( null == line ) {
done = true;
break;
}
String[] splits = line.split( "\\s+" );
assert ( splits.length == 2 );
int timeInState = Integer.parseInt( splits[1] );
if ( timeInState > 0 ) {
int freq = Integer.parseInt( splits[0] ) / 1000;
if ( freq > maxFreq ) {
maxFreq = freq;
}
}
}

} catch ( IOException ex ) {
ex.printStackTrace();
}

return maxFreq;
}
``````
• Thank you but development questions are off topic here :D – roxan Feb 23 '12 at 4:59
• Yeah, I realized this was not placed appropriately. I can't move it though :) – notlesh Feb 23 '12 at 6:47

I use this tiny, open source awesome app called CPU spy.

Not only this shows you the maximum CPU clock rate that your phone supports it also shows you all the clock rates that your phone supports. It will also display the total time and percentage the phone spends on each clock rate.

• Thanks, I might look through the source code there. I should have specified, though; I'm looking to do this in a game that I'm writing. – notlesh Feb 23 '12 at 4:03

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

Does not return the correct values. For example on my own device

``````cat /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state
1000000 192
800000 3
600000 12915
300000 20519

# cat /proc/cpuinfo
cat /proc/cpuinfo
Processor       : ARMv7 Processor rev 2 (v7l)
BogoMIPS        : 1197.81
Features        : swp half thumb fastmult vfp edsp thumbee neon vfpv3
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x3
CPU part        : 0xc08
CPU revision    : 2

Hardware        : mapphone_UMTS
Revision        : 0000
Serial          : 0000000000000000
CPU Tier        : 10
``````

The frequencies are not correctly reported. As you can see, the clock speed on my device is 1200MHz - but only 1000MHz max shows in `time_in_state`.

However, the following is correct

``````cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
1200000
``````

Read this file to get the Max frequency:

``````/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max
``````