[708] | 1 | package test.article.recs.plugins.scheduling; |
---|
| 2 | |
---|
| 3 | import gridsim.dcworms.DCWormsTags; |
---|
| 4 | |
---|
| 5 | import java.io.FileInputStream; |
---|
| 6 | import java.io.FileNotFoundException; |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.util.ArrayList; |
---|
| 9 | import java.util.Collections; |
---|
| 10 | import java.util.Comparator; |
---|
| 11 | import java.util.HashMap; |
---|
| 12 | import java.util.List; |
---|
| 13 | import java.util.Map; |
---|
| 14 | import java.util.MissingResourceException; |
---|
| 15 | import java.util.PropertyResourceBundle; |
---|
| 16 | import java.util.ResourceBundle; |
---|
| 17 | |
---|
| 18 | import schedframe.events.scheduling.SchedulingEvent; |
---|
| 19 | import schedframe.resources.ResourceStatus; |
---|
| 20 | import schedframe.resources.computing.ComputingNode; |
---|
| 21 | import schedframe.resources.computing.ComputingResource; |
---|
| 22 | import schedframe.resources.computing.Core; |
---|
| 23 | import schedframe.resources.computing.Processor; |
---|
| 24 | import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; |
---|
| 25 | import schedframe.resources.units.ProcessingElements; |
---|
| 26 | import schedframe.resources.units.ResourceUnit; |
---|
| 27 | import schedframe.resources.units.ResourceUnitName; |
---|
| 28 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 29 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 30 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 31 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 32 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 33 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
| 34 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 35 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 36 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 37 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 38 | import test.article.recs.utils.AppType; |
---|
| 39 | |
---|
| 40 | public class RecsExclusivenessEnOptNodePowManSP extends RecsSP { |
---|
| 41 | |
---|
| 42 | private static String TIME_DATA_FILE_NAME = "src/test/article/recs/data/time_data.properties"; |
---|
| 43 | private static String POWER_DATA_FILE_NAME = "src/test/article/recs/data/power_data.properties"; |
---|
| 44 | |
---|
| 45 | private ResourceBundle powBundle; |
---|
| 46 | private ResourceBundle timeBundle; |
---|
| 47 | |
---|
| 48 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
| 49 | ResourceManager resManager, ModuleList modules) { |
---|
| 50 | |
---|
| 51 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 52 | SchedulingPlan plan = new SchedulingPlan(); |
---|
| 53 | // choose 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 | //case TIMER: |
---|
| 59 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
| 60 | TaskQueue q = queues.get(0); |
---|
| 61 | // check all tasks in queue |
---|
| 62 | |
---|
| 63 | for (int i = 0; i < q.size(); i++) { |
---|
| 64 | TaskInterface<?> task = q.get(i); |
---|
| 65 | initApplicationType(task); |
---|
| 66 | |
---|
| 67 | // if status of the tasks in READY |
---|
| 68 | if (task.getStatus() == DCWormsTags.READY) { |
---|
| 69 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 70 | if (choosenResources != null) { |
---|
| 71 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 72 | }/* else { |
---|
| 73 | if(harnessIdleNodesToWork(task, resourceManager.getComputingNodes())) |
---|
| 74 | i--; |
---|
| 75 | }*/ |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | turnOffIdleNodes(resourceManager.getComputingNodes()); |
---|
| 79 | break; |
---|
| 80 | } |
---|
| 81 | return plan; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
| 85 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 86 | |
---|
| 87 | Map<ResourceUnitName, ResourceUnit> map; |
---|
| 88 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 89 | Collections.sort(nodes, new EnergyComparator(task)); |
---|
| 90 | for (ComputingNode node : nodes) { |
---|
| 91 | int cpuRequest; |
---|
| 92 | try { |
---|
| 93 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 94 | } catch (NoSuchFieldException e) { |
---|
| 95 | cpuRequest = 0; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | if (cpuRequest != 0) { |
---|
| 99 | |
---|
| 100 | /*Properties properties = new Properties(); |
---|
| 101 | properties.setProperty("type", StandardResourceType.Core.getName()); |
---|
| 102 | properties.setProperty("status", ResourceStatus.FREE.toString()); |
---|
| 103 | */ |
---|
| 104 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
| 105 | if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) { |
---|
| 106 | continue; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | int freeCores = 0; |
---|
| 110 | for(Core core: cores){ |
---|
| 111 | if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE) |
---|
| 112 | freeCores++; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | if(freeCores != cores.size()) |
---|
| 116 | continue; |
---|
| 117 | |
---|
| 118 | if(node.getStatus() == ResourceStatus.UNAVAILABLE) { |
---|
| 119 | node.getPowerInterface().setPowerState(StandardPowerStateName.ON); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | try { |
---|
| 123 | if(!getExecutiveness(createExecutivenessQuery(task, node))) |
---|
| 124 | continue; |
---|
| 125 | } catch (FileNotFoundException e) { |
---|
| 126 | continue; |
---|
| 127 | } catch (IOException e) { |
---|
| 128 | continue; |
---|
| 129 | } catch (MissingResourceException e){ |
---|
| 130 | continue; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
[734] | 134 | for (int i = 0; i < cores.size(); i++) { |
---|
[708] | 135 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
[734] | 136 | //choosenResources.add(cores.get(i)); |
---|
[708] | 137 | cpuRequest--; |
---|
| 138 | } |
---|
| 139 | } |
---|
[734] | 140 | choosenResources.add(node); |
---|
[708] | 141 | if (cpuRequest > 0) { |
---|
[734] | 142 | //continue; |
---|
[708] | 143 | } |
---|
| 144 | map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
| 145 | ProcessingElements pe = new ProcessingElements(); |
---|
| 146 | pe.addAll(choosenResources); |
---|
| 147 | map.put(StandardResourceUnitName.PE, pe); |
---|
| 148 | return map; |
---|
| 149 | |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | return null; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | protected String createQuery(TaskInterface<?> task, ComputingNode node) { |
---|
| 159 | String query; |
---|
| 160 | query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task); |
---|
| 161 | return query; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | private String getApplicationType(TaskInterface<?> task){ |
---|
| 165 | AppType appType = taskToApp.getAppType(task); |
---|
| 166 | return appType.toString(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | private String getNodeCategory(ComputingNode node){ |
---|
| 170 | |
---|
| 171 | return node.getCategory(); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | private int getFrequency(ComputingNode node){ |
---|
| 175 | Processor proc = (Processor) node.getProcessors().get(0); |
---|
| 176 | double freq = proc.getPowerInterface().getFrequency(); |
---|
| 177 | return Double.valueOf(freq).intValue(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | private int getCoreCnt(TaskInterface<?> task){ |
---|
| 181 | double cpuReq; |
---|
| 182 | try { |
---|
| 183 | cpuReq = task.getCpuCntRequest(); |
---|
| 184 | } catch (NoSuchFieldException e) { |
---|
| 185 | cpuReq = 1; |
---|
| 186 | } |
---|
| 187 | return Double.valueOf(cpuReq).intValue(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{ |
---|
| 191 | ResourceBundle powBundle = getPowBundle(); |
---|
| 192 | return Double.valueOf(powBundle.getString(query)).doubleValue(); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{ |
---|
| 196 | if(powBundle == null){ |
---|
| 197 | powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME)); |
---|
| 198 | } |
---|
| 199 | return powBundle; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{ |
---|
| 203 | ResourceBundle timeBundle = getTimeBundle(); |
---|
| 204 | return Double.valueOf(timeBundle.getString(query)).doubleValue(); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{ |
---|
| 208 | if(timeBundle == null){ |
---|
| 209 | timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME)); |
---|
| 210 | } |
---|
| 211 | return timeBundle; |
---|
| 212 | } |
---|
| 213 | class EnergyComparator implements Comparator<ComputingNode>{ |
---|
| 214 | private TaskInterface<?> task; |
---|
| 215 | |
---|
| 216 | public EnergyComparator(TaskInterface<?> task){ |
---|
| 217 | this.task = task; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 221 | double node1EU = Double.MAX_VALUE; |
---|
| 222 | double node2EU = Double.MAX_VALUE; |
---|
| 223 | try { |
---|
| 224 | node1EU = getMeasuredTime(createQuery(task, node1)) * getMeasuredPower(createQuery(task, node1)); |
---|
[711] | 225 | } catch (FileNotFoundException e) { |
---|
| 226 | |
---|
| 227 | } catch (IOException e) { |
---|
| 228 | |
---|
| 229 | } catch (MissingResourceException e){ |
---|
| 230 | |
---|
| 231 | } |
---|
| 232 | try { |
---|
[708] | 233 | node2EU = getMeasuredTime(createQuery(task, node2)) * getMeasuredPower(createQuery(task, node2)); |
---|
| 234 | } catch (FileNotFoundException e) { |
---|
| 235 | |
---|
| 236 | } catch (IOException e) { |
---|
| 237 | |
---|
| 238 | } catch (MissingResourceException e){ |
---|
| 239 | |
---|
| 240 | } |
---|
| 241 | if(node1EU > node2EU) |
---|
| 242 | return 1; |
---|
| 243 | else if (node1EU < node2EU) |
---|
| 244 | return -1; |
---|
| 245 | else return 0; |
---|
| 246 | |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | private void turnOffIdleNodes(List<ComputingNode> nodes){ |
---|
| 251 | for(ComputingNode node : nodes){ |
---|
| 252 | Processor proc = node.getProcessors().get(0); |
---|
| 253 | int freeCores = 0; |
---|
| 254 | for(Core core: proc.getCores()){ |
---|
| 255 | if(core.getStatus() == ResourceStatus.FREE) |
---|
| 256 | freeCores++; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | if(freeCores == proc.getCores().size()) |
---|
| 260 | node.getPowerInterface().setPowerState(StandardPowerStateName.OFF); |
---|
| 261 | |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /*private boolean harnessIdleNodesToWork(TaskInterface<?> task, List<ComputingNode> nodes){ |
---|
| 266 | |
---|
| 267 | int cpuRequest; |
---|
| 268 | try { |
---|
| 269 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 270 | } catch (NoSuchFieldException e) { |
---|
| 271 | cpuRequest = 0; |
---|
| 272 | } |
---|
| 273 | for (int i = 0; i < nodes.size(); i++) { |
---|
| 274 | ComputingNode node = nodes.get(i); |
---|
| 275 | if(node.getPowerInterface().getPowerState() == StandardPowerStateName.OFF){ |
---|
| 276 | |
---|
| 277 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
| 278 | if (cores.size() < cpuRequest) { |
---|
| 279 | continue; |
---|
| 280 | } |
---|
| 281 | node.getPowerInterface().setPowerState(StandardPowerStateName.ON); |
---|
| 282 | return true; |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | return false; |
---|
| 286 | }*/ |
---|
| 287 | } |
---|