48

Is there a way to return the current watt consumption on the command line? I have found about the powertop program, but have not seen a way to return the Watt consumption as a value to the command line. I'm thinking of some file that I can cat or grep.

Braiam
  • 35,991

7 Answers7

32

On my system I can obtain the power drawn from the battery from

cat /sys/class/power_supply/BAT0/power_now
9616000

On Thinkpads if the tp_smapi module is loaded, the file is

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

The value seems to be in µW, though. You can convert it with any tool you're comfortable with, e.g. awk:

awk '{print $1*10^-6 " W"}' /sys/class/power_supply/BAT0/power_now
9.616 W

In case you cannot find the location within the sysfs file system, you can search for it:

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

Additionally, the package lm-sensors may be used to determine the system power usage on some machines:

# sensors power_meter-acpi-0
power_meter-acpi-0
Adapter: ACPI interface
power1:      339.00 W  (interval =   1.00 s)
Marco
  • 33,548
  • 4
    I don't have the power_now file, but I have current_now (micro amperes) and voltage_now (micro volts), which must be multiplied to get the power (linux 5.9.14). Docs: https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power – njam Dec 20 '20 at 13:08
  • On my rooted Android 11 phone, the same file can be found here : /sys/class/power_supply/bms/power_now while there is no power information in /sys/class/power_supply/battery/ (but current and voltage) – TheBigBadBoy Jul 19 '22 at 21:56
  • The main issue with looking at the power_supply class for determining power consumption, is that it is only relevant when all the power is drawn from the battery. when you have a charger connected, at least some of the power will be drawn from the charger, and the battery will report no drain (on my system, with the charger connected and battery fully charged, current_now reports less than 0.1 W. Looking at powercap, as described in @alba-mendez answer is probably what you'd want. – Guss Jun 21 '23 at 14:52
13

My laptop has none of these (it also has no battery currently), but it does have a "powercap" device.
It seems this device is able to set restrictions on the user power.

And it (naturally) can read the power draw in order to enforce them.
The power draw can be found at the energy_uj file, i.e.:

cat /sys/class/powercap/*/energy_uj

It'll show the lecture (or lectures, if you have many powercap devices) in micro-Joules. This is actually a counter of the energy consumed, so you need to divide it by a time delta in order to get the power.

Is this the power drawn by my laptop, or just a part of it? I don't know.

Alba Mendez
  • 231
  • 2
  • 6
  • Well, these devices are fairly common on new computers... On all of my tests, at least one of them shows a realistic power draw value with very good precision. I should test with a multimeter, though. – Alba Mendez Oct 12 '14 at 09:03
  • This seems a pretty nice solution. Where do you get the time delta from? – Freefri Jan 11 '17 at 13:01
  • 2
    @Freefri you read the value, wait 1 second, read the value again. 1 second is your delta. (Energy_now-Energy_one_second_ago) / 1 second = joules consumed per second (or watts, 1 watt = 1 joule / 1 second) – GDR Nov 10 '17 at 22:03
  • 1
    Do you know, how to find out, what is each of these "powercap" devices? I would like to track the power consumption of the entire system. Probably, I should take all of them into account. – mabalenk Jun 18 '20 at 14:00
  • 2
    @mabalenk: There is a name value giving the name, apart from that it seems one has to research about RAPL to understand what they mean. RAPL seems to separate its measurements into package, core, uncore and optionally dram. To my understanding package should include both core and uncore, but despite the hierarchical layout I am not sure about dram. – aufziehvogel Dec 17 '20 at 16:49
  • 2
    I have created a script that uses the powercap feature shown here to summarize the current powerdraw. You can find it in this gist: https://gist.github.com/guss77/c91e1d6583fad5b5c917eafc345cb81b – Guss Jun 21 '23 at 15:45
8
sudo tlp-stat -b

It will show the current power consumption in milli Watt unit.

--- TLP 1.1 --------------------------------------------

+++ Battery Status
/sys/class/power_supply/BAT1/manufacturer                   = SANYO
/sys/class/power_supply/BAT1/model_name                     = L12S3F01
/sys/class/power_supply/BAT1/cycle_count                    =     16
/sys/class/power_supply/BAT1/energy_full_design             =  32560 [mWh]
/sys/class/power_supply/BAT1/energy_full                    =  16180 [mWh]
/sys/class/power_supply/BAT1/energy_now                     =  16090 [mWh]
/sys/class/power_supply/BAT1/power_now                      =      0 [mW]
/sys/class/power_supply/BAT1/status                         = Unknown

Charge                                                      =   99.4 [%]
Capacity                                                    =   49.7 [%]
rtnF14
  • 81
  • 1
  • 4
6

On a laptop by reading the ACPI data from either procfs or sysfs. On my system the files are:

/proc/acpi/battery/BAT0/state
/sys/bus/acpi/drivers/battery/PNP0C0A:00/power_supply/BAT0/power_now

Note that the sysfs is heavily symlinked so there are many ways to reach the file. power_now is the file name you are looking for.

peterph
  • 30,838
4

For non-battery driven machines try cat /sys/class/hwmon/hwmonN/device/powerM_average - it shows the average power consumption in mW within the last /sys/class/hwmon/hwmonN/device/powerM_average_interval ms (usually 1s; N, M are integers) for each identified device. On modern dual-socket servers often N = {0..2}, one monitor for each CPU package and one for all PSUs together (check powerM_oem_info). You probably need to load the acpi_power_meter kernel module to get the related information.

jelmd
  • 349
  • 2
  • 8
3

powerstat can give the CPU consumption in watts:

$ sudo powerstat -R -c -z 1 60
Running for 60.0 seconds (60 samples at 1.0 second intervals).
Power measurements will start in 0 seconds time.

Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Fork Exec Exit Watts 20:18:04 5.1 44.7 30.9 19.2 0.1 13 22220 15093 53 6 12 120.04 20:18:05 2.3 45.2 29.7 22.7 0.0 13 15554 11232 6 1 6 119.40 20:18:06 1.5 45.1 29.9 23.5 0.0 13 11006 9103 4 1 5 118.97 20:18:07 2.1 44.0 28.3 25.6 0.0 5 22597 13773 8 1 6 117.61 20:18:08 1.6 0.4 0.6 97.4 0.0 1 13086 7083 5 1 6 31.41 20:18:09 1.8 0.0 0.3 98.0 0.0 1 8632 4264 8 1 5 28.26 20:18:10 1.9 0.0 0.7 97.4 0.0 2 8175 3888 7 1 8 29.91

Source: https://www.golinuxhub.com/2018/06/how-to-measure-power-consumption-in-watts-powerstat-linux-examples/

A.L
  • 1,586
2

Last but not least for BMC driven *x machines one may try ipmitool dcmi power reading. On Linux it requires RW privileges for /dev/ipmi0 and the ipmi_si kernel module loaded, similar on Solaris 11.x /dev/bmc and module bmc. Other alternative is to use the LANplus variant of ipmitool if the BMC is accessible from remote.

The Instantaneous power reading: is usually ok, all other emitted values should be taken with some grains of salt ;-).

Example:

# ipmitool dcmi power reading
Instantaneous power reading:                   138 Watts
Minimum during sampling period:                 75 Watts
Maximum during sampling period:                420 Watts
Average power reading over sample period:      157 Watts
IPMI timestamp:                           Fri Jan  7 06:32:43 2022
Sampling period:                          00000001 Seconds.
Power reading state is:                   activated

jelmd
  • 349
  • 2
  • 8