Skip to content

Commit

Permalink
feat: reload configuration on dbus suspend/resume events
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-zlobintsev committed Mar 5, 2023
1 parent 072556c commit b702d29
Show file tree
Hide file tree
Showing 11 changed files with 629 additions and 101 deletions.
653 changes: 568 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lact-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lact-cli"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion lact-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lact-client"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

[dependencies]
Expand Down
3 changes: 2 additions & 1 deletion lact-daemon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lact-daemon"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down Expand Up @@ -33,3 +33,4 @@ futures = { version = "0.3.26", default-features = false, features = [
serde_with = { version = "2.2.0", default-features = false, features = [
"macros",
] }
zbus = "3.10.0"
6 changes: 4 additions & 2 deletions lact-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod config;
mod fork;
mod server;
mod socket;
mod suspend;

use anyhow::Context;
use config::Config;
Expand Down Expand Up @@ -44,7 +45,8 @@ pub fn run() -> anyhow::Result<()> {
let server = Server::new(config).await?;
let handler = server.handler.clone();

tokio::spawn(listen_shutdown(handler));
tokio::spawn(listen_exit_signals(handler.clone()));
tokio::spawn(suspend::listen_events(handler));
server.run().await;
Ok(())
})
Expand All @@ -69,7 +71,7 @@ pub fn run_embedded(stream: StdUnixStream) -> anyhow::Result<()> {
})
}

async fn listen_shutdown(handler: Handler) {
async fn listen_exit_signals(handler: Handler) {
let mut signals = SHUTDOWN_SIGNALS
.map(|signal_kind| signal(signal_kind).expect("Could not listen to shutdown signal"));
let signal_futures = signals.iter_mut().map(|signal| Box::pin(signal.recv()));
Expand Down
19 changes: 13 additions & 6 deletions lact-daemon/src/server/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,27 @@ impl<'a> Handler {
}
}

let handler = Self {
gpu_controllers: Arc::new(controllers),
config: Arc::new(RwLock::new(config)),
};
handler.load_config().await;

Ok(handler)
}

pub async fn load_config(&self) {
let config = self.config.read().expect("Faied to lock config").clone(); // Clone to avoid locking the RwLock on an await point

for (id, gpu_config) in &config.gpus {
if let Some(controller) = controllers.get(id) {
if let Some(controller) = self.gpu_controllers.get(id) {
if let Err(err) = controller.apply_config(gpu_config).await {
error!("could not apply existing config for gpu {id}: {err}");
}
} else {
info!("could not find GPU with id {id} defined in configuration");
}
}

Ok(Self {
gpu_controllers: Arc::new(controllers),
config: Arc::new(RwLock::new(config)),
})
}

async fn edit_gpu_config<F: FnOnce(&mut config::Gpu)>(
Expand Down
35 changes: 35 additions & 0 deletions lact-daemon/src/suspend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::server::handler::Handler;
use futures::StreamExt;
use tracing::{error, info};
use zbus::{Connection, Proxy};

pub async fn listen_events(handler: Handler) {
match connect_proxy().await {
// Note: despite the name, the events get triggered both on suspend and resume
Ok(proxy) => match proxy.receive_signal("PrepareForSleep").await {
Ok(mut stream) => {
while stream.next().await.is_some() {
info!("suspend/resume event detected, reloading config");
handler.load_config().await;
}
}
Err(err) => error!("could not subscribe to suspend events: {err:#}"),
},
Err(err) => {
error!("could not connect to dbus proxy: {err:#}");
}
}
error!("suspend/resume events will not be handled.");
}

async fn connect_proxy() -> anyhow::Result<Proxy<'static>> {
let conn = Connection::system().await?;
let proxy = Proxy::new_owned(
conn,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
)
.await?;
Ok(proxy)
}
2 changes: 1 addition & 1 deletion lact-gui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lact-gui"
version = "0.3.1"
version = "0.3.2"
authors = ["Ilya Zlobintsev <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion lact-schema/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lact-schema"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion lact/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lact"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

[features]
Expand Down
4 changes: 2 additions & 2 deletions pkg/recipes/lact/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ metadata:
description: AMDGPU control utility
arch: x86_64
license: MIT
version: 0.3.1
version: 0.3.2
maintainer: ilya-zlobintsev
url: https://github.com/ilya-zlobintsev/lact
source:
Expand All @@ -14,7 +14,7 @@ metadata:
debian-12+ubuntu-2204: [ libgtk-4-1 ]
fedora-37: [ gtk4 ]
build_depends:
all: [ curl, make ]
all: [ curl, make, dbus ]
debian-12+ubuntu-2204: [ libgtk-4-dev, pkg-config, build-essential ]
fedora-37: [ gtk4-devel, gcc ]
all_images: true
Expand Down

0 comments on commit b702d29

Please sign in to comment.