List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Allocate the VM in a Specific Host?

Allocate the VM in a Specific Host

Condition for Allocate the VM in a Specific Host

  • Description:
    To allocate a virtual machine to a specific host in CloudSim, create the host and VM, then access the host list from the datacenter. Select the desired host and use the getVmList() method to add the VM to the host's list. Ensure the host index is valid before assigning the VM, especially in a datacenter with multiple hosts.
Sample Code
  • import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.LinkedList;
    import java.util.List;
    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.UtilizationModel;
    import org.cloudbus.cloudsim.UtilizationModelFull;
    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;
    public class VMAllocationSpecificHost {
    private static List cloudletList;
    private static List vmlist;
    public static void main(String[] args) {
    Log.printLine("Starting VMAllocationSpecificHost...");
    try {
    int num_user = 1; // number of cloud users
    Calendar calendar = Calendar.getInstance(); // Calendar initialization
    boolean trace_flag = false; // trace events
    CloudSim.init(num_user, calendar, trace_flag);
    // Create Datacenter
    Datacenter datacenter0 = createDatacenter("Datacenter_0");
    // Create Broker
    DatacenterBroker broker = createBroker();
    int brokerId = broker.getId();
    // Create one virtual machine
    vmlist = new ArrayList();
    int vmid = 0;
    int mips = 1000;
    long size = 10000; // image size (MB)
    int ram = 512; // vm memory (MB)
    long bw = 1000;
    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());
    // Manually allocate VM to a specific host (let's say host 0)
    // Manually allocate VM to a specific host (let's say host 1)
    Host selectedHost = datacenter0.getHostList().get(1); // Get the host at index 1 (Host 1)
    selectedHost.getVmList().add(vm); // Add the VM to the selected host's VM list
    vmlist.add(vm);
    // Submit VM list to the broker
    broker.submitVmList(vmlist);
    // Create one Cloudlet
    cloudletList = new ArrayList();
    int id = 0;
    long length = 400000;
    long fileSize = 300;
    long outputSize = 300;
    UtilizationModel utilizationModel = new UtilizationModelFull();
    Cloudlet cloudlet = new Cloudlet(id, length, pesNumber, fileSize, outputSize,
    utilizationModel, utilizationModel, utilizationModel);
    cloudlet.setUserId(brokerId);
    cloudlet.setVmId(vmid);
    cloudletList.add(cloudlet);
    broker.submitCloudletList(cloudletList);
    // Start the simulation
    CloudSim.startSimulation();
    CloudSim.stopSimulation();
    // Print results when simulation is over
    List newList = broker.getCloudletReceivedList();
    printCloudletList(newList);
    Log.printLine("VMAllocationSpecificHost finished!");
    } catch (Exception e) {
    e.printStackTrace();
    Log.printLine("Unwanted errors happen");
    }
    }
    private static Datacenter createDatacenter(String name) {
    List hostList = new ArrayList();
    // Create two hosts with identical configurations
    List peList = new ArrayList();
    int mips = 1000;
    peList.add(new Pe(0, new PeProvisionerSimple(mips)));
    int ram = 2048;
    long storage = 1000000;
    int bw = 10000;
    // Add two hosts
    hostList.add(new Host(0, new RamProvisionerSimple(ram), new BwProvisionerSimple(bw),
    storage, peList, new VmSchedulerTimeShared(peList)));
    hostList.add(new Host(1, new RamProvisionerSimple(ram), new BwProvisionerSimple(bw),
    storage, peList, new VmSchedulerTimeShared(peList)));
    String arch = "x86";
    String os = "Linux";
    String vmm = "Xen";
    double time_zone = 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, time_zone, 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;
    }
    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) { int size = list.size();
    Cloudlet cloudlet;
    String indent = " ";
    Log.printLine();
    Log.printLine("========== OUTPUT ==========");
    Log.printLine("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);
    Log.print(indent + cloudlet.getCloudletId() + indent + indent);
    if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
    Log.print("SUCCESS");
    Log.printLine(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()));
    }
    }
    }
    }
ScreenShots
  • Allocate the VM in a Specific Host