259 lines
7.1 KiB
Rust
259 lines
7.1 KiB
Rust
//! Structured hardware model.
|
|
//!
|
|
//! These types are populated by read-only probing in `phoebus-executor` and
|
|
//! rendered into a human report here. Probing never mutates the system.
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::config::KernelProfile;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum FirmwareMode {
|
|
Uefi,
|
|
Bios,
|
|
}
|
|
|
|
impl FirmwareMode {
|
|
pub fn as_str(self) -> &'static str {
|
|
match self {
|
|
FirmwareMode::Uefi => "UEFI",
|
|
FirmwareMode::Bios => "BIOS",
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum GpuVendor {
|
|
Intel,
|
|
Amd,
|
|
Nvidia,
|
|
Unknown,
|
|
}
|
|
|
|
impl GpuVendor {
|
|
pub fn as_str(self) -> &'static str {
|
|
match self {
|
|
GpuVendor::Intel => "intel",
|
|
GpuVendor::Amd => "amd",
|
|
GpuVendor::Nvidia => "nvidia",
|
|
GpuVendor::Unknown => "unknown",
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct CpuInfo {
|
|
pub vendor: String,
|
|
pub model: String,
|
|
pub threads: u32,
|
|
pub cores: u32,
|
|
pub features: Vec<String>,
|
|
}
|
|
|
|
impl CpuInfo {
|
|
pub fn has_feature(&self, feature: &str) -> bool {
|
|
self.features.iter().any(|f| f == feature)
|
|
}
|
|
|
|
pub fn virt_summary(&self) -> String {
|
|
let mut out = Vec::new();
|
|
if self.has_feature("vmx") {
|
|
out.push("Intel VT-x");
|
|
}
|
|
if self.has_feature("svm") {
|
|
out.push("AMD-V");
|
|
}
|
|
if self.has_feature("avx2") {
|
|
out.push("AVX2");
|
|
}
|
|
if self.has_feature("avx512f") {
|
|
out.push("AVX-512");
|
|
}
|
|
if self.has_feature("aes") {
|
|
out.push("AES-NI");
|
|
}
|
|
out.join(" ")
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct GpuInfo {
|
|
pub vendor: GpuVendor,
|
|
pub description: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct NetworkDevice {
|
|
pub name: String,
|
|
pub wireless: bool,
|
|
pub operstate: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct HardwareReport {
|
|
pub firmware: Option<FirmwareMode>,
|
|
pub cpu: CpuInfo,
|
|
pub memory_mib: u64,
|
|
pub gpus: Vec<GpuInfo>,
|
|
pub network: Vec<NetworkDevice>,
|
|
pub storage_controllers: Vec<String>,
|
|
pub audio: Vec<String>,
|
|
pub bluetooth: Vec<String>,
|
|
pub usb_controllers: Vec<String>,
|
|
pub input_devices: u32,
|
|
pub laptop: bool,
|
|
}
|
|
|
|
impl HardwareReport {
|
|
pub fn firmware_str(&self) -> &'static str {
|
|
match self.firmware {
|
|
Some(FirmwareMode::Uefi) => "UEFI",
|
|
Some(FirmwareMode::Bios) => "BIOS",
|
|
None => "unknown",
|
|
}
|
|
}
|
|
|
|
pub fn gpu_vendors(&self) -> Vec<GpuVendor> {
|
|
let mut out: Vec<GpuVendor> = Vec::new();
|
|
for gpu in &self.gpus {
|
|
if !out.contains(&gpu.vendor) {
|
|
out.push(gpu.vendor);
|
|
}
|
|
}
|
|
if out.is_empty() {
|
|
out.push(GpuVendor::Unknown);
|
|
}
|
|
out
|
|
}
|
|
|
|
/// Deterministic, conservative recommendation.
|
|
pub fn recommended_kernel_profile(&self) -> KernelProfile {
|
|
if self.memory_mib > 0 && self.memory_mib < 2048 {
|
|
KernelProfile::Safe
|
|
} else {
|
|
KernelProfile::Optimized
|
|
}
|
|
}
|
|
|
|
fn section(title: &str, lines: &[String]) -> String {
|
|
let mut out = format!("{title}:\n");
|
|
if lines.is_empty() {
|
|
out.push_str(" (none detected)\n");
|
|
} else {
|
|
for l in lines {
|
|
out.push_str(" ");
|
|
out.push_str(l);
|
|
out.push('\n');
|
|
}
|
|
}
|
|
out.push('\n');
|
|
out
|
|
}
|
|
|
|
pub fn render(&self) -> String {
|
|
let mut out = String::new();
|
|
out.push_str(&format!("Firmware: {}\n", self.firmware_str()));
|
|
out.push_str(&format!("CPU: {}\n", self.cpu.model));
|
|
out.push_str(&format!(
|
|
"CPU cores: {} threads / {} logical CPUs\n",
|
|
self.cpu.threads, self.cpu.cores
|
|
));
|
|
let virt = self.cpu.virt_summary();
|
|
out.push_str(&format!(
|
|
"CPU features: {}\n",
|
|
if virt.is_empty() {
|
|
"none detected"
|
|
} else {
|
|
&virt
|
|
}
|
|
));
|
|
out.push_str(&format!("Memory: {} MiB\n", self.memory_mib));
|
|
out.push_str(&format!(
|
|
"Machine type: {}\n",
|
|
if self.laptop {
|
|
"laptop"
|
|
} else {
|
|
"desktop/server"
|
|
}
|
|
));
|
|
out.push_str(&format!("Input devices: {}\n\n", self.input_devices));
|
|
|
|
let gpus: Vec<String> = self.gpus.iter().map(|g| g.description.clone()).collect();
|
|
out.push_str(&Self::section("GPUs", &gpus));
|
|
out.push_str(&Self::section(
|
|
"Storage controllers",
|
|
&self.storage_controllers,
|
|
));
|
|
let net: Vec<String> = self
|
|
.network
|
|
.iter()
|
|
.map(|n| {
|
|
format!(
|
|
"{} {} {}",
|
|
n.name,
|
|
if n.wireless { "wireless" } else { "wired" },
|
|
n.operstate
|
|
)
|
|
})
|
|
.collect();
|
|
out.push_str(&Self::section("Network", &net));
|
|
out.push_str(&Self::section("Bluetooth", &self.bluetooth));
|
|
out.push_str(&Self::section("Audio", &self.audio));
|
|
out.push_str(&Self::section("USB controllers", &self.usb_controllers));
|
|
out
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn low_memory_recommends_safe() {
|
|
let mut hw = HardwareReport {
|
|
memory_mib: 1024,
|
|
..Default::default()
|
|
};
|
|
assert_eq!(hw.recommended_kernel_profile(), KernelProfile::Safe);
|
|
hw.memory_mib = 8192;
|
|
assert_eq!(hw.recommended_kernel_profile(), KernelProfile::Optimized);
|
|
}
|
|
|
|
#[test]
|
|
fn gpu_vendor_dedup_and_default() {
|
|
let hw = HardwareReport {
|
|
gpus: vec![
|
|
GpuInfo {
|
|
vendor: GpuVendor::Intel,
|
|
description: "iGPU".into(),
|
|
},
|
|
GpuInfo {
|
|
vendor: GpuVendor::Intel,
|
|
description: "iGPU2".into(),
|
|
},
|
|
GpuInfo {
|
|
vendor: GpuVendor::Nvidia,
|
|
description: "dGPU".into(),
|
|
},
|
|
],
|
|
..Default::default()
|
|
};
|
|
assert_eq!(hw.gpu_vendors(), vec![GpuVendor::Intel, GpuVendor::Nvidia]);
|
|
|
|
let empty = HardwareReport::default();
|
|
assert_eq!(empty.gpu_vendors(), vec![GpuVendor::Unknown]);
|
|
}
|
|
|
|
#[test]
|
|
fn cpu_feature_probe() {
|
|
let cpu = CpuInfo {
|
|
features: vec!["aes".into(), "avx2".into()],
|
|
..Default::default()
|
|
};
|
|
assert!(cpu.has_feature("aes"));
|
|
assert!(!cpu.has_feature("svm"));
|
|
assert!(cpu.virt_summary().contains("AVX2"));
|
|
}
|
|
}
|