Skip to main content

sort_governor/service/
pressure.rs

1//! The memory-pressure seam the actor reads to build a planner snapshot.
2//!
3//! A process-wide memory governor (anything that samples RSS against a
4//! target) implements [`MemoryPressure`]; keeping it behind a trait keeps
5//! this crate free of any particular governor and lets tests drive the
6//! planner with fixed readings.
7
8/// A live source of process memory pressure.
9pub trait MemoryPressure: Send + Sync + 'static {
10    /// The memory governor's effective RSS target (target minus active
11    /// leases).
12    fn effective_target_bytes(&self) -> u64;
13    /// Non-cache working-set bytes already resident.
14    fn resident_bytes(&self) -> u64;
15}
16
17/// A fixed pressure reading — for tests and for processes that run without a
18/// memory governor.
19#[derive(Debug, Clone, Copy)]
20pub struct StaticPressure {
21    effective_target_bytes: u64,
22    resident_bytes: u64,
23}
24
25impl StaticPressure {
26    /// A pressure source that always reports the given target and residency.
27    #[must_use]
28    pub fn new(effective_target_bytes: u64, resident_bytes: u64) -> Self {
29        Self {
30            effective_target_bytes,
31            resident_bytes,
32        }
33    }
34}
35
36impl MemoryPressure for StaticPressure {
37    fn effective_target_bytes(&self) -> u64 {
38        self.effective_target_bytes
39    }
40
41    fn resident_bytes(&self) -> u64 {
42        self.resident_bytes
43    }
44}