Keep that Laptop from Racing thru Battery

Linux and Laptops: a long, old story. However, one that with a bit homework, helps get the most out of your battery. Using powertop, you can see your energy usage profile and your device power settings. The downside to powertop is that you cannot "export as shell script." (Seriously, y u no export?) To get these power savings on boot, I wrote my own script.

  1 #!/bin/bash
  2 /usr/sbin/rfkill block 0 #bt
  3 /usr/sbin/rfkill block 1 #wifi
  4 /usr/sbin/rfkill block 2 #bt
  5 for i in /sys/class/scsi_host/host*/link_power_management_policy
  6 do
  7     echo "min_power" > $i
  8 done
  9 echo 0 > /proc/sys/kernel/nmi_watchdog
 10 echo 1 > /sys/module/snd_hda_intel/parameters/power_save
 11 echo 60000 > /proc/sys/vm/dirty_writeback_centisecs
 12 
 13 find /sys/devices/ \
 14 | grep 'power/control' \
 15 | while read i
 16 do
 17     echo -n "$i@"
 18     cat $i 2>/dev/nulll
 19 done \
 20 | grep -v auto \
 21 | awk -F@ '{print $1}' > /tmp/sleepy.tmp
 22 
 23 for i in `cat /tmp/sleepy.tmp`
 24 do
 25     echo "auto" > $i
 26 done
 27 
 28 for i in `find /sys -name autosuspend -exec echo {} \;`
 29 do
 30     echo "1" > $i
 31 done
 32 
 33 find /sys/devices/pci0000:00 -type f -iname autosuspend_delay_ms \
 34    -exec echo "1000" > {} \;
 35 
 36 for i in /sys/bus/usb/devices/usb*/power/autosuspend_delay_ms
 37 do
 38     echo "1000" > $i
 39 done
 40 
 41 for i in /sys/bus/usb/devices/usb*/power/autosuspend
 42 do
 43     echo "1" > $i
 44 done
 45 
 46 for i in /sys/bus/usb/devices/*/power/autosuspend
 47 do
 48     echo "1" > $i
 49 done
 50 
 51 webcamDev=$( lsusb | grep -i webcam | awk '{print $2 "-" $4 }' | tr -d ':0' )
 52 if [ ! -z "$webcamDev" ]
 53 then
 54     echo 1 > /sys/bus/usb/devices/$webcamDev/power/autosuspend
 55     echo auto > /sys/bus/usb/devices/$webcamDev/power/level
 56     echo 1000 > /sys/bus/usb/devices/$webcamDev/power/autosuspend_delay_ms
 57 fi
 58 
 59 # eof

The best way to run this script is to open two windows, one running powertop, the other for this script. After adjusting this script, type r in the powertop window to refresh the device stats. I was able to take my T60 from 30.1W down to somewhere between 19.8 – 16.1W. Unfortunately, I haven’t seen that 16.1W reading since.

I hope this serves as a good starting point for your own laptop power savings! And not just laptops, but this script can also be used for home NAS or media center or other small servers where you need thermal management, too.

%d bloggers like this: