Skip to content

Commit

Permalink
Added multicore test. Fixes #162
Browse files Browse the repository at this point in the history
  • Loading branch information
jounathaen committed Feb 14, 2025
1 parent 2ba7eb3 commit 53867a6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ memory_addresses = { version = "0.2.2", default-features = false, features = [
[dev-dependencies]
assert_fs = "1"
criterion = "0.5"
regex = { version = "1.11.1", default-features = false, features = ["unicode-perl"] }
44 changes: 44 additions & 0 deletions tests/multicore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
mod common;

use byte_unit::{Byte, Unit};
use common::{build_hermit_bin, check_result};
use regex::Regex;
use uhyvelib::{
params::{Output, Params},
vm::UhyveVm,
};

#[test]
fn multicore_test() {
env_logger::try_init().ok();
let bin_path = build_hermit_bin("multi-thread");

let re = Regex::new(r"Speedup: [\d]+us / \d+us =\s*([\d.]+)").unwrap();

for nr_cpus in [2, 4] {
println!("Launching kernel {}", bin_path.display());
let params = Params {
cpu_count: nr_cpus.try_into().unwrap(),
memory_size: Byte::from_u64_with_unit(64, Unit::MiB)
.unwrap()
.try_into()
.unwrap(),
output: Output::Buffer,
..Default::default()
};
let vm = UhyveVm::new(bin_path.clone(), params).unwrap();
let res = vm.run(None);
check_result(&res);

let outp = res.output.unwrap();
let caps = re
.captures(outp.as_str())
.expect("Speedup not present in test output");
dbg!(&caps);
let speedup = caps.get(1).unwrap().as_str().parse::<f64>().unwrap();
dbg!(&speedup);
if speedup < nr_cpus as f64 * 0.66 {
panic!("Speedup of {speedup} is not enough for a CPU count of {nr_cpus}");
}
}
}
47 changes: 47 additions & 0 deletions tests/test-kernels/src/bin/multi-thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::{hint::black_box, thread, time::Instant};

#[cfg(target_os = "hermit")]
use hermit as _;

fn gauss_sum(n: u64) -> u64 {
if n == 0 {
return 0;
}
n + gauss_sum(n - 1)
}

fn main() {
println!("Multi threading test");

const CNT: usize = 50000;
const NR_THREADS: usize = 4;

let start = Instant::now();
for _ in 0..CNT {
let _result = black_box(gauss_sum(black_box(10000)));
}
let duration_single_thread = start.elapsed(); // Calculate elapsed time
println!("Duration single thread: {duration_single_thread:?}");

let mut threads = Vec::with_capacity(NR_THREADS);
let start = Instant::now();
for _ in 0..NR_THREADS {
threads.push(thread::spawn(|| {
for _ in 0..(CNT / NR_THREADS) {
let _result = black_box(gauss_sum(black_box(10000)));
}
}));
}
for t in threads {
t.join().unwrap();
}

let duration_multi_thread = start.elapsed(); // Calculate elapsed time
println!("Duration mutli thread: {duration_multi_thread:?}");
println!(
"Speedup: {}us / {}us = {:.3}",
duration_single_thread.as_micros(),
duration_multi_thread.as_micros(),
duration_single_thread.as_micros() as f64 / duration_multi_thread.as_micros() as f64
);
}

0 comments on commit 53867a6

Please # to comment.