How to Allocate the Cloudlets in the VMs of a Specific Host?
Share
Condition for Allocate the Cloudlets in the VMs of a Specific Host
Description: To allocate cloudlets in the VMs of a specific host in CloudSim, first ensure the host is created and added to the datacenter. Then, assign each cloudlet to a VM on the desired host by setting the cloudlet's VM ID to the VM's ID using the setVmId method. After associating VMs with the correct host, submit the cloudlet list to the broker for execution on the specified host's VMs. This approach allows control over which cloudlets run on which host by managing the VM assignments.
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.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
public class CloudletToSpecificHost {
private static List cloudletList;
private static List vmlist;
public static void main(String[] args) {
Log.printLine("Starting Cloudlet Allocation to Specific Host Example...");
try {
int num_user = 1; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false;
CloudSim.init(num_user, calendar, trace_flag);
// Create a Datacenter with multiple hosts
Datacenter datacenter0 = createDatacenter("Datacenter_0");
// Create Broker
DatacenterBroker broker = createBroker();
int brokerId = broker.getId();
// Create VMs and assign them to a specific host
vmlist = new ArrayList<>();
int vmid = 0;
int mips = 1000;
long size = 10000; // Image size (MB)
int ram = 512; // VM memory (MB)
long bw = 1000; // Bandwidth (Mbps)
int pesNumber = 1; // Number of CPUs
String vmm = "Xen";
// Assign all VMs to Host ID 0
Vm vm1 = new Vm(vmid++, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
Vm vm2 = new Vm(vmid++, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
vmlist.add(vm1);
vmlist.add(vm2);
// Submit VM list to the broker
broker.submitVmList(vmlist);
// Create Cloudlets and assign them to VMs on the specific host
cloudletList = new ArrayList<>();
int cloudletId = 0;
long length = 400000; // Task length
long fileSize = 300; // File size (MB)
long outputSize = 300; // Output size (MB)
UtilizationModel utilizationModel = new UtilizationModelFull();
for (int i = 0; i < 4; i++) {
Cloudlet cloudlet = new Cloudlet(cloudletId++, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet.setUserId(brokerId);
// Assign cloudlets alternately to VMs
if (i % 2 == 0) {
cloudlet.setVmId(vm1.getId());
} else {
cloudlet.setVmId(vm2.getId());
}
cloudletList.add(cloudlet);
}
// Submit cloudlet list to the broker
broker.submitCloudletList(cloudletList);
// Start simulation
CloudSim.startSimulation();
// Retrieve results
List newList = broker.getCloudletReceivedList();
printCloudletList(newList);
CloudSim.stopSimulation();
Log.printLine("Cloudlet Allocation to Specific Host Example finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("An error occurred");
}
}
private static Datacenter createDatacenter(String name) {
List hostList = new ArrayList<>();
// Create a single host with two processing elements (PEs)
List peList = new ArrayList<>();
int mips = 1000;
peList.add(new Pe(0, new PeProvisionerSimple(mips)));
peList.add(new Pe(1, new PeProvisionerSimple(mips)));
int hostId = 0;
int ram = 2048; // Host memory (MB)
long storage = 1000000; // Storage capacity (MB)
int bw = 10000; // Bandwidth (Mbps)
hostList.add(new Host(hostId, new RamProvisionerSimple(ram), new BwProvisionerSimple(bw),
storage, peList, new VmSchedulerTimeShared(peList)));
String arch = "x86"; // Architecture
String os = "Linux"; // Operating System
String vmm = "Xen"; // Virtual Machine Monitor
double timeZone = 10.0; // Time zone
double cost = 3.0; // Cost per second
double costPerMem = 0.05; // Cost per MB of RAM
double costPerStorage = 0.001; // Cost per MB of storage
double costPerBw = 0.0; // Cost per Mbps of bandwidth
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), new LinkedList<>(), 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
private static DatacenterBroker createBroker() {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
private static void printCloudletList(List list) {
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (Cloudlet cloudlet : list) {
Log.printLine(cloudlet.getCloudletId() + indent
+ (cloudlet.getStatus() == Cloudlet.SUCCESS ? "SUCCESS" : "FAILED") + indent
+ cloudlet.getVmId() + indent
+ dft.format(cloudlet.getActualCPUTime()) + indent
+ dft.format(cloudlet.getExecStartTime()) + indent
+ dft.format(cloudlet.getFinishTime()));
}
}
}