Condition for Pause and Resume the Simulation using CloudSim in Cloud Computing
Description: In CloudSim, pausing and resuming simulations is not a built-in feature, as CloudSim's simulation typically runs as a continuous process. However, you can simulate pausing and resuming by controlling the flow of the simulation using specific mechanisms or by designing your simulation in a way that allows for pausing and resuming at certain points.
Steps
Custom Pause Implementation: To simulate a pause, you can manually control when events are processed in the simulation: Pause: You can simulate a pause by preventing the simulation from progressing until some condition is met.Run the simulation. Resume: To resume, you would allow the simulation to continue from where it was paused, processing the queued events.
Using Thread.sleep for Manual Pause/Resume: Another approach is to use Thread.sleep() to introduce a delay in the simulation. You can use this to simulate pausing by stopping execution for a specific period and then resuming by allowing the simulation to continue.
CloudSim Extensions for Pause/Resume: You could also explore CloudSim Plus or other CloudSim extensions, as they may offer more advanced simulation control features like pausing, resuming, or event scheduling.
Sample Code
package JavaProject2;
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 CloudPauseAndResume {
private static volatile boolean isPaused = false;
public static void main(String[] args) {
try {
int numUser = 1; // Number of cloud users
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false;
// Initialize the CloudSim library
CloudSim.init(numUser, calendar, traceFlag);
// Create Datacenter
Datacenter datacenter = createDatacenter("Datacenter_1");
// Create Broker
DatacenterBroker broker = createBroker();
int brokerId = broker.getId();
// Create virtual machines and cloudlets
List vmList = createVMs(brokerId, 3); // Create 3 VMs
List cloudletList = createCloudlets(brokerId, 5); // Create 5 Cloudlets
broker.submitVmList(vmList);
broker.submitCloudletList(cloudletList);
// Start a thread to handle the pause and resume control
new Thread(() -> {
try {
Thread.sleep(3000); // Wait for some time before pausing
isPaused = true;
System.out.println("Pausing simulation...");
CloudSim.pauseSimulation();
Thread.sleep(5000); // Wait while simulation is paused
isPaused = false;
System.out.println("Resuming simulation...");
CloudSim.resumeSimulation();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
// Start the simulation
double lastClock = CloudSim.startSimulation();
CloudSim.stopSimulation();
// Print results
List newList = broker.getCloudletReceivedList();
printCloudletList(newList);
System.out.println("Simulation finished at time: " + lastClock);
} catch (Exception e) {
e.printStackTrace();
System.out.println("An unexpected error occurred.");
}
}
private static Datacenter createDatacenter(String name) {
List hostList = new ArrayList<>();
// Create a host with specific properties
int hostId = 0;
int ram = 2048; // Host memory (MB)
long storage = 1000000; // Host storage (MB)
int bw = 10000; // Host bandwidth (Mbps)
List peList = new ArrayList<>();
int mips = 1000; // Host's CPU power
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // Adding a CPU core
hostList.add(new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
));
String arch = "x86"; // System architecture
String os = "Linux"; // Operating system
String vmm = "Xen"; // Virtual machine monitor
double time_zone = 10.0; // Time zone
double cost = 3.0; // Cost per second
double costPerMem = 0.05; // Cost per MB
double costPerStorage = 0.1; // Cost per MB
double costPerBw = 0.1; // Cost per Mbps
LinkedList storageList = new LinkedList<>(); // Storage list
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw),
new VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception ex) {
}
return datacenter;
}
private static DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
}
return broker;
}
private static List createVMs(int brokerId, int numVMs) {
List vmList = new ArrayList<>();
for (int i = 0; i < numVMs; i++) {
int vmId = i;
int mips = 1000;
long size = 10000; // Image size (MB)
int ram = 512; // RAM (MB)
long bw = 1000; // Bandwidth (Mbps)
int pesNumber = 1; // Number of CPUs
String vmm = "Xen"; // VMM name
Vm vm = new Vm(vmId, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
vmList.add(vm);
}
return vmList;
}
private static List createCloudlets(int brokerId, int numCloudlets) {
List cloudletList = new ArrayList<>();
for (int i = 0; i < numCloudlets; i++) {
int cloudletId = i;
long length = 40000;
int pesNumber = 1;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet = new Cloudlet(cloudletId, length, pesNumber, fileSize, outputSize, utilizationModel,
utilizationModel, utilizationModel);
cloudlet.setUserId(brokerId);
cloudletList.add(cloudlet);
}
return cloudletList;
}
private static void printCloudletList(List list) {
String indent = " ";
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");
for (Cloudlet cloudlet : list) {
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
System.out.println(cloudlet.getCloudletId() + indent + "SUCCESS" + indent + cloudlet.getResourceId() +
indent + cloudlet.getVmId() + indent + cloudlet.getActualCPUTime() + indent +
cloudlet.getExecStartTime() + indent + cloudlet.getFinishTime());
}
}
}
}