package example.localplugin; import gridsim.Gridlet; 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.Properties; import schedframe.resources.units.Memory; import schedframe.resources.units.ResourceUnit; import schedframe.scheduling.TaskInterface; import schedframe.scheduling.events.SchedulingEvent; 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.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.scheduling.JobRegistryInterface; 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 FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin { public FCFSConsolidationClusterLocalPlugin () { } 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 START_TASK_EXECUTION: case TASK_FINISHED: // 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) { Map choosenResources = chooseResourcesForExecution(resourceManager, task); if (choosenResources != null) { addToSchedulingPlan(plan, task, choosenResources); } } } break; } return plan; } private HashMap chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface task) { List nodes = resourceManager.getComputingNodes(); nodes = findSuitableNodes(task, 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) { ComputingNode node = nodes.get(0); HashMap map = new HashMap(); List choosenResources = new ArrayList(); int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 1; } for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) { if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) { choosenResources.add(node.getProcessors().get(i)); cpuRequest--; } } ProcessingElements result = new ProcessingElements(node.getName()); result.addAll(choosenResources); map.put(ResourceParameterName.PROCESSINGELEMENTS, result); int memoryRequest; try { memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); } catch (NoSuchFieldException e) { memoryRequest = 0; } if (memoryRequest != 0) { Memory memory = new Memory(node.getMemoryUnit(), memoryRequest, memoryRequest); map.put(ResourceParameterName.MEMORY, memory); } return map; } else return null; } private List findSuitableNodes(TaskInterface task, List nodes){ int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 1; } int memoryRequest; try { memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); } catch (NoSuchFieldException e) { memoryRequest = 0; } List suitableNodes = new ArrayList(); for(ComputingNode node: nodes){ if(node.getFreeProcessorsNumber() >= cpuRequest && node.getFreeMemory() >= memoryRequest){ suitableNodes.add(node); } } return suitableNodes; } public String getName() { return getClass().getName(); } public void init(Properties properties) { // no extra initialization is expected. } }