source: DCWoRMS/trunk/src/test/article/recs/plugins/scheduling/RecsExclusivenessNodePowManSP.java @ 663

Revision 663, 5.3 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.article.recs.plugins.scheduling;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8import schedframe.events.scheduling.SchedulingEvent;
9import schedframe.resources.ResourceStatus;
10import schedframe.resources.computing.ComputingNode;
11import schedframe.resources.computing.ComputingResource;
12import schedframe.resources.computing.Core;
13import schedframe.resources.computing.Processor;
14import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName;
15import schedframe.resources.units.ProcessingElements;
16import schedframe.resources.units.ResourceUnit;
17import schedframe.resources.units.ResourceUnitName;
18import schedframe.resources.units.StandardResourceUnitName;
19import schedframe.scheduling.manager.resources.ClusterResourceManager;
20import schedframe.scheduling.manager.resources.ResourceManager;
21import schedframe.scheduling.manager.tasks.JobRegistry;
22import schedframe.scheduling.plan.SchedulingPlanInterface;
23import schedframe.scheduling.plan.impl.SchedulingPlan;
24import schedframe.scheduling.plugin.grid.ModuleList;
25import schedframe.scheduling.queue.TaskQueue;
26import schedframe.scheduling.queue.TaskQueueList;
27import schedframe.scheduling.tasks.TaskInterface;
28import example.localplugin.BaseLocalSchedulingPlugin;
29import gridsim.dcworms.DCWormsTags;
30
31public class RecsExclusivenessNodePowManSP extends BaseLocalSchedulingPlugin {
32
33
34        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
35                        ResourceManager resManager, ModuleList modules) {
36
37                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
38                SchedulingPlan plan = new SchedulingPlan();
39
40                switch (event.getType()) {
41                case START_TASK_EXECUTION:
42                case TASK_FINISHED:
43
44                        TaskQueue q = queues.get(0);
45
46                        for (int i = 0; i < q.size(); i++) {
47                                TaskInterface<?> task = q.get(i);
48                                if (task.getStatus() == DCWormsTags.READY) {
49
50                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
51                                        if (choosenResources  != null) {
52                                                addToSchedulingPlan(plan, task, choosenResources);
53                                        } else {
54                                                if(harnessIdleNodesToWork(task, resourceManager.getComputingNodes()))
55                                                        i--;
56                                        }
57                                }
58                        }
59                        turnOffIdleNodes(resourceManager.getComputingNodes());
60                        break;
61                }
62                return plan;
63        }
64       
65        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) {
66
67                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
68               
69                List<ComputingNode> nodes = resourceManager.getComputingNodes();
70                List<ComputingNode> avNodes = filterNodes(nodes, task);
71                if(avNodes.size() == 0)
72                        return null;
73
74                for(ComputingNode node: avNodes){
75                        int cpuRequest;
76                        try {
77                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
78                        } catch (NoSuchFieldException e) {
79                                cpuRequest = 0;
80                        }
81       
82                        if (cpuRequest != 0) {
83       
84                                List<Core> cores = node.getProcessors().get(0).getCores();
85                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
86                                for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
87                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
88                                                choosenResources.add(cores.get(i));
89                                                cpuRequest--;
90                                        }
91                                }
92                                if (cpuRequest > 0) {
93                                        continue;
94                                }
95                                ProcessingElements pe = new ProcessingElements();
96                                pe.addAll(choosenResources);
97                                map.put(StandardResourceUnitName.PE, pe);
98                                return map;
99                        }
100                }
101                return null;
102        }
103
104        private void turnOffIdleNodes(List<ComputingNode> nodes){
105                for(ComputingNode node : nodes){
106                        Processor proc = node.getProcessors().get(0);
107                        int freeCores = 0;
108                        for(Core core: proc.getCores()){
109                                if(core.getStatus() == ResourceStatus.FREE)
110                                        freeCores++;
111                        }
112                       
113                        if(freeCores == proc.getCores().size())
114                                node.getPowerInterface().setPowerState(StandardPowerStateName.OFF);
115       
116                }
117        }
118       
119        private boolean harnessIdleNodesToWork(TaskInterface<?> task, List<ComputingNode> nodes){
120
121                int cpuRequest;
122                try {
123                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
124                } catch (NoSuchFieldException e) {
125                        cpuRequest = 0;
126                }
127                for (int i = 0; i < nodes.size(); i++) {
128                        ComputingNode node = nodes.get(i);
129                        if(node.getPowerInterface().getPowerState() == StandardPowerStateName.OFF){
130
131                                List<Core> cores = node.getProcessors().get(0).getCores();
132                                if (cores.size() < cpuRequest) {
133                                        continue;
134                                }
135                                node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
136                                return true;
137                        }
138                }
139                return false;
140        }
141
142       
143        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
144                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
145                for (ComputingNode node : nodes) {
146                        int cpuRequest;
147                        try {
148                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
149                        } catch (NoSuchFieldException e) {
150                                cpuRequest = 0;
151                        }
152
153                        if (cpuRequest != 0) {
154
155                                List<Core> cores = node.getProcessors().get(0).getCores();
156                                if (cores.size() < cpuRequest) {
157                                        continue;
158                                }
159
160                                int freeCores = 0;
161                                for(Core core: cores){
162                                        if(core.getStatus() == ResourceStatus.FREE)
163                                                freeCores++;
164                                }
165                               
166                                if(freeCores != cores.size())
167                                        continue;
168                               
169                                filteredNodes.add(node);
170                        }
171                }
172               
173                return filteredNodes;
174        }
175       
176}
Note: See TracBrowser for help on using the repository browser.