source: DCWoRMS/branches/coolemall/src/test/freqscaling/CB2_FCFS_Random_SP.java @ 1450

Revision 1450, 15.3 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.freqscaling;
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.exceptions.ResourceException;
11import schedframe.resources.ResourceStatus;
12import schedframe.resources.StandardResourceType;
13import schedframe.resources.computing.ComputingResource;
14import schedframe.resources.computing.Core;
15import schedframe.resources.computing.Node;
16import schedframe.resources.computing.Processor;
17import schedframe.resources.computing.coolemall.ComputeBox1;
18import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName;
19import schedframe.resources.computing.profiles.energy.power.PState;
20import schedframe.resources.devices.Device;
21import schedframe.resources.devices.Fan;
22import schedframe.resources.devices.PhysicalResource;
23import schedframe.resources.units.ProcessingElements;
24import schedframe.resources.units.ResourceUnit;
25import schedframe.resources.units.ResourceUnitName;
26import schedframe.resources.units.StandardResourceUnitName;
27import schedframe.scheduling.manager.resources.ClusterResourceManager;
28import schedframe.scheduling.manager.resources.ResourceManager;
29import schedframe.scheduling.manager.tasks.JobRegistry;
30import schedframe.scheduling.plan.SchedulingPlanInterface;
31import schedframe.scheduling.plan.impl.SchedulingPlan;
32import schedframe.scheduling.plugin.ModuleList;
33import schedframe.scheduling.queue.TaskQueue;
34import schedframe.scheduling.queue.TaskQueueList;
35import schedframe.scheduling.tasks.TaskInterface;
36import test.Node_Fan_Mapping;
37import example.localplugin.BaseLocalSchedulingPlugin;
38import gridsim.dcworms.DCWormsTags;
39
40public class CB2_FCFS_Random_SP extends BaseLocalSchedulingPlugin {
41
42        private Random rand;
43
44        public static String mode;
45        private int MAX_THRESHOLD = 450;
46        private int MIN_THRESHOLD = 400;
47        private Map<String, Double> currentFrequencyLevels;
48       
49        public CB2_FCFS_Random_SP () {
50                rand = new Random(47);
51                Node_Fan_Mapping.init();
52                currentFrequencyLevels = new HashMap<String, Double>();
53                mode = "";
54        }
55
56        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
57                         ResourceManager resManager, ModuleList modules) {
58
59                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
60
61                SchedulingPlan plan = new SchedulingPlan();
62                // choose the events types to serve.
63                // Different actions for different events are possible.
64                switch (event.getType()) {
65                case START_TASK_EXECUTION:
66                case TASK_FINISHED:
67                //case TIMER:
68                        // our tasks are placed only in first queue (see
69                        // BaseLocalSchedulingPlugin.placeJobsInQueues() method)
70                        TaskQueue q = queues.get(0);
71                       
72                        // check all tasks in queue
73                        for (int i = 0; i < q.size(); i++) {
74                                TaskInterface<?> task = q.get(i);
75                                // if status of the tasks in READY
76                                if (task.getStatus() == DCWormsTags.READY) {
77
78                                        if(resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Core, ResourceStatus.FREE).size() == 0)
79                                                break;
80                                       
81                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
82                                        if (choosenResources != null) {
83                                                addToSchedulingPlan(plan, task, choosenResources);
84                                        }
85                                }
86                        }
87                        break;
88                case RESOURCE_POWER_LIMIT_EXCEEDED:
89                        powerCap(resManager);
90                        break;
91                }
92               
93                return plan;
94        }
95        @SuppressWarnings("unchecked")
96        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
97                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
98
99                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
100               
101                List<Node> nodes = resourceManager.getNodes();
102                List<Node> avNodes = filterNodes(nodes, task);
103                if(avNodes.size() == 0)
104                        return null;
105                Node node = randomNode(avNodes);
106                int cpuRequest;
107                try {
108                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
109                } catch (NoSuchFieldException e) {
110                        cpuRequest = 0;
111                }
112
113                if (cpuRequest != 0) {
114
115                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); 
116                        List<Core> cores = node.getCores();                     
117                        for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
118                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
119                                        choosenResources.add(cores.get(i));
120                                        if(true) {
121                                                if(cores.get(i).getProcessor().getPowerInterface().getPState().equals(cores.get(i).getProcessor().getPowerInterface().getHighestPState()))
122                                                        cores.get(i).getProcessor().getPowerInterface().setPState(cores.get(i).getProcessor().getPowerInterface().getLowestPState().getName());
123                                        }
124                                        Node n = cores.get(i).getProcessor().getNode();
125                                        for(Device device: n.getParent().getResourceCharacteristic().getDevices()){
126                                               
127                                                if(device.getType().equals(StandardResourceType.Fan) || device.getType().equals(StandardResourceType.Inlet) | device.getType().equals(StandardResourceType.Outlet)){
128                                                        Fan fan = (Fan) device;
129
130                                                        if(fan.getChilledResources().contains(n.getFullName())){
131                                                                fan.getAirflowInterface().setAirflowState(new CustomAirflowStateName("2"));
132                                                        }
133                                                }
134                                        }
135                                        cpuRequest--;
136                                }
137                        }
138                        if (cpuRequest > 0) {
139                                return null;
140                        }
141
142                        //choosenResources.add(node.getProcessors().get(0));
143                        ProcessingElements pe = new ProcessingElements();
144                        pe.addAll(choosenResources);
145                        map.put(StandardResourceUnitName.PE, pe);
146                        return map;
147                }
148                return null;
149        }
150       
151        private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){
152                List<Node> filteredNodes = new ArrayList<Node>();
153                int cpuRequest;
154                try {
155                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
156                } catch (NoSuchFieldException e) {
157                        cpuRequest = 0;
158                }
159                for (Node node : nodes) {
160
161                        if (cpuRequest != 0) {
162
163                                List<Core> cores = node.getCores();
164                                if (cores.size() < cpuRequest) {
165                                        if(cores.size() == 0){
166                                                if(node.getProcessors().size() < cpuRequest)
167                                                        continue;
168                                        }
169                                }
170
171                                int freeCores = 0;
172                                for(Core core: cores){
173                                        if(core.getStatus() == ResourceStatus.FREE)
174                                                freeCores++;
175                                }
176                               
177                                if(freeCores < cpuRequest)
178                                        continue;
179                               
180                                filteredNodes.add(node);
181                        }
182                }
183               
184                return filteredNodes;
185        }
186       
187        private Node randomNode(List<Node> nodes){
188                return nodes.get(rand.nextInt(nodes.size()));
189        }
190       
191        private void powerCap(ResourceManager resManager){
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                MAX_THRESHOLD =450;
392                MIN_THRESHOLD = 400;
393        }
394
395}
396
Note: See TracBrowser for help on using the repository browser.