How to optimize laptop battery life with TLP on Linux

When using Linux on mobile devices such as Laptops, it is very important to tune the right kernel parameters in order to optimize battery life. Tlp is a highly customizable, free and open source command line utility released under the GPLv2 license (the source code is hosted on github) created with this exact goal. In this tutorial we see how to install Tlp on some of the most used Linux distributions, and how to configure it.

In this tutorial you will learn:

  • How to install Tlp
  • How to configure Tlp
  • How to check power saving status using tlp-stat
Introduction to Tlp

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software tlp, tlp-rdw (for radio device wizard)
Other Root privileges to perform administrative tasks
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Installing Tlp

Tlp is available in the repositories of the most used Linux distributions, therefore all we need to do to install it, is to use our favorite package manager. The base package we need to install is called tlp; additionally, if we intend to use the radio device wizard to enable or disable radio interfaces when certain events occur, we need to install the tlp-rdw package.



Fedora is the community distribution of the Red Hat family, and uses the dnf package manager to install or remove software packages. To install Tlp, all we need to do is to launch the following command:

$ sudo dnf install tlp tlp-rdw

Debian is one of the most widespread and stable Linux distributions, and represents the base for many derivatives such as Ubuntu and Linux Mint. The “modern” way to install software on this distribution is to use the apt wrapper, which provides a friendly user interface over  apt-get and other commands:

$ sudo apt install tlp tlp-rdw

Tlp is available also in the Archlinux “community” repository. To install the software on Arch, we use the pacman package manager:

$ sudo pacman -Sy tlp tlp-rdw

Once Tlp is installed on our system, we need to start the related service, and enable it at boot (on Fedora and Debian the service should be automatically started as part of the installation). Nowadays almost all distributions use Systemd as init manager, therefore to perform such tasks we can use the systemctl command:

$ sudo systemctl enable --now tlp.service

Configuring Tlp

To configure Tlp and adjust it to better suite our needs, we have basically two options:

  1. Edit the /etc/tlp.conf file
  2. Drop configuration files in the /etc/tlp.d directory

What approach should we adopt? That depends. If we open the main Tlp configuration file, /etc/tlp.conf, we can see how all its content is commented. For each option available a brief description is provided, including its default value and often the suggested one. To change the value of an option we need to remove the comment from the appropriate line and assign the desired value to the corresponding variable. This approach is easier to adopt if we are modifying settings manually.



As an alternative, we can create one or more settings files containing the options we want to use and their values, and drop them inside the /etc/tlp.d directory. Each one of those files should have the “.conf” extension in its name. This approach can be more convenient, for example, if we are deploying tlp as part of an automated installation performed with Ansible or Kickstart.

As we already said, the parameters set by Tlp are very well described inside the main configuration file, In the course of this tutorial, however, we will further investigate some of them, and see what Kernel subsystems they operate on. Let’s go.

Laptop mode

One of the first options we meet inside the main Tlp configuration file are DISK_IDLE_SECS and MAX_LOST_WORK_SECS. A value can be assigned to them depending if running on power adapter (ON_AC) or on battery (ON_BAT). Those options, under the hood, make use of one feature of the Linux kernel which is called “laptop mode“. When laptop mode is active, what changes is basically how Linux handles “dirty data“: the data in memory that needs to be written to disk.

The value we assign to DISK_IDLE_SECS_ON_AC or DISK_IDLE_SECS_ON_BAT are written to the /sys/proc/vm/laptop_mode file. A value greater than 0 enables laptop mode, and represents the number of seconds after the disk goes idle, before attempting to perform a data sync. The default value used by tlp, when power adapter is plugged in, is 0; on battery power, 2 is used instead.

The other option which modifies the behavior of laptop mode is MAX_LOST_WORK_SECS. The value for this option in the Tlp settings file is expressed in seconds; under the hood, the value is converted in centiseconds (multiplied by 100) and written to the following system files:

  • /proc/sys/vm/dirty_writeback_centisecs
  • /proc/sys/vm/dirty_expire_centisecs
  • /proc/sys/fs/xfs/age_buffer_centisecs
  • /proc/sys/fs/xfs/xfssyncd_centisecs

Each one of those files has a different role. The kernel periodically uses “flusher threads” to write “old” data to disk: the value written in the /proc/sys/vm/dirty_writeback_centisecs file, expresses the interval between these flushes should run.  What data has to be considered “old”? This is established by the value written to the /proc/sys/vm/dirty_expire_centisecs file: data which has been in memory for an interval of time longer than it, will be considered “old”.

The /proc/sys/fs/xfs/age_buffer_centisecs and /proc/sys/fs/xfs/xfssyncd_centisecs files, as it can be inferred by their names, influence the behavior of the xfs filesystem. The value inside the former represents the interval of time after which the dirty metadata buffers list is scanned, and the value inside the latter, establish the interval of time after which metadata is written to disk.

Setting the scaling Governor

Tlp can be used to set a Cpu scaling governor depending on the power source of a machine. A scaling governor sets and change the frequency of a CPU depending on certain factors. How the frequency is changed depends on the governor in use. The  available cpu governors can be retrieved by querying the /sys/devices/system/cpu/<cpun>/cpufreq/scaling_available_governors file, where <cpun> must be substituted by the cpu number, for example:

$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

The above command should return something like:

conservative ondemand userspace powersave performance schedutil

To set the scaling governor with Tlp, we must use the name of the governor as the value of the CPU_SCALING_GOVERNOR_ON_AC and CPU_SCALING_GOVERNOR_ON_BAT options. Under the hood, Tlp will write the desired choice into the /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor files.

Tlp allows also the user to easily fine tune also the maximum and minium frequencies for the selected scaling governor via the CPU_SCALING_MIN_FREQ_ON_AC, CPU_SCALING_MAX_FREQ_ON_AC, CPU_SCALING_MIN_FREQ_ON_BAT and
CPU_SCALING_MAX_FREQ_ON_BAT options. The provided values will be written in the /sys/devices/system/cpu*/cpufreq/scaling_max_freq and /sys/devices/system/cpu*/cpufreq/scaling_min_freq files.

Using the Intel HDA powersave

Another useful strategy to lower power consumption when running on battery, is to enable power saving for Intel HDA (High Definition Audio) devices. Tlp allows us to enable this strategy via the SOUND_POWER_SAVE_ON_AC and SOUND_POWER_SAVE_ON_BATvariables. A value of 0 disables the feature, while a bigger value enables it, and represents the number of seconds of inactivity after which the device should be put in power saving mode. Tlp writes the passed value to the /sys/module/snd_hda_intel/parameters/power_save and /sys/module/snd_ac97_codec/parameters/power_save files. This feature is enabled by default on both battery and ac.



The audio controller is also disabled when in power saving mode. This is controlled via the SOUND_POWER_SAVE_CONTROLLER option, which can be set to 'Y‘ or ‘N‘, to respectively enable and disable the feature. The system file which controls this parameter is /sys/module/snd_hda_intel/parameters/power_save_controller.

Setting battery thresholds

On certain machines, for examples on Thinkpads, the system firmware allows to set battery charging thresholds. This feature is really useful to preserve battery life. Modern lithium batteries potentially lost capacity when completely discharged or continuously kept at their maximum power level. By setting charging thresholds we can adjust charging behavior to our needs.

If the machine is seldom used on battery, for example, we can set the lower and upper thresholds to 40 and 50 respectively, in order to avoid complete battery cycles and overheating. If we often use the machine on battery, instead, we can set lower threshold to 85 and maximum to 95, for example (values are suggested on the lenovo page). The system will start charging the battery only if the charge percentage is below the minimum threshold, and will stop when the upper threshold is reached. Tlp controls this feature via the START_CHARGE_THRESH_BAT0 and STOP_CHARGE_THRESH_BAT0 variables. If our system has more than one battery we can add a dedicated setting by adding the same settings for BAT1, etc. The passed values are written to the /sys/class/power_supply/BAT0/charge_control_start_threshold and /sys/class/power_supply/BAT0/charge_control_end_threshold files.

Disabling radio interfaces on specific events

By using Tlp, we have the chance to enable or disable specific radio interfaces when certain events occur; for example, we can disconnect the wifi card(s) when the system is connected to the LAN via ethernet. Tlp controls this via the following options:

  • DEVICES_TO_DISABLE_ON_LAN_CONNECT
  • DEVICES_TO_DISABLE_ON_WIFI_CONNECT
  • DEVICES_TO_DISABLE_ON_WWAN_CONNECT
  • DEVICES_TO_ENABLE_ON_LAN_DISCONNECT
  • DEVICES_TO_ENABLE_ON_WIFI_DISCONNECT
  • DEVICES_TO_ENABLE_ON_WWAN_DISCONNECT

The names of the variables are pretty self-explanatory. They accept a comma-delimited, space separated list of device types as their value. For example, to disable all wifi devices when the system is connected to LAN with a cable, we would write:

DEVICES_TO_DISABLE_ON_LAN_CONNECT="wifi"

Tlp allows us also to disable devices on other events, like power adapter or dock connection or disconnection.

Checking power saving status using tlp-stat

To check the current power-saving status, we can use an utility which comes with tlp: tlp-status. This utility let us view various information such as the value of tlp settings, kernel configurations and battery data. When invoked without any option, tlp-stat prints all available information:

$ sudo tlp-stat

Here is an excerpt of the returned data (output truncated):

--- TLP 1.4.0 --------------------------------------------

+++ Configured Settings:
defaults.conf L0004: TLP_ENABLE="1"
defaults.conf L0005: TLP_WARN_LEVEL="3"
defaults.conf L0006: TLP_PERSISTENT_DEFAULT="0"
defaults.conf L0007: DISK_IDLE_SECS_ON_AC="0"
defaults.conf L0008: DISK_IDLE_SECS_ON_BAT="2"
defaults.conf L0009: MAX_LOST_WORK_SECS_ON_AC="15"
[...]
+++ System Info
System = ASUSTeK Computer Inc. 1.0 K52F
BIOS = K52F.218
OS Release = Fedora Linux 35 (Thirty Five)
Kernel = 5.15.18-200.fc35.x86_64 #1 SMP Sat Jan 29 13:54:17 UTC 2022 x86_64
[...]
+++ TLP Status
State = enabled
RDW state = not installed
Last run = 09:22:18 AM, 23834 sec(s) ago
Mode = AC
Power source = AC
[...]

Returned information can be filtered by using the appropriate options. To show only battery data, for example we would run:

$ sudo tlp-stat --battery

In the same way, to make so that only information about USB devices are displayed, we can use the --usb option. For the complete set of options which can be used, please take a look at the manpage of the utility.

Conclusions

In this tutorial we saw how to install and configure Tlp on some of the most used Linux distributions, and how we can configure it to optimize battery life on Linux. We saw what is the main configuration file, some of the directives contained in it, and how the passed settings are written under to system files, under the hood. Tlp can be used to perform a vast range of other power saving tweaks: the best way to know them is to take a look at the configuration file and see how they are implemented.



Comments and Discussions
Linux Forum