Lots of changes

This commit is contained in:
Ilya Zlobintsev
2020-10-17 17:09:15 +03:00
parent c482961c49
commit 7f8d4e1372
6 changed files with 169 additions and 30 deletions

View File

@@ -5,12 +5,12 @@ use std::thread;
use std::fs;
use serde::{Serialize, Deserialize};
use crate::gpu_controller::GpuController;
use crate::SOCK_PATH;
use crate::fan_controller::FanController;
#[derive(Clone)]
pub struct Daemon {
fan_controller: FanController,
gpu_controller: GpuController,
}
#[derive(Serialize, Deserialize, Debug)]
@@ -20,9 +20,9 @@ pub enum Action {
}
impl Daemon {
pub fn new(fan_controller: FanController) -> Daemon {
pub fn new(gpu_controller: GpuController) -> Daemon {
Daemon {
fan_controller,
gpu_controller,
}
}
@@ -36,9 +36,10 @@ impl Daemon {
Command::new("chmod").arg("666").arg(SOCK_PATH).output().expect("Failed to chmod");
for stream in listener.incoming() {
let d = self.clone();
match stream {
Ok(stream) => {
thread::spawn(|| Daemon::handle_connection(stream));
thread::spawn(move || Daemon::handle_connection(d, stream));
}
Err(err) => {
println!("Error: {}", err);
@@ -48,19 +49,17 @@ impl Daemon {
}
}
fn handle_connection(mut stream: UnixStream) {
fn handle_connection(self, mut stream: UnixStream) {
let mut buffer = Vec::<u8>::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"
},
let response: Vec<u8> = match action {
Action::GetStats => bincode::serialize(&self.gpu_controller.get_stats()).unwrap(),
Action::GetInfo => bincode::serialize(&self.gpu_controller.get_info()).unwrap(),
};
stream.write_all(response.as_bytes()).unwrap();
stream.write_all(&response).expect("Failed writing response");
}

View File

@@ -1,4 +1,6 @@
#[derive(Clone)]
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FanController {
hwmon_path: String,
}

View File

@@ -0,0 +1,119 @@
use crate::fan_controller::FanController;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
#[derive(Serialize, Deserialize, Debug)]
pub struct GpuStats {
pub mem_used: u64,
pub mem_total: u64,
}
#[derive(Clone)]
pub struct GpuController {
hw_path: PathBuf,
fan_controller: FanController,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GpuInfo {
pub gpu_vendor: String,
pub gpu_model: String,
pub card_model: String,
pub card_vendor: String,
pub driver: String,
pub vbios_version: String,
}
impl GpuController {
pub fn new(hw_path: &str) -> GpuController {
GpuController {
hw_path: PathBuf::from(hw_path),
fan_controller: FanController::new(hw_path),
}
}
pub fn get_info(self) -> GpuInfo {
let uevent =
fs::read_to_string(self.hw_path.join("uevent")).expect("Failed to read uevent");
//caps for raw values, lowercase for parsed
let mut driver = String::new();
let mut VENDOR_ID = String::new();
let mut MODEL_ID = String::new();
let mut CARD_VENDOR_ID= String::new();
let mut CARD_MODEL_ID = String::new();
for line in uevent.split('\n') {
let split = line.split('=').collect::<Vec<&str>>();
match split[0] {
"DRIVER" => driver = split[1].to_string(),
"PCI_ID" => {
let ids = split[1].split(':').collect::<Vec<&str>>();
VENDOR_ID = ids[0].to_string();
MODEL_ID = ids[1].to_string();
},
"PCI_SUBSYS_ID" => {
let ids = split[1].split(':').collect::<Vec<&str>>();
CARD_VENDOR_ID = ids[0].to_string();
CARD_MODEL_ID = ids[1].to_string();
},
_ => (),
}
}
let vendor = "AMD".to_string();
let mut model = String::new();
let mut card_vendor = String::new();
let mut card_model = String::new();
let full_hwid_list = fs::read_to_string("/usr/share/hwdata/pci.ids").expect("Could not read pci.ids. Perhaps the \"hwids\" package is not installed?");
//some weird space character, don't touch
let pci_id_line = format!(" {}", MODEL_ID.to_lowercase());
let card_ids_line = format!(" {} {}", CARD_VENDOR_ID.to_lowercase(), CARD_MODEL_ID.to_lowercase());
println!("looking for {}", pci_id_line);
for line in full_hwid_list.split('\n') {
if line.contains(&pci_id_line) {
model = line[pci_id_line.len()..].trim_start().to_string();
}
if line.contains(&card_ids_line) {
card_model = line[card_ids_line.len()..].trim_start().to_string();
}
}
let vbios_version = fs::read_to_string(self.hw_path.join("vbios_version"))
.expect("Failed to read vbios_info");
GpuInfo {
gpu_vendor: vendor,
gpu_model: model,
card_vendor: String::new(),
card_model,
driver,
vbios_version,
}
}
pub fn get_stats(self) -> GpuStats {
let mem_total = fs::read_to_string(self.hw_path.join("mem_info_vram_total"))
.expect("Could not read device file")
.trim()
.parse::<u64>()
.unwrap()
/ 1024
/ 1024;
let mem_used = fs::read_to_string(self.hw_path.join("mem_info_vram_used"))
.expect("Could not read device file")
.trim()
.parse::<u64>()
.unwrap()
/ 1024
/ 1024;
GpuStats {
mem_total,
mem_used,
}
}
}

View File

@@ -1,24 +1,32 @@
use std::os::unix::net::UnixStream;
use std::io::{Read, Write};
use gpu_controller::{GpuInfo, GpuStats};
pub mod daemon;
pub mod fan_controller;
pub mod gpu_controller;
pub const SOCK_PATH: &str = "/tmp/amdgpu-configurator.sock";
pub fn get_info() {
pub fn get_gpu_stats() -> GpuStats{
let mut stream = UnixStream::connect(SOCK_PATH).expect("Failed to connect to daemon");
stream.write_all(&bincode::serialize(&daemon::Action::GetStats).unwrap()).unwrap();
stream.shutdown(std::net::Shutdown::Write).expect("Could not shut down");
let mut buffer = Vec::<u8>::new();
stream.read_to_end(&mut buffer).unwrap();
bincode::deserialize(&buffer).unwrap()
}
pub fn get_gpu_info() -> GpuInfo {
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);
let mut buffer = Vec::<u8>::new();
stream.read_to_end(&mut buffer).unwrap();
bincode::deserialize(&buffer).unwrap()
}
pub struct DaemonConnection {
}
impl DaemonConnection {
}

View File

@@ -1,9 +1,9 @@
use daemon::daemon::Daemon;
use daemon::fan_controller::FanController;
use daemon::gpu_controller::GpuController;
fn main() {
let fan_controller = FanController::new("afsadfasdfa");
let d = Daemon::new(fan_controller);
let gpu_controller = GpuController::new("/sys/class/drm/card0/device");
let d = Daemon::new(gpu_controller);
d.run();
}