package test.thermal.recs.plugins.scheduling; import gridsim.dcworms.DCWormsTags; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.MissingResourceException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; import schedframe.events.scheduling.SchedulingEvent; import schedframe.resources.ResourceStatus; import schedframe.resources.computing.Node; import schedframe.resources.computing.ComputingResource; import schedframe.resources.computing.Core; import schedframe.resources.computing.Processor; import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; 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 test.thermal.recs.utils.AppType; import test.thermal.recs.plugins.energy.ThermalConstants; public class RecsExclusivenessMaxTempOptSP extends RecsSP { private static String TIME_DATA_FILE_NAME = "src/test/thermal/recs/data/time_data.properties"; private static String POWER_DATA_FILE_NAME = "src/test/thermal/recs/data/power_data.properties"; private ResourceBundle powBundle; private ResourceBundle timeBundle; // virtual nodes are to keep track of the intermediate power and temperature between job assignments private List virtualNodes = new ArrayList(); private boolean firstCall = true; public SchedulingPlanInterface schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, ResourceManager resManager, ModuleList modules) { ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; SchedulingPlan plan = new SchedulingPlan(); List nodes = resourceManager.getNodes(); // create the virtualNodes on the first scheduling call if (firstCall){ VirtualNode dummyVN = new VirtualNode(0); virtualNodes.add(dummyVN); int numNodes = nodes.size(); for (int i = 1; i <= numNodes; i++){ VirtualNode vn = new VirtualNode(i); virtualNodes.add(vn); } firstCall = false; } // update the power of the virtual nodes for (Node node : nodes){ double power = node.getPowerInterface().getRecentPowerUsage().getValue(); int nodeId = Integer.parseInt(node.getName().split("_")[1]); virtualNodes.get(nodeId).setPower(power); } // update the temperature of the virtual nodes for (int i = 1; i <= 9; i++){ VirtualNode vn1 = virtualNodes.get(i+9); VirtualNode vn2 = virtualNodes.get(i); double power1 = vn1.getPower(); double power2 = vn2.getPower(); double tout = ThermalConstants.calculateOutletTemp(power1, power2); vn1.setTemperature(tout); vn2.setTemperature(tout); } // choose the events types to serve. // Different actions for different events are possible. switch (event.getType()) { case START_TASK_EXECUTION: case TASK_FINISHED: //case TIMER: // 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); initApplicationType(task); // if status of the tasks in READY if (task.getStatus() == DCWormsTags.READY) { Map choosenResources = chooseResourcesForExecution(resourceManager, task); if (choosenResources != null) { addToSchedulingPlan(plan, task, choosenResources); } } } break; } return plan; } private Map chooseResourcesForExecution( ClusterResourceManager resourceManager, TaskInterface task) { Map map; List nodes = resourceManager.getNodes(); Collections.shuffle(nodes); Collections.sort(nodes, new TemperatureComparator(task)); for (Node node : nodes) { int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 0; } if (cpuRequest != 0) { List cores = node.getProcessors().get(0).getCores(); if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) { continue; } int freeCores = 0; for(Core core: cores){ if(core.getStatus() == ResourceStatus.FREE) freeCores++; } if(freeCores != cores.size()) continue; try { if(!getExecutiveness(createExecutivenessQuery(task, node))) continue; } catch (FileNotFoundException e) { continue; } catch (IOException e) { continue; } catch (MissingResourceException e){ continue; } List choosenResources = new ArrayList(); for (int i = 0; i < cores.size(); i++) { if (cores.get(i).getStatus() == ResourceStatus.FREE) { //choosenResources.add(cores.get(i)); cpuRequest--; } } choosenResources.add(node); if (cpuRequest > 0) { //continue; } map = new HashMap(); ProcessingElements pe = new ProcessingElements(); pe.addAll(choosenResources); map.put(StandardResourceUnitName.PE, pe); // update the power and temperature of the virtualNode corresponding to the node int nodeId = Integer.parseInt(node.getName().split("_")[1]); VirtualNode vn = virtualNodes.get(nodeId); // power update double extraPower = 0; try{ extraPower = Math.abs(getMeasuredPower(createQuery(task, node)) - node.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); } catch (FileNotFoundException e) { } catch (IOException e) { } catch (MissingResourceException e){ } catch (NoSuchFieldException e) { } vn.setPower(vn.getPower() + extraPower); // temperature update double delta = 1; VirtualNode vn2; if (nodeId >= 1 && nodeId <= 9){ delta = ThermalConstants.delta2; vn2 = virtualNodes.get(nodeId + 9); } else{ delta = ThermalConstants.delta1; vn2 = virtualNodes.get(nodeId - 9); } double extraTemp = delta * (extraPower/(ThermalConstants.Q * ThermalConstants.C * ThermalConstants.ro)); vn.setTemperature(vn.getTemperature() + extraTemp); vn2.setTemperature(vn2.getTemperature() + extraTemp); return map; } } return null; } protected String createQuery(TaskInterface task, Node node) { String query; query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task); return query; } protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{ ResourceBundle powBundle = getPowBundle(); return Double.valueOf(powBundle.getString(query)).doubleValue(); } private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{ if(powBundle == null){ powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME)); } return powBundle; } protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{ ResourceBundle timeBundle = getTimeBundle(); return Double.valueOf(timeBundle.getString(query)).doubleValue(); } private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{ if(timeBundle == null){ timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME)); } return timeBundle; } class TemperatureComparator implements Comparator{ private TaskInterface task; public TemperatureComparator(TaskInterface task){ this.task = task; } public int compare(Node node1, Node node2){ int id1 = Integer.parseInt(node1.getName().split("_")[1]); int id2 = Integer.parseInt(node2.getName().split("_")[1]); double currentNode1Temp = virtualNodes.get(id1).getTemperature(); double currentNode2Temp = virtualNodes.get(id2).getTemperature(); double power1 = 0; double power2 = 0; try{ power1 = Math.abs(getMeasuredPower(createQuery(task, node1)) - node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); power2 = Math.abs(getMeasuredPower(createQuery(task, node2)) - node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); } catch (FileNotFoundException e) { } catch (IOException e) { } catch (MissingResourceException e){ } catch (NoSuchFieldException e) { } double deltaOne = 1, deltaTwo = 1; if (id1 >= 1 && id1 <= 9) // outlet deltaOne = ThermalConstants.delta2; else if (id1 >= 10 && id1 <= 18) // inlet deltaOne = ThermalConstants.delta1; if (id2 >= 1 && id2 <= 9) // outlet deltaTwo = ThermalConstants.delta2; else if (id2 >= 10 && id2 <= 18) // inlet deltaTwo = ThermalConstants.delta1; double afterNode1Temp = currentNode1Temp + deltaOne * (power1/(ThermalConstants.Q * ThermalConstants.C * ThermalConstants.ro)); double afterNode2Temp = currentNode2Temp + deltaTwo * (power2/(ThermalConstants.Q * ThermalConstants.C * ThermalConstants.ro)); if (afterNode1Temp > afterNode2Temp) return 1; else if (afterNode1Temp < afterNode2Temp) return -1; else return 0; } } }