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

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