[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | |
---|
| 5 | import java.util.ArrayList; |
---|
| 6 | import java.util.Collections; |
---|
| 7 | import java.util.Comparator; |
---|
| 8 | import java.util.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
| 11 | import java.util.Properties; |
---|
| 12 | |
---|
| 13 | import schedframe.resources.PowerState; |
---|
| 14 | import schedframe.resources.units.ResourceUnit; |
---|
| 15 | import schedframe.scheduling.TaskInterface; |
---|
| 16 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 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.manager.implementation.ClusterResourceManager; |
---|
| 23 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 24 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 25 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
| 26 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 27 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 28 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
| 29 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
| 30 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 31 | |
---|
| 32 | public class FCFSNodePowerManagementClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 33 | |
---|
| 34 | public FCFSNodePowerManagementClusterLocalPlugin () { |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 38 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 39 | |
---|
| 40 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 41 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 42 | // chose the events types to serve. |
---|
| 43 | // Different actions for different events are possible. |
---|
| 44 | switch (event.getType()) { |
---|
| 45 | case START_TASK_EXECUTION: |
---|
| 46 | case TASK_FINISHED: |
---|
| 47 | // our tasks are placed only in first queue (see |
---|
| 48 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
| 49 | GSSIMQueue q = queues.get(0); |
---|
| 50 | // check all tasks in queue |
---|
| 51 | |
---|
| 52 | for (int i = 0; i < q.size(); i++) { |
---|
| 53 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 54 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 55 | // if status of the tasks in READY |
---|
| 56 | if (task.getStatus() == Gridlet.READY) { |
---|
| 57 | |
---|
| 58 | Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 59 | if (choosenResources != null) { |
---|
| 60 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 61 | } else { |
---|
| 62 | if(harnessNodes(task, resourceManager.getComputingNodes())) |
---|
| 63 | i--; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | turnOffIdleNodes(resourceManager.getComputingNodes()); |
---|
| 69 | |
---|
| 70 | break; |
---|
| 71 | } |
---|
| 72 | return plan; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 76 | |
---|
| 77 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 78 | nodes = findSuitableNodes(task, nodes); |
---|
| 79 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 80 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 81 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
| 82 | } |
---|
| 83 | }); |
---|
| 84 | if(nodes.size() > 0) |
---|
| 85 | { |
---|
| 86 | HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
| 87 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
| 88 | int cpuRequest; |
---|
| 89 | try { |
---|
| 90 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 91 | } catch (NoSuchFieldException e) { |
---|
| 92 | cpuRequest = 1; |
---|
| 93 | } |
---|
| 94 | for (int i = 0; i < nodes.get(0).getProcessors().size() && cpuRequest > 0; i++) { |
---|
| 95 | if (nodes.get(0).getProcessors().get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 96 | choosenResources.add(nodes.get(0).getProcessors().get(i)); |
---|
| 97 | cpuRequest--; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | ProcessingElements result = new ProcessingElements(nodes.get(0).getName()); |
---|
| 101 | result.addAll(choosenResources); |
---|
| 102 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
| 103 | return map; |
---|
| 104 | } else |
---|
| 105 | return null; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, List<ComputingNode> nodes){ |
---|
| 109 | int cpuRequest; |
---|
| 110 | try { |
---|
| 111 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 112 | } catch (NoSuchFieldException e) { |
---|
| 113 | cpuRequest = 1; |
---|
| 114 | } |
---|
| 115 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
| 116 | for(ComputingNode node: nodes){ |
---|
| 117 | if(node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
| 118 | avNodes.add(node); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | return avNodes; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | private void turnOffIdleNodes(List<ComputingNode> nodes){ |
---|
| 125 | for(ComputingNode node : nodes){ |
---|
| 126 | if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){ |
---|
| 127 | node.getPowerInterface().setPowerState(PowerState.OFF); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | private boolean harnessNodes(TaskInterface<?> task, List<ComputingNode> nodes){ |
---|
| 133 | int cpuRequest; |
---|
| 134 | try { |
---|
| 135 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 136 | } catch (NoSuchFieldException e) { |
---|
| 137 | cpuRequest = 1; |
---|
| 138 | } |
---|
| 139 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 140 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 141 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
| 142 | } |
---|
| 143 | }); |
---|
| 144 | for (int i = 0; i < nodes.size() && cpuRequest > 0; i++) { |
---|
| 145 | ComputingNode node = nodes.get(i); |
---|
| 146 | if(node.getPowerInterface().getPowerState() == PowerState.OFF){ |
---|
| 147 | node.getPowerInterface().setPowerState(PowerState.ON); |
---|
| 148 | cpuRequest -= node.getProcessorsNumber(); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | return cpuRequest > 0 ? false : true; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | public String getName() { |
---|
| 155 | return getClass().getName(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | public void init(Properties properties) { |
---|
| 159 | // no extra initialization is expected. |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } |
---|