[271] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | import gridsim.gssim.ResourceHistoryItem; |
---|
| 5 | import gridsim.gssim.SubmittedTask; |
---|
| 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 | |
---|
| 13 | import schedframe.resources.PowerState; |
---|
| 14 | import schedframe.scheduling.TaskInterface; |
---|
| 15 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 16 | import schedframe.scheduling.events.TaskFinishedEvent; |
---|
| 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.ResourceType; |
---|
| 23 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 24 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 25 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 26 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
| 27 | import test.rewolucja.resources.physical.implementation.Processor; |
---|
| 28 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 29 | import test.rewolucja.scheduling.UsedResourceList; |
---|
| 30 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 31 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
| 32 | import test.rewolucja.scheduling.queue.Queue; |
---|
| 33 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 34 | |
---|
| 35 | public class FCFSPreferedConsolidationClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 36 | |
---|
| 37 | private int scenario = 2; |
---|
| 38 | |
---|
| 39 | public FCFSPreferedConsolidationClusterLocalPlugin () { |
---|
| 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: |
---|
| 51 | switch(scenario) |
---|
| 52 | { |
---|
| 53 | case 1: |
---|
| 54 | case 2: |
---|
| 55 | case 3: |
---|
| 56 | TaskFinishedEvent finEvent = (TaskFinishedEvent) event; |
---|
| 57 | SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId()); |
---|
| 58 | UsedResourceList<ResourceHistoryItem> usedResourcesList = subTask.getUsedResources(); |
---|
| 59 | ProcessingElements pes = (ProcessingElements)usedResourcesList.getLast().getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 60 | |
---|
| 61 | ComputingNode cn = ((Processor)pes.get(0)).getComputingNode(); |
---|
| 62 | if( cn.getFreeProcessors().size() == cn.getProcessorsNumber()) |
---|
| 63 | { |
---|
| 64 | cn.getPowerInterface().setPowerState( PowerState.OFF); |
---|
| 65 | //System.out.println("Wylaczam wezel: " + cn.getName()); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | case START_TASK_EXECUTION: |
---|
| 69 | // our tasks are placed only in first queue (see |
---|
| 70 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
| 71 | Queue q = queues.get(0); |
---|
| 72 | // check all tasks in queue |
---|
| 73 | |
---|
| 74 | for (int i = 0; i < q.size(); i++) { |
---|
| 75 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 76 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 77 | // if status of the tasks in READY |
---|
| 78 | if (task.getStatus() == Gridlet.READY) { |
---|
| 79 | |
---|
| 80 | ComputingNode node = chooseResourcesForExecution(resourceManager, PowerState.ON, task); |
---|
| 81 | |
---|
| 82 | if (node != null) { |
---|
| 83 | List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task); |
---|
| 84 | //System.out.println( task.getJobId() + " -> " + node.getName()); |
---|
| 85 | addToSchedulingPlan(plan, task, cpus); |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | node = chooseResourcesForExecution(resourceManager, PowerState.OFF, task); |
---|
| 90 | |
---|
| 91 | if( node != null) |
---|
| 92 | { |
---|
| 93 | //System.out.println("Wlaczam wezel: " + node.getName()); |
---|
| 94 | node.getPowerInterface().setPowerState( PowerState.ON); |
---|
| 95 | i--; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | switch( scenario) |
---|
| 102 | { |
---|
| 103 | case 0: break; |
---|
| 104 | case 1: |
---|
| 105 | case 2: |
---|
| 106 | case 3: |
---|
| 107 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 108 | for( ComputingNode node : nodes) |
---|
| 109 | if( node.getFreeProcessors().size() == node.getProcessorsNumber()) |
---|
| 110 | { |
---|
| 111 | //System.out.println("Wylaczam zasob" + node.getName()); |
---|
| 112 | node.getPowerInterface().setPowerState( PowerState.OFF); |
---|
| 113 | //System.out.println("Zasob w stanie: " + node.getPowerInterface().getPowerState()); |
---|
| 114 | } |
---|
| 115 | break; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | break; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return plan; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | private ComputingNode chooseResourcesForExecution(ClusterResourceManager resourceManager, PowerState status, TaskInterface<?> task) { |
---|
| 126 | |
---|
| 127 | //System.out.println("Szukam zasobow w stanie: " +status); |
---|
| 128 | |
---|
| 129 | int type = Integer.parseInt( task.getJobId()) % 4; |
---|
| 130 | String category = null; |
---|
| 131 | |
---|
| 132 | switch( scenario) |
---|
| 133 | { |
---|
| 134 | case 2: |
---|
| 135 | if( type == 0) |
---|
| 136 | category = "B"; |
---|
| 137 | else |
---|
| 138 | category = "A"; |
---|
| 139 | break; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 144 | nodes = findSuitableNodes(task, category, status, nodes); |
---|
| 145 | |
---|
| 146 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 147 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 148 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
| 149 | } |
---|
| 150 | }); |
---|
| 151 | |
---|
| 152 | if(nodes.size() > 0) |
---|
| 153 | return nodes.get(0); |
---|
| 154 | |
---|
| 155 | switch( scenario) |
---|
| 156 | { |
---|
| 157 | case 3: |
---|
| 158 | nodes = findSuitableNodes(task, getUnprefered(category), status, nodes); |
---|
| 159 | |
---|
| 160 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 161 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 162 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
| 163 | } |
---|
| 164 | }); |
---|
| 165 | |
---|
| 166 | if(nodes.size() > 0) |
---|
| 167 | return nodes.get(0); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | return null; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, String category, PowerState status, List<ComputingNode> nodes){ |
---|
| 174 | |
---|
| 175 | int cpuRequest = getCpuRequest(task); |
---|
| 176 | |
---|
| 177 | List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>(); |
---|
| 178 | |
---|
| 179 | for(ComputingNode node: nodes) |
---|
| 180 | { |
---|
| 181 | if( category == null || category.equals(node.getCategory().getName()) && node.getPowerInterface().getPowerState() == status) |
---|
| 182 | switch( status) |
---|
| 183 | { |
---|
| 184 | case ON: |
---|
| 185 | if(node.getFreeProcessorsNumber() >= cpuRequest) |
---|
| 186 | { |
---|
| 187 | suitableNodes.add(node); |
---|
| 188 | } |
---|
| 189 | break; |
---|
| 190 | case OFF: |
---|
| 191 | if( node.getProcessorsNumber() >= cpuRequest) |
---|
| 192 | suitableNodes.add(node); |
---|
| 193 | break; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | return suitableNodes; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | public String getName() { |
---|
| 201 | return getClass().getName(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | public void init(Properties properties) { |
---|
| 205 | // no extra initialization is expected. |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | private List<Processor> chooseProcessorsForExecution( |
---|
| 209 | ComputingNode node, ResourceStatus status, TaskInterface<?> task) { |
---|
| 210 | |
---|
| 211 | int cpuRequest = getCpuRequest(task); |
---|
| 212 | |
---|
| 213 | List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); |
---|
| 214 | |
---|
| 215 | return cpus.subList(0, cpuRequest); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | private int getCpuRequest( TaskInterface<?> task) |
---|
| 219 | { |
---|
| 220 | int cpuRequest; |
---|
| 221 | |
---|
| 222 | try { |
---|
| 223 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 224 | } catch (NoSuchFieldException e) { |
---|
| 225 | cpuRequest = 1; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | return cpuRequest; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | private String getUnprefered( String preferedNode) |
---|
| 232 | { |
---|
| 233 | if( preferedNode.equals("A")) |
---|
| 234 | return "B"; |
---|
| 235 | |
---|
| 236 | if( preferedNode.equals("B")) |
---|
| 237 | return "A"; |
---|
| 238 | |
---|
| 239 | return null; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | } |
---|