Linux: Toggling CPU governor aka CPU speed

Throttling CPU speed on a laptop makes sense, but when I’m compiling and it takes longer, I can watch the CPU speed jump up and down when I think it should just stay at full speed.

There default CPU mode for me is “conservative”, this is recommended for laptops. Aside from that the governor “performance” is built into my kernel

There are tools to switch governors automatically, but I like to leave it at “conservative” and setting it to “performance” only when I need it.

The heat from the fan output is a reliable reminder to throttle it down later on ;-)

Update

What was I thinking last night …
The entire script below does only this:

cpufreq-set performance

and to reset:

cpufreq-set conservative

Of course you have to know the names of your governors that way, but who doesn’t? ;-)

The script: toggle-cpu-governor

Make this executable and start it as root. I placed it in /usr/local/bin.
It should just switch to the available governor – if you only have 2 governors it will toggle between them.

Note:
Check out http://www.gentoo.org/doc/en/power-management-guide.xml#doc_chap2 for required kernel settings.
You’ll need at least 2 cpu governors and ACPI Processor P-States driver. If you build the later as module, make sure the module is loaded (modprobe acpi-cpufreq).

#!/bin/sh
# Author: Stefan Kuhn <woudan@hispeed.ch>
#
# toggles cpu governors
# (or loops through the list if more then 2 governors are available)

# sanity check
if [ ! -x /usr/bin/cpufreq-info ]  || [ ! -x /usr/bin/cpufreq-set ]; then
	echo "Error: /usr/bin/cpufreq-info or /usr/bin/cpufreq-set not found"
	echo "Do you have sys-power/cpufrequtils installed?"
	exit 1
fi


setNextGovernor()
{
	# get available governors
	local allGovernors=$(cpufreq-info -g)
	local currentGov=$(cpufreq-info -p | sed -r 's/^.*[[:space:]]+([[:alnum:]]+)$/\1/')
	local found=0
	local newGov=
	for gov in $allGovernors; do
		if [ $found -ne 0 ]; then
			newGov=$gov
			break
		fi
		[ "$gov" == "$currentGov" ] && found=1
	done
	# last gov was active, set to first
	if [ "$newGov" == '' ]; then
		newGov=$(echo "$allGovernors" | sed -r 's/^([[:alnum:]]+)[[:space:]]+.*$/\1/')
	fi
	echo "Available governors: \"$allGovernors\""
	echo "Old governor: \"$currentGov\""
	# set new governor
	cpufreq-set -r -g $newGov
	if [ $? -eq 0 ]; then
		echo "New governor: $newGov"
		exit 0
	else
		echo "CPU governor not set ... are you root?"
	fi
}

setNextGovernor
Advertisements

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: