【os】Lock cpu freq on Linux and Android
Overview
Sometimes, we need to lock the cpu freq, so that:
- profile source code with timer functions
- etc.
Precondition
Need super user permision.
- Android
- devices must be rooted first, or else you only have read-only permision to files in /sys/devices/system/cpu/cpu0/cpufreq/...
- Linux
- Check if you have files in /sys/devices/system/cpu/cpu0/cpufreq/...
- You have to use super user.
How to lock
- set scaling_governor to userspace
- set scaling_setspeed to freq you want.
See more details here:
1 #!/bin/sh 2 3 cpucount=`cat /proc/cpuinfo|grep processor|wc -l` 4 5 if [ "x$1" = "x-h" ]; then 6 echo "There are ${cpucount} cpu on this device." 7 echo "This script will lock one of the cpu to the lowest freq." 8 echo "For example: '$ ./lock_to_lowest_freq.sh 0' will:" 9 echo " lock cpu0 to the lowest freq." 10 echo " The lowest freq is the smallest one in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies" 11 exit 12 fi 13 14 if [ "x$1" = "x" ]; then 15 printf "You must enter a cpu number, which is 0~%d.\n" $((cpucount-1)) 16 echo "Use -h to see more details." 17 exit 18 fi 19 20 if [ $1 -lt 0 -o $1 -ge $cpucount ]; then 21 printf "You must enter a cpu number, which is 0~%d.\n" $((cpucount-1)) 22 echo "Use -h to see more details." 23 exit 24 fi 25 26 FLROOT=/sys/devices/system/cpu/cpu${1}/cpufreq/ 27 28 echo "Available frequencies are: \c" 29 cat ${FLROOT}scaling_available_frequencies 30 31 echo "Available governors are: \c" 32 cat ${FLROOT}scaling_available_governors 33 34 echo "Current cpu${1} governor is: \c" 35 cat ${FLROOT}scaling_governor 36 37 echo "Current cpu${1} freq is: \c" 38 cat ${FLROOT}cpuinfo_cur_freq 39 40 target_freq=`cat ${FLROOT}scaling_available_frequencies | sed 's/ /\n/g' | sort -n | sed '/^$/d' | head -n 1` # get min freq 41 echo "This script will set cpu${1} governor to 'userspace', and set its freq to ${target_freq}." 42 echo -n "Continue? [y(default)/n]: " 43 read in 44 if [ -n "$in" -a ! "$in" = "y" ] ; then 45 exit 46 fi 47 48 echo userspace > ${FLROOT}scaling_governor 49 echo ${target_freq} > ${FLROOT}scaling_setspeed 50 51 echo "Current cpu${1} freq is: \c" 52 cat ${FLROOT}cpuinfo_cur_freq
Exceptions
For Android, sometimes only CPU0 /sys/devices/system/cpu/cpu0 has a directory named cpufreq/. Other CPU only has symlink file, which refer to cpu0/cpufreq/. Or other CPUs don't have cpufreq directory at all. For this situation, we only lock CPU0's freq, and we assume that all other CPUs' freq is locked to the same freq. We haven't find any documents or website to prove this, but we highly guess it's true from our profiling results.
Ref
- https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
- This page has a detailed explaination of files in /sys/devices/system/cpu/cpu0/cpufreq/...
- https://software.intel.com/sites/default/files/comment/1716807/how-to-change-frequency-on-linux-pub.txt
- This page has detailed explaination on how to lock cpu freq
- a nice script to lock all cpu freq, which is at the bottom of the page.