source: DCWoRMS/branches/coolemall/src/example/localplugin/Cluster_FCFSBF_ConsolidationHighPerf_NodePowMan_Plugin.java @ 1415

Revision 1415, 9.9 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.localplugin;
2
3import gridsim.dcworms.DCWormsTags;
4
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.Comparator;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.List;
11import java.util.Map;
12import java.util.Set;
13
14import schedframe.events.scheduling.SchedulingEvent;
15import schedframe.resources.ResourceStatus;
16import schedframe.resources.StandardResourceType;
17import schedframe.resources.computing.ComputingResource;
18import schedframe.resources.computing.Node;
19import schedframe.resources.computing.Processor;
20import schedframe.resources.computing.profiles.energy.airthroughput.AirflowState;
21import schedframe.resources.computing.profiles.energy.airthroughput.AirflowStateName;
22import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName;
23import schedframe.resources.computing.profiles.energy.power.PState;
24import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName;
25import schedframe.resources.devices.Device;
26import schedframe.resources.devices.Fan;
27import schedframe.resources.units.ProcessingElements;
28import schedframe.resources.units.ResourceUnit;
29import schedframe.resources.units.ResourceUnitName;
30import schedframe.resources.units.StandardResourceUnitName;
31import schedframe.scheduling.manager.resources.ClusterResourceManager;
32import schedframe.scheduling.manager.resources.ResourceManager;
33import schedframe.scheduling.manager.tasks.JobRegistry;
34import schedframe.scheduling.plan.SchedulingPlanInterface;
35import schedframe.scheduling.plan.impl.SchedulingPlan;
36import schedframe.scheduling.plugin.ModuleList;
37import schedframe.scheduling.queue.TaskQueue;
38import schedframe.scheduling.queue.TaskQueueList;
39import schedframe.scheduling.tasks.TaskInterface;
40
41public 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}
Note: See TracBrowser for help on using the repository browser.