RustForge

rust-kernel

Rustbelt: a hobby x86_64 kernel written in Rust

rust-kernel / user / spin.rs
701 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//! A CPU hog: busy-loops without ever making a syscall in between. The timer
//! interrupt still preempts it, so everything else keeps running.
//! `spin` does 3 rounds of 30 million additions; `spin <n>` does n rounds.
#![no_std]
#![no_main]

#[macro_use]
#[path = "ulib.rs"]
mod ulib;

fn main() -> i64 {
    println!("[spin] pid {}: spinning without syscalls...", ulib::getpid());
    let rounds: u64 = ulib::arg(1).and_then(|a| a.parse().ok()).unwrap_or(3);
    let mut count: u64 = 0;
    for round in 1..=rounds {
        for _ in 0..30_000_000 {
            count = core::hint::black_box(count + 1);
        }
        println!("[spin] round {} done (count = {})", round, count);
    }
    0
}