Using PowerAPI to measure the energy consumption of your device

What is PowerAPI

PowerAPI is a software solution to power consumption that can be used for green computing. With PowerAPI, it is possible to measure the energy consumption of a system or specific programs and services. This tool allows developers to understand how much energy a program consumes and, when necessary, to remediate those issues in the goal of creating greener computing. To work, Power API provides hardware sensors and a formula to estimate power consumption: HWPC Sensors and SmartWatts

HWPC Sensor

As mentioned previously, PowerAPI is a software solution for measuring power consumption, as opposed to traditional power-measuring physical devices. The Hardware Performance Counter (HWPC) sensor is responsible for that. Thanks to its ability to use the Running Average Power Limit (RAPL) technology to monitor CPU power consumption. However, a drawback to this method is that the RAPL technology is only available on Intel Sandy Bridge and AMD Zen architectures, and also can only be used on a Linux distribution.

Note that virtual machines do not work!

SmartWatts Formula

The SmartWatts formula can estimate the power consumption of the CPU from the HWPC reports. The metrics provided by the reports are then fed to a power model that estimates the power consumption and calibrates itself with every new report.

Why write a blog about this?

This blog will serve as a sort of documentation and a thorough guide to configuring Power API. This guide is based on the Power API documentation and my own experimentation while trying to set it up. Not everything was in black and white and my goal with this post is to clarify areas that I found confusing or lacked explanation.

Setting up Power API

The rest of this blog post will guide you through the configuration process of Power API.

Operating System

For this, I will assume you have a fresh install of Ubuntu 20.04 and can follow this tutorial if you don't already have one:

Install Ubuntu desktop | Ubuntu

Docker

Next, you can install Docker following this tutorial:

Install Docker Engine on Ubuntu | Docker Docs

HWPC and Formula Installation

Start by downloading this script:

https://powerapi.org/script/smartwatts_install.sh

It will guide you through the installation process, you can run it with the command:

sudo bash ./smartwatts_install.sh

Select the docker options when prompted.

MongoDB

Our HWPC Sensors will need a location to write their reports to, so let's install MongoDB and use it as a source.

sudo docker run -d --name mongo_source -p 27017:27017 mongo

InfluxDB

Now that the sensors are writing to MongoDB to store sensor reports, the SmartWatts formula will read from those, and generate new reports to estimate the power consumption of the system. We will write these reports to InfluxDB.

sudo docker run \
 --name influxdb2 \
 --publish 8086:8086 \
 --mount type=volume,source=influxdb2-data,target=/var/lib/influxdb2 \
 --mount type=volume,source=influxdb2-config,target=/etc/influxdb2 \
 --env DOCKER_INFLUXDB_INIT_MODE=setup \
 --env DOCKER_INFLUXDB_INIT_USERNAME=ADMIN_USERNAME \
 --env DOCKER_INFLUXDB_INIT_PASSWORD=ADMIN_PASSWORD \
 --env DOCKER_INFLUXDB_INIT_ORG=org_test \
 --env DOCKER_INFLUXDB_INIT_BUCKET=power_consumption \
 influxdb:2

You can now access your database by going to http://localhost:8086.

Before moving on, go onto your influxDB page, in the Load Data menu, under API Tokens, generate a new all-access API token. In my case, I have two, one for Grafana to use, and one for the SmartWatts formula. Make sure you save the token information somewhere because you will not be able to access it again after the creation.

Grafana

The Power API documentation recommends using the Grafana tool to display data generated by the formula.

sudo docker run -d -p 3000:3000 grafana/grafana

You should now be able to access it from here: http://localhost:3000

Good now let's configure InfluxDB as a data source. Under Connections -> Data sources, click "Add new data source" and select InfluxDB as the type.

Now for the configuration settings of our data source, we can keep the query language to InfluxQL. For the URL, I personally had an issue when using localhost:8086, so instead I used my local IP. You can find it by running this command:

hostname -I

Which will return something like:

192.168.0.228 xxx.xxx.0.1

We can use the first IP address the command returned with the port 8086 as seen below. Next, we will need authorization to access the InfluxDB bucket, and we can use the API token we generated in the previous section. Under Custom HTTP headers, add a new header with the key "Authorization", and with the value "Token <my-token>" and replace <my-token> with the token from InfluxDB

All that is left is to specify the name of the database we wish to connect to. In our case it should be "power_consumption", but set this to whatever you configured it to be in InfluxDB. If everything went well then you should see a success message appear when you press save & test.

Below is a basic dashboard configuration you can import from the dashboard menu

Running Power API

Alright, everything should be in order and we are now ready to start recording the power consumption of our machine!

If we recall, there were two components to Power API, the HWPC sensors, and the SmartWatts formula. To record the power consumption of our system we need to run both of them.

Running HWPC Sensors

I won't go through all of the information concerning the configuration files, instead refer to the Power API documentation as it is very clear on all of the options. HWPC Sensor - PowerAPI

You can download both of the files that are attached below (config-sensor.json & run-sensor.sh) and run the command:

sudo bash run-sensor.sh

config-sensor.json

{
  "name": "sensor",
  "verbose": true,
  "frequency": 500,
  "output": {
    "type": "mongodb",
    "uri": "mongodb://127.0.0.1",
    "database": "db_sensor",
    "collection": "report_0"
  },
  "system": {
    "rapl": {
      "events": ["RAPL_ENERGY_PKG"],
      "monitoring_type": "MONITOR_ONE_CPU_PER_SOCKET"
    },
    "msr": {
      "events": ["TSC", "APERF", "MPERF"]
    }
  },
  "container": {
    "core": {
      "events": [
        "CPU_CLK_THREAD_UNHALTED:REF_P",
        "CPU_CLK_THREAD_UNHALTED:THREAD_P",
        "LLC_MISSES",
        "INSTRUCTIONS_RETIRED"
      ]
    }
  }
}

Running SmartWatts

Similarly to the sensors, you can download both of the files that are attached below (config-formula.json & run-formula.sh) and run the command:

sudo bash run-formula.sh

config-fomula.json

{
  "verbose": true,
  "stream": true,
  "input": {
    "puller": {
      "type": "mongodb",
      "uri": "mongodb://127.0.0.1",
      "db": "db_sensor",
      "collection": "report_0"
      }
  },
  "output": {
    "pusher_power": {
      "type": "influxdb2",
      "uri": "127.0.0.1",
      "port": 8086,
      "db": "power_consumption",
      "org": "org_test",
      "token": "<my token>"
    }
  },
  "cpu-base-freq": 1900,
  "cpu-error-threshold": 2.0,
  "disable-dram-formula": true,
  "sensor-reports-frequency": 500
}

Don't forget to change the token field with the API token you generated in InfluxDB

Viewing the Reports

We have set up everything, our MongoDB, InfluxDB, and Grafana images are running, both our sensor and formula too, so let's view our data.

Go to your Grafana page http://localhost:3000 and to the imported dashboard. At the top right of the toolbar, set the time frame to the last 5 minutes, and right next to that set the auto refresh to 5 seconds. There we go, we are now viewing real-time data of the system's energy consumption.

You can also filter to see specific processes or services. For example, here is the consumption of power by Firefox while watching a 4K video. The dip in the middle corresponds to the video paused and both spikes on either side are when the video was playing.

Green Computing & The Future

With a tool like Power API, measuring the power consumption of programs and systems becomes simpler. This tool can also assist us in creating sustainable software and with the sustainable development goal (SDG) 7 of the U.N. ensure access to affordable, reliable, sustainable and modern energy for all by focusing on energy efficiency.

Sources

Power API
United Nations 17 Sustainable Development Goals