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

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