| @@ -73,6 +73,31 @@ const STACK_SIZE: usize = 256 * 1024; |
| 73 | 73 | const PROGRAM_STACK_SIZE: usize = 64 << 20; |
| 74 | 74 | /// The root actor (`main`). |
| 75 | 75 | const ROOT_STACK_SIZE: usize = 1 << 30; |
| 76 | + |
| 77 | +/// `LOUISE_STACK_KIB`, if set: the size of every compiled actor's stack, |
| 78 | +/// the root's included, in KiB. Tests use it to check that deep recursion |
| 79 | +/// runs in constant stack (tail calls, `return`, TRMC) without a 1 GiB |
| 80 | +/// stack hiding the difference. |
| 81 | +fn stack_override() -> Option<usize> { |
| 82 | + static KIB: OnceLock<Option<usize>> = OnceLock::new(); |
| 83 | + *KIB.get_or_init(|| { |
| 84 | + std::env::var("LOUISE_STACK_KIB") |
| 85 | + .ok() |
| 86 | + .and_then(|kib| kib.parse::<usize>().ok()) |
| 87 | + .filter(|kib| *kib > 0) |
| 88 | + .map(|kib| kib * 1024) |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +/// The stack size of compiled actors other than the root. |
| 93 | +fn program_stack_size() -> usize { |
| 94 | + stack_override().unwrap_or(PROGRAM_STACK_SIZE) |
| 95 | +} |
| 96 | + |
| 97 | +/// The stack size of the root actor. |
| 98 | +fn root_stack_size() -> usize { |
| 99 | + stack_override().unwrap_or(ROOT_STACK_SIZE) |
| 100 | +} |
| 76 | 101 | /// The reactor token of the blocking pool's waker (sockets count from 1). |
| 77 | 102 | const WAKER: mio::Token = mio::Token(0); |
| 78 | 103 | /// Finished program stacks kept for reuse: mapping and unmapping one per |
| @@ -1092,7 +1117,7 @@ fn spawn_actor( |
| 1092 | 1117 | let monitor = parent |
| 1093 | 1118 | .filter(|_| tie == Tie::Monitor) |
| 1094 | 1119 | .map(|watcher| (watcher, sched.tag())); |
| 1095 | | − let pooled = (stack_size == PROGRAM_STACK_SIZE) |
| 1120 | + let pooled = (stack_size == program_stack_size()) |
| 1096 | 1121 | .then(|| sched.stacks().pop()) |
| 1097 | 1122 | .flatten(); |
| 1098 | 1123 | let stack = pooled |
| @@ -1178,7 +1203,7 @@ pub(crate) unsafe fn spawn_program(ctx: *mut Ctx, body: Value, tie: Tie) -> (i64 |
| 1178 | 1203 | spawn_actor( |
| 1179 | 1204 | sched, |
| 1180 | 1205 | Body::Program(body), |
| 1181 | | − PROGRAM_STACK_SIZE, |
| 1206 | + program_stack_size(), |
| 1182 | 1207 | Some(a.pid), |
| 1183 | 1208 | tie, |
| 1184 | 1209 | ) |
| @@ -1629,7 +1654,7 @@ fn run_slice(sched: &Shared, pid: i64) { |
| 1629 | 1654 | cell.lock().run.finish(); |
| 1630 | 1655 | let empty = sched.actors.remove(pid); |
| 1631 | 1656 | drop(cell); |
| 1632 | | − if fiber.stack_size == PROGRAM_STACK_SIZE { |
| 1657 | + if fiber.stack_size == program_stack_size() { |
| 1633 | 1658 | let stack = fiber.co.into_stack(); |
| 1634 | 1659 | if PROGRAM_MODE.load(Ordering::Relaxed) && sched.stopping.load(Ordering::Acquire) { |
| 1635 | 1660 | // The program is ending: the process's exit frees every |
| @@ -2002,7 +2027,13 @@ pub extern "C" fn tl_actor_count() -> i64 { |
| 2002 | 2027 | pub unsafe extern "C" fn tl_main(entry: EntryFn) -> i32 { |
| 2003 | 2028 | let threads = thread_count(); |
| 2004 | 2029 | let sched = Shared::new(threads); |
| 2005 | | − let (root, _) = spawn_actor(&sched, Body::Entry(entry), ROOT_STACK_SIZE, None, Tie::None); |
| 2030 | + let (root, _) = spawn_actor( |
| 2031 | + &sched, |
| 2032 | + Body::Entry(entry), |
| 2033 | + root_stack_size(), |
| 2034 | + None, |
| 2035 | + Tie::None, |
| 2036 | + ); |
| 2006 | 2037 | let exit = run(&sched, root, threads); |
| 2007 | 2038 | crate::console::flush(); |
| 2008 | 2039 | match exit { |
| @@ -2065,7 +2096,13 @@ pub unsafe extern "C" fn tl_main_program( |
| 2065 | 2096 | std::process::abort(); |
| 2066 | 2097 | }; |
| 2067 | 2098 | sched.actor_caps = kinds; |
| 2068 | | − let (root, _) = spawn_actor(&sched, Body::Entry(entry), ROOT_STACK_SIZE, None, Tie::None); |
| 2099 | + let (root, _) = spawn_actor( |
| 2100 | + &sched, |
| 2101 | + Body::Entry(entry), |
| 2102 | + root_stack_size(), |
| 2103 | + None, |
| 2104 | + Tie::None, |
| 2105 | + ); |
| 2069 | 2106 | let exit = run(&sched, root, threads); |
| 2070 | 2107 | // The process exits next, which frees the pooled stacks' mappings at |
| 2071 | 2108 | // once: no `munmap` each. (The list itself is freed, for LeakSanitizer.) |