Where Do I Implement New VmAllocationPolicy in EdgeCloudSim?
Share
Condition for Implement New VmAllocationPolicy in EdgeCloudSim
Description: To implement a new VmAllocationPolicy in EdgeCloudSim, create a custom class that extends existing policies like VmAllocationPolicySimple or VmAllocationPolicyBestFit. Implement methods such as allocateVmToHost and deallocateVmFromHost to add custom logic for resource allocation. Integrate the new policy into the simulation by assigning it to the VmAllocationPolicy parameter during setup. This ensures the new policy is used to allocate VMs to hosts during the simulation.
Sample Code
EdgeVMAllocationPolicy.java: package EdgeApplication;
import edu.boun.edgecloudsim.core.ScenarioFactory;
import edu.boun.edgecloudsim.core.SimManager;
import edu.boun.edgecloudsim.core.SimSettings;
import edu.boun.edgecloudsim.utils.SimLogger;
import edu.boun.edgecloudsim.utils.SimUtils;
import org.cloudbus.cloudsim.core.CloudSim;
import java.util.Calendar;
public class EdgeVMAllocationPolicy {
public static void main(String[] args) {
SimLogger.enablePrintLog();
String configFile = "scripts/Edge_Application/config/default_config.properties";
String applicationsFile = "scripts/Edge_Application/config/applications.xml";
String edgeDevicesFile = "scripts/Edge_Application/config/edge_devices.xml";
String outputFolder = "sim_results/";
int iterationNumber = 1;
SimSettings SS = SimSettings.getInstance();
if (!SS.initialize(configFile, edgeDevicesFile, applicationsFile)) {
SimLogger.printLine("Cannot initialize simulation settings!");
System.exit(0);
}
SimLogger.enableFileLog();
SimUtils.cleanOutputFolder(outputFolder);
try {
// Initialize CloudSim
int num_user = 2;
Calendar calendar = Calendar.getInstance();
CloudSim.init(num_user, calendar, false, 0.01);
// Use the updated SampleScenarioFactory
ScenarioFactory factory = new SampleScenarioFactory(
SS.getMinNumOfMobileDev(),
SS.getSimulationTime(),
SS.getOrchestratorPolicies()[0],
SS.getSimulationScenarios()[0]
);
// Start simulation with the custom factory
SimManager manager = new SimManager(factory, SS.getMinNumOfMobileDev(),
SS.getSimulationScenarios()[0], SS.getOrchestratorPolicies()[0]);
manager.startSimulation();
} catch (Exception e) {
SimLogger.printLine("Simulation error: " + e.getMessage());
e.printStackTrace();
}
}
}
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Vm;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.VmAllocationPolicy;
public class CustomEdgeVmAllocationPolicy extends VmAllocationPolicy {
private Map vmTable; // Keeps track of which host each VM is allocated to
private int dataCenterIndex;
private int createdVmNum;
public CustomEdgeVmAllocationPolicy(List? extends Host list, int dataCenterIndex) {
super(list);
this.vmTable = new HashMap<>();
this.dataCenterIndex = dataCenterIndex;
this.createdVmNum = 0;
}
@Override
public boolean allocateHostForVm(Vm vm) {
boolean result = false;
// Check if VM is not already allocated and is an EdgeVM
if (!vmTable.containsKey(vm.getUid())) {
for (Host host : getHostList()) {
// Check if the host has enough resources for the VM
if (host.isSuitableForVm(vm)) {
result = host.vmCreate(vm); // Allocate VM to host
if (result) {
vmTable.put(vm.getUid(), host); // Update VM table
createdVmNum++;
break;
}
}
}
}
return result;
}
@Override
public void deallocateHostForVm(Vm vm) {
// Deallocate VM from host
Host host = vmTable.remove(vm.getUid());
if (host != null) {
host.vmDestroy(vm);
}
}
}
package EdgeApplication;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Vm;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.VmAllocationPolicy;
public class CustomEdgeVmAllocationPolicy extends VmAllocationPolicy {
private Map vmTable; // Keeps track of which host each VM is allocated to
private int dataCenterIndex;
private int createdVmNum;
public CustomEdgeVmAllocationPolicy(List? extends Host list, int dataCenterIndex) {
super(list);
this.vmTable = new HashMap<>();
this.dataCenterIndex = dataCenterIndex;
this.createdVmNum = 0;
}
@Override
public boolean allocateHostForVm(Vm vm) {
boolean result = false;
// Check if VM is not already allocated and is an EdgeVM
if (!vmTable.containsKey(vm.getUid())) {
for (Host host : getHostList()) {
// Check if the host has enough resources for the VM
if (host.isSuitableForVm(vm)) {
result = host.vmCreate(vm); // Allocate VM to host
if (result) {
vmTable.put(vm.getUid(), host); // Update VM table
createdVmNum++;
break;
}
}
}
}
return result;
}
@Override
public void deallocateHostForVm(Vm vm) {
// Deallocate VM from host
Host host = vmTable.remove(vm.getUid());
if (host != null) {
host.vmDestroy(vm);
}
}