collect

Function collect 

Source
pub fn collect() -> HostHeapState
Expand description

Capture the running process’s jemalloc heap state.

Advances the jemalloc epoch exactly once so cached stats.* values refresh (this is a jemalloc-internal operation — libjemalloc flushes per-thread stat caches into the shared counters under its mallctl lock, and the operation is thread-safe per jemalloc’s mallctl contract), then reads five mallctl values. Any individual read error lands that field as None; an epoch::advance() error short-circuits the whole function to HostHeapState::default because without a refreshed epoch the stats reads would return values from an arbitrary prior snapshot.

Since libjemalloc is linked unconditionally via tikv-jemalloc-sys (see module doc), epoch::advance() and the subsequent mallctl reads always succeed on a well-formed build. The is_err() branch below is a defensive guard against future jemalloc versions changing the error surface, not an expected fallback.

Direct callers may use this even when jemalloc is not the global allocator; the reads then describe only jemalloc’s own mallctl-triggered initialization. HostContext::heap_state checks the explicit process allocator marker before calling this function, so sidecars only publish these fields when they represent real application usage.

§Cost

One mallctl("epoch", ...) call plus five mallctl("stats.*"/"arenas.narenas", ...) reads. Each is a memcpy from a cached value after a short tree walk inside jemalloc — microseconds total. Safe to call on every sidecar write.