[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
[270] | 4 | import gridsim.gssim.ResourceHistoryItem; |
---|
| 5 | import gridsim.gssim.SubmittedTask; |
---|
[104] | 6 | |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.Collections; |
---|
| 9 | import java.util.Comparator; |
---|
| 10 | import java.util.List; |
---|
| 11 | import java.util.Properties; |
---|
| 12 | |
---|
[269] | 13 | import schedframe.resources.PowerState; |
---|
[104] | 14 | import schedframe.scheduling.TaskInterface; |
---|
| 15 | import schedframe.scheduling.events.SchedulingEvent; |
---|
[270] | 16 | import schedframe.scheduling.events.TaskFinishedEvent; |
---|
[104] | 17 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
[270] | 18 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
[104] | 19 | import test.rewolucja.GSSIMJobInterface; |
---|
[270] | 20 | import test.rewolucja.resources.ProcessingElements; |
---|
| 21 | import test.rewolucja.resources.ResourceStatus; |
---|
| 22 | import test.rewolucja.resources.ResourceType; |
---|
[104] | 23 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 24 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
[270] | 25 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
[104] | 26 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
[270] | 27 | import test.rewolucja.resources.physical.implementation.Processor; |
---|
[104] | 28 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
[270] | 29 | import test.rewolucja.scheduling.UsedResourceList; |
---|
[104] | 30 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 31 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
[163] | 32 | import test.rewolucja.scheduling.queue.Queue; |
---|
[104] | 33 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 34 | |
---|
| 35 | public class FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin { |
---|
[269] | 36 | |
---|
| 37 | private int scenario = 1; |
---|
[104] | 38 | |
---|
| 39 | public FCFSConsolidationClusterLocalPlugin () { |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 43 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 44 | |
---|
| 45 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 46 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 47 | // chose the events types to serve. |
---|
| 48 | // Different actions for different events are possible. |
---|
| 49 | switch (event.getType()) { |
---|
| 50 | case TASK_FINISHED: |
---|
[270] | 51 | if( scenario == 1) |
---|
| 52 | { |
---|
| 53 | TaskFinishedEvent finEvent = (TaskFinishedEvent) event; |
---|
| 54 | SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId()); |
---|
| 55 | UsedResourceList<ResourceHistoryItem> usedResourcesList = subTask.getUsedResources(); |
---|
| 56 | ProcessingElements pes = (ProcessingElements)usedResourcesList.getLast().getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 57 | |
---|
| 58 | ComputingNode cn = ((Processor)pes.get(0)).getComputingNode(); |
---|
| 59 | if( cn.getFreeProcessors().size() == cn.getProcessorsNumber()) |
---|
| 60 | cn.getPowerInterface().setPowerState( PowerState.OFF); |
---|
| 61 | } |
---|
| 62 | case START_TASK_EXECUTION: |
---|
[104] | 63 | // our tasks are placed only in first queue (see |
---|
[192] | 64 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
[163] | 65 | Queue q = queues.get(0); |
---|
[104] | 66 | // check all tasks in queue |
---|
| 67 | |
---|
| 68 | for (int i = 0; i < q.size(); i++) { |
---|
| 69 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 70 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 71 | // if status of the tasks in READY |
---|
| 72 | if (task.getStatus() == Gridlet.READY) { |
---|
| 73 | |
---|
[269] | 74 | ComputingNode node = chooseResourcesForExecution(resourceManager, PowerState.ON, task); |
---|
[270] | 75 | |
---|
[269] | 76 | if (node != null) { |
---|
[270] | 77 | List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task); |
---|
| 78 | //System.out.println("Uruchamiam na zasobie: " + node.getName()); |
---|
| 79 | addToSchedulingPlan(plan, task, cpus); |
---|
[104] | 80 | } |
---|
[269] | 81 | else |
---|
| 82 | { |
---|
| 83 | node = chooseResourcesForExecution(resourceManager, PowerState.OFF, task); |
---|
| 84 | |
---|
| 85 | if( node != null) |
---|
| 86 | { |
---|
[270] | 87 | //System.out.println("Wlaczam wezel: " + node.getName()); |
---|
[269] | 88 | node.getPowerInterface().setPowerState( PowerState.ON); |
---|
| 89 | i--; |
---|
| 90 | } |
---|
| 91 | } |
---|
[104] | 92 | } |
---|
| 93 | } |
---|
[269] | 94 | |
---|
| 95 | switch( scenario) |
---|
| 96 | { |
---|
| 97 | case 0: break; |
---|
| 98 | case 1: |
---|
| 99 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 100 | for( ComputingNode node : nodes) |
---|
| 101 | if( node.getFreeProcessors().size() == node.getProcessorsNumber()) |
---|
| 102 | { |
---|
[270] | 103 | //System.out.println("Wylaczam zasob" + node.getName()); |
---|
[269] | 104 | node.getPowerInterface().setPowerState( PowerState.OFF); |
---|
[270] | 105 | //System.out.println("Zasob w stanie: " + node.getPowerInterface().getPowerState()); |
---|
[269] | 106 | } |
---|
| 107 | break; |
---|
| 108 | } |
---|
| 109 | |
---|
[104] | 110 | |
---|
| 111 | break; |
---|
| 112 | } |
---|
[270] | 113 | |
---|
[104] | 114 | return plan; |
---|
| 115 | } |
---|
| 116 | |
---|
[269] | 117 | private ComputingNode chooseResourcesForExecution(ClusterResourceManager resourceManager, PowerState status, TaskInterface<?> task) { |
---|
[104] | 118 | |
---|
[270] | 119 | //System.out.println("Szukam zasobow w stanie: " +status); |
---|
| 120 | |
---|
[104] | 121 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
[269] | 122 | nodes = findSuitableNodes(task, status, nodes); |
---|
| 123 | |
---|
[104] | 124 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 125 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 126 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
| 127 | } |
---|
| 128 | }); |
---|
[269] | 129 | |
---|
[104] | 130 | if(nodes.size() > 0) |
---|
[269] | 131 | return nodes.get(0); |
---|
| 132 | |
---|
| 133 | return null; |
---|
[104] | 134 | } |
---|
| 135 | |
---|
[269] | 136 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, PowerState status, List<ComputingNode> nodes){ |
---|
| 137 | |
---|
[270] | 138 | int cpuRequest = getCpuRequest(task); |
---|
| 139 | |
---|
[192] | 140 | List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>(); |
---|
[270] | 141 | |
---|
[269] | 142 | for(ComputingNode node: nodes) |
---|
| 143 | { |
---|
[270] | 144 | if( node.getPowerInterface().getPowerState() == status) |
---|
[269] | 145 | switch( status) |
---|
| 146 | { |
---|
| 147 | case ON: |
---|
| 148 | if(node.getFreeProcessorsNumber() >= cpuRequest) |
---|
[270] | 149 | { |
---|
[269] | 150 | suitableNodes.add(node); |
---|
[270] | 151 | } |
---|
[269] | 152 | break; |
---|
| 153 | case OFF: |
---|
| 154 | if( node.getProcessorsNumber() >= cpuRequest) |
---|
| 155 | suitableNodes.add(node); |
---|
| 156 | break; |
---|
| 157 | } |
---|
[104] | 158 | } |
---|
[269] | 159 | |
---|
[192] | 160 | return suitableNodes; |
---|
[104] | 161 | } |
---|
| 162 | |
---|
| 163 | public String getName() { |
---|
| 164 | return getClass().getName(); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | public void init(Properties properties) { |
---|
| 168 | // no extra initialization is expected. |
---|
| 169 | } |
---|
[270] | 170 | |
---|
| 171 | private List<Processor> chooseProcessorsForExecution( |
---|
| 172 | ComputingNode node, ResourceStatus status, TaskInterface<?> task) { |
---|
| 173 | |
---|
| 174 | int cpuRequest = getCpuRequest(task); |
---|
[104] | 175 | |
---|
[270] | 176 | List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); |
---|
| 177 | |
---|
| 178 | return cpus.subList(0, cpuRequest); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | private int getCpuRequest( TaskInterface<?> task) |
---|
| 182 | { |
---|
| 183 | int cpuRequest; |
---|
| 184 | |
---|
| 185 | try { |
---|
| 186 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 187 | } catch (NoSuchFieldException e) { |
---|
| 188 | cpuRequest = 1; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | return cpuRequest; |
---|
| 192 | } |
---|
[104] | 193 | } |
---|