[658] | 1 | package test.article.recs.plugins.scheduling; |
---|
| 2 | |
---|
[679] | 3 | import gridsim.dcworms.DCWormsTags; |
---|
| 4 | |
---|
[658] | 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; |
---|
[708] | 24 | import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; |
---|
[658] | 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 | |
---|
[679] | 40 | public class RecsExclusivenessEnOptSP extends RecsSP { |
---|
[658] | 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); |
---|
[679] | 65 | initApplicationType(task); |
---|
| 66 | |
---|
[658] | 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 | } |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | break; |
---|
| 77 | } |
---|
| 78 | return plan; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
| 82 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 83 | |
---|
| 84 | Map<ResourceUnitName, ResourceUnit> map; |
---|
| 85 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 86 | Collections.sort(nodes, new EnergyComparator(task)); |
---|
[708] | 87 | //System.out.println("*****"); |
---|
[658] | 88 | for (ComputingNode node : nodes) { |
---|
[708] | 89 | //System.out.println(node.getCategory()); |
---|
[658] | 90 | int cpuRequest; |
---|
| 91 | try { |
---|
| 92 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 93 | } catch (NoSuchFieldException e) { |
---|
| 94 | cpuRequest = 0; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | if (cpuRequest != 0) { |
---|
| 98 | |
---|
| 99 | /*Properties properties = new Properties(); |
---|
| 100 | properties.setProperty("type", StandardResourceType.Core.getName()); |
---|
| 101 | properties.setProperty("status", ResourceStatus.FREE.toString()); |
---|
| 102 | */ |
---|
| 103 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
| 104 | if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) { |
---|
| 105 | continue; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | int freeCores = 0; |
---|
| 109 | for(Core core: cores){ |
---|
| 110 | if(core.getStatus() == ResourceStatus.FREE) |
---|
| 111 | freeCores++; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if(freeCores != cores.size()) |
---|
| 115 | continue; |
---|
[708] | 116 | |
---|
| 117 | try { |
---|
| 118 | if(!getExecutiveness(createExecutivenessQuery(task, node))) |
---|
| 119 | continue; |
---|
| 120 | } catch (FileNotFoundException e) { |
---|
| 121 | continue; |
---|
| 122 | } catch (IOException e) { |
---|
| 123 | continue; |
---|
| 124 | } catch (MissingResourceException e){ |
---|
| 125 | continue; |
---|
| 126 | } |
---|
| 127 | |
---|
[658] | 128 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
[734] | 129 | for (int i = 0; i < cores.size(); i++) { |
---|
[658] | 130 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
[734] | 131 | //choosenResources.add(cores.get(i)); |
---|
[658] | 132 | cpuRequest--; |
---|
| 133 | } |
---|
| 134 | } |
---|
[734] | 135 | choosenResources.add(node); |
---|
[658] | 136 | if (cpuRequest > 0) { |
---|
[734] | 137 | //continue; |
---|
[658] | 138 | } |
---|
| 139 | map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
| 140 | ProcessingElements pe = new ProcessingElements(); |
---|
| 141 | pe.addAll(choosenResources); |
---|
| 142 | map.put(StandardResourceUnitName.PE, pe); |
---|
| 143 | return map; |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | return null; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | protected String createQuery(TaskInterface<?> task, ComputingNode node) { |
---|
| 154 | String query; |
---|
| 155 | query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task); |
---|
| 156 | return query; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | private String getApplicationType(TaskInterface<?> task){ |
---|
| 160 | AppType appType = taskToApp.getAppType(task); |
---|
| 161 | return appType.toString(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | private String getNodeCategory(ComputingNode node){ |
---|
| 165 | |
---|
| 166 | return node.getCategory(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | private int getFrequency(ComputingNode node){ |
---|
| 170 | Processor proc = (Processor) node.getProcessors().get(0); |
---|
| 171 | double freq = proc.getPowerInterface().getFrequency(); |
---|
| 172 | return Double.valueOf(freq).intValue(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | private int getCoreCnt(TaskInterface<?> task){ |
---|
| 176 | double cpuReq; |
---|
| 177 | try { |
---|
| 178 | cpuReq = task.getCpuCntRequest(); |
---|
| 179 | } catch (NoSuchFieldException e) { |
---|
| 180 | cpuReq = 1; |
---|
| 181 | } |
---|
| 182 | return Double.valueOf(cpuReq).intValue(); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{ |
---|
| 186 | ResourceBundle powBundle = getPowBundle(); |
---|
| 187 | return Double.valueOf(powBundle.getString(query)).doubleValue(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{ |
---|
| 191 | if(powBundle == null){ |
---|
| 192 | powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME)); |
---|
| 193 | } |
---|
| 194 | return powBundle; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{ |
---|
| 198 | ResourceBundle timeBundle = getTimeBundle(); |
---|
| 199 | return Double.valueOf(timeBundle.getString(query)).doubleValue(); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{ |
---|
| 203 | if(timeBundle == null){ |
---|
| 204 | timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME)); |
---|
| 205 | } |
---|
| 206 | return timeBundle; |
---|
| 207 | } |
---|
| 208 | class EnergyComparator implements Comparator<ComputingNode>{ |
---|
| 209 | private TaskInterface<?> task; |
---|
| 210 | |
---|
| 211 | public EnergyComparator(TaskInterface<?> task){ |
---|
| 212 | this.task = task; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 216 | double node1EU = Double.MAX_VALUE; |
---|
| 217 | double node2EU = Double.MAX_VALUE; |
---|
| 218 | try { |
---|
[711] | 219 | node1EU = getMeasuredTime(createQuery(task, node1)) * Math.abs(getMeasuredPower(createQuery(task, node1)) - node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); |
---|
| 220 | //System.out.println("1" + node1.getCategory() +"; " + getApplicationType(task) + ": " + node1EU + "; "+ getMeasuredTime(createQuery(task, node1)) +";" + node1.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node1))); |
---|
[708] | 221 | } catch (FileNotFoundException e) { |
---|
[658] | 222 | |
---|
| 223 | } catch (IOException e) { |
---|
| 224 | |
---|
| 225 | } catch (MissingResourceException e){ |
---|
| 226 | |
---|
[708] | 227 | } catch (NoSuchFieldException e) { |
---|
| 228 | |
---|
[658] | 229 | } |
---|
[711] | 230 | |
---|
| 231 | try { |
---|
| 232 | node2EU = getMeasuredTime(createQuery(task, node2)) * Math.abs(getMeasuredPower(createQuery(task, node2)) - node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); |
---|
| 233 | //System.out.println("2" + node2.getCategory() +"; " + getApplicationType(task) + ": " + node2EU + "; "+ getMeasuredTime(createQuery(task, node2)) +";" + node2.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON) + ":" + getMeasuredPower(createQuery(task, node2))); |
---|
| 234 | } catch (FileNotFoundException e) { |
---|
| 235 | |
---|
| 236 | } catch (IOException e) { |
---|
| 237 | |
---|
| 238 | } catch (MissingResourceException e){ |
---|
| 239 | |
---|
| 240 | } catch (NoSuchFieldException e) { |
---|
| 241 | |
---|
| 242 | } |
---|
[658] | 243 | if(node1EU > node2EU) |
---|
| 244 | return 1; |
---|
| 245 | else if (node1EU < node2EU) |
---|
| 246 | return -1; |
---|
| 247 | else return 0; |
---|
| 248 | |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | } |
---|