diff --git a/crates/rocm-core/src/examine.rs b/crates/rocm-core/src/examine.rs index 7dca69b3b..98ad844b8 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_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 231127148..625ed221d 100644 --- a/crates/rocm-core/src/examine/host_env.rs +++ b/crates/rocm-core/src/examine/host_env.rs @@ -7,6 +7,9 @@ use super::Examination; use super::library_copies::libraries_on_ld_path; +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}; @@ -117,64 +120,53 @@ pub(super) fn parse_iommu_param(cmdline: &str) -> Option { }) } +/// 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; +} + +/// 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) { - let txt = read_host_text("/proc/cpuinfo"); - 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; - } - } + 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) { - let (rc, out, _) = run( - "powershell", - &[ - "-NoProfile", - "-Command", - "(Get-CimInstance Win32_Processor | Select-Object -First 1).Name", - ], - 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 { - e.probe_failures - .push("Get-CimInstance Win32_Processor failed; cannot identify CPU.".to_owned()); + 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(WINDOWS_CPU_NOT_FOUND.to_owned()); } + record_cpu(e, facts); } pub(super) fn probe_env(e: &mut Examination) { @@ -622,6 +614,114 @@ 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, 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() { + 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"); + } + + /// `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()); + } + // The kernel-release read, the second pair of `docs/host-probing.md` // ("Kernel version"). It used to be two readers: `detect_kernel_version` in // the summary stack (1.5 s, strict UTF-8, `None` on any failure or non-zero diff --git a/crates/rocm-core/src/host_gpu.rs b/crates/rocm-core/src/host_gpu.rs index 2e5009dbf..c2ea229b6 100644 --- a/crates/rocm-core/src/host_gpu.rs +++ b/crates/rocm-core/src/host_gpu.rs @@ -37,13 +37,15 @@ use crate::{ host_path, runtime_is_linux, runtime_is_windows, runtime_os_name, }; use anyhow::Result; -use os_facts::detect_cpu_model; -pub(crate) use os_facts::detect_distro_name; -pub(crate) 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, detect_kernel_version, 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; @@ -303,13 +305,23 @@ 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(); - } + summary_cpu(runtime_is_windows(), windows_inventory, detect_cpu_model) +} - detect_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>, + read: impl FnOnce() -> Option, +) -> Option { + if windows { + os_facts::cpu_facts_from_windows_inventory(windows_inventory).model + } else { + read() + } } fn detect_system_ram_gib_with_windows_inventory( @@ -524,6 +536,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 d78a459f1..c5cf1a4fd 100644 --- a/crates/rocm-core/src/host_gpu/os_facts.rs +++ b/crates/rocm-core/src/host_gpu/os_facts.rs @@ -8,6 +8,7 @@ use super::capture::{ OPTIONAL_COMMAND_TIMEOUT, capture_optional_command, capture_optional_command_with_timeout, }; +use super::windows_inventory::{WindowsExamineInventory, windows_system_inventory_with}; use crate::{hardware_root, host_path, runtime_is_linux, runtime_is_windows}; use std::fs; use std::time::Duration; @@ -96,40 +97,139 @@ fn distro_name_from_os_release(read: &hardware_root::OsRelease) -> String { } } -pub(super) 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()); +/// The CPU's model and vendor, read together by the one reader both stacks use: +/// 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)] +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(), + } } +} - 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) - }) - }) - { - return Some(model); +/// 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 { + linux_cpu_model(runtime_is_linux(), read_linux_cpu_facts) +} + +/// [`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`]. +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, 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( + query: impl FnOnce(&str, std::time::Duration) -> Option, +) -> CpuFacts { + cpu_facts_from_windows_inventory(windows_system_inventory_with(query).as_ref()) +} + +/// 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 { @@ -254,6 +354,352 @@ mod tests { ); } + // ── CPU model: the summary's read and the examination's read ────────── + // + // 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, probe_cpu_windows_with, record_cpu}; + use crate::host_gpu::windows_inventory::{ + InventoryScope, WindowsInventorySources, assemble_windows_inventory, + }; + + /// One `/proc/cpuinfo` and the CPU both reads make of it. + struct CpuinfoCase { + name: &'static str, + text: &'static str, + /// The model: `summary.cpu`, and `cpu_model` where `Some`, empty where `None`. + model: Option<&'static str>, + /// `cpu_vendor`. + vendor: &'static str, + } + + #[test] + fn linux_cpuinfo_both_reads_report_one_cpu() { + let cases = [ + CpuinfoCase { + name: "Intel, model name and vendor_id", + 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", + 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". + name: "model name and no vendor_id", + text: "processor\t: 0\nmodel name\t: ARMv8 Processor rev 1 (v8l)\n", + 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", + 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", + model: Some("ARMv7 Processor rev 4 (v7l)"), + vendor: "unknown", + }, + CpuinfoCase { + name: "nothing to read", + text: "processor\t: 0\nCPU implementer\t: 0x41\n", + model: None, + vendor: "unknown", + }, + CpuinfoCase { + name: "an empty file", + text: "", + model: None, + vendor: "unknown", + }, + CpuinfoCase { + // 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", + model: Some("Hygon C86 7185 32-core Processor"), + vendor: "hygongenuine", + }, + ]; + for case in cases { + assert_cpuinfo_case(&case); + } + } + + /// 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_inputs_the_two_reads_used_to_disagree_on() { + let cases = [ + CpuinfoCase { + // 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", + model: Some("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz"), + vendor: "intel", + }, + CpuinfoCase { + // 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", + model: Some("BCM2835"), + vendor: "unknown", + }, + CpuinfoCase { + // 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", + model: Some("BCM2835"), + vendor: "unknown", + }, + CpuinfoCase { + // 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", + 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 { + assert_cpuinfo_case(&case); + } + } + + fn assert_cpuinfo_case(case: &CpuinfoCase) { + // The summary's `cpu`: what `detect_cpu_model` makes of the file. + assert_eq!( + cpu_facts_from_cpuinfo(case.text).model.as_deref(), + case.model, + "summary, {}", + case.name + ); + // The examination's fields. + let mut examined = Examination::default(); + record_cpu(&mut examined, cpu_facts_from_cpuinfo(case.text)); + assert_eq!( + (examined.cpu_model.as_str(), examined.cpu_vendor.as_str()), + (case.model.unwrap_or(""), case.vendor), + "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 + ); + } + + /// `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(); + let mut asked = None; + probe_cpu_windows_with(&mut examined, |_, 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 and counts how many run. + struct SystemQuery<'a> { + output: Option<&'a str>, + asked: Vec<(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.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 `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, + asked: Vec::new(), + }; + let inventory = assemble_windows_inventory(&mut sources, InventoryScope::SUMMARY); + ( + 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_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()); + } + + /// 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_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.as_deref(), + Some("Intel(R) Pentium(R) CPU N3700 @ 1.60GHz") + ); + let (examined, _) = examination_on_windows(Some(output)); + assert_eq!( + examined.cpu_model, + "Intel(R) Pentium(R) CPU N3700 @ 1.60GHz" + ); + assert_eq!(examined.cpu_vendor, "intel"); + } + + #[test] + 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"), + ("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(Some(&format!("CPU\t{name}\n"))); + assert_eq!(examined.cpu_vendor, vendor, "{name}"); + assert_eq!(examined.cpu_model, name); + } + } + + /// 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_is_no_cpu_in_both_reads() { + for output in [ + Some("RAM\t17179869184\n"), + Some("CPU\t \nRAM\t17179869184\n"), + Some(""), + None, + ] { + 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:?}"); + // 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, + [ + "The Windows system inventory query (CPU and RAM) reported no CPU; cannot identify CPU." + ], + "{output:?}" + ); + } + } + + /// 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_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}" + ); + } + + /// 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_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", + ); + assert_eq!( + cpu_facts_from_windows_inventory(Some(&inventory)), + CpuFacts { + model: Some("AMD EPYC 7763".to_owned()), + vendor: "amd".to_owned() + } + ); + } + /// The decision recorded in `docs/host-probing.md`: one 5 s limit for the /// kernel read. The timeout behaviour itself is exercised, with a stand-in /// `uname`, by the `kernel_pair` tests in `examine/host_env.rs`. diff --git a/crates/rocm-core/src/host_gpu/windows_inventory.rs b/crates/rocm-core/src/host_gpu/windows_inventory.rs index 3a47d8674..ec6db3e7b 100644 --- a/crates/rocm-core/src/host_gpu/windows_inventory.rs +++ b/crates/rocm-core/src/host_gpu/windows_inventory.rs @@ -6,9 +6,7 @@ //! their output, and the assembly of one inventory from several sources. #[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 +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 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(windows)] 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,6 +513,13 @@ fn detect_windows_system_inventory_from_cim() -> Option if !runtime_is_windows() { return None; } + 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(crate) fn run_windows_system_query(script: &str, timeout: Duration) -> Option { capture_optional_command_with_timeout( "powershell", &[ @@ -514,9 +527,29 @@ fn detect_windows_system_inventory_from_cim() -> Option "-NoProfile", "-NonInteractive", "-Command", - WINDOWS_SYSTEM_INVENTORY_SCRIPT, + script, ], - OPTIONAL_COMMAND_TIMEOUT, + timeout, + ) +} + +/// Off Windows there is no PowerShell to run the system query. +#[cfg(not(windows))] +pub(crate) 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 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 { + query( + WINDOWS_SYSTEM_INVENTORY_SCRIPT, + WINDOWS_SYSTEM_INVENTORY_TIMEOUT, ) .map(|output| parse_windows_examine_inventory(&output)) } diff --git a/docs/architecture.md b/docs/architecture.md index 557ad354c..b13f4bb01 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -196,13 +196,13 @@ 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, with the kernel release read through the summary stack's `detect_kernel_version`, 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, with the kernel release read through the summary stack's `detect_kernel_version`, 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-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 (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. Two pairs are converged: the `PATH` lookup ([`probe_exec.rs`](../crates/rocm-core/src/probe_exec.rs)'s `which` is now the only copy) and the kernel release ([`os_facts.rs`](../crates/rocm-core/src/host_gpu/os_facts.rs)'s `detect_kernel_version` is now the only `uname -r` reader, which [`host_env.rs`](../crates/rocm-core/src/examine/host_env.rs)'s `probe_os` calls for `kernel_release`, and `rocm examine --json` runs it once: [`examine.rs`](../crates/rocm-core/src/examine.rs)'s `Examination::probe_with_summary` probes the host and hands the examination's kernel release to the summary). It is a snapshot, to be retired when the convergence tickets close. +- [`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 (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. Three pairs are converged: the `PATH` lookup ([`probe_exec.rs`](../crates/rocm-core/src/probe_exec.rs)'s `which` is now the only copy) and the kernel release ([`os_facts.rs`](../crates/rocm-core/src/host_gpu/os_facts.rs)'s `detect_kernel_version` is now the only `uname -r` reader, which [`host_env.rs`](../crates/rocm-core/src/examine/host_env.rs)'s `probe_os` calls for `kernel_release`, and `rocm examine --json` runs it once: [`examine.rs`](../crates/rocm-core/src/examine.rs)'s `Examination::probe_with_summary` probes the host and hands the examination's kernel release to the summary), and the CPU model ([`os_facts.rs`](../crates/rocm-core/src/host_gpu/os_facts.rs) has one reader per platform for the model and vendor, which `probe_cpu_linux` and `probe_cpu_windows` record and the summary reads). 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. - [`model_catalog.rs`](../crates/rocm-core/src/model_catalog.rs) — (ROCMAI-152, Phase 7c) the curated model catalog's types, built-in catalog JSON loading, signed external recipe indexes, and artifact cache-marker bookkeeping — see the module's own doc comment for what it owns. diff --git a/docs/host-probing.md b/docs/host-probing.md index f2a7444a5..cc67ef900 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 two 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 kernel release, pair 2, have: see "PATH lookup" and "Kernel version"). 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 three 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, the kernel release, pair 2, and the CPU model, pair 3, have: see "PATH lookup", "Kernel version" 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) | delivered: `detect_kernel_version`, called by `probe_os` for `kernel_release` on Linux; one 5 s limit and a strict read | before the change, where `uname` was slow or failed, `kernel:` and `kernel_release` could disagree (UNCONFIRMED on a real host); on Windows `kernel_release` is still always empty (out of this pair) | -| [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, through `detect_kernel_version` (before the change each ran `uname -r` itself) | 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 | @@ -101,7 +101,7 @@ The scenarios `examine-43` and `examine-44` in [examine.feature](../tests/e2e-cu ## 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 | |---|---|---| @@ -116,10 +116,43 @@ 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. `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-47`, 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 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. +- 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`): + +| 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-45` | +| `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-46` | +| `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 (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-47` | + +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 `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 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. @@ -232,12 +265,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 (delivered: see "Decided and delivered" under "Kernel version"). A single command; it settled 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-45` to `examine-47`) 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). @@ -247,9 +280,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 CPU read, and whether `kernel_release` should be filled on Windows (the kernel timeout is decided, see "Kernel version"). +- Whether `kernel_release` should be filled on Windows (the kernel timeout is decided, see "Kernel version"; 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-47` 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. diff --git a/tests/e2e-cucumber/features/examine.feature b/tests/e2e-cucumber/features/examine.feature index 9c51a2728..630b4c5ec 100644 --- a/tests/e2e-cucumber/features/examine.feature +++ b/tests/e2e-cucumber/features/examine.feature @@ -674,3 +674,44 @@ Feature: GPU detection and system inspection Given a machine whose uname prints a kernel release and then fails When the user inspects that machine in machine-readable form Then neither the summary nor the examination reports a kernel release, and neither calls it a failure + + # `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-47`). 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-45 - 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-46 - 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-47 - 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 0b7d2c9d0..49fd18295 100644 --- a/tests/e2e-cucumber/tests/e2e/examine_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/examine_steps.rs @@ -2904,6 +2904,129 @@ async fn assert_neither_reports_a_kernel_release(world: &mut E2eWorld) { assert_eq!(new_failures, Vec::::new(), "{json:#}"); } +// ── 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")]