[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.events.EventHandler; |
---|
[1415] | 12 | import schedframe.events.EventReason; |
---|
[477] | 13 | import schedframe.events.ResourceEventCommand; |
---|
[1330] | 14 | import schedframe.events.scheduling.ResourceStateChangedEvent; |
---|
[477] | 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; |
---|
[1415] | 22 | import schedframe.resources.computing.profiles.energy.ResourceEvent; |
---|
| 23 | import schedframe.resources.computing.profiles.energy.ResourceEventType; |
---|
[477] | 24 | import schedframe.resources.computing.profiles.energy.EnergyExtension; |
---|
[1299] | 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; |
---|
[477] | 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; |
---|
[1207] | 36 | import schedframe.resources.devices.Device; |
---|
| 37 | import schedframe.resources.devices.PhysicalResource; |
---|
[477] | 38 | import schedframe.scheduling.Scheduler; |
---|
[1299] | 39 | import simulator.DataCenterWorkloadSimulator; |
---|
[477] | 40 | |
---|
[1207] | 41 | public class ComputingResource extends PhysicalResource{ |
---|
[477] | 42 | |
---|
| 43 | protected ComputingResource parent; |
---|
| 44 | protected List<ComputingResource> children; |
---|
| 45 | |
---|
| 46 | protected Scheduler scheduler; |
---|
[1207] | 47 | //protected ResourceCharacteristics resourceCharacteristic; |
---|
[477] | 48 | |
---|
[1423] | 49 | |
---|
[477] | 50 | public ComputingResource(ComputingResourceDescription resDesc) { |
---|
| 51 | this.type = resDesc.getType(); |
---|
| 52 | this.name = resDesc.getId(); |
---|
[512] | 53 | this.category = resDesc.getCategory(); |
---|
[477] | 54 | this.status = ResourceStatus.FREE; |
---|
[1423] | 55 | this.extensionList = new ExtensionListImpl(2); |
---|
[477] | 56 | initCharacteristics(resDesc); |
---|
[1299] | 57 | accept(new LoadExtension(resDesc.getLoadProfile(), this)); |
---|
[1396] | 58 | accept(new EnergyExtension.Builder().resource(this).powerProfile(resDesc.getPowerProfile()).airflowProfile(resDesc.getAirflowProfile()).thermalProfile(resDesc.getThermalProfile()).build()); |
---|
[1423] | 59 | |
---|
[477] | 60 | } |
---|
| 61 | |
---|
[1207] | 62 | protected void initCharacteristics(ComputingResourceDescription resDesc){ |
---|
[1423] | 63 | this.resourceCharacteristic = ComputingResourceCharacteristics.builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).device(resDesc.getDevices()).build(); |
---|
[1207] | 64 | for(Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()){ |
---|
| 65 | device.setComputingResource(this); |
---|
[477] | 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public ComputingResource getParent() { |
---|
| 70 | return parent; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | public void setParent(ComputingResource newParent) { |
---|
[1423] | 74 | this.parent = newParent; |
---|
[1330] | 75 | /*if(this.getLoadInterface().getLoadCalendar().getLoadDistribution().size() == 0){ |
---|
[1299] | 76 | this.getLoadInterface().getLoadCalendar().getLoadDistribution().addAll(parent.getLoadInterface().getLoadCalendar().getLoadDistribution()); |
---|
[1330] | 77 | }*/ |
---|
[477] | 78 | } |
---|
| 79 | |
---|
| 80 | public List<ComputingResource> getChildren() { |
---|
| 81 | if (children == null) |
---|
[1423] | 82 | return new ArrayList<ComputingResource>(0); |
---|
[477] | 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 | |
---|
[894] | 93 | public String getFullName() { |
---|
[1005] | 94 | if(this.getResourceCharacteristic().getParameters().get("fullPath") != null){ |
---|
| 95 | String fullPath = this.getResourceCharacteristic().getParameters().get("fullPath").get(0).getContent(); |
---|
| 96 | return fullPath; |
---|
| 97 | } |
---|
[894] | 98 | if(parent!= null){ |
---|
| 99 | return parent.getFullName() + "/" + name; |
---|
| 100 | } else |
---|
| 101 | return name; |
---|
| 102 | } |
---|
[477] | 103 | |
---|
[1207] | 104 | public ComputingResourceCharacteristics getResourceCharacteristic() { |
---|
| 105 | return (ComputingResourceCharacteristics)resourceCharacteristic; |
---|
[477] | 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 | |
---|
[1415] | 119 | private void triggerEventUp(ResourceEvent event) { |
---|
[477] | 120 | if(parent != null) |
---|
| 121 | parent.handleEvent(event); |
---|
| 122 | } |
---|
| 123 | |
---|
[1415] | 124 | public void handleEvent(ResourceEvent event){ |
---|
[477] | 125 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
| 126 | rec.execute(event); |
---|
[1432] | 127 | if(event.getReason() != EventReason.SIM_INIT) |
---|
| 128 | triggerEventUp(event); |
---|
[1453] | 129 | if((scheduler != null && (parent != null && scheduler != parent.getScheduler())) && (event.getSource() != null && !event.getSource().equals(scheduler.getFullName()))){ |
---|
[1423] | 130 | SchedulingEventCommand sec = new SchedulingEventCommand(this); |
---|
| 131 | sec.execute(event); |
---|
| 132 | } |
---|
[477] | 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 | }*/ |
---|
[790] | 141 | //TODO - delete, check in advance |
---|
[477] | 142 | //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
| 143 | // String src = event.getSource() != null ? event.getSource() : name; |
---|
[494] | 144 | // scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); |
---|
[477] | 145 | //} |
---|
| 146 | //triggerEventUp(event); |
---|
| 147 | } |
---|
| 148 | |
---|
[1423] | 149 | |
---|
| 150 | public void updateState(ResourceEvent event){ |
---|
[1432] | 151 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
| 152 | rec.execute(event); |
---|
| 153 | /*for (Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()) { |
---|
[1423] | 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 | } |
---|
[1432] | 167 | }*/ |
---|
[1423] | 168 | |
---|
| 169 | } |
---|
| 170 | |
---|
[477] | 171 | public List <? extends ComputingResource> getDescendantsByType(ResourceType type) { |
---|
[1423] | 172 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(1); |
---|
[477] | 173 | validators.add(new ResourceTypeValidator(type)); |
---|
| 174 | return searchDescendants(validators, true); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) { |
---|
[1423] | 178 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(2); |
---|
[477] | 179 | validators.add(new ResourceStatusValidator(status)); |
---|
| 180 | validators.add(new ResourceTypeValidator(type)); |
---|
| 181 | return searchDescendants(validators, true); |
---|
| 182 | } |
---|
| 183 | |
---|
[1374] | 184 | public ComputingResource getDescendantByName(String resourceName){ |
---|
[1423] | 185 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(1); |
---|
[477] | 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 | |
---|
[495] | 197 | protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutOff) { |
---|
[477] | 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); |
---|
[495] | 212 | if(cutOff == false) { |
---|
[477] | 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 | |
---|
[495] | 252 | class ComputingResourceEventHandler implements EventHandler{ |
---|
[477] | 253 | |
---|
[1415] | 254 | public void handleResourceEvent(ResourceEvent event){ |
---|
[1423] | 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 | |
---|
[477] | 263 | if (extensionList != null) { |
---|
[1299] | 264 | for (Extension extension: extensionList) { |
---|
[477] | 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; |
---|
[1311] | 276 | //ResourceEventFilter filter = new ResourceEventFilter(src.hashCode(), DCWormsTags.UPDATE_PROCESSING); |
---|
| 277 | //scheduler.sim_cancel(filter, null); |
---|
[1330] | 278 | ResourceStateChangedEvent rscEvent = (ResourceStateChangedEvent) event; |
---|
[1415] | 279 | ResourceEventType reType = rscEvent.getResourceEventType(); |
---|
[1392] | 280 | //scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
[1415] | 281 | switch(reType){ |
---|
[1330] | 282 | |
---|
[1415] | 283 | case UTILIZATION_CHANGED: { |
---|
[1392] | 284 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
| 285 | break; |
---|
| 286 | } |
---|
[1415] | 287 | case CPU_FREQUENCY_CHANGED: { |
---|
[1330] | 288 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
| 289 | break; |
---|
| 290 | } |
---|
[1440] | 291 | case POWER_STATE_CHANGED: { |
---|
| 292 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.RESOURCE_POWER_STATE_CHANGED, src); |
---|
| 293 | break; |
---|
| 294 | } |
---|
[1330] | 295 | } |
---|
| 296 | |
---|
[477] | 297 | } else if(parent != null) |
---|
| 298 | parent.getEventHandler().handleSchedulingEvent(event); |
---|
[790] | 299 | |
---|
| 300 | //TODO - check if needed |
---|
| 301 | //if(event.getReason() != EventReason.SIM_INIT) |
---|
| 302 | // triggerEventUp(event); |
---|
[477] | 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | public EventHandler getEventHandler(){ |
---|
[495] | 307 | return new ComputingResourceEventHandler(); |
---|
[477] | 308 | } |
---|
| 309 | |
---|
[1207] | 310 | public final void initiate(){ |
---|
| 311 | for(Device dev: this.getResourceCharacteristic().getDevices()){ |
---|
| 312 | dev.initiate(); |
---|
| 313 | } |
---|
[790] | 314 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
[1415] | 315 | ResourceEvent event = new ResourceEvent(ResourceEventType.AIRFLOW_STATE_CHANGED, "Resource controller"); |
---|
[477] | 316 | event.setReason(EventReason.SIM_INIT); |
---|
[790] | 317 | rec.execute(event); |
---|
| 318 | /*ResourceEventCommand*/ rec = new ResourceEventCommand(this); |
---|
[1415] | 319 | /*ResourceEvent*/ event = new ResourceEvent(ResourceEventType.POWER_STATE_CHANGED, "Resource controller"); |
---|
[495] | 320 | event.setReason(EventReason.SIM_INIT); |
---|
| 321 | rec.execute(event); |
---|
| 322 | |
---|
[1207] | 323 | rec = new ResourceEventCommand(this); |
---|
[1415] | 324 | event = new ResourceEvent(ResourceEventType.TEMPERATURE_CHANGED, "Resource controller"); |
---|
[1207] | 325 | event.setReason(EventReason.SIM_INIT); |
---|
| 326 | rec.execute(event); |
---|
| 327 | |
---|
[1299] | 328 | LoadInterface li = getLoadInterface(); |
---|
| 329 | if(li != null){ |
---|
| 330 | ResourceLoadCalendar rlc = li.getLoadCalendar(); |
---|
[1415] | 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())); |
---|
[1299] | 335 | } |
---|
| 336 | } |
---|
[1447] | 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 | |
---|
[477] | 354 | //alternative way |
---|
[495] | 355 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller")); |
---|
[477] | 356 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller")); |
---|
| 357 | } |
---|
[1207] | 358 | |
---|
[1432] | 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 | } |
---|
[477] | 373 | } |
---|