Skip to content
Merged
2 changes: 2 additions & 0 deletions crates/rocm-core/src/examine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
206 changes: 153 additions & 53 deletions crates/rocm-core/src/examine/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -117,64 +120,53 @@ pub(super) fn parse_iommu_param(cmdline: &str) -> Option<String> {
})
}

/// 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<String>,
) {
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) {
Expand Down Expand Up @@ -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<T>(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
Expand Down
52 changes: 43 additions & 9 deletions crates/rocm-core/src/host_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -303,13 +305,23 @@ impl ExamineSummary {
fn detect_cpu_model_with_windows_inventory(
windows_inventory: Option<&WindowsExamineInventory>,
) -> Option<String> {
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<String>,
) -> Option<String> {
if windows {
os_facts::cpu_facts_from_windows_inventory(windows_inventory).model
} else {
read()
Comment on lines +314 to +323

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPU and RAM now disagree about what an absent inventory means. summary_cpu here drops the CPU's second query on the reasoning that no inventory implies the system query already failed, so asking again just repeats a failed query. But detect_system_ram_gib_with_windows_inventory immediately below (roughly lines 294-305) still falls back to its own query for the RAM row — the same row set, from the same query. If a repeat is pointless for the CPU it should be pointless for the RAM, and if it isn't pointless the CPU shouldn't have dropped it.

The RAM fallback predates this PR, so this isn't a regression you caused — it just became inconsistent here. A sentence in the note saying why RAM keeps its fallback (or a follow-up to drop it) would close the gap.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the RAM fallback and said why in docs/host-probing.md. The reason is not that a repeat is useful for RAM: 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 removing the summary's fallback would change what the summary shows for RAM when the inventory is absent, a behaviour change for a fact this PR does not touch and would want its own test. So the two disagree on purpose, for now, and dropping the RAM fallback to follow the CPU is recorded as a follow-up in the note and the PR text.

}
}

fn detect_system_ram_gib_with_windows_inventory(
Expand Down Expand Up @@ -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<String> { 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) {
Expand Down
Loading
Loading