Practical 7: Virtual Machine Scheduling in CloudSim
Aim
Run CloudSim with time-shared and space-shared scheduling. Compare when tasks start and finish. Note the difference between sharing a host's CPU (Central Processing Unit) between VMs (Virtual Machines) and sharing a VM's CPU between tasks.
Theory
Scheduling decides how work gets processor time. CloudSim does this at two levels. A host scheduler shares its processing elements between VMs. A cloudlet scheduler shares a VM's processing elements between tasks.
With time-sharing, several jobs take turns using the processor. With space-sharing, a job gets the processing elements it needs while other jobs may wait.
| Level | Class | Decides | Policies |
|---|---|---|---|
| Host level | VmScheduler (in Host) |
How the host’s PEs (Processing Elements) are shared among its VMs | VmSchedulerTimeShared, VmSchedulerSpaceShared, VmSchedulerTimeSharedOverSubscription |
| VM level | CloudletScheduler (in Vm) |
How a VM’s capacity is shared among its cloudlets | CloudletSchedulerTimeShared, CloudletSchedulerSpaceShared, CloudletSchedulerDynamicWorkload |
Space-shared
Each PE is dedicated to one VM (host level) or one cloudlet (VM level) at a time. Extra requests wait in a queue (first-come-first-served). Like a single cash counter: customers are served one by one.
Time-shared
Capacity is shared concurrently. At VM level all cloudlets start immediately and each receives a fraction of the VM’s MIPS (Million Instructions Per Second). Like a teacher dividing attention among all students at once.
gantt
title VM with 1 PE (1000 MIPS) – two cloudlets of 100,000 MI
dateFormat X
axisFormat %s
section Space-shared
Cloudlet 0 :a1, 0, 100
Cloudlet 1 :a2, after a1, 100
section Time-shared
Cloudlet 0 :b1, 0, 200
Cloudlet 1 :b2, 0, 200
| Space-shared | Time-shared | |
|---|---|---|
| Cloudlet 0 | starts 0, finishes 100 | starts 0, finishes 200 |
| Cloudlet 1 | starts 100, finishes 200 | starts 0, finishes 200 |
| Average completion | 150 s | 200 s |
| Makespan | 200 s | 200 s |
| Waiting time | yes (queue) | none |
The four combinations (from the CloudSim paper)
| Host (VM scheduler) | VM (cloudlet scheduler) | Behaviour |
|---|---|---|
| Space-shared | Space-shared | VMs get dedicated cores; tasks run one per core, FCFS (First Come, First Served) |
| Space-shared | Time-shared | VMs get dedicated cores; tasks inside a VM share it |
| Time-shared | Space-shared | VMs share cores; tasks inside a VM queue |
| Time-shared | Time-shared | Everything shared concurrently |
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
VmSchedulingDemo.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 – VmSchedulingDemo.java
The program runs the same workload twice – once with time-shared, once with space-shared cloudlet schedulers – by putting the scenario in a method and calling it with a flag.
package vlp.cloudsim;
import java.text.DecimalFormat;
import java.util.*;
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.*;
public class VmSchedulingDemo {
public static void main(String[] args) {
runScenario(false); // time-shared
runScenario(true); // space-shared
}
private static void runScenario(boolean spaceShared) {
String policy = spaceShared ? "SPACE-SHARED" : "TIME-SHARED";
Log.printLine("=========== " + policy + " ===========");
try {
CloudSim.init(1, Calendar.getInstance(), false);
Datacenter dc = createDatacenter("Datacenter_0", spaceShared);
DatacenterBroker broker = new DatacenterBroker("Broker");
int brokerId = broker.getId();
// Two VMs, 1 PE each, 1000 MIPS
List<Vm> vms = new ArrayList<Vm>();
for (int i = 0; i < 2; i++) {
CloudletScheduler cs = spaceShared ? new CloudletSchedulerSpaceShared() : new CloudletSchedulerTimeShared();
vms.add(new Vm(i, brokerId, 1000, 1, 512, 1000, 10000, "Xen", cs));
}
broker.submitVmList(vms);
// Four cloudlets of different lengths (MI)
long[] lengths = {100000, 200000, 300000, 400000};
List<Cloudlet> cloudlets = new ArrayList<Cloudlet>();
UtilizationModel full = new UtilizationModelFull();
for (int i = 0; i < lengths.length; i++) {
Cloudlet c = new Cloudlet(i, lengths[i], 1, 300, 300, full, full, full);
c.setUserId(brokerId);
cloudlets.add(c);
}
broker.submitCloudletList(cloudlets);
CloudSim.startSimulation();
List<Cloudlet> done = broker.getCloudletReceivedList();
CloudSim.stopSimulation();
print(done, policy);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Datacenter createDatacenter(String name, boolean spaceShared) throws Exception {
List<Pe> peList = new ArrayList<Pe>();
peList.add(new Pe(0, new PeProvisionerSimple(1000)));
peList.add(new Pe(1, new PeProvisionerSimple(1000)));
VmScheduler vmScheduler = spaceShared ? new VmSchedulerSpaceShared(peList) : new VmSchedulerTimeShared(peList);
List<Host> hostList = new ArrayList<Host>();
hostList.add(new Host(0, new RamProvisionerSimple(4096), new BwProvisionerSimple(10000), 1000000, peList, vmScheduler));
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);
}
private static void print(List<Cloudlet> list, String policy) {
DecimalFormat dft = new DecimalFormat("###.##");
String indent = " ";
double sum = 0;
Log.printLine("Cloudlet" + indent + "VM" + indent + "Length" + indent + "Start" + indent + "Finish" + indent + "CPU time");
for (Cloudlet c : list) {
Log.printLine(indent + c.getCloudletId() + indent + indent + c.getVmId() + indent + c.getCloudletLength()
+ indent + dft.format(c.getExecStartTime()) + indent + dft.format(c.getFinishTime())
+ indent + dft.format(c.getActualCPUTime()));
sum += c.getFinishTime();
}
Log.printLine(policy + " average finish time: " + dft.format(sum / list.size()));
Log.printLine();
}
}
Virtual lab · CloudSim · 30 min
Virtual machine scheduling: time-shared vs space-shared
Run the same workload with time-shared and space-shared schedulers at the VM and host level and compare start and finish times.
What the program does: it runs the same workload twice – once with time-shared schedulers and once with space-shared – using 2 VMs and 4 cloudlets of 100 000, 200 000, 300 000 and 400 000 MI (Million Instructions). For each run it prints a table of start/finish times and the average finish time, so you can compare them side by side.
Output (results tables)
Example output for the settings in this lesson:
=========== TIME-SHARED ===========
Cloudlet VM Length Start Finish CPU time
0 0 100000 0.1 200.1 200
1 1 200000 0.1 400.1 400
2 0 300000 0.1 400.1 400
3 1 400000 0.1 600.1 600
TIME-SHARED average finish time: 400.1
=========== SPACE-SHARED ===========
Cloudlet VM Length Start Finish CPU time
0 0 100000 0.1 100.1 100
1 1 200000 0.1 200.1 200
2 0 300000 100.1 400.1 300
3 1 400000 200.1 600.1 400
SPACE-SHARED average finish time: 325.1
Observations
- The broker distributes cloudlets round-robin: 0 and 2 to VM 0, 1 and 3 to VM 1.
- Time-shared: all cloudlets start at 0.1 and share the VM, so short cloudlets finish later than they would alone.
- Space-shared: cloudlets 2 and 3 wait until the first cloudlet on their VM finishes, but each runs at full speed.
- Makespan is the same (600.1 s), but the average finish time is lower with space-shared for this workload.
Conclusion
Compare the start, finish and waiting times in both runs. Explain which jobs shared processing time and which waited. Use the results for this workload to explain the difference between the policies.
Viva questions
- At which two levels does CloudSim perform scheduling? Host level (
VmScheduler– PEs among VMs) and VM level (CloudletScheduler– VM capacity among cloudlets). - Differentiate time-shared and space-shared policies. Time-shared runs all tasks at once, each slower; space-shared runs one per core and queues the rest at full speed.
- Which class is responsible for sharing a host's PEs among VMs?
VmScheduler(for exampleVmSchedulerTimeShared). - In space-shared mode, why do some cloudlets have a start time greater than 0.1? They wait in the queue until the earlier cloudlet on the same VM finishes.
- Calculate finish times for two cloudlets of 50,000 MI each on one 500-MIPS VM under both policies. Each needs 100 s alone. Space-shared: 100 s and 200 s. Time-shared: both 200 s.
- What is makespan? The time when the last cloudlet finishes.
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.