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

Revision 1498, 7.6 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.profiles.energy.power.StandardPowerStateName;
24import schedframe.resources.units.ProcessingElements;
25import schedframe.resources.units.ResourceUnit;
26import schedframe.resources.units.ResourceUnitName;
27import schedframe.resources.units.StandardResourceUnitName;
28import schedframe.scheduling.manager.resources.ClusterResourceManager;
29import schedframe.scheduling.manager.resources.ResourceManager;
30import schedframe.scheduling.manager.tasks.JobRegistry;
31import schedframe.scheduling.plan.SchedulingPlanInterface;
32import schedframe.scheduling.plan.impl.SchedulingPlan;
33import schedframe.scheduling.plugin.ModuleList;
34import schedframe.scheduling.queue.TaskQueue;
35import schedframe.scheduling.queue.TaskQueueList;
36import schedframe.scheduling.tasks.TaskInterface;
37
38public class RecsExclusivenessEnOptSP extends RecsSP {
39
40        private static String TIME_DATA_FILE_NAME = "src/experiments/simpat2012/recs/data/time_data.properties";
41        private static String POWER_DATA_FILE_NAME = "src/experiments/simpat2012/recs/data/power_data.properties";
42       
43        private ResourceBundle powBundle;
44        private ResourceBundle timeBundle;
45       
46        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
47                        ResourceManager resManager, ModuleList modules) {
48
49                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
50                SchedulingPlan plan = new SchedulingPlan();
51                // choose the events types to serve.
52                // Different actions for different events are possible.
53                switch (event.getType()) {
54                case START_TASK_EXECUTION:
55                case TASK_FINISHED:
56                //case TIMER:
57                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
58                        TaskQueue q = queues.get(0);
59                        // check all tasks in queue
60
61                        for (int i = 0; i < q.size(); i++) {
62                                TaskInterface<?> task = q.get(i);
63                                initApplicationType(task);
64                               
65                                // if status of the tasks in READY
66                                if (task.getStatus() == DCWormsTags.READY) {
67                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
68                                        if (choosenResources != null) {
69                                                addToSchedulingPlan(plan, task, choosenResources);
70                                        }
71                                }
72                        }
73
74                        break;
75                }
76                return plan;
77        }
78
79        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
80                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
81
82                Map<ResourceUnitName, ResourceUnit> map;
83                List<Node> nodes = resourceManager.getNodes();
84                Collections.sort(nodes, new EnergyComparator(task));
85                //System.out.println("*****");
86                for (Node node : nodes) {
87                        //System.out.println(node.getCategory());
88                        int cpuRequest;
89                        try {
90                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
91                        } catch (NoSuchFieldException e) {
92                                cpuRequest = 0;
93                        }
94
95                        if (cpuRequest != 0) {
96
97                                /*Properties properties = new Properties();
98                                properties.setProperty("type", StandardResourceType.Core.getName());
99                                properties.setProperty("status", ResourceStatus.FREE.toString());
100                                 */
101                                List<Core> cores = node.getProcessors().get(0).getCores();
102                                if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) {
103                                        continue;
104                                }
105
106                                int freeCores = 0;
107                                for(Core core: cores){
108                                        if(core.getStatus() == ResourceStatus.FREE)
109                                                freeCores++;
110                                }
111                               
112                                if(freeCores != cores.size())
113                                        continue;
114                               
115                                try {
116                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
117                                                continue;
118                                } catch (FileNotFoundException e) {
119                                        continue;
120                                } catch (IOException e) {
121                                        continue;
122                                } catch (MissingResourceException e){
123                                        continue;
124                                }
125                               
126                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
127                                for (int i = 0; i < cores.size(); i++) {
128                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
129                                                //choosenResources.add(cores.get(i));
130                                                cpuRequest--;
131                                        }
132                                }
133                                choosenResources.add(node);
134                                if (cpuRequest > 0) {
135                                        //continue;
136                                }
137                                map = new HashMap<ResourceUnitName, ResourceUnit>();
138                                ProcessingElements pe = new ProcessingElements();
139                                pe.addAll(choosenResources);
140                                map.put(StandardResourceUnitName.PE, pe);
141                                return map;
142
143                        }
144                }
145       
146                return null;
147        }
148       
149
150       
151        protected String createQuery(TaskInterface<?> task, Node node) {
152                String query;
153                query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task);
154                return query;
155        }
156
157        protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{
158                ResourceBundle powBundle = getPowBundle();
159                return Double.valueOf(powBundle.getString(query)).doubleValue();
160        }
161       
162        private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{
163                if(powBundle == null){
164                         powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME));
165                }
166                return powBundle;
167        }
168       
169        protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{
170                ResourceBundle timeBundle = getTimeBundle();
171                return Double.valueOf(timeBundle.getString(query)).doubleValue();
172        }
173
174        private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{
175                if(timeBundle == null){
176                        timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME));
177                }
178                return timeBundle;
179        }
180        class EnergyComparator implements Comparator<Node>{
181                private TaskInterface<?> task;
182               
183                public EnergyComparator(TaskInterface<?> task){
184                        this.task = task;
185                }
186               
187            public int compare(Node node1, Node node2){   
188                double node1EU = Double.MAX_VALUE;
189                double node2EU = Double.MAX_VALUE;
190                try {
191                                node1EU = getMeasuredTime(createQuery(task, node1)) * Math.abs(getMeasuredPower(createQuery(task, node1)) - node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
192                                //System.out.println("1" + node1.getCategory() +"; " + getApplicationType(task) + ": " + node1EU + "; "+ getMeasuredTime(createQuery(task, node1)) +";" + node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node1)));
193                } catch (FileNotFoundException e) {
194
195                        } catch (IOException e) {
196
197                        } catch (MissingResourceException e){
198                               
199                        } catch (NoSuchFieldException e) {
200
201                        }
202                       
203                try {
204                                node2EU = getMeasuredTime(createQuery(task, node2)) * Math.abs(getMeasuredPower(createQuery(task, node2)) - node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
205                                //System.out.println("2" + node2.getCategory() +"; " + getApplicationType(task) + ": " + node2EU + "; "+ getMeasuredTime(createQuery(task, node2)) +";" + node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node2)));
206                } catch (FileNotFoundException e) {
207
208                        } catch (IOException e) {
209
210                        } catch (MissingResourceException e){
211                               
212                        } catch (NoSuchFieldException e) {
213
214                        }
215                if(node1EU > node2EU)
216                        return 1;
217                else if (node1EU < node2EU)
218                        return -1;
219                else return 0;
220           
221            }
222        }
223}
Note: See TracBrowser for help on using the repository browser.