-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtdp.rs
More file actions
200 lines (174 loc) · 6.54 KB
/
Copy pathtdp.rs
File metadata and controls
200 lines (174 loc) · 6.54 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
use crate::performance::gpu::{
platform::hardware::Hardware,
tdp::{HardwareAccess, TDPDevice, TDPError, TDPResult},
};
/// Implementation of TDP control for Intel GPUs
pub struct Tdp {
//pub path: String,
hardware: Option<Hardware>,
base_path: Option<PathBuf>,
}
impl HardwareAccess for Tdp {
fn hardware(&self) -> Option<&Hardware> {
self.hardware.as_ref()
}
}
impl Tdp {
pub fn new(_path: String) -> Tdp {
let hardware = match Hardware::new() {
Some(hardware) => {
log::info!("Found Hardware interface for TDP control");
Some(hardware)
}
None => None,
};
// Discover the package domain path
let mut base_path = None;
if let Ok(mut rapl_dir) = fs::read_dir("/sys/class/powercap/intel-rapl") {
while let Some(Ok(entry)) = rapl_dir.next() {
let Ok(file_type) = entry.file_type() else {
continue;
};
if !file_type.is_dir() {
continue;
}
let file_name = entry.file_name();
let Some(file_name) = file_name.to_str() else {
continue;
};
if !file_name.starts_with("intel-rapl:") {
continue;
}
let domain_path = entry.path();
let name_path = domain_path.join("name");
let Ok(name) = fs::read_to_string(name_path) else {
continue;
};
if !name.as_str().trim().starts_with("package") {
continue;
}
base_path = Some(domain_path);
break;
}
}
Tdp {
hardware,
base_path,
}
}
}
impl TDPDevice for Tdp {
async fn tdp(&self) -> TDPResult<f64> {
let Some(base_path) = self.base_path.as_ref() else {
return Err(TDPError::FeatureUnsupported);
};
let path = base_path.join("constraint_0_power_limit_uw");
let result = fs::read_to_string(path);
let content = result.map_err(|err| TDPError::IOError(err.to_string()))?;
let content = content.trim();
// Parse the output to get the long TDP
let long_tdp = match content.parse::<f64>() {
Ok(v) => v,
Err(e) => {
log::error!("{}", e);
return Err(TDPError::FailedOperation(e.to_string()));
}
};
Ok(long_tdp / 1000000.0)
}
async fn set_tdp(&mut self, value: f64) -> TDPResult<()> {
let Some(base_path) = self.base_path.as_ref() else {
return Err(TDPError::FeatureUnsupported);
};
if value < 1.0 {
let err = "Cowardly refusing to set TDP less than 1";
log::warn!("{}", err);
return Err(TDPError::InvalidArgument(String::from(err)));
}
// Get the current boost value so the peak tdp can be set *boost*
// distance away.
let mut boost = self.boost().await?;
if boost < 0.0 {
log::warn!("Boost is less than 0, setting to 0");
boost = 0.0;
}
// Open the sysfs file to write to
let path = base_path.join("constraint_0_power_limit_uw");
let file = OpenOptions::new().write(true).open(path);
// Convert the value to a writable string
let value = format!("{}", value * 1000000.0);
// Write the value
file.map_err(|err| TDPError::FailedOperation(err.to_string()))?
.write_all(value.as_bytes())
.map_err(|err| TDPError::IOError(err.to_string()))?;
// Update the boost value
self.set_boost(boost).await
}
async fn boost(&self) -> TDPResult<f64> {
let Some(base_path) = self.base_path.as_ref() else {
return Err(TDPError::FeatureUnsupported);
};
let path = base_path.join("constraint_1_power_limit_uw");
let result = fs::read_to_string(path);
let content = result.map_err(|err| TDPError::IOError(err.to_string()))?;
let content = content.trim();
// Parse the output to get the peak TDP
let peak_tdp = match content.parse::<f64>() {
Ok(v) => v,
Err(e) => {
log::error!("{}", e);
return Err(TDPError::FailedOperation(e.to_string()));
}
};
let tdp = self.tdp().await?;
Ok((peak_tdp / 1000000.0) - tdp)
}
async fn set_boost(&mut self, value: f64) -> TDPResult<()> {
let Some(base_path) = self.base_path.as_ref() else {
return Err(TDPError::FeatureUnsupported);
};
log::debug!("Setting Boost: {}", value);
if value < 0.0 {
let err = "Cowardly refusing to set TDP Boost less than 0";
log::warn!("{}", err);
return Err(TDPError::InvalidArgument(String::from(err)));
}
let tdp = self.tdp().await?;
let boost = value;
let short_tdp = if boost > 0.0 {
(boost + tdp) * 1000000.0
} else {
tdp * 1000000.0
};
// Write the short tdp
let path = base_path.join("constraint_1_power_limit_uw");
let file = OpenOptions::new().write(true).open(path);
let value = format!("{}", short_tdp);
file.map_err(|err| TDPError::FailedOperation(err.to_string()))?
.write_all(value.as_bytes())
.map_err(|err| TDPError::IOError(err.to_string()))
}
async fn thermal_throttle_limit_c(&self) -> TDPResult<f64> {
log::error!("Thermal throttling not supported on intel gpu");
Err(TDPError::FeatureUnsupported)
}
async fn set_thermal_throttle_limit_c(&mut self, _limit: f64) -> TDPResult<()> {
log::error!("Thermal throttling not supported on intel gpu");
Err(TDPError::FeatureUnsupported)
}
async fn power_profile(&self) -> TDPResult<String> {
log::error!("Power profiles not supported on intel gpu");
Err(TDPError::FeatureUnsupported)
}
async fn set_power_profile(&mut self, _profile: String) -> TDPResult<()> {
log::error!("Power profiles not supported on intel gpu");
Err(TDPError::FeatureUnsupported)
}
async fn power_profiles_available(&self) -> TDPResult<Vec<String>> {
log::error!("Power profiles not supported on intel gpu");
Err(TDPError::FeatureUnsupported)
}
}