Condition for Perform Load Balancing using CloudSim
Description: Load balancing in CloudSim is the process of distributing the computational workload evenly across multiple virtual machines (VMs) or physical hosts to optimize resource utilization and reduce task execution time. To achieve load balancing, you can implement various algorithms within CloudSim, focusing on strategies like dynamic workload distribution, VM migration, and resource allocation.
Sample Code
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.*;
import java.util.*;
public class LoadBalancingExample {
public static void main(String[] args) {
try {
// Initialize CloudSim
int numUsers = 1; // Number of cloud users
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false; // No event tracing
CloudSim.init(numUsers, calendar, traceFlag);
// Step 1: Create Datacenter
Datacenter datacenter = createDatacenter("Datacenter_0");
// Step 2: Create Broker
DatacenterBroker broker = new DatacenterBroker("Broker_0");
int brokerId = broker.getId();
// Step 3: Create VMs
List vmlist = createVM(brokerId, 5); // Create 5 VMs
broker.submitVmList(vmlist);
// Step 4: Create Cloudlets
List cloudletList = createCloudlet(brokerId, 10); // Create 10 cloudlets
broker.submitCloudletList(cloudletList);
// Step 5: Start Simulation
CloudSim.startSimulation();
// Step 6: Apply Load Balancing (Round Robin)
roundRobinLoadBalancing(vmlist, cloudletList);
// Step 7: Stop Simulation
List newList = broker.getCloudletReceivedList();
CloudSim.stopSimulation();
// Print Results
for (Cloudlet cloudlet : newList) {
System.out.println("Cloudlet " + cloudlet.getCloudletId() +
" executed on VM " + cloudlet.getVmId() +
" with status " + cloudlet.getStatus());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Datacenter createDatacenter(String name) throws Exception {
List hostList = new ArrayList<>();
// Host Configuration
int hostId = 0;
int ram = 20480; // 20 GB RAM
long storage = 1000000; // 1 TB Storage
int bw = 10000; // 10 GB Bandwidth
int cores = 8; // Number of cores
int mips = 10000; // MIPS
// Create a list of processing elements (PEs)
List peList = new ArrayList<>();
for (int i = 0; i < cores; i++) {
peList.add(new Pe(i, new PeProvisionerSimple(mips))); // Create cores
}
// Create a Host
hostList.add(new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
));
// Create a Datacenter with the host list
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, 10.0, 3.0, 0.05, 0.001, 0.0
);
return new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), new LinkedList<>(), 0);
}
private static List createVM(int brokerId, int vmCount) {
List vmList = new ArrayList<>();
int mips = 1000;
int size = 10000; // 10 GB Storage
int ram = 2048; // 2 GB RAM
long bw = 1000; // 1 GB Bandwidth
int pesNumber = 1; // Number of cores
String vmm = "Xen";
for (int i = 0; i < vmCount; i++) {
Vm vm = new Vm(i, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
vmList.add(vm);
}
return vmList;
}
private static List createCloudlet(int brokerId, int cloudletCount) {
List cloudletList = new ArrayList<>();
long length = 40000; // Cloudlet length
long fileSize = 300; // File size
long outputSize = 300; // Output size
int pesNumber = 1; // Number of cores
UtilizationModel utilizationModel = new UtilizationModelFull();
for (int i = 0; i < cloudletCount; i++) {
Cloudlet cloudlet = new Cloudlet(i, length, pesNumber, fileSize, outputSize,
utilizationModel, utilizationModel, utilizationModel);
cloudlet.setUserId(brokerId);
cloudletList.add(cloudlet);
}
return cloudletList;
}
private static void roundRobinLoadBalancing(List vmlist, List cloudletList) {
// Round-robin load balancing of cloudlets to VMs
int vmIndex = 0;
// Assign each cloudlet to a VM in a round-robin manner
for (Cloudlet cloudlet : cloudletList) {
Vm vm = vmlist.get(vmIndex);
cloudlet.setVmId(vm.getId()); // Assign cloudlet to VM
System.out.println("Assigning Cloudlet " + cloudlet.getCloudletId() + " to VM " + vm.getId());
// Move to the next VM for the next cloudlet
vmIndex = (vmIndex + 1) % vmlist.size();
}
}
}