├── LICENSE ├── README └── pow /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Dylan Araps 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | POW - Simple laptop power management. 2 | 3 | This program has two commands, 'pow bat' for swapping to 4 | battery mode and 'pow pow' for swapping to power (or AC) 5 | mode. 6 | 7 | How exactly this utility runs is up to you, the user. 8 | All this program does is provide the switching mechanism. 9 | 10 | All powertop tunables passed in testing on my machine. 11 | There may be missing rules for hardware I do not own. 12 | Please open an issue with powertop output and I'll add 13 | support. 14 | 15 | There is no configuration file. Simply edit the program 16 | to enable/disable the functionality you require. 17 | It's tiny in size. 18 | -------------------------------------------------------------------------------- /pow: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # pow - Simple laptop power management. 4 | 5 | log() { 6 | printf '%s\n' "$*" 7 | } 8 | 9 | swap() { 10 | # Swap between two values based on mode. 11 | # Usage: swap bat_val pow_val dest_file. 12 | [ "$mode" = bat ] || set -- "$2" "" "$3" 13 | [ -f "$3" ] || return 0 14 | 15 | printf %s "$1" 2>/dev/null > "$3" || return 0 16 | log Changed "$3" to "$1" 17 | } 18 | 19 | main() { 20 | # Require root to run pow. Simply unset all arguments 21 | # so the usage error is triggered below. 22 | [ "$(id -u)" = 0 ] || 23 | set -- 24 | 25 | # Store the desired mode. If empty (no passed arg), 26 | # exit displaying usage. 27 | mode=${1:?Usage: ${0##*/} bat|pow (run as root)} 28 | 29 | # If an invalid mode was given as input, rerun main() 30 | # with no argument to display the above usage message. 31 | [ "$mode" = bat ] || [ "$mode" = pow ] || 32 | main "" 33 | 34 | log -\> Tweaking virtual memory; { 35 | # How often the kernel will swap physical memory 36 | # to the disk. The higher the value, the more 37 | # swaps will occur. 38 | # 39 | # Leave this set at '10' regardless of power mode. 40 | # The default is typically '60' for laptop/desktop 41 | # workloads though '10' is being recommended more 42 | # and more today. 43 | # 44 | # For reference, on server and hypervisor workloads 45 | # this is usually set to '0'. Users of systems 46 | # with a good amount of ram and an SSD sometimes 47 | # set this to '1' to increase the lifespan of SSDs. 48 | # 49 | # This has the potential to improve performance 50 | # though this is on a case-by-case basis and 51 | # depends on the system and its workloads. 52 | swap 10 10 /proc/sys/vm/swappiness 53 | 54 | # The kernel flusher threads will periodically wake up 55 | # and write old data out to disk. This controls how 56 | # often this happens. 57 | # 58 | # Set to '1500' on battery as per powertop 59 | # recommendations. 60 | swap 1500 500 /proc/sys/vm/dirty_writeback_centisecs 61 | 62 | # Let's not touch any other settings in /proc/sys/vm/ 63 | # and instead toggle the kernel's built in laptop 64 | # mode to handle them for us. 65 | # 66 | # kernel.org/doc/Documentation/laptops/laptop-mode.txt 67 | swap 5 0 /proc/sys/vm/laptop_mode 68 | } 69 | 70 | log -\> Tweaking CPU governor/scheduler; { 71 | # Set the CPU governor to 'ondemand' when running on 72 | # battery and 'performance' when running on power. 73 | # 74 | # Most distributions today will leave the system on 75 | # 'ondemand' by default regardless of mode. 76 | for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do 77 | swap ondemand performance "$cpu" 78 | done 79 | 80 | # Power aware CPU scheduler. Not all systems have 81 | # this. pow will silently skip changing this option. 82 | swap 1 0 /sys/devices/system/cpu/sched_mc_power_savings 83 | } 84 | 85 | log -\> Tweaking SCSI link power policy; { 86 | for cpu in /sys/class/scsi_host/host*/link_power_management_policy; do 87 | swap min_power max_performance "$cpu" 88 | done 89 | } 90 | 91 | log -\> Tweaking device auto-suspend; { 92 | for dev in /sys/bus/usb/devices/*/power/control \ 93 | /sys/bus/pci/devices/*/power/control \ 94 | /sys/bus/pci/devices/*/ata[0-9]*/power/control \ 95 | /sys/bus/i2c/devices/*/power/control \ 96 | /sys/block/*/power/control; do 97 | swap auto on "$dev" 98 | done 99 | } 100 | 101 | log -\> Tweaking ASPM; { 102 | # WARNING: This may cause a system hang if ASPM isn't 103 | # supported by your hardware. 104 | # 105 | # WARNING: Comment this out if that is the case. 106 | swap powersave performance /sys/module/pcie_aspm/parameters/policy 107 | } 108 | 109 | log -\> Tweaking display brightness; { 110 | # Set the brightness to '80%' of the maximum when on 111 | # battery. Change the '8' if you'd like to lower the 112 | # brightness further. 113 | for mon in /sys/class/backlight/*/; do 114 | read -r max < "$mon/max_brightness" 115 | 116 | swap "$((max * 8 / 10))" "$max" > "$mon/brightness" 117 | done 118 | } 119 | 120 | # log -\> Turning Wifi off/on; { 121 | # # Self explanatory. Uncomment to enable the 122 | # # behavior. 123 | # case $mode in 124 | # bat) rfkill block wlan ;; 125 | # pow) rfkill unblock wlan ;; 126 | # esac 127 | 128 | # rfkill list wlan 129 | # } ||: 130 | 131 | log -\> Turning bluetooth off/on; { 132 | # Self explanatory. Comment this out to disable the 133 | # behavior. Change pow's rfkill command to unblock 134 | # to enable bluetooth on power. 135 | case $mode in 136 | bat) rfkill block bluetooth ;; 137 | pow) rfkill block bluetooth ;; 138 | esac 139 | 140 | rfkill list bluetooth 141 | } ||: 142 | 143 | log -\> Tweaking wake on lan; { 144 | # Let's always disable this regardless of mode. 145 | # Comment this out or see the bluetooth section 146 | # above for how to toggle it. 147 | for dev in /sys/class/net/*; do 148 | ethtool -s "$dev" wol d >/dev/null 2>&1 149 | done 150 | } ||: 151 | 152 | log -\> Tweaking kernel watchdog; { 153 | # Disable the kernel watchdog when on battery. 154 | # 155 | # This disables periodic interrupts which the kernel 156 | # uses to monitor whether any CPU has locked up and 157 | # and print debug messages if so. 158 | # 159 | # Set this to '0 1' to enable the kernel watchdog 160 | # when on power. Default is always disabled. 161 | swap 0 0 /proc/sys/kernel/nmi_watchdog 162 | } 163 | } 164 | 165 | main "$@" 166 | --------------------------------------------------------------------------------