memopro 0.2.0

Budgeted memory runtime: keep, losslessly compress, re-read from verified source files or recompute large buffers so work runs within a guaranteed memory ceiling (alpha).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
//! macOS implementation of [`Pager`] (0229): page faults arrive as signals.
//!
//! macOS has no userfaultfd. Each pager reserves one large address range (the arena) with no
//! access and hands out regions from it. A chunk that is not in memory has no access; touching
//! it raises SIGBUS (or SIGSEGV). The signal handler does no paging itself: it reads the chunk's
//! state (an atomic per chunk), asks the pager thread for the chunk by writing its index to a
//! pipe, and sleeps until the state says the chunk is back. Everything else (choosing,
//! compressing, decompressing) runs on the pager thread, outside any signal handler.
//!
//! A chunk comes back whole: it is decompressed into a fresh mapping that is then moved into
//! place with `mach_vm_remap(VM_FLAGS_OVERWRITE)`, so no thread sees it half filled. Eviction
//! write-protects the chunk (readers go on, writers wait), compresses it, then replaces it with
//! a fresh mapping without access, which gives its pages back to the OS at once.
//!
//! Faults of other origin (addresses outside every arena, freed regions) go to the handler that
//! was installed before ours. Kernel accesses to a chunk that is not in memory (`read(2)` into
//! it, `write(2)` from it) are not faults but fail with `EFAULT`, as with Linux user-mode-only
//! userfaultfd.

use super::{PagerConfig, PagerStats};
use crate::codec;
use crate::error::{Error, Result};
use crate::rt::page_size;
use crate::rt::region::Region as Mapped;
use std::collections::{BTreeMap, HashMap};
use std::io;
use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use std::ptr::null_mut;
use std::sync::atomic::{AtomicPtr, AtomicU8, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
use std::thread::JoinHandle;
use std::time::Instant;

/// At most this many most recently faulted chunks count as in use and are not evicted.
const HOT_CHUNKS: u64 = 16;
/// Chunks the pager always keeps room for in memory, whatever the budget, a process budget and
/// the compressed copies leave: with room for fewer, the two operands of one copy evicted each
/// other forever (0230). Going over the budget to keep them is counted in `overruns`.
const MIN_CHUNKS: u64 = 8;
/// Milliseconds between looks at the process footprint when nothing faults (process budget).
const TRIM_MS: libc::c_int = 10;
/// Pagers that can exist at once in a process.
const SLOTS: usize = 64;

// chunk states, read by the signal handler
const FREE: u8 = 0; // not part of a region
const ABSENT: u8 = 1; // never touched: no access, comes back as zeros
const OUT: u8 = 2; // compressed: no access
const EVICTING: u8 = 3; // write-protected while it is compressed
const RESIDENT: u8 = 4; // readable and writable

unsafe extern "C" {
    fn mach_vm_remap(
        target_task: libc::mach_port_t,
        target_address: *mut u64,
        size: u64,
        mask: u64,
        flags: libc::c_int,
        src_task: libc::mach_port_t,
        src_address: u64,
        copy: libc::c_int,
        cur_protection: *mut libc::c_int,
        max_protection: *mut libc::c_int,
        inheritance: libc::c_uint,
    ) -> libc::c_int;
}
// <mach/vm_statistics.h>, <mach/vm_inherit.h>
const VM_FLAGS_FIXED: libc::c_int = 0;
const VM_FLAGS_OVERWRITE: libc::c_int = 0x4000;
const VM_INHERIT_COPY: libc::c_uint = 1;

unsafe extern "C" {
    static mach_task_self_: libc::mach_port_t;
}

fn task() -> libc::mach_port_t {
    // SAFETY: set by the system before any code runs; never changes.
    unsafe { mach_task_self_ }
}

thread_local! {
    static PAGER_THREAD: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// Whether the calling thread is a pager's own.
/// The pager's own pool: as many threads as cores, at most 8, each marked a pager thread.
fn pager_pool() -> Result<rayon::ThreadPool> {
    let n = std::thread::available_parallelism()
        .map_or(1, |n| n.get())
        .min(8);
    rayon::ThreadPoolBuilder::new()
        .num_threads(n)
        .thread_name(|i| format!("memopro-pager-{i}"))
        .start_handler(|_| PAGER_THREAD.with(|c| c.set(true)))
        .build()
        .map_err(|e| Error::Unsupported(format!("pager threads: {e}")))
}

pub fn in_pager_thread() -> bool {
    PAGER_THREAD.with(|c| c.get())
}

/// What the signal handler needs of a pager. Never freed (a handler may still read it after the
/// pager is gone; its states then say FREE).
struct Arena {
    base: usize,
    len: usize,
    chunk: usize,
    states: Box<[AtomicU8]>,
    /// Write end of the request pipe (non-blocking).
    request: RawFd,
    /// Requests served so far (a waiting handler asks again once it moved on).
    served: AtomicU64,
}

impl Arena {
    fn index(&self, addr: usize) -> Option<usize> {
        (addr >= self.base && addr < self.base + self.len).then(|| (addr - self.base) / self.chunk)
    }

    /// Wait in a signal handler until chunk `i` is accessible; false if it is not ours.
    fn wait(&self, i: usize) -> bool {
        let mut asked: Option<u64> = None;
        loop {
            match self.states[i].load(Ordering::Acquire) {
                RESIDENT => return true,
                FREE => return false,
                EVICTING => {}
                _ => {
                    let served = self.served.load(Ordering::Acquire);
                    if asked.is_none_or(|a| served > a) {
                        let msg = i as u64;
                        // SAFETY: write(2) is async-signal-safe; 8 bytes from a local.
                        unsafe {
                            libc::write(self.request, (&msg as *const u64).cast(), 8);
                        }
                        asked = Some(served);
                    }
                }
            }
            let ts = libc::timespec {
                tv_sec: 0,
                tv_nsec: 20_000,
            };
            // SAFETY: nanosleep(2) is async-signal-safe.
            unsafe { libc::nanosleep(&ts, null_mut()) };
        }
    }
}

static ARENAS: [AtomicPtr<Arena>; SLOTS] = [const { AtomicPtr::new(null_mut()) }; SLOTS];
/// The handlers installed before ours: SIGBUS, SIGSEGV.
static PREVIOUS: OnceLock<[libc::sigaction; 2]> = OnceLock::new();

extern "C" fn on_fault(sig: libc::c_int, info: *mut libc::siginfo_t, ctx: *mut libc::c_void) {
    // SAFETY: the kernel passes a valid siginfo for SA_SIGINFO handlers.
    let addr = unsafe { (*info).si_addr } as usize;
    for slot in &ARENAS {
        let a = slot.load(Ordering::Acquire);
        if a.is_null() {
            continue;
        }
        // SAFETY: arenas are never freed.
        let arena = unsafe { &*a };
        if let Some(i) = arena.index(addr) {
            if arena.wait(i) {
                return; // the access runs again and finds the chunk
            }
            break;
        }
    }
    chain(sig, info, ctx);
}

/// Hand a fault that is not ours to the handler installed before ours.
fn chain(sig: libc::c_int, info: *mut libc::siginfo_t, ctx: *mut libc::c_void) {
    let Some(previous) = PREVIOUS.get() else {
        return;
    };
    let old = &previous[if sig == libc::SIGBUS { 0 } else { 1 }];
    let handler = old.sa_sigaction;
    if handler == libc::SIG_DFL || handler == libc::SIG_IGN {
        // the default action happens when the access runs again
        // SAFETY: sigaction(2) is async-signal-safe.
        unsafe { libc::sigaction(sig, old, null_mut()) };
    } else if old.sa_flags & libc::SA_SIGINFO != 0 {
        // SAFETY: the previous handler, called the way it was installed.
        let f: extern "C" fn(libc::c_int, *mut libc::siginfo_t, *mut libc::c_void) =
            unsafe { std::mem::transmute(handler) };
        f(sig, info, ctx);
    } else {
        // SAFETY: as above.
        let f: extern "C" fn(libc::c_int) = unsafe { std::mem::transmute(handler) };
        f(sig);
    }
}

fn install_handler() -> Result<()> {
    static DONE: OnceLock<std::result::Result<(), String>> = OnceLock::new();
    DONE.get_or_init(|| {
        // SAFETY: plain sigaction calls with zeroed, then filled, structures.
        unsafe {
            let mut old: [libc::sigaction; 2] = std::mem::zeroed();
            let mut new: libc::sigaction = std::mem::zeroed();
            new.sa_sigaction = on_fault as *const () as usize;
            new.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;
            libc::sigemptyset(&mut new.sa_mask);
            if libc::sigaction(libc::SIGBUS, null_mut(), &mut old[0]) != 0
                || libc::sigaction(libc::SIGSEGV, null_mut(), &mut old[1]) != 0
            {
                return Err(io::Error::last_os_error().to_string());
            }
            let _ = PREVIOUS.set(old);
            if libc::sigaction(libc::SIGBUS, &new, null_mut()) != 0
                || libc::sigaction(libc::SIGSEGV, &new, null_mut()) != 0
            {
                return Err(io::Error::last_os_error().to_string());
            }
        }
        Ok(())
    })
    .clone()
    .map_err(|e| Error::Unsupported(format!("cannot install the page fault handler: {e}")))
}

fn pipe() -> io::Result<(OwnedFd, OwnedFd)> {
    let mut fds = [0 as RawFd; 2];
    // SAFETY: pipe(2) fills two descriptors we then own.
    if unsafe { libc::pipe(fds.as_mut_ptr()) } != 0 {
        return Err(io::Error::last_os_error());
    }
    for fd in fds {
        // SAFETY: fcntl on descriptors we own.
        unsafe {
            libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC);
            libc::fcntl(fd, libc::F_SETFL, libc::O_NONBLOCK);
        }
    }
    // SAFETY: fresh descriptors.
    Ok(unsafe { (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])) })
}

/// Replace `len` bytes at `addr` with a fresh mapping that has no access (pages go back).
fn blank(addr: usize, len: usize) -> io::Result<()> {
    // SAFETY: `addr..addr+len` lies in our arena; MAP_FIXED replaces only that range.
    let p = unsafe {
        libc::mmap(
            addr as *mut libc::c_void,
            len,
            libc::PROT_NONE,
            libc::MAP_PRIVATE | libc::MAP_ANON | libc::MAP_FIXED,
            -1,
            0,
        )
    };
    if p == libc::MAP_FAILED {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

fn protect(addr: usize, len: usize, prot: libc::c_int) -> io::Result<()> {
    // SAFETY: a range of our arena.
    if unsafe { libc::mprotect(addr as *mut libc::c_void, len, prot) } != 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}

enum Data {
    Absent,
    Resident,
    Compressed(Mapped),
}

struct Chunk {
    data: Data,
    last: u64,
    period: f64,
    faults: u64,
    incompressible: bool,
}

struct State {
    /// First chunk index of each region -> its chunk count.
    regions: BTreeMap<usize, usize>,
    /// Free runs of chunks: first index -> count.
    free: BTreeMap<usize, usize>,
    chunks: HashMap<usize, Chunk>,
    clock: u64,
    resident: u64,
    compressed: u64,
    /// The last chunk limit under which the process budget could still be kept (0306).
    held: u64,
    /// Bytes of chunks found incompressible: they stay in memory for good (0306).
    fixed: u64,
    machine: super::MachineRoom,
    stats: PagerStats,
}

struct Shared {
    config: PagerConfig,
    limit: u64,
    arena: &'static Arena,
    slot: usize,
    requests: OwnedFd,
    stop_r: OwnedFd,
    stop_w: OwnedFd,
    state: Mutex<State>,
    /// Compresses chunks being evicted, several at once (0326); its threads count as the
    /// pager's own (`in_pager_thread`), and it is not the process-wide pool user code runs on.
    pool: rayon::ThreadPool,
}

/// Transparent paging under a budget; see the [module documentation](super).
pub struct Pager {
    shared: Arc<Shared>,
    thread: Option<JoinHandle<()>>,
}

impl Pager {
    pub fn new(config: PagerConfig) -> Result<Pager> {
        let page = page_size();
        let chunk = config.chunk;
        if !matches!(config.elem, 1 | 2 | 4 | 8 | 16)
            || chunk == 0
            || chunk % page != 0
            || chunk > codec::CHUNK
            || chunk % config.elem != 0
        {
            return Err(Error::InvalidArgument(format!(
                "chunk {chunk} must be a multiple of the page size ({page}) and of the element \
                 size {}, at most {}",
                config.elem,
                codec::CHUNK
            )));
        }
        let limit = config
            .budget
            .checked_sub(chunk as u64)
            .filter(|l| *l >= chunk as u64)
            .ok_or_else(|| {
                Error::InvalidArgument(format!(
                    "a budget of {} bytes cannot hold two chunks of {chunk}",
                    config.budget
                ))
            })?;
        install_handler()?;
        let slot = ARENAS
            .iter()
            .position(|s| s.load(Ordering::Acquire).is_null())
            .ok_or_else(|| Error::Unsupported(format!("at most {SLOTS} pagers at once")))?;
        // address space for the regions, sized to the machine (no memory until used)
        let physical = crate::hwinfo::memory().map_or(0, |m| m.total_bytes);
        let mut len = super::arena_bytes(physical, chunk);
        let base = loop {
            // SAFETY: a new reservation without access; nothing refers to it.
            let p = unsafe {
                libc::mmap(
                    null_mut(),
                    len,
                    libc::PROT_NONE,
                    libc::MAP_PRIVATE | libc::MAP_ANON,
                    -1,
                    0,
                )
            };
            if p != libc::MAP_FAILED {
                break p as usize;
            }
            if len <= 1 << 30 {
                return Err(Error::Io(io::Error::last_os_error()));
            }
            len /= 2;
            len -= len % chunk;
        };
        let (requests, request_w) = pipe().map_err(Error::Io)?;
        let (stop_r, stop_w) = pipe().map_err(Error::Io)?;
        let n = len / chunk;
        let arena: &'static Arena = Box::leak(Box::new(Arena {
            base,
            len,
            chunk,
            states: (0..n).map(|_| AtomicU8::new(FREE)).collect(),
            // the handler may write to it after the pager is gone: never closed
            request: request_w.into_raw_fd(),
            served: AtomicU64::new(0),
        }));
        if ARENAS[slot]
            .compare_exchange(
                null_mut(),
                (arena as *const Arena).cast_mut(),
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .is_err()
        {
            // SAFETY: our reservation, not handed out.
            unsafe { libc::munmap(base as *mut libc::c_void, len) };
            return Err(Error::Unsupported(
                "pager slots were taken concurrently".into(),
            ));
        }
        let stats = PagerStats {
            budget: config.budget,
            limit,
            limit_low: limit,
            ..PagerStats::default()
        };
        let shared = Arc::new(Shared {
            config,
            limit,
            arena,
            slot,
            requests,
            stop_r,
            stop_w,
            state: Mutex::new(State {
                regions: BTreeMap::new(),
                free: BTreeMap::from([(0, n)]),
                chunks: HashMap::new(),
                clock: 0,
                resident: 0,
                compressed: 0,
                held: 0,
                fixed: 0,
                machine: Default::default(),
                stats,
            }),
            pool: pager_pool()?,
        });
        let serving = shared.clone();
        let thread = std::thread::Builder::new()
            .name("memopro-pager".into())
            .spawn(move || serving.serve())
            .map_err(Error::Io)?;
        Ok(Pager {
            shared,
            thread: Some(thread),
        })
    }

    /// A zero-filled region of at least `len` bytes (whole chunks), paged within the budget.
    /// It stays valid until [`Pager::unmap`].
    pub fn map(&self, len: usize) -> Result<*mut u8> {
        if len == 0 {
            return Err(Error::InvalidArgument(
                "a region holds at least one byte".into(),
            ));
        }
        let arena = self.shared.arena;
        let n = len.div_ceil(arena.chunk);
        let mut st = self.shared.lock();
        let Some((&start, &run)) = st.free.iter().find(|(_, run)| **run >= n) else {
            return Err(Error::Budget(format!(
                "the pager's {} GiB of address space is used up",
                arena.len >> 30
            )));
        };
        st.free.remove(&start);
        if run > n {
            st.free.insert(start + n, run - n);
        }
        st.regions.insert(start, n);
        for i in start..start + n {
            st.chunks.insert(
                i,
                Chunk {
                    data: Data::Absent,
                    last: 0,
                    period: 0.0,
                    faults: 0,
                    incompressible: false,
                },
            );
            arena.states[i].store(ABSENT, Ordering::Release);
        }
        st.stats.regions += 1;
        st.stats.mapped_bytes += (n * arena.chunk) as u64;
        Ok((arena.base + start * arena.chunk) as *mut u8)
    }

    /// Give a region back; returns its length.
    ///
    /// # Safety
    /// `addr` came from [`Pager::map`] of this pager, and no one uses the region any more.
    pub unsafe fn unmap(&self, addr: *mut u8) -> Result<usize> {
        let arena = self.shared.arena;
        let chunk = arena.chunk;
        let not_ours = || Error::InvalidArgument(format!("{addr:p} is not a region of this pager"));
        let a = addr as usize;
        let start = arena
            .index(a)
            .filter(|i| arena.base + i * chunk == a)
            .ok_or_else(not_ours)?;
        let mut st = self.shared.lock();
        let n = st.regions.remove(&start).ok_or_else(not_ours)?;
        for i in start..start + n {
            arena.states[i].store(FREE, Ordering::Release);
            let removed = st.chunks.remove(&i);
            if removed
                .as_ref()
                .is_some_and(|c| c.incompressible && matches!(c.data, Data::Resident))
            {
                st.fixed -= chunk as u64;
            }
            match removed.map(|c| c.data) {
                Some(Data::Resident) => st.resident -= chunk as u64,
                Some(Data::Compressed(p)) => st.compressed -= p.capacity() as u64,
                _ => {}
            }
        }
        blank(a, n * chunk).map_err(Error::Io)?;
        // give the run back, merged with free neighbours
        let (mut first, mut count) = (start, n);
        if let Some((&prev, &len)) = st.free.range(..start).next_back()
            && prev + len == start
        {
            st.free.remove(&prev);
            first = prev;
            count += len;
        }
        if let Some(len) = st.free.remove(&(start + n)) {
            count += len;
        }
        st.free.insert(first, count);
        st.stats.regions -= 1;
        st.stats.mapped_bytes -= (n * chunk) as u64;
        Ok(n * chunk)
    }

    /// The length of the region starting at `addr`, if it is one of ours.
    pub fn owns(&self, addr: *const u8) -> Option<usize> {
        let arena = self.shared.arena;
        let a = addr as usize;
        let i = arena
            .index(a)
            .filter(|i| arena.base + i * arena.chunk == a)?;
        self.shared.lock().regions.get(&i).map(|n| n * arena.chunk)
    }

    /// Whether `addr` lies inside one of this pager's regions.
    pub fn contains(&self, addr: *const u8) -> bool {
        let arena = self.shared.arena;
        arena
            .index(addr as usize)
            .is_some_and(|i| arena.states[i].load(Ordering::Acquire) != FREE)
    }

    pub fn limit(&self) -> u64 {
        self.shared.limit
    }

    pub fn stats(&self) -> PagerStats {
        let st = self.shared.lock();
        let mut s = st.stats.clone();
        s.resident_bytes = st.resident;
        s.compressed_bytes = st.compressed;
        s.used = st.resident + st.compressed;
        s
    }
}

impl Drop for Pager {
    fn drop(&mut self) {
        let one = 1u8;
        // SAFETY: one byte to our stop pipe wakes the pager thread.
        unsafe {
            libc::write(
                self.shared.stop_w.as_raw_fd(),
                (&one as *const u8).cast(),
                1,
            )
        };
        if let Some(t) = self.thread.take() {
            let _ = t.join();
        }
        let arena = self.shared.arena;
        ARENAS[self.shared.slot].store(null_mut(), Ordering::Release);
        for s in arena.states.iter() {
            s.store(FREE, Ordering::Release);
        }
        // SAFETY: our reservation; users must be done with its regions (as with Linux).
        unsafe { libc::munmap(arena.base as *mut libc::c_void, arena.len) };
    }
}

impl Shared {
    fn lock(&self) -> MutexGuard<'_, State> {
        self.state.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// The pager thread: serve requests until stopped.
    fn serve(&self) {
        PAGER_THREAD.with(|c| c.set(true));
        let mut msgs = [0u64; 64];
        loop {
            let mut fds = [
                libc::pollfd {
                    fd: self.requests.as_raw_fd(),
                    events: libc::POLLIN,
                    revents: 0,
                },
                libc::pollfd {
                    fd: self.stop_r.as_raw_fd(),
                    events: libc::POLLIN,
                    revents: 0,
                },
            ];
            // SAFETY: two valid pollfd entries.
            let r = unsafe { libc::poll(fds.as_mut_ptr(), 2, self.tick()) };
            if r < 0 {
                continue; // EINTR
            }
            if r == 0 {
                self.trim();
                continue;
            }
            if fds[1].revents != 0 {
                return;
            }
            // SAFETY: reading whole u64 messages into a buffer of that many bytes.
            let n = unsafe {
                libc::read(
                    self.requests.as_raw_fd(),
                    msgs.as_mut_ptr().cast(),
                    std::mem::size_of_val(&msgs),
                )
            };
            if n <= 0 {
                continue;
            }
            let mut st = self.lock();
            for &i in &msgs[..n as usize / 8] {
                self.fault(&mut st, i as usize);
            }
            drop(st);
            self.arena.served.fetch_add(1, Ordering::AcqRel);
        }
    }

    fn fault(&self, st: &mut State, i: usize) {
        st.stats.faults += 1;
        let chunk = self.config.chunk;
        let cstart = self.arena.base + i * chunk;
        st.clock += 1;
        let now = st.clock;
        match st.chunks.get(&i).map(|c| &c.data) {
            None => return, // freed meanwhile: the handler sees FREE
            Some(Data::Resident) => {
                st.stats.spurious += 1;
                return;
            }
            _ => {}
        }
        if !self.make_room(st, chunk as u64, i) {
            st.stats.overruns += 1;
        }
        let t0 = Instant::now();
        let c = st.chunks.get_mut(&i).expect("checked above");
        let was = std::mem::replace(&mut c.data, Data::Resident);
        if c.faults > 0 {
            let gap = (now - c.last) as f64;
            c.period = if c.period == 0.0 {
                gap
            } else {
                0.5 * c.period + 0.5 * gap
            };
        }
        c.last = now;
        c.faults += 1;
        match was {
            Data::Absent => {
                // fresh pages are zeros, which is what the chunk holds: no one can see it
                // half filled
                if let Err(e) = protect(cstart, chunk, libc::PROT_READ | libc::PROT_WRITE) {
                    eprintln!("memopro pager: a chunk could not be made accessible ({e})");
                    std::process::abort();
                }
                st.stats.zero_fills += 1;
            }
            Data::Compressed(p) => {
                if let Err(e) = self.restore(cstart, p.as_slice()) {
                    // the only copy of this memory cannot come back: stopping is the only
                    // honest thing
                    eprintln!("memopro pager: a compressed chunk could not be restored ({e})");
                    std::process::abort();
                }
                st.compressed -= p.capacity() as u64;
                st.stats.restores += 1;
            }
            Data::Resident => {}
        }
        self.arena.states[i].store(RESIDENT, Ordering::Release);
        st.resident += chunk as u64;
        let used = st.resident + st.compressed;
        st.stats.peak_used = st.stats.peak_used.max(used);
        st.stats.restore_seconds += t0.elapsed().as_secs_f64();
    }

    /// Decompress into a fresh mapping and move it over the chunk in one step.
    fn restore(&self, cstart: usize, packed: &[u8]) -> Result<()> {
        let chunk = self.config.chunk;
        // SAFETY: a new private mapping only this thread uses.
        let tmp = unsafe {
            libc::mmap(
                null_mut(),
                chunk,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_PRIVATE | libc::MAP_ANON,
                -1,
                0,
            )
        };
        if tmp == libc::MAP_FAILED {
            return Err(Error::Io(io::Error::last_os_error()));
        }
        // SAFETY: the mapping made above, `chunk` bytes.
        let out = unsafe { std::slice::from_raw_parts_mut(tmp.cast::<u8>(), chunk) };
        let unpacked = codec::unpack_chunk(packed, self.config.elem, out).map_err(Error::Io);
        let moved = unpacked.and_then(|()| {
            let (mut target, mut cur, mut max) = (cstart as u64, 0, 0);
            // SAFETY: moves our own pages over our own chunk (both in this task).
            let kr = unsafe {
                mach_vm_remap(
                    task(),
                    &mut target,
                    chunk as u64,
                    0,
                    VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
                    task(),
                    tmp as u64,
                    0,
                    &mut cur,
                    &mut max,
                    VM_INHERIT_COPY,
                )
            };
            if kr == 0 && target == cstart as u64 {
                Ok(())
            } else {
                Err(Error::Io(io::Error::other(format!("mach_vm_remap: {kr}"))))
            }
        });
        // SAFETY: the temporary mapping; the chunk keeps its pages.
        unsafe { libc::munmap(tmp, chunk) };
        moved
    }

    /// How long the pager thread waits for faults before it looks at the process again: with a
    /// process budget, memory outside the pager (a sort's scratch, a C extension's tables) can
    /// grow while nothing faults, so every {TRIM_MS} ms it gives chunks up if the process is
    /// over (0230); without one, it only wakes for faults.
    fn tick(&self) -> libc::c_int {
        if self.config.process_budget.is_some() {
            TRIM_MS
        } else {
            -1
        }
    }

    fn trim(&self) {
        let mut st = self.lock();
        let before = st.stats.evictions;
        self.make_room(&mut st, 0, usize::MAX);
        st.stats.trims += st.stats.evictions - before;
    }

    fn make_room(&self, st: &mut State, need: u64, exclude: usize) -> bool {
        let mut limit = self.limit;
        if let Some(total) = self.config.process_budget {
            let outside = crate::rt::process_footprint()
                .unwrap_or(0)
                .saturating_sub(st.resident + st.compressed);
            limit = limit.min(total.saturating_sub(outside + self.config.chunk as u64));
            st.stats.outside_peak = st.stats.outside_peak.max(outside);
            st.stats.limit_low = st.stats.limit_low.min(limit);
            // memory outside the pager and the chunks that cannot move (compressed or
            // incompressible) alone pass the budget: squeezing the rest cannot keep it, only
            // refaults (Polars 40x slower, E053). The budget is lost
            // anyway, so the chunks may use their own budget as far as the machine still has
            // room without swapping (and at least what they last had while it could be kept);
            // memopro.enable still warns and reports the excess (0306)
            if limit < st.compressed + st.fixed + MIN_CHUNKS * self.config.chunk as u64 {
                let room = st.machine.cap(st.resident + st.compressed);
                let allowed = self.limit.min(room).max(st.held);
                if allowed > limit {
                    limit = allowed;
                    st.stats.holds += 1;
                }
            } else {
                st.held = limit;
            }
        }
        let hard = limit;
        // the machine's room too: never more than the system can still give without swapping,
        // or it swaps these chunks out (0332); the ceiling alone still decides overruns
        let room = st.machine.cap(st.resident + st.compressed);
        if room < limit && st.resident + st.compressed + need > room {
            st.stats.machine_limited += 1;
        }
        let limit = limit
            .min(room)
            .max(st.compressed + MIN_CHUNKS * self.config.chunk as u64);
        let chunk = self.config.chunk as u64;
        while st.resident + st.compressed + need > limit {
            let fits = (limit - st.compressed) / chunk;
            // the chunks this fault needs, then (cold ones only) up to one per pool thread:
            // compressed side by side, and the next faults find room without waiting (0326)
            let short = (st.resident + st.compressed + need - limit).div_ceil(chunk) as usize;
            let mut victims: Vec<usize> = Vec::new();
            // ahead of need at most an eighth of what fits: more would wear away the chunks a
            // repeated scan keeps (a_repeated_scan_keeps_a_budget_worth)
            let ahead = self
                .pool
                .current_num_threads()
                .min((fits / 8).max(1) as usize);
            while victims.len() < short.max(ahead) {
                let Some((i, hot)) = self.choose(st, exclude, fits, &victims) else {
                    break;
                };
                if hot && victims.len() >= short {
                    break;
                }
                victims.push(i);
            }
            if victims.is_empty() {
                break;
            }
            self.evict(st, &victims);
        }
        st.resident + st.compressed + need <= hard
    }

    /// As on Linux: the resident chunk whose next fault is predicted farthest, never one of the
    /// most recently faulted (code works on them now); if only those are left, the oldest.
    fn choose(
        &self,
        st: &State,
        exclude: usize,
        fits: u64,
        taken: &[usize],
    ) -> Option<(usize, bool)> {
        let now = st.clock as f64;
        let hot = (fits / 4).clamp(2, HOT_CHUNKS) as f64;
        let (sum, count) = st
            .chunks
            .values()
            .filter(|c| c.period > 0.0)
            .fold((0.0, 0u64), |(s, n), c| (s + c.period, n + 1));
        let typical = if count > 0 { sum / count as f64 } else { 0.0 };
        let mut best: Option<(f64, usize)> = None;
        let mut oldest_hot: Option<(f64, usize)> = None;
        for (&i, c) in &st.chunks {
            if !matches!(c.data, Data::Resident)
                || c.incompressible
                || i == exclude
                || taken.contains(&i)
            {
                continue;
            }
            let age = now - c.last as f64;
            if age < hot {
                if oldest_hot.is_none_or(|(a, _)| age > a) {
                    oldest_hot = Some((age, i));
                }
                continue;
            }
            let period = if c.period > 0.0 { c.period } else { typical };
            let distance = if period > 0.0 {
                (c.last as f64 + period - now).max(0.0) + 1.0
            } else {
                age + 1.0
            };
            if best.is_none_or(|(d, _)| distance > d) {
                best = Some((distance, i));
            }
        }
        best.map(|(_, i)| (i, false))
            .or(oldest_hot.map(|(_, i)| (i, true)))
    }

    /// Evict `victims`: each is write-protected (readers go on, writers wait), all are
    /// compressed at once on the pager's pool, then each is given back or, when it does not
    /// compress enough, kept for good.
    fn evict(&self, st: &mut State, victims: &[usize]) {
        let chunk = self.config.chunk;
        let t0 = Instant::now();
        let protected: Vec<bool> = victims
            .iter()
            .map(|&i| {
                // from here on writers wait (the handler sees EVICTING); readers go on
                self.arena.states[i].store(EVICTING, Ordering::Release);
                protect(self.arena.base + i * chunk, chunk, libc::PROT_READ).is_ok()
            })
            .collect();
        let (elem, level, keep) = (
            self.config.elem,
            self.config.compress_level,
            chunk as f64 * (1.0 - self.config.min_saving),
        );
        let base = self.arena.base;
        let packed: Vec<Option<Mapped>> = self.pool.install(|| {
            use rayon::prelude::*;
            victims
                .par_iter()
                .zip(&protected)
                .map(|(&i, &ok)| {
                    if !ok {
                        return None;
                    }
                    // SAFETY: the chunk is resident and write-protected: its bytes cannot
                    // change while they are read, and reading them does not fault.
                    let src = unsafe {
                        std::slice::from_raw_parts((base + i * chunk) as *const u8, chunk)
                    };
                    let mut out = Vec::new();
                    let fine = codec::pack_chunk(src, elem, level, &mut out).is_ok()
                        && (out.len() as f64) < keep;
                    // kept in pages of its own, given back to the system when it is restored:
                    // `out` reserved a whole chunk, and glibc's moving mmap threshold kept such
                    // blocks on its heaps, outside what the budget sees (E068: +260 MB)
                    fine.then(|| Mapped::from_bytes(&out).ok()).flatten()
                })
                .collect()
        });
        st.stats.compress_seconds += t0.elapsed().as_secs_f64();
        for ((&i, ok), stored) in victims.iter().zip(protected).zip(packed) {
            let cstart = base + i * chunk;
            let blanked = stored.is_some() && blank(cstart, chunk).is_ok();
            let c = st.chunks.get_mut(&i).expect("chosen from the table");
            if !blanked {
                if ok {
                    let _ = protect(cstart, chunk, libc::PROT_READ | libc::PROT_WRITE);
                }
                self.arena.states[i].store(RESIDENT, Ordering::Release);
                c.incompressible = true;
                st.fixed += chunk as u64;
                st.stats.incompressible += 1;
                continue;
            }
            let stored = stored.expect("blanked only when saved");
            let (n, packed_len) = (stored.capacity() as u64, stored.len() as u64);
            c.data = Data::Compressed(stored);
            self.arena.states[i].store(OUT, Ordering::Release);
            st.resident -= chunk as u64;
            st.compressed += n;
            st.stats.evictions += 1;
            st.stats.compress_in += chunk as u64;
            st.stats.compress_out += packed_len;
        }
    }
}