CPU Frequency Scaling

24 05 2011

I have written a small shell script to increase or decrease CPU frequency. By default, it shows current CPU Frequency Scaling Governor. This can be changed only by root user. So this script needs to be run as root user or sudo as root, while changing the CPU Frequency Scaling governor.

#!/bin/bash

available_governors=$(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_available_governors \
            | head -1 | sed -e 's/ \([a-zA-Z0-9]\)/|\1/g' -e 's/ $//')
if [ $# -ne 1 ]
then

   echo "USAGE: $0 [$available_governors]"
fi

echo "Command line to change CPU Scaling."
echo "                    - By Mitesh Singh Jat"
echo ""

## CPU Governor path
#/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
function current_cpu_governor ()
{
   echo -n "Current CPU Scaling Governor is: "
   cpu_scaling_governor="NOT SET"
   for governor in $(ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
   do
       cpu_scaling_governor=$(cat $governor)
   done
   echo "$cpu_scaling_governor"
}

current_cpu_governor;

## Exit, if no governor is provided
new_governor=""
if [ $# -eq 0 ]
then
   exit 0
else
   new_governor="$1"
fi

## Run as root always
user_id=`whoami`
if [[ "$user_id" != "root" ]]
then
   echo "$0: please run this script as root user."
   exit
fi 

if [ -z $(echo $available_governors | sed -e 's/^/|/' -e 's/$/|/' | grep "|$new_governor|") ]
then
   echo "Sorry, this mode '$new_governor' is not supported."
   exit 1
else
   echo "Setting CPU into '$new_governor' Mode..."
fi
## Now set cpu governor to the given mode
for governor in $(ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
do
   echo "$new_governor" > $governor
done
current_cpu_governor;

exit 0

Sample Run

Getting current CPU Frequency Scaling Governor
$ cd /path/where/cpu_scaling.sh/is/copied/
$ ./cpu_scaling.sh
USAGE: ./cpu_scaling.sh [powersave|conservative|ondemand|userspace|performance]
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: ondemand

Increasing CPU frequency (Please run as root).
$ ./cpu_scaling.sh performance
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: ondemand
./cpu_scaling.sh: please run this script as root user.
$ sudo ./cpu_scaling.sh performance
[sudo] password for mitesh:
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: ondemand
Setting CPU into Performance Mode...
Current CPU Scaling Governor is: performance
$ ./cpu_scaling.sh
USAGE: ./cpu_scaling.sh [powersave|conservative|ondemand|userspace|performance]
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: performance

Decreasing CPU frequency
$ sudo ./cpu_scaling.sh ondemand
Command line to change CPU Scaling.
                   - By Mitesh Singh Jat

Current CPU Scaling Governor is: performance
Setting CPU into OnDemand Mode...
Current CPU Scaling Governor is: ondemand
Advertisement

Actions

Information

2 responses

27 05 2011
Jeff

Thanks for the bash script!

Useful while setuping my new laptop which was agressive on cpu.

Just an improvement idea: you can use “xxx/cpufreq/scaling_available_governor” to get a space delimited list of supported mode for each CPU, so the new_governor logic is not required anymore and you are sure that the CPU support the mode given. For now I’ve just commented the check to get the conservative mode working, which is the best for my usage.

27 05 2011
miteshsjat

Thank you! Jeff for using this script and valuable suggestion. :)

I have incorporated your suggestion into the script.

Regards,
Mitesh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.