package experiments.e2dc2015.models.i5; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import schedframe.events.scheduling.SchedulingEvent; import schedframe.resources.ResourceStatus; import schedframe.resources.StandardResourceType; import schedframe.resources.computing.ComputingResource; import schedframe.resources.computing.Core; import schedframe.resources.computing.Node; import schedframe.resources.computing.Processor; import schedframe.resources.computing.profiles.energy.ResourceEvent; import schedframe.resources.computing.profiles.energy.ResourceEventType; import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName; import schedframe.resources.devices.Device; import schedframe.resources.devices.Fan; import schedframe.resources.units.ProcessingElements; import schedframe.resources.units.ResourceUnit; import schedframe.resources.units.ResourceUnitName; import schedframe.resources.units.StandardResourceUnitName; import schedframe.scheduling.manager.resources.ClusterResourceManager; import schedframe.scheduling.manager.resources.ResourceManager; import schedframe.scheduling.manager.tasks.JobRegistry; import schedframe.scheduling.plan.SchedulingPlanInterface; import schedframe.scheduling.plan.impl.SchedulingPlan; import schedframe.scheduling.plugin.ModuleList; import schedframe.scheduling.queue.TaskQueue; import schedframe.scheduling.queue.TaskQueueList; import schedframe.scheduling.tasks.TaskInterface; import simulator.DataCenterWorkloadSimulator; import eduni.simjava.Sim_system; import example.localplugin.BaseLocalSchedulingPlugin; import experiments.e2dc2015.EnvironmentConditions; import gridsim.dcworms.DCWormsTags; public class FCFSBF_RandomPluginFansMngDynamicFuture extends BaseLocalSchedulingPlugin { private Random rand; public FCFSBF_RandomPluginFansMngDynamicFuture() { rand = new Random(5); } public SchedulingPlanInterface schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, ResourceManager resManager, ModuleList modules) { ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; List nodes = resourceManager.getNodes(); SchedulingPlan plan = new SchedulingPlan(); // choose the events types to serve. // Different actions for different events are possible. switch (event.getType()) { case TIMER: System.out.println("##" + Sim_system.clock()); DataCenterWorkloadSimulator.getEventManager().sendToResources(StandardResourceType.Processor, 0.0, new ResourceEvent(ResourceEventType.TIMER, null)); //DataCenterWorkloadSimulator.getEventManager().sendToAllSchedulers(EnvironmentConditions.SYSTEM_UDPATE_INTERVAL, DCWormsTags.TIMER, "Test"); break; case START_TASK_EXECUTION: case TASK_FINISHED: // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) TaskQueue q = queues.get(0); // check all tasks in queue for (int i = 0; i < q.size(); i++) { TaskInterface task = q.get(i); // if status of the tasks in READY if (task.getStatus() == DCWormsTags.READY) { Map choosenResources = chooseResourcesForExecution(nodes, task); if (choosenResources != null) { addToSchedulingPlan(plan, task, choosenResources); } } } break; case RESOURCE_TEMPERATURE_LIMIT_EXCEEDED: manageFans(resourceManager.getResourceByName(event.getSource())); break; } return plan; } private void manageFans(ComputingResource cr ){ //System.out.println("tuning fans"); double temp = cr.getThermalInterface().getRecentTemperature().getValue(); double prevTemp; double Tin = EnvironmentConditions.ROOM_TEMPERATURE; double Pcpu = cr.getPowerInterface().getRecentPowerUsage().getValue(); /*************/ double R = 0; double Rb = 0; double Rcv = 0; try{ Rb = Double.valueOf(cr.getPowerInterface().getParameters().get("thermalResistance").get(0).getContent()).doubleValue(); } catch (Exception e){ // } double k = 0; try{ k = Double.valueOf(cr.getPowerInterface().getParameters().get("k").get(0).getContent()).doubleValue(); } catch (Exception e){ // } double a = 0; try{ a = Double.valueOf(cr.getPowerInterface().getParameters().get("a").get(0).getContent()).doubleValue(); } catch (Exception e){ a = 1; } double V = 0; //System.out.println("------ " + cr.getFullName()); for(Device device: cr.getParent().getParent().getResourceCharacteristic().getDevices()){ if(device.getType().equals(StandardResourceType.Fan)){ int fanId = Integer.valueOf(device.getName().split("_")[1]).intValue(); int nodeId = Integer.valueOf(cr.getParent().getName().split("_")[1]).intValue(); //System.out.println(device.getName() + "; " + cpu.getParent().getName()); //System.out.println(fanId + "; " + cpuId); if(nodeId > (fanId - 1) * EnvironmentConditions.NODES_IN_A_COLUMN && nodeId <= fanId * EnvironmentConditions.NODES_IN_A_COLUMN /*|| Integer.valueOf(device.getName().split("_")[1]) == Integer.valueOf(cpu.getParent().getName().split("_")[1]) - EnvironmentConditions.NODES_IN_A_ROW*/){ //System.out.println("********"); Fan fan = (Fan) device; V = fan.getAirflowInterface().getRecentAirflow().getValue(); break; } } } //System.out.println("V: " + V); Rcv = 1/ (k * Math.pow(V, a)); R = Rb + Rcv; double C = 0; try{ C = Double.valueOf(cr.getPowerInterface().getParameters().get("thermalCapacity").get(0).getContent()).doubleValue(); } catch (Exception e){ // } double objective_temperature = Pcpu * R + Tin; double delta_t = 15; /*************/ double nextTemp = objective_temperature - (objective_temperature - temp) * Math.exp(-delta_t/(R * C)); if(cr.getThermalInterface().getTemperatureHistory().size() == 1) prevTemp = temp; else { prevTemp = cr.getThermalInterface().getTemperatureHistory().get(cr.getThermalInterface().getTemperatureHistory().size() - 2).getValue(); } //System.out.println(Sim_system.clock() + "; " + prevTemp +": " +temp + ": " + objective_temperature + ": " + nextTemp+"; " + Pcpu); //System.out.println(Sim_system.clock() + " === " + prevTemp+ "; " + temp); boolean getingHotter = prevTemp < temp; boolean getingColder = prevTemp > temp; Node n = (Node) cr.getParent(); List neighbours = n.getParent().getChildren(); for(Device d: n.getParent().getResourceCharacteristic().getDevices()){ //System.out.println(d.getName() + "; " + n.getName()); int fanId = Integer.valueOf(d.getName().split("_")[1]).intValue(); int nodeId = Integer.valueOf(n.getName().split("_")[1]).intValue(); if(nodeId > (fanId - 1) * EnvironmentConditions.NODES_IN_A_COLUMN && nodeId <= fanId * EnvironmentConditions.NODES_IN_A_COLUMN /*|| Integer.valueOf(device.getName().split("_")[1]) == Integer.valueOf(cpu.getParent().getName().split("_")[1]) - EnvironmentConditions.NODES_IN_A_ROW*/){ Fan f = (Fan) d; int speed; try{ speed= Integer.valueOf(f.getAirflowInterface().getAirflowState().getLabel().split("_")[1]).intValue(); } catch(Exception e){ speed = 0; } for(ComputingResource neighbour: neighbours){ int neighbourId = Integer.valueOf(neighbour.getName().split("_")[1]).intValue(); if(neighbourId > (fanId - 1) * EnvironmentConditions.NODES_IN_A_COLUMN && neighbourId <= fanId * EnvironmentConditions.NODES_IN_A_COLUMN){ double neighbourTemp = neighbour.getChildren().get(0).getThermalInterface().getRecentTemperature().getValue(); if(neighbourTemp > temp){ return; } } } //System.out.println("getingHotter: "+ (prevTemp < temp)); if(getingHotter){ if(nextTemp > temp) temp = nextTemp; if(temp > EnvironmentConditions._30_2_40- EnvironmentConditions.tempShift && temp < EnvironmentConditions._40_2_50- EnvironmentConditions.tempShift ){ if(speed < 40){ //System.out.println("+++++++++ 30 to 40"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_40")); } } else if(temp > EnvironmentConditions._40_2_50- EnvironmentConditions.tempShift && temp < EnvironmentConditions._50_2_60- EnvironmentConditions.tempShift ){ if(speed < 50){ //System.out.println("++++++++ 40 to 50"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_50")); } } else if(temp > EnvironmentConditions._50_2_60- EnvironmentConditions.tempShift && temp < EnvironmentConditions._60_2_70- EnvironmentConditions.tempShift ){ if(speed < 60){ //System.out.println("++++++++ 50 to 60"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_60")); } } else if(temp > EnvironmentConditions._60_2_70- EnvironmentConditions.tempShift && temp < EnvironmentConditions._70_2_80- EnvironmentConditions.tempShift ){ if(speed < 70){ //System.out.println("++++++++ 60 to 70"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_70")); } } else if(temp > EnvironmentConditions._70_2_80- EnvironmentConditions.tempShift && temp < EnvironmentConditions._80_2_90- EnvironmentConditions.tempShift ){ if(speed < 80){ //System.out.println("++++++++ 70 to 80"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_80")); } } else if(temp > EnvironmentConditions._80_2_90- EnvironmentConditions.tempShift && temp < EnvironmentConditions._90_2_100- EnvironmentConditions.tempShift ){ if(speed < 90){ //System.out.println("++++++++ 80 to 90"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_90")); } } else if(temp > EnvironmentConditions._90_2_100- EnvironmentConditions.tempShift ){ if(speed < 100){ //System.out.println("++++++++ 90 to 100"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_100")); } } } else if (getingColder){ if(temp > EnvironmentConditions._90_2_80 && temp < EnvironmentConditions._100_2_90){ if(speed == 100){ //System.out.println("-------- 100 to 90"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_90")); } } else if(temp > EnvironmentConditions._80_2_70 && temp < EnvironmentConditions._90_2_80){ if(speed >= 90){ //System.out.println("-------- 90 to 80"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_80")); } } else if(temp > EnvironmentConditions._70_2_60 && temp < EnvironmentConditions._80_2_70){ if(speed >= 80){ //System.out.println("-------- 80 to 70"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_70")); } } else if(temp > EnvironmentConditions._60_2_50 && temp < EnvironmentConditions._70_2_60){ if(speed >= 70){ //System.out.println("-------- 70 to 60"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_60")); } } else if(temp > EnvironmentConditions._50_2_40 && temp < EnvironmentConditions._60_2_50){ if(speed >= 60){ //System.out.println("-------- 60 to 50"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_50")); } } else if(temp > EnvironmentConditions._40_2_30 && temp < EnvironmentConditions._50_2_40){ if(speed >= 50){ //System.out.println("-------- 60 to 40"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_40")); } } else if (temp < EnvironmentConditions._40_2_30) { if(speed >= 40){ //System.out.println("-------- 40 to 30"); f.getAirflowInterface().setAirflowState(new CustomAirflowStateName("ON_30")); } } } } } } private Map chooseResourcesForExecution( List nodes, TaskInterface task) { Map map = new HashMap(1); int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 0; } List filteredNodes = filterNodes(nodes, task); if(filteredNodes.size()==0) return null; Node node = chooseRandomNode(filteredNodes); while(node.getFreeCores().size() < cpuRequest){ node = chooseRandomNode(nodes); } if (cpuRequest != 0) { if (node.getFreeCores().size() < cpuRequest) { return null; } List processors = node.getProcessors(); List cores = node.getCores(); List choosenResources = new ArrayList(cpuRequest); for (int i = 0; i < cores.size() && cpuRequest > 0; i++) { if (cores.get(i).getStatus() == ResourceStatus.FREE) { choosenResources.add(cores.get(i)); cpuRequest--; } } if (cpuRequest > 0) { return null; } ProcessingElements pe = new ProcessingElements(); pe.addAll(choosenResources); map.put(StandardResourceUnitName.PE, pe); return map; } return null; } private List filterNodes(List nodes, TaskInterface task){ List filteredNodes = new ArrayList(); int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 0; } for (Node node : nodes) { if (cpuRequest != 0) { List cores = node.getCores(); if (cores.size() < cpuRequest) { if(cores.size() == 0){ if(node.getProcessors().size() < cpuRequest) continue; } } int freeCores = 0; for(Core core: cores){ if(core.getStatus() == ResourceStatus.FREE) freeCores++; } if(freeCores < cpuRequest) continue; filteredNodes.add(node); } } return filteredNodes; } private Node chooseRandomNode(List nodes) { int nodeIdx = rand.nextInt(nodes.size()); return nodes.get(nodeIdx); } }