pub struct MemoryDriftTracker { /* private fields */ }Expand description
Compares allocated bytes against the total reserved by every pool that
reports to it (see PeakRecordingPool::with_drift_tracker).
A single tracker can be shared by many pools, e.g. one pool per
SessionContext in a process running several at once. The reserved total
is then summed across all of them, which is what has to be compared with a
process-wide allocated byte count.
Drift is allocated - reserved. A line is logged at info level each time
drift rises by at least the log threshold, naming the pool and consumer
whose reservation change triggered the check.
§Example
// A real caller would read a counting allocator or allocator stats here.
let tracker = Arc::new(MemoryDriftTracker::new(Arc::new(|| 10_000)));
let pool: Arc<dyn MemoryPool> = Arc::new(
PeakRecordingPool::new(Arc::new(UnboundedMemoryPool::default()))
.with_drift_tracker(Arc::clone(&tracker), "example"),
);
let reservation = MemoryConsumer::new("op").register(&pool);
reservation.grow(4_000);
assert_eq!(tracker.reserved(), 4_000);
assert_eq!(tracker.peak_drift().unwrap().drift, 6_000);Implementations§
Source§impl MemoryDriftTracker
impl MemoryDriftTracker
Sourcepub fn new(allocated: AllocatedBytesFn) -> Self
pub fn new(allocated: AllocatedBytesFn) -> Self
Create a tracker that reads allocated bytes from allocated, logging
every DEFAULT_DRIFT_LOG_THRESHOLD of drift.
Sourcepub fn with_log_threshold(self, log_threshold: usize) -> Self
pub fn with_log_threshold(self, log_threshold: usize) -> Self
Log a line each time drift rises by log_threshold bytes.
Sourcepub fn reserved(&self) -> usize
pub fn reserved(&self) -> usize
Bytes currently reserved across all pools reporting to this tracker.
Sourcepub fn peak_drift(&self) -> Option<DriftSample>
pub fn peak_drift(&self) -> Option<DriftSample>
The largest drift seen so far, if any reservation has been made.
Sourcepub fn sample(&self, source: &str)
pub fn sample(&self, source: &str)
Compare the current allocated bytes with the reserved total now, without a reservation change.
Drift is otherwise only sampled when a reservation changes, so memory
allocated by code that reserves little can go unseen until some other
reservation changes. Calling this periodically, e.g. from a timer,
closes that gap. source is recorded as the pool label.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MemoryDriftTracker
impl !RefUnwindSafe for MemoryDriftTracker
impl !UnwindSafe for MemoryDriftTracker
impl Forget for MemoryDriftTracker
impl Move for MemoryDriftTracker
impl Send for MemoryDriftTracker
impl Sync for MemoryDriftTracker
impl Unpin for MemoryDriftTracker
impl UnsafeUnpin for MemoryDriftTracker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more