Powersaving on a Thinkpad t480s running (Arch) Linux
Tags: linux, powerusage, battery, mobile
Published: 2020-07-07 20:46:14

Recently I tuned my t480s to enable more power-saving features and settings. I used the following tools:


1. [powertop](http://manpages.ubuntu.com/manpages/bionic/man8/x86_energy_perf_policy.8.html)
2. [intel-undervolt](https://github.com/kitsunyan/intel-undervolt)
3. [cpupower](https://linux.die.net/man/1/cpupower)
4. [x86_energy_perf_policy](http://manpages.ubuntu.com/manpages/bionic/man8/x86_energy_perf_policy.8.html)


## Powertop

Powertop is used to auto-tune several power-saving settings. Creating a systemd-service at `/etc/systemd/system/powertop.service` allows us to automatically run the auto-tune command on the system startup:
```
[Unit]
Description=Powertop tunings

[Service]
Type=oneshot
ExecStart=/usr/bin/powertop --auto-tune

[Install]
WantedBy=multi-user.target
``` 
Don't forget to enable and run the service via:
```
sudo systemctl enable powertop.service --now
```



## Intel-Undervolt

Intel-undervolt can be used to reduce the voltage used by several hardware components on intel based systems. The configuration is located at `/etc/intel-undervolt.conf`. I edited the file to include the following settings. 

Warning, **YOU MUST NOT JUST COPY THOSE!** Using values not suitable for your system can lead to voiding your warranty, or making your OS unstable. This can really f*ck up your system. On my Lenovo t480s with an i5-8250u everything works as expected. 

```
undervolt 0 'CPU' -125
undervolt 1 'GPU' -80
undervolt 2 'CPU Cache' -100
undervolt 3 'System Agent' -80
undervolt 4 'Analog I/O' 0
``` 
Again, don't forget to enable the corresponding systemd service:
```
sudo systemctl enable intel-undervolt --now
```



## cpupower and x86_energy_perf_policy

Utilizing powertop and intel-undervolt as described is a one-time, zero interaction configuration. Additionally, I wanted to have the possibility to modify the power consumption of my Thinkpad manually. 
I created a small bash program called `power.sh` to change some CPU related settings, based on a supplied command-line argument:
```
#!/bin/bash

setPowerSave() {
  sudo x86_energy_perf_policy --all 'balance-power'
  sudo cpupower frequency-set --max 1600MHz
}

setPerformance() {
  sudo x86_energy_perf_policy --all 'balance-performance'
  sudo cpupower frequency-set --max 3400MHz
}

setSilent() {
  sudo x86_energy_perf_policy --all 'balance-power'
  sudo cpupower frequency-set --max 2400MHz
}

case $1 in

  bat)
    setPowerSave
    ;;

  sil)
    setSilent
    ;;

  ac)
    setPerformance
    ;;

  *)
    echo "The argument $1 is not supported"
    ;;
esac
```
The script can process three different command-line arguments:

1. Argument value: `bat`  
Reduces the max frequency to 1600MHz and sets the performance hint to balance-power.
Useful when the battery is key.

2. Argument value: `sil`  
Reduces the max frequency to 2400MHz and sets the performance hint to balance_performance. Keeps my system mostly silent, as the fan usually never turns on.

3. Argument value: `ac`  
Sets the max frequency to 3400MHz (the design maximum on my i5) and sets the performance hint to balance_performance. Useful when developing or running anything more performance intensive.


For convenience, I add an alias to my `.bashrc` to be able to call my script via the shorthand `pwr` 
```
alias pwr="sh ~./power.sh"
```
Now I can execute `pwr bat` to force my system into a custom power-save mode or `pwr ac` when I want more performance.



## Testing the changes

I used two commands to test my changes. 

Firstly, to test the power in watt being drawn from the battery when the device is off AC reading `power_now` can be used:
``` 
cat /sys/class/power_supply/BAT0/power_now
```
Alternatively, [btm](https://github.com/ClementTsang/bottom) can automatically read the file for you when called with the `--battery` option like this:
```
btm -g --hide_time -c --battery
```

Secondly, intel-undervolt brings its own tool to monitor the power usage of the SOC, temperatures, and frequencies of the different cores:
```
intel-undervolt measure
```


Btw. I do not use TLP, as I had repeated problems with it, higher power usage, hot system especially when on AC, and no turbo mode are just some of them. And yes, I checked my config, reinstalled, and so on. As I am really happy with my current solution, I do not regret installing TLP.

Happy coding!


Update 01.08.2020:
Removed the part that undervolting could damage hardware. This is not correct as pointed out by Lukáš Hozda in the comments. Thanks!