Expand description
Memory pressure monitoring with threshold callbacks and eviction hooks.
A long-running allocator benefits from reacting before the device runs out of memory: trimming idle pool pages, evicting cached buffers, or throttling new work as free VRAM drops. This module models that control loop on the host side.
MemoryPressureMonitor holds configurable warning / critical thresholds
(expressed as a used fraction of total device memory) and, on each sample,
classifies the current state into a PressureLevel. Transitions into a
more severe level fire user callbacks; entering the critical level fires an
eviction hook that returns the number of bytes reclaimed so the loop can
account for the relief.
Samples are supplied as crate::memory_info::MemoryInfo values. In
production the MemoryPressureMonitor::poll method fetches a fresh sample
via crate::memory_info::memory_info (requires a GPU); in tests the
MemoryPressureMonitor::observe method feeds synthetic samples so the
whole state machine is deterministic and hardware-free.
§Example
let mut monitor = MemoryPressureMonitor::new(0.80, 0.95).expect("thresholds");
// 70% used -> nominal.
let lvl = monitor.observe(MemoryInfo { free: 30, total: 100 });
assert_eq!(lvl, PressureLevel::Nominal);
// 96% used -> critical.
let lvl = monitor.observe(MemoryInfo { free: 4, total: 100 });
assert_eq!(lvl, PressureLevel::Critical);Structs§
- Memory
Pressure Monitor - Monitors device memory pressure and drives reactive trim / eviction.
- Pressure
Sample - The outcome of classifying one memory sample.
Enums§
- Pressure
Level - The classified memory-pressure state.
Functions§
- validate_
thresholds - Validates a pair of
(warning, critical)used-fraction thresholds without constructing a monitor.