package schedframe.resources.computing; import gridsim.GridSimTags; import gridsim.dcworms.DCWormsTags; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Properties; import schedframe.events.EventHandler; import schedframe.events.EventReason; import schedframe.events.ResourceEventCommand; import schedframe.events.scheduling.ResourceStateChangedEvent; import schedframe.events.scheduling.SchedulingEvent; import schedframe.events.scheduling.SchedulingEventCommand; import schedframe.resources.ResourceStatus; import schedframe.resources.ResourceType; import schedframe.resources.computing.description.ComputingResourceDescription; import schedframe.resources.computing.extensions.Extension; import schedframe.resources.computing.extensions.ExtensionListImpl; import schedframe.resources.computing.profiles.energy.ResourceEvent; import schedframe.resources.computing.profiles.energy.ResourceEventType; import schedframe.resources.computing.profiles.energy.EnergyExtension; import schedframe.resources.computing.profiles.load.LoadExtension; import schedframe.resources.computing.profiles.load.ResourceLoadCalendar; import schedframe.resources.computing.profiles.load.TimestampUtilization; import schedframe.resources.computing.profiles.load.ui.LoadInterface; import schedframe.resources.computing.properties.DefaultPropertiesBuilder; import schedframe.resources.computing.properties.PropertiesDirector; import schedframe.resources.computing.validator.ResourceNameValidator; import schedframe.resources.computing.validator.ResourcePropertiesValidator; import schedframe.resources.computing.validator.ResourceStatusValidator; import schedframe.resources.computing.validator.ResourceTypeValidator; import schedframe.resources.computing.validator.ResourceValidator; import schedframe.resources.devices.Device; import schedframe.resources.devices.PhysicalResource; import schedframe.scheduling.Scheduler; import simulator.DataCenterWorkloadSimulator; public class ComputingResource extends PhysicalResource{ protected ComputingResource parent; protected List children; protected Scheduler scheduler; //protected ResourceCharacteristics resourceCharacteristic; public ComputingResource(ComputingResourceDescription resDesc) { this.type = resDesc.getType(); this.name = resDesc.getId(); this.category = resDesc.getCategory(); this.status = ResourceStatus.FREE; this.extensionList = new ExtensionListImpl(2); initCharacteristics(resDesc); accept(new LoadExtension(resDesc.getLoadProfile(), this)); accept(new EnergyExtension.Builder().resource(this).powerProfile(resDesc.getPowerProfile()).airflowProfile(resDesc.getAirflowProfile()).thermalProfile(resDesc.getThermalProfile()).build()); } protected void initCharacteristics(ComputingResourceDescription resDesc){ this.resourceCharacteristic = ComputingResourceCharacteristics.builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).device(resDesc.getDevices()).build(); for(Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()){ device.setComputingResource(this); } } public ComputingResource getParent() { return parent; } public void setParent(ComputingResource newParent) { this.parent = newParent; /*if(this.getLoadInterface().getLoadCalendar().getLoadDistribution().size() == 0){ this.getLoadInterface().getLoadCalendar().getLoadDistribution().addAll(parent.getLoadInterface().getLoadCalendar().getLoadDistribution()); }*/ } public List getChildren() { if (children == null) return new ArrayList(0); return children; } public void addChild(ComputingResource child) { child.setParent(this); if (children == null) children = new ArrayList(); children.add(child); } public String getFullName() { if(this.getResourceCharacteristic().getParameters().get("fullPath") != null){ String fullPath = this.getResourceCharacteristic().getParameters().get("fullPath").get(0).getContent(); return fullPath; } if(parent!= null){ return parent.getFullName() + "/" + name; } else return name; } public ComputingResourceCharacteristics getResourceCharacteristic() { return (ComputingResourceCharacteristics)resourceCharacteristic; } public void setStatus(ResourceStatus newStatus) { if(newStatus != status){ status = newStatus; if(children != null) { for(ComputingResource child: children){ child.setStatus(status); } } } } private void triggerEventUp(ResourceEvent event) { if(parent != null) parent.handleEvent(event); } public void handleEvent(ResourceEvent event){ ResourceEventCommand rec = new ResourceEventCommand(this); rec.execute(event); if(event.getReason() != EventReason.SIM_INIT) triggerEventUp(event); if((scheduler != null && (parent != null && scheduler != parent.getScheduler())) && (event.getSource() != null && !event.getSource().equals(scheduler.getFullName()))){ SchedulingEventCommand sec = new SchedulingEventCommand(this); sec.execute(event); } //old, correctly working method /*if (extensionList != null) { for (Extension extension : extensionList) { if (extension.supportsEvent(event)) { extension.handleEvent(event); } } }*/ //TODO - delete, check in advance //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ // String src = event.getSource() != null ? event.getSource() : name; // scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); //} //triggerEventUp(event); } public void updateState(ResourceEvent event){ ResourceEventCommand rec = new ResourceEventCommand(this); rec.execute(event); /*for (Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()) { for (Extension extension: device.getExtensionList()) { if (extension.supportsEvent(event)) { extension.handleEvent(event); } } } if (extensionList != null) { for (Extension extension: extensionList) { if (extension.supportsEvent(event)) { extension.handleEvent(event); } } }*/ } public List getDescendantsByType(ResourceType type) { List validators = new ArrayList(1); validators.add(new ResourceTypeValidator(type)); return searchDescendants(validators, true); } public List getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) { List validators = new ArrayList(2); validators.add(new ResourceStatusValidator(status)); validators.add(new ResourceTypeValidator(type)); return searchDescendants(validators, true); } public ComputingResource getDescendantByName(String resourceName){ List validators = new ArrayList(1); validators.add(new ResourceNameValidator(resourceName)); List resources = searchDescendants(validators, true); return resources.size() == 0 ? null : resources.get(0); } public List filterDescendants(Properties properties) { List validators = new ArrayList(); validators.add(new ResourcePropertiesValidator(properties)); return searchDescendants(validators, false); } protected List searchDescendants(List validators, boolean cutOff) { List descendants = new ArrayList(); if (children != null) { LinkedList toExamine = new LinkedList(); toExamine.push(this); while (!toExamine.isEmpty()) { ComputingResource resource = toExamine.pop(); List resources = resource.getChildren(); int numberOfRes = resources.size(); for (int i = 0; i < numberOfRes; i++) { ComputingResource resourceChild = resources.get(i); if (resourceChild.match(validators)) { descendants.add(resourceChild); if(cutOff == false) { toExamine.addLast(resourceChild); } } else //toExamine.insertElementAt(resourceChild, 0); toExamine.addLast(resourceChild); } } } return descendants; } protected boolean match(List validators){ for(ResourceValidator validator: validators){ if(validator.validate(this) == false) return false; } return true; } public Properties getProperties(){ PropertiesDirector propDirector = new PropertiesDirector(); propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder()); propDirector.constructProperties(this); return propDirector.getProperties(); } public Scheduler getScheduler() { return scheduler; } public void setScheduler(Scheduler scheduler) { this.scheduler = scheduler; if(children != null){ for(ComputingResource child: children){ child.setScheduler(scheduler); } } } class ComputingResourceEventHandler implements EventHandler{ public void handleResourceEvent(ResourceEvent event){ for (Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()) { for (Extension extension: device.getExtensionList()) { if (extension.supportsEvent(event)) { extension.handleEvent(event); } } } if (extensionList != null) { for (Extension extension: extensionList) { if (extension.supportsEvent(event)) { extension.handleEvent(event); } } } } public void handleSchedulingEvent(SchedulingEvent event){ if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ String src = event.getSource() != null ? event.getSource() : name; //ResourceEventFilter filter = new ResourceEventFilter(src.hashCode(), DCWormsTags.UPDATE_PROCESSING); //scheduler.sim_cancel(filter, null); ResourceStateChangedEvent rscEvent = (ResourceStateChangedEvent) event; ResourceEventType reType = rscEvent.getResourceEventType(); //scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); switch(reType){ case UTILIZATION_CHANGED: { scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); break; } case CPU_FREQUENCY_CHANGED: { scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); break; } case POWER_STATE_CHANGED: { scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.RESOURCE_POWER_STATE_CHANGED, src); break; } } } else if(parent != null) parent.getEventHandler().handleSchedulingEvent(event); //TODO - check if needed //if(event.getReason() != EventReason.SIM_INIT) // triggerEventUp(event); } } public EventHandler getEventHandler(){ return new ComputingResourceEventHandler(); } public final void initiate(){ for(Device dev: this.getResourceCharacteristic().getDevices()){ dev.initiate(); } ResourceEventCommand rec = new ResourceEventCommand(this); ResourceEvent event = new ResourceEvent(ResourceEventType.AIRFLOW_STATE_CHANGED, "Resource controller"); event.setReason(EventReason.SIM_INIT); rec.execute(event); /*ResourceEventCommand*/ rec = new ResourceEventCommand(this); /*ResourceEvent*/ event = new ResourceEvent(ResourceEventType.POWER_STATE_CHANGED, "Resource controller"); event.setReason(EventReason.SIM_INIT); rec.execute(event); rec = new ResourceEventCommand(this); event = new ResourceEvent(ResourceEventType.TEMPERATURE_CHANGED, "Resource controller"); event.setReason(EventReason.SIM_INIT); rec.execute(event); LoadInterface li = getLoadInterface(); if(li != null){ ResourceLoadCalendar rlc = li.getLoadCalendar(); LinkedList ld = rlc.getLoadDistribution(); for(TimestampUtilization tu: ld){ DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getStartTime(), new ResourceEvent(ResourceEventType.UTILIZATION_CHANGED, getFullName())); DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getEndTime(), new ResourceEvent(ResourceEventType.UTILIZATION_CHANGED, getFullName())); } } /*EnergyExtension ee = (EnergyExtension) extensionList.getExtension(ExtensionType.ENERGY_EXTENSION); if(ee != null && ee.getPowerProfile() != null){ PluginConfiguration pluginConfig = ee.getPowerProfile().getEnergyEstimationPlugin().getConfiguration(); if (pluginConfig != null) { Map events = pluginConfig.getServedEvents(); if (events != null) { Object obj = events.get(ResourceEventType.TIMER); if (obj != null) { int delay = (Integer) obj; DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), delay, new ResourceEvent(ResourceEventType.TIMER, null)); } } } }*/ //alternative way //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller")); //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller")); } public boolean contains(ComputingResource compRes){ if(compRes.equals(this)){ return true; } else { ComputingResource par = compRes.getParent(); while(par != null){ if (par.equals(this)){ return true; } par = par.getParent(); } } return false; } }