1 | package schedframe.resources.computing.profiles.energy.power; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Map; |
---|
7 | |
---|
8 | import org.joda.time.DateTimeUtils; |
---|
9 | |
---|
10 | import schedframe.Parameters; |
---|
11 | import schedframe.resources.computing.profiles.energy.power.plugin.EnergyEstimationPlugin; |
---|
12 | |
---|
13 | public class PowerProfile { |
---|
14 | |
---|
15 | protected List<PowerUsage> powerUsage; |
---|
16 | protected EnergyEstimationPlugin energyEstimationPlugin; |
---|
17 | |
---|
18 | protected List<schedframe.resources.computing.profiles.energy.power.PowerState> supportedPowerStates; |
---|
19 | protected Map<String, PState> supportedPStates; |
---|
20 | |
---|
21 | protected Parameters parameters; |
---|
22 | |
---|
23 | |
---|
24 | public PowerProfile(EnergyEstimationPlugin energyEstimationPlugin, List<PowerState> powerStates) { |
---|
25 | this.energyEstimationPlugin = energyEstimationPlugin; |
---|
26 | this.powerUsage = new ArrayList<PowerUsage>(); |
---|
27 | this.supportedPowerStates = powerStates; |
---|
28 | } |
---|
29 | |
---|
30 | public PowerProfile(EnergyEstimationPlugin energyEstimationPlugin, List<PowerState> powerStates, List<PState> pStates) { |
---|
31 | this.energyEstimationPlugin = energyEstimationPlugin; |
---|
32 | this.supportedPowerStates = powerStates; |
---|
33 | this.powerUsage = new ArrayList<PowerUsage>(); |
---|
34 | if(pStates.size() > 0) |
---|
35 | this.supportedPStates = new HashMap<String, PState>(); |
---|
36 | for(PState pState: pStates){ |
---|
37 | supportedPStates.put(pState.getName(), pState); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | /*public void initPlugin(String energyEstimationPluginClassName){ |
---|
42 | if(energyEstimationPluginClassName != null) { |
---|
43 | energyEstimationPlugin = (EnergyEstimationPluginInterface) InstanceFactory.createInstance( |
---|
44 | energyEstimationPluginClassName, EnergyEstimationPluginInterface.class); |
---|
45 | if(energyEstimationPlugin == null){ |
---|
46 | energyEstimationPlugin = new DefaultEnergyEstimationPlugin(); |
---|
47 | log.info("Using default energy estimation plugin: " + DefaultEnergyEstimationPlugin.class.getName()); |
---|
48 | |
---|
49 | } else { |
---|
50 | energyEstimationPlugin.init(null); |
---|
51 | } |
---|
52 | } else { |
---|
53 | energyEstimationPlugin = new DefaultEnergyEstimationPlugin(); |
---|
54 | log.info("Using default energy estimation plugin: " + DefaultEnergyEstimationPlugin.class.getName()); |
---|
55 | } |
---|
56 | }*/ |
---|
57 | |
---|
58 | public EnergyEstimationPlugin getEnergyEstimationPlugin() { |
---|
59 | return energyEstimationPlugin; |
---|
60 | } |
---|
61 | |
---|
62 | public List<schedframe.resources.computing.profiles.energy.power.PowerState> getSupportedPowerStates() throws NoSuchFieldException{ |
---|
63 | if(supportedPowerStates == null) |
---|
64 | throw new NoSuchFieldException("Supported power states are not defined."); |
---|
65 | return supportedPowerStates; |
---|
66 | } |
---|
67 | |
---|
68 | public Map<String, PState> getSupportedPStates() throws NoSuchFieldException { |
---|
69 | if(supportedPStates == null) |
---|
70 | throw new NoSuchFieldException("Supported p-states are not defined."); |
---|
71 | return supportedPStates; |
---|
72 | } |
---|
73 | |
---|
74 | public void addToPowerUsageHistory(double power) { |
---|
75 | |
---|
76 | if (powerUsage.size() == 0) { |
---|
77 | PowerUsage usage = new PowerUsage(DateTimeUtils.currentTimeMillis(), power); |
---|
78 | powerUsage.add(usage); |
---|
79 | return; |
---|
80 | } |
---|
81 | |
---|
82 | int lastIdx = powerUsage.size() - 1; |
---|
83 | double lastPower = powerUsage.get(lastIdx).getValue(); |
---|
84 | if (lastPower != power) { |
---|
85 | PowerUsage usage = powerUsage.get(lastIdx); |
---|
86 | long currentTime = DateTimeUtils.currentTimeMillis(); |
---|
87 | if (usage.getTimestamp() == currentTime) { |
---|
88 | usage.setValue(power); |
---|
89 | if(lastIdx > 0 && powerUsage.get(lastIdx - 1).getValue() == power) |
---|
90 | powerUsage.remove(usage); |
---|
91 | } else { |
---|
92 | usage = new PowerUsage(DateTimeUtils.currentTimeMillis(), power); |
---|
93 | powerUsage.add(usage); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | public List<PowerUsage> getPowerUsageHistory() { |
---|
99 | return powerUsage; |
---|
100 | } |
---|
101 | |
---|
102 | public void init(Parameters params){ |
---|
103 | this.parameters = params; |
---|
104 | } |
---|
105 | |
---|
106 | public Parameters getParameters() { |
---|
107 | return parameters; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | } |
---|