package example.localplugin; import gridsim.Gridlet; import gridsim.gssim.ResourceHistoryItem; import gridsim.gssim.SubmittedTask; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Properties; import schedframe.resources.PowerState; import schedframe.scheduling.TaskInterface; import schedframe.scheduling.events.SchedulingEvent; import schedframe.scheduling.events.TaskFinishedEvent; import schedframe.scheduling.plugin.grid.ModuleList; import schedframe.scheduling.utils.ResourceParameterName; import test.rewolucja.GSSIMJobInterface; import test.rewolucja.resources.ProcessingElements; import test.rewolucja.resources.ResourceStatus; import test.rewolucja.resources.ResourceType; import test.rewolucja.resources.manager.implementation.ClusterResourceManager; import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; import test.rewolucja.resources.physical.base.ComputingResource; import test.rewolucja.resources.physical.implementation.ComputingNode; import test.rewolucja.resources.physical.implementation.Processor; import test.rewolucja.scheduling.JobRegistryInterface; import test.rewolucja.scheduling.UsedResourceList; import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; import test.rewolucja.scheduling.plan.SchedulingPlanNew; import test.rewolucja.scheduling.queue.Queue; import test.rewolucja.scheduling.queue.QueueList; public class FCFSPreferedConsolidationClusterLocalPlugin extends BaseLocalPlugin { private int scenario = 1; //0 - konsolidacja bez wylaczania //1 - konsolidacja, wylaczanie bez preferencji zasobu //2 - konsolidacja, wylaczanie, 2 rozlaczne pule zasobobow //3 - konsolidacja, wylaczanie jesli nie ma preferowanych sprawdza druga kategorie public FCFSPreferedConsolidationClusterLocalPlugin () { } public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, ResourceManagerInterface resManager, ModuleList modules) { ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; SchedulingPlanNew plan = new SchedulingPlanNew(); // chose the events types to serve. // Different actions for different events are possible. switch (event.getType()) { case TASK_FINISHED: switch(scenario) { case 1: case 2: case 3: TaskFinishedEvent finEvent = (TaskFinishedEvent) event; SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId()); UsedResourceList usedResourcesList = subTask.getUsedResources(); ProcessingElements pes = (ProcessingElements)usedResourcesList.getLast().getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS); ComputingNode cn = ((Processor)pes.get(0)).getComputingNode(); if( cn.getFreeProcessors().size() == cn.getProcessorsNumber()) { cn.getPowerInterface().setPowerState( PowerState.OFF); //System.out.println("Wylaczam wezel: " + cn.getName()); } } case START_TASK_EXECUTION: // our tasks are placed only in first queue (see // BaseLocalPlugin.placeJobsInQueues() method) Queue q = queues.get(0); // check all tasks in queue for (int i = 0; i < q.size(); i++) { GSSIMJobInterface job = q.get(i); TaskInterface task = (TaskInterface) job; // if status of the tasks in READY if (task.getStatus() == Gridlet.READY) { ComputingNode node = chooseResourcesForExecution(resourceManager, PowerState.ON, task); if (node != null) { List cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task); //System.out.println( task.getJobId() + " -> " + node.getName()); addToSchedulingPlan(plan, task, cpus); } else { node = chooseResourcesForExecution(resourceManager, PowerState.OFF, task); if( node != null) { //System.out.println("Wlaczam wezel: " + node.getName()); node.getPowerInterface().setPowerState( PowerState.ON); i--; } } } } switch( scenario) { case 0: break; case 1: case 2: case 3: List nodes = resourceManager.getComputingNodes(); for( ComputingNode node : nodes) if( node.getFreeProcessors().size() == node.getProcessorsNumber()) { //System.out.println("Wylaczam zasob" + node.getName()); node.getPowerInterface().setPowerState( PowerState.OFF); //System.out.println("Zasob w stanie: " + node.getPowerInterface().getPowerState()); } break; } break; } return plan; } private ComputingNode chooseResourcesForExecution(ClusterResourceManager resourceManager, PowerState status, TaskInterface task) { //System.out.println("Szukam zasobow w stanie: " +status); int type = Integer.parseInt( task.getJobId()) % 4; String category = null; switch( scenario) { case 2: case 3: if( type == 0) category = "B"; else category = "A"; break; } List nodes = resourceManager.getComputingNodes(); nodes = findSuitableNodes(task, category, status, nodes); Collections.sort(nodes, new Comparator(){ public int compare(ComputingNode node1, ComputingNode node2){ return node1.getCategory().getName().compareTo(node2.getCategory().getName()); } }); if(nodes.size() > 0) return nodes.get(0); switch( scenario) { case 3: nodes = findSuitableNodes(task, getUnprefered(category), status, nodes); Collections.sort(nodes, new Comparator(){ public int compare(ComputingNode node1, ComputingNode node2){ return node1.getCategory().getName().compareTo(node2.getCategory().getName()); } }); if(nodes.size() > 0) return nodes.get(0); } return null; } private List findSuitableNodes(TaskInterface task, String category, PowerState status, List nodes){ int cpuRequest = getCpuRequest(task); List suitableNodes = new ArrayList(); for(ComputingNode node: nodes) { if( category == null || category.equals(node.getCategory().getName()) && node.getPowerInterface().getPowerState() == status) switch( status) { case ON: if(node.getFreeProcessorsNumber() >= cpuRequest) { suitableNodes.add(node); } break; case OFF: if( node.getProcessorsNumber() >= cpuRequest) suitableNodes.add(node); break; } } return suitableNodes; } public String getName() { return getClass().getName(); } public void init(Properties properties) { // no extra initialization is expected. } private List chooseProcessorsForExecution( ComputingNode node, ResourceStatus status, TaskInterface task) { int cpuRequest = getCpuRequest(task); List cpus = (List)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); return cpus.subList(0, cpuRequest); } private int getCpuRequest( TaskInterface task) { int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 1; } return cpuRequest; } private String getUnprefered( String preferedNode) { if( preferedNode.equals("A")) return "B"; if( preferedNode.equals("B")) return "A"; return null; } }