1 | package test.thermal.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.Node; |
---|
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.manager.tasks.JobRegistryImpl; |
---|
33 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
34 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
35 | import schedframe.scheduling.plugin.ModuleList; |
---|
36 | import schedframe.scheduling.queue.TaskQueue; |
---|
37 | import schedframe.scheduling.queue.TaskQueueList; |
---|
38 | import schedframe.scheduling.tasks.TaskInterface; |
---|
39 | import test.thermal.recs.plugins.energy.ThermalConstants; |
---|
40 | import test.thermal.recs.utils.AppType; |
---|
41 | |
---|
42 | public class RecsExclusivenessCoolestSP extends RecsSP { |
---|
43 | |
---|
44 | private static String TIME_DATA_FILE_NAME = "src/test/thermal/recs/data/time_data.properties"; |
---|
45 | private static String POWER_DATA_FILE_NAME = "src/test/thermal/recs/data/power_data.properties"; |
---|
46 | |
---|
47 | private ResourceBundle powBundle; |
---|
48 | private ResourceBundle timeBundle; |
---|
49 | |
---|
50 | // virtual nodes are to keep track of the intermediate power and temperature between job assignments |
---|
51 | private List<VirtualNode> virtualNodes = new ArrayList<VirtualNode>(); |
---|
52 | private boolean firstCall = true; |
---|
53 | |
---|
54 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
55 | ResourceManager resManager, ModuleList modules) { |
---|
56 | |
---|
57 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
58 | SchedulingPlan plan = new SchedulingPlan(); |
---|
59 | |
---|
60 | List<Node> nodes = resourceManager.getNodes(); |
---|
61 | |
---|
62 | // create the virtualNodes on the first scheduling call |
---|
63 | if (firstCall){ |
---|
64 | |
---|
65 | VirtualNode dummyVN = new VirtualNode(0); |
---|
66 | virtualNodes.add(dummyVN); |
---|
67 | |
---|
68 | int numNodes = nodes.size(); |
---|
69 | for (int i = 1; i <= numNodes; i++){ |
---|
70 | VirtualNode vn = new VirtualNode(i); |
---|
71 | virtualNodes.add(vn); |
---|
72 | } |
---|
73 | firstCall = false; |
---|
74 | } |
---|
75 | |
---|
76 | // update the power of the virtual nodes |
---|
77 | for (Node node : nodes){ |
---|
78 | double power = node.getPowerInterface().getRecentPowerUsage().getValue(); |
---|
79 | int nodeId = Integer.parseInt(node.getName().split("_")[1]); |
---|
80 | virtualNodes.get(nodeId).setPower(power); |
---|
81 | } |
---|
82 | |
---|
83 | // update the temperature of the virtual nodes |
---|
84 | for (int i = 1; i <= 9; i++){ |
---|
85 | VirtualNode vn1 = virtualNodes.get(i+9); |
---|
86 | VirtualNode vn2 = virtualNodes.get(i); |
---|
87 | |
---|
88 | double power1 = vn1.getPower(); |
---|
89 | double power2 = vn2.getPower(); |
---|
90 | |
---|
91 | double tout = ThermalConstants.calculateOutletTemp(power1, power2); |
---|
92 | vn1.setTemperature(tout); |
---|
93 | vn2.setTemperature(tout); |
---|
94 | } |
---|
95 | |
---|
96 | // choose the events types to serve. |
---|
97 | // Different actions for different events are possible. |
---|
98 | switch (event.getType()) { |
---|
99 | case START_TASK_EXECUTION: |
---|
100 | case TASK_FINISHED: |
---|
101 | //case TIMER: |
---|
102 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
103 | TaskQueue q = queues.get(0); |
---|
104 | // check all tasks in queue |
---|
105 | |
---|
106 | for (int i = 0; i < q.size(); i++) { |
---|
107 | TaskInterface<?> task = q.get(i); |
---|
108 | initApplicationType(task); |
---|
109 | |
---|
110 | // if status of the tasks in READY |
---|
111 | if (task.getStatus() == DCWormsTags.READY) { |
---|
112 | //System.out.println("Task " + task.getJobId()); |
---|
113 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
114 | if (choosenResources != null) { |
---|
115 | addToSchedulingPlan(plan, task, choosenResources); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | break; |
---|
121 | } |
---|
122 | return plan; |
---|
123 | } |
---|
124 | |
---|
125 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
126 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
127 | |
---|
128 | Map<ResourceUnitName, ResourceUnit> map; |
---|
129 | List<Node> nodes = resourceManager.getNodes(); |
---|
130 | Collections.shuffle(nodes); |
---|
131 | Collections.sort(nodes, new TemperatureComparator(task)); |
---|
132 | /* |
---|
133 | System.out.println("*************************"); |
---|
134 | System.out.println("Assigning task " + task.getJobId()); |
---|
135 | for (Node node : nodes) { |
---|
136 | int nodeId = Integer.parseInt(node.getName().split("_")[1]); |
---|
137 | double node_power = node.getPowerInterface().getRecentPowerUsage().getValue(); |
---|
138 | double node_temp = node.getThermalInterface().getRecentTemperature().getValue(); |
---|
139 | double vn_power = virtualNodes.get(nodeId).getPower(); |
---|
140 | double vn_temp = virtualNodes.get(nodeId).getTemperature(); |
---|
141 | System.out.println(node.getName() + ": node_power = " + node_power + " ; node_temp = " + node_temp + " ; vn_power = " + vn_power + " ; vn_temp = " + vn_temp); |
---|
142 | } |
---|
143 | System.out.println("*************************"); |
---|
144 | */ |
---|
145 | for (Node node : nodes) { |
---|
146 | int cpuRequest; |
---|
147 | try { |
---|
148 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
149 | } catch (NoSuchFieldException e) { |
---|
150 | cpuRequest = 0; |
---|
151 | } |
---|
152 | |
---|
153 | if (cpuRequest != 0) { |
---|
154 | |
---|
155 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
156 | if (cores.size() < cpuRequest/* || node.getProcessors().get(0).filterDescendants(properties).size() != cores.size()*/) { |
---|
157 | continue; |
---|
158 | } |
---|
159 | |
---|
160 | int freeCores = 0; |
---|
161 | for(Core core: cores){ |
---|
162 | if(core.getStatus() == ResourceStatus.FREE) |
---|
163 | freeCores++; |
---|
164 | } |
---|
165 | |
---|
166 | if(freeCores != cores.size()) |
---|
167 | continue; |
---|
168 | |
---|
169 | try { |
---|
170 | if(!getExecutiveness(createExecutivenessQuery(task, node))) |
---|
171 | continue; |
---|
172 | } catch (FileNotFoundException e) { |
---|
173 | continue; |
---|
174 | } catch (IOException e) { |
---|
175 | continue; |
---|
176 | } catch (MissingResourceException e){ |
---|
177 | continue; |
---|
178 | } |
---|
179 | |
---|
180 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
181 | for (int i = 0; i < cores.size(); i++) { |
---|
182 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
183 | //choosenResources.add(cores.get(i)); |
---|
184 | cpuRequest--; |
---|
185 | } |
---|
186 | } |
---|
187 | choosenResources.add(node); |
---|
188 | if (cpuRequest > 0) { |
---|
189 | //continue; |
---|
190 | } |
---|
191 | map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
192 | ProcessingElements pe = new ProcessingElements(); |
---|
193 | pe.addAll(choosenResources); |
---|
194 | map.put(StandardResourceUnitName.PE, pe); |
---|
195 | |
---|
196 | // update the power and temperature of the virtualNode corresponding to the node |
---|
197 | int nodeId = Integer.parseInt(node.getName().split("_")[1]); |
---|
198 | VirtualNode vn = virtualNodes.get(nodeId); |
---|
199 | |
---|
200 | // power update |
---|
201 | double extraPower = 0; |
---|
202 | try{ |
---|
203 | extraPower = Math.abs(getMeasuredPower(createQuery(task, node)) - node.getPowerInterface().getPowerConsumption(StandardPowerStateName.ON)); |
---|
204 | } catch (FileNotFoundException e) { |
---|
205 | |
---|
206 | } catch (IOException e) { |
---|
207 | |
---|
208 | } catch (MissingResourceException e){ |
---|
209 | |
---|
210 | } catch (NoSuchFieldException e) { |
---|
211 | |
---|
212 | } |
---|
213 | vn.setPower(vn.getPower() + extraPower); |
---|
214 | |
---|
215 | // temperature update |
---|
216 | double delta = 1; |
---|
217 | VirtualNode vn2; |
---|
218 | if (nodeId >= 1 && nodeId <= 9){ |
---|
219 | delta = ThermalConstants.delta2; |
---|
220 | vn2 = virtualNodes.get(nodeId + 9); |
---|
221 | } |
---|
222 | else{ |
---|
223 | delta = ThermalConstants.delta1; |
---|
224 | vn2 = virtualNodes.get(nodeId - 9); |
---|
225 | } |
---|
226 | double extraTemp = delta * (extraPower/(ThermalConstants.Q * ThermalConstants.C * ThermalConstants.ro)); |
---|
227 | vn.setTemperature(vn.getTemperature() + extraTemp); |
---|
228 | vn2.setTemperature(vn2.getTemperature() + extraTemp); |
---|
229 | |
---|
230 | return map; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | return null; |
---|
235 | } |
---|
236 | |
---|
237 | protected String createQuery(TaskInterface<?> task, Node node) { |
---|
238 | String query; |
---|
239 | query = getApplicationType(task) + "." + getNodeCategory(node) + "." + getFrequency(node) + "." + getCoreCnt(task); |
---|
240 | return query; |
---|
241 | } |
---|
242 | |
---|
243 | protected double getMeasuredPower(String query) throws FileNotFoundException, IOException{ |
---|
244 | ResourceBundle powBundle = getPowBundle(); |
---|
245 | return Double.valueOf(powBundle.getString(query)).doubleValue(); |
---|
246 | } |
---|
247 | |
---|
248 | private ResourceBundle getPowBundle() throws FileNotFoundException, IOException{ |
---|
249 | if(powBundle == null){ |
---|
250 | powBundle = new PropertyResourceBundle(new FileInputStream(POWER_DATA_FILE_NAME)); |
---|
251 | } |
---|
252 | return powBundle; |
---|
253 | } |
---|
254 | |
---|
255 | protected double getMeasuredTime(String query) throws FileNotFoundException, IOException{ |
---|
256 | ResourceBundle timeBundle = getTimeBundle(); |
---|
257 | return Double.valueOf(timeBundle.getString(query)).doubleValue(); |
---|
258 | } |
---|
259 | |
---|
260 | private ResourceBundle getTimeBundle() throws FileNotFoundException, IOException{ |
---|
261 | if(timeBundle == null){ |
---|
262 | timeBundle = new PropertyResourceBundle(new FileInputStream(TIME_DATA_FILE_NAME)); |
---|
263 | } |
---|
264 | return timeBundle; |
---|
265 | } |
---|
266 | |
---|
267 | class TemperatureComparator implements Comparator<Node>{ |
---|
268 | private TaskInterface<?> task; |
---|
269 | |
---|
270 | public TemperatureComparator(TaskInterface<?> task){ |
---|
271 | this.task = task; |
---|
272 | } |
---|
273 | |
---|
274 | public int compare(Node node1, Node node2){ |
---|
275 | |
---|
276 | int id1 = Integer.parseInt(node1.getName().split("_")[1]); |
---|
277 | int id2 = Integer.parseInt(node2.getName().split("_")[1]); |
---|
278 | |
---|
279 | double node1Temp = virtualNodes.get(id1).getTemperature(); |
---|
280 | double node2Temp = virtualNodes.get(id2).getTemperature(); |
---|
281 | |
---|
282 | if (node1Temp > node2Temp) |
---|
283 | return 1; |
---|
284 | else if (node1Temp < node2Temp) |
---|
285 | return -1; |
---|
286 | else |
---|
287 | return 0; |
---|
288 | |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|