Cloudbus simulation in Rust programming language.

4 views
Skip to first unread message

Ian Ajzenszmidt

unread,
Feb 24, 2025, 1:48:47 PMFeb 24
to mlug-au
an@ian-PORTEGE-Z930:~$ nano cloudbusdemo3.rs
ian@ian-PORTEGE-Z930:~$ rustc cloudbusdemo3.rs
ian@ian-PORTEGE-Z930:~$ ./cloudbusdemo3
CloudBus Simulation Started!
Based on: Buyya, R., Pandey, S. and Vecchiola, C. (2012) 'Market-Oriented Cloud Computing and the Cloudbus Toolkit', arXiv preprint, submitted 23 March 2012.

--- Simulation Step 1 ---
Task 1 assigned to Server-A with initial duration 5.
Task 2 assigned to Server-B with initial duration 8.
Task 3 assigned to Server-C with initial duration 3.
Task 4 assigned to Server-A with initial duration 7.
Task 5 assigned to Server-B with initial duration 4.
Processing Task 1 on Server-A. Remaining time: 4
Processing Task 2 on Server-B. Remaining time: 7
Processing Task 3 on Server-C. Remaining time: 2
Processing Task 4 on Server-A. Remaining time: 6
Processing Task 5 on Server-B. Remaining time: 3

--- Simulation Step 2 ---
Processing Task 1 on Server-A. Remaining time: 3
Processing Task 2 on Server-B. Remaining time: 6
Processing Task 3 on Server-C. Remaining time: 1
Processing Task 4 on Server-A. Remaining time: 5
Processing Task 5 on Server-B. Remaining time: 2

--- Simulation Step 3 ---
Processing Task 1 on Server-A. Remaining time: 2
Processing Task 2 on Server-B. Remaining time: 5
Processing Task 3 on Server-C. Remaining time: 0
Processing Task 4 on Server-A. Remaining time: 4
Processing Task 5 on Server-B. Remaining time: 1
Task 3 on Server-C completed.

--- Simulation Step 4 ---
Processing Task 1 on Server-A. Remaining time: 1
Processing Task 2 on Server-B. Remaining time: 4
Processing Task 4 on Server-A. Remaining time: 3
Processing Task 5 on Server-B. Remaining time: 0
Task 5 on Server-B completed.

--- Simulation Step 5 ---
Processing Task 1 on Server-A. Remaining time: 0
Processing Task 2 on Server-B. Remaining time: 3
Processing Task 4 on Server-A. Remaining time: 2
Task 1 on Server-A completed.

--- Simulation Step 6 ---
Processing Task 2 on Server-B. Remaining time: 2
Processing Task 4 on Server-A. Remaining time: 1

--- Simulation Step 7 ---
Processing Task 2 on Server-B. Remaining time: 1
Processing Task 4 on Server-A. Remaining time: 0
Task 4 on Server-A completed.

--- Simulation Step 8 ---
Processing Task 2 on Server-B. Remaining time: 0
Task 2 on Server-B completed.

CloudBus Simulation Completed in 8 steps.
ian@ian-PORTEGE-Z930:~$ cat cloudbusdemo3.rs
use std::thread;
use std::time::Duration;

/// CloudBus Simulation Demo Program
///
/// This simulation is inspired by the approaches described in the arXiv document:
///
/// Buyya, R., Pandey, S. and Vecchiola, C. (2012) 'Market-Oriented Cloud Computing and the Cloudbus Toolkit',
/// arXiv preprint, submitted 23 March 2012.
///
/// The program simulates task scheduling on a set of cloud servers (the "CloudBus") using a simple round-robin
/// assignment. It outputs progress status messages at each simulation step as it processes each task.
/// This implementation uses only Rust’s standard library (with no external crates or dependencies).
struct Task {
    id: usize,
    remaining: u32, // Number of simulation steps remaining for this task.
    assigned_server: Option<&'static str>,
}

impl Task {
    /// Creates a new task with a specified id and duration.
    ///
    /// # Arguments
    ///
    /// * `id` - A unique identifier for the task.
    /// * `duration` - The number of simulation steps required to complete the task.
    fn new(id: usize, duration: u32) -> Task {
        Task {
            id,
            remaining: duration,
            assigned_server: None,
        }
    }
}

fn main() {
    println!("CloudBus Simulation Started!");
    // Output a message that credits the original arXiv document.
    println!("Based on: Buyya, R., Pandey, S. and Vecchiola, C. (2012) 'Market-Oriented Cloud Computing and the Cloudbus Toolkit', arXiv preprint, submitted 23 March 2012.");

    // Define available servers (simulating cloud nodes in the CloudBus).
    let servers = ["Server-A", "Server-B", "Server-C"];

    // Create a list of tasks with predetermined durations (in simulation steps).
    let mut tasks = vec![
        Task::new(1, 5),
        Task::new(2, 8),
        Task::new(3, 3),
        Task::new(4, 7),
        Task::new(5, 4),
    ];

    let mut step = 0;
    // Run the simulation loop until all tasks are processed.
    while !tasks.is_empty() {
        step += 1;
        println!("\n--- Simulation Step {} ---", step);

        // Assign tasks to servers if not already assigned.
        for task in tasks.iter_mut() {
            if task.assigned_server.is_none() {
                // Use a round-robin strategy for server assignment.
                let server = servers[(task.id - 1) % servers.len()];
                task.assigned_server = Some(server);
                println!(
                    "Task {} assigned to {} with initial duration {}.",
                    task.id, server, task.remaining
                );
            }
        }

        // Process each task for one simulation step.
        for task in tasks.iter_mut() {
            if task.remaining > 0 {
                task.remaining -= 1;
                println!(
                    "Processing Task {} on {}. Remaining time: {}",
                    task.id,
                    task.assigned_server.unwrap(),
                    task.remaining
                );
            }
        }

        // Remove tasks that are completed.
        tasks.retain(|task| {
            if task.remaining == 0 {
                println!(
                    "Task {} on {} completed.",
                    task.id,
                    task.assigned_server.unwrap()
                );
                false
            } else {
                true
            }
        });

        // Sleep to simulate real-time progress between steps.
        thread::sleep(Duration::from_millis(500));
    }

    println!("\nCloudBus Simulation Completed in {} steps.", step);
}

/*
Harvard style citation:
Buyya, R., Pandey, S. and Vecchiola, C. (2012) 'Market-Oriented Cloud Computing and the Cloudbus Toolkit',
arXiv preprint, submitted 23 March 2012.
*/
ian@ian-PORTEGE-Z930:~$
Reply all
Reply to author
Forward
0 new messages