source: DCWoRMS/trunk/src/test/article2/recs/plugins/scheduling/exp1/RecsOutOffSP.java @ 826

Revision 826, 6.4 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.article2.recs.plugins.scheduling.exp1;
2
3import gridsim.dcworms.DCWormsTags;
4
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.MissingResourceException;
12import java.util.Random;
13
14import schedframe.events.scheduling.SchedulingEvent;
15import schedframe.resources.ResourceStatus;
16import schedframe.resources.computing.ComputingNode;
17import schedframe.resources.computing.ComputingResource;
18import schedframe.resources.computing.Core;
19import schedframe.resources.computing.Processor;
20import schedframe.resources.computing.profiles.energy.airthroughput.StandardAirThroughputStateName;
21import schedframe.resources.computing.profiles.energy.airthroughput.UserAirThroughputStateName;
22import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName;
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.manager.tasks.JobRegistryImpl;
31import schedframe.scheduling.plan.SchedulingPlanInterface;
32import schedframe.scheduling.plan.impl.SchedulingPlan;
33import schedframe.scheduling.plugin.grid.ModuleList;
34import schedframe.scheduling.queue.TaskQueue;
35import schedframe.scheduling.queue.TaskQueueList;
36import schedframe.scheduling.tasks.TaskInterface;
37import test.article2.recs.plugins.scheduling.RecsSP;
38
39public class RecsOutOffSP extends RecsSP {
40
41        private static int START_ID = 0;
42        private static int END_ID = 8;
43       
44        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
45                        ResourceManager resManager, ModuleList modules) {
46
47                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
48                SchedulingPlan plan = new SchedulingPlan();
49                // choose the events types to serve.
50                // Different actions for different events are possible.
51                switch (event.getType()) {
52                case START_TASK_EXECUTION:
53                case TASK_FINISHED:
54                //case TIMER:
55                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
56                        TaskQueue q = queues.get(0);
57
58                        List<ComputingNode> notSelectedNodes = resourceManager.getComputingNodes();
59                        // check all tasks in queue
60                        for (int i = 0; i < q.size(); i++) {
61                                TaskInterface<?> task = q.get(i);
62                                // if status of the tasks in READY
63                                if (task.getStatus() == DCWormsTags.READY) {
64                                        ComputingNode node = chooseProvider(resourceManager, task);
65                                        if (node != null) {
66                                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON);
67                                                notSelectedNodes.remove(node);
68                                                addToSchedulingPlan(plan, task, chooseResourcesForExecution(node, task));
69                                        }
70                                }
71                        }
72                        turnOffIdleNodes(resourceManager.getComputingNodes());
73                        adjustOtherFans(notSelectedNodes);
74                        break;
75                }
76                return plan;
77        }
78       
79        private ComputingNode chooseProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) {
80                List<ComputingNode> nodes = filterNodes(resourceManager.getComputingNodes(), task);
81                for(ComputingNode node: nodes){
82                        Integer id = Integer.parseInt(node.getName().split("_")[1]);
83                        if((id >= START_ID  && id <= END_ID)){
84                                if(node.getStatus() == ResourceStatus.UNAVAILABLE) {
85                                        node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
86                                }
87                                return node;
88                        }
89
90                }
91                return null;
92        }
93
94        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
95                        ComputingNode node, TaskInterface<?> task) {
96
97                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
98
99                int cpuRequest;
100                try {
101                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
102                } catch (NoSuchFieldException e) {
103                        cpuRequest = 0;
104                }
105
106                if (cpuRequest != 0) {
107
108                        List<Core> cores = node.getProcessors().get(0).getCores();
109
110                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
111                        for (int i = 0; i < cores.size(); i++) {
112                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
113                                        //choosenResources.add(cores.get(i));
114                                        cpuRequest--;
115                                }
116                        }
117                        if (cpuRequest > 0) {
118                                //return null;
119                        }
120                        choosenResources.add(node.getProcessors().get(0));
121                        ProcessingElements pe = new ProcessingElements();
122                        pe.addAll(choosenResources);
123                        map.put(StandardResourceUnitName.PE, pe);
124                        return map;
125                }
126                return null;
127        }
128       
129        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
130                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
131                for (ComputingNode node : nodes) {
132                        int cpuRequest;
133                        try {
134                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
135                        } catch (NoSuchFieldException e) {
136                                cpuRequest = 0;
137                        }
138
139                        if (cpuRequest != 0) {
140
141                                List<Core> cores = node.getProcessors().get(0).getCores();
142                                if (cores.size() < cpuRequest) {
143                                        continue;
144                                }
145
146                                int freeCores = 0;
147                                for(Core core: cores){
148                                        if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE)
149                                                freeCores++;
150                                }
151                               
152                                if(freeCores != cores.size())
153                                        continue;
154                               
155                                try {
156                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
157                                                continue;
158                                } catch (FileNotFoundException e) {
159                                        continue;
160                                } catch (IOException e) {
161                                        continue;
162                                } catch (MissingResourceException e){
163                                        continue;
164                                }
165                                filteredNodes.add(node);
166                        }
167                }
168               
169                return filteredNodes;
170        }
171
172        private void turnOffIdleNodes(List<ComputingNode> nodes){
173                for(ComputingNode node : nodes){
174                        Processor proc = node.getProcessors().get(0);
175                        int freeCores = 0;
176                        for(Core core: proc.getCores()){
177                                if(core.getStatus() == ResourceStatus.FREE)
178                                        freeCores++;
179                        }
180                       
181                        if(freeCores == proc.getCores().size())
182                                node.getPowerInterface().setPowerState(StandardPowerStateName.OFF);
183                }
184        }
185       
186        private void adjustOtherFans(List<ComputingNode> nodes){
187                for(ComputingNode node : nodes){
188                        if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){
189                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_OFF);
190                        } else {
191                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON);
192                        }
193                }
194        }
195}
Note: See TracBrowser for help on using the repository browser.