Skip to main content

MemoryDriftTracker

Struct MemoryDriftTracker 

Source
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

Source

pub fn new(allocated: AllocatedBytesFn) -> Self

Create a tracker that reads allocated bytes from allocated, logging every DEFAULT_DRIFT_LOG_THRESHOLD of drift.

Source

pub fn with_log_threshold(self, log_threshold: usize) -> Self

Log a line each time drift rises by log_threshold bytes.

Source

pub fn reserved(&self) -> usize

Bytes currently reserved across all pools reporting to this tracker.

Source

pub fn peak_drift(&self) -> Option<DriftSample>

The largest drift seen so far, if any reservation has been made.

Source

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§

Source§

impl Debug for MemoryDriftTracker

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V