How to Cancel the Execution of a Particular Cloudlet on VM?
Share
Condition for Cancel the Execution of a Particular Cloudlet on VM
Description: To cancel the execution of a cloudlet on a VM in CloudSim, you need to interact with the VM's CloudletScheduler. By creating a custom CloudletScheduler, you can add a method (e.g., cancelCloudlet()) to check if the cloudlet is executing, remove it from the execution list, and set its status to CANCELED. If the cloudlet has finished execution, it is processed accordingly. This allows for dynamic cancellation of cloudlets during simulation.
Sample Code
import org.cloudbus.cloudsim.*;
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.List;
public class CancelCloudletExample {
public static void main(String[] args) {
try {
// Initialize CloudSim library
int numUsers = 1;
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false;
CloudSim.init(numUsers, calendar, traceFlag);
// Create Datacenter
Datacenter datacenter = createDatacenter("Datacenter_0");
// Create Broker
DatacenterBroker broker = new DatacenterBroker("Broker");
// Create VMs
List vmList = new ArrayList<>();
int vmId = 0;
int mips = 1000;
int pesNumber = 1; // Number of CPUs
int ram = 2048; // VM memory (MB)
long bw = 10000; // Bandwidth
long size = 10000; // Image size (MB)
String vmm = "Xen"; // VMM name
Vm vm = new CustomVm(vmId, broker.getId(), mips, pesNumber, ram, bw, size, vmm, new CustomCloudletSchedulerTimeShared());
vmList.add(vm);
broker.submitVmList(vmList);
// Create Cloudlets
List cloudletList = new ArrayList<>();
int cloudletId = 0;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
// Create 10 cloudlets
for (int i = 0; i < 10; i++) {
Cloudlet cloudlet = new Cloudlet(cloudletId++, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet.setUserId(broker.getId());
cloudletList.add(cloudlet);
}
broker.submitCloudletList(cloudletList);
// Start simulation
CloudSim.startSimulation();
// Try canceling a cloudlet during the simulation
int cloudletToCancelId = 1; // ID of the cloudlet to cancel
CustomVm customVm = (CustomVm) vm;
customVm.getCloudletScheduler().cancelCloudlet(cloudletToCancelId);
// Stop the simulation
CloudSim.stopSimulation();
// Print results
for (Cloudlet cloudlet : broker.getCloudletReceivedList()) {
System.out.println("Cloudlet ID: " + cloudlet.getCloudletId()
+ " Status: " + cloudlet.getCloudletStatusString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Datacenter createDatacenter(String name) {
List hostList = new ArrayList<>();
int hostId = 0;
int ram = 16384; // Host memory (MB)
long storage = 1000000; // Host storage
int bw = 100000; // Bandwidth
int pesNumber = 4; // Number of CPUs
List peList = new ArrayList<>();
for (int i = 0; i < pesNumber; i++) {
peList.add(new Pe(i, new PeProvisionerSimple(1000))); // MIPS
}
Host host = new Host(hostId, new RamProvisionerSimple(ram), new BwProvisionerSimple(bw), storage, peList, new VmSchedulerTimeShared(peList));
hostList.add(host);
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
double timeZone = 10.0;
double costPerSec = 3.0;
double costPerMem = 0.05;
double costPerStorage = 0.1;
double costPerBw = 0.1;
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, timeZone, costPerSec, costPerMem, costPerStorage, costPerBw);
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), new ArrayList<>(), 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
}
// Custom VM class to use the custom scheduler
class CustomVm extends Vm {
public CustomVm(int id, int userId, double mips, int numberOfPes, int ram, long bw, long size, String vmm, CloudletScheduler cloudletScheduler) {
super(id, userId, mips, numberOfPes, ram, bw, size, vmm, cloudletScheduler);
}
@Override
public CustomCloudletSchedulerTimeShared getCloudletScheduler() {
return (CustomCloudletSchedulerTimeShared) super.getCloudletScheduler();
}
}
// Custom CloudletScheduler class
class CustomCloudletSchedulerTimeShared extends CloudletSchedulerTimeShared {
public void cancelCloudlet(int cloudletId) {
int position = 0;
boolean found = false;
// Check if the cloudlet is in the exec list
for (ResCloudlet rcl : getCloudletExecList()) {
if (rcl.getCloudletId() == cloudletId) {
found = true;
break;
}
position++;
}
if (found) {
// Remove from exec list and add to paused list
ResCloudlet rcl = getCloudletExecList().remove(position);
if (rcl.getRemainingCloudletLength() == 0) {
cloudletFinish(rcl);
} else {
rcl.setCloudletStatus(Cloudlet.CANCELED);
getCloudletPausedList().add(rcl);
System.out.println("Cloudlet " + cloudletId + " was canceled.");
}
} else {
System.out.println("Cloudlet " + cloudletId + " not found in execution list.");
}
}
}