redhat-performance / tuned

Tuning Profile Delivery Mechanism for Linux
GNU General Public License v2.0
841 stars 180 forks source link

Automatic switch profiles between AC and battery mode #299

Open sith-on-mars opened 4 years ago

sith-on-mars commented 4 years ago

Could you add the function to automatically switch profiles between AC and battery mode in the future? For example, I can use the balanced profile in the AC mode and automatically switch to the powersave profile once I unplugged the laptop. I think TLP has this feature which comes in pretty handy.

yarda commented 4 years ago

Tuned already reads udev events, so I think we could get the event. On my machine I am getting the following events:

KERNEL[5759.055142] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:01/power_supply/BAT1 (power_supply)
UDEV  [5759.062214] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:01/power_supply/BAT1 (power_supply)
KERNEL[5759.554613] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00/power_supply/AC (power_supply)
UDEV  [5759.559012] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00/power_supply/AC (power_supply)

I think we could filter for the power_supply event. The current power state can be also obtained from the /sys/class/power_supply/AC/online

The rules for switching could be incorporated into the /etc/tuned/recommend.conf, i.e. the current syntax could be extended. The only problem is that the recommend.conf file is evaluated upon start of Tuned. We will have to change it to re-evaluate it also on the power supply events. I.e. it should be doable without much changes to the current code. Also I think this feature could be beneficial. Patches are welcome.

everyx commented 1 year ago

Currently, automatic switching can be achieved through customizing udev rules.

❯ cat /etc/udev/rules.d/98-discharging.rules
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/tuned-adm profile accelerator-performance"

❯ cat /etc/udev/rules.d/98-charging.rules
SUBSYSTEM=="power_supply", ATTR{status}=="Charging", RUN+="/usr/bin/tuned-adm profile laptop-battery-powersave"
emanuelserpa commented 4 months ago

these rules didn't work here.

zacikpa commented 4 months ago

This can now be done via tuned-ppd, see PR #616.