Change CPU governor automatically
Introduction
I started this new project changegovernor to auto-magically change the CPU governor, from powersave to performance and the other way around, based on the presence of “know” process name, cpu threshold and hardware temperatures taken from libsensors.
I wrote it in python3 (mostly in python 3.7) and I kept it simple and stupid, like it’s programming flow, who is looking for:
- reaching critical system hardware temperatures;
- presence of user defined processes;
- looking for cpu threshold in percentages.
If you’re interested on more detailed programming flow, have a look at this flow chart
Configure kernel (optional)
For those people, who like builds own kernel’s, I whould suggest you to configure cpu frequency scaling as below:
Power management and ACPI options --->
CPU Frequency scaling --->
-*- CPU Frequency scaling
[*] CPU frequency transition statistics
Default CPUFreq governor (powersave) --->
<*> 'performance' governor
-*- 'powersave' governor
<*> 'userspace' governor for userspace frequency scaling
<*> 'ondemand' cpufreq policy governor
<*> 'conservative' cpufreq governor
[*] 'schedutil' cpufreq policy governor
Install
Simply clone the project changegovernor and move changegovernor.py and changegovernor.json to system directories:
$ git clone https://github.com/amedeos/changegovernor
$ cd changegovernor
$ sudo cp changegovernor.py /usr/sbin/
$ sudo cp changegovernor.json /etc/
Configure
Process configurations
If you want to switch your linux box to performance governor for a specific process name, simply edit the /etc/changegovernor.json json file with your desired process, for example:
"processes": [
{
"name": "vlc",
"state": "present",
"extra_commands": [],
"governor": "performance"
},
in the above example, the programm will search for a process name vlc, and if in case it will find it, changegovernor.py will change the cpu governor to performance.
Hardware Temperature configurations
Before configuring changegovernor for looking on critical hardware temperatures inside the system, you have to configure libsensors to read from the kernel those data / temperatures; on most linux distrubutions this is just done, but if you can’t get any data from sensors command, let’s try to execute sensors-detect command and follow it’s suggestions.
Once you have a working libsensors configuration, and suppose you have an AMD cpu (like me) the sensors output will like this:
$ sensors
...
k10temp-pci-00c3
Adapter: PCI adapter
Tdie: +30.5°C (high = +70.0°C)
Tctl: +30.5°C
from the above output we can see that our sensor name is k10temp and it’s principal label is Tdie (have a look at the high / critical value of 70°C); if you want to force powersave cpu governornor if you reach the 95% (in this example 66.5°C) of the critical temperatures (70.0°C) let’s edit the /etc/changegovernor.json with this values:
"sensors": [
{
"name": "k10temp",
"state": "present",
"label": "Tdie",
"percent_from_critical": "5.0",
"extra_commands": [],
"governor": "powersave"
},
Cpu threshold configurations
Finally, if you want to raise the cpu governor to performance if your system load reach for example a value upper than 70%, simply edit /etc/changegovernor.json with:
"percentages": [
{
"name": "highload",
"min": "70.0",
"max": "100.0",
"state": "present",
"extra_commands": [],
"governor": "performance"
},
systemd unit file (optional)
If you want to automatic start changegovernor.py at boot, and you are using systemd simply copy the unit file and enable it:
$ sudo cp changegovernor.service /etc/systemd/system/
$ sudo systemctl daemon-reload
$ sudo systemctl enable changegovernor.service
Gentoo installation
If you’re running gentoo you could add my amedeos-overlay and then install with emerge the sys-power/changegovernor ebuild:
$ sudo -i
# EPYTHON=python3.6 layman -o https://raw.githubusercontent.com/amedeos/amedeos-overlay/master/overlay.xml -f -a amedeos
# emerge sys-power/changegovernor
# systemctl enable changegovernor.service --now