Description: To change the VM allocation policy in CloudSim, extend the VmAllocationPolicySimple class and override the allocateHostForVm method to define your custom allocation logic. You can base the allocation on factors like CPU utilization, memory, or bandwidth. Once your custom policy is created, apply it by passing an instance of it when creating the datacenter, replacing the default policy.
Sample Code
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.lists.*;
import org.cloudbus.cloudsim.provisioners.*;
import java.util.*;
class CustomVmAllocationPolicy extends VmAllocationPolicySimple {
// Constructor
public CustomVmAllocationPolicy(List? extends Host hostList){
super(hostList);
}
// Override the allocateHostForVm method to implement custom allocation policy
@Override
public boolean allocateHostForVm(Vm vm) {
// Get the list of hosts
List? extends Host hostList = getHostList();
int size = hostList.size();
// Arrays to hold average CPU utilization for each host
double[] avgCPU = new double[size];
// Iterate through the hosts and calculate average CPU utilization
for (int i = 0; i < size; i++) {
Host host = hostList.get(i);
double availableMips = host.getAvailableMips(); // Available CPU power (MIPS)
double totalMips = host.getTotalMips(); // Total CPU power (MIPS)
avgCPU[i] = ((totalMips - availableMips) / totalMips) * 100; // CPU utilization percentage
}
// Find the host with the minimum average CPU utilization
double minCPU = Arrays.stream(avgCPU).min().getAsDouble();
int bestHostIndex = -1;
for (int i = 0; i < size; i++) {
if (avgCPU[i] == minCPU) {
bestHostIndex = i;
break;
}
}
// Get the host with the lowest CPU utilization
Host bestHost = hostList.get(bestHostIndex);
// Try to allocate the VM to the selected host
boolean result = bestHost.vmCreate(vm);
if (result) {
// Update the internal data structures if the VM is successfully allocated
getVmTable().put(vm.getUid(), bestHost);
return true;
}
return false;
}
}
public class VmAllocationExample {
public static void main(String[] args) {
try {
// Initialize CloudSim
int numUsers = 1; // number of users
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false; // turn off tracing
CloudSim.init(numUsers, calendar, traceFlag);
// Create Datacenter
Datacenter datacenter = createDatacenter("Datacenter_1");
// Create Broker
DatacenterBroker broker = new DatacenterBroker("Broker_1");
// Create VM List
List vmlist = new ArrayList<>();
int vmid = 0;
int mips = 1000;
long size = 10000; // image size (MB)
int ram = 512; // VM memory (MB)
long bw = 1000; // bandwidth
int pesNumber = 1; // number of CPUs
Vm vm = new Vm(vmid, broker.getId(), mips, pesNumber, ram, bw, size, "Xen", new CloudletSchedulerTimeShared());
vmlist.add(vm);
broker.submitVmList(vmlist);
// Create Cloudlets
List cloudletList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Cloudlet cloudlet = new Cloudlet(
i, 20000, pesNumber, 300, 300,
new UtilizationModelFull(), new UtilizationModelFull(), new UtilizationModelFull()
);
cloudlet.setUserId(broker.getId());
cloudletList.add(cloudlet);
}
broker.submitCloudletList(cloudletList);
// Start simulation
CloudSim.startSimulation();
// Stop simulation
CloudSim.stopSimulation();
// Display Results
for (Cloudlet cloudlet : broker.getCloudletReceivedList()) {
System.out.println("Cloudlet ID: " + cloudlet.getCloudletId() + " Status: " + cloudlet.getStatus());
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Create Datacenter
private static Datacenter createDatacenter(String name) throws Exception {
List hostList = new ArrayList<>();
// Create Hosts
int hostId = 0;
int ram = 2048; // host memory (MB)
long storage = 1000000; // host storage
int bw = 10000; // bandwidth
int pesNumber = 1; // number of CPUs
List peList = new ArrayList<>();
for (int i = 0; i < pesNumber; i++) {
peList.add(new Pe(i, new PeProvisionerSimple(1000))); // 1000 MIPS per PE
}
Host host = new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
);
hostList.add(host);
// Create Datacenter Characteristics
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
double timeZone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this resource
double costPerStorage = 0.001; // the cost of using storage in this resource
double costPerBw = 0.0; // the cost of using bandwidth in this resource
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, timeZone, cost, costPerMem, costPerStorage, costPerBw
);
// Create the Datacenter with the custom VM allocation policy
CustomVmAllocationPolicy vmAllocationPolicy = new CustomVmAllocationPolicy(hostList);
return new Datacenter(name, characteristics, vmAllocationPolicy, new ArrayList<>(), 0);
}
}
Steps
Step 1: Understand the Default VM Allocation Policy Familiarize yourself with the default VmAllocationPolicySimple class which is responsible for the current VM allocation strategy in CloudSim.
Step 2: Create a New Class to Extend VmAllocationPolicySimple Create a new class that extends the VmAllocationPolicySimple class or any other policy class provided by CloudSim.
Step 3: Override the allocateHostForVm() Method Override the allocateHostForVm() method in your new class. This method defines the logic for assigning a VM to a host. Implement your custom allocation logic based on your requirements (e.g., CPU, memory, bandwidth utilization).
Step 4: Implement Allocation Strategy In the overridden method, add your custom logic to choose which host a VM should be allocated to. You can base this on factors such as available resources, load balancing, or any other metric of interest.
Step 5: Create Datacenter Using Your Custom VM Allocation Policy While creating the Datacenter, use your custom VM allocation policy instead of the default one. Ensure that you pass the host list and your custom allocation policy to the Datacenter constructor.
Step 6: Adjust Other Methods (Optional) Optionally, override additional methods like deallocateHostForVm() if you want to customize the logic for VM deallocation.
Step 7: Test Your Custom Allocation Policy After implementing the custom policy, run a simulation to test if the allocation is working as expected. Use debugging and logging to inspect the VM allocation behavior.
Step 8: Refine and Optimize the Allocation Logic Based on the test results, fine-tune your allocation strategy to meet performance or efficiency goals. You may need to adjust how resources are prioritized or modify the selection of hosts.
Step 9: Finalize the Simulation Setup After your custom VM allocation policy is working, finalize your simulation by creating VMs, assigning tasks, and running the simulation.