//! The scheduler's glue: its counters (`running`, `pending`, //! `io_waiting`), idle and deadlock detection, and the reactor's wake-ups. //! The hand-offs it is built on have loom models (`handoff`, `timers`, //! `otp`, `inbox`); these run whole programs of test actors, on one thread //! and on several, and check what the scheduler decides. //! //! Test actors talk through statics: each test runs alone ([`test_turn`]). //! An assertion that fails in an actor aborts the test binary (a panic //! cannot unwind out of its `extern "C"` entry), as in the crate's other //! actor tests; a wake-up that never comes fails [`yield_until`]. mod dist; #[cfg(feature = "tls")] mod transport; use std::io::{ErrorKind, Read, Write}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::thread::JoinHandle; use std::time::{Duration, Instant}; use mio::net::UnixStream; use mio::{Interest, Token}; use crate::abi::{Ctx, UNIT, Value, YIELD_NONE}; use crate::fiber::Stack; use crate::heap::drop_value; use crate::otp::{Received, Tags, Timeout, Wait}; use super::{ Body, EntryFn, Exit, HANDLER_STACK_POOL, HandlerStacks, STACK_SIZE, Shared, Tie, WARM_HANDLER_STACKS, park, preempt_root, root_stack_size, run, spawn_actor, test_turn, this, }; /// Thread counts each program runs with: one (the interpreter's order), /// and several (stealing, one idle thread for all). const THREADS: [usize; 2] = [1, 4]; /// Run `entry` as the root actor on `threads` threads, as `tl_main` does, /// then check that the scheduler is at rest: nothing runs, is queued or /// waits on a socket, and every actor is gone. Returns the root's exit. fn run_root(threads: usize, entry: EntryFn) -> Exit { run_root_clock(threads, false, entry) } /// `run_root` with the `LOUISE_VIRTUAL_TIME` opt-in set explicitly, so /// the tests do not depend on the environment (ADR 0055). fn run_root_clock(threads: usize, virtual_time: bool, entry: EntryFn) -> Exit { let _turn = test_turn(); let mut sched = Shared::new(threads); sched.virtual_time = virtual_time; let (root, _) = spawn_actor( &sched, Body::Entry(entry), root_stack_size(), None, Tie::None, ); let exit = run(&sched, root, threads); crate::console::flush(); assert_eq!(sched.running.load(Ordering::SeqCst), 0, "running"); assert_eq!(sched.pending.load(Ordering::SeqCst), 0, "pending"); assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0, "io_waiting"); assert!(sched.control().io_waiters.is_empty(), "io_waiters"); assert!(sched.actors.all().is_empty(), "actors left"); exit } /// Spawn an actor running `entry`, untied to the running actor. /// /// # Safety /// `ctx` must be the running actor's context. unsafe fn spawn(ctx: *mut Ctx, entry: EntryFn) -> i64 { // SAFETY: forwarded caller guarantee. let (a, sched) = unsafe { this(ctx) }; spawn_actor( sched, Body::Entry(entry), STACK_SIZE, Some(a.pid), Tie::None, ) .0 } /// How long [`yield_until`] waits before it fails: far more than any /// condition here takes (microseconds), so a lost wake-up fails the test /// instead of hanging it. const PATIENCE: Duration = Duration::from_secs(10); /// Yield until `done` holds (`what`, for the failure); the other actors /// run meanwhile. /// /// # Safety /// `ctx` must be the running actor's context, on its own stack. unsafe fn yield_until(ctx: *mut Ctx, what: &str, done: impl Fn(&Shared) -> bool) { let limit = Instant::now() + PATIENCE; // SAFETY: forwarded caller guarantee; the scheduler outlives its actors. unsafe { let (_, sched) = this(ctx); while !done(sched) { assert!(Instant::now() < limit, "still waiting: {what}"); preempt_root(ctx); } } } /// Wait for a message nobody sends; returns once the actor is stopped. /// /// # Safety /// `ctx` must be the running actor's context. unsafe fn wait_forever(ctx: *mut Ctx) { // SAFETY: forwarded caller guarantee. unsafe { let (a, sched) = this(ctx); let tag = sched.tag(); let received = a.with_proc(|p| p.receive(a.inbox(), Tags::One(tag), false, Timeout::Never)); assert!(matches!(received, Received::Parked(_)), "no message"); let woken = park(ctx); drop_value(ctx, woken); assert_ne!((*ctx).yielding, YIELD_NONE, "only an exit ends the wait"); } } /// A connected pair of sockets; the first registered with the running /// actor's reactor, for reading only (so its only events are the second /// writing to it), with its token. /// /// # Safety /// `ctx` must be the running actor's context. unsafe fn socket_pair(ctx: *mut Ctx) -> (UnixStream, UnixStream, Token) { // SAFETY: forwarded caller guarantee. let (_, sched) = unsafe { this(ctx) }; let (mut reader, writer) = UnixStream::pair().expect("a socket pair"); let token = sched.io_token(); sched .with_registry(|r| r.register(&mut reader, token, Interest::READABLE)) .expect("registered"); (reader, writer, token) } /// Park on socket `token`, as `net` does, unless an event came since the /// count `seen`; false if the actor was stopped instead. /// /// # Safety /// `ctx` must be the running actor's context. unsafe fn wait_on_socket(ctx: *mut Ctx, token: Token, seen: u64) -> bool { // SAFETY: forwarded caller guarantee. unsafe { crate::net::wait_for(ctx, token, seen) } } static COUNTED_TOKEN: AtomicUsize = AtomicUsize::new(0); /// Yields a few times, then finishes. unsafe extern "C" fn yields_then_ends(ctx: *mut Ctx) -> Value { for _ in 0..3 { // SAFETY: an actor's entry, on its own stack. unsafe { preempt_root(ctx) }; } UNIT } /// Waits on the socket in `COUNTED_TOKEN` until woken, then finishes. unsafe extern "C" fn waits_on_the_socket(ctx: *mut Ctx) -> Value { let token = Token(COUNTED_TOKEN.load(Ordering::SeqCst)); // SAFETY: an actor's entry. unsafe { let seen = this(ctx).1.io_events(); assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); } UNIT } /// Spawns actors that yield and finish, and actors that wait on a socket /// until the root writes to it; checks the counters while they wait. unsafe extern "C" fn counts_actors(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { let (reader, mut writer, token) = socket_pair(ctx); COUNTED_TOKEN.store(token.0, Ordering::SeqCst); for _ in 0..4 { spawn(ctx, yields_then_ends); spawn(ctx, waits_on_the_socket); } // Only the root runs, nothing is queued, four wait on the socket. yield_until(ctx, "the waiters to park", |s| { s.io_waiting.load(Ordering::SeqCst) == 4 && s.running.load(Ordering::SeqCst) == 1 && s.pending.load(Ordering::SeqCst) == 0 && s.actors.all().len() == 5 }); let (_, sched) = this(ctx); assert_eq!(sched.control().io_waiters[&token].len(), 4); writer.write_all(b"x").expect("written"); yield_until(ctx, "the waiters to finish", |s| s.actors.all().len() == 1); assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); assert!(!sched.control().io_waiters.contains_key(&token)); drop(reader); } UNIT } #[test] fn counters_rest_once_actors_ran_waited_on_a_socket_and_finished() { for threads in THREADS { assert_eq!( run_root(threads, counts_actors), Exit::Normal, "{threads} threads" ); } } /// Waits for a message nobody sends. unsafe extern "C" fn waits_forever(ctx: *mut Ctx) -> Value { // SAFETY: an actor's entry. unsafe { wait_forever(ctx) }; UNIT } /// Spawns an actor waiting forever, then waits forever too. unsafe extern "C" fn all_wait_forever(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { spawn(ctx, waits_forever); wait_forever(ctx); } UNIT } #[test] fn actors_all_waiting_for_messages_are_a_deadlock() { for threads in THREADS { assert_eq!( run_root(threads, all_wait_forever), Exit::Crashed("deadlock: every actor is waiting for a message".into()), "{threads} threads" ); } } static BUSY_STARTED: AtomicUsize = AtomicUsize::new(0); static BUSY_DONE: AtomicUsize = AtomicUsize::new(0); /// Counts itself started, then holds its thread without yielding until /// the other busy actor started too: it can only start on another thread. unsafe extern "C" fn busy_until_both_run(_ctx: *mut Ctx) -> Value { BUSY_STARTED.fetch_add(1, Ordering::SeqCst); let limit = Instant::now() + PATIENCE; while BUSY_STARTED.load(Ordering::SeqCst) < 2 { assert!(Instant::now() < limit, "the other busy actor never ran"); std::hint::spin_loop(); } BUSY_DONE.fetch_add(1, Ordering::SeqCst); UNIT } /// Once the other threads parked, spawns two busy actors in one slice, /// then waits for both. unsafe extern "C" fn spawns_two_busy(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { yield_until(ctx, "the other threads parked", |sched| { sched.parking.parked() == 3 }); spawn(ctx, busy_until_both_run); spawn(ctx, busy_until_both_run); yield_until(ctx, "both busy actors done", |_| { BUSY_DONE.load(Ordering::SeqCst) == 2 }); } UNIT } /// The second of two actors spawned in one slice waits in the spawner's /// queue (the first is in its next slot) while every other thread sleeps: /// one is woken to take it, so the two run in parallel (ADR 0004, /// Revision 8). #[test] fn actors_spawned_together_run_in_parallel() { BUSY_STARTED.store(0, Ordering::SeqCst); BUSY_DONE.store(0, Ordering::SeqCst); assert_eq!(run_root(4, spawns_two_busy), Exit::Normal); } /// Sleeps (in virtual time) while another actor waits forever. unsafe extern "C" fn sleeps_while_another_waits(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { spawn(ctx, waits_forever); let (a, sched) = this(ctx); let deadline = sched.clock() + 60_000; let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); sched.add_timer(deadline, a.pid, wait); let woken = park(ctx); assert_eq!(woken, UNIT); assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); // Virtual time: the clock jumped to the deadline. assert_eq!(sched.clock(), deadline); } UNIT } #[test] fn an_actor_waiting_on_a_timer_is_no_deadlock() { for threads in THREADS { assert_eq!( run_root(threads, sleeps_while_another_waits), Exit::Normal, "{threads} threads" ); } } static VTIME_TOKEN: AtomicUsize = AtomicUsize::new(0); /// Waits on the socket in `VTIME_TOKEN` until it is stopped: while it /// waits, the root's timer must fire by a clock jump (LOUISE_VIRTUAL_TIME). unsafe extern "C" fn waits_on_the_vtime_socket(ctx: *mut Ctx) -> Value { let token = Token(VTIME_TOKEN.load(Ordering::SeqCst)); // SAFETY: an actor's entry. unsafe { let seen = this(ctx).1.io_events(); assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); } UNIT } /// With LOUISE_VIRTUAL_TIME, opening a socket does not switch the clock /// to real time, and a timer fires by a clock jump while another actor is /// parked on a socket that never becomes ready (ADR 0055). unsafe extern "C" fn sleeps_while_another_waits_on_a_socket(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { let (_reader, _writer, token) = socket_pair(ctx); VTIME_TOKEN.store(token.0, Ordering::SeqCst); let waiter = spawn(ctx, waits_on_the_vtime_socket); yield_until(ctx, "the waiter to park", |s| { s.io_waiting.load(Ordering::SeqCst) == 1 }); let (a, sched) = this(ctx); assert!(!sched.real_time(), "a socket did not switch to real time"); let deadline = sched.clock() + 60_000; let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); sched.add_timer(deadline, a.pid, wait); let woken = park(ctx); assert_eq!(woken, UNIT); assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); // Virtual time: the clock jumped to the deadline. assert_eq!(sched.clock(), deadline); assert!(!sched.real_time(), "the clock stayed virtual"); super::exit_actor(sched, waiter, Exit::Killed); yield_until(ctx, "the waiter to finish", |s| s.actors.all().len() == 1); assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); } UNIT } #[test] fn virtual_time_jumps_while_an_actor_waits_on_a_socket() { for threads in THREADS { assert_eq!( run_root_clock(threads, true, sleeps_while_another_waits_on_a_socket), Exit::Normal, "{threads} threads" ); } } /// Without LOUISE_VIRTUAL_TIME, the first socket switches the clock to /// real time (ADR 0004, Revision 3). unsafe extern "C" fn opens_a_socket(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { let (_reader, _writer, _token) = socket_pair(ctx); assert!( this(ctx).1.real_time(), "the first socket switches to real time" ); } UNIT } #[test] fn a_socket_switches_to_real_time_by_default() { for threads in THREADS { assert_eq!( run_root(threads, opens_a_socket), Exit::Normal, "{threads} threads" ); } } /// Write a byte to `writer` from a thread outside the scheduler, once /// every actor waits, one of them on a socket, and a scheduler thread /// idles: that thread must wait on the reactor, not report a deadlock. /// /// # Safety /// `sched` must outlive the thread: join it before the actor that /// started it returns. unsafe fn write_when_idle(sched: &Shared, mut writer: UnixStream) -> JoinHandle<()> { let address = std::ptr::from_ref(sched) as usize; std::thread::spawn(move || { // SAFETY: caller guarantee. let sched = unsafe { &*(address as *const Shared) }; while !(sched.idling.load(Ordering::SeqCst) && sched.io_waiting.load(Ordering::SeqCst) == 1) { std::thread::sleep(Duration::from_micros(100)); } writer.write_all(b"x").expect("written"); }) } /// Waits on a socket while another actor waits forever, until a thread /// outside the scheduler writes to it. unsafe extern "C" fn reads_while_another_waits(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry; it joins the writer before it // returns. unsafe { spawn(ctx, waits_forever); let (mut reader, writer, token) = socket_pair(ctx); let (_, sched) = this(ctx); let outside = write_when_idle(sched, writer); let mut byte = [0]; loop { let seen = sched.io_events(); match reader.read(&mut byte) { Ok(n) => { assert_eq!(n, 1); break; } Err(e) if e.kind() == ErrorKind::WouldBlock => { assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); } Err(e) => panic!("read: {e}"), } } outside.join().expect("the writer"); } UNIT } #[test] fn an_actor_waiting_on_a_socket_is_no_deadlock() { for threads in THREADS { assert_eq!( run_root(threads, reads_while_another_waits), Exit::Normal, "{threads} threads" ); } } static SHARED_TOKEN: AtomicUsize = AtomicUsize::new(0); static WOKEN: AtomicUsize = AtomicUsize::new(0); /// Waits on the socket in `SHARED_TOKEN`, then counts itself woken. unsafe extern "C" fn waits_on_the_shared_socket(ctx: *mut Ctx) -> Value { let token = Token(SHARED_TOKEN.load(Ordering::SeqCst)); // SAFETY: an actor's entry. unsafe { let seen = this(ctx).1.io_events(); assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); } WOKEN.fetch_add(1, Ordering::SeqCst); UNIT } /// Three actors wait on one socket; one event wakes them all. unsafe extern "C" fn wakes_every_waiter(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { WOKEN.store(0, Ordering::SeqCst); let (reader, mut writer, token) = socket_pair(ctx); SHARED_TOKEN.store(token.0, Ordering::SeqCst); let mut pids: Vec = (0..3) .map(|_| spawn(ctx, waits_on_the_shared_socket)) .collect(); yield_until(ctx, "three waiters", |s| { s.io_waiting.load(Ordering::SeqCst) == 3 }); let (_, sched) = this(ctx); let mut waiting = sched.control().io_waiters[&token].clone(); waiting.sort_unstable(); pids.sort_unstable(); assert_eq!(waiting, pids, "every waiter is kept"); assert_eq!(WOKEN.load(Ordering::SeqCst), 0); writer.write_all(b"x").expect("written"); yield_until(ctx, "three woken", |_| WOKEN.load(Ordering::SeqCst) == 3); assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); drop(reader); } UNIT } #[test] fn every_actor_waiting_on_a_socket_is_woken_by_its_event() { for threads in THREADS { assert_eq!( run_root(threads, wakes_every_waiter), Exit::Normal, "{threads} threads" ); } } static KEEPER_TOKEN: AtomicUsize = AtomicUsize::new(0); /// Waits on the socket in `KEEPER_TOKEN` until it is stopped: while it /// waits, scheduler threads poll the reactor between slices. unsafe extern "C" fn waits_until_stopped(ctx: *mut Ctx) -> Value { let token = Token(KEEPER_TOKEN.load(Ordering::SeqCst)); // SAFETY: an actor's entry. unsafe { let seen = this(ctx).1.io_events(); assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); } UNIT } /// An event that comes between an operation's `WouldBlock` and its /// parking: the parking is refused, and the retry finds the data. unsafe extern "C" fn retries_after_a_missed_event(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. unsafe { let (a, sched) = this(ctx); let (_keeper_reader, _keeper_writer, keeper_token) = socket_pair(ctx); KEEPER_TOKEN.store(keeper_token.0, Ordering::SeqCst); let keeper = spawn(ctx, waits_until_stopped); yield_until(ctx, "the keeper to park", |s| { s.io_waiting.load(Ordering::SeqCst) == 1 }); let (mut reader, mut writer, token) = socket_pair(ctx); let mut byte = [0]; let seen = sched.io_events(); let blocked = reader.read(&mut byte).expect_err("nothing to read yet"); assert_eq!(blocked.kind(), ErrorKind::WouldBlock); // The event comes now, and another thread's poll takes it: nobody // waits on this socket, but the count moves. writer.write_all(b"x").expect("written"); yield_until(ctx, "the event to be counted", |s| s.io_events() != seen); a.with_proc(|p| p.park_as(Wait::Io)); assert!(!sched.io_wait(token, a.pid, seen), "parking refused"); a.with_proc(|p| p.wait = Wait::Ready); assert!(!sched.control().io_waiters.contains_key(&token)); assert_eq!( sched.io_waiting.load(Ordering::SeqCst), 1, "the keeper only" ); assert_eq!(reader.read(&mut byte).expect("the retry reads"), 1); // With no event since, the same parking is accepted. let seen = sched.io_events(); let blocked = reader.read(&mut byte).expect_err("nothing to read"); assert_eq!(blocked.kind(), ErrorKind::WouldBlock); a.with_proc(|p| p.park_as(Wait::Io)); assert!(sched.io_wait(token, a.pid, seen), "parked"); assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 2); sched.io_unwait(token, a.pid); a.with_proc(|p| p.wait = Wait::Ready); assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 1); // A stopped waiter's wait is forgotten. super::exit_actor(sched, keeper, Exit::Killed); yield_until(ctx, "the keeper to finish", |s| s.actors.all().len() == 1); assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); assert!(sched.control().io_waiters.is_empty()); } UNIT } #[test] fn an_event_before_parking_makes_the_operation_retry() { for threads in THREADS { assert_eq!( run_root(threads, retries_after_a_missed_event), Exit::Normal, "{threads} threads" ); } } const TEST_HANDLER_STACK: usize = 64 << 10; /// Write a marker near the unused top so warm retention and `release` are /// observable. `top - 16` is aligned and above the guard page. fn mark_stack(stack: &Stack, marker: usize) -> usize { let address = stack.top() - 16; // SAFETY: an unused stack owns this aligned, mapped word. unsafe { (address as *mut usize).write_volatile(marker) }; address } fn stack_marker(address: usize) -> usize { // SAFETY: the caller keeps the marked stack alive. unsafe { (address as *const usize).read_volatile() } } #[test] fn handler_stack_pool_miss_hit_and_size() { let _turn = test_turn(); let pool = HandlerStacks::new(2, 4); assert!(pool.take(TEST_HANDLER_STACK).is_none(), "empty miss"); let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); assert_eq!(stack.size(), TEST_HANDLER_STACK); let top = stack.top(); pool.pool(stack, TEST_HANDLER_STACK); let reused = pool.take(TEST_HANDLER_STACK).expect("warm hit"); assert_eq!(reused.top(), top, "the same mapping was reused"); assert_eq!(reused.size(), TEST_HANDLER_STACK); pool.pool(reused, TEST_HANDLER_STACK); // Ordinary pool drop unmaps the retained stack. } #[test] fn handler_stack_pool_releases_only_past_warm_limit() { let _turn = test_turn(); let warm = HandlerStacks::new(1, 2); let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); let marked = mark_stack(&stack, 0x5a17); warm.pool(stack, TEST_HANDLER_STACK); let stack = warm.take(TEST_HANDLER_STACK).expect("retained"); assert_eq!(stack_marker(marked), 0x5a17, "warm pages retained"); // Keep one warm entry, then pool another: the latter is cold-released // and reads as zero when taken again, without any large mapping stress. warm.pool(stack, TEST_HANDLER_STACK); let cold = Stack::new(TEST_HANDLER_STACK).unwrap(); let cleared = mark_stack(&cold, 0x5a17); warm.pool(cold, TEST_HANDLER_STACK); let cold = warm .take(TEST_HANDLER_STACK) .expect("cold retained mapping"); assert_eq!( cold.size(), TEST_HANDLER_STACK, "cold mapping keeps its size" ); assert_eq!(stack_marker(cleared), 0, "cold pages released"); } #[test] fn handler_stack_pool_releases_over_capacity_without_double_insertion() { let _turn = test_turn(); let pool = HandlerStacks::new(0, 2); let first = Stack::new(TEST_HANDLER_STACK).unwrap(); let second = Stack::new(TEST_HANDLER_STACK).unwrap(); let surplus = Stack::new(TEST_HANDLER_STACK).unwrap(); let first_top = first.top(); let second_top = second.top(); pool.pool(first, TEST_HANDLER_STACK); pool.pool(second, TEST_HANDLER_STACK); // Over capacity: dropped/unmapped here, outside the pool mutex. pool.pool(surplus, TEST_HANDLER_STACK); assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), second_top); assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), first_top); assert!(pool.take(TEST_HANDLER_STACK).is_none(), "no duplicate"); let stats = pool.stats(); assert_eq!((stats.hits, stats.misses), (2, 1)); assert_eq!(stats.unmaps, 1); } #[test] fn handler_stack_pool_production_cap_is_stable_across_refills() { let _turn = test_turn(); // Small mappings exercise the real 16-warm/64-cap policy without making // this unit test reserve the production 72 × 18.131 MiB extent. let pool = HandlerStacks::new(WARM_HANDLER_STACKS, HANDLER_STACK_POOL); let mut live = Vec::new(); for round in 0..4 { while let Some(stack) = pool.take(TEST_HANDLER_STACK) { live.push(stack); } while live.len() < 72 { live.push(Stack::new(TEST_HANDLER_STACK).unwrap()); } for stack in live.drain(..) { pool.pool(stack, TEST_HANDLER_STACK); } assert_eq!(pool.stacks().len(), HANDLER_STACK_POOL); let stats = pool.stats(); assert_eq!(stats.warm_inserts, WARM_HANDLER_STACKS * (round + 1)); assert_eq!( stats.cold_releases, (HANDLER_STACK_POOL - WARM_HANDLER_STACKS) * (round + 1) ); assert_eq!(stats.unmaps, (72 - HANDLER_STACK_POOL) * (round + 1)); } let stats = pool.stats(); assert_eq!(stats.hits, HANDLER_STACK_POOL * 3); assert_eq!(stats.misses, 4); } #[test] fn ordinary_handler_pool_drop_unmaps_retained_stack() { // Shared's ordinary (non-PROGRAM_MODE) teardown drops this same vector. // Probe by address, without dereferencing the dropped stack: an exclusive // no-replace mapping can succeed only if the old extent was unmapped. let _turn = test_turn(); let pool = HandlerStacks::new(1, 1); let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); let (base, len) = stack.map_extent(); pool.pool(stack, TEST_HANDLER_STACK); drop(pool); // SAFETY: `base..base+len` was just released; NOREPLACE refuses rather // than replacing anything another thread may have mapped there. The new // PROT_NONE mapping is this test's own and is unmapped exactly once. unsafe { let replacement = libc::mmap( base.cast(), len, libc::PROT_NONE, libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE | libc::MAP_FIXED_NOREPLACE, -1, 0, ); assert_ne!( replacement, libc::MAP_FAILED, "retained extent is still mapped: {}", std::io::Error::last_os_error() ); assert_eq!(libc::munmap(replacement, len), 0); } }