source: DCWoRMS/trunk/src/test/article/recs/plugins/scheduling/RecsExclusivenessEnOptSP.java @ 734

Revision 734, 8.4 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 RecsExclusivenessEnOptSP 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                                        }
73                                }
74                        }
75
76                        break;
77                }
78                return plan;
79        }
80
81        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
82                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
83
84                Map<ResourceUnitName, ResourceUnit> map;
85                List<ComputingNode> nodes = resourceManager.getComputingNodes();
86                Collections.sort(nodes, new EnergyComparator(task));
87                //System.out.println("*****");
88                for (ComputingNode node : nodes) {
89                        //System.out.println(node.getCategory());
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)
111                                                freeCores++;
112                                }
113                               
114                                if(freeCores != cores.size())
115                                        continue;
116                               
117                                try {
118                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
119                                                continue;
120                                } catch (FileNotFoundException e) {
121                                        continue;
122                                } catch (IOException e) {
123                                        continue;
124                                } catch (MissingResourceException e){
125                                        continue;
126                                }
127                               
128                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
129                                for (int i = 0; i < cores.size(); i++) {
130                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
131                                                //choosenResources.add(cores.get(i));
132                                                cpuRequest--;
133                                        }
134                                }
135                                choosenResources.add(node);
136                                if (cpuRequest > 0) {
137                                        //continue;
138                                }
139                                map = new HashMap<ResourceUnitName, ResourceUnit>();
140                                ProcessingElements pe = new ProcessingElements();
141                                pe.addAll(choosenResources);
142                                map.put(StandardResourceUnitName.PE, pe);
143                                return map;
144
145                        }
146                }
147       
148                return null;
149        }
150       
151
152       
153        protected String createQuery(TaskInterface<?> task, ComputingNode node) {
154                String query;
155                query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task);
156                return query;
157        }
158
159        private String getApplicationType(TaskInterface<?> task){       
160                AppType appType = taskToApp.getAppType(task);
161                return appType.toString();
162        }
163       
164        private String getNodeCategory(ComputingNode node){
165
166                return node.getCategory();
167        }
168       
169        private int getFrequency(ComputingNode node){
170                Processor proc = (Processor) node.getProcessors().get(0);
171                double freq = proc.getPowerInterface().getFrequency();
172                return Double.valueOf(freq).intValue();
173        }
174       
175        private int getCoreCnt(TaskInterface<?> task){ 
176                double cpuReq;
177                try {
178                        cpuReq = task.getCpuCntRequest();
179                } catch (NoSuchFieldException e) {
180                                cpuReq = 1;
181                }
182                return Double.valueOf(cpuReq).intValue();
183        }
184       
185        protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{
186                ResourceBundle powBundle = getPowBundle();
187                return Double.valueOf(powBundle.getString(query)).doubleValue();
188        }
189       
190        private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{
191                if(powBundle == null){
192                         powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME));
193                }
194                return powBundle;
195        }
196       
197        protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{
198                ResourceBundle timeBundle = getTimeBundle();
199                return Double.valueOf(timeBundle.getString(query)).doubleValue();
200        }
201
202        private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{
203                if(timeBundle == null){
204                        timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME));
205                }
206                return timeBundle;
207        }
208        class EnergyComparator implements Comparator<ComputingNode>{
209                private TaskInterface<?> task;
210               
211                public EnergyComparator(TaskInterface<?> task){
212                        this.task = task;
213                }
214               
215            public int compare(ComputingNode node1, ComputingNode node2){   
216                double node1EU = Double.MAX_VALUE;
217                double node2EU = Double.MAX_VALUE;
218                try {
219                                node1EU = getMeasuredTime(createQuery(task, node1)) * Math.abs(getMeasuredPower(createQuery(task, node1)) - node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
220                                //System.out.println("1" + node1.getCategory() +"; " + getApplicationType(task) + ": " + node1EU + "; "+ getMeasuredTime(createQuery(task, node1)) +";" + node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node1)));
221                } catch (FileNotFoundException e) {
222
223                        } catch (IOException e) {
224
225                        } catch (MissingResourceException e){
226                               
227                        } catch (NoSuchFieldException e) {
228
229                        }
230                       
231                try {
232                                node2EU = getMeasuredTime(createQuery(task, node2)) * Math.abs(getMeasuredPower(createQuery(task, node2)) - node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
233                                //System.out.println("2" + node2.getCategory() +"; " + getApplicationType(task) + ": " + node2EU + "; "+ getMeasuredTime(createQuery(task, node2)) +";" + node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node2)));
234                } catch (FileNotFoundException e) {
235
236                        } catch (IOException e) {
237
238                        } catch (MissingResourceException e){
239                               
240                        } catch (NoSuchFieldException e) {
241
242                        }
243                if(node1EU > node2EU)
244                        return 1;
245                else if (node1EU < node2EU)
246                        return -1;
247                else return 0;
248           
249            }
250        }
251}
Note: See TracBrowser for help on using the repository browser.