Skip to content
This repository has been archived by the owner on Mar 11, 2019. It is now read-only.

Example of usage API

Loïc Huertas edited this page Nov 5, 2015 · 2 revisions

Process-level energy monitoring is based on a periodically computation that can be expressed via the API. Here there are several examples (in scala) to describe PowerAPI's API:

What is the CPU energy spent by the 123 process? Please give me fresh results every 500 milliseconds

Assume that process run under Linux, using a procfs file system on a standard CPU architecture. Consider, for example, the procfs CPU module (ProcFSCpuSimpleModule()). Add to this the desire to display CPU energy spent by process into a console (ConsoleDisplay). So we need to:

  • Choose the required module(s):
val cpu = PowerMeter.loadModule(ProcFSCpuSimpleModule())
  • Create the output object:
val console = new ConsoleDisplay
  • Ask to PowerAPI to provide the CPU energy spent by the 123 process during 5 minutes, every 500 milliseconds, using a console and aggregating results produced every 500 milliseconds:
val monitoring = cpu.monitor(500.milliseconds)(123) to console
cpu.waitFor(5.minutes)
monitoring.cancel
cpu.shutdown

Here is the full code of this example:

import org.powerapi.PowerMeter
import org.powerapi.module.cpu.simple.ProcFSCpuSimpleModule
import org.powerapi.core.target._
import org.powerapi.reporter.ConsoleDisplay
import scala.concurrent.duration._

object Monitor extends App {
  val cpu = PowerMeter.loadModule(ProcFSCpuSimpleModule())
  val console = new ConsoleDisplay
  val monitoring = cpu.monitor(500.milliseconds)(123) to console

  cpu.waitFor(5.minutes)
  
  monitoring.cancel
  cpu.shutdown
}

Based on the first request, how can I display CPU energy information into a chart too?

Based on the previous code, we simply have to add a new Reporter which will be able to display CPU energy information into a chart. PowerAPI integrates a Reporter using the JFreeChart Java graph library. So let's add it to the PowerAPI system:

val chart = new JFreeChartDisplay
monitoring to chart

Do not forget to configure correctly the modules (see here on how to configure a module)

Others examples

To see others examples about building of powermeters with the API, click here