collect_host_context

Function collect_host_context 

Source
pub fn collect_host_context() -> HostContext
Expand description

Capture the host context. Static fields are collected once and cached; dynamic fields are re-read on every call so intra-run sysctl / hugepage / THP changes are reflected.

Every sub-read is fallible; individual failures leave the corresponding field None and the rest of the context proceeds. Even on a host where every /proc and /sys read fails, the three uname-derived fields (kernel_name, kernel_release, arch) still populate because they come from the uname() syscall — filesystem-independent. An otherwise-empty HostContext serializes to a near-empty JSON object and distinguishes “collection attempted, nothing known” from “collection not attempted” (represented at the enclosing Option<HostContext> layer on SidecarResult).

§Timing: post-run snapshot

Production call sites invoke this at sidecar-write time (see test_support::sidecar::write_sidecar and write_skip_sidecar), which runs AFTER the VM finishes. The returned snapshot therefore reflects post-run host state, not the pre-run environment the scheduler booted into.

Fields fall into two groups by how they are read:

Static subset (memoised in STATIC_HOST_INFO — or, for cpufreq_governor, the parallel CPUFREQ_GOVERNORS cache — identical across every call in the process, shift only under CPU / memory / NUMA hotplug or runtime governor change): the uname triple, CPU identity (cpu_model + cpu_vendor), total_memory_kib, hugepages_size_kib, online_cpus, numa_nodes, and cpufreq_governor.

Dynamic subset (re-read on every call): kernel_cmdline, hugepages_total, hugepages_free, thp_enabled, thp_defrag, sched_tunables. kernel_cmdline is mechanically dynamic (re-read each call) but effectively static for the process (changes only across reboot). The others can genuinely drift between pre-run and post-run:

  • sched_tunables: a test that writes to /proc/sys/kernel/sched_* and does not restore the previous value will be observed with the test-mutated value.
  • hugepages_total / hugepages_free: a test that reserves or releases hugepages shifts the counts.
  • thp_enabled / thp_defrag: a test that flips THP policy is captured with the flipped policy.

Dashboards and regression tooling that need the environment the scheduler actually saw (not the post-run state) should treat the three drift-prone fields as “post-run snapshot” and either (a) disable them in the comparison, or (b) capture a pre-run snapshot via collect_host_context_pre_run and travel the pair via HostContextSnapshots.