source: DCWoRMS/trunk/src/test/article/recs/plugins/scheduling/RecsExclusivenessEnOptNodePowManSP.java @ 708

Revision 708, 8.8 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 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.ComputingNode;
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.grid.ModuleList;
35import schedframe.scheduling.queue.TaskQueue;
36import schedframe.scheduling.queue.TaskQueueList;
37import schedframe.scheduling.tasks.TaskInterface;
38import test.article.recs.utils.AppType;
39
40public class RecsExclusivenessEnOptNodePowManSP  extends RecsSP {
41
42        private static String TIME_DATA_FILE_NAME = "src/test/article/recs/data/time_data.properties";
43        private static String POWER_DATA_FILE_NAME = "src/test/article/recs/data/power_data.properties";
44       
45        private ResourceBundle powBundle;
46        private ResourceBundle timeBundle;
47       
48        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
49                        ResourceManager resManager, ModuleList modules) {
50
51                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
52                SchedulingPlan plan = new SchedulingPlan();
53                // choose the events types to serve.
54                // Different actions for different events are possible.
55                switch (event.getType()) {
56                case START_TASK_EXECUTION:
57                case TASK_FINISHED:
58                //case TIMER:
59                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
60                        TaskQueue q = queues.get(0);
61                        // check all tasks in queue
62
63                        for (int i = 0; i < q.size(); i++) {
64                                TaskInterface<?> task = q.get(i);
65                                initApplicationType(task);
66                               
67                                // if status of the tasks in READY
68                                if (task.getStatus() == DCWormsTags.READY) {
69                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
70                                        if (choosenResources != null) {
71                                                addToSchedulingPlan(plan, task, choosenResources);
72                                        }/* else {
73                                                if(harnessIdleNodesToWork(task, resourceManager.getComputingNodes()))
74                                                        i--;
75                                        }*/
76                                }
77                        }
78                        turnOffIdleNodes(resourceManager.getComputingNodes());
79                        break;
80                }
81                return plan;
82        }
83
84        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
85                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
86
87                Map<ResourceUnitName, ResourceUnit> map;
88                List<ComputingNode> nodes = resourceManager.getComputingNodes();
89                Collections.sort(nodes, new EnergyComparator(task));
90                for (ComputingNode node : nodes) {
91                        int cpuRequest;
92                        try {
93                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
94                        } catch (NoSuchFieldException e) {
95                                cpuRequest = 0;
96                        }
97
98                        if (cpuRequest != 0) {
99
100                                /*Properties properties = new Properties();
101                                properties.setProperty("type", StandardResourceType.Core.getName());
102                                properties.setProperty("status", ResourceStatus.FREE.toString());
103                                 */
104                                List<Core> cores = node.getProcessors().get(0).getCores();
105                                if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) {
106                                        continue;
107                                }
108
109                                int freeCores = 0;
110                                for(Core core: cores){
111                                        if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE)
112                                                freeCores++;
113                                }
114                               
115                                if(freeCores != cores.size())
116                                        continue;
117                               
118                                if(node.getStatus() == ResourceStatus.UNAVAILABLE) {
119                                        node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
120                                }
121                               
122                                try {
123                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
124                                                continue;
125                                } catch (FileNotFoundException e) {
126                                        continue;
127                                } catch (IOException e) {
128                                        continue;
129                                } catch (MissingResourceException e){
130                                        continue;
131                                }
132                               
133                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
134                                for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
135                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
136                                                choosenResources.add(cores.get(i));
137                                                cpuRequest--;
138                                        }
139                                }
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, ComputingNode node) {
158                String query;
159                query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task);
160                return query;
161        }
162
163        private String getApplicationType(TaskInterface<?> task){       
164                AppType appType = taskToApp.getAppType(task);
165                return appType.toString();
166        }
167       
168        private String getNodeCategory(ComputingNode node){
169
170                return node.getCategory();
171        }
172       
173        private int getFrequency(ComputingNode node){
174                Processor proc = (Processor) node.getProcessors().get(0);
175                double freq = proc.getPowerInterface().getFrequency();
176                return Double.valueOf(freq).intValue();
177        }
178       
179        private int getCoreCnt(TaskInterface<?> task){ 
180                double cpuReq;
181                try {
182                        cpuReq = task.getCpuCntRequest();
183                } catch (NoSuchFieldException e) {
184                                cpuReq = 1;
185                }
186                return Double.valueOf(cpuReq).intValue();
187        }
188       
189        protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{
190                ResourceBundle powBundle = getPowBundle();
191                return Double.valueOf(powBundle.getString(query)).doubleValue();
192        }
193       
194        private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{
195                if(powBundle == null){
196                         powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME));
197                }
198                return powBundle;
199        }
200       
201        protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{
202                ResourceBundle timeBundle = getTimeBundle();
203                return Double.valueOf(timeBundle.getString(query)).doubleValue();
204        }
205
206        private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{
207                if(timeBundle == null){
208                        timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME));
209                }
210                return timeBundle;
211        }
212        class EnergyComparator implements Comparator<ComputingNode>{
213                private TaskInterface<?> task;
214               
215                public EnergyComparator(TaskInterface<?> task){
216                        this.task = task;
217                }
218               
219            public int compare(ComputingNode node1, ComputingNode node2){   
220                double node1EU = Double.MAX_VALUE;
221                double node2EU = Double.MAX_VALUE;
222                try {
223                                node1EU = getMeasuredTime(createQuery(task, node1)) * getMeasuredPower(createQuery(task, node1));
224                                node2EU = getMeasuredTime(createQuery(task, node2)) * getMeasuredPower(createQuery(task, node2));
225                        } catch (FileNotFoundException e) {
226
227                        } catch (IOException e) {
228
229                        } catch (MissingResourceException e){
230                               
231                        }
232                if(node1EU > node2EU)
233                        return 1;
234                else if (node1EU < node2EU)
235                        return -1;
236                else return 0;
237           
238            }
239        }
240       
241        private void turnOffIdleNodes(List<ComputingNode> nodes){
242                for(ComputingNode node : nodes){
243                        Processor proc = node.getProcessors().get(0);
244                        int freeCores = 0;
245                        for(Core core: proc.getCores()){
246                                if(core.getStatus() == ResourceStatus.FREE)
247                                        freeCores++;
248                        }
249                       
250                        if(freeCores == proc.getCores().size())
251                                node.getPowerInterface().setPowerState(StandardPowerStateName.OFF);
252       
253                }
254        }
255       
256        /*private boolean harnessIdleNodesToWork(TaskInterface<?> task, List<ComputingNode> nodes){
257
258                int cpuRequest;
259                try {
260                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
261                } catch (NoSuchFieldException e) {
262                        cpuRequest = 0;
263                }
264                for (int i = 0; i < nodes.size(); i++) {
265                        ComputingNode node = nodes.get(i);
266                        if(node.getPowerInterface().getPowerState() == StandardPowerStateName.OFF){
267
268                                List<Core> cores = node.getProcessors().get(0).getCores();
269                                if (cores.size() < cpuRequest) {
270                                        continue;
271                                }
272                                node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
273                                return true;
274                        }
275                }
276                return false;
277        }*/
278}
Note: See TracBrowser for help on using the repository browser.