| @@ -76,8 +76,14 @@ const ROOT_STACK_SIZE: usize = 1 << 30; |
| 76 | 76 | /// The reactor token of the blocking pool's waker (sockets count from 1). |
| 77 | 77 | const WAKER: mio::Token = mio::Token(0); |
| 78 | 78 | /// Finished program stacks kept for reuse: mapping and unmapping one per |
| 79 | | −/// actor is slow, and with several threads every unmap interrupts them all. |
| 80 | | −const STACK_POOL: usize = 256; |
| 79 | +/// actor is slow (unmapping took 40% of `tests/bench/idle.lse`, ADR 0028), |
| 80 | +/// and with several threads every unmap interrupts them all. Beyond |
| 81 | +/// [`WARM_STACKS`], a pooled stack is released, so it costs only its two |
| 82 | +/// kernel mappings (and 66 MiB of address space, of 128 TiB). |
| 83 | +const STACK_POOL: usize = 16 << 10; |
| 84 | +/// Pooled stacks kept as they are: actors that finish and are replaced at |
| 85 | +/// once (`tests/stress/actors.lse`) reuse them without a system call. |
| 86 | +const WARM_STACKS: usize = 256; |
| 81 | 87 | /// How long a parked actor stays warm, in wall time, before its stack is |
| 82 | 88 | /// hibernated: it is looked at again two epochs after it was listed, so |
| 83 | 89 | /// after 50 to 150 ms (ADR 0028). `LOUISE_PARKED_EPOCH_MS` changes it; at |
| @@ -381,6 +387,8 @@ pub(crate) struct Shared { |
| 381 | 387 | idling: AtomicBool, |
| 382 | 388 | /// Every actor is gone: the threads stop. |
| 383 | 389 | finished: AtomicBool, |
| 390 | + /// The root exited: every other actor is stopping. |
| 391 | + stopping: AtomicBool, |
| 384 | 392 | /// Wall time, in [`PARKED_EPOCH`]s, for hibernating parked actors: |
| 385 | 393 | /// counted by whichever thread sees one pass. |
| 386 | 394 | epoch: AtomicU32, |
| @@ -651,6 +659,7 @@ impl Shared { |
| 651 | 659 | running: AtomicUsize::new(0), |
| 652 | 660 | idling: AtomicBool::new(false), |
| 653 | 661 | finished: AtomicBool::new(false), |
| 662 | + stopping: AtomicBool::new(false), |
| 654 | 663 | epoch: AtomicU32::new(0), |
| 655 | 664 | epoch_at: AtomicU64::new(0), |
| 656 | 665 | started: Instant::now(), |
| @@ -1120,6 +1129,7 @@ pub(crate) fn exit_actor(sched: &Shared, pid: i64, reason: Exit) { |
| 1120 | 1129 | |
| 1121 | 1130 | /// `main` stopped: stop every other actor, silently. |
| 1122 | 1131 | fn stop_all(sched: &Shared) { |
| 1132 | + sched.stopping.store(true, Ordering::Release); |
| 1123 | 1133 | let cells: Vec<(i64, Arc<Cell>)> = sched |
| 1124 | 1134 | .actors |
| 1125 | 1135 | .read() |
| @@ -1445,9 +1455,20 @@ fn run_slice(sched: &Shared, pid: i64) { |
| 1445 | 1455 | }; |
| 1446 | 1456 | drop(cell); |
| 1447 | 1457 | if fiber.stack_size == PROGRAM_STACK_SIZE { |
| 1448 | | − let mut c = sched.control(); |
| 1449 | | − if c.stacks.len() < STACK_POOL { |
| 1450 | | − c.stacks.push(fiber.co.into_stack()); |
| 1458 | + let stack = fiber.co.into_stack(); |
| 1459 | + if PROGRAM_MODE.load(Ordering::Relaxed) && sched.stopping.load(Ordering::Acquire) { |
| 1460 | + // The program is ending: the process's exit frees every |
| 1461 | + // mapping at once, far faster than one `munmap` each |
| 1462 | + // (ADR 0028). |
| 1463 | + std::mem::forget(stack); |
| 1464 | + } else { |
| 1465 | + let mut c = sched.control(); |
| 1466 | + if c.stacks.len() < STACK_POOL { |
| 1467 | + if c.stacks.len() >= WARM_STACKS { |
| 1468 | + stack.release(); |
| 1469 | + } |
| 1470 | + c.stacks.push(stack); |
| 1471 | + } |
| 1451 | 1472 | } |
| 1452 | 1473 | } |
| 1453 | 1474 | // SAFETY: the fiber has returned; nothing refers to the actor. |
| @@ -1843,6 +1864,11 @@ pub unsafe extern "C" fn tl_main_program(entry: EntryFn) -> i32 { |
| 1843 | 1864 | let sched = Shared::new(threads); |
| 1844 | 1865 | let (root, _) = spawn_actor(&sched, Body::Entry(entry), ROOT_STACK_SIZE, None, Tie::None); |
| 1845 | 1866 | let exit = run(&sched, root, threads); |
| 1867 | + // The process exits next, which frees the pooled stacks' mappings at |
| 1868 | + // once: no `munmap` each. (The list itself is freed, for LeakSanitizer.) |
| 1869 | + for stack in std::mem::take(&mut sched.control().stacks) { |
| 1870 | + std::mem::forget(stack); |
| 1871 | + } |
| 1846 | 1872 | // Every actor stopped: write what they printed before reporting. |
| 1847 | 1873 | crate::console::flush(); |
| 1848 | 1874 | match exit { |