source: DCWoRMS/branches/coolemall/src/experiments/e2dc2014/CB2_FCFS_Random_SP.java @ 1501

Revision 1501, 16.0 KB checked in by wojtekp, 10 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package experiments.e2dc2014;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Random;
8
9import schedframe.events.scheduling.SchedulingEvent;
10import schedframe.resources.ResourceStatus;
11import schedframe.resources.StandardResourceType;
12import schedframe.resources.computing.ComputingResource;
13import schedframe.resources.computing.Core;
14import schedframe.resources.computing.Node;
15import schedframe.resources.computing.Processor;
16import schedframe.resources.computing.coolemall.ComputeBox1;
17import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName;
18import schedframe.resources.computing.profiles.energy.power.PState;
19import schedframe.resources.devices.Device;
20import schedframe.resources.devices.Fan;
21import schedframe.resources.devices.PhysicalResource;
22import schedframe.resources.units.ProcessingElements;
23import schedframe.resources.units.ResourceUnit;
24import schedframe.resources.units.ResourceUnitName;
25import schedframe.resources.units.StandardResourceUnitName;
26import schedframe.scheduling.manager.resources.ClusterResourceManager;
27import schedframe.scheduling.manager.resources.ResourceManager;
28import schedframe.scheduling.manager.tasks.JobRegistry;
29import schedframe.scheduling.plan.SchedulingPlanInterface;
30import schedframe.scheduling.plan.impl.SchedulingPlan;
31import schedframe.scheduling.plugin.ModuleList;
32import schedframe.scheduling.queue.TaskQueue;
33import schedframe.scheduling.queue.TaskQueueList;
34import schedframe.scheduling.tasks.TaskInterface;
35import test.Node_Fan_Mapping;
36import example.localplugin.BaseLocalSchedulingPlugin;
37import gridsim.dcworms.DCWormsTags;
38
39public class CB2_FCFS_Random_SP extends BaseLocalSchedulingPlugin {
40
41        private Random rand;
42
43        public static String mode;
44        private int MAX_THRESHOLD = 1400;
45        private int MIN_THRESHOLD = 1200;
46        private Map<String, Double> currentFrequencyLevels;
47       
48        public CB2_FCFS_Random_SP () {
49                rand = new Random(47);
50                Node_Fan_Mapping.init();
51                currentFrequencyLevels = new HashMap<String, Double>();
52                mode = "";
53        }
54
55        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
56                         ResourceManager resManager, ModuleList modules) {
57
58                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
59
60                SchedulingPlan plan = new SchedulingPlan();
61                // choose the events types to serve.
62                // Different actions for different events are possible.
63                switch (event.getType()) {
64                case START_TASK_EXECUTION:
65                case TASK_FINISHED:
66                //case TIMER:
67                        // our tasks are placed only in first queue (see
68                        // BaseLocalSchedulingPlugin.placeJobsInQueues() method)
69                        TaskQueue q = queues.get(0);
70                       
71                        // check all tasks in queue
72                        for (int i = 0; i < q.size(); i++) {
73                                TaskInterface<?> task = q.get(i);
74                                // if status of the tasks in READY
75                                if (task.getStatus() == DCWormsTags.READY) {
76
77                                        if(resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Core, ResourceStatus.FREE).size() == 0)
78                                                break;
79
80                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
81                                        if (choosenResources != null) {
82                                                addToSchedulingPlan(plan, task, choosenResources);
83                                        }
84                                }
85                        }
86                        break;
87                case RESOURCE_POWER_LIMIT_EXCEEDED:
88                        powerCap(resManager);
89                        break;
90                }
91               
92                return plan;
93        }
94
95        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
96                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
97
98                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
99               
100                List<Node> nodes = resourceManager.getNodes();
101                List<Node> avNodes = filterNodes(nodes, task);
102                if(avNodes.size() == 0)
103                        return null;
104                Node node = randomNode(avNodes);
105                int cpuRequest;
106                try {
107                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
108                } catch (NoSuchFieldException e) {
109                        cpuRequest = 0;
110                }
111
112                if (cpuRequest != 0) {
113
114                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); 
115                        List<Core> cores = node.getCores();                     
116                        for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
117                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
118                                        choosenResources.add(cores.get(i));
119                                        if(true) {
120                                                if(cores.get(i).getProcessor().getPowerInterface().getPState().equals(cores.get(i).getProcessor().getPowerInterface().getHighestPState()))
121                                                        cores.get(i).getProcessor().getPowerInterface().setPState(cores.get(i).getProcessor().getPowerInterface().getLowestPState().getName());
122                                        }
123                                        Node n = cores.get(i).getProcessor().getNode();
124                                        for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
125                                               
126                                                if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
127                                                        Fan fan = (Fan) device;
128
129                                                        if(fan.getChilledResources().contains(n.getFullName())){
130                                                                fan.getAirflowInterface().setAirflowState(new CustomAirflowStateName("2"));
131                                                        }
132                                                }
133                                        }
134                                        cpuRequest--;
135                                }
136                        }
137                        if (cpuRequest > 0) {
138                                return null;
139                        }
140
141                        //choosenResources.add(node.getProcessors().get(0));
142                        ProcessingElements pe = new ProcessingElements();
143                        pe.addAll(choosenResources);
144                        map.put(StandardResourceUnitName.PE, pe);
145                        return map;
146                }
147                return null;
148        }
149       
150        private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){
151                List<Node> filteredNodes = new ArrayList<Node>();
152                int cpuRequest;
153                try {
154                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
155                } catch (NoSuchFieldException e) {
156                        cpuRequest = 0;
157                }
158                for (Node node : nodes) {
159
160                        if (cpuRequest != 0) {
161
162                                List<Core> cores = node.getCores();
163                                if (cores.size() < cpuRequest) {
164                                        if(cores.size() == 0){
165                                                if(node.getProcessors().size() < cpuRequest)
166                                                        continue;
167                                        }
168                                }
169
170                                int freeCores = 0;
171                                for(Core core: cores){
172                                        if(core.getStatus() == ResourceStatus.FREE)
173                                                freeCores++;
174                                }
175                               
176                                if(freeCores < cpuRequest)
177                                        continue;
178                               
179                                filteredNodes.add(node);
180                        }
181                }
182               
183                return filteredNodes;
184        }
185       
186        private Node randomNode(List<Node> nodes){
187                return nodes.get(rand.nextInt(nodes.size()));
188        }
189       
190        private void powerCap(ResourceManager resManager){
191               
192                for(ComputingResource compRes: resManager.getResourcesOfType(StandardResourceType.Rack)){
193                        loadThresholds(compRes);
194
195                        double powerConsumption = compRes.getPowerInterface().getRecentPowerUsage().getValue();
196                        //System.out.println(compRes.getFullName() + " current: " + powerConsumption);
197                        if(powerConsumption > MAX_THRESHOLD){
198                                mode = compRes.getFullName();
199                                ComputeBox1 cb1 = (ComputeBox1) compRes;
200                                List<Processor> processors = cb1.getProcessors();
201                               
202                                double currentFrequencyLevel = 0;
203                                if(currentFrequencyLevels.get(compRes.getFullName()) != null){
204                                        currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
205                                } else {
206                                        currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
207                                }
208                                boolean overThreshold = true;
209                                boolean canDoBetter = true;
210
211                                while(overThreshold && canDoBetter){
212                                        //System.out.println(compRes.getFullName() + " - downgrading");
213                                        int procNumberWithoutChange = 0;
214                                        boolean improvement = false;
215                                        //System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
216                                        for (Processor proc: processors){
217                                                PState currPState = proc.getPowerInterface().getPState();
218                                                //System.out.println(proc.getFullName() + " - checking");
219                                                if(currPState.getFrequency() <= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
220                                                        //System.out.println("operating same level");
221                                                        procNumberWithoutChange++;
222                                                        continue;
223                                                }
224                                                if (proc.getFreeCores().size() == proc.getCores().size()){     
225                                                        //System.out.println("free");
226                                                        procNumberWithoutChange++;
227                                                        continue;
228                                                }
229               
230                                                double lowerFrequency = proc.getPowerInterface().getHighestPState().getFrequency();
231                                                for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
232                                                        PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
233                                                        if(pState.getFrequency() > lowerFrequency && pState.getFrequency() < currPState.getFrequency()){
234                                                                lowerFrequency = pState.getFrequency();
235                                                        }
236                                                }
237                                                //System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
238                                                if(lowerFrequency < currentFrequencyLevel){
239                                                        //System.out.println(compRes.getFullName() + " - changing current frequency level");
240                                                        currentFrequencyLevels.put(compRes.getFullName(), lowerFrequency);
241                                                        currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
242                                                }
243                                               
244                                                if(lowerFrequency != currPState.getFrequency()) {
245                                                        //System.out.println(proc.getFullName() + " - lowering frequency");
246                                                        proc.getPowerInterface().setFrequency(lowerFrequency);
247                                                        Node n = proc.getNode();
248                                                        for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
249                                                               
250                                                                if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
251                                                                        Fan fan = (Fan) device;
252
253                                                                        if(fan.getChilledResources().contains(n.getFullName())){
254                                                                                fan.getAirflowInterface().setAirflowState(new CustomAirflowStateName("2"));
255                                                                        }
256                                                                }
257                                                        }
258                                                        improvement = true;
259                                                }
260                                                if(lowerFrequency == proc.getPowerInterface().getHighestPState().getFrequency()){
261                                                        canDoBetter = false;
262                                                } else {
263                                                        canDoBetter = true;
264                                                }
265                                                if(compRes.getPowerInterface().getRecentPowerUsage().getValue() <= MAX_THRESHOLD){
266                                                        overThreshold = false;
267                                                        break;
268                                                }
269                                        }       
270                                        if(procNumberWithoutChange == processors.size() || improvement == false){
271                                                //System.out.println(compRes.getFullName() + " - failed to improve");
272                                                double nextFrequencyLevel = processors.get(0).getPowerInterface().getHighestPState().getFrequency();
273                                                for (Processor proc: processors){
274                                                        for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
275                                                                PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
276                                                                if(pState.getFrequency() > nextFrequencyLevel && pState.getFrequency() < currentFrequencyLevel){
277                                                                        nextFrequencyLevel = pState.getFrequency();
278                                                                }
279                                                        }
280                                                }
281                                                //System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
282                                                currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
283                                                currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
284                                                if(currentFrequencyLevel == processors.get(0).getPowerInterface().getHighestPState().getFrequency()){
285                                                        canDoBetter = false;
286                                                }
287                                                procNumberWithoutChange = 0;
288                                        }
289                                }
290
291                        } else if (powerConsumption < MIN_THRESHOLD) {
292                                ComputeBox1 cb1 = (ComputeBox1) compRes;
293                                List<Processor> processors = cb1.getProcessors();
294                               
295                                double currentFrequencyLevel = 0;
296                                if(currentFrequencyLevels.get(compRes.getFullName()) != null){
297                                        currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
298                                } else {
299                                        currentFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
300                                }
301                                boolean overThreshold = true;
302                                boolean canDoBetter = true;
303
304                                while(overThreshold && canDoBetter){
305                                        //System.out.println(compRes.getFullName() + " - downgrading");
306                                        int procNumberWithoutChange = 0;
307                                        boolean improvement = false;
308                                        //System.out.println(compRes.getFullName() + "; currentFrequencyLevel: " + currentFrequencyLevel);
309                                        for (Processor proc: processors){
310                                                PState currPState = proc.getPowerInterface().getPState();
311                                                //System.out.println(proc.getFullName() + " - checking");
312                                                if(currPState.getFrequency() >= currentFrequencyLevel && proc.getFreeCores().size() != proc.getCores().size()){
313                                                        //System.out.println("operating same level");
314                                                        procNumberWithoutChange++;
315                                                        continue;
316                                                }
317                                                if (proc.getFreeCores().size() == proc.getCores().size()){     
318                                                        //System.out.println("free");
319                                                        procNumberWithoutChange++;
320                                                        continue;
321                                                }
322               
323                                                double higherFrequency = proc.getPowerInterface().getLowestPState().getFrequency();
324                                                for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
325                                                        PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
326                                                        if(pState.getFrequency() < higherFrequency && pState.getFrequency() > currPState.getFrequency()){
327                                                                higherFrequency = pState.getFrequency();
328                                                        }
329                                                }
330                                                //System.out.println(proc.getFullName() + "; lower frequency: " + lowerFrequency);
331                                                if(higherFrequency > currentFrequencyLevel){
332                                                        //System.out.println(compRes.getFullName() + " - changing current frequency level");
333                                                        currentFrequencyLevels.put(compRes.getFullName(), higherFrequency);
334                                                        currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
335                                                }
336                                               
337                                                if(higherFrequency != currPState.getFrequency()) {
338                                                        //System.out.println(proc.getFullName() + " - lowering frequency");
339                                                        proc.getPowerInterface().setFrequency(higherFrequency);
340                                                        Node n = proc.getNode();
341                                                        for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
342                                                               
343                                                                if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
344                                                                        Fan fan = (Fan) device;
345
346                                                                        if(fan.getChilledResources().contains(n.getFullName())){
347                                                                                fan.getAirflowInterface().setAirflowState(new CustomAirflowStateName("2"));
348                                                                        }
349                                                                }
350                                                        }
351                                                        improvement = true;
352                                                }
353                                                if(higherFrequency == proc.getPowerInterface().getLowestPState().getFrequency()){
354                                                        canDoBetter = false;
355                                                } else {
356                                                        canDoBetter = true;
357                                                }
358                                                if(compRes.getPowerInterface().getRecentPowerUsage().getValue() >= MAX_THRESHOLD){
359                                                        overThreshold = false;
360                                                        break;
361                                                }
362                                        }
363                                        if(procNumberWithoutChange == processors.size() || improvement == false){
364                                                //System.out.println(compRes.getFullName() + " - failed to improve");
365                                                double nextFrequencyLevel = processors.get(0).getPowerInterface().getLowestPState().getFrequency();
366                                                for (Processor proc: processors){
367                                                        for(String pStateName: proc.getPowerInterface().getSupportedPStates().keySet()){
368                                                                PState pState = proc.getPowerInterface().getSupportedPStates().get(pStateName);
369                                                                if(pState.getFrequency() < nextFrequencyLevel && pState.getFrequency() > currentFrequencyLevel){
370                                                                        nextFrequencyLevel = pState.getFrequency();
371                                                                }
372                                                        }
373                                                }
374                                                //System.out.println(compRes.getFullName() + "; nextFrequencyLevel: " + nextFrequencyLevel);
375                                                currentFrequencyLevels.put(compRes.getFullName(), nextFrequencyLevel);
376                                                currentFrequencyLevel = currentFrequencyLevels.get(compRes.getFullName());
377                                                if(currentFrequencyLevel == processors.get(0).getPowerInterface().getLowestPState().getFrequency()){
378                                                        canDoBetter = false;
379                                                }
380                                                procNumberWithoutChange = 0;
381                                        }
382                                }
383
384                        }
385                        mode = "";
386                }
387
388        }
389       
390        private void loadThresholds(PhysicalResource resource){
391                String productName = resource.getResourceCharacteristic().getParameters().get("product").get(0).getContent();
392                if(productName.equals("HS20")){
393                        MAX_THRESHOLD = PowerCapLevels.HS20_MAX;
394                        MIN_THRESHOLD = PowerCapLevels.HS20_MIN;
395                } else if (productName.equals("IBM 305x COMPxxx")){
396                        MAX_THRESHOLD = PowerCapLevels.IBM_305x_MAX;
397                        MIN_THRESHOLD = PowerCapLevels.IBM_305x_MIN;
398                } else if (productName.equals("iDataPlex M2 NRN20xxx")){
399                        MAX_THRESHOLD = PowerCapLevels.iDataPlex_M2_MAX;
400                        MIN_THRESHOLD = PowerCapLevels.iDataPlex_M2_MIN;
401                } else if (productName.equals("RenderCloud SDR31xxx")){
402                        MAX_THRESHOLD = PowerCapLevels.RenderCloud_MAX;
403                        MIN_THRESHOLD = PowerCapLevels.RenderCloud_MIN;
404                } else if (productName.equals("iDataPlex M3 NRN17xxx")){
405                        MAX_THRESHOLD = PowerCapLevels.iDataPlex_M3_MAX;
406                        MIN_THRESHOLD = PowerCapLevels.iDataPlex_M3_MIN;
407                }
408        }
409
410}
411
Note: See TracBrowser for help on using the repository browser.