source: DCWoRMS/branches/coolemall/src/test/drs_tst/recs/plugins/scheduling/RecsExclusivenessEnOptDFSSP.java @ 1399

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