FreeBSD: script to manage processor speed

Edit: this has been updated <broken link>.

You might ask: why not use powerd? Well, I tried. It sure does save energy, but there are also annoying pauses between when I start manically switching windows and it changes the frequency.

So, I wrote a C Shell script to do the thing. Yes, C Shell is a nuisance, but Bourne Shell doesn’t have arrays, and I’ve wasted quite some time trying to work around that. Then I just dumped the idea.

It ain’t that long anyway, so you are able to catch the bugs before they run away.

To use it, set the freq_levels array to what you’ve got.

The drawback is that it uses sudo when setting the frequency. I’m currently trying to find out how not to.

# Script to manage cpu freqency (scaling). Tested on FreeBSD.

## NOTE: allowed frequencies must be set manually.
## On FreeBSD, see: sysctl -a | grep dev.cpu
# -----
# dev.cpu.0.freq_levels: 1660/27000 1452/23625 1328/18900 1162/16537
# 996/10800 871/9450 747/8100 622/6750 498/5400 373/4050 249/2700 124/1350

# Smaller values go earlier.
set freq_levels = ( 373 871 996 1162 1660 )

# Get current frequency and check which level it is.
set freq_current = `sysctl dev.cpu.0.freq | cut -d' ' -f 2`
set level_current = 0	# level is unknown
set i = 1		# iterator

while ( $i <= $#freq_levels )
	if ( $freq_current == $freq_levels[$i] ) then
		@ level_current = $i
	@ i = $i + 1

if ( $level_current == 0 ) then
	echo "Current CPU frequency not in supported frequency list."
	exit 1

# Check which action is to be performed. If no argument specified,
# output help.
if ( $1 == "" ) then
	goto help
switch ($1)
	case "up":
		if ( $level_current  1 ) then
			@ level_current = $level_current - 1
			eval 'sudo sysctl dev.cpu.0.freq=$freq_levels[$level_current]'
	case "show":
		echo "Current frequency: $freq_current"
		echo "Current level:     ${level_current}/${#freq_levels}"

	case "-h":
		echo "cpuctl [ up | down | show ]"
	case "--help":
		goto help

	case "*":
		echo "Unknown parameter: $1"
		goto help

exit 0

Leave a Reply

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

You are commenting using your 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: