1 | package example.localplugin; |
---|
2 | |
---|
3 | import gridsim.dcworms.DCWormsTags; |
---|
4 | |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.Collections; |
---|
7 | import java.util.Comparator; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.util.HashSet; |
---|
10 | import java.util.List; |
---|
11 | import java.util.Map; |
---|
12 | import java.util.Set; |
---|
13 | |
---|
14 | import schedframe.events.scheduling.SchedulingEvent; |
---|
15 | import schedframe.resources.ResourceStatus; |
---|
16 | import schedframe.resources.StandardResourceType; |
---|
17 | import schedframe.resources.computing.ComputingResource; |
---|
18 | import schedframe.resources.computing.Node; |
---|
19 | import schedframe.resources.computing.Processor; |
---|
20 | import schedframe.resources.computing.profiles.energy.airthroughput.AirflowState; |
---|
21 | import schedframe.resources.computing.profiles.energy.airthroughput.AirflowStateName; |
---|
22 | import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName; |
---|
23 | import schedframe.resources.computing.profiles.energy.power.PState; |
---|
24 | import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; |
---|
25 | import schedframe.resources.devices.Device; |
---|
26 | import schedframe.resources.devices.Fan; |
---|
27 | import schedframe.resources.units.ProcessingElements; |
---|
28 | import schedframe.resources.units.ResourceUnit; |
---|
29 | import schedframe.resources.units.ResourceUnitName; |
---|
30 | import schedframe.resources.units.StandardResourceUnitName; |
---|
31 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
32 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
33 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
34 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
35 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
36 | import schedframe.scheduling.plugin.ModuleList; |
---|
37 | import schedframe.scheduling.queue.TaskQueue; |
---|
38 | import schedframe.scheduling.queue.TaskQueueList; |
---|
39 | import schedframe.scheduling.tasks.TaskInterface; |
---|
40 | |
---|
41 | public class Cluster_FCFSBF_ConsolidationHighPerf_NodePowMan_Plugin extends BaseLocalSchedulingPlugin { |
---|
42 | |
---|
43 | public Cluster_FCFSBF_ConsolidationHighPerf_NodePowMan_Plugin () { |
---|
44 | } |
---|
45 | |
---|
46 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
47 | ResourceManager resManager, ModuleList modules) { |
---|
48 | |
---|
49 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
50 | SchedulingPlan plan = new SchedulingPlan(); |
---|
51 | // choose the events types to serve. |
---|
52 | // Different actions for different events are possible. |
---|
53 | switch (event.getType()) { |
---|
54 | case START_TASK_EXECUTION: |
---|
55 | case TASK_FINISHED: |
---|
56 | // our tasks are placed only in first queue (see |
---|
57 | // BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
58 | TaskQueue q = queues.get(0); |
---|
59 | // check all tasks in queue |
---|
60 | |
---|
61 | Set<Node> selectedNodes = new HashSet<Node>(); |
---|
62 | |
---|
63 | for (int i = 0; i < q.size(); i++) { |
---|
64 | TaskInterface<?> task = q.get(i); |
---|
65 | // if status of the tasks in READY |
---|
66 | if (task.getStatus() == DCWormsTags.READY) { |
---|
67 | |
---|
68 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
69 | if (choosenResources != null) { |
---|
70 | addToSchedulingPlan(plan, task, choosenResources); |
---|
71 | ProcessingElements pe = (ProcessingElements) choosenResources.get(StandardResourceUnitName.PE); |
---|
72 | Node node = (Node) pe.get(0).getParent(); |
---|
73 | selectedNodes.add(node); |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | List<Node> nodes = resourceManager.getNodes(); |
---|
78 | nodes.removeAll(selectedNodes); |
---|
79 | turnOffIdleNodes(nodes); |
---|
80 | for(Node node: resourceManager.getNodes()){ |
---|
81 | for(Device device: node.getParent().getResourceCharacteristic().getDevices()){ |
---|
82 | if(device.getType().equals(StandardResourceType.Fan)){ |
---|
83 | Fan fan = (Fan) device; |
---|
84 | adjustFanSpeed(fan, selectedNodes); |
---|
85 | break; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | break; |
---|
90 | } |
---|
91 | return plan; |
---|
92 | } |
---|
93 | |
---|
94 | @SuppressWarnings("unchecked") |
---|
95 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task){ |
---|
96 | |
---|
97 | List<Node> nodes = resourceManager.getNodes(); |
---|
98 | List<Node> availableNodes = findSuitableNodes(task, nodes); |
---|
99 | Node node; |
---|
100 | if(availableNodes.size() == 0){ |
---|
101 | |
---|
102 | nodes = (List<Node>) resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Node, ResourceStatus.UNAVAILABLE); |
---|
103 | |
---|
104 | Collections.sort(nodes, new PerformanceComparator()); |
---|
105 | node = turnOnFirstNode(nodes, task); |
---|
106 | if(node == null) |
---|
107 | return null; |
---|
108 | }else{ |
---|
109 | Collections.sort(availableNodes, new PerformanceComparator()); |
---|
110 | node = availableNodes.get(0); |
---|
111 | } |
---|
112 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
113 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
114 | int cpuRequest; |
---|
115 | try { |
---|
116 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
117 | } catch (NoSuchFieldException e) { |
---|
118 | cpuRequest = 0; |
---|
119 | } |
---|
120 | for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) { |
---|
121 | if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) { |
---|
122 | choosenResources.add(node.getProcessors().get(i)); |
---|
123 | cpuRequest--; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | ProcessingElements result = new ProcessingElements(node.getFullName()); |
---|
128 | result.addAll(choosenResources); |
---|
129 | map.put(StandardResourceUnitName.PE, result); |
---|
130 | return map; |
---|
131 | } |
---|
132 | |
---|
133 | private List<Node> findSuitableNodes(TaskInterface<?> task, List<Node> nodes) { |
---|
134 | int cpuRequest; |
---|
135 | try { |
---|
136 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
137 | } catch (NoSuchFieldException e) { |
---|
138 | cpuRequest = 1; |
---|
139 | } |
---|
140 | |
---|
141 | List<Node> suitableNodes = new ArrayList<Node>(); |
---|
142 | for(Node node: nodes){ |
---|
143 | if(node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
144 | suitableNodes.add(node); |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|
148 | return suitableNodes; |
---|
149 | } |
---|
150 | |
---|
151 | private Node turnOnFirstNode(List<Node> nodes, TaskInterface<?> task){ |
---|
152 | Node startedNode = null; |
---|
153 | |
---|
154 | int cpuRequest; |
---|
155 | try { |
---|
156 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
157 | } catch (NoSuchFieldException e) { |
---|
158 | cpuRequest = 0; |
---|
159 | } |
---|
160 | |
---|
161 | for(Node node: nodes){ |
---|
162 | |
---|
163 | if (cpuRequest != 0) { |
---|
164 | |
---|
165 | List<Processor> processors = node.getProcessors(); |
---|
166 | if (processors.size() < cpuRequest) { |
---|
167 | if(processors.size() == 0){ |
---|
168 | if(node.getProcessors().size() < cpuRequest) |
---|
169 | continue; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | int freeProcessor = 0; |
---|
174 | for(Processor processor: processors){ |
---|
175 | if(processor.getStatus() == ResourceStatus.FREE || processor.getStatus() == ResourceStatus.UNAVAILABLE) |
---|
176 | freeProcessor++; |
---|
177 | } |
---|
178 | |
---|
179 | if(freeProcessor < cpuRequest) |
---|
180 | continue; |
---|
181 | else { |
---|
182 | node.getPowerInterface().setPowerState(StandardPowerStateName.ON); |
---|
183 | startedNode = node; |
---|
184 | break; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | return startedNode; |
---|
189 | } |
---|
190 | private void turnOffIdleNodes(List<Node> nodes){ |
---|
191 | for(Node node : nodes){ |
---|
192 | int freeProcessors = 0; |
---|
193 | for(Processor proc: node.getProcessors()){ |
---|
194 | if(proc.getStatus() == ResourceStatus.FREE) |
---|
195 | freeProcessors++; |
---|
196 | } |
---|
197 | |
---|
198 | if(freeProcessors == node.getProcessors().size()) { |
---|
199 | node.getPowerInterface().setPowerState(StandardPowerStateName.OFF); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | class PerformanceComparator implements Comparator<Node>{ |
---|
205 | |
---|
206 | public int compare(Node node1, Node node2){ |
---|
207 | double node1Rank = Double.MIN_VALUE; |
---|
208 | double node2Rank = Double.MIN_VALUE; |
---|
209 | |
---|
210 | double node1Speed = 0; |
---|
211 | for(Processor proc: node1.getProcessors()){ |
---|
212 | node1Speed = proc.getMIPS(); |
---|
213 | } |
---|
214 | node1Rank = node1Speed / node1.getProcessors().size(); |
---|
215 | |
---|
216 | double node2Speed = 0; |
---|
217 | for(Processor proc: node2.getProcessors()){ |
---|
218 | node2Speed = proc.getMIPS(); |
---|
219 | } |
---|
220 | node2Rank = node2Speed / node2.getProcessors().size(); |
---|
221 | |
---|
222 | if(node1Rank == node2Rank){ |
---|
223 | node1Rank = Double.MIN_VALUE; |
---|
224 | node2Rank = Double.MIN_VALUE; |
---|
225 | PState pState; |
---|
226 | for(Processor proc: node1.getProcessors()){ |
---|
227 | if(proc.getPowerInterface() != null) { |
---|
228 | pState = proc.getPowerInterface().getLowestPState(); |
---|
229 | if(pState != null && pState.getFrequency() > node1Rank){ |
---|
230 | node1Rank = proc.getPowerInterface().getLowestPState().getFrequency(); |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | for(Processor proc: node2.getProcessors()){ |
---|
235 | if(proc.getPowerInterface() != null) { |
---|
236 | pState = proc.getPowerInterface().getLowestPState(); |
---|
237 | if(pState != null && pState.getFrequency() > node2Rank){ |
---|
238 | node2Rank = proc.getPowerInterface().getLowestPState().getFrequency(); |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | if(node1Rank < node2Rank) |
---|
245 | return 1; |
---|
246 | else if (node1Rank > node2Rank) |
---|
247 | return -1; |
---|
248 | else return 0; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | private void adjustFanSpeed(Fan fan, Set<Node> selectedNodes){ |
---|
253 | AirflowStateName newState; |
---|
254 | double totalLoad = 0; |
---|
255 | double loadLevel; |
---|
256 | ComputingResource compRes = fan.getComputingResource(); |
---|
257 | for(ComputingResource n: compRes.getChildren()){ |
---|
258 | if(fan.getChilledResources().contains(n.getFullName())){ |
---|
259 | if(selectedNodes.contains(n)){ |
---|
260 | totalLoad = totalLoad + 100; |
---|
261 | } else { |
---|
262 | totalLoad = totalLoad + n.getLoadInterface().getRecentUtilization().getValue(); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | loadLevel = totalLoad / compRes.getChildren().size(); |
---|
267 | |
---|
268 | double highestLoadLevel = 0; |
---|
269 | |
---|
270 | if(fan.getAirflowInterface().supportAirflowState(new CustomAirflowStateName("ON_" + new Double(loadLevel).intValue()))){ |
---|
271 | newState = new CustomAirflowStateName("ON_" + new Double(loadLevel).intValue()); |
---|
272 | } else { |
---|
273 | for(AirflowState airflowState: fan.getAirflowInterface().getSupportedAirflowStates()){ |
---|
274 | Double load; |
---|
275 | try{ |
---|
276 | load = Double.valueOf(airflowState.getName().getLabel().substring(3)); |
---|
277 | }catch (Exception e){ |
---|
278 | continue; |
---|
279 | } |
---|
280 | if(highestLoadLevel < load){ |
---|
281 | highestLoadLevel = load; |
---|
282 | } |
---|
283 | } |
---|
284 | if(loadLevel == 0){ |
---|
285 | newState = new CustomAirflowStateName("OFF"); |
---|
286 | } else { |
---|
287 | |
---|
288 | double higherLoadLevel = highestLoadLevel; |
---|
289 | |
---|
290 | for(AirflowState airflowState: fan.getAirflowInterface().getSupportedAirflowStates()){ |
---|
291 | Double load; |
---|
292 | try{ |
---|
293 | load = Double.valueOf(airflowState.getName().getLabel().substring(3)); |
---|
294 | }catch (Exception e){ |
---|
295 | continue; |
---|
296 | } |
---|
297 | if(loadLevel < load){ |
---|
298 | higherLoadLevel = load; |
---|
299 | } |
---|
300 | } |
---|
301 | newState = new CustomAirflowStateName("ON_" + Double.valueOf(higherLoadLevel).intValue()); |
---|
302 | } |
---|
303 | } |
---|
304 | fan.getAirflowInterface().setAirflowState(newState); |
---|
305 | } |
---|
306 | |
---|
307 | } |
---|