<commit_before>/**
 * Copyright (C) 2012 Inria, University Lille 1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 * Contact: <EMAIL>
 */
package fr.inria.powerapi.example.demo2
import scala.collection.JavaConversions

import com.typesafe.config.ConfigFactory

import akka.util.duration._
import fr.inria.powerapi.core.Listener
import fr.inria.powerapi.core.Process
import fr.inria.powerapi.core.TickSubscription
import fr.inria.powerapi.formula.cpu.api.CpuFormulaValues
import fr.inria.powerapi.formula.cpu.general.CpuFormula
import fr.inria.powerapi.formula.disk.api.DiskFormulaValues
import fr.inria.powerapi.formula.disk.single.DiskFormula
import fr.inria.powerapi.library.PowerAPI
import fr.inria.powerapi.sensor.cpu.api.CpuSensorValues
import fr.inria.powerapi.sensor.cpu.proc.CpuSensor
import fr.inria.powerapi.sensor.disk.proc.DiskSensor

class Demo2Listener extends Listener {
  lazy val cpuUsageCache = collection.mutable.HashMap[TickSubscription, CpuSensorValues]()
  lazy val cache = collection.mutable.HashMap[Long, Map[String, Double]]()

  def messagesToListen = Array(classOf[CpuSensorValues], classOf[CpuFormulaValues], classOf[DiskFormulaValues])

  def acquire = {
    case cpuSensorValues: CpuSensorValues => process(cpuSensorValues)
    case cpuFormulaValues: CpuFormulaValues => process(cpuFormulaValues)
    case diskFormulaValues: DiskFormulaValues => process(diskFormulaValues)
  }

  def process(now: CpuSensorValues) {

    def usage(old: CpuSensorValues, now: CpuSensorValues) = {
      val processUsage = (now.processElapsedTime.time - old.processElapsedTime.time).toDouble
      val globalUsage = (now.globalElapsedTime.time - old.globalElapsedTime.time).toDouble
      if (globalUsage == 0) {
        0.0
      } else {
        math.max(0.0, processUsage / globalUsage)
      }
    }

    val entry = cache getOrElse (now.tick.timestamp, Map[String, Double]())
    cache += now.tick.timestamp -> (entry + ("cpu usage" -> usage(cpuUsageCache getOrElse (now.tick.subscription, now), now)))
    cpuUsageCache += (now.tick.subscription -> now)
  }

  def process(cpuFormulaValues: CpuFormulaValues) {
    val entry = cache getOrElse (cpuFormulaValues.tick.timestamp, Map[String, Double]())
    cache += cpuFormulaValues.tick.timestamp -> (entry + ("cpu" -> cpuFormulaValues.energy.power))
  }

  def process(diskFormulaValues: DiskFormulaValues) {
    val entry = cache getOrElse (diskFormulaValues.tick.timestamp, Map[String, Double]())
    cache += diskFormulaValues.tick.timestamp -> (entry + ("disk" -> diskFormulaValues.energy.power))
  }

  def display(timestamp: Long) {
    val entry = cache getOrElse (timestamp, Map[String, Double]())
    if (entry.size == 3) {
      Chart.process(Map("cpu usage" -> entry("cpu usage"), "total" -> (entry("cpu") + entry("disk"))), timestamp)
    }
  }
}

object Demo2 extends App {
  lazy val conf = ConfigFactory.load
  lazy val pids = JavaConversions.asScalaBuffer(conf.getIntList("powerapi.pids")).toList

  Array(
    classOf[CpuSensor],
    classOf[CpuFormula],
    classOf[DiskSensor],
    classOf[DiskFormula]).foreach(PowerAPI.startEnergyModule(_))

  pids.foreach(pid => PowerAPI.startMonitoring(Process(pid), 500 milliseconds, classOf[Demo2Listener]))
  Thread.sleep((2 hours).toMillis)
  pids.foreach(pid => PowerAPI.stopMonitoring(Process(pid), 500 milliseconds, classOf[Demo2Listener]))<commit_msg>ADAM evaluation:   - Set Demo2 duration from 500 milliseconds to 1 second<commit_after>/**
 * Copyright (C) 2012 Inria, University Lille 1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 * Contact: <EMAIL>
 */
package fr.inria.powerapi.example.demo2
import scala.collection.JavaConversions

import com.typesafe.config.ConfigFactory

import akka.util.duration._
import fr.inria.powerapi.core.Listener
import fr.inria.powerapi.core.Process
import fr.inria.powerapi.core.TickSubscription
import fr.inria.powerapi.formula.cpu.api.CpuFormulaValues
import fr.inria.powerapi.formula.cpu.general.CpuFormula
import fr.inria.powerapi.formula.disk.api.DiskFormulaValues
import fr.inria.powerapi.formula.disk.single.DiskFormula
import fr.inria.powerapi.library.PowerAPI
import fr.inria.powerapi.sensor.cpu.api.CpuSensorValues
import fr.inria.powerapi.sensor.cpu.proc.CpuSensor
import fr.inria.powerapi.sensor.disk.proc.DiskSensor

class Demo2Listener extends Listener {
  lazy val cpuUsageCache = collection.mutable.HashMap[TickSubscription, CpuSensorValues]()
  lazy val cache = collection.mutable.HashMap[Long, Map[String, Double]]()

  def messagesToListen = Array(classOf[CpuSensorValues], classOf[CpuFormulaValues], classOf[DiskFormulaValues])

  def acquire = {
    case cpuSensorValues: CpuSensorValues => process(cpuSensorValues)
    case cpuFormulaValues: CpuFormulaValues => process(cpuFormulaValues)
    case diskFormulaValues: DiskFormulaValues => process(diskFormulaValues)
  }

  def process(now: CpuSensorValues) {

    def usage(old: CpuSensorValues, now: CpuSensorValues) = {
      val processUsage = (now.processElapsedTime.time - old.processElapsedTime.time).toDouble
      val globalUsage = (now.globalElapsedTime.time - old.globalElapsedTime.time).toDouble
      if (globalUsage == 0) {
        0.0
      } else {
        math.max(0.0, processUsage / globalUsage)
      }
    }

    val entry = cache getOrElse (now.tick.timestamp, Map[String, Double]())
    cache += now.tick.timestamp -> (entry + ("cpu usage" -> usage(cpuUsageCache getOrElse (now.tick.subscription, now), now)))
    cpuUsageCache += (now.tick.subscription -> now)
  }

  def process(cpuFormulaValues: CpuFormulaValues) {
    val entry = cache getOrElse (cpuFormulaValues.tick.timestamp, Map[String, Double]())
    cache += cpuFormulaValues.tick.timestamp -> (entry + ("cpu" -> cpuFormulaValues.energy.power))
  }

  def process(diskFormulaValues: DiskFormulaValues) {
    val entry = cache getOrElse (diskFormulaValues.tick.timestamp, Map[String, Double]())
    cache += diskFormulaValues.tick.timestamp -> (entry + ("disk" -> diskFormulaValues.energy.power))
  }

  def display(timestamp: Long) {
    val entry = cache getOrElse (timestamp, Map[String, Double]())
    if (entry.size == 3) {
      Chart.process(Map("cpu usage" -> entry("cpu usage"), "total" -> (entry("cpu") + entry("disk"))), timestamp)
    }
  }
}

object Demo2 extends App {
  lazy val conf = ConfigFactory.load
  lazy val pids = JavaConversions.asScalaBuffer(conf.getIntList("powerapi.pids")).toList

  Array(
    classOf[CpuSensor],
    classOf[CpuFormula],
    classOf[DiskSensor],
    classOf[DiskFormula]).foreach(PowerAPI.startEnergyModule(_))

  pids.foreach(pid => PowerAPI.startMonitoring(Process(pid), 1 second, classOf[Demo2Listener]))
  Thread.sleep((2 hours).toMillis)
  pids.foreach(pid => PowerAPI.stopMonitoring(Process(pid), 1 second, classOf[Demo2Listener]))
{'fraction_non_alphanumeric': 0.10861606053209666, 'fraction_numerical': 0.009397119843788137, 'mean_word_length': 4.518518518518518, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1090745', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2490, 'n_words': 637}