use crate::awake::{Geometry, Section};
use crate::{Exec, PinStrategy, Pool};
use std::time::Duration;
fn own_cpus() -> Vec<usize> {
unsafe {
let mut set: libc::cpu_set_t = std::mem::zeroed();
let r = libc::sched_getaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &mut set);
assert_eq!(r, 0, "sched_getaffinity failed");
(0..libc::CPU_SETSIZE as usize)
.filter(|&cpu| libc::CPU_ISSET(cpu, &set))
.collect()
}
}
fn set_own_cpus(cpus: &[usize]) -> bool {
unsafe {
let mut set: libc::cpu_set_t = std::mem::zeroed();
libc::CPU_ZERO(&mut set);
for &cpu in cpus {
libc::CPU_SET(cpu, &mut set);
}
libc::sched_setaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &set) == 0
}
}
fn pool(width: usize) -> Pool {
Pool::builder()
.threads(width)
.idle_spin(Duration::ZERO)
.acquire_timeout(Duration::from_secs(10))
.build()
.unwrap()
}
fn on_two_cpus(f: impl FnOnce()) {
let all = own_cpus();
if all.len() < 3 {
eprintln!("skipped: need at least 3 allowed CPUs to restrict to 2");
return;
}
if !set_own_cpus(&all[..2]) {
eprintln!("skipped: cannot restrict the calling thread to two CPUs");
return;
}
f();
assert!(set_own_cpus(&all), "restoring the calling thread's mask");
}
#[test]
fn a_section_carries_the_oversubscription_flag_it_was_built_with() {
unsafe fn call(_: *const (), _: &Section, _: usize) {}
unsafe fn slot_of(_: *const (), pos: usize) -> usize {
pos
}
let geometry = |over| Geometry {
dispatched: 0,
size: 1,
over,
pin: false,
};
let quiet = Section::new(
call,
std::ptr::null(),
slot_of,
std::ptr::null(),
geometry(false),
None,
);
assert!(!quiet.over);
let crowded = Section::new(
call,
std::ptr::null(),
slot_of,
std::ptr::null(),
geometry(true),
None,
);
assert!(crowded.over);
}
#[test]
fn an_oversubscribed_root_keeps_the_flag_through_reborrows_splits_and_fork_children() {
on_two_cpus(|| {
let width = 4;
let pool = pool(width);
let mut exec = Exec::new(&pool, width).unwrap();
exec.team(|mut team| {
assert!(team.over, "4 participants on 2 CPUs is oversubscribed");
let over = team.reborrow().over; assert!(over);
team.reborrow()
.split(&[2, 2], |_, child| {
assert!(child.over, "a split child of an oversubscribed root");
child.run(|_, _| {});
})
.unwrap();
team.fork(2, |_, child| {
assert!(child.over, "a fork child of an oversubscribed root");
});
})
.expect("an idle pool");
drop(exec);
let team = pool.into_team(width).expect("an idle pool");
let mut exec = team.exec(width);
assert!(exec.team(|t| t.over).expect("a team token opens its scope"));
exec.reborrow().fork(2, |_, mut child| {
assert!(
child
.team(|t| t.over)
.expect("a fork child carries the sub-team"),
"a fork child of an oversubscribed team token"
);
});
});
}
#[test]
fn a_root_scope_that_fits_the_callers_cpus_is_not_oversubscribed() {
let all = own_cpus();
let width = all.len().min(3);
let pool = pool(width);
let mut exec = Exec::new(&pool, width).unwrap();
exec.team(|team| {
assert!(
!team.over,
"{width} participants on {} CPUs is not oversubscribed",
all.len()
);
})
.expect("an idle pool");
}
#[test]
fn a_team_scope_takes_the_oversubscription_from_the_thread_that_enters_it() {
let all = own_cpus();
if all.len() < 4 {
eprintln!("skipped: need at least 4 allowed CPUs for a one-CPU scope mask");
return;
}
let width = 4;
let pool = pool(width);
let team = pool.into_team(width).expect("an idle pool");
let mut exec = team.exec(width);
let scoped = std::thread::scope(|s| {
s.spawn(|| {
if !set_own_cpus(&all[..1]) {
eprintln!("skipped: cannot restrict the calling thread to one CPU");
return None;
}
exec.team(|mut team| {
let root = team.over;
let child = std::sync::atomic::AtomicBool::new(false);
team.reborrow().fork(2, |_, child_team| {
child.store(child_team.over, std::sync::atomic::Ordering::Relaxed);
});
(root, child.load(std::sync::atomic::Ordering::Relaxed))
})
.ok()
})
.join()
.unwrap()
});
drop(exec);
let Some(scoped) = scoped else { return };
assert!(
scoped.0,
"four participants on a one-CPU scope mask is oversubscribed"
);
assert!(scoped.1, "a fork child inherits the root's pressure");
}
#[test]
fn a_root_scope_chooses_the_throttled_spin_budget_when_oversubscribed() {
use crate::park::THROTTLED_POLLS;
let budget = 50_000;
let pool = |width| {
Pool::builder()
.threads(width)
.spin_polls(budget)
.acquire_timeout(Duration::from_secs(10))
.build()
.unwrap()
};
on_two_cpus(|| {
let crowded = pool(4);
let mut exec = Exec::new(&crowded, 4).unwrap();
exec.team(|team| {
let awake = team.awake.expect("a pool");
assert!(!team.ids.is_empty());
for &id in team.ids {
assert_eq!(awake.slot_spin_budget(id), THROTTLED_POLLS);
}
})
.expect("an idle pool");
drop(exec);
let team = crowded.into_team(4).expect("an idle pool");
let mut exec = team.exec(4);
exec.team(|t| {
for &id in t.ids {
assert_eq!(t.awake.unwrap().slot_spin_budget(id), THROTTLED_POLLS);
}
})
.expect("a team token opens its scope");
let fits = pool(2);
let mut exec = Exec::new(&fits, 2).unwrap();
exec.team(|team| {
for &id in team.ids {
assert_eq!(team.awake.unwrap().slot_spin_budget(id), budget);
}
})
.expect("an idle pool");
});
}
fn spinning_pool(width: usize, budget: u32) -> Pool {
Pool::builder()
.threads(width)
.spin_polls(budget)
.acquire_timeout(Duration::from_secs(10))
.build()
.unwrap()
}
fn member_budgets(team: &crate::Team) -> Vec<u32> {
let awake = &team.pool().awake;
team.reservation_ids()
.iter()
.map(|&id| awake.slot_spin_budget(id))
.collect()
}
#[test]
fn a_team_token_minted_by_its_leader_keeps_the_pool_spin_budget() {
use crate::park::THROTTLED_POLLS;
let budget = 50_000;
on_two_cpus(|| {
let set = own_cpus();
let team = spinning_pool(2, budget).into_team(2).expect("an idle pool");
assert_eq!(own_cpus(), set, "into_team leaves the caller's mask");
assert_eq!(crate::pin::available(), 2, "the leader leads |S| CPUs");
let exec = team.exec(2);
let budgets = member_budgets(&team);
assert!(!budgets.is_empty());
assert!(
budgets.iter().all(|&b| b == budget && b != THROTTLED_POLLS),
"2 participants on a 2-CPU S, token minted by the team's leader: {budgets:?}"
);
drop(exec);
});
}
#[test]
fn a_fork_sized_child_of_a_team_token_is_not_oversubscribed() {
let budget = 50_000;
on_two_cpus(|| {
let set = own_cpus();
let team = spinning_pool(2, budget).into_team(2).expect("an idle pool");
assert_eq!(own_cpus(), set, "into_team leaves the caller's mask");
let seen = std::sync::Mutex::new(None);
team.exec(2)
.fork_sized(&[2], |_, mut child| {
let got = child
.team(|t| {
let awake = t.awake.expect("a carried sub-team");
let budgets: Vec<u32> =
t.ids.iter().map(|&id| awake.slot_spin_budget(id)).collect();
(t.over, budgets)
})
.expect("a fork child carries the sub-team");
*seen.lock().unwrap() = Some(got);
})
.unwrap();
let (over, budgets) = seen.into_inner().unwrap().expect("the child ran");
assert!(!over, "a fork_sized child of a team token");
assert!(!budgets.is_empty(), "the child carries the member");
assert!(budgets.iter().all(|&b| b == budget), "{budgets:?}");
});
}
#[test]
fn a_team_created_on_too_few_cpus_is_still_paced() {
use crate::park::THROTTLED_POLLS;
let budget = 50_000;
on_two_cpus(|| {
let set = own_cpus();
let team = spinning_pool(4, budget).into_team(4).expect("an idle pool");
assert_eq!(own_cpus(), set, "an oversubscribed team pins no leader");
assert_eq!(crate::pin::available(), 2);
let exec = team.exec(4);
let budgets = member_budgets(&team);
assert!(!budgets.is_empty());
assert!(
budgets.iter().all(|&b| b == THROTTLED_POLLS),
"4 participants on a 2-CPU set: {budgets:?}"
);
let over = std::sync::atomic::AtomicBool::new(false);
exec.fork_sized(&[4], |_, mut child| {
let o = child
.team(|t| t.over)
.expect("a fork child carries the sub-team");
over.store(o, std::sync::atomic::Ordering::Relaxed);
})
.unwrap();
assert!(
over.into_inner(),
"a fork_sized child of an oversubscribed team"
);
});
}
struct HostTopology;
impl Drop for HostTopology {
fn drop(&mut self) {
crate::pin::set_test_topology(None);
}
}
fn participant_masks(pool: &Pool) -> Vec<Vec<usize>> {
let seen = std::sync::Mutex::new(Vec::new());
let mut exec = Exec::new(pool, 2).expect("a two-wide token");
exec.team(|mut team| {
team.reborrow().run_with(|p| {
seen.lock().unwrap().push((p.index(), own_cpus()));
});
})
.expect("an idle pool");
let mut rows = seen.into_inner().unwrap();
rows.sort_by_key(|&(index, _)| index);
assert_eq!(rows.len(), 2, "both participants ran a share");
rows.into_iter().map(|(_, mask)| mask).collect()
}
#[test]
fn a_root_scope_places_under_the_pools_pin_strategy() {
let set = own_cpus();
if set.len() < 4 {
eprintln!(
"skipped: needs 4 allowed CPUs to build two synthetic nodes, this process has {}",
set.len()
);
return;
}
let nodes = vec![vec![set[0], set[1]], vec![set[2], set[3]]];
let topology: &'static crate::pin::Topology =
Box::leak(Box::new(crate::pin::Topology::from_nodes(&nodes)));
crate::pin::set_test_topology(Some(topology));
let _host = HostTopology;
let member_cpu = |strategy: PinStrategy| {
let pool = Pool::builder()
.threads(2)
.pin_strategy(strategy)
.idle_spin(Duration::ZERO)
.acquire_timeout(Duration::from_secs(10))
.build()
.unwrap();
assert_eq!(pool.pin_strategy(), strategy);
let masks = participant_masks(&pool);
assert_eq!(masks[0], vec![set[0]], "the leader's CPU for {strategy:?}");
assert_eq!(pool.pin_skips(), 0, "nothing skipped for {strategy:?}");
masks[1].clone()
};
assert_eq!(
member_cpu(PinStrategy::Spread),
vec![set[2]],
"Spread puts the second participant on node 2's first CPU"
);
assert_eq!(
member_cpu(PinStrategy::Pack),
vec![set[1]],
"Pack puts it on node 1's second CPU"
);
assert_eq!(
own_cpus(),
set,
"the calling thread's mask after both scopes"
);
}
#[test]
fn a_mask_changed_by_another_library_is_seen_by_the_scope_that_follows() {
use crate::park::THROTTLED_POLLS;
let all = own_cpus();
if all.len() < 4 {
eprintln!("skipped: need at least 4 allowed CPUs for a 4-participant team");
return;
}
let budget = 50_000;
let team = spinning_pool(4, budget).into_team(4).expect("an idle pool");
drop(team.exec(4));
if !set_own_cpus(&all[..1]) {
eprintln!("skipped: cannot restrict the calling thread to one CPU");
return;
}
for _ in 0..3 {
team.exec(2)
.team(|_| {})
.expect("a narrower team token opens its scope");
}
let exec = team.exec(4);
let stale = member_budgets(&team);
assert!(
stale.iter().all(|&b| b == budget),
"neither a mint nor a narrower scope reads the mask: {stale:?}"
);
drop(exec);
let inside = std::sync::Mutex::new(None);
team.exec(4)
.team(|t| {
let awake = t.awake.expect("a pool");
let budgets: Vec<u32> = t.ids.iter().map(|&id| awake.slot_spin_budget(id)).collect();
*inside.lock().unwrap() = Some((t.over, budgets));
})
.expect("a team token opens its scope");
let (over, budgets) = inside.into_inner().unwrap().expect("the scope ran");
assert!(!over, "the scope itself keeps the stale decision");
assert!(
budgets.iter().all(|&b| b == budget),
"and the stale budget: {budgets:?}"
);
let exec = team.exec(4);
let throttled = member_budgets(&team);
assert!(
throttled.iter().all(|&b| b == THROTTLED_POLLS),
"the pin section refreshed the memo: {throttled:?}"
);
drop(exec);
assert!(set_own_cpus(&all[..4]), "widening the calling thread");
team.exec(4)
.team(|_| {})
.expect("a whole-team scope records the placement");
drop(team.exec(4));
assert!(set_own_cpus(&all[..2]), "two CPUs that hold the home");
team.exec(2)
.team(|_| {})
.expect("a narrower scope pins its leader home");
let exec = team.exec(4);
let refreshed = member_budgets(&team);
assert!(
refreshed.iter().all(|&b| b == THROTTLED_POLLS),
"the home pin of the narrower scope cleared the memo: {refreshed:?}"
);
drop(exec);
assert!(set_own_cpus(&all), "restoring the calling thread mask");
}
#[test]
fn a_pool_with_pinning_off_never_refreshes_the_count() {
let all = own_cpus();
if all.len() < 4 {
eprintln!("skipped: need at least 4 allowed CPUs for a 4-participant team");
return;
}
let budget = 50_000;
let pool = Pool::builder()
.threads(4)
.pin_participants(false)
.spin_polls(budget)
.acquire_timeout(Duration::from_secs(10))
.build()
.unwrap();
let team = pool.into_team(4).expect("an idle pool");
drop(team.exec(4));
if !set_own_cpus(&all[..1]) {
eprintln!("skipped: cannot restrict the calling thread to one CPU");
return;
}
for _ in 0..3 {
team.exec(4)
.team(|_| {})
.expect("a team token opens its scope");
let exec = team.exec(4);
let kept = member_budgets(&team);
assert!(
kept.iter().all(|&b| b == budget),
"pinning off runs no pin section: {kept:?}"
);
drop(exec);
}
assert!(set_own_cpus(&all), "restoring the calling thread mask");
}