DEV Community

Cover image for Building a CloudSim Project for Scheduling Algorithms
Ravi Kishan
Ravi Kishan

Posted on

Building a CloudSim Project for Scheduling Algorithms

Introduction

Cloud computing requires efficient resource management, especially when it comes to task scheduling. In large-scale cloud environments, it's essential to allocate tasks (cloudlets) to virtual machines (VMs) in a way that optimizes performance and minimizes latency. This blog covers my journey in building a project using CloudSim, a simulation framework for modeling and experimenting with cloud computing systems.

The project showcases several scheduling algorithms such as Round Robin, First-Come-First-Serve (FCFS), Shortest Job First (SJF), Genetic Algorithm, and Ant Colony Optimization (ACO) to provide flexibility in assigning cloudlets to VMs.

Cloud Sim Diagram

Project Overview

This CloudSim project involves simulating a cloud environment, creating VMs and cloudlets, and implementing various scheduling algorithms. It consists of:

  1. Setting up a Datacenter: This includes defining the hardware resources, like hosts and processing elements (PEs), and allocating them to the datacenter.
  2. Creating VMs and Cloudlets: VMs represent virtual resources in the cloud, while cloudlets represent the tasks needing execution.
  3. Implementing Scheduling Algorithms: These determine how cloudlets are assigned to VMs based on the selected strategy.
  4. Simulation Execution: Using the CloudSim API, we run the simulation to evaluate how effectively each algorithm schedules tasks.

Project Setup

To begin, you’ll need Java 8 or higher and the CloudSim library. Clone the repository from GitHub and add CloudSim to your project:

git clone https://github.com/Ravikisha/CloudSim-Examples.git
Enter fullscreen mode Exit fullscreen mode

Code Overview

Let’s break down the code structure and explain each component.

1. Initializing CloudSim

First, the CloudSim package is initialized with a single user and without tracing:

int numUser = 1;
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false;
CloudSim.init(numUser, calendar, traceFlag);
Enter fullscreen mode Exit fullscreen mode

2. Setting up the Datacenter

The createDatacenter method creates a datacenter, which represents a collection of hosts (each with its processing resources). The datacenter is built using the following components:

  • Hosts: Physical machines containing processing elements (PEs). Each host has properties like RAM, storage, bandwidth, and a time-shared VM scheduler.
  • Datacenter Characteristics: Define the architecture, OS, cost, and other resource properties.
List<Host> hostList = new ArrayList<>();
List<Pe> peList = new ArrayList<>();
peList.add(new Pe(0, new PeProvisionerSimple(1000))); // PE with 1000 MIPS

hostList.add(new Host(
    0, 
    new RamProvisionerSimple(2048), 
    new BwProvisionerSimple(10000), 
    1000000, 
    peList, 
    new VmSchedulerTimeShared(peList)
));

DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
    "x86", "Linux", "Xen", hostList, 10.0, 3.0, 0.05, 0.001, 0
);
Enter fullscreen mode Exit fullscreen mode

3. Creating the DatacenterBroker

A DatacenterBroker manages VM and cloudlet lists and handles task scheduling. It submits cloudlets and VMs to the datacenter and assigns cloudlets based on the selected algorithm:

DatacenterBroker broker = new DatacenterBroker("Broker");
int brokerId = broker.getId();
Enter fullscreen mode Exit fullscreen mode

4. Creating VMs and Cloudlets

The createVMs and createCloudlets methods define virtual resources and tasks.

  • VMs are assigned properties like MIPS, RAM, bandwidth, and storage.
  • Cloudlets represent tasks with specific lengths, file sizes, and utilization models.
List<Vm> vmList = createVMs(brokerId, 5); // 5 VMs
List<Cloudlet> cloudletList = createCloudlets(brokerId, 10); // 10 cloudlets
broker.submitVmList(vmList);
broker.submitCloudletList(cloudletList);
Enter fullscreen mode Exit fullscreen mode

5. Implementing Scheduling Algorithms

In this project, five scheduling algorithms are implemented. Users can choose an algorithm at runtime, which defines how cloudlets are assigned to VMs. The code uses a switch case to select and apply an algorithm.

System.out.println("Enter the algorithm to be used: (roundrobin, fcfs, ant, genetic, sjf): ");
String algorithm = sc.next();

switch (algorithm.toLowerCase()) {
    case "roundrobin":
        RoundRobinAlgorithm rrAlgo = new RoundRobinAlgorithm();
        rrAlgo.runAlgorithm(broker, vmList, cloudletList);
        break;
    case "fcfs":
        FCFSAlgorithm fcfsAlgo = new FCFSAlgorithm();
        fcfsAlgo.runAlgorithm(broker, vmList, cloudletList);
        break;
    case "ant":
        ACOAlgorithm antAlgo = new ACOAlgorithm();
        antAlgo.runAlgorithm(broker, vmList, cloudletList);
        break;
    case "genetic":
        GeneticAlgorithm geneticAlgo = new GeneticAlgorithm();
        geneticAlgo.runAlgorithm(broker, vmList, cloudletList);
        break;
    case "sjf":
        SJFAlgorithm sjfAlgo = new SJFAlgorithm();
        sjfAlgo.runAlgorithm(broker, vmList, cloudletList);
        break;
    default:
        System.out.println("Invalid algorithm selection");
}
Enter fullscreen mode Exit fullscreen mode

6. Running the Simulation

With CloudSim set up, VMs, cloudlets, and the algorithm specified, we can start the simulation:

CloudSim.startSimulation();
CloudSim.stopSimulation();
Enter fullscreen mode Exit fullscreen mode

After stopping the simulation, the results of each cloudlet (task) are printed, showing the status, datacenter ID, VM ID, and timing information.

List<Cloudlet> newList = broker.getCloudletReceivedList();
printCloudletList(newList);
Enter fullscreen mode Exit fullscreen mode

Example Output

The output lists each cloudlet with details about its execution:

========== OUTPUT ==========
CloudletID     STATUS    DataCenterID   VM ID    Time      StartTime       FinishTime
0              SUCCESS   0              1        10.0      0.5            10.5
1              SUCCESS   0              0        8.5       1.0            9.5
Enter fullscreen mode Exit fullscreen mode

Detailed Explanation of Scheduling Algorithms

  1. Round Robin (RR): Allocates tasks to VMs in a cyclical fashion, balancing workload evenly but without optimizing task completion times.
  2. First-Come-First-Serve (FCFS): Assigns tasks based on their arrival order, suitable for real-time or simple scheduling.
  3. Shortest Job First (SJF): Prioritizes tasks with shorter execution times, reducing overall latency.
  4. Genetic Algorithm (GA): Uses an evolutionary approach, attempting to find an optimal task-VM allocation by generating multiple candidate solutions.
  5. Ant Colony Optimization (ACO): Inspired by the foraging behavior of ants, ACO dynamically finds an optimized path to assign tasks to VMs.

Each algorithm has strengths suited to specific scenarios, from load balancing to minimizing latency.

Conclusion

By implementing multiple scheduling algorithms, this project offers insights into how various approaches can impact the performance of cloud environments. The CloudSim simulation framework made it easy to prototype, test, and evaluate these algorithms under different conditions, and the code structure is flexible enough for future expansions.

With this project, I learned not only about scheduling algorithms but also about setting up and configuring a cloud environment. You can find the project code on my GitHub: CloudSim-Examples.

Feel free to explore the code, experiment with the algorithms, and contribute to further optimizations.

Top comments (0)