banner image

Systemd service for PowerTop

PowerTop

PowerTop is a script which shows you whether the computer's CPU is spending time in low-power states.

Low power states are great for your battery life, and for the planet. But latency is also important, and some processes cause the CPU to "wake" out of low-power states very regularly.

PowerTop has a nice display which tells you the processes which are waking the CPU, so you can kill -9 them as needed.

Kernel tweaks

The kernel also wakes up the CPU. For example, it will wake up the CPU and the hard disk, so that it can free memory (ie: to do swapping, aka paging).

This is controllable using dirty-writeback-centisecs in /proc/. PowerTop suggests that /proc/sys/vm/dirty_writeback_centisecs is set to 1500 (15 seconds), and you can set this by hitting <enter> on the appropriate line.

This is one example, and PowerTop has a lot more suggestions. You can set them all by:

/usr/sbin/powertop --auto-tune

Job done... except that the values in /proc/ reset every time you restart your computer.

Time for some automation!

Systemd

Systemd is how modern linux boots up.

It's efficient and has a lot of features.

Lets run /usr/sbin/powertop --auto-tune as root, every time the computer boots up.

Doing something at boot

The man page for systemd.unit eventually tells you to put your scripts in /etc/systemd/system/

We want this to run as part of boot, which means it's WantedBy a fully booted system (multi-user.target). This matches the examples in the documentation.

WantedBy is one of many options to describe the dependency tree, as it tries to describe which things which happen in parallel and which have strict ordering. A useful overview of the end result is:

sudo systemctl list-dependencies

A systemd service

We want to run a command, so we need a service. Other types of unit are a target (roughly equivalent to a SystemV runlevel) and a socket (you can create a socket first, and then start the server and client in parallel)

The different types of service are listed in the systemd.service man page. We want to run a script and exit, which is oneshot.

Most services would want to think about the security-related options given in systemd.exec, which include the ability to protect /home/ and /proc/ from a service running as root.

But we need to be powerful here - we write to /proc/.

A Systemd script for PowerTop

It ends up being a simple script. In /etc/systemd/system/powertop-h2g2bob.service:

[Unit]
Description=Run powertop auto-tune

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

[Install]
WantedBy=multi-user.target

The script should probably have permissions u=rw,go=r.

You will need to get Systemd to reload its config files:

sudo systemctl daemon-reload

make sure it runs at all:

sudo systemctl start powertop-h2g2bob.service
sudo systemctl status powertop-h2g2bob.service

and enable it to run at boot:

sudo systemctl enable powertop-h2g2bob.service

Done! Power is saved, every time.