use ::anyhow::Result;
use ::demikernel::{
runtime::types::demi_sgarray_t,
LibOS,
LibOSName,
};
const SGA_SIZE_SMALL: usize = 64;
const SGA_SIZE_BIG: usize = 1280;
fn do_test_unit_sga_alloc_free_single(size: usize) -> Result<()> {
let libos_name: LibOSName = match LibOSName::from_env() {
Ok(libos_name) => libos_name.into(),
Err(e) => anyhow::bail!("{:?}", e),
};
let mut libos: LibOS = match LibOS::new(libos_name, None) {
Ok(libos) => libos,
Err(e) => anyhow::bail!("failed to initialize libos: {:?}", e),
};
let sga: demi_sgarray_t = match libos.sgaalloc(size) {
Ok(sga) => sga,
Err(e) => anyhow::bail!("failed to allocate sga: {:?}", e),
};
match libos.sgafree(sga) {
Ok(()) => Ok(()),
Err(e) => anyhow::bail!("failed to release sga: {:?}", e.cause),
}
}
#[test]
fn test_unit_sga_alloc_free_single_small() -> Result<()> {
do_test_unit_sga_alloc_free_single(SGA_SIZE_SMALL)
}
#[test]
fn test_unit_sga_alloc_free_single_big() -> Result<()> {
do_test_unit_sga_alloc_free_single(SGA_SIZE_BIG)
}
fn do_test_unit_sga_alloc_free_loop_tight(size: usize) -> Result<()> {
let libos_name: LibOSName = match LibOSName::from_env() {
Ok(libos_name) => libos_name.into(),
Err(e) => anyhow::bail!("{:?}", e),
};
let mut libos: LibOS = match LibOS::new(libos_name, None) {
Ok(libos) => libos,
Err(e) => anyhow::bail!("failed to initialize libos: {:?}", e),
};
for _ in 0..1_000_000 {
let sga: demi_sgarray_t = match libos.sgaalloc(size) {
Ok(sga) => sga,
Err(e) => {
anyhow::bail!("failed to allocate sga: {:?}", e)
},
};
match libos.sgafree(sga) {
Ok(()) => (),
Err(e) => {
anyhow::bail!("failed to release sga: {:?}", e.cause)
},
};
}
Ok(())
}
#[test]
fn test_unit_sga_alloc_free_loop_tight_small() -> Result<()> {
do_test_unit_sga_alloc_free_loop_tight(SGA_SIZE_SMALL)
}
#[test]
fn test_unit_sga_alloc_free_loop_tight_big() -> Result<()> {
do_test_unit_sga_alloc_free_loop_tight(SGA_SIZE_BIG)
}
fn do_test_unit_sga_alloc_free_loop_decoupled(size: usize) -> Result<()> {
let mut sgas: Vec<demi_sgarray_t> = Vec::with_capacity(1_000);
let libos_name: LibOSName = match LibOSName::from_env() {
Ok(libos_name) => libos_name.into(),
Err(e) => anyhow::bail!("{:?}", e),
};
let mut libos: LibOS = match LibOS::new(libos_name, None) {
Ok(libos) => libos,
Err(e) => anyhow::bail!("failed to initialize libos: {:?}", e),
};
for _ in 0..1_000 {
for _ in 0..1_000 {
let sga: demi_sgarray_t = match libos.sgaalloc(size) {
Ok(sga) => sga,
Err(e) => {
anyhow::bail!("failed to allocate sga: {:?}", e)
},
};
sgas.push(sga);
}
for _ in 0..1_000 {
let sga: demi_sgarray_t = match sgas.pop() {
Some(sga) => sga,
None => {
anyhow::bail!("pop from empty vector?")
},
};
match libos.sgafree(sga) {
Ok(()) => (),
Err(e) => {
anyhow::bail!("failed to release sga: {:?}", e.cause)
},
};
}
}
Ok(())
}
#[test]
fn test_unit_sga_alloc_free_loop_decoupled_small() -> Result<()> {
do_test_unit_sga_alloc_free_loop_decoupled(SGA_SIZE_SMALL)
}
#[test]
fn test_unit_sga_alloc_free_loop_decoupled_big() -> Result<()> {
do_test_unit_sga_alloc_free_loop_decoupled(SGA_SIZE_BIG)
}