[252] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | |
---|
| 5 | import java.util.ArrayList; |
---|
| 6 | import java.util.List; |
---|
| 7 | import java.util.Properties; |
---|
| 8 | import java.util.Random; |
---|
| 9 | |
---|
| 10 | import schedframe.scheduling.TaskInterface; |
---|
| 11 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 12 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 13 | import test.rewolucja.GSSIMJobInterface; |
---|
| 14 | import test.rewolucja.energy.profile.PStateType; |
---|
| 15 | import test.rewolucja.resources.ResourceStatus; |
---|
| 16 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 17 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 18 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
| 19 | import test.rewolucja.resources.physical.implementation.Processor; |
---|
| 20 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 21 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 22 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
| 23 | import test.rewolucja.scheduling.queue.Queue; |
---|
| 24 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 25 | |
---|
| 26 | public class FCFSPreferedRandomClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 27 | |
---|
| 28 | private Random rand; |
---|
| 29 | private boolean init = true; |
---|
| 30 | |
---|
| 31 | public FCFSPreferedRandomClusterLocalPlugin() { |
---|
| 32 | rand = new Random(5); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 36 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 37 | |
---|
| 38 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 39 | |
---|
| 40 | if( init) |
---|
| 41 | { |
---|
| 42 | List<Processor> cpus = resourceManager.getProcessors(); |
---|
| 43 | |
---|
| 44 | for( Processor cpu : cpus) |
---|
| 45 | cpu.getPowerInterface().setPState( PStateType.P0); |
---|
| 46 | |
---|
| 47 | init = false; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 53 | // chose the events types to serve. |
---|
| 54 | // Different actions for different events are possible. |
---|
| 55 | switch (event.getType()) { |
---|
| 56 | case START_TASK_EXECUTION: |
---|
| 57 | case TASK_FINISHED: |
---|
| 58 | // our tasks are placed only in first queue (see |
---|
| 59 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
| 60 | Queue q = queues.get(0); |
---|
| 61 | // check all tasks in queue |
---|
| 62 | |
---|
| 63 | for (int i = 0; i < q.size(); i++) { |
---|
| 64 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 65 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 66 | // if status of the tasks in READY |
---|
| 67 | if (task.getStatus() == Gridlet.READY) { |
---|
| 68 | String nodeName = chooseRandomProvider(resourceManager, task); |
---|
| 69 | if (nodeName != null) { |
---|
| 70 | addToSchedulingPlan(plan, task, nodeName); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | int scenario = 0; |
---|
| 76 | switch( scenario) |
---|
| 77 | { |
---|
| 78 | case 0: break; |
---|
| 79 | case 1: ; |
---|
| 80 | |
---|
| 81 | List<Processor> cpus = resourceManager.getProcessors(); |
---|
| 82 | for( Processor cpu : cpus) |
---|
| 83 | { |
---|
| 84 | switch( cpu.getStatus()) |
---|
| 85 | { |
---|
| 86 | case FREE: cpu.getPowerInterface().setPState( PStateType.P3); break; |
---|
| 87 | case PENDING: cpu.getPowerInterface().setPState( PStateType.P0); break; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | } |
---|
| 91 | break; |
---|
| 92 | default: break; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | break; |
---|
| 96 | } |
---|
| 97 | return plan; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | private List<ComputingNode> findSuitableNodes(String model, int cpuRequest, List<ComputingNode> nodes){ |
---|
| 101 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
| 102 | for(ComputingNode node: nodes){ |
---|
| 103 | if( (model == null || node.getCategory().getName().equals(model)) && node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
| 104 | avNodes.add(node); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | return avNodes; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | private String chooseRandomProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 111 | |
---|
| 112 | int cpuRequest; |
---|
| 113 | try { |
---|
| 114 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 115 | } catch (NoSuchFieldException e) { |
---|
| 116 | cpuRequest = 1; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | List<ComputingNode> nodes = null; |
---|
| 120 | |
---|
| 121 | String prefered = null; |
---|
| 122 | |
---|
| 123 | nodes = findSuitableNodes(prefered, cpuRequest, resourceManager.getComputingNodes()); |
---|
| 124 | if( nodes.size() > 0) |
---|
| 125 | { |
---|
| 126 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
| 127 | ComputingNode node = nodes.get(nodeIdx); |
---|
| 128 | |
---|
| 129 | //for( Processor cpu : node.getFreeProcessors()) |
---|
| 130 | // if( cpu.getPowerInterface().getPState().getName() != PStateType.P0) |
---|
| 131 | // cpu.getPowerInterface().setPState( PStateType.P0); |
---|
| 132 | |
---|
| 133 | return node.getName(); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if( prefered != null) |
---|
| 137 | { |
---|
| 138 | if( prefered.equals("A")) |
---|
| 139 | prefered = "B"; |
---|
| 140 | else |
---|
| 141 | prefered = "A"; |
---|
| 142 | |
---|
| 143 | nodes = findSuitableNodes(prefered, cpuRequest, resourceManager.getComputingNodes()); |
---|
| 144 | if( nodes.size() > 0) |
---|
| 145 | { |
---|
| 146 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
| 147 | ComputingNode node = nodes.get(nodeIdx); |
---|
| 148 | |
---|
| 149 | //for( Processor cpu : node.getFreeProcessors()) |
---|
| 150 | // if( cpu.getPowerInterface().getPState().getName() != PStateType.P0) |
---|
| 151 | // cpu.getPowerInterface().setPState( PStateType.P0); |
---|
| 152 | |
---|
| 153 | return node.getName(); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | return null; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | public String getName() { |
---|
| 161 | return getClass().getName(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | public void init(Properties properties) { |
---|
| 165 | // no extra initialization is expected. |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | } |
---|
| 169 | |
---|