Skip to main content

MemoryBudget

Struct MemoryBudget 

Source
pub struct MemoryBudget { /* private fields */ }
Expand description

Process-wide coordinator that periodically reapportions cache capacities so the application stays close to its target RSS.

Construct via MemoryBudget::new or MemoryBudget::from_env. Register every cache via MemoryBudget::register. Start the background tick via MemoryBudget::spawn.

Implementations§

Source§

impl MemoryBudget

Source

pub fn lease(&self, bytes: u64) -> LeaseGuard

Borrow bytes of slack from the budget. While the returned guard is alive, the effective target is reduced by bytes. Releases on drop.

Source

pub fn target_bytes(&self) -> u64

Soft RSS target in bytes, before any lease deductions.

Source

pub fn effective_target_bytes(&self) -> u64

Effective target after subtracting active leases. Saturates at zero if leases ever exceed the target (a misuse, but the budget refuses to underflow).

Source

pub fn executor_spill_run_buffer_bytes(&self) -> usize

Per-operator external-sort run buffer for executor spill paths.

Derived from the effective process target, so active leases shrink executor spill buffers before an operator starts rather than letting every operator size itself against the whole process budget.

§Panics

Panics if the bounded spill-buffer size cannot fit in usize.

Source

pub fn result_channel_bytes(&self) -> usize

Byte budget for one result stream’s producer→consumer channel (BME-22): the ceiling on encoded result bytes in flight between the encoder and its consumer/spool tee. A byte bound, not a frame count — frames vary in size by orders of magnitude, so counting them would bound the wrong quantity (BME-12’s lesson).

Derived from the effective process target so active leases shrink in-flight result buffering the same way they shrink spill buffers.

§Panics

Panics if the bounded channel size cannot fit in usize.

Source

pub fn tick_interval(&self) -> Duration

Tick cadence used by the spawned background loop.

Source

pub fn hard_ceiling_bytes(&self) -> Option<u64>

Optional hard ceiling. When set, the budget aborts the process if RSS exceeds this value — a backstop for runaway growth in production.

Source§

impl MemoryBudget

Source

pub fn new(config: BudgetConfig) -> Self

Construct a budget from an explicit config and the default RSS source and policy. Caller is responsible for spawning the background ticker via MemoryBudget::spawn.

Source

pub fn from_env() -> Self

Convenience constructor that resolves config from environment using the system RAM reported by the default SystemRss.

Source

pub fn start() -> Arc<Self>

Non-jemalloc fallback for builds that don’t link tikv-jemallocator (e.g. Windows-msvc, where the tikv-jemalloc-sys build needs an MSYS2/autoconf toolchain we don’t provide). Builds a budget with the default RSS source, spawns the ticker, and emits the same initialised breadcrumb so application startup logs stay consistent across platforms. The jemalloc-stats columns will read zero.

Source

pub fn with_rss(config: BudgetConfig, rss: Box<dyn RssSource>) -> Self

Construct with a custom RssSource (used by tests).

Source

pub fn with_jemalloc_stats(self, source: Box<dyn JemallocStatsSource>) -> Self

Install a JemallocStatsSource so the budget tick can split the non-cache bucket into “live allocations” vs “retained-by-jemalloc pages”. Must be called before any cache is registered.

§Panics

Panics if any cache is already registered, or if the budget’s internal state has been cloned (e.g. the ticker is already spawned).

Source

pub fn with_policy(self, policy: Box<dyn Policy>) -> Self

Replace the default policy. Mostly used in tests; production stays on ProportionalPolicy until benchmark data argues otherwise.

§Panics

Panics if any cache is already registered — the new policy must be installed before registration so that caches see a consistent reapportionment regime.

Source

pub fn register(&self, cache: &Arc<dyn Resizable>)

Register a cache. The cache’s current max_bytes is captured as its preferred ceiling — the policy will never grow a cache above this value.

§Panics

Panics if the internal registrations lock is poisoned.

Source

pub fn register_reporter(&self, reporter: &Arc<dyn NonCacheReporter>)

Register a read-only NonCacheReporter so the tick can attribute a named slice of the non_cache_live bucket to it. The budget holds a Weak, so dropping the reporter prunes it from the next tick. Unlike caches, reporters are never resized.

§Panics

Panics if the internal reporters lock is poisoned.

Source§

impl MemoryBudget

Source

pub fn tick_now(&self) -> BudgetSnapshot

Run one reapportionment tick synchronously. Returns the snapshot used for diagnostic emission. Exposed publicly so tests can drive the budget without spawning a tokio task.

Source

pub fn spawn(&self) -> JoinHandle<()>

Spawn a background tokio task that calls MemoryBudget::tick_now at the configured cadence. Returns a handle that aborts the task on drop.

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more