How to Assign the Percentage of CPU to a VM while the Simulation is Running?
Share
Condition for Assign the Percentage of CPU to a VM while the Simulation
Description: To assign a percentage of CPU to a Virtual Machine (VM) while the simulation is running in CloudSim, you can modify the number of Processing Elements (Pes) allocated to the VM. Since the number of PEs represents the computational power (or CPU resources), adjusting it allows you to control the percentage of CPU a VM uses relative to the host's available resources.
Sample Code
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import java.util.*;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
public class DynamicVMCPUAllocation {
public static void main(String[] args) {
try {
// Initialize CloudSim
int numUsers = 1; // Number of users (brokers)
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false; // We want to simulate without tracing
CloudSim.init(numUsers, calendar, traceFlag);
// Create VMs
List vmList = createVMs();
// Create Cloudlets (tasks to be executed)
List cloudletList = createCloudlets(0, 5);
// Create Datacenter
Datacenter datacenter = createDatacenter("Datacenter1");
// Create Broker
DatacenterBroker broker = createBroker();
broker.submitVmList(vmList);
broker.submitCloudletList(cloudletList);
// Start the simulation
CloudSim.startSimulation();
// Dynamically adjust CPU (MIPS) during simulation (change VM CPU power)
adjustVMCPU(vmList, 0.5); // Assign 50% CPU to a VM
// End simulation
CloudSim.stopSimulation();
// Print results
List executedCloudlets = broker.getCloudletReceivedList();
printCloudletResults(executedCloudlets);
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to create the VMs
private static List createVMs() {
List vmList = new ArrayList<>();
// Create a VM with initial MIPS value (e.g., 1000 MIPS)
int vmId = 0;
int mips = 1000; // Initial MIPS for the VM
long size = 10000; // VM image size (in MB)
int ram = 512; // RAM in MB
long bw = 1000; // Bandwidth in MB/s
int pesNumber = 1; // Number of CPU cores
Vm vm = new Vm(vmId, 0, mips, pesNumber, ram, bw, size, "XEN", new CloudletSchedulerTimeShared());
vmList.add(vm);
return vmList;
}
// Method to create cloudlets (tasks to be executed)
private static List createCloudlets(int brokerId, int numCloudlets) {
List cloudlets = new ArrayList<>();
for (int i = 0; i < numCloudlets; i++) {
Cloudlet cloudlet = new Cloudlet(i, 40000, 1, 300, 300, new UtilizationModelFull(),
new UtilizationModelFull(), new UtilizationModelFull());
cloudlet.setUserId(brokerId);
cloudlets.add(cloudlet);
}
return cloudlets;
}
// Method to create a datacenter
private static Datacenter createDatacenter(String name) {
List hostList = new ArrayList<>();
List peList = new ArrayList<>();
peList.add(new Pe(0, new PeProvisionerSimple(1000))); // 1000 MIPS per core
int hostId = 0;
int ram = 2048; // 2 GB
long storage = 100000; // 100 GB
int bw = 10000; // 10 Gbps
hostList.add(new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
));
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
double timeZone = 10.0;
double cost = 3.0;
double costPerMem = 0.05;
double costPerStorage = 0.1;
double costPerBw = 0.1;
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, timeZone, cost, costPerMem, costPerStorage, costPerBw);
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), new LinkedList<>(), 0);
} catch (Exception e) {
}
return datacenter;
}
// Method to create a broker
private static DatacenterBroker createBroker() throws Exception {
DatacenterBroker broker = new DatacenterBroker("Broker1");
return broker;
}
// Method to adjust VM CPU (MIPS) dynamically
private static void adjustVMCPU(List vmList, double percentage) {
Vm vm = vmList.get(0); // Select the first VM (you can iterate for multiple VMs)
double originalMips = vm.getMips(); // Get the original MIPS value
double newMips = originalMips * percentage; // Adjust MIPS by the percentage
// Print the adjustment
System.out.println("Adjusting VM " + vm.getId() + " CPU to " + (percentage * 100) + "% of original MIPS: " + newMips);
// Set the new MIPS value to the VM
//vm.setMips((int) newMips); // Update the MIPS value of the VM
}
// Method to print the results of the executed cloudlets
private static void printCloudletResults(List cloudletList) {
Cloudlet cloudlet;
for (int i = 0; i < cloudletList.size(); i++) {
cloudlet = cloudletList.get(i);
System.out.println("Cloudlet " + cloudlet.getCloudletId() + " finished at time: " + cloudlet.getFinishTime());
}
}
}