source: DCWoRMS/branches/coolemall/src/experiments/simpat2012/recs/plugins/scheduling/RecsExclusivenessEnOptNodePowManSP.java @ 1498

Revision 1498, 8.2 KB checked in by wojtekp, 10 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package experiments.simpat2012.recs.plugins.scheduling;
2
3import gridsim.dcworms.DCWormsTags;
4
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.IOException;
8import java.util.ArrayList;
9import java.util.Collections;
10import java.util.Comparator;
11import java.util.HashMap;
12import java.util.List;
13import java.util.Map;
14import java.util.MissingResourceException;
15import java.util.PropertyResourceBundle;
16import java.util.ResourceBundle;
17
18import schedframe.events.scheduling.SchedulingEvent;
19import schedframe.resources.ResourceStatus;
20import schedframe.resources.computing.Node;
21import schedframe.resources.computing.ComputingResource;
22import schedframe.resources.computing.Core;
23import schedframe.resources.computing.Processor;
24import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName;
25import schedframe.resources.units.ProcessingElements;
26import schedframe.resources.units.ResourceUnit;
27import schedframe.resources.units.ResourceUnitName;
28import schedframe.resources.units.StandardResourceUnitName;
29import schedframe.scheduling.manager.resources.ClusterResourceManager;
30import schedframe.scheduling.manager.resources.ResourceManager;
31import schedframe.scheduling.manager.tasks.JobRegistry;
32import schedframe.scheduling.plan.SchedulingPlanInterface;
33import schedframe.scheduling.plan.impl.SchedulingPlan;
34import schedframe.scheduling.plugin.ModuleList;
35import schedframe.scheduling.queue.TaskQueue;
36import schedframe.scheduling.queue.TaskQueueList;
37import schedframe.scheduling.tasks.TaskInterface;
38
39public class RecsExclusivenessEnOptNodePowManSP  extends RecsSP {
40
41        private static String TIME_DATA_FILE_NAME = "src/experiments/simpat2012/recs/data/time_data.properties";
42        private static String POWER_DATA_FILE_NAME = "src/experiments/simpat2012/recs/data/power_data.properties";
43       
44        private ResourceBundle powBundle;
45        private ResourceBundle timeBundle;
46       
47        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
48                        ResourceManager resManager, ModuleList modules) {
49
50                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
51                SchedulingPlan plan = new SchedulingPlan();
52                // choose the events types to serve.
53                // Different actions for different events are possible.
54                switch (event.getType()) {
55                case START_TASK_EXECUTION:
56                case TASK_FINISHED:
57                //case TIMER:
58                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
59                        TaskQueue q = queues.get(0);
60                        // check all tasks in queue
61
62                        for (int i = 0; i < q.size(); i++) {
63                                TaskInterface<?> task = q.get(i);
64                                initApplicationType(task);
65                               
66                                // if status of the tasks in READY
67                                if (task.getStatus() == DCWormsTags.READY) {
68                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
69                                        if (choosenResources != null) {
70                                                addToSchedulingPlan(plan, task, choosenResources);
71                                        }/* else {
72                                                if(harnessIdleNodesToWork(task, resourceManager.getComputingNodes()))
73                                                        i--;
74                                        }*/
75                                }
76                        }
77                        turnOffIdleNodes(resourceManager.getNodes());
78                        break;
79                }
80                return plan;
81        }
82
83        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
84                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
85
86                Map<ResourceUnitName, ResourceUnit> map;
87                List<Node> nodes = resourceManager.getNodes();
88                Collections.sort(nodes, new EnergyComparator(task));
89                for (Node node : nodes) {
90                        int cpuRequest;
91                        try {
92                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
93                        } catch (NoSuchFieldException e) {
94                                cpuRequest = 0;
95                        }
96
97                        if (cpuRequest != 0) {
98
99                                /*Properties properties = new Properties();
100                                properties.setProperty("type", StandardResourceType.Core.getName());
101                                properties.setProperty("status", ResourceStatus.FREE.toString());
102                                 */
103                                List<Core> cores = node.getProcessors().get(0).getCores();
104                                if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) {
105                                        continue;
106                                }
107
108                                int freeCores = 0;
109                                for(Core core: cores){
110                                        if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE)
111                                                freeCores++;
112                                }
113                               
114                                if(freeCores != cores.size())
115                                        continue;
116                               
117                                if(node.getStatus() == ResourceStatus.UNAVAILABLE) {
118                                        node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
119                                }
120                               
121                                try {
122                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
123                                                continue;
124                                } catch (FileNotFoundException e) {
125                                        continue;
126                                } catch (IOException e) {
127                                        continue;
128                                } catch (MissingResourceException e){
129                                        continue;
130                                }
131                               
132                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
133                                for (int i = 0; i < cores.size(); i++) {
134                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
135                                                //choosenResources.add(cores.get(i));
136                                                cpuRequest--;
137                                        }
138                                }
139                                choosenResources.add(node);
140                                if (cpuRequest > 0) {
141                                        //continue;
142                                }
143                                map = new HashMap<ResourceUnitName, ResourceUnit>();
144                                ProcessingElements pe = new ProcessingElements();
145                                pe.addAll(choosenResources);
146                                map.put(StandardResourceUnitName.PE, pe);
147                                return map;
148
149                        }
150                }
151       
152                return null;
153        }
154       
155
156       
157        protected String createQuery(TaskInterface<?> task, Node node) {
158                String query;
159                query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task);
160                return query;
161        }
162
163        protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{
164                ResourceBundle powBundle = getPowBundle();
165                return Double.valueOf(powBundle.getString(query)).doubleValue();
166        }
167       
168        private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{
169                if(powBundle == null){
170                         powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME));
171                }
172                return powBundle;
173        }
174       
175        protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{
176                ResourceBundle timeBundle = getTimeBundle();
177                return Double.valueOf(timeBundle.getString(query)).doubleValue();
178        }
179
180        private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{
181                if(timeBundle == null){
182                        timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME));
183                }
184                return timeBundle;
185        }
186        class EnergyComparator implements Comparator<Node>{
187                private TaskInterface<?> task;
188               
189                public EnergyComparator(TaskInterface<?> task){
190                        this.task = task;
191                }
192               
193            public int compare(Node node1, Node node2){   
194                double node1EU = Double.MAX_VALUE;
195                double node2EU = Double.MAX_VALUE;
196                try {
197                                node1EU = getMeasuredTime(createQuery(task, node1)) * getMeasuredPower(createQuery(task, node1));
198                        } catch (FileNotFoundException e) {
199
200                        } catch (IOException e) {
201
202                        } catch (MissingResourceException e){
203                               
204                        }
205                try {
206                                node2EU = getMeasuredTime(createQuery(task, node2)) * getMeasuredPower(createQuery(task, node2));
207                        } catch (FileNotFoundException e) {
208
209                        } catch (IOException e) {
210
211                        } catch (MissingResourceException e){
212                               
213                        }
214                if(node1EU > node2EU)
215                        return 1;
216                else if (node1EU < node2EU)
217                        return -1;
218                else return 0;
219           
220            }
221        }
222       
223        private void turnOffIdleNodes(List<Node> nodes){
224                for(Node node : nodes){
225                        Processor proc = node.getProcessors().get(0);
226                        int freeCores = 0;
227                        for(Core core: proc.getCores()){
228                                if(core.getStatus() == ResourceStatus.FREE)
229                                        freeCores++;
230                        }
231                       
232                        if(freeCores == proc.getCores().size())
233                                node.getPowerInterface().setPowerState(StandardPowerStateName.OFF);
234       
235                }
236        }
237       
238        /*private boolean harnessIdleNodesToWork(TaskInterface<?> task, List<ComputingNode> nodes){
239
240                int cpuRequest;
241                try {
242                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
243                } catch (NoSuchFieldException e) {
244                        cpuRequest = 0;
245                }
246                for (int i = 0; i < nodes.size(); i++) {
247                        ComputingNode node = nodes.get(i);
248                        if(node.getPowerInterface().getPowerState() == StandardPowerStateName.OFF){
249
250                                List<Core> cores = node.getProcessors().get(0).getCores();
251                                if (cores.size() < cpuRequest) {
252                                        continue;
253                                }
254                                node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
255                                return true;
256                        }
257                }
258                return false;
259        }*/
260}
Note: See TracBrowser for help on using the repository browser.