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

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