You are not logged in.
Hello,
I hope I did not oversee something about my question in the forums or in the wiki.
I am using cpupower for CPU power management of my AMD CPU based laptop. Manually switching between performance and ondemand governor works as expected.
I would like to setup my machine in a way such that the governor is chosen automatically, depending on whether the AC adapter is plugged in or not.
I read the article about Laptop Mode Tools and acpid, but this solution seems a bit overkill to me. Maybe there is an easier way nowadays? Is there an existing systemd service which can be used to execute commands when the AC power state has changed?
Greetings
Last edited by vibee (2016-01-06 16:32:14)
Offline
Here you go: http://pastebin.com/apVg4Uwb
Run it as root manually or create a systemd .service file to run automatically at startup.
EDIT: Oh yeah, I have tested this (not with a systemd file), and you might want to change the GOVERNOR_NOCHARGE, GOVERNOR_CHARGE and recur variables.
Last edited by HyperLink (2016-01-05 20:15:23)
Offline
Hey HyperLink, thanks for sharing your solution.
The best thing I could image is a single systemd service routine, which manages the CPU power settings. As far as I read, this is not implemented yet. Your solution uses kind of polling, which I would also like to prevent. I am going to share my solution, which works completely event-based.
The udev rule and the systemd script both run a bash script which has optional arguments "AC" or "BAT" (used by the udev rules). If no arguments are passed, then the current state is determined by a call to upower. I hope that I did not forget some case where governor is not adjusted.
1. Udev Rules for automatically changing the governor when system is running:
cat /etc/udev/rules.d/powersave.rules
SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/home/vibee/Repos/scripts/power.sh AC"
SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/home/vibee/Repos/scripts/power.sh BAT"
2. Systemd service file for adjusting governor when returning from suspend:
cat /etc/systemd/system/root-resume.service
[Unit]
Description=Local system resume actions
After=suspend.target
[Service]
Type=simple
ExecStart=/home/vibee/Repos/scripts/power.sh
[Install]
WantedBy=suspend.target
3. Systemd service file for adjusting governor when booting the system:
cat /etc/systemd/system/power_management.service
[Unit]
Description=Sets the CPU governor on boot according to AC mode
Requires=multi-user.target
After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/home/vibee/Repos/scripts/power.sh
[Install]
WantedBy=multi-user.target
4. The power.sh script:
cat /home/vibee/Repos/scripts/power.sh
#!/usr/bin/bash
STATE=""
BAT="BAT1"
if [[ "$1" == "BAT" || "$1" == "AC" ]]; then
STATE="$1"
fi
if [[ $STATE == "" ]]; then
if [[ $(upower -i /org/freedesktop/UPower/devices/battery_$BAT | grep state | grep discharging) == "" ]]; then
STATE="AC"
else STATE="BAT"
fi
fi
echo $STATE
if [[ $STATE == "BAT" ]]; then
echo "Discharging, set governor to ondemand"
cpupower frequency-set -g ondemand
elif [[ $STATE == "AC" ]]; then
echo "AC plugged in, set governor to performance"
cpupower frequency-set -g performance
fi
Offline
That is actually a way better solution than mine! Just one question: Why do you want to prevent polling? Unless you have a ridiculously bad PC, it shouldn't really impact your performance in any way.
Anyway, remember to mark the topic as solved if you have solved the problem.
Offline
In my opinion, polling is always a less attractive approach, whenever there is an event-based solution (this is not necessarily true for embedded systems). But with the polling solution, your system needs to switch the process context over and over again, although is most cases nothing changes (unless you attach and deattach your AC cable very often ). You most probably won't feel any performance decreases, but it's one more background process, asking for CPU each second. And it doesn't scale well, meaning that some more polling based background scripts probably will decrease battery life and performance.
By the way, as an alternative polling based solution, my script could also be run each minute by a root crontab.
Offline