From dbaf524ddf932853e6caf99f8b5564b86956b68b Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Mon, 9 Nov 2026 19:40:36 +0000 Subject: [PATCH 1/7] test(rocm-core): take the host-root lock in the two repo-scan tests that read through it live_rocm_repo_paths_in_dir_skips_a_real_bak_file_on_disk and merge_live_repo_paths_scans_every_family_dir_including_zypp scan temp directories through host_path, which a test holding a simulated host root re-roots, and neither takes HOST_ROOT_TEST_LOCK as every test that sets the root does. They pass while the tests that set the root are short, and fail in most full runs with the e2e-test-hooks feature once one of those tests runs a whole examination under the root, which holds it for much longer. They take the lock now, ahead of the test that does. Signed-off-by: Jussi Elo --- crates/rocm-core/src/examine/rocm_install_probe.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/rocm-core/src/examine/rocm_install_probe.rs b/crates/rocm-core/src/examine/rocm_install_probe.rs index 03bdda9535..6126d1d3bc 100644 --- a/crates/rocm-core/src/examine/rocm_install_probe.rs +++ b/crates/rocm-core/src/examine/rocm_install_probe.rs @@ -192,6 +192,12 @@ mod tests { // fix-7/fix-12 quarantine advice produces) through the directory scan // `probe_rocm_install` runs, closing the gap where only the live file // being listed first would still pass if the `.bak` skip were deleted. + // + // The scan reads through `host_path`, so a test that has a simulated + // host root set would re-root this temp directory away. + let _guard = crate::hardware_root::HOST_ROOT_TEST_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); let dir = tempfile::tempdir().expect("create temp dir"); std::fs::write(dir.path().join("amdgpu.list"), "deb ...\n").expect("write live repo"); std::fs::write(dir.path().join("amdgpu.list.bak"), "deb ...\n") @@ -222,6 +228,12 @@ mod tests { // helpers in isolation) across temp directories standing in for all // three package families, including the zypp one a mutation run // showed was silently unscanned by every pre-existing test. + // + // The scan reads through `host_path`, so a test that has a simulated + // host root set would re-root these temp directories away. + let _guard = crate::hardware_root::HOST_ROOT_TEST_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); let apt_dir = tempfile::tempdir().expect("create apt temp dir"); let yum_dir = tempfile::tempdir().expect("create yum temp dir"); let zypp_dir = tempfile::tempdir().expect("create zypp temp dir"); From 1da6845f784ede277cee964f5b087c5d7b68345a Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Mon, 9 Nov 2026 19:01:59 +0000 Subject: [PATCH 2/7] test(rocm-core): pin both CPU-model reads' answers before they are merged The summary stack (detect_cpu_model and the Windows inventory's CPU row) and the examination stack (probe_cpu_linux and probe_cpu_windows) both read the CPU, and are about to become one reader. Pin what each returns for the same input first, so the change that merges them shows exactly which answers it moves. They agree on an ordinary model name, on a model name with no vendor_id, on a blank model name line followed by a real one, on a cpuinfo with no CPU in it, and on Windows on a name the query returns. They differ on four Linux inputs (repeated spaces in the model name, which only the summary collapses; a Hardware line with no model name, which only the summary reads; Hardware before model name; an empty vendor_id value) and on Windows on whitespace, on the first line of several, and on the time allowed: 8 s for the examination, 1.5 s for both the summary's system query and its own fallback query. The summary asks its own fallback query only when there is no inventory at all, even when the inventory has no CPU row. On WSL the summary reads the CPU and the examination returns before it does, so cpu_model is empty and cpu_vendor is unknown; that is pinned as an existing difference from a simulated host root. The Windows logic runs on any host through injected queries: probe_cpu_windows_with, detect_cpu_model_windows and windows_system_inventory_with take the query as a closure, and windows_cpu_model makes the summary's inventory-or-fallback choice. Nothing else changes; the live queries are the same commands as before. Signed-off-by: Jussi Elo --- crates/rocm-core/src/examine.rs | 2 + crates/rocm-core/src/examine/host_env.rs | 117 ++++- crates/rocm-core/src/host_gpu.rs | 22 +- crates/rocm-core/src/host_gpu/os_facts.rs | 451 +++++++++++++++++- .../src/host_gpu/windows_inventory.rs | 49 +- 5 files changed, 589 insertions(+), 52 deletions(-) diff --git a/crates/rocm-core/src/examine.rs b/crates/rocm-core/src/examine.rs index 5ebd630c16..5f8b9ba25f 100644 --- a/crates/rocm-core/src/examine.rs +++ b/crates/rocm-core/src/examine.rs @@ -37,6 +37,8 @@ use host_env::{ probe_container, probe_cpu_linux, probe_cpu_windows, probe_dmesg_amdgpu, probe_env, probe_os, probe_shared_memory, }; +#[cfg(test)] +pub(crate) use host_env::{probe_cpu_linux_from, probe_cpu_windows_with}; pub(crate) use library_copies::comgr_matches_runtime; use library_copies::probe_comgr; use linux_driver::{probe_devices, probe_modules, probe_secure_boot, probe_user}; diff --git a/crates/rocm-core/src/examine/host_env.rs b/crates/rocm-core/src/examine/host_env.rs index da192981da..9ff73841a3 100644 --- a/crates/rocm-core/src/examine/host_env.rs +++ b/crates/rocm-core/src/examine/host_env.rs @@ -115,7 +115,12 @@ pub(super) fn parse_iommu_param(cmdline: &str) -> Option { } pub(super) fn probe_cpu_linux(e: &mut Examination) { - let txt = read_host_text("/proc/cpuinfo"); + probe_cpu_linux_from(e, &read_host_text("/proc/cpuinfo")); +} + +/// [`probe_cpu_linux`] over cpuinfo text already read, so a test can drive it +/// without a host root. +pub(crate) fn probe_cpu_linux_from(e: &mut Examination, txt: &str) { for line in txt.lines() { if (e.cpu_vendor == "unknown") && line.starts_with("vendor_id") @@ -143,15 +148,27 @@ pub(super) fn probe_cpu_linux(e: &mut Examination) { } pub(super) fn probe_cpu_windows(e: &mut Examination) { - let (rc, out, _) = run( - "powershell", - &[ - "-NoProfile", - "-Command", - "(Get-CimInstance Win32_Processor | Select-Object -First 1).Name", - ], - MEDIUM, - ); + probe_cpu_windows_with(e, |timeout| { + run( + "powershell", + &[ + "-NoProfile", + "-Command", + "(Get-CimInstance Win32_Processor | Select-Object -First 1).Name", + ], + timeout, + ) + }); +} + +/// [`probe_cpu_windows`] with the query injected, so a test on any host can +/// drive it. `query` is handed the timeout the examination allows it and +/// answers as [`run`] does: `(rc, stdout, stderr)`. +pub(crate) fn probe_cpu_windows_with( + e: &mut Examination, + query: impl FnOnce(std::time::Duration) -> (i32, String, String), +) { + let (rc, out, _) = query(MEDIUM); if rc == 0 && !out.trim().is_empty() { e.cpu_model = out .trim() @@ -618,4 +635,84 @@ mod tests { ); assert_eq!(name.as_deref(), Some("Fallback 1.2")); } + + /// Plant `files` (path under the root, body) in a fresh directory and run + /// `read` with it as the simulated host root. + #[cfg(all(feature = "e2e-test-hooks", target_os = "linux"))] + fn with_planted_host_root(files: &[(&str, &str)], read: impl FnOnce() -> T) -> T { + use crate::hardware_root::TEST_HOST_ROOT_ENV; + use crate::test_env::RestoredEnvVar; + + let root = std::env::temp_dir().join(format!( + "rocm-core-cpu-root-{}", + crate::test_support::unique_suffix() + )); + for (relative, body) in files { + let path = root.join(relative.trim_start_matches('/')); + std::fs::create_dir_all(path.parent().expect("planted path has a parent")) + .expect("create planted directory"); + std::fs::write(path, body).expect("write planted file"); + } + let result = { + let _guard = crate::hardware_root::HOST_ROOT_TEST_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let _root = RestoredEnvVar::set(TEST_HOST_ROOT_ENV, &root); + read() + }; + std::fs::remove_dir_all(&root).ok(); + result + } + + /// CPU-model pair of docs/host-probing.md, native Linux. Both reads take + /// `/proc/cpuinfo` from the simulated host root, so a planted CPU no runner + /// has is what each reports. They differ on the whitespace in it, which the + /// summary collapses and the examination keeps. + #[cfg(all(feature = "e2e-test-hooks", target_os = "linux"))] + #[test] + fn cpu_reads_follow_the_simulated_host_root() { + let (summary, examined) = with_planted_host_root( + &[( + "proc/cpuinfo", + "processor\t: 0\nvendor_id\t: AuthenticAMD\nmodel name\t: Planted CPU 9000 Processor\n", + )], + || { + let mut examined = Examination::default(); + probe_cpu_linux(&mut examined); + (crate::host_gpu::detect_cpu_model(), examined) + }, + ); + assert_eq!(summary.as_deref(), Some("Planted CPU 9000 Processor")); + assert_eq!(examined.cpu_model, "Planted CPU 9000 Processor"); + assert_eq!(examined.cpu_vendor, "amd"); + } + + /// CPU-model pair, WSL: the summary reads the CPU and the examination does + /// not. `probe_with_interpreter` returns from its WSL branch before + /// `probe_cpu_linux`, so on every WSL host `cpu_model` is empty and + /// `cpu_vendor` is `unknown` while the summary shows the CPU. Pinned as an + /// existing difference of the CPU-model pair, which leaves it as it is. + #[cfg(all(feature = "e2e-test-hooks", target_os = "linux"))] + #[test] + fn on_wsl_the_summary_reads_the_cpu_and_the_examination_does_not() { + let (summary, examined) = with_planted_host_root( + &[ + ("dev/dxg", ""), + ( + "proc/cpuinfo", + "processor\t: 0\nvendor_id\t: AuthenticAMD\nmodel name\t: Planted WSL CPU\n", + ), + ], + || { + ( + crate::host_gpu::detect_cpu_model(), + Examination::probe(crate::examine::FrameworkProbe::Skip), + ) + }, + ); + assert!(examined.is_wsl, "the planted /dev/dxg reads as WSL"); + assert_eq!(summary.as_deref(), Some("Planted WSL CPU")); + assert_eq!(examined.cpu_model, ""); + assert_eq!(examined.cpu_vendor, "unknown"); + } } diff --git a/crates/rocm-core/src/host_gpu.rs b/crates/rocm-core/src/host_gpu.rs index c1854ac0d8..f9caf206e9 100644 --- a/crates/rocm-core/src/host_gpu.rs +++ b/crates/rocm-core/src/host_gpu.rs @@ -37,9 +37,10 @@ use crate::{ host_path, runtime_is_linux, runtime_is_windows, runtime_os_name, }; use anyhow::Result; +pub(crate) use os_facts::detect_cpu_model; pub(crate) use os_facts::detect_distro_name; +use os_facts::detect_kernel_version; pub use os_facts::detect_system_ram_gib; -use os_facts::{detect_cpu_model, detect_kernel_version}; use serde::{Deserialize, Serialize}; use std::fs; use std::path::{Path, PathBuf}; @@ -269,15 +270,26 @@ impl ExamineSummary { fn detect_cpu_model_with_windows_inventory( windows_inventory: Option<&WindowsExamineInventory>, ) -> Option { - if runtime_is_windows() - && let Some(inventory) = windows_inventory - { - return inventory.cpu_model.clone(); + if runtime_is_windows() { + return windows_cpu_model(windows_inventory, detect_cpu_model); } detect_cpu_model() } +/// The summary's CPU on Windows: the inventory's CPU row whenever there is an +/// inventory, even one with no such row, and `fallback` (the summary's own CPU +/// query) only when there is no inventory at all. +fn windows_cpu_model( + windows_inventory: Option<&WindowsExamineInventory>, + fallback: impl FnOnce() -> Option, +) -> Option { + match windows_inventory { + Some(inventory) => inventory.cpu_model.clone(), + None => fallback(), + } +} + fn detect_system_ram_gib_with_windows_inventory( windows_inventory: Option<&WindowsExamineInventory>, ) -> Option { diff --git a/crates/rocm-core/src/host_gpu/os_facts.rs b/crates/rocm-core/src/host_gpu/os_facts.rs index 0cfab952ff..649c6d8fcf 100644 --- a/crates/rocm-core/src/host_gpu/os_facts.rs +++ b/crates/rocm-core/src/host_gpu/os_facts.rs @@ -64,35 +64,23 @@ fn distro_name_from_os_release(read: &hardware_root::OsRelease) -> String { } } -pub(super) fn detect_cpu_model() -> Option { +pub(crate) fn detect_cpu_model() -> Option { if runtime_is_windows() { - let script = - "Get-CimInstance Win32_Processor | Select-Object -First 1 -ExpandProperty Name"; - return capture_optional_command_with_timeout( - "powershell", - &["-NoProfile", "-Command", script], - OPTIONAL_COMMAND_TIMEOUT, - ) - .map(|value| normalize_cpu_model(&value)) - .filter(|value| !value.is_empty()); + return detect_cpu_model_windows(|timeout| { + let script = + "Get-CimInstance Win32_Processor | Select-Object -First 1 -ExpandProperty Name"; + capture_optional_command_with_timeout( + "powershell", + &["-NoProfile", "-Command", script], + timeout, + ) + }); } if runtime_is_linux() && let Some(model) = fs::read_to_string(host_path("/proc/cpuinfo")) .ok() - .and_then(|text| { - text.lines().find_map(|line| { - let value = line - .strip_prefix("model name") - .and_then(|rest| rest.split_once(':').map(|(_, value)| value)) - .or_else(|| { - line.strip_prefix("Hardware") - .and_then(|rest| rest.split_once(':').map(|(_, value)| value)) - })?; - let value = normalize_cpu_model(value); - (!value.is_empty()).then_some(value) - }) - }) + .and_then(|text| cpu_model_from_cpuinfo(&text)) { return Some(model); } @@ -100,6 +88,32 @@ pub(super) fn detect_cpu_model() -> Option { None } +/// The summary's own Windows CPU query, with the query injected so a test on any +/// host can drive it. `query` is handed the timeout the summary allows it. +pub(super) fn detect_cpu_model_windows( + query: impl FnOnce(std::time::Duration) -> Option, +) -> Option { + query(OPTIONAL_COMMAND_TIMEOUT) + .map(|value| normalize_cpu_model(&value)) + .filter(|value| !value.is_empty()) +} + +/// The first `model name` or `Hardware` line of `/proc/cpuinfo` text that has a +/// value, whitespace collapsed. +pub(super) fn cpu_model_from_cpuinfo(text: &str) -> Option { + text.lines().find_map(|line| { + let value = line + .strip_prefix("model name") + .and_then(|rest| rest.split_once(':').map(|(_, value)| value)) + .or_else(|| { + line.strip_prefix("Hardware") + .and_then(|rest| rest.split_once(':').map(|(_, value)| value)) + })?; + let value = normalize_cpu_model(value); + (!value.is_empty()).then_some(value) + }) +} + pub fn detect_system_ram_gib() -> Option { if runtime_is_windows() { let script = "(Get-CimInstance -ClassName Win32_ComputerSystem -Property TotalPhysicalMemory).TotalPhysicalMemory"; @@ -221,4 +235,395 @@ mod tests { "Linux (/usr/lib/os-release line 2 is not a plain assignment: \"unset ID\")" ); } + + // ── CPU model: the summary's read and the examination's read ────────── + // + // Characterisation tests for the CPU-model pair of docs/host-probing.md. + // `rocm examine --json` reads the CPU twice: the summary (`summary.cpu`, the + // `cpu:` line of the text) through `detect_cpu_model` and the Windows + // inventory's CPU row, and the examination (`cpu_model` and `cpu_vendor`) + // through `probe_cpu_linux` and `probe_cpu_windows`. Each case below gives + // both reads the same input and pins what each returns, so the change that + // merges them moves exactly the assertions it means to. Where the two + // agree, one value is asserted for both. Where they differ, each read's own + // answer is asserted, under a comment that says so. + + use crate::examine::{Examination, probe_cpu_linux_from, probe_cpu_windows_with}; + use crate::host_gpu::windows_inventory::{ + InventoryScope, WindowsExamineInventory, WindowsInventorySources, + assemble_windows_inventory, windows_system_inventory_with, + }; + use std::time::Duration; + + /// One `/proc/cpuinfo` and what each read makes of it. + struct CpuinfoCase { + name: &'static str, + text: &'static str, + /// `detect_cpu_model`'s answer. + summary: Option<&'static str>, + /// `Examination::cpu_model` and `Examination::cpu_vendor`. + examination: (&'static str, &'static str), + } + + const INTEL_CPUINFO: &str = "processor\t: 0\nvendor_id\t: GenuineIntel\ncpu family\t: 6\nmodel name\t: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz\nprocessor\t: 1\nvendor_id\t: GenuineIntel\nmodel name\t: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz\n"; + + #[test] + fn linux_cpuinfo_the_two_reads_agree_on_an_ordinary_cpu() { + let cases = [ + CpuinfoCase { + name: "Intel, model name and vendor_id", + text: INTEL_CPUINFO, + summary: Some("Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz"), + examination: ("Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz", "intel"), + }, + CpuinfoCase { + name: "AMD, model name and vendor_id", + text: "vendor_id\t: AuthenticAMD\nmodel name\t: AMD Ryzen 9 9950X 16-Core Processor\n", + summary: Some("AMD Ryzen 9 9950X 16-Core Processor"), + examination: ("AMD Ryzen 9 9950X 16-Core Processor", "amd"), + }, + CpuinfoCase { + // No `vendor_id` (Arm kernels print none): the model is read, the + // vendor stays "unknown" in the examination. + name: "model name and no vendor_id", + text: "processor\t: 0\nmodel name\t: ARMv8 Processor rev 1 (v8l)\n", + summary: Some("ARMv8 Processor rev 1 (v8l)"), + examination: ("ARMv8 Processor rev 1 (v8l)", "unknown"), + }, + CpuinfoCase { + name: "a blank model name line, then a real one", + text: "vendor_id\t: GenuineIntel\nmodel name\t: \nmodel name\t: Intel(R) Xeon(R) Gold 6248\n", + summary: Some("Intel(R) Xeon(R) Gold 6248"), + examination: ("Intel(R) Xeon(R) Gold 6248", "intel"), + }, + CpuinfoCase { + // `Hardware` after the per-core blocks is what an Arm board + // prints, and it follows `model name`, so both read the latter. + name: "model name first, Hardware last", + text: "processor\t: 0\nmodel name\t: ARMv7 Processor rev 4 (v7l)\nHardware\t: BCM2835\n", + summary: Some("ARMv7 Processor rev 4 (v7l)"), + examination: ("ARMv7 Processor rev 4 (v7l)", "unknown"), + }, + CpuinfoCase { + name: "nothing to read", + text: "processor\t: 0\nCPU implementer\t: 0x41\n", + summary: None, + examination: ("", "unknown"), + }, + CpuinfoCase { + name: "an empty file", + text: "", + summary: None, + examination: ("", "unknown"), + }, + CpuinfoCase { + // The examination lower-cases a vendor it does not know, the + // summary records no vendor at all. + name: "a vendor_id that is neither AMD nor Intel", + text: "vendor_id\t: HygonGenuine\nmodel name\t: Hygon C86 7185 32-core Processor\n", + summary: Some("Hygon C86 7185 32-core Processor"), + examination: ("Hygon C86 7185 32-core Processor", "hygongenuine"), + }, + ]; + for case in cases { + assert_cpuinfo_case(&case); + } + } + + /// The inputs on which the two reads give different answers today; each + /// read's own answer is pinned. + #[test] + fn linux_cpuinfo_the_two_reads_differ_on_four_inputs() { + let cases = [ + CpuinfoCase { + // The summary collapses whitespace runs, the examination trims + // only: one model string, two spellings in one `--json`. + name: "repeated spaces inside the model name", + text: "vendor_id\t: GenuineIntel\nmodel name\t: Intel(R) Pentium(R) CPU N3700 @ 1.60GHz \n", + summary: Some("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz"), + examination: ("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz", "intel"), + }, + CpuinfoCase { + // The summary falls back to `Hardware`; the examination reads + // `model name` only, so it reports no CPU at all. + name: "Hardware and no model name", + text: "processor\t: 0\nBogoMIPS\t: 38.40\nHardware\t: BCM2835\nRevision\t: a02082\n", + summary: Some("BCM2835"), + examination: ("", "unknown"), + }, + CpuinfoCase { + // The summary takes whichever of the two comes first, the + // examination only ever the `model name`. + name: "Hardware before model name", + text: "Hardware\t: BCM2835\nmodel name\t: ARMv7 Processor rev 4 (v7l)\n", + summary: Some("BCM2835"), + examination: ("ARMv7 Processor rev 4 (v7l)", "unknown"), + }, + CpuinfoCase { + // An empty `vendor_id` value is taken as the vendor by the + // examination, which then stops looking for another. + name: "an empty vendor_id value", + text: "vendor_id\t:\nmodel name\t: Some CPU\nvendor_id\t: GenuineIntel\n", + summary: Some("Some CPU"), + examination: ("Some CPU", ""), + }, + ]; + for case in cases { + assert_cpuinfo_case(&case); + } + } + + fn assert_cpuinfo_case(case: &CpuinfoCase) { + assert_eq!( + cpu_model_from_cpuinfo(case.text).as_deref(), + case.summary, + "summary, {}", + case.name + ); + let mut examined = Examination::default(); + probe_cpu_linux_from(&mut examined, case.text); + assert_eq!( + (examined.cpu_model.as_str(), examined.cpu_vendor.as_str()), + case.examination, + "examination, {}", + case.name + ); + assert!( + examined.probe_failures.is_empty(), + "a cpuinfo with no CPU in it is not a probe failure, {}: {:?}", + case.name, + examined.probe_failures + ); + } + + /// What the examination makes of one answer from its Windows query. + fn examination_on_windows(rc: i32, stdout: &str) -> (Examination, Duration) { + let mut examined = Examination::default(); + let mut asked = None; + probe_cpu_windows_with(&mut examined, |timeout| { + asked = Some(timeout); + (rc, stdout.to_owned(), String::new()) + }); + (examined, asked.expect("the examination ran its query")) + } + + /// The summary's system inventory with only `system` wired, so a test sees + /// the query the CPU row comes from. + struct SystemQuery<'a> { + output: Option<&'a str>, + asked: Option<(String, Duration)>, + } + + impl WindowsInventorySources for SystemQuery<'_> { + fn pnputil(&mut self) -> Option { + None + } + fn video_controller(&mut self) -> Option { + None + } + fn pnp_entity(&mut self) -> Option { + None + } + fn system(&mut self) -> Option { + windows_system_inventory_with(|script, timeout| { + self.asked = Some((script.to_owned(), timeout)); + self.output.map(str::to_owned) + }) + } + } + + /// What the summary reports for the CPU on Windows when the system query + /// prints `system_output` (`None`: it could not run) and its own fallback + /// query prints `fallback_output`. + struct SummaryOnWindows { + cpu: Option, + system_query: Option<(String, Duration)>, + fallback_timeout: Option, + } + + fn summary_on_windows( + system_output: Option<&str>, + fallback_output: Option<&str>, + ) -> SummaryOnWindows { + let mut sources = SystemQuery { + output: system_output, + asked: None, + }; + let inventory = assemble_windows_inventory(&mut sources, InventoryScope::SUMMARY); + let mut fallback_timeout = None; + let cpu = crate::host_gpu::windows_cpu_model(inventory.as_ref(), || { + detect_cpu_model_windows(|timeout| { + fallback_timeout = Some(timeout); + fallback_output.map(str::to_owned) + }) + }); + SummaryOnWindows { + cpu, + system_query: sources.asked, + fallback_timeout, + } + } + + const WINDOWS_CPU_NAME: &str = "AMD Ryzen 9 9950X 16-Core Processor"; + + #[test] + fn windows_the_two_reads_agree_on_a_name_the_query_returns() { + let summary = summary_on_windows( + Some(&format!("CPU\t{WINDOWS_CPU_NAME}\nRAM\t68719476736\n")), + None, + ); + assert_eq!(summary.cpu.as_deref(), Some(WINDOWS_CPU_NAME)); + assert_eq!( + summary.fallback_timeout, None, + "an inventory was read, so the summary's own query does not run" + ); + let (examined, _) = examination_on_windows(0, &format!("{WINDOWS_CPU_NAME}\r\n")); + assert_eq!(examined.cpu_model, WINDOWS_CPU_NAME); + assert_eq!(examined.cpu_vendor, "amd"); + assert!(examined.probe_failures.is_empty()); + } + + /// The summary collapses whitespace runs, the examination trims and keeps + /// the first line only. Windows pads the name of some CPUs. + #[test] + fn windows_the_two_reads_differ_on_whitespace_inside_the_name() { + let summary = summary_on_windows( + Some("CPU\t Intel(R) Pentium(R) CPU N3700 @ 1.60GHz \n"), + None, + ); + assert_eq!( + summary.cpu.as_deref(), + Some("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz") + ); + let (examined, _) = + examination_on_windows(0, " Intel(R) Pentium(R) CPU N3700 @ 1.60GHz \r\n"); + assert_eq!( + examined.cpu_model, + "Intel(R) Pentium(R) CPU N3700 @ 1.60GHz" + ); + assert_eq!(examined.cpu_vendor, "intel"); + // Only the examination reads a first line out of several. + let (several, _) = examination_on_windows(0, "AMD EPYC 7763\r\nAMD EPYC 7763\r\n"); + assert_eq!(several.cpu_model, "AMD EPYC 7763"); + } + + #[test] + fn windows_the_examination_derives_the_vendor_from_the_model_string() { + for (name, vendor) in [ + ("AMD Ryzen 7 5800X 8-Core Processor", "amd"), + ("amd epyc 7763", "amd"), + ("Intel(R) Core(TM) i9-13900K", "intel"), + ("Genuine Intel(R) CPU 0000", "intel"), + ("Qualcomm(R) Snapdragon(R) X Elite", "unknown"), + ] { + let (examined, _) = examination_on_windows(0, name); + assert_eq!(examined.cpu_vendor, vendor, "{name}"); + assert_eq!(examined.cpu_model, name); + } + } + + /// The system query printed no CPU row (here only the RAM row), or nothing. + /// The summary has an inventory, so it reports no CPU and does not ask + /// again; the examination reads blank output as a failed query. + #[test] + fn windows_an_answer_with_no_cpu_in_it() { + for (system_output, output) in [ + (Some("RAM\t17179869184\n"), "\r\n"), + (Some("CPU\t \nRAM\t17179869184\n"), " "), + ] { + let summary = summary_on_windows(system_output, Some("must not be read")); + assert_eq!(summary.cpu, None); + assert_eq!(summary.fallback_timeout, None); + let (examined, _) = examination_on_windows(0, output); + assert_eq!(examined.cpu_model, ""); + assert_eq!(examined.cpu_vendor, "unknown"); + assert_eq!( + examined.probe_failures, + ["Get-CimInstance Win32_Processor failed; cannot identify CPU."] + ); + } + } + + /// The system query printed nothing at all, so there is no inventory. The + /// summary asks the CPU question a second time, itself, and keeps that + /// answer; the examination reports a failure. + #[test] + fn windows_an_empty_answer_sends_the_summary_to_its_own_query() { + let summary = summary_on_windows(Some(""), Some("Intel(R) Core(TM) i5-12400\r\n")); + assert_eq!(summary.cpu.as_deref(), Some("Intel(R) Core(TM) i5-12400")); + assert!(summary.fallback_timeout.is_some()); + let (examined, _) = examination_on_windows(0, ""); + assert_eq!(examined.cpu_model, ""); + assert_eq!(examined.probe_failures.len(), 1); + } + + /// The query could not run, failed or timed out. Both stacks read that as + /// no CPU; the summary tries its own query once more first, the examination + /// records a failure and leaves the vendor unknown. + #[test] + fn windows_a_query_that_fails_or_times_out() { + // `run` answers 127 for a program it could not start and 124 for one + // it killed at the deadline. + for rc in [1, 124, 127] { + let (examined, _) = examination_on_windows(rc, ""); + assert_eq!(examined.cpu_model, "", "rc {rc}"); + assert_eq!(examined.cpu_vendor, "unknown", "rc {rc}"); + assert_eq!(examined.probe_failures.len(), 1, "rc {rc}"); + } + // A non-zero exit with a name on stdout is not trusted either. + let (failed_with_output, _) = examination_on_windows(1, WINDOWS_CPU_NAME); + assert_eq!(failed_with_output.cpu_model, ""); + assert_eq!(failed_with_output.probe_failures.len(), 1); + + let both_fail = summary_on_windows(None, None); + assert_eq!(both_fail.cpu, None); + assert!(both_fail.fallback_timeout.is_some()); + let second_try = summary_on_windows(None, Some(WINDOWS_CPU_NAME)); + assert_eq!(second_try.cpu.as_deref(), Some(WINDOWS_CPU_NAME)); + } + + /// The limits: the examination allows its query 8 s, the summary allows the + /// system query behind the inventory's CPU row 1.5 s, and its own fallback + /// query 1.5 s too. A host that answers in between is read by one and not + /// the other. + #[test] + fn windows_the_two_reads_are_given_different_time() { + let (_, examination_limit) = examination_on_windows(0, WINDOWS_CPU_NAME); + assert_eq!(examination_limit, Duration::from_secs(8)); + let summary = summary_on_windows(None, None); + let (script, system_limit) = summary.system_query.expect("the system query ran"); + assert_eq!(system_limit, Duration::from_millis(1_500)); + assert!( + script.contains("Win32_Processor") && script.contains("Win32_ComputerSystem"), + "the CPU row comes from the system query, which also asks for RAM: {script}" + ); + assert_eq!(summary.fallback_timeout, Some(Duration::from_millis(1_500))); + } + + /// With an inventory the summary never starts its own CPU query, even when + /// the inventory has no CPU row: that is `windows_cpu_model`'s rule, and the + /// fallback is for the case with no inventory at all. + #[test] + fn windows_the_summary_asks_its_own_query_only_without_an_inventory() { + let with_cpu = crate::host_gpu::windows_inventory::parse_windows_examine_inventory( + "CPU\tAMD EPYC 7763\n", + ); + let without_cpu = crate::host_gpu::windows_inventory::parse_windows_examine_inventory( + "RAM\t17179869184\n", + ); + let never = || -> Option { panic!("an inventory was given") }; + assert_eq!( + crate::host_gpu::windows_cpu_model(Some(&with_cpu), never).as_deref(), + Some("AMD EPYC 7763") + ); + assert_eq!( + crate::host_gpu::windows_cpu_model(Some(&without_cpu), never), + None + ); + assert_eq!( + crate::host_gpu::windows_cpu_model(None, || Some("from the fallback".to_owned())) + .as_deref(), + Some("from the fallback") + ); + } } diff --git a/crates/rocm-core/src/host_gpu/windows_inventory.rs b/crates/rocm-core/src/host_gpu/windows_inventory.rs index 3a47d86747..c1819726ff 100644 --- a/crates/rocm-core/src/host_gpu/windows_inventory.rs +++ b/crates/rocm-core/src/host_gpu/windows_inventory.rs @@ -5,10 +5,10 @@ //! The Windows display-adapter inventory: the PowerShell queries, the parsers for //! their output, and the assembly of one inventory from several sources. +#[cfg(any(windows, test))] +use super::capture::OPTIONAL_COMMAND_TIMEOUT; #[cfg(windows)] -use super::capture::{ - OPTIONAL_COMMAND_TIMEOUT, capture_optional_command, capture_optional_command_with_timeout, -}; +use super::capture::{capture_optional_command, capture_optional_command_with_timeout}; use super::os_facts::{bytes_text_to_gib, normalize_cpu_model}; use crate::gfx_target::{ amd_pci_device_id_from_pnp_id, gfx_target_from_amd_marketing_name, @@ -20,12 +20,17 @@ use std::time::Duration; pub(super) const WINDOWS_INVENTORY_QUERY_TIMEOUT: Duration = Duration::from_secs(5); +/// What the system query (CPU model and RAM) is allowed: the 1.5 s of an +/// optional command, not the 5 s the display queries get. +#[cfg(any(windows, test))] +const WINDOWS_SYSTEM_INVENTORY_TIMEOUT: Duration = OPTIONAL_COMMAND_TIMEOUT; + pub(super) const WINDOWS_VIDEO_CONTROLLER_INVENTORY_SCRIPT: &str = r#"$gpus = Get-CimInstance -ClassName Win32_VideoController -Property Name,DriverVersion,DriverDate,PNPDeviceID,AdapterCompatibility | Where-Object { $_.PNPDeviceID -match 'VEN_1002' -or $_.AdapterCompatibility -match 'AMD|Advanced Micro Devices' -or $_.Name -match 'AMD|Radeon|Instinct' }; foreach ($gpu in $gpus) { $date = ''; if ($gpu.DriverDate) { $date = $gpu.DriverDate.ToUniversalTime().ToString('yyyy-MM-dd', [System.Globalization.CultureInfo]::InvariantCulture) }; "GPU`t$($gpu.Name)`t$($gpu.DriverVersion)`t$($gpu.PNPDeviceID)`t$date" }"#; #[cfg(windows)] pub(super) const WINDOWS_PNP_ENTITY_INVENTORY_SCRIPT: &str = r#"$displayGuid = '{4d36e968-e325-11ce-bfc1-08002be10318}'; $gpus = Get-CimInstance -ClassName Win32_PnPEntity -Property Name,DeviceID,PNPClass,ClassGuid,Manufacturer | Where-Object { (($_.PNPClass -eq 'Display' -or $_.ClassGuid -eq $displayGuid) -and ($_.DeviceID -match 'VEN_1002' -or $_.Name -match 'AMD|Radeon|Instinct|Graphics' -or $_.Manufacturer -match 'AMD|Advanced Micro Devices')) -or ($_.DeviceID -match 'PCI\\VEN_1002' -and $_.Name -match 'Radeon|Instinct|Graphics') }; foreach ($gpu in $gpus) { "GPU`t$($gpu.Name)`t`t$($gpu.DeviceID)" }"#; -#[cfg(windows)] +#[cfg(any(windows, test))] const WINDOWS_SYSTEM_INVENTORY_SCRIPT: &str = r#"$cpu = Get-CimInstance -ClassName Win32_Processor -Property Name | Select-Object -First 1 -ExpandProperty Name; if ($cpu) { "CPU`t$cpu" }; $ram = Get-CimInstance -ClassName Win32_ComputerSystem -Property TotalPhysicalMemory | Select-Object -First 1 -ExpandProperty TotalPhysicalMemory; if ($ram) { "RAM`t$ram" }"#; #[cfg(windows)] @@ -507,16 +512,32 @@ fn detect_windows_system_inventory_from_cim() -> Option if !runtime_is_windows() { return None; } - capture_optional_command_with_timeout( - "powershell", - &[ - "-NoLogo", - "-NoProfile", - "-NonInteractive", - "-Command", - WINDOWS_SYSTEM_INVENTORY_SCRIPT, - ], - OPTIONAL_COMMAND_TIMEOUT, + windows_system_inventory_with(|script, timeout| { + capture_optional_command_with_timeout( + "powershell", + &[ + "-NoLogo", + "-NoProfile", + "-NonInteractive", + "-Command", + script, + ], + timeout, + ) + }) +} + +/// The system inventory (CPU model and installed RAM) from one query, with the +/// query injected so a test on any host can drive it. `query` is handed the +/// script and the timeout the inventory allows it, and answers with what the +/// script printed, or `None` when it could not run, failed or timed out. +#[cfg(any(windows, test))] +pub(super) fn windows_system_inventory_with( + query: impl FnOnce(&str, Duration) -> Option, +) -> Option { + query( + WINDOWS_SYSTEM_INVENTORY_SCRIPT, + WINDOWS_SYSTEM_INVENTORY_TIMEOUT, ) .map(|output| parse_windows_examine_inventory(&output)) } From 1c9f21b1fadacb03fd3f4813ff79064ef53b9c81 Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Mon, 9 Nov 2026 19:11:06 +0000 Subject: [PATCH 3/7] refactor(rocm-core): one CPU model and vendor reader for the summary and the examination The summary's detect_cpu_model and the examination's probe_cpu_linux and probe_cpu_windows each read the CPU their own way, so one rocm examine --json could show the model spelled two ways, a CPU in the summary and none in the examination, or a Windows CPU read three times. os_facts now has one reader that returns the model and the vendor together: on Linux from /proc/cpuinfo through host_path (the first model name or Hardware line with a value, and the first vendor_id with a value), on Windows from the CPU row of the system inventory query, with the vendor derived from the model string. probe_cpu_linux and probe_cpu_windows record its answer, detect_cpu_model returns its model, and the separate Win32_Processor query behind each Windows copy is gone. WSL is unchanged (option (a)): the examination still returns before the CPU probe, so cpu_model stays empty and cpu_vendor unknown there while the summary shows the CPU. Decided here. Whitespace: the collapse reaches cpu_model, so the model is one string in both forms; nothing reads cpu_model or cpu_vendor to diagnose, they reach the JSON and Doctor reports only. Hardware-only: the Hardware value is the model, the vendor stays unknown. Empty versus unknown: a CPU that could not be read is an absent model in the summary and an empty cpu_model with vendor unknown in the examination, as before; on Windows the examination still records a probe failure for it. Timeout: one limit of 5 s, the inventory's, for the one query; the summary's system query was 1.5 s and the examination's was 8 s, so the summary waits longer and the examination gives up 3 s sooner. Inventory fallback: dropped. With no inventory the summary used to ask for the CPU again with a query of its own, but the inventory run had just asked the same question, so a second try only repeated a failed query; the summary now reads no CPU there. User-visible differences: on Linux cpu_model has its whitespace collapsed; a Hardware-only cpuinfo now gives cpu_model the Hardware value; with Hardware before model name the examination reports the Hardware value; an empty vendor_id value no longer becomes the vendor, the next one is read, else unknown. On Windows cpu_model has its whitespace collapsed; the examination asks the system inventory query, which also asks for RAM, instead of Win32_Processor alone; that query is read as strict UTF-8 and runs through the optional-command helper, which tries each spelling of powershell after a timeout where run started it once, so a hung PowerShell can cost the examination more than one limit; the summary's system query, and so its RAM row too, waits 5 s instead of 1.5 s; and with no inventory the summary shows no CPU instead of retrying. The characterisation tests of the previous commit change only on those inputs. Signed-off-by: Jussi Elo --- crates/rocm-core/src/examine.rs | 2 +- crates/rocm-core/src/examine/host_env.rs | 92 +--- crates/rocm-core/src/host_gpu.rs | 52 +- crates/rocm-core/src/host_gpu/os_facts.rs | 519 +++++++++--------- .../src/host_gpu/windows_inventory.rs | 58 +- docs/architecture.md | 4 +- 6 files changed, 373 insertions(+), 354 deletions(-) diff --git a/crates/rocm-core/src/examine.rs b/crates/rocm-core/src/examine.rs index 5f8b9ba25f..3e0aad3dc3 100644 --- a/crates/rocm-core/src/examine.rs +++ b/crates/rocm-core/src/examine.rs @@ -38,7 +38,7 @@ use host_env::{ probe_shared_memory, }; #[cfg(test)] -pub(crate) use host_env::{probe_cpu_linux_from, probe_cpu_windows_with}; +pub(crate) use host_env::{record_cpu, record_cpu_windows}; pub(crate) use library_copies::comgr_matches_runtime; use library_copies::probe_comgr; use linux_driver::{probe_devices, probe_modules, probe_secure_boot, probe_user}; diff --git a/crates/rocm-core/src/examine/host_env.rs b/crates/rocm-core/src/examine/host_env.rs index 9ff73841a3..8127eaa9ab 100644 --- a/crates/rocm-core/src/examine/host_env.rs +++ b/crates/rocm-core/src/examine/host_env.rs @@ -7,6 +7,7 @@ use super::Examination; use super::library_copies::libraries_on_ld_path; +use crate::host_gpu::{CpuFacts, read_linux_cpu_facts, read_windows_cpu_facts}; use crate::probe_exec::{MEDIUM, SHORT, read_host_text, run, truncate_to_chars}; use crate::{runtime_is_linux, runtime_is_windows}; @@ -114,81 +115,36 @@ pub(super) fn parse_iommu_param(cmdline: &str) -> Option { }) } -pub(super) fn probe_cpu_linux(e: &mut Examination) { - probe_cpu_linux_from(e, &read_host_text("/proc/cpuinfo")); +/// Fill the CPU fields from the shared reader's answer. A CPU that could not be +/// read leaves the model empty and the vendor `unknown`, which is how the JSON +/// has always said "not collected". +pub(crate) fn record_cpu(e: &mut Examination, facts: CpuFacts) { + e.cpu_model = facts.model.unwrap_or_default(); + e.cpu_vendor = facts.vendor; } -/// [`probe_cpu_linux`] over cpuinfo text already read, so a test can drive it -/// without a host root. -pub(crate) fn probe_cpu_linux_from(e: &mut Examination, txt: &str) { - for line in txt.lines() { - if (e.cpu_vendor == "unknown") - && line.starts_with("vendor_id") - && let Some((_, value)) = line.split_once(':') - { - let value = value.trim(); - e.cpu_vendor = if value.contains("AMD") { - "amd".to_owned() - } else if value.contains("Intel") { - "intel".to_owned() - } else { - value.to_lowercase() - }; - } - if e.cpu_model.is_empty() - && line.starts_with("model name") - && let Some((_, value)) = line.split_once(':') - { - e.cpu_model = value.trim().to_owned(); - } - if e.cpu_vendor != "unknown" && !e.cpu_model.is_empty() { - break; - } - } +/// The CPU on Linux, from `/proc/cpuinfo` through the reader the host summary +/// uses. A cpuinfo with no CPU in it is not a probe failure. +pub(super) fn probe_cpu_linux(e: &mut Examination) { + record_cpu(e, read_linux_cpu_facts()); } +/// The CPU on Windows, from the CPU row of the system inventory through the +/// reader the host summary uses. pub(super) fn probe_cpu_windows(e: &mut Examination) { - probe_cpu_windows_with(e, |timeout| { - run( - "powershell", - &[ - "-NoProfile", - "-Command", - "(Get-CimInstance Win32_Processor | Select-Object -First 1).Name", - ], - timeout, - ) - }); + record_cpu_windows(e, read_windows_cpu_facts()); } -/// [`probe_cpu_windows`] with the query injected, so a test on any host can -/// drive it. `query` is handed the timeout the examination allows it and -/// answers as [`run`] does: `(rc, stdout, stderr)`. -pub(crate) fn probe_cpu_windows_with( - e: &mut Examination, - query: impl FnOnce(std::time::Duration) -> (i32, String, String), -) { - let (rc, out, _) = query(MEDIUM); - if rc == 0 && !out.trim().is_empty() { - e.cpu_model = out - .trim() - .lines() - .next() - .unwrap_or_default() - .trim() - .to_owned(); - let lname = e.cpu_model.to_lowercase(); - e.cpu_vendor = if lname.contains("amd") { - "amd".to_owned() - } else if lname.contains("intel") { - "intel".to_owned() - } else { - "unknown".to_owned() - }; - } else { +/// [`record_cpu`], and a failure entry when no CPU could be read: on Windows the +/// CPU comes from a query that can fail or time out, and an empty model alone +/// would not say so. Crate-visible so a test on any host can feed it the answer +/// of an injected query. +pub(crate) fn record_cpu_windows(e: &mut Examination, facts: CpuFacts) { + if facts.model.is_none() { e.probe_failures .push("Get-CimInstance Win32_Processor failed; cannot identify CPU.".to_owned()); } + record_cpu(e, facts); } pub(super) fn probe_env(e: &mut Examination) { @@ -666,8 +622,8 @@ mod tests { /// CPU-model pair of docs/host-probing.md, native Linux. Both reads take /// `/proc/cpuinfo` from the simulated host root, so a planted CPU no runner - /// has is what each reports. They differ on the whitespace in it, which the - /// summary collapses and the examination keeps. + /// has is what each reports, and both report the model with its whitespace + /// collapsed (the examination used to keep the runs of spaces). #[cfg(all(feature = "e2e-test-hooks", target_os = "linux"))] #[test] fn cpu_reads_follow_the_simulated_host_root() { @@ -683,7 +639,7 @@ mod tests { }, ); assert_eq!(summary.as_deref(), Some("Planted CPU 9000 Processor")); - assert_eq!(examined.cpu_model, "Planted CPU 9000 Processor"); + assert_eq!(examined.cpu_model, "Planted CPU 9000 Processor"); assert_eq!(examined.cpu_vendor, "amd"); } diff --git a/crates/rocm-core/src/host_gpu.rs b/crates/rocm-core/src/host_gpu.rs index f9caf206e9..fa841389b2 100644 --- a/crates/rocm-core/src/host_gpu.rs +++ b/crates/rocm-core/src/host_gpu.rs @@ -37,10 +37,11 @@ use crate::{ host_path, runtime_is_linux, runtime_is_windows, runtime_os_name, }; use anyhow::Result; -pub(crate) use os_facts::detect_cpu_model; -pub(crate) use os_facts::detect_distro_name; use os_facts::detect_kernel_version; pub use os_facts::detect_system_ram_gib; +pub(crate) use os_facts::{ + CpuFacts, detect_cpu_model, detect_distro_name, read_linux_cpu_facts, read_windows_cpu_facts, +}; use serde::{Deserialize, Serialize}; use std::fs; use std::path::{Path, PathBuf}; @@ -270,23 +271,22 @@ impl ExamineSummary { fn detect_cpu_model_with_windows_inventory( windows_inventory: Option<&WindowsExamineInventory>, ) -> Option { - if runtime_is_windows() { - return windows_cpu_model(windows_inventory, detect_cpu_model); - } - - detect_cpu_model() + summary_cpu(runtime_is_windows(), windows_inventory, detect_cpu_model) } -/// The summary's CPU on Windows: the inventory's CPU row whenever there is an -/// inventory, even one with no such row, and `fallback` (the summary's own CPU -/// query) only when there is no inventory at all. -fn windows_cpu_model( +/// The summary's CPU. On Windows it is the inventory's CPU row, which is the one +/// query the examination's reader runs too; with no inventory there is no CPU, +/// because the inventory run already asked for it and asking again is the same +/// query. Elsewhere it is `read`, the Linux reader. +fn summary_cpu( + windows: bool, windows_inventory: Option<&WindowsExamineInventory>, - fallback: impl FnOnce() -> Option, + read: impl FnOnce() -> Option, ) -> Option { - match windows_inventory { - Some(inventory) => inventory.cpu_model.clone(), - None => fallback(), + if windows { + os_facts::cpu_facts_from_windows_inventory(windows_inventory).model + } else { + read() } } @@ -502,6 +502,28 @@ mod tests { use std::path::PathBuf; use windows_inventory::WindowsInventorySources; + /// The summary's CPU on Windows is the inventory's CPU row and nothing else: + /// with an inventory that has no such row, or with no inventory at all, it + /// is no CPU, and the Linux reader is never asked. Before, an absent + /// inventory sent the summary to a CPU query of its own. Off Windows the + /// Linux reader answers and the inventory is not looked at. + #[test] + fn the_summary_cpu_comes_from_the_windows_inventory_or_not_at_all() { + let never = || -> Option { panic!("the Linux reader runs only off Windows") }; + let with_cpu = parse_windows_examine_inventory("CPU\tAMD EPYC 7763\nRAM\t17179869184\n"); + let without_cpu = parse_windows_examine_inventory("RAM\t17179869184\n"); + assert_eq!( + summary_cpu(true, Some(&with_cpu), never).as_deref(), + Some("AMD EPYC 7763") + ); + assert_eq!(summary_cpu(true, Some(&without_cpu), never), None); + assert_eq!(summary_cpu(true, None, never), None); + assert_eq!( + summary_cpu(false, Some(&with_cpu), || Some("from cpuinfo".to_owned())).as_deref(), + Some("from cpuinfo") + ); + } + #[test] fn windows_examine_gfx_detection_uses_the_display_inventory() { if !cfg!(windows) { diff --git a/crates/rocm-core/src/host_gpu/os_facts.rs b/crates/rocm-core/src/host_gpu/os_facts.rs index 649c6d8fcf..12165fa6ce 100644 --- a/crates/rocm-core/src/host_gpu/os_facts.rs +++ b/crates/rocm-core/src/host_gpu/os_facts.rs @@ -8,6 +8,9 @@ use super::capture::{ OPTIONAL_COMMAND_TIMEOUT, capture_optional_command, capture_optional_command_with_timeout, }; +use super::windows_inventory::{ + WindowsExamineInventory, run_windows_system_query, windows_system_inventory_with, +}; use crate::{hardware_root, host_path, runtime_is_linux, runtime_is_windows}; use std::fs; @@ -64,54 +67,140 @@ fn distro_name_from_os_release(read: &hardware_root::OsRelease) -> String { } } +/// The CPU's model and vendor, read together by the one reader both stacks use: +/// the summary's `cpu` through [`detect_cpu_model`] and the Windows inventory, +/// the examination's `cpu_model` and `cpu_vendor` through `probe_cpu_linux` and +/// `probe_cpu_windows`. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct CpuFacts { + /// The model with every run of whitespace collapsed to one space, or `None` + /// when no model could be read. `None` covers every way of not knowing (no + /// file, no line, a blank value, a query that failed or timed out), so a + /// caller cannot tell a failed query from an absent line; the Windows + /// examination records a failure for any `None`, and Linux for none. + pub(crate) model: Option, + /// `amd` or `intel`, any other vendor lower-cased as the file spells it, or + /// [`UNKNOWN_CPU_VENDOR`] when none could be read. + pub(crate) vendor: String, +} + +/// The vendor of a CPU no source named. +pub(crate) const UNKNOWN_CPU_VENDOR: &str = "unknown"; + +impl CpuFacts { + fn unknown() -> Self { + Self { + model: None, + vendor: UNKNOWN_CPU_VENDOR.to_owned(), + } + } +} + +/// The CPU summary line: the model of [`read_cpu_facts`]. pub(crate) fn detect_cpu_model() -> Option { + read_cpu_facts().model +} + +/// The CPU facts of the host this process runs on. +pub(crate) fn read_cpu_facts() -> CpuFacts { if runtime_is_windows() { - return detect_cpu_model_windows(|timeout| { - let script = - "Get-CimInstance Win32_Processor | Select-Object -First 1 -ExpandProperty Name"; - capture_optional_command_with_timeout( - "powershell", - &["-NoProfile", "-Command", script], - timeout, - ) - }); + read_windows_cpu_facts() + } else if runtime_is_linux() { + read_linux_cpu_facts() + } else { + CpuFacts::unknown() } +} - if runtime_is_linux() - && let Some(model) = fs::read_to_string(host_path("/proc/cpuinfo")) - .ok() - .and_then(|text| cpu_model_from_cpuinfo(&text)) - { - return Some(model); +/// The CPU facts from `/proc/cpuinfo`, read through [`host_path`]. +pub(crate) fn read_linux_cpu_facts() -> CpuFacts { + cpu_facts_from_cpuinfo(&fs::read_to_string(host_path("/proc/cpuinfo")).unwrap_or_default()) +} + +/// The CPU facts from `/proc/cpuinfo` text. +/// +/// The model is the first `model name` or `Hardware` line that has a value, +/// whichever comes first: an Arm board that prints no `model name` is still +/// named. The vendor is the first `vendor_id` line that has a value, `amd` or +/// `intel` by substring (`AuthenticAMD`, `GenuineIntel`) and any other +/// lower-cased. A file with no `vendor_id` (Arm kernels print none) has an +/// unknown vendor beside a known model. +pub(super) fn cpu_facts_from_cpuinfo(text: &str) -> CpuFacts { + let mut facts = CpuFacts::unknown(); + for line in text.lines() { + if facts.model.is_none() + && let Some(value) = line + .strip_prefix("model name") + .or_else(|| line.strip_prefix("Hardware")) + .and_then(|rest| rest.split_once(':').map(|(_, value)| value)) + { + let value = normalize_cpu_model(value); + if !value.is_empty() { + facts.model = Some(value); + } + } + if facts.vendor == UNKNOWN_CPU_VENDOR + && let Some(value) = line + .strip_prefix("vendor_id") + .and_then(|rest| rest.split_once(':').map(|(_, value)| value.trim())) + .filter(|value| !value.is_empty()) + { + facts.vendor = if value.contains("AMD") { + "amd".to_owned() + } else if value.contains("Intel") { + "intel".to_owned() + } else { + value.to_lowercase() + }; + } + if facts.model.is_some() && facts.vendor != UNKNOWN_CPU_VENDOR { + break; + } } + facts +} - None +/// The CPU facts from the Windows system inventory's `CPU` row: the one query +/// the summary's inventory runs for its CPU and RAM. +pub(crate) fn read_windows_cpu_facts() -> CpuFacts { + windows_cpu_facts_with(run_windows_system_query) } -/// The summary's own Windows CPU query, with the query injected so a test on any -/// host can drive it. `query` is handed the timeout the summary allows it. -pub(super) fn detect_cpu_model_windows( - query: impl FnOnce(std::time::Duration) -> Option, -) -> Option { - query(OPTIONAL_COMMAND_TIMEOUT) - .map(|value| normalize_cpu_model(&value)) - .filter(|value| !value.is_empty()) +/// [`read_windows_cpu_facts`] with the query injected, so a test on any host can +/// drive it. `query` is handed the script and the time the inventory allows it, +/// and answers with what the script printed, or `None` when it could not run, +/// failed or timed out. +pub(super) fn windows_cpu_facts_with( + query: impl FnOnce(&str, std::time::Duration) -> Option, +) -> CpuFacts { + cpu_facts_from_windows_inventory(windows_system_inventory_with(query).as_ref()) } -/// The first `model name` or `Hardware` line of `/proc/cpuinfo` text that has a -/// value, whitespace collapsed. -pub(super) fn cpu_model_from_cpuinfo(text: &str) -> Option { - text.lines().find_map(|line| { - let value = line - .strip_prefix("model name") - .and_then(|rest| rest.split_once(':').map(|(_, value)| value)) - .or_else(|| { - line.strip_prefix("Hardware") - .and_then(|rest| rest.split_once(':').map(|(_, value)| value)) - })?; - let value = normalize_cpu_model(value); - (!value.is_empty()).then_some(value) - }) +/// The CPU facts of a Windows inventory: its CPU row, or unknown when there is +/// no inventory or the row is absent or blank. The summary reads the inventory +/// it already holds through this, so it starts no second query for the CPU and +/// reads the same row the examination's reader does. +/// +/// Windows reports no vendor field here, so it is derived from the model string +/// (`amd` or `intel`, else unknown). +pub(super) fn cpu_facts_from_windows_inventory( + inventory: Option<&WindowsExamineInventory>, +) -> CpuFacts { + let Some(model) = inventory.and_then(|inventory| inventory.cpu_model.clone()) else { + return CpuFacts::unknown(); + }; + let lowered = model.to_lowercase(); + let vendor = if lowered.contains("amd") { + "amd" + } else if lowered.contains("intel") { + "intel" + } else { + UNKNOWN_CPU_VENDOR + }; + CpuFacts { + model: Some(model), + vendor: vendor.to_owned(), + } } pub fn detect_system_ram_gib() -> Option { @@ -238,91 +327,85 @@ mod tests { // ── CPU model: the summary's read and the examination's read ────────── // - // Characterisation tests for the CPU-model pair of docs/host-probing.md. - // `rocm examine --json` reads the CPU twice: the summary (`summary.cpu`, the - // `cpu:` line of the text) through `detect_cpu_model` and the Windows - // inventory's CPU row, and the examination (`cpu_model` and `cpu_vendor`) - // through `probe_cpu_linux` and `probe_cpu_windows`. Each case below gives - // both reads the same input and pins what each returns, so the change that - // merges them moves exactly the assertions it means to. Where the two - // agree, one value is asserted for both. Where they differ, each read's own - // answer is asserted, under a comment that says so. - - use crate::examine::{Examination, probe_cpu_linux_from, probe_cpu_windows_with}; + // Tests of the CPU-model pair of docs/host-probing.md. `rocm examine --json` + // reads the CPU in two places: the summary (`summary.cpu`, the `cpu:` line of + // the text) and the examination (`cpu_model` and `cpu_vendor`). Both go + // through one reader now, so each case below hands both the same input and + // asserts one answer. The cases that used to differ say so in their + // comment, with what each read answered before. + + use crate::examine::{Examination, record_cpu, record_cpu_windows}; use crate::host_gpu::windows_inventory::{ InventoryScope, WindowsExamineInventory, WindowsInventorySources, assemble_windows_inventory, windows_system_inventory_with, }; use std::time::Duration; - /// One `/proc/cpuinfo` and what each read makes of it. + /// One `/proc/cpuinfo` and the CPU both reads make of it. struct CpuinfoCase { name: &'static str, text: &'static str, - /// `detect_cpu_model`'s answer. - summary: Option<&'static str>, - /// `Examination::cpu_model` and `Examination::cpu_vendor`. - examination: (&'static str, &'static str), + /// The model: `summary.cpu`, and `cpu_model` where `Some`, empty where `None`. + model: Option<&'static str>, + /// `cpu_vendor`. + vendor: &'static str, } - const INTEL_CPUINFO: &str = "processor\t: 0\nvendor_id\t: GenuineIntel\ncpu family\t: 6\nmodel name\t: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz\nprocessor\t: 1\nvendor_id\t: GenuineIntel\nmodel name\t: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz\n"; - #[test] - fn linux_cpuinfo_the_two_reads_agree_on_an_ordinary_cpu() { + fn linux_cpuinfo_both_reads_report_one_cpu() { let cases = [ CpuinfoCase { name: "Intel, model name and vendor_id", - text: INTEL_CPUINFO, - summary: Some("Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz"), - examination: ("Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz", "intel"), + text: "processor\t: 0\nvendor_id\t: GenuineIntel\ncpu family\t: 6\nmodel name\t: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz\nprocessor\t: 1\nvendor_id\t: GenuineIntel\nmodel name\t: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz\n", + model: Some("Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz"), + vendor: "intel", }, CpuinfoCase { name: "AMD, model name and vendor_id", text: "vendor_id\t: AuthenticAMD\nmodel name\t: AMD Ryzen 9 9950X 16-Core Processor\n", - summary: Some("AMD Ryzen 9 9950X 16-Core Processor"), - examination: ("AMD Ryzen 9 9950X 16-Core Processor", "amd"), + model: Some("AMD Ryzen 9 9950X 16-Core Processor"), + vendor: "amd", }, CpuinfoCase { // No `vendor_id` (Arm kernels print none): the model is read, the - // vendor stays "unknown" in the examination. + // vendor stays "unknown". name: "model name and no vendor_id", text: "processor\t: 0\nmodel name\t: ARMv8 Processor rev 1 (v8l)\n", - summary: Some("ARMv8 Processor rev 1 (v8l)"), - examination: ("ARMv8 Processor rev 1 (v8l)", "unknown"), + model: Some("ARMv8 Processor rev 1 (v8l)"), + vendor: "unknown", }, CpuinfoCase { name: "a blank model name line, then a real one", text: "vendor_id\t: GenuineIntel\nmodel name\t: \nmodel name\t: Intel(R) Xeon(R) Gold 6248\n", - summary: Some("Intel(R) Xeon(R) Gold 6248"), - examination: ("Intel(R) Xeon(R) Gold 6248", "intel"), + model: Some("Intel(R) Xeon(R) Gold 6248"), + vendor: "intel", }, CpuinfoCase { // `Hardware` after the per-core blocks is what an Arm board // prints, and it follows `model name`, so both read the latter. name: "model name first, Hardware last", text: "processor\t: 0\nmodel name\t: ARMv7 Processor rev 4 (v7l)\nHardware\t: BCM2835\n", - summary: Some("ARMv7 Processor rev 4 (v7l)"), - examination: ("ARMv7 Processor rev 4 (v7l)", "unknown"), + model: Some("ARMv7 Processor rev 4 (v7l)"), + vendor: "unknown", }, CpuinfoCase { name: "nothing to read", text: "processor\t: 0\nCPU implementer\t: 0x41\n", - summary: None, - examination: ("", "unknown"), + model: None, + vendor: "unknown", }, CpuinfoCase { name: "an empty file", text: "", - summary: None, - examination: ("", "unknown"), + model: None, + vendor: "unknown", }, CpuinfoCase { - // The examination lower-cases a vendor it does not know, the - // summary records no vendor at all. + // A vendor other than AMD and Intel is kept, lower-cased. name: "a vendor_id that is neither AMD nor Intel", text: "vendor_id\t: HygonGenuine\nmodel name\t: Hygon C86 7185 32-core Processor\n", - summary: Some("Hygon C86 7185 32-core Processor"), - examination: ("Hygon C86 7185 32-core Processor", "hygongenuine"), + model: Some("Hygon C86 7185 32-core Processor"), + vendor: "hygongenuine", }, ]; for case in cases { @@ -330,42 +413,53 @@ mod tests { } } - /// The inputs on which the two reads give different answers today; each - /// read's own answer is pinned. + /// The inputs on which the two reads used to give different answers. Each + /// comment says what the summary and the examination answered before the + /// reads were merged; the answer asserted is the one both give now. #[test] - fn linux_cpuinfo_the_two_reads_differ_on_four_inputs() { + fn linux_cpuinfo_the_inputs_the_two_reads_used_to_disagree_on() { let cases = [ CpuinfoCase { - // The summary collapses whitespace runs, the examination trims - // only: one model string, two spellings in one `--json`. + // Before: summary collapsed whitespace runs, examination trimmed + // only (`Intel(R) Pentium(R) CPU N3700 @ 1.60GHz`). name: "repeated spaces inside the model name", text: "vendor_id\t: GenuineIntel\nmodel name\t: Intel(R) Pentium(R) CPU N3700 @ 1.60GHz \n", - summary: Some("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz"), - examination: ("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz", "intel"), + model: Some("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz"), + vendor: "intel", }, CpuinfoCase { - // The summary falls back to `Hardware`; the examination reads - // `model name` only, so it reports no CPU at all. + // Before: summary read `Hardware`, examination read `model name` + // only and reported no CPU (empty model, vendor unknown). name: "Hardware and no model name", text: "processor\t: 0\nBogoMIPS\t: 38.40\nHardware\t: BCM2835\nRevision\t: a02082\n", - summary: Some("BCM2835"), - examination: ("", "unknown"), + model: Some("BCM2835"), + vendor: "unknown", }, CpuinfoCase { - // The summary takes whichever of the two comes first, the - // examination only ever the `model name`. + // Before: summary took whichever of the two came first + // (`BCM2835`), examination only the `model name`. name: "Hardware before model name", text: "Hardware\t: BCM2835\nmodel name\t: ARMv7 Processor rev 4 (v7l)\n", - summary: Some("BCM2835"), - examination: ("ARMv7 Processor rev 4 (v7l)", "unknown"), + model: Some("BCM2835"), + vendor: "unknown", }, CpuinfoCase { - // An empty `vendor_id` value is taken as the vendor by the - // examination, which then stops looking for another. + // Before: examination took an empty `vendor_id` as the vendor + // (`""`) and stopped looking. An empty value names no vendor, so + // the next `vendor_id` line is read. name: "an empty vendor_id value", text: "vendor_id\t:\nmodel name\t: Some CPU\nvendor_id\t: GenuineIntel\n", - summary: Some("Some CPU"), - examination: ("Some CPU", ""), + model: Some("Some CPU"), + vendor: "intel", + }, + CpuinfoCase { + // The same input where no later line names a vendor. Before: + // examination `""`; now `unknown`, which is what the JSON says + // for a vendor that was not read. + name: "an empty vendor_id value and no other", + text: "vendor_id\t:\nmodel name\t: Some CPU\n", + model: Some("Some CPU"), + vendor: "unknown", }, ]; for case in cases { @@ -374,17 +468,19 @@ mod tests { } fn assert_cpuinfo_case(case: &CpuinfoCase) { + // The summary's `cpu`: what `detect_cpu_model` makes of the file. assert_eq!( - cpu_model_from_cpuinfo(case.text).as_deref(), - case.summary, + cpu_facts_from_cpuinfo(case.text).model.as_deref(), + case.model, "summary, {}", case.name ); + // The examination's fields. let mut examined = Examination::default(); - probe_cpu_linux_from(&mut examined, case.text); + record_cpu(&mut examined, cpu_facts_from_cpuinfo(case.text)); assert_eq!( (examined.cpu_model.as_str(), examined.cpu_vendor.as_str()), - case.examination, + (case.model.unwrap_or(""), case.vendor), "examination, {}", case.name ); @@ -396,22 +492,25 @@ mod tests { ); } - /// What the examination makes of one answer from its Windows query. - fn examination_on_windows(rc: i32, stdout: &str) -> (Examination, Duration) { + /// What the examination makes of one answer from the system query. + fn examination_on_windows(output: Option<&str>) -> (Examination, Duration) { let mut examined = Examination::default(); let mut asked = None; - probe_cpu_windows_with(&mut examined, |timeout| { - asked = Some(timeout); - (rc, stdout.to_owned(), String::new()) - }); + record_cpu_windows( + &mut examined, + windows_cpu_facts_with(|_, timeout| { + asked = Some(timeout); + output.map(str::to_owned) + }), + ); (examined, asked.expect("the examination ran its query")) } /// The summary's system inventory with only `system` wired, so a test sees - /// the query the CPU row comes from. + /// the query the CPU row comes from and counts how many run. struct SystemQuery<'a> { output: Option<&'a str>, - asked: Option<(String, Duration)>, + asked: Vec<(String, Duration)>, } impl WindowsInventorySources for SystemQuery<'_> { @@ -426,89 +525,60 @@ mod tests { } fn system(&mut self) -> Option { windows_system_inventory_with(|script, timeout| { - self.asked = Some((script.to_owned(), timeout)); + self.asked.push((script.to_owned(), timeout)); self.output.map(str::to_owned) }) } } /// What the summary reports for the CPU on Windows when the system query - /// prints `system_output` (`None`: it could not run) and its own fallback - /// query prints `fallback_output`. - struct SummaryOnWindows { - cpu: Option, - system_query: Option<(String, Duration)>, - fallback_timeout: Option, - } - - fn summary_on_windows( - system_output: Option<&str>, - fallback_output: Option<&str>, - ) -> SummaryOnWindows { + /// prints `output` (`None`: it could not run), and every query that ran. + fn summary_on_windows(output: Option<&str>) -> (Option, Vec<(String, Duration)>) { let mut sources = SystemQuery { - output: system_output, - asked: None, + output, + asked: Vec::new(), }; let inventory = assemble_windows_inventory(&mut sources, InventoryScope::SUMMARY); - let mut fallback_timeout = None; - let cpu = crate::host_gpu::windows_cpu_model(inventory.as_ref(), || { - detect_cpu_model_windows(|timeout| { - fallback_timeout = Some(timeout); - fallback_output.map(str::to_owned) - }) - }); - SummaryOnWindows { - cpu, - system_query: sources.asked, - fallback_timeout, - } + ( + cpu_facts_from_windows_inventory(inventory.as_ref()).model, + sources.asked, + ) } const WINDOWS_CPU_NAME: &str = "AMD Ryzen 9 9950X 16-Core Processor"; #[test] - fn windows_the_two_reads_agree_on_a_name_the_query_returns() { - let summary = summary_on_windows( - Some(&format!("CPU\t{WINDOWS_CPU_NAME}\nRAM\t68719476736\n")), - None, - ); - assert_eq!(summary.cpu.as_deref(), Some(WINDOWS_CPU_NAME)); - assert_eq!( - summary.fallback_timeout, None, - "an inventory was read, so the summary's own query does not run" - ); - let (examined, _) = examination_on_windows(0, &format!("{WINDOWS_CPU_NAME}\r\n")); + fn windows_both_reads_report_the_name_the_query_returns() { + let output = format!("CPU\t{WINDOWS_CPU_NAME}\r\nRAM\t68719476736\r\n"); + let (summary, _) = summary_on_windows(Some(&output)); + assert_eq!(summary.as_deref(), Some(WINDOWS_CPU_NAME)); + let (examined, _) = examination_on_windows(Some(&output)); assert_eq!(examined.cpu_model, WINDOWS_CPU_NAME); assert_eq!(examined.cpu_vendor, "amd"); assert!(examined.probe_failures.is_empty()); } - /// The summary collapses whitespace runs, the examination trims and keeps - /// the first line only. Windows pads the name of some CPUs. + /// Before: the summary collapsed whitespace runs and the examination + /// trimmed and kept the first line only. Windows pads the name of some CPUs. + /// Both collapse now. #[test] - fn windows_the_two_reads_differ_on_whitespace_inside_the_name() { - let summary = summary_on_windows( - Some("CPU\t Intel(R) Pentium(R) CPU N3700 @ 1.60GHz \n"), - None, - ); + fn windows_both_reads_collapse_whitespace_inside_the_name() { + let output = "CPU\t Intel(R) Pentium(R) CPU N3700 @ 1.60GHz \r\n"; + let (summary, _) = summary_on_windows(Some(output)); assert_eq!( - summary.cpu.as_deref(), + summary.as_deref(), Some("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz") ); - let (examined, _) = - examination_on_windows(0, " Intel(R) Pentium(R) CPU N3700 @ 1.60GHz \r\n"); + let (examined, _) = examination_on_windows(Some(output)); assert_eq!( examined.cpu_model, - "Intel(R) Pentium(R) CPU N3700 @ 1.60GHz" + "Intel(R) Pentium(R) CPU N3700 @ 1.60GHz" ); assert_eq!(examined.cpu_vendor, "intel"); - // Only the examination reads a first line out of several. - let (several, _) = examination_on_windows(0, "AMD EPYC 7763\r\nAMD EPYC 7763\r\n"); - assert_eq!(several.cpu_model, "AMD EPYC 7763"); } #[test] - fn windows_the_examination_derives_the_vendor_from_the_model_string() { + fn windows_the_vendor_is_derived_from_the_model_string() { for (name, vendor) in [ ("AMD Ryzen 7 5800X 8-Core Processor", "amd"), ("amd epyc 7763", "amd"), @@ -516,114 +586,73 @@ mod tests { ("Genuine Intel(R) CPU 0000", "intel"), ("Qualcomm(R) Snapdragon(R) X Elite", "unknown"), ] { - let (examined, _) = examination_on_windows(0, name); + let (examined, _) = examination_on_windows(Some(&format!("CPU\t{name}\n"))); assert_eq!(examined.cpu_vendor, vendor, "{name}"); assert_eq!(examined.cpu_model, name); } } - /// The system query printed no CPU row (here only the RAM row), or nothing. - /// The summary has an inventory, so it reports no CPU and does not ask - /// again; the examination reads blank output as a failed query. + /// The query printed no CPU row (only the RAM row, or a blank one), or + /// nothing, or could not run: both reads report no CPU. The examination + /// records a failure and leaves the vendor `unknown`; the summary starts no + /// second query. Before: an empty answer or a query that could not run + /// sent the summary to a second, separate PowerShell query for the CPU. #[test] - fn windows_an_answer_with_no_cpu_in_it() { - for (system_output, output) in [ - (Some("RAM\t17179869184\n"), "\r\n"), - (Some("CPU\t \nRAM\t17179869184\n"), " "), + fn windows_an_answer_with_no_cpu_is_no_cpu_in_both_reads() { + for output in [ + Some("RAM\t17179869184\n"), + Some("CPU\t \nRAM\t17179869184\n"), + Some(""), + None, ] { - let summary = summary_on_windows(system_output, Some("must not be read")); - assert_eq!(summary.cpu, None); - assert_eq!(summary.fallback_timeout, None); - let (examined, _) = examination_on_windows(0, output); - assert_eq!(examined.cpu_model, ""); - assert_eq!(examined.cpu_vendor, "unknown"); + let (summary, queries) = summary_on_windows(output); + assert_eq!(summary, None, "{output:?}"); + assert_eq!( + queries.len(), + 1, + "one system query, no second one: {output:?}" + ); + let (examined, _) = examination_on_windows(output); + assert_eq!(examined.cpu_model, "", "{output:?}"); + assert_eq!(examined.cpu_vendor, "unknown", "{output:?}"); assert_eq!( examined.probe_failures, - ["Get-CimInstance Win32_Processor failed; cannot identify CPU."] + ["Get-CimInstance Win32_Processor failed; cannot identify CPU."], + "{output:?}" ); } } - /// The system query printed nothing at all, so there is no inventory. The - /// summary asks the CPU question a second time, itself, and keeps that - /// answer; the examination reports a failure. - #[test] - fn windows_an_empty_answer_sends_the_summary_to_its_own_query() { - let summary = summary_on_windows(Some(""), Some("Intel(R) Core(TM) i5-12400\r\n")); - assert_eq!(summary.cpu.as_deref(), Some("Intel(R) Core(TM) i5-12400")); - assert!(summary.fallback_timeout.is_some()); - let (examined, _) = examination_on_windows(0, ""); - assert_eq!(examined.cpu_model, ""); - assert_eq!(examined.probe_failures.len(), 1); - } - - /// The query could not run, failed or timed out. Both stacks read that as - /// no CPU; the summary tries its own query once more first, the examination - /// records a failure and leaves the vendor unknown. + /// One limit for both reads: 5 s, the limit of every inventory query. Before: + /// 8 s for the examination, 1.5 s for the summary's system query and for its + /// own CPU query. #[test] - fn windows_a_query_that_fails_or_times_out() { - // `run` answers 127 for a program it could not start and 124 for one - // it killed at the deadline. - for rc in [1, 124, 127] { - let (examined, _) = examination_on_windows(rc, ""); - assert_eq!(examined.cpu_model, "", "rc {rc}"); - assert_eq!(examined.cpu_vendor, "unknown", "rc {rc}"); - assert_eq!(examined.probe_failures.len(), 1, "rc {rc}"); - } - // A non-zero exit with a name on stdout is not trusted either. - let (failed_with_output, _) = examination_on_windows(1, WINDOWS_CPU_NAME); - assert_eq!(failed_with_output.cpu_model, ""); - assert_eq!(failed_with_output.probe_failures.len(), 1); - - let both_fail = summary_on_windows(None, None); - assert_eq!(both_fail.cpu, None); - assert!(both_fail.fallback_timeout.is_some()); - let second_try = summary_on_windows(None, Some(WINDOWS_CPU_NAME)); - assert_eq!(second_try.cpu.as_deref(), Some(WINDOWS_CPU_NAME)); - } - - /// The limits: the examination allows its query 8 s, the summary allows the - /// system query behind the inventory's CPU row 1.5 s, and its own fallback - /// query 1.5 s too. A host that answers in between is read by one and not - /// the other. - #[test] - fn windows_the_two_reads_are_given_different_time() { - let (_, examination_limit) = examination_on_windows(0, WINDOWS_CPU_NAME); - assert_eq!(examination_limit, Duration::from_secs(8)); - let summary = summary_on_windows(None, None); - let (script, system_limit) = summary.system_query.expect("the system query ran"); - assert_eq!(system_limit, Duration::from_millis(1_500)); + fn windows_both_reads_are_given_the_same_time() { + let (_, examination_limit) = examination_on_windows(Some("CPU\tAMD EPYC 7763\n")); + assert_eq!(examination_limit, Duration::from_secs(5)); + let (_, queries) = summary_on_windows(Some("CPU\tAMD EPYC 7763\n")); + let (script, summary_limit) = &queries[0]; + assert_eq!(*summary_limit, Duration::from_secs(5)); assert!( script.contains("Win32_Processor") && script.contains("Win32_ComputerSystem"), "the CPU row comes from the system query, which also asks for RAM: {script}" ); - assert_eq!(summary.fallback_timeout, Some(Duration::from_millis(1_500))); } - /// With an inventory the summary never starts its own CPU query, even when - /// the inventory has no CPU row: that is `windows_cpu_model`'s rule, and the - /// fallback is for the case with no inventory at all. + /// With no inventory at all the summary has no CPU, and reads the CPU row of + /// an inventory it holds without asking again. #[test] - fn windows_the_summary_asks_its_own_query_only_without_an_inventory() { - let with_cpu = crate::host_gpu::windows_inventory::parse_windows_examine_inventory( + fn windows_the_inventory_is_the_summarys_only_source() { + assert_eq!(cpu_facts_from_windows_inventory(None), CpuFacts::unknown()); + let inventory = crate::host_gpu::windows_inventory::parse_windows_examine_inventory( "CPU\tAMD EPYC 7763\n", ); - let without_cpu = crate::host_gpu::windows_inventory::parse_windows_examine_inventory( - "RAM\t17179869184\n", - ); - let never = || -> Option { panic!("an inventory was given") }; - assert_eq!( - crate::host_gpu::windows_cpu_model(Some(&with_cpu), never).as_deref(), - Some("AMD EPYC 7763") - ); - assert_eq!( - crate::host_gpu::windows_cpu_model(Some(&without_cpu), never), - None - ); assert_eq!( - crate::host_gpu::windows_cpu_model(None, || Some("from the fallback".to_owned())) - .as_deref(), - Some("from the fallback") + cpu_facts_from_windows_inventory(Some(&inventory)), + CpuFacts { + model: Some("AMD EPYC 7763".to_owned()), + vendor: "amd".to_owned() + } ); } } diff --git a/crates/rocm-core/src/host_gpu/windows_inventory.rs b/crates/rocm-core/src/host_gpu/windows_inventory.rs index c1819726ff..02155e87eb 100644 --- a/crates/rocm-core/src/host_gpu/windows_inventory.rs +++ b/crates/rocm-core/src/host_gpu/windows_inventory.rs @@ -5,8 +5,6 @@ //! The Windows display-adapter inventory: the PowerShell queries, the parsers for //! their output, and the assembly of one inventory from several sources. -#[cfg(any(windows, test))] -use super::capture::OPTIONAL_COMMAND_TIMEOUT; #[cfg(windows)] use super::capture::{capture_optional_command, capture_optional_command_with_timeout}; use super::os_facts::{bytes_text_to_gib, normalize_cpu_model}; @@ -20,17 +18,20 @@ use std::time::Duration; pub(super) const WINDOWS_INVENTORY_QUERY_TIMEOUT: Duration = Duration::from_secs(5); -/// What the system query (CPU model and RAM) is allowed: the 1.5 s of an -/// optional command, not the 5 s the display queries get. -#[cfg(any(windows, test))] -const WINDOWS_SYSTEM_INVENTORY_TIMEOUT: Duration = OPTIONAL_COMMAND_TIMEOUT; +/// What the system query (CPU model and RAM) is allowed: the same 5 s every +/// other inventory query gets, for the summary's inventory and the +/// examination's CPU read alike. It was the 1.5 s of an optional command in +/// the summary and 8 s in the examination's own CPU query. One limit sits +/// between the two: a CIM query on a PowerShell that has not started yet can +/// outlast 1.5 s, so the summary's read waits longer than it did, and the +/// examination's gives up 3 s sooner. +const WINDOWS_SYSTEM_INVENTORY_TIMEOUT: Duration = WINDOWS_INVENTORY_QUERY_TIMEOUT; pub(super) const WINDOWS_VIDEO_CONTROLLER_INVENTORY_SCRIPT: &str = r#"$gpus = Get-CimInstance -ClassName Win32_VideoController -Property Name,DriverVersion,DriverDate,PNPDeviceID,AdapterCompatibility | Where-Object { $_.PNPDeviceID -match 'VEN_1002' -or $_.AdapterCompatibility -match 'AMD|Advanced Micro Devices' -or $_.Name -match 'AMD|Radeon|Instinct' }; foreach ($gpu in $gpus) { $date = ''; if ($gpu.DriverDate) { $date = $gpu.DriverDate.ToUniversalTime().ToString('yyyy-MM-dd', [System.Globalization.CultureInfo]::InvariantCulture) }; "GPU`t$($gpu.Name)`t$($gpu.DriverVersion)`t$($gpu.PNPDeviceID)`t$date" }"#; #[cfg(windows)] pub(super) const WINDOWS_PNP_ENTITY_INVENTORY_SCRIPT: &str = r#"$displayGuid = '{4d36e968-e325-11ce-bfc1-08002be10318}'; $gpus = Get-CimInstance -ClassName Win32_PnPEntity -Property Name,DeviceID,PNPClass,ClassGuid,Manufacturer | Where-Object { (($_.PNPClass -eq 'Display' -or $_.ClassGuid -eq $displayGuid) -and ($_.DeviceID -match 'VEN_1002' -or $_.Name -match 'AMD|Radeon|Instinct|Graphics' -or $_.Manufacturer -match 'AMD|Advanced Micro Devices')) -or ($_.DeviceID -match 'PCI\\VEN_1002' -and $_.Name -match 'Radeon|Instinct|Graphics') }; foreach ($gpu in $gpus) { "GPU`t$($gpu.Name)`t`t$($gpu.DeviceID)" }"#; -#[cfg(any(windows, test))] const WINDOWS_SYSTEM_INVENTORY_SCRIPT: &str = r#"$cpu = Get-CimInstance -ClassName Win32_Processor -Property Name | Select-Object -First 1 -ExpandProperty Name; if ($cpu) { "CPU`t$cpu" }; $ram = Get-CimInstance -ClassName Win32_ComputerSystem -Property TotalPhysicalMemory | Select-Object -First 1 -ExpandProperty TotalPhysicalMemory; if ($ram) { "RAM`t$ram" }"#; #[cfg(windows)] @@ -512,26 +513,37 @@ fn detect_windows_system_inventory_from_cim() -> Option if !runtime_is_windows() { return None; } - windows_system_inventory_with(|script, timeout| { - capture_optional_command_with_timeout( - "powershell", - &[ - "-NoLogo", - "-NoProfile", - "-NonInteractive", - "-Command", - script, - ], - timeout, - ) - }) + windows_system_inventory_with(run_windows_system_query) +} + +/// Run the system query's `script` under PowerShell for at most `timeout`, and +/// answer with its stdout, or `None` when it could not run, failed or timed out. +#[cfg(windows)] +pub(super) fn run_windows_system_query(script: &str, timeout: Duration) -> Option { + capture_optional_command_with_timeout( + "powershell", + &[ + "-NoLogo", + "-NoProfile", + "-NonInteractive", + "-Command", + script, + ], + timeout, + ) +} + +/// Off Windows there is no PowerShell to run the system query. +#[cfg(not(windows))] +pub(super) const fn run_windows_system_query(_script: &str, _timeout: Duration) -> Option { + None } /// The system inventory (CPU model and installed RAM) from one query, with the /// query injected so a test on any host can drive it. `query` is handed the -/// script and the timeout the inventory allows it, and answers with what the -/// script printed, or `None` when it could not run, failed or timed out. -#[cfg(any(windows, test))] +/// script and the time [`WINDOWS_SYSTEM_INVENTORY_TIMEOUT`] allows it, and +/// answers with what the script printed, or `None` when it could not run, +/// failed or timed out. pub(super) fn windows_system_inventory_with( query: impl FnOnce(&str, Duration) -> Option, ) -> Option { diff --git a/docs/architecture.md b/docs/architecture.md index 2c9377e771..2dc9d1f7a1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -189,12 +189,12 @@ Already-extracted subsystem modules, following the same full-domain-extraction p - [`download.rs`](../crates/rocm-core/src/download.rs) — streaming file downloads with resume and retry: `DownloadRequest`/`DownloadOutcome`, `download_file_streaming(_with_progress)` and the simpler `download_file_to_path(_with_progress)`, `Backoff`, and the retry policy (`DOWNLOAD_MAX_ATTEMPTS`). A download streams to a per-process `.part-` sibling, resumes with a `Range` request, and enforces the caller's size limit, expected length and SHA-256 digest before publishing the file. It uses `ureq`, not the loopback client in [`net.rs`](../crates/rocm-core/src/net.rs), and depends only on [`disk_space.rs`](../crates/rocm-core/src/disk_space.rs). The module is private and [`lib.rs`](../crates/rocm-core/src/lib.rs) re-exports its public names. - [`endpoint_key.rs`](../crates/rocm-core/src/endpoint_key.rs) — (ROCMAI-662) the bearer key that guards a managed service's endpoint: the CSPRNG-drawn generator and its header-safety check. Split out of [`service_identity.rs`](../crates/rocm-core/src/service_identity.rs): an id and its endpoint key are made by unrelated mechanisms (readable/timestamp-based vs. CSPRNG) and meet only at the filesystem layer, where the key's sidecar is named after the id — not a reason to keep the two in one module. - [`endpoint_readiness.rs`](../crates/rocm-core/src/endpoint_readiness.rs) — how far along a managed service's OpenAI-compatible endpoint is: `managed_service_endpoint_readiness` and `EndpointReadiness` (unreachable, listing the model, or serving a real single-token chat request), the lower-level `openai_chat_completion_probe`/`openai_chat_completion_status`/`openai_models_endpoint_has_model`, the shared inference-verified latch the engine adapters call (`engine_state_inference_verified`, `merge_json_state_file`) and the model-reference matching behind the model-listing check. It depends on [`net.rs`](../crates/rocm-core/src/net.rs), [`service_record.rs`](../crates/rocm-core/src/service_record.rs) and [`model_catalog.rs`](../crates/rocm-core/src/model_catalog.rs); [`net.rs`](../crates/rocm-core/src/net.rs) and [`service_record.rs`](../crates/rocm-core/src/service_record.rs) do not depend on it. The module is private and [`lib.rs`](../crates/rocm-core/src/lib.rs) re-exports its public names, so callers keep using `rocm_core::` paths. -- [`examine.rs`](../crates/rocm-core/src/examine.rs) — the host examination probe: the `Examination` document and its types, and `Examination::probe`, which runs the probes in order. The probes live in child modules under `examine/`, with `examine.rs` kept as the module root so the [`examine_driver_proptests.rs`](../crates/rocm-core/src/examine_driver_proptests.rs) `#[path]` attribute and the links to it stay valid: [`linux_gpus.rs`](../crates/rocm-core/src/examine/linux_gpus.rs) (Linux GPU enumeration from `lspci`, `rocminfo` and the kernel topology, with the marketing-name and APU classification they share), [`linux_driver.rs`](../crates/rocm-core/src/examine/linux_driver.rs) (kernel modules, device-node access and Secure Boot), [`rocm_install_probe.rs`](../crates/rocm-core/src/examine/rocm_install_probe.rs) (how ROCm was installed and which repository files configure it), [`library_copies.rs`](../crates/rocm-core/src/examine/library_copies.rs) (the loader library-copy search for comgr and the HIP runtime, and `comgr_matches_runtime`, which `diagnose/engine.rs` reads), [`framework.rs`](../crates/rocm-core/src/examine/framework.rs) (the PyTorch and llama.cpp probes), [`host_env.rs`](../crates/rocm-core/src/examine/host_env.rs) (the OS and distro, CPU, tracked environment variables, shared memory, container check and kernel log), [`wsl.rs`](../crates/rocm-core/src/examine/wsl.rs) (the WSL probe, including the remote probe that reads a named distribution from the Windows host, and the distro-floor check) and [`windows.rs`](../crates/rocm-core/src/examine/windows.rs) (the native Windows probes: the GPU query, the HIP SDK, the Adrenalin driver and the MSVC redistributable, plus `parse_dotted_version`, which `diagnose/windows.rs` reads). +- [`examine.rs`](../crates/rocm-core/src/examine.rs) — the host examination probe: the `Examination` document and its types, and `Examination::probe`, which runs the probes in order. The probes live in child modules under `examine/`, with `examine.rs` kept as the module root so the [`examine_driver_proptests.rs`](../crates/rocm-core/src/examine_driver_proptests.rs) `#[path]` attribute and the links to it stay valid: [`linux_gpus.rs`](../crates/rocm-core/src/examine/linux_gpus.rs) (Linux GPU enumeration from `lspci`, `rocminfo` and the kernel topology, with the marketing-name and APU classification they share), [`linux_driver.rs`](../crates/rocm-core/src/examine/linux_driver.rs) (kernel modules, device-node access and Secure Boot), [`rocm_install_probe.rs`](../crates/rocm-core/src/examine/rocm_install_probe.rs) (how ROCm was installed and which repository files configure it), [`library_copies.rs`](../crates/rocm-core/src/examine/library_copies.rs) (the loader library-copy search for comgr and the HIP runtime, and `comgr_matches_runtime`, which `diagnose/engine.rs` reads), [`framework.rs`](../crates/rocm-core/src/examine/framework.rs) (the PyTorch and llama.cpp probes), [`host_env.rs`](../crates/rocm-core/src/examine/host_env.rs) (the OS and distro, the CPU fields recorded from the shared reader in `host_gpu/os_facts.rs`, tracked environment variables, shared memory, container check and kernel log), [`wsl.rs`](../crates/rocm-core/src/examine/wsl.rs) (the WSL probe, including the remote probe that reads a named distribution from the Windows host, and the distro-floor check) and [`windows.rs`](../crates/rocm-core/src/examine/windows.rs) (the native Windows probes: the GPU query, the HIP SDK, the Adrenalin driver and the MSVC redistributable, plus `parse_dotted_version`, which `diagnose/windows.rs` reads). - [`gfx_target.rs`](../crates/rocm-core/src/gfx_target.rs) — gfx-target identification and the TheRock package-family tables, split out of [`host_gpu.rs`](../crates/rocm-core/src/host_gpu.rs) so [`managed_runtime.rs`](../crates/rocm-core/src/managed_runtime.rs) can read the family mapping without importing the module that probes the host: `extract_first_gfx_token`, `normalize_therock_family`/`known_therock_families`, `gfx_target_has_no_rocm_build`, the AMD PCI-device-id and marketing-name tables behind `gfx_target_from_pnp_device_id`, and the parsers for the Windows display text and the kernel's gfx-target version. The module is private; `lib.rs` re-exports four of the public names (all but `gfx_target_from_pnp_device_id`) and `host_gpu.rs`, as before, re-exports all five. - [`gguf.rs`](../crates/rocm-core/src/gguf.rs) — reads the shape of a GGUF model (layers, KV heads, head width, parameter count, data offset) from its header over any `Read + Seek`, never touching the weights. - [`gguf_fit.rs`](../crates/rocm-core/src/gguf_fit.rs) — pure arithmetic over that header: weights + KV cache at a context size and KV type + compute buffer + runtime reserve against dedicated VRAM, the largest context that fits, and what would make a model that does not fit, fit. - [`gguf_source.rs`](../crates/rocm-core/src/gguf_source.rs) — where a header comes from: a local file, or a Hugging Face file, found by one repository-listing request (and a `HEAD` request for a file the listing does not size) and read by HTTP range requests (`HF_ENDPOINT` selects a mirror; redirects are followed by hand, up to five, and never from https to plain http). The only code in the estimate that reads a file or the network. -- [`host_gpu.rs`](../crates/rocm-core/src/host_gpu.rs) — (ROCMAI-85, Phase 7a of ROCMAI-27's sequencing) GPU/host hardware detection — see the module's own doc comment for what it owns. Its leaf concerns are child modules under `host_gpu/`, with `host_gpu.rs` kept as the module root: [`capture.rs`](../crates/rocm-core/src/host_gpu/capture.rs) (running an optional tool under a timeout, trying each spelling [`probe_exec.rs`](../crates/rocm-core/src/probe_exec.rs) gives for it), [`os_facts.rs`](../crates/rocm-core/src/host_gpu/os_facts.rs) (kernel version, distro name, CPU model and installed RAM), [`linux.rs`](../crates/rocm-core/src/host_gpu/linux.rs) (the sysfs, KFD and DRM probes), [`gpu_visibility.rs`](../crates/rocm-core/src/host_gpu/gpu_visibility.rs) (which AMD GPUs this process may use, from the KFD/DRM counts and the `HIP_VISIBLE_DEVICES`/`ROCR_VISIBLE_DEVICES` masks, plus `windows_amd_gpu_listing`, the native-Windows adapter bound `rocm serve --gpu ` is checked against), [`windows_inventory.rs`](../crates/rocm-core/src/host_gpu/windows_inventory.rs) (the Windows display-adapter inventory and its parsers, including the AMD-adapter count behind `windows_amd_gpu_listing`), [`wsl.rs`](../crates/rocm-core/src/host_gpu/wsl.rs) (WSL detection, the librocdxg chain and the Windows host driver as seen from the guest), [`render.rs`](../crates/rocm-core/src/host_gpu/render.rs) (the `rocm examine` text of an `ExamineSummary`, as a second `impl` block), [`diagnostics.rs`](../crates/rocm-core/src/host_gpu/diagnostics.rs) (the GPU detection diagnostics text) and [`engine_default.rs`](../crates/rocm-core/src/host_gpu/engine_default.rs) (the default serve engine, and the vLLM preference for the TheRock families that serve through it). +- [`host_gpu.rs`](../crates/rocm-core/src/host_gpu.rs) — (ROCMAI-85, Phase 7a of ROCMAI-27's sequencing) GPU/host hardware detection — see the module's own doc comment for what it owns. Its leaf concerns are child modules under `host_gpu/`, with `host_gpu.rs` kept as the module root: [`capture.rs`](../crates/rocm-core/src/host_gpu/capture.rs) (running an optional tool under a timeout, trying each spelling [`probe_exec.rs`](../crates/rocm-core/src/probe_exec.rs) gives for it), [`os_facts.rs`](../crates/rocm-core/src/host_gpu/os_facts.rs) (kernel version, distro name, installed RAM, and the CPU model and vendor, which one reader returns together for both the summary and `Examination::probe`: Linux from `/proc/cpuinfo`, Windows from the CPU row of the system inventory), [`linux.rs`](../crates/rocm-core/src/host_gpu/linux.rs) (the sysfs, KFD and DRM probes), [`gpu_visibility.rs`](../crates/rocm-core/src/host_gpu/gpu_visibility.rs) (which AMD GPUs this process may use, from the KFD/DRM counts and the `HIP_VISIBLE_DEVICES`/`ROCR_VISIBLE_DEVICES` masks, plus `windows_amd_gpu_listing`, the native-Windows adapter bound `rocm serve --gpu ` is checked against), [`windows_inventory.rs`](../crates/rocm-core/src/host_gpu/windows_inventory.rs) (the Windows display-adapter inventory and its parsers, including the AMD-adapter count behind `windows_amd_gpu_listing` and the system query whose CPU row the CPU reader in `os_facts.rs` takes), [`wsl.rs`](../crates/rocm-core/src/host_gpu/wsl.rs) (WSL detection, the librocdxg chain and the Windows host driver as seen from the guest), [`render.rs`](../crates/rocm-core/src/host_gpu/render.rs) (the `rocm examine` text of an `ExamineSummary`, as a second `impl` block), [`diagnostics.rs`](../crates/rocm-core/src/host_gpu/diagnostics.rs) (the GPU detection diagnostics text) and [`engine_default.rs`](../crates/rocm-core/src/host_gpu/engine_default.rs) (the default serve engine, and the vLLM preference for the TheRock families that serve through it). - [`host-probing.md`](host-probing.md) — a design note, not a module: [`host_gpu.rs`](../crates/rocm-core/src/host_gpu.rs) and [`examine.rs`](../crates/rocm-core/src/examine.rs) are two separate host-probing stacks that read some of the same facts twice (kernel version, CPU model, the Windows GPU query, the marketing-name table and the subprocess runners). It proposes which of each pair should survive, how the two differ and the order in which they could converge, and records what each convergence decided; it changes no code itself. The `PATH` lookup is the first pair converged: [`probe_exec.rs`](../crates/rocm-core/src/probe_exec.rs)'s `which` is now the only copy. It is a snapshot, to be retired when the convergence tickets close. - [`jsonl_store.rs`](../crates/rocm-core/src/jsonl_store.rs) — the JSONL store engine behind the automation stores: the sibling `.lock` file (`jsonl_lock_path`, re-exported at the crate root), single-line appends, torn-tail repair, size-based rotation (`JsonlRotation`) and the tail-first readers that span rotated generations. It is generic over the record type; [`automation.rs`](../crates/rocm-core/src/automation.rs) supplies the record types and each store's policy, and nothing in this module depends on it, in production or in its own tests: those drive the engine with a small record type of their own. The module is private; its engine items are `pub(crate)`. - [`managed_runtime.rs`](../crates/rocm-core/src/managed_runtime.rs) — (ROCMAI-85, Phase 7a) the TheRock managed-runtime-environment surface — see the module's own doc comment for what it owns. From d4b383805f43230870f46697e791729531076c6e Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Mon, 9 Nov 2026 19:17:38 +0000 Subject: [PATCH 4/7] test(e2e): hold summary.cpu and cpu_model level in one examine run rocm examine --json reads the CPU in the host summary (summary.cpu, shown as cpu: in the text) and in the examination (cpu_model and cpu_vendor), which now share one reader. examine-39 plants a /proc/cpuinfo under a simulated host root that names an AMD CPU with runs of spaces in its model, on a machine that is not WSL, and asserts both reports carry the same collapsed model and the examination reports the vendor as amd. examine-40 plants an Arm-style cpuinfo with only a Hardware line and asserts both reports name that value and the vendor stays unknown. examine-41 plants the same CPU on a WSL machine and pins the difference this change leaves as it is: the summary reports the CPU, the examination reports no model and an unknown vendor. The comparison is scoped to hosts where is_wsl is false, and each scenario asserts the specific values and the is_wsl flag it is scoped by. Against the code before the reader was merged, examine-39 fails on the examination's spaces and examine-40 on its empty model; examine-41 passes before and after. Native Windows reads the CPU through the system inventory and cannot run on this lane, so its cases are the unit tests next to the reader. Signed-off-by: Jussi Elo --- tests/e2e-cucumber/features/examine.feature | 41 ++++++ tests/e2e-cucumber/tests/e2e/examine_steps.rs | 123 ++++++++++++++++++ 2 files changed, 164 insertions(+) diff --git a/tests/e2e-cucumber/features/examine.feature b/tests/e2e-cucumber/features/examine.feature index 7620e2d04a..23cc814540 100644 --- a/tests/e2e-cucumber/features/examine.feature +++ b/tests/e2e-cucumber/features/examine.feature @@ -598,3 +598,44 @@ Feature: GPU detection and system inspection Given a WSL machine whose only rocminfo is in the directory the command runs from, with that directory named on PATH When the user inspects that machine in machine-readable form Then both reports say rocminfo is installed + + # `rocm examine --json` reads the CPU twice: the host summary (`summary.cpu`, + # which the text form prints as `cpu:`) and the examination (`cpu_model` and + # `cpu_vendor`). They used to read `/proc/cpuinfo` separately and spelled the + # same model differently; they share one reader now, so one `--json` run holds + # the two level on a specific value, not only on both being filled. + # + # The planted machine is a simulated host root holding only a `/proc/cpuinfo`, + # so the scenario describes a CPU no runner has, and it is not WSL whatever the + # runner is: the comparison is scoped to hosts where `is_wsl` is false, because + # on WSL the examination does not read the CPU at all (`examine-41`). The model + # in `/proc/cpuinfo` has repeated spaces, which the examination used to keep and + # the summary to collapse. Native Windows reads the same CPU through the system + # inventory and cannot run on this lane; the unit tests in + # `crates/rocm-core/src/host_gpu/os_facts.rs` cover it. + @id:examine-reports-one-cpu-in-both-forms @requires-os:linux + Scenario: examine-39 - The summary and the examination report the same CPU model and its vendor + Given a machine that is not WSL whose cpuinfo names an AMD CPU with repeated spaces in its model + When the user inspects that machine in machine-readable form + Then the summary and the examination report that CPU model, and the examination reports its vendor as amd + + # A board whose cpuinfo has a `Hardware` line and no `model name` (an Arm board) + # showed a CPU in the summary and none in the examination. Both read the + # `Hardware` line now, and the vendor stays unknown because such a file names + # none. + @id:examine-reports-a-hardware-line-cpu-in-both-forms @requires-os:linux + Scenario: examine-40 - Both reports name a CPU that cpuinfo gives only as Hardware + Given a machine that is not WSL whose cpuinfo has a Hardware line and no model name + When the user inspects that machine in machine-readable form + Then the summary and the examination report that CPU model, and the examination reports its vendor as unknown + + # The difference this change leaves as it is: on WSL the examination returns + # before it reads the CPU, so `cpu_model` is empty and `cpu_vendor` is + # `unknown` while the summary shows the CPU. Pinned so that collecting CPU + # facts on WSL is a visible change of its own, which would turn this scenario. + # The other two scenarios compare the two reports on hosts that are not WSL. + @id:examine-reports-no-cpu-model-on-wsl-where-the-summary-has-one @requires-os:linux + Scenario: examine-41 - On WSL the summary reports the CPU and the examination does not + Given a WSL machine whose cpuinfo names an AMD CPU + When the user inspects that machine in machine-readable form + Then the summary reports that CPU model, and the examination reports no CPU model and an unknown vendor diff --git a/tests/e2e-cucumber/tests/e2e/examine_steps.rs b/tests/e2e-cucumber/tests/e2e/examine_steps.rs index 8d8af1abea..513b48df88 100644 --- a/tests/e2e-cucumber/tests/e2e/examine_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/examine_steps.rs @@ -2544,6 +2544,129 @@ async fn assert_rocminfo_not_installed_in_either_report(world: &mut E2eWorld) { ); } +// ── Both reports read the CPU from one reader ────────────────────── + +/// The model a planted AMD `/proc/cpuinfo` names, spelled as `/proc/cpuinfo` +/// does, with runs of spaces inside it. +const PLANTED_AMD_CPUINFO: &str = "processor\t: 0\nvendor_id\t: AuthenticAMD\ncpu family\t: 26\nmodel name\t: AMD Ryzen 9 9950X 16-Core Processor \nprocessor\t: 1\nvendor_id\t: AuthenticAMD\nmodel name\t: AMD Ryzen 9 9950X 16-Core Processor \n"; + +/// The same model with its whitespace collapsed: what both reports show. +const PLANTED_AMD_CPU_MODEL: &str = "AMD Ryzen 9 9950X 16-Core Processor"; + +/// An Arm board's cpuinfo: a `Hardware` line, no `model name` and no `vendor_id`. +const PLANTED_HARDWARE_ONLY_CPUINFO: &str = + "processor\t: 0\nBogoMIPS\t: 38.40\nCPU implementer\t: 0x41\nHardware\t: BCM2835\n"; + +/// Plant `cpuinfo` as `/proc/cpuinfo` under a simulated host root inside the +/// scenario's isolated tree, with a `/dev/dxg` when `wsl`, and hand that root to +/// the next `rocm` invocation. The root holds nothing else, so the machine reads +/// as WSL only when asked to and every other re-rooted probe sees an empty one. +fn plant_cpuinfo_machine(world: &mut E2eWorld, cpuinfo: &str, wsl: bool) { + let root = world + .isolated_root + .as_ref() + .expect("no isolated root") + .path() + .join("host-root"); + std::fs::create_dir_all(root.join("proc")).expect("failed to create planted /proc"); + std::fs::write(root.join("proc").join("cpuinfo"), cpuinfo) + .expect("failed to write planted cpuinfo"); + if wsl { + std::fs::create_dir_all(root.join("dev")).expect("failed to create planted /dev"); + std::fs::write(root.join("dev").join("dxg"), "").expect("failed to plant /dev/dxg"); + } + world + .command_env + .push(("ROCM_CLI_TEST_HOST_ROOT", root.into_os_string())); +} + +#[given( + "a machine that is not WSL whose cpuinfo names an AMD CPU with repeated spaces in its model" +)] +async fn machine_with_an_amd_cpuinfo(world: &mut E2eWorld) { + plant_cpuinfo_machine(world, PLANTED_AMD_CPUINFO, false); +} + +#[given("a machine that is not WSL whose cpuinfo has a Hardware line and no model name")] +async fn machine_with_a_hardware_only_cpuinfo(world: &mut E2eWorld) { + plant_cpuinfo_machine(world, PLANTED_HARDWARE_ONLY_CPUINFO, false); +} + +#[given("a WSL machine whose cpuinfo names an AMD CPU")] +async fn wsl_machine_with_an_amd_cpuinfo(world: &mut E2eWorld) { + plant_cpuinfo_machine(world, PLANTED_AMD_CPUINFO, true); +} + +/// `summary.cpu`, `cpu_model`, `cpu_vendor` and `is_wsl` of one `rocm examine +/// --json` run. +fn cpu_answers(world: &E2eWorld) -> (Option, String, String, bool) { + assert_eq!(world.cli_rc, Some(0), "`rocm examine --json` failed"); + let json = parsed_json(world); + let summary = json + .pointer("/summary/cpu") + .unwrap_or_else(|| panic!("`examine --json` has no summary.cpu:\n{json:#}")) + .as_str() + .map(str::to_owned); + let is_wsl = json + .pointer("/is_wsl") + .and_then(serde_json::Value::as_bool) + .unwrap_or_else(|| panic!("`examine --json` has no boolean is_wsl:\n{json:#}")); + ( + summary, + inspection_str(&json, "/cpu_model").to_owned(), + inspection_str(&json, "/cpu_vendor").to_owned(), + is_wsl, + ) +} + +#[then( + "the summary and the examination report that CPU model, and the examination reports its vendor as amd" +)] +async fn assert_both_report_the_planted_amd_cpu(world: &mut E2eWorld) { + assert_eq!( + cpu_answers(world), + ( + Some(PLANTED_AMD_CPU_MODEL.to_owned()), + PLANTED_AMD_CPU_MODEL.to_owned(), + "amd".to_owned(), + false + ), + "(summary.cpu, cpu_model, cpu_vendor, is_wsl)" + ); +} + +#[then( + "the summary and the examination report that CPU model, and the examination reports its vendor as unknown" +)] +async fn assert_both_report_the_hardware_line_cpu(world: &mut E2eWorld) { + assert_eq!( + cpu_answers(world), + ( + Some("BCM2835".to_owned()), + "BCM2835".to_owned(), + "unknown".to_owned(), + false + ), + "(summary.cpu, cpu_model, cpu_vendor, is_wsl)" + ); +} + +#[then( + "the summary reports that CPU model, and the examination reports no CPU model and an unknown vendor" +)] +async fn assert_only_the_summary_reports_the_cpu_on_wsl(world: &mut E2eWorld) { + assert_eq!( + cpu_answers(world), + ( + Some(PLANTED_AMD_CPU_MODEL.to_owned()), + String::new(), + "unknown".to_owned(), + true + ), + "(summary.cpu, cpu_model, cpu_vendor, is_wsl)" + ); +} + // ── `rocm serve` on a WSL host whose ROCDXG library the loader cannot find ── #[when("the user serves a model under the GPU-required default on that machine")] From 66c80ffbbbea79d1244942974570103f9aaa3a3e Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Mon, 9 Nov 2026 19:29:38 +0000 Subject: [PATCH 5/7] docs: record the CPU-model decisions in the host-probing note The CPU model is the third pair of the note to converge. Record what survived (one reader returning model and vendor, called by both probe functions and read by the summary), WSL option (a), the five decisions (the whitespace collapse reaches cpu_model, a Hardware line is the model when no model name comes first, a CPU that could not be read stays an absent model in the summary and an empty one in the examination, one 5 s limit for the Windows query, and the summary's own Windows fallback query dropped), the answers that moved with the test or scenario that pins each, the two differences that come with the optional-command helper and cannot be pinned off Windows, and what stays untested. The analysis of the old copies stays as the record of what changed; the timeout question leaves the open list for the CPU and two new questions join it. Signed-off-by: Jussi Elo --- docs/host-probing.md | 51 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/docs/host-probing.md b/docs/host-probing.md index 057f5517e0..5fb792fd58 100644 --- a/docs/host-probing.md +++ b/docs/host-probing.md @@ -6,7 +6,7 @@ SPDX-License-Identifier: MIT # Host probing: which stack owns each fact -Status: proposal, with the first pair delivered. This note changes no code and no behaviour itself, and what it proposes for a pair is decided by the change that converges that pair, which then records the decision here (the PATH lookup, pair 1, has: see "PATH lookup"). It lists the host facts that `rocm-core` reads twice (on the hosts each is read on, see "Which host reads which fact"), says how the two reads differ, and proposes which one should survive and in what order a later series of changes could remove the other. Each pair is meant to be a separate change with its own review. +Status: proposal, with the first and third pairs delivered. This note changes no code and no behaviour itself, and what it proposes for a pair is decided by the change that converges that pair, which then records the decision here (the PATH lookup, pair 1, and the CPU model, pair 3, have: see "PATH lookup" and "CPU model"). It lists the host facts that `rocm-core` reads twice (on the hosts each is read on, see "Which host reads which fact"), says how the two reads differ, and proposes which one should survive and in what order a later series of changes could remove the other. Each pair is meant to be a separate change with its own review. This note is a snapshot of `main` at the time of writing, not maintained documentation. It is to be retired (the file deleted, together with its entry in [architecture.md](architecture.md)) when the convergence tickets for these pairs close, so the inventory does not keep pointing at a stale design record. @@ -19,7 +19,7 @@ For each pair, the proposed survivor and the biggest difference a user can see t | Pair | Proposed survivor | Biggest user-visible difference today | |---|---|---| | [Kernel version](#kernel-version) | `detect_kernel_version`, called by `probe_os` for `kernel_release` on Linux (the timeout is open) | where `uname` is slow or fails, `kernel:` and `kernel_release` can disagree (UNCONFIRMED); on Windows `kernel_release` is always empty | -| [CPU model](#cpu-model) | one reader in `os_facts` returning model and vendor, called by `probe_cpu_linux` and `probe_cpu_windows` | on WSL the text shows the CPU while `cpu_model` is empty and `cpu_vendor` is `unknown`; the change leaves that as it is (option (a)) | +| [CPU model](#cpu-model) | delivered: one reader in `os_facts` returning model and vendor, called by `probe_cpu_linux` and `probe_cpu_windows` and read by the summary | before the change, the same model could be spelled two ways in one `--json` run and a `Hardware`-only board had a CPU in the text and none in `cpu_model`; now one answer, the differences it moved are listed under "CPU model". On WSL the text still shows the CPU while `cpu_model` is empty and `cpu_vendor` is `unknown` (option (a), unchanged) | | [Windows GPU query](#windows-gpu-query) | the summary stack's inventory, with `probe_gpus_windows` building `gpus[]` from it and NVIDIA kept in the JSON | an NVIDIA-only host reads "AMD GPU not detected yet" in the text beside `has_nvidia_gpu: true` in the JSON | | [Marketing name to gfx target](#marketing-name-to-gfx-target) | the `AMD_MARKETING_GFX_TARGETS` table, gaining the codenames `classify_amd_marketing_name` knows (finish [#521](https://github.com/ROCm/rocm-cli/pull/521)) | on Windows without a HIP SDK an RX 7900 XTX has an empty `gpus[].gfx_target` while `summary.detected_gfx_target` is `gfx1100` | | [PATH lookup](#path-lookup) | delivered: the behaviour of `tool_on_path`, housed in `probe_exec.rs` under the name `which`; an empty `PATH` entry is skipped | before the change, an empty `PATH` entry or a `PATH` that is not UTF-8 changed which tools read as present (for example `wsl.rocminfo`); now there is one answer, and the differences it moved are listed under "PATH lookup" | @@ -47,7 +47,7 @@ Every pair below is read on a subset of the hosts, and the subset decides which | Fact | Native Linux | WSL (probing inside the distro) | Native Windows | Windows host inspecting a WSL distro (`probe_wsl_distro_from_host`) | |---|---|---|---|---| | Kernel version | both, `uname -r` | both: `probe_os` runs before the WSL branch returns | summary `kernel` from `cmd /C ver`; `kernel_release` stays empty | examination only, from `uname -r` run inside the distro by a shell script (`WSL_REMOTE_PROBE`), a third implementation | -| CPU model and vendor | both | summary only: the examination returns before `probe_cpu_linux`, so `cpu_model` is empty and `cpu_vendor` is `unknown` | both, plus the inventory row | neither: `cpu_model` empty | +| CPU model and vendor | both, through one reader | summary only: the examination returns before `probe_cpu_linux`, so `cpu_model` is empty and `cpu_vendor` is `unknown` (unchanged by the CPU-model change, option (a)) | both, each running the system inventory's query, the examination through the reader and the summary through the inventory it holds | neither: `cpu_model` empty | | Windows adapter list | not applicable | examination `gpus[]` stays empty (the WSL branch skips the GPU probes); the summary asks the Windows host for the display target only | both | not collected here; the host driver is read through the summary's inventory (`detect_local_windows_host_driver`) | | Marketing name to gfx target | examination only (the `lspci` scan); the summary reads sysfs and KFD, not names | summary only, through the Windows host's display name | both | not collected | | PATH lookup | `which` | `which`, asked by both stacks for `rocminfo` (before the change the summary asked `tool_on_path`, a second copy) | `which` | `which` for `wsl.exe` only | @@ -75,7 +75,7 @@ Proposal: `detect_kernel_version` survives and `probe_os` calls it for `kernel_r ## CPU model -Verdict: confirmed duplicate on native Linux and Windows, and on Windows three implementations. On WSL it is not a duplicate: only the summary reads the CPU there (see the table above and the paragraph on WSL below). The three Windows implementations are `detect_cpu_model` (its own PowerShell call), the `CPU` row of `WINDOWS_SYSTEM_INVENTORY_SCRIPT` in [windows_inventory.rs](../crates/rocm-core/src/host_gpu/windows_inventory.rs), and `probe_cpu_windows`. They are not three reads of one run in the usual case: the summary reads the inventory row first and calls `detect_cpu_model` only when no inventory exists, so a host whose inventory answers is read twice in `rocm examine --json` and one whose inventory does not answer is read three times (derived from `detect_cpu_model_with_windows_inventory` in [host_gpu.rs](../crates/rocm-core/src/host_gpu.rs); UNCONFIRMED on a live host). +Verdict: confirmed duplicate on native Linux and Windows, and on Windows three implementations, now converged (see "Decided and delivered" below); the comparison that follows is how the copies differed before that change, kept as the record of what it moved. On WSL it is not a duplicate: only the summary reads the CPU there (see the table above and the paragraph on WSL below). The three Windows implementations are `detect_cpu_model` (its own PowerShell call), the `CPU` row of `WINDOWS_SYSTEM_INVENTORY_SCRIPT` in [windows_inventory.rs](../crates/rocm-core/src/host_gpu/windows_inventory.rs), and `probe_cpu_windows`. They are not three reads of one run in the usual case: the summary reads the inventory row first and calls `detect_cpu_model` only when no inventory exists, so a host whose inventory answers is read twice in `rocm examine --json` and one whose inventory does not answer is read three times (derived from `detect_cpu_model_with_windows_inventory` in [host_gpu.rs](../crates/rocm-core/src/host_gpu.rs); UNCONFIRMED on a live host). | | Summary | Examination | |---|---|---| @@ -90,10 +90,42 @@ What a user can see on WSL: the text and `summary.cpu` show the CPU, while `cpu_ What a user can see on native Linux and Windows: a board whose cpuinfo has `Hardware` but no `model name` shows a CPU in the text and an empty `cpu_model` with vendor `unknown` in the JSON. A model string with repeated spaces inside it differs between the two forms (UNCONFIRMED which real CPUs report one; the code path differs). A Windows host where the 1.5 s query times out shows `cpu: ` beside a populated `cpu_model` (UNCONFIRMED, timing). No diagnosis checker reads `cpu_model` or `cpu_vendor` today (verified by grep); they reach Doctor reports and the JSON only. -Proposal: one reader in `os_facts` that returns the model and the vendor together (Linux from cpuinfo with the `Hardware` fallback and `vendor_id`; Windows from the inventory row, deriving the vendor from the model as `probe_cpu_windows` does). `probe_cpu_linux` and `probe_cpu_windows` call it. Open: whether the whitespace collapse should reach `cpu_model` in Doctor reports. +Proposal: one reader in `os_facts` that returns the model and the vendor together (Linux from cpuinfo with the `Hardware` fallback and `vendor_id`; Windows from the inventory row, deriving the vendor from the model as `probe_cpu_windows` does). `probe_cpu_linux` and `probe_cpu_windows` call it. The open question here, whether the whitespace collapse should reach `cpu_model` in Doctor reports, is decided under "Decided and delivered". This change does not reach WSL. The early return stays, so on WSL `cpu_model` stays empty and a comparison of `summary.cpu` with `cpu_model` fails there before and after. There are two ways to handle that, and the change has to pick one and say so in its PR text. Either (a) the comparison, in the characterisation test and in the scenario, is scoped to hosts where `is_wsl` is false, and the empty WSL fields are pinned as an existing difference; or (b) collecting CPU facts on WSL becomes an explicit behaviour change: the WSL branch calls the reader too, `cpu_model` and `cpu_vendor` appear in the WSL JSON and in Doctor reports, and the Windows-hosted distro probe needs its own answer (its `WSL_REMOTE_PROBE` shell script would have to print the CPU, since the reader runs on the host, not in the distro). The note recommends (a) for the convergence change and (b), if wanted, as its own change, because (b) adds a populated field to a document that consumers read today as empty. Hosts the characterisation test covers: native Linux from a fixture `/proc/cpuinfo` through `host_path` (`model name` present, `Hardware` only, repeated spaces, no `vendor_id`), WSL (a simulated host root, pinning the summary populated and the examination empty), and Windows through an injectable runner (inventory row present, absent, query failing, query timing out, empty output). +### Decided and delivered + +The change took the survivor proposed above. `read_cpu_facts` in [os_facts.rs](../crates/rocm-core/src/host_gpu/os_facts.rs) returns a `CpuFacts` (the model, or none, and the vendor): on Linux `read_linux_cpu_facts` reads `/proc/cpuinfo` through `host_path` and `cpu_facts_from_cpuinfo` takes the first `model name` or `Hardware` line with a value and the first `vendor_id` with a value, and on Windows `read_windows_cpu_facts` runs the CPU-and-RAM query of the system inventory (`windows_system_inventory_with` in [windows_inventory.rs](../crates/rocm-core/src/host_gpu/windows_inventory.rs)) and `cpu_facts_from_windows_inventory` takes its CPU row, deriving the vendor from the model string (`amd` or `intel`, else `unknown`) as `probe_cpu_windows` did. `probe_cpu_linux` and `probe_cpu_windows` fill `cpu_model` and `cpu_vendor` from it, `detect_cpu_model` is its model, and the summary on Windows reads the CPU row of the inventory it already holds (`summary_cpu` in [host_gpu.rs](../crates/rocm-core/src/host_gpu.rs)), which is the same row through the same function. Both PowerShell `Win32_Processor` queries that were separate from the inventory are gone. The injectable query (`windows_cpu_facts_with`, and `windows_system_inventory_with` under it) is what runs the Windows logic in a unit test on the Linux and Windows jobs. + +WSL is option (a): the early return stays, so on WSL `cpu_model` is empty and `cpu_vendor` is `unknown` while the summary shows the CPU, exactly as before. The comparison of `summary.cpu` with `cpu_model` in the scenarios is scoped to hosts where `is_wsl` is false, and the empty WSL fields are pinned as an existing difference by `on_wsl_the_summary_reads_the_cpu_and_the_examination_does_not` and scenario `examine-41`, so collecting CPU facts on WSL (option (b)) is a visible change of its own, which turns both. + +The open questions are decided as follows. + +- Whitespace: the collapse reaches `cpu_model`, so the model is one string in both forms. Nothing diagnoses from `cpu_model` or `cpu_vendor` (grep of the crates), so the only places that see the change are the JSON and the Doctor reports; a consumer that matched the old padded spelling is the one that sees it. +- The `Hardware`-only case: the `Hardware` value is the model and the vendor stays `unknown`, because such a file names none. The model is the first line of either kind, as the summary always had it, so with `Hardware` before `model name` the examination now reads the `Hardware` value where it read the `model name`; real cpuinfo prints `Hardware` after the per-core blocks, so that order is a fixture, not a host this note knows of. +- Empty versus unknown: a CPU that could not be read is no model (shown `` in the text, `null` in `summary.cpu`) in the summary and an empty `cpu_model` with `cpu_vendor` `unknown` in the examination, as before, and a vendor line with no value names no vendor. On Linux that is not a `probe_failures` entry, as before. On Windows the examination still records `Get-CimInstance Win32_Processor failed; cannot identify CPU.`, now for any read that gives no CPU (a query that failed, timed out, printed nothing or printed no CPU row), where it recorded it for a non-zero exit or blank output. +- The Windows timeout: one limit of 5 s, the inventory's own for every other query, for the one query both stacks run. The summary's system query was 1.5 s (and its own CPU query too) and the examination's was 8 s, so the summary now waits longer, for its RAM row as well, and the examination gives up 3 s sooner. The reason is that the examination runs the query first in a `--json` run, on a PowerShell that has not started yet, where 1.5 s may not be enough and a spurious empty `cpu_model` with a `probe_failures` entry costs more than waiting; 8 s was not taken for the shared query because it would also be the worst case of every startup path that gathers the summary on a host whose PowerShell hangs. Neither number was measured: UNCONFIRMED on a real Windows host. +- The inventory fallback: dropped. With no inventory at all, `detect_cpu_model` used to run a `Win32_Processor` query of its own, but an absent inventory means every source, the system query included, had just failed, so the second try repeated a failed query. The summary now shows `` there. Keeping it would have meant keeping a second Windows implementation, which is what this change removes. + +The differences that moved, each with the test that pins it (the unit tests are in `os_facts.rs`, `host_gpu.rs` and `host_env.rs`; the scenarios are in `examine.feature`): + +| Difference | Before | Now | Pinned by | +|---|---|---|---| +| `cpu_model` with repeated spaces inside the model | summary collapsed, examination trimmed only | collapsed in both (Linux and Windows) | `linux_cpuinfo_the_inputs_the_two_reads_used_to_disagree_on`, `windows_both_reads_collapse_whitespace_inside_the_name`, `cpu_reads_follow_the_simulated_host_root`, scenario `examine-39` | +| `Hardware` line and no `model name` | summary read it, examination had an empty `cpu_model` | both read it, vendor `unknown` | `linux_cpuinfo_the_inputs_the_two_reads_used_to_disagree_on`, scenario `examine-40` | +| `Hardware` before `model name` | summary read the `Hardware` value, examination the `model name` | both read the `Hardware` value | `linux_cpuinfo_the_inputs_the_two_reads_used_to_disagree_on` | +| An empty `vendor_id` value | examination took `""` as the vendor and stopped | not a vendor: the next `vendor_id` is read, else `unknown` | `linux_cpuinfo_the_inputs_the_two_reads_used_to_disagree_on` | +| Windows: the query the examination runs | `Win32_Processor` alone, through `run`, 8 s | the system inventory query (CPU and RAM), through the optional-command helper, 5 s | `windows_both_reads_are_given_the_same_time` | +| Windows: the summary's system query | 1.5 s | 5 s, for its CPU and RAM rows | `windows_both_reads_are_given_the_same_time` | +| Windows: no inventory | the summary ran a second CPU query | ``, no second query | `windows_an_answer_with_no_cpu_is_no_cpu_in_both_reads`, `the_summary_cpu_comes_from_the_windows_inventory_or_not_at_all` | +| Windows: an answer with no CPU in it | failure entry only for a non-zero exit or blank output | failure entry for any read with no CPU | `windows_an_answer_with_no_cpu_is_no_cpu_in_both_reads` | +| WSL | summary populated, examination empty | unchanged | `on_wsl_the_summary_reads_the_cpu_and_the_examination_does_not`, scenario `examine-41` | + +Two more differences come with the optional-command helper that the examination's Windows read now runs through, and neither is pinned by a test, because the helper is not injectable and needs a Windows host: it reads the output as strict UTF-8 where `run` decoded it lossily, so a CPU name that is not valid UTF-8 reads as no CPU, and it tries each spelling of `powershell` after a timeout where `run` started it once, so a hung PowerShell can cost the examination more than one limit (the candidate loop under "Subprocess runners"; it belongs to that pair, and the summary's inventory had the same behaviour already). + +What stays untested: the live Windows command line itself, which a unit test cannot start (the injected query stands in for it, and the script it is handed is the one the summary's inventory already ran); the one-line `runtime_is_windows()` argument of `summary_cpu` at its call site; and the timing claims above. The scenarios run on the Linux lane only (`@requires-os:linux`): the planted machines are simulated host roots, and native Windows cannot run them, so its CPU cases are the unit tests, which run on the Windows job as well. + ## Windows GPU query Verdict: confirmed duplicate, and wider than the ticket names: the summary side has several Windows adapter queries, all in [windows_inventory.rs](../crates/rocm-core/src/host_gpu/windows_inventory.rs), and the examination has one. @@ -206,12 +238,12 @@ One pair per change. Each begins with a commit that adds a characterisation test 1. PATH lookup (delivered: see "Decided and delivered" under "PATH lookup"). Smallest, and it settles where `tool_path_candidates` lives before the runner work needs it. It is not output-preserving: the differences listed under "PATH lookup" change which tools are reported as present, and its PR text lists each one as intentional, with the characterisation test that pins it. Hosts covered: Linux, which is also WSL's code path, for the empty entry, the non-UTF-8 `PATH` (set through an `OsString` environment value) and the current-directory file; Windows through an injectable resolver for the `PATHEXT` spellings, the System32 candidates and the `;` splitting, since the Linux lane cannot run them. 2. Kernel version. A single command; settles the timeout and the empty-versus-`None` question. Hosts covered as set out under "Kernel version". -3. CPU model. Adds the model-and-vendor reader; needs the Windows inventory row, which already exists. Its PR says which WSL option under "CPU model" it takes, (a) or (b); a PR that does not is not ready. +3. CPU model (delivered: see "Decided and delivered" under "CPU model"). Adds the model-and-vendor reader; needs the Windows inventory row, which already exists. Its PR says which WSL option under "CPU model" it takes, (a) or (b); a PR that does not is not ready. It took (a). Not output-preserving: the differences listed under "CPU model" change a model's spelling, a `Hardware`-only board's `cpu_model`, and the time the Windows reads wait. 4. Marketing name to gfx target, by finishing [#521](https://github.com/ROCm/rocm-cli/pull/521). It must land before the next item, so that `gpus[]` is not rebuilt on top of two name tables. 5. Windows GPU query. The riskiest: it needs an answer on NVIDIA and on adapter names, and a Windows lane. Scenario `examine-windows-driver-version-date-and-age` already runs there; a new scenario should compare the text's `detected_gfx_target` with the JSON `gpus[]` on a named adapter. Native Windows only; no scenario can run this on the Linux or WSL lanes. 6. Subprocess runners. Last, because items 1 to 5 reduce the number of call sites, but independent: the bounded-join fix in [#623](https://github.com/ROCm/rocm-cli/issues/623) does not wait for it and can land at any time. It has two intended differences, and the second is easy to miss because it needs no timeout: a probe behind a hung grandchild returns at its deadline instead of late (with possibly different output kept after a timeout), and a successful capture can keep less, because a descendant that writes more than `DRAIN_GRACE` (200 ms) after the child exited loses that later output where the older helpers waited for EOF. Each of the other successful-run differences listed under "Subprocess runners" needs its own line in the PR text. The characterisation test for this item comes first and must include late descendant output: a child that prints `early`, exits 0 and leaves a descendant that prints `late` after about 0.5 s, run through each of `run` (over `run_raw`, which `run_utf16le` shares), `capture_optional_command*` and `run_with_timeout`, pinning that the first two return both lines and `run_with_timeout` only `early`. Hosts covered: Linux and WSL with shell fixtures (the same shape the `process.rs` tests already use for the grace); `capture_diagnostic_command` and the Windows spellings are Windows only, so on the Windows job they get the injectable-function test of the adapter's result translation instead. UNCONFIRMED whether a Windows child can be arranged to leave a late-writing descendant in the test harness; if not, the PR says that path is untested and why. -Items 2 and 3 should add a scenario that compares `summary.kernel` with `kernel_release` and `summary.cpu` with `cpu_model` in one `--json` run, since no existing scenario does. Each comparison is scoped to the hosts where both sides are filled: kernel on native Linux and WSL, CPU on native Linux and Windows (option (a) under "CPU model"). The Linux lane can also drive a WSL variant with a simulated host root (`ROCM_CLI_TEST_HOST_ROOT` in an `e2e-test-hooks` build, with a `/dev/dxg`, a `/proc/version` that reads as WSL and a `/proc/cpuinfo`, as [hardware_root.rs](../crates/rocm-core/src/hardware_root.rs) describes), which pins the empty WSL CPU fields; `uname` still runs for real there, so a kernel value is compared rather than fixed. Assert the specific values, not only that both are non-empty. +Items 2 and 3 should add a scenario (item 3 did: `examine-39` to `examine-41`) that compares `summary.kernel` with `kernel_release` and `summary.cpu` with `cpu_model` in one `--json` run, since no existing scenario does. Each comparison is scoped to the hosts where both sides are filled: kernel on native Linux and WSL, CPU on native Linux and Windows (option (a) under "CPU model"). The Linux lane can also drive a WSL variant with a simulated host root (`ROCM_CLI_TEST_HOST_ROOT` in an `e2e-test-hooks` build, with a `/dev/dxg`, a `/proc/version` that reads as WSL and a `/proc/cpuinfo`, as [hardware_root.rs](../crates/rocm-core/src/hardware_root.rs) describes), which pins the empty WSL CPU fields; `uname` still runs for real there, so a kernel value is compared rather than fixed. Assert the specific values, not only that both are non-empty. No item is output-preserving without qualification, and the PR for each says which of its differences are intended instead of claiming nothing changed. Items 2 to 5 change visible output (a kernel or CPU string, a gfx target, an adapter name or vendor). Item 1 changes which tools are reported as present, per the list under item 1. Item 3 changes nothing on WSL under option (a). Item 6 changes timing and timed-out output, and the output of a successful capture in the late-descendant case; a user sees most of it as a probe that no longer hangs, and that case as a fact that can go missing from a probe that used to wait for it. For every listed difference the PR either adds a scenario that exercises it in `tests/e2e-cucumber/features/` or states why none can: the empty-entry and non-UTF-8 cases can be driven from a scenario that sets `PATH` and plants a file in the working directory, whereas the Windows candidate spellings cannot run on the Linux lane and need the injectable-function unit test on the Windows job (UNCONFIRMED that the harness can set a non-UTF-8 `PATH`; if it cannot, the PR says so). @@ -221,9 +253,10 @@ No item is output-preserving without qualification, and the PR for each says whi - Whether one `rocm examine --json` run shares answers between the stacks (a single WSL summary and a single `ldconfig -p`, one inventory including its failure) or keeps calling shared code that asks again. - What an absent Windows inventory does once there is one inventory: remember the absence for the run, or drop the per-fact fallbacks. - Whether `gpus[].name` should come from `pnputil` or from WMI. -- Which timeout wins for the kernel and CPU reads, and whether `kernel_release` should be filled on Windows. +- Which timeout wins for the kernel read, and whether `kernel_release` should be filled on Windows. (The CPU read took one 5 s limit; see "CPU model".) - Whether the typed outcome belongs on `CommandCapture` or beside it. -- Whether CPU facts should be collected on WSL at all (option (b) under "CPU model"), and if so how the Windows-hosted distro probe obtains them. +- Whether CPU facts should be collected on WSL at all (option (b) under "CPU model", which the CPU-model change did not take), and if so how the Windows-hosted distro probe obtains them. Scenario `examine-41` and `on_wsl_the_summary_reads_the_cpu_and_the_examination_does_not` pin the empty WSL fields until it is decided. +- Whether the Windows reads should be given more or less time than 5 s once they can be measured on a host, and whether the examination should reuse the summary's inventory instead of running the query itself (the CPU read is still one query per stack). - Whether the adapters over `run_with_timeout` take a per-call drain grace, so that a probe whose tool is known to leave a late-writing descendant keeps its output, or whether the 200 ms grace is accepted for every probe. - Whether the near copy of `tool_path_candidates` in `apps/rocm` joins the PATH lookup change. - Whether the runners that replace the subprocess helpers should refuse to resolve a bare program name through an empty `PATH` entry, so that what the lookup calls absent is also what they do not start. From aac07b4255010938ac4f48772b4bb85d7755894d Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Tue, 10 Nov 2026 11:07:22 +0000 Subject: [PATCH 6/7] refactor(rocm-core): name the inventory query in the Windows CPU failure and inject the examination's runner The probe_failures entry for a Windows read that finds no CPU still named a Win32_Processor query, though the examination now runs the system inventory's CPU and RAM script, and the entry also fires when that query succeeds with no CPU row. It now says the Windows system inventory query reported no CPU, as a shared constant, and the characterisation test asserts the message together with the empty model, the unknown vendor and the single entry. probe_cpu_windows now passes the real PowerShell runner to probe_cpu_windows_with, so the whole examination read, failure entry included, is driven by an injected query on every host and the Windows job. A test off Windows runs probe_cpu_windows itself, where the real runner finds no PowerShell, and pins that it reports no CPU with the new entry; replacing the runner or the entry turns it red. What no test reaches is the live runner argument itself and the runtime_is_windows() argument summary_cpu is called with; the host-probing note says so. Signed-off-by: Jussi Elo --- crates/rocm-core/src/examine.rs | 2 +- crates/rocm-core/src/examine/host_env.rs | 55 +++++++++++++++++-- crates/rocm-core/src/host_gpu.rs | 3 +- crates/rocm-core/src/host_gpu/os_facts.rs | 23 ++++---- .../src/host_gpu/windows_inventory.rs | 4 +- docs/host-probing.md | 6 +- 6 files changed, 72 insertions(+), 21 deletions(-) diff --git a/crates/rocm-core/src/examine.rs b/crates/rocm-core/src/examine.rs index 3e0aad3dc3..bc8575d0ca 100644 --- a/crates/rocm-core/src/examine.rs +++ b/crates/rocm-core/src/examine.rs @@ -38,7 +38,7 @@ use host_env::{ probe_shared_memory, }; #[cfg(test)] -pub(crate) use host_env::{record_cpu, record_cpu_windows}; +pub(crate) use host_env::{probe_cpu_windows_with, record_cpu}; pub(crate) use library_copies::comgr_matches_runtime; use library_copies::probe_comgr; use linux_driver::{probe_devices, probe_modules, probe_secure_boot, probe_user}; diff --git a/crates/rocm-core/src/examine/host_env.rs b/crates/rocm-core/src/examine/host_env.rs index 8127eaa9ab..16ef12eaae 100644 --- a/crates/rocm-core/src/examine/host_env.rs +++ b/crates/rocm-core/src/examine/host_env.rs @@ -7,7 +7,9 @@ use super::Examination; use super::library_copies::libraries_on_ld_path; -use crate::host_gpu::{CpuFacts, read_linux_cpu_facts, read_windows_cpu_facts}; +use crate::host_gpu::{ + CpuFacts, read_linux_cpu_facts, run_windows_system_query, windows_cpu_facts_with, +}; use crate::probe_exec::{MEDIUM, SHORT, read_host_text, run, truncate_to_chars}; use crate::{runtime_is_linux, runtime_is_windows}; @@ -132,17 +134,34 @@ pub(super) fn probe_cpu_linux(e: &mut Examination) { /// The CPU on Windows, from the CPU row of the system inventory through the /// reader the host summary uses. pub(super) fn probe_cpu_windows(e: &mut Examination) { - record_cpu_windows(e, read_windows_cpu_facts()); + probe_cpu_windows_with(e, run_windows_system_query); } +/// [`probe_cpu_windows`] with the query injected, so a test on any host drives +/// the examination's whole Windows read, the failure entry included. `query` is +/// handed the inventory's script and time limit; see `windows_cpu_facts_with`. +/// [`probe_cpu_windows`] passes the real PowerShell runner, which is the one +/// argument no test on a Linux host can reach. +pub(crate) fn probe_cpu_windows_with( + e: &mut Examination, + query: impl FnOnce(&str, std::time::Duration) -> Option, +) { + record_cpu_windows(e, windows_cpu_facts_with(query)); +} + +/// The `probe_failures` entry for a Windows read that found no CPU. It names the +/// query that ran, the system inventory's CPU and RAM script, and fires whether +/// that query failed, timed out, printed nothing or printed no CPU row. +pub(crate) const WINDOWS_CPU_NOT_FOUND: &str = + "The Windows system inventory query (CPU and RAM) reported no CPU; cannot identify CPU."; + /// [`record_cpu`], and a failure entry when no CPU could be read: on Windows the /// CPU comes from a query that can fail or time out, and an empty model alone /// would not say so. Crate-visible so a test on any host can feed it the answer /// of an injected query. pub(crate) fn record_cpu_windows(e: &mut Examination, facts: CpuFacts) { if facts.model.is_none() { - e.probe_failures - .push("Get-CimInstance Win32_Processor failed; cannot identify CPU.".to_owned()); + e.probe_failures.push(WINDOWS_CPU_NOT_FOUND.to_owned()); } record_cpu(e, facts); } @@ -671,4 +690,32 @@ mod tests { assert_eq!(examined.cpu_model, ""); assert_eq!(examined.cpu_vendor, "unknown"); } + + /// `probe_cpu_windows` runs the system inventory's query and nothing else: + /// off Windows there is no PowerShell for the real runner, so the read finds + /// no CPU and the entry says which query that was, and the injected-query + /// form given a CPU row fills the fields the same way. Replacing the runner + /// `probe_cpu_windows` passes, or the failure entry, fails this test. On + /// Windows the runner is live, so it is skipped there. + #[cfg(not(windows))] + #[test] + fn probe_cpu_windows_runs_the_inventory_query_and_reports_no_cpu_without_one() { + let mut examined = Examination::default(); + probe_cpu_windows(&mut examined); + assert_eq!( + (examined.cpu_model.as_str(), examined.cpu_vendor.as_str()), + ("", "unknown") + ); + assert_eq!(examined.probe_failures, [WINDOWS_CPU_NOT_FOUND]); + + let mut with_cpu = Examination::default(); + probe_cpu_windows_with(&mut with_cpu, |_, _| { + Some("CPU\tIntel(R) Core(TM) i5-12400\nRAM\t17179869184\n".to_owned()) + }); + assert_eq!( + (with_cpu.cpu_model.as_str(), with_cpu.cpu_vendor.as_str()), + ("Intel(R) Core(TM) i5-12400", "intel") + ); + assert!(with_cpu.probe_failures.is_empty()); + } } diff --git a/crates/rocm-core/src/host_gpu.rs b/crates/rocm-core/src/host_gpu.rs index fa841389b2..3535e1f4ea 100644 --- a/crates/rocm-core/src/host_gpu.rs +++ b/crates/rocm-core/src/host_gpu.rs @@ -40,11 +40,12 @@ use anyhow::Result; use os_facts::detect_kernel_version; pub use os_facts::detect_system_ram_gib; pub(crate) use os_facts::{ - CpuFacts, detect_cpu_model, detect_distro_name, read_linux_cpu_facts, read_windows_cpu_facts, + CpuFacts, detect_cpu_model, detect_distro_name, read_linux_cpu_facts, windows_cpu_facts_with, }; use serde::{Deserialize, Serialize}; use std::fs; use std::path::{Path, PathBuf}; +pub(crate) use windows_inventory::run_windows_system_query; mod capture; mod diagnostics; diff --git a/crates/rocm-core/src/host_gpu/os_facts.rs b/crates/rocm-core/src/host_gpu/os_facts.rs index 12165fa6ce..6c537786e4 100644 --- a/crates/rocm-core/src/host_gpu/os_facts.rs +++ b/crates/rocm-core/src/host_gpu/os_facts.rs @@ -170,7 +170,7 @@ pub(crate) fn read_windows_cpu_facts() -> CpuFacts { /// drive it. `query` is handed the script and the time the inventory allows it, /// and answers with what the script printed, or `None` when it could not run, /// failed or timed out. -pub(super) fn windows_cpu_facts_with( +pub(crate) fn windows_cpu_facts_with( query: impl FnOnce(&str, std::time::Duration) -> Option, ) -> CpuFacts { cpu_facts_from_windows_inventory(windows_system_inventory_with(query).as_ref()) @@ -334,7 +334,7 @@ mod tests { // asserts one answer. The cases that used to differ say so in their // comment, with what each read answered before. - use crate::examine::{Examination, record_cpu, record_cpu_windows}; + use crate::examine::{Examination, probe_cpu_windows_with, record_cpu}; use crate::host_gpu::windows_inventory::{ InventoryScope, WindowsExamineInventory, WindowsInventorySources, assemble_windows_inventory, windows_system_inventory_with, @@ -496,13 +496,10 @@ mod tests { fn examination_on_windows(output: Option<&str>) -> (Examination, Duration) { let mut examined = Examination::default(); let mut asked = None; - record_cpu_windows( - &mut examined, - windows_cpu_facts_with(|_, timeout| { - asked = Some(timeout); - output.map(str::to_owned) - }), - ); + probe_cpu_windows_with(&mut examined, |_, timeout| { + asked = Some(timeout); + output.map(str::to_owned) + }); (examined, asked.expect("the examination ran its query")) } @@ -615,9 +612,15 @@ mod tests { let (examined, _) = examination_on_windows(output); assert_eq!(examined.cpu_model, "", "{output:?}"); assert_eq!(examined.cpu_vendor, "unknown", "{output:?}"); + // The message and the state it describes are asserted together: the + // entry names the inventory query that ran (CPU and RAM), not a + // `Win32_Processor` query nothing runs any more, and it is there + // exactly when the model is empty and the vendor unknown. assert_eq!( examined.probe_failures, - ["Get-CimInstance Win32_Processor failed; cannot identify CPU."], + [ + "The Windows system inventory query (CPU and RAM) reported no CPU; cannot identify CPU." + ], "{output:?}" ); } diff --git a/crates/rocm-core/src/host_gpu/windows_inventory.rs b/crates/rocm-core/src/host_gpu/windows_inventory.rs index 02155e87eb..ec6db3e7b5 100644 --- a/crates/rocm-core/src/host_gpu/windows_inventory.rs +++ b/crates/rocm-core/src/host_gpu/windows_inventory.rs @@ -519,7 +519,7 @@ fn detect_windows_system_inventory_from_cim() -> Option /// Run the system query's `script` under PowerShell for at most `timeout`, and /// answer with its stdout, or `None` when it could not run, failed or timed out. #[cfg(windows)] -pub(super) fn run_windows_system_query(script: &str, timeout: Duration) -> Option { +pub(crate) fn run_windows_system_query(script: &str, timeout: Duration) -> Option { capture_optional_command_with_timeout( "powershell", &[ @@ -535,7 +535,7 @@ pub(super) fn run_windows_system_query(script: &str, timeout: Duration) -> Optio /// Off Windows there is no PowerShell to run the system query. #[cfg(not(windows))] -pub(super) const fn run_windows_system_query(_script: &str, _timeout: Duration) -> Option { +pub(crate) const fn run_windows_system_query(_script: &str, _timeout: Duration) -> Option { None } diff --git a/docs/host-probing.md b/docs/host-probing.md index 5fb792fd58..0292fa63a6 100644 --- a/docs/host-probing.md +++ b/docs/host-probing.md @@ -104,7 +104,7 @@ The open questions are decided as follows. - Whitespace: the collapse reaches `cpu_model`, so the model is one string in both forms. Nothing diagnoses from `cpu_model` or `cpu_vendor` (grep of the crates), so the only places that see the change are the JSON and the Doctor reports; a consumer that matched the old padded spelling is the one that sees it. - The `Hardware`-only case: the `Hardware` value is the model and the vendor stays `unknown`, because such a file names none. The model is the first line of either kind, as the summary always had it, so with `Hardware` before `model name` the examination now reads the `Hardware` value where it read the `model name`; real cpuinfo prints `Hardware` after the per-core blocks, so that order is a fixture, not a host this note knows of. -- Empty versus unknown: a CPU that could not be read is no model (shown `` in the text, `null` in `summary.cpu`) in the summary and an empty `cpu_model` with `cpu_vendor` `unknown` in the examination, as before, and a vendor line with no value names no vendor. On Linux that is not a `probe_failures` entry, as before. On Windows the examination still records `Get-CimInstance Win32_Processor failed; cannot identify CPU.`, now for any read that gives no CPU (a query that failed, timed out, printed nothing or printed no CPU row), where it recorded it for a non-zero exit or blank output. +- Empty versus unknown: a CPU that could not be read is no model (shown `` in the text, `null` in `summary.cpu`) in the summary and an empty `cpu_model` with `cpu_vendor` `unknown` in the examination, as before, and a vendor line with no value names no vendor. On Linux that is not a `probe_failures` entry, as before. On Windows the examination still records a `probe_failures` entry, now worded for the query that runs (`The Windows system inventory query (CPU and RAM) reported no CPU; cannot identify CPU.`, where it named a `Win32_Processor` query that no longer runs), now for any read that gives no CPU (a query that failed, timed out, printed nothing or printed no CPU row), where it recorded it for a non-zero exit or blank output. - The Windows timeout: one limit of 5 s, the inventory's own for every other query, for the one query both stacks run. The summary's system query was 1.5 s (and its own CPU query too) and the examination's was 8 s, so the summary now waits longer, for its RAM row as well, and the examination gives up 3 s sooner. The reason is that the examination runs the query first in a `--json` run, on a PowerShell that has not started yet, where 1.5 s may not be enough and a spurious empty `cpu_model` with a `probe_failures` entry costs more than waiting; 8 s was not taken for the shared query because it would also be the worst case of every startup path that gathers the summary on a host whose PowerShell hangs. Neither number was measured: UNCONFIRMED on a real Windows host. - The inventory fallback: dropped. With no inventory at all, `detect_cpu_model` used to run a `Win32_Processor` query of its own, but an absent inventory means every source, the system query included, had just failed, so the second try repeated a failed query. The summary now shows `` there. Keeping it would have meant keeping a second Windows implementation, which is what this change removes. @@ -119,12 +119,12 @@ The differences that moved, each with the test that pins it (the unit tests are | Windows: the query the examination runs | `Win32_Processor` alone, through `run`, 8 s | the system inventory query (CPU and RAM), through the optional-command helper, 5 s | `windows_both_reads_are_given_the_same_time` | | Windows: the summary's system query | 1.5 s | 5 s, for its CPU and RAM rows | `windows_both_reads_are_given_the_same_time` | | Windows: no inventory | the summary ran a second CPU query | ``, no second query | `windows_an_answer_with_no_cpu_is_no_cpu_in_both_reads`, `the_summary_cpu_comes_from_the_windows_inventory_or_not_at_all` | -| Windows: an answer with no CPU in it | failure entry only for a non-zero exit or blank output | failure entry for any read with no CPU | `windows_an_answer_with_no_cpu_is_no_cpu_in_both_reads` | +| Windows: an answer with no CPU in it | failure entry (naming `Win32_Processor`) only for a non-zero exit or blank output | failure entry naming the inventory query, for any read with no CPU | `windows_an_answer_with_no_cpu_is_no_cpu_in_both_reads` | | WSL | summary populated, examination empty | unchanged | `on_wsl_the_summary_reads_the_cpu_and_the_examination_does_not`, scenario `examine-41` | Two more differences come with the optional-command helper that the examination's Windows read now runs through, and neither is pinned by a test, because the helper is not injectable and needs a Windows host: it reads the output as strict UTF-8 where `run` decoded it lossily, so a CPU name that is not valid UTF-8 reads as no CPU, and it tries each spelling of `powershell` after a timeout where `run` started it once, so a hung PowerShell can cost the examination more than one limit (the candidate loop under "Subprocess runners"; it belongs to that pair, and the summary's inventory had the same behaviour already). -What stays untested: the live Windows command line itself, which a unit test cannot start (the injected query stands in for it, and the script it is handed is the one the summary's inventory already ran); the one-line `runtime_is_windows()` argument of `summary_cpu` at its call site; and the timing claims above. The scenarios run on the Linux lane only (`@requires-os:linux`): the planted machines are simulated host roots, and native Windows cannot run them, so its CPU cases are the unit tests, which run on the Windows job as well. +What stays untested: the live Windows command line itself, which a unit test cannot start (the injected query stands in for it, and the script it is handed is the one the summary's inventory already ran); the `runtime_is_windows()` argument `summary_cpu` is called with, and the real PowerShell runner `probe_cpu_windows` passes (off Windows `probe_cpu_windows_runs_the_inventory_query_and_reports_no_cpu_without_one` runs it and pins that it reports no CPU, but the Windows job compiling is the only thing tying the live runner); and the timing claims above. The scenarios run on the Linux lane only (`@requires-os:linux`): the planted machines are simulated host roots, and native Windows cannot run them, so its CPU cases are the unit tests, which run on the Windows job as well. ## Windows GPU query From 27a5d64f741dd34814182b86a215de442a4c2151 Mon Sep 17 00:00:00 2001 From: Jussi Elo Date: Tue, 10 Nov 2026 12:55:12 +0000 Subject: [PATCH 7/7] refactor(rocm-core): make detect_cpu_model Linux-only so it cannot start a second Windows query detect_cpu_model had a Windows arm that no production path reached: its only caller hands it to summary_cpu as the non-Windows reader, and on Windows the summary reads the CPU row of the inventory it already holds. It was also the path that would have brought the duplicate PowerShell query back for whoever called it next. The arm, read_cpu_facts and read_windows_cpu_facts are gone, and detect_cpu_model answers None off Linux; the host check is an argument of a small helper, so detect_cpu_model_reads_nothing_off_linux shows on any host that the reader is never called when it is false, and goes red when the check is dropped. The host-probing note now says that native Windows runs the system query twice in one rocm examine --json (the summary's inventory and probe_cpu_windows) and why the two are not shared within a run, and why the summary's RAM keeps its own fallback query while the CPU dropped it: detect_system_ram_gib is a public reader that model_fit calls with no inventory, and changing the summary's RAM is a behaviour change for a fact this change does not touch. Signed-off-by: Jussi Elo --- crates/rocm-core/src/host_gpu/os_facts.rs | 57 ++++++++++++++--------- docs/host-probing.md | 5 +- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/crates/rocm-core/src/host_gpu/os_facts.rs b/crates/rocm-core/src/host_gpu/os_facts.rs index 6c537786e4..557e3a34fc 100644 --- a/crates/rocm-core/src/host_gpu/os_facts.rs +++ b/crates/rocm-core/src/host_gpu/os_facts.rs @@ -8,9 +8,7 @@ use super::capture::{ OPTIONAL_COMMAND_TIMEOUT, capture_optional_command, capture_optional_command_with_timeout, }; -use super::windows_inventory::{ - WindowsExamineInventory, run_windows_system_query, windows_system_inventory_with, -}; +use super::windows_inventory::{WindowsExamineInventory, windows_system_inventory_with}; use crate::{hardware_root, host_path, runtime_is_linux, runtime_is_windows}; use std::fs; @@ -68,7 +66,8 @@ fn distro_name_from_os_release(read: &hardware_root::OsRelease) -> String { } /// The CPU's model and vendor, read together by the one reader both stacks use: -/// the summary's `cpu` through [`detect_cpu_model`] and the Windows inventory, +/// the summary's `cpu` through [`detect_cpu_model`] on Linux and the Windows +/// inventory's CPU row on Windows, /// the examination's `cpu_model` and `cpu_vendor` through `probe_cpu_linux` and /// `probe_cpu_windows`. #[derive(Debug, Clone, PartialEq, Eq)] @@ -96,20 +95,22 @@ impl CpuFacts { } } -/// The CPU summary line: the model of [`read_cpu_facts`]. +/// The CPU summary line on Linux: the model of [`read_linux_cpu_facts`]. +/// +/// Linux only, on purpose. On Windows the summary's CPU is the CPU row of the +/// inventory it already holds (`summary_cpu` in `host_gpu.rs`), and the +/// examination's is its own injected read of the same query, so a Windows arm +/// here would be a second PowerShell query for the same fact. It answers `None` +/// off Linux instead, which is what a future caller on Windows gets rather than +/// a duplicate query. pub(crate) fn detect_cpu_model() -> Option { - read_cpu_facts().model + linux_cpu_model(runtime_is_linux(), read_linux_cpu_facts) } -/// The CPU facts of the host this process runs on. -pub(crate) fn read_cpu_facts() -> CpuFacts { - if runtime_is_windows() { - read_windows_cpu_facts() - } else if runtime_is_linux() { - read_linux_cpu_facts() - } else { - CpuFacts::unknown() - } +/// [`detect_cpu_model`] with the host check and the reader as arguments, so a +/// test on any host shows that nothing is read off Linux. +fn linux_cpu_model(linux: bool, read: impl FnOnce() -> CpuFacts) -> Option { + linux.then(read).and_then(|facts| facts.model) } /// The CPU facts from `/proc/cpuinfo`, read through [`host_path`]. @@ -160,14 +161,10 @@ pub(super) fn cpu_facts_from_cpuinfo(text: &str) -> CpuFacts { facts } -/// The CPU facts from the Windows system inventory's `CPU` row: the one query -/// the summary's inventory runs for its CPU and RAM. -pub(crate) fn read_windows_cpu_facts() -> CpuFacts { - windows_cpu_facts_with(run_windows_system_query) -} - -/// [`read_windows_cpu_facts`] with the query injected, so a test on any host can -/// drive it. `query` is handed the script and the time the inventory allows it, +/// The CPU facts from the Windows system inventory's `CPU` row, with the query +/// injected, so a test on any host can drive it. The examination's +/// `probe_cpu_windows` is the one caller that passes the real PowerShell runner. +/// `query` is handed the script and the time the inventory allows it, /// and answers with what the script printed, or `None` when it could not run, /// failed or timed out. pub(crate) fn windows_cpu_facts_with( @@ -492,6 +489,20 @@ mod tests { ); } + /// `detect_cpu_model` reads the CPU on Linux and nothing anywhere else, so no + /// Windows query can start from it: with the host check false the reader is + /// never called. Calling the reader whatever the host turns this red. + #[test] + fn detect_cpu_model_reads_nothing_off_linux() { + let never = + || -> CpuFacts { panic!("the CPU is read through detect_cpu_model on Linux only") }; + assert_eq!(linux_cpu_model(false, never), None); + assert_eq!( + linux_cpu_model(true, || cpu_facts_from_cpuinfo("model name\t: A B\n")).as_deref(), + Some("A B") + ); + } + /// What the examination makes of one answer from the system query. fn examination_on_windows(output: Option<&str>) -> (Examination, Duration) { let mut examined = Examination::default(); diff --git a/docs/host-probing.md b/docs/host-probing.md index 0292fa63a6..e533ac353c 100644 --- a/docs/host-probing.md +++ b/docs/host-probing.md @@ -96,7 +96,7 @@ This change does not reach WSL. The early return stays, so on WSL `cpu_model` st ### Decided and delivered -The change took the survivor proposed above. `read_cpu_facts` in [os_facts.rs](../crates/rocm-core/src/host_gpu/os_facts.rs) returns a `CpuFacts` (the model, or none, and the vendor): on Linux `read_linux_cpu_facts` reads `/proc/cpuinfo` through `host_path` and `cpu_facts_from_cpuinfo` takes the first `model name` or `Hardware` line with a value and the first `vendor_id` with a value, and on Windows `read_windows_cpu_facts` runs the CPU-and-RAM query of the system inventory (`windows_system_inventory_with` in [windows_inventory.rs](../crates/rocm-core/src/host_gpu/windows_inventory.rs)) and `cpu_facts_from_windows_inventory` takes its CPU row, deriving the vendor from the model string (`amd` or `intel`, else `unknown`) as `probe_cpu_windows` did. `probe_cpu_linux` and `probe_cpu_windows` fill `cpu_model` and `cpu_vendor` from it, `detect_cpu_model` is its model, and the summary on Windows reads the CPU row of the inventory it already holds (`summary_cpu` in [host_gpu.rs](../crates/rocm-core/src/host_gpu.rs)), which is the same row through the same function. Both PowerShell `Win32_Processor` queries that were separate from the inventory are gone. The injectable query (`windows_cpu_facts_with`, and `windows_system_inventory_with` under it) is what runs the Windows logic in a unit test on the Linux and Windows jobs. +The change took the survivor proposed above. `CpuFacts` in [os_facts.rs](../crates/rocm-core/src/host_gpu/os_facts.rs) carries the model, or none, and the vendor, and one reader per platform fills it: on Linux `read_linux_cpu_facts` reads `/proc/cpuinfo` through `host_path` and `cpu_facts_from_cpuinfo` takes the first `model name` or `Hardware` line with a value and the first `vendor_id` with a value, and on Windows `windows_cpu_facts_with` runs the CPU-and-RAM query of the system inventory (`windows_system_inventory_with` in [windows_inventory.rs](../crates/rocm-core/src/host_gpu/windows_inventory.rs)) and `cpu_facts_from_windows_inventory` takes its CPU row, deriving the vendor from the model string (`amd` or `intel`, else `unknown`) as `probe_cpu_windows` did. `probe_cpu_linux` and `probe_cpu_windows` fill `cpu_model` and `cpu_vendor` from them, and the summary reads the same facts: `detect_cpu_model` is the Linux model and answers `None` off Linux, so it cannot start a Windows query, and on Windows the summary reads the CPU row of the inventory it already holds (`summary_cpu` in [host_gpu.rs](../crates/rocm-core/src/host_gpu.rs)), which is the same row through the same function. Both PowerShell `Win32_Processor` queries that were separate from the inventory are gone, and so is the Windows arm `detect_cpu_model` had in the first version of this change: nothing reached it in production, and it was exactly the path that would have brought the second query back. The injectable query (`windows_cpu_facts_with`, and `windows_system_inventory_with` under it) is what runs the Windows logic in a unit test on the Linux and Windows jobs. WSL is option (a): the early return stays, so on WSL `cpu_model` is empty and `cpu_vendor` is `unknown` while the summary shows the CPU, exactly as before. The comparison of `summary.cpu` with `cpu_model` in the scenarios is scoped to hosts where `is_wsl` is false, and the empty WSL fields are pinned as an existing difference by `on_wsl_the_summary_reads_the_cpu_and_the_examination_does_not` and scenario `examine-41`, so collecting CPU facts on WSL (option (b)) is a visible change of its own, which turns both. @@ -106,7 +106,8 @@ The open questions are decided as follows. - The `Hardware`-only case: the `Hardware` value is the model and the vendor stays `unknown`, because such a file names none. The model is the first line of either kind, as the summary always had it, so with `Hardware` before `model name` the examination now reads the `Hardware` value where it read the `model name`; real cpuinfo prints `Hardware` after the per-core blocks, so that order is a fixture, not a host this note knows of. - Empty versus unknown: a CPU that could not be read is no model (shown `` in the text, `null` in `summary.cpu`) in the summary and an empty `cpu_model` with `cpu_vendor` `unknown` in the examination, as before, and a vendor line with no value names no vendor. On Linux that is not a `probe_failures` entry, as before. On Windows the examination still records a `probe_failures` entry, now worded for the query that runs (`The Windows system inventory query (CPU and RAM) reported no CPU; cannot identify CPU.`, where it named a `Win32_Processor` query that no longer runs), now for any read that gives no CPU (a query that failed, timed out, printed nothing or printed no CPU row), where it recorded it for a non-zero exit or blank output. - The Windows timeout: one limit of 5 s, the inventory's own for every other query, for the one query both stacks run. The summary's system query was 1.5 s (and its own CPU query too) and the examination's was 8 s, so the summary now waits longer, for its RAM row as well, and the examination gives up 3 s sooner. The reason is that the examination runs the query first in a `--json` run, on a PowerShell that has not started yet, where 1.5 s may not be enough and a spurious empty `cpu_model` with a `probe_failures` entry costs more than waiting; 8 s was not taken for the shared query because it would also be the worst case of every startup path that gathers the summary on a host whose PowerShell hangs. Neither number was measured: UNCONFIRMED on a real Windows host. -- The inventory fallback: dropped. With no inventory at all, `detect_cpu_model` used to run a `Win32_Processor` query of its own, but an absent inventory means every source, the system query included, had just failed, so the second try repeated a failed query. The summary now shows `` there. Keeping it would have meant keeping a second Windows implementation, which is what this change removes. +- Queries per run: on native Windows `rocm examine --json` runs the system query twice now (down from two or three): once in the summary's `assemble_windows_inventory` and once in `probe_cpu_windows`. They are not shared within a run because the examination is built first and the summary after it, by separate calls in `examine_cmd.rs` that pass nothing between them, and sharing the answer means choosing which stack owns it, which is the open question below about one run sharing answers between the stacks. This change removes the third read and the second implementation; it does not claim one query. +- The inventory fallback: dropped. With no inventory at all, `detect_cpu_model` used to run a `Win32_Processor` query of its own, but an absent inventory means every source, the system query included, had just failed, so the second try repeated a failed query. The summary now shows `` there. Keeping it would have meant keeping a second Windows implementation, which is what this change removes. The summary's RAM keeps its own fallback query (`detect_system_ram_gib_with_windows_inventory`), and that is deliberate here and not an oversight: `detect_system_ram_gib` is a public reader that `model_fit` calls with no inventory at all, so its Windows query has to exist, and changing what the summary shows for RAM when the inventory is absent is a behaviour change for a fact this change does not touch. Dropping that fallback so RAM follows the CPU is a follow-up, with its own scenario or unit test. The differences that moved, each with the test that pins it (the unit tests are in `os_facts.rs`, `host_gpu.rs` and `host_env.rs`; the scenarios are in `examine.feature`):