If you’re using an application that needs lots of CPU power, or you’re a gamer, you need to be able to switch your CPU mode (governor) from power-saving to full-performance mode. And of course vice-versa.
Usually your Linux CPU scaling is set to powersave
by default, which is more than enough and sufficient for most use cases. But sometimes you may need to get the best out of your CPU in order for games to run at their best.
To change your CPU mode (governor), you should become root
or use sudo
, then one of powersave
or performance
should be placed in the file /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
.
So, let’s say we want to switch from powersave
mode to performance
mode, we will do:
$ echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
And if we want to switch back to powersave
mode, we will do:
$ echo powersave | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
And to get our current CPU mode, we will type:
$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
That was easy, wasn’t it?
What if we create a BASH
script that makes it even easier for us to toggle between those modes? wouldn’t that be nicer? Indeed it would, and that’s why I wrote the following script:
#!/bin/bash # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 foldmethod=marker # ---------------------------------------------------------------------- # usage menu function show_usage() { #{{{ echo -e "USAGE:\t$0 powersave|performance|current" echo -e "\t\tpowersave\tSet CPU in power-saving mode." echo -e "\t\tperformance\tSet CPU in performance mode." echo -e "\t\tcurrent\t\tShow the current CPU mode." echo -e "\t\thelp\t\tShow this menu." exit 1 } #}}} # our error exit function function ee() { #{{{ echo "$1"; exit 1 } #}}} # get cpu mode function getcpumode() { #{{{ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor } #}}} # set cpu mode function setcpumode() { #{{{ [ $1 != 'powersave' -a $1 != 'performance' ] && ee 'Invalid given value..' [ $(getcpumode) == $1 ] && ee "It's already in '$1' mode" echo $1 | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor } #}}} # make sure we have root's power if [ $UID -ne 0 ]; then #{{{ echo 'Script should be executed by "root"' ee 'Or prefixed with "sudo".' fi #}}} # get & set actions case "$1" in #{{{ performance) setcpumode performance; exit 0 ;; powersave) setcpumode powersave; exit 0 ;; current|get) getcpumode; exit 0 ;; help) show_usage ;; *) ee "Try: $0 help" ;; esac #}}}
To use it, just copy it and save to /usr/local/sbin/cpumode
. And remember you should be root
or prefix the following commands with sudo
. Or clone it from my GitHub repository.
Then, if you want to switch to performance
mode, type:
$ cpumode performance
If you want to switch to powersave
mode, type:
$ cpumode powersave
If you want to get your current CPU mode, type:
$ cpumode current
If you ever forgot the commands, just type:
$ cpumode help
Nice script! However, Adnan Hodzic wrote a script to do this automagically, which might save some the trouble of setting it manually all the time.
https://github.com/AdnanHodzic/auto-cpufreq
Might not be for everybody, and sure some will like their box to do stuff when they want it, but then again no harm in checking it out…