1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::{collections::HashMap, fmt, path::PathBuf};
use crate::{hw_mon::HwMon, sysfs::SysFS};
#[derive(Debug)]
pub struct GpuController {
sysfs_path: PathBuf,
pub hw_monitors: Vec<HwMon>,
}
impl GpuController {
pub fn new_from_path(sysfs_path: PathBuf) -> Result<Self, GpuControllerError> {
let mut hw_monitors = Vec::new();
if let Ok(hw_mons_iter) = std::fs::read_dir(sysfs_path.join("hwmon")) {
for hw_mon_dir in hw_mons_iter {
if let Ok(hw_mon_dir) = hw_mon_dir {
if let Ok(hw_mon) = HwMon::new_from_path(hw_mon_dir.path()) {
hw_monitors.push(hw_mon);
}
}
}
}
let gpu_controller = Self {
sysfs_path,
hw_monitors,
};
gpu_controller.get_uevent()?;
Ok(gpu_controller)
}
fn get_uevent(&self) -> Result<HashMap<String, String>, GpuControllerError> {
let raw = self
.read_file("uevent")
.ok_or_else(|| GpuControllerError::InvalidSysFS)?;
let mut uevent = HashMap::new();
for line in raw.trim().split('\n') {
let (key, value) = line
.split_once("=")
.ok_or_else(|| GpuControllerError::ParseError("Missing =".to_string()))?;
uevent.insert(key.to_owned(), value.to_owned());
}
Ok(uevent)
}
pub fn get_driver(&self) -> String {
self.get_uevent().unwrap().get("DRIVER").unwrap().clone()
}
pub fn get_total_vram(&self) -> Option<u64> {
match self.read_file("mem_info_vram_total") {
Some(total_vram) => {
let total_vram = total_vram
.trim()
.parse()
.expect("Unexpected VRAM amount (driver bug?)");
if total_vram == 0 {
None
} else {
Some(total_vram)
}
}
None => todo!(),
}
}
pub fn get_used_vram(&self) -> Option<u64> {
match self.read_file("mem_info_vram_used") {
Some(total_vram) => {
let used_vram = total_vram
.trim()
.parse()
.expect("Unexpected VRAM amount (driver bug?)");
if used_vram == 0 {
None
} else {
Some(used_vram)
}
}
None => todo!(),
}
}
pub fn get_busy_percent(&self) -> Option<u8> {
self.read_file("gpu_busy_percent").map(|c| {
c.trim()
.parse()
.expect("Unexpected GPU load percentage (driver bug?)")
})
}
pub fn get_vbios_version(&self) -> Option<String> {
self.read_file("vbios_version")
}
pub fn get_power_level(&self) -> Option<PowerLevel> {
self.read_file("power_dpm_force_performance_level")
.map(|power_level| {
PowerLevel::from_str(&power_level).expect("Unexpected power level (driver bug?)")
})
}
}
impl SysFS for GpuController {
fn get_path(&self) -> &std::path::Path {
&self.sysfs_path
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PowerLevel {
Auto,
Low,
High,
}
impl Default for PowerLevel {
fn default() -> Self {
PowerLevel::Auto
}
}
impl PowerLevel {
pub fn from_str(power_level: &str) -> Result<Self, GpuControllerError> {
match power_level {
"auto" | "Automatic" => Ok(PowerLevel::Auto),
"high" | "Highest Clocks" => Ok(PowerLevel::High),
"low" | "Lowest Clocks" => Ok(PowerLevel::Low),
_ => Err(GpuControllerError::ParseError(
"unrecognized GPU power profile".to_string(),
)),
}
}
}
impl fmt::Display for PowerLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
PowerLevel::Auto => "auto",
PowerLevel::High => "high",
PowerLevel::Low => "low",
}
)
}
}
#[derive(Debug)]
pub enum GpuControllerError {
InvalidSysFS,
ParseError(String),
IoError(std::io::Error),
}
impl From<std::io::Error> for GpuControllerError {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}