| @@ -198,6 +198,79 @@ const STACK_POOL: usize = 16 << 10; |
| 198 | 198 | /// Pooled stacks kept as they are: actors that finish and are replaced at |
| 199 | 199 | /// once (`tests/stress/actors.lse`) reuse them without a system call. |
| 200 | 200 | const WARM_STACKS: usize = 256; |
| 201 | + |
| 202 | +/// Virtual size of a handler stack, distinct from the 64 MiB compiled |
| 203 | +/// actor-stack class above. These mappings have their own pool so neither |
| 204 | +/// size can be handed to a fiber expecting the other. |
| 205 | +pub(crate) const HANDLER_STACK_SIZE: usize = 16 << 20; |
| 206 | +/// Finished 16 MiB handler mappings retained for reuse. Start much smaller |
| 207 | +/// than the actor pool: 64 mappings cost about 1.2 GiB of lazily committed |
| 208 | +/// address space on x86-64 (including guard/colour/alignment overhead), and |
| 209 | +/// sequential handler churn needs only one. Like the actor pool, concurrent |
| 210 | +/// length checks make this a loose bound (two threads may both see room). |
| 211 | +const HANDLER_STACK_POOL: usize = 64; |
| 212 | +/// Pooled handler mappings kept warm; beyond this, `madvise` releases their |
| 213 | +/// pages and page tables while retaining the mappings for later reuse. |
| 214 | +const WARM_HANDLER_STACKS: usize = 16; |
| 215 | + |
| 216 | +/// A bounded pool for the handler mapping class only. Actor pooling stays |
| 217 | +/// separate; test-local capacities exercise the limits without allocating |
| 218 | +/// the production number of 16 MiB mappings. |
| 219 | +struct HandlerStacks { |
| 220 | + stacks: Mutex<Vec<Stack>>, |
| 221 | + warm: usize, |
| 222 | + capacity: usize, |
| 223 | +} |
| 224 | + |
| 225 | +impl HandlerStacks { |
| 226 | + fn new(warm: usize, capacity: usize) -> HandlerStacks { |
| 227 | + HandlerStacks { |
| 228 | + stacks: Mutex::new(Vec::new()), |
| 229 | + warm, |
| 230 | + capacity, |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + fn stacks(&self) -> MutexGuard<'_, Vec<Stack>> { |
| 235 | + #[cfg(feature = "lock-stats")] |
| 236 | + return lock_stats::lock(lock_stats::Kind::Stacks, &self.stacks); |
| 237 | + #[cfg(not(feature = "lock-stats"))] |
| 238 | + self.stacks.lock().expect("louise: scheduler poisoned") |
| 239 | + } |
| 240 | + |
| 241 | + /// Take a mapping whose requested usable size is exactly `size`. |
| 242 | + fn take(&self, size: usize) -> Option<Stack> { |
| 243 | + let stack = self.stacks().pop(); |
| 244 | + if let Some(stack) = &stack { |
| 245 | + debug_assert_eq!(stack.size(), size, "louise: wrong handler stack size"); |
| 246 | + } |
| 247 | + stack |
| 248 | + } |
| 249 | + |
| 250 | + /// Retain a finished handler mapping, or unmap it past the bound. The |
| 251 | + /// size assertion keeps handler and actor mappings in separate classes. |
| 252 | + /// `madvise` and `munmap` run only while no pool lock is held. |
| 253 | + fn pool(&self, stack: Stack, size: usize) { |
| 254 | + debug_assert_eq!(stack.size(), size, "louise: wrong handler stack size"); |
| 255 | + let pooled = self.stacks().len(); |
| 256 | + if pooled >= self.capacity { |
| 257 | + return; |
| 258 | + } |
| 259 | + if pooled >= self.warm { |
| 260 | + stack.release(); |
| 261 | + } |
| 262 | + self.stacks().push(stack); |
| 263 | + } |
| 264 | + |
| 265 | + /// Forget all retained mappings at true whole-process shutdown. Normal |
| 266 | + /// `Shared` drop still drops this vector and unmaps every stack. |
| 267 | + fn forget_all(&self) { |
| 268 | + for stack in std::mem::take(&mut *self.stacks()) { |
| 269 | + std::mem::forget(stack); |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 201 | 274 | /// How long a parked actor stays warm, in wall time, before its stack is |
| 202 | 275 | /// hibernated: it is looked at again two epochs after it was listed, so |
| 203 | 276 | /// after 400 to 600 ms (ADR 0028). With 50 ms, the 2,001 servers of |
| @@ -307,7 +380,7 @@ struct Control { |
| 307 | 380 | /// timer heap, then an actor's `proc` lock, then the list of parked |
| 308 | 381 | /// threads or one thread's parker (`park`). A |
| 309 | 382 | /// thread holding one never takes an earlier one, and holds at most one |
| 310 | | −/// actor lock. The actor table (one shard of it) and the stack pool are |
| 383 | +/// actor lock. The actor table (one shard of it) and each stack pool are |
| 311 | 384 | /// each held only for one operation, never while taking another lock, so |
| 312 | 385 | /// they may be taken under any of them (timers find their actors through |
| 313 | 386 | /// the table while holding `control` and a heap). |
| @@ -317,6 +390,9 @@ pub(crate) struct Shared { |
| 317 | 390 | /// actors. Its own lock: under `control`, spawning and finishing |
| 318 | 391 | /// actors waited on sockets and blocking calls. |
| 319 | 392 | stacks: Mutex<Vec<Stack>>, |
| 393 | + /// Separately sized finished handler mappings. As for actor stacks, |
| 394 | + /// completion and creation may occur on different scheduler threads. |
| 395 | + handler_stacks: HandlerStacks, |
| 320 | 396 | next_pid: AtomicI64, |
| 321 | 397 | next_tag: AtomicI64, |
| 322 | 398 | /// Time in milliseconds: virtual (jumps happen under `control`), or |
| @@ -380,6 +456,7 @@ impl Shared { |
| 380 | 456 | Shared { |
| 381 | 457 | actors: Table::default(), |
| 382 | 458 | stacks: Mutex::new(Vec::new()), |
| 459 | + handler_stacks: HandlerStacks::new(WARM_HANDLER_STACKS, HANDLER_STACK_POOL), |
| 383 | 460 | next_pid: AtomicI64::new(0), |
| 384 | 461 | next_tag: AtomicI64::new(0), |
| 385 | 462 | clock: AtomicU64::new(0), |
| @@ -428,6 +505,25 @@ impl Shared { |
| 428 | 505 | self.stacks.lock().expect("louise: scheduler poisoned") |
| 429 | 506 | } |
| 430 | 507 | |
| 508 | + /// Take a 16 MiB handler mapping, never a 64 MiB actor mapping. |
| 509 | + pub(crate) fn take_handler_stack(&self) -> Option<Stack> { |
| 510 | + self.handler_stacks.take(HANDLER_STACK_SIZE) |
| 511 | + } |
| 512 | + |
| 513 | + /// Pool a 16 MiB handler mapping whose coroutine returned and whose old |
| 514 | + /// addresses no snapshot can restore. The callers prove this lifecycle |
| 515 | + /// condition; this method supplies only the bounded retention policy. |
| 516 | + pub(crate) fn pool_handler_stack(&self, stack: Stack) { |
| 517 | + if PROGRAM_MODE.load(Ordering::Relaxed) && self.stopping.load(Ordering::Acquire) { |
| 518 | + // Mirror finished actor stacks during whole-process shutdown: |
| 519 | + // the process reclaims the mapping, not normal embedded/test |
| 520 | + // teardown (where `PROGRAM_MODE` is false). |
| 521 | + std::mem::forget(stack); |
| 522 | + return; |
| 523 | + } |
| 524 | + self.handler_stacks.pool(stack, HANDLER_STACK_SIZE); |
| 525 | + } |
| 526 | + |
| 431 | 527 | fn cell(&self, pid: i64) -> Option<Arc<Cell>> { |
| 432 | 528 | self.actors.get(pid) |
| 433 | 529 | } |
| @@ -1240,6 +1336,7 @@ pub unsafe extern "C" fn tl_main_program( |
| 1240 | 1336 | for stack in std::mem::take(&mut *sched.stacks()) { |
| 1241 | 1337 | std::mem::forget(stack); |
| 1242 | 1338 | } |
| 1339 | + sched.handler_stacks.forget_all(); |
| 1243 | 1340 | // Every actor stopped: write what they printed before reporting. |
| 1244 | 1341 | crate::console::flush(); |
| 1245 | 1342 | match exit { |