You are not logged in.

#1 2016-01-05 16:45:56

vibee
Member
Registered: 2013-06-15
Posts: 38

[SOLVED] cpupower: Auto-set governor depending on AC/Battery mode

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

#2 2016-01-05 19:56:00

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [SOLVED] cpupower: Auto-set governor depending on AC/Battery mode

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

#3 2016-01-06 13:00:22

vibee
Member
Registered: 2013-06-15
Posts: 38

Re: [SOLVED] cpupower: Auto-set governor depending on AC/Battery mode

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. smile

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

#4 2016-01-06 15:47:40

HyperLink
Member
From: In front of my Linux box...?!
Registered: 2014-03-14
Posts: 44

Re: [SOLVED] cpupower: Auto-set governor depending on AC/Battery mode

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. smile

Offline

#5 2016-01-06 16:31:11

vibee
Member
Registered: 2013-06-15
Posts: 38

Re: [SOLVED] cpupower: Auto-set governor depending on AC/Battery mode

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 wink). 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

Board footer

Powered by FluxBB