[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | |
---|
| 5 | import java.util.ArrayList; |
---|
| 6 | import java.util.Collections; |
---|
| 7 | import java.util.Comparator; |
---|
| 8 | import java.util.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
| 11 | import java.util.Properties; |
---|
| 12 | |
---|
| 13 | import schedframe.resources.units.Memory; |
---|
| 14 | import schedframe.resources.units.ResourceUnit; |
---|
| 15 | import schedframe.scheduling.TaskInterface; |
---|
| 16 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 17 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 18 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 19 | import test.rewolucja.GSSIMJobInterface; |
---|
| 20 | import test.rewolucja.resources.ProcessingElements; |
---|
| 21 | import test.rewolucja.resources.ResourceStatus; |
---|
| 22 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 23 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 24 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 25 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
| 26 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 27 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 28 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
[163] | 29 | import test.rewolucja.scheduling.queue.Queue; |
---|
[104] | 30 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 31 | |
---|
| 32 | public class FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 33 | |
---|
| 34 | public FCFSConsolidationClusterLocalPlugin () { |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 38 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 39 | |
---|
| 40 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 41 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 42 | // chose the events types to serve. |
---|
| 43 | // Different actions for different events are possible. |
---|
| 44 | switch (event.getType()) { |
---|
| 45 | case START_TASK_EXECUTION: |
---|
| 46 | case TASK_FINISHED: |
---|
| 47 | // our tasks are placed only in first queue (see |
---|
[192] | 48 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
[163] | 49 | Queue q = queues.get(0); |
---|
[104] | 50 | // check all tasks in queue |
---|
| 51 | |
---|
| 52 | for (int i = 0; i < q.size(); i++) { |
---|
| 53 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 54 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 55 | // if status of the tasks in READY |
---|
| 56 | if (task.getStatus() == Gridlet.READY) { |
---|
| 57 | |
---|
| 58 | Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 59 | if (choosenResources != null) { |
---|
| 60 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | break; |
---|
| 66 | } |
---|
| 67 | return plan; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 71 | |
---|
| 72 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 73 | nodes = findSuitableNodes(task, nodes); |
---|
| 74 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 75 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 76 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
| 77 | } |
---|
| 78 | }); |
---|
| 79 | if(nodes.size() > 0) |
---|
| 80 | { |
---|
| 81 | ComputingNode node = nodes.get(0); |
---|
| 82 | HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
| 83 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
| 84 | int cpuRequest; |
---|
| 85 | try { |
---|
| 86 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 87 | } catch (NoSuchFieldException e) { |
---|
| 88 | cpuRequest = 1; |
---|
| 89 | } |
---|
| 90 | for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) { |
---|
| 91 | if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 92 | choosenResources.add(node.getProcessors().get(i)); |
---|
| 93 | cpuRequest--; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | ProcessingElements result = new ProcessingElements(node.getName()); |
---|
| 97 | result.addAll(choosenResources); |
---|
| 98 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
| 99 | |
---|
| 100 | int memoryRequest; |
---|
| 101 | try { |
---|
| 102 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 103 | } catch (NoSuchFieldException e) { |
---|
| 104 | memoryRequest = 0; |
---|
| 105 | } |
---|
| 106 | if (memoryRequest != 0) { |
---|
[192] | 107 | Memory memory = new Memory(node.getMemoryUnit(), memoryRequest, memoryRequest); |
---|
[104] | 108 | map.put(ResourceParameterName.MEMORY, memory); |
---|
| 109 | } |
---|
| 110 | return map; |
---|
| 111 | } else |
---|
| 112 | return null; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, List<ComputingNode> nodes){ |
---|
| 116 | int cpuRequest; |
---|
| 117 | try { |
---|
| 118 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 119 | } catch (NoSuchFieldException e) { |
---|
| 120 | cpuRequest = 1; |
---|
| 121 | } |
---|
| 122 | int memoryRequest; |
---|
| 123 | try { |
---|
| 124 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 125 | } catch (NoSuchFieldException e) { |
---|
| 126 | memoryRequest = 0; |
---|
| 127 | } |
---|
[192] | 128 | List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>(); |
---|
[104] | 129 | for(ComputingNode node: nodes){ |
---|
| 130 | if(node.getFreeProcessorsNumber() >= cpuRequest && node.getFreeMemory() >= memoryRequest){ |
---|
[192] | 131 | suitableNodes.add(node); |
---|
[104] | 132 | } |
---|
| 133 | } |
---|
[192] | 134 | return suitableNodes; |
---|
[104] | 135 | } |
---|
| 136 | |
---|
| 137 | public String getName() { |
---|
| 138 | return getClass().getName(); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | public void init(Properties properties) { |
---|
| 142 | // no extra initialization is expected. |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | } |
---|