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

Revision 658, 7.2 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 java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.util.ArrayList;
7import java.util.Collections;
8import java.util.Comparator;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.MissingResourceException;
13import java.util.PropertyResourceBundle;
14import java.util.ResourceBundle;
15
16import schedframe.events.scheduling.SchedulingEvent;
17import schedframe.resources.ResourceStatus;
18import schedframe.resources.computing.ComputingNode;
19import schedframe.resources.computing.ComputingResource;
20import schedframe.resources.computing.Core;
21import schedframe.resources.computing.Processor;
22import schedframe.resources.units.ProcessingElements;
23import schedframe.resources.units.ResourceUnit;
24import schedframe.resources.units.ResourceUnitName;
25import schedframe.resources.units.StandardResourceUnitName;
26import schedframe.scheduling.manager.resources.ClusterResourceManager;
27import schedframe.scheduling.manager.resources.ResourceManager;
28import schedframe.scheduling.manager.tasks.JobRegistry;
29import schedframe.scheduling.plan.SchedulingPlanInterface;
30import schedframe.scheduling.plan.impl.SchedulingPlan;
31import schedframe.scheduling.plugin.grid.ModuleList;
32import schedframe.scheduling.queue.TaskQueue;
33import schedframe.scheduling.queue.TaskQueueList;
34import schedframe.scheduling.tasks.TaskInterface;
35import test.article.recs.utils.AppType;
36import test.article.recs.utils.TaskToApp;
37import example.localplugin.BaseLocalSchedulingPlugin;
38import gridsim.dcworms.DCWormsTags;
39
40public class RecsExclusivenessEnOptSP extends BaseLocalSchedulingPlugin {
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                                // if status of the tasks in READY
68                                if (task.getStatus() == DCWormsTags.READY) {
69                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
70                                        if (choosenResources != null) {
71                                                addToSchedulingPlan(plan, task, choosenResources);
72                                        }
73                                }
74                        }
75
76                        break;
77                }
78                return plan;
79        }
80
81        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
82                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
83
84                Map<ResourceUnitName, ResourceUnit> map;
85                List<ComputingNode> nodes = resourceManager.getComputingNodes();
86                Collections.sort(nodes, new EnergyComparator(task));
87                for (ComputingNode node : nodes) {
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                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
115                                for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
116                                        if (cores.get(i).getStatus() == ResourceStatus.FREE) {
117                                                choosenResources.add(cores.get(i));
118                                                cpuRequest--;
119                                        }
120                                }
121                                if (cpuRequest > 0) {
122                                        continue;
123                                }
124                                map = new HashMap<ResourceUnitName, ResourceUnit>();
125                                ProcessingElements pe = new ProcessingElements();
126                                pe.addAll(choosenResources);
127                                map.put(StandardResourceUnitName.PE, pe);
128                                return map;
129
130                        }
131                }
132       
133                return null;
134        }
135       
136
137       
138        protected String createQuery(TaskInterface<?> task, ComputingNode node) {
139                String query;
140                query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task);
141                return query;
142        }
143
144        private String getApplicationType(TaskInterface<?> task){       
145                AppType appType = taskToApp.getAppType(task);
146                return appType.toString();
147        }
148       
149        private String getNodeCategory(ComputingNode node){
150
151                return node.getCategory();
152        }
153       
154        private int getFrequency(ComputingNode node){
155                Processor proc = (Processor) node.getProcessors().get(0);
156                double freq = proc.getPowerInterface().getFrequency();
157                return Double.valueOf(freq).intValue();
158        }
159       
160        private int getCoreCnt(TaskInterface<?> task){ 
161                double cpuReq;
162                try {
163                        cpuReq = task.getCpuCntRequest();
164                } catch (NoSuchFieldException e) {
165                                cpuReq = 1;
166                }
167                return Double.valueOf(cpuReq).intValue();
168        }
169       
170        protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{
171                ResourceBundle powBundle = getPowBundle();
172                return Double.valueOf(powBundle.getString(query)).doubleValue();
173        }
174       
175        private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{
176                if(powBundle == null){
177                         powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME));
178                }
179                return powBundle;
180        }
181       
182        protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{
183                ResourceBundle timeBundle = getTimeBundle();
184                return Double.valueOf(timeBundle.getString(query)).doubleValue();
185        }
186
187        private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{
188                if(timeBundle == null){
189                        timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME));
190                }
191                return timeBundle;
192        }
193        class EnergyComparator implements Comparator<ComputingNode>{
194                private TaskInterface<?> task;
195               
196                public EnergyComparator(TaskInterface<?> task){
197                        this.task = task;
198                }
199               
200            public int compare(ComputingNode node1, ComputingNode node2){   
201                double node1EU = Double.MAX_VALUE;
202                double node2EU = Double.MAX_VALUE;
203                try {
204                                node1EU = getMeasuredTime(createQuery(task, node1)) * getMeasuredPower(createQuery(task, node1));
205                                node2EU = getMeasuredTime(createQuery(task, node2)) * getMeasuredPower(createQuery(task, node2));
206                        } catch (FileNotFoundException e) {
207
208                        } catch (IOException e) {
209
210                        } catch (MissingResourceException e){
211                               
212                        }
213                if(node1EU > node2EU)
214                        return 1;
215                else if (node1EU < node2EU)
216                        return -1;
217                else return 0;
218           
219            }
220        }
221}
Note: See TracBrowser for help on using the repository browser.