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.

When jemalloc is linked but is not #[global_allocator], the reads succeed and return small-or-zero values — HostContext::heap_state detects that shape and stores None so the sidecar does not carry an empty row. When jemalloc IS #[global_allocator] (every binary target in this workspace), every field reflects real runner memory 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.