1 | package test.drs_tst.recs.plugins.scheduling; |
---|
2 | |
---|
3 | import gridsim.dcworms.DCWormsTags; |
---|
4 | |
---|
5 | import java.io.FileInputStream; |
---|
6 | import java.io.FileNotFoundException; |
---|
7 | import java.io.IOException; |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.Collections; |
---|
10 | import java.util.Comparator; |
---|
11 | import java.util.HashMap; |
---|
12 | import java.util.List; |
---|
13 | import java.util.Map; |
---|
14 | import java.util.MissingResourceException; |
---|
15 | import java.util.PropertyResourceBundle; |
---|
16 | import java.util.ResourceBundle; |
---|
17 | |
---|
18 | import schedframe.events.scheduling.SchedulingEvent; |
---|
19 | import schedframe.resources.ResourceStatus; |
---|
20 | import schedframe.resources.computing.Node; |
---|
21 | import schedframe.resources.computing.ComputingResource; |
---|
22 | import schedframe.resources.computing.Core; |
---|
23 | import schedframe.resources.computing.Processor; |
---|
24 | import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; |
---|
25 | import schedframe.resources.units.ProcessingElements; |
---|
26 | import schedframe.resources.units.ResourceUnit; |
---|
27 | import schedframe.resources.units.ResourceUnitName; |
---|
28 | import schedframe.resources.units.StandardResourceUnitName; |
---|
29 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
30 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
31 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
32 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
33 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
34 | import schedframe.scheduling.plugin.ModuleList; |
---|
35 | import schedframe.scheduling.queue.TaskQueue; |
---|
36 | import schedframe.scheduling.queue.TaskQueueList; |
---|
37 | import schedframe.scheduling.tasks.TaskInterface; |
---|
38 | import test.drs_tst.recs.utils.AppType; |
---|
39 | |
---|
40 | public 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 | 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.sort(nodes, new EnergyComparator(task)); |
---|
87 | //System.out.println("*****"); |
---|
88 | for (Node node : nodes) { |
---|
89 | //System.out.println(node.getCategory()); |
---|
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 | |
---|
117 | try { |
---|
118 | if(!getExecutiveness(createExecutivenessQuery(task, node))) |
---|
119 | continue; |
---|
120 | } catch (FileNotFoundException e) { |
---|
121 | continue; |
---|
122 | } catch (IOException e) { |
---|
123 | continue; |
---|
124 | } catch (MissingResourceException e){ |
---|
125 | continue; |
---|
126 | } |
---|
127 | |
---|
128 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
129 | for (int i = 0; i < cores.size(); i++) { |
---|
130 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
131 | //choosenResources.add(cores.get(i)); |
---|
132 | cpuRequest--; |
---|
133 | } |
---|
134 | } |
---|
135 | choosenResources.add(node); |
---|
136 | if (cpuRequest > 0) { |
---|
137 | //continue; |
---|
138 | } |
---|
139 | map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
140 | ProcessingElements pe = new ProcessingElements(); |
---|
141 | pe.addAll(choosenResources); |
---|
142 | map.put(StandardResourceUnitName.PE, pe); |
---|
143 | return map; |
---|
144 | |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | return null; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | protected String createQuery(TaskInterface<?> task, Node node) { |
---|
154 | String query; |
---|
155 | query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task); |
---|
156 | return query; |
---|
157 | } |
---|
158 | |
---|
159 | private String getApplicationType(TaskInterface<?> task){ |
---|
160 | AppType appType = taskToApp.getAppType(task); |
---|
161 | return appType.toString(); |
---|
162 | } |
---|
163 | |
---|
164 | private String getNodeCategory(Node node){ |
---|
165 | |
---|
166 | return node.getCategory(); |
---|
167 | } |
---|
168 | |
---|
169 | private int getFrequency(Node node){ |
---|
170 | Processor proc = (Processor) node.getProcessors().get(0); |
---|
171 | double freq = proc.getPowerInterface().getFrequency(); |
---|
172 | return Double.valueOf(freq).intValue(); |
---|
173 | } |
---|
174 | |
---|
175 | private int getCoreCnt(TaskInterface<?> task){ |
---|
176 | |
---|
177 | double cpuReq; |
---|
178 | try { |
---|
179 | cpuReq = task.getCpuCntRequest(); |
---|
180 | } catch (NoSuchFieldException e) { |
---|
181 | cpuReq = 1; |
---|
182 | } |
---|
183 | return Double.valueOf(cpuReq).intValue(); |
---|
184 | } |
---|
185 | |
---|
186 | protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{ |
---|
187 | ResourceBundle powBundle = getPowBundle(); |
---|
188 | return Double.valueOf(powBundle.getString(query)).doubleValue(); |
---|
189 | } |
---|
190 | |
---|
191 | private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{ |
---|
192 | if(powBundle == null){ |
---|
193 | powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME)); |
---|
194 | } |
---|
195 | return powBundle; |
---|
196 | } |
---|
197 | |
---|
198 | protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{ |
---|
199 | ResourceBundle timeBundle = getTimeBundle(); |
---|
200 | return Double.valueOf(timeBundle.getString(query)).doubleValue(); |
---|
201 | } |
---|
202 | |
---|
203 | private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{ |
---|
204 | if(timeBundle == null){ |
---|
205 | timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME)); |
---|
206 | } |
---|
207 | return timeBundle; |
---|
208 | } |
---|
209 | class EnergyComparator implements Comparator<Node>{ |
---|
210 | private TaskInterface<?> task; |
---|
211 | |
---|
212 | public EnergyComparator(TaskInterface<?> task){ |
---|
213 | this.task = task; |
---|
214 | } |
---|
215 | |
---|
216 | public int compare(Node node1, Node node2){ |
---|
217 | double node1EU = Double.MAX_VALUE; |
---|
218 | double node2EU = Double.MAX_VALUE; |
---|
219 | try { |
---|
220 | node1EU = getMeasuredTime(createQuery(task, node1)) * Math.abs(getMeasuredPower(createQuery(task, node1)) - node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); |
---|
221 | //System.out.println("1" + node1.getCategory() +"; " + getApplicationType(task) + ": " + node1EU + "; "+ getMeasuredTime(createQuery(task, node1)) +";" + node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node1))); |
---|
222 | } catch (FileNotFoundException e) { |
---|
223 | |
---|
224 | } catch (IOException e) { |
---|
225 | |
---|
226 | } catch (MissingResourceException e){ |
---|
227 | |
---|
228 | } catch (NoSuchFieldException e) { |
---|
229 | |
---|
230 | } |
---|
231 | |
---|
232 | try { |
---|
233 | node2EU = getMeasuredTime(createQuery(task, node2)) * Math.abs(getMeasuredPower(createQuery(task, node2)) - node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); |
---|
234 | //System.out.println("2" + node2.getCategory() +"; " + getApplicationType(task) + ": " + node2EU + "; "+ getMeasuredTime(createQuery(task, node2)) +";" + node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node2))); |
---|
235 | } catch (FileNotFoundException e) { |
---|
236 | |
---|
237 | } catch (IOException e) { |
---|
238 | |
---|
239 | } catch (MissingResourceException e){ |
---|
240 | |
---|
241 | } catch (NoSuchFieldException e) { |
---|
242 | |
---|
243 | } |
---|
244 | if(node1EU > node2EU) |
---|
245 | return 1; |
---|
246 | else if (node1EU < node2EU) |
---|
247 | return -1; |
---|
248 | else return 0; |
---|
249 | |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|