|
| 1 | +/* |
| 2 | + * This software is licensed under the GNU Affero General Public License, quoted below. |
| 3 | + * |
| 4 | + * This file is a part of PowerAPI. |
| 5 | + * |
| 6 | + * Copyright (C) 2011-2015 Inria, University of Lille 1. |
| 7 | + * |
| 8 | + * PowerAPI is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as |
| 10 | + * published by the Free Software Foundation, either version 3 of |
| 11 | + * the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * PowerAPI is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with PowerAPI. |
| 20 | + * |
| 21 | + * If not, please consult http://www.gnu.org/licenses/agpl-3.0.html. |
| 22 | + */ |
| 23 | +package org.powerapi.app |
| 24 | + |
| 25 | +import java.lang.management.ManagementFactory |
| 26 | + |
| 27 | +import org.powerapi.core.target.{Application, All, Process, Target} |
| 28 | +import org.powerapi.reporter.{FileDisplay, JFreeChartDisplay, ConsoleDisplay} |
| 29 | +import org.powerapi.{PowerMonitoring, PowerMeter, PowerModule} |
| 30 | +import org.powerapi.core.power._ |
| 31 | +import org.powerapi.module.cpu.dvfs.CpuDvfsModule |
| 32 | +import org.powerapi.module.cpu.simple.CpuSimpleModule |
| 33 | +import org.powerapi.module.libpfm.{LibpfmHelper, LibpfmCoreProcessModule, LibpfmCoreModule} |
| 34 | +import org.powerapi.module.powerspy.PowerSpyModule |
| 35 | +import scala.concurrent.duration.DurationInt |
| 36 | +import scala.sys |
| 37 | +import scala.sys.process.stringSeqToProcess |
| 38 | + |
| 39 | +/** |
| 40 | + * PowerAPI CLI. |
| 41 | + * |
| 42 | + * @author <a href="mailto:maxime.colmant@gmail.com">Maxime Colmant</a> |
| 43 | + * @author <a href="mailto:l.huertas.pro@gmail.com">Loïc Huertas</a> |
| 44 | + */ |
| 45 | +object PowerAPI extends App { |
| 46 | + val modulesR = """(cpu-simple|cpu-dvfs|libpfm-core|libpfm-core-process|powerspy)(,(cpu-simple|cpu-dvfs|libpfm-core|libpfm-core-process|powerspy))*""".r |
| 47 | + val aggR = """max|min|geomean|logsum|mean|median|stdev|sum|variance""".r |
| 48 | + val durationR = """\d+""".r |
| 49 | + val pidR = """(\d+)""".r |
| 50 | + val appR = """(.+)""".r |
| 51 | + |
| 52 | + @volatile var powerMeters = Seq[PowerMeter]() |
| 53 | + @volatile var monitors = Seq[PowerMonitoring]() |
| 54 | + |
| 55 | + val shutdownHookThread = scala.sys.ShutdownHookThread { |
| 56 | + monitors.foreach(monitor => monitor.cancel()) |
| 57 | + monitors = Seq() |
| 58 | + powerMeters.foreach(powerMeter => powerMeter.shutdown()) |
| 59 | + powerMeters = Seq() |
| 60 | + } |
| 61 | + |
| 62 | + def validateModules(str: String) = str match { |
| 63 | + case modulesR(_*) => true |
| 64 | + case _ => false |
| 65 | + } |
| 66 | + |
| 67 | + implicit def modulesStrToPowerModules(str: String): Seq[PowerModule] = { |
| 68 | + (for(module <- str.split(",")) yield { |
| 69 | + module match { |
| 70 | + case "cpu-simple" => CpuSimpleModule() |
| 71 | + case "cpu-dvfs" => CpuDvfsModule() |
| 72 | + case "libpfm-core" => LibpfmCoreModule() |
| 73 | + case "libpfm-core-process" => LibpfmCoreProcessModule() |
| 74 | + case "powerspy" => PowerSpyModule() |
| 75 | + } |
| 76 | + }).toSeq |
| 77 | + } |
| 78 | + |
| 79 | + def validateAgg(str: String): Boolean = str match { |
| 80 | + case aggR(_*) => true |
| 81 | + case _ => false |
| 82 | + } |
| 83 | + |
| 84 | + implicit def aggStrToAggFunction(str: String): Seq[Power] => Power = { |
| 85 | + str match { |
| 86 | + case "max" => MAX |
| 87 | + case "min" => MIN |
| 88 | + case "geomean" => GEOMEAN |
| 89 | + case "logsum" => LOGSUM |
| 90 | + case "mean" => MEAN |
| 91 | + case "median" => MEDIAN |
| 92 | + case "stdev" => STDEV |
| 93 | + case "sum" => SUM |
| 94 | + case "variance" => VARIANCE |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + def validateDuration(str: String): Boolean = str match { |
| 99 | + case durationR(_*) => true |
| 100 | + case _ => false |
| 101 | + } |
| 102 | + |
| 103 | + implicit def targetsStrToTargets(str: String): Seq[Target] = { |
| 104 | + val strTargets = if(str.split(",").contains("all")) { |
| 105 | + "all" |
| 106 | + } |
| 107 | + else str |
| 108 | + |
| 109 | + (for(target <- strTargets.split(",")) yield { |
| 110 | + target match { |
| 111 | + case "" => Process(ManagementFactory.getRuntimeMXBean.getName.split("@")(0).toInt) |
| 112 | + case "all" => All |
| 113 | + case pidR(pid) => Process(pid.toInt) |
| 114 | + case appR(app) => Application(app) |
| 115 | + } |
| 116 | + }).toSeq |
| 117 | + } |
| 118 | + |
| 119 | + def printHelp(): Unit = { |
| 120 | + val str = |
| 121 | + """ |
| 122 | + |PowerAPI, Spirals Team" |
| 123 | + | |
| 124 | + |Build a software-defined power meter. Do not forget to configure correctly the modules (see the documentation). |
| 125 | + | |
| 126 | + |usage: ./powerapi modules [cpu-simple|cpu-dvfs|libpfm-core|libpfm-core-proces|powerspy, ...] \ |
| 127 | + | monitor --frequency [ms] --targets [pid, ..., app, ...)|all] --agg [max|min|geomean|logsum|mean|median|stdev|sum|variance] --[console,file [filepath],chart] \ |
| 128 | + | duration [s] |
| 129 | + | |
| 130 | + |example: ./powerapi modules cpu-simple monitor --frequency 1000 --targets firefox --agg max --console monitor --targets chrome --agg max --console \ |
| 131 | + | modules powerspy monitor --frequency 1000 --targets all --agg max --console \ |
| 132 | + | duration 30 |
| 133 | + """.stripMargin |
| 134 | + |
| 135 | + println(str) |
| 136 | + } |
| 137 | + |
| 138 | + def cli(options: List[Map[Symbol, Any]], duration: String, args: List[String]): (List[Map[Symbol, Any]], String) = args match { |
| 139 | + case Nil => (options, duration) |
| 140 | + case "modules" :: value :: "monitor" :: tail if validateModules(value) => { |
| 141 | + val (remainingArgs, monitors) = cliMonitorsSubcommand(List(), Map(), tail.map(_.toString)) |
| 142 | + cli(options :+ Map('modules -> value, 'monitors -> monitors), duration, remainingArgs) |
| 143 | + } |
| 144 | + case "duration" :: value :: tail if validateDuration(value) => cli(options, value, tail) |
| 145 | + case option :: tail => println(s"unknown cli option $option"); sys.exit(1) |
| 146 | + } |
| 147 | + |
| 148 | + def cliMonitorsSubcommand(options: List[Map[Symbol, Any]], currentMonitor: Map[Symbol, Any], args: List[String]): (List[String], List[Map[Symbol, Any]]) = args match { |
| 149 | + case Nil => (List(), options :+ currentMonitor) |
| 150 | + case "modules" :: value :: "monitor" :: tail if validateModules(value) => (List("modules", value, "monitor") ++ tail, options :+ currentMonitor) |
| 151 | + case "duration" :: value :: tail if validateDuration(value) => (List("duration", value) ++ tail, options :+ currentMonitor) |
| 152 | + case "monitor" :: tail => cliMonitorsSubcommand(options :+ currentMonitor, Map(), tail) |
| 153 | + case "--frequency" :: value :: tail if validateDuration(value) => cliMonitorsSubcommand(options, currentMonitor ++ Map('frequency -> value), tail) |
| 154 | + case "--targets" :: value :: tail => cliMonitorsSubcommand(options, currentMonitor ++ Map('targets -> value), tail) |
| 155 | + case "--agg" :: value :: tail if validateAgg(value) => cliMonitorsSubcommand(options, currentMonitor ++ Map('agg -> value), tail) |
| 156 | + case "--console" :: tail => cliMonitorsSubcommand(options, currentMonitor ++ Map('console -> "true"), tail) |
| 157 | + case "--file" :: value :: tail => cliMonitorsSubcommand(options, currentMonitor ++ Map('file -> value), tail) |
| 158 | + case "--chart" :: tail => cliMonitorsSubcommand(options, currentMonitor ++ Map('chart -> "true"), tail) |
| 159 | + case option :: tail => println(s"unknown monitor option $option"); sys.exit(1) |
| 160 | + } |
| 161 | + |
| 162 | + if(args.size == 0) { |
| 163 | + printHelp() |
| 164 | + sys.exit(1) |
| 165 | + } |
| 166 | + |
| 167 | + else { |
| 168 | + Seq("bash", "scripts/system.bash").! |
| 169 | + val (configuration, duration) = cli(List(), "3600", args.toList) |
| 170 | + |
| 171 | + for(powerMeterConf <- configuration) { |
| 172 | + val modules = powerMeterConf('modules).toString |
| 173 | + if(modules.contains("libpfm-core") || modules.contains("libpfm-core-process")) LibpfmHelper.init() |
| 174 | + |
| 175 | + val powerMeter = PowerMeter.loadModule(powerMeterConf('modules).toString: _*) |
| 176 | + powerMeters :+= powerMeter |
| 177 | + |
| 178 | + for(monitorConf <- powerMeterConf('monitors).asInstanceOf[List[Map[Symbol, Any]]]) { |
| 179 | + val frequency = monitorConf.getOrElse('frequency, "1000").toString.toInt.milliseconds |
| 180 | + val targets: Seq[Target] = monitorConf.getOrElse('targets, "").toString.toLowerCase |
| 181 | + val agg: Seq[Power] => Power = aggStrToAggFunction(monitorConf.getOrElse('agg, "max").toString.toLowerCase) |
| 182 | + val console = monitorConf.getOrElse('console, "").toString |
| 183 | + val file = monitorConf.getOrElse('file, "").toString |
| 184 | + val chart = monitorConf.getOrElse('chart, "").toString |
| 185 | + |
| 186 | + val monitor = powerMeter.monitor(frequency)(targets: _*)(agg) |
| 187 | + monitors :+= monitor |
| 188 | + |
| 189 | + if(console != "") { |
| 190 | + val consoleDisplay = new ConsoleDisplay() |
| 191 | + monitor.to(consoleDisplay) |
| 192 | + } |
| 193 | + |
| 194 | + if(file != "") { |
| 195 | + val fileDisplay = new FileDisplay(file) |
| 196 | + monitor.to(fileDisplay) |
| 197 | + } |
| 198 | + |
| 199 | + if(chart != "") { |
| 200 | + val chartDisplay = new JFreeChartDisplay() |
| 201 | + monitor.to(chartDisplay) |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + Thread.sleep(duration.toInt.seconds.toMillis) |
| 207 | + |
| 208 | + val isLibpfmInit = configuration.count(powerMeterConf => powerMeterConf('modules).toString.contains("libpfm-core") || powerMeterConf('modules).toString.contains("libpfm-core-process")) != 0 |
| 209 | + if(isLibpfmInit) LibpfmHelper.deinit() |
| 210 | + } |
| 211 | + |
| 212 | + shutdownHookThread.start() |
| 213 | + shutdownHookThread.join() |
| 214 | + shutdownHookThread.remove() |
| 215 | + sys.exit(0) |
| 216 | +} |
0 commit comments