Skip to content
VirtualLP Virtual Learning Platform

Practical 9: Simulate Task Scheduling using CloudSim

15 min read Topic 18 of 31 1 hands-on lab

Aim

Create several cloudlets and VMs (Virtual Machines) with different speeds. Assign the tasks using the scheduling plans in the program. Compare the time needed to finish all tasks.

Theory

Task scheduling decides which VM should run each task. The aim may be to finish sooner, reduce cost or share work more evenly.

Makespan is the time needed to finish the whole set of tasks. In this practical, compare round-robin assignment, which gives tasks to VMs in turn, with a plan that also considers VM speed.

giving homework to two friends

You have 6 homework jobs of different sizes and two friends – one fast, one slow. Round robin = give jobs one-by-one alternately, ignoring who is faster; the slow friend ends up working till midnight. Greedy = always give the next big job to whoever will finish it soonest; both friends finish around the same time, and you all go home early.

Algorithm Idea
Round robin (RR) Assign tasks to VMs cyclically – CloudSim broker’s default
FCFS (First Come, First Served) Run tasks in arrival order
SJF – Shortest Job First Shortest tasks first – minimises average waiting time
Min-Min Repeatedly assign the task with the smallest minimum completion time to the VM giving it
Max-Min Assign the task with the largest minimum completion time first – good when a few tasks are long
Greedy / speed-aware Long tasks to fast VMs, short tasks to slow VMs
Meta-heuristics Genetic algorithm, ant colony, particle swarm – used in research

In CloudSim a custom schedule is applied with:

broker.bindCloudletToVm(cloudletId, vmId);   // call after submitCloudletList, before startSimulation

Metrics

  • Completion (finish) time of each task.
  • Makespan = max(finish times).
  • Average completion time = Σ finish / n.
  • Load balance – how evenly work is spread across VMs.

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

  1. In the project from Practical 4, create TaskScheduling.java and copy the program below.
  2. Run it with Run As > Java Application. Read the Console output.
  3. Compare the IDs, task status and timing columns with the example output. Change one model setting, run again and explain the difference.

Program – TaskScheduling.java

Two VMs with different speeds (1000 and 2000 MIPS (Million Instructions Per Second)) and six tasks. First the default round robin is used, then a greedy “earliest finish time” scheduler that assigns each task (longest first) to the VM where it would finish earliest.

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 TaskScheduling {

    private static long[] lengths = {400000, 100000, 300000, 200000, 500000, 150000};
    private static int[] vmMips = {1000, 2000};

    public static void main(String[] args) {
        run(false);   // round robin
        run(true);    // greedy earliest-finish-time
    }

    private static void run(boolean greedy) {
        String name = greedy ? "GREEDY (longest task → earliest-finishing VM)" : "ROUND ROBIN";
        Log.printLine("=========== " + name + " ===========");
        try {
            CloudSim.init(1, Calendar.getInstance(), false);
            Datacenter dc = createDatacenter("Datacenter_0");
            DatacenterBroker broker = new DatacenterBroker("Broker");
            int brokerId = broker.getId();

            List<Vm> vms = new ArrayList<Vm>();
            for (int i = 0; i < vmMips.length; i++) {
                vms.add(new Vm(i, brokerId, vmMips[i], 1, 512, 1000, 10000, "Xen", new CloudletSchedulerSpaceShared()));
            }
            broker.submitVmList(vms);

            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);

            if (greedy) {
                double[] readyTime = new double[vmMips.length];      // when each VM becomes free
                Integer[] order = {4, 0, 2, 3, 5, 1};                // tasks sorted by length, longest first
                for (int k = 0; k < order.length; k++) {
                    int task = order[k];
                    int best = 0;
                    double bestFinish = Double.MAX_VALUE;
                    for (int v = 0; v < vmMips.length; v++) {
                        double finish = readyTime[v] + lengths[task] / (double) vmMips[v];
                        if (finish < bestFinish) { bestFinish = finish; best = v; }
                    }
                    readyTime[best] = bestFinish;
                    broker.bindCloudletToVm(task, best);
                }
            }

            CloudSim.startSimulation();
            List<Cloudlet> done = broker.getCloudletReceivedList();
            CloudSim.stopSimulation();
            report(done, name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Datacenter createDatacenter(String name) throws Exception {
        List<Pe> peList = new ArrayList<Pe>();
        peList.add(new Pe(0, new PeProvisionerSimple(2000)));
        peList.add(new Pe(1, new PeProvisionerSimple(2000)));
        List<Host> hostList = new ArrayList<Host>();
        hostList.add(new Host(0, new RamProvisionerSimple(4096), new BwProvisionerSimple(10000), 1000000, peList, new VmSchedulerSpaceShared(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);
    }

    private static void report(List<Cloudlet> list, String name) {
        DecimalFormat dft = new DecimalFormat("###.##");
        double makespan = 0, sum = 0;
        Log.printLine("Task    VM    Length    Start    Finish");
        for (Cloudlet c : list) {
            Log.printLine("  " + c.getCloudletId() + "     " + c.getVmId() + "    " + c.getCloudletLength() + "    "
                    + dft.format(c.getExecStartTime()) + "    " + dft.format(c.getFinishTime()));
            makespan = Math.max(makespan, c.getFinishTime());
            sum += c.getFinishTime();
        }
        Log.printLine(name + " -> Makespan: " + dft.format(makespan) + "   Average completion: " + dft.format(sum / list.size()));
        Log.printLine();
    }
}

Virtual lab · CloudSim · 30 min

Simulate task scheduling with cloudlet-to-VM binding

Compare the broker’s default round-robin assignment with explicit scheduling using bindCloudletToVm, and measure the effect on makespan.

Open lab

The program creates two VMs of different speeds and six tasks. It first assigns tasks in round-robin order. It then assigns longer tasks first to the VM expected to finish them earliest. Compare the printed makespan and average completion time.

Output (summary lines)

Example output for the settings in this lesson:

=========== ROUND ROBIN ===========
...
Task    VM    Length    Start    Finish
  1     1    100000    0.1    50.1
  3     1    200000    50.1    150.1
  5     1    150000    150.1    225.1
  0     0    400000    0.1    400.1
  2     0    300000    400.1    700.1
  4     0    500000    700.1    1200.1
ROUND ROBIN -> Makespan: 1200.1   Average completion: 454.27

=========== GREEDY (longest task → earliest-finishing VM) ===========
Task    VM    Length    Start    Finish
  1     1    100000    0.1    50.1
  2     1    300000    50.1    200.1
  3     1    200000    200.1    300.1
  0     0    400000    0.1    400.1
  4     1    500000    300.1    550.1
  5     0    150000    400.1    550.1
GREEDY (longest task → earliest-finishing VM) -> Makespan: 550.1   Average completion: 341.77

Observations

  • Round robin ignores VM speed: the slow VM 0 receives tasks 0, 2 and 4 (1,200,000 MI (Million Instructions) → 1200 s) while the fast VM finishes early.
  • The greedy scheduler gives four tasks (1,100,000 MI) to the fast VM and two to the slow VM, so both finish at 550.1 s – the makespan drops from 1200.1 s to 550.1 s (−54 %) and the average completion time from 454.27 s to 341.77 s.
  • Inside each space-shared VM, tasks still run in submission (ID) order, which is why task 1 runs before task 2 on VM 1.
  • bindCloudletToVm overrides the broker’s default assignment.

Conclusion

Compare each task's VM assignment and finish time. Calculate the makespan for both plans. Explain how using VM speed changed the result for this set of tasks.

Viva questions

  1. What is task scheduling in cloud computing? What objectives does it optimise? Deciding which task runs on which VM and when – to reduce makespan, completion time, cost or energy and balance load.
  2. How does the CloudSim broker assign cloudlets by default? Round robin over the created VMs.
  3. What does bindCloudletToVm do and when must it be called? It fixes a cloudlet to a chosen VM; call it after submitCloudletList and before startSimulation.
  4. Explain Min-Min and Max-Min scheduling. Min-Min assigns the task with the smallest minimum completion time first; Max-Min assigns the task with the largest minimum completion time first.
  5. Define makespan and average completion time. Makespan = the latest finish time; average completion = sum of finish times ÷ number of tasks.
  6. Why does round robin perform poorly on different VMs? It ignores VM speed, so slow VMs get as much work as fast ones and finish much later.

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.