Skip to content

Instantly share code, notes, and snippets.

@dreamcat4
Created June 23, 2014 00:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dreamcat4/640f40331719d3858483 to your computer and use it in GitHub Desktop.
Save dreamcat4/640f40331719d3858483 to your computer and use it in GitHub Desktop.
Get and set the max CPU frequency on FreeBSD / FreeNAS / NAS4Free
#!/bin/sh
#
# cpu-speed:
# Requirements - be the root user, FreeBSD 9.2 or higher.
#
# Get or set the CPU frequency. This command with no arguments will
# print the current CPU frequency. CPU may be idling at it's lowest speed.
#
# This command takes 1 optional argument - new MAX cpu freq (in Mhz).
# expressed as an integer number. e.g. "cpu 800" - set max freq 800 Mhz.
# This MAX figure is the new max frequency it is allowed to clock up to.
#
# Number is rounded off to the nearest allowed frequency multiplier.
#
show_all_cpu_sysctl_settings ()
{
# Most FreeBSD kernel settings are read-only. Some may look like duplicates.
# Very few are read-write (so we use powerd). Some CPU settings depend on the CPU family.
# This command should reveal all the CPU-related key names:
sysctl -a | grep -i cpu
}
set_cpu_speed ()
{
# To change the max clock, we must restart the "powerd" rc.d service
# with new cmdline arguments. "man powerd" for more information.
if [ "$(id -u)" = "0" ]; then
if [ ! "$(grep "cpuspeed" "/etc/rc.conf")" ]; then
echo "" >> "/etc/rc.conf"
echo "powerd_enable=\"YES\"" >> "/etc/rc.conf"
# make the cmdline argument to -M flag a txt substitution with the txt file "/etc/cpuspeed"
echo "powerd_flags=\"-M \$(cat /etc/cpuspeed)\"" >> "/etc/rc.conf"
fi
if [ "$1" ]; then
# write our new cpu speed to the txt file "/etc/cpuspeed"
echo "$1" > /etc/cpuspeed
# restart powerd daemon to read the new cpu speed
/etc/rc.d/powerd restart > /dev/null
fi
fi
}
print_current_cpu_freq ()
{
# Report back the current cpu frequency for core "0"
sysctl dev.cpu.0.freq
}
show_possible_cpu_speeds ()
{
# Show a list of all possible cpu frequency steps for core "0"
sysctl dev.cpu.0.freq_levels
}
main ()
{
set_cpu_speed "$@";
sleep 1; # give it a chance to update before we check the new value
print_current_cpu_freq;
show_possible_cpu_speeds;
}
# Entry point
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment