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.List; |
---|
10 | import java.util.Map; |
---|
11 | |
---|
12 | import schedframe.events.scheduling.SchedulingEvent; |
---|
13 | import schedframe.resources.ResourceStatus; |
---|
14 | import schedframe.resources.StandardResourceType; |
---|
15 | import schedframe.resources.computing.ComputingResource; |
---|
16 | import schedframe.resources.computing.Node; |
---|
17 | import schedframe.resources.computing.Processor; |
---|
18 | import schedframe.resources.computing.profiles.energy.airthroughput.StandardAirflowStateName; |
---|
19 | import schedframe.resources.computing.profiles.energy.power.PState; |
---|
20 | import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; |
---|
21 | import schedframe.resources.devices.Device; |
---|
22 | import schedframe.resources.units.ProcessingElements; |
---|
23 | import schedframe.resources.units.ResourceUnit; |
---|
24 | import schedframe.resources.units.ResourceUnitName; |
---|
25 | import schedframe.resources.units.StandardResourceUnitName; |
---|
26 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
27 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
28 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
29 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
30 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
31 | import schedframe.scheduling.plugin.ModuleList; |
---|
32 | import schedframe.scheduling.queue.TaskQueue; |
---|
33 | import schedframe.scheduling.queue.TaskQueueList; |
---|
34 | import schedframe.scheduling.tasks.TaskInterface; |
---|
35 | |
---|
36 | public class Cluster_FCFSBF_ConsolidationLowPower_NodePowMan_Plugin extends BaseLocalSchedulingPlugin { |
---|
37 | |
---|
38 | public Cluster_FCFSBF_ConsolidationLowPower_NodePowMan_Plugin () { |
---|
39 | } |
---|
40 | |
---|
41 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
42 | ResourceManager resManager, ModuleList modules) { |
---|
43 | |
---|
44 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
45 | SchedulingPlan plan = new SchedulingPlan(); |
---|
46 | // choose the events types to serve. |
---|
47 | // Different actions for different events are possible. |
---|
48 | switch (event.getType()) { |
---|
49 | case START_TASK_EXECUTION: |
---|
50 | case TASK_FINISHED: |
---|
51 | // our tasks are placed only in first queue (see |
---|
52 | // BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
53 | TaskQueue q = queues.get(0); |
---|
54 | // check all tasks in queue |
---|
55 | |
---|
56 | List<Node> notSelectedNodes = resourceManager.getNodes(); |
---|
57 | |
---|
58 | for (int i = 0; i < q.size(); i++) { |
---|
59 | TaskInterface<?> task = q.get(i); |
---|
60 | // if status of the tasks in READY |
---|
61 | if (task.getStatus() == DCWormsTags.READY) { |
---|
62 | |
---|
63 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
64 | if (choosenResources != null) { |
---|
65 | addToSchedulingPlan(plan, task, choosenResources); |
---|
66 | ProcessingElements pe = (ProcessingElements) choosenResources.get(StandardResourceUnitName.PE); |
---|
67 | Node node = (Node) pe.get(0).getParent(); |
---|
68 | notSelectedNodes.remove(node); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | turnOffIdleNodes(notSelectedNodes); |
---|
73 | break; |
---|
74 | } |
---|
75 | return plan; |
---|
76 | } |
---|
77 | |
---|
78 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task){ |
---|
79 | |
---|
80 | List<Node> nodes = resourceManager.getNodes(); |
---|
81 | List<Node> availableNodes = findSuitableNodes(task, nodes); |
---|
82 | Node node; |
---|
83 | if(availableNodes.size() == 0){ |
---|
84 | |
---|
85 | nodes = (List<Node>) resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Node, ResourceStatus.UNAVAILABLE); |
---|
86 | |
---|
87 | Collections.sort(nodes, new PowerComparator()); |
---|
88 | node = turnOnFirstNode(nodes, task); |
---|
89 | if(node == null) |
---|
90 | return null; |
---|
91 | }else{ |
---|
92 | Collections.sort(availableNodes, new PowerComparator()); |
---|
93 | node = availableNodes.get(0); |
---|
94 | } |
---|
95 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
96 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
97 | int cpuRequest; |
---|
98 | try { |
---|
99 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
100 | } catch (NoSuchFieldException e) { |
---|
101 | cpuRequest = 0; |
---|
102 | } |
---|
103 | for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) { |
---|
104 | if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) { |
---|
105 | choosenResources.add(node.getProcessors().get(i)); |
---|
106 | cpuRequest--; |
---|
107 | } |
---|
108 | } |
---|
109 | ProcessingElements result = new ProcessingElements(node.getFullName()); |
---|
110 | result.addAll(choosenResources); |
---|
111 | map.put(StandardResourceUnitName.PE, result); |
---|
112 | return map; |
---|
113 | } |
---|
114 | |
---|
115 | private List<Node> findSuitableNodes(TaskInterface<?> task, List<Node> nodes) { |
---|
116 | int cpuRequest; |
---|
117 | try { |
---|
118 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
119 | } catch (NoSuchFieldException e) { |
---|
120 | cpuRequest = 1; |
---|
121 | } |
---|
122 | |
---|
123 | List<Node> suitableNodes = new ArrayList<Node>(); |
---|
124 | for(Node node: nodes){ |
---|
125 | if(node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
126 | suitableNodes.add(node); |
---|
127 | } |
---|
128 | |
---|
129 | } |
---|
130 | return suitableNodes; |
---|
131 | } |
---|
132 | |
---|
133 | private Node turnOnFirstNode(List<Node> nodes, TaskInterface<?> task){ |
---|
134 | Node startedNode = null; |
---|
135 | |
---|
136 | int cpuRequest; |
---|
137 | try { |
---|
138 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
139 | } catch (NoSuchFieldException e) { |
---|
140 | cpuRequest = 0; |
---|
141 | } |
---|
142 | |
---|
143 | for(Node node: nodes){ |
---|
144 | |
---|
145 | if (cpuRequest != 0) { |
---|
146 | |
---|
147 | List<Processor> processors = node.getProcessors(); |
---|
148 | if (processors.size() < cpuRequest) { |
---|
149 | if(processors.size() == 0){ |
---|
150 | if(node.getProcessors().size() < cpuRequest) |
---|
151 | continue; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | int freeProcessor = 0; |
---|
156 | for(Processor processor: processors){ |
---|
157 | if(processor.getStatus() == ResourceStatus.FREE || processor.getStatus() == ResourceStatus.UNAVAILABLE) |
---|
158 | freeProcessor++; |
---|
159 | } |
---|
160 | |
---|
161 | if(freeProcessor < cpuRequest) |
---|
162 | continue; |
---|
163 | else { |
---|
164 | for(Device device: node.getParent().getResourceCharacteristic().getDevices()){ |
---|
165 | if(device.getType().equals(StandardResourceType.Fan)){ |
---|
166 | device.getAirflowInterface().setAirflowState(StandardAirflowStateName.ON); |
---|
167 | break; |
---|
168 | } |
---|
169 | } |
---|
170 | node.getPowerInterface().setPowerState(StandardPowerStateName.ON); |
---|
171 | startedNode = node; |
---|
172 | break; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | return startedNode; |
---|
177 | } |
---|
178 | private void turnOffIdleNodes(List<Node> nodes){ |
---|
179 | for(Node node : nodes){ |
---|
180 | int freeProcessors = 0; |
---|
181 | for(Processor proc: node.getProcessors()){ |
---|
182 | if(proc.getStatus() == ResourceStatus.FREE) |
---|
183 | freeProcessors++; |
---|
184 | } |
---|
185 | |
---|
186 | if(freeProcessors == node.getProcessors().size()) { |
---|
187 | for(Device device: node.getParent().getResourceCharacteristic().getDevices()){ |
---|
188 | if(device.getType().equals(StandardResourceType.Fan)){ |
---|
189 | device.getAirflowInterface().setAirflowState(StandardAirflowStateName.OFF); |
---|
190 | break; |
---|
191 | } |
---|
192 | } |
---|
193 | node.getPowerInterface().setPowerState(StandardPowerStateName.OFF); |
---|
194 | } |
---|
195 | |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | class PowerComparator implements Comparator<Node>{ |
---|
200 | |
---|
201 | public int compare(Node node1, Node node2){ |
---|
202 | double node1Rank = Double.MAX_VALUE; |
---|
203 | double node2Rank = Double.MAX_VALUE; |
---|
204 | |
---|
205 | PState pState; |
---|
206 | for(Processor proc: node1.getProcessors()){ |
---|
207 | if(proc.getPowerInterface() != null) { |
---|
208 | pState = proc.getPowerInterface().getLowestPState(); |
---|
209 | if(pState != null && pState.getPower() < node1Rank){ |
---|
210 | node1Rank = proc.getPowerInterface().getLowestPState().getFrequency(); |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | for(Processor proc: node2.getProcessors()){ |
---|
215 | if(proc.getPowerInterface() != null) { |
---|
216 | pState = proc.getPowerInterface().getLowestPState(); |
---|
217 | if(pState != null && pState.getPower() < node2Rank){ |
---|
218 | node2Rank = proc.getPowerInterface().getLowestPState().getFrequency(); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | if(node1Rank > node2Rank) |
---|
224 | return 1; |
---|
225 | else if (node1Rank < node2Rank) |
---|
226 | return -1; |
---|
227 | else return 0; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | } |
---|