| @@ -29,11 +29,23 @@ pub(crate) struct Stack { |
| 29 | 29 | /// The mapping, guard page first. |
| 30 | 30 | base: *mut u8, |
| 31 | 31 | len: usize, |
| 32 | | − /// Where the stack starts (it grows down): aligned on [`REGION`]. The |
| 33 | | − /// mapping's last bytes, above it, are never used. |
| 32 | + /// Aligned on [`REGION`]: what [`Stack::release`] gives back ends here. |
| 33 | + /// The mapping's last bytes, above it, are never used. |
| 34 | + region_top: usize, |
| 35 | + /// Where the stack starts (it grows down): `region_top` less this |
| 36 | + /// stack's color. |
| 34 | 37 | top: usize, |
| 35 | 38 | } |
| 36 | 39 | |
| 40 | +/// Stacks start a varying distance below their aligned top, in steps of a |
| 41 | +/// page and a cache line. Aligned alike, the top frames of every stack fell |
| 42 | +/// in the same cache sets: a 1,000-actor ring (`tests/bench/messages.lse`) |
| 43 | +/// ran 5% slower, most of it in `tl_fiber_switch`. The pages above a |
| 44 | +/// stack's start are never touched, so a color costs no memory. |
| 45 | +const COLORS: usize = 32; |
| 46 | +const COLOR_STEP: usize = PAGE + 64; |
| 47 | +static NEXT_COLOR: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 48 | + |
| 37 | 49 | // SAFETY: a stack is plain memory, owned by one fiber at a time. |
| 38 | 50 | unsafe impl Send for Stack {} |
| 39 | 51 | |
| @@ -53,7 +65,7 @@ impl Stack { |
| 53 | 65 | pub(crate) fn new(size: usize) -> std::io::Result<Stack> { |
| 54 | 66 | // The guard page, the stack, and room to align its top: virtual |
| 55 | 67 | // memory only, never touched. |
| 56 | | − let len = PAGE + size.div_ceil(PAGE) * PAGE + REGION; |
| 68 | + let len = PAGE + size.div_ceil(PAGE) * PAGE + COLORS * COLOR_STEP + REGION; |
| 57 | 69 | // SAFETY: a fresh private anonymous mapping; nothing aliases it. |
| 58 | 70 | unsafe { |
| 59 | 71 | let base = libc::mmap( |
| @@ -73,13 +85,15 @@ impl Stack { |
| 73 | 85 | libc::munmap(base, len); |
| 74 | 86 | return Err(e); |
| 75 | 87 | } |
| 76 | | − // At least `PAGE + size` above `base`, since the mapping is |
| 77 | | − // `REGION` longer than that. |
| 78 | | − let top = (base as usize + len) & !(REGION - 1); |
| 88 | + // At least `PAGE + size` above `base` once colored, since the |
| 89 | + // mapping is `REGION` and every color longer than that. |
| 90 | + let region_top = (base as usize + len) & !(REGION - 1); |
| 91 | + let color = NEXT_COLOR.fetch_add(1, std::sync::atomic::Ordering::Relaxed) % COLORS; |
| 79 | 92 | Ok(Stack { |
| 80 | 93 | base: base.cast(), |
| 81 | 94 | len, |
| 82 | | − top, |
| 95 | + region_top, |
| 96 | + top: region_top - color * COLOR_STEP, |
| 83 | 97 | }) |
| 84 | 98 | } |
| 85 | 99 | } |
| @@ -95,12 +109,12 @@ impl Stack { |
| 95 | 109 | /// suspended one already saved ([`Coroutine::hibernate`]). |
| 96 | 110 | pub(crate) fn release(&self) { |
| 97 | 111 | let low = self.base as usize + PAGE; |
| 98 | | − // SAFETY: `low..top` is inside the mapping, above the guard page; |
| 99 | | − // the caller guarantees nothing on it is live. |
| 112 | + // SAFETY: `low..region_top` is inside the mapping, above the guard |
| 113 | + // page; the caller guarantees nothing on it is live. |
| 100 | 114 | let r = unsafe { |
| 101 | 115 | libc::madvise( |
| 102 | 116 | low as *mut libc::c_void, |
| 103 | | − self.top - low, |
| 117 | + self.region_top - low, |
| 104 | 118 | libc::MADV_DONTNEED, |
| 105 | 119 | ) |
| 106 | 120 | }; |
| @@ -880,9 +894,10 @@ mod tests { |
| 880 | 894 | fn stack_tops_are_aligned_and_leave_the_size_asked_for() { |
| 881 | 895 | for size in [64 << 10, 256 << 10, 64 << 20] { |
| 882 | 896 | let s = Stack::new(size).unwrap(); |
| 883 | | − assert_eq!(s.top() % REGION, 0); |
| 897 | + assert_eq!(s.region_top % REGION, 0); |
| 898 | + assert!(s.top() <= s.region_top); |
| 884 | 899 | assert!(s.top() - (s.base as usize + PAGE) >= size); |
| 885 | | − assert!(s.top() <= s.base as usize + s.len); |
| 900 | + assert!(s.region_top <= s.base as usize + s.len); |
| 886 | 901 | } |
| 887 | 902 | } |
| 888 | 903 | |