Monday, March 24, 2014

[Tutorial] Energy saving in Linux


In this post I will show you some tips that I use in order to reduce my power consumption in Linux.

Bumblebee & Bbswitch (if you have an Optimus GPU)


First thing first, if you have a NVIDIA graphics card with Optimus technology you should install bumblebee and bbswitch.

This piece of software will actually turn off the dedicated video card when it is not in use. And turn it on when you want to use it.

By default, when you install Linux in you machine the card will always be on. So after installing Bumblebee you will see a real improvement in battery life (almost a 50% increase).

bumblebee is the daemon and bbswitch is the kernel module that actually turns the card on and off. Make sure you take a look at the Github page of the bbswitch project if you are interested to know how this actually works.

To install the packages:

Since Ubuntu 13.10, bumblebee is in the official packages (dependencies should pull bbswitch and the NVIDIA driver too):

$ sudo apt-get install bumblebee

If you are in Arch you install the packages manually:

# pacman -S bumblebee bbswitch nvidia

If you have intel-dri and xf86-video-intel installed, you will have to reinstall them together with the rest to avoid a dependency conflict between intel-dri and nvidia.

You can also choose if you want to use the binary blob made by NVIDIA or the Nouveau driver (xf86-video-nouveau) that is Open Source. It can be interesting to install bbswitch-dkms instead of bbswitch in Arch so the kernel module will survive kernel updates.

If you want to test if the card is actually off you can run the command:

# cat /proc/acpi/bbswitch


If you are in Arch make sure you check the Arch Wiki page about Bumbeblee, you will need to do some other configuration in order to use bumblebee correctly.

I made a post some months ago showing how to run some tests using the dedicated graphic card and how to configure the "bridges" used by Bumblebee. Make sure you check it here.

Intel Graphics (playing with Kernel Mode Settings)


If you don't have a dedicated graphic card or if you want to tweak a little more in your system you can do some things about intel graphics.

First of all you can load the i915 module manually using the configuration file in /etc/mkinitcpio.conf:

# nano /etc/mkinitcpio.conf

There is a line that contains the word MODULES, add the module i915. Actually I'm not really sure that this is really necessary, this will initialize the module during bootstrap and not after that is the normal behavior of the kernel.

MODULES="i915"

After you did that you need to regenerate the initramfs:

# mkinitcpio -p linux

You can gain some energy editing (or creating) the file /etc/modprobe.d/i915.conf and adding the line:

options i915 i915_enable_rc6=7 i915_enable_fbc=1 lvds_downclock=1

PowerTOP (analyzing power usage of your machine)


PowerTop is an interesting tool that shows you what is the most consuming program or process of your computer.

You can install it from the official packages of your distribution:

$ sudo apt-get install powertop

# pacman -S powertop

You will need root privileges to run powertop

# powertop

Without arguments the application will show a top-like interface showing applications and services. You can switch tabs and check some stats like Idle, Device and Frequency.
The PowerTOP interface

There are some interesting features in PowerTOP. You can make a html file that will be like a report of your power consumption and what is interesting is that it will show you the preferences that you should if some configuration files.

But before you do that, PowerTOP needs to collect data about your machine. So you can calibrate this tool using

# powertop --calibrate

Make sure you are using your laptop's battery and leave it for a while. The program will change your backlight and preferences. And when the interface of powertop will appear again you can exit it. The calibration should be done by then.

After you have done that you can generate the report with:

# powertop --html=report.html

Then you can check it with your favorite browser.
Example of the HTML file created by PowerTOP
The first time you run PowerTOP it is very likely that you will see that PulseAudio is draining a lot of power.

You can create a config file to solve this

$ sudo nano /etc/modprobe.d/audio_powersave.conf

Insert the line

options snd_hda_intel power_save=1

If you are using Intel, or

options snd_ac97_codec power_save=1

For ac97.

PowerTOP has the ability to tune your preferences in order to save power. You just need to run:

# powertop --auto-tune

You can even create a simple SystemD service (if you are using SystemD in your distribution)

$ sudo nano /etc/systemd/system/powertop.service

[Unit]
Description=Powertop tunings
[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/usr/bin/powertop --auto-tune
Environment="TERM=xterm"

[Install]
WantedBy=multi-user.target

After you created the service just enable it:

# systemctl enable powertop.service

TLP


If you don't run SystemD or if your distribution doesn't have the latest PowerTOP version with the "Auto tune" feature you can use TLP. I did a post about how to install TLP in Ubuntu.

So what does TLP does? It is a power management tool that will change the default option of your system depending in the power source of your computer. For example it will change the CPU governor when you are using your battery.

When you install it you will have a default configuration file, that is pretty good and will work. But you can edit the configuration as you want, the problem is that you actually need to know what you want. Make sure you check the page explaining the different settings that you can use.

If you want to install TLP, in Arch:

# pacman -S tlp tlp-rdw 

If you are using a Thinkpad (IBM/Lenovo) you will need this extra packages

# pacman -S tp_smapi acpi_call

Then you need to start the SystemD service

# systemctl enable tlp
# systemctl enable tlp-sleep

Then if you want to use tlp-rdw, the radio device wizard you need to enable the Network Manager dispatcher

# systemctl enable NetworkManager-dispatcher

If you are using Ubuntu:

$ sudo add-apt-repository ppa:linrunner/tlp
$ sudo apt-get update 


Then install the packages that you need

$ sudo apt-get install tlp tlp-rdw

(if you are using a Thinkpad)

$ sudo apt-get install tp-smapi-dkms acpi-call-tools 

Here you can check how to install it in other distributions.

If you want to check all the parameters used in the configuration files you can use TLP (needs root privileges)

# tlp stat

It will make an enormous log with all the information that you may want. Maybe you can redirect the output to a file:

# tlp stat > tlpoutput.data

Or you can grep the output to find an specific line, like is TLP actually in power saving mode right now?

# tlp stat | grep "TLP power save"
The first lines of the TLP stat output

Did you like this post? Don't forget to add me to your circles in G+ or to follow me on Twitter: @RobertoXMed

Please consider donating to the Blog it will be of great help! Thank you.

No comments:

Post a Comment