source: DCWoRMS/branches/coolemall/src/test/thermal/recs/plugins/scheduling/RecsExclusivenessMaxTempOptSP.java @ 1399

Revision 1399, 10.1 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.thermal.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;
38import test.thermal.recs.utils.AppType;
39import test.thermal.recs.plugins.energy.ThermalConstants;
40
41public class RecsExclusivenessMaxTempOptSP extends RecsSP {
42
43        private static String TIME_DATA_FILE_NAME = "src/test/thermal/recs/data/time_data.properties";
44        private static String POWER_DATA_FILE_NAME = "src/test/thermal/recs/data/power_data.properties";
45       
46        private ResourceBundle powBundle;
47        private ResourceBundle timeBundle;
48       
49        // virtual nodes are to keep track of the intermediate power and temperature between job assignments
50        private List<VirtualNode> virtualNodes = new ArrayList<VirtualNode>();
51        private boolean firstCall = true;
52       
53        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
54                        ResourceManager resManager, ModuleList modules) {
55
56                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
57                SchedulingPlan plan = new SchedulingPlan();
58               
59                List<Node> nodes = resourceManager.getNodes();
60               
61                // create the virtualNodes on the first scheduling call
62                if (firstCall){
63                       
64                        VirtualNode dummyVN = new VirtualNode(0);
65                        virtualNodes.add(dummyVN);
66                       
67                        int numNodes = nodes.size();
68                        for (int i = 1; i <= numNodes; i++){
69                                VirtualNode vn = new VirtualNode(i);
70                                virtualNodes.add(vn);
71                        }
72                        firstCall = false;
73                }
74               
75                // update the power of the virtual nodes
76                for (Node node : nodes){
77                        double power = node.getPowerInterface().getRecentPowerUsage().getValue();
78                        int nodeId = Integer.parseInt(node.getName().split("_")[1]);
79                        virtualNodes.get(nodeId).setPower(power);
80                }
81               
82                // update the temperature of the virtual nodes
83                for (int i = 1; i <= 9; i++){
84                        VirtualNode vn1 = virtualNodes.get(i+9);
85                        VirtualNode vn2 = virtualNodes.get(i);
86                       
87                        double power1 = vn1.getPower();
88                        double power2 = vn2.getPower();
89                       
90                        double tout = ThermalConstants.calculateOutletTemp(power1, power2);
91                        vn1.setTemperature(tout);
92                        vn2.setTemperature(tout);
93                }
94               
95                // choose the events types to serve.
96                // Different actions for different events are possible.
97                switch (event.getType()) {
98                case START_TASK_EXECUTION:
99                case TASK_FINISHED:
100                //case TIMER:
101                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
102                        TaskQueue q = queues.get(0);
103                        // check all tasks in queue
104
105                        for (int i = 0; i < q.size(); i++) {
106                                TaskInterface<?> task = q.get(i);
107                                initApplicationType(task);
108                               
109                                // if status of the tasks in READY
110                                if (task.getStatus() == DCWormsTags.READY) {
111                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
112                                        if (choosenResources != null) {
113                                                addToSchedulingPlan(plan, task, choosenResources);
114                                        }
115                                }
116                        }
117
118                        break;
119                }
120                return plan;
121        }
122
123        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
124                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
125
126                Map<ResourceUnitName, ResourceUnit> map;
127                List<Node> nodes = resourceManager.getNodes();
128                Collections.shuffle(nodes);
129                Collections.sort(nodes, new TemperatureComparator(task));
130                for (Node node : nodes) {
131                        int cpuRequest;
132                        try {
133                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
134                        } catch (NoSuchFieldException e) {
135                                cpuRequest = 0;
136                        }
137
138                        if (cpuRequest != 0) {
139
140                               
141                                List<Core> cores = node.getProcessors().get(0).getCores();
142                                if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) {
143                                        continue;
144                                }
145
146                                int freeCores = 0;
147                                for(Core core: cores){
148                                        if(core.getStatus() == ResourceStatus.FREE)
149                                                freeCores++;
150                                }
151                               
152                                if(freeCores != cores.size())
153                                        continue;
154                               
155                                try {
156                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
157                                                continue;
158                                } catch (FileNotFoundException e) {
159                                        continue;
160                                } catch (IOException e) {
161                                        continue;
162                                } catch (MissingResourceException e){
163                                        continue;
164                                }
165                               
166                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
167                                for (int i = 0; i < cores.size(); i++) {
168                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
169                                                //choosenResources.add(cores.get(i));
170                                                cpuRequest--;
171                                        }
172                                }
173                                choosenResources.add(node);
174                                if (cpuRequest > 0) {
175                                        //continue;
176                                }
177                                map = new HashMap<ResourceUnitName, ResourceUnit>();
178                                ProcessingElements pe = new ProcessingElements();
179                                pe.addAll(choosenResources);
180                                map.put(StandardResourceUnitName.PE, pe);
181                               
182                                // update the power and temperature of the virtualNode corresponding to the node
183                                int nodeId = Integer.parseInt(node.getName().split("_")[1]);
184                                VirtualNode vn = virtualNodes.get(nodeId);
185                               
186                                // power update
187                                double extraPower = 0;
188                        try{
189                                extraPower = Math.abs(getMeasuredPower(createQuery(task, node)) - node.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
190                            } catch (FileNotFoundException e) {
191               
192                                } catch (IOException e) {
193               
194                                } catch (MissingResourceException e){
195                                       
196                        } catch (NoSuchFieldException e) {
197
198                        }
199                        vn.setPower(vn.getPower() + extraPower);
200                       
201                                // temperature update
202                        double delta = 1;
203                        VirtualNode vn2;
204                        if (nodeId >= 1 && nodeId <= 9){
205                                delta = ThermalConstants.delta2;
206                                vn2 = virtualNodes.get(nodeId + 9);
207                        }
208                        else{
209                                delta = ThermalConstants.delta1;
210                                vn2 = virtualNodes.get(nodeId - 9);
211                        }
212                        double extraTemp = delta * (extraPower/(ThermalConstants.Q * ThermalConstants.C * ThermalConstants.ro));
213                        vn.setTemperature(vn.getTemperature() + extraTemp);
214                        vn2.setTemperature(vn2.getTemperature() + extraTemp);
215                               
216                                return map;
217
218                        }
219                }
220       
221                return null;
222        }
223       
224        protected String createQuery(TaskInterface<?> task, Node node) {
225                String query;
226                query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task);
227                return query;
228        }
229       
230        protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{
231                ResourceBundle powBundle = getPowBundle();
232                return Double.valueOf(powBundle.getString(query)).doubleValue();
233        }
234       
235        private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{
236                if(powBundle == null){
237                         powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME));
238                }
239                return powBundle;
240        }
241       
242        protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{
243                ResourceBundle timeBundle = getTimeBundle();
244                return Double.valueOf(timeBundle.getString(query)).doubleValue();
245        }
246
247        private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{
248                if(timeBundle == null){
249                        timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME));
250                }
251                return timeBundle;
252        }
253       
254        class TemperatureComparator implements Comparator<Node>{
255                private TaskInterface<?> task;
256               
257                public TemperatureComparator(TaskInterface<?> task){
258                        this.task = task;
259                }
260               
261            public int compare(Node node1, Node node2){   
262               
263                int id1 = Integer.parseInt(node1.getName().split("_")[1]);
264                int id2 = Integer.parseInt(node2.getName().split("_")[1]);
265               
266                double currentNode1Temp = virtualNodes.get(id1).getTemperature();
267                double currentNode2Temp = virtualNodes.get(id2).getTemperature();
268               
269                double power1 = 0;
270                double power2 = 0;
271                try{
272                        power1 = Math.abs(getMeasuredPower(createQuery(task, node1)) - node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
273                        power2 = Math.abs(getMeasuredPower(createQuery(task, node2)) - node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
274                    } catch (FileNotFoundException e) {
275       
276                        } catch (IOException e) {
277       
278                        } catch (MissingResourceException e){
279                               
280                } catch (NoSuchFieldException e) {
281
282                }
283               
284                double deltaOne = 1, deltaTwo = 1;
285                if (id1 >= 1 && id1 <= 9) // outlet
286                        deltaOne = ThermalConstants.delta2;
287                else if (id1 >= 10 && id1 <= 18) // inlet
288                        deltaOne = ThermalConstants.delta1;
289                if (id2 >= 1 && id2 <= 9) // outlet
290                        deltaTwo = ThermalConstants.delta2;
291                else if (id2 >= 10 && id2 <= 18) // inlet
292                        deltaTwo = ThermalConstants.delta1;
293               
294                double afterNode1Temp = currentNode1Temp + deltaOne * (power1/(ThermalConstants.Q * ThermalConstants.C * ThermalConstants.ro));
295                double afterNode2Temp = currentNode2Temp + deltaTwo * (power2/(ThermalConstants.Q * ThermalConstants.C * ThermalConstants.ro));
296               
297                if (afterNode1Temp > afterNode2Temp)
298                                return 1;
299                        else if (afterNode1Temp < afterNode2Temp)
300                                return -1;
301                        else
302                                return 0;
303           
304            }
305        }
306}
Note: See TracBrowser for help on using the repository browser.