1 | package schedframe.resources.computing.profiles.energy; |
---|
2 | |
---|
3 | import java.util.Properties; |
---|
4 | |
---|
5 | import org.apache.commons.logging.Log; |
---|
6 | import org.apache.commons.logging.LogFactory; |
---|
7 | |
---|
8 | import schedframe.events.Event; |
---|
9 | import schedframe.resources.computing.ComputingResource; |
---|
10 | import schedframe.resources.computing.extensions.Extension; |
---|
11 | import schedframe.resources.computing.extensions.ExtensionException; |
---|
12 | import schedframe.resources.computing.extensions.ExtensionType; |
---|
13 | import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputProfile; |
---|
14 | import schedframe.resources.computing.profiles.energy.airthroughput.ui.AirThroughputInterface; |
---|
15 | import schedframe.resources.computing.profiles.energy.power.PowerProfile; |
---|
16 | import schedframe.resources.computing.profiles.energy.power.ui.PowerInterface; |
---|
17 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
18 | |
---|
19 | public class EnergyExtension implements Extension{ |
---|
20 | |
---|
21 | private Log log = LogFactory.getLog(EnergyExtension.class); |
---|
22 | |
---|
23 | protected PowerInterface powerInterface; |
---|
24 | protected PowerProfile powerProfile; |
---|
25 | |
---|
26 | protected AirThroughputInterface airFlowInterface; |
---|
27 | protected AirThroughputProfile airFlowProfile; |
---|
28 | |
---|
29 | protected ComputingResource computingResource; |
---|
30 | |
---|
31 | public EnergyExtension(PowerInterface powerInterface, PowerProfile pp) { |
---|
32 | this.powerInterface = powerInterface; |
---|
33 | this.powerProfile = pp; |
---|
34 | } |
---|
35 | |
---|
36 | public EnergyExtension(PowerInterface powerInterface, PowerProfile powerProfile, |
---|
37 | AirThroughputInterface airFlowInterface, AirThroughputProfile airFlowProfile) { |
---|
38 | super(); |
---|
39 | this.powerInterface = powerInterface; |
---|
40 | this.powerProfile = powerProfile; |
---|
41 | this.airFlowInterface = airFlowInterface; |
---|
42 | this.airFlowProfile = airFlowProfile; |
---|
43 | } |
---|
44 | |
---|
45 | public boolean supportsEvent(Event event) { |
---|
46 | |
---|
47 | if(powerProfile == null || powerProfile.getEnergyEstimationPlugin() == null) |
---|
48 | return false; |
---|
49 | if(event.getType().getName().equals(EnergyEventType.POWER_STATE_CHANGED.getName())) |
---|
50 | return true; |
---|
51 | else if(event.getType().getName().equals(EnergyEventType.FREQUENCY_CHANGED.getName())) |
---|
52 | return true; |
---|
53 | else if(event.getType().getName().equals(EnergyEventType.TASK_STARTED.getName())) |
---|
54 | return true; |
---|
55 | else if(event.getType().getName().equals(EnergyEventType.TASK_FINISHED.getName())) |
---|
56 | return true; |
---|
57 | |
---|
58 | if(airFlowProfile == null) |
---|
59 | return false; |
---|
60 | if(event.getType().getName().equals(EnergyEventType.AIRFLOW_STATE_CHANGED.getName())) |
---|
61 | return true; |
---|
62 | |
---|
63 | else return false; |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | public void handleEvent(Event event) { |
---|
68 | EnergyEvent enEvent = (EnergyEvent)event; |
---|
69 | double power = 0; |
---|
70 | switch (enEvent.getType()) { |
---|
71 | |
---|
72 | case POWER_STATE_CHANGED: |
---|
73 | power = powerProfile.getEnergyEstimationPlugin().estimatePowerConsumption(enEvent, new JobRegistryImpl(computingResource.getName()), computingResource); |
---|
74 | /*if(resource instanceof ComputingNode){ |
---|
75 | ComputingNode node = (ComputingNode)resource; |
---|
76 | if(event.getData() instanceof PowerState){ |
---|
77 | PowerState newState = (PowerState)event.getData(); |
---|
78 | if(newState == PowerState.ON) { |
---|
79 | addToPowerUsageHistory(power+node.getPowerInterface().START_COST); |
---|
80 | addToPowerUsageHistory(DateTimeUtils.currentTimeMillis()+node.getPowerInterface().START_TIME, power); |
---|
81 | }else if(newState == PowerState.OFF){ |
---|
82 | addToPowerUsageHistory(power+node.getPowerInterface().SHUTDOWN_COST); |
---|
83 | addToPowerUsageHistory(DateTimeUtils.currentTimeMillis()+node.getPowerInterface().SHUTDOWN_TIME, power); |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | else */powerProfile.addToPowerUsageHistory(power); |
---|
88 | break; |
---|
89 | |
---|
90 | case FREQUENCY_CHANGED: |
---|
91 | power = powerProfile.getEnergyEstimationPlugin().estimatePowerConsumption(enEvent, new JobRegistryImpl(computingResource.getName()), computingResource); |
---|
92 | powerProfile.addToPowerUsageHistory(power); |
---|
93 | break; |
---|
94 | |
---|
95 | case TASK_STARTED: |
---|
96 | power = powerProfile.getEnergyEstimationPlugin().estimatePowerConsumption(enEvent, new JobRegistryImpl(computingResource.getName()), computingResource); |
---|
97 | powerProfile.addToPowerUsageHistory(power); |
---|
98 | break; |
---|
99 | |
---|
100 | case TASK_FINISHED: |
---|
101 | //System.out.println(this.resource.getName() + " - ENERGY EXTENSION: TASK FINISHED"); |
---|
102 | power = powerProfile.getEnergyEstimationPlugin().estimatePowerConsumption(enEvent, new JobRegistryImpl(computingResource.getName()), computingResource); |
---|
103 | //System.out.println(this.resource.getName() + " - ESTIMATED ENERGY:" + power); |
---|
104 | powerProfile.addToPowerUsageHistory(power); |
---|
105 | break; |
---|
106 | case AIRFLOW_STATE_CHANGED: |
---|
107 | System.out.println("====="); |
---|
108 | double airFlow = powerProfile.getEnergyEstimationPlugin().estimateAirThroughput(enEvent, new JobRegistryImpl(computingResource.getName()), computingResource); |
---|
109 | airFlowProfile.addToPowerUsageHistory(airFlow); |
---|
110 | power = powerProfile.getEnergyEstimationPlugin().estimatePowerConsumption(enEvent, new JobRegistryImpl(computingResource.getName()), computingResource); |
---|
111 | powerProfile.addToPowerUsageHistory(power); |
---|
112 | break; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | public void init(Properties properties) throws ExtensionException { |
---|
117 | // TODO Auto-generated method stub |
---|
118 | } |
---|
119 | |
---|
120 | public ExtensionType getType() { |
---|
121 | return ExtensionType.ENERGY_EXTENSION; |
---|
122 | } |
---|
123 | |
---|
124 | public ComputingResource getResource() { |
---|
125 | return computingResource; |
---|
126 | } |
---|
127 | |
---|
128 | public void setResource(ComputingResource compRes){ |
---|
129 | this.computingResource = compRes; |
---|
130 | } |
---|
131 | |
---|
132 | public PowerInterface getPowerInterface() { |
---|
133 | return powerInterface; |
---|
134 | } |
---|
135 | |
---|
136 | public PowerProfile getPowerProfile() { |
---|
137 | return powerProfile; |
---|
138 | } |
---|
139 | |
---|
140 | public AirThroughputInterface getAirThroughputInterface() { |
---|
141 | return airFlowInterface; |
---|
142 | } |
---|
143 | |
---|
144 | public AirThroughputProfile getAirFlowProfile() { |
---|
145 | return airFlowProfile; |
---|
146 | } |
---|
147 | |
---|
148 | } |
---|