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