Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reload GPU controller list on kernel drm events #450

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lact-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ mod tests;
use anyhow::Context;
use config::Config;
use futures::future::select_all;
use server::system;
use server::{handle_stream, handler::Handler, Server};
use std::cell::Cell;
use std::sync::Arc;
use std::time::Instant;
use std::{os::unix::net::UnixStream as StdUnixStream, time::Duration};
use tokio::net::UnixStream;
use tokio::sync::Notify;
use tokio::time::timeout;
use tokio::{
runtime,
signal::unix::{signal, SignalKind},
Expand All @@ -32,6 +36,7 @@ pub const AMDGPU_FAMILY_GC_11_0_0: u32 = 145;
pub use server::system::MODULE_CONF_PATH;

const MIN_SYSTEM_UPTIME_SECS: f32 = 15.0;
const DRM_EVENT_TIMEOUT_PERIOD_MS: u64 = 100;
const SHUTDOWN_SIGNALS: [SignalKind; 4] = [
SignalKind::terminate(),
SignalKind::interrupt(),
Expand Down Expand Up @@ -72,6 +77,7 @@ pub fn run() -> anyhow::Result<()> {

tokio::task::spawn_local(listen_config_changes(handler.clone()));
tokio::task::spawn_local(listen_exit_signals(handler.clone()));
tokio::task::spawn_local(listen_device_events(handler.clone()));
tokio::task::spawn_local(suspend::listen_events(handler));

server.run().await;
Expand Down Expand Up @@ -124,7 +130,7 @@ async fn listen_config_changes(handler: Handler) {
let mut rx = config::start_watcher(handler.config_last_saved.clone());
while let Some(new_config) = rx.recv().await {
info!("config file was changed, reloading");
handler.config.replace(new_config);
*handler.config.write().await = new_config;
match handler.apply_current_config().await {
Ok(()) => {
info!("configuration reloaded");
Expand All @@ -136,6 +142,32 @@ async fn listen_config_changes(handler: Handler) {
}
}

async fn listen_device_events(handler: Handler) {
let notify = Arc::new(Notify::new());
let task_notify = notify.clone();
tokio::task::spawn_blocking(move || {
if let Err(err) = system::listen_netlink_kernel_event(&task_notify) {
error!("kernel event listener error: {err:#}");
}
});

loop {
notify.notified().await;

// Wait until the timeout has passed with no new events coming in
while timeout(
Duration::from_millis(DRM_EVENT_TIMEOUT_PERIOD_MS),
notify.notified(),
)
.await
.is_ok()
{}

info!("got kernel drm subsystem event, reloading GPUs");
handler.reload_gpus().await;
}
}

async fn ensure_sufficient_uptime() {
match get_uptime() {
Ok(current_uptime) => {
Expand Down
16 changes: 8 additions & 8 deletions lact-daemon/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ async fn handle_request<'a>(request: Request<'a>, handler: &'a Handler) -> anyho
match request {
Request::Ping => ok_response(ping()),
Request::SystemInfo => ok_response(system::info().await?),
Request::ListDevices => ok_response(handler.list_devices()),
Request::DeviceInfo { id } => ok_response(handler.get_device_info(id)?),
Request::DeviceStats { id } => ok_response(handler.get_gpu_stats(id)?),
Request::DeviceClocksInfo { id } => ok_response(handler.get_clocks_info(id)?),
Request::ListDevices => ok_response(handler.list_devices().await),
Request::DeviceInfo { id } => ok_response(handler.get_device_info(id).await?),
Request::DeviceStats { id } => ok_response(handler.get_gpu_stats(id).await?),
Request::DeviceClocksInfo { id } => ok_response(handler.get_clocks_info(id).await?),
Request::DevicePowerProfileModes { id } => {
ok_response(handler.get_power_profile_modes(id)?)
ok_response(handler.get_power_profile_modes(id).await?)
}
Request::SetFanControl(opts) => ok_response(handler.set_fan_control(opts).await?),
Request::ResetPmfw { id } => ok_response(handler.reset_pmfw(id).await?),
Expand All @@ -160,13 +160,13 @@ async fn handle_request<'a>(request: Request<'a>, handler: &'a Handler) -> anyho
.set_power_profile_mode(id, index, custom_heuristics)
.await?,
),
Request::GetPowerStates { id } => ok_response(handler.get_power_states(id)?),
Request::GetPowerStates { id } => ok_response(handler.get_power_states(id).await?),
Request::SetEnabledPowerStates { id, kind, states } => {
ok_response(handler.set_enabled_power_states(id, kind, states).await?)
}
Request::VbiosDump { id } => ok_response(handler.vbios_dump(id)?),
Request::VbiosDump { id } => ok_response(handler.vbios_dump(id).await?),
Request::ListProfiles { include_state } => {
ok_response(handler.list_profiles(include_state))
ok_response(handler.list_profiles(include_state).await)
}
Request::SetProfile { name, auto_switch } => ok_response(
handler
Expand Down
1 change: 1 addition & 0 deletions lact-daemon/src/server/gpu_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{cell::LazyCell, collections::HashMap, fs, path::PathBuf, rc::Rc};
use tokio::{sync::Notify, task::JoinHandle};
use tracing::{error, warn};

pub type DynGpuController = Box<dyn GpuController>;
type FanControlHandle = (Rc<Notify>, JoinHandle<()>);

pub trait GpuController {
Expand Down
Loading
Loading