Skip to content

Instantly share code, notes, and snippets.

@jbg
Created July 21, 2017 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbg/2d02acaabe81b2d5fb6be1425af4f787 to your computer and use it in GitHub Desktop.
Save jbg/2d02acaabe81b2d5fb6be1425af4f787 to your computer and use it in GitHub Desktop.
Script to run on udev power_supply events to adjust Macbook performance for battery/AC
#!/bin/sh -e
# Executed by udev upon power supply status changes using this rule:
# SUBSYSTEM=="power_supply", RUN+="/usr/local/bin/powerchange"
# This script sets minimum performance on battery. Adjust to suit.
BATTERY_BRIGHTNESS=250
# AC brightness is the maximum level.
if [ $POWER_SUPPLY_ONLINE = 1 ]; then
echo "Power supply online" | systemd-cat -t powerchange
x86_energy_perf_policy performance
for cpufreq in /sys/devices/system/cpu/cpu*/cpufreq ; do
cat $cpufreq/cpuinfo_max_freq > $cpufreq/scaling_max_freq
done
echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo
echo 100 > /sys/devices/system/cpu/intel_pstate/max_perf_pct
# Change the brightness last. If it doesn't change, we know something above failed and can check the journal.
cat /sys/class/backlight/gmux_backlight/max_brightness > /sys/class/backlight/gmux_backlight/brightness
else
echo "Power supply offline" | systemd-cat -t powerchange
x86_energy_perf_policy powersave
for cpufreq in /sys/devices/system/cpu/cpu*/cpufreq ; do
cat $cpufreq/scaling_min_freq > $cpufreq/scaling_max_freq
done
echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo
cat /sys/devices/system/cpu/intel_pstate/min_perf_pct > /sys/devices/system/cpu/intel_pstate/max_perf_pct
# Change the brightness last. If it doesn't change, we know something above failed and can check the journal.
echo $BATTERY_BRIGHTNESS > /sys/class/backlight/gmux_backlight/brightness
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment