Skip to content
Snippets Groups Projects
Commit 75b10da2 authored by Simon Spinner's avatar Simon Spinner
Browse files

Add initial version of parameterization.

parent 39b6187a
No related branches found
No related tags found
No related merge requests found
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Prisma Parameterization
Bundle-SymbolicName: tools.descartes.prisma.parameterization
Bundle-Version: 0.1.0.qualifier
Bundle-Vendor: Descartes Research Group
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: edu.kit.ipd.descartes.mm.deployment;bundle-version="1.1.0",
tools.descartes.librede.model;bundle-version="1.1.0",
org.apache.log4j;bundle-version="1.2.15",
com.google.guava;bundle-version="15.0.0"
Export-Package: tools.descartes.prisma.parameterization
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
package tools.descartes.prisma.parameterization;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import edu.kit.ipd.descartes.mm.applicationlevel.repository.BasicComponent;
import edu.kit.ipd.descartes.mm.applicationlevel.repository.InterfaceProvidingRole;
import edu.kit.ipd.descartes.mm.applicationlevel.repository.Parameter;
import edu.kit.ipd.descartes.mm.applicationlevel.repository.RepositoryComponent;
import edu.kit.ipd.descartes.mm.applicationlevel.repository.Signature;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.AbstractAction;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.BranchAction;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.CoarseGrainedBehavior;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.ComponentInternalBehavior;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.FineGrainedBehavior;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.ForkAction;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.InternalAction;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.LoopAction;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.ResourceDemand;
import edu.kit.ipd.descartes.mm.applicationlevel.servicebehavior.util.ServicebehaviorSwitch;
import edu.kit.ipd.descartes.mm.deployment.Deployment;
import edu.kit.ipd.descartes.mm.deployment.DeploymentContext;
import edu.kit.ipd.descartes.mm.resourceconfiguration.ActiveResourceSpecification;
import edu.kit.ipd.descartes.mm.resourceconfiguration.ConfigurationSpecification;
import edu.kit.ipd.descartes.mm.resourceconfiguration.ProcessingResourceSpecification;
import edu.kit.ipd.descartes.mm.resourceconfiguration.SchedulingPolicy;
import edu.kit.ipd.descartes.mm.resourcelandscape.CompositeHardwareInfrastructure;
import edu.kit.ipd.descartes.mm.resourcelandscape.ComputingInfrastructure;
import edu.kit.ipd.descartes.mm.resourcelandscape.Container;
import edu.kit.ipd.descartes.mm.resourcelandscape.DataCenter;
import edu.kit.ipd.descartes.mm.resourcelandscape.HardwareInfrastructure;
import edu.kit.ipd.descartes.mm.resourcetype.ProcessingResourceType;
import edu.kit.ipd.descartes.mm.resourcetype.ResourceType;
import tools.descartes.librede.configuration.ConfigurationFactory;
import tools.descartes.librede.configuration.Resource;
import tools.descartes.librede.configuration.SchedulingStrategy;
import tools.descartes.librede.configuration.Service;
import tools.descartes.librede.configuration.WorkloadDescription;
public class WorkloadDescriptionDerivation {
private static final Logger log = Logger.getLogger(WorkloadDescriptionDerivation.class);
// a set of all visited containers for which resources have already been analysed.
// the containers are identified by their id.
private Table<String, ProcessingResourceType, Resource> resources = HashBasedTable.create();
private Map<String, Service> services = new HashMap<>();
private Table<Service, Resource, List<ResourceDemand>> demands = HashBasedTable.create();
public WorkloadDescription deriveWorkloadDescription(Deployment deployment) {
WorkloadDescription workload = ConfigurationFactory.eINSTANCE.createWorkloadDescription();
for (DataCenter dc : deployment.getTargetResourceLandscape().getConsistsOf()) {
for (HardwareInfrastructure hw : dc.getContains()) {
visitHardwareInfrastructure(hw);
}
}
for (DeploymentContext ctx : deployment.getDeploymentContexts()) {
// TODO: consider also assembly context and deployment context structure to distingiush services?
RepositoryComponent component = ctx.getAssemblyContext().getEncapsulatedComponent();
determineServicesOfComponent(ctx.getResourceContainer(), component);
}
workload.getResources().addAll(resources.values());
workload.getServices().addAll(services.values());
return workload;
}
private void visitHardwareInfrastructure(HardwareInfrastructure hw) {
if (hw instanceof CompositeHardwareInfrastructure) {
CompositeHardwareInfrastructure castedHw = (CompositeHardwareInfrastructure)hw;
for (HardwareInfrastructure child : castedHw.getContains()) {
visitHardwareInfrastructure(child);
}
} else if (hw instanceof Container) {
visitContainer((Container)hw);
}
}
private void visitContainer(Container container) {
// find all processing resources
for (ConfigurationSpecification spec : container.getConfigSpec()) {
if (spec instanceof ActiveResourceSpecification) {
ActiveResourceSpecification activeResources = ((ActiveResourceSpecification) spec);
for (ProcessingResourceSpecification procRes : activeResources.getProcessingResourceSpecifications()) {
Resource res = ConfigurationFactory.eINSTANCE.createResource();
res.setName(procRes.getId());
res.setNumberOfServers(procRes.getNrOfParProcUnits().getNumber());
res.setSchedulingStrategy(convertSchedulingStrategy(procRes.getSchedulingPolicy()));
Resource old = resources.put(container.getId(), procRes.getActiveResourceType(), res);
if (old != null) {
log.warn("Multiple resources of type " + procRes.getActiveResourceType().getName() + " available for container " + container.getName());
}
}
}
}
// visit all child containers.
for (Container child : container.getContains()) {
visitContainer(child);
}
}
private void determineServicesOfComponent(Container container, RepositoryComponent component) {
// TODO: also consider composite components
if (!(component instanceof BasicComponent)) {
throw new IllegalArgumentException("Only BasicComponent is currently supported");
}
BasicComponent casted = (BasicComponent)component;
for (CoarseGrainedBehavior coarse : casted.getCoarseGrainedBehavior()) {
// in a coarse grained behavior, each resource type should be used only once
Set<ResourceType> visitedTypes = new HashSet<ResourceType>();
Signature signature = coarse.getDescribedSignature();
for (ResourceDemand demand : coarse.getResourceDemand()) {
if (demand.getResourceType() instanceof ProcessingResourceType) {
if (visitedTypes.contains(demand.getResourceType())) {
throw new IllegalStateException("Multiple demands for the same resource type specificed.");
}
visitedTypes.add(demand.getResourceType());
Resource accessedResource = getResource(container, (ProcessingResourceType)demand.getResourceType());
Service curService = getService(signature, "");
addResourceDemand(curService, accessedResource, demand);
}
}
}
for (FineGrainedBehavior fine : casted.getFineGrainedBehavior()) {
Signature signature = fine.getDescribedSignature();
visitComponentInternalBehavior(signature, container, fine.getBehavior(), "");
}
}
private void visitComponentInternalBehavior(Signature signature, Container container, ComponentInternalBehavior behavior, String path) {
int i = 0;
path = path + "/actions.";
for (AbstractAction action : behavior.getActions()) {
if (action instanceof ForkAction) {
forEachComponentInternalBehavior(signature, container, ((ForkAction) action).getForkedBehaviors(), path + i +"/forkedBehaviors");
} else if (action instanceof BranchAction) {
forEachComponentInternalBehavior(signature, container, ((BranchAction) action).getBranches(), path + i + "/branches");
} else if (action instanceof LoopAction) {
forEachComponentInternalBehavior(signature, container, Collections.singletonList(((LoopAction) action).getLoopBodyBehavior()), path + i + "/loopBodyBehavior");
} else if (action instanceof InternalAction) {
visitInternalAction(signature, container, (InternalAction)action, path + i);
}
i++;
}
}
private void forEachComponentInternalBehavior(Signature signature, Container container, List<ComponentInternalBehavior> behaviors, String path) {
int j = 0;
for (ComponentInternalBehavior b : behaviors) {
visitComponentInternalBehavior(signature, container, b, path + path + "." + j);
j++;
}
}
private void visitInternalAction(Signature signature, Container container, InternalAction action, String path) {
for (ResourceDemand demand : action.getResourceDemand()) {
if (demand.getResourceType() instanceof ProcessingResourceType) {
Resource accessedResource = getResource(container, (ProcessingResourceType)demand.getResourceType());
// TODO: add service hierarchy?
Service curService = getService(signature, path);
addResourceDemand(curService, accessedResource, demand);
}
}
}
private Resource getResource(Container container, ProcessingResourceType resourceType) {
Resource accessedResource = resources.get(container.getId(), resourceType);
if (accessedResource == null) {
throw new IllegalStateException("Unknown resource.");
}
return accessedResource;
}
private void addResourceDemand(Service service, Resource resource, ResourceDemand demand) {
List<ResourceDemand> vars = demands.get(service, resource);
if (vars == null) {
//IMPORTANT: only register the service once with the resource.
resource.getServices().add(service);
vars = new LinkedList<>();
demands.put(service, resource, vars);
}
vars.add(demand);
}
private Service getService(Signature curSignature, String action) {
String serviceName = createServiceName(curSignature, action);
Service curService = services.get(serviceName);
if (curService == null) {
curService = ConfigurationFactory.eINSTANCE.createService();
curService.setName(serviceName);
curService.setBackgroundService(false);
services.put(serviceName, curService);
}
return curService;
}
private SchedulingStrategy convertSchedulingStrategy(SchedulingPolicy policy) {
switch(policy) {
case DELAY:
return SchedulingStrategy.IS;
case FCFS:
return SchedulingStrategy.FCFS;
case PROCESSOR_SHARING:
return SchedulingStrategy.PS;
case RANDOM:
case NA:
default:
return SchedulingStrategy.UNKOWN;
}
}
private String createServiceName(Signature signature, String action) {
StringBuilder name = new StringBuilder();
name.append(signature.getInterface().getName());
name.append("#");
name.append(signature.getName());
name.append("(");
boolean first = true;
for (Parameter param : signature.getParameters()) {
if (!first) {
name.append(",");
}
name.append(param.getDataType());
first = false;
}
name.append(")");
if (!action.isEmpty()) {
name.append(":");
name.append(action);
}
return name.toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment