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

Revision 1399, 6.8 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.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/test/thermal/recs/data/time_data.properties";
41        private static String POWER_DATA_FILE_NAME = "src/test/thermal/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.shuffle(nodes);
85                Collections.sort(nodes, new EnergyComparator(task));
86                for (Node node : nodes) {
87                        int cpuRequest;
88                        try {
89                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
90                        } catch (NoSuchFieldException e) {
91                                cpuRequest = 0;
92                        }
93
94                        if (cpuRequest != 0) {
95
96                               
97                                List<Core> cores = node.getProcessors().get(0).getCores();
98                                if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) {
99                                        continue;
100                                }
101
102                                int freeCores = 0;
103                                for(Core core: cores){
104                                        if(core.getStatus() == ResourceStatus.FREE)
105                                                freeCores++;
106                                }
107                               
108                                if(freeCores != cores.size())
109                                        continue;
110                               
111                                try {
112                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
113                                                continue;
114                                } catch (FileNotFoundException e) {
115                                        continue;
116                                } catch (IOException e) {
117                                        continue;
118                                } catch (MissingResourceException e){
119                                        continue;
120                                }
121                               
122                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
123                                for (int i = 0; i < cores.size(); i++) {
124                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
125                                                //choosenResources.add(cores.get(i));
126                                                cpuRequest--;
127                                        }
128                                }
129                                choosenResources.add(node);
130                                if (cpuRequest > 0) {
131                                        //continue;
132                                }
133                                map = new HashMap<ResourceUnitName, ResourceUnit>();
134                                ProcessingElements pe = new ProcessingElements();
135                                pe.addAll(choosenResources);
136                                map.put(StandardResourceUnitName.PE, pe);
137                                return map;
138
139                        }
140                }
141       
142                return null;
143        }
144       
145
146       
147        protected String createQuery(TaskInterface<?> task, Node node) {
148                String query;
149                query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task);
150                return query;
151        }
152
153        protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{
154                ResourceBundle powBundle = getPowBundle();
155                return Double.valueOf(powBundle.getString(query)).doubleValue();
156        }
157       
158        private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{
159                if(powBundle == null){
160                         powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME));
161                }
162                return powBundle;
163        }
164       
165        protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{
166                ResourceBundle timeBundle = getTimeBundle();
167                return Double.valueOf(timeBundle.getString(query)).doubleValue();
168        }
169
170        private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{
171                if(timeBundle == null){
172                        timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME));
173                }
174                return timeBundle;
175        }
176        class EnergyComparator implements Comparator<Node>{
177                private TaskInterface<?> task;
178               
179                public EnergyComparator(TaskInterface<?> task){
180                        this.task = task;
181                }
182               
183            public int compare(Node node1, Node node2){   
184                double node1EU = Double.MAX_VALUE;
185                double node2EU = Double.MAX_VALUE;
186                try {
187                                node1EU = getMeasuredTime(createQuery(task, node1)) * Math.abs(getMeasuredPower(createQuery(task, node1)) - node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
188                } catch (FileNotFoundException e) {
189
190                        } catch (IOException e) {
191
192                        } catch (MissingResourceException e){
193                               
194                        } catch (NoSuchFieldException e) {
195
196                        }
197                       
198                try {
199                                node2EU = getMeasuredTime(createQuery(task, node2)) * Math.abs(getMeasuredPower(createQuery(task, node2)) - node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON));
200                } catch (FileNotFoundException e) {
201
202                        } catch (IOException e) {
203
204                        } catch (MissingResourceException e){
205                               
206                        } catch (NoSuchFieldException e) {
207
208                        }
209                if(node1EU > node2EU)
210                        return 1;
211                else if (node1EU < node2EU)
212                        return -1;
213                else return 0;
214           
215            }
216        }
217}
Note: See TracBrowser for help on using the repository browser.