How to reduce power consumption with GNU/Linux?
image/svg+xml
How to reduce power consumption with GNU/Linux?
Michael Opdenacker
Toulouse, 2023
How to reduce power
consumption with GNU/Linux ?
Free Software enthusiast
Founder of Bootlin
Embedded Linux Software Engineering
Strong presence in Toulouse ;-)
Author of free training materials
(Embedded Linux, Linux kernel, Linux
boot time reduction...)
Contributor to the Linux Ecology HOWTO
in the 2000s.
Michael Opdenacker
Following the energy crisis and the climate emergency
Need to save energy rather than producing more
GNU/Linux allows to use your hardware longer
Why this presentation?
Have longer running time on battery,
make the battery life longer
Learn how to make my penguin
hibernate
Sharing our best techniques
More technical motives
Main target hardware: laptops running GNU/Linux
Too : desktop computers, servers
And (a bit) : embedded Linux boards
Target of this presentation
Measure your consumption
Suspending, hibernating
Screen management
Frequency and performance
Identifying the top sources
of power consumption
Covered topics (1)
Measure your power consumption
Disabling unused devices
Making good use of generated heat
Covered topics (2)
For more accurate measurements
Using the ACME board from Baylibre
Cost : about 100 EUR / USD (thanks for the gift!)
Open Source Hardware and Software for accurate
measurement of power consumption.
Perfect to measure the consumption of embedded
boards or small circuits.
This allowed me to measure the consumption
of my BeagleBone Black (ARM). Unfortunately,
it's hard to make this board suspend to RAM
or even go idle. It seems special firmware is needed
and I still have issues building it.
https://baylibre.com/acme/
https://bootlin.com/blog/power-measurement-with-acme/
Let your penguin relax
Suspend to Idle
Meaning of "mem" in /sys/power/state
When you are not using your computer any more.
Linux can support 4 energy saving states:
- "Suspend-To-Idle" (
freeze
) : devices are suspended,
The CPU stops executing processes ("idle").
- "Standby / Power-On Suspend" (
standby
) :
Same, but with some additional processors switched off.
Never seen on the systems I own.
- "Suspend-to-RAM" (
mem
) : everything but RAM is switched off
- "Suspend-to-disk" (
disk
) : hibernate to disk - everything is off
and the RAM contents are kept on storage.
See which modes are available:
$ cat /sys/power/state
freeze mem disk
https://www.kernel.org/doc/Documentation/power/states.txt
According to kernel documentation, the "Freeze" mode is always available and
doesn't require any special hardware support, at least at the processor level.
- The CPU stops executing processes
- The devices are suspended or even turned off
(requires drives able to suspend them)
- The CPU has nothing left to do and therefore switches to an "idle" mode, enabling
to cut the power supply of some of its hardware blocks and save a lot of power.
- Suspend to Idle remplaces Suspend to RAM when the latter is not supported on your
hardware. Except in this case, the only advantage of this mode is reduced wake-up time
(< 1s instead of 3-4s), which is not significant on a PC (but may be useful on an
embedded system always trying to switch to a power saving mode whenever possible).
To trigger a switch to this mode:
sudo sh -c "echo freeze > /sys/power/state"
To wake up your system, same techniques as for Suspend to RAM (see next slides).
mem
in
/sys/power/state
can take several meanings:
If Suspend-to-RAM is available,
deep
appears in
/sys/power_memsleep
:
s2idle [deep]
- Here
[deep]
means that by défaut,
mem
corresponds to Suspend-to-RAM.
-
s2idle
(Suspend-To-Idle, "freeze") is available but not selected.
You can temporarily change the default setting:
sudo sh -c "echo s2idle > /sys/power/power_memsleep"
Or in a permanent way by editing the kernel command line, by
adding
mem_sleep_default=s2idle
to
GRUB_CMDLINE_LINUX_DEFAULT
in
/etc/default/grub
. Then run
sudo update-grub
.
Suspend to RAM vs. hibernate?
On old Dell Latitude laptop
- Estimated life of a 100% charged battery : 1h12
- Suspended: -6.9% (94 mAh) per hour
- Hibernated: remains at 100% after one night.
On a recent Dell XPS laptop
- Estimated life of a 100% charged battery : 8h30
- Suspended: -0.8% (58 mAh) per hour
- Hibernated: remains at 100% after one night.
Avoid wearing out your battery by using hibernation!
Licence Creative Commons - BY - SA - 4.0
https://creativecommons.org/licenses/by-sa/4.0/
Raspberry Pi 3 Model B V1.2
Power
HDMI
Audio
USB 2x
USB 2x
ETHERNET
DSI (DISPLAY)
CSI (CAMERA)
GPIO
© Raspberry Pi 2015
Questions?
michael.opdenacker@bootlin.com
Link to presentation and its sources:
https://bootlin.com/pub/conferences/2023/cdl/
Presentation made with Free Software: Sozi and Inkscape
Get your hands on a wattmeter
New : about 20 EUR / USD
Not ideal for measuring the total
consumption of a PC (measured in kW),
but good for instant consumption (W)
This will be useful for other devices
in your household.
Suspend your system
Almost everything is off, except RAM
Negligible consumption for the wattmeter.
But still can drain your battery in a few days.
Very easy to do whenever you leave your PC:
- From the command line
- With systemd
How to suspend your system?
Should be easy with any graphical distro
From the command line (with systemd):
systemctl suspend
Or with a low-level command (doesn't lock the screen):
sudo sh -c "echo mem > /sys/power/state"
How to wake up your system?
It's always easy to suspend than to wake-up
On a PC, just need to press the power button
Sometimes, just moving a USB mouse is enough
The wake-up sources can be found in
/sys/class/wakeup/
More details on
https://wiki.archlinux.org/title/Power_management/Wakeup_triggers
No simple solution on a remote server
to resume from suspend.
Summary
Suspend your system every time
you leave it.
Make it hibernate every night, and using
a master switch, switch off all devices,
including the ISP box (if possible).
While traveling, use hibernation to extend
the lifetime of your battery.
Install the TLP service to deactivate unused
device and enable power friendly settings.
The CPU frequency automatically adapts
to the activity of your programs.
Pay attention of the power consumption
of your screens.
Harvest dissipated heat
Let your imagination guide you
My leaven loves my Livebox
(ISP box, consumption: 12 W)
Other idea...
Hibernate your system
The state of your system is stored on disk,
and them your system is switched off.
Better for long periods of inactivity , because no power
consumption and not wearing out the battery in a laptop.
Unfortunately, not often available by default in recent
distributions.
Not very difficult to set up fortunately.
Hibernation principles (1)
Hibernation is based on suspending to RAM
To hibernate:
1. You simulate suspending to RAM.
The state of devices is stored in RAM.
2. All the RAM gets copied to disk
Need a swap partition or a file at least as big as RAM.
3. The machine is switched off.
Hibernation principles (2)
For waking-up:
1. The system is powered on. Linux boots.
2. The memory contents are restored from
the contents stored on disk.
3. We resume from RAM,
as if we were returning from suspend.
How to set up hibernation? (1)
Need space on disk to store
the RAM contents
Simplest solution : a swap partition almost
as bit as the RAM size
Otherwise, possible to store the RAM contents
in a file, but a little bit more complicated:
https://ubuntuhandbook.org/index.php/2021/08/enable-hibernate-ubuntu-21-10/
How to set up hibernation? (2)
1. Find the UUID of your swap partition
cat /etc/fstab | grep UUID=
2. Add
resume=UUID=xxxx
to
GRUB_CMDLINE_LINUX_DEFAULT
in
/etc/default/grub
3. Update Grub's configuration file:
sudo update-grub
Tested on Ubuntu 22.04
How to trigger hibernation?
Easy to do with Systemd
(the most exhaustive soulution: locks the session):
systemctl hibernate
The low-level solution (no session locking):
sudo sh -c "echo disk > /sys/power/state"
How to get out of hibernation?
Power your system back on, that's all!
On a remote server, possible to use
"Wake on LAN"
1. Enable this in the BIOS
2. Find the MAC address of the network interface of your server:
ip a
3. Pass this address to the wakeonlan command :
wakeonlan f8:ca:b8:17:44:02
Sending magic packet to 255.255.255.255:9 with f8:ca:b8:17:44:02
Easy to do for a machine on the same network, but not over the Internet.
Add a "hibernate" option (1)
Tested on Ubuntu 22.04
Start GNOME's Extension Manager:
extension-manager
Look for "Hibernate Status Button", install and activate
this extension.
Add a "hibernate" option (2)
Tested on Ubuntu 22.04
You also have to create the
/etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
file:
[Re-enable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes
[Re-enable hibernate by default in logind]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;
org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions;
org.freedesktop.login1.hibernate-ignore-inhibit
ResultActive=yes
Source :
https://ubuntuhandbook.org/index.php/2021/08/enable-hibernate-ubuntu-21-10/
Beware of the consumption of your screens (1)
Laptop doing nothing, maximum brightness
14.88 W
Laptop doing nothing, minimum brightness
10.04 W
Laptop doing nothing, screen off
7.44 W
Conclusion: a display at maximum brightness can represent
half of the power consumption of your PC when it's not
running anything.
Beware of the consumption of your screens (2)
Using ddcutil
On my old LCD IIyama monitor (2012)
At maximum brightness:
45 W !
At 50% brightness, sufficient, mais making a light but annoying hiss :-/
30 W
At minimum brightness, sufficient at nuit, the annoying hiss remains :
15 W
ddcutil is a Linux program for managing monitor settings,
such as brightness, color levels, and input source. Generally
speaking, any setting that can be changed by pressing buttons
on the monitor can be modified by ddcutil.
https://www.ddcutil.com/
$ sudo ddcutil capabilities
...
Feature: D6 (Power mode)
Values:
01: DPM: On, DPMS: Off
04: DPM: Off, DPMS: Off
...
To switch off the monitor:
$ sudo ddcutil setvcp D6 04
To switch it back on:
$ sudo ddcutil setvcp D6 01
How to switch off your screens?
Difficult to find universal commands!
With X, to switch off a specific screen:
$ xrandr -q
(identifier les écrans)
$ xrandr --output DP-2-3 --off
(éteindre)
$ xrandr --output DP-2-3 --auto
(rallumer)
Wasn't working for me on a system with a single screen
With ddutil, you can control your external screens, the
way you would do it with the control buttons.
On my laptop, the F8 key allows to alternate between
powered on screens (all or only one), without having to
go through the configuration menus. However, I find it
easier to set brightness to its minimum leve.
The easiest solution to switch off your screens without
suspending a probably screen locking (Super - L)
But better to go to suspend if your PC is idle!
Adapt your CPU performance
Ubuntu offers standard options to control
CPU frequency.
This is based of the "cpufreq" infrastructure
of the Linux kernel. You can even have
different settings for each CPU core!
GNOME CPU Power Manager Extension (1)
GNOME offers more specific options
to control the frequency and performance
of your CPU.
GNOME CPU Power Manager Extension (2)
Compiling the Linux kernel ("defconfig" configuration on x86)
- "High performance" : 14:37 min (active mode: 35W, passive mode: 10 W)
- "Power save" : 40:45 min (active mode: 15.5 W (average), passive mode: 15 W)
- "Multimedia" : 13:47 min (active mode: 38W, passive mode: 13W)
- "Silent" : 36:20 min (active mode: 17.8 W, passive mode: 12W)
=> A bit surprising results, which I should run again starting from a cold mother
board. It seems that power management takes temperature into account.
Lessons learnt:
- The "Power save" mode guarantees a minimal energy consumption. Good when
running on battery..
- The "High performance" mode doesn't make your system run at full speed all the
time. It just authorizes to run at full speed.
(a bit raw) performance tests
PowerTop - Available everywhere
Measures the most consuming tasks on your PC.
Also gives you advise for improving settings.
Need to be root and operate on battery (more information):
$ sudo powertop
A variant that first runs tests on your system for finer
power consumption estimates:
$ sudo powertop --calibrate
Which will consume 30 times less ?
If what you need is a server or a display controler.
An exception to the rule of keeping your hardware.
PowerTop - Overview
PowerTop - Tunables
Replace your big old PC by an embedded board
Detect tasks consuming most energy
New options in the "power" menu
What's "Hybrid Sleep"?
It's suspending and hybernating at the same time
- If you get back to your computer quickly, it wakes up instantly
- If you leave your computer for a longer time, the battery could drain
completely, but when you switch it on again, you get back to the saved
state thanks to hibernation.
That's like a safer "Suspend" option, but not an alternative to hibernation,
which is the only one preserving your battery.
Bootlin is recruiting interns and engineers
https://bootlin.com/company/careers/
It's possible to use the tool manually and apply its
recommendations:
$ sudo powertop --auto-tune
But it's easier to make them permanent:
$ sudo systemctl enable powertop
$ sudo systemctl start powertop
(ou redémarrer)
Results on old Dell Latitude (on AC),
LCD at maximum brightness:
consumption reduced from 15.12W to 12.55W
PowerTop - Integration
https://linrunner.de/tlp/
Similar to
powertop --auto-tune
, applies settings to reduce
power consumption.
- Enabled automatically at installation time (no need for PowerTop) :
$ sudo apt-install tlp
(Ubuntu)
- Two profiles: on battery and on AC, automatically selected.
- The battery profile is said to be more aggressive than the AC one.
- Possible to tune the settings if wanted (unlike with PowerTop),
even if default settings are usually sufficient.
-
tlp-stat
command: many useful info on battery, disks, etc.
Results on old Dell Latitude on AC, LCD screen at max brightness :
consumption reduced from 15,12W to 12,65W.
Would be interesting to run a battery life comparison too.
Even better: TLP
1
Title
Michael Opdenacker
Why this presentation?
More technical motives
Target of this presentation