source: DCWoRMS/branches/coolemall/src/test/transitions/FCFS_ConsolidationHighPerf_NodePowMan_SP_Delay.java @ 1544

Revision 1544, 6.9 KB checked in by wojtekp, 10 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.transitions;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8
9import schedframe.events.scheduling.SchedulingEvent;
10import schedframe.resources.ResourceStatus;
11import schedframe.resources.StandardResourceType;
12import schedframe.resources.computing.ComputingResource;
13import schedframe.resources.computing.Node;
14import schedframe.resources.computing.Processor;
15import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName;
16import schedframe.resources.computing.profiles.energy.power.ui.NodePowerInterface;
17import schedframe.resources.units.ProcessingElements;
18import schedframe.resources.units.ResourceUnit;
19import schedframe.resources.units.ResourceUnitName;
20import schedframe.resources.units.StandardResourceUnitName;
21import schedframe.scheduling.manager.resources.ClusterResourceManager;
22import schedframe.scheduling.manager.resources.ResourceManager;
23import schedframe.scheduling.manager.tasks.JobRegistry;
24import schedframe.scheduling.manager.tasks.JobRegistryImpl;
25import schedframe.scheduling.plan.SchedulingPlanInterface;
26import schedframe.scheduling.plan.impl.SchedulingPlan;
27import schedframe.scheduling.plugin.ModuleList;
28import schedframe.scheduling.queue.TaskQueue;
29import schedframe.scheduling.queue.TaskQueueList;
30import schedframe.scheduling.tasks.TaskInterface;
31import example.localplugin.BaseLocalSchedulingPlugin;
32import gridsim.dcworms.DCWormsTags;
33
34public class FCFS_ConsolidationHighPerf_NodePowMan_SP_Delay extends BaseLocalSchedulingPlugin {
35
36        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
37                         ResourceManager resManager, ModuleList modules) {
38
39                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
40                SchedulingPlan plan = new SchedulingPlan();
41                // choose the events types to serve.
42                // Different actions for different events are possible.
43                switch (event.getType()) {
44                case START_TASK_EXECUTION:
45                case TASK_FINISHED:
46                case RESOURCE_POWER_STATE_CHANGED:
47                        // our tasks are placed only in first queue (see
48                        // BaseLocalSchedulingPlugin.placeJobsInQueues() method)
49                        TaskQueue q = queues.get(0);
50                        List<Node> notSelectedNodes = resourceManager.getNodes();
51                       
52                        // check all tasks in queue
53                        for (int i = 0; i < q.size(); i++) {
54                                TaskInterface<?> task = q.get(i);
55                                // if status of the tasks in READY
56                                if (task.getStatus() == DCWormsTags.READY) {
57
58                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
59                                        if (choosenResources != null) {
60                                                addToSchedulingPlan(plan, task, choosenResources);
61                                                ProcessingElements pe = (ProcessingElements) choosenResources.get(StandardResourceUnitName.PE);
62                                                Node node = (Node) pe.get(0).getParent();
63                                                notSelectedNodes.remove(node);
64                                        }
65                                }
66                        }
67                        manageResources(notSelectedNodes);
68                        break;
69                }
70                return plan;
71        }
72
73        @SuppressWarnings("unchecked")
74        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
75                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
76
77                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
78               
79                List<Node> nodes = resourceManager.getNodes();
80                List<Node> avNodes = filterNodes(nodes, task);
81                Node node;
82                if(avNodes.size() == 0){
83
84                        nodes = (List<Node>) resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Node, ResourceStatus.UNAVAILABLE);
85                        node = turnOnFirstNode(nodes, task);
86                        return null;
87                } else {
88                        node = findTheMostLoadedNode(avNodes);                 
89                }
90
91                int cpuRequest;
92                try {
93                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
94                } catch (NoSuchFieldException e) {
95                        cpuRequest = 0;
96                }
97
98                if (cpuRequest != 0) {
99
100                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); 
101                       
102                        List<Processor> cpus = node.getProcessors();                   
103                        for (int i = 0; i < cpus.size() && cpuRequest > 0; i++) {
104                                if (cpus.get(i).getStatus() == ResourceStatus.FREE) {
105                                        choosenResources.add(cpus.get(i));
106                                        cpuRequest--;
107                                }
108                        }
109                        if (cpuRequest > 0) {
110                                return null;
111                        }
112                        //choosenResources.add(node.getProcessors().get(0));
113                        ProcessingElements pe = new ProcessingElements();
114                        pe.addAll(choosenResources);
115                        map.put(StandardResourceUnitName.PE, pe);
116                        return map;
117                }
118                return null;
119        }
120       
121        private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){
122                List<Node> filteredNodes = new ArrayList<Node>();
123                int cpuRequest;
124                try {
125                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
126                } catch (NoSuchFieldException e) {
127                        cpuRequest = 0;
128                }
129                for (Node node : nodes) {
130
131                        if (cpuRequest != 0) {
132
133                                List<Processor> cpus = node.getProcessors();
134                                if (cpus.size() < cpuRequest) {
135                                        if(cpus.size() == 0){
136                                                continue;
137                                        }
138                                }
139
140                                int freeCpus = 0;
141                                for(Processor cpu: cpus){
142                                        if(cpu.getStatus() == ResourceStatus.FREE)
143                                                freeCpus++;
144                                }
145                               
146                                if(freeCpus < cpuRequest)
147                                        continue;
148                               
149                                filteredNodes.add(node);
150                        }
151                }
152               
153                return filteredNodes;
154        }
155       
156        private Node findTheMostLoadedNode(List<Node> nodes){
157                Node mostLoadedNode = null;
158                       
159                int maxRunningTask = -1;
160                int nrRunningTasks;
161                for(Node node: nodes){
162                        JobRegistry jr = new JobRegistryImpl(node.getFullName());
163                        nrRunningTasks = jr.getRunningTasks().size();
164                        if(nrRunningTasks > maxRunningTask){
165                                mostLoadedNode = node;
166                                maxRunningTask = nrRunningTasks;
167                        }
168                }
169                return mostLoadedNode;
170        }
171       
172        private Node turnOnFirstNode(List<Node> nodes, TaskInterface<?> task){
173                Node startedNode = null;
174
175                int cpuRequest;
176                try {
177                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
178                } catch (NoSuchFieldException e) {
179                        cpuRequest = 0;
180                }
181               
182                for(Node node: nodes){
183                       
184                        if (cpuRequest != 0) {
185
186                                List<Processor> cpus = node.getProcessors();
187                                if (cpus.size() < cpuRequest) {
188                                        if(cpus.size() == 0){
189                                                continue;
190                                        }
191                                }
192                               
193                                int freeCpus = 0;
194                                for(Processor cpu: cpus){
195                                        if(cpu.getStatus() == ResourceStatus.FREE || cpu.getStatus() == ResourceStatus.UNAVAILABLE)
196                                                freeCpus++;
197                                }
198                               
199                                if(freeCpus < cpuRequest)
200                                        continue;
201                                else if(node.getPowerInterface().getPowerState().getLabel().equals(StandardPowerStateName.OFF.getLabel())){
202                                        NodePowerInterface nodePI = (NodePowerInterface)node.getPowerInterface();
203                                        nodePI.turnOn();
204                                        startedNode = node;
205                                        break;
206                                }                               
207                        }
208                }
209                return startedNode;
210        }
211       
212        protected void manageResources(List<Node> notSelectedNodes) {
213                turnOffIdleNodes(notSelectedNodes);
214        }
215
216        private void turnOffIdleNodes(List<Node> nodes){
217                for(Node node : nodes){
218
219                        int freeCpus = 0;
220                        for(Processor cpu: node.getProcessors ()){
221                                if(cpu.getStatus() == ResourceStatus.FREE)
222                                        freeCpus++;
223                        }
224                       
225                        if(freeCpus == node.getProcessors().size() && node.getPowerInterface().getPowerState().getLabel().equals(StandardPowerStateName.ON.getLabel())) {
226                                NodePowerInterface nodePI = (NodePowerInterface)node.getPowerInterface();
227                                nodePI.turnOff();
228                        }
229
230                }
231        }
232
233}
Note: See TracBrowser for help on using the repository browser.