1 | package schedframe.resources.computing; |
---|
2 | |
---|
3 | import gridsim.GridSimTags; |
---|
4 | import gridsim.dcworms.DCWormsTags; |
---|
5 | |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.LinkedList; |
---|
8 | import java.util.List; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import schedframe.events.EventHandler; |
---|
12 | import schedframe.events.EventReason; |
---|
13 | import schedframe.events.ResourceEventCommand; |
---|
14 | import schedframe.events.scheduling.ResourceStateChangedEvent; |
---|
15 | import schedframe.events.scheduling.SchedulingEvent; |
---|
16 | import schedframe.events.scheduling.SchedulingEventCommand; |
---|
17 | import schedframe.resources.ResourceStatus; |
---|
18 | import schedframe.resources.ResourceType; |
---|
19 | import schedframe.resources.computing.description.ComputingResourceDescription; |
---|
20 | import schedframe.resources.computing.extensions.Extension; |
---|
21 | import schedframe.resources.computing.extensions.ExtensionListImpl; |
---|
22 | import schedframe.resources.computing.profiles.energy.ResourceEvent; |
---|
23 | import schedframe.resources.computing.profiles.energy.ResourceEventType; |
---|
24 | import schedframe.resources.computing.profiles.energy.EnergyExtension; |
---|
25 | import schedframe.resources.computing.profiles.load.LoadExtension; |
---|
26 | import schedframe.resources.computing.profiles.load.ResourceLoadCalendar; |
---|
27 | import schedframe.resources.computing.profiles.load.TimestampUtilization; |
---|
28 | import schedframe.resources.computing.profiles.load.ui.LoadInterface; |
---|
29 | import schedframe.resources.computing.properties.DefaultPropertiesBuilder; |
---|
30 | import schedframe.resources.computing.properties.PropertiesDirector; |
---|
31 | import schedframe.resources.computing.validator.ResourceNameValidator; |
---|
32 | import schedframe.resources.computing.validator.ResourcePropertiesValidator; |
---|
33 | import schedframe.resources.computing.validator.ResourceStatusValidator; |
---|
34 | import schedframe.resources.computing.validator.ResourceTypeValidator; |
---|
35 | import schedframe.resources.computing.validator.ResourceValidator; |
---|
36 | import schedframe.resources.devices.Device; |
---|
37 | import schedframe.resources.devices.PhysicalResource; |
---|
38 | import schedframe.scheduling.Scheduler; |
---|
39 | import simulator.DataCenterWorkloadSimulator; |
---|
40 | |
---|
41 | public class ComputingResource extends PhysicalResource{ |
---|
42 | |
---|
43 | protected ComputingResource parent; |
---|
44 | protected List<ComputingResource> children; |
---|
45 | |
---|
46 | protected Scheduler scheduler; |
---|
47 | //protected ResourceCharacteristics resourceCharacteristic; |
---|
48 | |
---|
49 | |
---|
50 | public ComputingResource(ComputingResourceDescription resDesc) { |
---|
51 | this.type = resDesc.getType(); |
---|
52 | this.name = resDesc.getId(); |
---|
53 | this.category = resDesc.getCategory(); |
---|
54 | this.status = ResourceStatus.FREE; |
---|
55 | this.extensionList = new ExtensionListImpl(2); |
---|
56 | initCharacteristics(resDesc); |
---|
57 | accept(new LoadExtension(resDesc.getLoadProfile(), this)); |
---|
58 | accept(new EnergyExtension.Builder().resource(this).powerProfile(resDesc.getPowerProfile()).airflowProfile(resDesc.getAirflowProfile()).thermalProfile(resDesc.getThermalProfile()).build()); |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | protected void initCharacteristics(ComputingResourceDescription resDesc){ |
---|
63 | this.resourceCharacteristic = ComputingResourceCharacteristics.builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).device(resDesc.getDevices()).build(); |
---|
64 | for(Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()){ |
---|
65 | device.setComputingResource(this); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | public ComputingResource getParent() { |
---|
70 | return parent; |
---|
71 | } |
---|
72 | |
---|
73 | public void setParent(ComputingResource newParent) { |
---|
74 | this.parent = newParent; |
---|
75 | /*if(this.getLoadInterface().getLoadCalendar().getLoadDistribution().size() == 0){ |
---|
76 | this.getLoadInterface().getLoadCalendar().getLoadDistribution().addAll(parent.getLoadInterface().getLoadCalendar().getLoadDistribution()); |
---|
77 | }*/ |
---|
78 | } |
---|
79 | |
---|
80 | public List<ComputingResource> getChildren() { |
---|
81 | if (children == null) |
---|
82 | return new ArrayList<ComputingResource>(0); |
---|
83 | return children; |
---|
84 | } |
---|
85 | |
---|
86 | public void addChild(ComputingResource child) { |
---|
87 | child.setParent(this); |
---|
88 | if (children == null) |
---|
89 | children = new ArrayList<ComputingResource>(); |
---|
90 | children.add(child); |
---|
91 | } |
---|
92 | |
---|
93 | public String getFullName() { |
---|
94 | if(this.getResourceCharacteristic().getParameters().get("fullPath") != null){ |
---|
95 | String fullPath = this.getResourceCharacteristic().getParameters().get("fullPath").get(0).getContent(); |
---|
96 | return fullPath; |
---|
97 | } |
---|
98 | if(parent!= null){ |
---|
99 | return parent.getFullName() + "/" + name; |
---|
100 | } else |
---|
101 | return name; |
---|
102 | } |
---|
103 | |
---|
104 | public ComputingResourceCharacteristics getResourceCharacteristic() { |
---|
105 | return (ComputingResourceCharacteristics)resourceCharacteristic; |
---|
106 | } |
---|
107 | |
---|
108 | public void setStatus(ResourceStatus newStatus) { |
---|
109 | if(newStatus != status){ |
---|
110 | status = newStatus; |
---|
111 | if(children != null) { |
---|
112 | for(ComputingResource child: children){ |
---|
113 | child.setStatus(status); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | private void triggerEventUp(ResourceEvent event) { |
---|
120 | if(parent != null) |
---|
121 | parent.handleEvent(event); |
---|
122 | } |
---|
123 | |
---|
124 | public void handleEvent(ResourceEvent event){ |
---|
125 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
126 | rec.execute(event); |
---|
127 | if(event.getReason() != EventReason.SIM_INIT) |
---|
128 | triggerEventUp(event); |
---|
129 | if((scheduler != null && (parent != null && scheduler != parent.getScheduler())) && (event.getSource() != null && !event.getSource().equals(scheduler.getFullName()))){ |
---|
130 | SchedulingEventCommand sec = new SchedulingEventCommand(this); |
---|
131 | sec.execute(event); |
---|
132 | } |
---|
133 | //old, correctly working method |
---|
134 | /*if (extensionList != null) { |
---|
135 | for (Extension extension : extensionList) { |
---|
136 | if (extension.supportsEvent(event)) { |
---|
137 | extension.handleEvent(event); |
---|
138 | } |
---|
139 | } |
---|
140 | }*/ |
---|
141 | //TODO - delete, check in advance |
---|
142 | //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
143 | // String src = event.getSource() != null ? event.getSource() : name; |
---|
144 | // scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); |
---|
145 | //} |
---|
146 | //triggerEventUp(event); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | public void updateState(ResourceEvent event){ |
---|
151 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
152 | rec.execute(event); |
---|
153 | /*for (Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()) { |
---|
154 | for (Extension extension: device.getExtensionList()) { |
---|
155 | if (extension.supportsEvent(event)) { |
---|
156 | extension.handleEvent(event); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | if (extensionList != null) { |
---|
162 | for (Extension extension: extensionList) { |
---|
163 | if (extension.supportsEvent(event)) { |
---|
164 | extension.handleEvent(event); |
---|
165 | } |
---|
166 | } |
---|
167 | }*/ |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | public List <? extends ComputingResource> getDescendantsByType(ResourceType type) { |
---|
172 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(1); |
---|
173 | validators.add(new ResourceTypeValidator(type)); |
---|
174 | return searchDescendants(validators, true); |
---|
175 | } |
---|
176 | |
---|
177 | public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) { |
---|
178 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(2); |
---|
179 | validators.add(new ResourceStatusValidator(status)); |
---|
180 | validators.add(new ResourceTypeValidator(type)); |
---|
181 | return searchDescendants(validators, true); |
---|
182 | } |
---|
183 | |
---|
184 | public ComputingResource getDescendantByName(String resourceName){ |
---|
185 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(1); |
---|
186 | validators.add(new ResourceNameValidator(resourceName)); |
---|
187 | List<? extends ComputingResource> resources = searchDescendants(validators, true); |
---|
188 | return resources.size() == 0 ? null : resources.get(0); |
---|
189 | } |
---|
190 | |
---|
191 | public List<? extends ComputingResource> filterDescendants(Properties properties) { |
---|
192 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
193 | validators.add(new ResourcePropertiesValidator(properties)); |
---|
194 | return searchDescendants(validators, false); |
---|
195 | } |
---|
196 | |
---|
197 | protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutOff) { |
---|
198 | |
---|
199 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
200 | if (children != null) { |
---|
201 | LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>(); |
---|
202 | toExamine.push(this); |
---|
203 | |
---|
204 | while (!toExamine.isEmpty()) { |
---|
205 | ComputingResource resource = toExamine.pop(); |
---|
206 | List<ComputingResource> resources = resource.getChildren(); |
---|
207 | int numberOfRes = resources.size(); |
---|
208 | for (int i = 0; i < numberOfRes; i++) { |
---|
209 | ComputingResource resourceChild = resources.get(i); |
---|
210 | if (resourceChild.match(validators)) { |
---|
211 | descendants.add(resourceChild); |
---|
212 | if(cutOff == false) { |
---|
213 | toExamine.addLast(resourceChild); |
---|
214 | } |
---|
215 | } else |
---|
216 | //toExamine.insertElementAt(resourceChild, 0); |
---|
217 | toExamine.addLast(resourceChild); |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | return descendants; |
---|
222 | } |
---|
223 | |
---|
224 | protected boolean match(List<ResourceValidator> validators){ |
---|
225 | for(ResourceValidator validator: validators){ |
---|
226 | if(validator.validate(this) == false) |
---|
227 | return false; |
---|
228 | } |
---|
229 | return true; |
---|
230 | } |
---|
231 | |
---|
232 | public Properties getProperties(){ |
---|
233 | PropertiesDirector propDirector = new PropertiesDirector(); |
---|
234 | propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder()); |
---|
235 | propDirector.constructProperties(this); |
---|
236 | return propDirector.getProperties(); |
---|
237 | } |
---|
238 | |
---|
239 | public Scheduler getScheduler() { |
---|
240 | return scheduler; |
---|
241 | } |
---|
242 | |
---|
243 | public void setScheduler(Scheduler scheduler) { |
---|
244 | this.scheduler = scheduler; |
---|
245 | if(children != null){ |
---|
246 | for(ComputingResource child: children){ |
---|
247 | child.setScheduler(scheduler); |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | class ComputingResourceEventHandler implements EventHandler{ |
---|
253 | |
---|
254 | public void handleResourceEvent(ResourceEvent event){ |
---|
255 | for (Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()) { |
---|
256 | for (Extension extension: device.getExtensionList()) { |
---|
257 | if (extension.supportsEvent(event)) { |
---|
258 | extension.handleEvent(event); |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | if (extensionList != null) { |
---|
264 | for (Extension extension: extensionList) { |
---|
265 | if (extension.supportsEvent(event)) { |
---|
266 | extension.handleEvent(event); |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | public void handleSchedulingEvent(SchedulingEvent event){ |
---|
273 | |
---|
274 | if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
275 | String src = event.getSource() != null ? event.getSource() : name; |
---|
276 | //ResourceEventFilter filter = new ResourceEventFilter(src.hashCode(), DCWormsTags.UPDATE_PROCESSING); |
---|
277 | //scheduler.sim_cancel(filter, null); |
---|
278 | ResourceStateChangedEvent rscEvent = (ResourceStateChangedEvent) event; |
---|
279 | ResourceEventType reType = rscEvent.getResourceEventType(); |
---|
280 | //scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
281 | switch(reType){ |
---|
282 | |
---|
283 | case UTILIZATION_CHANGED: { |
---|
284 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
285 | break; |
---|
286 | } |
---|
287 | case CPU_FREQUENCY_CHANGED: { |
---|
288 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
289 | break; |
---|
290 | } |
---|
291 | case POWER_STATE_CHANGED: { |
---|
292 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.RESOURCE_POWER_STATE_CHANGED, src); |
---|
293 | break; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | } else if(parent != null) |
---|
298 | parent.getEventHandler().handleSchedulingEvent(event); |
---|
299 | |
---|
300 | //TODO - check if needed |
---|
301 | //if(event.getReason() != EventReason.SIM_INIT) |
---|
302 | // triggerEventUp(event); |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | public EventHandler getEventHandler(){ |
---|
307 | return new ComputingResourceEventHandler(); |
---|
308 | } |
---|
309 | |
---|
310 | public final void initiate(){ |
---|
311 | for(Device dev: this.getResourceCharacteristic().getDevices()){ |
---|
312 | dev.initiate(); |
---|
313 | } |
---|
314 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
315 | ResourceEvent event = new ResourceEvent(ResourceEventType.AIRFLOW_STATE_CHANGED, "Resource controller"); |
---|
316 | event.setReason(EventReason.SIM_INIT); |
---|
317 | rec.execute(event); |
---|
318 | /*ResourceEventCommand*/ rec = new ResourceEventCommand(this); |
---|
319 | /*ResourceEvent*/ event = new ResourceEvent(ResourceEventType.POWER_STATE_CHANGED, "Resource controller"); |
---|
320 | event.setReason(EventReason.SIM_INIT); |
---|
321 | rec.execute(event); |
---|
322 | |
---|
323 | rec = new ResourceEventCommand(this); |
---|
324 | event = new ResourceEvent(ResourceEventType.TEMPERATURE_CHANGED, "Resource controller"); |
---|
325 | event.setReason(EventReason.SIM_INIT); |
---|
326 | rec.execute(event); |
---|
327 | |
---|
328 | LoadInterface li = getLoadInterface(); |
---|
329 | if(li != null){ |
---|
330 | ResourceLoadCalendar rlc = li.getLoadCalendar(); |
---|
331 | LinkedList<TimestampUtilization> ld = rlc.getLoadDistribution(); |
---|
332 | for(TimestampUtilization tu: ld){ |
---|
333 | DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getStartTime(), new ResourceEvent(ResourceEventType.UTILIZATION_CHANGED, getFullName())); |
---|
334 | DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getEndTime(), new ResourceEvent(ResourceEventType.UTILIZATION_CHANGED, getFullName())); |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | /*EnergyExtension ee = (EnergyExtension) extensionList.getExtension(ExtensionType.ENERGY_EXTENSION); |
---|
339 | if(ee != null && ee.getPowerProfile() != null){ |
---|
340 | PluginConfiguration pluginConfig = ee.getPowerProfile().getEnergyEstimationPlugin().getConfiguration(); |
---|
341 | if (pluginConfig != null) { |
---|
342 | Map<EventType, Object> events = pluginConfig.getServedEvents(); |
---|
343 | if (events != null) { |
---|
344 | Object obj = events.get(ResourceEventType.TIMER); |
---|
345 | if (obj != null) { |
---|
346 | int delay = (Integer) obj; |
---|
347 | DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), delay, new ResourceEvent(ResourceEventType.TIMER, null)); |
---|
348 | } |
---|
349 | } |
---|
350 | } |
---|
351 | }*/ |
---|
352 | |
---|
353 | |
---|
354 | //alternative way |
---|
355 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller")); |
---|
356 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller")); |
---|
357 | } |
---|
358 | |
---|
359 | public boolean contains(ComputingResource compRes){ |
---|
360 | if(compRes.equals(this)){ |
---|
361 | return true; |
---|
362 | } else { |
---|
363 | ComputingResource par = compRes.getParent(); |
---|
364 | while(par != null){ |
---|
365 | if (par.equals(this)){ |
---|
366 | return true; |
---|
367 | } |
---|
368 | par = par.getParent(); |
---|
369 | } |
---|
370 | } |
---|
371 | return false; |
---|
372 | } |
---|
373 | } |
---|