4

I am doing research and would like some information about the energy consumption of two algorithms I wrote, but I don't have the money for special-purposed devices. I plan to run both programs for 5 minutes, and would like to measure the ratio of their energy consumption (e.g., program A uses up 1.2 times more power (watts) than program B)

I did some research and find commands such as

upower -d

but I'm not sure how to interpret it properly even after reading the manual, and would like to know are there other solutions better than this.

I'm using the following distro on a bare metal laptop:

uname -a
Linux linuxbox 4.15.0-99-generic #100-Ubuntu SMP Wed Apr 22 20:32:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
5
  • To start with, which Linux distro have you installed (Ubuntu server, Ubuntu desktop, Kubuntu, Lubuntu, Xubuntu, Ubuntu MATE, Mint, et al.), & which release number? Different releases have different tools for us to recommend. Please click edit & add that to your question, so all facts we need are in the question. Please don't use Add Comment, since that's our one-way channel to you. All facts about your PC should go in the Question with edit
    – K7AAY
    May 18, 2020 at 22:20
  • If turbostat works on your computer, it would be a good tool to use. Example: sudo ./turbostat --Summary --quiet --Joules --show Pkg_J ~/c/consume 20 73 30 gave: 51.10 Joules. (the program consumed 20% of a CPU at 73 hertz sleep / work rate, for 30 seconds). Check that your power counters will not wrap around in 5 minutes (they shouldn't). May 19, 2020 at 0:26
  • I just read the manual of turbostat, but it looks like a tool measuring the overall energy consumption instead of measuring the stats only from a program. Did I get it wrong? May 19, 2020 at 2:01
  • You got it right. I just also do a run with an idle system and subtract it. (I am a server person, and can get my system very very idle, by disabling services.) May 19, 2020 at 14:58

2 Answers 2

1

Have you seen powertop? powertop is a process monitor like htop, or top, but it gives you an estimate power consumption in watts for each process. You can use the csv mode with --csv=output.csv to collect data in csv format. Check it out here:

Powertop info and download site

4
  • I've tried it a couple of days ago, but if my memory serves me right, it only provides hints about CPU usage but not the energy? May 19, 2020 at 1:56
  • You may have tried top or htop, not powertop. powertop will give you the power usage in watts. Here's a screenshot with power usage in the leftmost column: fedoramagazine.org/wp-content/uploads/2015/05/…
    – nrejack
    May 19, 2020 at 13:36
  • I just double-checked. Looks like the feature has been removed. imgur.com/a/j73iz0s May 20, 2020 at 20:40
  • It is likely you are missing elements that are needed to read the power usage from your CPU. In ubuntu, I would recommend installing the version you get from using 'sudo apt install powertop', as this will install the necessary dependencies.
    – nrejack
    May 21, 2020 at 17:55
1

The importance of efficient use of energy in Linux is increasing. More and more benchmark type tests are adding energy use as an additional evaluation criteria.

This answer uses the turbostat program, which works on most modern Intel processors, some AMD processors and perhaps some others. It is available in multiple packages, and while upstream guarantees no kernel version dependence, Ubuntu wraps it in a very very strict dependency wrapper.

I will use 1 minute instead of 5 minutes, but the suggestion here is to do the test many many times.

First, measure the idle energy perhaps for longer. Watch for how stable the idle energy draw is, i.e. how noisy. Note that I actually capture and plot idle data for many hours. Sometimes some cron job or other unexpected thing occurs during the test, so it is good to run the test multiple times so that outliers are more apparent. Wait at least 5, preferably 15, minutes after any re-boot before any tests, to allow the system to settle.

Oh, by the way check for your counter wrap around limit:

doug@s18:~$ sudo ./turbostat --num_iterations 1
...
RAPL: 2759 sec. Joule Counter Range, at 95 Watts
...

Future readers note: The above check will not be needed, as a pending patch will have turbostat take care of wrap around.

Idle:

doug@s18:~$ sudo ./turbostat --Summary --quiet --Joules --show Pkg_J --interval 60
Pkg_J
78.97
77.71
74.75
74.37
74.44
74.42
74.72
74.50
74.97
74.79
74.53
74.52
74.39
74.32
74.30
74.35
74.41
74.41
74.63
74.71

Throwing out the first 2 samples gives 74.53 Joules average.

Now acquire energy data when the test programs run. The best way to do this is test program specific, and is really a subject in itself. Assume constant energy consumption verses times (otherwise we should sample and plot), and that the program can keep running in some loop for easy acquisition of multiple samples.:

doug@s18:~$ sudo ./turbostat --Summary --quiet --Joules --show Pkg_J ~/c/consume 40 73 60 1
Ave. work percent: 44.343601
60.002474 sec
Pkg_J
174.13

That was with: intel_cpufreq CPU frequency scaling driver and the ondemand CPU frequency scaling governor. Now with the performance governor, so as to introduce change:

doug@s18:~$ sudo ./turbostat --Summary --quiet --Joules --show Pkg_J ~/c/consume 40 73 60 1
Ave. work percent: 29.533655
60.002356 sec
Pkg_J
240.78

It isn't relevant but the arguments for program I am using mean 40% work/sleep nominal ratio, but fixed work to do per interval (hence 29% only for performance), 73 hertz work/sleep rate, 60 seconds.

Method 2, more samples. If the program can be run in a loop, perhaps from a script, more samples are desirable so that outliers can be identified and omitted:

Ondemand:

doug@s18:~$ sudo ./turbostat --Summary --quiet --Joules --show Pkg_J --interval 60
Pkg_J
188.11
178.26
178.68
179.42
180.86
183.31
183.31
186.55

average: 182.31 Joules

Performance:

doug@s18:~$ sudo ./turbostat --Summary --quiet --Joules --show Pkg_J --interval 60
Pkg_J
241.97
245.07
244.82
253.09
263.40
261.08
255.77
251.53

average: 252.09 Joules

So for this 1 minute example:
Ondemand = 182.31 - 74.53 = 107.78 Joules (1.80 watts)
Performance = 252.09 - 74.53 = 177.56 Joules (2.96 watts)

Note: this answer was written using energy, Joules, but it could just have easily been written using power, Watts. Example:

doug@s18:~$ sudo ./turbostat --Summary --quiet --show PkgWatt --interval 60
PkgWatt
3.07
3.08
3.12
3.24

Now that is processor package energy/power, but what about the mains draw. For this computer it is 38.84 watts with the above running (ondemand) and 36.83 without, 2 watts difference, verses 1.80 above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.