From c482961c491ae477b0852630e4f6db5a68757eab Mon Sep 17 00:00:00 2001 From: Ilya Zlobintsev Date: Sat, 17 Oct 2020 07:38:28 +0300 Subject: [PATCH] Init --- .gitignore | 1 + .vscode/launch.json | 101 +++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + cli/Cargo.toml | 11 ++++ cli/src/main.rs | 15 ++++++ daemon/Cargo.toml | 11 ++++ daemon/src/daemon.rs | 67 +++++++++++++++++++++++ daemon/src/fan_controller.rs | 10 ++++ daemon/src/lib.rs | 24 +++++++++ daemon/src/main.rs | 9 ++++ 10 files changed, 251 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 Cargo.toml create mode 100644 cli/Cargo.toml create mode 100644 cli/src/main.rs create mode 100644 daemon/Cargo.toml create mode 100644 daemon/src/daemon.rs create mode 100644 daemon/src/fan_controller.rs create mode 100644 daemon/src/lib.rs create mode 100644 daemon/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..913063f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,101 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in library 'daemon'", + "cargo": { + "args": [ + "test", + "--no-run", + "--lib", + "--package=daemon" + ], + "filter": { + "name": "daemon", + "kind": "lib" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'daemon'", + "cargo": { + "args": [ + "build", + "--bin=daemon", + "--package=daemon" + ], + "filter": { + "name": "daemon", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'daemon'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=daemon", + "--package=daemon" + ], + "filter": { + "name": "daemon", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'cli'", + "cargo": { + "args": [ + "build", + "--bin=cli", + "--package=cli" + ], + "filter": { + "name": "cli", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'cli'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=cli", + "--package=cli" + ], + "filter": { + "name": "cli", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b47c287 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,2 @@ +[workspace] +members = ["daemon", "cli"] \ No newline at end of file diff --git a/cli/Cargo.toml b/cli/Cargo.toml new file mode 100644 index 0000000..ffea220 --- /dev/null +++ b/cli/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "cli" +version = "0.1.0" +authors = ["Ilya Zlobintsev "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +daemon = { path = "../daemon" } +structopt = "0.3" \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs new file mode 100644 index 0000000..560b29d --- /dev/null +++ b/cli/src/main.rs @@ -0,0 +1,15 @@ +use structopt::StructOpt; + +#[derive(StructOpt)] +#[structopt(rename_all = "lower")] +enum Opt { + ///Get information about the GPU. + Info, +} + +fn main() { + let opt = Opt::from_args(); + match opt { + Opt::Info => daemon::get_info(), + } +} diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml new file mode 100644 index 0000000..f7dd826 --- /dev/null +++ b/daemon/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "daemon" +version = "0.1.0" +authors = ["Ilya Zlobintsev "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bincode = "1.3" +serde = { version = "1.0", features = ["derive"] } \ No newline at end of file diff --git a/daemon/src/daemon.rs b/daemon/src/daemon.rs new file mode 100644 index 0000000..360f6bd --- /dev/null +++ b/daemon/src/daemon.rs @@ -0,0 +1,67 @@ +use std::os::unix::net::{UnixStream, UnixListener}; +use std::io::{Read, Write}; +use std::process::Command; +use std::thread; +use std::fs; +use serde::{Serialize, Deserialize}; + +use crate::SOCK_PATH; +use crate::fan_controller::FanController; + +#[derive(Clone)] +pub struct Daemon { + fan_controller: FanController, +} + +#[derive(Serialize, Deserialize, Debug)] +pub enum Action { + GetInfo, + GetStats, +} + +impl Daemon { + pub fn new(fan_controller: FanController) -> Daemon { + Daemon { + fan_controller, + } + } + + pub fn run(self) { + if fs::metadata(SOCK_PATH).is_ok() { + fs::remove_file(SOCK_PATH).expect("Failed to take control over socket"); + } + + let listener = UnixListener::bind(SOCK_PATH).unwrap(); + + Command::new("chmod").arg("666").arg(SOCK_PATH).output().expect("Failed to chmod"); + + for stream in listener.incoming() { + match stream { + Ok(stream) => { + thread::spawn(|| Daemon::handle_connection(stream)); + } + Err(err) => { + println!("Error: {}", err); + break; + } + } + } + } + + fn handle_connection(mut stream: UnixStream) { + let mut buffer = Vec::::new(); + stream.read_to_end(&mut buffer).unwrap(); + println!("finished reading"); + let action: Action = bincode::deserialize(&buffer).unwrap(); + + let response = match action { + Action::GetInfo => "gpu information", + Action::GetStats => { + "gpu stats" + }, + }; + stream.write_all(response.as_bytes()).unwrap(); + + } + +} diff --git a/daemon/src/fan_controller.rs b/daemon/src/fan_controller.rs new file mode 100644 index 0000000..0a8aa3c --- /dev/null +++ b/daemon/src/fan_controller.rs @@ -0,0 +1,10 @@ +#[derive(Clone)] +pub struct FanController { + hwmon_path: String, +} + +impl FanController { + pub fn new(hwmon_path: &str) -> FanController { + FanController { hwmon_path: hwmon_path.to_string() } + } +} \ No newline at end of file diff --git a/daemon/src/lib.rs b/daemon/src/lib.rs new file mode 100644 index 0000000..44739fd --- /dev/null +++ b/daemon/src/lib.rs @@ -0,0 +1,24 @@ +use std::os::unix::net::UnixStream; +use std::io::{Read, Write}; + +pub mod daemon; +pub mod fan_controller; + +pub const SOCK_PATH: &str = "/tmp/amdgpu-configurator.sock"; + +pub fn get_info() { + let mut stream = UnixStream::connect(SOCK_PATH).expect("Failed to connect to daemon"); + stream.write_all(&bincode::serialize(&daemon::Action::GetInfo).unwrap()).unwrap(); + stream.shutdown(std::net::Shutdown::Write).expect("Could not shut down"); + let mut response = String::new(); + stream.read_to_string(&mut response).unwrap(); + println!("{}", response); +} + +pub struct DaemonConnection { + +} + +impl DaemonConnection { + +} \ No newline at end of file diff --git a/daemon/src/main.rs b/daemon/src/main.rs new file mode 100644 index 0000000..7ad5cc3 --- /dev/null +++ b/daemon/src/main.rs @@ -0,0 +1,9 @@ +use daemon::daemon::Daemon; +use daemon::fan_controller::FanController; + +fn main() { + let fan_controller = FanController::new("afsadfasdfa"); + let d = Daemon::new(fan_controller); + d.run(); +} +