#[allow(unused_imports)]
use super::{ProfileGroup, ProfileProvider, Setting, SettingRisk, SettingValue, Subsystem};
pub struct GpuProfileProvider {
_private: (),
}
#[cfg(feature = "nvidia")]
fn pstate_name(p: nvml_wrapper::enum_wrappers::device::PerformanceState) -> Option<String> {
use nvml_wrapper::enum_wrappers::device::PerformanceState as P;
match p {
P::Zero => Some("P0".into()),
P::One => Some("P1".into()),
P::Two => Some("P2".into()),
P::Three => Some("P3".into()),
P::Four => Some("P4".into()),
P::Five => Some("P5".into()),
P::Six => Some("P6".into()),
P::Seven => Some("P7".into()),
P::Eight => Some("P8".into()),
P::Nine => Some("P9".into()),
P::Ten => Some("P10".into()),
P::Eleven => Some("P11".into()),
P::Twelve => Some("P12".into()),
P::Thirteen => Some("P13".into()),
P::Fourteen => Some("P14".into()),
P::Fifteen => Some("P15".into()),
P::Unknown => None,
}
}
impl GpuProfileProvider {
pub fn new() -> Self {
Self { _private: () }
}
}
impl Default for GpuProfileProvider {
fn default() -> Self {
Self::new()
}
}
impl ProfileProvider for GpuProfileProvider {
fn subsystem(&self) -> Subsystem {
Subsystem::Gpu
}
fn snapshot(&mut self) -> Result<Vec<ProfileGroup>, String> {
#[allow(unused_mut)]
let mut groups = Vec::new();
let mut failures: Vec<String> = Vec::new();
#[cfg(feature = "nvidia")]
match nvidia_groups() {
Ok(found) => groups.extend(found),
Err(reason) => failures.push(reason),
}
#[cfg(all(target_os = "linux", feature = "amd"))]
match amd_linux_groups() {
Ok(found) => groups.extend(found),
Err(reason) => failures.push(reason),
}
#[cfg(all(target_os = "linux", feature = "intel"))]
match intel_linux_groups() {
Ok(found) => groups.extend(found),
Err(reason) => failures.push(reason),
}
#[cfg(windows)]
match windows_display_class_groups() {
Ok(found) => groups.extend(found),
Err(reason) => failures.push(reason),
}
#[cfg(windows)]
let drs_unread: Vec<String> = {
let (drs_groups, unread) = super::split_scan(super::nvidia_drs::scan_drs_groups());
groups.extend(drs_groups);
unread
};
#[cfg(not(windows))]
let drs_unread: Vec<String> = Vec::new();
if groups.is_empty() {
failures.extend(
drs_unread
.iter()
.map(|r| format!("NVIDIA DRS not read: {r}")),
);
if failures.is_empty() {
failures.push(
"No supported GPU driver surfaces were readable on this platform. \
Compile with --features nvidia,amd,intel and run with adequate \
permissions for full data."
.to_string(),
);
}
return Err(failures.join("; "));
}
groups.extend(super::not_read_groups(
Subsystem::Gpu,
"GPU driver",
&failures,
));
groups.extend(super::not_read_groups(
Subsystem::Gpu,
"NVIDIA DRS",
&drs_unread,
));
Ok(groups)
}
}
#[cfg(feature = "nvidia")]
fn nvml_text(v: &std::result::Result<String, String>) -> SettingValue {
match v {
Ok(s) => SettingValue::Text(s.clone()),
Err(reason) => SettingValue::Unreadable(reason.clone()),
}
}
#[cfg(feature = "nvidia")]
fn nvidia_groups() -> Result<Vec<ProfileGroup>, String> {
use nvml_wrapper::enum_wrappers::device::{Clock, ClockId};
use nvml_wrapper::Nvml;
let nvml = match Nvml::init() {
Ok(n) => n,
Err(e) => return Err(format!("NVML init failed: {e}")),
};
let count = match nvml.device_count() {
Ok(c) => c,
Err(e) => {
let mut g = ProfileGroup::new(
Subsystem::Gpu,
"(NVIDIA device count unreadable)",
"Global driver profile",
"NVML device_count",
);
g.note(format!(
"NVML device_count failed: {}. No NVIDIA devices were enumerated; this is not a count of zero.",
e
));
return Ok(vec![g]);
}
};
let mut groups = Vec::new();
let driver_version = nvml.sys_driver_version().map_err(|e| e.to_string());
let nvml_version = nvml.sys_nvml_version().map_err(|e| e.to_string());
for i in 0..count {
let dev = match nvml.device_by_index(i) {
Ok(dev) => dev,
Err(e) => {
let mut g = ProfileGroup::new(
Subsystem::Gpu,
"(NVIDIA device unreadable)",
"Global driver profile",
format!("NVML index {}", i),
);
g.note(format!(
"NVML device_by_index({}) failed: {}. The adapter exists but was not read.",
i, e
));
groups.push(g);
continue;
}
};
let name_result = dev.name();
let name = match &name_result {
Ok(n) => n.clone(),
Err(_) => "(name unreadable)".to_string(),
};
let mut g = ProfileGroup::new(
Subsystem::Gpu,
&name,
"Global driver profile",
format!("NVML index {}", i),
);
g.key = Some(format!("NVML index {}", i));
if let Err(e) = &name_result {
g.note(format!("NVML name query failed: {}", e));
}
g.push(
Setting::info(
"driver_version",
"Driver Version",
nvml_text(&driver_version),
)
.with_source("NVML"),
);
g.push(
Setting::info("nvml_version", "NVML Version", nvml_text(&nvml_version))
.with_source("NVML"),
);
if let Ok(mode) = dev.is_ecc_enabled() {
g.push(
Setting::info(
"ecc_mode",
"ECC Memory",
SettingValue::Bool(mode.currently_enabled),
)
.with_description(
"Whether ECC error correction is currently active on this GPU's VRAM.",
)
.with_risk(SettingRisk::Moderate)
.with_source("nvmlDeviceIsEccEnabled"),
);
}
#[cfg(target_os = "linux")]
if let Ok(persistent) = dev.is_in_persistent_mode() {
g.push(
Setting::info(
"persistence_mode",
"Persistence Mode",
SettingValue::Bool(persistent),
)
.with_description(
"When enabled, the NVIDIA driver remains loaded with no active client. Reduces first-launch latency. Writable on Linux with root (NVML).",
)
.with_risk(SettingRisk::Safe)
.with_source("nvmlDeviceGetPersistenceMode")
.with_writable(true),
);
}
if let Ok(limit_mw) = dev.enforced_power_limit() {
let mut s = Setting::info(
"power_limit_mw",
"Power Limit",
SettingValue::Uint(limit_mw as u64),
)
.with_unit("mW")
.with_risk(SettingRisk::Moderate)
.with_source("nvmlDeviceGetEnforcedPowerLimit");
if let Ok(constraints) = dev.power_management_limit_constraints() {
s.range = Some((
SettingValue::Uint(constraints.min_limit as u64),
SettingValue::Uint(constraints.max_limit as u64),
));
}
if let Ok(default) = dev.power_management_limit_default() {
s.default = Some(SettingValue::Uint(default as u64));
}
g.push(s);
}
for (clock, label, id) in [
(Clock::Graphics, "Max Graphics Clock", "max_gfx_clock_mhz"),
(Clock::SM, "Max SM Clock", "max_sm_clock_mhz"),
(Clock::Memory, "Max Memory Clock", "max_mem_clock_mhz"),
] {
if let Ok(v) = dev.max_clock_info(clock) {
g.push(
Setting::info(id, label, SettingValue::Uint(v as u64))
.with_unit("MHz")
.with_source("nvmlDeviceGetMaxClockInfo"),
);
}
}
if let Ok(v) = dev.clock(Clock::Graphics, ClockId::Current) {
g.push(
Setting::info(
"current_gfx_clock_mhz",
"Current Graphics Clock",
SettingValue::Uint(v as u64),
)
.with_unit("MHz")
.with_source("nvmlDeviceGetClock"),
);
}
if let Some(pstate) = dev.performance_state().ok().and_then(pstate_name) {
g.push(
Setting::info(
"performance_state",
"Performance State",
SettingValue::Text(pstate),
)
.with_description(
"P0 = highest performance, P15 = lowest. The driver picks this automatically.",
)
.with_source("nvmlDeviceGetPerformanceState"),
);
}
if let Ok(brand) = dev.brand() {
g.push(
Setting::info("brand", "Brand", SettingValue::Text(format!("{:?}", brand)))
.with_source("NVML"),
);
}
if let Ok(vbios) = dev.vbios_version() {
g.push(
Setting::info("vbios_version", "VBIOS Version", SettingValue::Text(vbios))
.with_source("nvmlDeviceGetVbiosVersion"),
);
}
g.note(
"Per-application profiles (NVPI-style DRS database) require NVAPI access \
and are not yet implemented. The values shown here are NVML-readable \
global driver state.",
);
groups.push(g);
}
if let Some(extra) = nvidia_kmod_params_group() {
groups.push(extra);
}
Ok(groups)
}
#[cfg(all(feature = "nvidia", target_os = "linux"))]
fn nvidia_kmod_params_group() -> Option<ProfileGroup> {
let path = std::path::Path::new("/sys/module/nvidia/parameters");
if !path.exists() {
return None;
}
let mut g = ProfileGroup::new(
Subsystem::Gpu,
"NVIDIA kernel module",
"nvidia.ko parameters",
path.display().to_string(),
);
if let Ok(rd) = std::fs::read_dir(path) {
for entry in rd.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
let val = match std::fs::read_to_string(entry.path()) {
Ok(s) => SettingValue::Text(s.trim().to_string()),
Err(e) => SettingValue::Unreadable(e.to_string()),
};
g.push(Setting::info(&name, &name, val).with_source("/sys/module/nvidia/parameters"));
}
}
Some(g)
}
#[cfg(all(feature = "nvidia", not(target_os = "linux")))]
fn nvidia_kmod_params_group() -> Option<ProfileGroup> {
None
}
#[cfg(all(target_os = "linux", feature = "amd"))]
fn amd_linux_groups() -> Result<Vec<ProfileGroup>, String> {
use std::fs;
let mut groups = Vec::new();
let drm = match fs::read_dir("/sys/class/drm") {
Ok(rd) => rd,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(groups),
Err(e) => return Err(format!("/sys/class/drm: {e}")),
};
for entry in drm.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if !name.starts_with("card") || name.contains('-') {
continue;
}
let dev_dir = entry.path().join("device");
let vendor = fs::read_to_string(dev_dir.join("vendor")).unwrap_or_default();
if vendor.trim() != "0x1002" {
continue;
}
let model = fs::read_to_string(dev_dir.join("device"))
.unwrap_or_default()
.trim()
.to_string();
let mut g = ProfileGroup::new(
Subsystem::Gpu,
format!("AMD {} ({})", name, model),
"amdgpu profile",
dev_dir.display().to_string(),
);
for (file, id, label, risk) in [
(
"power_dpm_state",
"power_dpm_state",
"Power DPM State",
SettingRisk::Moderate,
),
(
"power_dpm_force_performance_level",
"perf_level",
"Performance Level",
SettingRisk::Moderate,
), (
"pp_power_profile_mode",
"power_profile_mode",
"Power Profile Mode",
SettingRisk::Moderate,
),
(
"pp_od_clk_voltage",
"od_clk_voltage",
"Overdrive Clock/Voltage Table",
SettingRisk::Dangerous,
),
(
"pp_dpm_sclk",
"dpm_sclk",
"SCLK DPM Table",
SettingRisk::Informational,
),
(
"pp_dpm_mclk",
"dpm_mclk",
"MCLK DPM Table",
SettingRisk::Informational,
),
(
"pp_dpm_pcie",
"dpm_pcie",
"PCIe DPM Table",
SettingRisk::Informational,
),
] {
let p = dev_dir.join(file);
if let Ok(v) = fs::read_to_string(&p) {
let value = if v.contains('\n') {
SettingValue::Text(v.trim().to_string())
} else if let Ok(u) = v.trim().parse::<u64>() {
SettingValue::Uint(u)
} else {
SettingValue::Text(v.trim().to_string())
};
let writable = id == "perf_level";
g.push(
Setting::info(id, label, value)
.with_risk(risk)
.with_source(p.display().to_string())
.with_writable(writable),
);
}
}
if let Ok(hwmon) = fs::read_dir(dev_dir.join("hwmon")) {
for hw in hwmon.flatten() {
let cap = hw.path().join("power1_cap");
if let Ok(v) = fs::read_to_string(&cap) {
if let Ok(uw) = v.trim().parse::<u64>() {
g.push(
Setting::info("power_cap_uw", "Power Cap", SettingValue::Uint(uw))
.with_unit("μW")
.with_risk(SettingRisk::Moderate)
.with_source(cap.display().to_string()),
);
}
}
}
}
groups.push(g);
}
Ok(groups)
}
#[cfg(all(target_os = "linux", feature = "intel"))]
fn intel_linux_groups() -> Result<Vec<ProfileGroup>, String> {
use std::fs;
let mut groups = Vec::new();
let drm = match fs::read_dir("/sys/class/drm") {
Ok(rd) => rd,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(groups),
Err(e) => return Err(format!("/sys/class/drm: {e}")),
};
for entry in drm.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if !name.starts_with("card") || name.contains('-') {
continue;
}
let dev_dir = entry.path().join("device");
let vendor = fs::read_to_string(dev_dir.join("vendor")).unwrap_or_default();
if vendor.trim() != "0x8086" {
continue;
}
let model = fs::read_to_string(dev_dir.join("device"))
.unwrap_or_default()
.trim()
.to_string();
let card_dir = entry.path();
let mut g = ProfileGroup::new(
Subsystem::Gpu,
format!("Intel {} ({})", name, model),
"i915/xe profile",
card_dir.display().to_string(),
);
for (file, id, label) in [
("gt_min_freq_mhz", "gt_min_freq_mhz", "GT Min Frequency"),
("gt_max_freq_mhz", "gt_max_freq_mhz", "GT Max Frequency"),
(
"gt_boost_freq_mhz",
"gt_boost_freq_mhz",
"GT Boost Frequency",
),
("gt_cur_freq_mhz", "gt_cur_freq_mhz", "GT Current Frequency"),
("rc6_enable", "rc6_enable", "RC6 Power Saving"),
] {
let p = card_dir.join(file);
if let Ok(v) = fs::read_to_string(&p) {
let value = v
.trim()
.parse::<u64>()
.map(SettingValue::Uint)
.unwrap_or_else(|_| SettingValue::Text(v.trim().to_string()));
let writable = id == "gt_max_freq_mhz";
g.push(
Setting::info(id, label, value)
.with_unit(if id.ends_with("_mhz") { "MHz" } else { "" })
.with_risk(SettingRisk::Moderate)
.with_source(p.display().to_string())
.with_writable(writable),
);
}
}
groups.push(g);
}
Ok(groups)
}
#[cfg(windows)]
fn registry_text(key: &winreg::RegKey, name: &str) -> SettingValue {
match key.get_value::<String, _>(name) {
Ok(s) if !s.is_empty() => SettingValue::Text(s),
Ok(_) => SettingValue::Unreadable(format!("{} is empty", name)),
Err(e) => SettingValue::Unreadable(format!("{} not readable: {}", name, e)),
}
}
#[cfg(windows)]
fn windows_display_class_groups() -> Result<Vec<ProfileGroup>, String> {
use winreg::enums::*;
use winreg::RegKey;
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let class_path =
r"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}";
let Ok(class_key) = hklm.open_subkey(class_path) else {
return Err("Windows display class registry key unreadable".to_string());
};
let mut groups = Vec::new();
let subkeys: Vec<String> = class_key.enum_keys().filter_map(|k| k.ok()).collect();
for sub in subkeys {
if sub.len() != 4 || !sub.chars().all(|c| c.is_ascii_digit()) {
continue;
}
let Ok(inst) = class_key.open_subkey(&sub) else {
continue;
};
let desc: String = inst.get_value("DriverDesc").unwrap_or_default();
if desc.is_empty() {
continue;
}
let mut g = ProfileGroup::new(
Subsystem::Gpu,
&desc,
"Driver class registry",
format!(r"HKLM\{}\{}", class_path, sub),
);
g.push(Setting::info(
"provider",
"Provider",
registry_text(&inst, "ProviderName"),
));
g.push(Setting::info(
"driver_version",
"Driver Version",
registry_text(&inst, "DriverVersion"),
));
g.push(Setting::info(
"driver_date",
"Driver Date",
registry_text(&inst, "DriverDate"),
));
for name in inst.enum_values().filter_map(|v| v.ok()).map(|(n, _)| n) {
if matches!(
name.as_str(),
"DriverDesc"
| "ProviderName"
| "DriverVersion"
| "DriverDate"
| "DriverDateData"
| "InfPath"
| "InfSection"
| "MatchingDeviceId"
) {
continue;
}
if let Ok(dw) = inst.get_value::<u32, _>(&name) {
g.push(
Setting::info(&name, &name, SettingValue::Uint(dw as u64))
.with_risk(SettingRisk::Moderate)
.with_source("registry"),
);
} else if let Ok(s) = inst.get_value::<String, _>(&name) {
if s.len() < 256 {
g.push(
Setting::info(&name, &name, SettingValue::Text(s))
.with_risk(SettingRisk::Moderate)
.with_source("registry"),
);
}
}
}
g.note(
"Vendor control panels (NVIDIA Control Panel, AMD Adrenalin, Intel Arc \
Control) and tweakers like NVIDIA Profile Inspector write per-driver \
overrides into this registry instance. Per-application profiles \
require NVAPI/ADL access (not yet implemented).",
);
groups.push(g);
}
Ok(groups)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn provider_smoke() {
let mut p = GpuProfileProvider::new();
let _ = p.snapshot();
}
}