pub struct SysMetrics { /* private fields */ }Expand description
Per-process RSS + CPU sampler bound to the current process.
Why: holding the System between calls is required for CPU measurement —
sysinfo derives CPU% from the delta in consumed CPU time between
two refreshes, so the same instance must be reused.
What: stores the long-lived System and our own Pid. Not Clone — it
carries mutable sampling state; share it behind a Mutex if multiple
handlers need it.
Test: sample_does_not_panic, rss_is_plausible.
Implementations§
Source§impl SysMetrics
impl SysMetrics
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct a sampler for the current process.
Why: the daemon builds one of these at startup and samples it on each
/health request.
What: resolves std::process::id() into a sysinfo::Pid and creates a
System configured to refresh only process memory + CPU (not the
whole machine), then performs one priming refresh so the next
sample call has a baseline for the CPU delta.
Test: sample_does_not_panic.
Sourcepub fn sample(&mut self) -> (u64, f32)
pub fn sample(&mut self) -> (u64, f32)
Refresh and return (rss_mb, cpu_pct) for the current process.
Why: the /health handler calls this once per request. Polling more
often than ~once per 500 ms yields noisy CPU readings because the
delta window shrinks; /health is typically polled every 2 s so
this is not a concern in practice.
What: refreshes this process’s memory + CPU stats. Returns RSS in
whole megabytes (bytes / 1_048_576) and CPU as a percentage
where 100.0 means one fully-saturated core (sysinfo’s
convention — a process on 4 cores can exceed 100). If the process
cannot be resolved (extremely rare; only in containers with
/proc hidden), returns (0, 0.0).
Test: sample_does_not_panic, rss_is_plausible.