chore: clippy pedantic in lact-daemon

This commit is contained in:
Ilya Zlobintsev
2023-02-20 09:04:20 +02:00
parent ede4886da9
commit fd81c7007c
8 changed files with 45 additions and 30 deletions

View File

@@ -10,16 +10,16 @@ const FILE_NAME: &str = "config.yaml";
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct Config {
pub daemon: DaemonConfig,
pub gpus: HashMap<String, GpuConfig>,
pub daemon: Daemon,
pub gpus: HashMap<String, Gpu>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DaemonConfig {
pub struct Daemon {
pub log_level: String,
}
impl Default for DaemonConfig {
impl Default for Daemon {
fn default() -> Self {
Self {
log_level: "info".to_owned(),
@@ -28,7 +28,7 @@ impl Default for DaemonConfig {
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct GpuConfig {
pub struct Gpu {
pub fan_control_enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub fan_control_settings: Option<FanControlSettings>,
@@ -68,13 +68,12 @@ impl Config {
}
pub fn load_or_create() -> anyhow::Result<Self> {
match Config::load()? {
Some(config) => Ok(config),
None => {
let config = Config::default();
config.save()?;
Ok(config)
}
if let Some(config) = Config::load()? {
Ok(config)
} else {
let config = Config::default();
config.save()?;
Ok(config)
}
}
}
@@ -94,16 +93,16 @@ fn get_path() -> PathBuf {
#[cfg(test)]
mod tests {
use super::{Config, DaemonConfig, FanControlSettings, GpuConfig};
use super::{Config, Daemon, FanControlSettings, Gpu};
use crate::server::gpu_controller::fan_control::FanCurve;
#[test]
fn serde_de_full() {
let config = Config {
daemon: DaemonConfig::default(),
daemon: Daemon::default(),
gpus: [(
"my-gpu-id".to_owned(),
GpuConfig {
Gpu {
fan_control_enabled: true,
fan_control_settings: Some(FanControlSettings {
curve: FanCurve::default(),

View File

@@ -87,7 +87,7 @@ mod tests {
})
.unwrap()
};
assert_eq!(response, ["hello", "world", "123"])
assert_eq!(response, ["hello", "world", "123"]);
}
#[test]

View File

@@ -1,3 +1,5 @@
#![warn(clippy::pedantic)]
mod config;
mod fork;
mod server;
@@ -22,6 +24,10 @@ const SHUTDOWN_SIGNALS: [SignalKind; 4] = [
SignalKind::hangup(),
];
/// Run the daemon, binding to the default socket.
///
/// # Errors
/// Returns an error when the daemon cannot initialize.
pub fn run() -> anyhow::Result<()> {
let rt = runtime::Builder::new_current_thread()
.enable_all()
@@ -42,6 +48,11 @@ pub fn run() -> anyhow::Result<()> {
})
}
/// Run the daemon with a given `UnixStream`.
/// This will NOT bind to a socket by itself, and the daemon will only be accessible via the given stream.
///
/// # Errors
/// Returns an error when the daemon cannot initialize.
pub fn run_embedded(stream: StdUnixStream) -> anyhow::Result<()> {
let rt = runtime::Builder::new_current_thread()
.enable_all()

View File

@@ -7,6 +7,11 @@ use tracing::warn;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FanCurve(pub FanCurveMap);
#[allow(
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_sign_loss
)]
impl FanCurve {
pub fn pwm_at_temp(&self, temp: Temperature) -> u8 {
let current = temp.current.expect("No current temp");
@@ -33,7 +38,7 @@ impl FanCurve {
(None, None) => panic!("Could not find fan speed on the curve! This is a bug."),
};
(u8::MAX as f32 * percentage) as u8
(f32::from(u8::MAX) * percentage) as u8
}
}

View File

@@ -2,7 +2,7 @@ pub mod fan_control;
use self::fan_control::FanCurve;
use super::vulkan::get_vulkan_info;
use crate::{config::GpuConfig, fork::run_forked};
use crate::{config, fork::run_forked};
use amdgpu_sysfs::{
error::Error,
gpu_handle::GpuHandle,
@@ -156,7 +156,7 @@ impl GpuController {
}
}
pub fn get_stats(&self, gpu_config: Option<&GpuConfig>) -> anyhow::Result<DeviceStats> {
pub fn get_stats(&self, gpu_config: Option<&config::Gpu>) -> anyhow::Result<DeviceStats> {
let fan_control_enabled = self
.fan_control_handle
.lock()
@@ -302,7 +302,7 @@ impl GpuController {
Ok(())
}
pub async fn apply_config(&self, config: &GpuConfig) -> anyhow::Result<()> {
pub async fn apply_config(&self, config: &config::Gpu) -> anyhow::Result<()> {
if config.fan_control_enabled {
if let Some(ref settings) = config.fan_control_settings {
if settings.curve.0.is_empty() {

View File

@@ -1,5 +1,5 @@
use super::gpu_controller::{fan_control::FanCurve, GpuController};
use crate::config::{Config, FanControlSettings, GpuConfig};
use crate::config::{self, Config, FanControlSettings};
use anyhow::{anyhow, Context};
use lact_schema::{
ClocksInfo, DeviceInfo, DeviceListEntry, DeviceStats, FanCurveMap, PerformanceLevel,
@@ -69,7 +69,7 @@ impl<'a> Handler {
})
}
async fn edit_gpu_config<F: FnOnce(&mut GpuConfig)>(
async fn edit_gpu_config<F: FnOnce(&mut config::Gpu)>(
&self,
id: String,
f: F,
@@ -173,7 +173,7 @@ impl<'a> Handler {
self.edit_gpu_config(id.to_owned(), |config| {
config.fan_control_enabled = enabled;
config.fan_control_settings = settings
config.fan_control_settings = settings;
})
.await
}
@@ -198,7 +198,7 @@ impl<'a> Handler {
pub async fn cleanup(self) {
for (id, controller) in self.gpu_controllers.iter() {
if let Err(err) = controller.apply_config(&GpuConfig::default()).await {
if let Err(err) = controller.apply_config(&config::Gpu::default()).await {
error!("Could not reset settings for controller {id}: {err:#}");
}
}

View File

@@ -23,9 +23,9 @@ pub struct Server {
impl Server {
pub async fn new(config: Config) -> anyhow::Result<Self> {
let handler = Handler::new(config).await?;
let listener = socket::listen().await?;
let listener = socket::listen()?;
Ok(Self { listener, handler })
Ok(Self { handler, listener })
}
pub async fn run(self) {
@@ -35,7 +35,7 @@ impl Server {
let handler = self.handler.clone();
tokio::spawn(async move {
if let Err(error) = handle_stream(stream, handler).await {
error!("{error}")
error!("{error}");
}
});
}

View File

@@ -14,7 +14,7 @@ pub fn get_socket_path() -> PathBuf {
if uid.is_root() {
PathBuf::from_str("/var/run/lactd.sock").unwrap()
} else {
PathBuf::from_str(&format!("/var/run/user/{}/lactd.sock", uid)).unwrap()
PathBuf::from_str(&format!("/var/run/user/{uid}/lactd.sock")).unwrap()
}
}
@@ -22,12 +22,12 @@ pub fn cleanup() {
let socket_path = get_socket_path();
if socket_path.exists() {
fs::remove_file(socket_path).expect("failed to remove socket")
fs::remove_file(socket_path).expect("failed to remove socket");
}
debug!("removed socket");
}
pub async fn listen() -> anyhow::Result<UnixListener> {
pub fn listen() -> anyhow::Result<UnixListener> {
let socket_path = get_socket_path();
if socket_path.exists() {