Practical 6: Create Virtual Machines in CloudSim
Aim
Create VMs (Virtual Machines) with different CPU (Central Processing Unit), memory and storage needs. Give them to the broker, which requests resources for the user. Check which hosts accept the VMs and what happens when a VM is too large.
Theory
A CloudSim VM is a model of a virtual computer. Its settings include speed, processor count, memory, network capacity and disk space.
The host must have enough resources to accept it. The broker sends the VM requests to the data centre. The code below shows the settings passed to the VM constructor.
Vm vm = new Vm(id, userId, mips, numberOfPes, ram, bw, size, vmm, cloudletScheduler);
| Parameter | Meaning | Example |
|---|---|---|
id |
Unique VM id | 0 |
userId |
Owner – the broker’s id | broker.getId() |
mips |
MIPS (Million Instructions Per Second) per PE (Processing Element) requested | 1000 |
numberOfPes |
Number of virtual CPUs | 1 |
ram |
Memory in MB | 512 |
bw |
Bandwidth | 1000 |
size |
Image size (storage) in MB | 10000 |
vmm |
Virtual machine monitor name | "Xen" |
cloudletScheduler |
How the VM shares its capacity among cloudlets | new CloudletSchedulerTimeShared() |
Creating a VM is like booking a room: you say how many beds (PEs), how big (RAM (Random Access Memory)), what Wi-Fi speed (bandwidth) and how much cupboard space (size). The hotel (data centre) gives you a room on the floor with the most empty rooms that matches your needs. If no floor has a room big enough, the booking fails and you try the next hotel.
How a VM is placed
- The broker sends VM creation requests to the first data centre.
VmAllocationPolicySimplepicks the host with the most free PEs that can satisfy the VM’s PEs, RAM, bandwidth, storage and MIPS-per-PE.- If no host fits, the log prints “Creation of VM #n failed in Datacenter #id” and the broker tries the next data centre.
- If no data centre can host any VM, the broker aborts.
flowchart LR
B[Broker] -->|create VM| DC[Datacenter]
DC --> P{Host with most free PEs<br/>that fits PEs, RAM, BW, size, MIPS?}
P -- yes --> OK[VM created on that host]
P -- no --> F[Creation failed → try next data centre]
Requirements
Use the CloudSim 3.0.3 project prepared in Practical 4, with its CloudSim and Commons Math library files on the classpath. Confirm that the first example runs before continuing.
Procedure
- In the project from Practical 4, create
VmCreation.javaand copy the program below. - Run it with Run As > Java Application. Read the Console output.
- Compare the IDs, task status and timing columns with the example output. Change one model setting, run again and explain the difference.
Program – VmCreation.java
package vlp.cloudsim;
import java.util.*;
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.*;
public class VmCreation {
public static void main(String[] args) {
Log.printLine("Starting VmCreation...");
try {
CloudSim.init(1, Calendar.getInstance(), false);
Datacenter dc = createDatacenter("Datacenter_0");
DatacenterBroker broker = new DatacenterBroker("Broker");
int brokerId = broker.getId();
// Create VMs with different capacities
List<Vm> vmList = createVms(brokerId);
broker.submitVmList(vmList);
// One cloudlet per VM so the simulation runs to completion
List<Cloudlet> cloudlets = new ArrayList<Cloudlet>();
UtilizationModel full = new UtilizationModelFull();
for (int i = 0; i < vmList.size(); i++) {
Cloudlet c = new Cloudlet(i, 100000, 1, 300, 300, full, full, full);
c.setUserId(brokerId);
cloudlets.add(c);
}
broker.submitCloudletList(cloudlets);
CloudSim.startSimulation();
CloudSim.stopSimulation();
Log.printLine();
Log.printLine("VM ID Host MIPS PEs RAM");
for (Vm vm : broker.getVmsCreatedList()) {
Log.printLine(" " + vm.getId() + " " + vm.getHost().getId() + " " + vm.getMips()
+ " " + vm.getNumberOfPes() + " " + vm.getRam());
}
Log.printLine("VMs requested: " + vmList.size() + ", created: " + broker.getVmsCreatedList().size());
} catch (Exception e) {
e.printStackTrace();
}
}
private static List<Vm> createVms(int brokerId) {
List<Vm> list = new ArrayList<Vm>();
String vmm = "Xen";
// id, userId, mips, pes, ram, bw, size
list.add(new Vm(0, brokerId, 1000, 1, 512, 1000, 10000, vmm, new CloudletSchedulerTimeShared()));
list.add(new Vm(1, brokerId, 500, 1, 1024, 1000, 10000, vmm, new CloudletSchedulerTimeShared()));
list.add(new Vm(2, brokerId, 1000, 2, 1024, 1000, 20000, vmm, new CloudletSchedulerTimeShared()));
// A VM needing more RAM than any host offers – its creation will fail
list.add(new Vm(3, brokerId, 1000, 1, 4096, 1000, 10000, vmm, new CloudletSchedulerTimeShared()));
return list;
}
private static Datacenter createDatacenter(String name) throws Exception {
List<Host> hostList = new ArrayList<Host>();
for (int h = 0; h < 2; h++) {
List<Pe> peList = new ArrayList<Pe>();
for (int p = 0; p < 4; p++) {
peList.add(new Pe(p, new PeProvisionerSimple(1000))); // 4 cores per host
}
hostList.add(new Host(h, new RamProvisionerSimple(2048), new BwProvisionerSimple(10000),
1000000, peList, new VmSchedulerTimeShared(peList)));
}
DatacenterCharacteristics ch = new DatacenterCharacteristics("x86", "Linux", "Xen", hostList,
10.0, 3.0, 0.05, 0.001, 0.0);
return new Datacenter(name, ch, new VmAllocationPolicySimple(hostList), new LinkedList<Storage>(), 0);
}
}
Virtual lab · CloudSim · 25 min
Create virtual machines in CloudSim
Define VMs with MIPS, PEs, RAM, bandwidth and image size, submit them to the broker, trigger a VM creation failure on purpose and fix it.
What the program does: it builds a small data centre (hosts with 2048 MB RAM each), creates 4 VMs in a loop – one of them (VM 3) asks for 4096 MB RAM – submits them and one cloudlet per VM to the broker, then prints how many VMs were requested and how many were actually created.
Output (key lines)
Example output for the settings in this lesson:
0.0: Broker: Trying to Create VM #0 in Datacenter_0
0.0: Broker: Trying to Create VM #1 in Datacenter_0
0.0: Broker: Trying to Create VM #2 in Datacenter_0
0.0: Broker: Trying to Create VM #3 in Datacenter_0
0.1: Broker: VM #0 has been created in Datacenter #2, Host #0
0.1: Broker: VM #1 has been created in Datacenter #2, Host #1
0.1: Broker: VM #2 has been created in Datacenter #2, Host #0
0.1: Broker: Creation of VM #3 failed in Datacenter #2
...
VMs requested: 4, created: 3
Observations
- VMs 0 and 1 go to different hosts because each new VM is placed on the host with the most free PEs; VM 2 (2 PEs) then fits on host 0, which still has 3 free PEs.
- If the hosts had only 2 PEs each, VM 2 would also fail – after VMs 0 and 1 no host would have 2 free PEs. Try it!
- VM 3 requests 4096 MB RAM, but each host has only 2048 MB, so its creation fails; its cloudlet is assigned to another VM.
- A VM cannot request more MIPS per PE than the host’s PE provides.
Conclusion
Check the VM creation messages and record each VM's host. For the VM that cannot fit, compare its requested resources with the host's available resources. Explain why the allocation failed.
Viva questions
- Explain each parameter of the
Vmconstructor. id, owner (broker id), MIPS per PE, number of PEs, RAM, bandwidth, image size, VMM name, cloudlet scheduler – see the table above. - Why is the broker id passed to the VM? It tells CloudSim which user/broker owns the VM, so results go back to that broker.
- What happens when no host can accommodate a VM? "Creation of VM #n failed" is logged and the broker tries the next data centre; if none can, the VM is not created.
- Can a VM request 2000 MIPS when the host PE is rated 1000 MIPS? Why? No – a VM's MIPS per PE cannot be more than one physical PE provides.
- What is the difference between
getVmList()andgetVmsCreatedList()? The first is every VM you submitted; the second only those that were actually created. - What is a VMM and why is it a parameter? The Virtual Machine Monitor (hypervisor) such as "Xen"; it is recorded as a property of the VM (CloudSim does not change behaviour by it).
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.