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<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.supportedPowerStates = powerStates; |
---|
27 | this.powerUsage = new ArrayList<PowerUsage>(); |
---|
28 | initTransitionStates(); |
---|
29 | initDefaultPowerStates(); |
---|
30 | } |
---|
31 | |
---|
32 | public PowerProfile(EnergyEstimationPlugin energyEstimationPlugin, List<PowerState> powerStates, List<PState> pStates) { |
---|
33 | this.energyEstimationPlugin = energyEstimationPlugin; |
---|
34 | this.supportedPowerStates = powerStates; |
---|
35 | this.powerUsage = new ArrayList<PowerUsage>(); |
---|
36 | if(pStates.size() > 0) |
---|
37 | this.supportedPStates = new HashMap<String, PState>(); |
---|
38 | for(PState pState: pStates){ |
---|
39 | supportedPStates.put(pState.getName(), pState); |
---|
40 | } |
---|
41 | initTransitionStates(); |
---|
42 | initDefaultPowerStates(); |
---|
43 | } |
---|
44 | |
---|
45 | /*public void initPlugin(String energyEstimationPluginClassName){ |
---|
46 | if(energyEstimationPluginClassName != null) { |
---|
47 | energyEstimationPlugin = (EnergyEstimationPluginInterface) InstanceFactory.createInstance( |
---|
48 | energyEstimationPluginClassName, EnergyEstimationPluginInterface.class); |
---|
49 | if(energyEstimationPlugin == null){ |
---|
50 | energyEstimationPlugin = new DefaultEnergyEstimationPlugin(); |
---|
51 | log.info("Using default energy estimation plugin: " + DefaultEnergyEstimationPlugin.class.getName()); |
---|
52 | |
---|
53 | } else { |
---|
54 | energyEstimationPlugin.init(null); |
---|
55 | } |
---|
56 | } else { |
---|
57 | energyEstimationPlugin = new DefaultEnergyEstimationPlugin(); |
---|
58 | log.info("Using default energy estimation plugin: " + DefaultEnergyEstimationPlugin.class.getName()); |
---|
59 | } |
---|
60 | }*/ |
---|
61 | |
---|
62 | public EnergyEstimationPlugin getEnergyEstimationPlugin() { |
---|
63 | return energyEstimationPlugin; |
---|
64 | } |
---|
65 | |
---|
66 | public List<PowerState> getSupportedPowerStates() { |
---|
67 | if(supportedPowerStates == null) |
---|
68 | return new ArrayList<PowerState>(0); |
---|
69 | return supportedPowerStates; |
---|
70 | } |
---|
71 | |
---|
72 | public Map<String, PState> getSupportedPStates() { |
---|
73 | if(supportedPStates == null) |
---|
74 | return new HashMap<String, PState>(0); |
---|
75 | return supportedPStates; |
---|
76 | } |
---|
77 | |
---|
78 | public boolean addToPowerUsageHistory(double power) { |
---|
79 | |
---|
80 | if (powerUsage.size() == 0) { |
---|
81 | PowerUsage usage = new PowerUsage(DateTimeUtils.currentTimeMillis(), power); |
---|
82 | powerUsage.add(usage); |
---|
83 | return true; |
---|
84 | } |
---|
85 | |
---|
86 | boolean status = false; |
---|
87 | int lastIdx = powerUsage.size() - 1; |
---|
88 | double lastPower = powerUsage.get(lastIdx).getValue(); |
---|
89 | if (lastPower != power) { |
---|
90 | status = true; |
---|
91 | PowerUsage usage = powerUsage.get(lastIdx); |
---|
92 | long currentTime = DateTimeUtils.currentTimeMillis(); |
---|
93 | if (usage.getTimestamp() == currentTime) { |
---|
94 | usage.setValue(power); |
---|
95 | if(lastIdx > 0 && powerUsage.get(lastIdx - 1).getValue() == power) |
---|
96 | powerUsage.remove(usage); |
---|
97 | } else { |
---|
98 | usage = new PowerUsage(DateTimeUtils.currentTimeMillis(), power); |
---|
99 | powerUsage.add(usage); |
---|
100 | } |
---|
101 | } |
---|
102 | return status; |
---|
103 | } |
---|
104 | |
---|
105 | public List<PowerUsage> getPowerUsageHistory() { |
---|
106 | return powerUsage; |
---|
107 | } |
---|
108 | |
---|
109 | public void init(Parameters params){ |
---|
110 | this.parameters = params; |
---|
111 | } |
---|
112 | |
---|
113 | public Parameters getParameters() { |
---|
114 | return parameters; |
---|
115 | } |
---|
116 | |
---|
117 | protected void initTransitionStates(){ |
---|
118 | if(supportedPowerStates != null){ |
---|
119 | List<PowerState> transitionPowerStates = new ArrayList<PowerState>(0); |
---|
120 | for(PowerState ps: supportedPowerStates){ |
---|
121 | for(Transition t: ps.getTransitions()){ |
---|
122 | transitionPowerStates.add(new PowerState(t.getName(), t.getPowerUsage(), new ArrayList<Transition>(0))); |
---|
123 | } |
---|
124 | } |
---|
125 | supportedPowerStates.addAll(transitionPowerStates); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | protected void initDefaultPowerStates(){ |
---|
130 | boolean supportsON = false; |
---|
131 | boolean supportsOFF = false; |
---|
132 | if(supportedPowerStates == null){ |
---|
133 | supportedPowerStates = new ArrayList<PowerState>(2); |
---|
134 | } |
---|
135 | for(PowerState ps: supportedPowerStates){ |
---|
136 | if(ps.getName().equals(StandardPowerStateName.ON)){ |
---|
137 | supportsON = true; |
---|
138 | } else if (ps.getName().equals(StandardPowerStateName.OFF)){ |
---|
139 | supportsOFF = true; |
---|
140 | } |
---|
141 | } |
---|
142 | if(!supportsON){ |
---|
143 | supportedPowerStates.add(new PowerState(StandardPowerStateName.ON, -1, new ArrayList<Transition>(0))); |
---|
144 | } |
---|
145 | if(!supportsOFF){ |
---|
146 | supportedPowerStates.add(new PowerState(StandardPowerStateName.OFF, -1, new ArrayList<Transition>(0))); |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|