Description: To perform VM migration in CloudSim, identify the VM and its current host, then move the VM to a new host within the datacenter. Remove the VM from the old host and add it to the new one, ensuring smooth migration without interrupting other tasks. After migration, the VM continues execution on the new host, processing the tasks assigned to it. This process helps with load balancing and resource optimization in cloud environments.
Sample Code
import java.text.DecimalFormat;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
public class VMMigrationExample {
private static List vmlist;
private static List cloudletList;
public static void main(String[] args) {
Log.printLine("Starting VM Migration Example...");
try {
int num_user = 1; // Number of cloud users
Calendar calendar = Calendar.getInstance(); // Current date and time
boolean trace_flag = false; // Enable trace
CloudSim.init(num_user, calendar, trace_flag);
// Create Datacenter
Datacenter datacenter = createDatacenter("Datacenter_0");
// Create Broker
DatacenterBroker broker = createBroker();
int brokerId = broker.getId();
// Create Virtual Machines (VMs)
vmlist = new ArrayList<>();
int vmid = 0;
int mips = 1000;
int ram = 2048; // 2 GB
long storage = 100000; // 100 GB
int bw = 1000; // 1 Gbps
int pesNumber = 1;
String vmm = "Xen";
Vm vm1 = new Vm(vmid++, brokerId, mips, pesNumber, ram, bw, storage, vmm, new CloudletSchedulerTimeShared());
Vm vm2 = new Vm(vmid++, brokerId, mips, pesNumber, ram, bw, storage, vmm, new CloudletSchedulerTimeShared());
vmlist.add(vm1);
vmlist.add(vm2);
// Submit VMs to the broker
broker.submitVmList(vmlist);
// Create Cloudlets
cloudletList = new ArrayList<>();
int cloudletId = 0;
long length = 40000; // Cloudlet length
long fileSize = 300; // File size (MB)
long outputSize = 300; // Output size (MB)
for (int i = 0; i < 5; i++) {
Cloudlet cloudlet = new Cloudlet(cloudletId++, length, pesNumber, fileSize, outputSize, null, null, null);
cloudlet.setUserId(brokerId);
cloudlet.setVmId(vm1.getId()); // Assign to VM1
cloudletList.add(cloudlet);
}
// Submit Cloudlets to the broker
broker.submitCloudletList(cloudletList);
// Start the simulation
CloudSim.startSimulation();
// Retrieve results
List newList = broker.getCloudletReceivedList();
printCloudletList(newList);
// Perform VM Migration (e.g., migrating VM1 from one host to another)
Vm vmToMigrate = vmlist.get(0); // Get VM1
migrateVm(datacenter, vmToMigrate);
Log.printLine("VM Migration completed.");
CloudSim.stopSimulation();
Log.printLine("VM Migration Example finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("An error occurred while performing VM migration.");
}
}
/**
* Creates the datacenter.
*/
private static Datacenter createDatacenter(String name) {
List hostList = new ArrayList<>();
int mips = 1000;
int ram = 2048;
long storage = 100000; // 100 GB
int bw = 1000; // 1 Gbps
// Create a PE list
List peList = new ArrayList<>();
peList.add(new Pe(0, new PeProvisionerSimple(mips)));
// Create a host with 1 core, 2 GB of RAM, 100 GB of storage, and 1 Gbps bandwidth
Host host = new Host(0, new RamProvisionerSimple(ram), new BwProvisionerSimple(bw), storage, peList, new VmSchedulerTimeShared(peList));
hostList.add(host);
String arch = "x86"; // OS architecture
String os = "Linux"; // Operating system
String vmm = "Xen"; // VM Monitor
double timeZone = 10.0;
double cost = 3.0;
double costPerMem = 0.05;
double costPerStorage = 0.001;
double costPerBw = 0.0;
LinkedList storageList = new LinkedList<>();
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), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
/**
* Creates the broker.
*/
private static DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
}
return broker;
}
/**
* Prints the cloudlet list.
*/
private static void printCloudletList(List list) {
String format = "%-15s%-10s%-10s%-15s%-15s%-15s%n";
System.out.printf(format, "Cloudlet ID", "Status", "VM ID", "Exec Time", "Start Time", "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (Cloudlet cloudlet : list) {
String status = cloudlet.getStatus() == Cloudlet.SUCCESS ? "SUCCESS" : "FAILED";
System.out.printf(format,
cloudlet.getCloudletId(),
status,
cloudlet.getVmId(),
dft.format(cloudlet.getActualCPUTime()),
dft.format(cloudlet.getExecStartTime()),
dft.format(cloudlet.getFinishTime())
);
}
}
/**
* Migrates a VM from one host to another.
*/
private static void migrateVm(Datacenter datacenter, Vm vmToMigrate) {
// Get the current host of the VM
Host currentHost = null;
for (Host host : datacenter.getHostList()) {
if (host.getVmList().contains(vmToMigrate)) {
currentHost = host;
break;
}
}
if (currentHost != null) {
// Remove VM from the current host
currentHost.removeMigratingInVm(vmToMigrate);
// Choose a new host (e.g., from the datacenter's host list)
Host newHost = datacenter.getHostList().get(1); // Just as an example, pick the next host in the list
// Assign the VM to the new host
newHost.addMigratingInVm(vmToMigrate);
Log.printLine("VM " + vmToMigrate.getId() + " migrated from Host " + currentHost.getId() + " to Host " + newHost.getId());
} else {
Log.printLine("VM " + vmToMigrate.getId() + " not found in any host.");
}
}
}