package schedframe.resources.computing.profiles.energy.power; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.joda.time.DateTimeUtils; import schedframe.Parameters; import schedframe.resources.computing.profiles.energy.power.plugin.EnergyEstimationPlugin; public class PowerProfile { protected List powerUsage; protected EnergyEstimationPlugin energyEstimationPlugin; protected List supportedPowerStates; protected Map supportedPStates; protected Parameters parameters; public PowerProfile(EnergyEstimationPlugin energyEstimationPlugin, List powerStates) { this.energyEstimationPlugin = energyEstimationPlugin; this.supportedPowerStates = powerStates; this.powerUsage = new ArrayList(); initTransitionStates(); initDefaultPowerStates(); } public PowerProfile(EnergyEstimationPlugin energyEstimationPlugin, List powerStates, List pStates) { this.energyEstimationPlugin = energyEstimationPlugin; this.supportedPowerStates = powerStates; this.powerUsage = new ArrayList(); if(pStates.size() > 0) this.supportedPStates = new HashMap(); for(PState pState: pStates){ supportedPStates.put(pState.getName(), pState); } initTransitionStates(); initDefaultPowerStates(); } /*public void initPlugin(String energyEstimationPluginClassName){ if(energyEstimationPluginClassName != null) { energyEstimationPlugin = (EnergyEstimationPluginInterface) InstanceFactory.createInstance( energyEstimationPluginClassName, EnergyEstimationPluginInterface.class); if(energyEstimationPlugin == null){ energyEstimationPlugin = new DefaultEnergyEstimationPlugin(); log.info("Using default energy estimation plugin: " + DefaultEnergyEstimationPlugin.class.getName()); } else { energyEstimationPlugin.init(null); } } else { energyEstimationPlugin = new DefaultEnergyEstimationPlugin(); log.info("Using default energy estimation plugin: " + DefaultEnergyEstimationPlugin.class.getName()); } }*/ public EnergyEstimationPlugin getEnergyEstimationPlugin() { return energyEstimationPlugin; } public List getSupportedPowerStates() { if(supportedPowerStates == null) return new ArrayList(0); return supportedPowerStates; } public Map getSupportedPStates() { if(supportedPStates == null) return new HashMap(0); return supportedPStates; } public boolean addToPowerUsageHistory(double power) { if (powerUsage.size() == 0) { PowerUsage usage = new PowerUsage(DateTimeUtils.currentTimeMillis(), power); powerUsage.add(usage); return true; } boolean status = false; int lastIdx = powerUsage.size() - 1; double lastPower = powerUsage.get(lastIdx).getValue(); if (lastPower != power) { status = true; PowerUsage usage = powerUsage.get(lastIdx); long currentTime = DateTimeUtils.currentTimeMillis(); if (usage.getTimestamp() == currentTime) { usage.setValue(power); if(lastIdx > 0 && powerUsage.get(lastIdx - 1).getValue() == power) powerUsage.remove(usage); } else { usage = new PowerUsage(DateTimeUtils.currentTimeMillis(), power); powerUsage.add(usage); } } return status; } public List getPowerUsageHistory() { return powerUsage; } public void init(Parameters params){ this.parameters = params; } public Parameters getParameters() { return parameters; } protected void initTransitionStates(){ if(supportedPowerStates != null){ List transitionPowerStates = new ArrayList(0); for(PowerState ps: supportedPowerStates){ for(Transition t: ps.getTransitions()){ transitionPowerStates.add(new PowerState(t.getName(), t.getPowerUsage(), new ArrayList(0))); } } supportedPowerStates.addAll(transitionPowerStates); } } protected void initDefaultPowerStates(){ boolean supportsON = false; boolean supportsOFF = false; if(supportedPowerStates == null){ supportedPowerStates = new ArrayList(2); } for(PowerState ps: supportedPowerStates){ if(ps.getName().equals(StandardPowerStateName.ON)){ supportsON = true; } else if (ps.getName().equals(StandardPowerStateName.OFF)){ supportsOFF = true; } } if(!supportsON){ supportedPowerStates.add(new PowerState(StandardPowerStateName.ON, -1, new ArrayList(0))); } if(!supportsOFF){ supportedPowerStates.add(new PowerState(StandardPowerStateName.OFF, -1, new ArrayList(0))); } } }