This commit is contained in:
2021-04-06 00:45:28 +02:00
commit 17fabc368e
836 changed files with 3042963 additions and 0 deletions

View File

@@ -0,0 +1,288 @@
package org.fog.placement;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.SimEntity;
import org.cloudbus.cloudsim.core.SimEvent;
import org.fog.application.AppEdge;
import org.fog.application.AppLoop;
import org.fog.application.AppModule;
import org.fog.application.Application;
import org.fog.entities.Actuator;
import org.fog.entities.FogDevice;
import org.fog.entities.Sensor;
import org.fog.utils.Config;
import org.fog.utils.FogEvents;
import org.fog.utils.FogUtils;
import org.fog.utils.NetworkUsageMonitor;
import org.fog.utils.TimeKeeper;
public class Controller extends SimEntity{
public static boolean ONLY_CLOUD = false;
private List<FogDevice> fogDevices;
private List<Sensor> sensors;
private List<Actuator> actuators;
private Map<String, Application> applications;
private Map<String, Integer> appLaunchDelays;
private Map<String, ModulePlacement> appModulePlacementPolicy;
public Controller(String name, List<FogDevice> fogDevices, List<Sensor> sensors, List<Actuator> actuators) {
super(name);
this.applications = new HashMap<String, Application>();
setAppLaunchDelays(new HashMap<String, Integer>());
setAppModulePlacementPolicy(new HashMap<String, ModulePlacement>());
for(FogDevice fogDevice : fogDevices){
fogDevice.setControllerId(getId());
}
setFogDevices(fogDevices);
setActuators(actuators);
setSensors(sensors);
connectWithLatencies();
}
private FogDevice getFogDeviceById(int id){
for(FogDevice fogDevice : getFogDevices()){
if(id==fogDevice.getId())
return fogDevice;
}
return null;
}
private void connectWithLatencies(){
for(FogDevice fogDevice : getFogDevices()){
FogDevice parent = getFogDeviceById(fogDevice.getParentId());
if(parent == null)
continue;
double latency = fogDevice.getUplinkLatency();
parent.getChildToLatencyMap().put(fogDevice.getId(), latency);
parent.getChildrenIds().add(fogDevice.getId());
}
}
@Override
public void startEntity() {
for(String appId : applications.keySet()){
if(getAppLaunchDelays().get(appId)==0)
processAppSubmit(applications.get(appId));
else
send(getId(), getAppLaunchDelays().get(appId), FogEvents.APP_SUBMIT, applications.get(appId));
}
send(getId(), Config.RESOURCE_MANAGE_INTERVAL, FogEvents.CONTROLLER_RESOURCE_MANAGE);
send(getId(), Config.MAX_SIMULATION_TIME, FogEvents.STOP_SIMULATION);
for(FogDevice dev : getFogDevices())
sendNow(dev.getId(), FogEvents.RESOURCE_MGMT);
}
@Override
public void processEvent(SimEvent ev) {
switch(ev.getTag()){
case FogEvents.APP_SUBMIT:
processAppSubmit(ev);
break;
case FogEvents.TUPLE_FINISHED:
processTupleFinished(ev);
break;
case FogEvents.CONTROLLER_RESOURCE_MANAGE:
manageResources();
break;
case FogEvents.STOP_SIMULATION:
CloudSim.stopSimulation();
printTimeDetails();
printPowerDetails();
printCostDetails();
printNetworkUsageDetails();
System.exit(0);
break;
}
}
private void printNetworkUsageDetails() {
System.out.println("Total network usage = "+NetworkUsageMonitor.getNetworkUsage()/Config.MAX_SIMULATION_TIME);
}
private FogDevice getCloud(){
for(FogDevice dev : getFogDevices())
if(dev.getName().equals("cloud"))
return dev;
return null;
}
private void printCostDetails(){
System.out.println("Cost of execution in cloud = "+getCloud().getTotalCost());
}
private void printPowerDetails() {
for(FogDevice fogDevice : getFogDevices()){
System.out.println(fogDevice.getName() + " : Energy Consumed = "+fogDevice.getEnergyConsumption());
}
}
private String getStringForLoopId(int loopId){
for(String appId : getApplications().keySet()){
Application app = getApplications().get(appId);
for(AppLoop loop : app.getLoops()){
if(loop.getLoopId() == loopId)
return loop.getModules().toString();
}
}
return null;
}
private void printTimeDetails() {
System.out.println("=========================================");
System.out.println("============== RESULTS ==================");
System.out.println("=========================================");
System.out.println("EXECUTION TIME : "+ (Calendar.getInstance().getTimeInMillis() - TimeKeeper.getInstance().getSimulationStartTime()));
System.out.println("=========================================");
System.out.println("APPLICATION LOOP DELAYS");
System.out.println("=========================================");
for(Integer loopId : TimeKeeper.getInstance().getLoopIdToTupleIds().keySet()){
/*double average = 0, count = 0;
for(int tupleId : TimeKeeper.getInstance().getLoopIdToTupleIds().get(loopId)){
Double startTime = TimeKeeper.getInstance().getEmitTimes().get(tupleId);
Double endTime = TimeKeeper.getInstance().getEndTimes().get(tupleId);
if(startTime == null || endTime == null)
break;
average += endTime-startTime;
count += 1;
}
System.out.println(getStringForLoopId(loopId) + " ---> "+(average/count));*/
System.out.println(getStringForLoopId(loopId) + " ---> "+TimeKeeper.getInstance().getLoopIdToCurrentAverage().get(loopId));
}
System.out.println("=========================================");
System.out.println("TUPLE CPU EXECUTION DELAY");
System.out.println("=========================================");
for(String tupleType : TimeKeeper.getInstance().getTupleTypeToAverageCpuTime().keySet()){
System.out.println(tupleType + " ---> "+TimeKeeper.getInstance().getTupleTypeToAverageCpuTime().get(tupleType));
}
System.out.println("=========================================");
}
protected void manageResources(){
send(getId(), Config.RESOURCE_MANAGE_INTERVAL, FogEvents.CONTROLLER_RESOURCE_MANAGE);
}
private void processTupleFinished(SimEvent ev) {
}
@Override
public void shutdownEntity() {
}
public void submitApplication(Application application, int delay, ModulePlacement modulePlacement){
FogUtils.appIdToGeoCoverageMap.put(application.getAppId(), application.getGeoCoverage());
getApplications().put(application.getAppId(), application);
getAppLaunchDelays().put(application.getAppId(), delay);
getAppModulePlacementPolicy().put(application.getAppId(), modulePlacement);
for(Sensor sensor : sensors){
sensor.setApp(getApplications().get(sensor.getAppId()));
}
for(Actuator ac : actuators){
ac.setApp(getApplications().get(ac.getAppId()));
}
for(AppEdge edge : application.getEdges()){
if(edge.getEdgeType() == AppEdge.ACTUATOR){
String moduleName = edge.getSource();
for(Actuator actuator : getActuators()){
if(actuator.getActuatorType().equalsIgnoreCase(edge.getDestination()))
application.getModuleByName(moduleName).subscribeActuator(actuator.getId(), edge.getTupleType());
}
}
}
}
public void submitApplication(Application application, ModulePlacement modulePlacement){
submitApplication(application, 0, modulePlacement);
}
private void processAppSubmit(SimEvent ev){
Application app = (Application) ev.getData();
processAppSubmit(app);
}
private void processAppSubmit(Application application){
System.out.println(CloudSim.clock()+" Submitted application "+ application.getAppId());
FogUtils.appIdToGeoCoverageMap.put(application.getAppId(), application.getGeoCoverage());
getApplications().put(application.getAppId(), application);
ModulePlacement modulePlacement = getAppModulePlacementPolicy().get(application.getAppId());
for(FogDevice fogDevice : fogDevices){
sendNow(fogDevice.getId(), FogEvents.ACTIVE_APP_UPDATE, application);
}
Map<Integer, List<AppModule>> deviceToModuleMap = modulePlacement.getDeviceToModuleMap();
for(Integer deviceId : deviceToModuleMap.keySet()){
for(AppModule module : deviceToModuleMap.get(deviceId)){
sendNow(deviceId, FogEvents.APP_SUBMIT, application);
sendNow(deviceId, FogEvents.LAUNCH_MODULE, module);
}
}
}
public List<FogDevice> getFogDevices() {
return fogDevices;
}
public void setFogDevices(List<FogDevice> fogDevices) {
this.fogDevices = fogDevices;
}
public Map<String, Integer> getAppLaunchDelays() {
return appLaunchDelays;
}
public void setAppLaunchDelays(Map<String, Integer> appLaunchDelays) {
this.appLaunchDelays = appLaunchDelays;
}
public Map<String, Application> getApplications() {
return applications;
}
public void setApplications(Map<String, Application> applications) {
this.applications = applications;
}
public List<Sensor> getSensors() {
return sensors;
}
public void setSensors(List<Sensor> sensors) {
for(Sensor sensor : sensors)
sensor.setControllerId(getId());
this.sensors = sensors;
}
public List<Actuator> getActuators() {
return actuators;
}
public void setActuators(List<Actuator> actuators) {
this.actuators = actuators;
}
public Map<String, ModulePlacement> getAppModulePlacementPolicy() {
return appModulePlacementPolicy;
}
public void setAppModulePlacementPolicy(Map<String, ModulePlacement> appModulePlacementPolicy) {
this.appModulePlacementPolicy = appModulePlacementPolicy;
}
}

View File

@@ -0,0 +1,43 @@
package org.fog.placement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ModuleMapping {
/**
* Mapping from node name to list of <moduleName, numInstances> of instances to be launched on node
*/
protected Map<String, List<String>> moduleMapping;
public static ModuleMapping createModuleMapping(){
return new ModuleMapping();
}
public Map<String, List<String>> getModuleMapping() {
return moduleMapping;
}
public void setModuleMapping(Map<String, List<String>> moduleMapping) {
this.moduleMapping = moduleMapping;
}
protected ModuleMapping(){
setModuleMapping(new HashMap<String, List<String>>());
}
/**
* Add <b>instanceCount</b> number of instances of module <b>moduleName</b> to <b>device deviceName</b>
* @param moduleName
* @param deviceName
* @param instanceCount
*/
public void addModuleToDevice(String moduleName, String deviceName){
if(!getModuleMapping().containsKey(deviceName))
getModuleMapping().put(deviceName, new ArrayList<String>());
if(!getModuleMapping().get(deviceName).contains(moduleName))
getModuleMapping().get(deviceName).add(moduleName);
}
}

View File

@@ -0,0 +1,124 @@
package org.fog.placement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.core.CloudSim;
import org.fog.application.AppModule;
import org.fog.application.Application;
import org.fog.entities.FogDevice;
public abstract class ModulePlacement {
public static int ONLY_CLOUD = 1;
public static int EDGEWARDS = 2;
public static int USER_MAPPING = 3;
private List<FogDevice> fogDevices;
private Application application;
private Map<String, List<Integer>> moduleToDeviceMap;
private Map<Integer, List<AppModule>> deviceToModuleMap;
private Map<Integer, Map<String, Integer>> moduleInstanceCountMap;
protected abstract void mapModules();
protected boolean canBeCreated(FogDevice fogDevice, AppModule module){
return fogDevice.getVmAllocationPolicy().allocateHostForVm(module);
}
protected int getParentDevice(int fogDeviceId){
return ((FogDevice)CloudSim.getEntity(fogDeviceId)).getParentId();
}
protected FogDevice getFogDeviceById(int fogDeviceId){
return (FogDevice)CloudSim.getEntity(fogDeviceId);
}
protected boolean createModuleInstanceOnDevice(AppModule _module, final FogDevice device, int instanceCount){
return false;
}
protected boolean createModuleInstanceOnDevice(AppModule _module, final FogDevice device){
AppModule module = null;
if(getModuleToDeviceMap().containsKey(_module.getName()))
module = new AppModule(_module);
else
module = _module;
if(canBeCreated(device, module)){
System.out.println("Creating "+module.getName()+" on device "+device.getName());
if(!getDeviceToModuleMap().containsKey(device.getId()))
getDeviceToModuleMap().put(device.getId(), new ArrayList<AppModule>());
getDeviceToModuleMap().get(device.getId()).add(module);
if(!getModuleToDeviceMap().containsKey(module.getName()))
getModuleToDeviceMap().put(module.getName(), new ArrayList<Integer>());
getModuleToDeviceMap().get(module.getName()).add(device.getId());
return true;
} else {
System.err.println("Module "+module.getName()+" cannot be created on device "+device.getName());
System.err.println("Terminating");
return false;
}
}
protected FogDevice getDeviceByName(String deviceName) {
for(FogDevice dev : getFogDevices()){
if(dev.getName().equals(deviceName))
return dev;
}
return null;
}
protected FogDevice getDeviceById(int id){
for(FogDevice dev : getFogDevices()){
if(dev.getId() == id)
return dev;
}
return null;
}
public List<FogDevice> getFogDevices() {
return fogDevices;
}
public void setFogDevices(List<FogDevice> fogDevices) {
this.fogDevices = fogDevices;
}
public Application getApplication() {
return application;
}
public void setApplication(Application application) {
this.application = application;
}
public Map<String, List<Integer>> getModuleToDeviceMap() {
return moduleToDeviceMap;
}
public void setModuleToDeviceMap(Map<String, List<Integer>> moduleToDeviceMap) {
this.moduleToDeviceMap = moduleToDeviceMap;
}
public Map<Integer, List<AppModule>> getDeviceToModuleMap() {
return deviceToModuleMap;
}
public void setDeviceToModuleMap(Map<Integer, List<AppModule>> deviceToModuleMap) {
this.deviceToModuleMap = deviceToModuleMap;
}
public Map<Integer, Map<String, Integer>> getModuleInstanceCountMap() {
return moduleInstanceCountMap;
}
public void setModuleInstanceCountMap(Map<Integer, Map<String, Integer>> moduleInstanceCountMap) {
this.moduleInstanceCountMap = moduleInstanceCountMap;
}
}

View File

@@ -0,0 +1,525 @@
package org.fog.placement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.math3.util.Pair;
import org.cloudbus.cloudsim.core.CloudSim;
import org.fog.application.AppEdge;
import org.fog.application.AppModule;
import org.fog.application.Application;
import org.fog.application.selectivity.SelectivityModel;
import org.fog.entities.Actuator;
import org.fog.entities.FogDevice;
import org.fog.entities.Sensor;
import org.fog.entities.Tuple;
import org.fog.utils.Logger;
public class ModulePlacementEdgewards extends ModulePlacement{
protected ModuleMapping moduleMapping;
protected List<Sensor> sensors;
protected List<Actuator> actuators;
protected Map<Integer, Double> currentCpuLoad;
/**
* Stores the current mapping of application modules to fog devices
*/
protected Map<Integer, List<String>> currentModuleMap;
protected Map<Integer, Map<String, Double>> currentModuleLoadMap;
protected Map<Integer, Map<String, Integer>> currentModuleInstanceNum;
public ModulePlacementEdgewards(List<FogDevice> fogDevices, List<Sensor> sensors, List<Actuator> actuators,
Application application, ModuleMapping moduleMapping){
this.setFogDevices(fogDevices);
this.setApplication(application);
this.setModuleMapping(moduleMapping);
this.setModuleToDeviceMap(new HashMap<String, List<Integer>>());
this.setDeviceToModuleMap(new HashMap<Integer, List<AppModule>>());
setSensors(sensors);
setActuators(actuators);
setCurrentCpuLoad(new HashMap<Integer, Double>());
setCurrentModuleMap(new HashMap<Integer, List<String>>());
setCurrentModuleLoadMap(new HashMap<Integer, Map<String, Double>>());
setCurrentModuleInstanceNum(new HashMap<Integer, Map<String, Integer>>());
for(FogDevice dev : getFogDevices()){
getCurrentCpuLoad().put(dev.getId(), 0.0);
getCurrentModuleLoadMap().put(dev.getId(), new HashMap<String, Double>());
getCurrentModuleMap().put(dev.getId(), new ArrayList<String>());
getCurrentModuleInstanceNum().put(dev.getId(), new HashMap<String, Integer>());
}
mapModules();
setModuleInstanceCountMap(getCurrentModuleInstanceNum());
}
@Override
protected void mapModules() {
for(String deviceName : getModuleMapping().getModuleMapping().keySet()){
for(String moduleName : getModuleMapping().getModuleMapping().get(deviceName)){
int deviceId = CloudSim.getEntityId(deviceName);
getCurrentModuleMap().get(deviceId).add(moduleName);
getCurrentModuleLoadMap().get(deviceId).put(moduleName, 0.0);
getCurrentModuleInstanceNum().get(deviceId).put(moduleName, 0);
}
}
List<List<Integer>> leafToRootPaths = getLeafToRootPaths();
for(List<Integer> path : leafToRootPaths){
placeModulesInPath(path);
}
for(int deviceId : getCurrentModuleMap().keySet()){
for(String module : getCurrentModuleMap().get(deviceId)){
createModuleInstanceOnDevice(getApplication().getModuleByName(module), getFogDeviceById(deviceId));
}
}
}
/**
* Get the list of modules that are ready to be placed
* @param placedModules Modules that have already been placed in current path
* @return list of modules ready to be placed
*/
private List<String> getModulesToPlace(List<String> placedModules){
Application app = getApplication();
List<String> modulesToPlace_1 = new ArrayList<String>();
List<String> modulesToPlace = new ArrayList<String>();
for(AppModule module : app.getModules()){
if(!placedModules.contains(module.getName()))
modulesToPlace_1.add(module.getName());
}
/*
* Filtering based on whether modules (to be placed) lower in physical topology are already placed
*/
for(String moduleName : modulesToPlace_1){
boolean toBePlaced = true;
for(AppEdge edge : app.getEdges()){
//CHECK IF OUTGOING DOWN EDGES ARE PLACED
if(edge.getSource().equals(moduleName) && edge.getDirection()==Tuple.DOWN && !placedModules.contains(edge.getDestination()))
toBePlaced = false;
//CHECK IF INCOMING UP EDGES ARE PLACED
if(edge.getDestination().equals(moduleName) && edge.getDirection()==Tuple.UP && !placedModules.contains(edge.getSource()))
toBePlaced = false;
}
if(toBePlaced)
modulesToPlace.add(moduleName);
}
return modulesToPlace;
}
protected double getRateOfSensor(String sensorType){
for(Sensor sensor : getSensors()){
if(sensor.getTupleType().equals(sensorType))
return 1/sensor.getTransmitDistribution().getMeanInterTransmitTime();
}
return 0;
}
private void placeModulesInPath(List<Integer> path) {
if(path.size()==0)return;
List<String> placedModules = new ArrayList<String>();
Map<AppEdge, Double> appEdgeToRate = new HashMap<AppEdge, Double>();
/**
* Periodic edges have a fixed periodicity of tuples, so setting the tuple rate beforehand
*/
for(AppEdge edge : getApplication().getEdges()){
if(edge.isPeriodic()){
appEdgeToRate.put(edge, 1/edge.getPeriodicity());
}
}
for(Integer deviceId : path){
FogDevice device = getFogDeviceById(deviceId);
Map<String, Integer> sensorsAssociated = getAssociatedSensors(device);
Map<String, Integer> actuatorsAssociated = getAssociatedActuators(device);
placedModules.addAll(sensorsAssociated.keySet()); // ADDING ALL SENSORS TO PLACED LIST
placedModules.addAll(actuatorsAssociated.keySet()); // ADDING ALL ACTUATORS TO PLACED LIST
/*
* Setting the rates of application edges emanating from sensors
*/
for(String sensor : sensorsAssociated.keySet()){
for(AppEdge edge : getApplication().getEdges()){
if(edge.getSource().equals(sensor)){
appEdgeToRate.put(edge, sensorsAssociated.get(sensor)*getRateOfSensor(sensor));
}
}
}
/*
* Updating the AppEdge rates for the entire application based on knowledge so far
*/
boolean changed = true;
while(changed){ //Loop runs as long as some new information is added
changed=false;
Map<AppEdge, Double> rateMap = new HashMap<AppEdge, Double>(appEdgeToRate);
for(AppEdge edge : rateMap.keySet()){
AppModule destModule = getApplication().getModuleByName(edge.getDestination());
if(destModule == null)continue;
Map<Pair<String, String>, SelectivityModel> map = destModule.getSelectivityMap();
for(Pair<String, String> pair : map.keySet()){
if(pair.getFirst().equals(edge.getTupleType())){
double outputRate = appEdgeToRate.get(edge)*map.get(pair).getMeanRate(); // getting mean rate from SelectivityModel
AppEdge outputEdge = getApplication().getEdgeMap().get(pair.getSecond());
if(!appEdgeToRate.containsKey(outputEdge) || appEdgeToRate.get(outputEdge)!=outputRate){
// if some new information is available
changed = true;
}
appEdgeToRate.put(outputEdge, outputRate);
}
}
}
}
/*
* Getting the list of modules ready to be placed on current device on path
*/
List<String> modulesToPlace = getModulesToPlace(placedModules);
while(modulesToPlace.size() > 0){ // Loop runs until all modules in modulesToPlace are deployed in the path
String moduleName = modulesToPlace.get(0);
double totalCpuLoad = 0;
//IF MODULE IS ALREADY PLACED UPSTREAM, THEN UPDATE THE EXISTING MODULE
int upsteamDeviceId = isPlacedUpstream(moduleName, path);
if(upsteamDeviceId > 0){
if(upsteamDeviceId==deviceId){
placedModules.add(moduleName);
modulesToPlace = getModulesToPlace(placedModules);
// NOW THE MODULE TO PLACE IS IN THE CURRENT DEVICE. CHECK IF THE NODE CAN SUSTAIN THE MODULE
for(AppEdge edge : getApplication().getEdges()){ // take all incoming edges
if(edge.getDestination().equals(moduleName)){
double rate = appEdgeToRate.get(edge);
totalCpuLoad += rate*edge.getTupleCpuLength();
}
}
if(totalCpuLoad + getCurrentCpuLoad().get(deviceId) > device.getHost().getTotalMips()){
Logger.debug("ModulePlacementEdgeward", "Need to shift module "+moduleName+" upstream from device " + device.getName());
List<String> _placedOperators = shiftModuleNorth(moduleName, totalCpuLoad, deviceId, modulesToPlace);
for(String placedOperator : _placedOperators){
if(!placedModules.contains(placedOperator))
placedModules.add(placedOperator);
}
} else{
placedModules.add(moduleName);
getCurrentCpuLoad().put(deviceId, getCurrentCpuLoad().get(deviceId)+totalCpuLoad);
getCurrentModuleInstanceNum().get(deviceId).put(moduleName, getCurrentModuleInstanceNum().get(deviceId).get(moduleName)+1);
Logger.debug("ModulePlacementEdgeward", "AppModule "+moduleName+" can be created on device "+device.getName());
}
}
}else{
// FINDING OUT WHETHER PLACEMENT OF OPERATOR ON DEVICE IS POSSIBLE
for(AppEdge edge : getApplication().getEdges()){ // take all incoming edges
if(edge.getDestination().equals(moduleName)){
double rate = appEdgeToRate.get(edge);
totalCpuLoad += rate*edge.getTupleCpuLength();
}
}
if(totalCpuLoad + getCurrentCpuLoad().get(deviceId) > device.getHost().getTotalMips()){
Logger.debug("ModulePlacementEdgeward", "Placement of operator "+moduleName+ "NOT POSSIBLE on device "+device.getName());
}
else{
Logger.debug("ModulePlacementEdgeward", "Placement of operator "+moduleName+ " on device "+device.getName() + " successful.");
getCurrentCpuLoad().put(deviceId, totalCpuLoad + getCurrentCpuLoad().get(deviceId));
System.out.println("Placement of operator "+moduleName+ " on device "+device.getName() + " successful.");
if(!currentModuleMap.containsKey(deviceId))
currentModuleMap.put(deviceId, new ArrayList<String>());
currentModuleMap.get(deviceId).add(moduleName);
placedModules.add(moduleName);
modulesToPlace = getModulesToPlace(placedModules);
getCurrentModuleLoadMap().get(device.getId()).put(moduleName, totalCpuLoad);
int max = 1;
for(AppEdge edge : getApplication().getEdges()){
if(edge.getSource().equals(moduleName) && actuatorsAssociated.containsKey(edge.getDestination()))
max = Math.max(actuatorsAssociated.get(edge.getDestination()), max);
if(edge.getDestination().equals(moduleName) && sensorsAssociated.containsKey(edge.getSource()))
max = Math.max(sensorsAssociated.get(edge.getSource()), max);
}
getCurrentModuleInstanceNum().get(deviceId).put(moduleName, max);
}
}
modulesToPlace.remove(moduleName);
}
}
}
/**
* Shifts a module moduleName from device deviceId northwards. This involves other modules that depend on it to be shifted north as well.
* @param moduleName
* @param cpuLoad cpuLoad of the module
* @param deviceId
*/
private List<String> shiftModuleNorth(String moduleName, double cpuLoad, Integer deviceId, List<String> operatorsToPlace) {
System.out.println(CloudSim.getEntityName(deviceId)+" is shifting "+moduleName+" north.");
List<String> modulesToShift = findModulesToShift(moduleName, deviceId);
Map<String, Integer> moduleToNumInstances = new HashMap<String, Integer>(); // Map of number of instances of modules that need to be shifted
double totalCpuLoad = 0;
Map<String, Double> loadMap = new HashMap<String, Double>();
for(String module : modulesToShift){
loadMap.put(module, getCurrentModuleLoadMap().get(deviceId).get(module));
moduleToNumInstances.put(module, getCurrentModuleInstanceNum().get(deviceId).get(module)+1);
totalCpuLoad += getCurrentModuleLoadMap().get(deviceId).get(module);
getCurrentModuleLoadMap().get(deviceId).remove(module);
getCurrentModuleMap().get(deviceId).remove(module);
getCurrentModuleInstanceNum().get(deviceId).remove(module);
}
getCurrentCpuLoad().put(deviceId, getCurrentCpuLoad().get(deviceId)-totalCpuLoad); // change info of current CPU load on device
loadMap.put(moduleName, loadMap.get(moduleName)+cpuLoad);
totalCpuLoad += cpuLoad;
int id = getParentDevice(deviceId);
while(true){ // Loop iterates over all devices in path upstream from current device. Tries to place modules (to be shifted northwards) on each of them.
if(id==-1){
// Loop has reached the apex fog device in hierarchy, and still could not place modules.
Logger.debug("ModulePlacementEdgeward", "Could not place modules "+modulesToShift+" northwards.");
break;
}
FogDevice fogDevice = getFogDeviceById(id);
if(getCurrentCpuLoad().get(id) + totalCpuLoad > fogDevice.getHost().getTotalMips()){
// Device cannot take up CPU load of incoming modules. Keep searching for device further north.
List<String> _modulesToShift = findModulesToShift(modulesToShift, id); // All modules in _modulesToShift are currently placed on device id
double cpuLoadShifted = 0; // the total CPU load shifted from device id to its parent
for(String module : _modulesToShift){
if(!modulesToShift.contains(module)){
// Add information of all newly added modules (to be shifted)
moduleToNumInstances.put(module, getCurrentModuleInstanceNum().get(id).get(module)+moduleToNumInstances.get(module));
loadMap.put(module, getCurrentModuleLoadMap().get(id).get(module));
cpuLoadShifted += getCurrentModuleLoadMap().get(id).get(module);
totalCpuLoad += getCurrentModuleLoadMap().get(id).get(module);
// Removing information of all modules (to be shifted north) in device with ID id
getCurrentModuleLoadMap().get(id).remove(module);
getCurrentModuleMap().get(id).remove(module);
getCurrentModuleInstanceNum().get(id).remove(module);
}
}
getCurrentCpuLoad().put(id, getCurrentCpuLoad().get(id)-cpuLoadShifted); // CPU load on device id gets reduced due to modules shifting northwards
modulesToShift = _modulesToShift;
id = getParentDevice(id); // iterating to parent device
} else{
// Device (@ id) can accommodate modules. Placing them here.
double totalLoad = 0;
for(String module : loadMap.keySet()){
totalLoad += loadMap.get(module);
getCurrentModuleLoadMap().get(id).put(module, loadMap.get(module));
getCurrentModuleMap().get(id).add(module);
String module_ = module;
int initialNumInstances = 0;
if(getCurrentModuleInstanceNum().get(id).containsKey(module_))
initialNumInstances = getCurrentModuleInstanceNum().get(id).get(module_);
int finalNumInstances = initialNumInstances + moduleToNumInstances.get(module_);
getCurrentModuleInstanceNum().get(id).put(module_, finalNumInstances);
}
getCurrentCpuLoad().put(id, totalLoad);
operatorsToPlace.removeAll(loadMap.keySet());
List<String> placedOperators = new ArrayList<String>();
for(String op : loadMap.keySet())placedOperators.add(op);
return placedOperators;
}
}
return new ArrayList<String>();
}
/**
* Get all modules that need to be shifted northwards along with <b>module</b>.
* Typically, these other modules are those that are hosted on device with ID <b>deviceId</b> and lie upstream of <b>module</b> in application model.
* @param module the module that needs to be shifted northwards
* @param deviceId the fog device ID that it is currently on
* @return list of all modules that need to be shifted north along with <b>module</b>
*/
private List<String> findModulesToShift(String module, Integer deviceId) {
List<String> modules = new ArrayList<String>();
modules.add(module);
return findModulesToShift(modules, deviceId);
/*List<String> upstreamModules = new ArrayList<String>();
upstreamModules.add(module);
boolean changed = true;
while(changed){ // Keep loop running as long as new information is added.
changed = false;
for(AppEdge edge : getApplication().getEdges()){
* If there is an application edge UP from the module to be shifted to another module in the same device
if(upstreamModules.contains(edge.getSource()) && edge.getDirection()==Tuple.UP &&
getCurrentModuleMap().get(deviceId).contains(edge.getDestination())
&& !upstreamModules.contains(edge.getDestination())){
upstreamModules.add(edge.getDestination());
changed = true;
}
}
}
return upstreamModules; */
}
/**
* Get all modules that need to be shifted northwards along with <b>modules</b>.
* Typically, these other modules are those that are hosted on device with ID <b>deviceId</b> and lie upstream of modules in <b>modules</b> in application model.
* @param module the module that needs to be shifted northwards
* @param deviceId the fog device ID that it is currently on
* @return list of all modules that need to be shifted north along with <b>modules</b>
*/
private List<String> findModulesToShift(List<String> modules, Integer deviceId) {
List<String> upstreamModules = new ArrayList<String>();
upstreamModules.addAll(modules);
boolean changed = true;
while(changed){ // Keep loop running as long as new information is added.
changed = false;
/*
* If there is an application edge UP from the module to be shifted to another module in the same device
*/
for(AppEdge edge : getApplication().getEdges()){
if(upstreamModules.contains(edge.getSource()) && edge.getDirection()==Tuple.UP &&
getCurrentModuleMap().get(deviceId).contains(edge.getDestination())
&& !upstreamModules.contains(edge.getDestination())){
upstreamModules.add(edge.getDestination());
changed = true;
}
}
}
return upstreamModules;
}
private int isPlacedUpstream(String operatorName, List<Integer> path) {
for(int deviceId : path){
if(currentModuleMap.containsKey(deviceId) && currentModuleMap.get(deviceId).contains(operatorName))
return deviceId;
}
return -1;
}
/**
* Gets all sensors associated with fog-device <b>device</b>
* @param device
* @return map from sensor type to number of such sensors
*/
private Map<String, Integer> getAssociatedSensors(FogDevice device) {
Map<String, Integer> endpoints = new HashMap<String, Integer>();
for(Sensor sensor : getSensors()){
if(sensor.getGatewayDeviceId()==device.getId()){
if(!endpoints.containsKey(sensor.getTupleType()))
endpoints.put(sensor.getTupleType(), 0);
endpoints.put(sensor.getTupleType(), endpoints.get(sensor.getTupleType())+1);
}
}
return endpoints;
}
/**
* Gets all actuators associated with fog-device <b>device</b>
* @param device
* @return map from actuator type to number of such sensors
*/
private Map<String, Integer> getAssociatedActuators(FogDevice device) {
Map<String, Integer> endpoints = new HashMap<String, Integer>();
for(Actuator actuator : getActuators()){
if(actuator.getGatewayDeviceId()==device.getId()){
if(!endpoints.containsKey(actuator.getActuatorType()))
endpoints.put(actuator.getActuatorType(), 0);
endpoints.put(actuator.getActuatorType(), endpoints.get(actuator.getActuatorType())+1);
}
}
return endpoints;
}
@SuppressWarnings("serial")
protected List<List<Integer>> getPaths(final int fogDeviceId){
FogDevice device = (FogDevice)CloudSim.getEntity(fogDeviceId);
if(device.getChildrenIds().size() == 0){
final List<Integer> path = (new ArrayList<Integer>(){{add(fogDeviceId);}});
List<List<Integer>> paths = (new ArrayList<List<Integer>>(){{add(path);}});
return paths;
}
List<List<Integer>> paths = new ArrayList<List<Integer>>();
for(int childId : device.getChildrenIds()){
List<List<Integer>> childPaths = getPaths(childId);
for(List<Integer> childPath : childPaths)
childPath.add(fogDeviceId);
paths.addAll(childPaths);
}
return paths;
}
protected List<List<Integer>> getLeafToRootPaths(){
FogDevice cloud=null;
for(FogDevice device : getFogDevices()){
if(device.getName().equals("cloud"))
cloud = device;
}
return getPaths(cloud.getId());
}
public ModuleMapping getModuleMapping() {
return moduleMapping;
}
public void setModuleMapping(ModuleMapping moduleMapping) {
this.moduleMapping = moduleMapping;
}
public Map<Integer, List<String>> getCurrentModuleMap() {
return currentModuleMap;
}
public void setCurrentModuleMap(Map<Integer, List<String>> currentModuleMap) {
this.currentModuleMap = currentModuleMap;
}
public List<Sensor> getSensors() {
return sensors;
}
public void setSensors(List<Sensor> sensors) {
this.sensors = sensors;
}
public List<Actuator> getActuators() {
return actuators;
}
public void setActuators(List<Actuator> actuators) {
this.actuators = actuators;
}
public Map<Integer, Double> getCurrentCpuLoad() {
return currentCpuLoad;
}
public void setCurrentCpuLoad(Map<Integer, Double> currentCpuLoad) {
this.currentCpuLoad= currentCpuLoad;
}
public Map<Integer, Map<String, Double>> getCurrentModuleLoadMap() {
return currentModuleLoadMap;
}
public void setCurrentModuleLoadMap(
Map<Integer, Map<String, Double>> currentModuleLoadMap) {
this.currentModuleLoadMap = currentModuleLoadMap;
}
public Map<Integer, Map<String, Integer>> getCurrentModuleInstanceNum() {
return currentModuleInstanceNum;
}
public void setCurrentModuleInstanceNum(
Map<Integer, Map<String, Integer>> currentModuleInstanceNum) {
this.currentModuleInstanceNum = currentModuleInstanceNum;
}
}

View File

@@ -0,0 +1,53 @@
package org.fog.placement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fog.application.AppModule;
import org.fog.application.Application;
import org.fog.entities.FogDevice;
public class ModulePlacementMapping extends ModulePlacement{
private ModuleMapping moduleMapping;
@Override
protected void mapModules() {
Map<String, List<String>> mapping = moduleMapping.getModuleMapping();
for(String deviceName : mapping.keySet()){
FogDevice device = getDeviceByName(deviceName);
for(String moduleName : mapping.get(deviceName)){
AppModule module = getApplication().getModuleByName(moduleName);
if(module == null)
continue;
createModuleInstanceOnDevice(module, device);
//getModuleInstanceCountMap().get(device.getId()).put(moduleName, mapping.get(deviceName).get(moduleName));
}
}
}
public ModulePlacementMapping(List<FogDevice> fogDevices, Application application,
ModuleMapping moduleMapping){
this.setFogDevices(fogDevices);
this.setApplication(application);
this.setModuleMapping(moduleMapping);
this.setModuleToDeviceMap(new HashMap<String, List<Integer>>());
this.setDeviceToModuleMap(new HashMap<Integer, List<AppModule>>());
this.setModuleInstanceCountMap(new HashMap<Integer, Map<String, Integer>>());
for(FogDevice device : getFogDevices())
getModuleInstanceCountMap().put(device.getId(), new HashMap<String, Integer>());
mapModules();
}
public ModuleMapping getModuleMapping() {
return moduleMapping;
}
public void setModuleMapping(ModuleMapping moduleMapping) {
this.moduleMapping = moduleMapping;
}
}

View File

@@ -0,0 +1,101 @@
package org.fog.placement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.core.CloudSim;
import org.fog.application.AppEdge;
import org.fog.application.AppModule;
import org.fog.application.Application;
import org.fog.entities.Actuator;
import org.fog.entities.FogDevice;
import org.fog.entities.Sensor;
import org.fog.entities.Tuple;
public class ModulePlacementOnlyCloud extends ModulePlacement{
private List<Sensor> sensors;
private List<Actuator> actuators;
private int cloudId;
public ModulePlacementOnlyCloud(List<FogDevice> fogDevices, List<Sensor> sensors, List<Actuator> actuators, Application application){
this.setFogDevices(fogDevices);
this.setApplication(application);
this.setSensors(sensors);
this.setActuators(actuators);
this.setModuleToDeviceMap(new HashMap<String, List<Integer>>());
this.setDeviceToModuleMap(new HashMap<Integer, List<AppModule>>());
this.setModuleInstanceCountMap(new HashMap<Integer, Map<String, Integer>>());
this.cloudId = CloudSim.getEntityId("cloud");
mapModules();
computeModuleInstanceCounts();
}
private void computeModuleInstanceCounts(){
FogDevice cloud = getDeviceById(CloudSim.getEntityId("cloud"));
getModuleInstanceCountMap().put(cloud.getId(), new HashMap<String, Integer>());
for(Sensor sensor : getSensors()){
String sensorType = sensor.getSensorName();
if(!getModuleInstanceCountMap().get(cloud.getId()).containsKey(sensorType))
getModuleInstanceCountMap().get(cloud.getId()).put(sensorType, 0);
getModuleInstanceCountMap().get(cloud.getId()).put(sensorType, getModuleInstanceCountMap().get(cloud.getId()).get(sensorType)+1);
}
for(Actuator actuator : getActuators()){
String actuatorType = actuator.getActuatorType();
if(!getModuleInstanceCountMap().get(cloud.getId()).containsKey(actuatorType))
getModuleInstanceCountMap().get(cloud.getId()).put(actuatorType, 0);
getModuleInstanceCountMap().get(cloud.getId()).put(actuatorType, getModuleInstanceCountMap().get(cloud.getId()).get(actuatorType)+1);
}
while(!isModuleInstanceCalculationComplete()){
for(AppModule module : getApplication().getModules()){
int maxInstances = 0;
for(AppEdge edge : getApplication().getEdges()){
if(!getModuleInstanceCountMap().get(cloudId).containsKey(edge.getSource()))
continue;
if(edge.getDestination().equals(module.getName()) && edge.getDirection()==Tuple.UP){
maxInstances = Math.max(maxInstances, getModuleInstanceCountMap().get(cloudId).get(edge.getSource()));
}
}
getModuleInstanceCountMap().get(cloudId).put(module.getName(), maxInstances);
}
}
System.out.println(getModuleInstanceCountMap());
}
private boolean isModuleInstanceCalculationComplete() {
for(AppModule module : getApplication().getModules()){
if(!getModuleInstanceCountMap().get(cloudId).containsKey(module.getName()))
return false;
}
return true;
}
@Override
protected void mapModules() {
List<AppModule> modules = getApplication().getModules();
for(AppModule module : modules){
FogDevice cloud = getDeviceById(cloudId);
createModuleInstanceOnDevice(module, cloud);
}
}
public List<Actuator> getActuators() {
return actuators;
}
public void setActuators(List<Actuator> actuators) {
this.actuators = actuators;
}
public List<Sensor> getSensors() {
return sensors;
}
public void setSensors(List<Sensor> sensors) {
this.sensors = sensors;
}
}

View File

@@ -0,0 +1,284 @@
package org.fog.placement;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.SimEntity;
import org.cloudbus.cloudsim.core.SimEvent;
import org.fog.application.AppEdge;
import org.fog.application.AppLoop;
import org.fog.application.AppModule;
import org.fog.application.MyApplication;
import org.fog.entities.MyActuator;
import org.fog.entities.MyFogDevice;
import org.fog.entities.MySensor;
import org.fog.utils.Config;
import org.fog.utils.FogEvents;
import org.fog.utils.FogUtils;
import org.fog.utils.NetworkUsageMonitor;
import org.fog.utils.TimeKeeper;
public class MyController extends SimEntity{
public static boolean ONLY_CLOUD = false;
private List<MyFogDevice> fogDevices;
private List<MySensor> sensors;
private List<MyActuator> actuators;
private Map<String, MyApplication> applications;
private Map<String, Integer> appLaunchDelays;
private Map<String, MyModulePlacement> appModulePlacementPolicy;
public MyController(String name, List<MyFogDevice> fogDevices, List<MySensor> sensors, List<MyActuator> actuators) {
super(name);
this.applications = new HashMap<String, MyApplication>();
setAppLaunchDelays(new HashMap<String, Integer>());
setAppModulePlacementPolicy(new HashMap<String, MyModulePlacement>());
for(MyFogDevice fogDevice : fogDevices){
fogDevice.setControllerId(getId());
}
setMyFogDevices(fogDevices);
setMyActuators(actuators);
setMySensors(sensors);
connectWithLatencies();
}
private MyFogDevice getMyFogDeviceById(int id){
for(MyFogDevice fogDevice : getMyFogDevices()){
if(id==fogDevice.getId())
return fogDevice;
}
return null;
}
private void connectWithLatencies(){
for(MyFogDevice fogDevice : getMyFogDevices()){
MyFogDevice parent = getMyFogDeviceById(fogDevice.getParentId());
if(parent == null)
continue;
double latency = fogDevice.getUplinkLatency();
parent.getChildToLatencyMap().put(fogDevice.getId(), latency);
parent.getChildrenIds().add(fogDevice.getId());
}
}
@Override
public void startEntity() {
for(String appId : applications.keySet()){
if(getAppLaunchDelays().get(appId)==0)
processAppSubmit(applications.get(appId));
else
send(getId(), getAppLaunchDelays().get(appId), FogEvents.APP_SUBMIT, applications.get(appId));
}
send(getId(), Config.RESOURCE_MANAGE_INTERVAL, FogEvents.CONTROLLER_RESOURCE_MANAGE);
send(getId(), Config.MAX_SIMULATION_TIME, FogEvents.STOP_SIMULATION);
for(MyFogDevice dev : getMyFogDevices())
sendNow(dev.getId(), FogEvents.RESOURCE_MGMT);
}
@Override
public void processEvent(SimEvent ev) {
switch(ev.getTag()){
case FogEvents.APP_SUBMIT:
processAppSubmit(ev);
break;
case FogEvents.TUPLE_FINISHED:
processTupleFinished(ev);
break;
case FogEvents.CONTROLLER_RESOURCE_MANAGE:
manageResources();
break;
case FogEvents.STOP_SIMULATION:
CloudSim.stopSimulation();
printTimeDetails();
printPowerDetails();
printCostDetails();
printNetworkUsageDetails();
System.exit(0);
break;
}
}
private void printNetworkUsageDetails() {
System.out.println("Total network usage = "+NetworkUsageMonitor.getNetworkUsage()/Config.MAX_SIMULATION_TIME);
}
private MyFogDevice getCloud(){
for(MyFogDevice dev : getMyFogDevices())
if(dev.getName().equals("cloud"))
return dev;
return null;
}
private void printCostDetails(){
System.out.println("Cost of execution in cloud = "+getCloud().getTotalCost());
}
private void printPowerDetails() {
for(MyFogDevice fogDevice : getMyFogDevices()){
System.out.println(fogDevice.getName() + " : Energy Consumed = "+fogDevice.getEnergyConsumption());
}
}
private String getStringForLoopId(int loopId){
for(String appId : getMyApplications().keySet()){
MyApplication app = getMyApplications().get(appId);
for(AppLoop loop : app.getLoops()){
if(loop.getLoopId() == loopId)
return loop.getModules().toString();
}
}
return null;
}
private void printTimeDetails() {
System.out.println("=========================================");
System.out.println("============== RESULTS ==================");
System.out.println("=========================================");
System.out.println("EXECUTION TIME : "+ (Calendar.getInstance().getTimeInMillis() - TimeKeeper.getInstance().getSimulationStartTime()));
System.out.println("=========================================");
System.out.println("APPLICATION LOOP DELAYS");
System.out.println("=========================================");
for(Integer loopId : TimeKeeper.getInstance().getLoopIdToTupleIds().keySet()){
System.out.println(getStringForLoopId(loopId) + " ---> "+TimeKeeper.getInstance().getLoopIdToCurrentAverage().get(loopId));
}
System.out.println("=========================================");
System.out.println("TUPLE CPU EXECUTION DELAY");
System.out.println("=========================================");
for(String tupleType : TimeKeeper.getInstance().getTupleTypeToAverageCpuTime().keySet()){
System.out.println(tupleType + " ---> "+TimeKeeper.getInstance().getTupleTypeToAverageCpuTime().get(tupleType));
}
System.out.println("=========================================");
}
protected void manageResources(){
send(getId(), Config.RESOURCE_MANAGE_INTERVAL, FogEvents.CONTROLLER_RESOURCE_MANAGE);
}
private void processTupleFinished(SimEvent ev) {
}
@Override
public void shutdownEntity() {
}
public void submitApplication(MyApplication application, int delay, MyModulePlacement modulePlacement){
FogUtils.appIdToGeoCoverageMap.put(application.getAppId(), application.getGeoCoverage());
getMyApplications().put(application.getAppId(), application);
getAppLaunchDelays().put(application.getAppId(), delay);
getAppModulePlacementPolicy().put(application.getAppId(), modulePlacement);
for(MySensor sensor : sensors){
sensor.setApp(getMyApplications().get(sensor.getAppId()));
}
for(MyActuator ac : actuators){
ac.setApp(getMyApplications().get(ac.getAppId()));
}
for(AppEdge edge : application.getEdges()){
if(edge.getEdgeType() == AppEdge.ACTUATOR){
String moduleName = edge.getSource();
for(MyActuator actuator : getMyActuators()){
if(actuator.getMyActuatorType().equalsIgnoreCase(edge.getDestination()))
application.getModuleByName(moduleName).subscribeActuator(actuator.getId(), edge.getTupleType());
}
}
}
}
public void submitApplication(MyApplication application, MyModulePlacement modulePlacement){
submitApplication(application, 0, modulePlacement);
}
private void processAppSubmit(SimEvent ev){
MyApplication app = (MyApplication) ev.getData();
processAppSubmit(app);
}
private void processAppSubmit(MyApplication application){
System.out.println(CloudSim.clock()+" Submitted application "+ application.getAppId());
FogUtils.appIdToGeoCoverageMap.put(application.getAppId(), application.getGeoCoverage());
getMyApplications().put(application.getAppId(), application);
MyModulePlacement modulePlacement = getAppModulePlacementPolicy().get(application.getAppId());
for(MyFogDevice fogDevice : fogDevices){
sendNow(fogDevice.getId(), FogEvents.ACTIVE_APP_UPDATE, application);
}
Map<Integer, List<AppModule>> deviceToModuleMap = modulePlacement.getDeviceToModuleMap();
for(Integer deviceId : deviceToModuleMap.keySet()){
for(AppModule module : deviceToModuleMap.get(deviceId)){
sendNow(deviceId, FogEvents.APP_SUBMIT, application);
System.out.println(CloudSim.clock()+" Trying to Launch "+ module.getName() + " in "+getMyFogDeviceById(deviceId).getName());
sendNow(deviceId, FogEvents.LAUNCH_MODULE, module);
}
}
}
public List<MyFogDevice> getMyFogDevices() {
return fogDevices;
}
public void setMyFogDevices(List<MyFogDevice> fogDevices) {
this.fogDevices = fogDevices;
}
public Map<String, Integer> getAppLaunchDelays() {
return appLaunchDelays;
}
public void setAppLaunchDelays(Map<String, Integer> appLaunchDelays) {
this.appLaunchDelays = appLaunchDelays;
}
public Map<String, MyApplication> getMyApplications() {
return applications;
}
public void setMyApplications(Map<String, MyApplication> applications) {
this.applications = applications;
}
public List<MySensor> getMySensors() {
return sensors;
}
public void setMySensors(List<MySensor> sensors) {
for(MySensor sensor : sensors)
sensor.setControllerId(getId());
this.sensors = sensors;
}
public List<MyActuator> getMyActuators() {
return actuators;
}
public void setMyActuators(List<MyActuator> actuators) {
this.actuators = actuators;
}
public Map<String, MyModulePlacement> getAppModulePlacementPolicy() {
return appModulePlacementPolicy;
}
public void setAppModulePlacementPolicy(Map<String, MyModulePlacement> appModulePlacementPolicy) {
this.appModulePlacementPolicy = appModulePlacementPolicy;
}
}

View File

@@ -0,0 +1,168 @@
package org.fog.placement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.core.CloudSim;
import org.fog.application.AppModule;
import org.fog.application.MyApplication;
import org.fog.entities.MyActuator;
import org.fog.entities.MyFogDevice;
import org.fog.entities.MySensor;
public class MyModulePlacement extends MyPlacement{
protected ModuleMapping moduleMapping;
protected List<MySensor> sensors;
protected List<MyActuator> actuators;
protected String moduleToPlace;
protected Map<Integer, Integer> deviceMipsInfo;
public MyModulePlacement(List<MyFogDevice> fogDevices, List<MySensor> sensors, List<MyActuator> actuators,
MyApplication application, ModuleMapping moduleMapping, String moduleToPlace){
this.setMyFogDevices(fogDevices);
this.setMyApplication(application);
this.setModuleMapping(moduleMapping);
this.setModuleToDeviceMap(new HashMap<String, List<Integer>>());
this.setDeviceToModuleMap(new HashMap<Integer, List<AppModule>>());
setMySensors(sensors);
setMyActuators(actuators);
this.moduleToPlace = moduleToPlace;
this.deviceMipsInfo = new HashMap<Integer, Integer>();
mapModules();
}
@Override
protected void mapModules() {
for(String deviceName : getModuleMapping().getModuleMapping().keySet()){
for(String moduleName : getModuleMapping().getModuleMapping().get(deviceName)){
int deviceId = CloudSim.getEntityId(deviceName);
AppModule appModule = getMyApplication().getModuleByName(moduleName);
if(!getDeviceToModuleMap().containsKey(deviceId))
{
List<AppModule>placedModules = new ArrayList<AppModule>();
placedModules.add(appModule);
getDeviceToModuleMap().put(deviceId, placedModules);
}
else
{
List<AppModule>placedModules = getDeviceToModuleMap().get(deviceId);
placedModules.add(appModule);
getDeviceToModuleMap().put(deviceId, placedModules);
}
}
}
for(MyFogDevice device:getMyFogDevices())
{
int deviceParent = -1;
List<Integer>children = new ArrayList<Integer>();
if(device.getLevel()==1)
{
if(!deviceMipsInfo.containsKey(device.getId()))
deviceMipsInfo.put(device.getId(), 0);
deviceParent = device.getParentId();
for(MyFogDevice deviceChild:getMyFogDevices())
{
if(deviceChild.getParentId()==device.getId())
{
children.add(deviceChild.getId());
}
}
Map<Integer, Double>childDeadline = new HashMap<Integer, Double>();
for(int childId:children)
childDeadline.put(childId,getMyApplication().getDeadlineInfo().get(childId).get(moduleToPlace));
List<Integer> keys = new ArrayList<Integer>(childDeadline.keySet());
for(int i = 0; i<keys.size()-1; i++)
{
for(int j=0;j<keys.size()-i-1;j++)
{
if(childDeadline.get(keys.get(j))>childDeadline.get(keys.get(j+1)))
{
int tempJ = keys.get(j);
int tempJn = keys.get(j+1);
keys.set(j, tempJn);
keys.set(j+1, tempJ);
}
}
}
int baseMipsOfPlacingModule = (int)getMyApplication().getModuleByName(moduleToPlace).getMips();
for(int key:keys)
{
int currentMips = deviceMipsInfo.get(device.getId());
AppModule appModule = getMyApplication().getModuleByName(moduleToPlace);
int additionalMips = getMyApplication().getAdditionalMipsInfo().get(key).get(moduleToPlace);
if(currentMips+baseMipsOfPlacingModule+additionalMips<device.getMips())
{
currentMips = currentMips+baseMipsOfPlacingModule+additionalMips;
deviceMipsInfo.put(device.getId(), currentMips);
if(!getDeviceToModuleMap().containsKey(device.getId()))
{
List<AppModule>placedModules = new ArrayList<AppModule>();
placedModules.add(appModule);
getDeviceToModuleMap().put(device.getId(), placedModules);
}
else
{
List<AppModule>placedModules = getDeviceToModuleMap().get(device.getId());
placedModules.add(appModule);
getDeviceToModuleMap().put(device.getId(), placedModules);
}
}
else
{
List<AppModule>placedModules = getDeviceToModuleMap().get(deviceParent);
placedModules.add(appModule);
getDeviceToModuleMap().put(deviceParent, placedModules);
}
}
}
}
}
public ModuleMapping getModuleMapping() {
return moduleMapping;
}
public void setModuleMapping(ModuleMapping moduleMapping) {
this.moduleMapping = moduleMapping;
}
public List<MySensor> getMySensors() {
return sensors;
}
public void setMySensors(List<MySensor> sensors) {
this.sensors = sensors;
}
public List<MyActuator> getMyActuators() {
return actuators;
}
public void setMyActuators(List<MyActuator> actuators) {
this.actuators = actuators;
}
}

View File

@@ -0,0 +1,124 @@
package org.fog.placement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.core.CloudSim;
import org.fog.application.AppModule;
import org.fog.application.MyApplication;
import org.fog.entities.MyFogDevice;
public abstract class MyPlacement {
public static int ONLY_CLOUD = 1;
public static int EDGEWARDS = 2;
public static int USER_MAPPING = 3;
private List<MyFogDevice> fogDevices;
private MyApplication application;
private Map<String, List<Integer>> moduleToDeviceMap;
private Map<Integer, List<AppModule>> deviceToModuleMap;
private Map<Integer, Map<String, Integer>> moduleInstanceCountMap;
protected abstract void mapModules();
protected boolean canBeCreated(MyFogDevice fogDevice, AppModule module){
return fogDevice.getVmAllocationPolicy().allocateHostForVm(module);
}
protected int getParentDevice(int fogDeviceId){
return ((MyFogDevice)CloudSim.getEntity(fogDeviceId)).getParentId();
}
protected MyFogDevice getMyFogDeviceById(int fogDeviceId){
return (MyFogDevice)CloudSim.getEntity(fogDeviceId);
}
protected boolean createModuleInstanceOnDevice(AppModule _module, final MyFogDevice device, int instanceCount){
return false;
}
protected boolean createModuleInstanceOnDevice(AppModule _module, final MyFogDevice device){
AppModule module = null;
if(getModuleToDeviceMap().containsKey(_module.getName()))
module = new AppModule(_module);
else
module = _module;
if(canBeCreated(device, module)){
System.out.println("Creating "+module.getName()+" on device "+device.getName());
if(!getDeviceToModuleMap().containsKey(device.getId()))
getDeviceToModuleMap().put(device.getId(), new ArrayList<AppModule>());
getDeviceToModuleMap().get(device.getId()).add(module);
if(!getModuleToDeviceMap().containsKey(module.getName()))
getModuleToDeviceMap().put(module.getName(), new ArrayList<Integer>());
getModuleToDeviceMap().get(module.getName()).add(device.getId());
return true;
} else {
System.err.println("Module "+module.getName()+" cannot be created on device "+device.getName());
System.err.println("Terminating");
return false;
}
}
protected MyFogDevice getDeviceByName(String deviceName) {
for(MyFogDevice dev : getMyFogDevices()){
if(dev.getName().equals(deviceName))
return dev;
}
return null;
}
protected MyFogDevice getDeviceById(int id){
for(MyFogDevice dev : getMyFogDevices()){
if(dev.getId() == id)
return dev;
}
return null;
}
public List<MyFogDevice> getMyFogDevices() {
return fogDevices;
}
public void setMyFogDevices(List<MyFogDevice> fogDevices) {
this.fogDevices = fogDevices;
}
public MyApplication getMyApplication() {
return application;
}
public void setMyApplication(MyApplication application) {
this.application = application;
}
public Map<String, List<Integer>> getModuleToDeviceMap() {
return moduleToDeviceMap;
}
public void setModuleToDeviceMap(Map<String, List<Integer>> moduleToDeviceMap) {
this.moduleToDeviceMap = moduleToDeviceMap;
}
public Map<Integer, List<AppModule>> getDeviceToModuleMap() {
return deviceToModuleMap;
}
public void setDeviceToModuleMap(Map<Integer, List<AppModule>> deviceToModuleMap) {
this.deviceToModuleMap = deviceToModuleMap;
}
public Map<Integer, Map<String, Integer>> getModuleInstanceCountMap() {
return moduleInstanceCountMap;
}
public void setModuleInstanceCountMap(Map<Integer, Map<String, Integer>> moduleInstanceCountMap) {
this.moduleInstanceCountMap = moduleInstanceCountMap;
}
}