a500e25705
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260 lines
8.1 KiB
Rust
260 lines
8.1 KiB
Rust
//! The machine a report was measured on. CPU, RAM and OS are detected here; the GPU and the display
|
|
//! come from the program that rendered (the game knows its adapter, the launcher its window).
|
|
|
|
use std::process::Command;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
pub struct Hardware {
|
|
/// `macos`, `linux`, `windows`.
|
|
pub os: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub os_version: Option<String>,
|
|
pub arch: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub cpu: Option<String>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub cpu_threads: Option<usize>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub ram_mb: Option<u64>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub gpu: Option<Gpu>,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub display: Option<Display>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
pub struct Gpu {
|
|
pub name: String,
|
|
pub vendor: String,
|
|
pub driver: String,
|
|
/// `metal`, `vulkan`, `dx12`, `gl`.
|
|
pub backend: String,
|
|
/// `integrated`, `discrete`, `virtual`, `cpu`, `other`.
|
|
pub kind: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
pub struct Display {
|
|
/// Physical pixels.
|
|
pub width: u32,
|
|
pub height: u32,
|
|
pub scale: f64,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub refresh_hz: Option<u32>,
|
|
}
|
|
|
|
impl Hardware {
|
|
pub fn detect() -> Hardware {
|
|
Hardware {
|
|
os: std::env::consts::OS.to_owned(),
|
|
os_version: os_version(),
|
|
arch: std::env::consts::ARCH.to_owned(),
|
|
cpu: cpu(),
|
|
cpu_threads: std::thread::available_parallelism().ok().map(|n| n.get()),
|
|
ram_mb: ram_bytes().map(|b| b >> 20),
|
|
gpu: None,
|
|
display: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The one-minute load average: above about the number of threads the machine was busy.
|
|
pub fn load_average() -> Option<f64> {
|
|
if cfg!(target_os = "macos") {
|
|
// "{ 1.23 1.10 1.00 }"
|
|
output("sysctl", &["-n", "vm.loadavg"])?
|
|
.trim_start_matches('{')
|
|
.split_whitespace()
|
|
.next()?
|
|
.parse()
|
|
.ok()
|
|
} else {
|
|
std::fs::read_to_string("/proc/loadavg")
|
|
.ok()?
|
|
.split_whitespace()
|
|
.next()?
|
|
.parse()
|
|
.ok()
|
|
}
|
|
}
|
|
|
|
/// `ac` or `battery`; `None` where it can't be read (and on machines without a battery on Linux).
|
|
pub fn power_source() -> Option<&'static str> {
|
|
if cfg!(target_os = "macos") {
|
|
let batt = output("pmset", &["-g", "batt"])?;
|
|
return Some(if batt.contains("'Battery Power'") {
|
|
"battery"
|
|
} else {
|
|
"ac"
|
|
});
|
|
}
|
|
let mut on_battery = None;
|
|
for entry in std::fs::read_dir("/sys/class/power_supply").ok()?.flatten() {
|
|
let read =
|
|
|name: &str| std::fs::read_to_string(entry.path().join(name)).unwrap_or_default();
|
|
match read("type").trim() {
|
|
"Mains" if read("online").trim() == "1" => return Some("ac"),
|
|
"Battery" => on_battery = Some("battery"),
|
|
_ => {}
|
|
}
|
|
}
|
|
on_battery
|
|
}
|
|
|
|
/// Resident memory of a process, in bytes (macOS, Linux).
|
|
pub fn rss_bytes(pid: u32) -> Option<u64> {
|
|
if cfg!(target_os = "macos") {
|
|
return output("ps", &["-o", "rss=", "-p", &pid.to_string()])?
|
|
.parse::<u64>()
|
|
.ok()
|
|
.map(|kb| kb * 1024);
|
|
}
|
|
let status = std::fs::read_to_string(format!("/proc/{pid}/status")).ok()?;
|
|
let line = status.lines().find(|l| l.starts_with("VmRSS:"))?;
|
|
line.split_whitespace()
|
|
.nth(1)?
|
|
.parse::<u64>()
|
|
.ok()
|
|
.map(|kb| kb * 1024)
|
|
}
|
|
|
|
/// CPU time a process has used, user and system, in seconds (macOS, Linux).
|
|
pub fn cpu_seconds(pid: u32) -> Option<f64> {
|
|
if cfg!(target_os = "macos") {
|
|
// "[[dd-]hh:]mm:ss.cc"
|
|
let time = output("ps", &["-o", "time=", "-p", &pid.to_string()])?;
|
|
let (rest, days) = match time.split_once('-') {
|
|
Some((d, rest)) => (rest.to_owned(), d.parse::<f64>().ok()?),
|
|
None => (time, 0.0),
|
|
};
|
|
let seconds = rest.split(':').try_fold(0.0, |acc, part| {
|
|
part.parse::<f64>().ok().map(|v| acc * 60.0 + v)
|
|
})?;
|
|
return Some(days * 86_400.0 + seconds);
|
|
}
|
|
// utime and stime, fields 14 and 15, in clock ticks (100 per second on Linux).
|
|
let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
|
|
let fields: Vec<&str> = stat.rsplit_once(')')?.1.split_whitespace().collect();
|
|
let ticks: f64 = fields.get(11)?.parse::<f64>().ok()? + fields.get(12)?.parse::<f64>().ok()?;
|
|
Some(ticks / 100.0)
|
|
}
|
|
|
|
fn output(program: &str, args: &[&str]) -> Option<String> {
|
|
let out = Command::new(program).args(args).output().ok()?;
|
|
let text = String::from_utf8(out.stdout).ok()?.trim().to_owned();
|
|
(out.status.success() && !text.is_empty()).then_some(text)
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
fn os_version() -> Option<String> {
|
|
output("sw_vers", &["-productVersion"])
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
fn cpu() -> Option<String> {
|
|
output("sysctl", &["-n", "machdep.cpu.brand_string"])
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
fn ram_bytes() -> Option<u64> {
|
|
output("sysctl", &["-n", "hw.memsize"])?.parse().ok()
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
fn os_version() -> Option<String> {
|
|
let release = std::fs::read_to_string("/etc/os-release").ok()?;
|
|
let name = key_value(&release, "PRETTY_NAME", '=')?
|
|
.trim_matches('"')
|
|
.to_owned();
|
|
Some(match output("uname", &["-r"]) {
|
|
Some(kernel) => format!("{name} (kernel {kernel})"),
|
|
None => name,
|
|
})
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
fn cpu() -> Option<String> {
|
|
let info = std::fs::read_to_string("/proc/cpuinfo").ok()?;
|
|
// x86 names the CPU per core; ARM boards name the machine once.
|
|
key_value(&info, "model name", ':')
|
|
.or_else(|| key_value(&info, "Model", ':'))
|
|
.map(str::to_owned)
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
fn ram_bytes() -> Option<u64> {
|
|
let info = std::fs::read_to_string("/proc/meminfo").ok()?;
|
|
let kb = key_value(&info, "MemTotal", ':')?
|
|
.trim_end_matches("kB")
|
|
.trim();
|
|
kb.parse::<u64>().ok().map(|kb| kb * 1024)
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
fn key_value<'a>(text: &'a str, key: &str, sep: char) -> Option<&'a str> {
|
|
text.lines().find_map(|line| {
|
|
let (k, v) = line.split_once(sep)?;
|
|
(k.trim() == key).then(|| v.trim())
|
|
})
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
fn os_version() -> Option<String> {
|
|
output("cmd", &["/C", "ver"])
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
fn cpu() -> Option<String> {
|
|
std::env::var("PROCESSOR_IDENTIFIER").ok()
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
fn ram_bytes() -> Option<u64> {
|
|
let out = output(
|
|
"powershell",
|
|
&[
|
|
"-NoProfile",
|
|
"-Command",
|
|
"(Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory",
|
|
],
|
|
)?;
|
|
out.parse().ok()
|
|
}
|
|
|
|
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
|
|
fn os_version() -> Option<String> {
|
|
None
|
|
}
|
|
|
|
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
|
|
fn cpu() -> Option<String> {
|
|
None
|
|
}
|
|
|
|
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
|
|
fn ram_bytes() -> Option<u64> {
|
|
None
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn detects_this_machine() {
|
|
let hw = Hardware::detect();
|
|
assert_eq!(hw.os, std::env::consts::OS);
|
|
assert!(hw.cpu_threads.unwrap() >= 1);
|
|
if cfg!(any(target_os = "macos", target_os = "linux")) {
|
|
assert!(hw.cpu.is_some(), "{hw:?}");
|
|
assert!(hw.ram_mb.unwrap() > 256, "{hw:?}");
|
|
assert!(hw.os_version.is_some(), "{hw:?}");
|
|
assert!(load_average().is_some());
|
|
assert!(rss_bytes(std::process::id()).unwrap() > 1 << 20);
|
|
assert!(cpu_seconds(std::process::id()).unwrap() >= 0.0);
|
|
}
|
|
}
|
|
}
|