1 | package experiments.simpat2014.models.basic; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Map; |
---|
7 | |
---|
8 | import schedframe.events.scheduling.SchedulingEvent; |
---|
9 | import schedframe.events.scheduling.TaskFinishedEvent; |
---|
10 | import schedframe.resources.ResourceStatus; |
---|
11 | import schedframe.resources.StandardResourceType; |
---|
12 | import schedframe.resources.computing.ComputingResource; |
---|
13 | import schedframe.resources.computing.Node; |
---|
14 | import schedframe.resources.computing.Processor; |
---|
15 | import schedframe.resources.computing.profiles.energy.ResourceEvent; |
---|
16 | import schedframe.resources.computing.profiles.energy.ResourceEventType; |
---|
17 | import schedframe.resources.computing.profiles.energy.airthroughput.AirflowStateName; |
---|
18 | import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName; |
---|
19 | import schedframe.resources.computing.profiles.energy.power.CustomPowerStateName; |
---|
20 | import schedframe.resources.computing.profiles.energy.power.PowerState; |
---|
21 | import schedframe.resources.computing.profiles.energy.power.PowerUsage; |
---|
22 | import schedframe.resources.devices.Device; |
---|
23 | import schedframe.resources.devices.Fan; |
---|
24 | import schedframe.resources.units.ProcessingElements; |
---|
25 | import schedframe.resources.units.ResourceUnit; |
---|
26 | import schedframe.resources.units.ResourceUnitName; |
---|
27 | import schedframe.resources.units.StandardResourceUnitName; |
---|
28 | import schedframe.scheduling.ResourceItem; |
---|
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.manager.tasks.JobRegistryImpl; |
---|
33 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
34 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
35 | import schedframe.scheduling.plugin.ModuleList; |
---|
36 | import schedframe.scheduling.queue.TaskQueue; |
---|
37 | import schedframe.scheduling.queue.TaskQueueList; |
---|
38 | import schedframe.scheduling.tasks.TaskInterface; |
---|
39 | import simulator.DataCenterWorkloadSimulator; |
---|
40 | import dcworms.schedframe.scheduling.ExecTask; |
---|
41 | import eduni.simjava.Sim_system; |
---|
42 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
43 | import experiments.simpat2014.EnvironmentConditions; |
---|
44 | import gridsim.dcworms.DCWormsTags; |
---|
45 | |
---|
46 | public class FCFSBF_RandomPluginMigrate extends BaseLocalSchedulingPlugin { |
---|
47 | |
---|
48 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
49 | ResourceManager resManager, ModuleList modules) { |
---|
50 | |
---|
51 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
52 | List<Processor> processors = resourceManager.getProcessors(); |
---|
53 | SchedulingPlan plan = new SchedulingPlan(); |
---|
54 | //for(Node node: resourceManager.getNodes()){ |
---|
55 | // System.out.println(node.getFullName()); |
---|
56 | //} |
---|
57 | // choose the events types to serve. |
---|
58 | // Different actions for different events are possible. |
---|
59 | switch (event.getType()) { |
---|
60 | |
---|
61 | |
---|
62 | case TIMER: |
---|
63 | |
---|
64 | System.out.println("##########################" + Sim_system.clock()); |
---|
65 | DataCenterWorkloadSimulator.getEventManager().sendToResources(StandardResourceType.Processor, 0.0, new ResourceEvent(ResourceEventType.TIMER, null)); |
---|
66 | //DataCenterWorkloadSimulator.getEventManager().sendToAllSchedulers(EnvironmentConditions.SYSTEM_UDPATE_INTERVAL, DCWormsTags.TIMER, "Test"); |
---|
67 | break; |
---|
68 | |
---|
69 | case START_TASK_EXECUTION: |
---|
70 | case TASK_FINISHED: |
---|
71 | |
---|
72 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
73 | TaskQueue q = queues.get(0); |
---|
74 | // check all tasks in queue |
---|
75 | |
---|
76 | for (int i = 0; i < q.size(); i++) { |
---|
77 | TaskInterface<?> task = q.get(i); |
---|
78 | // if status of the tasks in READY |
---|
79 | if (task.getStatus() == DCWormsTags.READY) { |
---|
80 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task); |
---|
81 | if (choosenResources != null) { |
---|
82 | addToSchedulingPlan(plan, task, choosenResources); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | break; |
---|
88 | case RESOURCE_TEMPERATURE_LIMIT_EXCEEDED: |
---|
89 | System.out.println("tuning fans"); |
---|
90 | ComputingResource cr = resourceManager.getResourceByName(event.getSource()); |
---|
91 | double temp = cr.getThermalInterface().getRecentTemperature().getValue(); |
---|
92 | double prevTemp; |
---|
93 | if(cr.getThermalInterface().getTemperatureHistory().size() == 1) |
---|
94 | prevTemp = temp; |
---|
95 | else { |
---|
96 | prevTemp = cr.getThermalInterface().getTemperatureHistory().get(cr.getThermalInterface().getTemperatureHistory().size() - 2).getValue(); |
---|
97 | } |
---|
98 | |
---|
99 | System.out.println(prevTemp+ "; " + temp); |
---|
100 | boolean getingHotter = prevTemp < temp; |
---|
101 | Node n = (Node) cr.getParent(); |
---|
102 | for(Device d: n.getParent().getResourceCharacteristic().getDevices()){ |
---|
103 | if(Integer.valueOf(d.getName().split("_")[1]) == Integer.valueOf(n.getName().split("_")[1])){ |
---|
104 | Fan f = (Fan) d; |
---|
105 | int speed; |
---|
106 | try{ |
---|
107 | speed= Integer.valueOf(f.getAirflowInterface().getAirflowState().getLabel().split("_")[1]).intValue(); |
---|
108 | } catch(Exception e){ |
---|
109 | speed = 0; |
---|
110 | } |
---|
111 | if(getingHotter){ |
---|
112 | if(temp > EnvironmentConditions.tempLevelLow2Medium && temp < EnvironmentConditions.tempLevelMedium2High){ |
---|
113 | if(speed < 60){ |
---|
114 | System.out.println("+++++++++ low 2 medium"); |
---|
115 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_60")); |
---|
116 | } |
---|
117 | } else if (temp > EnvironmentConditions.tempLevelMedium2High && temp < EnvironmentConditions.tempLevelHigh2Max) { |
---|
118 | if(speed < 80){ |
---|
119 | System.out.println("++++++++medium 2 high"); |
---|
120 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_80")); |
---|
121 | } |
---|
122 | } else if (temp > EnvironmentConditions.tempLevelHigh2Max) { |
---|
123 | if(speed < 100){ |
---|
124 | System.out.println("++++++++high 2 max"); |
---|
125 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_100")); |
---|
126 | } |
---|
127 | } |
---|
128 | } else { |
---|
129 | if(temp > EnvironmentConditions.tempLevelHigh2Medium && temp < EnvironmentConditions.tempLevelMax2High){ |
---|
130 | System.out.println("-------- max 2 high"); |
---|
131 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_80")); |
---|
132 | } else if(temp > EnvironmentConditions.tempLevelMedium2Low && temp < EnvironmentConditions.tempLevelHigh2Medium){ |
---|
133 | System.out.println("-------- high 2 medium"); |
---|
134 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_60")); |
---|
135 | } else if (temp < EnvironmentConditions.tempLevelMedium2Low) { |
---|
136 | System.out.println("-------- medium 2 low"); |
---|
137 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_30")); |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /*AirflowStateName state = f.getAirflowInterface().getAirflowState(); |
---|
144 | try{ |
---|
145 | Integer level = Integer.valueOf(state.getLabel().split("_")[1]); |
---|
146 | Integer newLevel = level + 10; |
---|
147 | if(newLevel <= 100) |
---|
148 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_" + newLevel)); |
---|
149 | } catch (Exception e){ |
---|
150 | f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_10")); |
---|
151 | } */ |
---|
152 | } |
---|
153 | //optimizeEnergyUsage(jobRegistry, resourceManager , src); |
---|
154 | break; |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | return plan; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | // IRIT |
---|
164 | private void firstFitConsolidation(JobRegistry jobRegistry, ClusterResourceManager resourceManager, SchedulingEvent event) { |
---|
165 | Node underLoadedNode = getNodeFromEvent(jobRegistry, (TaskFinishedEvent) event); |
---|
166 | double remainingLoad = underLoadedNode.getLoadInterface().getRecentUtilization().getValue(); |
---|
167 | if(remainingLoad == 0) { return; } |
---|
168 | |
---|
169 | PowerUsage pow = underLoadedNode.getPowerInterface().getRecentPowerUsage(); |
---|
170 | System.out.println(" last : "+pow.getValue()); |
---|
171 | System.out.println(" Estimate : "+getPowerConsumptionAtLoad(underLoadedNode, remainingLoad)); |
---|
172 | |
---|
173 | for(Node node: resourceManager.getNodes()) { |
---|
174 | double destLoad = node.getLoadInterface().getRecentUtilization().getValue(); |
---|
175 | |
---|
176 | if((node != underLoadedNode) && (destLoad!=0) && (destLoad + remainingLoad <= 100)) { |
---|
177 | if(destLoad < remainingLoad) { |
---|
178 | moveAllTask(jobRegistry, node, underLoadedNode); |
---|
179 | } |
---|
180 | else { |
---|
181 | moveAllTask(jobRegistry, underLoadedNode, node); |
---|
182 | } |
---|
183 | return; |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | // IRIT |
---|
188 | private Node getNodeFromEvent(JobRegistry jobRegistry, TaskFinishedEvent event) { |
---|
189 | ExecTask task = jobRegistry.getTask(event.getJobId(), event.getTaskId()); |
---|
190 | ResourceItem r = task.getAllocatedResources().getLast(); |
---|
191 | ProcessingElements pe = (ProcessingElements) r.getResourceUnits().get(StandardResourceUnitName.PE); |
---|
192 | ComputingResource c = pe.get(0); |
---|
193 | return (Node) c.getParent(); |
---|
194 | |
---|
195 | } |
---|
196 | // IRIT |
---|
197 | private void moveAllTask(JobRegistry jobRegistry, Node source, Node dest) { |
---|
198 | double totalLoad = source.getLoadInterface().getRecentUtilization().getValue()+dest.getLoadInterface().getRecentUtilization().getValue(); |
---|
199 | if(totalLoad>100) {return;} |
---|
200 | |
---|
201 | JobRegistry srcHostJr = new JobRegistryImpl(source.getFullName()); |
---|
202 | int runningTasksNumber = srcHostJr.getRunningTasks().size(); |
---|
203 | for(int i=runningTasksNumber-1; i>=0; i--) { |
---|
204 | ExecTask execTask = srcHostJr.getRunningTasks().get(i); |
---|
205 | Map<ResourceUnitName, ResourceUnit> destinationResources = chooseResourcesForExecution(dest.getProcessors(), execTask); |
---|
206 | jobRegistry.migrateTask(execTask.getJobId(), execTask.getId(), destinationResources); |
---|
207 | } |
---|
208 | } |
---|
209 | // IRIT |
---|
210 | public double getPowerConsumptionAtLoad(Node node, double loadLevel){ |
---|
211 | double powerConsumption = 0; |
---|
212 | double lowestLoadLevel = 100; |
---|
213 | double highestLoadLevel = 0; |
---|
214 | |
---|
215 | System.out.println("ON_" + new Double(loadLevel).intValue()); |
---|
216 | System.out.println(node.getPowerInterface().supportPowerState(new CustomPowerStateName("ON_50"))); |
---|
217 | |
---|
218 | try{ |
---|
219 | if(node.getPowerInterface().supportPowerState(new CustomPowerStateName("ON_" + new Double(loadLevel).intValue()))){ |
---|
220 | System.out.println("2 ON_" + new Double(loadLevel).intValue()); |
---|
221 | |
---|
222 | powerConsumption = node.getPowerInterface().getPowerConsumption(new CustomPowerStateName("ON_" + new Double(loadLevel).intValue())); |
---|
223 | } else { |
---|
224 | for(PowerState powerState: node.getPowerInterface().getSupportedPowerStates()){ |
---|
225 | Double load; |
---|
226 | System.out.println(" getname "+powerState.getName().getLabel()); |
---|
227 | try{ |
---|
228 | load = Double.valueOf(powerState.getName().getLabel().substring(3)); |
---|
229 | }catch (Exception e){ |
---|
230 | continue; |
---|
231 | } |
---|
232 | if(lowestLoadLevel > load){ |
---|
233 | lowestLoadLevel = load; |
---|
234 | } |
---|
235 | if(highestLoadLevel < load){ |
---|
236 | highestLoadLevel = load; |
---|
237 | } |
---|
238 | } |
---|
239 | if(loadLevel == 0){ |
---|
240 | powerConsumption = node.getPowerInterface().getPowerConsumption(new CustomPowerStateName("ON_0")); |
---|
241 | } else { |
---|
242 | |
---|
243 | double lowerLoadLevel = lowestLoadLevel; |
---|
244 | double higherLoadLevel = highestLoadLevel; |
---|
245 | |
---|
246 | for(PowerState powerState: node.getPowerInterface().getSupportedPowerStates()){ |
---|
247 | Double load = Double.valueOf(powerState.getName().getLabel().substring(3)); |
---|
248 | if(loadLevel > load){ |
---|
249 | lowerLoadLevel = load; |
---|
250 | } |
---|
251 | else if(loadLevel < load){ |
---|
252 | higherLoadLevel = load; |
---|
253 | break; |
---|
254 | } |
---|
255 | } |
---|
256 | double powerBelow; |
---|
257 | double powerAbove; |
---|
258 | double a; |
---|
259 | double b; |
---|
260 | if(lowerLoadLevel != higherLoadLevel) { |
---|
261 | powerBelow = node.getPowerInterface().getPowerConsumption(new CustomPowerStateName("ON_" + new Double(lowerLoadLevel).intValue())); |
---|
262 | powerAbove = node.getPowerInterface().getPowerConsumption(new CustomPowerStateName("ON_" + new Double(higherLoadLevel).intValue())); |
---|
263 | a = (powerAbove - powerBelow)/(higherLoadLevel - lowerLoadLevel); |
---|
264 | b = powerAbove - a * higherLoadLevel; |
---|
265 | } else { |
---|
266 | powerBelow = node.getPowerInterface().getPowerConsumption(new CustomPowerStateName("ON_" + new Double(lowestLoadLevel).intValue())); |
---|
267 | powerAbove = node.getPowerInterface().getPowerConsumption(new CustomPowerStateName("ON_" + new Double(highestLoadLevel).intValue())); |
---|
268 | a = (powerAbove - powerBelow)/(highestLoadLevel - lowestLoadLevel); |
---|
269 | b = powerAbove - a * highestLoadLevel; |
---|
270 | } |
---|
271 | powerConsumption = a * loadLevel + b; |
---|
272 | } |
---|
273 | } |
---|
274 | } catch (Exception e){ |
---|
275 | |
---|
276 | } |
---|
277 | |
---|
278 | return powerConsumption; |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | private void globalEnergyOptimizer(JobRegistry jobRegistry, ClusterResourceManager resourceManager, String resName) { |
---|
284 | |
---|
285 | Node leastLoadedNode = findLeastLoadedResource(resourceManager.getNodes()); |
---|
286 | Node mostLoadedNode = findMostLoadedResource(resourceManager.getNodes()); |
---|
287 | System.out.println("-------------ALLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLERT"); |
---|
288 | System.out.println(" max : "+mostLoadedNode.getLoadInterface().getRecentUtilization().getValue()); |
---|
289 | System.out.println(" min : "+leastLoadedNode.getLoadInterface().getRecentUtilization().getValue()); |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | if(leastLoadedNode.getLoadInterface().getRecentUtilization().getValue() >= mostLoadedNode.getLoadInterface().getRecentUtilization().getValue()){ |
---|
294 | return; |
---|
295 | } |
---|
296 | System.out.println(" ALLLLLLLLLLLLLLLL"); |
---|
297 | if(leastLoadedNode.getLoadInterface().getRecentUtilization().getValue() != 0){ |
---|
298 | return; |
---|
299 | } |
---|
300 | System.out.println(" LLLLLLLLLLLLLLLLLLLLERT"); |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | JobRegistry srcHostJr = new JobRegistryImpl(mostLoadedNode.getFullName()); |
---|
305 | int runningTasksNumber = srcHostJr.getRunningTasks().size(); |
---|
306 | System.out.println(" nbtasks source "+runningTasksNumber); |
---|
307 | if(runningTasksNumber >= 1) { |
---|
308 | ExecTask execTask = srcHostJr.getRunningTasks().get(0); |
---|
309 | Map<ResourceUnitName, ResourceUnit> destinationResources = chooseResourcesForExecution(findLeastLoadedResource(resourceManager.getNodes()).getProcessors(), execTask); |
---|
310 | System.out.println(" "+destinationResources.toString()); |
---|
311 | jobRegistry.migrateTask(execTask.getJobId(), execTask.getId(), destinationResources); |
---|
312 | } |
---|
313 | /*for(int i = srcHostJr.getRunningTasks().size() - 1; i >= 0 && runningTasksNumber > 2; i--) { |
---|
314 | System.out.println(" Migration"); |
---|
315 | |
---|
316 | ExecTask execTask = srcHostJr.getRunningTasks().get(i); |
---|
317 | Map<ResourceUnitName, ResourceUnit> destinationResources = chooseResourcesForExecution(findLeastLoadedResource(resourceManager.getNodes()).getProcessors(), execTask); |
---|
318 | System.out.println(" "+destinationResources.toString()); |
---|
319 | jobRegistry.migrateTask(execTask.getJobId(), execTask.getId(), destinationResources); |
---|
320 | runningTasksNumber--; |
---|
321 | |
---|
322 | } |
---|
323 | */ |
---|
324 | |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | private void optimizeEnergyUsage(JobRegistry jobRegistry, ClusterResourceManager resourceManager, String resName) { |
---|
330 | Node overLoadedNode = (Node)resourceManager .getResourceByName(resName); |
---|
331 | Node leastLoadedNode = findLeastLoadedResource(resourceManager.getNodes()); |
---|
332 | |
---|
333 | if(leastLoadedNode.getLoadInterface().getRecentUtilization().getValue() >= overLoadedNode.getLoadInterface().getRecentUtilization().getValue()){ |
---|
334 | return; |
---|
335 | } |
---|
336 | |
---|
337 | JobRegistry srcHostJr = new JobRegistryImpl(overLoadedNode.getFullName()); |
---|
338 | int runningTasksNumber = srcHostJr.getRunningTasks().size(); |
---|
339 | for(int i = srcHostJr.getRunningTasks().size() - 1; i >= 0 && runningTasksNumber > 2; i--) { |
---|
340 | ExecTask execTask = srcHostJr.getRunningTasks().get(i); |
---|
341 | Map<ResourceUnitName, ResourceUnit> destinationResources = chooseResourcesForExecution(findLeastLoadedResource(resourceManager.getNodes()).getProcessors(), execTask); |
---|
342 | jobRegistry.migrateTask(execTask.getJobId(), execTask.getId(), destinationResources); |
---|
343 | runningTasksNumber--; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
348 | List<Processor> processors, TaskInterface<?> task) { |
---|
349 | |
---|
350 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(1); |
---|
351 | |
---|
352 | int cpuRequest; |
---|
353 | try { |
---|
354 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
355 | } catch (NoSuchFieldException e) { |
---|
356 | cpuRequest = 0; |
---|
357 | } |
---|
358 | |
---|
359 | if (cpuRequest != 0) { |
---|
360 | |
---|
361 | if (processors.size() < cpuRequest) { |
---|
362 | return null; |
---|
363 | } |
---|
364 | |
---|
365 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest); |
---|
366 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
367 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
368 | choosenResources.add(processors.get(i)); |
---|
369 | cpuRequest--; |
---|
370 | } |
---|
371 | } |
---|
372 | if (cpuRequest > 0) { |
---|
373 | return null; |
---|
374 | } |
---|
375 | |
---|
376 | ProcessingElements pe = new ProcessingElements(); |
---|
377 | pe.addAll(choosenResources); |
---|
378 | map.put(StandardResourceUnitName.PE, pe); |
---|
379 | return map; |
---|
380 | } |
---|
381 | |
---|
382 | return null; |
---|
383 | } |
---|
384 | |
---|
385 | private Node findLeastLoadedResource(List<Node> nodes ){ |
---|
386 | Node leastLoadedNoded = null; |
---|
387 | double minLoad = Double.MAX_VALUE; |
---|
388 | |
---|
389 | for(int i = 0; i < nodes.size(); i++){ |
---|
390 | Node node = nodes.get(i); |
---|
391 | double totalLoad = node.getLoadInterface().getRecentUtilization().getValue(); |
---|
392 | if(totalLoad < minLoad){ |
---|
393 | leastLoadedNoded= node; |
---|
394 | minLoad = totalLoad; |
---|
395 | } |
---|
396 | } |
---|
397 | return leastLoadedNoded; |
---|
398 | } |
---|
399 | private Node findMostLoadedResource(List<Node> nodes ){ |
---|
400 | Node mostLoadedNoded = null; |
---|
401 | double minLoad = 0; |
---|
402 | |
---|
403 | for(int i = 0; i < nodes.size(); i++){ |
---|
404 | Node node = nodes.get(i); |
---|
405 | double totalLoad = node.getLoadInterface().getRecentUtilization().getValue(); |
---|
406 | if(totalLoad >= minLoad){ |
---|
407 | mostLoadedNoded= node; |
---|
408 | minLoad = totalLoad; |
---|
409 | } |
---|
410 | } |
---|
411 | return mostLoadedNoded; |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|