Saturday, April 30, 2016

How to get a longer battery life on linux

Main problem of laptops is battery life on Linux. It’s mainly becouse laptop manufacturers didn’t provide linux drivers a power save features. You can almost always experience that Windows operating system will last longer on battery. Sometimes it’s a few minutes, but sometimes it’s few hours. If you are not satisfied with your battery endurance, feel free to take some ideas from this article. It’s written mainly for Arch Linux, but with small changes it can be applied on all modern linux distributions like Ubuntu, Debian, Fedora etc.



1. Powertop

This little app is grate for monitoring your power consumption. When on battery, it can calculate your current power consumption in Watts, so you can monitor it in real-time.

It has it’s own power saving features, which is recommend to enable all. You can experiment with enabling and disablign them and watch your power consumption. It’s known that on few systems, enabling PCI power-save feature can lead to bigger consumption, so watch it carefully.

It’s good to leave it for at least 10 minutes, to collect data about your battery discharge rate.

2. Display settings

One of the most power hungry part of your system is display. It’s good to run it at lowest possible brightness which is comfortable for you. The realtionship between brightness and power consumption is not linear. Good value is somewhere under 2/3 of your maximum brightness. Also on LCD screens is good to use bright colors and backgrounds, becouse it uses more power to display black color. On OLED displays is perfect black color, becouse it will shut down segments, which are used to display black. Set your screen blanking to some low value, eg. 2 minutes of inactivity.

3. Intel i915 power saving features

With this driver, you can make a few changes, which can grately increase battery life. Intel have written a piece of nice battery save features, but they are mostly disabled, becouse they are not 100% compatible with all laptops. You can try all this tricks and experiment with them. If something goes wrong, you can allways revert back to default setting. We’ll be modifying GRUB kernel parameters, so you can change them during early boot in GRUB.

3.1 Intel RC6

This feature enables deep, low power state of Intel graphics. It can be applied to Sandy bridge and newer CPUs. If you wish to enable this feature, edit:
/etc/default/grub
find line:
GRUB_CMDLINE_LINUX_DEFAULT="...”
and add i915.i915_enable_rc6=1 to the end so it’ll looks like
GRUB_CMDLINE_LINUX_DEFAULT="… i915.i915_enable_rc6=1”

Save file and regenerate your GRUB file

$ sudo grub-mkconfig -o /boot/grub/grub.cfg

3.2 Framebuffer compression

This option can show some artifacts on non-compatible hardware, but it’s worth of trying. It can save 0.5-1W of energy. It’ll enable compression of framebuffer, so less bandwidth is used. To enable this option, edit
/etc/default/grub
and modify line
GRUB_CMDLINE_LINUX_DEFAULT="...”
to
GRUB_CMDLINE_LINUX_DEFAULT="… i915.i915_enable_fbc=1”

Save file and regenerate your GRUB file

$ sudo grub-mkconfig -o /boot/grub/grub.cfg

3.3 LVDS Downclocking

If you have unsupported hardware, your screen can flicker with this parameter enabled. Basically it’ll downclock refresh rate of LVDS. It’s not a big energy saver, but every piece counts. To enable this option, edit
/etc/default/grub
and modify line
GRUB_CMDLINE_LINUX_DEFAULT="...”
to
GRUB_CMDLINE_LINUX_DEFAULT="… i915.lvds_downclock=1”

Save file and regenerate your GRUB file

$ sudo grub-mkconfig -o /boot/grub/grub.cfg

4. Other devices power save features

Disable all devices which are not used. Disabling bluetooth, WIFI, ethernet, webcam can leads to big power saving. If you don’t have hardware switch for this devices, look in your BIOS settings, or unload all modules for that devices.
For webcam use

$ sudo rmmod uvcvideo
For bluetooth use
$ sudo rmmod bluetooth
If you want to look at your modules, run this command
$ lsmod
and investigate, what can be unloaded.

4.1 USB autosuspend

Very good feature is to autosuspend unused USB ports. To do this, edit file
/etc/udev/rules.d/50-usb_power_save.rules
and add to it
ACTION=="add", SUBSYSTEM=="usb", TEST=="power/control", ATTR{power/control}="auto"
You can specify, how long it will take to suspend your devices in file
/etc/modprobe.d/usb-autosuspend.conf
This option will set how many seconds it will wait for inactivity
options usbcore autosuspend=5

4.2 SATA Active Link Power Management

If your laptop has SATA ports, you can enable it’s power management.
Warning: On some laptops it can leads to data loss.
Edit file

/etc/udev/rules.d/hd_power_save.rules
and add to it
ACTION=="add", SUBSYSTEM=="scsi_host", KERNEL=="host*", ATTR{link_power_management_policy}="min_power".
This setting will add latency when accessing your SATA devices, so it’s good to enable it only on battery.

4.3 HDD

Mechanical hard drives uses a lots of power. You can save power here, but it can shorten lifetime of your HDD. So consider a appropriate values, depending on how often are you gonna access your data on drive. It’s not good to set spindown to low value, if you have a lot’s of disk activity. It’ll be spinning down and up all the time.
First setting is Advanced Power Managment. Current value can be shown with

# hdparm -B /dev/sda
To set a new value (for example 127), write
# hdparm -B 127 /dev/sda
Values from 1 to 127 permit spin-down, whereas values from 128 to 254 do not. A value of 255 completely disables the feature.
Second you can set standby timeout for your drive with -S parameter. The value of 0 disables spindown, the values from 1 to 240 specify multiples of 5 seconds and values from 241 to 251 specify multiples of 30 minutes. For example

# hdparm -S 20 /dev/sda

If you want to save a lot of power, change your HDD for SSD. It has no mechanical parts and it consumes a lot less energy than HDD. Or at least, use SSD for your system and HDD for data.

4.4. nVidia Optimus dual graphics

The biggest power consumer is your dedicated graphic card. If you have nVidia Optimus enabled graphic card, you can disable it, when you are not using it. If you are not using nVidia graphic card at all, try to disabling it in BIOS settings. If you want to still use advantages of more powerfull card, simply install Bumblebee and Bbswitch module. That module will automatically disable dedicated graphic card when not used. On Arch linux you can install it with
$ sudo pacman -S bumblebee bbswitch
After this, if you want to run some application with dedicated graphic card, simply put optirun command before your application, eg.
$ optirun glxgears

4.5 Wake-on-LAN

This function is used to wake up your computer through network. If you are not using it, disable it becouse it allways waits for magic packet. To do this, create file
/etc/udev/rules.d/70-disable_wol.rules
and add to it
ACTION=="add", SUBSYSTEM=="net", KERNEL=="eth*", RUN+="/usr/bin/ethtool -s %k wol d"
Also install ethtool to enable this.

4.6 Wifi powersave

To enable powersave on all wireless devices, create file
/etc/udev/rules.d/70-wifi-powersave.rules
and add to it
ACTION=="add", SUBSYSTEM=="net", KERNEL=="wlan*", RUN+="/usr/bin/iw dev %k set power_save on"

4.7 Audio powersave

You can get a bit of energy by enabling powersave on your sound card. To do this, create file
/etc/modprobe.d/audio_powersave.conf
and add to it
options snd_hda_intel power_save=1

4.8 Disable NMI watchdog

If you are not going to debug kernel, disable this by editing file
/etc/sysctl.d/disable_watchdog.conf
add to it
kernel.nmi_watchdog = 0

4.9 Writeback time

This will aggregate disk I/O, so less writes will be performed. Default value for writeback is 5 seconds. Edit file
/etc/sysctl.d/dirty.conf
and set your writeback to 60 seconds
vm.dirty_writeback_centisecs = 6000

4.10 Set laptop mode

According to Kernel documentation "A sensible value for the knob is 5 seconds.".
Edit file

/etc/sysctl.d/laptop.conf
and add to it
vm.laptop_mode = 5

5. Custom powersave features only on battery

If you have your custom scripts which are intend to turn on this features, you can set udev rule to run it automatically only on battery. To do this, create file
/etc/udev/rules.d/powersave.rules
and add lines to run your custom script
SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/path/to/your/script true"
SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/path/to/your/script false"


I hope this article will be useful for someone. I’m using this tweaks on all my laptops. I get about 2 hours of more battery energy, so it’s really usefull. If I’ll find some more tweaks, I’ll add them or if you have your own tweaks, feel free to leave them in comments section.

No comments:

Post a Comment