1. Overview

In this tutorial, we’ll see several ways to find the power that our Linux machine is consuming. First, we’ll see the power being consumed by the battery using basic and more complex tools. Secondly, we’ll discuss what can be done to get the real-time power usage of our system.

2. Power Consumed From the Battery

We might be only interested in the power that our machine is consuming from the battery. In this case, the system filesystem (sysfs) and basic shell tools are all we need.

2.1. Using the power_now File

We can look at the content of a file called power_now with cat. This file contains the instantaneous power in microwatts:

$ cat /sys/class/power_supply/BAT*/power_now

We’ve used asterisk bash globbing to match any battery that our laptop has. There are laptops with several batteries, and even if our laptop has only one battery, it might not be BAT0. However, for the following code snippets, we’ll use BAT0 as the device battery.

We can manipulate the string in the power_now file with awk to convert the power to watts and show its units:

$ awk '{print $1*1e-6 " W"}' /sys/class/power_supply/BAT0/power_now
22.040 W

We multiply the value from the file ($1) with 10^-6 to convert from microwatts to watts.

2.2. Searching on Other Locations

We might get an error when checking the power_now file with the previous commands:

$ cat /sys/class/power_supply/BAT*/power_now
cat: '/sys/class/power_supply/BAT*/power_now': No such file or directory

In this case, the power_now file is not in that directory. For example, if we’ve got a Thinkpad with the tp_smapi module loaded, we can find the power_now file in another directory:

$ cat /sys/devices/platform/smapi/BAT0/power_now

We can always use find to locate the folder that contains the power_now file:

$ find /sys/ -type f -name power_now 2> /dev/null

We’ve used the 2> operator to redirect the standard error to /dev/null to hide the errors for denied access. The path found doesn’t look at all like the /sys/class/power_supply/BAT*/power_now from the previous section. The sysfs relies on symbolic links, and there might be many different ways to reach the same file. We can check it by seeing the content of the file that find has returned:

$ cat /sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:00/power_supply/BAT0/power_now

We get the same instantaneous power in microwatts with the path returned by find.

2.3. Computing the Power When the power_now File Is Not Available

Our device might not have the power_now file. However, we can compute the instantaneous power if we have the current_now and votage_now files. We can do this since power is current times voltage:

$ cat /sys/class/power_supply/BAT0/current_now /sys/class/power_supply/BAT0/voltage_now | xargs | awk '{print $1*$2/1e12 " W"}'
22.040 W

We’ve concatenated both files with cat and used xargs to get them in a single line. Then, with awk, we can multiply them and apply the correct unit conversion.

2.4. Estimate the Power Based on the Energy

There’s another set of files inside the sysfs that provides energy information. Based on the energy information, we can take a timestep and compute the power. We compute power with energy divided by time. This approach uses the RAPL (Running Average Power Limit), which is one interface for reporting the accumulated energy consumption of various device components.

The energy can be retrieved from the powercap directory:

$ sudo cat /sys/class/powercap/*/energy_uj

We need super-user rights since we can’t read the energy_uj file from the powercap directory without the SYS_ADMIN_CAP. With this command, we just retrieved the values of the microjoules that our device has used. To get the power, we need to get these values in two instants and compute the difference:

$ time=1
$ declare T0=($(sudo cat /sys/class/powercap/*/energy_uj)); sleep $time; declare T1=($(sudo cat /sys/class/powercap/*/energy_uj))
$ for i in "${!T0[@]}"; do echo - | awk "{printf \"%.1f W\", $((${T1[i]}-${T0[i]})) / $time / 1e6 }" ; done
22.040 W

The for loop is there since we might get several energy_uj values depending on our configuration. Moreover, results may diverge from those of the previous approach since the source of the energy is different than before.

3. Retrieving Detailed Information on the Battery Status

Let’s look at some additional utilities that provide detailed information on the battery status and other sensor information.

3.1. Using lm_sensors

The lm_sensors application comes with the sensors command, which we can use to get the battery information. The first step is to install the lm_sensors package with the package manager of our distribution. Then, we need to generate a list of the kernel modules:

$ sudo sensors-detect

We should be careful if we modify the default options since this can cause screen issues. Just accepting the default options with Enter is the safest option.

Once we’ve detected the sensors of our machine, we can get the instantaneous power, along with other device information:

$ sensors
Adapter:      ACPI interface 
power1:       23.00 W (interval = 1.00 s)

We can parse the output of sensors with grep if we need just the power output. The exact power value might differ from the previous approach although it’ll be close.

3.2. Using tlp-stat

tlp-stat is another application designed to view information on the system related to power and battery. We need to run the tlp-stat command with super-user rights to get all the detailed information about our system. However, we can use the -b flag to get only the information concerning the battery:

$ sudo tlp-stat -b
--- TLP 1.6.1 --------------------------------------------
/sys/class/power_supply/BAT0/power_now                      =  22040 [mW]
Charge                                                      =   99.0 [%]
Capacity                                                    =   65.1 [%]

In this case, since tlp-stat retrieves the information from the sysfs, the values should match those we obtained in Section 2.

3.3. Using powerstat

powerstat is another tool that we can use to measure power consumption. The tool provides advanced statistics on battery information and several options to customize the tests. We can run the tool without any option:

$ powerstat
Running for 300.0 seconds (30 samples at 10.0 second intervals).
Power measurements will start in 180 seconds time.

Waiting 180 seconds before starting (gathering samples).

By default, the tool waits 180 seconds to stabilize battery usage and runs for at least 300 seconds. We can start taking measurements from the launch of the command with the -d 0 option, but then it will run for at least 480 seconds.

We can also specify a custom interval sampling (instead of the default 10 seconds) and a custom number of samples (instead of the default of 30 samples). In the case that we don’t have a large enough number of readings, the program will stop. Otherwise, we’ll see instantaneous values and a summary at the end:

$ powerstat -d 0 0.5 960 
Running for 480.0 seconds (960 samples at 0.5 second intervals). 
Power measurements will start in 0 seconds time. 
Time    User  Nice Sys  Idle  IO   Run  Ctxt/s  IRQ/s Watts 
22:48:12 30.8 0.0  3.0  62.1  4.0  2    5650    1510  -nanE 
22:48:13 32.3 0.0  2.5  65.2  0.0  2    5846    1364   0.00E
22:48:13 30.2 0.0  1.0  68.8  0.0  3    3934    1228   0.00E
22:48:14 29.9 0.0  1.5  68.5  0.0  4    7490    1546   22.04
22:48:14 29.3 0.0  1.5  69.2  0.0  2    3634    1208   22.04
22:48:15 29.4 0.0  2.0  68.5  0.0  2    7482    1444   22.04
22:48:15 29.4 0.0  1.5  69.0  0.0  2    4306    1268   22.04
22:56:09 33.0 0.0  1.5  65.5  0.0  3    4176    1466   23.51
22:56:10 57.9 0.0  5.6  36.5  0.0  3    8670    2212   23.51
22:56:10 42.6 0.0  3.0  54.5  0.0  2    6588    3020   22.66
22:56:11 41.0 0.0  2.1  56.9  0.0  3    7228    1842   22.66
22:56:11 52.8 0.0  3.0  44.2  0.0  3    9324    2158   22.66
22:56:12 62.8 0.0  3.5  33.7  0.0  3    7002    2812   23.34
Average  38.0 0.0  2.6  58.7  0.7  2.9  7519.9  2396.1 23.37
GeoMean  37.1 0.0  0.0  57.3  0.0  2.7  7197.9  2141.8 23.33
StdDev   9.2  0.0  2.2  11.0  2.4  1.4  2251.1  2144.8  1.40
Minimum  27.2 0.0  0.0  10.0  0.0  2.0  2710.0  1016.0 21.07
Maximum  84.8 0.0 20.5  72.3 35.4 12.0 19566.0 42258.0 28.46
System:  23.37 Watts on average with standard deviation 1.40   
Note: Power calculated from battery capacity drain, may not be accurate.

We see the disclaimer at the end, where it says that the measurements from the battery capacity drain might not be accurate. However, powerstat is a useful tool that we can use to study the status of our battery with the same accuracy as previous methods.

3.4. powertop

There’s yet another tool that provides information about the power in our system: powertop. The name refers to top because the tool gives not only the overall power usage of the battery but also a detailed breakdown based on each process. 

This tool is very powerful but it has two caveats: We need to run this tool with super-user rights, and it might only work properly on Intel devices.

Once we run it, we’ll see an interactive list of each process and its power information after an overview:

$ sudo powertop                                                           
# PowerTOP 2.15     Overview   Idle stats   Frequency stats   Device stats   Tunables   WakeUp
The battery reports a discharge rate of 19.7 W
The energy consumed was 0.00 J
The estimated remaining time is 2 hours, 18 minutes
Summary: 785.4 wakeups/second,  0.0 GPU ops/seconds, 0.0 VFS ops/sec and 154.6% CPU use
Power est.  Usage         Events/s    Category       Description
22.6 mW     8.8 ms/s      390.2       Timer          tick_sched_timer
2.22 mW     719.8 ms/s    12.7        Process        [PID 50025] /usr/lib/firefox/firefox
<ESC> Exit | <TAB> / <Shift + TAB> Navigate |

We see a discharge rate, energy consumed, and estimated remaining battery time. Then, we see the breakdown for each process and its power estimate. Moreover, there are several tabs with more information that we can explore with the Tab button.

In case we don’t see the first column, we need to run powertop for some time to calibrate it. powertop requires some measurements to give power usage estimates, and it will show the error “A minimum of 270 measurements are needed” if calibration is missing.

4. Real-Time Power Usage From the Outlet

Finally, let’s mention external devices for a truly accurate measurement of power consumption. This is relevant for servers and other large-scale applications. We can find in the market devices such as Kill-A-Watt or other power meters that we can use to check the power that a device is drawing. Some of them even allow sending the data to a device remotely, so we can see the power consumption in our smartphones.

We’ll need to unplug the device and reconnect it through the power meter. This will make that electricity run through the power meter that we installed between the plug and our device.

However, we’ll be getting an accurate measurement of the power consumption from the device. This will account for all the variance that a computer might experience in terms of CPU power management, disks turning idle, and other factors.

5. Conclusion

In this article, we’ve talked about ways to find out the power consumption of our Linux machine. We can use different approaches to obtain the power consumed from the battery, which we use depending on the configuration of our device. To find out the precise real-time power usage, we’ll most probably need an external power meter.

Comments are closed on this article!