
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1201 · main · 9 min 57 s
1Étoiles
0Forks
0Issues
| 1 | //! The scheduler's glue: its counters (`running`, `pending`, |
| 2 | //! `io_waiting`), idle and deadlock detection, and the reactor's wake-ups. |
| 3 | //! The hand-offs it is built on have loom models (`handoff`, `timers`, |
| 4 | //! `otp`, `inbox`); these run whole programs of test actors, on one thread |
| 5 | //! and on several, and check what the scheduler decides. |
| 6 | //! |
| 7 | //! Test actors talk through statics: each test runs alone ([`test_turn`]). |
| 8 | //! An assertion that fails in an actor aborts the test binary (a panic |
| 9 | //! cannot unwind out of its `extern "C"` entry), as in the crate's other |
| 10 | //! actor tests; a wake-up that never comes fails [`yield_until`]. |
| 11 | |
| 12 | mod dist; |
| 13 | #[cfg(feature = "tls")] |
| 14 | mod transport; |
| 15 | |
| 16 | use std::io::{ErrorKind, Read, Write}; |
| 17 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 18 | use std::thread::JoinHandle; |
| 19 | use std::time::{Duration, Instant}; |
| 20 | |
| 21 | use mio::net::UnixStream; |
| 22 | use mio::{Interest, Token}; |
| 23 | |
| 24 | use crate::abi::{Ctx, UNIT, Value, YIELD_NONE}; |
| 25 | use crate::fiber::Stack; |
| 26 | use crate::heap::drop_value; |
| 27 | use crate::otp::{Received, Tags, Timeout, Wait}; |
| 28 | |
| 29 | use super::{ |
| 30 | Body, EntryFn, Exit, HANDLER_STACK_POOL, HandlerStacks, STACK_SIZE, Shared, Tie, |
| 31 | WARM_HANDLER_STACKS, park, preempt_root, root_stack_size, run, spawn_actor, test_turn, this, |
| 32 | }; |
| 33 | |
| 34 | /// Thread counts each program runs with: one (the interpreter's order), |
| 35 | /// and several (stealing, one idle thread for all). |
| 36 | const THREADS: [usize; 2] = [1, 4]; |
| 37 | |
| 38 | /// Run `entry` as the root actor on `threads` threads, as `tl_main` does, |
| 39 | /// then check that the scheduler is at rest: nothing runs, is queued or |
| 40 | /// waits on a socket, and every actor is gone. Returns the root's exit. |
| 41 | fn run_root(threads: usize, entry: EntryFn) -> Exit { |
| 42 | run_root_clock(threads, false, entry) |
| 43 | } |
| 44 | |
| 45 | /// `run_root` with the `LOUISE_VIRTUAL_TIME` opt-in set explicitly, so |
| 46 | /// the tests do not depend on the environment (ADR 0055). |
| 47 | fn run_root_clock(threads: usize, virtual_time: bool, entry: EntryFn) -> Exit { |
| 48 | let _turn = test_turn(); |
| 49 | let mut sched = Shared::new(threads); |
| 50 | sched.virtual_time = virtual_time; |
| 51 | let (root, _) = spawn_actor( |
| 52 | &sched, |
| 53 | Body::Entry(entry), |
| 54 | root_stack_size(), |
| 55 | None, |
| 56 | Tie::None, |
| 57 | ); |
| 58 | let exit = run(&sched, root, threads); |
| 59 | crate::console::flush(); |
| 60 | assert_eq!(sched.running.load(Ordering::SeqCst), 0, "running"); |
| 61 | assert_eq!(sched.pending.load(Ordering::SeqCst), 0, "pending"); |
| 62 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0, "io_waiting"); |
| 63 | assert!(sched.control().io_waiters.is_empty(), "io_waiters"); |
| 64 | assert!(sched.actors.all().is_empty(), "actors left"); |
| 65 | exit |
| 66 | } |
| 67 | |
| 68 | /// Spawn an actor running `entry`, untied to the running actor. |
| 69 | /// |
| 70 | /// # Safety |
| 71 | /// `ctx` must be the running actor's context. |
| 72 | unsafe fn spawn(ctx: *mut Ctx, entry: EntryFn) -> i64 { |
| 73 | // SAFETY: forwarded caller guarantee. |
| 74 | let (a, sched) = unsafe { this(ctx) }; |
| 75 | spawn_actor( |
| 76 | sched, |
| 77 | Body::Entry(entry), |
| 78 | STACK_SIZE, |
| 79 | Some(a.pid), |
| 80 | Tie::None, |
| 81 | ) |
| 82 | .0 |
| 83 | } |
| 84 | |
| 85 | /// How long [`yield_until`] waits before it fails: far more than any |
| 86 | /// condition here takes (microseconds), so a lost wake-up fails the test |
| 87 | /// instead of hanging it. |
| 88 | const PATIENCE: Duration = Duration::from_secs(10); |
| 89 | |
| 90 | /// Yield until `done` holds (`what`, for the failure); the other actors |
| 91 | /// run meanwhile. |
| 92 | /// |
| 93 | /// # Safety |
| 94 | /// `ctx` must be the running actor's context, on its own stack. |
| 95 | unsafe fn yield_until(ctx: *mut Ctx, what: &str, done: impl Fn(&Shared) -> bool) { |
| 96 | let limit = Instant::now() + PATIENCE; |
| 97 | // SAFETY: forwarded caller guarantee; the scheduler outlives its actors. |
| 98 | unsafe { |
| 99 | let (_, sched) = this(ctx); |
| 100 | while !done(sched) { |
| 101 | assert!(Instant::now() < limit, "still waiting: {what}"); |
| 102 | preempt_root(ctx); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// Wait for a message nobody sends; returns once the actor is stopped. |
| 108 | /// |
| 109 | /// # Safety |
| 110 | /// `ctx` must be the running actor's context. |
| 111 | unsafe fn wait_forever(ctx: *mut Ctx) { |
| 112 | // SAFETY: forwarded caller guarantee. |
| 113 | unsafe { |
| 114 | let (a, sched) = this(ctx); |
| 115 | let tag = sched.tag(); |
| 116 | let received = a.with_proc(|p| p.receive(a.inbox(), Tags::One(tag), false, Timeout::Never)); |
| 117 | assert!(matches!(received, Received::Parked(_)), "no message"); |
| 118 | let woken = park(ctx); |
| 119 | drop_value(ctx, woken); |
| 120 | assert_ne!((*ctx).yielding, YIELD_NONE, "only an exit ends the wait"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// A connected pair of sockets; the first registered with the running |
| 125 | /// actor's reactor, for reading only (so its only events are the second |
| 126 | /// writing to it), with its token. |
| 127 | /// |
| 128 | /// # Safety |
| 129 | /// `ctx` must be the running actor's context. |
| 130 | unsafe fn socket_pair(ctx: *mut Ctx) -> (UnixStream, UnixStream, Token) { |
| 131 | // SAFETY: forwarded caller guarantee. |
| 132 | let (_, sched) = unsafe { this(ctx) }; |
| 133 | let (mut reader, writer) = UnixStream::pair().expect("a socket pair"); |
| 134 | let token = sched.io_token(); |
| 135 | sched |
| 136 | .with_registry(|r| r.register(&mut reader, token, Interest::READABLE)) |
| 137 | .expect("registered"); |
| 138 | (reader, writer, token) |
| 139 | } |
| 140 | |
| 141 | /// Park on socket `token`, as `net` does, unless an event came since the |
| 142 | /// count `seen`; false if the actor was stopped instead. |
| 143 | /// |
| 144 | /// # Safety |
| 145 | /// `ctx` must be the running actor's context. |
| 146 | unsafe fn wait_on_socket(ctx: *mut Ctx, token: Token, seen: u64) -> bool { |
| 147 | // SAFETY: forwarded caller guarantee. |
| 148 | unsafe { crate::net::wait_for(ctx, token, seen) } |
| 149 | } |
| 150 | |
| 151 | static COUNTED_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 152 | |
| 153 | /// Yields a few times, then finishes. |
| 154 | unsafe extern "C" fn yields_then_ends(ctx: *mut Ctx) -> Value { |
| 155 | for _ in 0..3 { |
| 156 | // SAFETY: an actor's entry, on its own stack. |
| 157 | unsafe { preempt_root(ctx) }; |
| 158 | } |
| 159 | UNIT |
| 160 | } |
| 161 | |
| 162 | /// Waits on the socket in `COUNTED_TOKEN` until woken, then finishes. |
| 163 | unsafe extern "C" fn waits_on_the_socket(ctx: *mut Ctx) -> Value { |
| 164 | let token = Token(COUNTED_TOKEN.load(Ordering::SeqCst)); |
| 165 | // SAFETY: an actor's entry. |
| 166 | unsafe { |
| 167 | let seen = this(ctx).1.io_events(); |
| 168 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 169 | } |
| 170 | UNIT |
| 171 | } |
| 172 | |
| 173 | /// Spawns actors that yield and finish, and actors that wait on a socket |
| 174 | /// until the root writes to it; checks the counters while they wait. |
| 175 | unsafe extern "C" fn counts_actors(ctx: *mut Ctx) -> Value { |
| 176 | // SAFETY: the root actor's entry. |
| 177 | unsafe { |
| 178 | let (reader, mut writer, token) = socket_pair(ctx); |
| 179 | COUNTED_TOKEN.store(token.0, Ordering::SeqCst); |
| 180 | for _ in 0..4 { |
| 181 | spawn(ctx, yields_then_ends); |
| 182 | spawn(ctx, waits_on_the_socket); |
| 183 | } |
| 184 | // Only the root runs, nothing is queued, four wait on the socket. |
| 185 | yield_until(ctx, "the waiters to park", |s| { |
| 186 | s.io_waiting.load(Ordering::SeqCst) == 4 |
| 187 | && s.running.load(Ordering::SeqCst) == 1 |
| 188 | && s.pending.load(Ordering::SeqCst) == 0 |
| 189 | && s.actors.all().len() == 5 |
| 190 | }); |
| 191 | let (_, sched) = this(ctx); |
| 192 | assert_eq!(sched.control().io_waiters[&token].len(), 4); |
| 193 | writer.write_all(b"x").expect("written"); |
| 194 | yield_until(ctx, "the waiters to finish", |s| s.actors.all().len() == 1); |
| 195 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 196 | assert!(!sched.control().io_waiters.contains_key(&token)); |
| 197 | drop(reader); |
| 198 | } |
| 199 | UNIT |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn counters_rest_once_actors_ran_waited_on_a_socket_and_finished() { |
| 204 | for threads in THREADS { |
| 205 | assert_eq!( |
| 206 | run_root(threads, counts_actors), |
| 207 | Exit::Normal, |
| 208 | "{threads} threads" |
| 209 | ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /// Waits for a message nobody sends. |
| 214 | unsafe extern "C" fn waits_forever(ctx: *mut Ctx) -> Value { |
| 215 | // SAFETY: an actor's entry. |
| 216 | unsafe { wait_forever(ctx) }; |
| 217 | UNIT |
| 218 | } |
| 219 | |
| 220 | /// Spawns an actor waiting forever, then waits forever too. |
| 221 | unsafe extern "C" fn all_wait_forever(ctx: *mut Ctx) -> Value { |
| 222 | // SAFETY: the root actor's entry. |
| 223 | unsafe { |
| 224 | spawn(ctx, waits_forever); |
| 225 | wait_forever(ctx); |
| 226 | } |
| 227 | UNIT |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn actors_all_waiting_for_messages_are_a_deadlock() { |
| 232 | for threads in THREADS { |
| 233 | assert_eq!( |
| 234 | run_root(threads, all_wait_forever), |
| 235 | Exit::Crashed("deadlock: every actor is waiting for a message".into()), |
| 236 | "{threads} threads" |
| 237 | ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static BUSY_STARTED: AtomicUsize = AtomicUsize::new(0); |
| 242 | static BUSY_DONE: AtomicUsize = AtomicUsize::new(0); |
| 243 | |
| 244 | /// Counts itself started, then holds its thread without yielding until |
| 245 | /// the other busy actor started too: it can only start on another thread. |
| 246 | unsafe extern "C" fn busy_until_both_run(_ctx: *mut Ctx) -> Value { |
| 247 | BUSY_STARTED.fetch_add(1, Ordering::SeqCst); |
| 248 | let limit = Instant::now() + PATIENCE; |
| 249 | while BUSY_STARTED.load(Ordering::SeqCst) < 2 { |
| 250 | assert!(Instant::now() < limit, "the other busy actor never ran"); |
| 251 | std::hint::spin_loop(); |
| 252 | } |
| 253 | BUSY_DONE.fetch_add(1, Ordering::SeqCst); |
| 254 | UNIT |
| 255 | } |
| 256 | |
| 257 | /// Once the other threads parked, spawns two busy actors in one slice, |
| 258 | /// then waits for both. |
| 259 | unsafe extern "C" fn spawns_two_busy(ctx: *mut Ctx) -> Value { |
| 260 | // SAFETY: the root actor's entry. |
| 261 | unsafe { |
| 262 | yield_until(ctx, "the other threads parked", |sched| { |
| 263 | sched.parking.parked() == 3 |
| 264 | }); |
| 265 | spawn(ctx, busy_until_both_run); |
| 266 | spawn(ctx, busy_until_both_run); |
| 267 | yield_until(ctx, "both busy actors done", |_| { |
| 268 | BUSY_DONE.load(Ordering::SeqCst) == 2 |
| 269 | }); |
| 270 | } |
| 271 | UNIT |
| 272 | } |
| 273 | |
| 274 | /// The second of two actors spawned in one slice waits in the spawner's |
| 275 | /// queue (the first is in its next slot) while every other thread sleeps: |
| 276 | /// one is woken to take it, so the two run in parallel (ADR 0004, |
| 277 | /// Revision 8). |
| 278 | #[test] |
| 279 | fn actors_spawned_together_run_in_parallel() { |
| 280 | BUSY_STARTED.store(0, Ordering::SeqCst); |
| 281 | BUSY_DONE.store(0, Ordering::SeqCst); |
| 282 | assert_eq!(run_root(4, spawns_two_busy), Exit::Normal); |
| 283 | } |
| 284 | |
| 285 | /// Sleeps (in virtual time) while another actor waits forever. |
| 286 | unsafe extern "C" fn sleeps_while_another_waits(ctx: *mut Ctx) -> Value { |
| 287 | // SAFETY: the root actor's entry. |
| 288 | unsafe { |
| 289 | spawn(ctx, waits_forever); |
| 290 | let (a, sched) = this(ctx); |
| 291 | let deadline = sched.clock() + 60_000; |
| 292 | let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); |
| 293 | sched.add_timer(deadline, a.pid, wait); |
| 294 | let woken = park(ctx); |
| 295 | assert_eq!(woken, UNIT); |
| 296 | assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); |
| 297 | // Virtual time: the clock jumped to the deadline. |
| 298 | assert_eq!(sched.clock(), deadline); |
| 299 | } |
| 300 | UNIT |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn an_actor_waiting_on_a_timer_is_no_deadlock() { |
| 305 | for threads in THREADS { |
| 306 | assert_eq!( |
| 307 | run_root(threads, sleeps_while_another_waits), |
| 308 | Exit::Normal, |
| 309 | "{threads} threads" |
| 310 | ); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static VTIME_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 315 | |
| 316 | /// Waits on the socket in `VTIME_TOKEN` until it is stopped: while it |
| 317 | /// waits, the root's timer must fire by a clock jump (LOUISE_VIRTUAL_TIME). |
| 318 | unsafe extern "C" fn waits_on_the_vtime_socket(ctx: *mut Ctx) -> Value { |
| 319 | let token = Token(VTIME_TOKEN.load(Ordering::SeqCst)); |
| 320 | // SAFETY: an actor's entry. |
| 321 | unsafe { |
| 322 | let seen = this(ctx).1.io_events(); |
| 323 | assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); |
| 324 | } |
| 325 | UNIT |
| 326 | } |
| 327 | |
| 328 | /// With LOUISE_VIRTUAL_TIME, opening a socket does not switch the clock |
| 329 | /// to real time, and a timer fires by a clock jump while another actor is |
| 330 | /// parked on a socket that never becomes ready (ADR 0055). |
| 331 | unsafe extern "C" fn sleeps_while_another_waits_on_a_socket(ctx: *mut Ctx) -> Value { |
| 332 | // SAFETY: the root actor's entry. |
| 333 | unsafe { |
| 334 | let (_reader, _writer, token) = socket_pair(ctx); |
| 335 | VTIME_TOKEN.store(token.0, Ordering::SeqCst); |
| 336 | let waiter = spawn(ctx, waits_on_the_vtime_socket); |
| 337 | yield_until(ctx, "the waiter to park", |s| { |
| 338 | s.io_waiting.load(Ordering::SeqCst) == 1 |
| 339 | }); |
| 340 | let (a, sched) = this(ctx); |
| 341 | assert!(!sched.real_time(), "a socket did not switch to real time"); |
| 342 | let deadline = sched.clock() + 60_000; |
| 343 | let wait = a.with_proc(|p| p.park_as(Wait::Sleep { deadline })); |
| 344 | sched.add_timer(deadline, a.pid, wait); |
| 345 | let woken = park(ctx); |
| 346 | assert_eq!(woken, UNIT); |
| 347 | assert_eq!((*ctx).yielding, YIELD_NONE, "woken by the timer"); |
| 348 | // Virtual time: the clock jumped to the deadline. |
| 349 | assert_eq!(sched.clock(), deadline); |
| 350 | assert!(!sched.real_time(), "the clock stayed virtual"); |
| 351 | super::exit_actor(sched, waiter, Exit::Killed); |
| 352 | yield_until(ctx, "the waiter to finish", |s| s.actors.all().len() == 1); |
| 353 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 354 | } |
| 355 | UNIT |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn virtual_time_jumps_while_an_actor_waits_on_a_socket() { |
| 360 | for threads in THREADS { |
| 361 | assert_eq!( |
| 362 | run_root_clock(threads, true, sleeps_while_another_waits_on_a_socket), |
| 363 | Exit::Normal, |
| 364 | "{threads} threads" |
| 365 | ); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /// Without LOUISE_VIRTUAL_TIME, the first socket switches the clock to |
| 370 | /// real time (ADR 0004, Revision 3). |
| 371 | unsafe extern "C" fn opens_a_socket(ctx: *mut Ctx) -> Value { |
| 372 | // SAFETY: the root actor's entry. |
| 373 | unsafe { |
| 374 | let (_reader, _writer, _token) = socket_pair(ctx); |
| 375 | assert!( |
| 376 | this(ctx).1.real_time(), |
| 377 | "the first socket switches to real time" |
| 378 | ); |
| 379 | } |
| 380 | UNIT |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn a_socket_switches_to_real_time_by_default() { |
| 385 | for threads in THREADS { |
| 386 | assert_eq!( |
| 387 | run_root(threads, opens_a_socket), |
| 388 | Exit::Normal, |
| 389 | "{threads} threads" |
| 390 | ); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /// Write a byte to `writer` from a thread outside the scheduler, once |
| 395 | /// every actor waits, one of them on a socket, and a scheduler thread |
| 396 | /// idles: that thread must wait on the reactor, not report a deadlock. |
| 397 | /// |
| 398 | /// # Safety |
| 399 | /// `sched` must outlive the thread: join it before the actor that |
| 400 | /// started it returns. |
| 401 | unsafe fn write_when_idle(sched: &Shared, mut writer: UnixStream) -> JoinHandle<()> { |
| 402 | let address = std::ptr::from_ref(sched) as usize; |
| 403 | std::thread::spawn(move || { |
| 404 | // SAFETY: caller guarantee. |
| 405 | let sched = unsafe { &*(address as *const Shared) }; |
| 406 | while !(sched.idling.load(Ordering::SeqCst) && sched.io_waiting.load(Ordering::SeqCst) == 1) |
| 407 | { |
| 408 | std::thread::sleep(Duration::from_micros(100)); |
| 409 | } |
| 410 | writer.write_all(b"x").expect("written"); |
| 411 | }) |
| 412 | } |
| 413 | |
| 414 | /// Waits on a socket while another actor waits forever, until a thread |
| 415 | /// outside the scheduler writes to it. |
| 416 | unsafe extern "C" fn reads_while_another_waits(ctx: *mut Ctx) -> Value { |
| 417 | // SAFETY: the root actor's entry; it joins the writer before it |
| 418 | // returns. |
| 419 | unsafe { |
| 420 | spawn(ctx, waits_forever); |
| 421 | let (mut reader, writer, token) = socket_pair(ctx); |
| 422 | let (_, sched) = this(ctx); |
| 423 | let outside = write_when_idle(sched, writer); |
| 424 | let mut byte = [0]; |
| 425 | loop { |
| 426 | let seen = sched.io_events(); |
| 427 | match reader.read(&mut byte) { |
| 428 | Ok(n) => { |
| 429 | assert_eq!(n, 1); |
| 430 | break; |
| 431 | } |
| 432 | Err(e) if e.kind() == ErrorKind::WouldBlock => { |
| 433 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 434 | } |
| 435 | Err(e) => panic!("read: {e}"), |
| 436 | } |
| 437 | } |
| 438 | outside.join().expect("the writer"); |
| 439 | } |
| 440 | UNIT |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn an_actor_waiting_on_a_socket_is_no_deadlock() { |
| 445 | for threads in THREADS { |
| 446 | assert_eq!( |
| 447 | run_root(threads, reads_while_another_waits), |
| 448 | Exit::Normal, |
| 449 | "{threads} threads" |
| 450 | ); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static SHARED_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 455 | static WOKEN: AtomicUsize = AtomicUsize::new(0); |
| 456 | |
| 457 | /// Waits on the socket in `SHARED_TOKEN`, then counts itself woken. |
| 458 | unsafe extern "C" fn waits_on_the_shared_socket(ctx: *mut Ctx) -> Value { |
| 459 | let token = Token(SHARED_TOKEN.load(Ordering::SeqCst)); |
| 460 | // SAFETY: an actor's entry. |
| 461 | unsafe { |
| 462 | let seen = this(ctx).1.io_events(); |
| 463 | assert!(wait_on_socket(ctx, token, seen), "woken, not stopped"); |
| 464 | } |
| 465 | WOKEN.fetch_add(1, Ordering::SeqCst); |
| 466 | UNIT |
| 467 | } |
| 468 | |
| 469 | /// Three actors wait on one socket; one event wakes them all. |
| 470 | unsafe extern "C" fn wakes_every_waiter(ctx: *mut Ctx) -> Value { |
| 471 | // SAFETY: the root actor's entry. |
| 472 | unsafe { |
| 473 | WOKEN.store(0, Ordering::SeqCst); |
| 474 | let (reader, mut writer, token) = socket_pair(ctx); |
| 475 | SHARED_TOKEN.store(token.0, Ordering::SeqCst); |
| 476 | let mut pids: Vec<i64> = (0..3) |
| 477 | .map(|_| spawn(ctx, waits_on_the_shared_socket)) |
| 478 | .collect(); |
| 479 | yield_until(ctx, "three waiters", |s| { |
| 480 | s.io_waiting.load(Ordering::SeqCst) == 3 |
| 481 | }); |
| 482 | let (_, sched) = this(ctx); |
| 483 | let mut waiting = sched.control().io_waiters[&token].clone(); |
| 484 | waiting.sort_unstable(); |
| 485 | pids.sort_unstable(); |
| 486 | assert_eq!(waiting, pids, "every waiter is kept"); |
| 487 | assert_eq!(WOKEN.load(Ordering::SeqCst), 0); |
| 488 | writer.write_all(b"x").expect("written"); |
| 489 | yield_until(ctx, "three woken", |_| WOKEN.load(Ordering::SeqCst) == 3); |
| 490 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 491 | drop(reader); |
| 492 | } |
| 493 | UNIT |
| 494 | } |
| 495 | |
| 496 | #[test] |
| 497 | fn every_actor_waiting_on_a_socket_is_woken_by_its_event() { |
| 498 | for threads in THREADS { |
| 499 | assert_eq!( |
| 500 | run_root(threads, wakes_every_waiter), |
| 501 | Exit::Normal, |
| 502 | "{threads} threads" |
| 503 | ); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | static KEEPER_TOKEN: AtomicUsize = AtomicUsize::new(0); |
| 508 | |
| 509 | /// Waits on the socket in `KEEPER_TOKEN` until it is stopped: while it |
| 510 | /// waits, scheduler threads poll the reactor between slices. |
| 511 | unsafe extern "C" fn waits_until_stopped(ctx: *mut Ctx) -> Value { |
| 512 | let token = Token(KEEPER_TOKEN.load(Ordering::SeqCst)); |
| 513 | // SAFETY: an actor's entry. |
| 514 | unsafe { |
| 515 | let seen = this(ctx).1.io_events(); |
| 516 | assert!(!wait_on_socket(ctx, token, seen), "stopped, not woken"); |
| 517 | } |
| 518 | UNIT |
| 519 | } |
| 520 | |
| 521 | /// An event that comes between an operation's `WouldBlock` and its |
| 522 | /// parking: the parking is refused, and the retry finds the data. |
| 523 | unsafe extern "C" fn retries_after_a_missed_event(ctx: *mut Ctx) -> Value { |
| 524 | // SAFETY: the root actor's entry. |
| 525 | unsafe { |
| 526 | let (a, sched) = this(ctx); |
| 527 | let (_keeper_reader, _keeper_writer, keeper_token) = socket_pair(ctx); |
| 528 | KEEPER_TOKEN.store(keeper_token.0, Ordering::SeqCst); |
| 529 | let keeper = spawn(ctx, waits_until_stopped); |
| 530 | yield_until(ctx, "the keeper to park", |s| { |
| 531 | s.io_waiting.load(Ordering::SeqCst) == 1 |
| 532 | }); |
| 533 | |
| 534 | let (mut reader, mut writer, token) = socket_pair(ctx); |
| 535 | let mut byte = [0]; |
| 536 | let seen = sched.io_events(); |
| 537 | let blocked = reader.read(&mut byte).expect_err("nothing to read yet"); |
| 538 | assert_eq!(blocked.kind(), ErrorKind::WouldBlock); |
| 539 | // The event comes now, and another thread's poll takes it: nobody |
| 540 | // waits on this socket, but the count moves. |
| 541 | writer.write_all(b"x").expect("written"); |
| 542 | yield_until(ctx, "the event to be counted", |s| s.io_events() != seen); |
| 543 | a.with_proc(|p| p.park_as(Wait::Io)); |
| 544 | assert!(!sched.io_wait(token, a.pid, seen), "parking refused"); |
| 545 | a.with_proc(|p| p.wait = Wait::Ready); |
| 546 | assert!(!sched.control().io_waiters.contains_key(&token)); |
| 547 | assert_eq!( |
| 548 | sched.io_waiting.load(Ordering::SeqCst), |
| 549 | 1, |
| 550 | "the keeper only" |
| 551 | ); |
| 552 | assert_eq!(reader.read(&mut byte).expect("the retry reads"), 1); |
| 553 | |
| 554 | // With no event since, the same parking is accepted. |
| 555 | let seen = sched.io_events(); |
| 556 | let blocked = reader.read(&mut byte).expect_err("nothing to read"); |
| 557 | assert_eq!(blocked.kind(), ErrorKind::WouldBlock); |
| 558 | a.with_proc(|p| p.park_as(Wait::Io)); |
| 559 | assert!(sched.io_wait(token, a.pid, seen), "parked"); |
| 560 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 2); |
| 561 | sched.io_unwait(token, a.pid); |
| 562 | a.with_proc(|p| p.wait = Wait::Ready); |
| 563 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 1); |
| 564 | |
| 565 | // A stopped waiter's wait is forgotten. |
| 566 | super::exit_actor(sched, keeper, Exit::Killed); |
| 567 | yield_until(ctx, "the keeper to finish", |s| s.actors.all().len() == 1); |
| 568 | assert_eq!(sched.io_waiting.load(Ordering::SeqCst), 0); |
| 569 | assert!(sched.control().io_waiters.is_empty()); |
| 570 | } |
| 571 | UNIT |
| 572 | } |
| 573 | |
| 574 | #[test] |
| 575 | fn an_event_before_parking_makes_the_operation_retry() { |
| 576 | for threads in THREADS { |
| 577 | assert_eq!( |
| 578 | run_root(threads, retries_after_a_missed_event), |
| 579 | Exit::Normal, |
| 580 | "{threads} threads" |
| 581 | ); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | const TEST_HANDLER_STACK: usize = 64 << 10; |
| 586 | |
| 587 | /// Write a marker near the unused top so warm retention and `release` are |
| 588 | /// observable. `top - 16` is aligned and above the guard page. |
| 589 | fn mark_stack(stack: &Stack, marker: usize) -> usize { |
| 590 | let address = stack.top() - 16; |
| 591 | // SAFETY: an unused stack owns this aligned, mapped word. |
| 592 | unsafe { (address as *mut usize).write_volatile(marker) }; |
| 593 | address |
| 594 | } |
| 595 | |
| 596 | fn stack_marker(address: usize) -> usize { |
| 597 | // SAFETY: the caller keeps the marked stack alive. |
| 598 | unsafe { (address as *const usize).read_volatile() } |
| 599 | } |
| 600 | |
| 601 | #[test] |
| 602 | fn handler_stack_pool_miss_hit_and_size() { |
| 603 | let _turn = test_turn(); |
| 604 | let pool = HandlerStacks::new(2, 4); |
| 605 | assert!(pool.take(TEST_HANDLER_STACK).is_none(), "empty miss"); |
| 606 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 607 | assert_eq!(stack.size(), TEST_HANDLER_STACK); |
| 608 | let top = stack.top(); |
| 609 | pool.pool(stack, TEST_HANDLER_STACK); |
| 610 | let reused = pool.take(TEST_HANDLER_STACK).expect("warm hit"); |
| 611 | assert_eq!(reused.top(), top, "the same mapping was reused"); |
| 612 | assert_eq!(reused.size(), TEST_HANDLER_STACK); |
| 613 | pool.pool(reused, TEST_HANDLER_STACK); |
| 614 | // Ordinary pool drop unmaps the retained stack. |
| 615 | } |
| 616 | |
| 617 | #[test] |
| 618 | fn handler_stack_pool_releases_only_past_warm_limit() { |
| 619 | let _turn = test_turn(); |
| 620 | let warm = HandlerStacks::new(1, 2); |
| 621 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 622 | let marked = mark_stack(&stack, 0x5a17); |
| 623 | warm.pool(stack, TEST_HANDLER_STACK); |
| 624 | let stack = warm.take(TEST_HANDLER_STACK).expect("retained"); |
| 625 | assert_eq!(stack_marker(marked), 0x5a17, "warm pages retained"); |
| 626 | |
| 627 | // Keep one warm entry, then pool another: the latter is cold-released |
| 628 | // and reads as zero when taken again, without any large mapping stress. |
| 629 | warm.pool(stack, TEST_HANDLER_STACK); |
| 630 | let cold = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 631 | let cleared = mark_stack(&cold, 0x5a17); |
| 632 | warm.pool(cold, TEST_HANDLER_STACK); |
| 633 | let cold = warm |
| 634 | .take(TEST_HANDLER_STACK) |
| 635 | .expect("cold retained mapping"); |
| 636 | assert_eq!( |
| 637 | cold.size(), |
| 638 | TEST_HANDLER_STACK, |
| 639 | "cold mapping keeps its size" |
| 640 | ); |
| 641 | assert_eq!(stack_marker(cleared), 0, "cold pages released"); |
| 642 | } |
| 643 | |
| 644 | #[test] |
| 645 | fn handler_stack_pool_releases_over_capacity_without_double_insertion() { |
| 646 | let _turn = test_turn(); |
| 647 | let pool = HandlerStacks::new(0, 2); |
| 648 | let first = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 649 | let second = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 650 | let surplus = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 651 | let first_top = first.top(); |
| 652 | let second_top = second.top(); |
| 653 | pool.pool(first, TEST_HANDLER_STACK); |
| 654 | pool.pool(second, TEST_HANDLER_STACK); |
| 655 | // Over capacity: dropped/unmapped here, outside the pool mutex. |
| 656 | pool.pool(surplus, TEST_HANDLER_STACK); |
| 657 | assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), second_top); |
| 658 | assert_eq!(pool.take(TEST_HANDLER_STACK).unwrap().top(), first_top); |
| 659 | assert!(pool.take(TEST_HANDLER_STACK).is_none(), "no duplicate"); |
| 660 | let stats = pool.stats(); |
| 661 | assert_eq!((stats.hits, stats.misses), (2, 1)); |
| 662 | assert_eq!(stats.unmaps, 1); |
| 663 | } |
| 664 | |
| 665 | #[test] |
| 666 | fn handler_stack_pool_production_cap_is_stable_across_refills() { |
| 667 | let _turn = test_turn(); |
| 668 | // Small mappings exercise the real 16-warm/64-cap policy without making |
| 669 | // this unit test reserve the production 72 × 18.131 MiB extent. |
| 670 | let pool = HandlerStacks::new(WARM_HANDLER_STACKS, HANDLER_STACK_POOL); |
| 671 | let mut live = Vec::new(); |
| 672 | for round in 0..4 { |
| 673 | while let Some(stack) = pool.take(TEST_HANDLER_STACK) { |
| 674 | live.push(stack); |
| 675 | } |
| 676 | while live.len() < 72 { |
| 677 | live.push(Stack::new(TEST_HANDLER_STACK).unwrap()); |
| 678 | } |
| 679 | for stack in live.drain(..) { |
| 680 | pool.pool(stack, TEST_HANDLER_STACK); |
| 681 | } |
| 682 | assert_eq!(pool.stacks().len(), HANDLER_STACK_POOL); |
| 683 | let stats = pool.stats(); |
| 684 | assert_eq!(stats.warm_inserts, WARM_HANDLER_STACKS * (round + 1)); |
| 685 | assert_eq!( |
| 686 | stats.cold_releases, |
| 687 | (HANDLER_STACK_POOL - WARM_HANDLER_STACKS) * (round + 1) |
| 688 | ); |
| 689 | assert_eq!(stats.unmaps, (72 - HANDLER_STACK_POOL) * (round + 1)); |
| 690 | } |
| 691 | let stats = pool.stats(); |
| 692 | assert_eq!(stats.hits, HANDLER_STACK_POOL * 3); |
| 693 | assert_eq!(stats.misses, 4); |
| 694 | } |
| 695 | |
| 696 | #[test] |
| 697 | fn ordinary_handler_pool_drop_unmaps_retained_stack() { |
| 698 | // Shared's ordinary (non-PROGRAM_MODE) teardown drops this same vector. |
| 699 | // Probe by address, without dereferencing the dropped stack: an exclusive |
| 700 | // no-replace mapping can succeed only if the old extent was unmapped. |
| 701 | let _turn = test_turn(); |
| 702 | let pool = HandlerStacks::new(1, 1); |
| 703 | let stack = Stack::new(TEST_HANDLER_STACK).unwrap(); |
| 704 | let (base, len) = stack.map_extent(); |
| 705 | pool.pool(stack, TEST_HANDLER_STACK); |
| 706 | drop(pool); |
| 707 | // SAFETY: `base..base+len` was just released; NOREPLACE refuses rather |
| 708 | // than replacing anything another thread may have mapped there. The new |
| 709 | // PROT_NONE mapping is this test's own and is unmapped exactly once. |
| 710 | unsafe { |
| 711 | let replacement = libc::mmap( |
| 712 | base.cast(), |
| 713 | len, |
| 714 | libc::PROT_NONE, |
| 715 | libc::MAP_PRIVATE |
| 716 | | libc::MAP_ANONYMOUS |
| 717 | | libc::MAP_NORESERVE |
| 718 | | libc::MAP_FIXED_NOREPLACE, |
| 719 | -1, |
| 720 | 0, |
| 721 | ); |
| 722 | assert_ne!( |
| 723 | replacement, |
| 724 | libc::MAP_FAILED, |
| 725 | "retained extent is still mapped: {}", |
| 726 | std::io::Error::last_os_error() |
| 727 | ); |
| 728 | assert_eq!(libc::munmap(replacement, len), 0); |
| 729 | } |
| 730 | } |