[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | |
---|
[241] | 5 | import java.util.ArrayList; |
---|
[104] | 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; |
---|
[241] | 14 | import test.rewolucja.energy.profile.PStateType; |
---|
[104] | 15 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 16 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 17 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
[241] | 18 | import test.rewolucja.resources.physical.implementation.Processor; |
---|
[104] | 19 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 20 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 21 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
[163] | 22 | import test.rewolucja.scheduling.queue.Queue; |
---|
[104] | 23 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 24 | |
---|
| 25 | public class FCFSRandomClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 26 | |
---|
| 27 | private Random rand; |
---|
[241] | 28 | private boolean init = true; |
---|
[104] | 29 | |
---|
| 30 | public FCFSRandomClusterLocalPlugin() { |
---|
| 31 | rand = new Random(5); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 35 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
[241] | 36 | |
---|
| 37 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 38 | |
---|
| 39 | if( init) |
---|
| 40 | { |
---|
| 41 | List<Processor> cpus = resourceManager.getProcessors(); |
---|
| 42 | |
---|
| 43 | for( Processor cpu : cpus) |
---|
[252] | 44 | cpu.getPowerInterface().setPState( PStateType.P0); |
---|
[241] | 45 | |
---|
| 46 | init = false; |
---|
| 47 | } |
---|
| 48 | |
---|
[104] | 49 | |
---|
[241] | 50 | |
---|
[104] | 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 START_TASK_EXECUTION: |
---|
| 56 | case TASK_FINISHED: |
---|
| 57 | // our tasks are placed only in first queue (see |
---|
[192] | 58 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
[163] | 59 | Queue q = queues.get(0); |
---|
[104] | 60 | // check all tasks in queue |
---|
| 61 | |
---|
| 62 | for (int i = 0; i < q.size(); i++) { |
---|
| 63 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 64 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 65 | // if status of the tasks in READY |
---|
| 66 | if (task.getStatus() == Gridlet.READY) { |
---|
[241] | 67 | String nodeName = chooseRandomProvider(resourceManager, task); |
---|
[104] | 68 | if (nodeName != null) { |
---|
| 69 | addToSchedulingPlan(plan, task, nodeName); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | break; |
---|
| 74 | } |
---|
| 75 | return plan; |
---|
| 76 | } |
---|
| 77 | |
---|
[241] | 78 | private List<ComputingNode> findSuitableNodes(int cpuRequest, List<ComputingNode> nodes){ |
---|
| 79 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
| 80 | for(ComputingNode node: nodes){ |
---|
| 81 | if(node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
| 82 | avNodes.add(node); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | return avNodes; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | private String chooseRandomProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 89 | |
---|
| 90 | int cpuRequest; |
---|
| 91 | try { |
---|
| 92 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 93 | } catch (NoSuchFieldException e) { |
---|
| 94 | cpuRequest = 1; |
---|
| 95 | } |
---|
| 96 | |
---|
[104] | 97 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
[241] | 98 | |
---|
| 99 | nodes = findSuitableNodes(cpuRequest, nodes); |
---|
| 100 | |
---|
[250] | 101 | if( nodes.size() > 0) |
---|
| 102 | { |
---|
| 103 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
| 104 | return nodes.get(nodeIdx).getName(); |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | return null; |
---|
[104] | 108 | } |
---|
| 109 | |
---|
| 110 | public String getName() { |
---|
| 111 | return getClass().getName(); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | public void init(Properties properties) { |
---|
| 115 | // no extra initialization is expected. |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | } |
---|
| 119 | |
---|