[252] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
[269] | 4 | import gridsim.gssim.ResourceHistoryItem; |
---|
| 5 | import gridsim.gssim.SubmittedTask; |
---|
[252] | 6 | |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.List; |
---|
| 9 | import java.util.Properties; |
---|
| 10 | import java.util.Random; |
---|
| 11 | |
---|
[256] | 12 | import schedframe.resources.PowerState; |
---|
[252] | 13 | import schedframe.scheduling.TaskInterface; |
---|
| 14 | import schedframe.scheduling.events.SchedulingEvent; |
---|
[269] | 15 | import schedframe.scheduling.events.TaskFinishedEvent; |
---|
[252] | 16 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
[269] | 17 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
[252] | 18 | import test.rewolucja.GSSIMJobInterface; |
---|
| 19 | import test.rewolucja.energy.profile.PStateType; |
---|
[269] | 20 | import test.rewolucja.resources.ProcessingElements; |
---|
[252] | 21 | import test.rewolucja.resources.ResourceStatus; |
---|
[256] | 22 | import test.rewolucja.resources.ResourceType; |
---|
[252] | 23 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 24 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
[269] | 25 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
[252] | 26 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
| 27 | import test.rewolucja.resources.physical.implementation.Processor; |
---|
| 28 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
[269] | 29 | import test.rewolucja.scheduling.UsedResourceList; |
---|
[252] | 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 FCFSPreferedRandomClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 36 | |
---|
| 37 | private Random rand; |
---|
| 38 | private boolean init = true; |
---|
| 39 | |
---|
[269] | 40 | int scenario = 3; |
---|
[257] | 41 | String prefered = null; |
---|
| 42 | |
---|
[252] | 43 | public FCFSPreferedRandomClusterLocalPlugin() { |
---|
| 44 | rand = new Random(5); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 48 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 49 | |
---|
| 50 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 51 | |
---|
| 52 | if( init) |
---|
| 53 | { |
---|
| 54 | List<Processor> cpus = resourceManager.getProcessors(); |
---|
| 55 | |
---|
| 56 | for( Processor cpu : cpus) |
---|
| 57 | cpu.getPowerInterface().setPState( PStateType.P0); |
---|
| 58 | |
---|
| 59 | init = false; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 65 | // chose the events types to serve. |
---|
| 66 | // Different actions for different events are possible. |
---|
| 67 | switch (event.getType()) { |
---|
[269] | 68 | case TASK_FINISHED: |
---|
| 69 | if( scenario == 2) |
---|
| 70 | { |
---|
| 71 | TaskFinishedEvent finEvent = (TaskFinishedEvent) event; |
---|
| 72 | SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId()); |
---|
| 73 | UsedResourceList<ResourceHistoryItem> usedResourcesList = subTask.getUsedResources(); |
---|
| 74 | ProcessingElements pes = (ProcessingElements)usedResourcesList.getLast().getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 75 | |
---|
| 76 | for( ComputingResource cr : pes) |
---|
| 77 | { |
---|
| 78 | ((Processor)cr).getPowerInterface().setPowerState( PowerState.OFF); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
[252] | 82 | case START_TASK_EXECUTION: |
---|
| 83 | // our tasks are placed only in first queue (see |
---|
| 84 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
| 85 | Queue q = queues.get(0); |
---|
| 86 | // check all tasks in queue |
---|
| 87 | |
---|
| 88 | for (int i = 0; i < q.size(); i++) { |
---|
| 89 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 90 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 91 | // if status of the tasks in READY |
---|
| 92 | if (task.getStatus() == Gridlet.READY) { |
---|
[256] | 93 | |
---|
[257] | 94 | ComputingNode node = chooseRandomProvider(resourceManager, ResourceStatus.FREE, task); |
---|
[256] | 95 | |
---|
| 96 | if (node != null) { |
---|
| 97 | List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task); |
---|
[269] | 98 | |
---|
| 99 | int type = Integer.parseInt( task.getJobId()) % 4; |
---|
| 100 | String model = cpus.get(0).getComputingNode().getCategory().getName(); |
---|
| 101 | //System.out.println(type + " -> " + model); |
---|
| 102 | |
---|
[256] | 103 | addToSchedulingPlan(plan, task, cpus); |
---|
[252] | 104 | } |
---|
[256] | 105 | else |
---|
| 106 | { |
---|
| 107 | switch( scenario) |
---|
| 108 | { |
---|
| 109 | case 0: break; |
---|
| 110 | case 1: break; |
---|
| 111 | case 2: |
---|
[258] | 112 | node = chooseRandomProvider(resourceManager, ResourceStatus.UNAVAILABLE, task); |
---|
| 113 | if( node != null) |
---|
| 114 | { |
---|
| 115 | List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.UNAVAILABLE, task); |
---|
| 116 | for( Processor cpu: cpus) |
---|
| 117 | cpu.getPowerInterface().setPowerState( PowerState.ON); |
---|
[269] | 118 | |
---|
| 119 | i--; |
---|
| 120 | } |
---|
[256] | 121 | break; |
---|
| 122 | } |
---|
| 123 | } |
---|
[252] | 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | switch( scenario) |
---|
| 128 | { |
---|
| 129 | case 0: break; |
---|
[256] | 130 | case 1: |
---|
[252] | 131 | |
---|
[256] | 132 | for( Processor cpu : resourceManager.getProcessors()) |
---|
[252] | 133 | { |
---|
[256] | 134 | switch( cpu.getStatus()) |
---|
| 135 | { |
---|
| 136 | case FREE: cpu.getPowerInterface().setPState( PStateType.P3); break; |
---|
| 137 | case PENDING: cpu.getPowerInterface().setPState( PStateType.P0); break; |
---|
| 138 | } |
---|
| 139 | |
---|
[252] | 140 | } |
---|
[256] | 141 | break; |
---|
| 142 | |
---|
| 143 | case 2: |
---|
| 144 | for( Processor cpu : resourceManager.getProcessors()) |
---|
| 145 | { |
---|
| 146 | switch( cpu.getStatus()) |
---|
| 147 | { |
---|
| 148 | case FREE: cpu.getPowerInterface().setPowerState( PowerState.OFF); break; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | break; |
---|
| 152 | |
---|
[252] | 153 | default: break; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | break; |
---|
| 157 | } |
---|
| 158 | return plan; |
---|
| 159 | } |
---|
| 160 | |
---|
[256] | 161 | private List<ComputingNode> findSuitableNodes(String model, int cpuRequest, ResourceStatus status, List<ComputingNode> nodes){ |
---|
[252] | 162 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
[256] | 163 | for(ComputingNode node: nodes) |
---|
| 164 | { |
---|
| 165 | if( (model == null || node.getCategory().getName().equals(model))) |
---|
| 166 | { |
---|
| 167 | @SuppressWarnings("unchecked") |
---|
| 168 | List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); |
---|
| 169 | |
---|
| 170 | if( cpus.size() >= cpuRequest) |
---|
| 171 | avNodes.add(node); |
---|
[252] | 172 | } |
---|
| 173 | } |
---|
| 174 | return avNodes; |
---|
| 175 | } |
---|
| 176 | |
---|
[256] | 177 | private int getCpuRequest( TaskInterface<?> task) |
---|
| 178 | { |
---|
| 179 | int cpuRequest; |
---|
[252] | 180 | |
---|
| 181 | try { |
---|
| 182 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 183 | } catch (NoSuchFieldException e) { |
---|
| 184 | cpuRequest = 1; |
---|
| 185 | } |
---|
| 186 | |
---|
[256] | 187 | return cpuRequest; |
---|
| 188 | } |
---|
| 189 | |
---|
[257] | 190 | private ComputingNode chooseRandomProvider(ClusterResourceManager resourceManager, ResourceStatus status, TaskInterface<?> task) { |
---|
[256] | 191 | |
---|
[269] | 192 | String preferedNode = null; |
---|
| 193 | |
---|
[256] | 194 | int cpuRequest = getCpuRequest(task); |
---|
| 195 | |
---|
[252] | 196 | List<ComputingNode> nodes = null; |
---|
| 197 | |
---|
[269] | 198 | switch( scenario) |
---|
| 199 | { |
---|
| 200 | case 3: |
---|
| 201 | int type = Integer.parseInt( task.getJobId()) % 4; |
---|
| 202 | switch( type) |
---|
| 203 | { |
---|
| 204 | case 0: preferedNode = "B"; break; |
---|
| 205 | default: preferedNode = "A"; break; |
---|
| 206 | } |
---|
| 207 | break; |
---|
| 208 | default: |
---|
| 209 | preferedNode = prefered; |
---|
| 210 | break; |
---|
| 211 | |
---|
| 212 | } |
---|
[252] | 213 | |
---|
[269] | 214 | nodes = findSuitableNodes(preferedNode, cpuRequest, status, resourceManager.getComputingNodes()); |
---|
| 215 | switch( nodes.size()) |
---|
[256] | 216 | { |
---|
[269] | 217 | case 0: break; |
---|
| 218 | case 1: return nodes.get(0); |
---|
| 219 | default: |
---|
| 220 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
| 221 | ComputingNode node = nodes.get(nodeIdx); |
---|
| 222 | return node; |
---|
[256] | 223 | } |
---|
[252] | 224 | |
---|
[269] | 225 | |
---|
| 226 | if( preferedNode != null) |
---|
[252] | 227 | { |
---|
[269] | 228 | nodes = findSuitableNodes( getUnprefered(preferedNode), cpuRequest, status, resourceManager.getComputingNodes()); |
---|
| 229 | switch( nodes.size()) |
---|
| 230 | { |
---|
| 231 | case 0: break; |
---|
| 232 | case 1: return nodes.get(0); |
---|
| 233 | default: |
---|
| 234 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
| 235 | ComputingNode node = nodes.get(nodeIdx); |
---|
| 236 | return node; |
---|
[252] | 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | return null; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | public String getName() { |
---|
| 244 | return getClass().getName(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | public void init(Properties properties) { |
---|
| 248 | // no extra initialization is expected. |
---|
| 249 | } |
---|
[256] | 250 | |
---|
| 251 | private List<Processor> chooseProcessorsForExecution( |
---|
| 252 | ComputingNode node, ResourceStatus status, TaskInterface<?> task) { |
---|
| 253 | |
---|
| 254 | int cpuRequest = getCpuRequest(task); |
---|
[252] | 255 | |
---|
[256] | 256 | List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); |
---|
| 257 | |
---|
| 258 | return cpus.subList(0, cpuRequest); |
---|
| 259 | } |
---|
[257] | 260 | |
---|
[269] | 261 | private String getUnprefered( String preferedNode) |
---|
[257] | 262 | { |
---|
[269] | 263 | if( preferedNode.equals("A")) |
---|
[257] | 264 | return "B"; |
---|
| 265 | |
---|
[269] | 266 | if( preferedNode.equals("B")) |
---|
[257] | 267 | return "A"; |
---|
| 268 | |
---|
| 269 | return null; |
---|
| 270 | } |
---|
[256] | 271 | |
---|
[252] | 272 | } |
---|
| 273 | |
---|