An extensive guide to optimizing a Linux laptop for battery life and performance
Optimization is fun. When it comes to laptops, one of those things you can never have too much of is battery life. Although modern Linux distributions are doing a great job improving on this aspect, using some well known and some less known tools, battery life and performance can be greatly improved. This guide will go over some tools to tweak a system for optimal results.
Although it is unlikely that any harm will be done, do note that some tools require changing configuration files, and are more involved than others. I provide absolutely no warranty that the presented steps will work for everyone and will not damage the system or its components.
This guide is recommended to anyone who has some familiarity with Linux based systems and is looking to maximize their utility.
Basics of battery life and power consumption
One of the main sources for power consumption of any computer is the CPU. The speed at which a CPU processes instructions depends on two main factors. IPC, or instructions per cycle, determines how efficient the CPU is in executing commands. All things similar, a CPU with a higher IPC, will outperform a CPU with a lower IPC. For example, if one CPU executes 2 instructions per cycle, while the other executes 4, it will take the second one fewer cycles to finish the same work.
The IPC is usually determined by the CPU architecture and workload. There is little we can do to improve it besides buying a newer generation CPU. (Of course, there are many factors at play here, architecture, instruction set etc.)
The second important factor is the clock speed of frequency. This is usually measured in GHz (Gigahertz) and is a measurement of how many cycles a CPU can perform per second. Given the same IPC, a CPU with a higher frequency will outperform a CPU with a lower frequency. If both perform 4 instructions per cycle, the one working at 4 cycles per second, will be twice as fast as the one working at 2 cycles per second.
Multiplying IPC and clock speed is a rough estimation of how fast a CPU works:
Perf = IPC·f
Since we cannot (easily) improve the IPC to get better performance, controlling the frequency is our main way to improve the performance of a given system.
However, increasing clock speed comes with a price, the higher the frequency, the higher the power consumption.
The equation for CPU power consumption is roughly Pow =CV²f (source). Where C is a constant (capacitance) V is the voltage provided for the CPU and f is the current frequency.
As we increase the frequency, power consumption will increase. However, power consumption does not grow linearly with the frequency as it might seem from the equation. The CPU can only sustain high clock speeds if it is provided with enough voltage. Thus the power management system of the CPU will also increase the voltage as we increase clocks.
The result is that at some point, any increase in frequency becomes a cubic increase in power consumption (Pow ~ f³).
This is what we need to keep in mind while optimizing battery life and performance. We want to maximize frequency when power is abundant (when connected to a power outlet), and minimize power consumption when running off the battery.
Choosing the CPU management driver
CPU power management in Linux is handled partially by the operating system itself. Although the CPU does have power management tools built-in, the OS is given some control of the CPU to try and best suit its needs. Speeding up the clocks when there is a heavy workload to process, and slowing it down to a minimum when the system is idle and there is little to do.
There are several drivers available to manage the power, notably acpi and intel_pstate.
To check which one is currently in use, install the following tools (Ubuntu is used in all examples)
sudo apt install linux-tools-generic
sudo apt install linux-tools-common
Run the tool:
sudo cpupower frequency-info
The output should tell you which driver is being used, and looksomething like this:
> sudo cpupower frequency-info
analyzing CPU 0:
driver: intel_pstate
...
Next, to evaluate the maximum frequency the CPU can reach, we can use a tool such as s-tui (shameless plug). s-tui is a tool focused on displaying the CPU frequency, temperature, utilization, and power consumption. Everything we need to measure for these optimizations.
Running a stress test will create a heavy workload on the CPU and push it to its limit. Take a note of the highest frequency the CPU can reach. s-tui has a built-in stress test that will do the job, but any other tool will work.
When running with intel_pstate in this example, the CPU could only reach 2.7 GHz, which is the base clock of the CPU.
Most modern CPUs have a base block and a boost block. The base block is the guaranteed frequency the CPU can run at unless limited by some other factor. The boost clock is a higher clock the CPU can reach, usually for a short period. In general, boost block is only applied for short bursts, so that the CPU can accomplish a short task quickly (e.g. opening a browser tab), followed by a quick return to the idle state, with much lower clocks. Yet, if power delivery and cooling are under control, the CPU could potentially keep operating at the boost block indefinitely.
In this case, the CPU I’m using is the Intel-Core-I7–7500U. Its base block is 2.7 GHz, and a boost clock of 3.5 GHz. Looking at s-tui, we can see that the CPU never broke past 2.7 GHz, basically leaving performance on the table.
To mitigate this, one solution is to change the driver to acpi-cpufreq. To achieve this, start by installing acpi:
sudo apt-get install acpi-support acpid acpi
Next, to disable the intel_pstate driver. Open the grub configuration (usually /etc/default/grub).
Find the line that starts with GRUB_CMDLINE_LINUX_DEFAULT
Add the following value: intel_pstate=disable
So that the line looks like something like this:
GRUB_CMDLINE_LINUX_DEFAULT=”quiet nosplash debug intel_pstate=disable”
Save the file and exit. Finally, update grub:
sudo update-grub
Reboot for the change to take effect.
After the switch to acpi, running “sudo cpupower frequency-info” should result in something like this:
analyzing CPU 0:driver: acpi-cpufreq
CPUs which run at the same hardware frequency: 0
CPUs which need to have their frequency coordinated by software: 0
maximum transition latency: 10.0 us
hardware limits: 400 MHz — 2.70 GHz
available frequency steps: 2.70 GHz, 2.70 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.20 GHz, 2.00 GHz, 1.80 GHz, 1.60 GHz, 1.50 GHz, 1.30 GHz, 1.10 GHz, 800 MHz, 700 MHz, 600 MHz, 400 MHzavailable cpufreq governors: conservative ondemand userspace powersave performance schedutil
current policy: frequency should be within 400 MHz and 1.60 GHz.
The governor “ondemand” may decide which speed to use
within this range.current CPU frequency: 400 MHz (asserted by call to hardware)boost state support:Supported: yesActive: yes
Running s-tui and the stress test again shows a better picture.
The CPU frequency is now 3.5 GHz as expected, and is sustained for a long time.
This has a significant impact on performance when running benchmarks. Running Geekbench 5 gives the following results before and after the change:
Before: Single core: 738; Multi core: 1567
After: Single core:961; Multi core: 2022
That’s about 1.3x improvement. Exactly as we would expect from scaling the frequency 1.3 times from 2.7 GHz to 3.5.
Making these simple modifications gives us 30% more performance from the same hardware. Either acpi or intel_pstate can work better for a particular setup. Things to look for are whether the system reaches its full boost clock when under heavy load and scales the frequency all the way down when idling. While the system is connected to the wall, there is no reason to not utilize its full potential. Next, let’s deal with battery life.
Taming the horses when on battery power
When the system is connected to a power supply, we want maximum performance. This is not necessarily true when on the go. Usually, we would like the laptop to last as long as possible on battery. One of the best tools to achieve this on Linux is TLP. TLP has been around for a long time, and it is the go-to tool to improve battery life on Linux laptops.
It does a great job out of the box so if to achieve instant improvements. Simply install it from the distribution repository.
sudo apt install tlp
TLP controls various aspects of the system. It can shut down hard drives, turn off Bluetooth, control CPU frequency settings, and more. Most of the default configurations are great and require no modifications. Still, some of the configurations of TLP are worth mentioning. (The configuration file is usually located in /etc/default/tlp.conf or /etc/tlp.conf)
Boost clock
CPU_BOOST_ON_AC=1
CPU_BOOST_ON_BAT=0
TLP gives manual control over enabling or disabling Turbo Boost on battery vs on AC power. The tool automatically detects when the laptop is connected to power and will switch between them.
Looking back at the equation for power consumption (Pow~f³) and the equation for performance (Perf = IPC·f), the faster we let the CPU run, the more we are going to pay in power consumption for each additional clock.
There are diminishing returns. The extra performance comes at a higher cost the more we push it. As an example, the difference between 2.7 GHz to 3.5 GHz might give us a 30% improvement in performance but cost 50% more power.
There is a trade-off here. One the one hand, reducing the clock speed will result in less power consumption. On the other hand, the faster the CPU finishes the task the faster it will be able to return to its idle state. This is something to experiment with and can vary a lot depending on a specific workload. Still, the base clock is usually high enough to manage most tasks, and forfeiting the boost will make a big difference to battery life.
Max frequency
Following the same logic, TLP can be used to restrict clocks on battery power even further by modifying the following values in the config file.
#CPU_SCALING_MIN_FREQ_ON_AC=0
#CPU_SCALING_MAX_FREQ_ON_AC=0
#CPU_SCALING_MIN_FREQ_ON_BAT=0
#CPU_SCALING_MAX_FREQ_ON_BAT=0
This can be used to set the MAX_FREQ value to be even lower than the base clock. Again, this requires experimenting with particular requirements. After all, if the system is slowed down to the point where it becomes non-responsive, there is little merit in all this work.
Undervolting
The more advanced trick to maximize battery saving is CPU undervolting. Undervolting is the process of setting a lower value for the voltage provided to the CPU. Looking back at the equation again, if we reduce the voltage, that would give us a quadratic improvement in power consumption. As long as we can keep the same clock speed, there will be no loss in performance. Undervolting is practically free additional battery life without cost.
There is no immediate risk caused by undervolting. Unlike overclocking or overvolting, there is no risk to “fry” the CPU. The greatest risk of undervolting is for the system to become unstable. Lowering the values by too much might cause some functionality to fail, causing the system to freeze or panic. Thus even when undervolting, the proper approach is to gradually decrease the values, making sure the system remains stable. The ability of each CPU to undervolt may and will vary from model to model and even between two identical CPUs, so mileage may vary.
For Intel processors from the 4th to the 10th generations, undervolting is usually possible and can save both heat and battery life. Due to some security vulnerabilities resulting in that process, Intel might be disabling this feature going forward.
There are several available tools for undervolting on Linux, one of them is the fantastic tool throttled.
The tool was originally developed to fix an issue with many Lenovo Thinkpads, which would start throttling their CPUs way before they were reaching their power and thermal limits. Installing this tool solves this issue, but also allows for easy undervolting.
Throttled is very configurable, and the coolest feature about it is that it allows configuring different profiles for AC power and battery. Using this feature, we can set reasonable undervolting values when on battery, which coupled with limited CPU frequency should give significant improvements to power consumption. Similarly when on AC power, leaving the values at default will make sure the CPU gets the full voltage required by the Intel spec.
Installation and setup guides are found on the project Github. Although developed for Thinkpads, it was tested and confirmed to work on various Intel-based laptops. Read the README of the project for installation instructions specific to the required distribution.
After installation, edit the config file (/etc/lenovo_fix.conf) and set values for an undervolting profile for AC and battery. I used the following values with my CPU:
All voltage values are expressed in mV and *MUST* be negative (i.e. undervolt)!
[UNDERVOLT.BATTERY]
# CPU core voltage offset (mV)
CORE: -120
# Integrated GPU voltage offset (mV)
GPU: -95
# CPU cache voltage offset (mV)
CACHE: -120
# System Agent voltage offset (mV)
UNCORE: -85
# Analog I/O voltage offset (mV)
ANALOGIO: 0
The tool also provides a monitoring option to see the current applied values, determine the reason for the CPUs throttling (if any), and read the current CPU voltage.
Important to note that throttled does not play well with other tools that sometimes comes preinstalled on systems, thermald.
Thermald has different functionalities, its purpose is to take control of the CPU power management and lower the clocks when the CPU gets too hot. The problem with thermald is that it is often too conservative in terms of temperatures, reducing the performance of the CPU well before it reaches dangerous thresholds. Thus, it is recommended to disable this process to gain the most performance from a system.
Dealing with thermal throttling
Thermal throttling is the process of a CPU’s power management system slowing down the CPU to prevent it from literally cooking itself to death.
Modern CPUs can produce and handle quite a lot of heat, usually, temperatures of up to 100°C are Ok for a CPU (however not recommended for long periods). If the laptop’s cooling system cannot handle the heat, it will automatically reduce voltage and frequency until the CPU returns to normal operating temperatures.
To determine if a CPU thermal throttles, we can run a stress test with s-tui and monitor the temperatures the CPU reaches after a few minutes of heavy load. If temperatures start rising to above 90°C, frequency is likely to drop, lowering the temperature and power consumption with it.
Thus, while more power means more performance, it also requires more cooling. Hence, the additional benefits of undervolting are lower CPU temperatures! If the CPU consumes less power, without lowering the clocks, less heat will be produced, and less heat will need to be dissipated.
Throttled can help here as well. Specifying an undervolting profile with throttled for an AC profile, can help to lower the heat without hurting performance. Again, achievable values may vary depending on a system.
When on battery, the fans will have to spin a lot less to remove heat, saving some more power on fan operation.
The bottom line, as long as reasonable values are used, undervolting has positive effects only.
More tools to monitor power consumption
Intel powertop has a gauge to measure the power consumption of the entire system. While s-tui measures power consumption of the CPU, using special registers available on the CPU itself, powertop measures power consumption by assessing how much battery has been drained at a given time. This is perhaps not as accurate but covers power usage of the system as a whole, including the screen brightness, wifi card, etc.
Install it with
sudo apt install powertop
Here is a screenshot of powertop running on my laptop while writing the article. The screen is at ~30% brightness with ~10 Chromium tabs open. WiFi is on.
Combining both s-tui and powertop can give great insights into how efficient the system is running after applying modifications.
Conclusion
Using some minor configurations, and a few great tools can measurably improve the battery life of any Linux laptop. The open nature of Linux platforms and the tools developed for them, give great flexibility in optimizing a system to particular needs. I hope this overview gives a good idea of what tools are available and why they work.
Happy hacking.
Bonus
There are various additional techniques to increase battery life and improve performance. In an old laptop, replacing the thermal paste might be an easy way to improve heat dissipation and improve temperatures. The fans on the laptop will not have to work as hard to reduce temperatures, resulting in less heat and less battery on fan spin.
Other battery guzzlers are of course the screen, WiFi, and Bluetooth. Reducing screen brightness will have a dramatic effect on battery life. Similarly, not using Bluetooth or WiFi, turning them off will save some watts.
Add more tips and tricks in the comments.