How to Migrate the Cloudlet from One VM to Another VM?
Share
Condition for Migrate the Cloudlet from One VM to Another VM
Description: Migrating a cloudlet from one VM to another in CloudSim involves several key steps to ensure proper task execution and state management. First, identify the cloudlet to be migrated based on criteria like load balancing or resource optimization. Pause or stop the cloudlet on the current VM by updating its status to PAUSED, and remove it from the VM's cloudlet execution list. Next, select a suitable destination VM that has sufficient resources and add the cloudlet to its scheduler. Update the cloudlet status to INEXE or SCHEDULED to resume its execution on the new VM. It is also important to notify relevant simulation entities, such as the DatacenterBroker, to keep resource allocations and cost calculations consistent. During migration, consider handling partial execution states and ensuring minimal performance impact on the simulation.
Sample Code
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Calendar;
import java.text.DecimalFormat;
import java.util.LinkedList;
public class CloudletMigrationExample {
public static void main(String[] args) {
try {
// Initialize CloudSim
int numUser = 1; // Number of users
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false; // Disable trace
CloudSim.init(numUser, calendar, traceFlag);
// Create Datacenter with custom VM allocation policy
Datacenter datacenter = createDatacenter("Datacenter1");
// Create Virtual Machines (VMs)
List vmList = new ArrayList<>();
vmList.add(createVm(0));
vmList.add(createVm(1));
// Create Cloudlets (jobs)
List cloudletList = new ArrayList<>();
cloudletList.add(createCloudlet(0));
cloudletList.add(createCloudlet(1));
// Create a Broker (to manage VM and Cloudlet scheduling)
DatacenterBroker broker = createBroker();
broker.submitVmList(vmList);
broker.submitCloudletList(cloudletList);
// Start the simulation
CloudSim.startSimulation();
// Retrieve the results (completed cloudlets)
List finishedCloudlets = broker.getCloudletReceivedList();
printCloudletList(finishedCloudlets);
// Cloudlet Migration: Move the first cloudlet from VM 0 to VM 1
migrateCloudlet(broker, cloudletList, vmList.get(0), vmList.get(1));
// Start the simulation again after migration
CloudSim.startSimulation();
// Retrieve the results after migration
List finishedCloudletsAfterMigration = broker.getCloudletReceivedList();
printCloudletList(finishedCloudletsAfterMigration);
// Stop the simulation
CloudSim.stopSimulation();
System.out.println("CloudSim simulation finished!");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates the datacenter with a simple VM allocation policy.
*
* @param name the name of the datacenter
* @return the created datacenter
*/
private static Datacenter createDatacenter(String name) {
// Create a list to store the hosts
List hostList = new ArrayList<>();
// Create a list to store the PEs (processing elements or CPUs)
List peList = new ArrayList<>();
int mips = 1000;
// Add a PE (CPU) to the list
peList.add(new Pe(0, new PeProvisionerSimple(mips)));
// Create a host and add it to the host list
int hostId = 0;
int ram = 2048; // Host memory (MB)
long storage = 1000000; // Host storage
int bw = 10000; // Host bandwidth
hostList.add(new Host(hostId, new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw), storage, peList,
new VmSchedulerTimeShared(peList)));
// Datacenter characteristics
String arch = "x86"; // System architecture
String os = "Linux"; // Operating system
String vmm = "Xen"; // Virtual Machine Monitor
double timeZone = 10.0; // Time zone of the datacenter
double cost = 3.0; // Cost of using the datacenter
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 {
VmAllocationPolicy policy = new VmAllocationPolicySimple(hostList);
datacenter = new Datacenter(name, characteristics, policy, storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
/**
* Creates a virtual machine (VM).
*
* @param vmId the ID of the VM
* @return the created VM
*/
private static Vm createVm(int vmId) {
int mips = 1000;
long size = 10000; // VM image size (MB)
int ram = 512; // VM memory (MB)
long bw = 1000;
int pesNumber = 1; // Number of CPUs
String vmm = "Xen"; // VMM name
CloudletScheduler cloudletScheduler = new CloudletSchedulerTimeShared(); // Use CloudletSchedulerTimeShared instead of VmSchedulerTimeShared
// Create the VM using the Vm constructor that accepts a CloudletScheduler
Vm vm = new Vm(vmId, 0, mips, pesNumber, ram, bw, size, vmm, cloudletScheduler);
return vm;
}
/**
* Creates a cloudlet (job).
*
* @param id the ID of the cloudlet
* @return the created cloudlet
*/
private static Cloudlet createCloudlet(int id) {
long length = 400000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet = new Cloudlet(id, length, 1, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet.setUserId(0);
cloudlet.setVmId(id);
return cloudlet;
}
/**
* Creates a broker to manage the virtual machines and cloudlets.
*
* @return the created broker
*/
private static DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
}
return broker;
}
/**
* Migrates a cloudlet from one VM to another.
*
* @param broker the DatacenterBroker managing the cloudlets and VMs
* @param cloudlet the cloudlet to be migrated
* @param sourceVm the source VM
* @param targetVm the target VM
*/
/**
* Prints the Cloudlet objects.
*
* @param list the list of Cloudlets
*/
private static void printCloudletList(List list) {
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
System.out.println();
System.out.println("========== OUTPUT ==========");
System.out.println("Cloudlet ID" + indent + "STATUS" + indent
+ "Data center ID" + indent + "VM ID" + indent + "Time" + indent
+ "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
System.out.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
System.out.print("SUCCESS");
System.out.println(indent + indent + cloudlet.getResourceId()
+ indent + indent + indent + cloudlet.getVmId()
+ indent + indent
+ dft.format(cloudlet.getActualCPUTime()) + indent
+ indent + dft.format(cloudlet.getExecStartTime())
+ indent + indent
+ dft.format(cloudlet.getFinishTime()));
}
}
}
private static void migrateCloudlet(DatacenterBroker broker, List list, Vm sourceVm, Vm targetVm) {
Cloudlet cloudlet;
int size = list.size();
DecimalFormat dft = new DecimalFormat("###.##");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
// System.out.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
System.out.print("SUCCESS");
/* System.out.println(indent + indent + cloudlet.getResourceId()
+ indent + indent + indent + cloudlet.getVmId()
+ indent + indent
+ dft.format(cloudlet.getActualCPUTime()) + indent
+ indent + dft.format(cloudlet.getExecStartTime())
+ indent + indent
+ dft.format(cloudlet.getFinishTime()));
}*/
}
// Terminate the cloudlet on the source VM
sourceVm.getCloudletScheduler().cloudletCancel(cloudlet.getCloudletId());
// Submit the cloudlet to the target VM
cloudlet.setVmId(targetVm.getId());
broker.submitCloudletList(list);
System.out.println("Cloudlet " + cloudlet.getCloudletId() + " migrated from VM " + sourceVm.getId() + " to VM " + targetVm.getId());
}
}
}