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