package test.transitions; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import schedframe.events.scheduling.SchedulingEvent; import schedframe.resources.ResourceStatus; import schedframe.resources.StandardResourceType; import schedframe.resources.computing.ComputingResource; import schedframe.resources.computing.Node; import schedframe.resources.computing.Processor; import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; import schedframe.resources.computing.profiles.energy.power.ui.NodePowerInterface; 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.manager.tasks.JobRegistryImpl; 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 example.localplugin.BaseLocalSchedulingPlugin; import gridsim.dcworms.DCWormsTags; public class FCFS_ConsolidationHighPerf_NodePowMan_SP_Delay extends BaseLocalSchedulingPlugin { public SchedulingPlanInterface schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, ResourceManager resManager, ModuleList modules) { ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; SchedulingPlan plan = new SchedulingPlan(); // choose the events types to serve. // Different actions for different events are possible. switch (event.getType()) { case START_TASK_EXECUTION: case TASK_FINISHED: case RESOURCE_POWER_STATE_CHANGED: // our tasks are placed only in first queue (see // BaseLocalSchedulingPlugin.placeJobsInQueues() method) TaskQueue q = queues.get(0); List notSelectedNodes = resourceManager.getNodes(); // 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(resourceManager, task); if (choosenResources != null) { addToSchedulingPlan(plan, task, choosenResources); ProcessingElements pe = (ProcessingElements) choosenResources.get(StandardResourceUnitName.PE); Node node = (Node) pe.get(0).getParent(); notSelectedNodes.remove(node); } } } manageResources(notSelectedNodes); break; } return plan; } @SuppressWarnings("unchecked") private Map chooseResourcesForExecution( ClusterResourceManager resourceManager, TaskInterface task) { Map map = new HashMap(); List nodes = resourceManager.getNodes(); List avNodes = filterNodes(nodes, task); Node node; if(avNodes.size() == 0){ nodes = (List) resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Node, ResourceStatus.UNAVAILABLE); node = turnOnFirstNode(nodes, task); return null; } else { node = findTheMostLoadedNode(avNodes); } int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 0; } if (cpuRequest != 0) { List choosenResources = new ArrayList(); List cpus = node.getProcessors(); for (int i = 0; i < cpus.size() && cpuRequest > 0; i++) { if (cpus.get(i).getStatus() == ResourceStatus.FREE) { choosenResources.add(cpus.get(i)); cpuRequest--; } } if (cpuRequest > 0) { return null; } //choosenResources.add(node.getProcessors().get(0)); 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 cpus = node.getProcessors(); if (cpus.size() < cpuRequest) { if(cpus.size() == 0){ continue; } } int freeCpus = 0; for(Processor cpu: cpus){ if(cpu.getStatus() == ResourceStatus.FREE) freeCpus++; } if(freeCpus < cpuRequest) continue; filteredNodes.add(node); } } return filteredNodes; } private Node findTheMostLoadedNode(List nodes){ Node mostLoadedNode = null; int maxRunningTask = -1; int nrRunningTasks; for(Node node: nodes){ JobRegistry jr = new JobRegistryImpl(node.getFullName()); nrRunningTasks = jr.getRunningTasks().size(); if(nrRunningTasks > maxRunningTask){ mostLoadedNode = node; maxRunningTask = nrRunningTasks; } } return mostLoadedNode; } private Node turnOnFirstNode(List nodes, TaskInterface task){ Node startedNode = null; int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 0; } for(Node node: nodes){ if (cpuRequest != 0) { List cpus = node.getProcessors(); if (cpus.size() < cpuRequest) { if(cpus.size() == 0){ continue; } } int freeCpus = 0; for(Processor cpu: cpus){ if(cpu.getStatus() == ResourceStatus.FREE || cpu.getStatus() == ResourceStatus.UNAVAILABLE) freeCpus++; } if(freeCpus < cpuRequest) continue; else if(node.getPowerInterface().getPowerState().getLabel().equals(StandardPowerStateName.OFF.getLabel())){ NodePowerInterface nodePI = (NodePowerInterface)node.getPowerInterface(); nodePI.turnOn(); startedNode = node; break; } } } return startedNode; } protected void manageResources(List notSelectedNodes) { turnOffIdleNodes(notSelectedNodes); } private void turnOffIdleNodes(List nodes){ for(Node node : nodes){ int freeCpus = 0; for(Processor cpu: node.getProcessors ()){ if(cpu.getStatus() == ResourceStatus.FREE) freeCpus++; } if(freeCpus == node.getProcessors().size() && node.getPowerInterface().getPowerState().getLabel().equals(StandardPowerStateName.ON.getLabel())) { NodePowerInterface nodePI = (NodePowerInterface)node.getPowerInterface(); nodePI.turnOff(); } } } }