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.Initializable; |
---|
12 | import schedframe.events.Event; |
---|
13 | import schedframe.events.EventHandler; |
---|
14 | import schedframe.events.ResourceEventCommand; |
---|
15 | import schedframe.events.scheduling.EventReason; |
---|
16 | import schedframe.events.scheduling.SchedulingEvent; |
---|
17 | import schedframe.events.scheduling.SchedulingEventCommand; |
---|
18 | import schedframe.exceptions.ResourceException; |
---|
19 | import schedframe.resources.Resource; |
---|
20 | import schedframe.resources.ResourceStatus; |
---|
21 | import schedframe.resources.ResourceType; |
---|
22 | import schedframe.resources.computing.description.ComputingResourceDescription; |
---|
23 | import schedframe.resources.computing.extensions.Extension; |
---|
24 | import schedframe.resources.computing.extensions.ExtensionList; |
---|
25 | import schedframe.resources.computing.extensions.ExtensionListImpl; |
---|
26 | import schedframe.resources.computing.extensions.ExtensionType; |
---|
27 | import schedframe.resources.computing.profiles.energy.EnergyEvent; |
---|
28 | import schedframe.resources.computing.profiles.energy.EnergyEventType; |
---|
29 | import schedframe.resources.computing.profiles.energy.EnergyExtension; |
---|
30 | import schedframe.resources.computing.profiles.energy.airthroughput.ui.AirThroughputInterface; |
---|
31 | import schedframe.resources.computing.profiles.energy.power.ui.PowerInterface; |
---|
32 | import schedframe.resources.computing.profiles.energy.thermal.ui.ThermalInterface; |
---|
33 | import schedframe.resources.computing.properties.DefaultPropertiesBuilder; |
---|
34 | import schedframe.resources.computing.properties.PropertiesDirector; |
---|
35 | import schedframe.resources.computing.validator.ResourceNameValidator; |
---|
36 | import schedframe.resources.computing.validator.ResourcePropertiesValidator; |
---|
37 | import schedframe.resources.computing.validator.ResourceStatusValidator; |
---|
38 | import schedframe.resources.computing.validator.ResourceTypeValidator; |
---|
39 | import schedframe.resources.computing.validator.ResourceValidator; |
---|
40 | import schedframe.resources.units.PEUnit; |
---|
41 | import schedframe.resources.units.ResourceUnit; |
---|
42 | import schedframe.resources.units.StandardResourceUnitName; |
---|
43 | import schedframe.scheduling.Scheduler; |
---|
44 | |
---|
45 | public class ComputingResource implements Resource, Initializable{ |
---|
46 | |
---|
47 | protected String name; |
---|
48 | protected ResourceType type; |
---|
49 | protected String category; |
---|
50 | protected ResourceStatus status; |
---|
51 | |
---|
52 | protected ComputingResource parent; |
---|
53 | protected List<ComputingResource> children; |
---|
54 | |
---|
55 | protected Scheduler scheduler; |
---|
56 | protected ResourceCharacteristics resourceCharacteristic; |
---|
57 | protected ExtensionList extensionList; |
---|
58 | |
---|
59 | |
---|
60 | public ExtensionList getExtensionList() { |
---|
61 | return extensionList; |
---|
62 | } |
---|
63 | |
---|
64 | public ComputingResource(ComputingResourceDescription resDesc) { |
---|
65 | this.type = resDesc.getType(); |
---|
66 | this.name = resDesc.getId(); |
---|
67 | this.category = resDesc.getCategory(); |
---|
68 | this.status = ResourceStatus.FREE; |
---|
69 | this.extensionList = new ExtensionListImpl(1); |
---|
70 | initCharacteristics(resDesc); |
---|
71 | accept(new EnergyExtension(this, resDesc.getPowerProfile(), resDesc.getAirThroughputProfile(), resDesc.getThermalProfile())); |
---|
72 | addFakeProcessors(); |
---|
73 | } |
---|
74 | |
---|
75 | //TODO remove if possible (check if all scenarios can be realized - statistics issue), since it's a temporary method |
---|
76 | private void addFakeProcessors() { |
---|
77 | if(getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE) != null){ |
---|
78 | for(ResourceUnit resUnit: getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE)){ |
---|
79 | PEUnit peUnit = (PEUnit) resUnit; |
---|
80 | for(int i = 0; i < peUnit.getAmount(); i++){ |
---|
81 | schemas.ComputingResource fakeCompResource = new schemas.ComputingResource(); |
---|
82 | fakeCompResource.setClazz("Processor"); |
---|
83 | addChild(ResourceFactory.createResource(new ComputingResourceDescription(fakeCompResource))); |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | protected void initCharacteristics(ComputingResourceDescription resDesc){ |
---|
90 | resourceCharacteristic = new ResourceCharacteristics.Builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).build(); |
---|
91 | } |
---|
92 | |
---|
93 | public ComputingResource getParent() { |
---|
94 | return parent; |
---|
95 | } |
---|
96 | |
---|
97 | public void setParent(ComputingResource newParent) { |
---|
98 | parent = newParent; |
---|
99 | } |
---|
100 | |
---|
101 | public List<ComputingResource> getChildren() { |
---|
102 | if (children == null) |
---|
103 | return new ArrayList<ComputingResource>(1); |
---|
104 | return children; |
---|
105 | } |
---|
106 | |
---|
107 | public void addChild(ComputingResource child) { |
---|
108 | child.setParent(this); |
---|
109 | if (children == null) |
---|
110 | children = new ArrayList<ComputingResource>(); |
---|
111 | children.add(child); |
---|
112 | } |
---|
113 | |
---|
114 | public String getName() { |
---|
115 | return name; |
---|
116 | } |
---|
117 | |
---|
118 | public ResourceType getType() { |
---|
119 | return type; |
---|
120 | } |
---|
121 | |
---|
122 | public ResourceCharacteristics getResourceCharacteristic() { |
---|
123 | return resourceCharacteristic; |
---|
124 | } |
---|
125 | |
---|
126 | public ResourceStatus getStatus() { |
---|
127 | return status; |
---|
128 | } |
---|
129 | |
---|
130 | public void setStatus(ResourceStatus newStatus) { |
---|
131 | if(newStatus != status){ |
---|
132 | status = newStatus; |
---|
133 | if(children != null) { |
---|
134 | for(ComputingResource child: children){ |
---|
135 | child.setStatus(status); |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | private void triggerEventUp(Event event) { |
---|
142 | if(parent != null) |
---|
143 | parent.handleEvent(event); |
---|
144 | } |
---|
145 | |
---|
146 | public void handleEvent(Event event){ |
---|
147 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
148 | rec.execute(event); |
---|
149 | SchedulingEventCommand sec = new SchedulingEventCommand(this); |
---|
150 | sec.execute(event); |
---|
151 | |
---|
152 | //old, correctly working method |
---|
153 | /*if (extensionList != null) { |
---|
154 | for (Extension extension : extensionList) { |
---|
155 | if (extension.supportsEvent(event)) { |
---|
156 | extension.handleEvent(event); |
---|
157 | } |
---|
158 | } |
---|
159 | }*/ |
---|
160 | //TODO - delete, check in advance |
---|
161 | //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
162 | // String src = event.getSource() != null ? event.getSource() : name; |
---|
163 | // scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); |
---|
164 | //} |
---|
165 | //triggerEventUp(event); |
---|
166 | } |
---|
167 | |
---|
168 | public List <? extends ComputingResource> getDescendantsByType(ResourceType type) { |
---|
169 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
170 | validators.add(new ResourceTypeValidator(type)); |
---|
171 | return searchDescendants(validators, true); |
---|
172 | } |
---|
173 | |
---|
174 | public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) { |
---|
175 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
176 | validators.add(new ResourceStatusValidator(status)); |
---|
177 | validators.add(new ResourceTypeValidator(type)); |
---|
178 | return searchDescendants(validators, true); |
---|
179 | } |
---|
180 | |
---|
181 | public ComputingResource getDescendantByName(String resourceName) throws ResourceException { |
---|
182 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
183 | validators.add(new ResourceNameValidator(resourceName)); |
---|
184 | List<? extends ComputingResource> resources = searchDescendants(validators, true); |
---|
185 | return resources.size() == 0 ? null : resources.get(0); |
---|
186 | } |
---|
187 | |
---|
188 | public List<? extends ComputingResource> filterDescendants(Properties properties) { |
---|
189 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
190 | validators.add(new ResourcePropertiesValidator(properties)); |
---|
191 | return searchDescendants(validators, false); |
---|
192 | } |
---|
193 | |
---|
194 | protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutOff) { |
---|
195 | |
---|
196 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
197 | if (children != null) { |
---|
198 | LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>(); |
---|
199 | toExamine.push(this); |
---|
200 | |
---|
201 | while (!toExamine.isEmpty()) { |
---|
202 | ComputingResource resource = toExamine.pop(); |
---|
203 | List<ComputingResource> resources = resource.getChildren(); |
---|
204 | int numberOfRes = resources.size(); |
---|
205 | for (int i = 0; i < numberOfRes; i++) { |
---|
206 | ComputingResource resourceChild = resources.get(i); |
---|
207 | if (resourceChild.match(validators)) { |
---|
208 | descendants.add(resourceChild); |
---|
209 | if(cutOff == false) { |
---|
210 | toExamine.addLast(resourceChild); |
---|
211 | } |
---|
212 | } else |
---|
213 | //toExamine.insertElementAt(resourceChild, 0); |
---|
214 | toExamine.addLast(resourceChild); |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | return descendants; |
---|
219 | } |
---|
220 | |
---|
221 | protected boolean match(List<ResourceValidator> validators){ |
---|
222 | for(ResourceValidator validator: validators){ |
---|
223 | if(validator.validate(this) == false) |
---|
224 | return false; |
---|
225 | } |
---|
226 | return true; |
---|
227 | } |
---|
228 | |
---|
229 | public Properties getProperties(){ |
---|
230 | PropertiesDirector propDirector = new PropertiesDirector(); |
---|
231 | propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder()); |
---|
232 | propDirector.constructProperties(this); |
---|
233 | return propDirector.getProperties(); |
---|
234 | } |
---|
235 | |
---|
236 | public String getCategory(){ |
---|
237 | return category; |
---|
238 | } |
---|
239 | |
---|
240 | public PowerInterface getPowerInterface(){ |
---|
241 | Extension extension = getExtension(ExtensionType.ENERGY_EXTENSION); |
---|
242 | if(extension != null){ |
---|
243 | EnergyExtension ee = (EnergyExtension)extension; |
---|
244 | return ee.getPowerInterface(); |
---|
245 | } |
---|
246 | return null; |
---|
247 | } |
---|
248 | |
---|
249 | public AirThroughputInterface getAirThroughputInterface(){ |
---|
250 | Extension extension = getExtension(ExtensionType.ENERGY_EXTENSION); |
---|
251 | if(extension != null){ |
---|
252 | EnergyExtension ee = (EnergyExtension)extension; |
---|
253 | return ee.getAirThroughputInterface(); |
---|
254 | } |
---|
255 | return null; |
---|
256 | } |
---|
257 | |
---|
258 | public ThermalInterface getThermalInterface(){ |
---|
259 | Extension extension = getExtension(ExtensionType.ENERGY_EXTENSION); |
---|
260 | if(extension != null){ |
---|
261 | EnergyExtension ee = (EnergyExtension)extension; |
---|
262 | return ee.getThermalInterface(); |
---|
263 | } |
---|
264 | return null; |
---|
265 | } |
---|
266 | |
---|
267 | private Extension getExtension(ExtensionType type){ |
---|
268 | if (extensionList != null) { |
---|
269 | for (Extension extension : extensionList) { |
---|
270 | if (extension.getType() == type) { |
---|
271 | return extension; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | return null; |
---|
276 | } |
---|
277 | |
---|
278 | public Scheduler getScheduler() { |
---|
279 | return scheduler; |
---|
280 | } |
---|
281 | |
---|
282 | public void setScheduler(Scheduler scheduler) { |
---|
283 | this.scheduler = scheduler; |
---|
284 | if(children != null){ |
---|
285 | for(ComputingResource child: children){ |
---|
286 | child.setScheduler(scheduler); |
---|
287 | } |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | class ComputingResourceEventHandler implements EventHandler{ |
---|
292 | |
---|
293 | public void handleResourceEvent(Event event){ |
---|
294 | if (extensionList != null) { |
---|
295 | for (Extension extension : extensionList) { |
---|
296 | if (extension.supportsEvent(event)) { |
---|
297 | extension.handleEvent(event); |
---|
298 | } |
---|
299 | } |
---|
300 | } |
---|
301 | if(event.getReason() != EventReason.SIM_INIT) |
---|
302 | triggerEventUp(event); |
---|
303 | } |
---|
304 | |
---|
305 | public void handleSchedulingEvent(SchedulingEvent event){ |
---|
306 | |
---|
307 | if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
308 | String src = event.getSource() != null ? event.getSource() : name; |
---|
309 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
310 | } else if(parent != null) |
---|
311 | parent.getEventHandler().handleSchedulingEvent(event); |
---|
312 | |
---|
313 | //TODO - check if needed |
---|
314 | //if(event.getReason() != EventReason.SIM_INIT) |
---|
315 | // triggerEventUp(event); |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | public EventHandler getEventHandler(){ |
---|
320 | return new ComputingResourceEventHandler(); |
---|
321 | } |
---|
322 | |
---|
323 | public void initiate(){ |
---|
324 | |
---|
325 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
326 | EnergyEvent event = new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller"); |
---|
327 | event.setReason(EventReason.SIM_INIT); |
---|
328 | rec.execute(event); |
---|
329 | /*ResourceEventCommand*/ rec = new ResourceEventCommand(this); |
---|
330 | /*EnergyEvent*/ event = new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller"); |
---|
331 | event.setReason(EventReason.SIM_INIT); |
---|
332 | rec.execute(event); |
---|
333 | |
---|
334 | //alternative way |
---|
335 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller")); |
---|
336 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller")); |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | private void accept(EnergyExtension e){ |
---|
341 | extensionList.add(e); |
---|
342 | } |
---|
343 | } |
---|