-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
config.rs
401 lines (362 loc) · 14.1 KB
/
config.rs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use crate::server::gpu_controller::fan_control::FanCurve;
use amdgpu_sysfs::gpu_handle::{PerformanceLevel, PowerLevelKind};
use anyhow::Context;
use indexmap::IndexMap;
use lact_schema::{default_fan_curve, request::SetClocksCommand, FanControlMode, PmfwOptions};
use nix::unistd::getuid;
use notify::{RecommendedWatcher, Watcher};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::{
collections::HashMap,
env, fs,
path::PathBuf,
sync::{Arc, Mutex},
time::{Duration, Instant},
};
use tokio::{sync::mpsc, time};
use tracing::{debug, error};
const FILE_NAME: &str = "config.yaml";
const DEFAULT_ADMIN_GROUPS: [&str; 2] = ["wheel", "sudo"];
/// Minimum amount of time between separate config reloads
const CONFIG_RELOAD_INTERVAL_MILLIS: u64 = 50;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Config {
pub daemon: Daemon,
#[serde(default = "default_apply_settings_timer")]
pub apply_settings_timer: u64,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
gpus: HashMap<String, Gpu>,
#[serde(default, skip_serializing_if = "IndexMap::is_empty")]
pub profiles: IndexMap<String, Profile>,
#[serde(default)]
pub current_profile: Option<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
daemon: Daemon::default(),
apply_settings_timer: default_apply_settings_timer(),
gpus: HashMap::new(),
profiles: IndexMap::new(),
current_profile: None,
}
}
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Daemon {
pub log_level: String,
pub admin_groups: Vec<String>,
#[serde(default)]
pub disable_clocks_cleanup: bool,
pub tcp_listen_address: Option<String>,
}
impl Default for Daemon {
fn default() -> Self {
Self {
log_level: "info".to_owned(),
admin_groups: DEFAULT_ADMIN_GROUPS.map(str::to_owned).to_vec(),
disable_clocks_cleanup: false,
tcp_listen_address: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct Profile {
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub gpus: HashMap<String, Gpu>,
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct Gpu {
pub fan_control_enabled: bool,
pub fan_control_settings: Option<FanControlSettings>,
#[serde(default, skip_serializing_if = "PmfwOptions::is_empty")]
pub pmfw_options: PmfwOptions,
pub power_cap: Option<f64>,
pub performance_level: Option<PerformanceLevel>,
#[serde(default, flatten)]
pub clocks_configuration: ClocksConfiguration,
pub power_profile_mode_index: Option<u16>,
/// Outer vector is for power profile components, inner vector is for the heuristics within a component
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub custom_power_profile_mode_hueristics: Vec<Vec<Option<i32>>>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub power_states: HashMap<PowerLevelKind, Vec<u8>>,
}
#[skip_serializing_none]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct ClocksConfiguration {
pub min_core_clock: Option<i32>,
pub min_memory_clock: Option<i32>,
pub min_voltage: Option<i32>,
pub max_core_clock: Option<i32>,
pub max_memory_clock: Option<i32>,
pub max_voltage: Option<i32>,
pub voltage_offset: Option<i32>,
}
impl Gpu {
pub fn is_core_clocks_used(&self) -> bool {
self.clocks_configuration != ClocksConfiguration::default()
}
pub fn apply_clocks_command(&mut self, command: &SetClocksCommand) {
let clocks = &mut self.clocks_configuration;
match command {
SetClocksCommand::MaxCoreClock(clock) => clocks.max_core_clock = Some(*clock),
SetClocksCommand::MaxMemoryClock(clock) => clocks.max_memory_clock = Some(*clock),
SetClocksCommand::MaxVoltage(voltage) => clocks.max_voltage = Some(*voltage),
SetClocksCommand::MinCoreClock(clock) => clocks.min_core_clock = Some(*clock),
SetClocksCommand::MinMemoryClock(clock) => clocks.min_memory_clock = Some(*clock),
SetClocksCommand::MinVoltage(voltage) => clocks.min_voltage = Some(*voltage),
SetClocksCommand::VoltageOffset(offset) => clocks.voltage_offset = Some(*offset),
SetClocksCommand::Reset => {
*clocks = ClocksConfiguration::default();
assert!(!self.is_core_clocks_used());
}
}
}
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FanControlSettings {
#[serde(default)]
pub mode: FanControlMode,
#[serde(default = "default_fan_static_speed")]
pub static_speed: f64,
pub temperature_key: String,
pub interval_ms: u64,
pub curve: FanCurve,
pub spindown_delay_ms: Option<u64>,
pub change_threshold: Option<u64>,
}
impl Default for FanControlSettings {
fn default() -> Self {
Self {
mode: FanControlMode::default(),
static_speed: default_fan_static_speed(),
temperature_key: "edge".to_owned(),
interval_ms: 500,
curve: FanCurve(default_fan_curve()),
spindown_delay_ms: None,
change_threshold: None,
}
}
}
pub fn default_fan_static_speed() -> f64 {
0.5
}
impl Config {
pub fn load() -> anyhow::Result<Option<Self>> {
let path = get_path();
if path.exists() {
let raw_config = fs::read_to_string(path).context("Could not open config file")?;
let config =
serde_yaml::from_str(&raw_config).context("Could not deserialize config")?;
Ok(Some(config))
} else {
let parent = path.parent().unwrap();
fs::create_dir_all(parent)?;
Ok(None)
}
}
pub fn save(&self) -> anyhow::Result<()> {
let path = get_path();
debug!("saving config to {path:?}");
let raw_config = serde_yaml::to_string(self)?;
fs::write(path, raw_config).context("Could not write config")
}
pub fn load_or_create() -> anyhow::Result<Self> {
if let Some(config) = Config::load()? {
Ok(config)
} else {
let config = Config::default();
config.save()?;
Ok(config)
}
}
/// Gets the GPU configs according to the current profile. Returns an error if the current profile could not be found.
pub fn gpus(&self) -> anyhow::Result<&HashMap<String, Gpu>> {
match &self.current_profile {
Some(profile) => {
let profile = self
.profiles
.get(profile)
.with_context(|| format!("Could not find profile '{profile}'"))?;
Ok(&profile.gpus)
}
None => Ok(&self.gpus),
}
}
/// Same as [`gpus`], but with a mutable reference
pub fn gpus_mut(&mut self) -> anyhow::Result<&mut HashMap<String, Gpu>> {
match &self.current_profile {
Some(profile) => {
let profile = self
.profiles
.get_mut(profile)
.with_context(|| format!("Could not find profile '{profile}'"))?;
Ok(&mut profile.gpus)
}
None => Ok(&mut self.gpus),
}
}
/// Get a specific profile
pub fn profile(&self, profile: &str) -> anyhow::Result<&Profile> {
self.profiles
.get(profile)
.with_context(|| format!("Profile {profile} not found"))
}
/// Get the settings for "default" profile (aka no profile)
pub fn default_profile(&self) -> Profile {
Profile {
gpus: self.gpus.clone(),
}
}
pub fn clear(&mut self) {
self.gpus.clear();
self.profiles.clear();
self.current_profile = None;
}
}
pub fn start_watcher(config_last_saved: Arc<Mutex<Instant>>) -> mpsc::UnboundedReceiver<Config> {
let (config_tx, config_rx) = mpsc::unbounded_channel();
let (event_tx, mut event_rx) = mpsc::channel(64);
tokio::spawn(async move {
let mut watcher =
RecommendedWatcher::new(SenderEventHandler(event_tx), notify::Config::default())
.expect("Could not create config file watcher");
let config_path = get_path();
let watch_path = config_path
.parent()
.expect("Config path always has a parent");
watcher
.watch(watch_path, notify::RecursiveMode::Recursive)
.expect("Could not subscribe to config file changes");
while let Some(res) = event_rx.recv().await {
debug!("got config file event {res:?}");
match res {
Ok(event) => {
use notify::EventKind;
if let EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_) =
event.kind
{
if config_last_saved.lock().unwrap().elapsed()
< Duration::from_millis(CONFIG_RELOAD_INTERVAL_MILLIS)
{
debug!("ignoring fs event after self-inflicted config change");
continue;
}
// Accumulate FS events, reload config only after a period has passed since the last event
debug!(
"waiting for {CONFIG_RELOAD_INTERVAL_MILLIS}ms before reloading config"
);
let timeout =
time::sleep(Duration::from_millis(CONFIG_RELOAD_INTERVAL_MILLIS));
tokio::pin!(timeout);
loop {
tokio::select! {
() = &mut timeout => {
break;
}
Some(res) = event_rx.recv() => {
match res {
Ok(event) => {
if let EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_) = event.kind {
debug!("got another fs event, resetting reload timer");
timeout.as_mut().reset(time::Instant::now() + Duration::from_millis(CONFIG_RELOAD_INTERVAL_MILLIS));
}
}
Err(err) => error!("filesystem event error: {err}")
}
}
}
}
match Config::load() {
Ok(Some(new_config)) => config_tx.send(new_config).unwrap(),
Ok(None) => error!("config was removed!"),
Err(err) => {
error!("could not read config after it was changed: {err:#}");
}
}
}
}
Err(err) => error!("filesystem event error: {err}"),
}
}
debug!("registered config file event listener at path {watch_path:?}");
});
config_rx
}
struct SenderEventHandler(mpsc::Sender<notify::Result<notify::Event>>);
impl notify::EventHandler for SenderEventHandler {
fn handle_event(&mut self, event: notify::Result<notify::Event>) {
let _ = self.0.blocking_send(event);
}
}
fn get_path() -> PathBuf {
let uid = getuid();
if uid.is_root() {
PathBuf::from("/etc/lact").join(FILE_NAME)
} else {
let config_dir = PathBuf::from(env::var("XDG_CONFIG_HOME").unwrap_or_else(|_| {
let home = env::var("HOME").expect("$HOME variable is not set");
format!("{home}/.config")
}));
config_dir.join("lact").join(FILE_NAME)
}
}
fn default_apply_settings_timer() -> u64 {
5
}
#[cfg(test)]
mod tests {
use super::{ClocksConfiguration, Config, Daemon, FanControlSettings, Gpu};
use crate::server::gpu_controller::fan_control::FanCurve;
use lact_schema::{FanControlMode, PmfwOptions};
use std::collections::HashMap;
#[test]
fn serde_de_full() {
let config = Config {
daemon: Daemon::default(),
gpus: [(
"my-gpu-id".to_owned(),
Gpu {
fan_control_enabled: true,
fan_control_settings: Some(FanControlSettings {
curve: FanCurve::default(),
temperature_key: "edge".to_owned(),
interval_ms: 500,
mode: FanControlMode::Curve,
static_speed: 0.5,
spindown_delay_ms: Some(5000),
change_threshold: Some(3),
}),
..Default::default()
},
)]
.into(),
..Default::default()
};
let data = serde_yaml::to_string(&config).unwrap();
let deserialized_config: Config = serde_yaml::from_str(&data).unwrap();
assert_eq!(config, deserialized_config);
}
#[test]
fn clocks_configuration_applied() {
let mut gpu = Gpu {
fan_control_enabled: false,
fan_control_settings: None,
pmfw_options: PmfwOptions::default(),
power_cap: None,
performance_level: None,
clocks_configuration: ClocksConfiguration::default(),
power_profile_mode_index: None,
custom_power_profile_mode_hueristics: vec![],
power_states: HashMap::new(),
};
assert!(!gpu.is_core_clocks_used());
gpu.clocks_configuration.voltage_offset = Some(10);
assert!(gpu.is_core_clocks_used());
}
}