[708] | 1 | package test.article.recs.plugins.scheduling; |
---|
| 2 | |
---|
| 3 | import gridsim.dcworms.DCWormsTags; |
---|
| 4 | |
---|
| 5 | import java.io.FileNotFoundException; |
---|
| 6 | import java.io.IOException; |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
| 11 | import java.util.MissingResourceException; |
---|
| 12 | import java.util.Random; |
---|
| 13 | |
---|
| 14 | import schedframe.events.scheduling.SchedulingEvent; |
---|
| 15 | import schedframe.resources.ResourceStatus; |
---|
| 16 | import schedframe.resources.computing.ComputingNode; |
---|
| 17 | import schedframe.resources.computing.ComputingResource; |
---|
| 18 | import schedframe.resources.computing.Core; |
---|
| 19 | import schedframe.resources.computing.Processor; |
---|
| 20 | import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; |
---|
| 21 | import schedframe.resources.units.ProcessingElements; |
---|
| 22 | import schedframe.resources.units.ResourceUnit; |
---|
| 23 | import schedframe.resources.units.ResourceUnitName; |
---|
| 24 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 25 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 26 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 27 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 28 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 29 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
| 30 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 31 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 32 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 33 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 34 | |
---|
| 35 | public class RecsExclusivenessRandomNodePowManSP extends RecsSP { |
---|
| 36 | |
---|
| 37 | private Random rand = new Random(5); |
---|
| 38 | |
---|
| 39 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
| 40 | ResourceManager resManager, ModuleList modules) { |
---|
| 41 | |
---|
| 42 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 43 | SchedulingPlan plan = new SchedulingPlan(); |
---|
| 44 | // choose the events types to serve. |
---|
| 45 | // Different actions for different events are possible. |
---|
| 46 | switch (event.getType()) { |
---|
| 47 | case START_TASK_EXECUTION: |
---|
| 48 | case TASK_FINISHED: |
---|
| 49 | //case TIMER: |
---|
| 50 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
| 51 | TaskQueue q = queues.get(0); |
---|
| 52 | // check all tasks in queue |
---|
| 53 | |
---|
| 54 | for (int i = 0; i < q.size(); i++) { |
---|
| 55 | TaskInterface<?> task = q.get(i); |
---|
| 56 | initApplicationType(task); |
---|
| 57 | |
---|
| 58 | // if status of the tasks in READY |
---|
| 59 | if (task.getStatus() == DCWormsTags.READY) { |
---|
| 60 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 61 | if (choosenResources != null) { |
---|
| 62 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | turnOffIdleNodes(resourceManager.getComputingNodes()); |
---|
| 67 | break; |
---|
| 68 | } |
---|
| 69 | return plan; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
| 73 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 74 | |
---|
| 75 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
| 76 | |
---|
| 77 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 78 | List<ComputingNode> avNodes = filterNodes(nodes, task); |
---|
| 79 | if(avNodes.size() == 0) |
---|
| 80 | return null; |
---|
| 81 | ComputingNode node = randomNode(avNodes); |
---|
| 82 | if(node.getStatus() == ResourceStatus.UNAVAILABLE) { |
---|
| 83 | node.getPowerInterface().setPowerState(StandardPowerStateName.ON); |
---|
| 84 | } |
---|
| 85 | int cpuRequest; |
---|
| 86 | try { |
---|
| 87 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 88 | } catch (NoSuchFieldException e) { |
---|
| 89 | cpuRequest = 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if (cpuRequest != 0) { |
---|
| 93 | |
---|
| 94 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
| 95 | |
---|
| 96 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
[734] | 97 | for (int i = 0; i < cores.size(); i++) { |
---|
[708] | 98 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
[734] | 99 | //choosenResources.add(cores.get(i)); |
---|
[708] | 100 | cpuRequest--; |
---|
| 101 | } |
---|
| 102 | } |
---|
[734] | 103 | choosenResources.add(node); |
---|
[708] | 104 | if (cpuRequest > 0) { |
---|
[734] | 105 | //return null; |
---|
[708] | 106 | } |
---|
| 107 | ProcessingElements pe = new ProcessingElements(); |
---|
| 108 | pe.addAll(choosenResources); |
---|
| 109 | map.put(StandardResourceUnitName.PE, pe); |
---|
| 110 | return map; |
---|
| 111 | } |
---|
| 112 | return null; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){ |
---|
| 116 | List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>(); |
---|
| 117 | for (ComputingNode node : nodes) { |
---|
| 118 | int cpuRequest; |
---|
| 119 | try { |
---|
| 120 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 121 | } catch (NoSuchFieldException e) { |
---|
| 122 | cpuRequest = 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if (cpuRequest != 0) { |
---|
| 126 | |
---|
| 127 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
| 128 | if (cores.size() < cpuRequest) { |
---|
| 129 | continue; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | int freeCores = 0; |
---|
| 133 | for(Core core: cores){ |
---|
| 134 | if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE) |
---|
| 135 | freeCores++; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | if(freeCores != cores.size()) |
---|
| 139 | continue; |
---|
| 140 | |
---|
| 141 | try { |
---|
| 142 | if(!getExecutiveness(createExecutivenessQuery(task, node))) |
---|
| 143 | continue; |
---|
| 144 | } catch (FileNotFoundException e) { |
---|
| 145 | continue; |
---|
| 146 | } catch (IOException e) { |
---|
| 147 | continue; |
---|
| 148 | } catch (MissingResourceException e){ |
---|
| 149 | continue; |
---|
| 150 | } |
---|
| 151 | filteredNodes.add(node); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | return filteredNodes; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | private ComputingNode randomNode(List<ComputingNode> nodes){ |
---|
| 159 | return nodes.get(rand.nextInt(nodes.size())); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | private void turnOffIdleNodes(List<ComputingNode> nodes){ |
---|
| 163 | for(ComputingNode node : nodes){ |
---|
| 164 | Processor proc = node.getProcessors().get(0); |
---|
| 165 | int freeCores = 0; |
---|
| 166 | for(Core core: proc.getCores()){ |
---|
| 167 | if(core.getStatus() == ResourceStatus.FREE) |
---|
| 168 | freeCores++; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | if(freeCores == proc.getCores().size()) |
---|
| 172 | node.getPowerInterface().setPowerState(StandardPowerStateName.OFF); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | } |
---|