I want to measure power usage of the whole PC in software on Linux. I've tried these two methods from here and here:
using powertop:
rm -f powertop.csv
sudo powertop -q -t 60 -C powertop.csv
cat powertop.csv | grep -i --color 'baseline power'
using /sys/class/powercap/intel-rapl*/energy_uj files:
time=60
sum_1=$(cat /sys/class/powercap/intel-rapl*/energy_uj | awk 'BEGIN { sum = 0; } { sum += $1; } END { print sum; }' "$@");
echo "before" $sum_1
sleep $time;
sum_2=$(cat /sys/class/powercap/intel-rapl*/energy_uj | awk 'BEGIN { sum = 0; } { sum += $1; } END { print sum; }' "$@");
echo "after" $sum_2
res=$(echo "(($sum_2 - $sum_1) / 1000000) / $time" | bc -l)
echo "Used power: $res W"
1. Why are the results different from both methods?
When running under heavy load (avg from 3 tests):
- method with
powertopreturned 64.8 W - method with
energy_ujfiles returned 138 W
TDP for this cpu is specified as 65 W, so I guess powertop method is more accurate, but the question is why? Why are the results different?
What exactly does energy_uj files store? Where does powertop gets its data from?
2. Does this method measures all power usage?
I run meausrements under different loads. These are the results.
powertop method |
energy_uj files method |
|
|---|---|---|
| idle | 1.2 W | 9 W |
| cpu heavy load | 64.8 W | 138 W |
| igpu heavy load | 40.6 W | 110 W |
| gpu heavy load | 14.1 W | 27 W |
I would expect results from gpu heavy load test to be much bigger, when its TDP is specified as 47 W (3.5 times bigger then measured by powertop). The gpu usage was in the 95% range during tests. Do you think this method measures all power used by whole PC, or just some part? How can I measure all power (within reason), without buying special power-measuring plugs?