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